| 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 "gdkeventsource.h" |
| 21 |
|
| 22 |
#include "gdkinternals.h" |
| 23 |
#include "gdkwindow-x11.h" |
| 24 |
#include "gdkprivate-x11.h" |
| 25 |
|
| 26 |
|
| 27 |
static gboolean gdk_event_source_prepare (GSource *source, |
| 28 |
gint *timeout); |
| 29 |
static gboolean gdk_event_source_check (GSource *source); |
| 30 |
static gboolean gdk_event_source_dispatch (GSource *source, |
| 31 |
GSourceFunc callback, |
| 32 |
gpointer user_data); |
| 33 |
static void gdk_event_source_finalize (GSource *source); |
| 34 |
|
| 35 |
#define HAS_FOCUS(toplevel) \ |
| 36 |
((toplevel)->has_focus || (toplevel)->has_pointer_focus) |
| 37 |
|
| 38 |
struct _GdkEventSource |
| 39 |
{ |
| 40 |
GSource source; |
| 41 |
|
| 42 |
GdkDisplay *display; |
| 43 |
GPollFD event_poll_fd; |
| 44 |
GList *translators; |
| 45 |
}; |
| 46 |
|
| 47 |
static GSourceFuncs event_funcs = { |
| 48 |
gdk_event_source_prepare, |
| 49 |
gdk_event_source_check, |
| 50 |
gdk_event_source_dispatch, |
| 51 |
gdk_event_source_finalize |
| 52 |
}; |
| 53 |
|
| 54 |
static gint |
| 55 |
gdk_event_apply_filters (XEvent *xevent, |
| 56 |
GdkEvent *event, |
| 57 |
GdkWindow *window) |
| 58 |
{ |
| 59 |
GList *tmp_list; |
| 60 |
GdkFilterReturn result; |
| 61 |
|
| 62 |
if (window == NULL) |
| 63 |
tmp_list = _gdk_default_filters; |
| 64 |
else |
| 65 |
tmp_list = window->filters; |
| 66 |
|
| 67 |
while (tmp_list) |
| 68 |
{ |
| 69 |
GdkEventFilter *filter = (GdkEventFilter*) tmp_list->data; |
| 70 |
GList *node; |
| 71 |
|
| 72 |
if ((filter->flags & GDK_EVENT_FILTER_REMOVED) != 0) |
| 73 |
{ |
| 74 |
tmp_list = tmp_list->next; |
| 75 |
continue; |
| 76 |
} |
| 77 |
|
| 78 |
filter->ref_count++; |
| 79 |
result = filter->function (xevent, event, filter->data); |
| 80 |
|
| 81 |
/* Protect against unreffing the filter mutating the list */ |
| 82 |
node = tmp_list->next; |
| 83 |
|
| 84 |
_gdk_event_filter_unref (window, filter); |
| 85 |
|
| 86 |
tmp_list = node; |
| 87 |
|
| 88 |
if (result != GDK_FILTER_CONTINUE) |
| 89 |
return result; |
| 90 |
} |
| 91 |
|
| 92 |
return GDK_FILTER_CONTINUE; |
| 93 |
} |
| 94 |
|
| 95 |
static GdkWindow * |
| 96 |
gdk_event_source_get_filter_window (GdkEventSource *event_source, |
| 97 |
XEvent *xevent, |
| 98 |
GdkEventTranslator **event_translator) |
| 99 |
{ |
| 100 |
GList *list = event_source->translators; |
| 101 |
GdkWindow *window; |
| 102 |
|
| 103 |
*event_translator = NULL; |
| 104 |
|
| 105 |
while (list) |
| 106 |
{ |
| 107 |
GdkEventTranslator *translator = list->data; |
| 108 |
|
| 109 |
list = list->next; |
| 110 |
window = _gdk_x11_event_translator_get_window (translator, |
| 111 |
event_source->display, |
| 112 |
xevent); |
| 113 |
if (window) |
| 114 |
{ |
| 115 |
*event_translator = translator; |
| 116 |
return window; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
window = gdk_x11_window_lookup_for_display (event_source->display, |
| 121 |
xevent->xany.window); |
| 122 |
|
| 123 |
if (window && !GDK_IS_WINDOW (window)) |
| 124 |
window = NULL; |
| 125 |
|
| 126 |
return window; |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
handle_focus_change (GdkEventCrossing *event) |
| 131 |
{ |
| 132 |
GdkToplevelX11 *toplevel; |
| 133 |
GdkX11Screen *x11_screen; |
| 134 |
gboolean focus_in, had_focus; |
| 135 |
|
| 136 |
toplevel = _gdk_x11_window_get_toplevel (event->window); |
| 137 |
x11_screen = GDK_X11_SCREEN (gdk_window_get_screen (event->window)); |
| 138 |
focus_in = (event->type == GDK_ENTER_NOTIFY); |
| 139 |
|
| 140 |
if (x11_screen->wmspec_check_window) |
| 141 |
return; |
| 142 |
|
| 143 |
if (!toplevel || event->detail == GDK_NOTIFY_INFERIOR) |
| 144 |
return; |
| 145 |
|
| 146 |
toplevel->has_pointer = focus_in; |
| 147 |
|
| 148 |
if (!event->focus || toplevel->has_focus_window) |
| 149 |
return; |
| 150 |
|
| 151 |
had_focus = HAS_FOCUS (toplevel); |
| 152 |
toplevel->has_pointer_focus = focus_in; |
| 153 |
|
| 154 |
if (HAS_FOCUS (toplevel) != had_focus) |
| 155 |
{ |
| 156 |
GdkEvent *focus_event; |
| 157 |
|
| 158 |
focus_event = gdk_event_new (GDK_FOCUS_CHANGE); |
| 159 |
focus_event->focus_change.window = g_object_ref (event->window); |
| 160 |
focus_event->focus_change.send_event = FALSE; |
| 161 |
focus_event->focus_change.in = focus_in; |
| 162 |
gdk_event_set_device (focus_event, gdk_event_get_device ((GdkEvent *) event)); |
| 163 |
|
| 164 |
gdk_event_put (focus_event); |
| 165 |
gdk_event_free (focus_event); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
static GdkEvent * |
| 170 |
gdk_event_source_translate_event (GdkEventSource *event_source, |
| 171 |
XEvent *xevent) |
| 172 |
{ |
| 173 |
GdkEvent *event = gdk_event_new (GDK_NOTHING); |
| 174 |
GdkFilterReturn result = GDK_FILTER_CONTINUE; |
| 175 |
GdkEventTranslator *event_translator; |
| 176 |
GdkWindow *filter_window; |
| 177 |
Display *dpy; |
| 178 |
|
| 179 |
dpy = GDK_DISPLAY_XDISPLAY (event_source->display); |
| 180 |
|
| 181 |
#ifdef HAVE_XGENERICEVENTS |
| 182 |
/* Get cookie data here so it's available |
| 183 |
* to every event translator and event filter. |
| 184 |
*/ |
| 185 |
if (xevent->type == GenericEvent) |
| 186 |
XGetEventData (dpy, &xevent->xcookie); |
| 187 |
#endif |
| 188 |
|
| 189 |
filter_window = gdk_event_source_get_filter_window (event_source, xevent, |
| 190 |
&event_translator); |
| 191 |
if (filter_window) |
| 192 |
event->any.window = g_object_ref (filter_window); |
| 193 |
|
| 194 |
/* Run default filters */ |
| 195 |
if (_gdk_default_filters) |
| 196 |
{ |
| 197 |
/* Apply global filters */ |
| 198 |
result = gdk_event_apply_filters (xevent, event, NULL); |
| 199 |
} |
| 200 |
|
| 201 |
if (result == GDK_FILTER_CONTINUE && |
| 202 |
filter_window && filter_window->filters) |
| 203 |
{ |
| 204 |
/* Apply per-window filters */ |
| 205 |
result = gdk_event_apply_filters (xevent, event, filter_window); |
| 206 |
} |
| 207 |
|
| 208 |
if (result != GDK_FILTER_CONTINUE) |
| 209 |
{ |
| 210 |
#ifdef HAVE_XGENERICEVENTS |
| 211 |
if (xevent->type == GenericEvent) |
| 212 |
XFreeEventData (dpy, &xevent->xcookie); |
| 213 |
#endif |
| 214 |
|
| 215 |
if (result == GDK_FILTER_REMOVE) |
| 216 |
{ |
| 217 |
gdk_event_free (event); |
| 218 |
return NULL; |
| 219 |
} |
| 220 |
else /* GDK_FILTER_TRANSLATE */ |
| 221 |
return event; |
| 222 |
} |
| 223 |
|
| 224 |
gdk_event_free (event); |
| 225 |
event = NULL; |
| 226 |
|
| 227 |
if (event_translator) |
| 228 |
{ |
| 229 |
/* Event translator was gotten before in get_filter_window() */ |
| 230 |
event = _gdk_x11_event_translator_translate (event_translator, |
| 231 |
event_source->display, |
| 232 |
xevent); |
| 233 |
} |
| 234 |
else |
| 235 |
{ |
| 236 |
GList *list = event_source->translators; |
| 237 |
|
| 238 |
while (list && !event) |
| 239 |
{ |
| 240 |
GdkEventTranslator *translator = list->data; |
| 241 |
|
| 242 |
list = list->next; |
| 243 |
event = _gdk_x11_event_translator_translate (translator, |
| 244 |
event_source->display, |
| 245 |
xevent); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (event && |
| 250 |
(event->type == GDK_ENTER_NOTIFY || |
| 251 |
event->type == GDK_LEAVE_NOTIFY) && |
| 252 |
event->crossing.window != NULL) |
| 253 |
{ |
| 254 |
/* Handle focusing (in the case where no window manager is running */ |
| 255 |
handle_focus_change (&event->crossing); |
| 256 |
} |
| 257 |
|
| 258 |
#ifdef HAVE_XGENERICEVENTS |
| 259 |
if (xevent->type == GenericEvent) |
| 260 |
XFreeEventData (dpy, &xevent->xcookie); |
| 261 |
#endif |
| 262 |
|
| 263 |
return event; |
| 264 |
} |
| 265 |
|
| 266 |
static gboolean |
| 267 |
gdk_check_xpending (GdkDisplay *display) |
| 268 |
{ |
| 269 |
return XPending (GDK_DISPLAY_XDISPLAY (display)); |
| 270 |
} |
| 271 |
|
| 272 |
static gboolean |
| 273 |
gdk_event_source_prepare (GSource *source, |
| 274 |
gint *timeout) |
| 275 |
{ |
| 276 |
GdkDisplay *display = ((GdkEventSource*) source)->display; |
| 277 |
gboolean retval; |
| 278 |
|
| 279 |
gdk_threads_enter (); |
| 280 |
|
| 281 |
*timeout = -1; |
| 282 |
|
| 283 |
if (display->event_pause_count > 0) |
| 284 |
retval = _gdk_event_queue_find_first (display) != NULL; |
| 285 |
else |
| 286 |
retval = (_gdk_event_queue_find_first (display) != NULL || |
| 287 |
gdk_check_xpending (display)); |
| 288 |
|
| 289 |
gdk_threads_leave (); |
| 290 |
|
| 291 |
return retval; |
| 292 |
} |
| 293 |
|
| 294 |
static gboolean |
| 295 |
gdk_event_source_check (GSource *source) |
| 296 |
{ |
| 297 |
GdkEventSource *event_source = (GdkEventSource*) source; |
| 298 |
gboolean retval; |
| 299 |
|
| 300 |
gdk_threads_enter (); |
| 301 |
|
| 302 |
if (event_source->display->event_pause_count > 0) |
| 303 |
retval = _gdk_event_queue_find_first (event_source->display) != NULL; |
| 304 |
else if (event_source->event_poll_fd.revents & G_IO_IN) |
| 305 |
retval = (_gdk_event_queue_find_first (event_source->display) != NULL || |
| 306 |
gdk_check_xpending (event_source->display)); |
| 307 |
else |
| 308 |
retval = FALSE; |
| 309 |
|
| 310 |
gdk_threads_leave (); |
| 311 |
|
| 312 |
return retval; |
| 313 |
} |
| 314 |
|
| 315 |
void |
| 316 |
_gdk_x11_display_queue_events (GdkDisplay *display) |
| 317 |
{ |
| 318 |
GdkEvent *event; |
| 319 |
XEvent xevent; |
| 320 |
Display *xdisplay = GDK_DISPLAY_XDISPLAY (display); |
| 321 |
GdkEventSource *event_source; |
| 322 |
GdkX11Display *display_x11; |
| 323 |
|
| 324 |
display_x11 = GDK_X11_DISPLAY (display); |
| 325 |
event_source = (GdkEventSource *) display_x11->event_source; |
| 326 |
|
| 327 |
while (!_gdk_event_queue_find_first (display) && XPending (xdisplay)) |
| 328 |
{ |
| 329 |
XNextEvent (xdisplay, &xevent); |
| 330 |
|
| 331 |
switch (xevent.type) |
| 332 |
{ |
| 333 |
case KeyPress: |
| 334 |
case KeyRelease: |
| 335 |
break; |
| 336 |
default: |
| 337 |
if (XFilterEvent (&xevent, None)) |
| 338 |
continue; |
| 339 |
} |
| 340 |
|
| 341 |
event = gdk_event_source_translate_event (event_source, &xevent); |
| 342 |
|
| 343 |
if (event) |
| 344 |
{ |
| 345 |
GList *node; |
| 346 |
|
| 347 |
node = _gdk_event_queue_append (display, event); |
| 348 |
_gdk_windowing_got_event (display, node, event, xevent.xany.serial); |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
static gboolean |
| 354 |
gdk_event_source_dispatch (GSource *source, |
| 355 |
GSourceFunc callback, |
| 356 |
gpointer user_data) |
| 357 |
{ |
| 358 |
GdkDisplay *display = ((GdkEventSource*) source)->display; |
| 359 |
GdkEvent *event; |
| 360 |
|
| 361 |
gdk_threads_enter (); |
| 362 |
|
| 363 |
event = gdk_display_get_event (display); |
| 364 |
|
| 365 |
if (event) |
| 366 |
{ |
| 367 |
_gdk_event_emit (event); |
| 368 |
|
| 369 |
gdk_event_free (event); |
| 370 |
} |
| 371 |
|
| 372 |
gdk_threads_leave (); |
| 373 |
|
| 374 |
return TRUE; |
| 375 |
} |
| 376 |
|
| 377 |
static void |
| 378 |
gdk_event_source_finalize (GSource *source) |
| 379 |
{ |
| 380 |
GdkEventSource *event_source = (GdkEventSource *)source; |
| 381 |
|
| 382 |
g_list_free (event_source->translators); |
| 383 |
event_source->translators = NULL; |
| 384 |
} |
| 385 |
|
| 386 |
GSource * |
| 387 |
gdk_x11_event_source_new (GdkDisplay *display) |
| 388 |
{ |
| 389 |
GSource *source; |
| 390 |
GdkEventSource *event_source; |
| 391 |
GdkX11Display *display_x11; |
| 392 |
int connection_number; |
| 393 |
char *name; |
| 394 |
|
| 395 |
source = g_source_new (&event_funcs, sizeof (GdkEventSource)); |
| 396 |
name = g_strdup_printf ("GDK X11 Event source (%s)", |
| 397 |
gdk_display_get_name (display)); |
| 398 |
g_source_set_name (source, name); |
| 399 |
g_free (name); |
| 400 |
event_source = (GdkEventSource *) source; |
| 401 |
event_source->display = display; |
| 402 |
|
| 403 |
display_x11 = GDK_X11_DISPLAY (display); |
| 404 |
connection_number = ConnectionNumber (display_x11->xdisplay); |
| 405 |
|
| 406 |
event_source->event_poll_fd.fd = connection_number; |
| 407 |
event_source->event_poll_fd.events = G_IO_IN; |
| 408 |
g_source_add_poll (source, &event_source->event_poll_fd); |
| 409 |
|
| 410 |
g_source_set_priority (source, GDK_PRIORITY_EVENTS); |
| 411 |
g_source_set_can_recurse (source, TRUE); |
| 412 |
g_source_attach (source, NULL); |
| 413 |
|
| 414 |
return source; |
| 415 |
} |
| 416 |
|
| 417 |
void |
| 418 |
gdk_x11_event_source_add_translator (GdkEventSource *source, |
| 419 |
GdkEventTranslator *translator) |
| 420 |
{ |
| 421 |
g_return_if_fail (GDK_IS_EVENT_TRANSLATOR (translator)); |
| 422 |
|
| 423 |
source->translators = g_list_append (source->translators, translator); |
| 424 |
} |
| 425 |
|
| 426 |
void |
| 427 |
gdk_x11_event_source_select_events (GdkEventSource *source, |
| 428 |
Window window, |
| 429 |
GdkEventMask event_mask, |
| 430 |
unsigned int extra_x_mask) |
| 431 |
{ |
| 432 |
unsigned int xmask = extra_x_mask; |
| 433 |
GList *list; |
| 434 |
gint i; |
| 435 |
|
| 436 |
list = source->translators; |
| 437 |
|
| 438 |
while (list) |
| 439 |
{ |
| 440 |
GdkEventTranslator *translator = list->data; |
| 441 |
GdkEventMask translator_mask, mask; |
| 442 |
|
| 443 |
translator_mask = _gdk_x11_event_translator_get_handled_events (translator); |
| 444 |
mask = event_mask & translator_mask; |
| 445 |
|
| 446 |
if (mask != 0) |
| 447 |
{ |
| 448 |
_gdk_x11_event_translator_select_window_events (translator, window, mask); |
| 449 |
event_mask &= ~mask; |
| 450 |
} |
| 451 |
|
| 452 |
list = list->next; |
| 453 |
} |
| 454 |
|
| 455 |
for (i = 0; i < _gdk_x11_event_mask_table_size; i++) |
| 456 |
{ |
| 457 |
if (event_mask & (1 << (i + 1))) |
| 458 |
xmask |= _gdk_x11_event_mask_table[i]; |
| 459 |
} |
| 460 |
|
| 461 |
XSelectInput (GDK_DISPLAY_XDISPLAY (source->display), window, xmask); |
| 462 |
} |