aboutsummaryrefslogtreecommitdiffstats
path: root/meta-xfce/recipes-xfce/xfce4-settings/xfce4-settings/touchscreen/0005-pointers-detect-a-change-of-pointer-device-used-and-.patch
blob: f4c28397da667ec2d7f36feb439061ca39690a4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
From 37f5e33511499d320c3035c5377425004657faa5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20M=C3=BCller?= <schnitzeltony@googlemail.com>
Date: Thu, 20 Dec 2012 16:58:19 +0100
Subject: [PATCH 5/5] pointers: detect a change of pointer-device used and set
 "touchscreen-pointer" as found in xfconf
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

To detect pointer device in use the DevicePresence-, DeviceMotionNotify- and
DeviceButtonPress-events are handled. Tests showed that by repopenning and
closing the pointer devices (e.g when another setting was modified), the events
stopped working. Therefore all pointer devices are opened only once and managed
in a hash table.

Signed-off-by: Andreas Müller <schnitzeltony@googlemail.com>
Upstream-Status: Submitted [1]
[1] https://bugzilla.xfce.org/show_bug.cgi?id=9474
---
 xfsettingsd/pointers.c |  337 +++++++++++++++++++++++++++++++----------------
 1 files changed, 222 insertions(+), 115 deletions(-)

diff --git a/xfsettingsd/pointers.c b/xfsettingsd/pointers.c
index 62ebc60..32662cc 100644
--- a/xfsettingsd/pointers.c
+++ b/xfsettingsd/pointers.c
@@ -66,11 +66,9 @@ static void             xfce_pointers_helper_channel_property_changed (XfconfCha
                                                                        const gchar        *property_name,
                                                                        const GValue       *value,
                                                                        XfcePointersHelper *helper);
-#ifdef DEVICE_HOTPLUGGING
 static GdkFilterReturn  xfce_pointers_helper_event_filter             (GdkXEvent          *xevent,
                                                                        GdkEvent           *gdk_event,
                                                                        gpointer            user_data);
-#endif
 static void             xfce_pointers_helper_set_property             (GObject              *object,
                                                                        guint                 prop_id,
                                                                        const GValue         *value,
@@ -96,8 +94,11 @@ struct _XfcePointersHelper
     GPid           syndaemon_pid;
 #endif
 
+    GHashTable    *pointer_devices;
+    XID            last_pointer_active;
+    gint           device_motion_event_type;
+    gint           device_button_press_event_type;
 #ifdef DEVICE_HOTPLUGGING
-    /* device presence event type */
     gint           device_presence_event_type;
 #endif
 };
@@ -117,6 +118,15 @@ G_DEFINE_TYPE (XfcePointersHelper, xfce_pointers_helper, G_TYPE_OBJECT);
 
 
 
+typedef struct
+{
+    XDevice     *device;
+    gchar*      name;
+}
+XfcePointerDeviceData;
+
+
+
 static void
 xfce_pointers_helper_class_init (XfcePointersHelperClass *klass)
 {
@@ -147,6 +157,18 @@ xfce_pointers_helper_init (XfcePointersHelper *helper)
 
 
 static void
+xfce_pointers_device_close (gpointer data)
+{
+    XfcePointerDeviceData *device_data = data;
+
+    XCloseDevice (GDK_DISPLAY (), device_data->device);
+    g_free (device_data->name);
+    g_free (device_data);
+}
+
+
+
+static void
 xfce_pointers_helper_constructed (GObject *object)
 {
     XfcePointersHelper *helper = XFCE_POINTERS_HELPER (object);
@@ -185,6 +207,10 @@ xfce_pointers_helper_constructed (GObject *object)
         helper->channel = xfconf_channel_get ("pointers");
 
         /* restore the pointer devices */
+        helper->pointer_devices = g_hash_table_new_full (g_int_hash,
+                                                         g_int_equal,
+                                                         g_free,
+                                                         xfce_pointers_device_close);
         xfce_pointers_helper_restore_devices (helper, NULL);
 
         /* monitor the channel */
@@ -194,21 +220,20 @@ xfce_pointers_helper_constructed (GObject *object)
         /* launch syndaemon if required */
         xfce_pointers_helper_syndaemon_check (helper);
 
-#ifdef DEVICE_HOTPLUGGING
         if (G_LIKELY (xdisplay != NULL))
         {
+#ifdef DEVICE_HOTPLUGGING
             /* monitor device changes */
             gdk_error_trap_push ();
             DevicePresence (xdisplay, helper->device_presence_event_type, event_class);
             XSelectExtensionEvent (xdisplay, RootWindow (xdisplay, DefaultScreen (xdisplay)), &event_class, 1);
 
             /* add an event filter */
-            if (gdk_error_trap_pop () == 0)
-                gdk_window_add_filter (NULL, xfce_pointers_helper_event_filter, helper);
-            else
+            if (gdk_error_trap_pop () != 0)
                 g_warning ("Failed to create device filter");
-        }
 #endif
+            gdk_window_add_filter (NULL, xfce_pointers_helper_event_filter, helper);
+        }
     }
 }
 
