gdkevents-x11.c 84.6 KB
Newer Older
1
/* GDK - The GIMP Drawing Kit
2 3
 * Copyright (C) 1995-2007 Peter Mattis, Spencer Kimball,
 * 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 8 9 10 11 12
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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 17 18 19 20
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

21
/*
22
 * Modified by the GTK+ Team and others 1997-2007.  See the AUTHORS
23 24 25 26 27
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

28
#include "config.h"
29

30
#include "gdk.h"
31 32
#include "gdkprivate-x11.h"
#include "gdkinternals.h"
Owen Taylor's avatar
Started  
Owen Taylor committed
33
#include "gdkx.h"
34 35
#include "gdkscreen-x11.h"
#include "gdkdisplay-x11.h"
36
#include "gdkasync.h"
Owen Taylor's avatar
Started  
Owen Taylor committed
37

38 39
#include "gdkkeysyms.h"

40 41
#include "xsettings-client.h"

42
#include <string.h>
43

Owen Taylor's avatar
Started  
Owen Taylor committed
44
#include "gdkinputprivate.h"
45
#include "gdksettings.c"
46 47
#include "gdkalias.h"

48

Havoc Pennington's avatar
Havoc Pennington committed
49 50 51 52
#ifdef HAVE_XKB
#include <X11/XKBlib.h>
#endif

53 54 55 56
#ifdef HAVE_XSYNC
#include <X11/extensions/sync.h>
#endif

Matthias Clasen's avatar
Matthias Clasen committed
57 58 59 60
#ifdef HAVE_XFIXES
#include <X11/extensions/Xfixes.h>
#endif

Søren Sandmann's avatar
Søren Sandmann committed
61 62 63 64
#ifdef HAVE_RANDR
#include <X11/extensions/Xrandr.h>
#endif

Havoc Pennington's avatar
Havoc Pennington committed
65 66
#include <X11/Xatom.h>

67
typedef struct _GdkIOClosure GdkIOClosure;
68
typedef struct _GdkDisplaySource GdkDisplaySource;
69
typedef struct _GdkEventTypeX11 GdkEventTypeX11;
70

71 72
struct _GdkIOClosure
{
73 74
  GdkInputFunction function;
  GdkInputCondition condition;
Michael Natterer's avatar
Michael Natterer committed
75
  GDestroyNotify notify;
76 77 78
  gpointer data;
};

79 80 81 82 83 84 85 86
struct _GdkDisplaySource
{
  GSource source;
  
  GdkDisplay *display;
  GPollFD event_poll_fd;
};

87 88 89 90 91 92
struct _GdkEventTypeX11
{
  gint base;
  gint n_events;
};

93 94 95 96
/* 
 * Private function declarations
 */

97
static gint	 gdk_event_apply_filters (XEvent   *xevent,
98
					  GdkEvent *event,
99
					  GList    *filters);
100 101 102 103
static gboolean	 gdk_event_translate	 (GdkDisplay *display,
					  GdkEvent   *event, 
					  XEvent     *xevent,
					  gboolean    return_exposes);
104

105 106 107 108 109 110
static gboolean gdk_event_prepare  (GSource     *source,
				    gint        *timeout);
static gboolean gdk_event_check    (GSource     *source);
static gboolean gdk_event_dispatch (GSource     *source,
				    GSourceFunc  callback,
				    gpointer     user_data);
111

112 113 114
static GdkFilterReturn gdk_wm_protocols_filter (GdkXEvent *xev,
						GdkEvent  *event,
						gpointer   data);
115

116 117 118
static GSource *gdk_display_source_new (GdkDisplay *display);
static gboolean gdk_check_xpending     (GdkDisplay *display);

119
static Bool gdk_xsettings_watch_cb  (Window            window,
120 121 122 123 124 125 126 127
				     Bool              is_start,
				     long              mask,
				     void             *cb_data);
static void gdk_xsettings_notify_cb (const char       *name,
				     XSettingsAction   action,
				     XSettingsSetting *setting,
				     void             *data);

128 129 130
/* Private variable declarations
 */

131
static GList *display_sources;
132 133 134 135 136

static GSourceFuncs event_funcs = {
  gdk_event_prepare,
  gdk_event_check,
  gdk_event_dispatch,
137
  NULL
138 139
};

