Debian Codegraph @

gdk/x11/gdkdevicemanager-core-x11.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2009 Carlos Garnacho <[email protected]>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include "gdkx11devicemanager-core.h"
21 #include "gdkdevicemanagerprivate-core.h"
22 #include "gdkx11device-core.h"
23
24 #include "gdkdeviceprivate.h"
25 #include "gdkseatdefaultprivate.h"
26 #include "gdkdisplayprivate.h"
27 #include "gdkeventtranslator.h"
28 #include "gdkprivate-x11.h"
29 #include "gdkkeysyms.h"
30
31
32 #define HAS_FOCUS(toplevel) \
33 ((toplevel)->has_focus || (toplevel)->has_pointer_focus)
34
35 static void gdk_x11_device_manager_core_finalize (GObject *object);
36 static void gdk_x11_device_manager_core_constructed (GObject *object);
37
38 static GList * gdk_x11_device_manager_core_list_devices (GdkDeviceManager *device_manager,
39 GdkDeviceType type);
40 static GdkDevice * gdk_x11_device_manager_core_get_client_pointer (GdkDeviceManager *device_manager);
41
42 static void gdk_x11_device_manager_event_translator_init (GdkEventTranslatorIface *iface);
43
44 static gboolean gdk_x11_device_manager_core_translate_event (GdkEventTranslator *translator,
45 GdkDisplay *display,
46 GdkEvent *event,
47 XEvent *xevent);
48
49
50 G_DEFINE_TYPE_WITH_CODE (GdkX11DeviceManagerCore, gdk_x11_device_manager_core, GDK_TYPE_DEVICE_MANAGER,
51 G_IMPLEMENT_INTERFACE (GDK_TYPE_EVENT_TRANSLATOR,
52 gdk_x11_device_manager_event_translator_init))
53
54 static void
55 gdk_x11_device_manager_core_class_init (GdkX11DeviceManagerCoreClass *klass)
56 {
57 GdkDeviceManagerClass *device_manager_class = GDK_DEVICE_MANAGER_CLASS (klass);
58 GObjectClass *object_class = G_OBJECT_CLASS (klass);
59
60 object_class->finalize = gdk_x11_device_manager_core_finalize;
61 object_class->constructed = gdk_x11_device_manager_core_constructed;
62 device_manager_class->list_devices = gdk_x11_device_manager_core_list_devices;
63 device_manager_class->get_client_pointer = gdk_x11_device_manager_core_get_client_pointer;
64 }
65
66 static void
67 gdk_x11_device_manager_event_translator_init (GdkEventTranslatorIface *iface)
68 {
69 iface->translate_event = gdk_x11_device_manager_core_translate_event;
70 }
71
72 static GdkDevice *
73 create_core_pointer (GdkDeviceManager *device_manager,
74 GdkDisplay *display)
75 {
76 return g_object_new (GDK_TYPE_X11_DEVICE_CORE,
77 "name", "Core Pointer",
78 "type", GDK_DEVICE_TYPE_MASTER,
79 "input-source", GDK_SOURCE_MOUSE,
80 "input-mode", GDK_MODE_SCREEN,
81 "has-cursor", TRUE,
82 "display", display,
83 "device-manager", device_manager,
84 NULL);
85 }
86
87 static GdkDevice *
88 create_core_keyboard (GdkDeviceManager *device_manager,
89 GdkDisplay *display)
90 {
91 return g_object_new (GDK_TYPE_X11_DEVICE_CORE,
92 "name", "Core Keyboard",
93 "type", GDK_DEVICE_TYPE_MASTER,
94 "input-source", GDK_SOURCE_KEYBOARD,
95 "input-mode", GDK_MODE_SCREEN,
96 "has-cursor", FALSE,
97 "display", display,
98 "device-manager", device_manager,
99 NULL);
100 }
101
102 static void
103 gdk_x11_device_manager_core_init (GdkX11DeviceManagerCore *device_manager)
104 {
105 }
106
107 static void
108 gdk_x11_device_manager_core_finalize (GObject *object)
109 {
110 GdkX11DeviceManagerCore *device_manager_core;
111
112 device_manager_core = GDK_X11_DEVICE_MANAGER_CORE (object);
113
114 g_object_unref (device_manager_core->core_pointer);
115 g_object_unref (device_manager_core->core_keyboard);
116
117 G_OBJECT_CLASS (gdk_x11_device_manager_core_parent_class)->finalize (object);
118 }
119
120 static void
121 gdk_x11_device_manager_core_constructed (GObject *object)
122 {
123 GdkX11DeviceManagerCore *device_manager;
124 GdkDisplay *display;
125
126 device_manager = GDK_X11_DEVICE_MANAGER_CORE (object);
127 display = gdk_device_manager_get_display (GDK_DEVICE_MANAGER (object));
128 device_manager->core_pointer = create_core_pointer (GDK_DEVICE_MANAGER (device_manager), display);
129 device_manager->core_keyboard = create_core_keyboard (GDK_DEVICE_MANAGER (device_manager), display);
130
131 _gdk_device_set_associated_device (device_manager->core_pointer, device_manager->core_keyboard);
132 _gdk_device_set_associated_device (device_manager->core_keyboard, device_manager->core_pointer);
133
134 /* We expect subclasses to handle their own seats */
135 if (G_OBJECT_TYPE (object) == GDK_TYPE_X11_DEVICE_MANAGER_CORE)
136 {
137 GdkSeat *seat;
138
139 seat = gdk_seat_default_new_for_master_pair (device_manager->core_pointer,
140 device_manager->core_keyboard);
141
142 gdk_display_add_seat (display, seat);
143 g_object_unref (seat);
144 }
145 }
146
147 static void
148 translate_key_event (GdkDisplay *display,
149 GdkX11DeviceManagerCore *device_manager,
150 GdkEvent *event,
151 XEvent *xevent)
152 {
153 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
154 GdkModifierType consumed, state;
155
156 event->key.type = xevent->xany.type == KeyPress ? GDK_KEY_PRESS : GDK_KEY_RELEASE;
157 event->key.time = xevent->xkey.time;
158 gdk_event_set_device (event, device_manager->core_keyboard);
159
160 event->key.state = (GdkModifierType) xevent->xkey.state;
161 event->key.group = gdk_x11_keymap_get_group_for_state (keymap, xevent->xkey.state);
162 event->key.hardware_keycode = xevent->xkey.keycode;
163 gdk_event_set_scancode (event, xevent->xkey.keycode);
164
165 event->key.keyval = GDK_KEY_VoidSymbol;
166
167 gdk_keymap_translate_keyboard_state (keymap,
168 event->key.hardware_keycode,
169 event->key.state,
170 event->key.group,
171 &event->key.keyval,
172 NULL, NULL, &consumed);
173
174 state = event->key.state & ~consumed;
175 _gdk_x11_keymap_add_virt_mods (keymap, &state);
176 event->key.state |= state;
177
178 event->key.is_modifier = gdk_x11_keymap_key_is_modifier (keymap, event->key.hardware_keycode);
179
180 _gdk_x11_event_translate_keyboard_string (&event->key);
181
182 #ifdef G_ENABLE_DEBUG
183 if (GDK_DEBUG_CHECK (EVENTS))
184 {
185 g_message ("%s:\t\twindow: %ld key: %12s %d",
186 event->type == GDK_KEY_PRESS ? "key press " : "key release",
187 xevent->xkey.window,
188 event->key.keyval ? gdk_keyval_name (event->key.keyval) : "(none)",
189 event->key.keyval);
190
191 if (event->key.length > 0)
192 g_message ("\t\tlength: %4d string: \"%s\"",
193 event->key.length, event->key.string);
194 }
195 #endif /* G_ENABLE_DEBUG */
196 return;
197 }
198
199 #ifdef G_ENABLE_DEBUG
200 static const char notify_modes[][19] = {
201 "NotifyNormal",
202 "NotifyGrab",
203 "NotifyUngrab",
204 "NotifyWhileGrabbed"
205 };
206
207 static const char notify_details[][23] = {
208 "NotifyAncestor",
209 "NotifyVirtual",
210 "NotifyInferior",
211 "NotifyNonlinear",
212 "NotifyNonlinearVirtual",
213 "NotifyPointer",
214 "NotifyPointerRoot",
215 "NotifyDetailNone"
216 };
217 #endif
218
219 static void
220 set_user_time (GdkWindow *window,
221 GdkEvent *event)
222 {
223 g_return_if_fail (event != NULL);
224
225 window = gdk_window_get_toplevel (event->any.window);
226 g_return_if_fail (GDK_IS_WINDOW (window));
227
228 /* If an event doesn't have a valid timestamp, we shouldn't use it
229 * to update the latest user interaction time.
230 */
231 if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
232 gdk_x11_window_set_user_time (gdk_window_get_toplevel (window),
233 gdk_event_get_time (event));
234 }
235
236 static gboolean
237 set_screen_from_root (GdkDisplay *display,
238 GdkEvent *event,
239 Window xrootwin)
240 {
241 GdkScreen *screen;
242
243 screen = _gdk_x11_display_screen_for_xrootwin (display, xrootwin);
244
245 if (screen)
246 {
247 gdk_event_set_screen (event, screen);
248
249 return TRUE;
250 }
251
252 return FALSE;
253 }
254
255 static GdkCrossingMode
256 translate_crossing_mode (int mode)
257 {
258 switch (mode)
259 {
260 case NotifyNormal:
261 return GDK_CROSSING_NORMAL;
262 case NotifyGrab:
263 return GDK_CROSSING_GRAB;
264 case NotifyUngrab:
265 return GDK_CROSSING_UNGRAB;
266 default:
267 g_assert_not_reached ();
268 }
269 }
270
271 static GdkNotifyType
272 translate_notify_type (int detail)
273 {
274 switch (detail)
275 {
276 case NotifyInferior:
277 return GDK_NOTIFY_INFERIOR;
278 case NotifyAncestor:
279 return GDK_NOTIFY_ANCESTOR;
280 case NotifyVirtual:
281 return GDK_NOTIFY_VIRTUAL;
282 case NotifyNonlinear:
283 return GDK_NOTIFY_NONLINEAR;
284 case NotifyNonlinearVirtual:
285 return GDK_NOTIFY_NONLINEAR_VIRTUAL;
286 default:
287 g_assert_not_reached ();
288 }
289 }
290
291 static gboolean
292 is_parent_of (GdkWindow *parent,
293 GdkWindow *child)
294 {
295 GdkWindow *w;
296
297 w = child;
298 while (w != NULL)
299 {
300 if (w == parent)
301 return TRUE;
302
303 w = gdk_window_get_parent (w);
304 }
305
306 return FALSE;
307 }
308
309 static GdkWindow *
310 get_event_window (GdkEventTranslator *translator,
311 XEvent *xevent)
312 {
313 GdkDeviceManager *device_manager;
314 GdkDisplay *display;
315 GdkWindow *window;
316
317 device_manager = GDK_DEVICE_MANAGER (translator);
318 display = gdk_device_manager_get_display (device_manager);
319 window = gdk_x11_window_lookup_for_display (display, xevent->xany.window);
320
321 /* Apply keyboard grabs to non-native windows */
322 if (xevent->type == KeyPress || xevent->type == KeyRelease)
323 {
324 GdkDeviceGrabInfo *info;
325 gulong serial;
326
327 serial = _gdk_display_get_next_serial (display);
328 info = _gdk_display_has_device_grab (display,
329 GDK_X11_DEVICE_MANAGER_CORE (device_manager)->core_keyboard,
330 serial);
331 if (info &&
332 (!is_parent_of (info->window, window) ||
333 !info->owner_events))
334 {
335 /* Report key event against grab window */
336 window = info->window;
337 }
338 }
339
340 return window;
341 }
342
343 static gboolean
344 gdk_x11_device_manager_core_translate_event (GdkEventTranslator *translator,
345 GdkDisplay *display,
346 GdkEvent *event,
347 XEvent *xevent)
348 {
349 GdkWindowImplX11 *impl;
350 GdkX11DeviceManagerCore *device_manager;
351 GdkWindow *window;
352 gboolean return_val;
353 int scale;
354 GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
355
356 device_manager = GDK_X11_DEVICE_MANAGER_CORE (translator);
357
358 window = get_event_window (translator, xevent);
359
360 scale = 1;
361 if (window)
362 {
363 if (GDK_WINDOW_DESTROYED (window) || !GDK_IS_WINDOW (window))
364 return FALSE;
365
366 g_object_ref (window);
367 impl = GDK_WINDOW_IMPL_X11 (window->impl);
368 scale = impl->window_scale;
369 }
370
371 event->any.window = window;
372 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
373
374 if (window && GDK_WINDOW_DESTROYED (window))
375 {
376 if (xevent->type != DestroyNotify)
377 {
378 return_val = FALSE;
379 goto done;
380 }
381 }
382
383 if (window &&
384 (xevent->type == MotionNotify ||
385 xevent->type == ButtonRelease))
386 {
387 if (_gdk_x11_moveresize_handle_event (xevent))
388 {
389 return_val = FALSE;
390 goto done;
391 }
392 }
393
394 /* We do a "manual" conversion of the XEvent to a
395 * GdkEvent. The structures are mostly the same so
396 * the conversion is fairly straightforward. We also
397 * optionally print debugging info regarding events
398 * received.
399 */
400
401 return_val = TRUE;
402
403 switch (xevent->type)
404 {
405 case KeyPress:
406 if (window == NULL)
407 {
408 return_val = FALSE;
409 break;
410 }
411 translate_key_event (display, device_manager, event, xevent);
412 set_user_time (window, event);
413 break;
414
415 case KeyRelease:
416 if (window == NULL)
417 {
418 return_val = FALSE;
419 break;
420 }
421
422 /* Emulate detectable auto-repeat by checking to see
423 * if the next event is a key press with the same
424 * keycode and timestamp, and if so, ignoring the event.
425 */
426
427 if (!display_x11->have_xkb_autorepeat && XPending (xevent->xkey.display))
428 {
429 XEvent next_event;
430
431 XPeekEvent (xevent->xkey.display, &next_event);
432
433 if (next_event.type == KeyPress &&
434 next_event.xkey.keycode == xevent->xkey.keycode &&
435 next_event.xkey.time == xevent->xkey.time)
436 {
437 return_val = FALSE;
438 break;
439 }
440 }
441
442 translate_key_event (display, device_manager, event, xevent);
443 break;
444
445 case ButtonPress:
446 GDK_NOTE (EVENTS,
447 g_message ("button press:\t\twindow: %ld x,y: %d %d button: %d",
448 xevent->xbutton.window,
449 xevent->xbutton.x, xevent->xbutton.y,
450 xevent->xbutton.button));
451
452 if (window == NULL)
453 {
454 return_val = FALSE;
455 break;
456 }
457
458 /* If we get a ButtonPress event where the button is 4 or 5,
459 it's a Scroll event */
460 switch (xevent->xbutton.button)
461 {
462 case 4: /* up */
463 case 5: /* down */
464 case 6: /* left */
465 case 7: /* right */
466 event->scroll.type = GDK_SCROLL;
467
468 if (xevent->xbutton.button == 4)
469 event->scroll.direction = GDK_SCROLL_UP;
470 else if (xevent->xbutton.button == 5)
471 event->scroll.direction = GDK_SCROLL_DOWN;
472 else if (xevent->xbutton.button == 6)
473 event->scroll.direction = GDK_SCROLL_LEFT;
474 else
475 event->scroll.direction = GDK_SCROLL_RIGHT;
476
477 event->scroll.window = window;
478 event->scroll.time = xevent->xbutton.time;
479 event->scroll.x = (gdouble) xevent->xbutton.x / scale;
480 event->scroll.y = (gdouble) xevent->xbutton.y / scale;
481 event->scroll.x_root = (gdouble) xevent->xbutton.x_root / scale;
482 event->scroll.y_root = (gdouble) xevent->xbutton.y_root / scale;
483 event->scroll.state = (GdkModifierType) xevent->xbutton.state;
484 event->scroll.device = device_manager->core_pointer;
485
486 event->scroll.delta_x = 0;
487 event->scroll.delta_y = 0;
488
489 if (!set_screen_from_root (display, event, xevent->xbutton.root))
490 {
491 return_val = FALSE;
492 break;
493 }
494
495 break;
496
497 default:
498 event->button.type = GDK_BUTTON_PRESS;
499 event->button.window = window;
500 event->button.time = xevent->xbutton.time;
501 event->button.x = (gdouble) xevent->xbutton.x / scale;
502 event->button.y = (gdouble) xevent->xbutton.y / scale;
503 event->button.x_root = (gdouble) xevent->xbutton.x_root / scale;
504 event->button.y_root = (gdouble) xevent->xbutton.y_root / scale;
505 event->button.axes = NULL;
506 event->button.state = (GdkModifierType) xevent->xbutton.state;
507 event->button.button = xevent->xbutton.button;
508 event->button.device = device_manager->core_pointer;
509
510 if (!set_screen_from_root (display, event, xevent->xbutton.root))
511 return_val = FALSE;
512
513 break;
514 }
515
516 set_user_time (window, event);
517
518 break;
519
520 case ButtonRelease:
521 GDK_NOTE (EVENTS,
522 g_message ("button release:\twindow: %ld x,y: %d %d button: %d",
523 xevent->xbutton.window,
524 xevent->xbutton.x, xevent->xbutton.y,
525 xevent->xbutton.button));
526
527 if (window == NULL)
528 {
529 return_val = FALSE;
530 break;
531 }
532
533 /* We treat button presses as scroll wheel events, so ignore the release */
534 if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 ||
535 xevent->xbutton.button == 6 || xevent->xbutton.button == 7)
536 {
537 return_val = FALSE;
538 break;
539 }
540
541 event->button.type = GDK_BUTTON_RELEASE;
542 event->button.window = window;
543 event->button.time = xevent->xbutton.time;
544 event->button.x = (gdouble) xevent->xbutton.x / scale;
545 event->button.y = (gdouble) xevent->xbutton.y / scale;
546 event->button.x_root = (gdouble) xevent->xbutton.x_root / scale;
547 event->button.y_root = (gdouble) xevent->xbutton.y_root / scale;
548 event->button.axes = NULL;
549 event->button.state = (GdkModifierType) xevent->xbutton.state;
550 event->button.button = xevent->xbutton.button;
551 event->button.device = device_manager->core_pointer;
552
553 if (!set_screen_from_root (display, event, xevent->xbutton.root))
554 return_val = FALSE;
555
556 break;
557
558 case MotionNotify:
559 GDK_NOTE (EVENTS,
560 g_message ("motion notify:\t\twindow: %ld x,y: %d %d hint: %s",
561 xevent->xmotion.window,
562 xevent->xmotion.x, xevent->xmotion.y,
563 (xevent->xmotion.is_hint) ? "true" : "false"));
564
565 if (window == NULL)
566 {
567 return_val = FALSE;
568 break;
569 }
570
571 event->motion.type = GDK_MOTION_NOTIFY;
572 event->motion.window = window;
573 event->motion.time = xevent->xmotion.time;
574 event->motion.x = (gdouble) xevent->xmotion.x / scale;
575 event->motion.y = (gdouble) xevent->xmotion.y / scale;
576 event->motion.x_root = (gdouble) xevent->xmotion.x_root / scale;
577 event->motion.y_root = (gdouble) xevent->xmotion.y_root / scale;
578 event->motion.axes = NULL;
579 event->motion.state = (GdkModifierType) xevent->xmotion.state;
580 event->motion.is_hint = xevent->xmotion.is_hint;
581 event->motion.device = device_manager->core_pointer;
582
583 if (!set_screen_from_root (display, event, xevent->xbutton.root))
584 {
585 return_val = FALSE;
586 break;
587 }
588
589 break;
590
591 case EnterNotify:
592 GDK_NOTE (EVENTS,
593 g_message ("enter notify:\t\twindow: %ld detail: %d subwin: %ld",
594 xevent->xcrossing.window,
595 xevent->xcrossing.detail,
596 xevent->xcrossing.subwindow));
597
598 if (window == NULL)
599 {
600 return_val = FALSE;
601 break;
602 }
603
604 if (!set_screen_from_root (display, event, xevent->xbutton.root))
605 {
606 return_val = FALSE;
607 break;
608 }
609
610 event->crossing.type = GDK_ENTER_NOTIFY;
611 event->crossing.window = window;
612 gdk_event_set_device (event, device_manager->core_pointer);
613
614 /* If the subwindow field of the XEvent is non-NULL, then
615 * lookup the corresponding GdkWindow.
616 */
617 if (xevent->xcrossing.subwindow != None)
618 event->crossing.subwindow = gdk_x11_window_lookup_for_display (display, xevent->xcrossing.subwindow);
619 else
620 event->crossing.subwindow = NULL;
621
622 event->crossing.time = xevent->xcrossing.time;
623 event->crossing.x = (gdouble) xevent->xcrossing.x / scale;
624 event->crossing.y = (gdouble) xevent->xcrossing.y / scale;
625 event->crossing.x_root = (gdouble) xevent->xcrossing.x_root / scale;
626 event->crossing.y_root = (gdouble) xevent->xcrossing.y_root / scale;
627
628 event->crossing.mode = translate_crossing_mode (xevent->xcrossing.mode);
629 event->crossing.detail = translate_notify_type (xevent->xcrossing.detail);
630
631 event->crossing.focus = xevent->xcrossing.focus;
632 event->crossing.state = xevent->xcrossing.state;
633
634 break;
635
636 case LeaveNotify:
637 GDK_NOTE (EVENTS,
638 g_message ("leave notify:\t\twindow: %ld detail: %d subwin: %ld",
639 xevent->xcrossing.window,
640 xevent->xcrossing.detail, xevent->xcrossing.subwindow));
641
642 if (window == NULL)
643 {
644 return_val = FALSE;
645 break;
646 }
647
648 if (!set_screen_from_root (display, event, xevent->xbutton.root))
649 {
650 return_val = FALSE;
651 break;
652 }
653
654 event->crossing.type = GDK_LEAVE_NOTIFY;
655 event->crossing.window = window;
656 gdk_event_set_device (event, device_manager->core_pointer);
657
658 /* If the subwindow field of the XEvent is non-NULL, then
659 * lookup the corresponding GdkWindow.
660 */
661 if (xevent->xcrossing.subwindow != None)
662 event->crossing.subwindow = gdk_x11_window_lookup_for_display (display, xevent->xcrossing.subwindow);
663 else
664 event->crossing.subwindow = NULL;
665
666 event->crossing.time = xevent->xcrossing.time;
667 event->crossing.x = (gdouble) xevent->xcrossing.x / scale;
668 event->crossing.y = (gdouble) xevent->xcrossing.y / scale;
669 event->crossing.x_root = (gdouble) xevent->xcrossing.x_root / scale;
670 event->crossing.y_root = (gdouble) xevent->xcrossing.y_root / scale;
671
672 event->crossing.mode = translate_crossing_mode (xevent->xcrossing.mode);
673 event->crossing.detail = translate_notify_type (xevent->xcrossing.detail);
674
675 event->crossing.focus = xevent->xcrossing.focus;
676 event->crossing.state = xevent->xcrossing.state;
677
678 break;
679
680 case FocusIn:
681 case FocusOut:
682 if (window)
683 _gdk_device_manager_core_handle_focus (window,
684 xevent->xfocus.window,
685 device_manager->core_keyboard,
686 NULL,
687 xevent->type == FocusIn,
688 xevent->xfocus.detail,
689 xevent->xfocus.mode);
690 return_val = FALSE;
691 break;
692
693 default:
694 return_val = FALSE;
695 }
696
697 done:
698 if (return_val)
699 {
700 if (event->any.window)
701 g_object_ref (event->any.window);
702
703 if (((event->any.type == GDK_ENTER_NOTIFY) ||
704 (event->any.type == GDK_LEAVE_NOTIFY)) &&
705 (event->crossing.subwindow != NULL))
706 g_object_ref (event->crossing.subwindow);
707 }
708 else
709 {
710 /* Mark this event as having no resources to be freed */
711 event->any.window = NULL;
712 event->any.type = GDK_NOTHING;
713 }
714
715 if (window)
716 g_object_unref (window);
717
718 return return_val;
719 }
720
721 static GList *
722 gdk_x11_device_manager_core_list_devices (GdkDeviceManager *device_manager,
723 GdkDeviceType type)
724 {
725 GdkX11DeviceManagerCore *device_manager_core;
726 GList *devices = NULL;
727
728 if (type == GDK_DEVICE_TYPE_MASTER)
729 {
730 device_manager_core = (GdkX11DeviceManagerCore *) device_manager;
731 devices = g_list_prepend (devices, device_manager_core->core_keyboard);
732 devices = g_list_prepend (devices, device_manager_core->core_pointer);
733 }
734
735 return devices;
736 }
737
738 static GdkDevice *
739 gdk_x11_device_manager_core_get_client_pointer (GdkDeviceManager *device_manager)
740 {
741 GdkX11DeviceManagerCore *device_manager_core;
742
743 device_manager_core = (GdkX11DeviceManagerCore *) device_manager;
744 return device_manager_core->core_pointer;
745 }
746
747 void
748 _gdk_x11_event_translate_keyboard_string (GdkEventKey *event)
749 {
750 gunichar c = 0;
751 gchar buf[7];
752
753 /* Fill in event->string crudely, since various programs
754 * depend on it.
755 */
756 event->string = NULL;
757
758 if (event->keyval != GDK_KEY_VoidSymbol)
759 c = gdk_keyval_to_unicode (event->keyval);
760
761 if (c)
762 {
763 gsize bytes_written;
764 gint len;
765
766 /* Apply the control key - Taken from Xlib
767 */
768 if (event->state & GDK_CONTROL_MASK)
769 {
770 if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
771 else if (c == '2')
772 {
773 event->string = g_memdup ("\0\0", 2);
774 event->length = 1;
775 buf[0] = '\0';
776 return;
777 }
778 else if (c >= '3' && c <= '7') c -= ('3' - '\033');
779 else if (c == '8') c = '\177';
780 else if (c == '/') c = '_' & 0x1F;
781 }
782
783 len = g_unichar_to_utf8 (c, buf);
784 buf[len] = '\0';
785
786 event->string = g_locale_from_utf8 (buf, len,
787 NULL, &bytes_written,
788 NULL);
789 if (event->string)
790 event->length = bytes_written;
791 }
792 else if (event->keyval == GDK_KEY_Escape)
793 {
794 event->length = 1;
795 event->string = g_strdup ("\033");
796 }
797 else if (event->keyval == GDK_KEY_Return ||
798 event->keyval == GDK_KEY_KP_Enter)
799 {
800 event->length = 1;
801 event->string = g_strdup ("\r");
802 }
803
804 if (!event->string)
805 {
806 event->length = 0;
807 event->string = g_strdup ("");
808 }
809 }
810
811 /* We only care about focus events that indicate that _this_
812 * window (not a ancestor or child) got or lost the focus
813 */
814 void
815 _gdk_device_manager_core_handle_focus (GdkWindow *window,
816 Window original,
817 GdkDevice *device,
818 GdkDevice *source_device,
819 gboolean focus_in,
820 int detail,
821 int mode)
822 {
823 GdkToplevelX11 *toplevel;
824 GdkX11Screen *x11_screen;
825 gboolean had_focus;
826
827 g_return_if_fail (GDK_IS_WINDOW (window));
828 g_return_if_fail (GDK_IS_DEVICE (device));
829 g_return_if_fail (source_device == NULL || GDK_IS_DEVICE (source_device));
830
831 GDK_NOTE (EVENTS,
832 g_message ("focus out:\t\twindow: %ld, detail: %s, mode: %s",
833 GDK_WINDOW_XID (window),
834 notify_details[detail],
835 notify_modes[mode]));
836
837 toplevel = _gdk_x11_window_get_toplevel (window);
838
839 if (!toplevel)
840 return;
841
842 if (toplevel->focus_window == original)
843 return;
844
845 had_focus = HAS_FOCUS (toplevel);
846 x11_screen = GDK_X11_SCREEN (gdk_window_get_screen (window));
847
848 switch (detail)
849 {
850 case NotifyAncestor:
851 case NotifyVirtual:
852 /* When the focus moves from an ancestor of the window to
853 * the window or a descendent of the window, *and* the
854 * pointer is inside the window, then we were previously
855 * receiving keystroke events in the has_pointer_focus
856 * case and are now receiving them in the
857 * has_focus_window case.
858 */
859 if (toplevel->has_pointer &&
860 !x11_screen->wmspec_check_window &&
861 mode != NotifyGrab &&
862 #ifdef XINPUT_2
863 mode != XINotifyPassiveGrab &&
864 mode != XINotifyPassiveUngrab &&
865 #endif /* XINPUT_2 */
866 mode != NotifyUngrab)
867 toplevel->has_pointer_focus = (focus_in) ? FALSE : TRUE;
868
869 /* fall through */
870 case NotifyNonlinear:
871 case NotifyNonlinearVirtual:
872 if (mode != NotifyGrab &&
873 #ifdef XINPUT_2
874 mode != XINotifyPassiveGrab &&
875 mode != XINotifyPassiveUngrab &&
876 #endif /* XINPUT_2 */
877 mode != NotifyUngrab)
878 toplevel->has_focus_window = (focus_in) ? TRUE : FALSE;
879 /* We pretend that the focus moves to the grab
880 * window, so we pay attention to NotifyGrab
881 * NotifyUngrab, and ignore NotifyWhileGrabbed
882 */
883 if (mode != NotifyWhileGrabbed)
884 toplevel->has_focus = (focus_in) ? TRUE : FALSE;
885 break;
886 case NotifyPointer:
887 /* The X server sends NotifyPointer/NotifyGrab,
888 * but the pointer focus is ignored while a
889 * grab is in effect
890 */
891 if (!x11_screen->wmspec_check_window &&
892 mode != NotifyGrab &&
893 #ifdef XINPUT_2
894 mode != XINotifyPassiveGrab &&
895 mode != XINotifyPassiveUngrab &&
896 #endif /* XINPUT_2 */
897 mode != NotifyUngrab)
898 toplevel->has_pointer_focus = (focus_in) ? TRUE : FALSE;
899 break;
900 case NotifyInferior:
901 case NotifyPointerRoot:
902 case NotifyDetailNone:
903 default:
904 break;
905 }
906
907 if (HAS_FOCUS (toplevel) != had_focus)
908 {
909 GdkEvent *event;
910
911 event = gdk_event_new (GDK_FOCUS_CHANGE);
912 event->focus_change.window = g_object_ref (window);
913 event->focus_change.send_event = FALSE;
914 event->focus_change.in = focus_in;
915 gdk_event_set_device (event, device);
916 if (source_device)
917 gdk_event_set_source_device (event, source_device);
918
919 gdk_event_put (event);
920 gdk_event_free (event);
921 }
922 }
923