1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2010 Bastien Nocera <hadess@hadess.net>
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.
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.
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.
28
#include <sys/types.h>
29
#include <X11/Xatom.h>
30
#include <X11/extensions/XInput2.h>
32
#include "gsd-input-helper.h"
34
#define INPUT_DEVICES_SCHEMA "org.gnome.settings-daemon.peripherals.input-devices"
35
#define KEY_HOTPLUG_COMMAND "hotplug-command"
37
typedef gboolean (* InfoIdentifyFunc) (XDeviceInfo *device_info);
38
typedef gboolean (* DeviceIdentifyFunc) (XDevice *xdevice);
41
device_set_property (XDevice *xdevice,
42
const char *device_name,
43
PropertyHelper *property)
49
unsigned long nitems, bytes_after;
52
prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
53
property->name, False);
57
gdk_error_trap_push ();
59
rc = XGetDeviceProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
60
xdevice, prop, 0, property->nitems, False,
61
AnyPropertyType, &realtype, &realformat, &nitems,
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);
73
for (i = 0; i < nitems; i++) {
74
switch (property->format) {
76
data[i] = property->data.c[i];
79
((long*)data)[i] = property->data.i[i];
84
XChangeDeviceProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
85
xdevice, prop, realtype, realformat,
86
PropModeReplace, data, nitems);
90
if (gdk_error_trap_pop ()) {
91
g_warning ("Error in setting \"%s\" for \"%s\"", property->name, device_name);
99
supports_xinput_devices_with_opcode (int *opcode)
101
gint op_code, event, error;
104
retval = XQueryExtension (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
116
supports_xinput_devices (void)
118
return supports_xinput_devices_with_opcode (NULL);
122
supports_xtest (void)
124
gint op_code, event, error;
127
retval = XQueryExtension (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
137
supports_xinput2_devices (int *opcode)
141
if (supports_xinput_devices_with_opcode (opcode) == FALSE)
144
gdk_error_trap_push ();
149
if (XIQueryVersion (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), &major, &minor) != Success) {
150
gdk_error_trap_pop_ignored ();
153
gdk_error_trap_pop_ignored ();
155
if ((major * 1000 + minor) < (2000))
162
device_is_touchpad (XDevice *xdevice)
166
unsigned long nitems, bytes_after;
169
/* we don't check on the type being XI_TOUCHPAD here,
170
* but having a "Synaptics Off" property should be enough */
172
prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "Synaptics Off", False);
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 ();
184
gdk_error_trap_pop_ignored ();
190
device_info_is_touchpad (XDeviceInfo *device_info)
192
return (device_info->type == XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), XI_TOUCHPAD, False));
196
device_info_is_touchscreen (XDeviceInfo *device_info)
198
return (device_info->type == XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), XI_TOUCHSCREEN, False));
202
device_info_is_tablet (XDeviceInfo *device_info)
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));
209
device_info_is_mouse (XDeviceInfo *device_info)
211
return (device_info->type == XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), XI_MOUSE, False));
215
device_info_is_trackball (XDeviceInfo *device_info)
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) {
224
lowercase = g_ascii_strdown (device_info->name, -1);
225
retval = strstr (lowercase, "trackball") != NULL;
233
device_type_is_present (InfoIdentifyFunc info_func,
234
DeviceIdentifyFunc device_func)
236
XDeviceInfo *device_info;
241
if (supports_xinput_devices () == FALSE)
246
device_info = XListInputDevices (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), &n_devices);
247
if (device_info == NULL)
250
for (i = 0; i < n_devices; i++) {
253
/* Check with the device info first */
254
retval = (info_func) (&device_info[i]);
258
/* If we only have an info func, we're done checking */
259
if (device_func == NULL)
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))
267
retval = (device_func) (device);
269
XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), device);
273
XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), device);
275
XFreeDeviceList (device_info);
281
touchscreen_is_present (void)
283
return device_type_is_present (device_info_is_touchscreen,
288
touchpad_is_present (void)
290
return device_type_is_present (device_info_is_touchpad,
295
mouse_is_present (void)
297
return device_type_is_present (device_info_is_mouse,
302
trackball_is_present (void)
304
return device_type_is_present (device_info_is_trackball,
309
xdevice_get_device_node (int deviceid)
314
unsigned long nitems, bytes_after;
318
gdk_display_sync (gdk_display_get_default ());
320
prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "Device Node", False);
324
gdk_error_trap_push ();
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 ();
333
if (gdk_error_trap_pop ())
339
if (act_type != XA_STRING)
342
/* Unknown string format */
346
ret = g_strdup ((char *) data);
356
#define TOOL_ID_FORMAT_SIZE 32
358
get_id_for_index (guchar *data,
365
ptr += TOOL_ID_FORMAT_SIZE / 8 * idx;
367
id = *((int32_t*)ptr);
374
#define STYLUS_DEVICE_ID 0x02
375
#define ERASER_DEVICE_ID 0x0A
378
xdevice_get_last_tool_id (int deviceid)
383
unsigned long nitems, bytes_after;
389
gdk_display_sync (gdk_display_get_default ());
391
prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), WACOM_SERIAL_IDS_PROP, False);
397
gdk_error_trap_push ();
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 ();
407
if (gdk_error_trap_pop ())
410
if (nitems != 4 && nitems != 5)
413
if (act_type != XA_INTEGER)
416
if (act_format != TOOL_ID_FORMAT_SIZE)
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)
425
* Get the current tool ID first, if available, then the old one */
428
id = get_id_for_index (data, 4);
430
id = get_id_for_index (data, 2);
432
/* That means that no tool was set down yet */
433
if (id == STYLUS_DEVICE_ID ||
434
id == ERASER_DEVICE_ID)
444
set_device_enabled (int device_id,
450
prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "Device Enabled", False);
454
gdk_error_trap_push ();
456
value = enabled ? 1 : 0;
457
XIChangeProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
458
device_id, prop, XA_INTEGER, 8, PropModeReplace, &value, 1);
460
if (gdk_error_trap_pop ())
467
custom_command_to_string (CustomCommand command)
470
case COMMAND_DEVICE_ADDED:
472
case COMMAND_DEVICE_REMOVED:
474
case COMMAND_DEVICE_PRESENT:
477
g_assert_not_reached ();
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'.
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
492
* More options may be added in the future.
494
* This function returns TRUE if we should not apply any more settings
498
run_custom_command (GdkDevice *device,
499
CustomCommand command)
508
settings = g_settings_new (INPUT_DEVICES_SCHEMA);
509
cmd = g_settings_get_string (settings, KEY_HOTPLUG_COMMAND);
510
g_object_unref (settings);
512
if (!cmd || cmd[0] == '\0') {
518
g_object_get (device, "device-id", &id, NULL);
522
argv[2] = (char *) custom_command_to_string (command);
524
argv[4] = g_strdup_printf ("%d", id);
525
argv[5] = (char*) gdk_device_get_name (device);
528
rc = g_spawn_sync (g_get_home_dir (), argv, NULL, G_SPAWN_SEARCH_PATH,
529
NULL, NULL, NULL, NULL, &exit_status, NULL);
532
g_warning ("Couldn't execute command '%s', verify that this is a valid command.", cmd);
537
return (exit_status == 1);
541
get_disabled_devices (GdkDeviceManager *manager)
543
XDeviceInfo *device_info;
550
device_info = XListInputDevices (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), &n_devices);
551
if (device_info == NULL)
554
for (i = 0; i < n_devices; i++) {
557
/* Ignore core devices */
558
if (device_info[i].use == IsXKeyboard ||
559
device_info[i].use == IsXPointer)
562
/* Check whether the device is actually available */
563
device = gdk_x11_device_manager_lookup (manager, device_info[i].id);
567
ret = g_list_prepend (ret, GINT_TO_POINTER (device_info[i].id));
570
XFreeDeviceList (device_info);