140 141 142 143 144 145 146 147 148 149
static GSource *
gdk_display_source_new (GdkDisplay *display)
{
  GSource *source = g_source_new (&event_funcs, sizeof (GdkDisplaySource));
  GdkDisplaySource *display_source = (GdkDisplaySource *)source;
  
  display_source->display = display;
  
  return source;
}
150

151 152 153 154 155
static gboolean
gdk_check_xpending (GdkDisplay *display)
{
  return XPending (GDK_DISPLAY_XDISPLAY (display));
}
156

157 158 159 160
/*********************************************
 * Functions for maintaining the event queue *
 *********************************************/

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
static void
refcounted_grab_server (Display *xdisplay)
{
  GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);

  gdk_x11_display_grab (display);
}

static void
refcounted_ungrab_server (Display *xdisplay)
{
  GdkDisplay *display = gdk_x11_lookup_xdisplay (xdisplay);
  
  gdk_x11_display_ungrab (display);
}

177 178 179 180
void
_gdk_x11_events_init_screen (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);
181 182 183 184

  /* Keep a flag to avoid extra notifies that we don't need
   */
  screen_x11->xsettings_in_init = TRUE;
185 186 187 188 189 190 191
  screen_x11->xsettings_client = xsettings_client_new_with_grab_funcs (screen_x11->xdisplay,
						                       screen_x11->screen_num,
						                       gdk_xsettings_notify_cb,
						                       gdk_xsettings_watch_cb,
						                       screen,
                                                                       refcounted_grab_server,
                                                                       refcounted_ungrab_server);
192
  screen_x11->xsettings_in_init = FALSE;
193 194
}

195 196 197 198 199
void
_gdk_x11_events_uninit_screen (GdkScreen *screen)
{
  GdkScreenX11 *screen_x11 = GDK_SCREEN_X11 (screen);

200 201 202 203 204
  if (screen_x11->xsettings_client)
    {
      xsettings_client_destroy (screen_x11->xsettings_client);
      screen_x11->xsettings_client = NULL;
    }
205 206
}

207
void 
208
_gdk_events_init (GdkDisplay *display)
209
{
210
  GSource *source;
211 212
  GdkDisplaySource *display_source;
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
213
  
214 215
  int connection_number = ConnectionNumber (display_x11->xdisplay);
  GDK_NOTE (MISC, g_message ("connection number: %d", connection_number));
216 217


218
  source = display_x11->event_source = gdk_display_source_new (display);
219
  display_source = (GdkDisplaySource*) source;
220 221
  g_source_set_priority (source, GDK_PRIORITY_EVENTS);
  
222 223
  display_source->event_poll_fd.fd = connection_number;
  display_source->event_poll_fd.events = G_IO_IN;
224
  
225
  g_source_add_poll (source, &display_source->event_poll_fd);
226 227
  g_source_set_can_recurse (source, TRUE);
  g_source_attach (source, NULL);
228

229 230
  display_sources = g_list_prepend (display_sources,display_source);

231
  gdk_display_add_client_message_filter (display,
Matthias Clasen's avatar
Matthias Clasen committed
232
					 gdk_atom_intern_static_string ("WM_PROTOCOLS"), 
233 234
					 gdk_wm_protocols_filter,   
					 NULL);
235 236
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250
void
_gdk_events_uninit (GdkDisplay *display)
{
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);

  if (display_x11->event_source)
    {
      display_sources = g_list_remove (display_sources,
                                       display_x11->event_source);
      g_source_destroy (display_x11->event_source);
      g_source_unref (display_x11->event_source);
      display_x11->event_source = NULL;
    }
}
251

252 253 254 255 256 257 258
/**
 * gdk_events_pending:
 * 
 * Checks if any events are ready to be processed for any display.
 * 
 * Return value:  %TRUE if any events are pending.
 **/
259
gboolean
260 261
gdk_events_pending (void)
{
262
  GList *tmp_list;
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  for (tmp_list = display_sources; tmp_list; tmp_list = tmp_list->next)
    {
      GdkDisplaySource *tmp_source = tmp_list->data;
      GdkDisplay *display = tmp_source->display;
      
      if (_gdk_event_queue_find_first (display))
	return TRUE;
    }

  for (tmp_list = display_sources; tmp_list; tmp_list = tmp_list->next)
    {
      GdkDisplaySource *tmp_source = tmp_list->data;
      GdkDisplay *display = tmp_source->display;
      
      if (gdk_check_xpending (display))
	return TRUE;
    }
  
  return FALSE;
}
284 285

