/
cog-platform-gtk4.c
827 lines (687 loc) · 28.3 KB
/
cog-platform-gtk4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
* cog-platform-gtk4.c
* Copyright (C) 2021-2022 Igalia S.L.
*
* SPDX-License-Identifier: MIT
*/
#include "../../core/cog.h"
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <wpe/fdo-egl.h>
#include <wpe/fdo.h>
#if COG_HAVE_LIBPORTAL
# include <libportal-gtk4/portal-gtk4.h>
#endif /* COG_HAVE_LIBPORTAL */
#if COG_HAVE_LIBPORTAL
# include "../common/cog-file-chooser.h"
#endif /* COG_HAVE_LIBPORTAL */
#include "../common/cog-cursors.h"
#include "../common/cog-gl-utils.h"
#include "cog-gtk-settings-dialog.h"
#define DEFAULT_WIDTH 1280
#define DEFAULT_HEIGHT 720
struct _CogGtk4PlatformClass {
CogPlatformClass parent_class;
};
struct _CogGtk4Platform {
CogPlatform parent;
};
G_DECLARE_FINAL_TYPE(CogGtk4Platform, cog_gtk4_platform, COG, GTK4_PLATFORM, CogPlatform)
G_DEFINE_DYNAMIC_TYPE_EXTENDED(
CogGtk4Platform,
cog_gtk4_platform,
COG_TYPE_PLATFORM,
0,
g_io_extension_point_implement(COG_MODULES_PLATFORM_EXTENSION_POINT, g_define_type_id, "gtk4", 400);)
/*
* TODO:
* - Multiple views.
* - Call cog_gl_renderer_finalize() when GL area is being destroyed.
* - Implement an actual widget that wraps exported EGLImages into a
* GdkTexture and uses gtk_snapshot_append_texture() to participate in
* the GTK scene graph directly and avoid CogGLRenderer.
*/
struct platform_window {
WebKitWebView* web_view;
GtkWidget* gtk_window;
GtkWidget* gl_drawing_area;
GtkWidget* back_button;
GtkWidget* forward_button;
GtkWidget* url_entry;
GtkWidget* popover_menu;
GtkWidget* settings_dialog;
CogGLRenderer gl_render;
int width;
int height;
bool is_fullscreen;
bool waiting_fullscreen_notify;
double device_scale_factor;
GdkModifierType key_modifiers;
struct wpe_view_backend_exportable_fdo* exportable;
WebKitWebViewBackend* view_backend;
struct wpe_fdo_egl_exported_image* current_image;
struct wpe_fdo_egl_exported_image* commited_image;
};
static struct platform_window win = {
.width = DEFAULT_WIDTH,
.height = DEFAULT_HEIGHT,
.device_scale_factor = 1,
};
static char *
get_extensions(void)
{
if (epoxy_gl_version() < 30) {
return g_strdup((const char *) glGetString(GL_EXTENSIONS));
} else {
GLint i, num_extensions = 0;
g_autoptr(GStrvBuilder) extensions = g_strv_builder_new();
g_auto(GStrv) strv = NULL;
glGetIntegerv(GL_NUM_EXTENSIONS, &num_extensions);
if (num_extensions == 0)
return NULL;
for (i = 0; i < num_extensions; i++)
g_strv_builder_add(extensions, (const char *) glGetStringi(GL_EXTENSIONS, i));
strv = g_strv_builder_end(extensions);
return g_strjoinv(" ", strv);
}
}
static bool
setup_shader(struct platform_window* window, GError** error)
{
char *extensions = NULL;
g_assert_nonnull(window);
gtk_gl_area_make_current(GTK_GL_AREA(window->gl_drawing_area));
g_debug("GL vendor: %s", glGetString(GL_VENDOR));
g_debug("GL renderer: %s", glGetString(GL_RENDERER));
g_debug("GL extensions: %s", (extensions = get_extensions(), extensions));
g_debug("GL version: %s", glGetString(GL_VERSION));
g_debug("GLSL version: %s", glGetString(GL_SHADING_LANGUAGE_VERSION));
g_free(extensions);
/* if the GtkGLArea is in an error state we don't do anything */
if (gtk_gl_area_get_error(GTK_GL_AREA(window->gl_drawing_area)) != NULL) {
if (error)
*error = g_error_copy(gtk_gl_area_get_error(GTK_GL_AREA(window->gl_drawing_area)));
return false;
}
if (!cog_gl_renderer_initialize(&window->gl_render, error))
return false;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* Configuration for the FDO backend. */
EGLDisplay display = eglGetCurrentDisplay();
g_assert(display != EGL_NO_DISPLAY);
EGLint egl_major = 0, egl_minor = 0;
if (eglInitialize(display, &egl_major, &egl_minor) == EGL_FALSE) {
g_set_error_literal(error, COG_PLATFORM_EGL_ERROR, eglGetError(),
"Cannot initialize EGL");
return false;
}
g_debug("EGL %i.%i successfully initialized.", egl_major, egl_minor);
wpe_fdo_initialize_for_egl_display(display);
return true;
}
static void
enter_monitor(GdkSurface *surface, GdkMonitor *monitor, gpointer user_data)
{
struct platform_window *win = user_data;
wpe_view_backend_set_target_refresh_rate(wpe_view_backend_exportable_fdo_get_view_backend(win->exportable),
gdk_monitor_get_refresh_rate(monitor));
}
static void
realize(GtkWidget *widget, gpointer user_data)
{
struct platform_window *win = user_data;
g_autoptr(GError) error = NULL;
if (!setup_shader(win, &error)) {
g_warning("Shader setup failed: %s", error->message);
g_application_quit(g_application_get_default());
}
g_signal_connect(gtk_native_get_surface(gtk_widget_get_native(win->gtk_window)), "enter-monitor",
G_CALLBACK(enter_monitor), user_data);
}
static gboolean
render(GtkGLArea *area, GdkGLContext *context, gpointer user_data)
{
struct platform_window *win = user_data;
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, win->width * win->device_scale_factor, win->height * win->device_scale_factor);
if (win->commited_image)
wpe_view_backend_exportable_fdo_egl_dispatch_release_exported_image(
win->exportable, win->commited_image);
if (!win->current_image) {
gtk_gl_area_queue_render(GTK_GL_AREA(win->gl_drawing_area));
return TRUE;
}
cog_gl_renderer_paint(&win->gl_render, wpe_fdo_egl_exported_image_get_egl_image(win->current_image),
COG_GL_RENDERER_ROTATION_0);
win->commited_image = win->current_image;
wpe_view_backend_exportable_fdo_dispatch_frame_complete(win->exportable);
return TRUE;
}
static void
resize(GtkGLArea* area, int width, int height, gpointer user_data)
{
struct platform_window* win = user_data;
win->width = width / win->device_scale_factor;
win->height = height / win->device_scale_factor;
wpe_view_backend_dispatch_set_size(
wpe_view_backend_exportable_fdo_get_view_backend(win->exportable),
win->width, win->height);
}
static void
scale_factor_change(GtkWidget *area, GParamSpec *pspec, gpointer user_data)
{
struct platform_window *win = user_data;
win->device_scale_factor = gtk_widget_get_scale_factor(area);
wpe_view_backend_dispatch_set_device_scale_factor(wpe_view_backend_exportable_fdo_get_view_backend(win->exportable),
win->device_scale_factor);
}
static void
dispatch_wpe_fullscreen_event(struct platform_window* win)
{
struct wpe_view_backend* backend = webkit_web_view_backend_get_wpe_backend(win->view_backend);
if (win->is_fullscreen)
wpe_view_backend_dispatch_did_enter_fullscreen(backend);
else
wpe_view_backend_dispatch_did_exit_fullscreen(backend);
}
static void
on_fullscreen_change(GtkWidget* window, GParamSpec* pspec, gpointer user_data)
{
struct platform_window* win = user_data;
bool was_fullscreen_requested_from_dom = win->waiting_fullscreen_notify;
win->waiting_fullscreen_notify = false;
win->is_fullscreen = gtk_window_is_fullscreen(GTK_WINDOW(window));
if (!win->is_fullscreen && !was_fullscreen_requested_from_dom)
wpe_view_backend_dispatch_request_exit_fullscreen(webkit_web_view_backend_get_wpe_backend(win->view_backend));
else if (was_fullscreen_requested_from_dom)
dispatch_wpe_fullscreen_event(win);
}
static void
on_quit(GtkWidget* widget, gpointer data)
{
if (g_application_get_default())
g_application_quit(g_application_get_default());
}
static void
on_click_pressed(GtkGestureClick* gesture, int n_press, double x,
double y, gpointer user_data)
{
struct platform_window* win = user_data;
int scale_factor = gtk_widget_get_scale_factor(GTK_WIDGET(win->gl_drawing_area));
gtk_widget_grab_focus(win->gl_drawing_area);
struct wpe_input_pointer_event wpe_event = {
.type = wpe_input_pointer_event_type_button,
.x = (int) (x * scale_factor),
.y = (int) (y * scale_factor),
.modifiers = wpe_input_pointer_modifier_button1,
.button = 1,
.state = 1,
};
wpe_view_backend_dispatch_pointer_event(
wpe_view_backend_exportable_fdo_get_view_backend(win->exportable),
&wpe_event);
}
static void
on_click_released(GtkGestureClick* gesture, int n_press, double x,
double y, gpointer user_data)
{
struct platform_window* win = user_data;
int scale_factor = gtk_widget_get_scale_factor(GTK_WIDGET(win->gl_drawing_area));
struct wpe_input_pointer_event wpe_event = {
.type = wpe_input_pointer_event_type_button,
.x = (int) (x * scale_factor),
.y = (int) (y * scale_factor),
.modifiers = wpe_input_pointer_modifier_button1,
.button = 1,
.state = 0,
};
wpe_view_backend_dispatch_pointer_event(
wpe_view_backend_exportable_fdo_get_view_backend(win->exportable),
&wpe_event);
}
static void
on_motion(GtkEventControllerMotion* controller, double x, double y,
gpointer user_data)
{
struct platform_window* win = user_data;
int scale_factor = gtk_widget_get_scale_factor(GTK_WIDGET(win->gl_drawing_area));
struct wpe_input_pointer_event wpe_event = {
.type = wpe_input_pointer_event_type_motion,
.x = (int) (x * scale_factor),
.y = (int) (y * scale_factor),
};
wpe_view_backend_dispatch_pointer_event(
wpe_view_backend_exportable_fdo_get_view_backend(win->exportable),
&wpe_event);
}
static gboolean
on_scroll(GtkEventControllerScroll* controller, double dx,
double dy, gpointer user_data)
{
struct platform_window* win = user_data;
struct wpe_input_axis_event axis_event = {
.type = wpe_input_axis_event_type_mask_2d | wpe_input_axis_event_type_motion_smooth,
.axis = dx ? 0 : 1,
.value = dx ? -dx * 100 : -dy * 100,
};
struct wpe_input_axis_2d_event event2d = {
.base = axis_event,
.x_axis = dx ? -dx * 100 : 0,
.y_axis = dx ? 0 : -dy * 100,
};
wpe_view_backend_dispatch_axis_event(
wpe_view_backend_exportable_fdo_get_view_backend(win->exportable),
&event2d.base);
return TRUE;
}
static gboolean
dispatch_key_event(struct platform_window* win, guint keycode, guint hardware_keycode, gboolean pressed, GdkModifierType state)
{
uint32_t modifiers = 0;
state |= win->key_modifiers;
if (state & GDK_CONTROL_MASK)
modifiers |= wpe_input_keyboard_modifier_control;
if (state & GDK_ALT_MASK)
modifiers |= wpe_input_keyboard_modifier_alt;
if (state & GDK_SHIFT_MASK)
modifiers |= wpe_input_keyboard_modifier_shift;
struct wpe_input_keyboard_event wpe_event = {
.key_code = keycode,
.hardware_key_code = hardware_keycode,
.pressed = pressed,
.modifiers = modifiers,
};
cog_view_handle_key_event(COG_VIEW(win->web_view), &wpe_event);
return TRUE;
}
static gboolean
on_key_pressed(GtkEventControllerKey* controller,
guint keycode,
guint hardware_keycode,
GdkModifierType state,
gpointer user_data)
{
struct platform_window* win = user_data;
return dispatch_key_event(win, keycode, hardware_keycode, TRUE, state);
}
static gboolean
on_key_released(GtkEventControllerKey* controller,
guint keycode,
guint hardware_keycode,
GdkModifierType state,
gpointer user_data)
{
struct platform_window* win = user_data;
return dispatch_key_event(win, keycode, hardware_keycode, FALSE, state);
}
static gboolean
on_key_modifiers(GtkEventControllerKey* controller,
GdkModifierType modifiers,
gpointer user_data)
{
struct platform_window* win = user_data;
win->key_modifiers = modifiers;
return TRUE;
}
static void
on_back_clicked(GtkWidget* widget, gpointer user_data)
{
struct platform_window* win = user_data;
webkit_web_view_go_back(win->web_view);
}
static void
on_forward_clicked(GtkWidget* widget, gpointer user_data)
{
struct platform_window* win = user_data;
webkit_web_view_go_forward(win->web_view);
}
static void
on_refresh_clicked(GtkWidget* widget, gpointer user_data)
{
struct platform_window* win = user_data;
webkit_web_view_reload(win->web_view);
}
static void
on_entry_activated(GtkWidget* widget, gpointer user_data)
{
struct platform_window *win = user_data;
GtkEntryBuffer *buffer = gtk_entry_get_buffer(GTK_ENTRY(win->url_entry));
const gchar *user_input = gtk_entry_buffer_get_text(buffer);
g_autoptr(GError) error = NULL;
g_autofree gchar *uri = cog_uri_guess_from_user_input(user_input, FALSE, &error);
if (error) {
g_warning("Failed to parse user input \"%s\": %s", user_input, error->message);
return;
}
webkit_web_view_load_uri(win->web_view, uri);
}
static gboolean
action_quit(GtkWidget* widget, GVariant* args,
gpointer user_data)
{
if (g_application_get_default())
g_application_quit(g_application_get_default());
return TRUE;
}
static gboolean
action_activate_entry(GtkWidget* widget, GVariant* args,
gpointer user_data)
{
struct platform_window* win = user_data;
gtk_widget_grab_focus(win->url_entry);
return TRUE;
}
static bool
on_dom_fullscreen_request(void* unused, bool fullscreen)
{
if (win.waiting_fullscreen_notify)
return false;
if (fullscreen == win.is_fullscreen) {
// Handle situations where DOM fullscreen requests are mixed with system fullscreen commands (e.g F11)
dispatch_wpe_fullscreen_event(&win);
return true;
}
win.waiting_fullscreen_notify = true;
if (fullscreen)
gtk_window_fullscreen(GTK_WINDOW(win.gtk_window));
else
gtk_window_unfullscreen(GTK_WINDOW(win.gtk_window));
return true;
}
static gboolean
action_open_settings(GtkWidget* widget, GVariant* args,
gpointer user_data)
{
struct platform_window* win = user_data;
if (win->settings_dialog) {
gtk_window_present(GTK_WINDOW(win->settings_dialog));
return TRUE;
}
win->settings_dialog = browser_settings_dialog_new(webkit_web_view_get_settings(win->web_view));
gtk_window_set_transient_for(GTK_WINDOW(win->settings_dialog), GTK_WINDOW(win->gtk_window));
g_object_add_weak_pointer(G_OBJECT(win->settings_dialog), (gpointer*)&win->settings_dialog);
gtk_window_present(GTK_WINDOW(win->settings_dialog));
return TRUE;
}
static void
setup_window(struct platform_window* window)
{
g_assert_nonnull(window);
window->gtk_window = gtk_window_new();
g_object_set(window->gtk_window, "default-width", DEFAULT_WIDTH,
"default-height", DEFAULT_HEIGHT, NULL);
g_signal_connect(window->gtk_window, "destroy", G_CALLBACK(on_quit), NULL);
GtkWidget* header_bar = gtk_header_bar_new();
GtkWidget* left_stack = gtk_stack_new();
GtkWidget* buttons_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
window->back_button = gtk_button_new_from_icon_name("go-previous-symbolic");
g_signal_connect(window->back_button, "clicked", G_CALLBACK(on_back_clicked), window);
gtk_box_append(GTK_BOX(buttons_box), window->back_button);
window->forward_button = gtk_button_new_from_icon_name("go-next-symbolic");
g_signal_connect(window->forward_button, "clicked",
G_CALLBACK(on_forward_clicked), window);
gtk_box_append(GTK_BOX(buttons_box), window->forward_button);
GtkWidget* refresh_button = gtk_button_new_from_icon_name("view-refresh-symbolic");
g_signal_connect(refresh_button, "clicked", G_CALLBACK(on_refresh_clicked), window);
gtk_box_append(GTK_BOX(buttons_box), refresh_button);
gtk_stack_add_named(GTK_STACK(left_stack), buttons_box, "buttons");
gtk_header_bar_pack_start(GTK_HEADER_BAR(header_bar), left_stack);
GtkWidget* title_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_valign(title_box, GTK_ALIGN_CENTER);
gtk_widget_set_hexpand(title_box, TRUE);
window->url_entry = gtk_entry_new();
g_signal_connect(window->url_entry, "activate", G_CALLBACK(on_entry_activated), window);
gtk_widget_set_parent(window->url_entry, title_box);
GtkWidget* right_stack = gtk_stack_new();
g_autoptr(GSimpleActionGroup) action_group = g_simple_action_group_new();
g_autoptr(GMenu) menu = g_menu_new();
g_autoptr(GSimpleAction) action = g_simple_action_new("open-settings", NULL);
g_signal_connect(action, "activate", G_CALLBACK(action_open_settings), window);
g_action_map_add_action(G_ACTION_MAP(action_group), G_ACTION(action));
g_autoptr(GMenuItem) menuItem = g_menu_item_new("Settings", "win.open-settings");
g_menu_append_item(menu, menuItem);
window->popover_menu = gtk_popover_menu_new_from_model(G_MENU_MODEL(menu));
gtk_widget_insert_action_group(window->popover_menu, "win", G_ACTION_GROUP(action_group));
GtkWidget* right_buttons_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
GtkWidget* button = gtk_menu_button_new();
gtk_menu_button_set_icon_name(GTK_MENU_BUTTON(button), "open-menu-symbolic");
gtk_box_append(GTK_BOX(right_buttons_box), button);
gtk_stack_add_named(GTK_STACK(right_stack), right_buttons_box, "right_buttons");
gtk_menu_button_set_popover(GTK_MENU_BUTTON(button), window->popover_menu);
gtk_header_bar_pack_end(GTK_HEADER_BAR(header_bar), right_stack);
gtk_header_bar_set_title_widget(GTK_HEADER_BAR(header_bar), title_box);
gtk_header_bar_set_show_title_buttons(GTK_HEADER_BAR(header_bar), TRUE);
gtk_window_set_titlebar(GTK_WINDOW(window->gtk_window), header_bar);
window->gl_drawing_area = gtk_gl_area_new();
gtk_widget_set_hexpand(window->gl_drawing_area, TRUE);
gtk_widget_set_vexpand(window->gl_drawing_area, TRUE);
gtk_widget_set_can_focus(window->gl_drawing_area, TRUE);
gtk_widget_set_sensitive(window->gl_drawing_area, TRUE);
gtk_widget_set_focusable(window->gl_drawing_area, TRUE);
gtk_widget_set_focus_on_click(window->gl_drawing_area, TRUE);
g_object_set(window->gl_drawing_area, "use-es", TRUE, NULL);
g_signal_connect(window->gl_drawing_area, "realize", G_CALLBACK(realize), window);
g_signal_connect(window->gl_drawing_area, "render", G_CALLBACK(render), window);
g_signal_connect(window->gl_drawing_area, "resize", G_CALLBACK(resize), window);
g_signal_connect(window->gl_drawing_area, "notify::scale-factor", G_CALLBACK(scale_factor_change), window);
g_signal_connect(window->gtk_window, "notify::fullscreened", G_CALLBACK(on_fullscreen_change), window);
GtkGesture* press = gtk_gesture_click_new();
gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(press), GDK_BUTTON_PRIMARY);
g_signal_connect(press, "pressed", G_CALLBACK(on_click_pressed), window);
g_signal_connect(press, "released", G_CALLBACK(on_click_released), window);
gtk_widget_add_controller(window->gl_drawing_area, GTK_EVENT_CONTROLLER(press));
GtkEventController* motion_controller = gtk_event_controller_motion_new();
GtkEventControllerMotion* motion = GTK_EVENT_CONTROLLER_MOTION(motion_controller);
g_signal_connect(motion, "motion", G_CALLBACK(on_motion), window);
gtk_widget_add_controller(window->gl_drawing_area, motion_controller);
GtkEventController* scroll_controller = gtk_event_controller_scroll_new(GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES);
GtkEventControllerScroll* scroll = GTK_EVENT_CONTROLLER_SCROLL(scroll_controller);
g_signal_connect(scroll, "scroll", G_CALLBACK(on_scroll), window);
gtk_widget_add_controller(window->gl_drawing_area, scroll_controller);
GtkEventController* key_controller = gtk_event_controller_key_new();
GtkEventControllerKey* key = GTK_EVENT_CONTROLLER_KEY(key_controller);
g_signal_connect(key, "key-pressed", G_CALLBACK(on_key_pressed), window);
g_signal_connect(key, "key-released", G_CALLBACK(on_key_released), window);
g_signal_connect(key, "modifiers", G_CALLBACK(on_key_modifiers), window);
gtk_widget_add_controller(window->gtk_window, key_controller);
// FIXME: Surely there is a simpler way to add shortcuts?
GtkEventController* shortcut_controller = gtk_shortcut_controller_new();
GtkShortcutController* shortcut = GTK_SHORTCUT_CONTROLLER(shortcut_controller);
GtkShortcutAction* quit_action = gtk_callback_action_new(action_quit, NULL, NULL);
GtkShortcutTrigger* quit_trigger = gtk_shortcut_trigger_parse_string("<Control>q");
GtkShortcut* quit_shortcut = gtk_shortcut_new(quit_trigger, quit_action);
gtk_shortcut_controller_add_shortcut(shortcut, quit_shortcut);
GtkShortcutAction* activate_entry_action = gtk_callback_action_new(action_activate_entry, window, NULL);
GtkShortcutTrigger* activate_entry_trigger = gtk_shortcut_trigger_parse_string("<Control>l");
GtkShortcut* activate_entry_shortcut = gtk_shortcut_new(activate_entry_trigger, activate_entry_action);
gtk_shortcut_controller_add_shortcut(shortcut, activate_entry_shortcut);
GtkShortcutAction* open_settings_action = gtk_callback_action_new(action_open_settings, window, NULL);
GtkShortcutTrigger* open_settings_trigger = gtk_shortcut_trigger_parse_string("<Control>s");
GtkShortcut* open_settings_shortcut = gtk_shortcut_new(open_settings_trigger, open_settings_action);
gtk_shortcut_controller_add_shortcut(shortcut, open_settings_shortcut);
gtk_widget_add_controller(window->gtk_window, shortcut_controller);
GtkWidget* box = gtk_box_new(GTK_ORIENTATION_VERTICAL, FALSE);
gtk_window_set_child(GTK_WINDOW(window->gtk_window), box);
gtk_box_append(GTK_BOX(box), window->gl_drawing_area);
gtk_window_present(GTK_WINDOW(window->gtk_window));
}
static void
on_export_egl_image(void* userdata,
struct wpe_fdo_egl_exported_image* image)
{
struct platform_window* window = userdata;
window->current_image = image;
gtk_gl_area_queue_render(GTK_GL_AREA(window->gl_drawing_area));
}
static void
setup_fdo_exportable(struct platform_window* window)
{
g_assert_nonnull(window);
static const struct wpe_view_backend_exportable_fdo_egl_client client = {
.export_fdo_egl_image = on_export_egl_image,
};
window->exportable = wpe_view_backend_exportable_fdo_egl_create(
&client, window, (uint32_t)DEFAULT_WIDTH, (uint32_t)DEFAULT_HEIGHT);
g_assert_nonnull(window->exportable);
window->view_backend = webkit_web_view_backend_new(
wpe_view_backend_exportable_fdo_get_view_backend(window->exportable),
(GDestroyNotify)wpe_view_backend_exportable_fdo_destroy,
window->exportable);
g_assert_nonnull(window->view_backend);
}
static void
shell_device_factor_changed(CogShell *shell, GParamSpec *param_spec, gpointer data)
{
struct platform_window *window = (struct platform_window *) data;
window->device_scale_factor = cog_shell_get_device_scale_factor(shell);
}
static struct wpe_view_backend *
gamepad_provider_get_view_backend_for_gamepad(void *provider G_GNUC_UNUSED, void *gamepad G_GNUC_UNUSED)
{
return wpe_view_backend_exportable_fdo_get_view_backend(win.exportable);
}
static gboolean
cog_gtk4_platform_setup(CogPlatform *platform, CogShell *shell, const char *params, GError **error)
{
g_assert_nonnull(platform);
wpe_loader_init("libWPEBackend-fdo-1.0.so");
if (!gtk_init_check()) {
g_set_error_literal(error, COG_PLATFORM_EGL_ERROR, 0,
"GTK initialization failed");
return FALSE;
}
g_signal_connect(shell, "notify::device-scale-factor", G_CALLBACK(shell_device_factor_changed), &win);
setup_window(&win);
setup_fdo_exportable(&win);
cog_gamepad_setup(gamepad_provider_get_view_backend_for_gamepad);
wpe_view_backend_set_fullscreen_handler(webkit_web_view_backend_get_wpe_backend(win.view_backend),
on_dom_fullscreen_request, NULL);
return TRUE;
}
static WebKitWebViewBackend*
cog_gtk4_platform_get_view_backend(CogPlatform* platform, WebKitWebView* related_view, GError** error)
{
g_assert_nonnull(platform);
g_assert_nonnull(win.view_backend);
return win.view_backend;
}
static void
on_title_change(WebKitWebView* view, GParamSpec* pspec,
gpointer user_data)
{
struct platform_window* win = user_data;
g_autofree gchar* title;
g_object_get(view, "title", &title, NULL);
g_autofree gchar* win_title = g_strdup_printf("Cog - %s", title);
gtk_window_set_title(GTK_WINDOW(win->gtk_window), win_title);
}
static void
on_uri_change(WebKitWebView* view, GParamSpec* pspec,
gpointer user_data)
{
struct platform_window* win = user_data;
const char* uri = webkit_web_view_get_uri(view);
GtkEntryBuffer* buffer = gtk_entry_get_buffer(GTK_ENTRY(win->url_entry));
gtk_entry_buffer_set_text(buffer, uri, strlen(uri));
}
static void
on_load_progress(WebKitWebView* view, GParamSpec* pspec,
gpointer user_data)
{
struct platform_window* win = user_data;
gdouble progress;
g_object_get(view, "estimated-load-progress", &progress, NULL);
gtk_entry_set_progress_fraction(GTK_ENTRY(win->url_entry),
progress < 1 ? progress : 0);
}
static void
on_back_forward_changed(WebKitBackForwardList* back_forward_list,
WebKitBackForwardListItem* item_added,
gpointer items_removed, gpointer user_data)
{
struct platform_window* win = user_data;
gtk_widget_set_sensitive(win->back_button,
webkit_web_view_can_go_back(win->web_view));
gtk_widget_set_sensitive(win->forward_button,
webkit_web_view_can_go_forward(win->web_view));
}
static void
on_mouse_target_changed(WebKitWebView *view, WebKitHitTestResult *hitTestResult, guint mouseModifiers)
{
CogCursorNames cursor_names = cog_cursors_get_names_for_hit_test(hitTestResult);
g_autoptr(GdkCursor) cursor = NULL;
for (unsigned i = 0; !cursor && cursor_names[i]; i++)
cursor = gdk_cursor_new_from_name(cursor_names[i], NULL);
if (!cursor) {
g_warning("Could not get %s cursor", cursor_names[0]);
return;
}
gtk_widget_set_cursor(win.gtk_window, cursor);
}
#if COG_HAVE_LIBPORTAL
static void
on_run_file_chooser(WebKitWebView *view, WebKitFileChooserRequest *request)
{
g_autoptr(XdpParent) xdp_parent = xdp_parent_new_gtk(GTK_WINDOW(win.gtk_window));
run_file_chooser(view, request, xdp_parent);
}
#endif /* COG_HAVE_LIBPORTAL */
static void
cog_gtk4_platform_init_web_view(CogPlatform* platform, WebKitWebView* view)
{
g_signal_connect(view, "notify::title", G_CALLBACK(on_title_change), &win);
g_signal_connect(view, "notify::uri", G_CALLBACK(on_uri_change), &win);
g_signal_connect(view, "notify::estimated-load-progress",
G_CALLBACK(on_load_progress), &win);
g_signal_connect(webkit_web_view_get_back_forward_list(view), "changed",
G_CALLBACK(on_back_forward_changed), &win);
#if COG_HAVE_LIBPORTAL
g_signal_connect(view, "run-file-chooser", G_CALLBACK(on_run_file_chooser), NULL);
#endif /* COG_HAVE_LIBPORTAL */
g_signal_connect(view, "mouse-target-changed", G_CALLBACK(on_mouse_target_changed), NULL);
win.web_view = view;
win.device_scale_factor = gtk_widget_get_scale_factor(win.gl_drawing_area);
wpe_view_backend_dispatch_set_device_scale_factor(wpe_view_backend_exportable_fdo_get_view_backend(win.exportable),
win.device_scale_factor);
}
static void*
check_supported(void* data G_GNUC_UNUSED)
{
return GINT_TO_POINTER(gtk_init_check());
}
static gboolean
cog_gtk4_platform_is_supported(void)
{
static GOnce once = G_ONCE_INIT;
g_once(&once, check_supported, NULL);
return GPOINTER_TO_INT(once.retval);
}
static void
cog_gtk4_platform_class_init(CogGtk4PlatformClass* klass)
{
CogPlatformClass* platform_class = COG_PLATFORM_CLASS(klass);
platform_class->is_supported = cog_gtk4_platform_is_supported;
platform_class->setup = cog_gtk4_platform_setup;
platform_class->get_view_backend = cog_gtk4_platform_get_view_backend;
platform_class->init_web_view = cog_gtk4_platform_init_web_view;
}
static void
cog_gtk4_platform_class_finalize(CogGtk4PlatformClass* klass)
{
}
static void
cog_gtk4_platform_init(CogGtk4Platform* self)
{
}
G_MODULE_EXPORT void
g_io_cogplatform_gtk4_load(GIOModule* module)
{
GTypeModule* type_module = G_TYPE_MODULE(module);
cog_gtk4_platform_register_type(type_module);
}
G_MODULE_EXPORT void
g_io_cogplatform_gtk4_unload(GIOModule* module G_GNUC_UNUSED)
{
}