Debian Codegraph @

gdraw/ggdkdraw.c

raw symbols SCIP
1 /* Copyright (C) 2016 by Jeremy Tan */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
21 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #include <fontforge-config.h>
26
27 /**
28 * \file ggdkdraw.c
29 * \brief GDK drawing backend.
30 */
31
32 #include "ggdkdrawP.h"
33
34
35 #include "gkeysym.h"
36 #include "gresource.h"
37 #include "ustring.h"
38 #include "utype.h"
39
40 #include <assert.h>
41 #include <math.h>
42
43 // HACK HACK HACK
44 #ifdef GDK_WINDOWING_WIN32
45 # define GDK_COMPILATION
46 # include <gdk/gdkwin32.h>
47 # undef GDK_COMPILATION
48 #endif
49
50 // Forward declarations
51 void gtk_main_do_event (GdkEvent* event);
52
53 static void GGDKDrawCancelTimer(GTimer *timer);
54 static void GGDKDrawDestroyWindow(GWindow w);
55 static void GGDKDrawPostEvent(GEvent *e);
56 static void GGDKDrawProcessPendingEvents(GDisplay *gdisp);
57 static void GGDKDrawSetCursor(GWindow w, GCursor gcursor);
58 static void GGDKDrawSetTransientFor(GWindow transient, GWindow owner);
59 static void GGDKDrawSetWindowBackground(GWindow w, Color gcol);
60
61 // Private member functions (file-level)
62 static void _GGDKDraw_InitiateWindowDestroy(GGDKWindow gw);
63
64 static GGC *_GGDKDraw_NewGGC(void) {
65 GGC *ggc = calloc(1, sizeof(GGC));
66 if (ggc == NULL) {
67 Log(LOGDEBUG, "GGC: Memory allocation failed!");
68 return NULL;
69 }
70
71 ggc->clip.width = ggc->clip.height = 0x7fff;
72 ggc->fg = 0;
73 ggc->bg = 0xffffff;
74 return ggc;
75 }
76
77 static void _GGDKDraw_SetOpaqueRegion(GGDKWindow gw) {
78 cairo_rectangle_int_t r = {
79 gw->pos.x, gw->pos.y, gw->pos.width, gw->pos.height
80 };
81 cairo_region_t *cr = cairo_region_create_rectangle(&r);
82 if (cairo_region_status(cr) == CAIRO_STATUS_SUCCESS) {
83 gdk_window_set_opaque_region(gw->w, cr);
84 cairo_region_destroy(cr);
85 }
86 }
87
88 static void _GGDKDraw_ClearSelData(GGDKDisplay *gdisp, enum selnames sn) {
89 GList_Glib *ptr = gdisp->selinfo[sn].datalist;
90 while (ptr != NULL) {
91 GGDKSelectionData *data = (GGDKSelectionData *)ptr->data;
92 if (data->data) {
93 if (data->freedata) {
94 (data->freedata)(data->data);
95 } else {
96 free(data->data);
97 }
98 }
99 free(data);
100 ptr = g_list_delete_link(ptr, ptr);
101 }
102 gdisp->selinfo[sn].datalist = NULL;
103 gdisp->selinfo[sn].owner = NULL;
104 }
105
106 static bool _GGDKDraw_TransmitSelection(GGDKDisplay *gdisp, GdkEventSelection *e) {
107 // Default location to store data if none specified
108 if (e->property == GDK_NONE) {
109 e->property = e->target;
110 }
111
112 // Check that we own the selection request
113 enum selnames sn;
114 for (sn = 0; sn < sn_max; sn++) {
115 if (e->selection == gdisp->selinfo[sn].sel_atom) {
116 break;
117 }
118 }
119 if (sn == sn_max) {
120 return false;
121 }
122
123 GGDKSelectionInfo *sel = &gdisp->selinfo[sn];
124 GdkWindow *requestor = (GdkWindow *)g_object_ref(e->requestor);
125
126 if (e->target == gdk_atom_intern_static_string("TARGETS")) {
127 guint i = 0, dlen = g_list_length(sel->datalist);
128 GdkAtom *targets = calloc(2 + dlen, sizeof(GdkAtom));
129 GList_Glib *ptr = sel->datalist;
130
131 targets[i++] = gdk_atom_intern_static_string("TIMESTAMP");
132 targets[i++] = gdk_atom_intern_static_string("TARGETS");
133
134 while (ptr != NULL) {
135 targets[i++] = ((GGDKSelectionData *)ptr->data)->type_atom;
136 ptr = ptr->next;
137 }
138
139 #ifdef GDK_WINDOWING_WIN32
140 gdk_win32_selection_clear_targets(gdk_window_get_display(e->window), e->selection);
141 gdk_win32_selection_add_targets(e->window, e->selection, i, targets);
142 #endif
143
144 gdk_property_change(requestor, e->property, gdk_atom_intern_static_string("ATOM"),
145 32, GDK_PROP_MODE_REPLACE, (const guchar *)targets, i);
146 } else if (e->target == gdk_atom_intern_static_string("TIMESTAMP")) {
147 gdk_property_change(requestor, e->property, gdk_atom_intern_static_string("INTEGER"),
148 32, GDK_PROP_MODE_REPLACE, (const guchar *)&sel->timestamp, 1);
149 } else {
150 GList_Glib *ptr = sel->datalist;
151 GGDKSelectionData *data = NULL;
152 while (ptr != NULL) {
153 if (((GGDKSelectionData *)ptr->data)->type_atom == e->target) {
154 data = (GGDKSelectionData *)ptr->data;
155 break;
156 }
157 ptr = ptr->next;
158 }
159 if (data == NULL) { // Unknown selection
160 g_object_unref(requestor);
161 return false;
162 }
163
164 void *tmp = data->data;
165 int len = data->cnt;
166 if (data->gendata) {
167 tmp = data->gendata(data->data, &len);
168 if (tmp == NULL) {
169 g_object_unref(requestor);
170 return false;
171 }
172 }
173
174 gdk_property_change(requestor, e->property, e->target,
175 data->unit_size * 8, GDK_PROP_MODE_REPLACE, (const guchar *)tmp, len);
176 if (data->gendata) {
177 free(tmp);
178 }
179 }
180
181 g_object_unref(requestor);
182 return true;
183 }
184
185 static gboolean _GGDKDraw_OnWindowDestroyed(gpointer data) {
186 GGDKWindow gw = (GGDKWindow)data;
187 Log(LOGDEBUG, "Window: %p", gw);
188 if (gw->is_cleaning_up) {
189 return false;
190 }
191 gw->is_cleaning_up = true; // We're in the process of destroying it.
192
193 if (gw->cc != NULL) {
194 cairo_destroy(gw->cc);
195 gw->cc = NULL;
196 }
197
198 if (gw->resize_timeout != 0) {
199 g_source_remove(gw->resize_timeout);
200 gw->resize_timeout = 0;
201 }
202
203 if (!gw->is_pixmap) {
204 if (gw != gw->display->groot) {
205 if (!gdk_window_is_destroyed(gw->w)) {
206 gdk_window_destroy(gw->w);
207 // Wait for it to die
208 while (!gw->display->is_dying && !gdk_window_is_destroyed(gw->w)) {
209 GGDKDrawProcessPendingEvents((GDisplay *)gw->display);
210 }
211 }
212 }
213
214 // Signal that it has been destroyed - only if we're not cleaning up the display
215 if (!gw->display->is_dying) {
216 struct gevent die = {0};
217 die.w = (GWindow)gw;
218 die.native_window = gw->w;
219 die.type = et_destroy;
220 GGDKDrawPostEvent(&die);
221 }
222
223 // Remove all relevant timers that haven't been cleaned up by the user
224 // Note: We do not free the GTimer struct as the user may then call DestroyTimer themselves...
225 GList_Glib *ent = gw->display->timers;
226 while (ent != NULL) {
227 GList_Glib *next = ent->next;
228 GGDKTimer *timer = (GGDKTimer *)ent->data;
229 if (timer->owner == (GWindow)gw) {
230 //Since we update the timer list ourselves, don't all GGDKDrawCancelTimer.
231 Log(LOGDEBUG, "WARNING: Unstopped timer on window destroy!!! %p -> %p", gw, timer);
232 timer->active = false;
233 timer->stopped = true;
234 g_source_remove(timer->glib_timeout_id);
235 gw->display->timers = g_list_delete_link(gw->display->timers, ent);
236 }
237 ent = next;
238 }
239
240 // If it owns any of the selections, clear it
241 for (int i = 0; i < sn_max; i++) {
242 if (gw->display->selinfo[i].owner == gw) {
243 _GGDKDraw_ClearSelData(gw->display, i);
244 }
245 }
246
247 // Decrement the toplevel window count
248 if (gw->display->groot == gw->parent && !gw->is_dlg) {
249 gw->display->top_window_count--;
250 }
251
252 assert(gw->display->dirty_window != gw);
253
254 Log(LOGDEBUG, "Window destroyed: %p[%p][%s][toplevel:%d][pixmap:%d]",
255 gw, gw->w, gw->window_title, gw->is_toplevel, gw->is_pixmap);
256 free(gw->window_title);
257 if (gw != gw->display->groot) {
258 // Unreference our reference to the window
259 g_object_unref(G_OBJECT(gw->w));
260 }
261 }
262
263 if (gw->pango_layout != NULL) {
264 g_object_unref(gw->pango_layout);
265 }
266
267 if (gw->cs != NULL) {
268 cairo_surface_destroy(gw->cs);
269 }
270
271 g_hash_table_remove(gw->display->windows, gw);
272
273 // Remove our reference on the parent
274 if (gw->parent != NULL && gw->parent != gw->display->groot) {
275 GGDKDRAW_DECREF(gw->parent, _GGDKDraw_InitiateWindowDestroy);
276 }
277
278 free(gw->ggc);
279 free(gw);
280 return false;
281 }
282
283 // FF expects the destroy call to happen asynchronously to
284 // the actual GGDKDrawDestroyWindow call. So we add it to the queue...
285 static void _GGDKDraw_InitiateWindowDestroy(GGDKWindow gw) {
286 if (gw->is_pixmap) {
287 _GGDKDraw_OnWindowDestroyed(gw);
288 } else if (!gw->is_cleaning_up) { // Check for nested call - if we're already being destroyed.
289 // Note: This *MUST* be a 0-length timer so it always gets picked up on the next
290 // call to GDrawProcessPendingEvents. There are assumptions made that the destroy
291 // event will be invoked when that function is called after calling GDrawDestroyWindow.
292 // Ideally fix wherever the GDrawSync/GDrawProcessPendingEvents pattern is used
293 // to have a more concrete check that it's actually gone.
294 g_timeout_add(0, _GGDKDraw_OnWindowDestroyed, gw);
295 }
296 }
297
298 static void _GGDKDraw_OnTimerDestroyed(GGDKTimer *timer) {
299 GGDKDisplay *gdisp = ((GGDKWindow)(timer->owner))->display;
300
301 // We may stop the timer without destroying it -
302 // e.g. if the user doesn't cancel the timer before a window is destroyed.
303 if (!timer->stopped) {
304 g_source_remove(timer->glib_timeout_id);
305 }
306 gdisp->timers = g_list_remove(gdisp->timers, timer);
307 free(timer);
308 }
309
310 static gboolean _GGDKDraw_OnFakedConfigure(gpointer user_data) {
311 GGDKWindow gw = (GGDKWindow)user_data;
312 if (!gw->is_dying) {
313 GdkEventConfigure evt = {0};
314
315 evt.type = GDK_CONFIGURE;
316 evt.window = gw->w;
317 evt.send_event = true;
318 evt.width = gdk_window_get_width(gw->w);
319 evt.height = gdk_window_get_height(gw->w);
320 gdk_window_get_position(gw->w, &evt.x, &evt.y);
321
322 gdk_event_put((GdkEvent *)&evt);
323 }
324 gw->resize_timeout = 0;
325 return false;
326 }
327
328 // In their infinite wisdom, GDK does not send configure events for child windows.
329 static void _GGDKDraw_FakeConfigureEvent(GGDKWindow gw) {
330 if (gw->resize_timeout != 0) {
331 g_source_remove(gw->resize_timeout);
332 gw->resize_timeout = 0;
333 }
334
335 // Always fire the first faked configure event immediately
336 if (!gw->has_had_faked_configure) {
337 _GGDKDraw_OnFakedConfigure(gw);
338 gw->has_had_faked_configure = true;
339 } else {
340 gw->resize_timeout = g_timeout_add(150, _GGDKDraw_OnFakedConfigure, gw);
341 }
342 }
343
344 static void _GGDKDraw_CallEHChecked(GGDKWindow gw, GEvent *event, int (*eh)(GWindow gw, GEvent *)) {
345 if (eh) {
346 // Increment reference counter
347 GGDKDRAW_ADDREF(gw);
348 (eh)((GWindow)gw, event);
349
350 // Cleanup after our user...
351 _GGDKDraw_CleanupAutoPaint(gw->display);
352 // Decrement reference counter
353 GGDKDRAW_DECREF(gw, _GGDKDraw_InitiateWindowDestroy);
354 }
355 }
356
357 static GdkDevice *_GGDKDraw_GetPointer(GGDKDisplay *gdisp) {
358 #ifdef GGDKDRAW_GDK_3_20
359 GdkSeat *seat = gdk_display_get_default_seat(gdisp->display);
360 if (seat == NULL) {
361 return NULL;
362 }
363
364 return gdk_seat_get_pointer(seat);
365 #else
366 GdkDeviceManager *manager = gdk_display_get_device_manager(gdisp->display);
367 if (manager == NULL) {
368 return NULL;
369 }
370
371 return gdk_device_manager_get_client_pointer(manager);
372 #endif
373 }
374
375 static void _GGDKDraw_CenterWindowOnScreen(GGDKWindow gw) {
376 GGDKDisplay *gdisp = gw->display;
377 GdkRectangle work_area, window_size;
378 int x, y;
379
380 #ifdef GGDKDRAW_GDK_3_22
381 GdkMonitor *monitor;
382
383 gdk_device_get_position(_GGDKDraw_GetPointer(gdisp), NULL, &x, &y);
384 monitor = gdk_display_get_monitor_at_point(gdisp->display, x, y);
385 gdk_monitor_get_workarea(monitor, &work_area);
386 #else
387 GdkScreen *pointer_screen;
388 int monitor = 0;
389
390 gdk_device_get_position(_GGDKDraw_GetPointer(gdisp), &pointer_screen, &x, &y);
391
392 if (pointer_screen == gdisp->screen) { // Ensure it's on the same screen
393 monitor = gdk_screen_get_monitor_at_point(gdisp->screen, x, y);
394 }
395
396 gdk_screen_get_monitor_workarea(pointer_screen, monitor, &work_area);
397 #endif // GGDKDRAW_GDK_3_22
398
399 gdk_window_get_frame_extents(gw->w, &window_size);
400 gw->pos.x = (work_area.width - window_size.width) / 2 + work_area.x;
401 gw->pos.y = (work_area.height - window_size.height) / 2 + work_area.y;
402
403 if (gw->pos.x < work_area.x) {
404 gw->pos.x = work_area.x;
405 }
406
407 if (gw->pos.y < work_area.y) {
408 gw->pos.y = work_area.y;
409 }
410 gdk_window_move(gw->w, gw->pos.x, gw->pos.y);
411 }
412
413 static GWindow _GGDKDraw_CreateWindow(GGDKDisplay *gdisp, GGDKWindow gw, GRect *pos,
414 int (*eh)(GWindow, GEvent *), void *user_data, GWindowAttrs *wattrs) {
415
416 GWindowAttrs temp = GWINDOWATTRS_EMPTY;
417 GdkWindowAttr attribs = {0};
418 gint attribs_mask = 0;
419 GGDKWindow nw = (GGDKWindow)calloc(1, sizeof(struct ggdkwindow));
420
421 if (nw == NULL) {
422 Log(LOGDEBUG, "_GGDKDraw_CreateWindow: GGDKWindow calloc failed.");
423 return NULL;
424 }
425 if (wattrs == NULL) {
426 wattrs = &temp;
427 }
428
429 if (gw == NULL) { // Creating a top-level window. Set parent as default root.
430 gw = gdisp->groot;
431 attribs.window_type = GDK_WINDOW_TOPLEVEL;
432 } else {
433 attribs.window_type = GDK_WINDOW_CHILD;
434 }
435
436 // Now check window type
437 if ((wattrs->mask & wam_nodecor) && wattrs->nodecoration) {
438 // Is a modeless dialogue
439 nw->is_popup = true;
440 nw->is_dlg = true;
441 nw->not_restricted = true;
442 attribs.window_type = GDK_WINDOW_TEMP;
443 } else if ((wattrs->mask & wam_isdlg) && wattrs->is_dlg) {
444 nw->is_dlg = true;
445 }
446 if ((wattrs->mask & wam_notrestricted) && wattrs->not_restricted) {
447 nw->not_restricted = true;
448 }
449 nw->is_toplevel = attribs.window_type != GDK_WINDOW_CHILD;
450
451 // Drawing context
452 nw->ggc = _GGDKDraw_NewGGC();
453 if (nw->ggc == NULL) {
454 Log(LOGDEBUG, "_GGDKDraw_CreateWindow: _GGDKDraw_NewGGC returned NULL");
455 free(nw);
456 return NULL;
457 }
458
459 // Base fields
460 nw->display = gdisp;
461 nw->eh = eh;
462 nw->parent = gw;
463 nw->pos = *pos;
464 nw->user_data = user_data;
465
466 // Window title, hints and event mask
467 attribs.event_mask = GDK_EXPOSURE_MASK | GDK_STRUCTURE_MASK;
468 if (nw->is_toplevel) {
469 // Default event mask for toplevel windows
470 attribs.event_mask |= GDK_FOCUS_CHANGE_MASK | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK;
471
472 // Icon titles are ignored.
473 if ((wattrs->mask & wam_utf8_wtitle) && (wattrs->utf8_window_title != NULL)) {
474 nw->window_title = copy(wattrs->utf8_window_title);
475 } else if ((wattrs->mask & wam_wtitle) && (wattrs->window_title != NULL)) {
476 nw->window_title = u2utf8_copy(wattrs->window_title);
477 }
478
479 attribs.title = nw->window_title;
480 attribs.type_hint = (nw->is_popup || (wattrs->mask & wam_palette)) ?
481 GDK_WINDOW_TYPE_HINT_UTILITY : GDK_WINDOW_TYPE_HINT_NORMAL;
482
483 if (attribs.title != NULL) {
484 attribs_mask |= GDK_WA_TITLE;
485 }
486 attribs_mask |= GDK_WA_TYPE_HINT;
487 }
488
489 // Further event mask flags
490 if (wattrs->mask & wam_events) {
491 if (wattrs->event_masks & (1 << et_char)) {
492 attribs.event_mask |= GDK_KEY_PRESS_MASK;
493 }
494 if (wattrs->event_masks & (1 << et_charup)) {
495 attribs.event_mask |= GDK_KEY_RELEASE_MASK;
496 }
497 if (wattrs->event_masks & (1 << et_mousemove)) {
498 attribs.event_mask |= GDK_POINTER_MOTION_MASK;
499 }
500 if (wattrs->event_masks & (1 << et_mousedown)) {
501 attribs.event_mask |= GDK_BUTTON_PRESS_MASK | GDK_SCROLL_MASK;
502 }
503 if (wattrs->event_masks & (1 << et_mouseup)) {
504 attribs.event_mask |= GDK_BUTTON_RELEASE_MASK | GDK_SCROLL_MASK;
505 }
506 if (wattrs->event_masks & (1 << et_visibility)) {
507 attribs.event_mask |= GDK_VISIBILITY_NOTIFY_MASK;
508 }
509 }
510
511 if (wattrs->mask & wam_restrict) {
512 nw->restrict_input_to_me = wattrs->restrict_input_to_me;
513 }
514 if (wattrs->mask & wam_redirect) {
515 nw->redirect_chars_to_me = wattrs->redirect_chars_to_me;
516 nw->redirect_from = wattrs->redirect_from;
517 }
518
519 attribs.width = pos->width;
520 attribs.height = pos->height;
521 attribs.wclass = GDK_INPUT_OUTPUT;
522 // GDK docs say to not use this. But that's because it's done by GTK...
523 attribs.wmclass_name = GResourceProgramName;
524 attribs.wmclass_class = GResourceProgramName;
525 attribs_mask |= GDK_WA_WMCLASS;
526
527 nw->w = gdk_window_new(gw->w, &attribs, attribs_mask);
528 if (nw->w == NULL) {
529 Log(LOGDEBUG, "GGDKDraw: Failed to create window!");
530 free(nw->window_title);
531 free(nw->ggc);
532 free(nw);
533 return NULL;
534 }
535
536 // We center windows here because we need to know the window size+decor
537 // There is a bug on Windows (all versions < 3.21.1, <= 2.24.30) so don't use GDK_WA_X/GDK_WA_Y
538 // https://bugzilla.gnome.org/show_bug.cgi?id=764996
539 if (nw->is_toplevel && (!(wattrs->mask & wam_positioned) || (wattrs->mask & wam_centered))) {
540 nw->is_centered = true;
541 _GGDKDraw_CenterWindowOnScreen(nw);
542 } else {
543 gdk_window_move(nw->w, nw->pos.x, nw->pos.y);
544 }
545
546 // Set background
547 if (!(wattrs->mask & wam_backcol) || wattrs->background_color == COLOR_DEFAULT) {
548 wattrs->background_color = _GDraw_res_bg;
549 }
550 nw->ggc->bg = wattrs->background_color;
551 GGDKDrawSetWindowBackground((GWindow)nw, wattrs->background_color);
552
553 if (nw->is_toplevel) {
554 // Set opaque region
555 _GGDKDraw_SetOpaqueRegion(nw);
556
557 // Set icon
558 GGDKWindow icon = gdisp->default_icon;
559 if (((wattrs->mask & wam_icon) && wattrs->icon != NULL) && ((GGDKWindow)wattrs->icon)->is_pixmap) {
560 icon = (GGDKWindow) wattrs->icon;
561 }
562 if (icon != NULL) {
563 GdkPixbuf *pb = gdk_pixbuf_get_from_surface(icon->cs, 0, 0, icon->pos.width, icon->pos.height);
564 if (pb != NULL) {
565 GList_Glib ent = {.data = pb};
566 gdk_window_set_icon_list(nw->w, &ent);
567 g_object_unref(pb);
568 }
569 } else {
570 gdk_window_set_decorations(nw->w, GDK_DECOR_ALL | GDK_DECOR_MENU);
571 }
572
573 GdkGeometry geom = {0};
574 GdkWindowHints hints = 0;
575 if (wattrs->mask & wam_palette) {
576 hints |= GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE;
577 }
578 if ((wattrs->mask & wam_noresize) && wattrs->noresize) {
579 hints |= GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE | GDK_HINT_BASE_SIZE;
580 }
581
582 // Hmm does this seem right?
583 geom.base_width = geom.min_width = geom.max_width = pos->width;
584 geom.base_height = geom.min_height = geom.max_height = pos->height;
585
586 hints |= GDK_HINT_POS;
587 nw->was_positioned = true;
588
589 gdk_window_set_geometry_hints(nw->w, &geom, hints);
590
591 if ((wattrs->mask & wam_transient) && wattrs->transient != NULL) {
592 GGDKDrawSetTransientFor((GWindow)nw, wattrs->transient);
593 nw->is_dlg = true;
594 } else if (!nw->is_dlg) {
595 ++gdisp->top_window_count;
596 } else if (nw->restrict_input_to_me && gdisp->mru_windows->length > 0) {
597 GGDKDrawSetTransientFor((GWindow)nw, (GWindow) - 1);
598 }
599 nw->isverytransient = (wattrs->mask & wam_verytransient) ? 1 : 0;
600
601 // Don't allow popups or 'very transient' windows from entering this list
602 if (!nw->is_popup && !nw->isverytransient) {
603 // This creates a single link with the data being the window, and next/prev NULL
604 nw->mru_link = g_list_append(NULL, (gpointer)nw);
605 if (nw->mru_link == NULL) {
606 Log(LOGDEBUG, "Failed to create mru_link");
607 gdk_window_destroy(nw->w);
608 free(nw->window_title);
609 free(nw->ggc);
610 free(nw);
611 return NULL;
612 }
613 }
614 }
615
616 // Establish Pango/Cairo context
617 if (!_GGDKDraw_InitPangoCairo(nw)) {
618 gdk_window_destroy(nw->w);
619 free(nw->window_title);
620 free(nw->ggc);
621 free(nw->mru_link);
622 free(nw);
623 return NULL;
624 }
625
626 if ((wattrs->mask & wam_cursor) && wattrs->cursor != ct_default) {
627 GGDKDrawSetCursor((GWindow)nw, wattrs->cursor);
628 }
629
630 // Add a reference to our own structure.
631 GGDKDRAW_ADDREF(nw);
632
633 // Add a reference on the parent window
634 if (nw->parent != gdisp->groot) {
635 GGDKDRAW_ADDREF(nw->parent);
636 }
637
638 if (nw->mru_link) {
639 // Add it into the mru list
640 Log(LOGDEBUG, "Adding %p(%s) to the mru list", nw, nw->window_title);
641 g_queue_push_tail_link(gdisp->mru_windows, nw->mru_link);
642 }
643
644 // Event handler
645 if (eh != NULL) {
646 GEvent e = {0};
647 e.type = et_create;
648 e.w = (GWindow) nw;
649 e.native_window = nw->w;
650 _GGDKDraw_CallEHChecked(nw, &e, eh);
651 }
652
653 // Set the user data to the GWindow
654 // Although there is gdk_window_set_user_data, if non-NULL,
655 // GTK assumes it's a GTKWindow.
656 g_object_set_data(G_OBJECT(nw->w), "GGDKWindow", nw);
657 // Add our reference to the window
658 // This will be unreferenced when the window is destroyed.
659 g_object_ref(G_OBJECT(nw->w));
660 // Add our window to the display's hashmap
661 g_hash_table_add(gdisp->windows, nw);
662 //gdk_window_move_resize(nw->w, nw->pos.x, nw->pos.y, nw->pos.width, nw->pos.height);
663
664 Log(LOGDEBUG, "Window created: %p[%p][%s][toplevel:%d]", nw, nw->w, nw->window_title, nw->is_toplevel);
665 return (GWindow)nw;
666 }
667
668 static GWindow _GGDKDraw_NewPixmap(GDisplay *disp, GWindow similar, uint16_t width, uint16_t height, bool is_bitmap,
669 unsigned char *data) {
670 GGDKDisplay *gdisp = (GGDKDisplay *)disp;
671 GGDKWindow gw = (GGDKWindow)calloc(1, sizeof(struct ggdkwindow));
672 if (gw == NULL) {
673 Log(LOGDEBUG, "GGDKDRAW: GGDKWindow calloc failed!");
674 return NULL;
675 }
676
677 gw->ggc = _GGDKDraw_NewGGC();
678 if (gw->ggc == NULL) {
679 Log(LOGDEBUG, "GGDKDRAW: GGC alloc failed!");
680 free(gw);
681 return NULL;
682 }
683 gw->ggc->bg = _GDraw_res_bg;
684 width &= 0x7fff; // We're always using a cairo surface...
685
686 gw->display = gdisp;
687 gw->is_pixmap = 1;
688 gw->parent = NULL;
689 gw->pos.x = gw->pos.y = 0;
690 gw->pos.width = width;
691 gw->pos.height = height;
692
693 if (data == NULL) {
694 if (similar == NULL) {
695 gw->cs = cairo_image_surface_create(is_bitmap ? CAIRO_FORMAT_A1 : CAIRO_FORMAT_ARGB32, width, height);
696 } else {
697 gw->cs = gdk_window_create_similar_surface(((GGDKWindow)similar)->w, CAIRO_CONTENT_COLOR, width, height);
698 }
699 } else {
700 cairo_format_t format = is_bitmap ? CAIRO_FORMAT_A1 : CAIRO_FORMAT_ARGB32;
701 gw->cs = cairo_image_surface_create_for_data(data, format, width, height, cairo_format_stride_for_width(format, width));
702 }
703
704 if (gw->cs == NULL) {
705 Log(LOGDEBUG, "GGDKDRAW: Cairo image surface creation failed!");
706 free(gw->ggc);
707 free(gw);
708 return NULL;
709 }
710
711 if (!_GGDKDraw_InitPangoCairo(gw)) {
712 cairo_surface_destroy(gw->cs);
713 free(gw->ggc);
714 free(gw);
715 return NULL;
716 }
717 // Add a reference to ourselves
718 GGDKDRAW_ADDREF(gw);
719 return (GWindow)gw;
720 }
721
722 static int16_t _GGDKDraw_GdkModifierToKsm(GdkModifierType mask) {
723 int16_t state = 0;
724 //Translate from mask to X11 state
725 if (mask & GDK_SHIFT_MASK) {
726 state |= ksm_shift;
727 }
728 if (mask & GDK_LOCK_MASK) {
729 state |= ksm_capslock;
730 }
731 if (mask & GDK_CONTROL_MASK) {
732 state |= ksm_control;
733 }
734 if (mask & GDK_MOD1_MASK) { //Guess
735 state |= ksm_meta;
736 }
737 if (mask & GDK_MOD2_MASK) { //Guess
738 state |= ksm_cmdmacosx;
739 }
740 if (mask & GDK_SUPER_MASK) {
741 state |= ksm_super;
742 }
743 if (mask & GDK_HYPER_MASK) {
744 state |= ksm_hyper;
745 }
746 //ksm_option?
747 if (mask & GDK_BUTTON1_MASK) {
748 state |= ksm_button1;
749 }
750 if (mask & GDK_BUTTON2_MASK) {
751 state |= ksm_button2;
752 }
753 if (mask & GDK_BUTTON3_MASK) {
754 state |= ksm_button3;
755 }
756 if (mask & GDK_BUTTON4_MASK) {
757 state |= ksm_button4;
758 }
759 if (mask & GDK_BUTTON5_MASK) {
760 state |= ksm_button5;
761 }
762
763 return state;
764 }
765
766 static VisibilityState _GGDKDraw_GdkVisibilityStateToVS(GdkVisibilityState state) {
767 switch (state) {
768 case GDK_VISIBILITY_FULLY_OBSCURED:
769 return vs_obscured;
770 break;
771 case GDK_VISIBILITY_PARTIAL:
772 return vs_partially;
773 break;
774 case GDK_VISIBILITY_UNOBSCURED:
775 default:
776 return vs_unobscured;
777 break;
778 }
779 }
780
781 static int _GGDKDraw_WindowOrParentsDying(GGDKWindow gw) {
782 while (gw != NULL) {
783 if (gw->is_dying) {
784 return true;
785 }
786 if (gw->is_toplevel) {
787 return false;
788 }
789 gw = gw->parent;
790 }
791 return false;
792 }
793
794 static bool _GGDKDraw_FilterByModal(GdkEvent *event, GGDKWindow gw) {
795 switch (event->type) {
796 case GDK_KEY_PRESS:
797 case GDK_KEY_RELEASE:
798 case GDK_BUTTON_PRESS:
799 case GDK_BUTTON_RELEASE:
800 case GDK_SCROLL:
801 case GDK_MOTION_NOTIFY:
802 case GDK_DELETE:
803 case GDK_FOCUS_CHANGE:
804 break;
805 default:
806 return false;
807 break;
808 }
809
810 // Check if this window has an active GTK modal dialog over it
811 GdkWindow* top_gdk_win = gdk_window_get_effective_toplevel(gw->w);
812 if (g_object_get_data(G_OBJECT(top_gdk_win), "GTKModalBlock")) {
813 return true;
814 }
815
816 GGDKWindow gww = gw;
817 GPtrArray *stack = gw->display->transient_stack;
818
819 if (gww && gw->display->restrict_count == 0) {
820 return false;
821 }
822
823 GGDKWindow last_modal = NULL;
824 while (gww != NULL) {
825 if (gww->is_toplevel) {
826 for (int i = ((int)stack->len) - 1; i >= 0; --i) {
827 GGDKWindow ow = (GGDKWindow)stack->pdata[i];
828 if (ow == gww || ow->restrict_input_to_me) { // fudged
829 last_modal = ow;
830 break;
831 }
832 }
833
834 if (last_modal == NULL || last_modal == gww) {
835 return false;
836 }
837 }
838
839 gww = gww->parent;
840 }
841
842 if (event->type != GDK_MOTION_NOTIFY && event->type != GDK_BUTTON_RELEASE && event->type != GDK_FOCUS_CHANGE) {
843 gdk_window_beep(gw->w);
844 if (last_modal != NULL) {
845 GDrawRaise((GWindow)last_modal);
846 }
847 }
848
849 return true;
850 }
851
852 static gboolean _GGDKDraw_ProcessTimerEvent(gpointer user_data) {
853 GGDKTimer *timer = (GGDKTimer *)user_data;
854 GEvent e = {0};
855 bool ret = true;
856
857 if (!timer->active || _GGDKDraw_WindowOrParentsDying((GGDKWindow)timer->owner)) {
858 timer->active = false;
859 timer->stopped = true;
860 return false;
861 }
862
863 e.type = et_timer;
864 e.w = timer->owner;
865 e.native_window = timer->owner->native_window;
866 e.u.timer.timer = (GTimer *)timer;
867 e.u.timer.userdata = timer->userdata;
868
869 GGDKDRAW_ADDREF(timer);
870 _GGDKDraw_CallEHChecked((GGDKWindow)timer->owner, &e, timer->owner->eh);
871 if (timer->active) {
872 if (timer->repeat_time == 0) {
873 timer->stopped = true; // Since we return false, this timer is no longer valid.
874 GGDKDrawCancelTimer((GTimer *)timer);
875 ret = false;
876 } else if (timer->has_differing_repeat_time) {
877 timer->has_differing_repeat_time = false;
878 timer->glib_timeout_id = g_timeout_add(timer->repeat_time, _GGDKDraw_ProcessTimerEvent, timer);
879 ret = false;
880 }
881 }
882
883 GGDKDRAW_DECREF(timer, _GGDKDraw_OnTimerDestroyed);
884 return ret;
885 }
886
887 static void _GGDKDraw_DispatchEvent(GdkEvent *event, gpointer data) {
888 //static int request_id = 0;
889 struct gevent gevent = {0};
890 GGDKDisplay *gdisp = (GGDKDisplay *)data;
891 GdkWindow *w = ((GdkEventAny *)event)->window;
892 GGDKWindow gw;
893 guint32 event_time = gdk_event_get_time(event);
894 if (event_time != GDK_CURRENT_TIME) {
895 gdisp->last_event_time = event_time;
896 }
897
898 //Log(LOGDEBUG, "[%d] Received event %d(%s) %p", request_id++, event->type, GdkEventName(event->type), w);
899 //fflush(stderr);
900
901 if (w == NULL) {
902 return;
903 } else if ((gw = g_object_get_data(G_OBJECT(w), "GGDKWindow")) == NULL) {
904 //Log(LOGDEBUG, "MISSING GW!");
905
906 // TODO: rework this hack to integrate better into event loop
907 // (atm GTK is just snuffling up any events ignored by gdraw)
908 gtk_main_do_event(event);
909
910 return;
911 } else if (_GGDKDraw_WindowOrParentsDying(gw) || gdk_window_is_destroyed(w)) {
912 Log(LOGDEBUG, "DYING! %p", w);
913 return;
914 } else if (_GGDKDraw_FilterByModal(event, gw)) {
915 Log(LOGDEBUG, "Discarding event - has modals!");
916 return;
917 }
918 // TODO(iorsh): Discard GDraw window mouse events when blocked by modal GTK window.
919 // GDK doesn't manage internally modal relations, so we need to keep track ourselves.
920 // This is too dangerous to implement at the inital GTK introduction.
921
922 gevent.w = (GWindow)gw;
923 gevent.native_window = (void *)gw->w;
924 gevent.type = et_noevent;
925
926 switch (event->type) {
927 case GDK_KEY_PRESS:
928 case GDK_KEY_RELEASE: {
929 GdkEventKey *key = (GdkEventKey *)event;
930 gevent.type = event->type == GDK_KEY_PRESS ? et_char : et_charup;
931 gevent.u.chr.state = _GGDKDraw_GdkModifierToKsm(((GdkEventKey *)event)->state);
932
933 gevent.u.chr.autorepeat =
934 event->type == GDK_KEY_PRESS &&
935 gdisp->ks.type == GDK_KEY_PRESS &&
936 key->keyval == gdisp->ks.keyval &&
937 key->state == gdisp->ks.state;
938 // Mumble mumble Mac
939 //if ((event->xkey.state & ksm_option) && gdisp->macosx_cmd) {
940 // gevent.u.chr.state |= ksm_meta;
941 //}
942
943 if (gevent.u.chr.autorepeat && GKeysymIsModifier(key->keyval)) {
944 gevent.type = et_noevent;
945 } else {
946 if (key->keyval == GDK_KEY_space) {
947 gw->display->is_space_pressed = event->type == GDK_KEY_PRESS;
948 }
949
950 gevent.u.chr.keysym = key->keyval;
951 gevent.u.chr.chars[0] = gdk_keyval_to_unicode(key->keyval);
952 }
953
954 gdisp->ks.group = key->group;
955 gdisp->ks.keycode = key->hardware_keycode;
956 gdisp->ks.keyval = key->keyval;
957 gdisp->ks.state = key->state;
958 gdisp->ks.type = key->type;
959 }
960 break;
961 case GDK_MOTION_NOTIFY: {
962 GdkEventMotion *evt = (GdkEventMotion *)event;
963 gevent.type = et_mousemove;
964 gevent.u.mouse.state = _GGDKDraw_GdkModifierToKsm(evt->state);
965 gevent.u.mouse.x = evt->x;
966 gevent.u.mouse.y = evt->y;
967 //Log(LOGDEBUG, "Motion: [%f %f]", evt->x, evt->y);
968 }
969 break;
970 case GDK_SCROLL: { //Synthesize a button press
971 GdkEventScroll *evt = (GdkEventScroll *)event;
972 gevent.u.mouse.state = _GGDKDraw_GdkModifierToKsm(evt->state);
973 gevent.u.mouse.x = evt->x;
974 gevent.u.mouse.y = evt->y;
975 gevent.u.mouse.clicks = gdisp->bs.cur_click = 1;
976 gevent.type = et_mousedown;
977
978 switch (evt->direction) {
979 case GDK_SCROLL_UP:
980 gevent.u.mouse.button = 4;
981 break;
982 case GDK_SCROLL_DOWN:
983 gevent.u.mouse.button = 5;
984 break;
985 case GDK_SCROLL_LEFT:
986 gevent.u.mouse.button = 6;
987 break;
988 case GDK_SCROLL_RIGHT:
989 gevent.u.mouse.button = 7;
990 break;
991 default: // Ignore GDK_SCROLL_SMOOTH
992 gevent.type = et_noevent;
993 }
994 // We need to simulate two events... I think.
995 if (gevent.type != et_noevent) {
996 GGDKDrawPostEvent(&gevent);
997 gevent.type = et_mouseup;
998 }
999 }
1000 break;
1001 case GDK_BUTTON_PRESS:
1002 case GDK_BUTTON_RELEASE: {
1003 GdkEventButton *evt = (GdkEventButton *)event;
1004 gevent.u.mouse.state = _GGDKDraw_GdkModifierToKsm(evt->state);
1005 gevent.u.mouse.x = evt->x;
1006 gevent.u.mouse.y = evt->y;
1007 gevent.u.mouse.button = evt->button;
1008 gevent.u.mouse.time = evt->time;
1009
1010 Log(LOGDEBUG, "Button %7s: [%f %f]", evt->type == GDK_BUTTON_PRESS ? "press" : "release", evt->x, evt->y);
1011
1012 #ifdef GDK_WINDOWING_QUARTZ
1013 // Quartz backend fails to give a button press event when the click
1014 // lands outside a grabbed window (e.g. clicking off an open menu to
1015 // dismiss it): only the release is delivered, never the press.
1016 // https://bugzilla.gnome.org/show_bug.cgi?id=769961
1017 //
1018 // Synthesize the missing press when an out-of-bounds release arrives
1019 // and no real press is currently held down (which is what tells us
1020 // the press was swallowed, rather than this being the release of a
1021 // genuine press-inside/drag-outside gesture). Gating on the button
1022 // actually being down fixes dismissal on the *first* click; the old
1023 // heuristic (release_w == gw) only became true after a first,
1024 // wasted click had already set release_w to this window.
1025 if (!evt->send_event && evt->type == GDK_BUTTON_RELEASE &&
1026 !gdisp->bs.is_pressed &&
1027 (evt->x < 0 || evt->x > gw->pos.width ||
1028 evt->y < 0 || evt->y > gw->pos.height)) {
1029 evt->send_event = true;
1030 gdk_event_put(event);
1031 event->type = GDK_BUTTON_PRESS;
1032 }
1033 #endif
1034
1035 if (event->type == GDK_BUTTON_PRESS) {
1036 int xdiff, ydiff;
1037 gevent.type = et_mousedown;
1038
1039 xdiff = abs(((int)evt->x) - gdisp->bs.release_x);
1040 ydiff = abs(((int)evt->y) - gdisp->bs.release_y);
1041
1042 if (xdiff + ydiff < gdisp->bs.double_wiggle &&
1043 gw == gdisp->bs.release_w &&
1044 gevent.u.mouse.button == gdisp->bs.release_button &&
1045 (int32_t)(gevent.u.mouse.time - gdisp->bs.last_press_time) < gdisp->bs.double_time &&
1046 gevent.u.mouse.time >= gdisp->bs.last_press_time) { // Time can wrap
1047
1048 gdisp->bs.cur_click++;
1049 } else {
1050 gdisp->bs.cur_click = 1;
1051 }
1052 gdisp->bs.last_press_time = gevent.u.mouse.time;
1053 // Track only genuine presses; a synthetic press (see the Quartz
1054 // workaround above) is marked send_event and must not register as
1055 // a real button-down, or the paired release would look unbalanced.
1056 if (!evt->send_event) {
1057 gdisp->bs.is_pressed = true;
1058 }
1059 } else {
1060 gevent.type = et_mouseup;
1061 gdisp->bs.release_w = gw;
1062 gdisp->bs.release_x = evt->x;
1063 gdisp->bs.release_y = evt->y;
1064 gdisp->bs.release_button = evt->button;
1065 gdisp->bs.is_pressed = false;
1066 }
1067 gevent.u.mouse.clicks = gdisp->bs.cur_click;
1068 }
1069 break;
1070 case GDK_EXPOSE: {
1071 GdkEventExpose *expose = (GdkEventExpose *)event;
1072 // Okay. So on GDK3, we must exclude child window regions to prevent flicker.
1073 // However, we cannot modify the expose event's region directly, as this is
1074 // used by GDK to determine what child windows to invalidate. Hence, we must
1075 // make a copy of the region, sans any child window areas.
1076 cairo_rectangle_int_t extents;
1077 cairo_region_t *reg = _GGDKDraw_ExcludeChildRegions(gw, expose->region, true);
1078 cairo_region_get_extents(reg, &extents);
1079
1080 gevent.type = et_expose;
1081 gevent.u.expose.rect.x = extents.x;
1082 gevent.u.expose.rect.y = extents.y;
1083 gevent.u.expose.rect.width = extents.width;
1084 gevent.u.expose.rect.height = extents.height;
1085
1086 // This can happen if say gdk_window_raise is called, which then runs the
1087 // event loop itself, which is outside of our checked event handler
1088 // I've added autopaint cleanups before such calls, so this should
1089 // never happen any more.
1090 assert(gw->cc == NULL);
1091
1092 #ifdef GGDKDRAW_GDK_3_22
1093 gw->drawing_ctx = gdk_window_begin_draw_frame(w, reg);
1094 #else
1095 gdk_window_begin_paint_region(w, reg);
1096 #endif
1097 gw->is_in_paint = true;
1098 gdisp->dirty_window = gw;
1099
1100 // But wait, there's more! On GDK3, if the window isn't native,
1101 // gdk_window_begin_paint_region does nothing.
1102 // So we have to (a) ensure we draw to an offscreen surface,
1103 // and (b) clip the region appropriately. But on Windows
1104 // (and maybe Quartz), it seems to double buffer anyway.
1105 if (!gdk_window_has_native(gw->w)) {
1106 #if !defined(GDK_WINDOWING_WIN32) && !defined(GDK_WINDOWING_QUARTZ)
1107 double sx = 1, sy = 1;
1108
1109 gw->cs = gdk_window_create_similar_surface(gw->w, CAIRO_CONTENT_COLOR, extents.width, extents.height);
1110 #if (CAIRO_VERSION_MAJOR >= 2) || (CAIRO_VERSION_MAJOR >= 1 && CAIRO_VERSION_MINOR >= 14)
1111 cairo_surface_get_device_scale(gw->cs, &sx, &sy);
1112 #endif
1113 cairo_surface_set_device_offset(gw->cs, -extents.x * sx, -extents.y * sy);
1114 gw->cc = cairo_create(gw->cs);
1115 gw->expose_region = cairo_region_reference(reg);
1116 #else
1117 #ifdef GGDKDRAW_GDK_3_22
1118 gw->cc = cairo_reference(gdk_drawing_context_get_cairo_context(gw->drawing_ctx));
1119 #else
1120 gw->cc = gdk_cairo_create(gw->w);
1121 #endif
1122 #endif
1123 gdk_cairo_region(gw->cc, reg);
1124 cairo_clip(gw->cc);
1125 }
1126 cairo_region_destroy(reg);
1127 }
1128 break;
1129 case GDK_VISIBILITY_NOTIFY:
1130 gevent.type = et_visibility;
1131 gevent.u.visibility.state = _GGDKDraw_GdkVisibilityStateToVS(((GdkEventVisibility *)event)->state);
1132 break;
1133 case GDK_FOCUS_CHANGE:
1134 gevent.type = et_focus;
1135 gevent.u.focus.gained_focus = ((GdkEventFocus *)event)->in;
1136 gevent.u.focus.mnemonic_focus = false;
1137
1138 Log(LOGDEBUG, "Focus change %s %p(%s %d %d)",
1139 gevent.u.focus.gained_focus ? "IN" : "OUT",
1140 gw, gw->window_title, gw->is_toplevel, gw->istransient);
1141
1142 if (gw->mru_link && gevent.u.focus.gained_focus) {
1143 GGDKWindow cur_toplevel = (GGDKWindow)g_queue_peek_head(gdisp->mru_windows);
1144 if (cur_toplevel != gw) {
1145 Log(LOGDEBUG, "Last active toplevel updated: %p(%s)", gw, gw->window_title);
1146 g_queue_unlink(gdisp->mru_windows, gw->mru_link);
1147 g_queue_push_head_link(gdisp->mru_windows, gw->mru_link);
1148 }
1149 }
1150
1151 break;
1152 case GDK_ENTER_NOTIFY:
1153 case GDK_LEAVE_NOTIFY: { // Should only get this on top level
1154 GdkEventCrossing *crossing = (GdkEventCrossing *)event;
1155 if (crossing->focus) { //Focus or inferior
1156 break;
1157 }
1158 if (gdisp->focusfollowsmouse && gw != NULL && gw->eh != NULL) {
1159 gevent.type = et_focus;
1160 gevent.u.focus.gained_focus = crossing->type == GDK_ENTER_NOTIFY;
1161 gevent.u.focus.mnemonic_focus = false;
1162 _GGDKDraw_CallEHChecked(gw, &gevent, gw->eh);
1163 }
1164 gevent.type = et_crossing;
1165 gevent.u.crossing.x = crossing->x;
1166 gevent.u.crossing.y = crossing->y;
1167 gevent.u.crossing.state = _GGDKDraw_GdkModifierToKsm(crossing->state);
1168 gevent.u.crossing.entered = crossing->type == GDK_ENTER_NOTIFY;
1169 gevent.u.crossing.device = NULL;
1170 gevent.u.crossing.time = crossing->time;
1171
1172 // Always set to false when crossing boundary...
1173 gw->display->is_space_pressed = false;
1174 }
1175 break;
1176 case GDK_CONFIGURE: {
1177 GdkEventConfigure *configure = (GdkEventConfigure *)event;
1178 gevent.type = et_resize;
1179 gevent.u.resize.size.x = configure->x;
1180 gevent.u.resize.size.y = configure->y;
1181 gevent.u.resize.size.width = configure->width;
1182 gevent.u.resize.size.height = configure->height;
1183 gevent.u.resize.dx = configure->x - gw->pos.x;
1184 gevent.u.resize.dy = configure->y - gw->pos.y;
1185 gevent.u.resize.dwidth = configure->width - gw->pos.width;
1186 gevent.u.resize.dheight = configure->height - gw->pos.height;
1187 gevent.u.resize.moved = gevent.u.resize.sized = false;
1188 if (gevent.u.resize.dx != 0 || gevent.u.resize.dy != 0) {
1189 gevent.u.resize.moved = true;
1190 gw->is_centered = false;
1191 }
1192 if (gevent.u.resize.dwidth != 0 || gevent.u.resize.dheight != 0) {
1193 gevent.u.resize.sized = true;
1194 }
1195
1196 // I could make this Windows specific... But it doesn't seem necessary on other platforms too.
1197 // On Windows, repeated configure messages are sent if we move the window around.
1198 // This causes CPU usage to go up because mouse handlers of this message just redraw the whole window.
1199 if (gw->is_toplevel && !gevent.u.resize.sized && gevent.u.resize.moved) {
1200 gevent.type = et_noevent;
1201 Log(LOGDEBUG, "Configure DISCARDED: %p:%s, %d %d %d %d", gw, gw->window_title, gw->pos.x, gw->pos.y, gw->pos.width, gw->pos.height);
1202 } else {
1203 Log(LOGDEBUG, "CONFIGURED: %p:%s, %d %d %d %d", gw, gw->window_title, gw->pos.x, gw->pos.y, gw->pos.width, gw->pos.height);
1204 }
1205 gw->pos = gevent.u.resize.size;
1206
1207 // Update the opaque region (we're always completely opaque)
1208 // Although I don't actually know if this is completely necessary
1209 if (gw->is_toplevel && gevent.u.resize.sized) {
1210 _GGDKDraw_SetOpaqueRegion(gw);
1211 }
1212 }
1213 break;
1214 case GDK_MAP:
1215 gevent.type = et_map;
1216 gevent.u.map.is_visible = true;
1217 gw->is_visible = true;
1218 break;
1219 case GDK_UNMAP:
1220 gevent.type = et_map;
1221 gevent.u.map.is_visible = false;
1222 gw->is_visible = false;
1223 break;
1224 case GDK_DESTROY:
1225 GGDKDrawDestroyWindow((GWindow)gw); //Note: If we get here, something's probably wrong.
1226 break;
1227 case GDK_DELETE:
1228 gw->is_waiting_for_selection = false;
1229 gevent.type = et_close;
1230 break;
1231 case GDK_SELECTION_CLEAR: {
1232 gevent.type = et_selclear;
1233 gevent.u.selclear.sel = sn_primary;
1234 for (int i = 0; i < sn_max; i++) {
1235 GdkEventSelection *se = (GdkEventSelection *)event;
1236 if (se->selection == gdisp->selinfo[i].sel_atom) {
1237 gevent.u.selclear.sel = i;
1238 break;
1239 }
1240 }
1241 _GGDKDraw_ClearSelData(gdisp, gevent.u.selclear.sel);
1242 }
1243 break;
1244 case GDK_SELECTION_REQUEST: {
1245 GdkEventSelection *e = (GdkEventSelection *)event;
1246 gdk_selection_send_notify(
1247 e->requestor, e->selection, e->target,
1248 _GGDKDraw_TransmitSelection(gdisp, e) ? e->property : GDK_NONE,
1249 e->time);
1250 }
1251 break;
1252 case GDK_SELECTION_NOTIFY: // paste
1253 if (gw->is_waiting_for_selection) {
1254 gw->is_waiting_for_selection = false;
1255 gw->is_notified_of_selection = ((GdkEventSelection *)event)->property != GDK_NONE;
1256 }
1257 break;
1258 case GDK_PROPERTY_NOTIFY:
1259 break;
1260 default:
1261 Log(LOGDEBUG, "UNPROCESSED GDK EVENT %d %s", event->type, GdkEventName(event->type));
1262 break;
1263 }
1264
1265 if (gevent.type != et_noevent && gw != NULL && gw->eh != NULL) {
1266 _GGDKDraw_CallEHChecked(gw, &gevent, gw->eh);
1267 }
1268 //Log(LOGDEBUG, "[%d] Finished processing %d(%s)", request_id++, event->type, GdkEventName(event->type));
1269 }
1270
1271 static void GGDKDrawInit(GDisplay *gdisp) {
1272 FState *fs = calloc(1, sizeof(FState));
1273 if (fs == NULL) {
1274 Log(LOGDEBUG, "GGDKDraw: FState alloc failed!");
1275 assert(false);
1276 }
1277
1278 // In inches, because that's how fonts are measured
1279 gdisp->fontstate = fs;
1280 fs->res = gdisp->res;
1281 }
1282
1283 static void GGDKDrawSetDefaultIcon(GWindow icon) {
1284 GGDKWindow gicon = (GGDKWindow)icon;
1285 if (gicon->is_pixmap) {
1286 gicon->display->default_icon = gicon;
1287 }
1288 }
1289
1290 static GWindow GGDKDrawCreateTopWindow(GDisplay *gdisp, GRect *pos, int (*eh)(GWindow gw, GEvent *), void *user_data,
1291 GWindowAttrs *gattrs) {
1292 Log(LOGDEBUG, " ");
1293 return _GGDKDraw_CreateWindow((GGDKDisplay *) gdisp, NULL, pos, eh, user_data, gattrs);
1294 }
1295
1296 static GWindow GGDKDrawCreateSubWindow(GWindow gw, GRect *pos, int (*eh)(GWindow gw, GEvent *), void *user_data,
1297 GWindowAttrs *gattrs) {
1298 Log(LOGDEBUG, " ");
1299 return _GGDKDraw_CreateWindow(((GGDKWindow) gw)->display, (GGDKWindow) gw, pos, eh, user_data, gattrs);
1300 }
1301
1302 static GWindow GGDKDrawCreatePixmap(GDisplay *gdisp, GWindow similar, uint16_t width, uint16_t height) {
1303 Log(LOGDEBUG, " ");
1304
1305 //TODO: Check format?
1306 return _GGDKDraw_NewPixmap(gdisp, similar, width, height, false, NULL);
1307 }
1308
1309 static GWindow GGDKDrawCreateBitmap(GDisplay *gdisp, uint16_t width, uint16_t height, uint8_t *data) {
1310 Log(LOGDEBUG, " ");
1311 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_A1, width);
1312 int actual = (width & 0x7fff) / 8;
1313
1314 if (actual != stride) {
1315 GWindow ret = _GGDKDraw_NewPixmap(gdisp, NULL, width, height, true, NULL);
1316 if (ret == NULL) {
1317 return NULL;
1318 }
1319
1320 cairo_surface_flush(((GGDKWindow)ret)->cs);
1321 uint8_t *buf = cairo_image_surface_get_data(((GGDKWindow)ret)->cs);
1322 for (int j = 0; j < height; j++) {
1323 memcpy(buf + stride * j, data + actual * j, actual);
1324 }
1325 cairo_surface_mark_dirty(((GGDKWindow)ret)->cs);
1326 return ret;
1327 }
1328
1329 return _GGDKDraw_NewPixmap(gdisp, NULL, width, height, true, data);
1330 }
1331
1332 static GCursor GGDKDrawCreateCursor(GWindow src, GWindow mask, Color fg, Color bg, int16_t x, int16_t y) {
1333 Log(LOGDEBUG, " ");
1334
1335 GGDKDisplay *gdisp = (GGDKDisplay *)(src->display);
1336 GdkCursor *cursor = NULL;
1337 if (mask == NULL) { // Use src directly
1338 assert(src != NULL);
1339 assert(src->is_pixmap);
1340 cursor = gdk_cursor_new_from_surface(gdisp->display, ((GGDKWindow)src)->cs, x, y);
1341 } else { // Assume it's an X11-style cursor
1342 cairo_surface_t *cs = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, src->pos.width, src->pos.height);
1343 cairo_t *cc = cairo_create(cs);
1344
1345 // Masking
1346 //Background
1347 cairo_set_source_rgb(cc, COLOR_RED(bg) / 255., COLOR_GREEN(bg) / 255., COLOR_BLUE(bg) / 255.);
1348 cairo_mask_surface(cc, ((GGDKWindow)mask)->cs, 0, 0);
1349 //Foreground
1350 cairo_set_source_rgb(cc, COLOR_RED(fg) / 255., COLOR_GREEN(fg) / 255., COLOR_BLUE(fg) / 255.);
1351 cairo_mask_surface(cc, ((GGDKWindow)src)->cs, 0, 0);
1352
1353 cursor = gdk_cursor_new_from_surface(gdisp->display, cs, x, y);
1354
1355 cairo_destroy(cc);
1356 cairo_surface_destroy(cs);
1357 }
1358
1359 g_ptr_array_add(gdisp->cursors, cursor);
1360 return ct_user + (gdisp->cursors->len - 1);
1361 }
1362
1363 static void GGDKDrawDestroyCursor(GDisplay *disp, GCursor gcursor) {
1364 Log(LOGDEBUG, " ");
1365
1366 GGDKDisplay *gdisp = (GGDKDisplay *)disp;
1367 gcursor -= ct_user;
1368 if ((int)gcursor >= 0 && gcursor < gdisp->cursors->len) {
1369 g_object_unref(gdisp->cursors->pdata[gcursor]);
1370 gdisp->cursors->pdata[gcursor] = NULL;
1371 }
1372 }
1373
1374 static void GGDKDrawDestroyWindow(GWindow w) {
1375 GGDKWindow gw = (GGDKWindow) w;
1376 Log(LOGDEBUG, "%p[%p][%s][toplevel:%d][pixmap:%d]",
1377 gw, gw->w, gw->window_title, gw->is_toplevel, gw->is_pixmap);
1378
1379 if (gw->is_dying) {
1380 return;
1381 }
1382
1383 gw->is_dying = true;
1384 gw->is_waiting_for_selection = false;
1385 gw->is_notified_of_selection = false;
1386
1387 if (!gw->is_pixmap && gw != gw->display->groot) {
1388 GList_Glib *list = gdk_window_get_children(gw->w);
1389 GList_Glib *child = list;
1390 while (child != NULL) {
1391 GWindow c = g_object_get_data(child->data, "GGDKWindow");
1392 assert(c != NULL);
1393 GGDKDrawDestroyWindow(c);
1394 child = child->next;
1395 }
1396 g_list_free(list);
1397
1398 if (gw->is_toplevel) {
1399 // Remove itself from the transient list, if present
1400 GGDKDrawSetTransientFor((GWindow)gw, NULL);
1401
1402 // Remove it from the mru window list
1403 if (gw->mru_link != NULL) {
1404 Log(LOGDEBUG, "Removing %p(%s) from mru window list", gw, gw->window_title);
1405 g_queue_delete_link(gw->display->mru_windows, gw->mru_link);
1406 gw->mru_link = NULL;
1407 }
1408 // This is so that e.g. popup menus get hidden immediately
1409 // On Quartz, this is also necessary for toplevel windows,
1410 // as focus change events are not received when a window
1411 // is destroyed, but they *are* when it's first hidden...
1412 GDrawSetVisible((GWindow)gw, false);
1413
1414 // Delete all transient references to this window
1415 for (int i = ((int)gw->display->transient_stack->len) - 1; i >= 0; --i) {
1416 GGDKWindow tw = (GGDKWindow)gw->display->transient_stack->pdata[i];
1417 if (tw->transient_owner == gw) {
1418 GGDKDrawSetTransientFor((GWindow)tw, NULL);
1419 }
1420 }
1421 }
1422
1423 if (gw->display->last_dd.w == gw) {
1424 gw->display->last_dd.w = NULL;
1425 }
1426 // Ensure an invalid value is returned if someone tries to get this value again.
1427 g_object_set_data(G_OBJECT(gw->w), "GGDKWindow", NULL);
1428 }
1429 GGDKDRAW_DECREF(gw, _GGDKDraw_InitiateWindowDestroy);
1430 }
1431
1432 static int GGDKDrawNativeWindowExists(GDisplay *UNUSED(gdisp), void *native_window) {
1433 //Log(LOGDEBUG, " ");
1434 GdkWindow *w = (GdkWindow *)native_window;
1435
1436 if (w != NULL) {
1437 GGDKWindow gw = g_object_get_data(G_OBJECT(w), "GGDKWindow");
1438 // So if the window is dying, the gdk window is already gone.
1439 // But gcontainer.c expects this to return true on et_destroy...
1440 if (gw == NULL || gw->is_dying) {
1441 return true;
1442 } else {
1443 return !gdk_window_is_destroyed(w);
1444 }
1445 }
1446 return false;
1447 }
1448
1449 static void GGDKDrawSetZoom(GWindow UNUSED(gw), GRect *UNUSED(size), enum gzoom_flags UNUSED(flags)) {
1450 //Log(LOGDEBUG, " ");
1451 // Not implemented.
1452 }
1453
1454 static void GGDKDrawSetWindowBackground(GWindow w, Color gcol) {
1455 Log(LOGDEBUG, " ");
1456 GGDKWindow gw = (GGDKWindow)w;
1457 GdkRGBA col = {
1458 .red = COLOR_RED(gcol) / 255.,
1459 .green = COLOR_GREEN(gcol) / 255.,
1460 .blue = COLOR_BLUE(gcol) / 255.,
1461 .alpha = 1.
1462 };
1463 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1464 gdk_window_set_background_rgba(gw->w, &col);
1465 G_GNUC_END_IGNORE_DEPRECATIONS
1466 }
1467
1468 static int GGDKDrawSetDither(GDisplay *UNUSED(gdisp), int UNUSED(set)) {
1469 // Not implemented; does nothing.
1470 return false;
1471 }
1472
1473 static void GGDKDrawSetVisible(GWindow w, int show) {
1474 Log(LOGDEBUG, "0x%p %d", w, show);
1475 GGDKWindow gw = (GGDKWindow)w;
1476 _GGDKDraw_CleanupAutoPaint(gw->display);
1477 if (show) {
1478 if (gdk_window_is_visible(gw->w)) {
1479 Log(LOGDEBUG, "0x%p %d DISCARDED", w, show);
1480 return;
1481 }
1482 #ifdef GDK_WINDOWING_QUARTZ
1483 // Quartz backend fails to send a configure event after showing a window
1484 // But FF expects one.
1485 _GGDKDraw_OnFakedConfigure(gw);
1486 #endif
1487 gdk_window_show(gw->w);
1488 if (gw->restrict_input_to_me && gw->transient_owner == NULL && gw->display->mru_windows->length > 0) {
1489 GGDKDrawSetTransientFor((GWindow)gw, (GWindow) - 1);
1490 }
1491 } else {
1492 GGDKDrawSetTransientFor((GWindow)gw, NULL);
1493 gdk_window_hide(gw->w);
1494 }
1495 }
1496
1497 static void GGDKDrawMove(GWindow gw, int32_t x, int32_t y) {
1498 Log(LOGDEBUG, "%p:%s, %d %d", gw, ((GGDKWindow)gw)->window_title, x, y);
1499 _GGDKDraw_CleanupAutoPaint(((GGDKWindow)gw)->display);
1500 gdk_window_move(((GGDKWindow)gw)->w, x, y);
1501 ((GGDKWindow)gw)->is_centered = false;
1502 if (!gw->is_toplevel) {
1503 _GGDKDraw_FakeConfigureEvent((GGDKWindow)gw);
1504 }
1505 }
1506
1507 static void GGDKDrawTrueMove(GWindow w, int32_t x, int32_t y) {
1508 Log(LOGDEBUG, " ");
1509 GGDKDrawMove(w, x, y);
1510 }
1511
1512 static void GGDKDrawResize(GWindow gw, int32_t w, int32_t h) {
1513 Log(LOGDEBUG, "%p:%s, %d %d", gw, ((GGDKWindow)gw)->window_title, w, h);
1514 _GGDKDraw_CleanupAutoPaint(((GGDKWindow)gw)->display);
1515 gdk_window_resize(((GGDKWindow)gw)->w, w, h);
1516 if (gw->is_toplevel && ((GGDKWindow)gw)->is_centered) {
1517 _GGDKDraw_CenterWindowOnScreen((GGDKWindow)gw);
1518 }
1519 if (!gw->is_toplevel) {
1520 _GGDKDraw_FakeConfigureEvent((GGDKWindow)gw);
1521 }
1522 }
1523
1524 static void GGDKDrawMoveResize(GWindow gw, int32_t x, int32_t y, int32_t w, int32_t h) {
1525 Log(LOGDEBUG, "%p:%s, %d %d %d %d", gw, ((GGDKWindow)gw)->window_title, x, y, w, h);
1526 _GGDKDraw_CleanupAutoPaint(((GGDKWindow)gw)->display);
1527 gdk_window_move_resize(((GGDKWindow)gw)->w, x, y, w, h);
1528 if (!gw->is_toplevel) {
1529 _GGDKDraw_FakeConfigureEvent((GGDKWindow)gw);
1530 }
1531 }
1532
1533 static void GGDKDrawRaise(GWindow w) {
1534 GGDKWindow gw = (GGDKWindow) w;
1535 Log(LOGDEBUG, "%p[%p][%s]", gw, gw->w, gw->window_title);
1536 if (!gw->is_visible) {
1537 Log(LOGINFO, "Discarding raise on hidden window: %p[%p][%s]",
1538 gw, gw->w, gw->window_title);
1539 return;
1540 }
1541
1542 _GGDKDraw_CleanupAutoPaint(gw->display);
1543 gdk_window_raise(gw->w);
1544 if (!w->is_toplevel) {
1545 _GGDKDraw_FakeConfigureEvent(gw);
1546 }
1547 }
1548
1549 // Icon title is ignored.
1550 static void GGDKDrawSetWindowTitles8(GWindow w, const char *title, const char *UNUSED(icontitle)) {
1551 Log(LOGDEBUG, " ");// assert(false);
1552 GGDKWindow gw = (GGDKWindow)w;
1553 free(gw->window_title);
1554 gw->window_title = copy(title);
1555
1556 if (title != NULL && gw->is_toplevel) {
1557 gdk_window_set_title(gw->w, title);
1558 }
1559 }
1560
1561 // Sigh. GDK doesn't provide a way to get the window title...
1562 static char *GGDKDrawGetWindowTitle8(GWindow gw) {
1563 Log(LOGDEBUG, " ");
1564 return copy(((GGDKWindow)gw)->window_title);
1565 }
1566
1567 static void GGDKDrawSetTransientFor(GWindow transient, GWindow owner) {
1568 Log(LOGDEBUG, "transient=%p, owner=%p", transient, owner);
1569 GGDKWindow gw = (GGDKWindow) transient, ow = NULL;
1570 GGDKDisplay *gdisp = gw->display;
1571 assert(owner == NULL || gw->is_toplevel);
1572
1573 if (!gw->is_toplevel) {
1574 return; // Transient child windows?!
1575 }
1576
1577 if (owner == (GWindow) - 1) {
1578 for (GList_Glib *pw = gdisp->mru_windows->head; pw != NULL; pw = pw->next) {
1579 GGDKWindow tw = (GGDKWindow)pw->data;
1580 if (tw != gw && tw->is_visible) {
1581 ow = tw;
1582 break;
1583 }
1584 }
1585 } else if (owner == NULL) {
1586 ow = NULL; // Does this work with GDK?
1587 } else {
1588 ow = (GGDKWindow)owner;
1589 }
1590
1591 if (gw->transient_owner != NULL) {
1592 for (int i = ((int)gdisp->transient_stack->len) - 1; i >= 0; --i) {
1593 if (gw == (GGDKWindow)gdisp->transient_stack->pdata[i]) {
1594 g_ptr_array_remove_index(gw->display->transient_stack, i);
1595 if (gw->restrict_input_to_me) {
1596 gdisp->restrict_count--;
1597 }
1598 break;
1599 }
1600 }
1601 }
1602
1603 if (ow != NULL) {
1604 gdk_window_set_transient_for(gw->w, ow->w);
1605 gdk_window_set_modal_hint(gw->w, gw->restrict_input_to_me || gdisp->restrict_count > 0);
1606 gw->istransient = true;
1607 g_ptr_array_add(gdisp->transient_stack, gw);
1608 if (gw->restrict_input_to_me) {
1609 gdisp->restrict_count++;
1610 }
1611 } else {
1612 gdk_window_set_modal_hint(gw->w, false);
1613 gdk_window_set_transient_for(gw->w, gw->display->groot->w);
1614 gw->istransient = false;
1615 }
1616
1617 gw->transient_owner = ow;
1618 }
1619
1620 static void GGDKDrawGetPointerPosition(GWindow w, GEvent *ret) {
1621 Log(LOGDEBUG, " ");
1622 GGDKWindow gw = (GGDKWindow)w;
1623 GdkModifierType mask;
1624 int x, y;
1625
1626 GdkDevice *pointer = _GGDKDraw_GetPointer(gw->display);
1627 if (pointer == NULL) {
1628 ret->u.mouse.x = 0;
1629 ret->u.mouse.y = 0;
1630 ret->u.mouse.state = 0;
1631 return;
1632 }
1633
1634 gdk_window_get_device_position(gw->w, pointer, &x, &y, &mask);
1635 ret->u.mouse.x = x;
1636 ret->u.mouse.y = y;
1637 ret->u.mouse.state = _GGDKDraw_GdkModifierToKsm(mask);
1638 }
1639
1640 static GWindow GGDKDrawGetPointerWindow(GWindow gw) {
1641 Log(LOGDEBUG, " ");
1642 GdkDevice *pointer = _GGDKDraw_GetPointer(((GGDKWindow)gw)->display);
1643 GdkWindow *window = gdk_device_get_window_at_position(pointer, NULL, NULL);
1644
1645 if (window != NULL) {
1646 return (GWindow)g_object_get_data(G_OBJECT(window), "GGDKWindow");
1647 }
1648 return NULL;
1649 }
1650
1651 static void GGDKDrawSetCursor(GWindow w, GCursor gcursor) {
1652 Log(LOGDEBUG, " ");
1653 GGDKWindow gw = (GGDKWindow)w;
1654 GdkCursor *cursor = NULL;
1655
1656 switch (gcursor) {
1657 case ct_default:
1658 case ct_backpointer:
1659 case ct_pointer:
1660 cursor = gdk_cursor_new_from_name(gw->display->display, "default");
1661 break;
1662 case ct_hand:
1663 cursor = gdk_cursor_new_from_name(gw->display->display, "hand");
1664 break;
1665 case ct_question:
1666 cursor = gdk_cursor_new_from_name(gw->display->display, "help");
1667 break;
1668 case ct_cross:
1669 cursor = gdk_cursor_new_from_name(gw->display->display, "crosshair");
1670 break;
1671 case ct_4way:
1672 cursor = gdk_cursor_new_from_name(gw->display->display, "move");
1673 break;
1674 case ct_text:
1675 cursor = gdk_cursor_new_from_name(gw->display->display, "text");
1676 break;
1677 case ct_watch:
1678 cursor = gdk_cursor_new_from_name(gw->display->display, "wait");
1679 break;
1680 case ct_draganddrop:
1681 cursor = gdk_cursor_new_from_name(gw->display->display, "pointer");
1682 break;
1683 case ct_invisible:
1684 return; // There is no *good* reason to make the cursor invisible
1685 break;
1686 default:
1687 Log(LOGDEBUG, "CUSTOM CURSOR! %d", gcursor);
1688 }
1689
1690 if (gcursor >= ct_user) {
1691 GGDKDisplay *gdisp = gw->display;
1692 gcursor -= ct_user;
1693 if (gcursor < gdisp->cursors->len && gdisp->cursors->pdata[gcursor] != NULL) {
1694 gdk_window_set_cursor(gw->w, (GdkCursor *)gdisp->cursors->pdata[gcursor]);
1695 gw->current_cursor = gcursor;
1696 } else {
1697 Log(LOGWARN, "Invalid cursor value passed: %d", gcursor);
1698 }
1699 } else {
1700 gdk_window_set_cursor(gw->w, cursor);
1701 gw->current_cursor = gcursor;
1702 if (cursor != NULL) {
1703 g_object_unref(cursor);
1704 }
1705 }
1706 }
1707
1708 static GCursor GGDKDrawGetCursor(GWindow gw) {
1709 Log(LOGDEBUG, " ");
1710 return ((GGDKWindow)gw)->current_cursor;
1711 }
1712
1713 static void GGDKDrawTranslateCoordinates(GWindow from, GWindow to, GPoint *pt) {
1714 //Log(LOGDEBUG, " ");
1715 GGDKWindow gfrom = (GGDKWindow)from, gto = (GGDKWindow)to;
1716 GGDKDisplay *gdisp = gfrom->display;
1717
1718
1719 if (gto == gdisp->groot) {
1720 // The actual meaning of this command...
1721 int x, y;
1722 gdk_window_get_root_coords(gfrom->w, pt->x, pt->y, &x, &y);
1723 pt->x = x;
1724 pt->y = y;
1725 } else {
1726 GdkPoint from_root, to_root;
1727 gdk_window_get_origin(gfrom->w, &from_root.x, &from_root.y);
1728 gdk_window_get_origin(gto->w, &to_root.x, &to_root.y);
1729 pt->x = from_root.x - to_root.x + pt->x;
1730 pt->y = from_root.y - to_root.y + pt->y;
1731 }
1732 }
1733
1734
1735 static void GGDKDrawBeep(GDisplay *gdisp) {
1736 //Log(LOGDEBUG, " ");
1737 gdk_display_beep(((GGDKDisplay *)gdisp)->display);
1738 }
1739
1740 static void GGDKDrawScroll(GWindow w, GRect *rect, int32_t hor, int32_t vert) {
1741 //Log(LOGDEBUG, " ");
1742 GGDKWindow gw = (GGDKWindow) w;
1743 GRect temp;
1744
1745 vert = -vert;
1746 if (rect == NULL) {
1747 temp.x = temp.y = 0;
1748 temp.width = gw->pos.width;
1749 temp.height = gw->pos.height;
1750 rect = &temp;
1751 }
1752
1753 GDrawRequestExpose(w, rect, false);
1754 }
1755
1756
1757 static GIC *GGDKDrawCreateInputContext(GWindow UNUSED(gw), enum gic_style UNUSED(style)) {
1758 Log(LOGDEBUG, " ");
1759 return NULL;
1760 }
1761
1762 static void GGDKDrawSetGIC(GWindow UNUSED(gw), GIC *UNUSED(gic), int UNUSED(x), int UNUSED(y)) {
1763 Log(LOGDEBUG, " ");
1764 }
1765
1766 static int GGDKDrawKeyState(GWindow w, int keysym) {
1767 //Log(LOGDEBUG, " ");
1768 if (keysym != ' ') {
1769 Log(LOGWARN, "Cannot check state of unsupported character!");
1770 return 0;
1771 }
1772 // Since this function is only used to check the state of the space button
1773 // Don't bother with a full implementation...
1774 return ((GGDKWindow)w)->display->is_space_pressed;
1775 }
1776
1777 static void GGDKDrawGrabSelection(GWindow w, enum selnames sn) {
1778 Log(LOGDEBUG, " ");
1779
1780 if ((int)sn < 0 || sn >= sn_max) {
1781 return;
1782 }
1783
1784 GGDKWindow gw = (GGDKWindow)w;
1785 GGDKDisplay *gdisp = gw->display;
1786 GGDKSelectionInfo *sel = &gdisp->selinfo[sn];
1787
1788 // Send a SelectionClear event to the current owner of the selection
1789 if (sel->owner != NULL && sel->datalist != NULL) {
1790 GEvent e = {0};
1791 e.w = (GWindow)sel->owner;
1792 e.type = et_selclear;
1793 e.u.selclear.sel = sn;
1794 e.native_window = sel->owner->w;
1795 if (sel->owner->eh != NULL) {
1796 _GGDKDraw_CallEHChecked(sel->owner, &e, sel->owner->eh);
1797 }
1798 }
1799 _GGDKDraw_ClearSelData(gdisp, sn);
1800
1801 gdk_selection_owner_set(gw->w, sel->sel_atom, gdisp->last_event_time, false);
1802 sel->owner = gw;
1803 sel->timestamp = gdisp->last_event_time;
1804 }
1805
1806 static void GGDKDrawAddSelectionType(GWindow w, enum selnames sel, char *type, void *data, int32_t cnt, int32_t unitsize,
1807 void *gendata(void *, int32_t *len), void freedata(void *)) {
1808 Log(LOGDEBUG, " ");
1809
1810 GGDKWindow gw = (GGDKWindow)w;
1811 GGDKDisplay *gdisp = gw->display;
1812 GdkAtom type_atom = gdk_atom_intern(type, false);
1813 GGDKSelectionData *sd = NULL;
1814 GList_Glib *dl = gdisp->selinfo[sel].datalist;
1815
1816 if (unitsize != 1 && unitsize != 2 && unitsize != 4) {
1817 GDrawIError("Bad unitsize to GGDKDrawAddSelectionType");
1818 return;
1819 } else if ((int)sel < 0 || sel >= sn_max) {
1820 GDrawIError("Bad selname value");
1821 return;
1822 }
1823
1824 while (dl != NULL && (sd = ((GGDKSelectionData *)dl->data))->type_atom != type_atom) {
1825 dl = dl->next;
1826 }
1827
1828 if (dl == NULL) {
1829 sd = calloc(1, sizeof(GGDKSelectionData));
1830 sd->type_atom = type_atom;
1831 gdisp->selinfo[sel].datalist = g_list_append(gdisp->selinfo[sel].datalist, sd);
1832 }
1833
1834 sd->cnt = cnt;
1835 sd->data = data;
1836 sd->unit_size = unitsize;
1837 sd->gendata = gendata;
1838 sd->freedata = freedata;
1839
1840 #ifdef GDK_WINDOWING_QUARTZ
1841 if (sel == sn_clipboard && type_atom == gdk_atom_intern_static_string("UTF8_STRING")) {
1842 char *data = sd->data;
1843 if (sd->gendata) {
1844 int len;
1845 data = sd->gendata(sd->data, &len);
1846 }
1847 _GGDKDrawCocoa_SetClipboardText(data);
1848 if (sd->gendata) {
1849 free(data);
1850 }
1851 }
1852 #endif
1853 }
1854
1855 static void *GGDKDrawRequestSelection(GWindow w, enum selnames sn, char *typename, int32_t *len) {
1856 GGDKWindow gw = (GGDKWindow)w;
1857 GGDKDisplay *gdisp = gw->display;
1858 GdkAtom type_atom = gdk_atom_intern(typename, false);
1859 void *ret = NULL;
1860
1861 if (len != NULL) {
1862 *len = 0;
1863 }
1864
1865 if ((int)sn < 0 || sn >= sn_max || gw->is_waiting_for_selection || gw->is_dying) {
1866 return NULL;
1867 }
1868
1869 #ifdef GDK_WINDOWING_QUARTZ
1870 if (sn == sn_clipboard && type_atom == gdk_atom_intern_static_string("UTF8_STRING")) {
1871 ret = _GGDKDrawCocoa_GetClipboardText();
1872 if (ret && len) {
1873 *len = strlen(ret);
1874 }
1875 return ret;
1876 }
1877 #endif
1878
1879 // If we own the selection, get the data ourselves...
1880 if (gdisp->selinfo[sn].owner != NULL) {
1881 GList_Glib *ptr = gdisp->selinfo[sn].datalist;
1882 while (ptr != NULL) {
1883 GGDKSelectionData *sd = (GGDKSelectionData *)ptr->data;
1884 if (sd->type_atom == type_atom) {
1885 if (sd->gendata != NULL) {
1886 ret = (sd->gendata)(sd->data, len);
1887 if (len != NULL) {
1888 *len *= sd->unit_size;
1889 }
1890 } else {
1891 int sz = sd->unit_size * sd->cnt;
1892 ret = calloc(sz + 4, 1);
1893 if (ret) {
1894 memcpy(ret, sd->data, sz);
1895 if (len != NULL) {
1896 *len = sz;
1897 }
1898 }
1899 }
1900 return ret;
1901 }
1902 ptr = ptr->next;
1903 }
1904 }
1905
1906 #ifndef GDK_WINDOWING_QUARTZ
1907 // Otherwise we have to ask the owner for the data.
1908 gdk_selection_convert(gw->w, gdisp->selinfo[sn].sel_atom, type_atom, gdisp->last_event_time);
1909
1910 // On Windows, gdk_selection_convert is synchronous. Avoid the fluff of waiting
1911 // which also can cause segfault if a window gets destroyed in the meantime...
1912 #ifdef GDK_WINDOWING_X11
1913 gw->is_waiting_for_selection = true;
1914 gw->is_notified_of_selection = false;
1915
1916 GTimer_GTK *timer = g_timer_new();
1917 g_timer_start(timer);
1918
1919 while (gw->is_waiting_for_selection) {
1920 if (g_timer_elapsed(timer, NULL) >= (double)gdisp->sel_notify_timeout) {
1921 break;
1922 }
1923 GGDKDrawProcessPendingEvents((GDisplay *)gdisp);
1924 }
1925 g_timer_destroy(timer);
1926
1927 if (gw->is_notified_of_selection) {
1928 #endif
1929 guchar *data;
1930 GdkAtom received_type;
1931 gint received_format;
1932 gint rlen = gdk_selection_property_get(gw->w, &data, &received_type, &received_format);
1933 if (data != NULL) {
1934 ret = calloc(rlen + 4, 1);
1935 if (ret) {
1936 memcpy(ret, data, rlen);
1937 if (len) {
1938 *len = rlen;
1939 }
1940 }
1941 g_free(data);
1942 }
1943 #ifdef GDK_WINDOWING_X11
1944 }
1945
1946 gw->is_waiting_for_selection = false;
1947 gw->is_notified_of_selection = false;
1948 #endif // GDK_WINDOWING_X11
1949 #endif // GDK_WINDOWING_QUARTZ
1950
1951 return ret;
1952 }
1953
1954 static int GGDKDrawSelectionHasType(GWindow w, enum selnames sn, char *typename) {
1955 Log(LOGDEBUG, " ");
1956
1957 GGDKWindow gw = (GGDKWindow)w;
1958 if (gw->is_dying || gw->is_waiting_for_selection || (int)sn < 0 || sn >= sn_max) {
1959 return false;
1960 }
1961
1962 GGDKDisplay *gdisp = gw->display;
1963 GdkAtom sel_type = gdk_atom_intern(typename, false);
1964
1965 // Check if we own it
1966 if (gdisp->selinfo[sn].owner != NULL) {
1967 GList_Glib *ptr = gdisp->selinfo[sn].datalist;
1968 while (ptr != NULL) {
1969 if (((GGDKSelectionData *)ptr->data)->type_atom == sel_type) {
1970 return true;
1971 }
1972 ptr = ptr->next;
1973 }
1974 return false;
1975 }
1976
1977 #ifndef GDK_WINDOWING_QUARTZ
1978 // Else query
1979 if (gdisp->seltypes.timestamp != gdisp->last_event_time || gdisp->seltypes.sel_atom != gdisp->selinfo[sn].sel_atom) {
1980 free(gdisp->seltypes.types);
1981 gdisp->seltypes.types = NULL;
1982 gdisp->seltypes.sel_atom = gdisp->selinfo[sn].sel_atom;
1983 gdisp->seltypes.types = (GdkAtom *) GGDKDrawRequestSelection(w, sn, "TARGETS", &gdisp->seltypes.len);
1984 gdisp->seltypes.len /= sizeof(GdkAtom *);
1985 }
1986
1987 if (gdisp->seltypes.types != NULL) {
1988 for (int i = 0; i < gdisp->seltypes.len; i++) {
1989 if (gdisp->seltypes.types[i] == sel_type) {
1990 return true;
1991 }
1992 }
1993 }
1994 return false;
1995 #else
1996 return sel_type == gdk_atom_intern_static_string("UTF8_STRING");
1997 #endif
1998 }
1999
2000 static void GGDKDrawBindSelection(GDisplay *disp, enum selnames sn, char *atomname) {
2001 Log(LOGDEBUG, " ");
2002 GGDKDisplay *gdisp = (GGDKDisplay *) disp;
2003 if ((int)sn >= 0 && sn < sn_max) {
2004 gdisp->selinfo[sn].sel_atom = gdk_atom_intern(atomname, false);
2005 }
2006 }
2007
2008 static int GGDKDrawSelectionHasOwner(GDisplay *disp, enum selnames sn) {
2009 Log(LOGDEBUG, " ");
2010
2011 if ((int)sn < 0 || sn >= sn_max) {
2012 return false;
2013 }
2014
2015 GGDKDisplay *gdisp = (GGDKDisplay *)disp;
2016 // Check if we own it
2017 if (gdisp->selinfo[sn].owner != NULL) {
2018 return true;
2019 }
2020
2021 // Else query. Note, we cannot use gdk_selection_owner_get, as this only works for
2022 // windows that GDK created. This code is untested.
2023 void *data = GGDKDrawRequestSelection((GWindow)gdisp->groot, sn, "TIMESTAMP", NULL);
2024 if (data != NULL) {
2025 free(data);
2026 return true;
2027 }
2028 return false;
2029 }
2030
2031 static void GGDKDrawPointerUngrab(GDisplay *gdisp) {
2032 Log(LOGDEBUG, " ");
2033 #ifndef GGDKDRAW_GDK_3_20
2034 GdkDevice *pointer = _GGDKDraw_GetPointer((GGDKDisplay *)gdisp);
2035 if (pointer == NULL) {
2036 return;
2037 }
2038
2039 gdk_device_ungrab(pointer, GDK_CURRENT_TIME);
2040 #else
2041 GdkSeat *seat = gdk_display_get_default_seat(((GGDKDisplay *)gdisp)->display);
2042 if (seat == NULL) {
2043 return;
2044 }
2045
2046 gdk_seat_ungrab(seat);
2047 #endif
2048 }
2049
2050 static void GGDKDrawPointerGrab(GWindow w) {
2051 Log(LOGDEBUG, " ");
2052 GGDKWindow gw = (GGDKWindow)w;
2053 #ifndef GGDKDRAW_GDK_3_20
2054 GdkDevice *pointer = _GGDKDraw_GetPointer(gw->display);
2055 if (pointer == NULL) {
2056 return;
2057 }
2058
2059 gdk_device_grab(pointer, gw->w,
2060 GDK_OWNERSHIP_NONE,
2061 false,
2062 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_SCROLL_MASK,
2063 NULL, GDK_CURRENT_TIME);
2064 #else
2065 GdkSeat *seat = gdk_display_get_default_seat(gw->display->display);
2066 if (seat == NULL) {
2067 return;
2068 }
2069
2070 gdk_seat_grab(seat, gw->w,
2071 GDK_SEAT_CAPABILITY_ALL_POINTING,
2072 false, NULL, NULL, NULL, NULL);
2073 #endif
2074 }
2075
2076 static void GGDKDrawRequestExpose(GWindow w, GRect *rect, int UNUSED(doclear)) {
2077 Log(LOGDEBUG, "%p [%s]", w, ((GGDKWindow)w)->window_title);
2078
2079 GGDKWindow gw = (GGDKWindow) w;
2080 GdkRectangle clip;
2081
2082 if (!gw->is_visible || _GGDKDraw_WindowOrParentsDying(gw)) {
2083 return;
2084 }
2085 if (rect == NULL) {
2086 clip.x = clip.y = 0;
2087 clip.width = gw->pos.width;
2088 clip.height = gw->pos.height;
2089 } else {
2090 clip.x = rect->x;
2091 clip.y = rect->y;
2092 clip.width = rect->width;
2093 clip.height = rect->height;
2094
2095 if (rect->x < 0 || rect->y < 0 || rect->x + rect->width > gw->pos.width ||
2096 rect->y + rect->height > gw->pos.height) {
2097
2098 if (clip.x < 0) {
2099 clip.width += clip.x;
2100 clip.x = 0;
2101 }
2102 if (clip.y < 0) {
2103 clip.height += clip.y;
2104 clip.y = 0;
2105 }
2106 if (clip.x + clip.width > gw->pos.width) {
2107 clip.width = gw->pos.width - clip.x;
2108 }
2109 if (clip.y + clip.height > gw->pos.height) {
2110 clip.height = gw->pos.height - clip.y;
2111 }
2112 if (clip.height <= 0 || clip.width <= 0) {
2113 return;
2114 }
2115 }
2116 }
2117
2118 gdk_window_invalidate_rect(gw->w, &clip, false);
2119 }
2120
2121 static void GGDKDrawForceUpdate(GWindow gw) {
2122 //Log(LOGDEBUG, " ");
2123 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2124 gdk_window_process_updates(((GGDKWindow)gw)->w, true);
2125 G_GNUC_END_IGNORE_DEPRECATIONS
2126 }
2127
2128 static void GGDKDrawSync(GDisplay *gdisp) {
2129 //Log(LOGDEBUG, " ");
2130 gdk_display_sync(((GGDKDisplay *)gdisp)->display);
2131 }
2132
2133 static void GGDKDrawSkipMouseMoveEvents(GWindow UNUSED(gw), GEvent *UNUSED(gevent)) {
2134 //Log(LOGDEBUG, " ");
2135 // Not implemented, not needed.
2136 }
2137
2138 static void GGDKDrawProcessPendingEvents(GDisplay *gdisp) {
2139 //Log(LOGDEBUG, " ");
2140 GMainContext *ctx = g_main_loop_get_context(((GGDKDisplay *)gdisp)->main_loop);
2141 if (ctx != NULL) {
2142 _GGDKDraw_CleanupAutoPaint((GGDKDisplay *)gdisp);
2143 while (g_main_context_iteration(ctx, false));
2144 }
2145 }
2146
2147 static void GGDKDrawProcessOneEvent(GDisplay *gdisp) {
2148 //Log(LOGDEBUG, " ");
2149 GMainContext *ctx = g_main_loop_get_context(((GGDKDisplay *)gdisp)->main_loop);
2150 if (ctx != NULL) {
2151 _GGDKDraw_CleanupAutoPaint((GGDKDisplay *)gdisp);
2152 g_main_context_iteration(ctx, true);
2153 }
2154 }
2155
2156 static void GGDKDrawEventLoop(GDisplay *gdisp) {
2157 Log(LOGDEBUG, " ");
2158 GMainContext *ctx = g_main_loop_get_context(((GGDKDisplay *)gdisp)->main_loop);
2159 if (ctx != NULL) {
2160 _GGDKDraw_CleanupAutoPaint((GGDKDisplay *)gdisp);
2161 do {
2162 while (((GGDKDisplay *)gdisp)->top_window_count > 0) {
2163 g_main_context_iteration(ctx, true);
2164 if ((gdisp->err_flag) && (gdisp->err_report)) {
2165 GDrawIErrorRun("%s", gdisp->err_report);
2166 }
2167 if (gdisp->err_report) {
2168 free(gdisp->err_report);
2169 gdisp->err_report = NULL;
2170 }
2171 }
2172 GGDKDrawSync(gdisp);
2173 GGDKDrawProcessPendingEvents(gdisp);
2174 GGDKDrawSync(gdisp);
2175 } while (((GGDKDisplay *)gdisp)->top_window_count > 0 || g_main_context_pending(ctx));
2176 }
2177 }
2178
2179 static void GGDKDrawPostEvent(GEvent *e) {
2180 //Log(LOGDEBUG, " ");
2181 GGDKWindow gw = (GGDKWindow)(e->w);
2182 e->native_window = gw->w;
2183 _GGDKDraw_CallEHChecked(gw, e, gw->eh);
2184 }
2185
2186 static void GGDKDrawPostDragEvent(GWindow w, GEvent *mouse, enum event_type et) {
2187 Log(LOGDEBUG, " ");
2188 GGDKWindow gw = (GGDKWindow)w, cw;
2189 GGDKDisplay *gdisp = gw->display;
2190 int x, y;
2191
2192 // If the cursor hasn't moved much, don't bother to send a drag event
2193 x = abs(mouse->u.mouse.x - gdisp->last_dd.x);
2194 y = abs(mouse->u.mouse.y - gdisp->last_dd.y);
2195 if (x + y < 4 && et == et_drag) {
2196 return;
2197 }
2198
2199 cw = (GGDKWindow)GGDKDrawGetPointerWindow(w);
2200
2201 if (gdisp->last_dd.w != cw) {
2202 if (gdisp->last_dd.w != NULL) {
2203 GEvent e = {0};
2204 e.type = et_dragout;
2205 e.u.drag_drop.x = gdisp->last_dd.rx;
2206 e.u.drag_drop.y = gdisp->last_dd.ry;
2207 e.w = (GWindow)gdisp->last_dd.w;
2208 e.native_window = e.w->native_window;
2209
2210 _GGDKDraw_CallEHChecked(gdisp->last_dd.w, &e, gdisp->last_dd.w->eh);
2211 } else {
2212 /* Send foreign dragout message */
2213 }
2214 gdisp->last_dd.w = NULL;
2215 }
2216
2217 GEvent e = {0};
2218 // Are we still within the original window?
2219 if (cw == gw) {
2220 e.type = et;
2221 e.w = w;
2222 e.native_window = w->native_window;
2223 x = e.u.drag_drop.x = mouse->u.mouse.x;
2224 y = e.u.drag_drop.y = mouse->u.mouse.y;
2225 _GGDKDraw_CallEHChecked(gw, &e, gw->eh);
2226 } else if (cw != NULL) {
2227 GPoint pt = {.x = mouse->u.mouse.x, .y = mouse->u.mouse.y};
2228 GGDKDrawTranslateCoordinates(w, (GWindow)cw, &pt);
2229 x = pt.x;
2230 y = pt.y;
2231
2232 e.type = et;
2233 e.u.drag_drop.x = x;
2234 e.u.drag_drop.y = y;
2235 e.w = (GWindow)cw;
2236 e.native_window = cw->w;
2237 _GGDKDraw_CallEHChecked(cw, &e, cw->eh);
2238 } else {
2239 // Foreign window
2240 }
2241
2242 if (et != et_drop) {
2243 gdisp->last_dd.w = cw;
2244 gdisp->last_dd.x = mouse->u.mouse.x;
2245 gdisp->last_dd.y = mouse->u.mouse.y;
2246 gdisp->last_dd.rx = x;
2247 gdisp->last_dd.ry = y;
2248 } else {
2249 gdisp->last_dd.w = NULL;
2250 }
2251 }
2252
2253 static int GGDKDrawRequestDeviceEvents(GWindow w, int devcnt, struct gdeveventmask *de) {
2254 Log(LOGDEBUG, " ");
2255 return 0; //Not sure how to handle... For tablets...
2256 }
2257
2258 static int GGDKDrawShortcutKeyMatches(const GEvent *e, unichar_t ch) {
2259 if ((e->type != et_char && e->type != et_charup) || ch == 0) {
2260 return false;
2261 }
2262
2263 unichar_t k = toupper(e->u.chr.chars[0]);
2264 if (k == ch) {
2265 return true;
2266 }
2267
2268 GGDKWindow gw = (GGDKWindow)e->w;
2269 if (gw->display->ks.group == 0) {
2270 return false;
2271 }
2272
2273 GdkKeymap *km = gdk_keymap_get_for_display(gw->display->display);
2274 if (!km) {
2275 return false;
2276 }
2277
2278 guint keyval;
2279 if (!gdk_keymap_translate_keyboard_state(km, gw->display->ks.keycode,
2280 gw->display->ks.state, 0, &keyval, NULL, NULL, NULL)) {
2281 return false;
2282 }
2283
2284 k = toupper(gdk_keyval_to_unicode(keyval));
2285 return k == ch;
2286 }
2287
2288 static GTimer *GGDKDrawRequestTimer(GWindow w, int32_t time_from_now, int32_t frequency, void *userdata) {
2289 //Log(LOGDEBUG, " ");
2290 GGDKTimer *timer = calloc(1, sizeof(GGDKTimer));
2291 GGDKWindow gw = (GGDKWindow)w;
2292 if (timer == NULL) {
2293 return NULL;
2294 }
2295
2296 gw->display->timers = g_list_append(gw->display->timers, timer);
2297
2298 timer->owner = w;
2299 timer->repeat_time = frequency;
2300 timer->userdata = userdata;
2301 timer->active = true;
2302 timer->has_differing_repeat_time = (time_from_now != frequency);
2303 timer->glib_timeout_id = g_timeout_add(time_from_now, _GGDKDraw_ProcessTimerEvent, timer);
2304
2305 GGDKDRAW_ADDREF(timer);
2306 return (GTimer *)timer;
2307 }
2308
2309 static void GGDKDrawCancelTimer(GTimer *timer) {
2310 //Log(LOGDEBUG, " ");
2311 GGDKTimer *gtimer = (GGDKTimer *)timer;
2312 gtimer->active = false;
2313 GGDKDRAW_DECREF(gtimer, _GGDKDraw_OnTimerDestroyed);
2314 }
2315
2316 // Our function VTable
2317 static struct displayfuncs gdkfuncs = {
2318 GGDKDrawInit,
2319
2320 GGDKDrawSetDefaultIcon,
2321
2322 GGDKDrawCreateTopWindow,
2323 GGDKDrawCreateSubWindow,
2324 GGDKDrawCreatePixmap,
2325 GGDKDrawCreateBitmap,
2326 GGDKDrawCreateCursor,
2327 GGDKDrawDestroyWindow,
2328 GGDKDrawDestroyCursor,
2329 GGDKDrawNativeWindowExists, //Not sure what this is meant to do...
2330 GGDKDrawSetZoom,
2331 GGDKDrawSetWindowBackground,
2332 GGDKDrawSetDither,
2333
2334 GGDKDrawSetVisible,
2335 GGDKDrawMove,
2336 GGDKDrawTrueMove,
2337 GGDKDrawResize,
2338 GGDKDrawMoveResize,
2339 GGDKDrawRaise,
2340 GGDKDrawSetWindowTitles8,
2341 GGDKDrawGetWindowTitle8,
2342 GGDKDrawSetTransientFor,
2343 GGDKDrawGetPointerPosition,
2344 GGDKDrawGetPointerWindow,
2345 GGDKDrawSetCursor,
2346 GGDKDrawGetCursor,
2347 GGDKDrawTranslateCoordinates,
2348
2349 GGDKDrawBeep,
2350
2351 GGDKDrawPushClip,
2352 GGDKDrawPopClip,
2353
2354 GGDKDrawDrawLine,
2355 GGDKDrawDrawArrow,
2356 GGDKDrawDrawRect,
2357 GGDKDrawFillRect,
2358 GGDKDrawFillRoundRect,
2359 GGDKDrawDrawEllipse,
2360 GGDKDrawFillEllipse,
2361 GGDKDrawDrawArc,
2362 GGDKDrawDrawPoly,
2363 GGDKDrawFillPoly,
2364 GGDKDrawScroll,
2365
2366 GGDKDrawDrawImage,
2367 GGDKDrawDrawGlyph,
2368 GGDKDrawDrawImageMagnified,
2369 GGDKDrawDrawPixmap,
2370
2371 GGDKDrawCreateInputContext,
2372 GGDKDrawSetGIC,
2373 GGDKDrawKeyState,
2374
2375 GGDKDrawGrabSelection,
2376 GGDKDrawAddSelectionType,
2377 GGDKDrawRequestSelection,
2378 GGDKDrawSelectionHasType,
2379 GGDKDrawBindSelection,
2380 GGDKDrawSelectionHasOwner,
2381
2382 GGDKDrawPointerUngrab,
2383 GGDKDrawPointerGrab,
2384 GGDKDrawRequestExpose,
2385 GGDKDrawForceUpdate,
2386 GGDKDrawSync,
2387 GGDKDrawSkipMouseMoveEvents,
2388 GGDKDrawProcessPendingEvents,
2389 GGDKDrawProcessOneEvent,
2390 GGDKDrawEventLoop,
2391 GGDKDrawPostEvent,
2392 GGDKDrawPostDragEvent,
2393 GGDKDrawRequestDeviceEvents,
2394 GGDKDrawShortcutKeyMatches,
2395
2396 GGDKDrawRequestTimer,
2397 GGDKDrawCancelTimer,
2398
2399 GGDKDrawGetFontMetrics,
2400
2401 GGDKDrawPathStartNew,
2402 GGDKDrawPathClose,
2403 GGDKDrawPathMoveTo,
2404 GGDKDrawPathLineTo,
2405 GGDKDrawPathCurveTo,
2406 GGDKDrawPathStroke,
2407 GGDKDrawPathFill,
2408 GGDKDrawPathFillAndStroke, // Currently unused
2409
2410 GGDKDrawLayoutInit,
2411 GGDKDrawLayoutDraw,
2412 GGDKDrawLayoutIndexToPos,
2413 GGDKDrawLayoutXYToIndex,
2414 GGDKDrawLayoutExtents,
2415 GGDKDrawLayoutSetWidth,
2416 GGDKDrawLayoutLineCount,
2417 GGDKDrawLayoutLineStart,
2418 GGDKDrawStartNewSubPath,
2419 GGDKDrawFillRuleSetWinding,
2420
2421 GGDKDrawDoText8,
2422
2423 GGDKDrawPushClipOnly,
2424 GGDKDrawClipPreserve
2425 };
2426
2427 // Protected member functions (package-level)
2428
2429 GDisplay *_GGDKDraw_CreateDisplay(char *displayname, char *UNUSED(programname)) {
2430 GGDKDisplay *gdisp;
2431 GdkDisplay *display;
2432 GGDKWindow groot;
2433
2434 LogInit();
2435
2436 if (displayname == NULL) {
2437 display = gdk_display_get_default();
2438 } else {
2439 display = gdk_display_open(displayname);
2440 }
2441
2442 if (display == NULL) {
2443 return NULL;
2444 }
2445
2446 gdisp = (GGDKDisplay *)calloc(1, sizeof(GGDKDisplay));
2447 if (gdisp == NULL) {
2448 return NULL;
2449 }
2450
2451 // cursors.c creates ~41.
2452 gdisp->cursors = g_ptr_array_sized_new(50);
2453 gdisp->windows = g_hash_table_new(g_direct_hash, g_direct_equal);
2454 gdisp->mru_windows = g_queue_new();
2455 if (gdisp->windows == NULL || gdisp->mru_windows == NULL) {
2456 if (gdisp->windows) {
2457 g_hash_table_destroy(gdisp->windows);
2458 }
2459 free(gdisp);
2460 return NULL;
2461 }
2462 gdisp->transient_stack = g_ptr_array_sized_new(5);
2463
2464 gdisp->funcs = &gdkfuncs;
2465 gdisp->display = display;
2466 gdisp->screen = gdk_display_get_default_screen(display);
2467 gdisp->root = gdk_screen_get_root_window(gdisp->screen);
2468 gdisp->res = gdk_screen_get_resolution(gdisp->screen);
2469 gdisp->pangoc_context = gdk_pango_context_get_for_screen(gdisp->screen);
2470 if (gdisp->res <= 0) {
2471 gdisp->res = 96;
2472 }
2473 #ifdef GDK_WINDOWING_QUARTZ
2474 if (gdisp->res <= 72) {
2475 gdisp->res = 96;
2476 }
2477 #endif
2478
2479 gdisp->main_loop = g_main_loop_new(NULL, true);
2480 gdisp->bs.double_time = 200;
2481 gdisp->bs.double_wiggle = 3;
2482 gdisp->sel_notify_timeout = 2; // 2 second timeout
2483
2484 //Initialise selection atoms
2485 gdisp->selinfo[sn_primary].sel_atom = gdk_atom_intern_static_string("PRIMARY");
2486 gdisp->selinfo[sn_clipboard].sel_atom = gdk_atom_intern_static_string("CLIPBOARD");
2487 gdisp->selinfo[sn_drag_and_drop].sel_atom = gdk_atom_intern_static_string("DRAG_AND_DROP");
2488 gdisp->selinfo[sn_user1].sel_atom = gdk_atom_intern_static_string("PRIMARY");
2489 gdisp->selinfo[sn_user2].sel_atom = gdk_atom_intern_static_string("PRIMARY");
2490
2491 GDrawResourceFind();
2492 gdisp->bs.double_time = _GDraw_res_multiclicktime;
2493 gdisp->bs.double_wiggle = _GDraw_res_multiclickwiggle;
2494 gdisp->sel_notify_timeout = _GDraw_res_selnottime;
2495 gdisp->macosx_cmd = _GDraw_res_macosxcmd;
2496 gdisp->twobmouse_win = _GDraw_res_twobuttonfixup;
2497 if (_GDraw_res_res != 0) {
2498 gdisp->res = _GDraw_res_res;
2499 }
2500 if (_GDraw_res_synchronize){
2501 gdk_display_sync(gdisp->display);
2502 }
2503
2504 pango_cairo_context_set_resolution(gdisp->pangoc_context, gdisp->res);
2505
2506 groot = (GGDKWindow)calloc(1, sizeof(struct ggdkwindow));
2507 if (groot == NULL) {
2508 g_object_unref(gdisp->pangoc_context);
2509 g_queue_free(gdisp->mru_windows);
2510 g_hash_table_destroy(gdisp->windows);
2511 free(gdisp);
2512 return NULL;
2513 }
2514
2515 gdisp->groot = groot;
2516 groot->ggc = _GGDKDraw_NewGGC();
2517 groot->display = gdisp;
2518 groot->w = gdisp->root;
2519 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2520 groot->pos.width = gdk_screen_get_width(gdisp->screen);
2521 groot->pos.height = gdk_screen_get_height(gdisp->screen);
2522 G_GNUC_END_IGNORE_DEPRECATIONS
2523 groot->is_toplevel = true;
2524 groot->is_visible = true;
2525 g_object_set_data(G_OBJECT(gdisp->root), "GGDKWindow", groot);
2526
2527 (gdisp->funcs->init)((GDisplay *) gdisp);
2528
2529 gdk_event_handler_set(_GGDKDraw_DispatchEvent, (gpointer)gdisp, NULL);
2530
2531 _GDraw_InitError((GDisplay *) gdisp);
2532
2533 //DEBUG
2534 if (getenv("GGDK_DEBUG")) {
2535 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
2536 gdk_window_set_debug_updates(true);
2537 G_GNUC_END_IGNORE_DEPRECATIONS
2538 }
2539 return (GDisplay *)gdisp;
2540 }
2541
2542 void _GGDKDraw_DestroyDisplay(GDisplay *disp) {
2543 GGDKDisplay *gdisp = (GGDKDisplay *)(disp);
2544 guint window_count = g_hash_table_size(gdisp->windows);
2545
2546 // Indicate we're dying...
2547 gdisp->is_dying = true;
2548
2549 // Destroy remaining windows
2550 if (window_count > 0) {
2551 Log(LOGINFO, "Windows left allocated - forcibly freeing!");
2552
2553 // Note that windows are destroyed in random order. When a window is
2554 // destroyed, it decrements its parent reference count, so we need a
2555 // large enough count to keep it around until we're ready to destroy it
2556 GHashTableIter iter;
2557 GGDKWindow gw;
2558 g_hash_table_iter_init(&iter, gdisp->windows);
2559 while (g_hash_table_iter_next(&iter, (void **)&gw, NULL)) {
2560 gw->reference_count = window_count + 2;
2561 }
2562 while (g_hash_table_size(gdisp->windows) > 0) {
2563 g_hash_table_iter_init(&iter, gdisp->windows);
2564 if (g_hash_table_iter_next(&iter, (void **)&gw, NULL)) {
2565 Log(LOGINFO, "Forcibly destroying window (%p:%s)", gw, gw->window_title);
2566 assert(gw->reference_count >= 2);
2567 GGDKDrawDestroyWindow((GWindow)gw);
2568 _GGDKDraw_OnWindowDestroyed(gw);
2569 }
2570 }
2571 }
2572
2573 // Destroy root window
2574 _GGDKDraw_OnWindowDestroyed(gdisp->groot);
2575 gdisp->groot = NULL;
2576
2577 // Destroy the mru window list
2578 g_queue_free(gdisp->mru_windows);
2579 gdisp->mru_windows = NULL;
2580
2581 // Finally destroy the window table
2582 g_hash_table_destroy(gdisp->windows);
2583 gdisp->windows = NULL;
2584
2585 g_ptr_array_free(gdisp->transient_stack, true);
2586
2587 // Destroy held selection types, if any
2588 free(gdisp->seltypes.types);
2589 gdisp->seltypes.types = NULL;
2590
2591 // Destroy cursors
2592 for (guint i = 0; i < gdisp->cursors->len; i++) {
2593 if (gdisp->cursors->pdata[i] != NULL) {
2594 GGDKDrawDestroyCursor((GDisplay *)gdisp, (GCursor)(ct_user + i));
2595 }
2596 }
2597 g_ptr_array_free(gdisp->cursors, true);
2598 gdisp->cursors = NULL;
2599
2600 // Destroy any orphaned timers (???)
2601 if (gdisp->timers != NULL) {
2602 Log(LOGWARN, "Orphaned timers present - forcibly freeing!");
2603 while (gdisp->timers != NULL) {
2604 GGDKTimer *timer = (GGDKTimer *)gdisp->timers->data;
2605 timer->reference_count = 1;
2606 GGDKDrawCancelTimer((GTimer *)timer);
2607 }
2608 }
2609
2610 // Get rid of our pango context
2611 g_object_unref(gdisp->pangoc_context);
2612 gdisp->pangoc_context = NULL;
2613
2614 // Destroy the fontstate
2615 free(gdisp->fontstate);
2616 gdisp->fontstate = NULL;
2617
2618 // Close the display
2619 if (gdisp->display != NULL) {
2620 gdisp->display = NULL;
2621 }
2622
2623 // Unreference the main loop
2624 g_main_loop_unref(gdisp->main_loop);
2625 gdisp->main_loop = NULL;
2626
2627 // Free the data structure
2628 free(gdisp);
2629 return;
2630 }
2631