static Bool
286 287 288
graphics_expose_predicate (Display  *display,
			   XEvent   *xevent,
			   XPointer  arg)
289
{
290
  if (xevent->xany.window == GDK_DRAWABLE_XID ((GdkDrawable *)arg) &&
291 292
      (xevent->xany.type == GraphicsExpose ||
       xevent->xany.type == NoExpose))
293 294 295 296 297
    return True;
  else
    return False;
}

298 299 300 301 302 303 304 305 306 307
/**
 * gdk_event_get_graphics_expose:
 * @window: the #GdkWindow to wait for the events for.
 * 
 * Waits for a GraphicsExpose or NoExpose event from the X server.
 * This is used in the #GtkText and #GtkCList widgets in GTK+ to make sure any
 * GraphicsExpose events are handled before the widget is scrolled.
 *
 * Return value:  a #GdkEventExpose if a GraphicsExpose was received, or %NULL if a
 * NoExpose event was received.
308
 *
309
 * Deprecated: 2.18:
310
 **/
311
GdkEvent*
312 313 314 315 316 317
gdk_event_get_graphics_expose (GdkWindow *window)
{
  XEvent xevent;
  GdkEvent *event;
  
  g_return_val_if_fail (window != NULL, NULL);
318

319 320
  XIfEvent (GDK_WINDOW_XDISPLAY (window), &xevent, 
	    graphics_expose_predicate, (XPointer) window);
321 322 323
  
  if (xevent.xany.type == GraphicsExpose)
    {
324
      event = gdk_event_new (GDK_NOTHING);
325
      
326 327
      if (gdk_event_translate (GDK_WINDOW_DISPLAY (window), event,
			       &xevent, TRUE))
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
	return event;
      else
	gdk_event_free (event);
    }
  
  return NULL;	
}

static gint
gdk_event_apply_filters (XEvent *xevent,
			 GdkEvent *event,
			 GList *filters)
{
  GList *tmp_list;
  GdkFilterReturn result;
  
  tmp_list = filters;
  
  while (tmp_list)
    {
348
      GdkEventFilter *filter = (GdkEventFilter*) tmp_list->data;
349
      
350 351
      tmp_list = tmp_list->next;
      result = filter->function (xevent, event, filter->data);
352 353 354 355 356 357 358
      if (result !=  GDK_FILTER_CONTINUE)
	return result;
    }
  
  return GDK_FILTER_CONTINUE;
}

359 360 361 362 363 364 365 366 367 368
/**
 * gdk_display_add_client_message_filter:
 * @display: a #GdkDisplay for which this message filter applies
 * @message_type: the type of ClientMessage events to receive.
 *   This will be checked against the @message_type field 
 *   of the XClientMessage event struct.
 * @func: the function to call to process the event.
 * @data: user data to pass to @func.
 *
 * Adds a filter to be called when X ClientMessage events are received.
Matthias Clasen's avatar
Matthias Clasen committed
369 370
 * See gdk_window_add_filter() if you are interested in filtering other
 * types of events.
371
 *
Matthias Clasen's avatar
Matthias Clasen committed
372
 * Since: 2.2
373
 **/ 
374
void 
375 376 377 378
gdk_display_add_client_message_filter (GdkDisplay   *display,
				       GdkAtom       message_type,
				       GdkFilterFunc func,
				       gpointer      data)
