gdkdisplay.c 51.5 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GDK - The GIMP Drawing Kit
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * gdkdisplay.c
 * 
 * Copyright 2001 Sun Microsystems Inc. 
 *
 * Erwann Chenede <erwann.chenede@sun.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20
21
 */

22
#include "config.h"
23

24
#include "gdkdisplay.h"
25
#include "gdkdisplayprivate.h"
26

27
#include "gdkintl.h"
28
29
#include "gdk-private.h"

30
#include "gdkclipboardprivate.h"
31
#include "gdkdeviceprivate.h"
32
#include "gdkdisplaymanagerprivate.h"
33
#include "gdkevents.h"
34
#include "gdkwindowimpl.h"
35
#include "gdkinternals.h"
36
#include "gdkmonitorprivate.h"
37

38
#include <math.h>
39
#include <glib.h>
40

41
42
/* for the use of round() */
#include "fallback-c89.c"
43
44
45

/**
 * SECTION:gdkdisplay
46
 * @Short_description: Controls a set of monitors and their associated input devices
47
48
49
 * @Title: GdkDisplay
 *
 * #GdkDisplay objects purpose are two fold:
Matthias Clasen's avatar
Matthias Clasen committed
50
51
52
 *
 * - To manage and provide information about input devices (pointers and keyboards)
 *
53
 * - To manage and provide information about output devices (monitors)
54
 *
55
56
 * GdkDisplay objects are the GDK representation of a workstation, consisting
 * of a keyboard, a pointing device (such as a mouse) and one or more screens.
Matthias Clasen's avatar
Matthias Clasen committed
57
 *
58
59
60
61
62
63
 * Most of the input device handling has been factored out into separate #GdkSeat
 * objects. Every display has a one or more seats, which can be accessed with
 * gdk_display_get_default_seat() and gdk_display_list_seats().
 *
 * Output devices are represented by #GdkMonitor objects, which can be accessed
 * with gdk-display_get_monitor() and similar APIs.
64
65
 */

66
67
68
69
70
71
72
73
74
enum
{
  PROP_0,
  PROP_COMPOSITED,
  PROP_RGBA,
  LAST_PROP
};

static GParamSpec *props[LAST_PROP] = { NULL, };
75

76
enum {
77
  OPENED,
78
  CLOSED,
79
80
  SEAT_ADDED,
  SEAT_REMOVED,
81
82
  MONITOR_ADDED,
  MONITOR_REMOVED,
83
  SETTING_CHANGED,
84
85
86
  LAST_SIGNAL
};

87
88
static void gdk_display_dispose     (GObject         *object);
static void gdk_display_finalize    (GObject         *object);
Matthias Clasen's avatar
Matthias Clasen committed
89
90
static void gdk_display_put_event_nocopy (GdkDisplay *display,
                                          GdkEvent   *event);
91

92

93
static GdkAppLaunchContext *gdk_display_real_get_app_launch_context (GdkDisplay *display);
94

95
96
static guint signals[LAST_SIGNAL] = { 0 };

