Debian Codegraph @

gdk/gdksurface.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-2007 Peter Mattis, Spencer Kimball,
3 * Josh MacDonald, Ryan Lortie
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
24 */
25
26 #include "config.h"
27
28 #include <cairo-gobject.h>
29
30 #include "gdksurface.h"
31
32 #include "gdkprivate.h"
33 #include "gdkcairoprivate.h"
34 #include "gdkcontentprovider.h"
35 #include "gdkdeviceprivate.h"
36 #include "gdkdisplayprivate.h"
37 #include "gdkdragsurfaceprivate.h"
38 #include "gdkeventsprivate.h"
39 #include "gdkframeclockidleprivate.h"
40 #include "gdkglcontextprivate.h"
41 #include <glib/gi18n-lib.h>
42 #include "gdkmarshalers.h"
43 #include "gdkpopupprivate.h"
44 #include "gdkrectangleprivate.h"
45 #include "gdktoplevelprivate.h"
46 #include "gdkvulkancontext.h"
47 #include "gdksubsurfaceprivate.h"
48
49 #include "gsk/gskrectprivate.h"
50
51 #include <math.h>
52
53 #ifdef HAVE_EGL
54 #include <epoxy/egl.h>
55 #endif
56
57 /**
58 * GdkSurface:
59 *
60 * Represents a rectangular region on the screen.
61 *
62 * It’s a low-level object, used to implement high-level objects
63 * such as [GtkWindow](../gtk4/class.Window.html).
64 *
65 * The surfaces you see in practice are either [[email protected]] or
66 * [[email protected]], and those interfaces provide much of the required
67 * API to interact with these surfaces. Other, more specialized surface
68 * types exist, but you will rarely interact with them directly.
69 */
70
71 typedef struct _GdkSurfacePrivate GdkSurfacePrivate;
72
73 struct _GdkSurfacePrivate
74 {
75 cairo_region_t *opaque_region;
76 cairo_rectangle_int_t opaque_rect; /* This is different from the region */
77
78 GdkDrawContext *attached_context;
79 gpointer widget;
80
81 GdkColorState *color_state;
82 };
83
84 enum {
85 LAYOUT,
86 RENDER,
87 EVENT,
88 ENTER_MONITOR,
89 LEAVE_MONITOR,
90 LAST_SIGNAL
91 };
92
93 enum {
94 PROP_0,
95 PROP_CURSOR,
96 PROP_DISPLAY,
97 PROP_FRAME_CLOCK,
98 PROP_MAPPED,
99 PROP_WIDTH,
100 PROP_HEIGHT,
101 PROP_SCALE_FACTOR,
102 PROP_SCALE,
103 LAST_PROP
104 };
105
106 /* Global info */
107
108 static void gdk_surface_finalize (GObject *object);
109
110 static void gdk_surface_set_property (GObject *object,
111 guint prop_id,
112 const GValue *value,
113 GParamSpec *pspec);
114 static void gdk_surface_get_property (GObject *object,
115 guint prop_id,
116 GValue *value,
117 GParamSpec *pspec);
118
119 static void update_cursor (GdkDisplay *display,
120 GdkDevice *device);
121
122 static void gdk_surface_queue_set_is_mapped (GdkSurface *surface,
123 gboolean is_mapped);
124
125
126 static guint signals[LAST_SIGNAL] = { 0 };
127 static GParamSpec *properties[LAST_PROP] = { NULL, };
128
129 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GdkSurface, gdk_surface, G_TYPE_OBJECT)
130
131 static gboolean
132 gdk_surface_real_beep (GdkSurface *surface)
133 {
134 return FALSE;
135 }
136
137 static GdkDisplay *
138 get_display_for_surface (GdkSurface *primary,
139 GdkSurface *secondary)
140 {
141 GdkDisplay *display = primary->display;
142
143 if (display)
144 return display;
145
146 display = secondary->display;
147
148 if (display)
149 return display;
150
151 g_warning ("no display for surface, using default");
152 return gdk_display_get_default ();
153 }
154
155 static GdkMonitor *
156 get_monitor_for_rect (GdkDisplay *display,
157 const GdkRectangle *rect,
158 void (*get_bounds) (GdkMonitor *monitor,
159 GdkRectangle *bounds))
160 {
161 int biggest_area = G_MININT;
162 GdkMonitor *best_monitor = NULL;
163 GdkMonitor *monitor;
164 GdkRectangle workarea;
165 GdkRectangle intersection;
166 GListModel *monitors;
167 guint i;
168
169 monitors = gdk_display_get_monitors (display);
170 for (i = 0; i < g_list_model_get_n_items (monitors); i++)
171 {
172 monitor = g_list_model_get_item (monitors, i);
173 get_bounds (monitor, &workarea);
174
175 if (gdk_rectangle_intersect (&workarea, rect, &intersection))
176 {
177 if (intersection.width * intersection.height > biggest_area)
178 {
179 biggest_area = intersection.width * intersection.height;
180 best_monitor = monitor;
181 }
182 }
183 g_object_unref (monitor);
184 }
185
186 return best_monitor;
187 }
188
189 static int
190 get_anchor_x_sign (GdkGravity anchor)
191 {
192 switch (anchor)
193 {
194 case GDK_GRAVITY_STATIC:
195 case GDK_GRAVITY_NORTH_WEST:
196 case GDK_GRAVITY_WEST:
197 case GDK_GRAVITY_SOUTH_WEST:
198 return -1;
199
200 default:
201 case GDK_GRAVITY_NORTH:
202 case GDK_GRAVITY_CENTER:
203 case GDK_GRAVITY_SOUTH:
204 return 0;
205
206 case GDK_GRAVITY_NORTH_EAST:
207 case GDK_GRAVITY_EAST:
208 case GDK_GRAVITY_SOUTH_EAST:
209 return 1;
210 }
211 }
212
213 static int
214 get_anchor_y_sign (GdkGravity anchor)
215 {
216 switch (anchor)
217 {
218 case GDK_GRAVITY_STATIC:
219 case GDK_GRAVITY_NORTH_WEST:
220 case GDK_GRAVITY_NORTH:
221 case GDK_GRAVITY_NORTH_EAST:
222 return -1;
223
224 default:
225 case GDK_GRAVITY_WEST:
226 case GDK_GRAVITY_CENTER:
227 case GDK_GRAVITY_EAST:
228 return 0;
229
230 case GDK_GRAVITY_SOUTH_WEST:
231 case GDK_GRAVITY_SOUTH:
232 case GDK_GRAVITY_SOUTH_EAST:
233 return 1;
234 }
235 }
236
237 static int
238 maybe_flip_position (int bounds_pos,
239 int bounds_size,
240 int rect_pos,
241 int rect_size,
242 int surface_size,
243 int rect_sign,
244 int surface_sign,
245 int offset,
246 gboolean flip,
247 gboolean *flipped)
248 {
249 int primary;
250 int secondary;
251
252 *flipped = FALSE;
253 primary = rect_pos + (1 + rect_sign) * rect_size / 2 + offset - (1 + surface_sign) * surface_size / 2;
254
255 if (!flip || (primary >= bounds_pos && primary + surface_size <= bounds_pos + bounds_size))
256 return primary;
257
258 *flipped = TRUE;
259 secondary = rect_pos + (1 - rect_sign) * rect_size / 2 - offset - (1 - surface_sign) * surface_size / 2;
260
261 if ((secondary >= bounds_pos && secondary + surface_size <= bounds_pos + bounds_size) || primary > bounds_pos + bounds_size)
262 return secondary;
263
264 *flipped = FALSE;
265 return primary;
266 }
267
268 GdkMonitor *
269 gdk_surface_get_layout_monitor (GdkSurface *surface,
270 GdkPopupLayout *layout,
271 void (*get_bounds) (GdkMonitor *monitor,
272 GdkRectangle *bounds))
273 {
274 GdkDisplay *display;
275 GdkRectangle root_rect;
276
277 root_rect = *gdk_popup_layout_get_anchor_rect (layout);
278 gdk_surface_get_root_coords (surface->parent,
279 root_rect.x,
280 root_rect.y,
281 &root_rect.x,
282 &root_rect.y);
283
284 root_rect.width = MAX (1, root_rect.width);
285 root_rect.height = MAX (1, root_rect.height);
286
287 display = get_display_for_surface (surface, surface->transient_for);
288 return get_monitor_for_rect (display, &root_rect, get_bounds);
289 }
290
291 void
292 gdk_surface_layout_popup_helper (GdkSurface *surface,
293 int width,
294 int height,
295 int shadow_left,
296 int shadow_right,
297 int shadow_top,
298 int shadow_bottom,
299 GdkMonitor *monitor,
300 GdkRectangle *bounds,
301 GdkPopupLayout *layout,
302 GdkRectangle *out_final_rect)
303 {
304 GdkRectangle root_rect;
305 GdkGravity rect_anchor;
306 GdkGravity surface_anchor;
307 int rect_anchor_dx;
308 int rect_anchor_dy;
309 GdkAnchorHints anchor_hints;
310 GdkRectangle final_rect;
311 gboolean flipped_x;
312 gboolean flipped_y;
313 int x, y;
314
315 g_return_if_fail (GDK_IS_POPUP (surface));
316
317 root_rect = *gdk_popup_layout_get_anchor_rect (layout);
318 gdk_surface_get_root_coords (surface->parent,
319 root_rect.x,
320 root_rect.y,
321 &root_rect.x,
322 &root_rect.y);
323
324 rect_anchor = gdk_popup_layout_get_rect_anchor (layout);
325 surface_anchor = gdk_popup_layout_get_surface_anchor (layout);
326 gdk_popup_layout_get_offset (layout, &rect_anchor_dx, &rect_anchor_dy);
327 anchor_hints = gdk_popup_layout_get_anchor_hints (layout);
328
329 final_rect.width = width - shadow_left - shadow_right;
330 final_rect.height = height - shadow_top - shadow_bottom;
331 final_rect.x = maybe_flip_position (bounds->x,
332 bounds->width,
333 root_rect.x,
334 root_rect.width,
335 final_rect.width,
336 get_anchor_x_sign (rect_anchor),
337 get_anchor_x_sign (surface_anchor),
338 rect_anchor_dx,
339 anchor_hints & GDK_ANCHOR_FLIP_X,
340 &flipped_x);
341 final_rect.y = maybe_flip_position (bounds->y,
342 bounds->height,
343 root_rect.y,
344 root_rect.height,
345 final_rect.height,
346 get_anchor_y_sign (rect_anchor),
347 get_anchor_y_sign (surface_anchor),
348 rect_anchor_dy,
349 anchor_hints & GDK_ANCHOR_FLIP_Y,
350 &flipped_y);
351
352 if (anchor_hints & GDK_ANCHOR_SLIDE_X)
353 {
354 if (final_rect.x + final_rect.width > bounds->x + bounds->width)
355 final_rect.x = bounds->x + bounds->width - final_rect.width;
356
357 if (final_rect.x < bounds->x)
358 final_rect.x = bounds->x;
359 }
360
361 if (anchor_hints & GDK_ANCHOR_SLIDE_Y)
362 {
363 if (final_rect.y + final_rect.height > bounds->y + bounds->height)
364 final_rect.y = bounds->y + bounds->height - final_rect.height;
365
366 if (final_rect.y < bounds->y)
367 final_rect.y = bounds->y;
368 }
369
370 if (anchor_hints & GDK_ANCHOR_RESIZE_X)
371 {
372 if (final_rect.x < bounds->x)
373 {
374 final_rect.width -= bounds->x - final_rect.x;
375 final_rect.x = bounds->x;
376 }
377
378 if (final_rect.x + final_rect.width > bounds->x + bounds->width)
379 final_rect.width = bounds->x + bounds->width - final_rect.x;
380 }
381
382 if (anchor_hints & GDK_ANCHOR_RESIZE_Y)
383 {
384 if (final_rect.y < bounds->y)
385 {
386 final_rect.height -= bounds->y - final_rect.y;
387 final_rect.y = bounds->y;
388 }
389
390 if (final_rect.y + final_rect.height > bounds->y + bounds->height)
391 final_rect.height = bounds->y + bounds->height - final_rect.y;
392 }
393
394 final_rect.x -= shadow_left;
395 final_rect.y -= shadow_top;
396 final_rect.width += shadow_left + shadow_right;
397 final_rect.height += shadow_top + shadow_bottom;
398
399 gdk_surface_get_origin (surface->parent, &x, &y);
400 final_rect.x -= x;
401 final_rect.y -= y;
402
403 if (flipped_x)
404 {
405 rect_anchor = gdk_gravity_flip_horizontally (rect_anchor);
406 surface_anchor = gdk_gravity_flip_horizontally (surface_anchor);
407 }
408 if (flipped_y)
409 {
410 rect_anchor = gdk_gravity_flip_vertically (rect_anchor);
411 surface_anchor = gdk_gravity_flip_vertically (surface_anchor);
412 }
413
414 surface->popup.rect_anchor = rect_anchor;
415 surface->popup.surface_anchor = surface_anchor;
416
417 *out_final_rect = final_rect;
418 }
419
420 /* Since GdkEvent is a GTypeInstance, GValue can only store it as a pointer,
421 * and GClosure does not know how to handle its memory management. To avoid
422 * the event going away in the middle of the signal emission, we provide a
423 * marshaller that keeps the event alive for the duration of the closure.
424 */
425 static void
426 gdk_surface_event_marshaller (GClosure *closure,
427 GValue *return_value,
428 guint n_param_values,
429 const GValue *param_values,
430 gpointer invocation_hint,
431 gpointer marshal_data)
432 {
433 GdkEvent *event = g_value_get_pointer (&param_values[1]);
434
435 gdk_event_ref (event);
436
437 _gdk_marshal_BOOLEAN__POINTER (closure,
438 return_value,
439 n_param_values,
440 param_values,
441 invocation_hint,
442 marshal_data);
443
444
445 gdk_event_unref (event);
446 }
447
448 static void
449 gdk_surface_event_marshallerv (GClosure *closure,
450 GValue *return_value,
451 gpointer instance,
452 va_list args,
453 gpointer marshal_data,
454 int n_params,
455 GType *param_types)
456 {
457 va_list args_copy;
458 GdkEvent *event;
459
460 G_VA_COPY (args_copy, args);
461 event = va_arg (args_copy, gpointer);
462
463 gdk_event_ref (event);
464
465 _gdk_marshal_BOOLEAN__POINTERv (closure,
466 return_value,
467 instance,
468 args,
469 marshal_data,
470 n_params,
471 param_types);
472
473 gdk_event_unref (event);
474
475 va_end (args_copy);
476 }
477
478 static void
479 gdk_surface_init (GdkSurface *surface)
480 {
481 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (surface);
482
483 /* 0-initialization is good for all other fields. */
484
485 surface->state = 0;
486 surface->fullscreen_mode = GDK_FULLSCREEN_ON_CURRENT_MONITOR;
487 surface->width = 1;
488 surface->height = 1;
489
490 surface->alpha = 255;
491
492 surface->device_cursor = g_hash_table_new_full (NULL, NULL,
493 NULL, g_object_unref);
494
495 surface->subsurfaces = g_ptr_array_new ();
496
497 priv->color_state = gdk_color_state_ref (gdk_color_state_get_srgb ());
498 }
499
500 static double
501 gdk_surface_real_get_scale (GdkSurface *surface)
502 {
503 return 1.0;
504 }
505
506 static void
507 gdk_surface_default_get_buffer_size (GdkSurface *surface,
508 GdkDrawContext *context,
509 guint *out_width,
510 guint *out_height)
511 {
512 double scale = gdk_surface_get_scale (surface);
513
514 *out_width = ceil (scale * surface->width);
515 *out_height = ceil (scale * surface->height);
516 }
517
518 static GdkSubsurface *
519 gdk_surface_real_create_subsurface (GdkSurface *surface)
520 {
521 GDK_DISPLAY_DEBUG (gdk_surface_get_display (surface), OFFLOAD,
522 "Subsurfaces not supported for %s", G_OBJECT_TYPE_NAME (surface));
523 return NULL;
524 }
525
526 static void
527 gdk_surface_default_set_opaque_region (GdkSurface *surface,
528 cairo_region_t *region)
529 {
530 }
531
532 static void
533 gdk_surface_constructed (GObject *object)
534 {
535 G_GNUC_UNUSED GdkSurface *surface = GDK_SURFACE (object);
536
537 g_assert (surface->frame_clock != NULL);
538
539 G_OBJECT_CLASS (gdk_surface_parent_class)->constructed (object);
540 }
541
542 static void
543 gdk_surface_class_init (GdkSurfaceClass *klass)
544 {
545 GObjectClass *object_class = G_OBJECT_CLASS (klass);
546
547 object_class->constructed = gdk_surface_constructed;
548 object_class->finalize = gdk_surface_finalize;
549 object_class->set_property = gdk_surface_set_property;
550 object_class->get_property = gdk_surface_get_property;
551
552 klass->beep = gdk_surface_real_beep;
553 klass->get_scale = gdk_surface_real_get_scale;
554 klass->get_buffer_size = gdk_surface_default_get_buffer_size;
555 klass->create_subsurface = gdk_surface_real_create_subsurface;
556 klass->set_opaque_region = gdk_surface_default_set_opaque_region;
557
558 /**
559 * GdkSurface:cursor:
560 *
561 * The mouse pointer for the `GdkSurface`.
562 */
563 properties[PROP_CURSOR] =
564 g_param_spec_object ("cursor", NULL, NULL,
565 GDK_TYPE_CURSOR,
566 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
567
568 /**
569 * GdkSurface:display:
570 *
571 * The `GdkDisplay` connection of the surface.
572 */
573 properties[PROP_DISPLAY] =
574 g_param_spec_object ("display", NULL, NULL,
575 GDK_TYPE_DISPLAY,
576 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
577
578 /**
579 * GdkSurface:frame-clock:
580 *
581 * The `GdkFrameClock` of the surface.
582 */
583 properties[PROP_FRAME_CLOCK] =
584 g_param_spec_object ("frame-clock", NULL, NULL,
585 GDK_TYPE_FRAME_CLOCK,
586 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
587
588 /**
589 * GdkSurface:mapped:
590 *
591 * Whether the surface is mapped.
592 */
593 properties[PROP_MAPPED] =
594 g_param_spec_boolean ("mapped", NULL, NULL,
595 FALSE,
596 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
597
598 /**
599 * GdkSurface:width:
600 *
601 * The width of the surface in pixels.
602 */
603 properties[PROP_WIDTH] =
604 g_param_spec_int ("width", NULL, NULL,
605 0, G_MAXINT, 0,
606 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
607
608 /**
609 * GdkSurface:height:
610 *
611 * The height of the surface, in pixels.
612 */
613 properties[PROP_HEIGHT] =
614 g_param_spec_int ("height", NULL, NULL,
615 0, G_MAXINT, 0,
616 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
617
618 /**
619 * GdkSurface:scale-factor:
620 *
621 * The scale factor of the surface.
622 *
623 * The scale factor is the next larger integer,
624 * compared to [[email protected]:scale].
625 */
626 properties[PROP_SCALE_FACTOR] =
627 g_param_spec_int ("scale-factor", NULL, NULL,
628 1, G_MAXINT, 1,
629 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
630
631 /**
632 * GdkSurface:scale:
633 *
634 * The scale of the surface.
635 *
636 * Since: 4.12
637 */
638 properties[PROP_SCALE] =
639 g_param_spec_double ("scale", NULL, NULL,
640 1., G_MAXDOUBLE, 1.,
641 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
642
643 g_object_class_install_properties (object_class, LAST_PROP, properties);
644
645 /**
646 * GdkSurface::layout:
647 * @surface: the `GdkSurface`
648 * @width: the current width
649 * @height: the current height
650 *
651 * Emitted when the size of @surface is changed, or when relayout should
652 * be performed.
653 *
654 * Surface size is reported in ”application pixels”, not
655 * ”device pixels” (see gdk_surface_get_scale_factor()).
656 */
657 signals[LAYOUT] =
658 g_signal_new (g_intern_static_string ("layout"),
659 G_OBJECT_CLASS_TYPE (object_class),
660 G_SIGNAL_RUN_FIRST,
661 0,
662 NULL,
663 NULL,
664 _gdk_marshal_VOID__INT_INT,
665 G_TYPE_NONE,
666 2,
667 G_TYPE_INT,
668 G_TYPE_INT);
669 g_signal_set_va_marshaller (signals[LAYOUT],
670 G_OBJECT_CLASS_TYPE (object_class),
671 _gdk_marshal_VOID__INT_INTv);
672
673 /**
674 * GdkSurface::render:
675 * @surface: the `GdkSurface`
676 * @region: the region that needs to be redrawn
677 *
678 * Emitted when part of the surface needs to be redrawn.
679 *
680 * Returns: %TRUE to indicate that the signal has been handled
681 */
682 signals[RENDER] =
683 g_signal_new (g_intern_static_string ("render"),
684 G_OBJECT_CLASS_TYPE (object_class),
685 G_SIGNAL_RUN_LAST,
686 0,
687 g_signal_accumulator_true_handled,
688 NULL,
689 _gdk_marshal_BOOLEAN__BOXED,
690 G_TYPE_BOOLEAN,
691 1,
692 CAIRO_GOBJECT_TYPE_REGION);
693 g_signal_set_va_marshaller (signals[RENDER],
694 G_OBJECT_CLASS_TYPE (object_class),
695 _gdk_marshal_BOOLEAN__BOXEDv);
696
697 /**
698 * GdkSurface::event:
699 * @surface: the `GdkSurface`
700 * @event: (type Gdk.Event): an input event
701 *
702 * Emitted when GDK receives an input event for @surface.
703 *
704 * Returns: %TRUE to indicate that the event has been handled
705 */
706 signals[EVENT] =
707 g_signal_new (g_intern_static_string ("event"),
708 G_OBJECT_CLASS_TYPE (object_class),
709 G_SIGNAL_RUN_LAST,
710 0,
711 g_signal_accumulator_true_handled,
712 NULL,
713 gdk_surface_event_marshaller,
714 G_TYPE_BOOLEAN,
715 1,
716 G_TYPE_POINTER);
717 g_signal_set_va_marshaller (signals[EVENT],
718 G_OBJECT_CLASS_TYPE (object_class),
719 gdk_surface_event_marshallerv);
720
721 /**
722 * GdkSurface::enter-monitor:
723 * @surface: the `GdkSurface`
724 * @monitor: the monitor
725 *
726 * Emitted when @surface starts being present on the monitor.
727 */
728 signals[ENTER_MONITOR] =
729 g_signal_new (g_intern_static_string ("enter-monitor"),
730 G_OBJECT_CLASS_TYPE (object_class),
731 G_SIGNAL_RUN_FIRST,
732 0,
733 NULL,
734 NULL,
735 NULL,
736 G_TYPE_NONE,
737 1,
738 GDK_TYPE_MONITOR);
739
740 /**
741 * GdkSurface::leave-monitor:
742 * @surface: the `GdkSurface`
743 * @monitor: the monitor
744 *
745 * Emitted when @surface stops being present on the monitor.
746 */
747 signals[LEAVE_MONITOR] =
748 g_signal_new (g_intern_static_string ("leave-monitor"),
749 G_OBJECT_CLASS_TYPE (object_class),
750 G_SIGNAL_RUN_FIRST,
751 0,
752 NULL,
753 NULL,
754 NULL,
755 G_TYPE_NONE,
756 1,
757 GDK_TYPE_MONITOR);
758 }
759
760 static void
761 seat_removed_cb (GdkDisplay *display,
762 GdkSeat *seat,
763 GdkSurface *surface)
764 {
765 GdkDevice *device = gdk_seat_get_pointer (seat);
766
767 surface->devices_inside = g_list_remove (surface->devices_inside, device);
768 g_hash_table_remove (surface->device_cursor, device);
769 }
770
771 static void
772 gdk_surface_finalize (GObject *object)
773 {
774 GdkSurface *surface = GDK_SURFACE (object);
775 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (surface);
776
777 g_clear_handle_id (&surface->request_motion_id, g_source_remove);
778
779 g_signal_handlers_disconnect_by_func (surface->display,
780 seat_removed_cb, surface);
781
782 if (!GDK_SURFACE_DESTROYED (surface))
783 {
784 g_warning ("losing last reference to undestroyed surface");
785 _gdk_surface_destroy (surface, FALSE);
786 }
787
788 g_clear_pointer (&surface->input_region, cairo_region_destroy);
789 g_clear_object (&surface->cursor);
790 g_clear_pointer (&surface->device_cursor, g_hash_table_destroy);
791 g_clear_pointer (&surface->devices_inside, g_list_free);
792
793 g_clear_object (&surface->display);
794
795 g_clear_pointer (&priv->opaque_region, cairo_region_destroy);
796
797 if (surface->parent)
798 surface->parent->children = g_list_remove (surface->parent->children, surface);
799
800 g_assert (surface->subsurfaces->len == 0);
801
802 g_ptr_array_unref (surface->subsurfaces);
803
804 g_clear_pointer (&priv->color_state, gdk_color_state_unref);
805
806 G_OBJECT_CLASS (gdk_surface_parent_class)->finalize (object);
807 }
808
809 static void
810 gdk_surface_set_property (GObject *object,
811 guint prop_id,
812 const GValue *value,
813 GParamSpec *pspec)
814 {
815 GdkSurface *surface = GDK_SURFACE (object);
816
817 switch (prop_id)
818 {
819 case PROP_CURSOR:
820 gdk_surface_set_cursor (surface, g_value_get_object (value));
821 break;
822
823 case PROP_DISPLAY:
824 surface->display = g_value_dup_object (value);
825 g_assert (surface->display != NULL);
826 g_signal_connect (surface->display, "seat-removed",
827 G_CALLBACK (seat_removed_cb), surface);
828 break;
829
830 case PROP_FRAME_CLOCK:
831 gdk_surface_set_frame_clock (surface, GDK_FRAME_CLOCK (g_value_get_object (value)));
832 break;
833
834 default:
835 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
836 break;
837 }
838 }
839
840 #define GDK_SURFACE_IS_STICKY(surface) (((surface)->state & GDK_TOPLEVEL_STATE_STICKY))
841
842 static void
843 gdk_surface_get_property (GObject *object,
844 guint prop_id,
845 GValue *value,
846 GParamSpec *pspec)
847 {
848 GdkSurface *surface = GDK_SURFACE (object);
849
850 switch (prop_id)
851 {
852 case PROP_CURSOR:
853 g_value_set_object (value, gdk_surface_get_cursor (surface));
854 break;
855
856 case PROP_DISPLAY:
857 g_value_set_object (value, surface->display);
858 break;
859
860 case PROP_FRAME_CLOCK:
861 g_value_set_object (value, surface->frame_clock);
862 break;
863
864 case PROP_MAPPED:
865 g_value_set_boolean (value, GDK_SURFACE_IS_MAPPED (surface));
866 break;
867
868 case PROP_WIDTH:
869 g_value_set_int (value, surface->width);
870 break;
871
872 case PROP_HEIGHT:
873 g_value_set_int (value, surface->height);
874 break;
875
876 case PROP_SCALE_FACTOR:
877 g_value_set_int (value, gdk_surface_get_scale_factor (surface));
878 break;
879
880 case PROP_SCALE:
881 g_value_set_double (value, gdk_surface_get_scale (surface));
882 break;
883
884 default:
885 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
886 break;
887 }
888 }
889
890 void
891 _gdk_surface_update_size (GdkSurface *surface)
892 {
893 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (surface);
894
895 if (priv->attached_context)
896 gdk_draw_context_surface_resized (priv->attached_context);
897
898 g_object_notify (G_OBJECT (surface), "width");
899 g_object_notify (G_OBJECT (surface), "height");
900 }
901
902 /**
903 * gdk_surface_new_toplevel: (constructor)
904 * @display: the display to create the surface on
905 *
906 * Creates a new toplevel surface.
907 *
908 * Returns: (transfer full): the new `GdkSurface`
909 */
910 GdkSurface *
911 gdk_surface_new_toplevel (GdkDisplay *display)
912 {
913 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
914
915 return g_object_new (GDK_DISPLAY_GET_CLASS (display)->toplevel_type,
916 "display", display,
917 NULL);
918 }
919
920 /**
921 * gdk_surface_new_popup: (constructor)
922 * @parent: the parent surface to attach the surface to
923 * @autohide: whether to hide the surface on outside clicks
924 *
925 * Create a new popup surface.
926 *
927 * The surface will be attached to @parent and can be positioned
928 * relative to it using [[email protected]].
929 *
930 * Returns: (transfer full): a new `GdkSurface`
931 */
932 GdkSurface *
933 gdk_surface_new_popup (GdkSurface *parent,
934 gboolean autohide)
935 {
936 GdkSurface *surface;
937 GdkDisplay *display;
938
939 g_return_val_if_fail (GDK_IS_SURFACE (parent), NULL);
940
941 display = gdk_surface_get_display (parent);
942
943 surface = g_object_new (GDK_DISPLAY_GET_CLASS (display)->popup_type,
944 "display", display,
945 "parent", parent,
946 NULL);
947
948 surface->autohide = autohide;
949
950 return surface;
951 }
952
953 static void
954 update_pointer_info_foreach (GdkDisplay *display,
955 GdkDevice *device,
956 GdkPointerSurfaceInfo *pointer_info,
957 gpointer user_data)
958 {
959 GdkSurface *surface = user_data;
960
961 if (pointer_info->surface_under_pointer == surface)
962 {
963 g_object_unref (pointer_info->surface_under_pointer);
964 pointer_info->surface_under_pointer = NULL;
965 }
966 }
967
968 static void
969 surface_remove_from_pointer_info (GdkSurface *surface,
970 GdkDisplay *display)
971 {
972 _gdk_display_pointer_info_foreach (display,
973 update_pointer_info_foreach,
974 surface);
975 }
976
977 /**
978 * _gdk_surface_destroy_hierarchy:
979 * @surface: a `GdkSurface`
980 * @recursing_native: If %TRUE, then this is being called because a native
981 * parent was destroyed. This generally means that the call to the windowing
982 * system to destroy the surface can be omitted, since it will be destroyed
983 * as a result of the parent being destroyed. Unless @foreign_destroy.
984 * @foreign_destroy: If %TRUE, the surface or a parent was destroyed by some
985 * external agency. The surface has already been destroyed and no windowing
986 * system calls should be made. (This may never happen for some windowing
987 * systems.)
988 *
989 * Internal function to destroy a surface. Like gdk_surface_destroy(),
990 * but does not drop the reference count created by gdk_surface_new().
991 */
992 static void
993 _gdk_surface_destroy_hierarchy (GdkSurface *surface,
994 gboolean foreign_destroy)
995 {
996 G_GNUC_UNUSED GdkSurfacePrivate *priv = gdk_surface_get_instance_private (surface);
997
998 g_return_if_fail (GDK_IS_SURFACE (surface));
999
1000 if (GDK_SURFACE_DESTROYED (surface))
1001 return;
1002
1003 if (priv->attached_context)
1004 {
1005 gdk_draw_context_detach (priv->attached_context);
1006 g_assert (priv->attached_context == NULL);
1007 }
1008
1009 GDK_SURFACE_GET_CLASS (surface)->destroy (surface, foreign_destroy);
1010
1011 if (surface->gl_paint_context)
1012 {
1013 /* Make sure to destroy if current */
1014 g_object_run_dispose (G_OBJECT (surface->gl_paint_context));
1015 g_object_unref (surface->gl_paint_context);
1016 surface->gl_paint_context = NULL;
1017 }
1018
1019 if (surface->frame_clock)
1020 {
1021 if (surface->parent == NULL)
1022 g_object_run_dispose (G_OBJECT (surface->frame_clock));
1023 gdk_surface_set_frame_clock (surface, NULL);
1024 }
1025
1026 _gdk_surface_clear_update_area (surface);
1027
1028 g_clear_handle_id (&surface->set_is_mapped_source_id, g_source_remove);
1029 surface->is_mapped = FALSE;
1030 surface->pending_is_mapped = FALSE;
1031
1032 surface->destroyed = TRUE;
1033
1034 surface_remove_from_pointer_info (surface, surface->display);
1035
1036 if (GDK_IS_TOPLEVEL (surface))
1037 g_object_notify (G_OBJECT (surface), "state");
1038 g_object_notify_by_pspec (G_OBJECT (surface), properties[PROP_MAPPED]);
1039 }
1040
1041 /**
1042 * _gdk_surface_destroy:
1043 * @surface: a `GdkSurface`
1044 * @foreign_destroy: If %TRUE, the surface or a parent was destroyed by some
1045 * external agency. The surface has already been destroyed and no windowing
1046 * system calls should be made. (This may never happen for some windowing
1047 * systems.)
1048 *
1049 * Internal function to destroy a surface. Like gdk_surface_destroy(),
1050 * but does not drop the reference count created by gdk_surface_new().
1051 */
1052 void
1053 _gdk_surface_destroy (GdkSurface *surface,
1054 gboolean foreign_destroy)
1055 {
1056 _gdk_surface_destroy_hierarchy (surface, foreign_destroy);
1057 }
1058
1059 /**
1060 * gdk_surface_destroy:
1061 * @surface: a `GdkSurface`
1062 *
1063 * Destroys the window system resources associated with @surface and
1064 * decrements @surface's reference count.
1065 *
1066 * The window system resources for all children of @surface are also
1067 * destroyed, but the children’s reference counts are not decremented.
1068 *
1069 * Note that a surface will not be destroyed automatically when its
1070 * reference count reaches zero. You must call this function yourself
1071 * before that happens.
1072 */
1073 void
1074 gdk_surface_destroy (GdkSurface *surface)
1075 {
1076 _gdk_surface_destroy_hierarchy (surface, FALSE);
1077 g_object_unref (surface);
1078 }
1079
1080 void
1081 gdk_surface_set_widget (GdkSurface *self,
1082 gpointer widget)
1083 {
1084 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (self);
1085
1086 priv->widget = widget;
1087 }
1088
1089 gpointer
1090 gdk_surface_get_widget (GdkSurface *self)
1091 {
1092 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (self);
1093
1094 return priv->widget;
1095 }
1096
1097 /**
1098 * gdk_surface_get_display:
1099 * @surface: a `GdkSurface`
1100 *
1101 * Gets the `GdkDisplay` associated with a `GdkSurface`.
1102 *
1103 * Returns: (transfer none): the `GdkDisplay` associated with @surface
1104 */
1105 GdkDisplay *
1106 gdk_surface_get_display (GdkSurface *surface)
1107 {
1108 g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
1109
1110 return surface->display;
1111 }
1112 /**
1113 * gdk_surface_is_destroyed:
1114 * @surface: a `GdkSurface`
1115 *
1116 * Check to see if a surface is destroyed.
1117 *
1118 * Returns: %TRUE if the surface is destroyed
1119 */
1120 gboolean
1121 gdk_surface_is_destroyed (GdkSurface *surface)
1122 {
1123 return GDK_SURFACE_DESTROYED (surface);
1124 }
1125
1126 /**
1127 * gdk_surface_get_mapped:
1128 * @surface: a `GdkSurface`
1129 *
1130 * Checks whether the surface has been mapped.
1131 *
1132 * A surface is mapped with [[email protected]]
1133 * or [[email protected]].
1134 *
1135 * Returns: %TRUE if the surface is mapped
1136 */
1137 gboolean
1138 gdk_surface_get_mapped (GdkSurface *surface)
1139 {
1140 g_return_val_if_fail (GDK_IS_SURFACE (surface), FALSE);
1141
1142 return GDK_SURFACE_IS_MAPPED (surface);
1143 }
1144
1145 gboolean
1146 gdk_surface_get_gl_is_srgb (GdkSurface *self)
1147 {
1148 return self->is_srgb;
1149 }
1150
1151 GdkGLContext *
1152 gdk_surface_get_paint_gl_context (GdkSurface *surface,
1153 GError **error)
1154 {
1155 if (!gdk_display_prepare_gl (surface->display, error))
1156 return NULL;
1157
1158 if (surface->gl_paint_context == NULL)
1159 {
1160 surface->gl_paint_context = gdk_surface_create_gl_context (surface, error);
1161 if (surface->gl_paint_context == NULL)
1162 return NULL;
1163 }
1164
1165 if (!gdk_gl_context_realize (surface->gl_paint_context, error))
1166 {
1167 g_clear_object (&surface->gl_paint_context);
1168 return NULL;
1169 }
1170
1171 return surface->gl_paint_context;
1172 }
1173
1174 /**
1175 * gdk_surface_create_gl_context:
1176 * @surface: a `GdkSurface`
1177 * @error: return location for an error
1178 *
1179 * Creates a new `GdkGLContext` for the `GdkSurface`.
1180 *
1181 * The context is disconnected from any particular surface or surface.
1182 * If the creation of the `GdkGLContext` failed, @error will be set.
1183 * Before using the returned `GdkGLContext`, you will need to
1184 * call [[email protected]_current] or [[email protected]].
1185 *
1186 * Returns: (transfer full): the newly created `GdkGLContext`
1187 */
1188 GdkGLContext *
1189 gdk_surface_create_gl_context (GdkSurface *surface,
1190 GError **error)
1191 {
1192 g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
1193 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1194
1195 if (!gdk_display_prepare_gl (surface->display, error))
1196 return NULL;
1197
1198 return gdk_gl_context_new (surface->display, surface, FALSE);
1199 }
1200
1201 /**
1202 * gdk_surface_create_cairo_context:
1203 * @surface: a `GdkSurface`
1204 *
1205 * Creates a new `GdkCairoContext` for rendering on @surface.
1206 *
1207 * Returns: (transfer full): the newly created `GdkCairoContext`
1208 *
1209 * Deprecated: 4.18: Drawing content with Cairo should be done via
1210 * Cairo rendernodes, not by using the Cairo renderer.
1211 */
1212 GdkCairoContext *
1213 gdk_surface_create_cairo_context (GdkSurface *surface)
1214 {
1215 GdkDisplay *display;
1216
1217 g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
1218
1219 display = surface->display;
1220
1221 return g_object_new (GDK_DISPLAY_GET_CLASS (display)->cairo_context_type,
1222 "surface", surface,
1223 NULL);
1224 }
1225
1226 /**
1227 * gdk_surface_create_vulkan_context:
1228 * @surface: a `GdkSurface`
1229 * @error: return location for an error
1230 *
1231 * Sets an error and returns %NULL.
1232 *
1233 * Returns: (transfer full): %NULL
1234 *
1235 * Deprecated: 4.14: GTK does not expose any Vulkan internals. This
1236 * function is a leftover that was accidentally exposed.
1237 */
1238 GdkVulkanContext *
1239 gdk_surface_create_vulkan_context (GdkSurface *surface,
1240 GError **error)
1241 {
1242 g_set_error (error, GDK_VULKAN_ERROR, GDK_VULKAN_ERROR_UNSUPPORTED,
1243 "GTK does not expose Vulkan internals.");
1244 return FALSE;
1245 }
1246
1247 static gboolean
1248 gdk_surface_is_toplevel_frozen (GdkSurface *surface)
1249 {
1250 return surface->update_and_descendants_freeze_count > 0;
1251 }
1252
1253 static void
1254 gdk_surface_schedule_update (GdkSurface *surface)
1255 {
1256 GdkFrameClock *frame_clock;
1257
1258 g_return_if_fail (surface);
1259
1260 surface->pending_phases |= GDK_FRAME_CLOCK_PHASE_PAINT;
1261
1262 if (surface->update_freeze_count ||
1263 gdk_surface_is_toplevel_frozen (surface))
1264 return;
1265
1266 /* If there's no frame clock (a foreign surface), then the invalid
1267 * region will just stick around unless gdk_surface_process_updates()
1268 * is called.
1269 */
1270 frame_clock = gdk_surface_get_frame_clock (surface);
1271 if (frame_clock)
1272 gdk_frame_clock_request_phase (gdk_surface_get_frame_clock (surface),
1273 GDK_FRAME_CLOCK_PHASE_PAINT);
1274 }
1275
1276 static void
1277 gdk_surface_layout_on_clock (GdkFrameClock *clock,
1278 void *data)
1279 {
1280 GdkSurface *surface = GDK_SURFACE (data);
1281 GdkSurfaceClass *class;
1282
1283 g_return_if_fail (GDK_IS_SURFACE (surface));
1284
1285 if (GDK_SURFACE_DESTROYED (surface))
1286 return;
1287
1288 if (!GDK_SURFACE_IS_MAPPED (surface))
1289 return;
1290
1291 surface->pending_phases &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
1292
1293 class = GDK_SURFACE_GET_CLASS (surface);
1294 if (class->compute_size)
1295 {
1296 if (class->compute_size (surface))
1297 return;
1298 }
1299
1300 g_signal_emit (surface, signals[LAYOUT], 0, surface->width, surface->height);
1301 }
1302
1303 /**
1304 * gdk_surface_request_layout:
1305 * @surface: a `GdkSurface`
1306 *
1307 * Request a layout phase from the surface's frame clock.
1308 *
1309 * See [[email protected]_phase].
1310 */
1311 void
1312 gdk_surface_request_layout (GdkSurface *surface)
1313 {
1314 GdkSurfaceClass *class;
1315 GdkFrameClock *frame_clock;
1316
1317 class = GDK_SURFACE_GET_CLASS (surface);
1318 if (class->request_layout)
1319 class->request_layout (surface);
1320
1321 frame_clock = gdk_surface_get_frame_clock (surface);
1322 g_return_if_fail (frame_clock);
1323
1324 gdk_frame_clock_request_phase (frame_clock,
1325 GDK_FRAME_CLOCK_PHASE_LAYOUT);
1326 }
1327
1328 static void
1329 gdk_surface_paint_on_clock (GdkFrameClock *clock,
1330 void *data)
1331 {
1332 GdkSurface *surface = GDK_SURFACE (data);
1333 cairo_region_t *expose_region;
1334
1335 g_return_if_fail (GDK_IS_SURFACE (surface));
1336
1337 if (GDK_SURFACE_DESTROYED (surface) ||
1338 !surface->update_area ||
1339 surface->update_freeze_count ||
1340 gdk_surface_is_toplevel_frozen (surface))
1341 return;
1342
1343 surface->pending_phases &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
1344 expose_region = surface->update_area;
1345 surface->update_area = NULL;
1346
1347 if (GDK_SURFACE_IS_MAPPED (surface))
1348 {
1349 gboolean handled;
1350
1351 g_object_ref (surface);
1352
1353 g_signal_emit (surface, signals[RENDER], 0, expose_region, &handled);
1354
1355 g_object_unref (surface);
1356 }
1357
1358 cairo_region_destroy (expose_region);
1359 }
1360
1361 /*
1362 * gdk_surface_invalidate_rect:
1363 * @surface: a `GdkSurface`
1364 * @rect: (nullable): rectangle to invalidate or %NULL to
1365 * invalidate the whole surface
1366 *
1367 * Invalidate a rectangular region of @surface.
1368 *
1369 * This is a convenience wrapper around
1370 * [[email protected]_region].
1371 */
1372 void
1373 gdk_surface_invalidate_rect (GdkSurface *surface,
1374 const GdkRectangle *rect)
1375 {
1376 GdkRectangle surface_rect;
1377 cairo_region_t *region;
1378
1379 g_return_if_fail (GDK_IS_SURFACE (surface));
1380
1381 if (!GDK_SURFACE_IS_MAPPED (surface))
1382 return;
1383
1384 if (!rect)
1385 {
1386 surface_rect.x = 0;
1387 surface_rect.y = 0;
1388 surface_rect.width = surface->width;
1389 surface_rect.height = surface->height;
1390 rect = &surface_rect;
1391 }
1392
1393 region = cairo_region_create_rectangle (rect);
1394 gdk_surface_invalidate_region (surface, region);
1395 cairo_region_destroy (region);
1396 }
1397
1398 static void
1399 impl_surface_add_update_area (GdkSurface *impl_surface,
1400 cairo_region_t *region)
1401 {
1402 if (impl_surface->update_area)
1403 cairo_region_union (impl_surface->update_area, region);
1404 else
1405 {
1406 impl_surface->update_area = cairo_region_copy (region);
1407 gdk_surface_schedule_update (impl_surface);
1408 }
1409 }
1410
1411 /**
1412 * gdk_surface_queue_render:
1413 * @surface: a `GdkSurface`
1414 *
1415 * Forces a [[email protected]::render] signal emission for @surface
1416 * to be scheduled.
1417 *
1418 * This function is useful for implementations that track invalid
1419 * regions on their own.
1420 */
1421 void
1422 gdk_surface_queue_render (GdkSurface *surface)
1423 {
1424 cairo_region_t *region;
1425
1426 g_return_if_fail (GDK_IS_SURFACE (surface));
1427
1428 region = cairo_region_create ();
1429 impl_surface_add_update_area (surface, region);
1430 cairo_region_destroy (region);
1431 }
1432
1433 /*
1434 * gdk_surface_invalidate_region:
1435 * @surface: a `GdkSurface`
1436 * @region: a `cairo_region_t`
1437 *
1438 * Adds @region to the update area for @surface.
1439 *
1440 * The update area is the region that needs to be redrawn,
1441 * or “dirty region.”
1442 *
1443 * GDK will process all updates whenever the frame clock schedules
1444 * a redraw, so there’s no need to do forces redraws manually, you
1445 * just need to invalidate regions that you know should be redrawn.
1446 */
1447 void
1448 gdk_surface_invalidate_region (GdkSurface *surface,
1449 const cairo_region_t *region)
1450 {
1451 cairo_region_t *visible_region;
1452 cairo_rectangle_int_t r;
1453
1454 g_return_if_fail (GDK_IS_SURFACE (surface));
1455
1456 if (!GDK_SURFACE_IS_MAPPED (surface))
1457 return;
1458
1459 if (cairo_region_is_empty (region))
1460 return;
1461
1462 r.x = 0;
1463 r.y = 0;
1464 r.width = surface->width;
1465 r.height = surface->height;
1466
1467 visible_region = cairo_region_copy (region);
1468
1469 cairo_region_intersect_rectangle (visible_region, &r);
1470 impl_surface_add_update_area (surface, visible_region);
1471
1472 cairo_region_destroy (visible_region);
1473 }
1474
1475 /*
1476 * _gdk_surface_clear_update_area:
1477 * @surface: a `GdkSurface`
1478 *
1479 * Internal function to clear the update area for a surface.
1480 * This is called when the surface is hidden or destroyed.
1481 */
1482 void
1483 _gdk_surface_clear_update_area (GdkSurface *surface)
1484 {
1485 g_return_if_fail (GDK_IS_SURFACE (surface));
1486
1487 if (surface->update_area)
1488 {
1489 cairo_region_destroy (surface->update_area);
1490 surface->update_area = NULL;
1491 }
1492 }
1493
1494 /*
1495 * gdk_surface_freeze_updates:
1496 * @surface: a `GdkSurface`
1497 *
1498 * Temporarily freezes a surface such that it won’t receive expose
1499 * events. The surface will begin receiving expose events again when
1500 * gdk_surface_thaw_updates() is called. If gdk_surface_freeze_updates()
1501 * has been called more than once, gdk_surface_thaw_updates() must be
1502 * called an equal number of times to begin processing exposes.
1503 */
1504 void
1505 gdk_surface_freeze_updates (GdkSurface *surface)
1506 {
1507 g_return_if_fail (GDK_IS_SURFACE (surface));
1508
1509 if (GDK_DEBUG_CHECK (NO_VSYNC))
1510 return;
1511
1512 surface->update_freeze_count++;
1513 if (surface->update_freeze_count == 1)
1514 _gdk_frame_clock_uninhibit_freeze (surface->frame_clock);
1515 }
1516
1517 static gboolean
1518 request_motion_cb (void *data)
1519 {
1520 GdkSurface *surface = GDK_SURFACE (data);
1521 GdkFrameClock *clock = gdk_surface_get_frame_clock (surface);
1522
1523 if (clock)
1524 gdk_frame_clock_request_phase (clock, GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS);
1525 surface->request_motion_id = 0;
1526
1527 return G_SOURCE_REMOVE;
1528 }
1529
1530
1531 /*
1532 * gdk_surface_thaw_updates:
1533 * @surface: a `GdkSurface`
1534 *
1535 * Thaws a surface frozen with gdk_surface_freeze_updates(). Note that this
1536 * will not necessarily schedule updates if the surface freeze count reaches
1537 * zero.
1538 */
1539 void
1540 gdk_surface_thaw_updates (GdkSurface *surface)
1541 {
1542 g_return_if_fail (GDK_IS_SURFACE (surface));
1543
1544 if (GDK_DEBUG_CHECK (NO_VSYNC))
1545 return;
1546
1547 g_return_if_fail (surface->update_freeze_count > 0);
1548
1549 if (--surface->update_freeze_count == 0)
1550 {
1551 GdkFrameClock *frame_clock = surface->frame_clock;
1552
1553 _gdk_frame_clock_inhibit_freeze (frame_clock);
1554
1555 if (surface->pending_phases)
1556 gdk_frame_clock_request_phase (frame_clock, surface->pending_phases);
1557
1558 if (surface->request_motion && surface->request_motion_id == 0)
1559 {
1560 surface->request_motion_id =
1561 g_idle_add_full (GDK_PRIORITY_REDRAW + 20, request_motion_cb, surface, NULL);
1562 gdk_source_set_static_name_by_id (surface->request_motion_id, "[gtk] request_motion_cb");
1563 }
1564 }
1565 }
1566
1567 /*
1568 * gdk_surface_constrain_size:
1569 * @geometry: a `GdkGeometry` structure
1570 * @flags: a mask indicating what portions of @geometry are set
1571 * @width: desired width of surface
1572 * @height: desired height of the surface
1573 * @new_width: (out): location to store resulting width
1574 * @new_height: (out): location to store resulting height
1575 *
1576 * Constrains a desired width and height according to a
1577 * set of geometry hints (such as minimum and maximum size).
1578 */
1579 void
1580 gdk_surface_constrain_size (GdkGeometry *geometry,
1581 GdkSurfaceHints flags,
1582 int width,
1583 int height,
1584 int *new_width,
1585 int *new_height)
1586 {
1587 /* This routine is partially borrowed from fvwm.
1588 *
1589 * Copyright 1993, Robert Nation
1590 * You may use this code for any purpose, as long as the original
1591 * copyright remains in the source code and all documentation
1592 *
1593 * which in turn borrows parts of the algorithm from uwm
1594 */
1595 int min_width = 0;
1596 int min_height = 0;
1597 int max_width = G_MAXINT;
1598 int max_height = G_MAXINT;
1599
1600 if (flags & GDK_HINT_MIN_SIZE)
1601 {
1602 min_width = geometry->min_width;
1603 min_height = geometry->min_height;
1604 }
1605
1606 if (flags & GDK_HINT_MAX_SIZE)
1607 {
1608 max_width = geometry->max_width ;
1609 max_height = geometry->max_height;
1610 }
1611
1612 /* clamp width and height to min and max values
1613 */
1614 width = CLAMP (width, min_width, max_width);
1615 height = CLAMP (height, min_height, max_height);
1616
1617 *new_width = width;
1618 *new_height = height;
1619 }
1620
1621 /**
1622 * gdk_surface_get_device_position:
1623 * @surface: a `GdkSurface`
1624 * @device: pointer `GdkDevice` to query to
1625 * @x: (out) (optional): return location for the X coordinate of @device
1626 * @y: (out) (optional): return location for the Y coordinate of @device
1627 * @mask: (out) (optional): return location for the modifier mask
1628 *
1629 * Obtains the current device position and modifier state.
1630 *
1631 * The position is given in coordinates relative to the upper
1632 * left corner of @surface.
1633 *
1634 * Return: %TRUE if the device is over the surface
1635 */
1636 gboolean
1637 gdk_surface_get_device_position (GdkSurface *surface,
1638 GdkDevice *device,
1639 double *x,
1640 double *y,
1641 GdkModifierType *mask)
1642 {
1643 double tmp_x, tmp_y;
1644 GdkModifierType tmp_mask;
1645 gboolean ret;
1646
1647 g_return_val_if_fail (GDK_IS_SURFACE (surface), FALSE);
1648 g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
1649 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, FALSE);
1650
1651 tmp_x = 0;
1652 tmp_y = 0;
1653 tmp_mask = 0;
1654
1655 ret = GDK_SURFACE_GET_CLASS (surface)->get_device_state (surface,
1656 device,
1657 &tmp_x, &tmp_y,
1658 &tmp_mask);
1659
1660 if (x)
1661 *x = tmp_x;
1662 if (y)
1663 *y = tmp_y;
1664 if (mask)
1665 *mask = tmp_mask;
1666
1667 return ret;
1668 }
1669
1670 /**
1671 * gdk_surface_hide:
1672 * @surface: a `GdkSurface`
1673 *
1674 * Hide the surface.
1675 *
1676 * For toplevel surfaces, withdraws them, so they will no longer be
1677 * known to the window manager; for all surfaces, unmaps them, so
1678 * they won’t be displayed. Normally done automatically as
1679 * part of [gtk_widget_hide()](../gtk4/method.Widget.hide.html).
1680 */
1681 void
1682 gdk_surface_hide (GdkSurface *surface)
1683 {
1684 gboolean was_mapped;
1685
1686 g_return_if_fail (GDK_IS_SURFACE (surface));
1687
1688 if (surface->destroyed)
1689 return;
1690
1691 was_mapped = GDK_SURFACE_IS_MAPPED (surface);
1692
1693 gdk_surface_queue_set_is_mapped (surface, FALSE);
1694
1695 if (was_mapped)
1696 {
1697 GdkDisplay *display;
1698 GdkSeat *seat;
1699 GList *devices, *d;
1700
1701 /* May need to break grabs on children */
1702 display = surface->display;
1703 seat = gdk_display_get_default_seat (display);
1704 if (seat)
1705 {
1706 devices = gdk_seat_get_devices (seat, GDK_SEAT_CAPABILITY_ALL);
1707 devices = g_list_prepend (devices, gdk_seat_get_keyboard (seat));
1708 devices = g_list_prepend (devices, gdk_seat_get_pointer (seat));
1709 }
1710 else
1711 devices = NULL;
1712
1713 for (d = devices; d; d = d->next)
1714 {
1715 GdkDevice *device = d->data;
1716
1717 if (_gdk_display_end_device_grab (display,
1718 device,
1719 _gdk_display_get_next_serial (display),
1720 surface,
1721 TRUE))
1722 {
1723 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1724 gdk_device_ungrab (device, GDK_CURRENT_TIME);
1725 G_GNUC_END_IGNORE_DEPRECATIONS
1726 }
1727 }
1728
1729 g_list_free (devices);
1730 }
1731
1732 GDK_SURFACE_GET_CLASS (surface)->hide (surface);
1733
1734 surface->popup.rect_anchor = 0;
1735 surface->popup.surface_anchor = 0;
1736 surface->x = 0;
1737 surface->y = 0;
1738 }
1739
1740 static void
1741 gdk_surface_set_cursor_internal (GdkSurface *surface,
1742 GdkDevice *device,
1743 GdkCursor *cursor)
1744 {
1745 GdkPointerSurfaceInfo *pointer_info;
1746
1747 if (GDK_SURFACE_DESTROYED (surface))
1748 return;
1749
1750 g_assert (surface->display == gdk_device_get_display (device));
1751
1752 pointer_info = _gdk_display_get_pointer_info (surface->display, device);
1753
1754 if (surface == pointer_info->surface_under_pointer)
1755 update_cursor (surface->display, device);
1756 }
1757
1758 /**
1759 * gdk_surface_get_cursor:
1760 * @surface: a `GdkSurface`
1761 *
1762 * Retrieves a `GdkCursor` pointer for the cursor currently set on the
1763 * `GdkSurface`.
1764 *
1765 * If the return value is %NULL then there is no custom cursor set on
1766 * the surface, and it is using the cursor for its parent surface.
1767 *
1768 * Use [[email protected]_cursor] to unset the cursor of the surface.
1769 *
1770 * Returns: (nullable) (transfer none): a `GdkCursor`
1771 */
1772 GdkCursor *
1773 gdk_surface_get_cursor (GdkSurface *surface)
1774 {
1775 g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
1776
1777 return surface->cursor;
1778 }
1779
1780 /**
1781 * gdk_surface_set_cursor:
1782 * @surface: a `GdkSurface`
1783 * @cursor: (nullable): a `GdkCursor`
1784 *
1785 * Sets the default mouse pointer for a `GdkSurface`.
1786 *
1787 * Passing %NULL for the @cursor argument means that @surface will use
1788 * the cursor of its parent surface. Most surfaces should use this default.
1789 * Note that @cursor must be for the same display as @surface.
1790 *
1791 * Use [[email protected]_from_name] or [[email protected]_from_texture]
1792 * to create the cursor. To make the cursor invisible, use %GDK_BLANK_CURSOR.
1793 */
1794 void
1795 gdk_surface_set_cursor (GdkSurface *surface,
1796 GdkCursor *cursor)
1797 {
1798 g_return_if_fail (GDK_IS_SURFACE (surface));
1799
1800 if (surface->cursor)
1801 {
1802 g_object_unref (surface->cursor);
1803 surface->cursor = NULL;
1804 }
1805
1806 if (!GDK_SURFACE_DESTROYED (surface))
1807 {
1808 GdkDevice *device;
1809 GList *seats, *s;
1810
1811 if (cursor)
1812 surface->cursor = g_object_ref (cursor);
1813
1814 seats = gdk_display_list_seats (surface->display);
1815
1816 for (s = seats; s; s = s->next)
1817 {
1818 GList *devices, *d;
1819
1820 device = gdk_seat_get_pointer (s->data);
1821 gdk_surface_set_cursor_internal (surface, device, surface->cursor);
1822
1823 devices = gdk_seat_get_devices (s->data, GDK_SEAT_CAPABILITY_TABLET_STYLUS);
1824 for (d = devices; d; d = d->next)
1825 {
1826 device = d->data;
1827 gdk_surface_set_cursor_internal (surface, device, surface->cursor);
1828 }
1829 g_list_free (devices);
1830 }
1831
1832 g_list_free (seats);
1833 g_object_notify_by_pspec (G_OBJECT (surface), properties[PROP_CURSOR]);
1834 }
1835 }
1836
1837 /**
1838 * gdk_surface_get_device_cursor:
1839 * @surface: a `GdkSurface`
1840 * @device: a pointer `GdkDevice`
1841 *
1842 * Retrieves a `GdkCursor` pointer for the @device currently set on the
1843 * specified `GdkSurface`.
1844 *
1845 * If the return value is %NULL then there is no custom cursor set on the
1846 * specified surface, and it is using the cursor for its parent surface.
1847 *
1848 * Use [[email protected]_cursor] to unset the cursor of the surface.
1849 *
1850 * Returns: (nullable) (transfer none): a `GdkCursor`
1851 */
1852 GdkCursor *
1853 gdk_surface_get_device_cursor (GdkSurface *surface,
1854 GdkDevice *device)
1855 {
1856 g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
1857 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
1858 g_return_val_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD, NULL);
1859
1860 return g_hash_table_lookup (surface->device_cursor, device);
1861 }
1862
1863 /**
1864 * gdk_surface_set_device_cursor:
1865 * @surface: a `GdkSurface`
1866 * @device: a pointer `GdkDevice`
1867 * @cursor: a `GdkCursor`
1868 *
1869 * Sets a specific `GdkCursor` for a given device when it gets inside @surface.
1870 *
1871 * Passing %NULL for the @cursor argument means that @surface will use the
1872 * cursor of its parent surface. Most surfaces should use this default.
1873 *
1874 * Use [[email protected]_from_name] or [[email protected]_from_texture]
1875 * to create the cursor. To make the cursor invisible, use %GDK_BLANK_CURSOR.
1876 */
1877 void
1878 gdk_surface_set_device_cursor (GdkSurface *surface,
1879 GdkDevice *device,
1880 GdkCursor *cursor)
1881 {
1882 g_return_if_fail (GDK_IS_SURFACE (surface));
1883 g_return_if_fail (GDK_IS_DEVICE (device));
1884 g_return_if_fail (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD);
1885
1886 if (!cursor)
1887 g_hash_table_remove (surface->device_cursor, device);
1888 else
1889 g_hash_table_replace (surface->device_cursor, device, g_object_ref (cursor));
1890
1891 gdk_surface_set_cursor_internal (surface, device, cursor);
1892 }
1893
1894 /*
1895 * gdk_surface_get_geometry:
1896 * @surface: a `GdkSurface`
1897 * @x: (out) (optional): return location for X coordinate of surface (relative to its parent)
1898 * @y: (out) (optional): return location for Y coordinate of surface (relative to its parent)
1899 * @width: (out) (optional): return location for width of surface
1900 * @height: (out) (optional): return location for height of surface
1901 *
1902 * Get the geometry of the surface.
1903 *
1904 * The X and Y coordinates returned are relative to the parent surface
1905 * of @surface, which for toplevels usually means relative to the
1906 * surface decorations (titlebar, etc.) rather than relative to the
1907 * root window (screen-size background window).
1908 *
1909 * On the X11 platform, the geometry is obtained from the X server, so
1910 * reflects the latest position of @surface; this may be out-of-sync with
1911 * the position of @surface delivered in the most-recently-processed
1912 * `GdkEventConfigure`. [[email protected]_position] in contrast gets
1913 * the position from the most recent configure event.
1914 *
1915 * Any of the return location arguments to this function may be %NULL,
1916 * if you aren’t interested in getting the value of that field.
1917 *
1918 * Note: If @surface is not a toplevel, it is much better to call
1919 * [[email protected]_position], [[email protected]_width] and
1920 * [[email protected]_height] instead, because it avoids the roundtrip
1921 * to the X server and because these functions support the full 32-bit
1922 * coordinate space, whereas gdk_surface_get_geometry() is restricted to
1923 * the 16-bit coordinates of X11.
1924 */
1925 void
1926 gdk_surface_get_geometry (GdkSurface *surface,
1927 int *x,
1928 int *y,
1929 int *width,
1930 int *height)
1931 {
1932 g_return_if_fail (GDK_IS_SURFACE (surface));
1933
1934 if (GDK_SURFACE_DESTROYED (surface))
1935 return;
1936
1937 GDK_SURFACE_GET_CLASS (surface)->get_geometry (surface, x, y, width, height);
1938 }
1939
1940 /**
1941 * gdk_surface_get_width:
1942 * @surface: a `GdkSurface`
1943 *
1944 * Returns the width of the given @surface.
1945 *
1946 * Surface size is reported in ”application pixels”, not
1947 * ”device pixels” (see [[email protected]_scale_factor]).
1948 *
1949 * Returns: The width of @surface
1950 */
1951 int
1952 gdk_surface_get_width (GdkSurface *surface)
1953 {
1954 g_return_val_if_fail (GDK_IS_SURFACE (surface), 0);
1955
1956 return surface->width;
1957 }
1958
1959 /**
1960 * gdk_surface_get_height:
1961 * @surface: a `GdkSurface`
1962 *
1963 * Returns the height of the given @surface.
1964 *
1965 * Surface size is reported in ”application pixels”, not
1966 * ”device pixels” (see [[email protected]_scale_factor]).
1967 *
1968 * Returns: The height of @surface
1969 */
1970 int
1971 gdk_surface_get_height (GdkSurface *surface)
1972 {
1973 g_return_val_if_fail (GDK_IS_SURFACE (surface), 0);
1974
1975 return surface->height;
1976 }
1977
1978 /*
1979 * gdk_surface_get_origin:
1980 * @surface: a `GdkSurface`
1981 * @x: (out): return location for X coordinate
1982 * @y: (out): return location for Y coordinate
1983 *
1984 * Obtains the position of a surface in root window coordinates.
1985 *
1986 * (Compare with gdk_surface_get_position() and
1987 * gdk_surface_get_geometry() which return the position
1988 * of a surface relative to its parent surface.)
1989 */
1990 void
1991 gdk_surface_get_origin (GdkSurface *surface,
1992 int *x,
1993 int *y)
1994 {
1995 g_return_if_fail (GDK_IS_SURFACE (surface));
1996
1997 gdk_surface_get_root_coords (surface, 0, 0, x, y);
1998 }
1999
2000 /*
2001 * gdk_surface_get_root_coords:
2002 * @surface: a `GdkSurface`
2003 * @x: X coordinate in surface
2004 * @y: Y coordinate in surface
2005 * @root_x: (out): return location for X coordinate
2006 * @root_y: (out): return location for Y coordinate
2007 *
2008 * Obtains the position of a surface position in root
2009 * window coordinates. This is similar to
2010 * gdk_surface_get_origin() but allows you to pass
2011 * in any position in the surface, not just the origin.
2012 */
2013 void
2014 gdk_surface_get_root_coords (GdkSurface *surface,
2015 int x,
2016 int y,
2017 int *root_x,
2018 int *root_y)
2019 {
2020 g_return_if_fail (GDK_IS_SURFACE (surface));
2021
2022 if (GDK_SURFACE_DESTROYED (surface))
2023 {
2024 *root_x = 0;
2025 *root_y = 0;
2026 return;
2027 }
2028
2029 GDK_SURFACE_GET_CLASS (surface)->get_root_coords (surface, x, y, root_x, root_y);
2030 }
2031
2032 /**
2033 * gdk_surface_set_input_region:
2034 * @surface: a `GdkSurface`
2035 * @region: (nullable): region of surface to be reactive,
2036 * or %NULL to make the entire surface reactive
2037 *
2038 * Apply the region to the surface for the purpose of event
2039 * handling.
2040 *
2041 * Mouse events which happen while the pointer position corresponds
2042 * to an unset bit in the mask will be passed on the surface below
2043 * @surface.
2044 *
2045 * An input region is typically used with RGBA surfaces. The alpha
2046 * channel of the surface defines which pixels are invisible and
2047 * allows for nicely antialiased borders, and the input region
2048 * controls where the surface is “clickable”.
2049 *
2050 * Use [[email protected]_input_shapes] to find out if
2051 * a particular backend supports input regions.
2052 */
2053 void
2054 gdk_surface_set_input_region (GdkSurface *surface,
2055 cairo_region_t *region)
2056 {
2057 g_return_if_fail (GDK_IS_SURFACE (surface));
2058
2059 if (GDK_SURFACE_DESTROYED (surface))
2060 return;
2061
2062 if (cairo_region_equal (surface->input_region, region))
2063 return;
2064
2065 if (surface->input_region)
2066 cairo_region_destroy (surface->input_region);
2067
2068 if (region)
2069 surface->input_region = cairo_region_copy (region);
2070 else
2071 surface->input_region = NULL;
2072
2073 GDK_SURFACE_GET_CLASS (surface)->set_input_region (surface, surface->input_region);
2074 }
2075
2076 static void
2077 update_cursor (GdkDisplay *display,
2078 GdkDevice *device)
2079 {
2080 GdkSurface *cursor_surface;
2081 GdkSurface *pointer_surface;
2082 GdkPointerSurfaceInfo *pointer_info;
2083 GdkDeviceGrabInfo *grab;
2084 GdkCursor *cursor;
2085
2086 g_assert (display);
2087 g_assert (device);
2088
2089 pointer_info = _gdk_display_get_pointer_info (display, device);
2090 pointer_surface = pointer_info->surface_under_pointer;
2091
2092 /* We ignore the serials here and just pick the last grab
2093 we've sent, as that would shortly be used anyway. */
2094 grab = _gdk_display_get_last_device_grab (display, device);
2095 if (grab != NULL)
2096 {
2097 /* use the cursor from the grab surface */
2098 cursor_surface = grab->surface;
2099 }
2100 else
2101 {
2102 /* otherwise use the cursor from the pointer surface */
2103 cursor_surface = pointer_surface;
2104 }
2105
2106 cursor = g_hash_table_lookup (cursor_surface->device_cursor, device);
2107
2108 if (!cursor)
2109 cursor = cursor_surface->cursor;
2110
2111 GDK_DEVICE_GET_CLASS (device)->set_surface_cursor (device, pointer_surface, cursor);
2112 }
2113
2114 /**
2115 * gdk_surface_beep:
2116 * @surface: a toplevel `GdkSurface`
2117 *
2118 * Emits a short beep associated to @surface.
2119 *
2120 * If the display of @surface does not support per-surface beeps,
2121 * emits a short beep on the display just as [[email protected]].
2122 */
2123 void
2124 gdk_surface_beep (GdkSurface *surface)
2125 {
2126 g_return_if_fail (GDK_IS_SURFACE (surface));
2127
2128 if (GDK_SURFACE_DESTROYED (surface))
2129 return;
2130
2131 if (GDK_SURFACE_GET_CLASS (surface)->beep (surface))
2132 return;
2133
2134 gdk_display_beep (surface->display);
2135 }
2136
2137 void
2138 _gdk_display_set_surface_under_pointer (GdkDisplay *display,
2139 GdkDevice *device,
2140 GdkSurface *surface)
2141 {
2142 GdkPointerSurfaceInfo *device_info;
2143
2144 device_info = _gdk_display_get_pointer_info (display, device);
2145
2146 if (device_info->surface_under_pointer)
2147 g_object_unref (device_info->surface_under_pointer);
2148 device_info->surface_under_pointer = surface;
2149
2150 if (surface)
2151 {
2152 g_object_ref (surface);
2153 update_cursor (display, device);
2154 }
2155 }
2156
2157 #define GDK_ANY_BUTTON_MASK (GDK_BUTTON1_MASK | \
2158 GDK_BUTTON2_MASK | \
2159 GDK_BUTTON3_MASK | \
2160 GDK_BUTTON4_MASK | \
2161 GDK_BUTTON5_MASK)
2162
2163 void
2164 _gdk_windowing_got_event (GdkDisplay *display,
2165 GList *event_link,
2166 GdkEvent *event,
2167 gulong serial)
2168 {
2169 GdkSurface *event_surface = NULL;
2170 gboolean unlink_event = FALSE;
2171 GdkDeviceGrabInfo *button_release_grab;
2172 GdkPointerSurfaceInfo *pointer_info = NULL;
2173 GdkDevice *device;
2174 GdkEventType type;
2175 guint32 timestamp;
2176
2177 _gdk_display_update_last_event (display, event);
2178
2179 device = gdk_event_get_device (event);
2180 timestamp = gdk_event_get_time (event);
2181
2182 if (device)
2183 {
2184 if (timestamp != GDK_CURRENT_TIME)
2185 gdk_device_set_timestamp (device, timestamp);
2186
2187 if (gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD &&
2188 gdk_device_get_source (device) != GDK_SOURCE_TABLET_PAD)
2189 {
2190 pointer_info = _gdk_display_get_pointer_info (display, device);
2191 pointer_info->last_physical_device = device;
2192 }
2193
2194 _gdk_display_device_grab_update (display, device, serial);
2195 }
2196
2197 event_surface = gdk_event_get_surface (event);
2198 if (!event_surface)
2199 goto out;
2200
2201 type = gdk_event_get_event_type (event);
2202 if (type == GDK_ENTER_NOTIFY)
2203 _gdk_display_set_surface_under_pointer (display, device, event_surface);
2204 else if (type == GDK_LEAVE_NOTIFY)
2205 _gdk_display_set_surface_under_pointer (display, device, NULL);
2206
2207 if (type == GDK_BUTTON_PRESS)
2208 {
2209 GdkSurface *grab_surface;
2210 gboolean owner_events;
2211
2212 if (!gdk_device_grab_info (display, device, &grab_surface, &owner_events))
2213 {
2214 _gdk_display_add_device_grab (display,
2215 device,
2216 event_surface,
2217 FALSE,
2218 GDK_ALL_EVENTS_MASK,
2219 serial,
2220 gdk_event_get_time (event),
2221 TRUE);
2222 _gdk_display_device_grab_update (display, device, serial);
2223 }
2224 }
2225 else if (type == GDK_BUTTON_RELEASE ||
2226 type == GDK_TOUCH_CANCEL ||
2227 type == GDK_TOUCH_END)
2228 {
2229 if (type == GDK_BUTTON_RELEASE ||
2230 gdk_event_get_pointer_emulated (event))
2231 {
2232 button_release_grab =
2233 _gdk_display_has_device_grab (display, device, serial);
2234
2235 if (button_release_grab &&
2236 button_release_grab->implicit &&
2237 (gdk_event_get_modifier_state (event) & GDK_ANY_BUTTON_MASK & ~(GDK_BUTTON1_MASK << (gdk_button_event_get_button (event) - 1))) == 0)
2238 {
2239 button_release_grab->serial_end = serial;
2240 button_release_grab->implicit_ungrab = FALSE;
2241 _gdk_display_device_grab_update (display, device, serial);
2242 }
2243 }
2244 }
2245
2246 out:
2247 if (unlink_event)
2248 {
2249 _gdk_event_queue_remove_link (display, event_link);
2250 g_list_free_1 (event_link);
2251 gdk_event_unref (event);
2252 }
2253
2254 /* This does two things - first it sees if there are motions at the
2255 * end of the queue that can be compressed. Second, if there is just
2256 * a single motion that won't be dispatched because it is a compression
2257 * candidate it queues up flushing the event queue.
2258 */
2259 _gdk_event_queue_handle_motion_compression (display);
2260 gdk_event_queue_handle_scroll_compression (display);
2261
2262 if (event_surface)
2263 {
2264 GdkFrameClock *clock = gdk_surface_get_frame_clock (event_surface);
2265
2266 if (clock) /* might be NULL if surface was destroyed */
2267 gdk_frame_clock_request_phase (clock, GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS);
2268 }
2269 }
2270
2271 /**
2272 * gdk_surface_create_similar_surface:
2273 * @surface: surface to make new surface similar to
2274 * @content: the content for the new surface
2275 * @width: width of the new surface
2276 * @height: height of the new surface
2277 *
2278 * Create a new Cairo surface that is as compatible as possible with the
2279 * given @surface.
2280 *
2281 * For example the new surface will have the same fallback resolution
2282 * and font options as @surface. Generally, the new surface will also
2283 * use the same backend as @surface, unless that is not possible for
2284 * some reason. The type of the returned surface may be examined with
2285 * cairo_surface_get_type().
2286 *
2287 * Initially the surface contents are all 0 (transparent if contents
2288 * have transparency, black otherwise.)
2289 *
2290 * This function always returns a valid pointer, but it will return a
2291 * pointer to a “nil” surface if @other is already in an error state
2292 * or any other error occurs.
2293 *
2294 * Returns: a pointer to the newly allocated surface. The caller
2295 * owns the surface and should call cairo_surface_destroy() when done
2296 * with it.
2297 *
2298 * Deprecated: 4.12: Create a suitable cairo image surface yourself
2299 */
2300 cairo_surface_t *
2301 gdk_surface_create_similar_surface (GdkSurface *surface,
2302 cairo_content_t content,
2303 int width,
2304 int height)
2305 {
2306 cairo_surface_t *similar_surface;
2307 int scale;
2308
2309 g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
2310
2311 scale = gdk_surface_get_scale_factor (surface);
2312
2313 similar_surface = cairo_image_surface_create (gdk_cairo_format_for_content (content),
2314 width * scale, height * scale);
2315 cairo_surface_set_device_scale (similar_surface, scale, scale);
2316
2317 return similar_surface;
2318 }
2319
2320 /* This function is called when the XWindow is really gone.
2321 */
2322 void
2323 gdk_surface_destroy_notify (GdkSurface *surface)
2324 {
2325 GDK_SURFACE_GET_CLASS (surface)->destroy_notify (surface);
2326 }
2327
2328 /**
2329 * gdk_drag_begin:
2330 * @surface: the source surface for this drag
2331 * @device: the device that controls this drag
2332 * @content: (transfer none): the offered content
2333 * @actions: the actions supported by this drag
2334 * @dx: the x offset to @device's position where the drag nominally started
2335 * @dy: the y offset to @device's position where the drag nominally started
2336 *
2337 * Starts a drag and creates a new drag context for it.
2338 *
2339 * This function is called by the drag source. After this call, you
2340 * probably want to set up the drag icon using the surface returned
2341 * by [[email protected]_drag_surface].
2342 *
2343 * This function returns a reference to the [[email protected]] object,
2344 * but GTK keeps its own reference as well, as long as the DND operation
2345 * is going on.
2346 *
2347 * Note: if @actions include %GDK_ACTION_MOVE, you need to listen for
2348 * the [[email protected]::dnd-finished] signal and delete the data at
2349 * the source if [[email protected]_selected_action] returns
2350 * %GDK_ACTION_MOVE.
2351 *
2352 * Returns: (transfer full) (nullable): a newly created `GdkDrag`
2353 */
2354 GdkDrag *
2355 gdk_drag_begin (GdkSurface *surface,
2356 GdkDevice *device,
2357 GdkContentProvider *content,
2358 GdkDragAction actions,
2359 double dx,
2360 double dy)
2361 {
2362 g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
2363 g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
2364 g_return_val_if_fail (surface->display == gdk_device_get_display (device), NULL);
2365 g_return_val_if_fail (GDK_IS_CONTENT_PROVIDER (content), NULL);
2366
2367 return GDK_SURFACE_GET_CLASS (surface)->drag_begin (surface, device, content, actions, dx, dy);
2368 }
2369
2370 static void
2371 gdk_surface_ensure_motion (GdkSurface *surface)
2372 {
2373 GdkDisplay *display;
2374 GdkSeat *seat;
2375 GdkDevice *device;
2376 GdkEvent *event;
2377 double x, y;
2378 GdkModifierType state;
2379 GdkSurface *grab_surface;
2380
2381 if (!surface->request_motion)
2382 return;
2383
2384 surface->request_motion = FALSE;
2385
2386 display = gdk_surface_get_display (surface);
2387 seat = gdk_display_get_default_seat (display);
2388 if (!seat)
2389 return;
2390
2391 device = gdk_seat_get_pointer (seat);
2392
2393 if (!gdk_surface_get_device_position (surface, device, &x, &y, &state))
2394 return;
2395
2396 if (gdk_device_grab_info (display, device, &grab_surface, NULL))
2397 {
2398 if (grab_surface != surface)
2399 return;
2400 }
2401
2402 event = gdk_motion_event_new (surface,
2403 device,
2404 NULL,
2405 GDK_CURRENT_TIME,
2406 state,
2407 x, y,
2408 NULL);
2409
2410 gdk_surface_handle_event (event);
2411 gdk_event_unref (event);
2412 }
2413
2414 static void
2415 gdk_surface_flush_events (GdkFrameClock *clock,
2416 void *data)
2417 {
2418 GdkSurface *surface = GDK_SURFACE (data);
2419
2420 _gdk_event_queue_flush (surface->display);
2421 gdk_surface_ensure_motion (surface);
2422 _gdk_display_pause_events (surface->display);
2423
2424 gdk_frame_clock_request_phase (clock, GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS);
2425 surface->frame_clock_events_paused = TRUE;
2426 }
2427
2428 static void
2429 gdk_surface_resume_events (GdkFrameClock *clock,
2430 void *data)
2431 {
2432 GdkSurface *surface = GDK_SURFACE (data);
2433
2434 if (surface->frame_clock_events_paused)
2435 {
2436 _gdk_display_unpause_events (surface->display);
2437 surface->frame_clock_events_paused = FALSE;
2438 }
2439 }
2440
2441 void
2442 gdk_surface_set_frame_clock (GdkSurface *surface,
2443 GdkFrameClock *clock)
2444 {
2445 g_return_if_fail (GDK_IS_SURFACE (surface));
2446 g_return_if_fail (clock == NULL || GDK_IS_FRAME_CLOCK (clock));
2447
2448 if (clock == surface->frame_clock)
2449 return;
2450
2451 if (clock)
2452 {
2453 g_object_ref (clock);
2454
2455 g_signal_connect (G_OBJECT (clock),
2456 "flush-events",
2457 G_CALLBACK (gdk_surface_flush_events),
2458 surface);
2459 g_signal_connect (G_OBJECT (clock),
2460 "resume-events",
2461 G_CALLBACK (gdk_surface_resume_events),
2462 surface);
2463 g_signal_connect (G_OBJECT (clock),
2464 "layout",
2465 G_CALLBACK (gdk_surface_layout_on_clock),
2466 surface);
2467 g_signal_connect (G_OBJECT (clock),
2468 "paint",
2469 G_CALLBACK (gdk_surface_paint_on_clock),
2470 surface);
2471
2472 if (surface->update_freeze_count == 0)
2473 _gdk_frame_clock_inhibit_freeze (clock);
2474 }
2475
2476 if (surface->frame_clock)
2477 {
2478 if (surface->frame_clock_events_paused)
2479 gdk_surface_resume_events (surface->frame_clock, G_OBJECT (surface));
2480
2481 g_signal_handlers_disconnect_by_func (G_OBJECT (surface->frame_clock),
2482 G_CALLBACK (gdk_surface_flush_events),
2483 surface);
2484 g_signal_handlers_disconnect_by_func (G_OBJECT (surface->frame_clock),
2485 G_CALLBACK (gdk_surface_resume_events),
2486 surface);
2487 g_signal_handlers_disconnect_by_func (G_OBJECT (surface->frame_clock),
2488 G_CALLBACK (gdk_surface_layout_on_clock),
2489 surface);
2490 g_signal_handlers_disconnect_by_func (G_OBJECT (surface->frame_clock),
2491 G_CALLBACK (gdk_surface_paint_on_clock),
2492 surface);
2493
2494 if (surface->update_freeze_count == 0)
2495 _gdk_frame_clock_uninhibit_freeze (surface->frame_clock);
2496
2497 g_object_unref (surface->frame_clock);
2498 }
2499
2500 surface->frame_clock = clock;
2501 }
2502
2503 /**
2504 * gdk_surface_get_frame_clock:
2505 * @surface: surface to get frame clock for
2506 *
2507 * Gets the frame clock for the surface.
2508 *
2509 * The frame clock for a surface never changes unless the surface is
2510 * reparented to a new toplevel surface.
2511 *
2512 * Returns: (transfer none): the frame clock
2513 */
2514 GdkFrameClock *
2515 gdk_surface_get_frame_clock (GdkSurface *surface)
2516 {
2517 g_return_val_if_fail (GDK_IS_SURFACE (surface), NULL);
2518
2519 return surface->frame_clock;
2520 }
2521
2522 /**
2523 * gdk_surface_get_scale_factor:
2524 * @surface: surface to get scale factor for
2525 *
2526 * Returns the internal scale factor that maps from surface coordinates
2527 * to the actual device pixels.
2528 *
2529 * On traditional systems this is 1, but on very high density outputs
2530 * this can be a higher value (often 2). A higher value means that drawing
2531 * is automatically scaled up to a higher resolution, so any code doing
2532 * drawing will automatically look nicer. However, if you are supplying
2533 * pixel-based data the scale value can be used to determine whether to
2534 * use a pixel resource with higher resolution data.
2535 *
2536 * The scale factor may change during the lifetime of the surface.
2537 *
2538 * Returns: the scale factor
2539 */
2540 int
2541 gdk_surface_get_scale_factor (GdkSurface *surface)
2542 {
2543 g_return_val_if_fail (GDK_IS_SURFACE (surface), 1);
2544
2545 return (int) ceil (gdk_surface_get_scale (surface));
2546 }
2547
2548 /**
2549 * gdk_surface_get_scale:
2550 * @surface: surface to get scale for
2551 *
2552 * Returns the internal scale that maps from surface coordinates
2553 * to the actual device pixels.
2554 *
2555 * When the scale is bigger than 1, the windowing system prefers to get
2556 * buffers with a resolution that is bigger than the surface size (e.g.
2557 * to show the surface on a high-resolution display, or in a magnifier).
2558 *
2559 * Compare with [[email protected]_scale_factor], which returns the
2560 * next larger integer.
2561 *
2562 * The scale may change during the lifetime of the surface.
2563 *
2564 * Returns: the scale
2565 *
2566 * Since: 4.12
2567 */
2568 double
2569 gdk_surface_get_scale (GdkSurface *surface)
2570 {
2571 g_return_val_if_fail (GDK_IS_SURFACE (surface), 1.);
2572
2573 if (GDK_SURFACE_DESTROYED (surface))
2574 return 1.;
2575
2576 return GDK_SURFACE_GET_CLASS (surface)->get_scale (surface);
2577 }
2578
2579 static void
2580 gdk_surface_update_opaque_region (GdkSurface *self)
2581 {
2582 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (self);
2583 cairo_region_t *region;
2584
2585 if (priv->opaque_region == NULL)
2586 {
2587 if (priv->opaque_rect.width <= 0)
2588 region = NULL;
2589 else
2590 region = cairo_region_create_rectangle (&priv->opaque_rect);
2591 }
2592 else
2593 {
2594 if (priv->opaque_rect.width <= 0)
2595 region = cairo_region_reference (priv->opaque_region);
2596 else
2597 {
2598 region = cairo_region_copy (priv->opaque_region);
2599 cairo_region_union_rectangle (region, &priv->opaque_rect);
2600 }
2601 }
2602
2603 GDK_SURFACE_GET_CLASS (self)->set_opaque_region (self, region);
2604
2605 g_clear_pointer (&region, cairo_region_destroy);
2606 }
2607
2608 /**
2609 * gdk_surface_set_opaque_region:
2610 * @surface: a top-level `GdkSurface`
2611 * @region: (nullable): a region, or %NULL to make the entire
2612 * surface opaque
2613 *
2614 * Marks a region of the `GdkSurface` as opaque.
2615 *
2616 * For optimisation purposes, compositing window managers may
2617 * like to not draw obscured regions of surfaces, or turn off blending
2618 * during for these regions. With RGB windows with no transparency,
2619 * this is just the shape of the window, but with ARGB32 windows, the
2620 * compositor does not know what regions of the window are transparent
2621 * or not.
2622 *
2623 * This function only works for toplevel surfaces.
2624 *
2625 * GTK will update this property automatically if the @surface background
2626 * is opaque, as we know where the opaque regions are. If your surface
2627 * background is not opaque, please update this property in your
2628 * [GtkWidgetClass.css_changed](../gtk4/vfunc.Widget.css_changed.html) handler.
2629 *
2630 * Deprecated: 4.16: GDK can figure out the opaque parts of a window itself
2631 * by inspecting the contents that are drawn.
2632 */
2633 void
2634 gdk_surface_set_opaque_region (GdkSurface *surface,
2635 cairo_region_t *region)
2636 {
2637 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (surface);
2638
2639 g_return_if_fail (GDK_IS_SURFACE (surface));
2640 g_return_if_fail (!GDK_SURFACE_DESTROYED (surface));
2641
2642 if (cairo_region_equal (priv->opaque_region, region))
2643 return;
2644
2645 g_clear_pointer (&priv->opaque_region, cairo_region_destroy);
2646
2647 if (region != NULL)
2648 priv->opaque_region = cairo_region_reference (region);
2649
2650 gdk_surface_update_opaque_region (surface);
2651 }
2652
2653 /* Sets the opaque rect from the rendernode via end_frame() */
2654 void
2655 gdk_surface_set_opaque_rect (GdkSurface *self,
2656 const graphene_rect_t *rect)
2657 {
2658 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (self);
2659 cairo_rectangle_int_t opaque;
2660
2661 if (rect == NULL || !gsk_rect_to_cairo_shrink (rect, &opaque))
2662 opaque = (cairo_rectangle_int_t) { 0, 0, 0, 0 };
2663
2664 if (gdk_rectangle_equal (&priv->opaque_rect, &opaque))
2665 return;
2666
2667 priv->opaque_rect = opaque;
2668
2669 gdk_surface_update_opaque_region (self);
2670 }
2671
2672 /*
2673 * gdk_surface_is_opaque:
2674 * @self: a surface
2675 *
2676 * Checks if the whole surface is known to be opaque.
2677 * This allows using an RGBx buffer instead of RGBA.
2678 *
2679 * This function works for the currently rendered frame inside
2680 * begin_frame() implementations.
2681 *
2682 * Returns: %TRUE if the whole surface is provably opaque
2683 **/
2684 gboolean
2685 gdk_surface_is_opaque (GdkSurface *self)
2686 {
2687 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (self);
2688 cairo_rectangle_int_t whole = { 0, 0, self->width, self->height };
2689
2690 if (gdk_rectangle_contains (&priv->opaque_rect, &whole))
2691 return TRUE;
2692
2693 if (priv->opaque_region &&
2694 cairo_region_contains_rectangle (priv->opaque_region, &whole) == CAIRO_REGION_OVERLAP_IN)
2695 return TRUE;
2696
2697 return FALSE;
2698 }
2699
2700 void
2701 gdk_surface_set_state (GdkSurface *surface,
2702 GdkToplevelState new_state)
2703 {
2704 gboolean was_sticky, sticky;
2705 g_return_if_fail (GDK_IS_SURFACE (surface));
2706
2707 if (new_state == surface->state)
2708 return; /* No actual work to do, nothing changed. */
2709
2710 /* Actually update the field in GdkSurface, this is sort of an odd
2711 * place to do it, but seems like the safest since it ensures we expose no
2712 * inconsistent state to the user.
2713 */
2714
2715 was_sticky = GDK_SURFACE_IS_STICKY (surface);
2716
2717 surface->state = new_state;
2718
2719 sticky = GDK_SURFACE_IS_STICKY (surface);
2720
2721 if (GDK_IS_TOPLEVEL (surface))
2722 g_object_notify (G_OBJECT (surface), "state");
2723
2724 if (was_sticky != sticky)
2725 g_object_notify (G_OBJECT (surface), "sticky");
2726 }
2727
2728 void
2729 gdk_synthesize_surface_state (GdkSurface *surface,
2730 GdkToplevelState unset_flags,
2731 GdkToplevelState set_flags)
2732 {
2733 gdk_surface_set_state (surface, (surface->state | set_flags) & ~unset_flags);
2734 }
2735
2736 void
2737 gdk_surface_queue_state_change (GdkSurface *surface,
2738 GdkToplevelState unset_flags,
2739 GdkToplevelState set_flags)
2740 {
2741 GdkFrameClock *frame_clock;
2742
2743 surface->pending_unset_flags |= unset_flags;
2744 surface->pending_set_flags &= ~unset_flags;
2745
2746 surface->pending_set_flags |= set_flags;
2747 surface->pending_unset_flags &= ~set_flags;
2748
2749 frame_clock = gdk_surface_get_frame_clock (surface);
2750 if (!frame_clock || gdk_frame_clock_is_frozen (frame_clock))
2751 gdk_surface_apply_state_change (surface);
2752 }
2753
2754 void
2755 gdk_surface_apply_state_change (GdkSurface *surface)
2756 {
2757 if (!surface->pending_unset_flags && !surface->pending_set_flags)
2758 return;
2759
2760 gdk_synthesize_surface_state (surface,
2761 surface->pending_unset_flags,
2762 surface->pending_set_flags);
2763 surface->pending_unset_flags = 0;
2764 surface->pending_set_flags = 0;
2765 }
2766
2767 static gboolean
2768 set_is_mapped_idle (gpointer user_data)
2769 {
2770 GdkSurface *surface = GDK_SURFACE (user_data);
2771
2772 surface->set_is_mapped_source_id = 0;
2773
2774 g_return_val_if_fail (surface->pending_is_mapped != surface->is_mapped,
2775 G_SOURCE_REMOVE);
2776
2777 surface->is_mapped = surface->pending_is_mapped;
2778 if (surface->is_mapped)
2779 gdk_surface_invalidate_rect (surface, NULL);
2780
2781 g_object_notify (G_OBJECT (surface), "mapped");
2782
2783 return G_SOURCE_REMOVE;
2784 }
2785
2786 void
2787 gdk_surface_set_is_mapped (GdkSurface *surface,
2788 gboolean is_mapped)
2789 {
2790 gboolean was_mapped;
2791
2792 if (surface->pending_is_mapped != surface->is_mapped)
2793 g_clear_handle_id (&surface->set_is_mapped_source_id, g_source_remove);
2794
2795 surface->pending_is_mapped = is_mapped;
2796
2797 was_mapped = surface->is_mapped;
2798 surface->is_mapped = is_mapped;
2799 if (surface->is_mapped)
2800 gdk_surface_invalidate_rect (surface, NULL);
2801
2802 if (was_mapped != is_mapped)
2803 g_object_notify (G_OBJECT (surface), "mapped");
2804 }
2805
2806 static void
2807 gdk_surface_queue_set_is_mapped (GdkSurface *surface,
2808 gboolean is_mapped)
2809 {
2810 if (surface->pending_is_mapped == is_mapped)
2811 return;
2812
2813 surface->pending_is_mapped = is_mapped;
2814
2815 if (surface->is_mapped == surface->pending_is_mapped)
2816 {
2817 g_clear_handle_id (&surface->set_is_mapped_source_id, g_source_remove);
2818 }
2819 else
2820 {
2821 g_return_if_fail (!surface->set_is_mapped_source_id);
2822
2823 surface->set_is_mapped_source_id =
2824 g_idle_add_full (G_PRIORITY_HIGH - 10, set_is_mapped_idle, surface, NULL);
2825 gdk_source_set_static_name_by_id (surface->set_is_mapped_source_id, "[gtk] set_is_mapped_idle");
2826 }
2827 }
2828
2829 static gboolean
2830 check_autohide (GdkEvent *event)
2831 {
2832 GdkDisplay *display;
2833 GdkDevice *device;
2834 GdkSurface *grab_surface, *event_surface;
2835 GdkEventType evtype = gdk_event_get_event_type (event);
2836
2837 switch ((guint) evtype)
2838 {
2839 case GDK_BUTTON_PRESS:
2840 #if 0
2841 // FIXME: we need to ignore the release that is paired
2842 // with the press starting the grab - due to implicit
2843 // grabs, it will be delivered to the same place as the
2844 // press, and will cause the auto dismissal to be triggered.
2845 case GDK_BUTTON_RELEASE:
2846 case GDK_TOUCH_END:
2847 case GDK_TOUCH_CANCEL:
2848 #endif
2849 case GDK_TOUCH_BEGIN:
2850 case GDK_TOUCHPAD_SWIPE:
2851 case GDK_TOUCHPAD_PINCH:
2852 display = gdk_event_get_display (event);
2853 device = gdk_event_get_device (event);
2854 if (gdk_device_grab_info (display, device, &grab_surface, NULL))
2855 {
2856 event_surface = gdk_event_get_surface (event);
2857 if (event_surface->autohide &&
2858 evtype != GDK_TOUCH_BEGIN &&
2859 !event_surface->has_pointer)
2860 event_surface = NULL;
2861
2862 if (grab_surface->autohide &&
2863 (!event_surface ||
2864 (grab_surface != event_surface &&
2865 grab_surface != event_surface->parent)))
2866 {
2867 GdkSurface *surface = grab_surface;
2868
2869 do
2870 {
2871 gdk_surface_hide (surface);
2872 surface = surface->parent;
2873 }
2874 while (surface->autohide && surface != event_surface);
2875
2876 return TRUE;
2877 }
2878 }
2879 break;
2880 case GDK_ENTER_NOTIFY:
2881 case GDK_LEAVE_NOTIFY:
2882 event_surface = gdk_event_get_surface (event);
2883 if (event_surface->autohide &&
2884 gdk_crossing_event_get_mode (event) == GDK_CROSSING_NORMAL)
2885 event_surface->has_pointer = evtype == GDK_ENTER_NOTIFY;
2886 break;
2887 default:;
2888 }
2889
2890 return FALSE;
2891 }
2892
2893 static inline void
2894 add_event_mark (GdkEvent *event,
2895 gint64 time,
2896 gint64 end_time)
2897 {
2898 #ifdef HAVE_SYSPROF
2899 char *message = NULL;
2900 const char *kind;
2901 GEnumClass *class;
2902 GEnumValue *value;
2903 GdkEventType event_type;
2904
2905 event_type = gdk_event_get_event_type (event);
2906 class = g_type_class_ref (GDK_TYPE_EVENT_TYPE);
2907 value = g_enum_get_value (class, event_type);
2908 g_type_class_unref (class);
2909 kind = value ? value->value_nick : "Event";
2910
2911 switch ((int) event_type)
2912 {
2913 case GDK_MOTION_NOTIFY:
2914 {
2915 double x, y;
2916 gdk_event_get_position (event, &x, &y);
2917 message = g_strdup_printf ("%s {x=%lf, y=%lf, state=0x%x}",
2918 kind,
2919 x, y,
2920 gdk_event_get_modifier_state (event));
2921 break;
2922 }
2923
2924 case GDK_BUTTON_PRESS:
2925 case GDK_BUTTON_RELEASE:
2926 {
2927 double x, y;
2928 gdk_event_get_position (event, &x, &y);
2929 message = g_strdup_printf ("%s {button=%u, x=%lf, y=%lf, state=0x%x}",
2930 kind,
2931 gdk_button_event_get_button (event),
2932 x, y,
2933 gdk_event_get_modifier_state (event));
2934 break;
2935 }
2936
2937 case GDK_KEY_PRESS:
2938 case GDK_KEY_RELEASE:
2939 {
2940 message = g_strdup_printf ("%s {keyval=%u, state=0x%x, keycode=%u layout=%u level=%u is_modifier=%u}",
2941 kind,
2942 gdk_key_event_get_keyval (event),
2943 gdk_event_get_modifier_state (event),
2944 gdk_key_event_get_keycode (event),
2945 gdk_key_event_get_layout (event),
2946 gdk_key_event_get_level (event),
2947 gdk_key_event_is_modifier (event));
2948 break;
2949 }
2950
2951 case GDK_ENTER_NOTIFY:
2952 case GDK_LEAVE_NOTIFY:
2953 case GDK_TOUCHPAD_SWIPE:
2954 case GDK_TOUCHPAD_PINCH:
2955 case GDK_SCROLL:
2956 case GDK_DRAG_ENTER:
2957 case GDK_DRAG_LEAVE:
2958 case GDK_DRAG_MOTION:
2959 case GDK_DROP_START:
2960 case GDK_TOUCH_BEGIN:
2961 case GDK_TOUCH_UPDATE:
2962 case GDK_TOUCH_END:
2963 case GDK_TOUCH_CANCEL:
2964 case GDK_PAD_BUTTON_PRESS:
2965 case GDK_PAD_BUTTON_RELEASE:
2966 case GDK_PAD_RING:
2967 case GDK_PAD_STRIP:
2968 case GDK_PAD_DIAL:
2969 case GDK_PAD_GROUP_MODE:
2970 case GDK_GRAB_BROKEN:
2971 case GDK_DELETE:
2972 case GDK_FOCUS_CHANGE:
2973 case GDK_PROXIMITY_IN:
2974 case GDK_PROXIMITY_OUT:
2975 case GDK_EVENT_LAST:
2976 default:
2977 break;
2978 }
2979
2980 gdk_profiler_add_mark (time, end_time - time, "Event", message ? message : kind);
2981
2982 g_free (message);
2983 #endif
2984 }
2985
2986 gboolean
2987 gdk_surface_handle_event (GdkEvent *event)
2988 {
2989 GdkSurface *surface = gdk_event_get_surface (event);
2990 gint64 begin_time = GDK_PROFILER_CURRENT_TIME;
2991 gboolean handled = FALSE;
2992
2993 if (!GDK_SURFACE_IS_MAPPED (surface))
2994 return FALSE;
2995
2996 if (check_autohide (event))
2997 return TRUE;
2998
2999
3000 if (gdk_event_get_event_type (event) == GDK_MOTION_NOTIFY)
3001 surface->request_motion = FALSE;
3002
3003 g_signal_emit (surface, signals[EVENT], 0, event, &handled);
3004
3005 if (GDK_PROFILER_IS_RUNNING)
3006 add_event_mark (event, begin_time, GDK_PROFILER_CURRENT_TIME);
3007
3008 return handled;
3009 }
3010
3011 /*
3012 * gdk_surface_request_motion:
3013 * @surface: a `GdkSurface`
3014 *
3015 * Request that the next frame cycle should deliver a motion
3016 * event for @surface.
3017 *
3018 * The motion event will be delivered if the pointer is over the
3019 * surface, regardless whether the pointer has moved or not. This
3020 * is used by GTK after moving widgets around.
3021 */
3022 void
3023 gdk_surface_request_motion (GdkSurface *surface)
3024 {
3025 surface->request_motion = TRUE;
3026 }
3027
3028 /**
3029 * gdk_surface_translate_coordinates:
3030 * @from: the origin surface
3031 * @to: the target surface
3032 * @x: (inout): coordinates to translate
3033 * @y: (inout): coordinates to translate
3034 *
3035 * Translates coordinates between two surfaces.
3036 *
3037 * Note that this only works if @to and @from are popups or
3038 * transient-for to the same toplevel (directly or indirectly).
3039 *
3040 * Returns: %TRUE if the coordinates were successfully translated
3041 */
3042 gboolean
3043 gdk_surface_translate_coordinates (GdkSurface *from,
3044 GdkSurface *to,
3045 double *x,
3046 double *y)
3047 {
3048 double in_x, in_y, out_x, out_y;
3049 int x1, y1, x2, y2;
3050 GdkSurface *f, *t;
3051
3052 g_return_val_if_fail (GDK_IS_SURFACE (from), FALSE);
3053 g_return_val_if_fail (GDK_IS_SURFACE (to), FALSE);
3054 g_return_val_if_fail (x != NULL, FALSE);
3055 g_return_val_if_fail (y != NULL, FALSE);
3056
3057 in_x = *x;
3058 in_y = *y;
3059
3060 x1 = 0;
3061 y1 = 0;
3062 f = from;
3063 while (f->parent)
3064 {
3065 x1 += f->x;
3066 y1 += f->y;
3067 f = f->parent;
3068 }
3069
3070 x2 = 0;
3071 y2 = 0;
3072 t = to;
3073 while (t->parent)
3074 {
3075 x2 += t->x;
3076 y2 += t->y;
3077 t = t->parent;
3078 }
3079
3080 if (f != t)
3081 return FALSE;
3082
3083 out_x = in_x + (x1 - x2);
3084 out_y = in_y + (y1 - y2);
3085
3086 *x = out_x;
3087 *y = out_y;
3088
3089 return TRUE;
3090 }
3091
3092 GdkSeat *
3093 gdk_surface_get_seat_from_event (GdkSurface *surface,
3094 GdkEvent *event)
3095 {
3096 if (event)
3097 {
3098 GdkSeat *seat = NULL;
3099
3100 seat = gdk_event_get_seat (event);
3101
3102 if (seat)
3103 return seat;
3104 }
3105 return gdk_display_get_default_seat (surface->display);
3106 }
3107
3108 void
3109 gdk_surface_enter_monitor (GdkSurface *surface,
3110 GdkMonitor *monitor)
3111 {
3112 g_signal_emit (surface, signals[ENTER_MONITOR], 0, monitor);
3113 }
3114
3115 void
3116 gdk_surface_leave_monitor (GdkSurface *surface,
3117 GdkMonitor *monitor)
3118 {
3119 g_signal_emit (surface, signals[LEAVE_MONITOR], 0, monitor);
3120 }
3121
3122 GdkSubsurface *
3123 gdk_surface_create_subsurface (GdkSurface *surface)
3124 {
3125 GdkSubsurface *subsurface;
3126
3127 subsurface = GDK_SURFACE_GET_CLASS (surface)->create_subsurface (surface);
3128
3129 if (subsurface)
3130 {
3131 subsurface->parent = g_object_ref (surface);
3132 g_ptr_array_add (surface->subsurfaces, subsurface);
3133 }
3134
3135 return subsurface;
3136 }
3137
3138 gsize
3139 gdk_surface_get_n_subsurfaces (GdkSurface *surface)
3140 {
3141 return surface->subsurfaces->len;
3142 }
3143
3144 GdkSubsurface *
3145 gdk_surface_get_subsurface (GdkSurface *surface,
3146 gsize idx)
3147 {
3148 return g_ptr_array_index (surface->subsurfaces, idx);
3149 }
3150
3151 GdkColorState *
3152 gdk_surface_get_color_state (GdkSurface *surface)
3153 {
3154 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (surface);
3155
3156 return priv->color_state;
3157 }
3158
3159 void
3160 gdk_surface_set_color_state (GdkSurface *surface,
3161 GdkColorState *color_state)
3162 {
3163 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (surface);
3164
3165 if (gdk_color_state_equal (priv->color_state, color_state))
3166 return;
3167
3168 gdk_color_state_unref (priv->color_state);
3169 priv->color_state = gdk_color_state_ref (color_state);
3170
3171 gdk_surface_invalidate_rect (surface, NULL);
3172 }
3173
3174 /*<private>
3175 * gdk_surface_set_attached_context:
3176 * @self: the surface
3177 * @context: (nullable): the context to attach
3178 *
3179 * This function is an implementation detail for GdkDrawContext.
3180 *
3181 * Use draw context functions like gdk_draw_context_attach() or
3182 * gdk_draw_context_detach().
3183 **/
3184 void
3185 gdk_surface_set_attached_context (GdkSurface *self,
3186 GdkDrawContext *context)
3187 {
3188 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (self);
3189
3190 priv->attached_context = context;
3191 }
3192
3193 GdkDrawContext *
3194 gdk_surface_get_attached_context (GdkSurface *self)
3195 {
3196 GdkSurfacePrivate *priv = gdk_surface_get_instance_private (self);
3197
3198 return priv->attached_context;
3199 }