379
{
380 381 382
  GdkClientFilter *filter;
  g_return_if_fail (GDK_IS_DISPLAY (display));
  filter = g_new (GdkClientFilter, 1);
383 384 385 386 387

  filter->type = message_type;
  filter->function = func;
  filter->data = data;
  
388
  GDK_DISPLAY_X11(display)->client_filters = 
389 390
    g_list_append (GDK_DISPLAY_X11 (display)->client_filters,
		   filter);
391 392
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
/**
 * gdk_add_client_message_filter:
 * @message_type: the type of ClientMessage events to receive. This will be
 *     checked against the <structfield>message_type</structfield> field of the
 *     XClientMessage event struct.
 * @func: the function to call to process the event.
 * @data: user data to pass to @func. 
 * 
 * Adds a filter to the default display to be called when X ClientMessage events
 * are received. See gdk_display_add_client_message_filter().
 **/
void 
gdk_add_client_message_filter (GdkAtom       message_type,
			       GdkFilterFunc func,
			       gpointer      data)
{
Owen Taylor's avatar
Owen Taylor committed
409
  gdk_display_add_client_message_filter (gdk_display_get_default (),
410 411
					 message_type, func, data);
}
Havoc Pennington's avatar
Havoc Pennington committed
412 413

static void
414
do_net_wm_state_changes (GdkWindow *window)
415 416
{
  GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
Havoc Pennington's avatar
Havoc Pennington committed
417 418
  GdkWindowState old_state;
  
419 420
  if (GDK_WINDOW_DESTROYED (window) ||
      gdk_window_get_window_type (window) != GDK_WINDOW_TOPLEVEL)
Havoc Pennington's avatar
Havoc Pennington committed
421 422
    return;
  
423
  old_state = gdk_window_get_state (window);
Havoc Pennington's avatar
Havoc Pennington committed
424 425 426 427 428 429

  /* For found_sticky to remain TRUE, we have to also be on desktop
   * 0xFFFFFFFF
   */
  if (old_state & GDK_WINDOW_STATE_STICKY)
    {
430
      if (!(toplevel->have_sticky && toplevel->on_all_desktops))
Havoc Pennington's avatar
Havoc Pennington committed
431 432 433 434 435 436
        gdk_synthesize_window_state (window,
                                     GDK_WINDOW_STATE_STICKY,
                                     0);
    }
  else
    {
437
      if (toplevel->have_sticky || toplevel->on_all_desktops)
Havoc Pennington's avatar
Havoc Pennington committed
438 439 440 441 442
        gdk_synthesize_window_state (window,
                                     0,
                                     GDK_WINDOW_STATE_STICKY);
    }

443 444
  if (old_state & GDK_WINDOW_STATE_FULLSCREEN)
    {
445
      if (!toplevel->have_fullscreen)
446 447 448 449 450 451
        gdk_synthesize_window_state (window,
                                     GDK_WINDOW_STATE_FULLSCREEN,
                                     0);
    }
  else
    {
452
      if (toplevel->have_fullscreen)
453 454 455 456 457
        gdk_synthesize_window_state (window,
                                     0,
                                     GDK_WINDOW_STATE_FULLSCREEN);
    }
  
Havoc Pennington's avatar
Havoc Pennington committed
458 459 460 461 462
  /* Our "maximized" means both vertical and horizontal; if only one,
   * we don't expose that via GDK
   */
  if (old_state & GDK_WINDOW_STATE_MAXIMIZED)
    {
463
      if (!(toplevel->have_maxvert && toplevel->have_maxhorz))
Havoc Pennington's avatar
Havoc Pennington committed
464 465 466 467 468 469
        gdk_synthesize_window_state (window,
                                     GDK_WINDOW_STATE_MAXIMIZED,
                                     0);
    }
  else
    {
470
      if (toplevel->have_maxvert && toplevel->have_maxhorz)
Havoc Pennington's avatar
Havoc Pennington committed
471 472 473 474 475 476
        gdk_synthesize_window_state (window,
                                     0,
                                     GDK_WINDOW_STATE_MAXIMIZED);
    }
}

477 478 479
static void
gdk_check_wm_desktop_changed (GdkWindow *window)
{
480
  GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
481 482 483 484 485 486
  GdkDisplay *display = GDK_WINDOW_DISPLAY (window);

  Atom type;
  gint format;
  gulong nitems;
  gulong bytes_after;
487 488 489 490 491 492 493 494 495 496 497 498 499 500
  guchar *data;
  gulong *desktop;

  type = None;
  gdk_error_trap_push ();
  XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), 
                      GDK_WINDOW_XID (window),
                      gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_DESKTOP"),
                      0, G_MAXLONG, False, XA_CARDINAL, &type, 
                      &format, &nitems,
                      &bytes_after, &data);
  gdk_error_trap_pop ();

  if (type != None)
501
    {
502 503 504
      desktop = (gulong *)data;
      toplevel->on_all_desktops = (*desktop == 0xFFFFFFFF);
      XFree (desktop);
505
    }
506 507 508 509
  else
    toplevel->on_all_desktops = FALSE;
      
  do_net_wm_state_changes (window);
510 511 512 513 514
}

