~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/wacom/gsd-input-helper.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2010 Bastien Nocera <hadess@hadess.net>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <gdk/gdk.h>
 
24
#include <gdk/gdkx.h>
 
25
 
 
26
#include <sys/types.h>
 
27
#include <X11/Xatom.h>
 
28
#include <X11/extensions/XInput2.h>
 
29
 
 
30
#include "gsd-input-helper.h"
 
31
 
 
32
#define INPUT_DEVICES_SCHEMA "org.gnome.settings-daemon.peripherals.input-devices"
 
33
#define KEY_HOTPLUG_COMMAND  "hotplug-command"
 
34
 
 
35
typedef gboolean (* InfoIdentifyFunc) (XDeviceInfo *device_info);
 
36
typedef gboolean (* DeviceIdentifyFunc) (XDevice *xdevice);
 
37
 
 
38
gboolean
 
39
device_set_property (XDevice        *xdevice,
 
40
                     const char     *device_name,
 
41
                     PropertyHelper *property)
 
42
{
 
43
        int rc, i;
 
44
        Atom prop;
 
45
        Atom realtype;
 
46
        int realformat;
 
47
        unsigned long nitems, bytes_after;
 
48
        unsigned char *data;
 
49
 
 
50
        prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
 
51
                            property->name, False);
 
52
        if (!prop)
 
53
                return FALSE;
 
54
 
 
55
        gdk_error_trap_push ();
 
56
 
 
57
        rc = XGetDeviceProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
 
58
                                 xdevice, prop, 0, property->nitems, False,
 
59
                                 AnyPropertyType, &realtype, &realformat, &nitems,
 
60
                                 &bytes_after, &data);
 
61
 
 
62
        if (rc != Success ||
 
63
            realtype != property->type ||
 
64
            realformat != property->format ||
 
65
            nitems < property->nitems) {
 
66
                gdk_error_trap_pop_ignored ();
 
67
                g_warning ("Error reading property \"%s\" for \"%s\"", property->name, device_name);
 
68
                return FALSE;
 
69
        }
 
70
 
 
71
        for (i = 0; i < nitems; i++) {
 
72
                switch (property->format) {
 
73
                        case 8:
 
74
                                data[i] = property->data.c[i];
 
75
                                break;
 
76
                        case 32:
 
77
                                ((long*)data)[i] = property->data.i[i];
 
78
                                break;
 
79
                }
 
80
        }
 
81
 
 
82
        XChangeDeviceProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
 
83
                               xdevice, prop, realtype, realformat,
 
84
                               PropModeReplace, data, nitems);
 
85
 
 
86
        XFree (data);
 
87
 
 
88
        if (gdk_error_trap_pop ()) {
 
89
                g_warning ("Error in setting \"%s\" for \"%s\"", property->name, device_name);
 
90
                return FALSE;
 
91
        }
 
92
 
 
93
        return TRUE;
 
94
}
 
95
 
 
96
static gboolean
 
97
supports_xinput_devices_with_opcode (int *opcode)
 
98
{
 
99
        gint op_code, event, error;
 
100
        gboolean retval;
 
101
 
 
102
        retval = XQueryExtension (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
 
103
                                  "XInputExtension",
 
104
                                  &op_code,
 
105
                                  &event,
 
106
                                  &error);
 
107
        if (opcode)
 
108
                *opcode = op_code;
 
109
 
 
110
        return retval;
 
111
}
 
112
 
 
113
gboolean
 
114
supports_xinput_devices (void)
 
115
{
 
116
        return supports_xinput_devices_with_opcode (NULL);
 
117
}
 
118
 
 
119
gboolean
 
120
supports_xinput2_devices (int *opcode)
 
121
{
 
122
        int major, minor;
 
123
 
 
124
        if (supports_xinput_devices_with_opcode (opcode) == FALSE)
 
125
                return FALSE;
 
126
 
 
127
        gdk_error_trap_push ();
 
128
 
 
129
        major = 2;
 
130
        minor = 0;
 
131
 
 
132
        if (XIQueryVersion (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), &major, &minor) != Success) {
 
133
                gdk_error_trap_pop_ignored ();
 
134
                /* try for 2.2, maybe gtk has already announced 2.2 support */
 
135
                gdk_error_trap_push ();
 
136
                major = 2;
 
137
                minor = 2;
 
138
                if (XIQueryVersion (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), &major, &minor) != Success) {
 
139
                    gdk_error_trap_pop_ignored ();
 
140
                    return FALSE;
 
141
                }
 
142
        }
 
143
        gdk_error_trap_pop_ignored ();
 
