Debian Codegraph @

gdk/x11/gdkevents-x11.c

raw symbols SCIP
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-2007 Peter Mattis, Spencer Kimball,
3 * Josh MacDonald, Ryan Lortie
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21 /*
22 * Modified by the GTK+ Team and others 1997-2007. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 */
27
28 #include "config.h"
29
30 #include "gdk.h"
31 #include "gdkprivate-x11.h"
32 #include "gdkinternals.h"
33 #include "gdkx.h"
34 #include "gdkscreen-x11.h"
35 #include "gdkdisplay-x11.h"
36 #include "gdkasync.h"
37
38 #include "gdkkeysyms.h"
39
40 #include "xsettings-client.h"
41
42 #include <string.h>
43
44 #include "gdkinputprivate.h"
45 #include "gdksettings.c"
46 #include "gdkalias.h"
47
48
49 #ifdef HAVE_XKB
50 #include <X11/XKBlib.h>
51 #endif
52
53 #ifdef HAVE_XSYNC
54 #include <X11/extensions/sync.h>
55 #endif
56
57 #ifdef HAVE_XFIXES
58 #include <X11/extensions/Xfixes.h>
59 #endif
60
61 #ifdef HAVE_RANDR
62 #include <X11/extensions/Xrandr.h>
63 #endif
64
65 #include <X11/Xatom.h>
66
67 typedef struct _GdkIOClosure GdkIOClosure;
68 typedef struct _GdkDisplaySource GdkDisplaySource;
69 typedef struct _GdkEventTypeX11 GdkEventTypeX11;
70
71 struct _GdkIOClosure
72 {
73 GdkInputFunction function;
74 GdkInputCondition condition;
75 GDestroyNotify notify;
76 gpointer data;
77 };
78
79 struct _GdkDisplaySource
80 {
81 GSource source;
82
83 GdkDisplay *display;
84 GPollFD event_poll_fd;
85 };
86
87 struct _GdkEventTypeX11
88 {
89 gint base;
90 gint n_events;
91 };
92
93 /*
94 * Private function declarations
95 */
96
97 static gint gdk_event_apply_filters (XEvent *xevent,
98 GdkEvent *event,
99 GdkWindow *window);
100 static gboolean gdk_event_translate (GdkDisplay *display,
101 GdkEvent *event,
102 XEvent *xevent,
103 gboolean return_exposes);
104
105 static gboolean gdk_event_prepare (GSource *source,
106 gint *timeout);
107 static gboolean gdk_event_check (GSource *source);
108 static gboolean gdk_event_dispatch (GSource *source,
109 GSourceFunc callback,
110 gpointer user_data);
111
112 static GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
113 GdkEvent *event,
114 gpointer data);
115
116 static GSource *gdk_display_source_new (GdkDisplay *display);
117 static gboolean gdk_check_xpending (GdkDisplay *display);
118
119 static Bool gdk_xsettings_watch_cb (Window window,
120 Bool is_start,
121 long mask,
122 void *cb_data);
123 static void gdk_xsettings_notify_cb (const char *name,
124 XSettingsAction action,
125 XSettingsSetting *setting,
126 void *data);
127
128 /* Private variable declarations
129 */
130
131 static GList *display_sources;
132
133 static GSourceFuncs event_funcs = {
134 gdk_event_prepare,
135 gdk_event_check,
136 gdk_event_dispatch,
137 NULL
138 };
139
140 static GSource *
141 gdk_display_source_new (GdkDisplay *display)
142 {
143 GSource *source = g_source_new (&event_funcs, sizeof (GdkDisplaySource));
144 GdkDisplaySource *display_source = (GdkDisplaySource *)source;
145 char *name;
146
147 name = g_strdup_printf ("GDK X11 Event source (%s)",
148 gdk_display_get_name (display));
149 g_source_set_name (source, name);
150 g_free (name);
151 display_source->display = display;
152
153 return source;
154 }
155
156 static gboolean
157 gdk_check_xpending (GdkDisplay *display)
158 {
159 return XPending (GDK_DISPLAY_XDISPLAY (display));
160 }
161
162 /*********************************************
163 * Functions for maintaining the event queue *
164 *********************************************/
165
166 static void
167 refcounted_grab_server (Display *xdisplay)
168 {
169 GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);
170
171 gdk_x11_display_grab (display);
172 }
173
174 static void
175 refcounted_ungrab_server (Display *xdisplay)
176 {
177 GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);
178
179 gdk_x11_display_ungrab (display);
180 }
181
182 void
183 _gdk_x11_events_init_screen (GdkScreen *screen)
184 {
185 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
186
187 /* Keep a flag to avoid extra notifies that we don't need
188 */
189 screen_x11->xsettings_in_init = TRUE;
190 screen_x11->xsettings_client = xsettings_client_new_with_grab_funcs (screen_x11->xdisplay,
191 screen_x11->screen_num,
192 gdk_xsettings_notify_cb,
193 gdk_xsettings_watch_cb,
194 screen,
195 refcounted_grab_server,
196 refcounted_ungrab_server);
197 screen_x11->xsettings_in_init = FALSE;
198 }
199
200 void
201 _gdk_x11_events_uninit_screen (GdkScreen *screen)
202 {
203 GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
204
205 if (screen_x11->xsettings_client)
206 {
207 xsettings_client_destroy (screen_x11->xsettings_client);
208 screen_x11->xsettings_client = NULL;
209 }
210 }
211
212 void
213 _gdk_events_init (GdkDisplay *display)
214 {
215 GSource *source;
216 GdkDisplaySource *display_source;
217 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
218
219 int connection_number = ConnectionNumber (display_x11->xdisplay);
220 GDK_NOTE (MISC, g_message ("connection number: %d", connection_number));
221
222
223 source = display_x11->event_source = gdk_display_source_new (display);
224 display_source = (GdkDisplaySource*) source;
225 g_source_set_priority (source, GDK_PRIORITY_EVENTS);
226
227 display_source->event_poll_fd.fd = connection_number;
228 display_source->event_poll_fd.events = G_IO_IN;
229
230 g_source_add_poll (source, &display_source->event_poll_fd);
231 g_source_set_can_recurse (source, TRUE);
232 g_source_attach (source, NULL);
233
234 display_sources = g_list_prepend (display_sources,display_source);
235
236 gdk_display_add_client_message_filter (display,
237 gdk_atom_intern_static_string ("WM_PROTOCOLS"),
238 gdk_wm_protocols_filter,
239 NULL);
240 }
241
242 void
243 _gdk_events_uninit (GdkDisplay *display)
244 {
245 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
246
247 if (display_x11->event_source)
248 {
249 display_sources = g_list_remove (display_sources,
250 display_x11->event_source);
251 g_source_destroy (display_x11->event_source);
252 g_source_unref (display_x11->event_source);
253 display_x11->event_source = NULL;
254 }
255 }
256
257 /**
258 * gdk_events_pending:
259 *
260 * Checks if any events are ready to be processed for any display.
261 *
262 * Return value: %TRUE if any events are pending.
263 **/
264 gboolean
265 gdk_events_pending (void)
266 {
267 GList *tmp_list;
268
269 for (tmp_list = display_sources; tmp_list; tmp_list = tmp_list->next)
270 {
271 GdkDisplaySource *tmp_source = tmp_list->data;
272 GdkDisplay *display = tmp_source->display;
273
274 if (_gdk_event_queue_find_first (display))
275 return TRUE;
276 }
277
278 for (tmp_list = display_sources; tmp_list; tmp_list = tmp_list->next)
279 {
280 GdkDisplaySource *tmp_source = tmp_list->data;
281 GdkDisplay *display = tmp_source->display;
282
283 if (gdk_check_xpending (display))
284 return TRUE;
285 }
286
287 return FALSE;
288 }
289
290 static Bool
291 graphics_expose_predicate (Display *display,
292 XEvent *xevent,
293 XPointer arg)
294 {
295 if (xevent->xany.window == GDK_DRAWABLE_XID ((GdkDrawable *)arg) &&
296 (xevent->xany.type == GraphicsExpose ||
297 xevent->xany.type == NoExpose))
298 return True;
299 else
300 return False;
301 }
302
303 /**
304 * gdk_event_get_graphics_expose:
305 * @window: the #GdkWindow to wait for the events for.
306 *
307 * Waits for a GraphicsExpose or NoExpose event from the X server.
308 * This is used in the #GtkText and #GtkCList widgets in GTK+ to make sure any
309 * GraphicsExpose events are handled before the widget is scrolled.
310 *
311 * Return value: a #GdkEventExpose if a GraphicsExpose was received, or %NULL if a
312 * NoExpose event was received.
313 *
314 * Deprecated: 2.18:
315 **/
316 GdkEvent*
317 gdk_event_get_graphics_expose (GdkWindow *window)
318 {
319 XEvent xevent;
320 GdkEvent *event;
321
322 g_return_val_if_fail (window != NULL, NULL);
323
324 XIfEvent (GDK_WINDOW_XDISPLAY (window), &xevent,
325 graphics_expose_predicate, (XPointer) window);
326
327 if (xevent.xany.type == GraphicsExpose)
328 {
329 event = gdk_event_new (GDK_NOTHING);
330
331 if (gdk_event_translate (GDK_WINDOW_DISPLAY (window), event,
332 &xevent, TRUE))
333 return event;
334 else
335 gdk_event_free (event);
336 }
337
338 return NULL;
339 }
340
341 static gint
342 gdk_event_apply_filters (XEvent *xevent,
343 GdkEvent *event,
344 GdkWindow *window)
345 {
346 GList *tmp_list;
347 GdkFilterReturn result;
348
349 if (window == NULL)
350 tmp_list = _gdk_default_filters;
351 else
352 {
353 GdkWindowObject *window_private;
354 window_private = (GdkWindowObject *) window;
355 tmp_list = window_private->filters;
356 }
357
358
359 while (tmp_list)
360 {
361 GdkEventFilter *filter = (GdkEventFilter*) tmp_list->data;
362 GList *node;
363
364 if ((filter->flags & GDK_EVENT_FILTER_REMOVED) != 0)
365 {
366 tmp_list = tmp_list->next;
367 continue;
368 }
369
370 filter->ref_count++;
371 result = filter->function (xevent, event, filter->data);
372
373 /* Protect against unreffing the filter mutating the list */
374 node = tmp_list->next;
375
376 _gdk_event_filter_unref (window, filter);
377
378 tmp_list = node;
379
380 if (result != GDK_FILTER_CONTINUE)
381 return result;
382 }
383
384 return GDK_FILTER_CONTINUE;
385 }
386
387 /**
388 * gdk_display_add_client_message_filter:
389 * @display: a #GdkDisplay for which this message filter applies
390 * @message_type: the type of ClientMessage events to receive.
391 * This will be checked against the @message_type field
392 * of the XClientMessage event struct.
393 * @func: the function to call to process the event.
394 * @data: user data to pass to @func.
395 *
396 * Adds a filter to be called when X ClientMessage events are received.
397 * See gdk_window_add_filter() if you are interested in filtering other
398 * types of events.
399 *
400 * Since: 2.2
401 **/
402 void
403 gdk_display_add_client_message_filter (GdkDisplay *display,
404 GdkAtom message_type,
405 GdkFilterFunc func,
406 gpointer data)
407 {
408 GdkClientFilter *filter;
409 g_return_if_fail (GDK_IS_DISPLAY (display));
410 filter = g_new (GdkClientFilter, 1);
411
412 filter->type = message_type;
413 filter->function = func;
414 filter->data = data;
415
416 GDK_DISPLAY_X11(display)->client_filters =
417 g_list_append (GDK_DISPLAY_X11 (display)->client_filters,
418 filter);
419 }
420
421 /**
422 * gdk_add_client_message_filter:
423 * @message_type: the type of ClientMessage events to receive. This will be
424 * checked against the <structfield>message_type</structfield> field of the
425 * XClientMessage event struct.
426 * @func: the function to call to process the event.
427 * @data: user data to pass to @func.
428 *
429 * Adds a filter to the default display to be called when X ClientMessage events
430 * are received. See gdk_display_add_client_message_filter().
431 **/
432 void
433 gdk_add_client_message_filter (GdkAtom message_type,
434 GdkFilterFunc func,
435 gpointer data)
436 {
437 gdk_display_add_client_message_filter (gdk_display_get_default (),
438 message_type, func, data);
439 }
440
441 static void
442 do_net_wm_state_changes (GdkWindow *window)
443 {
444 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
445 GdkWindowState old_state;
446
447 if (GDK_WINDOW_DESTROYED (window) ||
448 gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
449 return;
450
451 old_state = gdk_window_get_state (window);
452
453 /* For found_sticky to remain TRUE, we have to also be on desktop
454 * 0xFFFFFFFF
455 */
456 if (old_state & GDK_WINDOW_STATE_STICKY)
457 {
458 if (!(toplevel->have_sticky && toplevel->on_all_desktops))
459 gdk_synthesize_window_state (window,
460 GDK_WINDOW_STATE_STICKY,
461 0);
462 }
463 else
464 {
465 if (toplevel->have_sticky && toplevel->on_all_desktops)
466 gdk_synthesize_window_state (window,
467 0,
468 GDK_WINDOW_STATE_STICKY);
469 }
470
471 if (old_state & GDK_WINDOW_STATE_FULLSCREEN)
472 {
473 if (!toplevel->have_fullscreen)
474 gdk_synthesize_window_state (window,
475 GDK_WINDOW_STATE_FULLSCREEN,
476 0);
477 }
478 else
479 {
480 if (toplevel->have_fullscreen)
481 gdk_synthesize_window_state (window,
482 0,
483 GDK_WINDOW_STATE_FULLSCREEN);
484 }
485
486 /* Our "maximized" means both vertical and horizontal; if only one,
487 * we don't expose that via GDK
488 */
489 if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
490 {
491 if (!(toplevel->have_maxvert && toplevel->have_maxhorz))
492 gdk_synthesize_window_state (window,
493 GDK_WINDOW_STATE_MAXIMIZED,
494 0);
495 }
496 else
497 {
498 if (toplevel->have_maxvert && toplevel->have_maxhorz)
499 gdk_synthesize_window_state (window,
500 0,
501 GDK_WINDOW_STATE_MAXIMIZED);
502 }
503
504 if (old_state & GDK_WINDOW_STATE_ICONIFIED)
505 {
506 if (!toplevel->have_hidden)
507 gdk_synthesize_window_state (window,
508 GDK_WINDOW_STATE_ICONIFIED,
509 0);
510 }
511 else
512 {
513 if (toplevel->have_hidden)
514 gdk_synthesize_window_state (window,
515 0,
516 GDK_WINDOW_STATE_ICONIFIED);
517 }
518 }
519
520 static void
521 gdk_check_wm_desktop_changed (GdkWindow *window)
522 {
523 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
524 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
525
526 Atom type;
527 gint format;
528 gulong nitems;
529 gulong bytes_after;
530 guchar *data;
531 gulong *desktop;
532
533 type = None;
534 gdk_error_trap_push ();
535 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
536 GDK_WINDOW_XID (window),
537 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
538 0, G_MAXLONG, False, XA_CARDINAL, &type,
539 &format, &nitems,
540 &bytes_after, &data);
541 gdk_error_trap_pop ();
542
543 if (type != None)
544 {
545 desktop = (gulong *)data;
546 toplevel->on_all_desktops = ((*desktop & 0xFFFFFFFF) == 0xFFFFFFFF);
547 XFree (desktop);
548 }
549 else
550 toplevel->on_all_desktops = FALSE;
551
552 do_net_wm_state_changes (window);
553 }
554
555 static void
556 gdk_check_wm_state_changed (GdkWindow *window)
557 {
558 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
559 GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
560
561 Atom type;
562 gint format;
563 gulong nitems;
564 gulong bytes_after;
565 guchar *data;
566 Atom *atoms = NULL;
567 gulong i;
568
569 gboolean had_sticky = toplevel->have_sticky;
570
571 toplevel->have_sticky = FALSE;
572 toplevel->have_maxvert = FALSE;
573 toplevel->have_maxhorz = FALSE;
574 toplevel->have_fullscreen = FALSE;
575 toplevel->have_hidden = FALSE;
576
577 type = None;
578 gdk_error_trap_push ();
579 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
580 gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
581 0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
582 &bytes_after, &data);
583 gdk_error_trap_pop ();
584
585 if (type != None)
586 {
587 Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
588 Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
589 Atom maxhorz_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
590 Atom fullscreen_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FULLSCREEN");
591 Atom hidden_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_HIDDEN");
592
593 atoms = (Atom *)data;
594
595 i = 0;
596 while (i < nitems)
597 {
598 if (atoms[i] == sticky_atom)
599 toplevel->have_sticky = TRUE;
600 else if (atoms[i] == maxvert_atom)
601 toplevel->have_maxvert = TRUE;
602 else if (atoms[i] == maxhorz_atom)
603 toplevel->have_maxhorz = TRUE;
604 else if (atoms[i] == fullscreen_atom)
605 toplevel->have_fullscreen = TRUE;
606 else if (atoms[i] == hidden_atom)
607 toplevel->have_hidden = TRUE;
608
609 ++i;
610 }
611
612 XFree (atoms);
613 }
614
615 /* When have_sticky is turned on, we have to check the DESKTOP property
616 * as well.
617 */
618 if (toplevel->have_sticky && !had_sticky)
619 gdk_check_wm_desktop_changed (window);
620 else
621 do_net_wm_state_changes (window);
622 }
623
624 #define HAS_FOCUS(toplevel) \
625 ((toplevel)->has_focus || (toplevel)->has_pointer_focus)
626
627 static void
628 generate_focus_event (GdkWindow *window,
629 gboolean in)
630 {
631 GdkEvent event;
632
633 event.type = GDK_FOCUS_CHANGE;
634 event.focus_change.window = window;
635 event.focus_change.send_event = FALSE;
636 event.focus_change.in = in;
637
638 gdk_event_put (&event);
639 }
640
641 static gboolean
642 set_screen_from_root (GdkDisplay *display,
643 GdkEvent *event,
644 Window xrootwin)
645 {
646 GdkScreen *screen;
647
648 screen = _gdk_x11_display_screen_for_xrootwin (display, xrootwin);
649
650 if (screen)
651 {
652 gdk_event_set_screen (event, screen);
653
654 return TRUE;
655 }
656
657 return FALSE;
658 }
659
660 static void
661 translate_key_event (GdkDisplay *display,
662 GdkEvent *event,
663 XEvent *xevent)
664 {
665 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
666 gunichar c = 0;
667 gchar buf[7];
668 GdkModifierType consumed, state;
669
670 event->key.type = xevent->xany.type == KeyPress ? GDK_KEY_PRESS : GDK_KEY_RELEASE;
671 event->key.time = xevent->xkey.time;
672
673 event->key.state = (GdkModifierType) xevent->xkey.state;
674 event->key.group = _gdk_x11_get_group_for_state (display, xevent->xkey.state);
675 event->key.hardware_keycode = xevent->xkey.keycode;
676
677 event->key.keyval = GDK_VoidSymbol;
678
679 gdk_keymap_translate_keyboard_state (keymap,
680 event->key.hardware_keycode,
681 event->key.state,
682 event->key.group,
683 &event->key.keyval,
684 NULL, NULL, &consumed);
685 state = event->key.state & ~consumed;
686 _gdk_keymap_add_virtual_modifiers_compat (keymap, &state);
687 event->key.state |= state;
688
689 event->key.is_modifier = _gdk_keymap_key_is_modifier (keymap, event->key.hardware_keycode);
690
691 /* Fill in event->string crudely, since various programs
692 * depend on it.
693 */
694 event->key.string = NULL;
695
696 if (event->key.keyval != GDK_VoidSymbol)
697 c = gdk_keyval_to_unicode (event->key.keyval);
698
699 if (c)
700 {
701 gsize bytes_written;
702 gint len;
703
704 /* Apply the control key - Taken from Xlib
705 */
706 if (event->key.state & GDK_CONTROL_MASK)
707 {
708 if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
709 else if (c == '2')
710 {
711 event->key.string = g_memdup ("\0\0", 2);
712 event->key.length = 1;
713 buf[0] = '\0';
714 goto out;
715 }
716 else if (c >= '3' && c <= '7') c -= ('3' - '\033');
717 else if (c == '8') c = '\177';
718 else if (c == '/') c = '_' & 0x1F;
719 }
720
721 len = g_unichar_to_utf8 (c, buf);
722 buf[len] = '\0';
723
724 event->key.string = g_locale_from_utf8 (buf, len,
725 NULL, &bytes_written,
726 NULL);
727 if (event->key.string)
728 event->key.length = bytes_written;
729 }
730 else if (event->key.keyval == GDK_Escape)
731 {
732 event->key.length = 1;
733 event->key.string = g_strdup ("\033");
734 }
735 else if (event->key.keyval == GDK_Return ||
736 event->key.keyval == GDK_KP_Enter)
737 {
738 event->key.length = 1;
739 event->key.string = g_strdup ("\r");
740 }
741
742 if (!event->key.string)
743 {
744 event->key.length = 0;
745 event->key.string = g_strdup ("");
746 }
747
748 out:
749 #ifdef G_ENABLE_DEBUG
750 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
751 {
752 g_message ("%s:\t\twindow: %ld key: %12s %d",
753 event->type == GDK_KEY_PRESS ? "key press " : "key release",
754 xevent->xkey.window,
755 event->key.keyval ? gdk_keyval_name (event->key.keyval) : "(none)",
756 event->key.keyval);
757
758 if (event->key.length > 0)
759 g_message ("\t\tlength: %4d string: \"%s\"",
760 event->key.length, buf);
761 }
762 #endif /* G_ENABLE_DEBUG */
763 return;
764 }
765
766 /**
767 * gdk_x11_register_standard_event_type:
768 * @display: a #GdkDisplay
769 * @event_base: first event type code to register
770 * @n_events: number of event type codes to register
771 *
772 * Registers interest in receiving extension events with type codes
773 * between @event_base and <literal>event_base + n_events - 1</literal>.
774 * The registered events must have the window field in the same place
775 * as core X events (this is not the case for e.g. XKB extension events).
776 *
777 * If an event type is registered, events of this type will go through
778 * global and window-specific filters (see gdk_window_add_filter()).
779 * Unregistered events will only go through global filters.
780 * GDK may register the events of some X extensions on its own.
781 *
782 * This function should only be needed in unusual circumstances, e.g.
783 * when filtering XInput extension events on the root window.
784 *
785 * Since: 2.4
786 **/
787 void
788 gdk_x11_register_standard_event_type (GdkDisplay *display,
789 gint event_base,
790 gint n_events)
791 {
792 GdkEventTypeX11 *event_type;
793 GdkDisplayX11 *display_x11;
794
795 display_x11 = GDK_DISPLAY_X11 (display);
796 event_type = g_new (GdkEventTypeX11, 1);
797
798 event_type->base = event_base;
799 event_type->n_events = n_events;
800
801 display_x11->event_types = g_slist_prepend (display_x11->event_types, event_type);
802 }
803
804 /* Return the window this has to do with, if any, rather
805 * than the frame or root window that was selecting
806 * for substructure
807 */
808 static void
809 get_real_window (GdkDisplay *display,
810 XEvent *event,
811 Window *event_window,
812 Window *filter_window)
813 {
814 /* Core events all have an event->xany.window field, but that's
815 * not true for extension events
816 */
817 if (event->type >= KeyPress &&
818 event->type <= MappingNotify)
819 {
820 *filter_window = event->xany.window;
821 switch (event->type)
822 {
823 case CreateNotify:
824 *event_window = event->xcreatewindow.window;
825 break;
826 case DestroyNotify:
827 *event_window = event->xdestroywindow.window;
828 break;
829 case UnmapNotify:
830 *event_window = event->xunmap.window;
831 break;
832 case MapNotify:
833 *event_window = event->xmap.window;
834 break;
835 case MapRequest:
836 *event_window = event->xmaprequest.window;
837 break;
838 case ReparentNotify:
839 *event_window = event->xreparent.window;
840 break;
841 case ConfigureNotify:
842 *event_window = event->xconfigure.window;
843 break;
844 case ConfigureRequest:
845 *event_window = event->xconfigurerequest.window;
846 break;
847 case GravityNotify:
848 *event_window = event->xgravity.window;
849 break;
850 case CirculateNotify:
851 *event_window = event->xcirculate.window;
852 break;
853 case CirculateRequest:
854 *event_window = event->xcirculaterequest.window;
855 break;
856 default:
857 *event_window = event->xany.window;
858 }
859 }
860 else
861 {
862 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
863 GSList *tmp_list;
864
865 for (tmp_list = display_x11->event_types;
866 tmp_list;
867 tmp_list = tmp_list->next)
868 {
869 GdkEventTypeX11 *event_type = tmp_list->data;
870
871 if (event->type >= event_type->base &&
872 event->type < event_type->base + event_type->n_events)
873 {
874 *event_window = event->xany.window;
875 *filter_window = event->xany.window;
876 return;
877 }
878 }
879
880 *event_window = None;
881 *filter_window = None;
882 }
883 }
884
885 #ifdef G_ENABLE_DEBUG
886 static const char notify_modes[][19] = {
887 "NotifyNormal",
888 "NotifyGrab",
889 "NotifyUngrab",
890 "NotifyWhileGrabbed"
891 };
892
893 static const char notify_details[][23] = {
894 "NotifyAncestor",
895 "NotifyVirtual",
896 "NotifyInferior",
897 "NotifyNonlinear",
898 "NotifyNonlinearVirtual",
899 "NotifyPointer",
900 "NotifyPointerRoot",
901 "NotifyDetailNone"
902 };
903 #endif
904
905 static void
906 set_user_time (GdkWindow *window,
907 GdkEvent *event)
908 {
909 g_return_if_fail (event != NULL);
910
911 window = gdk_window_get_toplevel (event->client.window);
912 g_return_if_fail (GDK_IS_WINDOW (window));
913
914 /* If an event doesn't have a valid timestamp, we shouldn't use it
915 * to update the latest user interaction time.
916 */
917 if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
918 gdk_x11_window_set_user_time (gdk_window_get_toplevel (window),
919 gdk_event_get_time (event));
920 }
921
922 static gboolean
923 is_parent_of (GdkWindow *parent,
924 GdkWindow *child)
925 {
926 GdkWindow *w;
927
928 w = child;
929 while (w != NULL)
930 {
931 if (w == parent)
932 return TRUE;
933
934 w = gdk_window_get_parent (w);
935 }
936
937 return FALSE;
938 }
939
940 static gboolean
941 gdk_event_translate (GdkDisplay *display,
942 GdkEvent *event,
943 XEvent *xevent,
944 gboolean return_exposes)
945 {
946
947 GdkWindow *window;
948 GdkWindowObject *window_private;
949 GdkWindow *filter_window;
950 GdkWindowImplX11 *window_impl = NULL;
951 gboolean return_val;
952 GdkScreen *screen = NULL;
953 GdkScreenX11 *screen_x11 = NULL;
954 GdkToplevelX11 *toplevel = NULL;
955 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
956 Window xwindow, filter_xwindow;
957
958 return_val = FALSE;
959
960 /* init these, since the done: block uses them */
961 window = NULL;
962 window_private = NULL;
963 event->any.window = NULL;
964
965 if (_gdk_default_filters)
966 {
967 /* Apply global filters */
968 GdkFilterReturn result;
969 result = gdk_event_apply_filters (xevent, event, NULL);
970
971 if (result != GDK_FILTER_CONTINUE)
972 {
973 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
974 goto done;
975 }
976 }
977
978 /* Find the GdkWindow that this event relates to.
979 * Basically this means substructure events
980 * are reported same as structure events
981 */
982 get_real_window (display, xevent, &xwindow, &filter_xwindow);
983
984 window = gdk_window_lookup_for_display (display, xwindow);
985 /* We may receive events such as NoExpose/GraphicsExpose
986 * and ShmCompletion for pixmaps
987 */
988 if (window && !GDK_IS_WINDOW (window))
989 window = NULL;
990 window_private = (GdkWindowObject *) window;
991
992 /* We always run the filters for the window where the event
993 * is delivered, not the window that it relates to
994 */
995 if (filter_xwindow == xwindow)
996 filter_window = window;
997 else
998 {
999 filter_window = gdk_window_lookup_for_display (display, filter_xwindow);
1000 if (filter_window && !GDK_IS_WINDOW (filter_window))
1001 filter_window = NULL;
1002 }
1003
1004 if (window)
1005 {
1006 screen = GDK_WINDOW_SCREEN (window);
1007 screen_x11 = GDK_SCREEN_X11 (screen);
1008 toplevel = _gdk_x11_window_get_toplevel (window);
1009 }
1010
1011 if (window != NULL)
1012 {
1013 /* Apply keyboard grabs to non-native windows */
1014 if (/* Is key event */
1015 (xevent->type == KeyPress || xevent->type == KeyRelease) &&
1016 /* And we have a grab */
1017 display->keyboard_grab.window != NULL &&
1018 (
1019 /* The window is not a descendant of the grabbed window */
1020 !is_parent_of ((GdkWindow *)display->keyboard_grab.window, window) ||
1021 /* Or owner event is false */
1022 !display->keyboard_grab.owner_events
1023 )
1024 )
1025 {
1026 /* Report key event against grab window */
1027 window = display->keyboard_grab.window;;
1028 window_private = (GdkWindowObject *) window;
1029 }
1030
1031 window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
1032
1033 /* Move key events on focus window to the real toplevel, and
1034 * filter out all other events on focus window
1035 */
1036 if (toplevel && xwindow == toplevel->focus_window)
1037 {
1038 switch (xevent->type)
1039 {
1040 case KeyPress:
1041 case KeyRelease:
1042 xwindow = GDK_WINDOW_XID (window);
1043 xevent->xany.window = xwindow;
1044 break;
1045 default:
1046 return FALSE;
1047 }
1048 }
1049
1050 g_object_ref (window);
1051 }
1052
1053 event->any.window = window;
1054 event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
1055
1056 if (window_private && GDK_WINDOW_DESTROYED (window))
1057 {
1058 if (xevent->type != DestroyNotify)
1059 {
1060 return_val = FALSE;
1061 goto done;
1062 }
1063 }
1064 else if (filter_window)
1065 {
1066 /* Apply per-window filters */
1067 GdkWindowObject *filter_private = (GdkWindowObject *) filter_window;
1068 GdkFilterReturn result;
1069
1070 if (filter_private->filters)
1071 {
1072 g_object_ref (filter_window);
1073
1074 result = gdk_event_apply_filters (xevent, event,
1075 filter_window);
1076
1077 g_object_unref (filter_window);
1078
1079 if (result != GDK_FILTER_CONTINUE)
1080 {
1081 return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
1082 goto done;
1083 }
1084 }
1085 }
1086
1087 if (xevent->type == DestroyNotify)
1088 {
1089 int i, n;
1090
1091 n = gdk_display_get_n_screens (display);
1092 for (i = 0; i < n; i++)
1093 {
1094 screen = gdk_display_get_screen (display, i);
1095 screen_x11 = GDK_SCREEN_X11 (screen);
1096
1097 if (screen_x11->wmspec_check_window == xwindow)
1098 {
1099 screen_x11->wmspec_check_window = None;
1100 screen_x11->last_wmspec_check_time = 0;
1101 g_free (screen_x11->window_manager_name);
1102 screen_x11->window_manager_name = g_strdup ("unknown");
1103
1104 /* careful, reentrancy */
1105 _gdk_x11_screen_window_manager_changed (screen);
1106
1107 return_val = FALSE;
1108 goto done;
1109 }
1110 }
1111 }
1112
1113 if (window &&
1114 (xevent->xany.type == MotionNotify ||
1115 xevent->xany.type == ButtonRelease))
1116 {
1117 if (_gdk_moveresize_handle_event (xevent))
1118 {
1119 return_val = FALSE;
1120 goto done;
1121 }
1122 }
1123
1124 /* We do a "manual" conversion of the XEvent to a
1125 * GdkEvent. The structures are mostly the same so
1126 * the conversion is fairly straightforward. We also
1127 * optionally print debugging info regarding events
1128 * received.
1129 */
1130
1131 return_val = TRUE;
1132
1133 switch (xevent->type)
1134 {
1135 case KeyPress:
1136 if (window_private == NULL)
1137 {
1138 return_val = FALSE;
1139 break;
1140 }
1141 translate_key_event (display, event, xevent);
1142 set_user_time (window, event);
1143 break;
1144
1145 case KeyRelease:
1146 if (window_private == NULL)
1147 {
1148 return_val = FALSE;
1149 break;
1150 }
1151
1152 /* Emulate detectable auto-repeat by checking to see
1153 * if the next event is a key press with the same
1154 * keycode and timestamp, and if so, ignoring the event.
1155 */
1156
1157 if (!display_x11->have_xkb_autorepeat && XPending (xevent->xkey.display))
1158 {
1159 XEvent next_event;
1160
1161 XPeekEvent (xevent->xkey.display, &next_event);
1162
1163 if (next_event.type == KeyPress &&
1164 next_event.xkey.keycode == xevent->xkey.keycode &&
1165 next_event.xkey.time == xevent->xkey.time)
1166 {
1167 return_val = FALSE;
1168 break;
1169 }
1170 }
1171
1172 translate_key_event (display, event, xevent);
1173 break;
1174
1175 case ButtonPress:
1176 GDK_NOTE (EVENTS,
1177 g_message ("button press:\t\twindow: %ld x,y: %d %d button: %d",
1178 xevent->xbutton.window,
1179 xevent->xbutton.x, xevent->xbutton.y,
1180 xevent->xbutton.button));
1181
1182 if (window_private == NULL)
1183 {
1184 return_val = FALSE;
1185 break;
1186 }
1187
1188 /* If we get a ButtonPress event where the button is 4 or 5,
1189 it's a Scroll event */
1190 switch (xevent->xbutton.button)
1191 {
1192 case 4: /* up */
1193 case 5: /* down */
1194 case 6: /* left */
1195 case 7: /* right */
1196 event->scroll.type = GDK_SCROLL;
1197
1198 if (xevent->xbutton.button == 4)
1199 event->scroll.direction = GDK_SCROLL_UP;
1200 else if (xevent->xbutton.button == 5)
1201 event->scroll.direction = GDK_SCROLL_DOWN;
1202 else if (xevent->xbutton.button == 6)
1203 event->scroll.direction = GDK_SCROLL_LEFT;
1204 else
1205 event->scroll.direction = GDK_SCROLL_RIGHT;
1206
1207 event->scroll.window = window;
1208 event->scroll.time = xevent->xbutton.time;
1209 event->scroll.x = xevent->xbutton.x;
1210 event->scroll.y = xevent->xbutton.y;
1211 event->scroll.x_root = (gfloat)xevent->xbutton.x_root;
1212 event->scroll.y_root = (gfloat)xevent->xbutton.y_root;
1213 event->scroll.state = (GdkModifierType) xevent->xbutton.state;
1214 event->scroll.device = display->core_pointer;
1215
1216 if (!set_screen_from_root (display, event, xevent->xbutton.root))
1217 {
1218 return_val = FALSE;
1219 break;
1220 }
1221
1222 break;
1223
1224 default:
1225 event->button.type = GDK_BUTTON_PRESS;
1226 event->button.window = window;
1227 event->button.time = xevent->xbutton.time;
1228 event->button.x = xevent->xbutton.x;
1229 event->button.y = xevent->xbutton.y;
1230 event->button.x_root = (gfloat)xevent->xbutton.x_root;
1231 event->button.y_root = (gfloat)xevent->xbutton.y_root;
1232 event->button.axes = NULL;
1233 event->button.state = (GdkModifierType) xevent->xbutton.state;
1234 event->button.button = xevent->xbutton.button;
1235 event->button.device = display->core_pointer;
1236
1237 if (!set_screen_from_root (display, event, xevent->xbutton.root))
1238 {
1239 return_val = FALSE;
1240 break;
1241 }
1242 break;
1243 }
1244
1245 set_user_time (window, event);
1246
1247 break;
1248
1249 case ButtonRelease:
1250 GDK_NOTE (EVENTS,
1251 g_message ("button release:\twindow: %ld x,y: %d %d button: %d",
1252 xevent->xbutton.window,
1253 xevent->xbutton.x, xevent->xbutton.y,
1254 xevent->xbutton.button));
1255
1256 if (window_private == NULL)
1257 {
1258 return_val = FALSE;
1259 break;
1260 }
1261
1262 /* We treat button presses as scroll wheel events, so ignore the release */
1263 if (xevent->xbutton.button == 4 || xevent->xbutton.button == 5 ||
1264 xevent->xbutton.button == 6 || xevent->xbutton.button ==7)
1265 {
1266 return_val = FALSE;
1267 break;
1268 }
1269
1270 event->button.type = GDK_BUTTON_RELEASE;
1271 event->button.window = window;
1272 event->button.time = xevent->xbutton.time;
1273 event->button.x = xevent->xbutton.x;
1274 event->button.y = xevent->xbutton.y;
1275 event->button.x_root = (gfloat)xevent->xbutton.x_root;
1276 event->button.y_root = (gfloat)xevent->xbutton.y_root;
1277 event->button.axes = NULL;
1278 event->button.state = (GdkModifierType) xevent->xbutton.state;
1279 event->button.button = xevent->xbutton.button;
1280 event->button.device = display->core_pointer;
1281
1282 if (!set_screen_from_root (display, event, xevent->xbutton.root))
1283 return_val = FALSE;
1284
1285 break;
1286
1287 case MotionNotify:
1288 GDK_NOTE (EVENTS,
1289 g_message ("motion notify:\t\twindow: %ld x,y: %d %d hint: %s",
1290 xevent->xmotion.window,
1291 xevent->xmotion.x, xevent->xmotion.y,
1292 (xevent->xmotion.is_hint) ? "true" : "false"));
1293
1294 if (window_private == NULL)
1295 {
1296 return_val = FALSE;
1297 break;
1298 }
1299
1300 event->motion.type = GDK_MOTION_NOTIFY;
1301 event->motion.window = window;
1302 event->motion.time = xevent->xmotion.time;
1303 event->motion.x = xevent->xmotion.x;
1304 event->motion.y = xevent->xmotion.y;
1305 event->motion.x_root = (gfloat)xevent->xmotion.x_root;
1306 event->motion.y_root = (gfloat)xevent->xmotion.y_root;
1307 event->motion.axes = NULL;
1308 event->motion.state = (GdkModifierType) xevent->xmotion.state;
1309 event->motion.is_hint = xevent->xmotion.is_hint;
1310 event->motion.device = display->core_pointer;
1311
1312 if (!set_screen_from_root (display, event, xevent->xbutton.root))
1313 {
1314 return_val = FALSE;
1315 break;
1316 }
1317
1318 break;
1319
1320 case EnterNotify:
1321 GDK_NOTE (EVENTS,
1322 g_message ("enter notify:\t\twindow: %ld detail: %d subwin: %ld mode: %d",
1323 xevent->xcrossing.window,
1324 xevent->xcrossing.detail,
1325 xevent->xcrossing.subwindow,
1326 xevent->xcrossing.mode));
1327
1328 if (window_private == NULL)
1329 {
1330 return_val = FALSE;
1331 break;
1332 }
1333
1334 if (!set_screen_from_root (display, event, xevent->xbutton.root))
1335 {
1336 return_val = FALSE;
1337 break;
1338 }
1339
1340 /* Handle focusing (in the case where no window manager is running */
1341 if (toplevel && xevent->xcrossing.detail != NotifyInferior)
1342 {
1343 toplevel->has_pointer = TRUE;
1344
1345 if (xevent->xcrossing.focus && !toplevel->has_focus_window)
1346 {
1347 gboolean had_focus = HAS_FOCUS (toplevel);
1348
1349 toplevel->has_pointer_focus = TRUE;
1350
1351 if (HAS_FOCUS (toplevel) != had_focus)
1352 generate_focus_event (window, TRUE);
1353 }
1354 }
1355
1356 event->crossing.type = GDK_ENTER_NOTIFY;
1357 event->crossing.window = window;
1358
1359 /* If the subwindow field of the XEvent is non-NULL, then
1360 * lookup the corresponding GdkWindow.
1361 */
1362 if (xevent->xcrossing.subwindow != None)
1363 event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1364 else
1365 event->crossing.subwindow = NULL;
1366
1367 event->crossing.time = xevent->xcrossing.time;
1368 event->crossing.x = xevent->xcrossing.x;
1369 event->crossing.y = xevent->xcrossing.y;
1370 event->crossing.x_root = xevent->xcrossing.x_root;
1371 event->crossing.y_root = xevent->xcrossing.y_root;
1372
1373 /* Translate the crossing mode into Gdk terms.
1374 */
1375 switch (xevent->xcrossing.mode)
1376 {
1377 case NotifyNormal:
1378 event->crossing.mode = GDK_CROSSING_NORMAL;
1379 break;
1380 case NotifyGrab:
1381 event->crossing.mode = GDK_CROSSING_GRAB;
1382 break;
1383 case NotifyUngrab:
1384 event->crossing.mode = GDK_CROSSING_UNGRAB;
1385 break;
1386 };
1387
1388 /* Translate the crossing detail into Gdk terms.
1389 */
1390 switch (xevent->xcrossing.detail)
1391 {
1392 case NotifyInferior:
1393 event->crossing.detail = GDK_NOTIFY_INFERIOR;
1394 break;
1395 case NotifyAncestor:
1396 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1397 break;
1398 case NotifyVirtual:
1399 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1400 break;
1401 case NotifyNonlinear:
1402 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1403 break;
1404 case NotifyNonlinearVirtual:
1405 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1406 break;
1407 default:
1408 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1409 break;
1410 }
1411
1412 event->crossing.focus = xevent->xcrossing.focus;
1413 event->crossing.state = xevent->xcrossing.state;
1414
1415 break;
1416
1417 case LeaveNotify:
1418 GDK_NOTE (EVENTS,
1419 g_message ("leave notify:\t\twindow: %ld detail: %d subwin: %ld mode: %d",
1420 xevent->xcrossing.window,
1421 xevent->xcrossing.detail,
1422 xevent->xcrossing.subwindow,
1423 xevent->xcrossing.mode));
1424
1425 if (window_private == NULL)
1426 {
1427 return_val = FALSE;
1428 break;
1429 }
1430
1431 if (!set_screen_from_root (display, event, xevent->xbutton.root))
1432 {
1433 return_val = FALSE;
1434 break;
1435 }
1436
1437 /* Handle focusing (in the case where no window manager is running */
1438 if (toplevel && xevent->xcrossing.detail != NotifyInferior)
1439 {
1440 toplevel->has_pointer = FALSE;
1441
1442 if (xevent->xcrossing.focus && !toplevel->has_focus_window)
1443 {
1444 gboolean had_focus = HAS_FOCUS (toplevel);
1445
1446 toplevel->has_pointer_focus = FALSE;
1447
1448 if (HAS_FOCUS (toplevel) != had_focus)
1449 generate_focus_event (window, FALSE);
1450 }
1451 }
1452
1453 event->crossing.type = GDK_LEAVE_NOTIFY;
1454 event->crossing.window = window;
1455
1456 /* If the subwindow field of the XEvent is non-NULL, then
1457 * lookup the corresponding GdkWindow.
1458 */
1459 if (xevent->xcrossing.subwindow != None)
1460 event->crossing.subwindow = gdk_window_lookup_for_display (display, xevent->xcrossing.subwindow);
1461 else
1462 event->crossing.subwindow = NULL;
1463
1464 event->crossing.time = xevent->xcrossing.time;
1465 event->crossing.x = xevent->xcrossing.x;
1466 event->crossing.y = xevent->xcrossing.y;
1467 event->crossing.x_root = xevent->xcrossing.x_root;
1468 event->crossing.y_root = xevent->xcrossing.y_root;
1469
1470 /* Translate the crossing mode into Gdk terms.
1471 */
1472 switch (xevent->xcrossing.mode)
1473 {
1474 case NotifyNormal:
1475 event->crossing.mode = GDK_CROSSING_NORMAL;
1476 break;
1477 case NotifyGrab:
1478 event->crossing.mode = GDK_CROSSING_GRAB;
1479 break;
1480 case NotifyUngrab:
1481 event->crossing.mode = GDK_CROSSING_UNGRAB;
1482 break;
1483 };
1484
1485 /* Translate the crossing detail into Gdk terms.
1486 */
1487 switch (xevent->xcrossing.detail)
1488 {
1489 case NotifyInferior:
1490 event->crossing.detail = GDK_NOTIFY_INFERIOR;
1491 break;
1492 case NotifyAncestor:
1493 event->crossing.detail = GDK_NOTIFY_ANCESTOR;
1494 break;
1495 case NotifyVirtual:
1496 event->crossing.detail = GDK_NOTIFY_VIRTUAL;
1497 break;
1498 case NotifyNonlinear:
1499 event->crossing.detail = GDK_NOTIFY_NONLINEAR;
1500 break;
1501 case NotifyNonlinearVirtual:
1502 event->crossing.detail = GDK_NOTIFY_NONLINEAR_VIRTUAL;
1503 break;
1504 default:
1505 event->crossing.detail = GDK_NOTIFY_UNKNOWN;
1506 break;
1507 }
1508
1509 event->crossing.focus = xevent->xcrossing.focus;
1510 event->crossing.state = xevent->xcrossing.state;
1511
1512 break;
1513
1514 /* We only care about focus events that indicate that _this_
1515 * window (not a ancestor or child) got or lost the focus
1516 */
1517 case FocusIn:
1518 GDK_NOTE (EVENTS,
1519 g_message ("focus in:\t\twindow: %ld, detail: %s, mode: %s",
1520 xevent->xfocus.window,
1521 notify_details[xevent->xfocus.detail],
1522 notify_modes[xevent->xfocus.mode]));
1523
1524 if (toplevel)
1525 {
1526 gboolean had_focus = HAS_FOCUS (toplevel);
1527
1528 switch (xevent->xfocus.detail)
1529 {
1530 case NotifyAncestor:
1531 case NotifyVirtual:
1532 /* When the focus moves from an ancestor of the window to
1533 * the window or a descendent of the window, *and* the
1534 * pointer is inside the window, then we were previously
1535 * receiving keystroke events in the has_pointer_focus
1536 * case and are now receiving them in the
1537 * has_focus_window case.
1538 */
1539 if (toplevel->has_pointer &&
1540 xevent->xfocus.mode != NotifyGrab &&
1541 xevent->xfocus.mode != NotifyUngrab)
1542 toplevel->has_pointer_focus = FALSE;
1543
1544 /* fall through */
1545 case NotifyNonlinear:
1546 case NotifyNonlinearVirtual:
1547 if (xevent->xfocus.mode != NotifyGrab &&
1548 xevent->xfocus.mode != NotifyUngrab)
1549 toplevel->has_focus_window = TRUE;
1550 /* We pretend that the focus moves to the grab
1551 * window, so we pay attention to NotifyGrab
1552 * NotifyUngrab, and ignore NotifyWhileGrabbed
1553 */
1554 if (xevent->xfocus.mode != NotifyWhileGrabbed)
1555 toplevel->has_focus = TRUE;
1556 break;
1557 case NotifyPointer:
1558 /* The X server sends NotifyPointer/NotifyGrab,
1559 * but the pointer focus is ignored while a
1560 * grab is in effect
1561 */
1562 if (xevent->xfocus.mode != NotifyGrab &&
1563 xevent->xfocus.mode != NotifyUngrab)
1564 toplevel->has_pointer_focus = TRUE;
1565 break;
1566 case NotifyInferior:
1567 case NotifyPointerRoot:
1568 case NotifyDetailNone:
1569 break;
1570 }
1571
1572 if (HAS_FOCUS (toplevel) != had_focus)
1573 generate_focus_event (window, TRUE);
1574 }
1575 break;
1576 case FocusOut:
1577 GDK_NOTE (EVENTS,
1578 g_message ("focus out:\t\twindow: %ld, detail: %s, mode: %s",
1579 xevent->xfocus.window,
1580 notify_details[xevent->xfocus.detail],
1581 notify_modes[xevent->xfocus.mode]));
1582
1583 if (toplevel)
1584 {
1585 gboolean had_focus = HAS_FOCUS (toplevel);
1586
1587 switch (xevent->xfocus.detail)
1588 {
1589 case NotifyAncestor:
1590 case NotifyVirtual:
1591 /* When the focus moves from the window or a descendent
1592 * of the window to an ancestor of the window, *and* the
1593 * pointer is inside the window, then we were previously
1594 * receiving keystroke events in the has_focus_window
1595 * case and are now receiving them in the
1596 * has_pointer_focus case.
1597 */
1598 if (toplevel->has_pointer &&
1599 xevent->xfocus.mode != NotifyGrab &&
1600 xevent->xfocus.mode != NotifyUngrab)
1601 toplevel->has_pointer_focus = TRUE;
1602
1603 /* fall through */
1604 case NotifyNonlinear:
1605 case NotifyNonlinearVirtual:
1606 if (xevent->xfocus.mode != NotifyGrab &&
1607 xevent->xfocus.mode != NotifyUngrab)
1608 toplevel->has_focus_window = FALSE;
1609 if (xevent->xfocus.mode != NotifyWhileGrabbed)
1610 toplevel->has_focus = FALSE;
1611 break;
1612 case NotifyPointer:
1613 if (xevent->xfocus.mode != NotifyGrab &&
1614 xevent->xfocus.mode != NotifyUngrab)
1615 toplevel->has_pointer_focus = FALSE;
1616 break;
1617 case NotifyInferior:
1618 case NotifyPointerRoot:
1619 case NotifyDetailNone:
1620 break;
1621 }
1622
1623 if (HAS_FOCUS (toplevel) != had_focus)
1624 generate_focus_event (window, FALSE);
1625 }
1626 break;
1627
1628 #if 0
1629 /* gdk_keyboard_grab() causes following events. These events confuse
1630 * the XIM focus, so ignore them.
1631 */
1632 if (xevent->xfocus.mode == NotifyGrab ||
1633 xevent->xfocus.mode == NotifyUngrab)
1634 break;
1635 #endif
1636
1637 case KeymapNotify:
1638 GDK_NOTE (EVENTS,
1639 g_message ("keymap notify"));
1640
1641 /* Not currently handled */
1642 return_val = FALSE;
1643 break;
1644
1645 case Expose:
1646 GDK_NOTE (EVENTS,
1647 g_message ("expose:\t\twindow: %ld %d x,y: %d %d w,h: %d %d%s",
1648 xevent->xexpose.window, xevent->xexpose.count,
1649 xevent->xexpose.x, xevent->xexpose.y,
1650 xevent->xexpose.width, xevent->xexpose.height,
1651 event->any.send_event ? " (send)" : ""));
1652
1653 if (window_private == NULL)
1654 {
1655 return_val = FALSE;
1656 break;
1657 }
1658
1659 {
1660 GdkRectangle expose_rect;
1661
1662 expose_rect.x = xevent->xexpose.x;
1663 expose_rect.y = xevent->xexpose.y;
1664 expose_rect.width = xevent->xexpose.width;
1665 expose_rect.height = xevent->xexpose.height;
1666
1667 _gdk_window_process_expose (window, xevent->xexpose.serial, &expose_rect);
1668 return_val = FALSE;
1669 }
1670
1671 break;
1672
1673 case GraphicsExpose:
1674 {
1675 GdkRectangle expose_rect;
1676
1677 GDK_NOTE (EVENTS,
1678 g_message ("graphics expose:\tdrawable: %ld",
1679 xevent->xgraphicsexpose.drawable));
1680
1681 if (window_private == NULL)
1682 {
1683 return_val = FALSE;
1684 break;
1685 }
1686
1687 expose_rect.x = xevent->xgraphicsexpose.x;
1688 expose_rect.y = xevent->xgraphicsexpose.y;
1689 expose_rect.width = xevent->xgraphicsexpose.width;
1690 expose_rect.height = xevent->xgraphicsexpose.height;
1691
1692 if (return_exposes)
1693 {
1694 event->expose.type = GDK_EXPOSE;
1695 event->expose.area = expose_rect;
1696 event->expose.region = gdk_region_rectangle (&expose_rect);
1697 event->expose.window = window;
1698 event->expose.count = xevent->xgraphicsexpose.count;
1699
1700 return_val = TRUE;
1701 }
1702 else
1703 {
1704 _gdk_window_process_expose (window, xevent->xgraphicsexpose.serial, &expose_rect);
1705
1706 return_val = FALSE;
1707 }
1708
1709 }
1710 break;
1711
1712 case NoExpose:
1713 GDK_NOTE (EVENTS,
1714 g_message ("no expose:\t\tdrawable: %ld",
1715 xevent->xnoexpose.drawable));
1716
1717 event->no_expose.type = GDK_NO_EXPOSE;
1718 event->no_expose.window = window;
1719
1720 break;
1721
1722 case VisibilityNotify:
1723 #ifdef G_ENABLE_DEBUG
1724 if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
1725 switch (xevent->xvisibility.state)
1726 {
1727 case VisibilityFullyObscured:
1728 g_message ("visibility notify:\twindow: %ld none",
1729 xevent->xvisibility.window);
1730 break;
1731 case VisibilityPartiallyObscured:
1732 g_message ("visibility notify:\twindow: %ld partial",
1733 xevent->xvisibility.window);
1734 break;
1735 case VisibilityUnobscured:
1736 g_message ("visibility notify:\twindow: %ld full",
1737 xevent->xvisibility.window);
1738 break;
1739 }
1740 #endif /* G_ENABLE_DEBUG */
1741
1742 if (window_private == NULL)
1743 {
1744 return_val = FALSE;
1745 break;
1746 }
1747
1748 event->visibility.type = GDK_VISIBILITY_NOTIFY;
1749 event->visibility.window = window;
1750
1751 switch (xevent->xvisibility.state)
1752 {
1753 case VisibilityFullyObscured:
1754 event->visibility.state = GDK_VISIBILITY_FULLY_OBSCURED;
1755 break;
1756
1757 case VisibilityPartiallyObscured:
1758 event->visibility.state = GDK_VISIBILITY_PARTIAL;
1759 break;
1760
1761 case VisibilityUnobscured:
1762 event->visibility.state = GDK_VISIBILITY_UNOBSCURED;
1763 break;
1764 }
1765
1766 break;
1767
1768 case CreateNotify:
1769 GDK_NOTE (EVENTS,
1770 g_message ("create notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d parent: %ld ovr: %d",
1771 xevent->xcreatewindow.window,
1772 xevent->xcreatewindow.x,
1773 xevent->xcreatewindow.y,
1774 xevent->xcreatewindow.width,
1775 xevent->xcreatewindow.height,
1776 xevent->xcreatewindow.border_width,
1777 xevent->xcreatewindow.parent,
1778 xevent->xcreatewindow.override_redirect));
1779 /* not really handled */
1780 break;
1781
1782 case DestroyNotify:
1783 GDK_NOTE (EVENTS,
1784 g_message ("destroy notify:\twindow: %ld",
1785 xevent->xdestroywindow.window));
1786
1787 /* Ignore DestroyNotify from SubstructureNotifyMask */
1788 if (xevent->xdestroywindow.window == xevent->xdestroywindow.event)
1789 {
1790 event->any.type = GDK_DESTROY;
1791 event->any.window = window;
1792
1793 return_val = window_private && !GDK_WINDOW_DESTROYED (window);
1794
1795 if (window && GDK_WINDOW_XID (window) != screen_x11->xroot_window)
1796 gdk_window_destroy_notify (window);
1797 }
1798 else
1799 return_val = FALSE;
1800
1801 break;
1802
1803 case UnmapNotify:
1804 GDK_NOTE (EVENTS,
1805 g_message ("unmap notify:\t\twindow: %ld",
1806 xevent->xmap.window));
1807
1808 event->any.type = GDK_UNMAP;
1809 event->any.window = window;
1810
1811 /* If the WM supports the _NET_WM_STATE_HIDDEN hint, we do not want to
1812 * interpret UnmapNotify events as implying iconic state.
1813 * http://bugzilla.gnome.org/show_bug.cgi?id=590726.
1814 */
1815 if (screen &&
1816 !gdk_x11_screen_supports_net_wm_hint (screen,
1817 gdk_atom_intern_static_string ("_NET_WM_STATE_HIDDEN")))
1818 {
1819 /* If we are shown (not withdrawn) and get an unmap, it means we were
1820 * iconified in the X sense. If we are withdrawn, and get an unmap, it
1821 * means we hid the window ourselves, so we will have already flipped
1822 * the iconified bit off.
1823 */
1824 if (window && GDK_WINDOW_IS_MAPPED (window))
1825 gdk_synthesize_window_state (window,
1826 0,
1827 GDK_WINDOW_STATE_ICONIFIED);
1828 }
1829
1830 if (window)
1831 _gdk_xgrab_check_unmap (window, xevent->xany.serial);
1832
1833 break;
1834
1835 case MapNotify:
1836 GDK_NOTE (EVENTS,
1837 g_message ("map notify:\t\twindow: %ld",
1838 xevent->xmap.window));
1839
1840 event->any.type = GDK_MAP;
1841 event->any.window = window;
1842
1843 /* Unset iconified if it was set */
1844 if (window && (((GdkWindowObject*)window)->state & GDK_WINDOW_STATE_ICONIFIED))
1845 gdk_synthesize_window_state (window,
1846 GDK_WINDOW_STATE_ICONIFIED,
1847 0);
1848
1849 break;
1850
1851 case ReparentNotify:
1852 GDK_NOTE (EVENTS,
1853 g_message ("reparent notify:\twindow: %ld x,y: %d %d parent: %ld ovr: %d",
1854 xevent->xreparent.window,
1855 xevent->xreparent.x,
1856 xevent->xreparent.y,
1857 xevent->xreparent.parent,
1858 xevent->xreparent.override_redirect));
1859
1860 /* Not currently handled */
1861 return_val = FALSE;
1862 break;
1863
1864 case ConfigureNotify:
1865 GDK_NOTE (EVENTS,
1866 g_message ("configure notify:\twindow: %ld x,y: %d %d w,h: %d %d b-w: %d above: %ld ovr: %d%s",
1867 xevent->xconfigure.window,
1868 xevent->xconfigure.x,
1869 xevent->xconfigure.y,
1870 xevent->xconfigure.width,
1871 xevent->xconfigure.height,
1872 xevent->xconfigure.border_width,
1873 xevent->xconfigure.above,
1874 xevent->xconfigure.override_redirect,
1875 !window
1876 ? " (discarding)"
1877 : GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD
1878 ? " (discarding child)"
1879 : xevent->xconfigure.event != xevent->xconfigure.window
1880 ? " (discarding substructure)"
1881 : ""));
1882 if (window && GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1883 {
1884 window_private->width = xevent->xconfigure.width;
1885 window_private->height = xevent->xconfigure.height;
1886
1887 _gdk_window_update_size (window);
1888 _gdk_x11_drawable_update_size (window_private->impl);
1889 _gdk_x11_screen_size_changed (screen, xevent);
1890 }
1891
1892 if (window &&
1893 xevent->xconfigure.event == xevent->xconfigure.window &&
1894 !GDK_WINDOW_DESTROYED (window) &&
1895 window_private->input_window != NULL)
1896 _gdk_input_configure_event (&xevent->xconfigure, window);
1897
1898 #ifdef HAVE_XSYNC
1899 if (toplevel && display_x11->use_sync && !XSyncValueIsZero (toplevel->pending_counter_value))
1900 {
1901 toplevel->current_counter_value = toplevel->pending_counter_value;
1902 XSyncIntToValue (&toplevel->pending_counter_value, 0);
1903 }
1904 #endif
1905
1906 if (!window ||
1907 xevent->xconfigure.event != xevent->xconfigure.window ||
1908 GDK_WINDOW_TYPE (window) == GDK_WINDOW_CHILD ||
1909 GDK_WINDOW_TYPE (window) == GDK_WINDOW_ROOT)
1910 return_val = FALSE;
1911 else
1912 {
1913 event->configure.type = GDK_CONFIGURE;
1914 event->configure.window = window;
1915 event->configure.width = xevent->xconfigure.width;
1916 event->configure.height = xevent->xconfigure.height;
1917
1918 if (!xevent->xconfigure.send_event &&
1919 !xevent->xconfigure.override_redirect &&
1920 !GDK_WINDOW_DESTROYED (window))
1921 {
1922 gint tx = 0;
1923 gint ty = 0;
1924 Window child_window = 0;
1925
1926 gdk_error_trap_push ();
1927 if (XTranslateCoordinates (GDK_DRAWABLE_XDISPLAY (window),
1928 GDK_DRAWABLE_XID (window),
1929 screen_x11->xroot_window,
1930 0, 0,
1931 &tx, &ty,
1932 &child_window))
1933 {
1934 event->configure.x = tx;
1935 event->configure.y = ty;
1936 }
1937 gdk_error_trap_pop ();
1938 }
1939 else
1940 {
1941 event->configure.x = xevent->xconfigure.x;
1942 event->configure.y = xevent->xconfigure.y;
1943 }
1944 window_private->x = event->configure.x;
1945 window_private->y = event->configure.y;
1946 window_private->width = xevent->xconfigure.width;
1947 window_private->height = xevent->xconfigure.height;
1948
1949 _gdk_window_update_size (window);
1950 _gdk_x11_drawable_update_size (window_private->impl);
1951
1952 if (window_private->resize_count >= 1)
1953 {
1954 window_private->resize_count -= 1;
1955
1956 if (window_private->resize_count == 0)
1957 _gdk_moveresize_configure_done (display, window);
1958 }
1959 }
1960 break;
1961
1962 case PropertyNotify:
1963 GDK_NOTE (EVENTS,
1964 g_message ("property notify:\twindow: %ld, atom(%ld): %s%s%s",
1965 xevent->xproperty.window,
1966 xevent->xproperty.atom,
1967 "\"",
1968 gdk_x11_get_xatom_name_for_display (display, xevent->xproperty.atom),
1969 "\""));
1970
1971 if (window_private == NULL)
1972 {
1973 return_val = FALSE;
1974 break;
1975 }
1976
1977 /* We compare with the serial of the last time we mapped the
1978 * window to avoid refetching properties that we set ourselves
1979 */
1980 if (toplevel &&
1981 xevent->xproperty.serial >= toplevel->map_serial)
1982 {
1983 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"))
1984 gdk_check_wm_state_changed (window);
1985
1986 if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"))
1987 gdk_check_wm_desktop_changed (window);
1988 }
1989
1990 if (window_private->event_mask & GDK_PROPERTY_CHANGE_MASK)
1991 {
1992 event->property.type = GDK_PROPERTY_NOTIFY;
1993 event->property.window = window;
1994 event->property.atom = gdk_x11_xatom_to_atom_for_display (display, xevent->xproperty.atom);
1995 event->property.time = xevent->xproperty.time;
1996 event->property.state = xevent->xproperty.state;
1997 }
1998 else
1999 return_val = FALSE;
2000
2001 break;
2002
2003 case SelectionClear:
2004 GDK_NOTE (EVENTS,
2005 g_message ("selection clear:\twindow: %ld",
2006 xevent->xproperty.window));
2007
2008 if (_gdk_selection_filter_clear_event (&xevent->xselectionclear))
2009 {
2010 event->selection.type = GDK_SELECTION_CLEAR;
2011 event->selection.window = window;
2012 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionclear.selection);
2013 event->selection.time = xevent->xselectionclear.time;
2014 }
2015 else
2016 return_val = FALSE;
2017
2018 break;
2019
2020 case SelectionRequest:
2021 GDK_NOTE (EVENTS,
2022 g_message ("selection request:\twindow: %ld",
2023 xevent->xproperty.window));
2024
2025 event->selection.type = GDK_SELECTION_REQUEST;
2026 event->selection.window = window;
2027 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.selection);
2028 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.target);
2029 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselectionrequest.property);
2030 event->selection.requestor = xevent->xselectionrequest.requestor;
2031 event->selection.time = xevent->xselectionrequest.time;
2032
2033 break;
2034
2035 case SelectionNotify:
2036 GDK_NOTE (EVENTS,
2037 g_message ("selection notify:\twindow: %ld",
2038 xevent->xproperty.window));
2039
2040
2041 event->selection.type = GDK_SELECTION_NOTIFY;
2042 event->selection.window = window;
2043 event->selection.selection = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.selection);
2044 event->selection.target = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.target);
2045 if (xevent->xselection.property == None)
2046 event->selection.property = GDK_NONE;
2047 else
2048 event->selection.property = gdk_x11_xatom_to_atom_for_display (display, xevent->xselection.property);
2049 event->selection.time = xevent->xselection.time;
2050
2051 break;
2052
2053 case ColormapNotify:
2054 GDK_NOTE (EVENTS,
2055 g_message ("colormap notify:\twindow: %ld",
2056 xevent->xcolormap.window));
2057
2058 /* Not currently handled */
2059 return_val = FALSE;
2060 break;
2061
2062 case ClientMessage:
2063 {
2064 GList *tmp_list;
2065 GdkFilterReturn result = GDK_FILTER_CONTINUE;
2066 GdkAtom message_type = gdk_x11_xatom_to_atom_for_display (display, xevent->xclient.message_type);
2067
2068 GDK_NOTE (EVENTS,
2069 g_message ("client message:\twindow: %ld",
2070 xevent->xclient.window));
2071
2072 tmp_list = display_x11->client_filters;
2073 while (tmp_list)
2074 {
2075 GdkClientFilter *filter = tmp_list->data;
2076 tmp_list = tmp_list->next;
2077
2078 if (filter->type == message_type)
2079 {
2080 result = (*filter->function) (xevent, event, filter->data);
2081 if (result != GDK_FILTER_CONTINUE)
2082 break;
2083 }
2084 }
2085
2086 switch (result)
2087 {
2088 case GDK_FILTER_REMOVE:
2089 return_val = FALSE;
2090 break;
2091 case GDK_FILTER_TRANSLATE:
2092 return_val = TRUE;
2093 break;
2094 case GDK_FILTER_CONTINUE:
2095 /* Send unknown ClientMessage's on to Gtk for it to use */
2096 if (window_private == NULL)
2097 {
2098 return_val = FALSE;
2099 }
2100 else
2101 {
2102 event->client.type = GDK_CLIENT_EVENT;
2103 event->client.window = window;
2104 event->client.message_type = message_type;
2105 event->client.data_format = xevent->xclient.format;
2106 memcpy(&event->client.data, &xevent->xclient.data,
2107 sizeof(event->client.data));
2108 }
2109 break;
2110 }
2111 }
2112
2113 break;
2114
2115 case MappingNotify:
2116 GDK_NOTE (EVENTS,
2117 g_message ("mapping notify"));
2118
2119 /* Let XLib know that there is a new keyboard mapping.
2120 */
2121 XRefreshKeyboardMapping (&xevent->xmapping);
2122 _gdk_keymap_keys_changed (display);
2123 return_val = FALSE;
2124 break;
2125
2126 default:
2127 #ifdef HAVE_XKB
2128 if (xevent->type == display_x11->xkb_event_type)
2129 {
2130 XkbEvent *xkb_event = (XkbEvent *)xevent;
2131
2132 switch (xkb_event->any.xkb_type)
2133 {
2134 case XkbNewKeyboardNotify:
2135 case XkbMapNotify:
2136 _gdk_keymap_keys_changed (display);
2137
2138 return_val = FALSE;
2139 break;
2140
2141 case XkbStateNotify:
2142 _gdk_keymap_state_changed (display, xevent);
2143 break;
2144 }
2145 }
2146 else
2147 #endif
2148 #ifdef HAVE_XFIXES
2149 if (xevent->type - display_x11->xfixes_event_base == XFixesSelectionNotify)
2150 {
2151 XFixesSelectionNotifyEvent *selection_notify = (XFixesSelectionNotifyEvent *)xevent;
2152
2153 _gdk_x11_screen_process_owner_change (screen, xevent);
2154
2155 event->owner_change.type = GDK_OWNER_CHANGE;
2156 event->owner_change.window = window;
2157 event->owner_change.owner = selection_notify->owner;
2158 event->owner_change.reason = selection_notify->subtype;
2159 event->owner_change.selection =
2160 gdk_x11_xatom_to_atom_for_display (display,
2161 selection_notify->selection);
2162 event->owner_change.time = selection_notify->timestamp;
2163 event->owner_change.selection_time = selection_notify->selection_timestamp;
2164
2165 return_val = TRUE;
2166 }
2167 else
2168 #endif
2169 #ifdef HAVE_RANDR
2170 if (xevent->type - display_x11->xrandr_event_base == RRScreenChangeNotify ||
2171 xevent->type - display_x11->xrandr_event_base == RRNotify)
2172 {
2173 if (screen)
2174 _gdk_x11_screen_size_changed (screen, xevent);
2175 }
2176 else
2177 #endif
2178 #if defined(HAVE_XCOMPOSITE) && defined (HAVE_XDAMAGE) && defined (HAVE_XFIXES)
2179 if (display_x11->have_xdamage && window_private && window_private->composited &&
2180 xevent->type == display_x11->xdamage_event_base + XDamageNotify &&
2181 ((XDamageNotifyEvent *) xevent)->damage == window_impl->damage)
2182 {
2183 XDamageNotifyEvent *damage_event = (XDamageNotifyEvent *) xevent;
2184 XserverRegion repair;
2185 GdkRectangle rect;
2186
2187 rect.x = window_private->x + damage_event->area.x;
2188 rect.y = window_private->y + damage_event->area.y;
2189 rect.width = damage_event->area.width;
2190 rect.height = damage_event->area.height;
2191
2192 repair = XFixesCreateRegion (display_x11->xdisplay,
2193 &damage_event->area, 1);
2194 XDamageSubtract (display_x11->xdisplay,
2195 window_impl->damage,
2196 repair, None);
2197 XFixesDestroyRegion (display_x11->xdisplay, repair);
2198
2199 if (window_private->parent != NULL)
2200 _gdk_window_process_expose (GDK_WINDOW (window_private->parent),
2201 damage_event->serial, &rect);
2202
2203 return_val = TRUE;
2204 }
2205 else
2206 #endif
2207 {
2208 /* something else - (e.g., a Xinput event) */
2209
2210 if (window_private &&
2211 !GDK_WINDOW_DESTROYED (window_private) &&
2212 window_private->input_window)
2213 return_val = _gdk_input_other_event (event, xevent, window);
2214 else
2215 return_val = FALSE;
2216
2217 break;
2218 }
2219 }
2220
2221 done:
2222 if (return_val)
2223 {
2224 if (event->any.window)
2225 g_object_ref (event->any.window);
2226 if (((event->any.type == GDK_ENTER_NOTIFY) ||
2227 (event->any.type == GDK_LEAVE_NOTIFY)) &&
2228 (event->crossing.subwindow != NULL))
2229 g_object_ref (event->crossing.subwindow);
2230 }
2231 else
2232 {
2233 /* Mark this event as having no resources to be freed */
2234 event->any.window = NULL;
2235 event->any.type = GDK_NOTHING;
2236 }
2237
2238 if (window)
2239 g_object_unref (window);
2240
2241 return return_val;
2242 }
2243
2244 static GdkFilterReturn
2245 gdk_wm_protocols_filter (GdkXEvent *xev,
2246 GdkEvent *event,
2247 gpointer data)
2248 {
2249 XEvent *xevent = (XEvent *)xev;
2250 GdkWindow *win = event->any.window;
2251 GdkDisplay *display;
2252 Atom atom;
2253
2254 if (!win)
2255 return GDK_FILTER_REMOVE;
2256
2257 display = GDK_WINDOW_DISPLAY (win);
2258 atom = (Atom)xevent->xclient.data.l[0];
2259
2260 if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
2261 {
2262 /* The delete window request specifies a window
2263 * to delete. We don't actually destroy the
2264 * window because "it is only a request". (The
2265 * window might contain vital data that the
2266 * program does not want destroyed). Instead
2267 * the event is passed along to the program,
2268 * which should then destroy the window.
2269 */
2270 GDK_NOTE (EVENTS,
2271 g_message ("delete window:\t\twindow: %ld",
2272 xevent->xclient.window));
2273
2274 event->any.type = GDK_DELETE;
2275
2276 gdk_x11_window_set_user_time (win, xevent->xclient.data.l[1]);
2277
2278 return GDK_FILTER_TRANSLATE;
2279 }
2280 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_TAKE_FOCUS"))
2281 {
2282 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
2283 GdkWindowObject *private = (GdkWindowObject *)win;
2284
2285 /* There is no way of knowing reliably whether we are viewable;
2286 * _gdk_x11_set_input_focus_safe() traps errors asynchronously.
2287 */
2288 if (toplevel && private->accept_focus)
2289 _gdk_x11_set_input_focus_safe (display, toplevel->focus_window,
2290 RevertToParent,
2291 xevent->xclient.data.l[1]);
2292
2293 return GDK_FILTER_REMOVE;
2294 }
2295 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_PING") &&
2296 !_gdk_x11_display_is_root_window (display,
2297 xevent->xclient.window))
2298 {
2299 XClientMessageEvent xclient = xevent->xclient;
2300
2301 xclient.window = GDK_WINDOW_XROOTWIN (win);
2302 XSendEvent (GDK_WINDOW_XDISPLAY (win),
2303 xclient.window,
2304 False,
2305 SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xclient);
2306
2307 return GDK_FILTER_REMOVE;
2308 }
2309 else if (atom == gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_SYNC_REQUEST") &&
2310 GDK_DISPLAY_X11 (display)->use_sync)
2311 {
2312 GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (event->any.window);
2313 if (toplevel)
2314 {
2315 #ifdef HAVE_XSYNC
2316 XSyncIntsToValue (&toplevel->pending_counter_value,
2317 xevent->xclient.data.l[2],
2318 xevent->xclient.data.l[3]);
2319 #endif
2320 }
2321 return GDK_FILTER_REMOVE;
2322 }
2323
2324 return GDK_FILTER_CONTINUE;
2325 }
2326
2327 void
2328 _gdk_events_queue (GdkDisplay *display)
2329 {
2330 GList *node;
2331 GdkEvent *event;
2332 XEvent xevent;
2333 Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
2334
2335 while (!_gdk_event_queue_find_first(display) && XPending (xdisplay))
2336 {
2337 XNextEvent (xdisplay, &xevent);
2338
2339 switch (xevent.type)
2340 {
2341 case KeyPress:
2342 case KeyRelease:
2343 break;
2344 default:
2345 if (XFilterEvent (&xevent, None))
2346 continue;
2347 }
2348
2349 event = gdk_event_new (GDK_NOTHING);
2350
2351 event->any.window = NULL;
2352 event->any.send_event = xevent.xany.send_event ? TRUE : FALSE;
2353
2354 ((GdkEventPrivate *)event)->flags |= GDK_EVENT_PENDING;
2355
2356 node = _gdk_event_queue_append (display, event);
2357
2358 if (gdk_event_translate (display, event, &xevent, FALSE))
2359 {
2360 ((GdkEventPrivate *)event)->flags &= ~GDK_EVENT_PENDING;
2361 _gdk_windowing_got_event (display, node, event, xevent.xany.serial);
2362 }
2363 else
2364 {
2365 _gdk_event_queue_remove_link (display, node);
2366 g_list_free_1 (node);
2367 gdk_event_free (event);
2368 }
2369 }
2370 }
2371
2372 static gboolean
2373 gdk_event_prepare (GSource *source,
2374 gint *timeout)
2375 {
2376 GdkDisplay *display = ((GdkDisplaySource*)source)->display;
2377 gboolean retval;
2378
2379 GDK_THREADS_ENTER ();
2380
2381 *timeout = -1;
2382 retval = (_gdk_event_queue_find_first (display) != NULL ||
2383 gdk_check_xpending (display));
2384
2385 GDK_THREADS_LEAVE ();
2386
2387 return retval;
2388 }
2389
2390 static gboolean
2391 gdk_event_check (GSource *source)
2392 {
2393 GdkDisplaySource *display_source = (GdkDisplaySource*)source;
2394 gboolean retval;
2395
2396 GDK_THREADS_ENTER ();
2397
2398 if (display_source->event_poll_fd.revents & G_IO_IN)
2399 retval = (_gdk_event_queue_find_first (display_source->display) != NULL ||
2400 gdk_check_xpending (display_source->display));
2401 else
2402 retval = FALSE;
2403
2404 GDK_THREADS_LEAVE ();
2405
2406 return retval;
2407 }
2408
2409 static gboolean
2410 gdk_event_dispatch (GSource *source,
2411 GSourceFunc callback,
2412 gpointer user_data)
2413 {
2414 GdkDisplay *display = ((GdkDisplaySource*)source)->display;
2415 GdkEvent *event;
2416
2417 GDK_THREADS_ENTER ();
2418
2419 _gdk_events_queue (display);
2420 event = _gdk_event_unqueue (display);
2421
2422 if (event)
2423 {
2424 if (_gdk_event_func)
2425 (*_gdk_event_func) (event, _gdk_event_data);
2426
2427 gdk_event_free (event);
2428 }
2429
2430 GDK_THREADS_LEAVE ();
2431
2432 return TRUE;
2433 }
2434
2435 /**
2436 * gdk_event_send_client_message_for_display:
2437 * @display: the #GdkDisplay for the window where the message is to be sent.
2438 * @event: the #GdkEvent to send, which should be a #GdkEventClient.
2439 * @winid: the window to send the client message to.
2440 *
2441 * On X11, sends an X ClientMessage event to a given window. On
2442 * Windows, sends a message registered with the name
2443 * GDK_WIN32_CLIENT_MESSAGE.
2444 *
2445 * This could be used for communicating between different
2446 * applications, though the amount of data is limited to 20 bytes on
2447 * X11, and to just four bytes on Windows.
2448 *
2449 * Returns: non-zero on success.
2450 *
2451 * Since: 2.2
2452 */
2453 gboolean
2454 gdk_event_send_client_message_for_display (GdkDisplay *display,
2455 GdkEvent *event,
2456 GdkNativeWindow winid)
2457 {
2458 XEvent sev;
2459
2460 g_return_val_if_fail(event != NULL, FALSE);
2461
2462 /* Set up our event to send, with the exception of its target window */
2463 sev.xclient.type = ClientMessage;
2464 sev.xclient.display = GDK_DISPLAY_XDISPLAY (display);
2465 sev.xclient.format = event->client.data_format;
2466 sev.xclient.window = winid;
2467 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2468 sev.xclient.message_type = gdk_x11_atom_to_xatom_for_display (display, event->client.message_type);
2469
2470 return _gdk_send_xevent (display, winid, False, NoEventMask, &sev);
2471 }
2472
2473
2474
2475 /* Sends a ClientMessage to all toplevel client windows */
2476 static gboolean
2477 gdk_event_send_client_message_to_all_recurse (GdkDisplay *display,
2478 XEvent *xev,
2479 guint32 xid,
2480 guint level)
2481 {
2482 Atom type = None;
2483 int format;
2484 unsigned long nitems, after;
2485 unsigned char *data;
2486 Window *ret_children, ret_root, ret_parent;
2487 unsigned int ret_nchildren;
2488 gboolean send = FALSE;
2489 gboolean found = FALSE;
2490 gboolean result = FALSE;
2491 int i;
2492
2493 gdk_error_trap_push ();
2494
2495 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), xid,
2496 gdk_x11_get_xatom_by_name_for_display (display, "WM_STATE"),
2497 0, 0, False, AnyPropertyType,
2498 &type, &format, &nitems, &after, &data) != Success)
2499 goto out;
2500
2501 if (type)
2502 {
2503 send = TRUE;
2504 XFree (data);
2505 }
2506 else
2507 {
2508 /* OK, we're all set, now let's find some windows to send this to */
2509 if (!XQueryTree (GDK_DISPLAY_XDISPLAY (display), xid,
2510 &ret_root, &ret_parent,
2511 &ret_children, &ret_nchildren))
2512 goto out;
2513
2514 for(i = 0; i < ret_nchildren; i++)
2515 if (gdk_event_send_client_message_to_all_recurse (display, xev, ret_children[i], level + 1))
2516 found = TRUE;
2517
2518 XFree (ret_children);
2519 }
2520
2521 if (send || (!found && (level == 1)))
2522 {
2523 xev->xclient.window = xid;
2524 _gdk_send_xevent (display, xid, False, NoEventMask, xev);
2525 }
2526
2527 result = send || found;
2528
2529 out:
2530 gdk_error_trap_pop ();
2531
2532 return result;
2533 }
2534
2535 /**
2536 * gdk_screen_broadcast_client_message:
2537 * @screen: the #GdkScreen where the event will be broadcasted.
2538 * @event: the #GdkEvent.
2539 *
2540 * On X11, sends an X ClientMessage event to all toplevel windows on
2541 * @screen.
2542 *
2543 * Toplevel windows are determined by checking for the WM_STATE property,
2544 * as described in the Inter-Client Communication Conventions Manual (ICCCM).
2545 * If no windows are found with the WM_STATE property set, the message is
2546 * sent to all children of the root window.
2547 *
2548 * On Windows, broadcasts a message registered with the name
2549 * GDK_WIN32_CLIENT_MESSAGE to all top-level windows. The amount of
2550 * data is limited to one long, i.e. four bytes.
2551 *
2552 * Since: 2.2
2553 */
2554
2555 void
2556 gdk_screen_broadcast_client_message (GdkScreen *screen,
2557 GdkEvent *event)
2558 {
2559 XEvent sev;
2560 GdkWindow *root_window;
2561
2562 g_return_if_fail (event != NULL);
2563
2564 root_window = gdk_screen_get_root_window (screen);
2565
2566 /* Set up our event to send, with the exception of its target window */
2567 sev.xclient.type = ClientMessage;
2568 sev.xclient.display = GDK_WINDOW_XDISPLAY (root_window);
2569 sev.xclient.format = event->client.data_format;
2570 memcpy(&sev.xclient.data, &event->client.data, sizeof (sev.xclient.data));
2571 sev.xclient.message_type =
2572 gdk_x11_atom_to_xatom_for_display (GDK_WINDOW_DISPLAY (root_window),
2573 event->client.message_type);
2574
2575 gdk_event_send_client_message_to_all_recurse (gdk_screen_get_display (screen),
2576 &sev,
2577 GDK_WINDOW_XID (root_window),
2578 0);
2579 }
2580
2581 /*
2582 *--------------------------------------------------------------
2583 * gdk_flush
2584 *
2585 * Flushes the Xlib output buffer and then waits
2586 * until all requests have been received and processed
2587 * by the X server. The only real use for this function
2588 * is in dealing with XShm.
2589 *
2590 * Arguments:
2591 *
2592 * Results:
2593 *
2594 * Side effects:
2595 *
2596 *--------------------------------------------------------------
2597 */
2598
2599 void
2600 gdk_flush (void)
2601 {
2602 GSList *tmp_list = _gdk_displays;
2603
2604 while (tmp_list)
2605 {
2606 XSync (GDK_DISPLAY_XDISPLAY (tmp_list->data), False);
2607 tmp_list = tmp_list->next;
2608 }
2609 }
2610
2611 static Bool
2612 timestamp_predicate (Display *display,
2613 XEvent *xevent,
2614 XPointer arg)
2615 {
2616 Window xwindow = GPOINTER_TO_UINT (arg);
2617 GdkDisplay *gdk_display = gdk_x11_lookup_xdisplay (display);
2618
2619 if (xevent->type == PropertyNotify &&
2620 xevent->xproperty.window == xwindow &&
2621 xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (gdk_display,
2622 "GDK_TIMESTAMP_PROP"))
2623 return True;
2624
2625 return False;
2626 }
2627
2628 /**
2629 * gdk_x11_get_server_time:
2630 * @window: a #GdkWindow, used for communication with the server.
2631 * The window must have GDK_PROPERTY_CHANGE_MASK in its
2632 * events mask or a hang will result.
2633 *
2634 * Routine to get the current X server time stamp.
2635 *
2636 * Return value: the time stamp.
2637 **/
2638 guint32
2639 gdk_x11_get_server_time (GdkWindow *window)
2640 {
2641 Display *xdisplay;
2642 Window xwindow;
2643 guchar c = 'a';
2644 XEvent xevent;
2645 Atom timestamp_prop_atom;
2646
2647 g_return_val_if_fail (GDK_IS_WINDOW (window), 0);
2648 g_return_val_if_fail (!GDK_WINDOW_DESTROYED (window), 0);
2649
2650 xdisplay = GDK_WINDOW_XDISPLAY (window);
2651 xwindow = GDK_WINDOW_XWINDOW (window);
2652 timestamp_prop_atom =
2653 gdk_x11_get_xatom_by_name_for_display (GDK_WINDOW_DISPLAY (window),
2654 "GDK_TIMESTAMP_PROP");
2655
2656 XChangeProperty (xdisplay, xwindow, timestamp_prop_atom,
2657 timestamp_prop_atom,
2658 8, PropModeReplace, &c, 1);
2659
2660 XIfEvent (xdisplay, &xevent,
2661 timestamp_predicate, GUINT_TO_POINTER(xwindow));
2662
2663 return xevent.xproperty.time;
2664 }
2665
2666 static void
2667 fetch_net_wm_check_window (GdkScreen *screen)
2668 {
2669 GdkScreenX11 *screen_x11;
2670 GdkDisplay *display;
2671 Atom type;
2672 gint format;
2673 gulong n_items;
2674 gulong bytes_after;
2675 guchar *data;
2676 Window *xwindow;
2677 GTimeVal tv;
2678 gint error;
2679
2680 screen_x11 = GDK_SCREEN_X11 (screen);
2681 display = screen_x11->display;
2682
2683 g_return_if_fail (GDK_DISPLAY_X11 (display)->trusted_client);
2684
2685 g_get_current_time (&tv);
2686
2687 if (ABS (tv.tv_sec - screen_x11->last_wmspec_check_time) < 15)
2688 return; /* we've checked recently */
2689
2690 screen_x11->last_wmspec_check_time = tv.tv_sec;
2691
2692 data = NULL;
2693 XGetWindowProperty (screen_x11->xdisplay, screen_x11->xroot_window,
2694 gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTING_WM_CHECK"),
2695 0, G_MAXLONG, False, XA_WINDOW, &type, &format,
2696 &n_items, &bytes_after, &data);
2697
2698 if (type != XA_WINDOW)
2699 {
2700 if (data)
2701 XFree (data);
2702 return;
2703 }
2704
2705 xwindow = (Window *)data;
2706
2707 if (screen_x11->wmspec_check_window == *xwindow)
2708 {
2709 XFree (xwindow);
2710 return;
2711 }
2712
2713 gdk_error_trap_push ();
2714
2715 /* Find out if this WM goes away, so we can reset everything. */
2716 XSelectInput (screen_x11->xdisplay, *xwindow, StructureNotifyMask);
2717 gdk_display_sync (display);
2718
2719 error = gdk_error_trap_pop ();
2720 if (!error)
2721 {
2722 screen_x11->wmspec_check_window = *xwindow;
2723 screen_x11->need_refetch_net_supported = TRUE;
2724 screen_x11->need_refetch_wm_name = TRUE;
2725
2726 /* Careful, reentrancy */
2727 _gdk_x11_screen_window_manager_changed (GDK_SCREEN (screen_x11));
2728 }
2729 else if (error == BadWindow)
2730 {
2731 /* Leftover property, try again immediately, new wm may be starting up */
2732 screen_x11->last_wmspec_check_time = 0;
2733 }
2734
2735 XFree (xwindow);
2736 }
2737
2738 /**
2739 * gdk_x11_screen_get_window_manager_name:
2740 * @screen: a #GdkScreen
2741 *
2742 * Returns the name of the window manager for @screen.
2743 *
2744 * Return value: the name of the window manager screen @screen, or
2745 * "unknown" if the window manager is unknown. The string is owned by GDK
2746 * and should not be freed.
2747 *
2748 * Since: 2.2
2749 **/
2750 const char*
2751 gdk_x11_screen_get_window_manager_name (GdkScreen *screen)
2752 {
2753 GdkScreenX11 *screen_x11;
2754
2755 screen_x11 = GDK_SCREEN_X11 (screen);
2756
2757 if (!G_LIKELY (GDK_DISPLAY_X11 (screen_x11->display)->trusted_client))
2758 return screen_x11->window_manager_name;
2759
2760 fetch_net_wm_check_window (screen);
2761
2762 if (screen_x11->need_refetch_wm_name)
2763 {
2764 /* Get the name of the window manager */
2765 screen_x11->need_refetch_wm_name = FALSE;
2766
2767 g_free (screen_x11->window_manager_name);
2768 screen_x11->window_manager_name = g_strdup ("unknown");
2769
2770 if (screen_x11->wmspec_check_window != None)
2771 {
2772 Atom type;
2773 gint format;
2774 gulong n_items;
2775 gulong bytes_after;
2776 gchar *name;
2777
2778 name = NULL;
2779
2780 gdk_error_trap_push ();
2781
2782 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (screen_x11->display),
2783 screen_x11->wmspec_check_window,
2784 gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2785 "_NET_WM_NAME"),
2786 0, G_MAXLONG, False,
2787 gdk_x11_get_xatom_by_name_for_display (screen_x11->display,
2788 "UTF8_STRING"),
2789 &type, &format,
2790 &n_items, &bytes_after,
2791 (guchar **)&name);
2792
2793 gdk_display_sync (screen_x11->display);
2794
2795 gdk_error_trap_pop ();
2796
2797 if (name != NULL)
2798 {
2799 g_free (screen_x11->window_manager_name);
2800 screen_x11->window_manager_name = g_strdup (name);
2801 XFree (name);
2802 }
2803 }
2804 }
2805
2806 return GDK_SCREEN_X11 (screen)->window_manager_name;
2807 }
2808
2809 typedef struct _NetWmSupportedAtoms NetWmSupportedAtoms;
2810
2811 struct _NetWmSupportedAtoms
2812 {
2813 Atom *atoms;
2814 gulong n_atoms;
2815 };
2816
2817 static void
2818 cleanup_atoms(gpointer data)
2819 {
2820 NetWmSupportedAtoms *supported_atoms = data;
2821 if (supported_atoms->atoms)
2822 XFree (supported_atoms->atoms);
2823 g_free (supported_atoms);
2824 }
2825
2826 /**
2827 * gdk_x11_screen_supports_net_wm_hint:
2828 * @screen: the relevant #GdkScreen.
2829 * @property: a property atom.
2830 *
2831 * This function is specific to the X11 backend of GDK, and indicates
2832 * whether the window manager supports a certain hint from the
2833 * Extended Window Manager Hints Specification. You can find this
2834 * specification on
2835 * <ulink url="http://www.freedesktop.org">http://www.freedesktop.org</ulink>.
2836 *
2837 * When using this function, keep in mind that the window manager
2838 * can change over time; so you shouldn't use this function in
2839 * a way that impacts persistent application state. A common bug
2840 * is that your application can start up before the window manager
2841 * does when the user logs in, and before the window manager starts
2842 * gdk_x11_screen_supports_net_wm_hint() will return %FALSE for every property.
2843 * You can monitor the window_manager_changed signal on #GdkScreen to detect
2844 * a window manager change.
2845 *
2846 * Return value: %TRUE if the window manager supports @property
2847 *
2848 * Since: 2.2
2849 **/
2850 gboolean
2851 gdk_x11_screen_supports_net_wm_hint (GdkScreen *screen,
2852 GdkAtom property)
2853 {
2854 gulong i;
2855 GdkScreenX11 *screen_x11;
2856 NetWmSupportedAtoms *supported_atoms;
2857 GdkDisplay *display;
2858
2859 g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
2860
2861 screen_x11 = GDK_SCREEN_X11 (screen);
2862 display = screen_x11->display;
2863
2864 if (!G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
2865 return FALSE;
2866
2867 supported_atoms = g_object_get_data (G_OBJECT (screen), "gdk-net-wm-supported-atoms");
2868 if (!supported_atoms)
2869 {
2870 supported_atoms = g_new0 (NetWmSupportedAtoms, 1);
2871 g_object_set_data_full (G_OBJECT (screen), "gdk-net-wm-supported-atoms", supported_atoms, cleanup_atoms);
2872 }
2873
2874 fetch_net_wm_check_window (screen);
2875
2876 if (screen_x11->wmspec_check_window == None)
2877 return FALSE;
2878
2879 if (screen_x11->need_refetch_net_supported)
2880 {
2881 /* WM has changed since we last got the supported list,
2882 * refetch it.
2883 */
2884 Atom type;
2885 gint format;
2886 gulong bytes_after;
2887
2888 screen_x11->need_refetch_net_supported = FALSE;
2889
2890 if (supported_atoms->atoms)
2891 XFree (supported_atoms->atoms);
2892
2893 supported_atoms->atoms = NULL;
2894 supported_atoms->n_atoms = 0;
2895
2896 XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), screen_x11->xroot_window,
2897 gdk_x11_get_xatom_by_name_for_display (display, "_NET_SUPPORTED"),
2898 0, G_MAXLONG, False, XA_ATOM, &type, &format,
2899 &supported_atoms->n_atoms, &bytes_after,
2900 (guchar **)&supported_atoms->atoms);
2901
2902 if (type != XA_ATOM)
2903 return FALSE;
2904 }
2905
2906 if (supported_atoms->atoms == NULL)
2907 return FALSE;
2908
2909 i = 0;
2910 while (i < supported_atoms->n_atoms)
2911 {
2912 if (supported_atoms->atoms[i] == gdk_x11_atom_to_xatom_for_display (display, property))
2913 return TRUE;
2914
2915 ++i;
2916 }
2917
2918 return FALSE;
2919 }
2920
2921 /**
2922 * gdk_net_wm_supports:
2923 * @property: a property atom.
2924 *
2925 * This function is specific to the X11 backend of GDK, and indicates
2926 * whether the window manager for the default screen supports a certain
2927 * hint from the Extended Window Manager Hints Specification. See
2928 * gdk_x11_screen_supports_net_wm_hint() for complete details.
2929 *
2930 * Return value: %TRUE if the window manager supports @property
2931 *
2932 * Deprecated:2.24: Use gdk_x11_screen_supports_net_wm_hint() instead
2933 **/
2934 gboolean
2935 gdk_net_wm_supports (GdkAtom property)
2936 {
2937 return gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (), property);
2938 }
2939
2940
2941 static void
2942 gdk_xsettings_notify_cb (const char *name,
2943 XSettingsAction action,
2944 XSettingsSetting *setting,
2945 void *data)
2946 {
2947 GdkEvent new_event;
2948 GdkScreen *screen = data;
2949 GdkScreenX11 *screen_x11 = data;
2950 int i;
2951
2952 if (screen_x11->xsettings_in_init)
2953 return;
2954
2955 new_event.type = GDK_SETTING;
2956 new_event.setting.window = gdk_screen_get_root_window (screen);
2957 new_event.setting.send_event = FALSE;
2958 new_event.setting.name = NULL;
2959
2960 for (i = 0; i < GDK_SETTINGS_N_ELEMENTS() ; i++)
2961 if (strcmp (GDK_SETTINGS_X_NAME (i), name) == 0)
2962 {
2963 new_event.setting.name = (char*) GDK_SETTINGS_GDK_NAME (i);
2964 break;
2965 }
2966
2967 if (!new_event.setting.name)
2968 return;
2969
2970 switch (action)
2971 {
2972 case XSETTINGS_ACTION_NEW:
2973 new_event.setting.action = GDK_SETTING_ACTION_NEW;
2974 break;
2975 case XSETTINGS_ACTION_CHANGED:
2976 new_event.setting.action = GDK_SETTING_ACTION_CHANGED;
2977 break;
2978 case XSETTINGS_ACTION_DELETED:
2979 new_event.setting.action = GDK_SETTING_ACTION_DELETED;
2980 break;
2981 }
2982
2983 gdk_event_put (&new_event);
2984 }
2985
2986 static gboolean
2987 check_transform (const gchar *xsettings_name,
2988 GType src_type,
2989 GType dest_type)
2990 {
2991 if (!g_value_type_transformable (src_type, dest_type))
2992 {
2993 g_warning ("Cannot transform xsetting %s of type %s to type %s\n",
2994 xsettings_name,
2995 g_type_name (src_type),
2996 g_type_name (dest_type));
2997 return FALSE;
2998 }
2999 else
3000 return TRUE;
3001 }
3002
3003 /**
3004 * gdk_screen_get_setting:
3005 * @screen: the #GdkScreen where the setting is located
3006 * @name: the name of the setting
3007 * @value: location to store the value of the setting
3008 *
3009 * Retrieves a desktop-wide setting such as double-click time
3010 * for the #GdkScreen @screen.
3011 *
3012 * FIXME needs a list of valid settings here, or a link to
3013 * more information.
3014 *
3015 * Returns: %TRUE if the setting existed and a value was stored
3016 * in @value, %FALSE otherwise.
3017 *
3018 * Since: 2.2
3019 **/
3020 gboolean
3021 gdk_screen_get_setting (GdkScreen *screen,
3022 const gchar *name,
3023 GValue *value)
3024 {
3025
3026 const char *xsettings_name = NULL;
3027 XSettingsResult result;
3028 XSettingsSetting *setting = NULL;
3029 GdkScreenX11 *screen_x11;
3030 gboolean success = FALSE;
3031 gint i;
3032 GValue tmp_val = { 0, };
3033
3034 g_return_val_if_fail (GDK_IS_SCREEN (screen), FALSE);
3035
3036 screen_x11 = GDK_SCREEN_X11 (screen);
3037
3038 for (i = 0; i < GDK_SETTINGS_N_ELEMENTS(); i++)
3039 if (strcmp (GDK_SETTINGS_GDK_NAME (i), name) == 0)
3040 {
3041 xsettings_name = GDK_SETTINGS_X_NAME (i);
3042 break;
3043 }
3044
3045 if (!xsettings_name)
3046 goto out;
3047
3048 result = xsettings_client_get_setting (screen_x11->xsettings_client,
3049 xsettings_name, &setting);
3050 if (result != XSETTINGS_SUCCESS)
3051 goto out;
3052
3053 switch (setting->type)
3054 {
3055 case XSETTINGS_TYPE_INT:
3056 if (check_transform (xsettings_name, G_TYPE_INT, G_VALUE_TYPE (value)))
3057 {
3058 g_value_init (&tmp_val, G_TYPE_INT);
3059 g_value_set_int (&tmp_val, setting->data.v_int);
3060 g_value_transform (&tmp_val, value);
3061
3062 success = TRUE;
3063 }
3064 break;
3065 case XSETTINGS_TYPE_STRING:
3066 if (check_transform (xsettings_name, G_TYPE_STRING, G_VALUE_TYPE (value)))
3067 {
3068 g_value_init (&tmp_val, G_TYPE_STRING);
3069 g_value_set_string (&tmp_val, setting->data.v_string);
3070 g_value_transform (&tmp_val, value);
3071
3072 success = TRUE;
3073 }
3074 break;
3075 case XSETTINGS_TYPE_COLOR:
3076 if (!check_transform (xsettings_name, GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
3077 {
3078 GdkColor color;
3079
3080 g_value_init (&tmp_val, GDK_TYPE_COLOR);
3081
3082 color.pixel = 0;
3083 color.red = setting->data.v_color.red;
3084 color.green = setting->data.v_color.green;
3085 color.blue = setting->data.v_color.blue;
3086
3087 g_value_set_boxed (&tmp_val, &color);
3088
3089 g_value_transform (&tmp_val, value);
3090
3091 success = TRUE;
3092 }
3093 break;
3094 }
3095
3096 g_value_unset (&tmp_val);
3097
3098 out:
3099 if (setting)
3100 xsettings_setting_free (setting);
3101
3102 if (success)
3103 return TRUE;
3104 else
3105 return _gdk_x11_get_xft_setting (screen, name, value);
3106 }
3107
3108 static GdkFilterReturn
3109 gdk_xsettings_client_event_filter (GdkXEvent *xevent,
3110 GdkEvent *event,
3111 gpointer data)
3112 {
3113 GdkScreenX11 *screen = data;
3114
3115 if (xsettings_client_process_event (screen->xsettings_client, (XEvent *)xevent))
3116 return GDK_FILTER_REMOVE;
3117 else
3118 return GDK_FILTER_CONTINUE;
3119 }
3120
3121 static Bool
3122 gdk_xsettings_watch_cb (Window window,
3123 Bool is_start,
3124 long mask,
3125 void *cb_data)
3126 {
3127 GdkWindow *gdkwin;
3128 GdkScreen *screen = cb_data;
3129
3130 gdkwin = gdk_window_lookup_for_display (gdk_screen_get_display (screen), window);
3131
3132 if (is_start)
3133 {
3134 if (gdkwin)
3135 g_object_ref (gdkwin);
3136 else
3137 {
3138 gdkwin = gdk_window_foreign_new_for_display (gdk_screen_get_display (screen), window);
3139
3140 /* gdk_window_foreign_new_for_display() can fail and return NULL if the
3141 * window has already been destroyed.
3142 */
3143 if (!gdkwin)
3144 return False;
3145 }
3146
3147 gdk_window_add_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
3148 }
3149 else
3150 {
3151 if (!gdkwin)
3152 {
3153 /* gdkwin should not be NULL here, since if starting the watch succeeded
3154 * we have a reference on the window. It might mean that the caller didn't
3155 * remove the watch when it got a DestroyNotify event. Or maybe the
3156 * caller ignored the return value when starting the watch failed.
3157 */
3158 g_warning ("gdk_xsettings_watch_cb(): Couldn't find window to unwatch");
3159 return False;
3160 }
3161
3162 gdk_window_remove_filter (gdkwin, gdk_xsettings_client_event_filter, screen);
3163 g_object_unref (gdkwin);
3164 }
3165
3166 return True;
3167 }
3168
3169 void
3170 _gdk_windowing_event_data_copy (const GdkEvent *src,
3171 GdkEvent *dst)
3172 {
3173 }
3174
3175 void
3176 _gdk_windowing_event_data_free (GdkEvent *event)
3177 {
3178 }
3179
3180 #define __GDK_EVENTS_X11_C__
3181 #include "gdkaliasdef.c"