static void
gdk_check_wm_state_changed (GdkWindow *window)
{
515
  GdkToplevelX11 *toplevel = _gdk_x11_window_get_toplevel (window);
516 517 518 519 520 521
  GdkDisplay *display = GDK_WINDOW_DISPLAY (window);
  
  Atom type;
  gint format;
  gulong nitems;
  gulong bytes_after;
Owen Taylor's avatar
Owen Taylor committed
522
  guchar *data;
523 524 525
  Atom *atoms = NULL;
  gulong i;

526
  gboolean had_sticky = toplevel->have_sticky;
527

528 529 530 531 532
  toplevel->have_sticky = FALSE;
  toplevel->have_maxvert = FALSE;
  toplevel->have_maxhorz = FALSE;
  toplevel->have_fullscreen = FALSE;

533 534
  type = None;
  gdk_error_trap_push ();
535 536 537
  XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display), GDK_WINDOW_XID (window),
		      gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE"),
		      0, G_MAXLONG, False, XA_ATOM, &type, &format, &nitems,
Owen Taylor's avatar
Owen Taylor committed
538
		      &bytes_after, &data);
539
  gdk_error_trap_pop ();
540 541 542 543 544 545 546

  if (type != None)
    {
      Atom sticky_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_STICKY");
      Atom maxvert_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_VERT");
      Atom maxhorz_atom	= gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_MAXIMIZED_HORZ");
      Atom fullscreen_atom = gdk_x11_get_xatom_by_name_for_display (display, "_NET_WM_STATE_FULLSCREEN");
547

Owen Taylor's avatar
Owen Taylor committed
548 549
      atoms = (Atom *)data;

550 551 552 553
      i = 0;
      while (i < nitems)
        {
          if (atoms[i] == sticky_atom)
554
            toplevel->have_sticky = TRUE;
555
          else if (atoms[i] == maxvert_atom)
556
            toplevel->have_maxvert = TRUE;
557
          else if (atoms[i] == maxhorz_atom)
558
            toplevel->have_maxhorz = TRUE;
559
          else if (atoms[i] == fullscreen_atom)
560
            toplevel->have_fullscreen = TRUE;
561 562 563 564 565 566 567 568 569 570
          
          ++i;
        }

      XFree (atoms);
    }

  /* When have_sticky is turned on, we have to check the DESKTOP property
   * as well.
   */
571
  if (toplevel->have_sticky && !had_sticky)
572 573 574 575 576
    gdk_check_wm_desktop_changed (window);
  else
    do_net_wm_state_changes (window);
}

577 578
#define HAS_FOCUS(toplevel)                           \
  ((toplevel)->has_focus || (toplevel)->has_pointer_focus)
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

static void
generate_focus_event (GdkWindow *window,
		      gboolean   in)
{
  GdkEvent event;
  
  event.type = GDK_FOCUS_CHANGE;
  event.focus_change.window = window;
  event.focus_change.send_event = FALSE;
  event.focus_change.in = in;
  
  gdk_event_put (&event);
}

594
static gboolean
595 596 597 598 599 600 601 602
set_screen_from_root (GdkDisplay *display,
		      GdkEvent   *event,
		      Window      xrootwin)
{
  GdkScreen *screen;

  screen = _gdk_x11_display_screen_for_xrootwin (display, xrootwin);

603 604 605 606 607 608 609 610
  if (screen)
    {
      gdk_event_set_screen (event, screen);

      return TRUE;
    }
  
  return FALSE;
611 612
}