@@ -238,7 +263,10 @@ xfce_pointers_helper_set_property (GObject      *object,
 static void
 xfce_pointers_helper_finalize (GObject *object)
 {
-    xfce_pointers_helper_syndaemon_stop (XFCE_POINTERS_HELPER (object));
+    XfcePointersHelper* helper = XFCE_POINTERS_HELPER (object);
+
+    xfce_pointers_helper_syndaemon_stop (helper);
+    g_hash_table_destroy (helper->pointer_devices);
 
     (*G_OBJECT_CLASS (xfce_pointers_helper_parent_class)->finalize) (object);
 }
@@ -267,19 +295,19 @@ static void
 xfce_pointers_helper_syndaemon_check (XfcePointersHelper *helper)
 {
 #ifdef DEVICE_PROPERTIES
-    Display     *xdisplay = GDK_DISPLAY ();
-    XDeviceInfo *device_list;
-    XDevice     *device;
-    gint         n, ndevices;
-    Atom         touchpad_type;
-    Atom         touchpad_off_prop;
-    Atom        *props;
-    gint         i, nprops;
-    gboolean     have_synaptics = FALSE;
-    gdouble      disable_duration;
-    gchar        disable_duration_string[64];
-    gchar       *args[] = { "syndaemon", "-i", disable_duration_string, "-K", "-R", NULL };
-    GError      *error = NULL;
+    Display               *xdisplay = GDK_DISPLAY ();
+    XDeviceInfo           *device_list;
+    XfcePointerDeviceData *device_data;
+    gint                   n, ndevices;
+    Atom                   touchpad_type;
+    Atom                   touchpad_off_prop;
+    Atom                  *props;
+    gint                   i, nprops;
+    gboolean               have_synaptics = FALSE;
+    gdouble                disable_duration;
+    gchar                  disable_duration_string[64];
+    gchar                 *args[] = { "syndaemon", "-i", disable_duration_string, "-K", "-R", NULL };
+    GError                *error = NULL;
 
     /* only stop a running daemon */
     if (!xfconf_channel_get_bool (helper->channel, "/DisableTouchpadWhileTyping", FALSE))
@@ -297,17 +325,16 @@ xfce_pointers_helper_syndaemon_check (XfcePointersHelper *helper)
         if (device_list[n].type != touchpad_type)
             continue;
 
-        gdk_error_trap_push ();
-        device = XOpenDevice (xdisplay, device_list[n].id);
-        if (gdk_error_trap_pop () != 0 || device == NULL)
+        device_data = g_hash_table_lookup (helper->pointer_devices, &device_list[n].id);
+        if (device_data == NULL)
         {
-            g_critical ("Unable to open device %s", device_list[n].name);
+            g_critical ("xfce_pointers_helper_syndaemon_check: Unable to find device %s / ID %i in hash table", device_list[n].name, device_list[n].id);
             break;
         }
 
         /* look for the Synaptics Off property */
         gdk_error_trap_push ();
-        props = XListDeviceProperties (xdisplay, device, &nprops);
+        props = XListDeviceProperties (xdisplay, device_data->device, &nprops);
         if (gdk_error_trap_pop () == 0
             && props != NULL)
         {
@@ -317,8 +344,6 @@ xfce_pointers_helper_syndaemon_check (XfcePointersHelper *helper)
             XFree (props);
         }
 
-        XCloseDevice (xdisplay, device);
-
         if (have_synaptics)
             break;
     }
@@ -844,24 +869,56 @@ xfce_pointers_helper_change_properties (gpointer key,
 
 
 static void
+xfce_pointers_helper_change_current_device (XfcePointersHelper *helper,
+                                            XID *xid)
+{
+    XfcePointerDeviceData *device_data;
+    gchar*                 prop;
+    GValue                 bool_val = { 0, };
+
+    helper->last_pointer_active = *xid;
+    device_data = g_hash_table_lookup (helper->pointer_devices, xid);
+    if (device_data == NULL)
+    {
+        g_critical ("Unable to find device ID %i in hash table", *xid);
+        return;
+    }
+    if (G_LIKELY (G_IS_OBJECT (helper->xsettings_helper)))
+    {
+        prop = g_strconcat ("/", device_data->name, "/TouchscreenPointer", NULL);
+        g_value_init (&bool_val, G_TYPE_BOOLEAN);
+        g_value_set_boolean (&bool_val,
+                             xfconf_channel_get_bool (helper->channel, prop, FALSE));
+        g_object_set_property (helper->xsettings_helper, "touchscreen-pointer", &bool_val);
+        g_value_unset (&bool_val);
+        g_free (prop);
+    }
+    else
+        g_critical ("xsettings_helper was not properly set");
+}
+
+
+static void
 xfce_pointers_helper_restore_devices (XfcePointersHelper *helper,
                                       XID                *xid)
 {
-    Display         *xdisplay = GDK_DISPLAY ();
-    XDeviceInfo     *device_list, *device_info;
-    gint             n, ndevices;
-    XDevice         *device;
-    gchar           *device_name;
-    gchar            prop[256];
-    gboolean         right_handed;
-    gboolean         reverse_scrolling;
-    gint             threshold;
-    gdouble          acceleration;
+    Display               *xdisplay = GDK_DISPLAY ();
+    XDeviceInfo           *device_list, *device_info;
+    gint                   n, ndevices;
+    XDevice               *device;
+    XfcePointerDeviceData *device_data;
+    gchar                 *device_name;
+    gchar                  prop[256];
+    gboolean               right_handed;
+    gboolean               reverse_scrolling;
+    gint                   threshold;
+    gdouble                acceleration;
+    XEventClass            event_classes[2];
 #ifdef DEVICE_PROPERTIES
-    GHashTable      *props;
-    XfcePointerData  pointer_data;
+    GHashTable            *props;
+    XfcePointerData        pointer_data;
 #endif
-    const gchar     *mode;
+    const gchar           *mode;
 
     gdk_error_trap_push ();
     device_list = XListInputDevices (xdisplay, &ndevices);
@@ -873,84 +930,107 @@ xfce_pointers_helper_restore_devices (XfcePointersHelper *helper,
 
     for (n = 0; n < ndevices; n++)
     {
-        /* filter the pointer devices */
+        /* filter the physical pointer devices */
         device_info = &device_list[n];
         if (device_info->use != IsXExtensionPointer
-            || device_info->name == NULL)
+            || device_info->name == NULL
+            || g_str_has_prefix (device_info->name, "Virtual core XTEST"))
             continue;
 
         /* filter out the device if one is set */
         if (xid != NULL && device_info->id != *xid)
             continue;
 
-        /* open the device */
-        gdk_error_trap_push ();
-        device = XOpenDevice (xdisplay, device_info->id);
-        if (gdk_error_trap_pop () != 0 || device == NULL)
+        device_data = g_hash_table_lookup (helper->pointer_devices, &device_info->id);
+        if (device_data == NULL)
         {
-            g_critical ("Unable to open device %s", device_info->name);
-            continue;
-        }
+            /* open the device and insert to hash */
+            gdk_error_trap_push ();
+            device = XOpenDevice (xdisplay, device_info->id);
+            if (G_UNLIKELY(gdk_error_trap_pop () != 0 || device == NULL))
+            {
+                g_critical ("Unable to open device %s / ID: %i", device_info->name, device_info->id);
+                continue;
+            }
 
-        /* create a valid xfconf property name for the device */
-        device_name = xfce_pointers_helper_device_xfconf_name (device_info->name);
+            /* create a valid xfconf property name for the device */
+            device_name = xfce_pointers_helper_device_xfconf_name (device_info->name);
 
-        /* read buttonmap properties */
-        g_snprintf (prop, sizeof (prop), "/%s/RightHanded", device_name);
-        right_handed = xfconf_channel_get_bool (helper->channel, prop, -1);
+            /* add device to our list */
+            device_data = g_new (XfcePointerDeviceData, 1);
+            device_data->device = device;
+            device_data->name = device_name;
+            g_hash_table_insert (helper->pointer_devices, g_memdup (&device_info->id, sizeof(device_info->id)), device_data);
 
-        g_snprintf (prop, sizeof (prop), "/%s/ReverseScrolling", device_name);
-        reverse_scrolling = xfconf_channel_get_bool (helper->channel, prop, -1);
+            /* catch motion event / button-press for new device */
+            gdk_error_trap_push ();
+            DeviceMotionNotify (device, helper->device_motion_event_type, event_classes[0]);
+            DeviceButtonPress (device, helper->device_button_press_event_type, event_classes[1]);
+            XSelectExtensionEvent (xdisplay, RootWindow (xdisplay, DefaultScreen (xdisplay)), event_classes, 2);
+            if (G_UNLIKELY (gdk_error_trap_pop () != 0))
+               g_critical ("Unable to register DeviceButtonPress/DeviceMotionNotify for %i", device_info->id);
 
-        if (right_handed != -1 || reverse_scrolling != -1)
-        {
-            xfce_pointers_helper_change_button_mapping (device_info, device, xdisplay,
+
+            /* read buttonmap properties */
+            g_snprintf (prop, sizeof (prop), "/%s/RightHanded", device_name);
+            right_handed = xfconf_channel_get_bool (helper->channel, prop, -1);
+
+            g_snprintf (prop, sizeof (prop), "/%s/ReverseScrolling", device_name);
+            reverse_scrolling = xfconf_channel_get_bool (helper->channel, prop, -1);
+
+            if (right_handed != -1 || reverse_scrolling != -1)
+            {
+                xfce_pointers_helper_change_button_mapping (device_info, device, xdisplay,
                                                         right_handed, reverse_scrolling);
-        }
+            }
 
-        /* read feedback settings */
-        g_snprintf (prop, sizeof (prop), "/%s/Threshold", device_name);
-        threshold = xfconf_channel_get_int (helper->channel, prop, -1);
+            /* read feedback settings */
+            g_snprintf (prop, sizeof (prop), "/%s/Threshold", device_name);
+            threshold = xfconf_channel_get_int (helper->channel, prop, -1);
 
-        g_snprintf (prop, sizeof (prop), "/%s/Acceleration", device_name);
-        acceleration = xfconf_channel_get_double (helper->channel, prop, -1.00);
+            g_snprintf (prop, sizeof (prop), "/%s/Acceleration", device_name);
+            acceleration = xfconf_channel_get_double (helper->channel, prop, -1.00);
 
-        if (threshold != -1 || acceleration != -1.00)
-        {
-            xfce_pointers_helper_change_feedback (device_info, device, xdisplay,
-                                                  threshold, acceleration);
-        }
+            if (threshold != -1 || acceleration != -1.00)
+            {
+                xfce_pointers_helper_change_feedback (device_info, device, xdisplay,
+                                                      threshold, acceleration);
+            }
 
-        /* read mode settings */
-        g_snprintf (prop, sizeof (prop), "/%s/Mode", device_name);
-        mode =  xfconf_channel_get_string  (helper->channel, prop, NULL);
+            /* read mode settings */
+            g_snprintf (prop, sizeof (prop), "/%s/Mode", device_name);
+            mode =  xfconf_channel_get_string  (helper->channel, prop, NULL);
 
-        if (mode != NULL)
-            xfce_pointers_helper_change_mode (device_info, device, xdisplay, mode);
+            if (mode != NULL)
+                xfce_pointers_helper_change_mode (device_info, device, xdisplay, mode);
 
 #ifdef DEVICE_PROPERTIES
-        /* set device properties */
-        g_snprintf (prop, sizeof (prop), "/%s/Properties", device_name);
-        props = xfconf_channel_get_properties (helper->channel, prop);
+            /* set device properties */
+            g_snprintf (prop, sizeof (prop), "/%s/Properties", device_name);
+            props = xfconf_channel_get_properties (helper->channel, prop);
 
-        if (props != NULL)
-        {
-            pointer_data.xdisplay = xdisplay;
-            pointer_data.device = device;
-            pointer_data.device_info = device_info;
-            pointer_data.prop_name_len = strlen (prop) + 1;
+            if (props != NULL)
+            {
+                pointer_data.xdisplay = xdisplay;
+                pointer_data.device = device;
+                pointer_data.device_info = device_info;
+                pointer_data.prop_name_len = strlen (prop) + 1;
 
-            g_hash_table_foreach (props, xfce_pointers_helper_change_properties, &pointer_data);
+                g_hash_table_foreach (props, xfce_pointers_helper_change_properties, &pointer_data);
 
-            g_hash_table_destroy (props);
-        }
+                g_hash_table_destroy (props);
+            }
 #endif
-
-        g_free (device_name);
-        XCloseDevice (xdisplay, device);
+        }
     }
-
     XFreeDeviceList (device_list);
+    if (G_LIKELY (device_data != NULL))
+    {
+        if (helper->last_pointer_active != device_data->device->device_id)
+            xfce_pointers_helper_change_current_device (helper, &device_data->device->device_id);
+    }
+    else
+        g_critical("no device selected in xfce_pointers_helper_restore_devices");
 }
 
 
@@ -961,12 +1041,12 @@ xfce_pointers_helper_channel_property_changed (XfconfChannel      *channel,
                                                const GValue       *value,
                                                XfcePointersHelper *helper)
 {
-    Display      *xdisplay = GDK_DISPLAY ();
-    XDeviceInfo  *device_list, *device_info;
-    XDevice      *device;
-    gint          n, ndevices;
-    gchar       **names;
-    gchar        *device_name;
+    Display               *xdisplay = GDK_DISPLAY ();
+    XDeviceInfo           *device_list, *device_info;
+    XfcePointerDeviceData *device_data;
+    gint                   n, ndevices;
+    gchar                **names;
+    gchar                 *device_name;
 
     if (G_UNLIKELY (property_name == NULL))
          return;
@@ -996,63 +1076,72 @@ xfce_pointers_helper_channel_property_changed (XfconfChannel      *channel,
             /* filter the pointer devices */
             device_info = &device_list[n];
             if (device_info->use != IsXExtensionPointer
-                || device_info->name == NULL)
+                || device_info->name == NULL
+                || g_str_has_prefix (device_info->name, "Virtual core XTEST"))
                 continue;
 
             /* search the device name */
             device_name = xfce_pointers_helper_device_xfconf_name (device_info->name);
             if (strcmp (names[0], device_name) == 0)
             {
-                /* open the device */
-                gdk_error_trap_push ();
-                device = XOpenDevice (xdisplay, device_info->id);
-                if (gdk_error_trap_pop () != 0 || device == NULL)
+                /* find the device */
+                device_data = g_hash_table_lookup (helper->pointer_devices, &device_info->id);
+                if (device_data == NULL)
                 {
-                    g_critical ("Unable to open device %s", device_info->name);
+                    g_critical ("xfce_pointers_helper_channel_property_changed: Unable to find device %s / ID %i in hash table", device_info->name, device_info->id);
                     continue;
                 }
 
                 /* check the property that requires updating */
                 if (strcmp (names[1], "RightHanded") == 0)
                 {
-                    xfce_pointers_helper_change_button_mapping (device_info, device, xdisplay,
+                    xfce_pointers_helper_change_button_mapping (device_info, device_data->device, xdisplay,
                                                                 g_value_get_boolean (value), -1);
                 }
                 else if (strcmp (names[1], "ReverseScrolling") == 0)
                 {
-                    xfce_pointers_helper_change_button_mapping (device_info, device, xdisplay,
+                    xfce_pointers_helper_change_button_mapping (device_info, device_data->device, xdisplay,
                                                                 -1, g_value_get_boolean (value));
                 }
                 else if (strcmp (names[1], "Threshold") == 0)
                 {
-                    xfce_pointers_helper_change_feedback (device_info, device, xdisplay,
+                    xfce_pointers_helper_change_feedback (device_info, device_data->device, xdisplay,
                                                           g_value_get_int (value), -2.00);
                 }
                 else if (strcmp (names[1], "Acceleration") == 0)
                 {
-                    xfce_pointers_helper_change_feedback (device_info, device, xdisplay,
+                    xfce_pointers_helper_change_feedback (device_info, device_data->device, xdisplay,
                                                           -2, g_value_get_double (value));
                 }
 #ifdef DEVICE_PROPERTIES
                 else if (strcmp (names[1], "Properties") == 0)
                 {
-                    xfce_pointers_helper_change_property (device_info, device, xdisplay,
+                    xfce_pointers_helper_change_property (device_info, device_data->device, xdisplay,
                                                           names[2], value);
                 }
 #endif
                 else if (strcmp (names[1], "Mode") == 0)
                 {
-                    xfce_pointers_helper_change_mode (device_info, device, xdisplay,
+                    xfce_pointers_helper_change_mode (device_info, device_data->device, xdisplay,
                                                       g_value_get_string (value));
                 }
+                else if (strcmp (names[1], "TouchscreenPointer") == 0)
+                {
+                   /* only hide if the current device's property was changed */
+                   if (device_info->id == helper->last_pointer_active)
+                   {
+                     if (G_LIKELY (G_IS_OBJECT (helper->xsettings_helper)))
+                         g_object_set_property (helper->xsettings_helper, "touchscreen-pointer", value);
+                     else
+                         g_critical ("xsettings_helper was not properly set");
+                   }
+                }
                 else
                 {
                     g_warning ("Unknown property %s set for device %s",
                                property_name, device_info->name);
                 }
 
-                XCloseDevice (xdisplay, device);
-
                 /* stop searching */
                 n = ndevices;
             }
@@ -1068,26 +1157,44 @@ xfce_pointers_helper_channel_property_changed (XfconfChannel      *channel,
 
 
 
-#ifdef DEVICE_HOTPLUGGING
 static GdkFilterReturn
 xfce_pointers_helper_event_filter (GdkXEvent *xevent,
                                    GdkEvent  *gdk_event,
                                    gpointer   user_data)
 {
+    XDevicePresenceNotifyEvent *dpn_event;
     XEvent                     *event = xevent;
-    XDevicePresenceNotifyEvent *dpn_event = xevent;
     XfcePointersHelper         *helper = XFCE_POINTERS_HELPER (user_data);
 
-    if (event->type == helper->device_presence_event_type)
+    /* Comparison for device changed is done here redundantly to prevent call
+     * on every mouse move.
+     */
+    if (event->type == helper->device_motion_event_type
+             && helper->last_pointer_active != ((XDeviceMotionEvent*)xevent)->deviceid)
+        xfce_pointers_helper_change_current_device (helper, &((XDeviceMotionEvent*)xevent)->deviceid);
+    else if (event->type == helper->device_button_press_event_type
+             && helper->last_pointer_active != ((XDeviceButtonEvent*)xevent)->deviceid)
+        xfce_pointers_helper_change_current_device (helper, &((XDeviceButtonEvent*)xevent)->deviceid);
+
+#ifdef DEVICE_HOTPLUGGING
+    /* handle device add/remove */
+    else if (event->type == helper->device_presence_event_type)
     {
+        dpn_event = xevent;
         /* restore device settings */
         if (dpn_event->devchange == DeviceAdded)
             xfce_pointers_helper_restore_devices (helper, &dpn_event->deviceid);
+        else if(dpn_event->devchange == DeviceRemoved)
+            /* we could try to find a remaining pointer to set that as active
+             * one but that might not fit and as soon as the user works with
+             * another pointer we are changing to correct one.
+             */
+            g_hash_table_remove (helper->pointer_devices, &dpn_event->deviceid);
 
         /* check if we need to launch syndaemon */
         xfce_pointers_helper_syndaemon_check (helper);
     }
+#endif
 
     return GDK_FILTER_CONTINUE;
 }
-#endif
-- 
1.7.6.5