144
 
 
145
        if ((major * 1000 + minor) < (2000))
 
146
                return FALSE;
 
147
 
 
148
        return TRUE;
 
149
}
 
150
 
 
151
gboolean
 
152
device_is_touchpad (XDevice *xdevice)
 
153
{
 
154
        Atom realtype, prop;
 
155
        int realformat;
 
156
        unsigned long nitems, bytes_after;
 
157
        unsigned char *data;
 
158
 
 
159
        /* we don't check on the type being XI_TOUCHPAD here,
 
160
         * but having a "Synaptics Off" property should be enough */
 
161
 
 
162
        prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "Synaptics Off", False);
 
163
        if (!prop)
 
164
                return FALSE;
 
165
 
 
166
        gdk_error_trap_push ();
 
167
        if ((XGetDeviceProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice, prop, 0, 1, False,
 
168
                                XA_INTEGER, &realtype, &realformat, &nitems,
 
169
                                &bytes_after, &data) == Success) && (realtype != None)) {
 
170
                gdk_error_trap_pop_ignored ();
 
171
                XFree (data);
 
172
                return TRUE;
 
173
        }
 
174
        gdk_error_trap_pop_ignored ();
 
175
 
 
176
        return FALSE;
 
177
}
 
178
 
 
179
gboolean
 
180
device_info_is_touchpad (XDeviceInfo *device_info)
 
181
{
 
182
        return (device_info->type == XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), XI_TOUCHPAD, False));
 
183
}
 
184
 
 
185
gboolean
 
186
device_info_is_touchscreen (XDeviceInfo *device_info)
 
187
{
 
188
        return (device_info->type == XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), XI_TOUCHSCREEN, False));
 
189
}
 
190
 
 
191
gboolean
 
192
device_info_is_mouse (XDeviceInfo *device_info)
 
193
{
 
194
        return (device_info->type == XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), XI_MOUSE, False));
 
195
}
 
196
 
 
197
static gboolean
 
198
device_type_is_present (InfoIdentifyFunc info_func,
 
199
                        DeviceIdentifyFunc device_func)
 
200
{
 
201
        XDeviceInfo *device_info;
 
202
        gint n_devices;
 
203
        guint i;
 
204
        gboolean retval;
 
205
 
 
206
        if (supports_xinput_devices () == FALSE)
 
207
                return TRUE;
 
208
 
 
209
        retval = FALSE;
 
210
 
 
211
        device_info = XListInputDevices (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), &n_devices);
 
212
        if (device_info == NULL)
 
213
                return FALSE;
 
214
 
 
215
        for (i = 0; i < n_devices; i++) {
 
216
                XDevice *device;
 
217
 
 
218
                /* Check with the device info first */
 
219
                retval = (info_func) (&device_info[i]);
 
220
                if (retval == FALSE)
 
221
                        continue;
 
222
 
 
223
                /* If we only have an info func, we're done checking */
 
224
                if (device_func == NULL)
 
225
                        break;
 
226
 
 
227
                gdk_error_trap_push ();
 
228
                device = XOpenDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), device_info[i].id);
 
229
                if (gdk_error_trap_pop () || (device == NULL))
 
230
                        continue;
 
231
 
 
232
                retval = (device_func) (device);
 
233
                if (retval) {
 
234
                        XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), device);
 
235
                        break;
 
236
                }
 
237
 
 
238
                XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), device);
 
239
        }
 
240
        XFreeDeviceList (device_info);
 
241
 
 
242
        return retval;
 
243
}
 
244
 
 
245
gboolean
 
246
touchscreen_is_present (void)
 
247
{
 
248
        return device_type_is_present (device_info_is_touchscreen,
 
249
                                       NULL);
 
250
}
 
251
 
 
252
gboolean
 
253
touchpad_is_present (void)
 
254
{
 
255
        return device_type_is_present (device_info_is_touchpad,
 
256
                                       device_is_touchpad);
 
257
}
 
258
 
 
259
gboolean
 
260
mouse_is_present (void)
 
261
{
 
262
        return device_type_is_present (device_info_is_mouse,
 
263
                                       NULL);
 
264
}
 
265
 
 
266
char *
 
267
xdevice_get_device_node (int deviceid)
 
268
{
 
269
        Atom           prop;
 
270
        Atom           act_type;
 
271
        int            act_format;
 
272
        unsigned long  nitems, bytes_after;
 
273
        unsigned char *data;
 
274
        char          *ret;
 
275
 
 
276
        gdk_display_sync (gdk_display_get_default ());
 
277
 
 
278
        prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "Device Node", False);
 
279
        if (!prop)
 