613 614 615 616 617 618 619
static void
translate_key_event (GdkDisplay *display,
		     GdkEvent   *event,
		     XEvent     *xevent)
{
  GdkKeymap *keymap = gdk_keymap_get_for_display (display);
  gunichar c = 0;
620
  gchar buf[7];
Matthias Clasen's avatar
Matthias Clasen committed
621
  GdkModifierType consumed, state;
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

  event->key.type = xevent->xany.type == KeyPress ? GDK_KEY_PRESS : GDK_KEY_RELEASE;
  event->key.time = xevent->xkey.time;

  event->key.state = (GdkModifierType) xevent->xkey.state;
  event->key.group = _gdk_x11_get_group_for_state (display, xevent->xkey.state);
  event->key.hardware_keycode = xevent->xkey.keycode;

  event->key.keyval = GDK_VoidSymbol;

  gdk_keymap_translate_keyboard_state (keymap,
				       event->key.hardware_keycode,
				       event->key.state,
				       event->key.group,
				       &event->key.keyval,
Matthias Clasen's avatar
Matthias Clasen committed
637 638 639 640
                                       NULL, NULL, &consumed);
   state = event->key.state & ~consumed;
   _gdk_keymap_add_virtual_modifiers (keymap, &state);
   event->key.state |= state;
641

642
  event->key.is_modifier = _gdk_keymap_key_is_modifier (keymap, event->key.hardware_keycode);
643

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
  /* Fill in event->string crudely, since various programs
   * depend on it.
   */
  event->key.string = NULL;
  
  if (event->key.keyval != GDK_VoidSymbol)
    c = gdk_keyval_to_unicode (event->key.keyval);

  if (c)
    {
      gsize bytes_written;
      gint len;

      /* Apply the control key - Taken from Xlib
       */
      if (event->key.state & GDK_CONTROL_MASK)
	{
	  if ((c >= '@' && c < '\177') || c == ' ') c &= 0x1F;
	  else if (c == '2')
	    {
	      event->key.string = g_memdup ("\0\0", 2);
	      event->key.length = 1;
	      buf[0] = '\0';
	      goto out;
	    }
	  else if (c >= '3' && c <= '7') c -= ('3' - '\033');
	  else if (c == '8') c = '\177';
	  else if (c == '/') c = '_' & 0x1F;
	}
      
      len = g_unichar_to_utf8 (c, buf);
      buf[len] = '\0';
      
      event->key.string = g_locale_from_utf8 (buf, len,
					      NULL, &bytes_written,
					      NULL);
      if (event->key.string)
	event->key.length = bytes_written;
    }
683 684 685 686 687
  else if (event->key.keyval == GDK_Escape)
    {
      event->key.length = 1;
      event->key.string = g_strdup ("\033");
    }
Owen Taylor's avatar
Owen Taylor committed
688 689 690 691
  else if (event->key.keyval == GDK_Return ||
	  event->key.keyval == GDK_KP_Enter)
    {
      event->key.length = 1;
692
      event->key.string = g_strdup ("\r");
Owen Taylor's avatar
Owen Taylor committed
693
    }
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

  if (!event->key.string)
    {
      event->key.length = 0;
      event->key.string = g_strdup ("");
    }
  
 out:
#ifdef G_ENABLE_DEBUG
  if (_gdk_debug_flags & GDK_DEBUG_EVENTS)
    {
      g_message ("%s:\t\twindow: %ld	 key: %12s  %d",
		 event->type == GDK_KEY_PRESS ? "key press  " : "key release",
		 xevent->xkey.window,
		 event->key.keyval ? gdk_keyval_name (event->key.keyval) : "(none)",
		 event->key.keyval);
  
      if (event->key.length > 0)
	g_message ("\t\tlength: %4d string: \"%s\"",
		   event->key.length, buf);
    }
#endif /* G_ENABLE_DEBUG */  
716
  return;
717 718
}

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
/**
 * gdk_x11_register_standard_event_type:
 * @display: a #GdkDisplay
 * @event_base: first event type code to register
 * @n_events: number of event type codes to register
 * 
 * Registers interest in receiving extension events with type codes
 * between @event_base and <literal>event_base + n_events - 1</literal>.
 * The registered events must have the window field in the same place
 * as core X events (this is not the case for e.g. XKB extension events).
 *
 * If an event type is registered, events of this type will go through
 * global and window-specific filters (see gdk_window_add_filter()). 
 * Unregistered events will only go through global filters.
 * GDK may register the events of some X extensions on its own.
 * 
 * This function should only be needed in unusual circumstances, e.g.
 * when filtering XInput extension events on the root window.
 *
 * Since: 2.4
 **/
740
void
741 742 743
gdk_x11_register_standard_event_type (GdkDisplay          *display,
				      gint                 event_base,
				      gint                 n_events)
744
{
745 746
  GdkEventTypeX11 *event_type;
  GdkDisplayX11 *display_x11;
747

748 749
  display_x11 = GDK_DISPLAY_X11 (display);
  event_type = g_new (GdkEventTypeX11, 1);
750

751 752
  event_type->base = event_base;
  event_type->n_events = n_events;
753

754 755
  display_x11->event_types = g_slist_prepend (display_x11->event_types, event_type);
}
756

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
/* Return the window this has to do with, if any, rather
 * than the frame or root window that was selecting
 * for substructure
 */
