Debian Codegraph @

clutter/x11/clutter-device-manager-core-x11.c

raw
1 /*
2 * Clutter.
3 *
4 * An OpenGL based 'interactive canvas' library.
5 *
6 * Copyright © 2009, 2010, 2011 Intel Corp.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * Author: Emmanuele Bassi <[email protected]>
22 */
23
24 #include "config.h"
25
26 #include "clutter-device-manager-core-x11.h"
27
28 #include "clutter-backend-x11.h"
29 #include "clutter-input-device-core-x11.h"
30 #include "clutter-stage-x11.h"
31
32 #include "clutter-backend.h"
33 #include "clutter-debug.h"
34 #include "clutter-device-manager-private.h"
35 #include "clutter-event-private.h"
36 #include "clutter-event-translator.h"
37 #include "clutter-stage-private.h"
38 #include "clutter-private.h"
39
40 enum
41 {
42 PROP_0,
43
44 PROP_EVENT_BASE,
45
46 PROP_LAST
47 };
48
49 static GParamSpec *obj_props[PROP_LAST] = { NULL, };
50
51 static void clutter_event_translator_iface_init (ClutterEventTranslatorIface *iface);
52
53 #define clutter_device_manager_x11_get_type _clutter_device_manager_x11_get_type
54
55 G_DEFINE_TYPE_WITH_CODE (ClutterDeviceManagerX11,
56 clutter_device_manager_x11,
57 CLUTTER_TYPE_DEVICE_MANAGER,
58 G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_EVENT_TRANSLATOR,
59 clutter_event_translator_iface_init));
60
61 static inline void
62 translate_key_event (ClutterBackendX11 *backend_x11,
63 ClutterDeviceManagerX11 *manager_x11,
64 ClutterEvent *event,
65 XEvent *xevent)
66 {
67 ClutterEventX11 *event_x11;
68 char buffer[256 + 1];
69 int n;
70
71 event->key.type = xevent->xany.type == KeyPress ? CLUTTER_KEY_PRESS
72 : CLUTTER_KEY_RELEASE;
73 event->key.time = xevent->xkey.time;
74
75 clutter_event_set_device (event, manager_x11->core_keyboard);
76
77 /* KeyEvents have platform specific data associated to them */
78 event_x11 = _clutter_event_x11_new ();
79 _clutter_event_set_platform_data (event, event_x11);
80
81 event->key.modifier_state = (ClutterModifierType) xevent->xkey.state;
82 event->key.hardware_keycode = xevent->xkey.keycode;
83
84 /* keyval is the key ignoring all modifiers ('1' vs. '!') */
85 event->key.keyval =
86 _clutter_keymap_x11_translate_key_state (backend_x11->keymap,
87 event->key.hardware_keycode,
88 &event->key.modifier_state,
89 NULL);
90
91 event_x11->key_group =
92 _clutter_keymap_x11_get_key_group (backend_x11->keymap,
93 event->key.modifier_state);
94 event_x11->key_is_modifier =
95 _clutter_keymap_x11_get_is_modifier (backend_x11->keymap,
96 event->key.hardware_keycode);
97 event_x11->num_lock_set =
98 _clutter_keymap_x11_get_num_lock_state (backend_x11->keymap);
99 event_x11->caps_lock_set =
100 _clutter_keymap_x11_get_caps_lock_state (backend_x11->keymap);
101
102 /* unicode_value is the printable representation */
103 n = XLookupString (&xevent->xkey, buffer, sizeof (buffer) - 1, NULL, NULL);
104
105 if (n != NoSymbol)
106 {
107 event->key.unicode_value = g_utf8_get_char_validated (buffer, n);
108 if ((event->key.unicode_value != (gunichar) -1) &&
109 (event->key.unicode_value != (gunichar) -2))
110 goto out;
111 }
112 else
113 event->key.unicode_value = (gunichar)'\0';
114
115 out:
116 CLUTTER_NOTE (EVENT,
117 "%s: win:0x%x, key: %12s (%d)",
118 event->any.type == CLUTTER_KEY_PRESS
119 ? "key press "
120 : "key release",
121 (unsigned int) xevent->xkey.window,
122 event->key.keyval ? buffer : "(none)",
123 event->key.keyval);
124 return;
125 }
126
127 static ClutterTranslateReturn
128 clutter_device_manager_x11_translate_event (ClutterEventTranslator *translator,
129 gpointer native,
130 ClutterEvent *event)
131 {
132 ClutterDeviceManagerX11 *manager_x11;
133 ClutterBackendX11 *backend_x11;
134 ClutterStageX11 *stage_x11;
135 ClutterTranslateReturn res;
136 ClutterStage *stage;
137 XEvent *xevent;
138 int window_scale;
139
140 manager_x11 = CLUTTER_DEVICE_MANAGER_X11 (translator);
141 backend_x11 = CLUTTER_BACKEND_X11 (clutter_get_default_backend ());
142
143 xevent = native;
144
145 stage = clutter_x11_get_stage_from_window (xevent->xany.window);
146 if (stage == NULL)
147 return CLUTTER_TRANSLATE_CONTINUE;
148
149 if (CLUTTER_ACTOR_IN_DESTRUCTION (stage))
150 return CLUTTER_TRANSLATE_CONTINUE;
151
152 stage_x11 = CLUTTER_STAGE_X11 (_clutter_stage_get_window (stage));
153
154 window_scale = stage_x11->scale_factor;
155
156 event->any.stage = stage;
157
158 res = CLUTTER_TRANSLATE_CONTINUE;
159
160 switch (xevent->type)
161 {
162 case KeyPress:
163 translate_key_event (backend_x11, manager_x11, event, xevent);
164 _clutter_stage_x11_set_user_time (stage_x11, xevent->xkey.time);
165 res = CLUTTER_TRANSLATE_QUEUE;
166 break;
167
168 case KeyRelease:
169 /* old-style X11 terminals require that even modern X11 send
170 * KeyPress/KeyRelease pairs when auto-repeating. for this
171 * reason modern(-ish) API like XKB has a way to detect
172 * auto-repeat and do a single KeyRelease at the end of a
173 * KeyPress sequence.
174 *
175 * this check emulates XKB's detectable auto-repeat; we peek
176 * the next event and check if it's a KeyPress for the same key
177 * and timestamp - and then ignore it if it matches the
178 * KeyRelease
179 *
180 * if we have XKB, and autorepeat is enabled, then this becomes
181 * a no-op
182 */
183 if (!backend_x11->have_xkb_autorepeat && XPending (xevent->xkey.display))
184 {
185 XEvent next_event;
186
187 XPeekEvent (xevent->xkey.display, &next_event);
188
189 if (next_event.type == KeyPress &&
190 next_event.xkey.keycode == xevent->xkey.keycode &&
191 next_event.xkey.time == xevent->xkey.time)
192 {
193 res = CLUTTER_TRANSLATE_REMOVE;
194 break;
195 }
196 }
197
198 translate_key_event (backend_x11, manager_x11, event, xevent);
199 res = CLUTTER_TRANSLATE_QUEUE;
200 break;
201
202 case ButtonPress:
203 CLUTTER_NOTE (EVENT,
204 "button press: win: 0x%x, coords: %d, %d, button: %d",
205 (unsigned int) stage_x11->xwin,
206 xevent->xbutton.x,
207 xevent->xbutton.y,
208 xevent->xbutton.button);
209
210 switch (xevent->xbutton.button)
211 {
212 case 4: /* up */
213 case 5: /* down */
214 case 6: /* left */
215 case 7: /* right */
216 event->scroll.type = CLUTTER_SCROLL;
217
218 if (xevent->xbutton.button == 4)
219 event->scroll.direction = CLUTTER_SCROLL_UP;
220 else if (xevent->xbutton.button == 5)
221 event->scroll.direction = CLUTTER_SCROLL_DOWN;
222 else if (xevent->xbutton.button == 6)
223 event->scroll.direction = CLUTTER_SCROLL_LEFT;
224 else
225 event->scroll.direction = CLUTTER_SCROLL_RIGHT;
226
227 event->scroll.time = xevent->xbutton.time;
228 event->scroll.x = xevent->xbutton.x / window_scale;
229 event->scroll.y = xevent->xbutton.y / window_scale;
230 event->scroll.modifier_state = xevent->xbutton.state;
231 event->scroll.axes = NULL;
232 break;
233
234 default:
235 event->button.type = event->type = CLUTTER_BUTTON_PRESS;
236 event->button.time = xevent->xbutton.time;
237 event->button.x = xevent->xbutton.x / window_scale;
238 event->button.y = xevent->xbutton.y / window_scale;
239 event->button.modifier_state = xevent->xbutton.state;
240 event->button.button = xevent->xbutton.button;
241 event->button.axes = NULL;
242 break;
243 }
244
245 clutter_event_set_device (event, manager_x11->core_pointer);
246
247 _clutter_stage_x11_set_user_time (stage_x11, xevent->xbutton.time);
248 res = CLUTTER_TRANSLATE_QUEUE;
249 break;
250
251 case ButtonRelease:
252 CLUTTER_NOTE (EVENT,
253 "button press: win: 0x%x, coords: %d, %d, button: %d",
254 (unsigned int) stage_x11->xwin,
255 xevent->xbutton.x,
256 xevent->xbutton.y,
257 xevent->xbutton.button);
258
259 /* scroll events don't have a corresponding release */
260 if (xevent->xbutton.button == 4 ||
261 xevent->xbutton.button == 5 ||
262 xevent->xbutton.button == 6 ||
263 xevent->xbutton.button == 7)
264 {
265 res = CLUTTER_TRANSLATE_REMOVE;
266 break;
267 }
268
269 event->button.type = event->type = CLUTTER_BUTTON_RELEASE;
270 event->button.time = xevent->xbutton.time;
271 event->button.x = xevent->xbutton.x / window_scale;
272 event->button.y = xevent->xbutton.y / window_scale;
273 event->button.modifier_state = xevent->xbutton.state;
274 event->button.button = xevent->xbutton.button;
275 event->button.axes = NULL;
276 clutter_event_set_device (event, manager_x11->core_pointer);
277 res = CLUTTER_TRANSLATE_QUEUE;
278 break;
279
280 case MotionNotify:
281 CLUTTER_NOTE (EVENT,
282 "motion: win: 0x%x, coords: %d, %d",
283 (unsigned int) stage_x11->xwin,
284 xevent->xmotion.x,
285 xevent->xmotion.y);
286
287 event->motion.type = event->type = CLUTTER_MOTION;
288 event->motion.time = xevent->xmotion.time;
289 event->motion.x = xevent->xmotion.x / window_scale;
290 event->motion.y = xevent->xmotion.y / window_scale;
291 event->motion.modifier_state = xevent->xmotion.state;
292 event->motion.axes = NULL;
293 clutter_event_set_device (event, manager_x11->core_pointer);
294 res = CLUTTER_TRANSLATE_QUEUE;
295 break;
296
297 case EnterNotify:
298 CLUTTER_NOTE (EVENT, "Entering the stage (time:%u)",
299 (unsigned int) xevent->xcrossing.time);
300
301 event->crossing.type = CLUTTER_ENTER;
302 event->crossing.time = xevent->xcrossing.time;
303 event->crossing.x = xevent->xcrossing.x / window_scale;
304 event->crossing.y = xevent->xcrossing.y / window_scale;
305 event->crossing.source = CLUTTER_ACTOR (stage);
306 event->crossing.related = NULL;
307 clutter_event_set_device (event, manager_x11->core_pointer);
308
309 _clutter_input_device_set_stage (manager_x11->core_pointer, stage);
310
311 res = CLUTTER_TRANSLATE_QUEUE;
312 break;
313
314 case LeaveNotify:
315 if (manager_x11->core_pointer->stage == NULL)
316 {
317 CLUTTER_NOTE (EVENT, "Discarding LeaveNotify for "
318 "ButtonRelease event off-stage");
319 res = CLUTTER_TRANSLATE_REMOVE;
320 break;
321 }
322
323 /* we know that we are leaving the stage here */
324 CLUTTER_NOTE (EVENT, "Leaving the stage (time:%u)",
325 (unsigned int) xevent->xcrossing.time);
326
327 event->crossing.type = CLUTTER_LEAVE;
328 event->crossing.time = xevent->xcrossing.time;
329 event->crossing.x = xevent->xcrossing.x / window_scale;
330 event->crossing.y = xevent->xcrossing.y / window_scale;
331 event->crossing.source = CLUTTER_ACTOR (stage);
332 event->crossing.related = NULL;
333 clutter_event_set_device (event, manager_x11->core_pointer);
334
335 _clutter_input_device_set_stage (manager_x11->core_pointer, NULL);
336
337 res = CLUTTER_TRANSLATE_QUEUE;
338 break;
339
340 default:
341 break;
342 }
343
344 return res;
345 }
346
347 static void
348 clutter_event_translator_iface_init (ClutterEventTranslatorIface *iface)
349 {
350 iface->translate_event = clutter_device_manager_x11_translate_event;
351 }
352
353 static void
354 clutter_device_manager_x11_constructed (GObject *gobject)
355 {
356 ClutterDeviceManagerX11 *manager_x11;
357 ClutterBackendX11 *backend_x11;
358
359 manager_x11 = CLUTTER_DEVICE_MANAGER_X11 (gobject);
360
361 g_object_get (gobject, "backend", &backend_x11, NULL);
362 g_assert (backend_x11 != NULL);
363
364 manager_x11->core_pointer =
365 g_object_new (CLUTTER_TYPE_INPUT_DEVICE_X11,
366 "name", "Core Pointer",
367 "has-cursor", TRUE,
368 "device-type", CLUTTER_POINTER_DEVICE,
369 "device-manager", manager_x11,
370 "device-mode", CLUTTER_INPUT_MODE_MASTER,
371 "backend", backend_x11,
372 "enabled", TRUE,
373 NULL);
374 CLUTTER_NOTE (BACKEND, "Added core pointer device");
375
376 manager_x11->core_keyboard =
377 g_object_new (CLUTTER_TYPE_INPUT_DEVICE_X11,
378 "name", "Core Keyboard",
379 "has-cursor", FALSE,
380 "device-type", CLUTTER_KEYBOARD_DEVICE,
381 "device-manager", manager_x11,
382 "device-mode", CLUTTER_INPUT_MODE_MASTER,
383 "backend", backend_x11,
384 "enabled", TRUE,
385 NULL);
386 CLUTTER_NOTE (BACKEND, "Added core keyboard device");
387
388 /* associate core devices */
389 _clutter_input_device_set_associated_device (manager_x11->core_pointer,
390 manager_x11->core_keyboard);
391 _clutter_input_device_set_associated_device (manager_x11->core_keyboard,
392 manager_x11->core_pointer);
393
394 if (G_OBJECT_CLASS (clutter_device_manager_x11_parent_class)->constructed)
395 G_OBJECT_CLASS (clutter_device_manager_x11_parent_class)->constructed (gobject);
396 }
397
398 static void
399 clutter_device_manager_x11_add_device (ClutterDeviceManager *manager,
400 ClutterInputDevice *device)
401 {
402 ClutterDeviceManagerX11 *manager_x11 = CLUTTER_DEVICE_MANAGER_X11 (manager);
403
404 manager_x11->devices = g_slist_prepend (manager_x11->devices, device);
405 g_hash_table_replace (manager_x11->devices_by_id,
406 GINT_TO_POINTER (device->id),
407 device);
408
409 /* blow the cache */
410 g_slist_free (manager_x11->all_devices);
411 manager_x11->all_devices = NULL;
412 }
413
414 static void
415 clutter_device_manager_x11_remove_device (ClutterDeviceManager *manager,
416 ClutterInputDevice *device)
417 {
418 ClutterDeviceManagerX11 *manager_x11 = CLUTTER_DEVICE_MANAGER_X11 (manager);
419
420 g_hash_table_remove (manager_x11->devices_by_id,
421 GINT_TO_POINTER (device->id));
422 manager_x11->devices = g_slist_remove (manager_x11->devices, device);
423
424 /* blow the cache */
425 g_slist_free (manager_x11->all_devices);
426 manager_x11->all_devices = NULL;
427 }
428
429 static const GSList *
430 clutter_device_manager_x11_get_devices (ClutterDeviceManager *manager)
431 {
432 ClutterDeviceManagerX11 *manager_x11 = CLUTTER_DEVICE_MANAGER_X11 (manager);
433
434 /* cache the devices list so that we can keep the core pointer
435 * and keyboard outside of the ManagerX11:devices list
436 */
437 if (manager_x11->all_devices == NULL)
438 {
439 GSList *all_devices = manager_x11->devices;
440
441 all_devices = g_slist_prepend (all_devices, manager_x11->core_keyboard);
442 all_devices = g_slist_prepend (all_devices, manager_x11->core_pointer);
443
444 manager_x11->all_devices = all_devices;
445 }
446
447 return CLUTTER_DEVICE_MANAGER_X11 (manager)->all_devices;
448 }
449
450 static ClutterInputDevice *
451 clutter_device_manager_x11_get_core_device (ClutterDeviceManager *manager,
452 ClutterInputDeviceType type)
453 {
454 ClutterDeviceManagerX11 *manager_x11;
455
456 manager_x11 = CLUTTER_DEVICE_MANAGER_X11 (manager);
457
458 switch (type)
459 {
460 case CLUTTER_POINTER_DEVICE:
461 return manager_x11->core_pointer;
462
463 case CLUTTER_KEYBOARD_DEVICE:
464 return manager_x11->core_keyboard;
465
466 default:
467 return NULL;
468 }
469
470 return NULL;
471 }
472
473 static ClutterInputDevice *
474 clutter_device_manager_x11_get_device (ClutterDeviceManager *manager,
475 gint id)
476 {
477 ClutterDeviceManagerX11 *manager_x11 = CLUTTER_DEVICE_MANAGER_X11 (manager);
478
479 return g_hash_table_lookup (manager_x11->devices_by_id,
480 GINT_TO_POINTER (id));
481 }
482
483 static void
484 clutter_device_manager_x11_set_property (GObject *gobject,
485 guint prop_id,
486 const GValue *value,
487 GParamSpec *pspec)
488 {
489 ClutterDeviceManagerX11 *manager_x11 = CLUTTER_DEVICE_MANAGER_X11 (gobject);
490
491 switch (prop_id)
492 {
493 case PROP_EVENT_BASE:
494 manager_x11->xi_event_base = g_value_get_int (value);
495 break;
496
497 default:
498 G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
499 break;
500 }
501 }
502
503 static void
504 clutter_device_manager_x11_class_init (ClutterDeviceManagerX11Class *klass)
505 {
506 ClutterDeviceManagerClass *manager_class;
507 GObjectClass *gobject_class;
508
509 obj_props[PROP_EVENT_BASE] =
510 g_param_spec_int ("event-base",
511 "Event Base",
512 "The first XI event",
513 -1, G_MAXINT,
514 -1,
515 CLUTTER_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY);
516
517 gobject_class = G_OBJECT_CLASS (klass);
518 gobject_class->constructed = clutter_device_manager_x11_constructed;
519 gobject_class->set_property = clutter_device_manager_x11_set_property;
520
521 g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
522
523 manager_class = CLUTTER_DEVICE_MANAGER_CLASS (klass);
524 manager_class->add_device = clutter_device_manager_x11_add_device;
525 manager_class->remove_device = clutter_device_manager_x11_remove_device;
526 manager_class->get_devices = clutter_device_manager_x11_get_devices;
527 manager_class->get_core_device = clutter_device_manager_x11_get_core_device;
528 manager_class->get_device = clutter_device_manager_x11_get_device;
529 }
530
531 static void
532 clutter_device_manager_x11_init (ClutterDeviceManagerX11 *self)
533 {
534 self->devices_by_id = g_hash_table_new (NULL, NULL);
535 }