280
                return NULL;
 
281
 
 
282
        gdk_error_trap_push ();
 
283
 
 
284
        if (!XIGetProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
 
285
                            deviceid, prop, 0, 1000, False,
 
286
                            AnyPropertyType, &act_type, &act_format,
 
287
                            &nitems, &bytes_after, &data) == Success) {
 
288
                gdk_error_trap_pop_ignored ();
 
289
                return NULL;
 
290
        }
 
291
        if (gdk_error_trap_pop ())
 
292
                goto out;
 
293
 
 
294
        if (nitems == 0)
 
295
                goto out;
 
296
 
 
297
        if (act_type != XA_STRING)
 
298
                goto out;
 
299
 
 
300
        /* Unknown string format */
 
301
        if (act_format != 8)
 
302
                goto out;
 
303
 
 
304
        ret = g_strdup ((char *) data);
 
305
 
 
306
        XFree (data);
 
307
        return ret;
 
308
 
 
309
out:
 
310
        XFree (data);
 
311
        return NULL;
 
312
}
 
313
 
 
314
#define TOOL_ID_FORMAT_SIZE 32
 
315
static int
 
316
get_id_for_index (guchar *data,
 
317
                  guint   idx)
 
318
{
 
319
        guchar *ptr;
 
320
        int id;
 
321
 
 
322
        ptr = data;
 
323
        ptr += TOOL_ID_FORMAT_SIZE / 8 * idx;
 
324
 
 
325
        id = *((int32_t*)ptr);
 
326
        id = id & 0xfffff;
 
327
 
 
328
        return id;
 
329
}
 
330
 
 
331
 
 
332
#define STYLUS_DEVICE_ID        0x02
 
333
#define ERASER_DEVICE_ID        0x0A
 
334
 
 
335
int
 
336
xdevice_get_last_tool_id (int  deviceid)
 
337
{
 
338
        Atom           prop;
 
339
        Atom           act_type;
 
340
        int            act_format;
 
341
        unsigned long  nitems, bytes_after;
 
342
        unsigned char *data;
 
343
        int            id;
 
344
 
 
345
        id = -1;
 
346
 
 
347
        gdk_display_sync (gdk_display_get_default ());
 
348
 
 
349
        prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), WACOM_SERIAL_IDS_PROP, False);
 
350
        if (!prop)
 
351
                return -1;
 
352
 
 
353
        data = NULL;
 
354
 
 
355
        gdk_error_trap_push ();
 
356
 
 
357
        if (XIGetProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
 
358
                            deviceid, prop, 0, 1000, False,
 
359
                            AnyPropertyType, &act_type, &act_format,
 
360
                            &nitems, &bytes_after, &data) != Success) {
 
361
                gdk_error_trap_pop_ignored ();
 
362
                goto out;
 
363
        }
 
364
 
 
365
        if (gdk_error_trap_pop ())
 
366
                goto out;
 
367
 
 
368
        if (nitems != 4 && nitems != 5)
 
369
                goto out;
 
370
 
 
371
        if (act_type != XA_INTEGER)
 
372
                goto out;
 
373
 
 
374
        if (act_format != TOOL_ID_FORMAT_SIZE)
 
375
                goto out;
 
376
 
 
377
        /* item 0 = tablet ID
 
378
         * item 1 = old device serial number (== last tool in proximity)
 
379
         * item 2 = old hardware serial number (including tool ID)
 
380
         * item 3 = current serial number (0 if no tool in proximity)
 
381
         * item 4 = current tool ID (since Feb 2012)
 
382
         *
 
383
         * Get the current tool ID first, if available, then the old one */
 
384
        id = 0x0;
 
385
        if (nitems == 5)
 
386
                id = get_id_for_index (data, 4);
 
387
        if (id == 0x0)
 
388
                id = get_id_for_index (data, 2);
 
389
 
 
390
        /* That means that no tool was set down yet */
 
391
        if (id == STYLUS_DEVICE_ID ||
 
392
            id == ERASER_DEVICE_ID)
 
393
                id = 0x0;
 
394
 
 
395
out:
 
396
        if (data != NULL)
 
397
                XFree (data);
 
398
        return id;
 
399
}
 
400
 
 
401
gboolean
 
402
set_device_enabled (int device_id,
 
403
                    gboolean enabled)
 
404
{
 
405
        Atom prop;
 
406
        guchar value;
 
407
 
 
408
        prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "Device Enabled", False);
 
409
        if (!prop)
 
410
                return FALSE;
 
411
 
 
412
        gdk_error_trap_push ();
 
413
 
 
414
        value = enabled ? 1 : 0;
 