Matthias Clasen's avatar
Matthias Clasen committed
97
G_DEFINE_TYPE (GdkDisplay, gdk_display, G_TYPE_OBJECT)
98

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
static void
gdk_display_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  GdkDisplay *display = GDK_DISPLAY (object);

  switch (prop_id)
    {
    case PROP_COMPOSITED:
      g_value_set_boolean (value, gdk_display_is_composited (display));
      break;

    case PROP_RGBA:
      g_value_set_boolean (value, gdk_display_is_rgba (display));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

122
123
124
125
126
static void
gdk_display_real_make_default (GdkDisplay *display)
{
}

127
128
129
static void
gdk_display_real_opened (GdkDisplay *display)
{
130
  _gdk_display_manager_add_display (gdk_display_manager_get (), display);
131
132
}

133
134
135
136
137
138
139
140
141
142
143
144
145
static void
gdk_display_real_event_data_copy (GdkDisplay     *display,
                                  const GdkEvent *src,
                                  GdkEvent       *dst)
{
}

static void
gdk_display_real_event_data_free (GdkDisplay     *display,
                                  GdkEvent       *dst)
{
}

146
147
148
149
150
151
152
153
154
static GdkSeat *
gdk_display_real_get_default_seat (GdkDisplay *display)
{
  if (!display->seats)
    return NULL;

  return display->seats->data;
}

155
156
157
158
159
160
static GdkMonitor *
gdk_display_real_get_primary_monitor (GdkDisplay *display)
{
  return gdk_display_get_monitor (display, 0);
}

161
162
163
164
static void
gdk_display_class_init (GdkDisplayClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
165

166
  object_class->finalize = gdk_display_finalize;
167
  object_class->dispose = gdk_display_dispose;
168
  object_class->get_property = gdk_display_get_property;
169

170
  class->get_app_launch_context = gdk_display_real_get_app_launch_context;
171
  class->window_type = GDK_TYPE_WINDOW;
172

173
  class->opened = gdk_display_real_opened;
174
  class->make_default = gdk_display_real_make_default;
175
176
  class->event_data_copy = gdk_display_real_event_data_copy;
  class->event_data_free = gdk_display_real_event_data_free;
177
  class->get_default_seat = gdk_display_real_get_default_seat;
178

179
180
  class->get_primary_monitor = gdk_display_real_get_primary_monitor;

181
182
183
184
185
186
187
188
189
190
191
192
193
  /**
   * GdkDisplay:composited:
   *
   * %TRUE if the display properly composits the alpha channel.
   * See gdk_display_is_composited() for details.
   *
   * Since: 3.90
   */
  props[PROP_COMPOSITED] =
    g_param_spec_boolean ("composited",
                          P_("Composited"),
                          P_("Composited"),
                          TRUE,
194
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
195
196
197
198
199
200
201
202
203
204
205
206
207
208

  /**
   * GdkDisplay:rgba:
   *
   * %TRUE if the display supports an alpha channel. See gdk_display_is_rgba()
   * for details.
   *
   * Since: 3.90
   */
  props[PROP_RGBA] =
    g_param_spec_boolean ("rgba",
                          P_("RGBA"),
                          P_("RGBA"),
                          TRUE,
209
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
210
211
212

  g_object_class_install_properties (object_class, LAST_PROP, props);

213
  /**
Matthias Clasen's avatar
Matthias Clasen committed
214
   * GdkDisplay::opened:
215
216
   * @display: the object on which the signal is emitted
   *
Matthias Clasen's avatar
Matthias Clasen committed
217
218
   * The ::opened signal is emitted when the connection to the windowing
   * system for @display is opened.
219
220
221
222
223
   */
  signals[OPENED] =
    g_signal_new (g_intern_static_string ("opened"),
		  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
224
225
		  G_STRUCT_OFFSET (GdkDisplayClass, opened),
                  NULL, NULL,
226
227
228
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

229
230
231
232
233
234
235
236
237
238
  /**
   * GdkDisplay::closed:
   * @display: the object on which the signal is emitted
   * @is_error: %TRUE if the display was closed due to an error
   *
   * The ::closed signal is emitted when the connection to the windowing
   * system for @display is closed.
   *
   * Since: 2.2
   */   
239
  signals[CLOSED] =
Matthias Clasen's avatar
Matthias Clasen committed
240
    g_signal_new (g_intern_static_string ("closed"),
241
242
243
244
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GdkDisplayClass, closed),
		  NULL, NULL,
Benjamin Otte's avatar
Benjamin Otte committed
245
                  g_cclosure_marshal_VOID__BOOLEAN,
246
247
248
		  G_TYPE_NONE,
		  1,
		  G_TYPE_BOOLEAN);
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

  /**
   * GdkDisplay::seat-added:
   * @display: the object on which the signal is emitted
   * @seat: the seat that was just added
   *
   * The ::seat-added signal is emitted whenever a new seat is made
   * known to the windowing system.
   *
   * Since: 3.20
   */
  signals[SEAT_ADDED] =
    g_signal_new (g_intern_static_string ("seat-added"),
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  0, NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1, GDK_TYPE_SEAT);

  /**
   * GdkDisplay::seat-removed:
   * @display: the object on which the signal is emitted
Matthias Clasen's avatar
Matthias Clasen committed
271
   * @seat: the seat that was just removed
272
273
274
275
276
277
278
279
280
281
282
283
284
   *
   * The ::seat-removed signal is emitted whenever a seat is removed
   * by the windowing system.
   *
   * Since: 3.20
   */
  signals[SEAT_REMOVED] =
    g_signal_new (g_intern_static_string ("seat-removed"),
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  0, NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1, GDK_TYPE_SEAT);
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

  /**
   * GdkDisplay::monitor-added:
   * @display: the objedct on which the signal is emitted
   * @monitor: the monitor that was just added
   *
   * The ::monitor-added signal is emitted whenever a monitor is
   * added.
   *
   * Since: 3.22
   */
  signals[MONITOR_ADDED] =
    g_signal_new (g_intern_static_string ("monitor-added"),
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  0, NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1, GDK_TYPE_MONITOR);

  /**
   * GdkDisplay::monitor-removed:
   * @display: the object on which the signal is emitted
   * @monitor: the monitor that was just removed
   *
   * The ::monitor-removed signal is emitted whenever a monitor is
   * removed.
   *
   * Since: 3.22
   */
  signals[MONITOR_REMOVED] =
    g_signal_new (g_intern_static_string ("monitor-removed"),
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  0, NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1, GDK_TYPE_MONITOR);
321

322
323
324
325
326
327
328
329
330
331
  /**
   * GdkDisplay::setting-changed:
   * @display: the object on which the signal is emitted
   * @setting: the name of the setting that changed
   *
   * The ::setting-changed signal is emitted whenever a setting
   * changes its value.
   *
   * Since: 3.94
   */
332
333
334
335
336
337
338
  signals[SETTING_CHANGED] =
    g_signal_new (g_intern_static_string ("setting-changed"),
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  0, NULL, NULL,
                  NULL,
		  G_TYPE_NONE, 1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
339
340
}

341
342
343
static void
free_pointer_info (GdkPointerWindowInfo *info)
{
344
  g_clear_object (&info->window_under_pointer);
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  g_slice_free (GdkPointerWindowInfo, info);
}

static void
free_device_grab (GdkDeviceGrabInfo *info)
{
  g_object_unref (info->window);
  g_object_unref (info->native_window);
  g_free (info);
}

static gboolean
free_device_grabs_foreach (gpointer key,
                           gpointer value,
                           gpointer user_data)
{
  GList *list = value;

363
  g_list_free_full (list, (GDestroyNotify) free_device_grab);
364
365
366
367

  return TRUE;
}

368
369
370
371
static void
gdk_display_init (GdkDisplay *display)
{
  display->double_click_time = 250;
372
  display->double_click_distance = 5;
373

374
375
376
377
378
  display->device_grabs = g_hash_table_new (NULL, NULL);

  display->pointers_info = g_hash_table_new_full (NULL, NULL, NULL,
                                                  (GDestroyNotify) free_pointer_info);

379
  display->rendering_mode = _gdk_rendering_mode;
380
381
382

  display->composited = TRUE;
  display->rgba = TRUE;
383
384
}

385
386
387
static void
gdk_display_dispose (GObject *object)
{
388
  GdkDisplay *display = GDK_DISPLAY (object);
389

390
391
  _gdk_display_manager_remove_display (gdk_display_manager_get (), display);

392
  g_list_free_full (display->queued_events, (GDestroyNotify) gdk_event_free);
393
394
395
  display->queued_events = NULL;
  display->queued_tail = NULL;

Matthias Clasen's avatar
Matthias Clasen committed
396
  G_OBJECT_CLASS (gdk_display_parent_class)->dispose (object);
397
398
}

399
400
401
static void
gdk_display_finalize (GObject *object)
{
402
  GdkDisplay *display = GDK_DISPLAY (object);
403
404
405
406
407
408
409
410

  g_hash_table_foreach_remove (display->device_grabs,
                               free_device_grabs_foreach,
                               NULL);
  g_hash_table_destroy (display->device_grabs);

  g_hash_table_destroy (display->pointers_info);

411
  g_list_free_full (display->seats, g_object_unref);
412

Matthias Clasen's avatar
Matthias Clasen committed
413
  G_OBJECT_CLASS (gdk_display_parent_class)->finalize (object);
414
415
416
417
418
419
}

/**
 * gdk_display_close:
 * @display: a #GdkDisplay
 *
420
 * Closes the connection to the windowing system for the given display,
421
 * and cleans up associated resources.
Matthias Clasen's avatar
Matthias Clasen committed
422
423
 *
 * Since: 2.2
424
425
426
427
428
 */
void
gdk_display_close (GdkDisplay *display)
{
  g_return_if_fail (GDK_IS_DISPLAY (display));
429
430
431
432

  if (!display->closed)
    {
      display->closed = TRUE;
433
434
      
      g_signal_emit (display, signals[CLOSED], 0, FALSE);
435
436
      g_object_run_dispose (G_OBJECT (display));
      
437
      g_object_unref (display);
438
    }
439
440
}

John Stowers's avatar
John Stowers committed
441
442
443
444
/**
 * gdk_display_is_closed:
 * @display: a #GdkDisplay
 *
445
446
447
 * Finds out if the display has been closed.
 *
 * Returns: %TRUE if the display is closed.
John Stowers's avatar
John Stowers committed
448
449
450
451
452
453
454
455
456
457
458
 *
 * Since: 2.22
 */
gboolean
gdk_display_is_closed  (GdkDisplay  *display)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);

  return display->closed;
}

459
460
461
462
463
464
465
/**
 * gdk_display_get_event:
 * @display: a #GdkDisplay
 * 
 * Gets the next #GdkEvent to be processed for @display, fetching events from the
 * windowing system if necessary.
 * 
466
467
 * Returns: (nullable) (transfer full): the next #GdkEvent to be processed,
 *   or %NULL if no events are pending
Matthias Clasen's avatar
Matthias Clasen committed
468
469
 *
 * Since: 2.2
470
471
 */
GdkEvent *
472
473
474
gdk_display_get_event (GdkDisplay *display)
{
  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
475

476
477
  if (display->event_pause_count == 0)
    GDK_DISPLAY_GET_CLASS (display)->queue_events (display);
478
479

  return _gdk_event_unqueue (display);
480
481
482
483
}

/**
 * gdk_display_peek_event:
Alexander Larsson's avatar
Alexander Larsson committed
484
 * @display: a #GdkDisplay 
485
 * 
486
 * Gets a copy of the first #GdkEvent in the @display’s event queue, without
487
488
489
490
 * removing the event from the queue.  (Note that this function will
 * not get more events from the windowing system.  It only checks the events
 * that have already been moved to the GDK event queue.)
 * 
491
492
 * Returns: (nullable) (transfer full): the first #GdkEvent on the
 *   event queue
Matthias Clasen's avatar
Matthias Clasen committed
493
494
 *
 * Since: 2.2
495
 **/
496
GdkEvent *
497
498
499
500
501
502
503
504
gdk_display_peek_event (GdkDisplay *display)
{
  GList *tmp_list;

  g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);

  tmp_list = _gdk_event_queue_find_first (display);
  
505
  if (tmp_list != NULL)
506
    return g_object_ref (tmp_list->data);
507
508

  return NULL;
509
510
}

Matthias Clasen's avatar
Matthias Clasen committed
511
512
513
514
515
516
517
518
519
static void
gdk_display_put_event_nocopy (GdkDisplay *display,
                              GdkEvent   *event)
{
  _gdk_event_queue_append (display, event);
  /* If the main loop is blocking in a different thread, wake it up */
  g_main_context_wakeup (NULL);
}

520
521
522
523
524
525
526
/**
 * gdk_display_put_event:
 * @display: a #GdkDisplay
 * @event: a #GdkEvent.
 *
 * Appends a copy of the given event onto the front of the event
 * queue for @display.
Matthias Clasen's avatar
Matthias Clasen committed
527
528
 *
 * Since: 2.2
529
530
 **/
void
531
532
gdk_display_put_event (GdkDisplay     *display,
		       const GdkEvent *event)
533
534
535
536
{
  g_return_if_fail (GDK_IS_DISPLAY (display));
  g_return_if_fail (event != NULL);

Matthias Clasen's avatar
Matthias Clasen committed
537
  gdk_display_put_event_nocopy (display, gdk_event_copy (event));
538
}
539

540
static void
Matthias Clasen's avatar
Matthias Clasen committed
541
542
543
544
545
generate_grab_broken_event (GdkDisplay *display,
                            GdkWindow  *window,
                            GdkDevice  *device,
			    gboolean    implicit,
			    GdkWindow  *grab_window)
546
547
548
549
550
{
  g_return_if_fail (window != NULL);

  if (!GDK_WINDOW_DESTROYED (window))
    {
551
552
553
      GdkEvent *event;

      event = gdk_event_new (GDK_GRAB_BROKEN);
554
555
      event->any.window = g_object_ref (window);
      event->any.send_event = FALSE;
556
557
558
      event->grab_broken.implicit = implicit;
      event->grab_broken.grab_window = grab_window;
      gdk_event_set_device (event, device);
559
      event->grab_broken.keyboard = (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD) ? TRUE : FALSE;
560

Matthias Clasen's avatar
Matthias Clasen committed
561
      gdk_display_put_event_nocopy (display, event);
562
563
564
    }
}

565
566
567
GdkDeviceGrabInfo *
_gdk_display_get_last_device_grab (GdkDisplay *display,
                                   GdkDevice  *device)
568
569
570
{
  GList *l;

571
  l = g_hash_table_lookup (display->device_grabs, device);
572

573
574
575
576
577
  if (l)
    {
      l = g_list_last (l);
      return l->data;
    }
578

579
580
  return NULL;
}
581

582
583
584
585
586
587
588
589
590
591
592
GdkDeviceGrabInfo *
_gdk_display_add_device_grab (GdkDisplay       *display,
                              GdkDevice        *device,
                              GdkWindow        *window,
                              GdkWindow        *native_window,
                              GdkGrabOwnership  grab_ownership,
                              gboolean          owner_events,
                              GdkEventMask      event_mask,
                              unsigned long     serial_start,
                              guint32           time,
                              gboolean          implicit)
593
{
594
595
  GdkDeviceGrabInfo *info, *other_info;
  GList *grabs, *l;
596

597
  info = g_new0 (GdkDeviceGrabInfo, 1);
598
599
600
601
602
603
604
605
606

  info->window = g_object_ref (window);
  info->native_window = g_object_ref (native_window);
  info->serial_start = serial_start;
  info->serial_end = G_MAXULONG;
  info->owner_events = owner_events;
  info->event_mask = event_mask;
  info->time = time;
  info->implicit = implicit;
607
608
609
  info->ownership = grab_ownership;

  grabs = g_hash_table_lookup (display->device_grabs, device);
610
611
612
613

  /* Find the first grab that has a larger start time (if any) and insert
   * before that. I.E we insert after already existing grabs with same
   * start time */
614
  for (l = grabs; l != NULL; l = l->next)
615
    {
616
      other_info = l->data;
617

618
619
620
      if (info->serial_start < other_info->serial_start)
	break;
    }
621
622

  grabs = g_list_insert_before (grabs, l, info);
623
624
625
626
627
628

  /* Make sure the new grab end before next grab */
  if (l)
    {
      other_info = l->data;
      info->serial_end = other_info->serial_start;
629
    }
630

631
  /* Find any previous grab and update its end time */
632
  l = g_list_find (grabs, info);
633
634
  l = l->prev;
  if (l)
635
    {
636
637
638
639
      other_info = l->data;
      other_info->serial_end = serial_start;
    }

640
  g_hash_table_insert (display->device_grabs, device, grabs);
641

642
  return info;
643
644
}

645
static GdkWindow *
646
647
648
649
get_current_toplevel (GdkDisplay      *display,
                      GdkDevice       *device,
                      int             *x_out,
                      int             *y_out,
650
651
652
		      GdkModifierType *state_out)
{
  GdkWindow *pointer_window;
653
  gdouble x, y;
654
655
  GdkModifierType state;

656
  pointer_window = _gdk_device_window_at_position (device, &x, &y, &state, TRUE);
657

658
659
660
661
662
663
  if (pointer_window != NULL &&
      (GDK_WINDOW_DESTROYED (pointer_window) ||
       GDK_WINDOW_TYPE (pointer_window) == GDK_WINDOW_ROOT ||
       GDK_WINDOW_TYPE (pointer_window) == GDK_WINDOW_FOREIGN))
    pointer_window = NULL;

664
665
  *x_out = round (x);
  *y_out = round (y);
666
  *state_out = state;
667

668
  return pointer_window;
669
670
}

671
static void
672
673
switch_to_pointer_grab (GdkDisplay        *display,
                        GdkDevice         *device,
674
                        GdkDevice         *source_device,
675
676
677
678
			GdkDeviceGrabInfo *grab,
			GdkDeviceGrabInfo *last_grab,
			guint32            time,
			gulong             serial)
679
{
680
  GdkWindow *new_toplevel;
681
  GdkPointerWindowInfo *info;
682
683
  GList *old_grabs;
  GdkModifierType state;
684
  int x = 0, y = 0;
685
686

  /* Temporarily unset pointer to make sure we send the crossing events below */
687
688
689
690
  old_grabs = g_hash_table_lookup (display->device_grabs, device);
  g_hash_table_steal (display->device_grabs, device);
  info = _gdk_display_get_pointer_info (display, device);

691
692
693
694
  if (grab)
    {
      /* New grab is in effect */
      if (!grab->implicit)
695
	{
696
697
	  /* !owner_event Grabbing a window that we're not inside, current status is
	     now NULL (i.e. outside grabbed window) */
698
699
	  if (!grab->owner_events && info->window_under_pointer != grab->window)
	    _gdk_display_set_window_under_pointer (display, device, NULL);
700
	}
701

702
703
      grab->activated = TRUE;
    }
704
705

  if (last_grab)
706
    {
707
708
709
710
711
      new_toplevel = NULL;

      if (grab == NULL /* ungrab */ ||
	  (!last_grab->owner_events && grab->owner_events) /* switched to owner_events */ )
	{
712
713
714
715
716
717
718
719
          /* Ungrabbed slave devices don't have a position by
           * itself, rather depend on its master pointer, so
           * it doesn't make sense to track any position for
           * these after the grab
           */
          if (grab || gdk_device_get_device_type (device) != GDK_DEVICE_TYPE_SLAVE)
            new_toplevel = get_current_toplevel (display, device, &x, &y, &state);

720
721
722
	  if (new_toplevel)
	    {
	      /* w is now toplevel and x,y in toplevel coords */
723
              _gdk_display_set_window_under_pointer (display, device, new_toplevel);
724
725
726
	      info->toplevel_x = x;
	      info->toplevel_y = y;
	      info->state = state;
727
728
729
730
	    }
	}

      if (grab == NULL) /* Ungrabbed, send events */
731
	{
732
	  /* We're now ungrabbed, update the window_under_pointer */
733
	  _gdk_display_set_window_under_pointer (display, device, new_toplevel);
734
	}
735
736
    }

737
  g_hash_table_insert (display->device_grabs, device, old_grabs);
738
739
}

740
741
742
743
744
745
746
747
void
_gdk_display_update_last_event (GdkDisplay     *display,
                                const GdkEvent *event)
{
  if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
    display->last_event_time = gdk_event_get_time (event);
}

748
void
749
750
_gdk_display_device_grab_update (GdkDisplay *display,
                                 GdkDevice  *device,
751
                                 GdkDevice  *source_device,
752
                                 gulong      current_serial)
753
{
754
755
  GdkDeviceGrabInfo *current_grab, *next_grab;
  GList *grabs;
756
  guint32 time;
757

758
  time = display->last_event_time;
759
  grabs = g_hash_table_lookup (display->device_grabs, device);
760

761
  while (grabs != NULL)
762
    {
763
      current_grab = grabs->data;
764

765
766
      if (current_grab->serial_start > current_serial)
	return; /* Hasn't started yet */
767

768
      if (current_grab->serial_end > current_serial)
769
770
771
	{
	  /* This one hasn't ended yet.
	     its the currently active one or scheduled to be active */
772

773
	  if (!current_grab->activated)
774
            {
775
              if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
776
                switch_to_pointer_grab (display, device, source_device, current_grab, NULL, time, current_serial);
777
778
            }

779
780
781
782
	  break;
	}

      next_grab = NULL;
783
      if (grabs->next)
784
	{
785
	  /* This is the next active grab */
786
787
	  next_grab = grabs->next->data;

788
789
	  if (next_grab->serial_start > current_serial)
	    next_grab = NULL; /* Actually its not yet active */
790
791
	}

792
      if ((next_grab == NULL && current_grab->implicit_ungrab) ||
793
          (next_grab != NULL && current_grab->window != next_grab->window))
Matthias Clasen's avatar
Matthias Clasen committed
794
        generate_grab_broken_event (display, GDK_WINDOW (current_grab->window),
795
                                    device,
796
797
                                    current_grab->implicit,
                                    next_grab? next_grab->window : NULL);
798

799
      /* Remove old grab */
800
801
802
      grabs = g_list_delete_link (grabs, grabs);
      g_hash_table_insert (display->device_grabs, device, grabs);

803
      if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
804
        switch_to_pointer_grab (display, device, source_device,
805
806
807
808
                                next_grab, current_grab,
                                time, current_serial);

      free_device_grab (current_grab);
809
    }
810
811
812
}

static GList *
813
814
grab_list_find (GList  *grabs,
                gulong  serial)
815
{
816
  GdkDeviceGrabInfo *grab;
817

818
  while (grabs)
819
    {
820
      grab = grabs->data;
821

822
      if (serial >= grab->serial_start && serial < grab->serial_end)
823
824
825
	return grabs;

      grabs = grabs->next;
826
    }
827

828
829
830
  return NULL;
}

831
832
833
834
835
836
static GList *
find_device_grab (GdkDisplay *display,
                   GdkDevice  *device,
                   gulong      serial)
{
  GList *l;
837

838
839
840
  l = g_hash_table_lookup (display->device_grabs, device);
  return grab_list_find (l, serial);
}
841

842
843
844
845
GdkDeviceGrabInfo *
_gdk_display_has_device_grab (GdkDisplay *display,
                              GdkDevice  *device,
                              gulong      serial)
846
847
848
{
  GList *l;

849
  l = find_device_grab (display, device, serial);
850
851
  if (l)
    return l->data;
852

853
854
855
  return NULL;
}

856
857
858
/* Returns true if last grab was ended
 * If if_child is non-NULL, end the grab only if the grabbed
 * window is the same as if_child or a descendant of it */
859
gboolean
860
861
862
863
864
_gdk_display_end_device_grab (GdkDisplay *display,
                              GdkDevice  *device,
                              gulong      serial,
                              GdkWindow  *if_child,
                              gboolean    implicit)
865
{
866
  GdkDeviceGrabInfo *grab;
867
868
  GList *l;

869
870
  l = find_device_grab (display, device, serial);

871
872
873
874
875
876
  if (l == NULL)
    return FALSE;

  grab = l->data;
  if (grab &&
      (if_child == NULL ||
877
       _gdk_window_event_parent_of (if_child, grab->window)))
878
879
880
881
882
883
884
    {
      grab->serial_end = serial;
      grab->implicit_ungrab = implicit;
      return l->next == NULL;
    }
  
  return FALSE;
885
886
}

887
888
889
890
891
892
893
894
895
896
897
898
899
/* Returns TRUE if device events are not blocked by any grab */
gboolean
_gdk_display_check_grab_ownership (GdkDisplay *display,
                                   GdkDevice  *device,
                                   gulong      serial)
{
  GHashTableIter iter;
  gpointer key, value;
  GdkGrabOwnership higher_ownership, device_ownership;
  gboolean device_is_keyboard;

  g_hash_table_iter_init (&iter, display->device_grabs);
  higher_ownership = device_ownership = GDK_OWNERSHIP_NONE;
900
  device_is_keyboard = (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD);
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915

  while (g_hash_table_iter_next (&iter, &key, &value))
    {
      GdkDeviceGrabInfo *grab;
      GdkDevice *dev;
      GList *grabs;

      dev = key;
      grabs = value;
      grabs = grab_list_find (grabs, serial);

      if (!grabs)
        continue;

      /* Discard device if it's not of the same type */
916
917
      if ((device_is_keyboard && gdk_device_get_source (dev) != GDK_SOURCE_KEYBOARD) ||
          (!device_is_keyboard && gdk_device_get_source (dev) == GDK_SOURCE_KEYBOARD))
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
        continue;

      grab = grabs->data;

      if (dev == device)
        device_ownership = grab->ownership;
      else
        {
          if (grab->ownership > higher_ownership)
            higher_ownership = grab->ownership;
        }
    }

  if (higher_ownership > device_ownership)
    {
      /* There's a higher priority ownership
       * going on for other device(s)
       */
      return FALSE;
    }

  return TRUE;
940
941
}

942
943
944
GdkPointerWindowInfo *
_gdk_display_get_pointer_info (GdkDisplay *display,
                               GdkDevice  *device)
945
{
946
947
  GdkPointerWindowInfo *info;

948
949
950
  if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
    device = gdk_device_get_associated_device (device);

951
952
953
954
955
956
957
958
959
960
961
962
  if (G_UNLIKELY (!device))
    return NULL;

  info = g_hash_table_lookup (display->pointers_info, device);

  if (G_UNLIKELY (!info))
    {
      info = g_slice_new0 (GdkPointerWindowInfo);
      g_hash_table_insert (display->pointers_info, device, info);
    }

  return info;
963
964
}

965
966
967
968
void
_gdk_display_pointer_info_foreach (GdkDisplay                   *display,
                                   GdkDisplayPointerInfoForeach  func,
                                   gpointer                      user_data)
969
{
970
971
  GHashTableIter iter;
  gpointer key, value;
972

973
974
975
  g_hash_table_iter_init (&iter, display->pointers_info);

  while (g_hash_table_iter_next (&iter, &key, &value))
976
    {
977
978
      GdkPointerWindowInfo *info = value;
      GdkDevice *device = key;
979

980
      (func) (display, device, info, user_data);
981
982
    }
}
983

984
985
/*< private >
 * gdk_device_grab_info:
986
987
 * @display: the display for which to get the grab information
 * @device: device to get the grab information from
988
989
 * @grab_window: (out) (transfer none): location to store current grab window
 * @owner_events: (out): location to store boolean indicating whether
990
 *   the @owner_events flag to gdk_device_grab() was %TRUE.
991
992
 *
 * Determines information about the current keyboard grab.
993
 * This is not public API and must not be used by applications.
994
 *
995
 * Returns: %TRUE if this application currently has the
996
 *  keyboard grabbed.
997
 */
998
gboolean
999
1000
gdk_device_grab_info (GdkDisplay  *display,
                      GdkDevice   *device,