static void
get_real_window (GdkDisplay *display,
		 XEvent     *event,
		 Window     *event_window,
		 Window     *filter_window)
{
  /* Core events all have an event->xany.window field, but that's
   * not true for extension events
   */
  if (event->type >= KeyPress &&
      event->type <= MappingNotify)
    {
      *filter_window = event->xany.window;
      switch (event->type)
	{      
	case CreateNotify:
	  *event_window = event->xcreatewindow.window;
778
	  break;
779 780
	case DestroyNotify:
	  *event_window = event->xdestroywindow.window;
781
	  break;
782 783
	case UnmapNotify:
	  *event_window = event->xunmap.window;
784
	  break;
785 786
	case MapNotify:
	  *event_window = event->xmap.window;
787
	  break;
788 789
	case MapRequest:
	  *event_window = event->xmaprequest.window;
790
	  break;
791 792
	case ReparentNotify:
	  *event_window = event->xreparent.window;
793
	  break;
794 795
	case ConfigureNotify:
	  *event_window = event->xconfigure.window;
796
	  break;
797 798
	case ConfigureRequest:
	  *event_window = event->xconfigurerequest.window;
799
	  break;
800 801
	case GravityNotify:
	  *event_window = event->xgravity.window;
802
	  break;
803 804
	case CirculateNotify:
	  *event_window = event->xcirculate.window;
805
	  break;
806 807
	case CirculateRequest:
	  *event_window = event->xcirculaterequest.window;
808
	  break;
809 810 811 812 813 814 815 816
	default:
	  *event_window = event->xany.window;
	}
    }
  else
    {
      GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
      GSList *tmp_list;
817

818 819 820 821 822
      for (tmp_list = display_x11->event_types;
	   tmp_list;
	   tmp_list = tmp_list->next)
	{
	  GdkEventTypeX11 *event_type = tmp_list->data;
823

824 825 826 827 828 829 830 831 832 833 834
	  if (event->type >= event_type->base &&
	      event->type < event_type->base + event_type->n_events)
	    {
	      *event_window = event->xany.window;
	      *filter_window = event->xany.window;
	      return;
	    }
	}
	   
      *event_window = None;
      *filter_window = None;
835 836 837
    }
}

838
#ifdef G_ENABLE_DEBUG
839
static const char notify_modes[][19] = {
840 841 842 843 844 845
  "NotifyNormal",
  "NotifyGrab",
  "NotifyUngrab",
  "NotifyWhileGrabbed"
};

846
static const char notify_details[][23] = {
847 848 849 850 851 852 853 854 855 856 857
  "NotifyAncestor",
  "NotifyVirtual",
  "NotifyInferior",
  "NotifyNonlinear",
  "NotifyNonlinearVirtual",
  "NotifyPointer",
  "NotifyPointerRoot",
  "NotifyDetailNone"
};
#endif

858
static void
859 860 861 862 863 864 865 866 867 868 869 870
set_user_time (GdkWindow *window,
	       GdkEvent  *event)
{
  g_return_if_fail (event != NULL);

  window = gdk_window_get_toplevel (event->client.window);
  g_return_if_fail (GDK_IS_WINDOW (window));

  /* If an event doesn't have a valid timestamp, we shouldn't use it
   * to update the latest user interaction time.
   */
  if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
871 872
    gdk_x11_window_set_user_time (gdk_window_get_toplevel (window),
                                  gdk_event_get_time (event));
873 874
}

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
static gboolean
is_parent_of (GdkWindow *parent,
              GdkWindow *child)
{
  GdkWindow *w;

  w = child;
  while (w != NULL)
    {
      if (w == parent)
	return TRUE;

      w = gdk_window_get_parent (w);
    }

  return FALSE;
}

893 894 895 896 897
static gboolean
gdk_event_translate (GdkDisplay *display,
		     GdkEvent   *event,
		     XEvent     *xevent,
		     gboolean    return_exposes)
