2
* Copyright (C) 2011 Red Hat, Inc.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
* Author: Bastien Nocera <hadess@hadess.net>
25
#include <glib/gi18n-lib.h>
29
#include <X11/Xatom.h>
30
#define GNOME_DESKTOP_USE_UNSTABLE_API
31
#include <libgnome-desktop/gnome-rr.h>
32
#include <libgnome-desktop/gnome-rr-config.h>
34
#include <libwacom/libwacom.h>
35
#include <X11/extensions/XInput.h>
36
#include <X11/extensions/XInput2.h>
38
#include "gsd-input-helper.h"
40
#include "gsd-enums.h"
41
#include "gsd-wacom-device.h"
43
#define GSD_WACOM_STYLUS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_WACOM_STYLUS, GsdWacomStylusPrivate))
45
#define WACOM_TABLET_SCHEMA "org.gnome.settings-daemon.peripherals.wacom"
46
#define WACOM_DEVICE_CONFIG_BASE "/org/gnome/settings-daemon/peripherals/wacom/%s/"
47
#define WACOM_STYLUS_SCHEMA "org.gnome.settings-daemon.peripherals.wacom.stylus"
48
#define WACOM_ERASER_SCHEMA "org.gnome.settings-daemon.peripherals.wacom.eraser"
50
static WacomDeviceDatabase *db = NULL;
52
struct GsdWacomStylusPrivate
54
GsdWacomDevice *device;
58
const char *icon_name;
64
static void gsd_wacom_stylus_class_init (GsdWacomStylusClass *klass);
65
static void gsd_wacom_stylus_init (GsdWacomStylus *wacom_stylus);
66
static void gsd_wacom_stylus_finalize (GObject *object);
68
G_DEFINE_TYPE (GsdWacomStylus, gsd_wacom_stylus, G_TYPE_OBJECT)
71
gsd_wacom_stylus_class_init (GsdWacomStylusClass *klass)
73
GObjectClass *object_class = G_OBJECT_CLASS (klass);
75
object_class->finalize = gsd_wacom_stylus_finalize;
77
g_type_class_add_private (klass, sizeof (GsdWacomStylusPrivate));
81
gsd_wacom_stylus_init (GsdWacomStylus *stylus)
83
stylus->priv = GSD_WACOM_STYLUS_GET_PRIVATE (stylus);
87
gsd_wacom_stylus_finalize (GObject *object)
89
GsdWacomStylus *stylus;
90
GsdWacomStylusPrivate *p;
92
g_return_if_fail (object != NULL);
93
g_return_if_fail (GSD_IS_WACOM_STYLUS (object));
95
stylus = GSD_WACOM_STYLUS (object);
97
g_return_if_fail (stylus->priv != NULL);
101
if (p->settings != NULL) {
102
g_object_unref (p->settings);
109
G_OBJECT_CLASS (gsd_wacom_stylus_parent_class)->finalize (object);
113
get_icon_name_from_type (WacomStylusType type)
118
/* The stroke pen is the same as the inking pen with
120
return "wacom-stylus-inking";
121
case WSTYLUS_AIRBRUSH:
122
return "wacom-stylus-airbrush";
124
return "wacom-stylus-art-pen";
125
case WSTYLUS_CLASSIC:
126
return "wacom-stylus-classic";
128
return "wacom-stylus";
132
static GsdWacomStylus *
133
gsd_wacom_stylus_new (GsdWacomDevice *device,
134
const WacomStylus *wstylus,
137
GsdWacomStylus *stylus;
139
g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
140
g_return_val_if_fail (wstylus != NULL, NULL);
142
stylus = GSD_WACOM_STYLUS (g_object_new (GSD_TYPE_WACOM_STYLUS,
144
stylus->priv->device = device;
145
stylus->priv->id = libwacom_stylus_get_id (wstylus);
146
stylus->priv->name = g_strdup (libwacom_stylus_get_name (wstylus));
147
stylus->priv->settings = settings;
148
stylus->priv->type = libwacom_stylus_get_type (wstylus);
149
stylus->priv->icon_name = get_icon_name_from_type (stylus->priv->type);
150
stylus->priv->has_eraser = libwacom_stylus_has_eraser (wstylus);
151
stylus->priv->num_buttons = libwacom_stylus_get_num_buttons (wstylus);
157
gsd_wacom_stylus_get_settings (GsdWacomStylus *stylus)
159
g_return_val_if_fail (GSD_IS_WACOM_STYLUS (stylus), NULL);
161
return stylus->priv->settings;
165
gsd_wacom_stylus_get_name (GsdWacomStylus *stylus)
167
g_return_val_if_fail (GSD_IS_WACOM_STYLUS (stylus), NULL);
169
return stylus->priv->name;
173
gsd_wacom_stylus_get_icon_name (GsdWacomStylus *stylus)
175
g_return_val_if_fail (GSD_IS_WACOM_STYLUS (stylus), NULL);
177
return stylus->priv->icon_name;
181
gsd_wacom_stylus_get_device (GsdWacomStylus *stylus)
183
g_return_val_if_fail (GSD_IS_WACOM_STYLUS (stylus), NULL);
185
return stylus->priv->device;
189
gsd_wacom_stylus_get_has_eraser (GsdWacomStylus *stylus)
191
g_return_val_if_fail (GSD_IS_WACOM_STYLUS (stylus), FALSE);
193
return stylus->priv->has_eraser;
197
gsd_wacom_stylus_get_num_buttons (GsdWacomStylus *stylus)
199
g_return_val_if_fail (GSD_IS_WACOM_STYLUS (stylus), -1);
201
return stylus->priv->num_buttons;
205
gsd_wacom_stylus_get_stylus_type (GsdWacomStylus *stylus)
207
g_return_val_if_fail (GSD_IS_WACOM_STYLUS (stylus), WACOM_STYLUS_TYPE_UNKNOWN);
209
switch (stylus->priv->type) {
210
case WSTYLUS_UNKNOWN:
211
return WACOM_STYLUS_TYPE_UNKNOWN;
212
case WSTYLUS_GENERAL:
213
return WACOM_STYLUS_TYPE_GENERAL;
215
return WACOM_STYLUS_TYPE_INKING;
216
case WSTYLUS_AIRBRUSH:
217
return WACOM_STYLUS_TYPE_AIRBRUSH;
218
case WSTYLUS_CLASSIC:
219
return WACOM_STYLUS_TYPE_CLASSIC;
221
return WACOM_STYLUS_TYPE_MARKER;
223
return WACOM_STYLUS_TYPE_STROKE;
225
return WACOM_STYLUS_TYPE_PUCK;
227
g_assert_not_reached ();
230
return WACOM_STYLUS_TYPE_UNKNOWN;
234
gsd_wacom_stylus_get_id (GsdWacomStylus *stylus)
236
g_return_val_if_fail (GSD_IS_WACOM_STYLUS (stylus), -1);
238
return stylus->priv->id;
242
static GsdWacomTabletButton *
243
gsd_wacom_tablet_button_new (const char *name,
245
GsdWacomTabletButtonType type,
248
GsdWacomTabletButton *ret;
250
ret = g_new0 (GsdWacomTabletButton, 1);
251
ret->name = g_strdup (name);
252
ret->id = g_strdup (id);
253
ret->group_id = group_id;
260
gsd_wacom_tablet_button_free (GsdWacomTabletButton *button)
262
g_return_if_fail (button != NULL);
264
g_free (button->name);
269
GsdWacomTabletButton *
270
gsd_wacom_tablet_button_copy (GsdWacomTabletButton *button)
272
GsdWacomTabletButton *ret;
274
g_return_val_if_fail (button != NULL, NULL);
276
ret = g_new0 (GsdWacomTabletButton, 1);
277
ret->name = g_strdup (button->name);
278
ret->id = button->id;
279
ret->type = button->type;
280
ret->group_id = button->group_id;
285
#define GSD_WACOM_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_WACOM_DEVICE, GsdWacomDevicePrivate))
287
/* we support two types of settings:
288
* Tablet-wide settings: applied to each tool on the tablet. e.g. rotation
289
* Tool-specific settings: applied to one tool only.
291
#define SETTINGS_WACOM_DIR "org.gnome.settings-daemon.peripherals.wacom"
292
#define SETTINGS_STYLUS_DIR "stylus"
293
#define SETTINGS_ERASER_DIR "eraser"
295
struct GsdWacomDevicePrivate
297
GdkDevice *gdk_device;
301
GsdWacomDeviceType type;
306
gboolean is_screen_tablet;
308
GsdWacomStylus *last_stylus;
310
GSettings *wacom_settings;
319
static void gsd_wacom_device_class_init (GsdWacomDeviceClass *klass);
320
static void gsd_wacom_device_init (GsdWacomDevice *wacom_device);
321
static void gsd_wacom_device_finalize (GObject *object);
323
G_DEFINE_TYPE (GsdWacomDevice, gsd_wacom_device, G_TYPE_OBJECT)
325
static GdkFilterReturn
326
filter_events (XEvent *xevent,
328
GsdWacomDevice *device)
331
XIPropertyEvent *pev;
332
XGenericEventCookie *cookie;
336
/* verify we have a property event */
337
if (xevent->type != GenericEvent)
338
return GDK_FILTER_CONTINUE;
340
cookie = &xevent->xcookie;
341
if (cookie->extension != device->priv->opcode)
342
return GDK_FILTER_CONTINUE;
344
xiev = (XIEvent *) xevent->xcookie.data;
346
if (xiev->evtype != XI_PropertyEvent)
347
return GDK_FILTER_CONTINUE;
349
pev = (XIPropertyEvent *) xiev;
351
/* Is the event for us? */
352
if (pev->deviceid != device->priv->device_id)
353
return GDK_FILTER_CONTINUE;
355
name = XGetAtomName (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), pev->property);
357
g_strcmp0 (name, WACOM_SERIAL_IDS_PROP) != 0) {
358
return GDK_FILTER_CONTINUE;
362
tool_id = xdevice_get_last_tool_id (device->priv->device_id);
364
g_warning ("Failed to get value for changed stylus ID on device '%d", device->priv->device_id);
365
return GDK_FILTER_CONTINUE;
367
gsd_wacom_device_set_current_stylus (device, tool_id);
369
return GDK_FILTER_CONTINUE;
373
setup_property_notify (GsdWacomDevice *device)
377
unsigned char bitmask[2] = { 0 };
380
XISetMask (bitmask, XI_PropertyEvent);
382
evmask.deviceid = device->priv->device_id;
383
evmask.mask_len = sizeof (bitmask);
384
evmask.mask = bitmask;
386
dpy = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
387
XISelectEvents (dpy, DefaultRootWindow(dpy), &evmask, 1);
389
gdk_window_add_filter (NULL,
390
(GdkFilterFunc) filter_events,
393
tool_id = xdevice_get_last_tool_id (device->priv->device_id);
395
g_warning ("Failed to get value for changed stylus ID on device '%d", device->priv->device_id);
398
gsd_wacom_device_set_current_stylus (device, tool_id);
403
static GsdWacomDeviceType
404
get_device_type (XDeviceInfo *dev)
406
GsdWacomDeviceType ret;
407
static Atom stylus, cursor, eraser, pad, touch, prop;
411
unsigned long nitems, bytes_after;
412
unsigned char *data = NULL;
415
ret = WACOM_TYPE_INVALID;
417
if ((dev->use == IsXPointer) || (dev->use == IsXKeyboard))
421
stylus = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "STYLUS", False);
423
eraser = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "ERASER", False);
425
cursor = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "CURSOR", False);
427
pad = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "PAD", False);
429
touch = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "TOUCH", False);
431
prop = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "Wacom Tool Type", False);
433
if (dev->type == stylus)
434
ret = WACOM_TYPE_STYLUS;
435
else if (dev->type == eraser)
436
ret = WACOM_TYPE_ERASER;
437
else if (dev->type == cursor)
438
ret = WACOM_TYPE_CURSOR;
439
else if (dev->type == pad)
440
ret = WACOM_TYPE_PAD;
441
else if (dev->type == touch)
442
ret = WACOM_TYPE_TOUCH;
444
if (ret == WACOM_TYPE_INVALID)
447
/* There is currently no good way of detecting the driver for a device
448
* other than checking for a driver-specific property.
449
* Wacom Tool Type exists on all tools
451
gdk_error_trap_push ();
452
device = XOpenDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), dev->id);
453
if (gdk_error_trap_pop () || (device == NULL))
456
gdk_error_trap_push ();
458
rc = XGetDeviceProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
459
device, prop, 0, 1, False,
460
XA_ATOM, &realtype, &realformat, &nitems,
461
&bytes_after, &data);
462
if (gdk_error_trap_pop () || rc != Success || realtype == None) {
463
XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), device);
464
ret = WACOM_TYPE_INVALID;
472
/* Finds an output which matches the given EDID information. Any NULL
473
* parameter will be interpreted to match any value.
475
static GnomeRROutputInfo*
476
find_output_by_edid (const gchar *vendor, const gchar *product, const gchar *serial)
478
GError *error = NULL;
479
GnomeRRScreen *rr_screen;
480
GnomeRRConfig *rr_config;
481
GnomeRROutputInfo **rr_output_info;
482
GnomeRROutputInfo *retval = NULL;
484
/* TODO: Check the value of 'error' */
485
rr_screen = gnome_rr_screen_new (gdk_screen_get_default (), &error);
486
rr_config = gnome_rr_config_new_current (rr_screen, &error);
487
rr_output_info = gnome_rr_config_get_outputs (rr_config);
489
for (; *rr_output_info != NULL; rr_output_info++) {
495
o_vendor = g_malloc0 (4);
496
gnome_rr_output_info_get_vendor (*rr_output_info, o_vendor);
497
o_product = g_strdup_printf ("%d", gnome_rr_output_info_get_product (*rr_output_info));
498
o_serial = g_strdup_printf ("%d", gnome_rr_output_info_get_serial (*rr_output_info));
500
g_debug ("Checking for match between '%s','%s','%s' and '%s','%s','%s'", \
501
vendor,product,serial, o_vendor,o_product,o_serial);
503
match = (vendor == NULL || g_strcmp0 (vendor, o_vendor) == 0) && \
504
(product == NULL || g_strcmp0 (product, o_product) == 0) && \
505
(serial == NULL || g_strcmp0 (serial, o_serial) == 0);
512
retval = g_object_ref (*rr_output_info);
517
g_object_unref (rr_config);
522
static GnomeRROutputInfo*
523
find_output_by_heuristic (GsdWacomDevice *device)
525
GnomeRROutputInfo *rr_output_info;
527
/* TODO: This heuristic will fail for non-Wacom display
528
* tablets and may give the wrong result if multiple Wacom
529
* display tablets are connected.
531
rr_output_info = find_output_by_edid("WAC", NULL, NULL);
532
return rr_output_info;
535
static GnomeRROutputInfo*
536
find_output_by_display (GsdWacomDevice *device)
546
tablet = device->priv->wacom_settings;
547
display = g_settings_get_value (tablet, "display");
548
edid = g_variant_get_strv (display, &n);
551
g_critical ("Expected 'display' key to store %d values; got %"G_GSIZE_FORMAT".", 3, n);
555
if (strlen(edid[0]) == 0 || strlen(edid[1]) == 0 || strlen(edid[2]) == 0) {
556
g_warning ("EDID not completely defined.");
560
return find_output_by_edid (edid[0], edid[1], edid[2]);
564
set_display_by_output (GsdWacomDevice *device,
565
GnomeRROutputInfo *rr_output_info)
571
gchar *o_vendor, *o_product, *o_serial;
572
const gchar *values[3];
574
tablet = gsd_wacom_device_get_settings (device);
575
c_array = g_settings_get_value (tablet, "display");
576
g_variant_get_strv (c_array, &nvalues);
578
g_warning("Unable set set display property. Got %"G_GSIZE_FORMAT" items; expected %d items.\n", nvalues, 4);
582
if (rr_output_info == NULL)
584
o_vendor = g_strdup ("");
585
o_product = g_strdup ("");
586
o_serial = g_strdup ("");
590
o_vendor = g_malloc0 (4);
591
gnome_rr_output_info_get_vendor (rr_output_info, o_vendor);
592
o_product = g_strdup_printf ("%d", gnome_rr_output_info_get_product (rr_output_info));
593
o_serial = g_strdup_printf ("%d", gnome_rr_output_info_get_serial (rr_output_info));
596
values[0] = o_vendor;
597
values[1] = o_product;
598
values[2] = o_serial;
599
n_array = g_variant_new_strv((const gchar * const *) &values, 3);
600
g_settings_set_value (tablet, "display", n_array);
607
static GnomeRROutputInfo*
608
find_output (GsdWacomDevice *device)
610
GnomeRROutputInfo *rr_output_info;
612
rr_output_info = find_output_by_display(device);
614
if (rr_output_info == NULL)
616
g_warning ("No strict EDID match was found.");
618
if (gsd_wacom_device_is_screen_tablet (device))
620
rr_output_info = find_output_by_heuristic (device);
621
if (rr_output_info == NULL)
623
g_warning ("No fuzzy match based on heuristics was found.");
627
g_warning("Automatically mapping tablet to heuristically-found display.");
628
set_display_by_output (device, rr_output_info);
633
return rr_output_info;
637
calculate_transformation_matrix (const GdkRectangle mapped, const GdkRectangle desktop, float matrix[NUM_ELEMS_MATRIX])
639
float x_scale = (float)mapped.x / desktop.width;
640
float y_scale = (float)mapped.y / desktop.height;
641
float width_scale = (float)mapped.width / desktop.width;
642
float height_scale = (float)mapped.height / desktop.height;
644
matrix[0] = width_scale;
649
matrix[4] = height_scale;
656
g_debug ("Matrix is %f,%f,%f,%f,%f,%f,%f,%f,%f.",
657
matrix[0], matrix[1], matrix[2],
658
matrix[3], matrix[4], matrix[5],
659
matrix[6], matrix[7], matrix[8]);
665
gsd_wacom_device_get_display_monitor (GsdWacomDevice *device)
669
GnomeRROutputInfo *rr_output_info;
671
rr_output_info = find_output(device);
672
if (rr_output_info == NULL)
675
is_active = gnome_rr_output_info_is_active (rr_output_info);
676
gnome_rr_output_info_get_geometry (rr_output_info, &area[0], &area[1], &area[2], &area[3]);
678
g_object_unref (rr_output_info);
682
g_warning ("Output is not active.");
686
if (area[2] <= 0 || area[3] <= 0)
688
g_warning ("Output has non-positive area.");
692
g_debug ("Area: %d,%d %dx%d", area[0], area[1], area[2], area[3]);
693
return gdk_screen_get_monitor_at_point (gdk_screen_get_default (), area[0], area[1]);
697
gsd_wacom_device_get_display_matrix (GsdWacomDevice *device, float matrix[NUM_ELEMS_MATRIX])
700
GdkRectangle display;
701
GdkRectangle desktop;
702
GdkScreen *screen = gdk_screen_get_default ();
714
monitor = gsd_wacom_device_get_display_monitor (device);
720
desktop.width = gdk_screen_get_width (screen);
721
desktop.height = gdk_screen_get_height (screen);
723
gdk_screen_get_monitor_geometry (screen, monitor, &display);
724
calculate_transformation_matrix (display, desktop, matrix);
729
add_stylus_to_device (GsdWacomDevice *device,
730
const char *settings_path,
733
const WacomStylus *wstylus;
735
wstylus = libwacom_stylus_get_for_id (db, id);
737
GsdWacomStylus *stylus;
738
char *stylus_settings_path;
741
if (device->priv->type == WACOM_TYPE_STYLUS &&
742
libwacom_stylus_is_eraser (wstylus))
744
if (device->priv->type == WACOM_TYPE_ERASER &&
745
libwacom_stylus_is_eraser (wstylus) == FALSE)
748
stylus_settings_path = g_strdup_printf ("%s0x%x/", settings_path, id);
749
if (device->priv->type == WACOM_TYPE_STYLUS) {
750
settings = g_settings_new_with_path (WACOM_STYLUS_SCHEMA, stylus_settings_path);
751
stylus = gsd_wacom_stylus_new (device, wstylus, settings);
753
settings = g_settings_new_with_path (WACOM_ERASER_SCHEMA, stylus_settings_path);
754
stylus = gsd_wacom_stylus_new (device, wstylus, settings);
756
g_free (stylus_settings_path);
757
device->priv->styli = g_list_prepend (device->priv->styli, stylus);
762
flags_to_group (WacomButtonFlags flags)
764
if (flags & WACOM_BUTTON_RING_MODESWITCH)
766
if (flags & WACOM_BUTTON_RING2_MODESWITCH)
768
if (flags & WACOM_BUTTON_TOUCHSTRIP_MODESWITCH)
770
if (flags & WACOM_BUTTON_TOUCHSTRIP2_MODESWITCH)
777
gsd_wacom_device_add_ring_modes (WacomDevice *wacom_device,
778
WacomButtonFlags direction)
787
if ((direction & WACOM_BUTTON_POSITION_LEFT) && libwacom_has_ring (wacom_device)) {
788
num_modes = libwacom_get_ring_num_modes (wacom_device);
789
for (i = 1; i <= num_modes; i++) {
790
name = g_strdup_printf (_("Left Ring Mode #%d"), i);
791
id = g_strdup_printf ("left-ring-mode-%d", i);
792
l = g_list_append (l, gsd_wacom_tablet_button_new (name, id, WACOM_TABLET_BUTTON_TYPE_ELEVATOR, flags_to_group (WACOM_BUTTON_RING_MODESWITCH)));
794
} else if ((direction & WACOM_BUTTON_POSITION_RIGHT) && libwacom_has_ring2 (wacom_device)) {
795
num_modes = libwacom_get_ring2_num_modes (wacom_device);
796
for (i = 1; i <= num_modes; i++) {
797
name = g_strdup_printf (_("Right Ring Mode #%d"), i);
798
id = g_strdup_printf ("right-ring-mode-%d", i);
799
l = g_list_append (l, gsd_wacom_tablet_button_new (name, id, WACOM_TABLET_BUTTON_TYPE_ELEVATOR, flags_to_group (WACOM_BUTTON_RING2_MODESWITCH)));
807
gsd_wacom_device_add_strip_modes (WacomDevice *wacom_device,
808
WacomButtonFlags direction)
817
num_strips = libwacom_get_num_strips (wacom_device);
819
g_warning ("Unhandled number of touchstrips: %d", num_strips);
821
if ((direction & WACOM_BUTTON_POSITION_LEFT) && num_strips >= 1) {
822
num_modes = libwacom_get_strips_num_modes (wacom_device);
823
for (i = 1; i <= num_modes; i++) {
824
name = g_strdup_printf (_("Left Touchstrip Mode #%d"), i);
825
id = g_strdup_printf ("left-strip-mode-%d", i);
826
l = g_list_append (l, gsd_wacom_tablet_button_new (name, id, WACOM_TABLET_BUTTON_TYPE_ELEVATOR, flags_to_group (WACOM_BUTTON_TOUCHSTRIP_MODESWITCH)));
828
} else if ((direction & WACOM_BUTTON_POSITION_RIGHT) && num_strips >= 2) {
829
num_modes = libwacom_get_strips_num_modes (wacom_device);
830
for (i = 1; i <= num_modes; i++) {
831
name = g_strdup_printf (_("Right Touchstrip Mode #%d"), i);
832
id = g_strdup_printf ("right-strip-mode-%d", i);
833
l = g_list_append (l, gsd_wacom_tablet_button_new (name, id, WACOM_TABLET_BUTTON_TYPE_ELEVATOR, flags_to_group (WACOM_BUTTON_TOUCHSTRIP2_MODESWITCH)));
841
gsd_wacom_device_modeswitch_name (WacomButtonFlags flags,
844
if (flags & WACOM_BUTTON_RINGS_MODESWITCH) {
845
if (flags & WACOM_BUTTON_POSITION_LEFT)
846
return g_strdup_printf (_("Left Touchring Mode Switch"));
848
return g_strdup_printf (_("Right Touchring Mode Switch"));
849
} else if (flags & WACOM_BUTTON_TOUCHSTRIPS_MODESWITCH) {
850
if (flags & WACOM_BUTTON_POSITION_LEFT)
851
return g_strdup_printf (_("Left Touchstrip Mode Switch"));
853
return g_strdup_printf (_("Right Touchstrip Mode Switch"));
856
g_warning ("Unhandled modeswitch and direction combination");
858
return g_strdup_printf (_("Mode Switch #%d"), button_num);
862
gsd_wacom_device_add_buttons_dir (WacomDevice *wacom_device,
863
WacomButtonFlags direction,
864
const char *button_str,
865
const char *button_str_id)
868
guint num_buttons, i, button_num;
873
num_buttons = libwacom_get_num_buttons (wacom_device);
874
for (i = 'A'; i < 'A' + num_buttons; i++) {
875
WacomButtonFlags flags;
877
flags = libwacom_get_button_flag (wacom_device, i);
878
if (!(flags & direction))
880
/* Ignore mode switches */
881
if (flags & WACOM_BUTTON_MODESWITCH)
884
name = g_strdup_printf (button_str, button_num++);
885
id = g_strdup_printf ("%s%c", button_str_id, i);
886
l = g_list_append (l, gsd_wacom_tablet_button_new (name, id, WACOM_TABLET_BUTTON_TYPE_NORMAL, flags_to_group (flags)));
891
/* Handle modeswitches */
892
for (i = 'A'; i < 'A' + num_buttons; i++) {
893
WacomButtonFlags flags;
895
flags = libwacom_get_button_flag (wacom_device, i);
896
if (!(flags & direction))
898
/* Ignore non-mode switches */
899
if (flags & WACOM_BUTTON_MODESWITCH) {
902
name = gsd_wacom_device_modeswitch_name (flags, button_num++);
903
id = g_strdup_printf ("%s%c", button_str_id, i);
904
l = g_list_append (l, gsd_wacom_tablet_button_new (name, id, WACOM_TABLET_BUTTON_TYPE_HARDCODED, flags_to_group (flags)));
908
if (flags & WACOM_BUTTON_RINGS_MODESWITCH)
909
l = g_list_concat (l, gsd_wacom_device_add_ring_modes (wacom_device, direction));
910
else if (flags & WACOM_BUTTON_TOUCHSTRIPS_MODESWITCH)
911
l = g_list_concat (l, gsd_wacom_device_add_strip_modes (wacom_device, direction));
913
g_warning ("Unhandled modeswitches");
921
gsd_wacom_device_add_buttons (GsdWacomDevice *device,
922
WacomDevice *wacom_device)
928
l = gsd_wacom_device_add_buttons_dir (wacom_device, WACOM_BUTTON_POSITION_LEFT, _("Left Button #%d"), "left-button");
931
l = gsd_wacom_device_add_buttons_dir (wacom_device, WACOM_BUTTON_POSITION_RIGHT, _("Right Button #%d"), "right-button");
933
ret = g_list_concat (ret, l);
934
l = gsd_wacom_device_add_buttons_dir (wacom_device, WACOM_BUTTON_POSITION_TOP, _("Top Button #%d"), "top-button");
936
ret = g_list_concat (ret, l);
937
l = gsd_wacom_device_add_buttons_dir (wacom_device, WACOM_BUTTON_POSITION_BOTTOM, _("Bottom Button #%d"), "bottom-button");
939
ret = g_list_concat (ret, l);
941
device->priv->buttons = ret;
945
gsd_wacom_device_update_from_db (GsdWacomDevice *device,
946
WacomDevice *wacom_device,
947
const char *identifier)
951
settings_path = g_strdup_printf (WACOM_DEVICE_CONFIG_BASE, libwacom_get_match (wacom_device));
952
device->priv->wacom_settings = g_settings_new_with_path (WACOM_TABLET_SCHEMA,
955
device->priv->name = g_strdup (libwacom_get_name (wacom_device));
956
device->priv->reversible = libwacom_is_reversible (wacom_device);
957
device->priv->is_screen_tablet = libwacom_is_builtin (wacom_device);
958
if (device->priv->is_screen_tablet) {
959
if (libwacom_get_class (wacom_device) == WCLASS_CINTIQ)
960
device->priv->icon_name = g_strdup ("wacom-tablet-cintiq");
962
device->priv->icon_name = g_strdup ("wacom-tablet-pc");
964
device->priv->icon_name = g_strdup ("wacom-tablet");
967
if (device->priv->type == WACOM_TYPE_PAD)
968
gsd_wacom_device_add_buttons (device, wacom_device);
970
if (device->priv->type == WACOM_TYPE_STYLUS ||
971
device->priv->type == WACOM_TYPE_ERASER) {
976
ids = libwacom_get_supported_styli(wacom_device, &num_styli);
977
for (i = 0; i < num_styli; i++)
978
add_stylus_to_device (device, settings_path, ids[i]);
979
/* Create a fallback stylus if we don't have one */
981
add_stylus_to_device (device, settings_path,
982
device->priv->type == WACOM_TYPE_STYLUS ?
983
WACOM_STYLUS_FALLBACK_ID : WACOM_ERASER_FALLBACK_ID);
985
device->priv->styli = g_list_reverse (device->priv->styli);
987
g_free (settings_path);
991
gsd_wacom_device_constructor (GType type,
992
guint n_construct_properties,
993
GObjectConstructParam *construct_properties)
995
GsdWacomDevice *device;
996
GdkDeviceManager *device_manager;
997
XDeviceInfo *device_info;
998
WacomDevice *wacom_device;
1003
device = GSD_WACOM_DEVICE (G_OBJECT_CLASS (gsd_wacom_device_parent_class)->constructor (type,
1004
n_construct_properties,
1005
construct_properties));
1007
if (device->priv->gdk_device == NULL)
1008
return G_OBJECT (device);
1010
device_manager = gdk_display_get_device_manager (gdk_display_get_default ());
1011
g_object_get (device_manager, "opcode", &device->priv->opcode, NULL);
1013
g_object_get (device->priv->gdk_device, "device-id", &device->priv->device_id, NULL);
1015
device_info = XListInputDevices (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), &n_devices);
1016
if (device_info == NULL) {
1017
g_warning ("Could not list any input devices through XListInputDevices()");
1021
for (i = 0; i < n_devices; i++) {
1022
if (device_info[i].id == device->priv->device_id) {
1023
device->priv->type = get_device_type (&device_info[i]);
1024
device->priv->tool_name = g_strdup (device_info[i].name);
1029
XFreeDeviceList (device_info);
1031
if (device->priv->type == WACOM_TYPE_INVALID)
1034
path = xdevice_get_device_node (device->priv->device_id);
1036
g_warning ("Could not get the device node path for ID '%d'", device->priv->device_id);
1037
device->priv->type = WACOM_TYPE_INVALID;
1042
db = libwacom_database_new ();
1044
wacom_device = libwacom_new_from_path (db, path, FALSE, NULL);
1045
if (!wacom_device) {
1046
WacomError *wacom_error;
1048
g_debug ("Creating fallback driver for wacom tablet '%s' ('%s')",
1049
gdk_device_get_name (device->priv->gdk_device),
1052
wacom_error = libwacom_error_new ();
1053
wacom_device = libwacom_new_from_path (db, path, TRUE, wacom_error);
1054
if (wacom_device == NULL) {
1055
g_warning ("Failed to create fallback wacom device for '%s': %s (%d)",
1057
libwacom_error_get_message (wacom_error),
1058
libwacom_error_get_code (wacom_error));
1060
libwacom_error_free (&wacom_error);
1061
device->priv->type = WACOM_TYPE_INVALID;
1066
gsd_wacom_device_update_from_db (device, wacom_device, path);
1067
libwacom_destroy (wacom_device);
1070
if (device->priv->type == WACOM_TYPE_STYLUS ||
1071
device->priv->type == WACOM_TYPE_ERASER) {
1072
setup_property_notify (device);
1076
return G_OBJECT (device);
1080
gsd_wacom_device_set_property (GObject *object,
1082
const GValue *value,
1085
GsdWacomDevice *device;
1087
device = GSD_WACOM_DEVICE (object);
1090
case PROP_GDK_DEVICE:
1091
device->priv->gdk_device = g_value_get_pointer (value);
1093
case PROP_LAST_STYLUS:
1094
device->priv->last_stylus = g_value_get_pointer (value);
1097
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1103
gsd_wacom_device_get_property (GObject *object,
1108
GsdWacomDevice *device;
1110
device = GSD_WACOM_DEVICE (object);
1113
case PROP_GDK_DEVICE:
1114
g_value_set_pointer (value, device->priv->gdk_device);
1116
case PROP_LAST_STYLUS:
1117
g_value_set_pointer (value, device->priv->last_stylus);
1120
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1126
gsd_wacom_device_class_init (GsdWacomDeviceClass *klass)
1128
GObjectClass *object_class = G_OBJECT_CLASS (klass);
1130
object_class->constructor = gsd_wacom_device_constructor;
1131
object_class->finalize = gsd_wacom_device_finalize;
1132
object_class->set_property = gsd_wacom_device_set_property;
1133
object_class->get_property = gsd_wacom_device_get_property;
1135
g_type_class_add_private (klass, sizeof (GsdWacomDevicePrivate));
1137
g_object_class_install_property (object_class, PROP_GDK_DEVICE,
1138
g_param_spec_pointer ("gdk-device", "gdk-device", "gdk-device",
1139
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
1140
g_object_class_install_property (object_class, PROP_LAST_STYLUS,
1141
g_param_spec_pointer ("last-stylus", "last-stylus", "last-stylus",
1142
G_PARAM_READWRITE));
1146
gsd_wacom_device_init (GsdWacomDevice *device)
1148
device->priv = GSD_WACOM_DEVICE_GET_PRIVATE (device);
1149
device->priv->type = WACOM_TYPE_INVALID;
1153
gsd_wacom_device_finalize (GObject *object)
1155
GsdWacomDevice *device;
1156
GsdWacomDevicePrivate *p;
1158
g_return_if_fail (object != NULL);
1159
g_return_if_fail (GSD_IS_WACOM_DEVICE (object));
1161
device = GSD_WACOM_DEVICE (object);
1163
g_return_if_fail (device->priv != NULL);
1167
if (p->wacom_settings != NULL) {
1168
g_object_unref (p->wacom_settings);
1169
p->wacom_settings = NULL;
1172
g_list_foreach (p->buttons, (GFunc) gsd_wacom_tablet_button_free, NULL);
1173
g_list_free (p->buttons);
1178
g_free (p->tool_name);
1179
p->tool_name = NULL;
1181
g_free (p->icon_name);
1182
p->icon_name = NULL;
1184
gdk_window_remove_filter (NULL,
1185
(GdkFilterFunc) filter_events,
1188
G_OBJECT_CLASS (gsd_wacom_device_parent_class)->finalize (object);
1192
gsd_wacom_device_new (GdkDevice *device)
1194
return GSD_WACOM_DEVICE (g_object_new (GSD_TYPE_WACOM_DEVICE,
1195
"gdk-device", device,
1200
gsd_wacom_device_list_styli (GsdWacomDevice *device)
1202
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), NULL);
1204
return g_list_copy (device->priv->styli);
1208
gsd_wacom_device_get_stylus_for_type (GsdWacomDevice *device,
1209
GsdWacomStylusType type)
1213
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), NULL);
1215
for (l = device->priv->styli; l != NULL; l = l->next) {
1216
GsdWacomStylus *stylus = l->data;
1218
if (gsd_wacom_stylus_get_stylus_type (stylus) == type)
1225
gsd_wacom_device_get_name (GsdWacomDevice *device)
1227
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), NULL);
1229
return device->priv->name;
1233
gsd_wacom_device_get_icon_name (GsdWacomDevice *device)
1235
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), NULL);
1237
return device->priv->icon_name;
1241
gsd_wacom_device_get_tool_name (GsdWacomDevice *device)
1243
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), NULL);
1245
return device->priv->tool_name;
1249
gsd_wacom_device_reversible (GsdWacomDevice *device)
1251
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), FALSE);
1253
return device->priv->reversible;
1257
gsd_wacom_device_is_screen_tablet (GsdWacomDevice *device)
1259
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), FALSE);
1261
return device->priv->is_screen_tablet;
1265
gsd_wacom_device_get_settings (GsdWacomDevice *device)
1267
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), NULL);
1269
return device->priv->wacom_settings;
1273
gsd_wacom_device_set_current_stylus (GsdWacomDevice *device,
1277
GsdWacomStylus *stylus;
1279
g_return_if_fail (GSD_IS_WACOM_DEVICE (device));
1281
/* Don't change anything if the stylus is already set */
1282
if (device->priv->last_stylus != NULL) {
1283
GsdWacomStylus *stylus = device->priv->last_stylus;
1284
if (stylus->priv->id == stylus_id)
1288
for (l = device->priv->styli; l; l = l->next) {
1291
/* Set a nice default if 0x0 */
1292
if (stylus_id == 0x0 &&
1293
stylus->priv->type == WSTYLUS_GENERAL) {
1294
g_object_set (device, "last-stylus", stylus, NULL);
1298
if (stylus->priv->id == stylus_id) {
1299
g_object_set (device, "last-stylus", stylus, NULL);
1304
/* Setting the default stylus to be the generic one */
1305
for (l = device->priv->styli; l; l = l->next) {
1308
/* Set a nice default if 0x0 */
1309
if (stylus->priv->type == WSTYLUS_GENERAL) {
1310
g_debug ("Could not find stylus ID 0x%x for tablet '%s', setting general pen ID 0x%x instead",
1311
stylus_id, device->priv->name, stylus->priv->id);
1312
g_object_set (device, "last-stylus", stylus, NULL);
1317
g_warning ("Could not set the current stylus ID 0x%x for tablet '%s', no general pen found",
1318
stylus_id, device->priv->name);
1320
/* Setting the default stylus to be the first one */
1321
g_assert (device->priv->styli);
1323
stylus = device->priv->styli->data;
1324
g_object_set (device, "last-stylus", stylus, NULL);
1328
gsd_wacom_device_get_device_type (GsdWacomDevice *device)
1330
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), WACOM_TYPE_INVALID);
1332
return device->priv->type;
1336
gsd_wacom_device_get_area (GsdWacomDevice *device)
1340
Atom area, realtype;
1342
unsigned long nitems, bytes_after;
1343
unsigned char *data = NULL;
1346
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), NULL);
1348
g_object_get (device->priv->gdk_device, "device-id", &id, NULL);
1350
area = XInternAtom (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), "Wacom Tablet Area", False);
1352
gdk_error_trap_push ();
1353
xdevice = XOpenDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), id);
1354
if (gdk_error_trap_pop () || (device == NULL))
1357
gdk_error_trap_push ();
1358
rc = XGetDeviceProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
1359
xdevice, area, 0, 4, False,
1360
XA_INTEGER, &realtype, &realformat, &nitems,
1361
&bytes_after, &data);
1362
if (gdk_error_trap_pop () || rc != Success || realtype == None || bytes_after != 0 || nitems != 4) {
1363
XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
1367
device_area = g_new0 (int, nitems);
1368
for (i = 0; i < nitems; i++)
1369
device_area[i] = ((long*)data)[i];
1372
XCloseDevice (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xdevice);
1378
gsd_wacom_device_type_to_string (GsdWacomDeviceType type)
1381
case WACOM_TYPE_INVALID:
1383
case WACOM_TYPE_STYLUS:
1385
case WACOM_TYPE_ERASER:
1387
case WACOM_TYPE_CURSOR:
1389
case WACOM_TYPE_PAD:
1391
case WACOM_TYPE_TOUCH:
1394
return "Unknown type";
1399
gsd_wacom_device_get_buttons (GsdWacomDevice *device)
1401
g_return_val_if_fail (GSD_IS_WACOM_DEVICE (device), NULL);
1403
return g_list_copy (device->priv->buttons);
1407
gsd_wacom_device_create_fake (GsdWacomDeviceType type,
1409
const char *tool_name)
1411
GsdWacomDevice *device;
1412
GsdWacomDevicePrivate *priv;
1413
WacomDevice *wacom_device;
1415
device = GSD_WACOM_DEVICE (g_object_new (GSD_TYPE_WACOM_DEVICE, NULL));
1418
db = libwacom_database_new ();
1420
wacom_device = libwacom_new_from_name (db, name, NULL);
1421
if (wacom_device == NULL)
1424
priv = device->priv;
1426
priv->tool_name = g_strdup (tool_name);
1427
gsd_wacom_device_update_from_db (device, wacom_device, name);
1428
libwacom_destroy (wacom_device);
1434
gsd_wacom_device_create_fake_cintiq (void)
1436
GsdWacomDevice *device;
1439
device = gsd_wacom_device_create_fake (WACOM_TYPE_STYLUS,
1440
"Wacom Cintiq 21UX2",
1441
"Wacom Cintiq 21UX2 stylus");
1442
devices = g_list_prepend (NULL, device);
1444
device = gsd_wacom_device_create_fake (WACOM_TYPE_ERASER,
1445
"Wacom Cintiq 21UX2",
1446
"Wacom Cintiq 21UX2 eraser");
1447
devices = g_list_prepend (devices, device);
1449
device = gsd_wacom_device_create_fake (WACOM_TYPE_PAD,
1450
"Wacom Cintiq 21UX2",
1451
"Wacom Cintiq 21UX2 pad");
1452
devices = g_list_prepend (devices, device);
1458
gsd_wacom_device_create_fake_bt (void)
1460
GsdWacomDevice *device;
1463
device = gsd_wacom_device_create_fake (WACOM_TYPE_STYLUS,
1464
"Wacom Graphire Wireless",
1465
"Graphire Wireless stylus");
1466
devices = g_list_prepend (NULL, device);
1468
device = gsd_wacom_device_create_fake (WACOM_TYPE_ERASER,
1469
"Wacom Graphire Wireless",
1470
"Graphire Wireless eraser");
1471
devices = g_list_prepend (devices, device);
1473
device = gsd_wacom_device_create_fake (WACOM_TYPE_PAD,
1474
"Wacom Graphire Wireless",
1475
"Graphire Wireless pad");
1476
devices = g_list_prepend (devices, device);
1478
device = gsd_wacom_device_create_fake (WACOM_TYPE_CURSOR,
1479
"Wacom Graphire Wireless",
1480
"Graphire Wireless cursor");
1481
devices = g_list_prepend (devices, device);
1487
gsd_wacom_device_create_fake_x201 (void)
1489
GsdWacomDevice *device;
1492
device = gsd_wacom_device_create_fake (WACOM_TYPE_STYLUS,
1493
"Wacom Serial Tablet WACf004",
1494
"Wacom Serial Tablet WACf004 stylus");
1495
devices = g_list_prepend (NULL, device);
1497
device = gsd_wacom_device_create_fake (WACOM_TYPE_ERASER,
1498
"Wacom Serial Tablet WACf004",
1499
"Wacom Serial Tablet WACf004 eraser");
1500
devices = g_list_prepend (devices, device);
1506
gsd_wacom_device_create_fake_intuos4 (void)
1508
GsdWacomDevice *device;
1511
device = gsd_wacom_device_create_fake (WACOM_TYPE_STYLUS,
1512
"Wacom Intuos4 6x9",
1513
"Wacom Intuos4 6x9 stylus");
1514
devices = g_list_prepend (NULL, device);
1516
device = gsd_wacom_device_create_fake (WACOM_TYPE_ERASER,
1517
"Wacom Intuos4 6x9",
1518
"Wacom Intuos4 6x9 eraser");
1519
devices = g_list_prepend (devices, device);
1521
device = gsd_wacom_device_create_fake (WACOM_TYPE_PAD,
1522
"Wacom Intuos4 6x9",
1523
"Wacom Intuos4 6x9 pad");
1524
devices = g_list_prepend (devices, device);
1526
device = gsd_wacom_device_create_fake (WACOM_TYPE_CURSOR,
1527
"Wacom Intuos4 6x9",
1528
"Wacom Intuos4 6x9 cursor");
1529
devices = g_list_prepend (devices, device);