415
        XIChangeProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
 
416
                          device_id, prop, XA_INTEGER, 8, PropModeReplace, &value, 1);
 
417
 
 
418
        if (gdk_error_trap_pop ())
 
419
                return FALSE;
 
420
 
 
421
        return TRUE;
 
422
}
 
423
 
 
424
static const char *
 
425
custom_command_to_string (CustomCommand command)
 
426
{
 
427
        switch (command) {
 
428
        case COMMAND_DEVICE_ADDED:
 
429
                return "added";
 
430
        case COMMAND_DEVICE_REMOVED:
 
431
                return "removed";
 
432
        case COMMAND_DEVICE_PRESENT:
 
433
                return "present";
 
434
        default:
 
435
                g_assert_not_reached ();
 
436
        }
 
437
}
 
438
 
 
439
/* Run a custom command on device presence events. Parameters passed into
 
440
 * the custom command are:
 
441
 * command -t [added|removed|present] -i <device ID> <device name>
 
442
 * Type 'added' and 'removed' signal 'device added' and 'device removed',
 
443
 * respectively. Type 'present' signals 'device present at
 
444
 * gnome-settings-daemon init'.
 
445
 *
 
446
 * The script is expected to run synchronously, and an exit value
 
447
 * of "1" means that no other settings will be applied to this
 
448
 * particular device.
 
449
 *
 
450
 * More options may be added in the future.
 
451
 *
 
452
 * This function returns TRUE if we should not apply any more settings
 
453
 * to the device.
 
454
 */
 
455
gboolean
 
456
run_custom_command (GdkDevice              *device,
 
457
                    CustomCommand           command)
 
458
{
 
459
        GSettings *settings;
 
460
        char *cmd;
 
461
        char *argv[7];
 
462
        int exit_status;
 
463
        gboolean rc;
 
464
        int id;
 
465
 
 
466
        settings = g_settings_new (INPUT_DEVICES_SCHEMA);
 
467
        cmd = g_settings_get_string (settings, KEY_HOTPLUG_COMMAND);
 
468
        g_object_unref (settings);
 
469
 
 
470
        if (!cmd || cmd[0] == '\0') {
 
471
                g_free (cmd);
 
472
                return FALSE;
 
473
        }
 
474
 
 
475
        /* Easter egg! */
 
476
        g_object_get (device, "device-id", &id, NULL);
 
477
 
 
478
        argv[0] = cmd;
 
479
        argv[1] = "-t";
 
480
        argv[2] = (char *) custom_command_to_string (command);
 
481
        argv[3] = "-i";
 
482
        argv[4] = g_strdup_printf ("%d", id);
 
483
        argv[5] = g_strdup_printf ("%s", gdk_device_get_name (device));
 
484
        argv[6] = NULL;
 
485
 
 
486
        rc = g_spawn_sync (g_get_home_dir (), argv, NULL, G_SPAWN_SEARCH_PATH,
 
487
                           NULL, NULL, NULL, NULL, &exit_status, NULL);
 
488
 
 
489
        if (rc == FALSE)
 
490
                g_warning ("Couldn't execute command '%s', verify that this is a valid command.", cmd);
 
491
 
 
492
        g_free (argv[0]);
 
493
        g_free (argv[4]);
 
494
        g_free (argv[5]);
 
495
 
 
496
        return (exit_status == 0);
 
497
}
 
498
 
 
499
GList *
 
500
get_disabled_devices (GdkDeviceManager *manager)
 
501
{
 
502
        XDeviceInfo *device_info;
 
503
        gint n_devices;
 
504
        guint i;
 
505
        GList *ret;
 
506
 
 
507
        ret = NULL;
 
508
 
 
509
        device_info = XListInputDevices (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), &n_devices);
 
510
        if (device_info == NULL)
 
511
                return ret;
 
512
 
 
513
        for (i = 0; i < n_devices; i++) {
 
514
                GdkDevice *device;
 
515
 
 
516
                /* Ignore core devices */
 
517
                if (device_info[i].use == IsXKeyboard ||
 
518
                    device_info[i].use == IsXPointer)
 
519
                        continue;
 
520
 
 
521
                /* Check whether the device is actually available */
 
522
                device = gdk_x11_device_manager_lookup (manager, device_info[i].id);
 
523
                if (device != NULL)
 
524
                        continue;
 
525
 
 
526
                ret = g_list_prepend (ret, GINT_TO_POINTER (device_info[i].id));
 
527
        }
 
528
 
 
529
        XFreeDeviceList (device_info);
 
530
 
 
531
        return ret;
 
532
}