~ctf/unity-settings-daemon/bug1389099_mic_volume_icons

« back to all changes in this revision

Viewing changes to plugins/common/gsd-input-helper.c

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