-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdkdisplay.c
2599 lines (2197 loc) · 71.6 KB
/
gdkdisplay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GDK - The GIMP Drawing Kit
* gdkdisplay.c
*
* Copyright 2001 Sun Microsystems Inc.
*
* Erwann Chenede <[email protected]>
*
* 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gdkdisplay.h"
#include "gdkdisplayprivate.h"
#include <glib/gi18n-lib.h>
#include "gdkprivate.h"
#include "gdkapplaunchcontext.h"
#include "gdkclipboardprivate.h"
#include "gdkdeviceprivate.h"
#include "gdkdisplaymanagerprivate.h"
#include "gdkdmabufeglprivate.h"
#include "gdkdmabufformatsbuilderprivate.h"
#include "gdkdmabufformatsprivate.h"
#include "gdkdmabuftextureprivate.h"
#include "gdkeventsprivate.h"
#include "gdkframeclockidleprivate.h"
#include "gdkglcontextprivate.h"
#include "gdkmonitorprivate.h"
#include "gdkrectangle.h"
#include "gdkvulkancontextprivate.h"
#ifdef HAVE_EGL
#include <epoxy/egl.h>
#endif
#ifdef HAVE_SYS_SYSMACROS_H
#include <sys/sysmacros.h>
#endif
#include <math.h>
#include <stdlib.h>
/**
* GdkDisplay:
*
* `GdkDisplay` objects are the GDK representation of a workstation.
*
* Their purpose are two-fold:
*
* - To manage and provide information about input devices (pointers, keyboards, etc)
* - To manage and provide information about output devices (monitors, projectors, etc)
*
* Most of the input device handling has been factored out into separate
* [[email protected]] objects. Every display has a one or more seats, which
* can be accessed with [[email protected]_default_seat] and
* [[email protected]_seats].
*
* Output devices are represented by [[email protected]] objects, which can
* be accessed with [[email protected]_monitor_at_surface] and similar APIs.
*/
enum
{
PROP_0,
PROP_COMPOSITED,
PROP_RGBA,
PROP_SHADOW_WIDTH,
PROP_INPUT_SHAPES,
PROP_DMABUF_FORMATS,
LAST_PROP
};
static GParamSpec *props[LAST_PROP] = { NULL, };
enum {
OPENED,
CLOSED,
SEAT_ADDED,
SEAT_REMOVED,
SETTING_CHANGED,
LAST_SIGNAL
};
typedef struct _GdkDisplayPrivate GdkDisplayPrivate;
struct _GdkDisplayPrivate {
/* The base context that all other contexts inherit from.
* This context is never exposed to public API and is
* allowed to have a %NULL surface.
*/
GdkGLContext *gl_context;
GError *gl_error;
#ifdef HAVE_EGL
EGLDisplay egl_display;
EGLConfig egl_config;
EGLConfig egl_config_high_depth;
#endif
guint rgba : 1;
guint composited : 1;
guint shadow_width: 1;
guint input_shapes : 1;
GdkDebugFlags debug_flags;
};
static void gdk_display_dispose (GObject *object);
static void gdk_display_finalize (GObject *object);
static GdkAppLaunchContext *gdk_display_real_get_app_launch_context (GdkDisplay *display);
static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_WITH_PRIVATE (GdkDisplay, gdk_display, G_TYPE_OBJECT)
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;
case PROP_SHADOW_WIDTH:
g_value_set_boolean (value, gdk_display_supports_shadow_width (display));
break;
case PROP_INPUT_SHAPES:
g_value_set_boolean (value, gdk_display_supports_input_shapes (display));
break;
case PROP_DMABUF_FORMATS:
g_value_set_boxed (value, gdk_display_get_dmabuf_formats (display));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gdk_display_real_make_default (GdkDisplay *display)
{
}
static GdkGLContext *
gdk_display_default_init_gl (GdkDisplay *display,
GError **error)
{
g_set_error_literal (error, GDK_GL_ERROR, GDK_GL_ERROR_NOT_AVAILABLE,
_("The current backend does not support OpenGL"));
return NULL;
}
static guint
gdk_display_default_rate_egl_config (GdkDisplay *display,
gpointer egl_display,
gpointer config)
{
guint distance = 0;
#ifdef HAVE_EGL
int tmp;
if (!eglGetConfigAttrib (egl_display, config, EGL_SAMPLE_BUFFERS, &tmp) || tmp != 0)
distance += 0x20000;
if (!eglGetConfigAttrib (egl_display, config, EGL_DEPTH_SIZE, &tmp) || tmp != 0 ||
!eglGetConfigAttrib (egl_display, config, EGL_STENCIL_SIZE, &tmp) || tmp != 0)
distance += 0x10000;
#endif
return distance;
}
static GdkSeat *
gdk_display_real_get_default_seat (GdkDisplay *display)
{
if (!display->seats)
return NULL;
return display->seats->data;
}
static void
gdk_display_real_opened (GdkDisplay *display)
{
_gdk_display_manager_add_display (gdk_display_manager_get (), display);
}
static void
gdk_display_class_init (GdkDisplayClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->finalize = gdk_display_finalize;
object_class->dispose = gdk_display_dispose;
object_class->get_property = gdk_display_get_property;
class->make_default = gdk_display_real_make_default;
class->get_app_launch_context = gdk_display_real_get_app_launch_context;
class->init_gl = gdk_display_default_init_gl;
class->rate_egl_config = gdk_display_default_rate_egl_config;
class->get_default_seat = gdk_display_real_get_default_seat;
class->opened = gdk_display_real_opened;
/**
* GdkDisplay:composited: (getter is_composited)
*
* %TRUE if the display properly composites the alpha channel.
*/
props[PROP_COMPOSITED] =
g_param_spec_boolean ("composited", NULL, NULL,
TRUE,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/**
* GdkDisplay:rgba: (getter is_rgba)
*
* %TRUE if the display supports an alpha channel.
*/
props[PROP_RGBA] =
g_param_spec_boolean ("rgba", NULL, NULL,
TRUE,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/**
* GdkDisplay:shadow-width: (getter supports_shadow_width)
*
* %TRUE if the display supports extensible frames.
*
* Since: 4.14
*/
props[PROP_SHADOW_WIDTH] =
g_param_spec_boolean ("shadow-width", NULL, NULL,
TRUE,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/**
* GdkDisplay:input-shapes: (getter supports_input_shapes)
*
* %TRUE if the display supports input shapes.
*/
props[PROP_INPUT_SHAPES] =
g_param_spec_boolean ("input-shapes", NULL, NULL,
TRUE,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
/**
* GdkDisplay:dmabuf-formats:
*
* The dma-buf formats that are supported on this display
*
* Since: 4.14
*/
props[PROP_DMABUF_FORMATS] =
g_param_spec_boxed ("dmabuf-formats", NULL, NULL,
GDK_TYPE_DMABUF_FORMATS,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, LAST_PROP, props);
/**
* GdkDisplay::opened:
* @display: the object on which the signal is emitted
*
* Emitted when the connection to the windowing system for @display is opened.
*/
signals[OPENED] =
g_signal_new (g_intern_static_string ("opened"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdkDisplayClass, opened),
NULL, NULL,
NULL,
G_TYPE_NONE, 0);
/**
* GdkDisplay::closed:
* @display: the object on which the signal is emitted
* @is_error: %TRUE if the display was closed due to an error
*
* Emitted when the connection to the windowing system for @display is closed.
*/
signals[CLOSED] =
g_signal_new (g_intern_static_string ("closed"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdkDisplayClass, closed),
NULL, NULL,
NULL,
G_TYPE_NONE,
1,
G_TYPE_BOOLEAN);
/**
* GdkDisplay::seat-added:
* @display: the object on which the signal is emitted
* @seat: the seat that was just added
*
* Emitted whenever a new seat is made known to the windowing system.
*/
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,
NULL,
G_TYPE_NONE, 1, GDK_TYPE_SEAT);
/**
* GdkDisplay::seat-removed:
* @display: the object on which the signal is emitted
* @seat: the seat that was just removed
*
* Emitted whenever a seat is removed by the windowing system.
*/
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,
NULL,
G_TYPE_NONE, 1, GDK_TYPE_SEAT);
/**
* GdkDisplay::setting-changed:
* @display: the object on which the signal is emitted
* @setting: the name of the setting that changed
*
* Emitted whenever a setting changes its value.
*/
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);
}
static void
free_pointer_info (GdkPointerSurfaceInfo *info)
{
g_clear_object (&info->surface_under_pointer);
g_free (info);
}
static void
free_device_grab (GdkDeviceGrabInfo *info)
{
g_object_unref (info->surface);
g_free (info);
}
static gboolean
free_device_grabs_foreach (gpointer key,
gpointer value,
gpointer user_data)
{
GList *list = value;
g_list_free_full (list, (GDestroyNotify) free_device_grab);
return TRUE;
}
static void
gdk_display_init (GdkDisplay *display)
{
GdkDisplayPrivate *priv = gdk_display_get_instance_private (display);
display->double_click_time = 250;
display->double_click_distance = 5;
display->device_grabs = g_hash_table_new (NULL, NULL);
display->pointers_info = g_hash_table_new_full (NULL, NULL, NULL,
(GDestroyNotify) free_pointer_info);
g_queue_init (&display->queued_events);
priv->debug_flags = _gdk_debug_flags;
priv->composited = TRUE;
priv->rgba = TRUE;
priv->shadow_width = TRUE;
priv->input_shapes = TRUE;
}
static void
gdk_display_dispose (GObject *object)
{
GdkDisplay *display = GDK_DISPLAY (object);
GdkDisplayPrivate *priv = gdk_display_get_instance_private (display);
if (display->vk_downloader)
{
gdk_dmabuf_downloader_close (display->vk_downloader);
g_clear_object (&display->vk_downloader);
}
if (display->egl_downloader)
{
gdk_dmabuf_downloader_close (display->egl_downloader);
g_clear_object (&display->egl_downloader);
}
_gdk_display_manager_remove_display (gdk_display_manager_get (), display);
g_queue_clear (&display->queued_events);
g_clear_pointer (&display->egl_dmabuf_formats, gdk_dmabuf_formats_unref);
g_clear_pointer (&display->egl_internal_formats, gdk_dmabuf_formats_unref);
#ifdef GDK_RENDERING_VULKAN
if (display->vk_instance)
gdk_display_destroy_vulkan_instance (display);
g_assert (display->vk_dmabuf_formats == NULL);
g_clear_error (&display->vulkan_error);
#endif
g_clear_object (&priv->gl_context);
#ifdef HAVE_EGL
g_clear_pointer (&priv->egl_display, eglTerminate);
#endif
g_clear_error (&priv->gl_error);
for (GList *l = display->seats; l; l = l->next)
g_object_run_dispose (G_OBJECT (l->data));
G_OBJECT_CLASS (gdk_display_parent_class)->dispose (object);
}
static void
gdk_display_finalize (GObject *object)
{
GdkDisplay *display = GDK_DISPLAY (object);
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);
g_list_free_full (display->seats, g_object_unref);
g_clear_pointer (&display->dmabuf_formats, gdk_dmabuf_formats_unref);
G_OBJECT_CLASS (gdk_display_parent_class)->finalize (object);
}
/**
* gdk_display_close:
* @display: a `GdkDisplay`
*
* Closes the connection to the windowing system for the given display.
*
* This cleans up associated resources.
*/
void
gdk_display_close (GdkDisplay *display)
{
g_return_if_fail (GDK_IS_DISPLAY (display));
if (!display->closed)
{
display->closed = TRUE;
g_signal_emit (display, signals[CLOSED], 0, FALSE);
g_object_run_dispose (G_OBJECT (display));
g_object_unref (display);
}
}
/**
* gdk_display_is_closed:
* @display: a `GdkDisplay`
*
* Finds out if the display has been closed.
*
* Returns: %TRUE if the display is closed.
*/
gboolean
gdk_display_is_closed (GdkDisplay *display)
{
g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
return display->closed;
}
/*<private>
* gdk_display_get_event:
* @display: a `GdkDisplay`
*
* Gets the next `GdkEvent` to be processed for @display,
* fetching events from the windowing system if necessary.
*
* Returns: (nullable) (transfer full): the next `GdkEvent`
* to be processed
*/
GdkEvent *
gdk_display_get_event (GdkDisplay *display)
{
g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
if (display->event_pause_count == 0)
GDK_DISPLAY_GET_CLASS (display)->queue_events (display);
return _gdk_event_unqueue (display);
}
/**
* gdk_display_put_event:
* @display: a `GdkDisplay`
* @event: (transfer none): a `GdkEvent`
*
* Adds the given event to the event queue for @display.
*
* Deprecated: 4.10: This function is only useful in very
* special situations and should not be used by applications.
**/
void
gdk_display_put_event (GdkDisplay *display,
GdkEvent *event)
{
g_return_if_fail (GDK_IS_DISPLAY (display));
g_return_if_fail (event != NULL);
_gdk_event_queue_append (display, gdk_event_ref ((GdkEvent *)event));
}
static void
generate_grab_broken_event (GdkDisplay *display,
GdkSurface *surface,
GdkDevice *device,
gboolean implicit,
GdkSurface *grab_surface)
{
g_return_if_fail (surface != NULL);
if (!GDK_SURFACE_DESTROYED (surface))
{
GdkEvent *event;
event = gdk_grab_broken_event_new (surface,
device,
grab_surface,
implicit);
_gdk_event_queue_append (display, event);
}
}
GdkDeviceGrabInfo *
_gdk_display_get_last_device_grab (GdkDisplay *display,
GdkDevice *device)
{
GList *l;
l = g_hash_table_lookup (display->device_grabs, device);
if (l)
{
l = g_list_last (l);
return l->data;
}
return NULL;
}
GdkDeviceGrabInfo *
_gdk_display_add_device_grab (GdkDisplay *display,
GdkDevice *device,
GdkSurface *surface,
gboolean owner_events,
GdkEventMask event_mask,
unsigned long serial_start,
guint32 time,
gboolean implicit)
{
GdkDeviceGrabInfo *info, *other_info;
GList *grabs, *l;
info = g_new0 (GdkDeviceGrabInfo, 1);
info->surface = g_object_ref (surface);
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;
grabs = g_hash_table_lookup (display->device_grabs, device);
/* 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 */
for (l = grabs; l != NULL; l = l->next)
{
other_info = l->data;
if (info->serial_start < other_info->serial_start)
break;
}
grabs = g_list_insert_before (grabs, l, info);
/* Make sure the new grab end before next grab */
if (l)
{
other_info = l->data;
info->serial_end = other_info->serial_start;
}
/* Find any previous grab and update its end time */
l = g_list_find (grabs, info);
l = l->prev;
if (l)
{
other_info = l->data;
other_info->serial_end = serial_start;
}
g_hash_table_insert (display->device_grabs, device, grabs);
return info;
}
static GdkSurface *
get_current_toplevel (GdkDisplay *display,
GdkDevice *device,
int *x_out,
int *y_out,
GdkModifierType *state_out)
{
GdkSurface *pointer_surface;
double x, y;
GdkModifierType state;
pointer_surface = _gdk_device_surface_at_position (device, &x, &y, &state);
if (pointer_surface != NULL &&
GDK_SURFACE_DESTROYED (pointer_surface))
pointer_surface = NULL;
*x_out = round (x);
*y_out = round (y);
*state_out = state;
return pointer_surface;
}
static void
switch_to_pointer_grab (GdkDisplay *display,
GdkDevice *device,
GdkDeviceGrabInfo *grab,
GdkDeviceGrabInfo *last_grab,
guint32 time,
gulong serial)
{
GdkSurface *new_toplevel;
GdkPointerSurfaceInfo *info;
GList *old_grabs;
GdkModifierType state;
int x = 0, y = 0;
/* Temporarily unset pointer to make sure we send the crossing events below */
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);
if (grab)
{
/* New grab is in effect */
if (!grab->implicit)
{
/* !owner_event Grabbing a surface that we're not inside, current status is
now NULL (i.e. outside grabbed surface) */
if (!grab->owner_events && info->surface_under_pointer != grab->surface)
_gdk_display_set_surface_under_pointer (display, device, NULL);
}
grab->activated = TRUE;
}
if (last_grab)
{
new_toplevel = NULL;
if (grab == NULL /* ungrab */ ||
(!last_grab->owner_events && grab->owner_events) /* switched to owner_events */ )
{
new_toplevel = get_current_toplevel (display, device, &x, &y, &state);
if (new_toplevel)
{
/* w is now toplevel and x,y in toplevel coords */
_gdk_display_set_surface_under_pointer (display, device, new_toplevel);
info->toplevel_x = x;
info->toplevel_y = y;
info->state = state;
}
}
if (grab == NULL) /* Ungrabbed, send events */
{
/* We're now ungrabbed, update the surface_under_pointer */
_gdk_display_set_surface_under_pointer (display, device, new_toplevel);
}
}
g_hash_table_insert (display->device_grabs, device, old_grabs);
}
void
_gdk_display_update_last_event (GdkDisplay *display,
GdkEvent *event)
{
if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
display->last_event_time = gdk_event_get_time (event);
}
void
_gdk_display_device_grab_update (GdkDisplay *display,
GdkDevice *device,
gulong current_serial)
{
GdkDeviceGrabInfo *current_grab, *next_grab;
GList *grabs;
guint32 time;
time = display->last_event_time;
grabs = g_hash_table_lookup (display->device_grabs, device);
while (grabs != NULL)
{
current_grab = grabs->data;
if (current_grab->serial_start > current_serial)
return; /* Hasn't started yet */
if (current_grab->serial_end > current_serial)
{
/* This one hasn't ended yet.
its the currently active one or scheduled to be active */
if (!current_grab->activated)
{
if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
switch_to_pointer_grab (display, device, current_grab, NULL, time, current_serial);
}
break;
}
next_grab = NULL;
if (grabs->next)
{
/* This is the next active grab */
next_grab = grabs->next->data;
if (next_grab->serial_start > current_serial)
next_grab = NULL; /* Actually its not yet active */
}
if ((next_grab == NULL && current_grab->implicit_ungrab) ||
(next_grab != NULL && current_grab->surface != next_grab->surface))
generate_grab_broken_event (display, GDK_SURFACE (current_grab->surface),
device,
current_grab->implicit,
next_grab? next_grab->surface : NULL);
/* Remove old grab */
grabs = g_list_delete_link (grabs, grabs);
g_hash_table_insert (display->device_grabs, device, grabs);
if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
switch_to_pointer_grab (display, device,
next_grab, current_grab,
time, current_serial);
free_device_grab (current_grab);
}
}
static GList *
grab_list_find (GList *grabs,
gulong serial)
{
GdkDeviceGrabInfo *grab;
while (grabs)
{
grab = grabs->data;
if (serial >= grab->serial_start && serial < grab->serial_end)
return grabs;
grabs = grabs->next;
}
return NULL;
}
static GList *
find_device_grab (GdkDisplay *display,
GdkDevice *device,
gulong serial)
{
GList *l;
l = g_hash_table_lookup (display->device_grabs, device);
return grab_list_find (l, serial);
}
GdkDeviceGrabInfo *
_gdk_display_has_device_grab (GdkDisplay *display,
GdkDevice *device,
gulong serial)
{
GList *l;
l = find_device_grab (display, device, serial);
if (l)
return l->data;
return NULL;
}
/* Returns true if last grab was ended
* If if_child is non-NULL, end the grab only if the grabbed
* surface is the same as if_child or a descendant of it */
gboolean
_gdk_display_end_device_grab (GdkDisplay *display,
GdkDevice *device,
gulong serial,
GdkSurface *if_child,
gboolean implicit)
{
GdkDeviceGrabInfo *grab;
GList *l;
l = find_device_grab (display, device, serial);
if (l == NULL)
return FALSE;
grab = l->data;
if (grab && (if_child == NULL || if_child == grab->surface))
{
grab->serial_end = serial;
grab->implicit_ungrab = implicit;
return l->next == NULL;
}
return FALSE;
}
GdkPointerSurfaceInfo *
_gdk_display_get_pointer_info (GdkDisplay *display,
GdkDevice *device)
{
GdkPointerSurfaceInfo *info;
GdkSeat *seat;
if (device)
{
seat = gdk_device_get_seat (device);
if (device == gdk_seat_get_keyboard (seat))
device = gdk_seat_get_pointer (seat);
}
if (G_UNLIKELY (!device))
return NULL;
info = g_hash_table_lookup (display->pointers_info, device);
if (G_UNLIKELY (!info))
{
info = g_new0 (GdkPointerSurfaceInfo, 1);
g_hash_table_insert (display->pointers_info, device, info);
}
return info;
}
void
_gdk_display_pointer_info_foreach (GdkDisplay *display,
GdkDisplayPointerInfoForeach func,
gpointer user_data)
{
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init (&iter, display->pointers_info);
while (g_hash_table_iter_next (&iter, &key, &value))
{
GdkPointerSurfaceInfo *info = value;
GdkDevice *device = key;
(func) (display, device, info, user_data);
}
}
/*< private >
* gdk_device_grab_info:
* @display: the display for which to get the grab information
* @device: device to get the grab information from
* @grab_surface: (out) (transfer none): location to store current grab surface
* @owner_events: (out): location to store boolean indicating whether
* the @owner_events flag to gdk_device_grab() was %TRUE.
*
* Determines information about the current keyboard grab.
* This is not public API and must not be used by applications.
*
* Returns: %TRUE if this application currently has the
* keyboard grabbed.
*/
gboolean
gdk_device_grab_info (GdkDisplay *display,
GdkDevice *device,
GdkSurface **grab_surface,
gboolean *owner_events)
{
GdkDeviceGrabInfo *info;
g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE);
g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
info = _gdk_display_get_last_device_grab (display, device);
if (info)
{
if (grab_surface)
*grab_surface = info->surface;
if (owner_events)
*owner_events = info->owner_events;
return TRUE;
}
else
return FALSE;
}
/**
* gdk_display_device_is_grabbed:
* @display: a `GdkDisplay`
* @device: a `GdkDevice`
*
* Returns %TRUE if there is an ongoing grab on @device for @display.
*
* Returns: %TRUE if there is a grab in effect for @device.
*/
gboolean
gdk_display_device_is_grabbed (GdkDisplay *display,
GdkDevice *device)
{
GdkDeviceGrabInfo *info;
g_return_val_if_fail (GDK_IS_DISPLAY (display), TRUE);
g_return_val_if_fail (GDK_IS_DEVICE (device), TRUE);
/* What we're interested in is the steady state (ie last grab),
because we're interested e.g. if we grabbed so that we
can ungrab, even if our grab is not active just yet. */
info = _gdk_display_get_last_device_grab (display, device);
return (info && !info->implicit);
}