-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathcog-platform-fdo.c
More file actions
2338 lines (1986 loc) · 74.4 KB
/
cog-platform-fdo.c
File metadata and controls
2338 lines (1986 loc) · 74.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* cog-fdo-platform.c
* Copyright (C) 2018 Eduardo Lima <elima@igalia.com>
* Copyright (C) 2018 Adrian Perez de Castro <aperez@igalia.com>
*
* Distributed under terms of the MIT license.
*/
#include <cog.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <wpe/webkit.h>
#include <wpe/fdo.h>
#include <wpe/fdo-egl.h>
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <glib-object.h>
#include <string.h>
#include <wayland-client.h>
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
/* for mmap */
#include <sys/mman.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <locale.h>
#include "xdg-shell-client.h"
#include "fullscreen-shell-unstable-v1-client.h"
#include "presentation-time-client.h"
#if COG_IM_API_SUPPORTED
#include "cog-im-context-fdo.h"
#include "cog-im-context-fdo-v1.h"
#include "text-input-unstable-v1-client.h"
#include "text-input-unstable-v3-client.h"
#endif
#include "cog-popup-menu-fdo.h"
#include "linux-dmabuf-unstable-v1-client.h"
#if COG_ENABLE_WESTON_DIRECT_DISPLAY
#include <drm_fourcc.h>
#include <wpe/extensions/video-plane-display-dmabuf.h>
#include "weston-direct-display-client-protocol.h"
#include "weston-content-protection-client-protocol.h"
#endif
#ifdef COG_USE_WAYLAND_CURSOR
#include <wayland-cursor.h>
#endif
#define DEFAULT_WIDTH 1024
#define DEFAULT_HEIGHT 768
#define DEFAULT_ZOOM_STEP 0.1f
#if defined(WPE_CHECK_VERSION)
# define HAVE_DEVICE_SCALING WPE_CHECK_VERSION(1, 3, 0)
# define HAVE_2D_AXIS_EVENT WPE_CHECK_VERSION(1, 5, 0) && WEBKIT_CHECK_VERSION(2, 27, 4)
#else
# define HAVE_DEVICE_SCALING 0
# define HAVE_2D_AXIS_EVENT 0
#endif /* WPE_CHECK_VERSION */
#if COG_ENABLE_WESTON_DIRECT_DISPLAY
#define VIDEO_BUFFER_FORMAT DRM_FORMAT_YUYV
struct video_buffer {
struct wl_buffer *buffer;
int32_t x;
int32_t y;
int32_t width;
int32_t height;
int fd;
struct wpe_video_plane_display_dmabuf_export* dmabuf_export;
};
struct video_surface {
struct weston_protected_surface *protected_surface;
struct wl_surface *wl_surface;
struct wl_subsurface *wl_subsurface;
};
#endif
#ifndef EGL_WL_create_wayland_buffer_from_image
typedef struct wl_buffer * (EGLAPIENTRYP PFNEGLCREATEWAYLANDBUFFERFROMIMAGEWL) (EGLDisplay dpy, EGLImageKHR image);
#endif
typedef struct output_metrics {
struct wl_output *output;
int32_t name;
int32_t scale;
int32_t width;
int32_t height;
} output_metrics;
static struct {
struct wl_display *display;
struct wl_registry *registry;
struct wl_compositor *compositor;
struct wl_subcompositor *subcompositor;
struct wl_shm *shm;
struct xdg_wm_base *xdg_shell;
struct zwp_fullscreen_shell_v1 *fshell;
struct wl_shell *shell;
struct wl_seat *seat;
uint32_t event_serial;
#if COG_ENABLE_WESTON_DIRECT_DISPLAY
struct zwp_linux_dmabuf_v1 *dmabuf;
struct weston_direct_display_v1 *direct_display;
struct weston_content_protection *protection;
#endif
#ifdef COG_USE_WAYLAND_CURSOR
struct wl_shm *wl_shm;
struct wl_cursor_theme *cursor_theme;
struct wl_cursor *cursor_left_ptr;
struct wl_surface *cursor_left_ptr_surface;
#endif /* COG_USE_WAYLAND_CURSOR */
struct output_metrics metrics[16];
#if COG_IM_API_SUPPORTED
struct zwp_text_input_manager_v3 *text_input_manager;
struct zwp_text_input_manager_v1 *text_input_manager_v1;
#endif
struct wp_presentation *presentation;
struct {
int32_t scale;
} current_output;
struct {
struct wl_pointer *obj;
struct wl_surface *surface;
int32_t x;
int32_t y;
uint32_t button;
uint32_t state;
} pointer;
struct {
bool has_delta;
uint32_t time;
wl_fixed_t x_delta;
wl_fixed_t y_delta;
} axis;
struct {
struct wl_keyboard *obj;
struct {
int32_t rate;
int32_t delay;
} repeat_info;
struct {
uint32_t key;
uint32_t time;
uint32_t state;
uint32_t event_source;
} repeat_data;
} keyboard;
struct {
struct wl_touch *obj;
struct wl_surface *surface;
struct wpe_input_touch_event_raw points[10];
} touch;
GSource *event_src;
} wl_data = {
.current_output.scale = 1,
};
static struct {
struct egl_display *display;
} egl_data;
static struct {
struct wl_surface *wl_surface;
#if COG_ENABLE_WESTON_DIRECT_DISPLAY
GHashTable *video_surfaces;
#endif
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
struct wl_shell_surface *shell_surface;
uint32_t width;
uint32_t height;
bool is_fullscreen;
bool is_maximized;
bool should_resize_to_largest_output;
} win_data = {
.width = DEFAULT_WIDTH,
.height = DEFAULT_HEIGHT,
.is_fullscreen = false,
.is_maximized = false,
.should_resize_to_largest_output = false,
};
static struct {
struct wl_surface *wl_surface;
struct xdg_positioner *xdg_positioner;
struct xdg_surface *xdg_surface;
struct xdg_popup *xdg_popup;
struct wl_shell_surface *shell_surface;
uint32_t width;
uint32_t height;
CogPopupMenu *popup_menu;
WebKitOptionMenu *option_menu;
bool configured;
} popup_data = {
.configured = false,
};
static struct {
struct xkb_context* context;
struct xkb_keymap* keymap;
struct xkb_state* state;
struct xkb_compose_table* compose_table;
struct xkb_compose_state* compose_state;
struct {
xkb_mod_index_t control;
xkb_mod_index_t alt;
xkb_mod_index_t shift;
} indexes;
uint8_t modifiers;
} xkb_data = {NULL, };
static struct {
struct wpe_view_backend_exportable_fdo *exportable;
} wpe_host_data;
static struct {
struct wpe_view_backend *backend;
struct wpe_fdo_egl_exported_image *image;
struct wl_buffer *buffer;
struct wl_callback *frame_callback;
bool should_update_opaque_region;
} wpe_view_data = {
.should_update_opaque_region = true, /* Force initial update. */
};
struct wl_event_source {
GSource source;
GPollFD pfd;
struct wl_display* display;
};
static gboolean
wl_src_prepare (GSource *base, gint *timeout)
{
struct wl_event_source *src = (struct wl_event_source *) base;
*timeout = -1;
while (wl_display_prepare_read (src->display) != 0) {
if (wl_display_dispatch_pending (src->display) < 0)
return false;
}
wl_display_flush (src->display);
return false;
}
static gboolean
wl_src_check (GSource *base)
{
struct wl_event_source *src = (struct wl_event_source *) base;
if (src->pfd.revents & G_IO_IN) {
if (wl_display_read_events(src->display) < 0)
return false;
return true;
} else {
wl_display_cancel_read(src->display);
return false;
}
}
static gboolean
wl_src_dispatch (GSource *base, GSourceFunc callback, gpointer user_data)
{
struct wl_event_source *src = (struct wl_event_source *) base;
if (src->pfd.revents & G_IO_IN) {
if (wl_display_dispatch_pending(src->display) < 0)
return false;
}
if (src->pfd.revents & (G_IO_ERR | G_IO_HUP))
return false;
src->pfd.revents = 0;
return true;
}
static void
wl_src_finalize (GSource *base)
{
}
static GSource *
setup_wayland_event_source (GMainContext *main_context,
struct wl_display *display)
{
static GSourceFuncs wl_src_funcs = {
.prepare = wl_src_prepare,
.check = wl_src_check,
.dispatch = wl_src_dispatch,
.finalize = wl_src_finalize,
};
struct wl_event_source *wl_source =
(struct wl_event_source *) g_source_new (&wl_src_funcs,
sizeof (struct wl_event_source));
wl_source->display = display;
wl_source->pfd.fd = wl_display_get_fd (display);
wl_source->pfd.events = G_IO_IN | G_IO_ERR | G_IO_HUP;
wl_source->pfd.revents = 0;
g_source_add_poll (&wl_source->source, &wl_source->pfd);
g_source_set_can_recurse (&wl_source->source, TRUE);
g_source_attach (&wl_source->source, g_main_context_get_thread_default());
g_source_unref (&wl_source->source);
return &wl_source->source;
}
static void create_popup (WebKitOptionMenu *option_menu);
static void display_popup (void);
static void update_popup (void);
static void destroy_popup (void);
static void
configure_surface_geometry (int32_t width, int32_t height)
{
const char* env_var;
if (width == 0) {
env_var = g_getenv("COG_PLATFORM_FDO_VIEW_WIDTH");
if (env_var != NULL)
width = (int32_t) g_ascii_strtod(env_var, NULL);
else
width = DEFAULT_WIDTH;
}
if (height == 0) {
env_var = g_getenv("COG_PLATFORM_FDO_VIEW_HEIGHT");
if (env_var != NULL)
height = (int32_t) g_ascii_strtod(env_var, NULL);
else
height = DEFAULT_HEIGHT;
}
if (win_data.width != width || win_data.height != height) {
g_debug("Configuring new size: %" PRId32 "x%" PRId32, width, height);
win_data.width = width;
win_data.height = height;
wpe_view_data.should_update_opaque_region = true;
}
}
static void
resize_window (void)
{
int32_t pixel_width = win_data.width * wl_data.current_output.scale;
int32_t pixel_height = win_data.height * wl_data.current_output.scale;
wpe_view_backend_dispatch_set_size (wpe_view_data.backend,
win_data.width,
win_data.height);
g_debug ("Resized EGL buffer to: (%u, %u) @%ix\n",
pixel_width, pixel_height, wl_data.current_output.scale);
}
static void
shell_surface_ping (void *data,
struct wl_shell_surface *shell_surface,
uint32_t serial)
{
wl_shell_surface_pong (shell_surface, serial);
}
static void
shell_surface_configure (void *data,
struct wl_shell_surface *shell_surface,
uint32_t edges,
int32_t width, int32_t height)
{
configure_surface_geometry (width, height);
g_debug ("New wl_shell configuration: (%" PRIu32 ", %" PRIu32 ")", width, height);
resize_window ();
}
static const struct wl_shell_surface_listener shell_surface_listener = {
.ping = shell_surface_ping,
.configure = shell_surface_configure,
};
static void
shell_popup_surface_ping (void *data,
struct wl_shell_surface *shell_surface,
uint32_t serial)
{
wl_shell_surface_pong (shell_surface, serial);
}
static void
shell_popup_surface_configure (void *data,
struct wl_shell_surface *shell_surface,
uint32_t edges,
int32_t width, int32_t height)
{
}
static void
shell_popup_surface_popup_done (void *data,
struct wl_shell_surface *shell_surface)
{
}
static const struct wl_shell_surface_listener shell_popup_surface_listener = {
.ping = shell_popup_surface_ping,
.configure = shell_popup_surface_configure,
.popup_done = shell_popup_surface_popup_done,
};
static void
xdg_shell_ping (void *data, struct xdg_wm_base *shell, uint32_t serial)
{
xdg_wm_base_pong(shell, serial);
}
static const struct xdg_wm_base_listener xdg_shell_listener = {
.ping = xdg_shell_ping,
};
static void
xdg_surface_on_configure (void *data,
struct xdg_surface *surface,
uint32_t serial)
{
xdg_surface_ack_configure (surface, serial);
if (popup_data.xdg_surface == surface && !popup_data.configured) {
popup_data.configured = true;
display_popup ();
}
}
static const struct xdg_surface_listener xdg_surface_listener = {
.configure = xdg_surface_on_configure
};
static void
xdg_toplevel_on_configure (void *data,
struct xdg_toplevel *toplevel,
int32_t width, int32_t height,
struct wl_array *states)
{
configure_surface_geometry (width, height);
g_debug ("New XDG toplevel configuration: (%" PRIu32 ", %" PRIu32 ")", width, height);
resize_window ();
}
static void
xdg_toplevel_on_close (void *data, struct xdg_toplevel *xdg_toplevel)
{
g_application_quit (g_application_get_default ());
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = xdg_toplevel_on_configure,
.close = xdg_toplevel_on_close,
};
static void
xdg_popup_on_configure (void *data,
struct xdg_popup *xdg_popup,
int32_t x,
int32_t y,
int32_t width,
int32_t height)
{
}
static void
xdg_popup_on_popup_done (void *data, struct xdg_popup *xdg_popup)
{
destroy_popup ();
}
static const struct xdg_popup_listener xdg_popup_listener = {
.configure = xdg_popup_on_configure,
.popup_done = xdg_popup_on_popup_done,
};
static void
resize_to_largest_output ()
{
/* Find the largest output and resize the surface to match */
int32_t width = 0;
int32_t height = 0;
for (int i = 0; i < G_N_ELEMENTS (wl_data.metrics); i++)
{
if (wl_data.metrics[i].output &&
wl_data.metrics[i].width * wl_data.metrics[i].height >= width * height) {
width = wl_data.metrics[i].width;
height = wl_data.metrics[i].height;
}
}
configure_surface_geometry (width, height);
if (wpe_view_data.backend != NULL) {
resize_window();
}
}
static void
noop()
{
}
static struct output_metrics *find_output(struct wl_output *output)
{
for (int i = 0; i < G_N_ELEMENTS (wl_data.metrics); i++)
{
if (wl_data.metrics[i].output == output) {
return &wl_data.metrics[i];
}
}
g_warning ("Unknown output %p\n", output);
return NULL;
}
static void
output_handle_mode (void *data,
struct wl_output *output,
uint32_t flags,
int32_t width,
int32_t height,
int32_t refresh)
{
struct output_metrics *metrics = find_output (output);
if (!metrics) {
return;
}
if (flags & WL_OUTPUT_MODE_CURRENT) {
metrics->width = width;
metrics->height = height;
g_info ("Output %p is %" PRId32 "x%" PRId32, output, width, height);
}
}
static void
output_handle_done (void *data, struct wl_output *output)
{
if (win_data.should_resize_to_largest_output) {
resize_to_largest_output ();
}
}
static void
output_handle_scale (void *data,
struct wl_output *output,
int32_t factor)
{
struct output_metrics *metrics = find_output (output);
if (!metrics) {
return;
}
metrics->scale = factor;
g_info ("Got scale factor %i for output %p\n", factor, output);
}
static const struct wl_output_listener output_listener = {
.geometry = noop,
.mode = output_handle_mode,
.done = output_handle_done,
.scale = output_handle_scale,
};
static void
surface_handle_enter (void *data, struct wl_surface *surface, struct wl_output *output)
{
#if HAVE_DEVICE_SCALING
int32_t scale_factor = -1;
for (int i=0; i < G_N_ELEMENTS (wl_data.metrics); i++)
{
if (wl_data.metrics[i].output == output) {
scale_factor = wl_data.metrics[i].scale;
}
}
if (scale_factor == -1) {
g_warning ("No scale factor available for output %p\n", output);
return;
}
g_debug ("Surface entered output %p with scale factor %i\n", output, scale_factor);
wl_surface_set_buffer_scale (surface, scale_factor);
wpe_view_backend_dispatch_set_device_scale_factor (wpe_view_data.backend, scale_factor);
wl_data.current_output.scale = scale_factor;
#endif /* HAVE_DEVICE_SCALING */
}
static const struct wl_surface_listener surface_listener = {
.enter = surface_handle_enter,
.leave = noop,
};
static void
registry_global (void *data,
struct wl_registry *registry,
uint32_t name,
const char *interface,
uint32_t version)
{
gboolean interface_used = TRUE;
if (strcmp (interface, wl_compositor_interface.name) == 0) {
wl_data.compositor = wl_registry_bind (registry,
name,
&wl_compositor_interface,
version);
} else if (strcmp (interface, wl_subcompositor_interface.name) == 0) {
wl_data.subcompositor = wl_registry_bind (registry,
name,
&wl_subcompositor_interface,
version);
} else if (strcmp (interface, wl_shell_interface.name) == 0) {
wl_data.shell = wl_registry_bind (registry,
name,
&wl_shell_interface,
version);
} else if (strcmp (interface, wl_shm_interface.name) == 0) {
wl_data.shm = wl_registry_bind (registry,
name,
&wl_shm_interface,
version);
} else if (strcmp (interface, xdg_wm_base_interface.name) == 0) {
wl_data.xdg_shell = wl_registry_bind (registry,
name,
&xdg_wm_base_interface,
version);
g_assert (wl_data.xdg_shell);
xdg_wm_base_add_listener (wl_data.xdg_shell, &xdg_shell_listener, NULL);
} else if (strcmp (interface,
zwp_fullscreen_shell_v1_interface.name) == 0) {
wl_data.fshell = wl_registry_bind (registry,
name,
&zwp_fullscreen_shell_v1_interface,
version);
} else if (strcmp (interface, wl_seat_interface.name) == 0) {
wl_data.seat = wl_registry_bind (registry,
name,
&wl_seat_interface,
version);
#if COG_ENABLE_WESTON_DIRECT_DISPLAY
} else if (strcmp (interface, zwp_linux_dmabuf_v1_interface.name) == 0) {
if (version < 3) {
g_warning ("Version %d of the zwp_linux_dmabuf_v1 protocol is not supported", version);
return;
}
wl_data.dmabuf = wl_registry_bind (registry, name, &zwp_linux_dmabuf_v1_interface, version);
} else if (strcmp (interface, weston_direct_display_v1_interface.name) == 0) {
wl_data.direct_display = wl_registry_bind (registry,
name,
&weston_direct_display_v1_interface,
version);
} else if (strcmp (interface, weston_content_protection_interface.name) == 0) {
wl_data.protection = wl_registry_bind (registry,
name,
&weston_content_protection_interface,
version);
#endif /* COG_ENABLE_WESTON_DIRECT_DISPLAY */
} else if (strcmp (interface, wl_output_interface.name) == 0) {
struct wl_output* output = wl_registry_bind (registry,
name,
&wl_output_interface,
version);
wl_output_add_listener (output, &output_listener, NULL);
bool inserted = false;
for (int i = 0; i < G_N_ELEMENTS (wl_data.metrics); i++)
{
if (wl_data.metrics[i].output == NULL) {
wl_data.metrics[i].output = output;
wl_data.metrics[i].name = name;
inserted = true;
break;
}
}
if (!inserted) {
g_warning ("Exceeded %" G_GSIZE_FORMAT " connected outputs(!)", G_N_ELEMENTS (wl_data.metrics));
}
#if COG_IM_API_SUPPORTED
} else if (strcmp (interface, zwp_text_input_manager_v3_interface.name) == 0) {
wl_data.text_input_manager = wl_registry_bind (registry,
name,
&zwp_text_input_manager_v3_interface,
version);
} else if (strcmp (interface, zwp_text_input_manager_v1_interface.name) == 0) {
wl_data.text_input_manager_v1 = wl_registry_bind (registry,
name,
&zwp_text_input_manager_v1_interface,
version);
#endif
#ifdef COG_USE_WAYLAND_CURSOR
} else if (strcmp (interface, wl_shm_interface.name) == 0) {
wl_data.wl_shm = wl_registry_bind (registry,
name,
&wl_shm_interface,
version);
#endif /* COG_USE_WAYLAND_CURSOR */
} else if (strcmp (interface, wp_presentation_interface.name) == 0) {
wl_data.presentation = wl_registry_bind (registry,
name,
&wp_presentation_interface,
version);
} else {
interface_used = FALSE;
}
g_debug ("%s '%s' interface obtained from the Wayland registry.",
interface_used ? "Using" : "Ignoring", interface);
}
static void
pointer_on_enter (void* data,
struct wl_pointer* pointer,
uint32_t serial,
struct wl_surface* surface,
wl_fixed_t fixed_x,
wl_fixed_t fixed_y)
{
wl_data.event_serial = serial;
wl_data.pointer.surface = surface;
#ifdef COG_USE_WAYLAND_CURSOR
if (wl_data.cursor_left_ptr) {
/*
* TODO: Take the output device scaling into account and load
* a cursor image of the appropriate size, if possible.
*/
if (!wl_data.cursor_left_ptr_surface) {
struct wl_buffer *buffer =
wl_cursor_image_get_buffer (wl_data.cursor_left_ptr->images[0]);
if (buffer) {
struct wl_surface *surface =
wl_compositor_create_surface (wl_data.compositor);
wl_surface_attach (surface, buffer, 0, 0);
wl_surface_damage (surface, 0, 0,
wl_data.cursor_left_ptr->images[0]->width,
wl_data.cursor_left_ptr->images[0]->height);
wl_surface_commit (surface);
wl_data.cursor_left_ptr_surface = surface;
}
}
wl_pointer_set_cursor (wl_data.pointer.obj,
serial,
wl_data.cursor_left_ptr_surface,
wl_data.cursor_left_ptr->images[0]->hotspot_x,
wl_data.cursor_left_ptr->images[0]->hotspot_y);
}
#endif /* COG_USE_WAYLAND_CURSOR */
}
static void
pointer_on_leave (void* data,
struct wl_pointer *pointer,
uint32_t serial,
struct wl_surface* surface)
{
wl_data.event_serial = serial;
wl_data.pointer.surface = NULL;
}
static void
pointer_on_motion (void* data,
struct wl_pointer *pointer,
uint32_t time,
wl_fixed_t fixed_x,
wl_fixed_t fixed_y)
{
wl_data.pointer.x = wl_fixed_to_int (fixed_x);
wl_data.pointer.y = wl_fixed_to_int (fixed_y);
struct wpe_input_pointer_event event = {
wpe_input_pointer_event_type_motion,
time,
wl_data.pointer.x * wl_data.current_output.scale,
wl_data.pointer.y * wl_data.current_output.scale,
wl_data.pointer.button,
wl_data.pointer.state
};
wpe_view_backend_dispatch_pointer_event (wpe_view_data.backend, &event);
}
static void
pointer_on_button (void* data,
struct wl_pointer *pointer,
uint32_t serial,
uint32_t time,
uint32_t button,
uint32_t state)
{
wl_data.event_serial = serial;
/* @FIXME: what is this for?
if (button >= BTN_MOUSE)
button = button - BTN_MOUSE + 1;
else
button = 0;
*/
wl_data.pointer.button = !!state ? button : 0;
wl_data.pointer.state = state;
struct wpe_input_pointer_event event = {
wpe_input_pointer_event_type_button,
time,
wl_data.pointer.x * wl_data.current_output.scale,
wl_data.pointer.y * wl_data.current_output.scale,
wl_data.pointer.button,
wl_data.pointer.state,
};
if (popup_data.wl_surface) {
if (wl_data.pointer.surface == popup_data.wl_surface) {
cog_popup_menu_handle_event (popup_data.popup_menu,
!!state ? COG_POPUP_MENU_EVENT_STATE_PRESSED : COG_POPUP_MENU_EVENT_STATE_RELEASED,
event.x, event.y);
update_popup ();
return;
} else {
if (!!state)
destroy_popup ();
}
}
wpe_view_backend_dispatch_pointer_event (wpe_view_data.backend, &event);
}
static void
dispatch_axis_event ()
{
if (!wl_data.axis.has_delta)
return;
#if HAVE_2D_AXIS_EVENT
struct wpe_input_axis_2d_event event = { 0, };
event.base.type = wpe_input_axis_event_type_mask_2d | wpe_input_axis_event_type_motion_smooth;
event.base.time = wl_data.axis.time;
event.base.x = wl_data.pointer.x * wl_data.current_output.scale;
event.base.y = wl_data.pointer.y * wl_data.current_output.scale;
event.x_axis = wl_fixed_to_double(wl_data.axis.x_delta) * wl_data.current_output.scale;
event.y_axis = -wl_fixed_to_double(wl_data.axis.y_delta) * wl_data.current_output.scale;
wpe_view_backend_dispatch_axis_event (wpe_view_data.backend, &event.base);
#else
struct wpe_input_axis_event event = {
wpe_input_axis_event_type_motion,
wl_data.axis.time,
wl_data.pointer.x * wl_data.current_output.scale,
wl_data.pointer.y * wl_data.current_output.scale,
0, 0, 0,
};
if (wl_data.axis.x_delta) {
event.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
event.value = wl_fixed_to_int (wl_data.axis.x_delta) > 0 ? 1 : -1;
wpe_view_backend_dispatch_axis_event (wpe_view_data.backend, &event);
}
if (wl_data.axis.y_delta) {
event.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
event.value = wl_fixed_to_int (wl_data.axis.y_delta) > 0 ? -1 : 1;
wpe_view_backend_dispatch_axis_event (wpe_view_data.backend, &event);
}
#endif
wl_data.axis.has_delta = false;
wl_data.axis.time = 0;
wl_data.axis.x_delta = wl_data.axis.y_delta = 0;
}
static void
pointer_on_axis (void* data,
struct wl_pointer *pointer,
uint32_t time,
uint32_t axis,
wl_fixed_t value)
{
if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
wl_data.axis.has_delta = true;
wl_data.axis.time = time;
wl_data.axis.y_delta += value;
}
if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
wl_data.axis.has_delta = true;
wl_data.axis.time = time;
wl_data.axis.x_delta += value;
}
#if !WAYLAND_1_10_OR_GREATER
// No 'frame' event in this case, so dispatch immediately.
dispatch_axis_event ();
#endif
}
#if WAYLAND_1_10_OR_GREATER
static void
pointer_on_frame (void* data,
struct wl_pointer *pointer)
{
/* @FIXME: buffer pointer events and handle them in frame. That's the
* recommended usage of this interface.
*/
dispatch_axis_event();
}
static void
pointer_on_axis_source (void *data,
struct wl_pointer *wl_pointer,
uint32_t axis_source)
{
}
static void
pointer_on_axis_stop (void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
uint32_t axis)
{
}
static void
pointer_on_axis_discrete (void *data,
struct wl_pointer *wl_pointer,
uint32_t axis,
int32_t discrete)
{
}
#endif /* WAYLAND_1_10_OR_GREATER */
static const struct wl_pointer_listener pointer_listener = {
.enter = pointer_on_enter,
.leave = pointer_on_leave,
.motion = pointer_on_motion,
.button = pointer_on_button,
.axis = pointer_on_axis,
#if WAYLAND_1_10_OR_GREATER
.frame = pointer_on_frame,
.axis_source = pointer_on_axis_source,
.axis_stop = pointer_on_axis_stop,
.axis_discrete = pointer_on_axis_discrete,
#endif /* WAYLAND_1_10_OR_GREATER */
};