898 899 900
{
  
  GdkWindow *window;
901
  GdkWindowObject *window_private;
902
  GdkWindow *filter_window;
903
  GdkWindowImplX11 *window_impl = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
904
  gboolean return_val;
905 906
  GdkScreen *screen = NULL;
  GdkScreenX11 *screen_x11 = NULL;
907
  GdkToplevelX11 *toplevel = NULL;
908
  GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
909
  Window xwindow, filter_xwindow;
910 911
  
  return_val = FALSE;
912 913 914 915 916

  /* init these, since the done: block uses them */
  window = NULL;
  window_private = NULL;
  event->any.window = NULL;
917

918
  if (_gdk_default_filters)
919 920 921 922
    {
      /* Apply global filters */
      GdkFilterReturn result;
      result = gdk_event_apply_filters (xevent, event,
923
                                        _gdk_default_filters);
924 925 926 927 928 929 930 931
      
      if (result != GDK_FILTER_CONTINUE)
        {
          return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
          goto done;
        }
    }  

932 933 934 935
  /* Find the GdkWindow that this event relates to.
   * Basically this means substructure events
   * are reported same as structure events
   */
936
  get_real_window (display, xevent, &xwindow, &filter_xwindow);
937
  
938
  window = gdk_window_lookup_for_display (display, xwindow);
939 940 941 942
  /* We may receive events such as NoExpose/GraphicsExpose
   * and ShmCompletion for pixmaps
   */
  if (window && !GDK_IS_WINDOW (window))
943
    window = NULL;
944
  window_private = (GdkWindowObject *) window;
945

946 947 948
  /* We always run the filters for the window where the event
   * is delivered, not the window that it relates to
   */
949
  if (filter_xwindow == xwindow)
950 951
    filter_window = window;
  else
952 953 954 955 956
    {
      filter_window = gdk_window_lookup_for_display (display, filter_xwindow);
      if (filter_window && !GDK_IS_WINDOW (filter_window))
	filter_window = NULL;
    }
957

958 959 960 961
  if (window)
    {
      screen = GDK_WINDOW_SCREEN (window);
      screen_x11 = GDK_SCREEN_X11 (screen);
962
      toplevel = _gdk_x11_window_get_toplevel (window);
963 964
    }
    
965 966
  if (window != NULL)
    {
967 968 969 970
      /* Apply keyboard grabs to non-native windows */
      if (/* Is key event */
	  (xevent->type == KeyPress || xevent->type == KeyRelease) &&
	  /* And we have a grab */
971
	  display->keyboard_grab.window != NULL &&
972 973
	  (
	   /* The window is not a descendant of the grabbed window */
974
	   !is_parent_of ((GdkWindow *)display->keyboard_grab.window, window) ||
975
	   /* Or owner event is false */
976
	   !display->keyboard_grab.owner_events
977 978
	   )
	  )
979
        {
980
	  /* Report key event against grab window */
981 982
          window = display->keyboard_grab.window;;
          window_private = (GdkWindowObject *) window;
983 984
        }

985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
      window_impl = GDK_WINDOW_IMPL_X11 (window_private->impl);
      
      /* Move key events on focus window to the real toplevel, and
       * filter out all other events on focus window
       */          
      if (toplevel && xwindow == toplevel->focus_window)
	{
	  switch (xevent->type)
	    {
	    case KeyPress:
	    case KeyRelease:
	      xwindow = GDK_WINDOW_XID (window);
	      xevent->xany.window = xwindow;
	      break;
	    default:
	      return FALSE;
	    }
	}
1003

1004
      g_object_ref (window);
1005 1006
    }

1007
  event->any.window = window;
1008
  event->any.send_event = xevent->xany.send_event ? TRUE : FALSE;
1009
  
1010
  if (window_private && GDK_WINDOW_DESTROYED (window))
1011 1012
    {
      if (xevent->type != DestroyNotify)
1013 1014 1015 1016
	{
	  return_val = FALSE;
	  goto done;
	}
1017
    }
1018
  else if (filter_window)
1019
    {
1020
      /* Apply per-window filters */
1021
      GdkWindowObject *filter_private = (GdkWindowObject *) filter_window;
1022
      GdkFilterReturn result;
1023 1024

      if (filter_private->filters)
1025
	{
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
	  g_object_ref (filter_window);
	  
	  result = gdk_event_apply_filters (xevent, event,
					    filter_private->filters);
	  
	  g_object_unref (filter_window);
      
	  if (result != GDK_FILTER_CONTINUE)
	    {
	      return_val = (result == GDK_FILTER_TRANSLATE) ? TRUE : FALSE;
	      goto done;
	    }
1038 1039
	}
    }
1040 1041

  if (xevent->type == DestroyNotify)
1042
    {
1043
      int i, n;
1044

1045 1046 1047 1048 1049 1050 1051 1052 1053