/
cog-platform-drm.c
1670 lines (1406 loc) · 54.8 KB
/
cog-platform-drm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* cog-platform-x11.c
* Copyright (C) 2019-2022 Igalia S.L.
* Copyright (C) 2020 Michal Artazov <michal@artazov.cz>
*
* SPDX-License-Identifier: MIT
*/
#include "../../core/cog.h"
#include "cog-drm-renderer.h"
#include "cursor-drm.h"
#include "kms.h"
#include <assert.h>
#include <drm_fourcc.h>
#include <errno.h>
#include <fcntl.h>
#include <gbm.h>
#include <libinput.h>
#include <libudev.h>
#include <string.h>
#include <wayland-server.h>
#include <wpe/fdo-egl.h>
#include <wpe/fdo.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <xkbcommon/xkbcommon.h>
#include <epoxy/egl.h>
#include "../common/egl-proc-address.h"
#ifndef LIBINPUT_CHECK_VERSION
# define LIBINPUT_CHECK_VERSION(a, b, c) \
((LIBINPUT_VER_MAJOR > (a)) || (LIBINPUT_VER_MAJOR == (a) && (LIBINPUT_VER_MINOR > (b))) || \
(LIBINPUT_VER_MAJOR == (a) && (LIBINPUT_VER_MINOR == (b)) && (LIBINPUT_VER_MICRO >= (c))))
#endif /* !LIBINPUT_CHECK_VERSION */
#if !defined(EGL_EXT_platform_base)
typedef EGLDisplay (EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC) (EGLenum platform, void *native_display, const EGLint *attrib_list);
#endif
#if !defined(EGL_MESA_platform_gbm) || !defined(EGL_KHR_platform_gbm)
#define EGL_PLATFORM_GBM_KHR 0x31D7
#endif
#define KEY_STARTUP_DELAY 500000
#define KEY_REPEAT_DELAY 100000
#ifndef g_debug_once
# define g_debug_once(...) \
G_STMT_START \
{ \
static int G_PASTE(_GDebugOnceBoolean_, __LINE__) = 0; /* (atomic) */ \
if (g_atomic_int_compare_and_exchange(&G_PASTE(_GDebugOnceBoolean_, __LINE__), 0, 1)) \
g_debug(__VA_ARGS__); \
} \
G_STMT_END
#endif /* !g_debug_once */
struct _CogDrmPlatformClass {
CogPlatformClass parent_class;
};
struct _CogDrmPlatform {
CogPlatform parent;
CogView *web_view;
CogDrmRenderer *renderer;
CogGLRendererRotation rotation;
GList *rotatable_input_devices;
bool use_gles;
};
enum {
PROP_0,
PROP_ROTATION,
PROP_RENDERER,
N_PROPERTIES,
};
static GParamSpec *s_properties[N_PROPERTIES] = {
NULL,
};
G_DECLARE_FINAL_TYPE(CogDrmPlatform, cog_drm_platform, COG, DRM_PLATFORM, CogPlatform)
G_DEFINE_DYNAMIC_TYPE_EXTENDED(
CogDrmPlatform,
cog_drm_platform,
COG_TYPE_PLATFORM,
0,
g_io_extension_point_implement(COG_MODULES_PLATFORM_EXTENSION_POINT, g_define_type_id, "drm", 200);)
static struct {
int fd;
drmModeRes *base_resources;
drmModePlaneRes *plane_resources;
struct {
drmModeConnector *obj;
uint32_t obj_id;
} connector;
struct {
drmModeCrtc *obj;
uint32_t obj_id;
uint32_t index;
} crtc;
struct {
drmModePlane *obj;
uint32_t obj_id;
} plane;
drmModeModeInfo *mode;
drmModeEncoder *encoder;
uint32_t width;
uint32_t height;
uint32_t refresh;
double device_scale;
bool atomic_modesetting;
bool addfb2_modifiers;
bool mode_set;
} drm_data = {
.fd = -1,
.base_resources = NULL,
.plane_resources = NULL,
.connector =
{
NULL,
0,
},
.crtc =
{
NULL,
0,
0,
},
.plane =
{
NULL,
0,
},
.mode = NULL,
.encoder = NULL,
.width = 0,
.height = 0,
.refresh = 0,
.device_scale = 1.0,
.atomic_modesetting = true,
.mode_set = false,
};
static struct {
gboolean enabled;
struct kms_device *device;
struct kms_plane *plane;
struct kms_framebuffer *cursor;
unsigned int x;
unsigned int y;
unsigned int screen_width;
unsigned int screen_height;
} cursor = {
.enabled = FALSE,
.device = NULL,
.plane = NULL,
.cursor = NULL,
};
static struct {
struct gbm_device *device;
} gbm_data = {
.device = NULL,
};
static struct {
EGLDisplay display;
} egl_data = {
.display = EGL_NO_DISPLAY,
};
typedef struct keyboard_event {
uint32_t time;
uint32_t key;
} keyboard_event;
static struct {
struct udev *udev;
struct libinput *libinput;
uint32_t input_width;
uint32_t input_height;
struct keyboard_event repeating_key;
struct wpe_input_touch_event_raw touch_points[10];
enum wpe_input_touch_event_type last_touch_type;
int last_touch_id;
} input_data = {
.udev = NULL,
.libinput = NULL,
.input_width = 0,
.input_height = 0,
.repeating_key = {0, 0},
.last_touch_type = wpe_input_touch_event_type_null,
.last_touch_id = 0,
};
static struct {
GSource *drm_source;
GSource *input_source;
GSource *key_repeat_source;
} glib_data = {
.drm_source = NULL,
.input_source = NULL,
.key_repeat_source = NULL,
};
static struct {
struct wpe_view_backend_exportable_fdo *exportable;
} wpe_host_data;
static struct {
struct wpe_view_backend *backend;
} wpe_view_data;
static void
init_config(CogDrmPlatform *self, CogShell *shell, const char *params_string)
{
drm_data.device_scale = cog_shell_get_device_scale_factor (shell);
g_debug ("init_config: overriding device_scale value, using %.2f from shell",
drm_data.device_scale);
GKeyFile *key_file = cog_shell_get_config_file (shell);
if (key_file) {
{
g_autoptr(GError) lookup_error = NULL;
gboolean value = g_key_file_get_boolean(key_file, "drm", "disable-atomic-modesetting", &lookup_error);
if (!lookup_error) {
drm_data.atomic_modesetting = !value;
g_debug("init_config: atomic modesetting reconfigured to value '%s'",
drm_data.atomic_modesetting ? "true" : "false");
}
}
{
g_autoptr(GError) lookup_error = NULL;
gdouble value = g_key_file_get_double(key_file, "drm", "device-scale-factor", &lookup_error);
if (!lookup_error) {
drm_data.device_scale = value;
g_debug("init_config: overriding device_scale value, using %.2f from config", drm_data.device_scale);
}
}
{
g_autofree char *value = g_key_file_get_string(key_file, "drm", "renderer", NULL);
if (g_strcmp0(value, "gles") == 0)
self->use_gles = true;
else if (g_strcmp0(value, "modeset") != 0)
self->use_gles = false;
else if (value)
g_warning("Invalid renderer '%s', using default.", value);
}
}
if (params_string) {
g_auto(GStrv) params = g_strsplit(params_string, ",", 0);
for (unsigned i = 0; params[i]; i++) {
g_auto(GStrv) kv = g_strsplit(params[i], "=", 2);
if (g_strv_length(kv) != 2) {
g_warning("Invalid parameter syntax '%s'.", params[i]);
continue;
}
const char *k = g_strstrip(kv[0]);
const char *v = g_strstrip(kv[1]);
if (g_strcmp0(k, "renderer") == 0) {
if (g_strcmp0(v, "modeset") == 0)
self->use_gles = false;
else if (g_strcmp0(v, "gles") == 0)
self->use_gles = true;
else
g_warning("Invalid value '%s' for parameter '%s'.", v, k);
} else if (g_strcmp0(k, "rotation") == 0) {
char *endp = NULL;
const char *str = v ? v : "";
uint64_t val = g_ascii_strtoull(str, &endp, 10);
if (val > 3 || *endp != '\0')
g_warning("Invalid value '%s' for parameter '%s'.", v, k);
else
self->rotation = val;
} else {
g_warning("Invalid parameter '%s'.", k);
}
}
}
}
static void
clear_drm (void)
{
g_clear_pointer (&drm_data.base_resources, drmModeFreeResources);
g_clear_pointer (&drm_data.plane_resources, drmModeFreePlaneResources);
g_clear_pointer (&drm_data.encoder, drmModeFreeEncoder);
g_clear_pointer (&drm_data.plane.obj, drmModeFreePlane);
g_clear_pointer (&drm_data.crtc.obj, drmModeFreeCrtc);
g_clear_pointer (&drm_data.connector.obj, drmModeFreeConnector);
if (drm_data.fd != -1) {
close (drm_data.fd);
drm_data.fd = -1;
}
}
static gboolean
check_drm(void)
{
drmDevice *devices[64];
memset(devices, 0, sizeof(*devices) * 64);
int num_devices = drmGetDevices2(0, devices, 64);
if (num_devices < 0)
return FALSE;
gboolean supported = FALSE;
for (int i = 0; !supported && i < num_devices; ++i) {
if (devices[i]->available_nodes & (1 << DRM_NODE_PRIMARY)) {
supported = TRUE;
break;
}
}
drmFreeDevices(devices, num_devices);
return supported;
}
static int32_t
find_crtc_for_encoder(const drmModeRes *resources, const drmModeEncoder *encoder)
{
for (int i = 0; i < resources->count_crtcs; i++) {
const uint32_t crtc_mask = 1 << i;
const uint32_t crtc_id = resources->crtcs[i];
if (encoder->possible_crtcs & crtc_mask && encoder->crtc_id == crtc_id) {
return crtc_id;
}
}
/* no match found */
return -1;
}
static gboolean
init_drm(void)
{
drmDevice *devices[64];
memset(devices, 0, sizeof(*devices) * 64);
int num_devices = drmGetDevices2(0, devices, 64);
if (num_devices < 0)
return FALSE;
for (int i = 0; i < num_devices; ++i) {
drmDevice* device = devices[i];
g_debug ("init_drm: enumerated device %p, available_nodes %d",
device, device->available_nodes);
if (device->available_nodes & (1 << DRM_NODE_PRIMARY))
g_debug ("init_drm: DRM_NODE_PRIMARY: %s", device->nodes[DRM_NODE_PRIMARY]);
if (device->available_nodes & (1 << DRM_NODE_CONTROL))
g_debug ("init_drm: DRM_NODE_CONTROL: %s", device->nodes[DRM_NODE_CONTROL]);
if (device->available_nodes & (1 << DRM_NODE_RENDER))
g_debug ("init_drm: DRM_NODE_RENDER: %s", device->nodes[DRM_NODE_RENDER]);
}
for (int i = 0; i < num_devices; ++i) {
drmDevice* device = devices[i];
if (!(device->available_nodes & (1 << DRM_NODE_PRIMARY)))
continue;
drm_data.fd = open (device->nodes[DRM_NODE_PRIMARY], O_RDWR);
if (drm_data.fd < 0)
continue;
drm_data.base_resources = drmModeGetResources (drm_data.fd);
if (drm_data.base_resources) {
g_debug ("init_drm: using device %p, DRM_NODE_PRIMARY %s",
device, device->nodes[DRM_NODE_PRIMARY]);
break;
}
close (drm_data.fd);
drm_data.fd = -1;
}
drmFreeDevices(devices, num_devices);
if (!drm_data.base_resources)
return FALSE;
if (drm_data.atomic_modesetting) {
int ret = drmSetClientCap (drm_data.fd, DRM_CLIENT_CAP_ATOMIC, 1);
if (ret) {
drm_data.atomic_modesetting = false;
g_debug ("init_drm: atomic mode not usable, falling back to non-atomic mode");
}
}
g_debug ("init_drm: %d connectors available", drm_data.base_resources->count_connectors);
for (int i = 0; i < drm_data.base_resources->count_connectors; ++i) {
drmModeConnector *connector = drmModeGetConnector (drm_data.fd,
drm_data.base_resources->connectors[i]);
g_debug ("init_drm: connector id %u, type %u, %sconnected, %d usable modes",
connector->connector_id, connector->connector_type,
(connector->connection == DRM_MODE_CONNECTED) ? "" : "not ",
connector->count_modes);
for (int j = 0; j < connector->count_modes; ++j) {
drmModeModeInfo *mode = &connector->modes[j];
g_debug ("init_drm: [%d]: '%s', %ux%u@%u, flags %u, type %u %s%s",
j, mode->name, mode->hdisplay, mode->vdisplay, mode->vrefresh,
mode->flags, mode->type,
(mode->type & DRM_MODE_TYPE_PREFERRED) ? "(preferred) " : "",
(mode->type & DRM_MODE_TYPE_DEFAULT) ? "(default) " : "");
}
g_clear_pointer (&connector, drmModeFreeConnector);
}
for (int i = 0; i < drm_data.base_resources->count_connectors; ++i) {
drm_data.connector.obj = drmModeGetConnector (drm_data.fd, drm_data.base_resources->connectors[i]);
if (drm_data.connector.obj->connection == DRM_MODE_CONNECTED)
break;
g_clear_pointer (&drm_data.connector.obj, drmModeFreeConnector);
}
if (!drm_data.connector.obj)
return FALSE;
g_debug("init_drm: using connector id %d, type %d", drm_data.connector.obj->connector_id,
drm_data.connector.obj->connector_type);
const char *user_selected_mode = g_getenv("COG_PLATFORM_DRM_VIDEO_MODE");
int user_max_width = 0;
int user_max_height = 0;
int user_max_refresh = 0;
const char *user_mode_max = g_getenv("COG_PLATFORM_DRM_MODE_MAX");
if (user_mode_max) {
if (sscanf(user_mode_max, "%dx%d@%d", &user_max_width, &user_max_height, &user_max_refresh) < 2 ||
user_max_width < 0 || user_max_height < 0 || user_max_refresh < 0) {
fprintf(stderr, "invalid value for COG_PLATFORM_DRM_MODE_MAX\n");
user_max_width = 0;
user_max_height = 0;
user_max_refresh = 0;
}
}
for (int i = 0, area = 0; i < drm_data.connector.obj->count_modes; ++i) {
drmModeModeInfo *current_mode = &drm_data.connector.obj->modes[i];
if (user_selected_mode && strcmp(user_selected_mode, current_mode->name) != 0) {
continue;
}
if (user_max_width && current_mode->hdisplay > user_max_width) {
continue;
}
if (user_max_height && current_mode->vdisplay > user_max_height) {
continue;
}
if (user_max_refresh && current_mode->vrefresh > user_max_refresh) {
continue;
}
if (current_mode->type & DRM_MODE_TYPE_PREFERRED) {
drm_data.mode = current_mode;
break;
}
int current_area = current_mode->hdisplay * current_mode->vdisplay;
if (current_area > area) {
drm_data.mode = current_mode;
area = current_area;
}
}
if (!drm_data.mode)
return FALSE;
g_debug("init_drm: using mode [%ld] '%s' @ %dHz",
(long)((drm_data.mode - drm_data.connector.obj->modes) / sizeof(drmModeModeInfo *)), drm_data.mode->name,
drm_data.mode->vrefresh);
/* Try the currently connected encoder+crtc */
for (int i = 0; i < drm_data.base_resources->count_encoders; ++i) {
drm_data.encoder = drmModeGetEncoder(drm_data.fd, drm_data.base_resources->encoders[i]);
if (!drm_data.encoder) {
/* cannot retrieve encoder, ignoring... */
continue;
}
const int32_t crtc_id = find_crtc_for_encoder(drm_data.base_resources, drm_data.encoder);
if (crtc_id != -1) {
drm_data.crtc.obj_id = crtc_id;
break;
}
g_clear_pointer (&drm_data.encoder, drmModeFreeEncoder);
}
if (!drm_data.encoder) {
fprintf(stderr, "no crtc for encoder found!\n");
return FALSE;
}
drm_data.connector.obj_id = drm_data.connector.obj->connector_id;
drm_data.crtc.obj = drmModeGetCrtc (drm_data.fd, drm_data.crtc.obj_id);
for (int i = 0; i < drm_data.base_resources->count_crtcs; ++i) {
if (drm_data.base_resources->crtcs[i] == drm_data.crtc.obj_id) {
drm_data.crtc.index = i;
break;
}
}
drm_data.plane_resources = drmModeGetPlaneResources (drm_data.fd);
if (!drm_data.plane_resources)
return FALSE;
for (int i = 0; i < drm_data.plane_resources->count_planes; ++i) {
uint32_t plane_id = drm_data.plane_resources->planes[i];
drmModePlane *plane = drmModeGetPlane (drm_data.fd, plane_id);
if (!plane)
continue;
if (!(plane->possible_crtcs & (1 << drm_data.crtc.index))) {
drmModeFreePlane (plane);
continue;
}
g_clear_pointer (&drm_data.plane.obj, drmModeFreePlane);
drm_data.plane.obj_id = 0;
drm_data.plane.obj = plane;
drm_data.plane.obj_id = plane_id;
bool is_primary = false;
drmModeObjectProperties *plane_props = drmModeObjectGetProperties (drm_data.fd, plane_id, DRM_MODE_OBJECT_PLANE);
for (int j = 0; j < plane_props->count_props; ++j) {
drmModePropertyRes *prop = drmModeGetProperty (drm_data.fd, plane_props->props[j]);
is_primary = !g_strcmp0 (prop->name, "type")
&& plane_props->prop_values[j] == DRM_PLANE_TYPE_PRIMARY;
drmModeFreeProperty (prop);
if (is_primary)
break;
}
drmModeFreeObjectProperties (plane_props);
if (is_primary)
break;
}
drm_data.width = drm_data.mode->hdisplay;
drm_data.height = drm_data.mode->vdisplay;
drm_data.refresh = drm_data.mode->vrefresh;
g_clear_pointer(&drm_data.base_resources, drmModeFreeResources);
g_clear_pointer(&drm_data.plane_resources, drmModeFreePlaneResources);
return TRUE;
}
static const uint32_t formats[] = {
DRM_FORMAT_RGBA8888,
DRM_FORMAT_ARGB8888,
};
static uint32_t
choose_format (struct kms_plane *plane)
{
for (int i = 0; i < G_N_ELEMENTS(formats); i++) {
if (kms_plane_supports_format(plane, formats[i]))
return formats[i];
}
return 0;
}
static void
clear_cursor (void) {
g_clear_pointer(&cursor.cursor, kms_framebuffer_free);
g_clear_pointer(&cursor.device, kms_device_free);
cursor.plane = NULL;
}
static gboolean
init_cursor (void)
{
bool cursor_supported = drmSetClientCap(drm_data.fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) == 0;
if (!cursor_supported) {
g_warning("cursor not supported");
return FALSE;
}
cursor.device = kms_device_open(drm_data.fd);
if (!cursor.device)
return FALSE;
cursor.plane = kms_device_find_plane_by_type(cursor.device, DRM_PLANE_TYPE_CURSOR, 0);
if (!cursor.plane) {
g_clear_pointer(&cursor.device, kms_device_free);
return FALSE;
}
uint32_t format = choose_format(cursor.plane);
if (!format) {
g_clear_pointer(&cursor.device, kms_device_free);
return FALSE;
}
cursor.cursor = create_cursor_framebuffer(cursor.device, format);
if (!cursor.cursor) {
g_clear_pointer(&cursor.device, kms_device_free);
return FALSE;
}
cursor.x = (cursor.device->screens[0]->width - cursor.cursor->width) / 2;
cursor.y = (cursor.device->screens[0]->height - cursor.cursor->height) / 2;
cursor.screen_width = cursor.device->screens[0]->width;
cursor.screen_height = cursor.device->screens[0]->height;
if (kms_plane_set(cursor.plane, cursor.cursor, cursor.x, cursor.y)) {
g_clear_pointer(&cursor.device, kms_device_free);
g_clear_pointer(&cursor.cursor, kms_framebuffer_free);
return FALSE;
}
cursor.enabled = TRUE;
return TRUE;
}
static void
clear_gbm (void)
{
g_clear_pointer (&gbm_data.device, gbm_device_destroy);
}
static gboolean
init_gbm (void)
{
gbm_data.device = gbm_create_device (drm_data.fd);
if (!gbm_data.device)
return FALSE;
return TRUE;
}
static void
clear_egl (void)
{
if (egl_data.display != EGL_NO_DISPLAY)
eglTerminate (egl_data.display);
eglReleaseThread ();
}
static gboolean
init_egl (void)
{
static PFNEGLGETPLATFORMDISPLAYEXTPROC s_eglGetPlatformDisplay = NULL;
if (!s_eglGetPlatformDisplay)
s_eglGetPlatformDisplay = (PFNEGLGETPLATFORMDISPLAYEXTPROC) load_egl_proc_address ("eglGetPlatformDisplayEXT");
if (s_eglGetPlatformDisplay)
egl_data.display = s_eglGetPlatformDisplay (EGL_PLATFORM_GBM_KHR, gbm_data.device, NULL);
else
egl_data.display = eglGetDisplay((EGLNativeDisplayType) gbm_data.device);
if (!egl_data.display) {
clear_egl ();
return FALSE;
}
if (!eglInitialize (egl_data.display, NULL, NULL)) {
clear_egl ();
return FALSE;
}
return TRUE;
}
static void
input_dispatch_key_event(CogView *view, uint32_t time, uint32_t key, enum libinput_key_state state)
{
struct wpe_input_xkb_context *default_context = wpe_input_xkb_context_get_default ();
// if wpe is unable to prepare an xkb context (e.g. environment without
// any prepared keymap data), ignore the key event
if (!default_context)
return;
// libwpe (<=v1.14.1) may provide a broken context; ensure an underlying
// xkb_context has been configured
struct xkb_context *ctx = wpe_input_xkb_context_get_context(default_context);
if (!ctx)
return;
struct xkb_state *context_state = wpe_input_xkb_context_get_state (default_context);
// if wpe cannot determine the xkb state (e.g. required keymap does are not
// available in this environment), ignore the key event
if (!context_state)
return;
uint32_t keysym = wpe_input_xkb_context_get_key_code (default_context, key, !!state);
xkb_state_update_key (context_state, key, !!state ? XKB_KEY_DOWN : XKB_KEY_UP);
uint32_t modifiers = wpe_input_xkb_context_get_modifiers (default_context,
xkb_state_serialize_mods (context_state, XKB_STATE_MODS_DEPRESSED),
xkb_state_serialize_mods (context_state, XKB_STATE_MODS_LATCHED),
xkb_state_serialize_mods (context_state, XKB_STATE_MODS_LOCKED),
xkb_state_serialize_layout (context_state, XKB_STATE_LAYOUT_EFFECTIVE));
struct wpe_input_keyboard_event event = {
.time = time,
.key_code = keysym,
.hardware_key_code = key,
.pressed = (!!state),
.modifiers = modifiers,
};
cog_view_handle_key_event(view, &event);
}
static void
start_repeating_key (uint32_t time, uint32_t key)
{
if (!!input_data.repeating_key.time && input_data.repeating_key.time == time
&& input_data.repeating_key.key == key) {
return;
}
input_data.repeating_key = (keyboard_event) {time, key};
g_source_set_ready_time (glib_data.key_repeat_source,
g_get_monotonic_time() + KEY_STARTUP_DELAY);
}
static void
stop_repeating_key (void)
{
if (!!input_data.repeating_key.time) {
g_source_set_ready_time (glib_data.key_repeat_source, -1);
}
input_data.repeating_key = (keyboard_event) {0, 0};
}
static void
input_handle_key_event(CogView *view, struct libinput_event_keyboard *key_event)
{
// Explanation for the offset-by-8, copied from Weston:
// evdev XKB rules reflect X's broken keycode system, which starts at 8
uint32_t key = libinput_event_keyboard_get_key (key_event) + 8;
uint32_t time = libinput_event_keyboard_get_time (key_event);
enum libinput_key_state key_state = libinput_event_keyboard_get_key_state (key_event);
input_dispatch_key_event(view, time, key, key_state);
if (!!key_state) {
start_repeating_key (time, key);
} else {
stop_repeating_key ();
}
}
static void
input_handle_touch_event (enum libinput_event_type touch_type, struct libinput_event_touch *touch_event)
{
uint32_t time = libinput_event_touch_get_time (touch_event);
enum wpe_input_touch_event_type event_type = wpe_input_touch_event_type_null;
switch (touch_type) {
case LIBINPUT_EVENT_TOUCH_DOWN:
event_type = wpe_input_touch_event_type_down;
break;
case LIBINPUT_EVENT_TOUCH_UP:
event_type = wpe_input_touch_event_type_up;
break;
case LIBINPUT_EVENT_TOUCH_MOTION:
event_type = wpe_input_touch_event_type_motion;
break;
case LIBINPUT_EVENT_TOUCH_FRAME: {
struct wpe_input_touch_event event = {
.touchpoints = input_data.touch_points,
.touchpoints_length = G_N_ELEMENTS (input_data.touch_points),
.type = input_data.last_touch_type,
.id = input_data.last_touch_id,
.time = time,
};
wpe_view_backend_dispatch_touch_event (wpe_view_data.backend, &event);
for (int i = 0; i < G_N_ELEMENTS (input_data.touch_points); ++i) {
struct wpe_input_touch_event_raw *touch_point = &input_data.touch_points[i];
if (touch_point->type != wpe_input_touch_event_type_up)
continue;
memset (touch_point, 0, sizeof (struct wpe_input_touch_event_raw));
touch_point->type = wpe_input_touch_event_type_null;
}
return;
}
default:
g_assert_not_reached ();
return;
}
int id = libinput_event_touch_get_seat_slot (touch_event);
if (id < 0 || id >= G_N_ELEMENTS (input_data.touch_points))
return;
input_data.last_touch_type = event_type;
input_data.last_touch_id = id;
struct wpe_input_touch_event_raw *touch_point = &input_data.touch_points[id];
touch_point->type = event_type;
touch_point->time = time;
touch_point->id = id;
if (touch_type == LIBINPUT_EVENT_TOUCH_DOWN
|| touch_type == LIBINPUT_EVENT_TOUCH_MOTION) {
touch_point->x = libinput_event_touch_get_x_transformed (touch_event,
input_data.input_width);
touch_point->y = libinput_event_touch_get_y_transformed (touch_event,
input_data.input_height);
}
}
static void
input_handle_pointer_motion_event(struct libinput_event_pointer *pointer_event, bool absolute)
{
if (!cursor.enabled)
return;
if (absolute) {
cursor.x = libinput_event_pointer_get_absolute_x_transformed(pointer_event, cursor.screen_width);
cursor.y = libinput_event_pointer_get_absolute_y_transformed(pointer_event, cursor.screen_height);
} else {
cursor.x += libinput_event_pointer_get_dx(pointer_event);
cursor.y += libinput_event_pointer_get_dy(pointer_event);
}
if (cursor.x < 0) {
cursor.x = 0;
} else if (cursor.x > cursor.screen_width - 1) {
cursor.x = cursor.screen_width - 1;
}
if (cursor.y < 0) {
cursor.y = 0;
} else if (cursor.y > cursor.screen_height - 1) {
cursor.y = cursor.screen_height - 1;
}
struct wpe_input_pointer_event event = {
.type = wpe_input_pointer_event_type_motion,
.time = libinput_event_pointer_get_time(pointer_event),
.x = cursor.x,
.y = cursor.y,
.button = 0,
.state = 0,
.modifiers = 0,
};
wpe_view_backend_dispatch_pointer_event(wpe_view_data.backend, &event);
kms_plane_set(cursor.plane, cursor.cursor, cursor.x, cursor.y);
}
static void
input_handle_pointer_button_event (struct libinput_event_pointer *pointer_event)
{
if (!cursor.enabled)
return;
struct wpe_input_pointer_event event = {
.type = wpe_input_pointer_event_type_button,
.time = libinput_event_pointer_get_time(pointer_event),
.x = cursor.x,
.y = cursor.y,
.button = libinput_event_pointer_get_button(pointer_event),
.state = libinput_event_pointer_get_button_state(pointer_event),
.modifiers = 0,
};
wpe_view_backend_dispatch_pointer_event(wpe_view_data.backend, &event);
}
#if LIBINPUT_CHECK_VERSION(1, 19, 0)
static void
input_handle_pointer_discrete_scroll_event(struct libinput_event_pointer *pointer_event)
{
struct wpe_input_axis_2d_event event = {
.base.type = wpe_input_axis_event_type_mask_2d | wpe_input_axis_event_type_motion,
.base.time = libinput_event_pointer_get_time(pointer_event),
.base.x = cursor.x,
.base.y = cursor.y,
.x_axis = 0.0,
.y_axis = 0.0,
};
if (libinput_event_pointer_has_axis(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
event.y_axis = -drm_data.device_scale * libinput_event_pointer_get_scroll_value_v120(
pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
if (libinput_event_pointer_has_axis(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
event.x_axis = drm_data.device_scale * libinput_event_pointer_get_scroll_value_v120(
pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
wpe_view_backend_dispatch_axis_event(wpe_view_data.backend, &event.base);
}
static void
input_handle_pointer_smooth_scroll_event(struct libinput_event_pointer *pointer_event)
{
struct wpe_input_axis_2d_event event = {
.base.type = wpe_input_axis_event_type_mask_2d | wpe_input_axis_event_type_motion_smooth,
.base.time = libinput_event_pointer_get_time(pointer_event),
.base.x = cursor.x,
.base.y = cursor.y,
.x_axis = 0.0,
.y_axis = 0.0,
};
if (libinput_event_pointer_has_axis(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
event.y_axis = drm_data.device_scale *
libinput_event_pointer_get_scroll_value(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
if (libinput_event_pointer_has_axis(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
event.x_axis = drm_data.device_scale *
libinput_event_pointer_get_scroll_value(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
wpe_view_backend_dispatch_axis_event(wpe_view_data.backend, &event.base);
}
#else
static void
input_handle_pointer_axis_event(struct libinput_event_pointer *pointer_event)
{
struct wpe_input_axis_2d_event event = {
.base.type = wpe_input_axis_event_type_mask_2d,
.base.time = libinput_event_pointer_get_time(pointer_event),
.base.x = cursor.x,
.base.y = cursor.y,
.x_axis = 0.0,
.y_axis = 0.0,
};
if (libinput_event_pointer_get_axis_source(pointer_event) == LIBINPUT_POINTER_AXIS_SOURCE_WHEEL) {
event.base.type |= wpe_input_axis_event_type_motion;
if (libinput_event_pointer_has_axis(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
event.y_axis = -120.0 * libinput_event_pointer_get_axis_value_discrete(
pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
if (libinput_event_pointer_has_axis(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
event.x_axis = 120.0 * libinput_event_pointer_get_axis_value_discrete(
pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
} else {
event.base.type |= wpe_input_axis_event_type_motion_smooth;
if (libinput_event_pointer_has_axis(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
event.y_axis = libinput_event_pointer_get_axis_value(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
if (libinput_event_pointer_has_axis(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
event.x_axis =
libinput_event_pointer_get_axis_value(pointer_event, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
}
event.x_axis *= drm_data.device_scale;
event.y_axis *= drm_data.device_scale;
wpe_view_backend_dispatch_axis_event(wpe_view_data.backend, &event.base);
}
#endif /* !LIBINPUT_CHECK_VERSION(1, 19, 0) */
static bool
input_device_needs_config(struct libinput_device *device)
{
static const enum libinput_device_capability device_caps[] = {
LIBINPUT_DEVICE_CAP_GESTURE, LIBINPUT_DEVICE_CAP_POINTER, LIBINPUT_DEVICE_CAP_TABLET_PAD,
LIBINPUT_DEVICE_CAP_TABLET_TOOL, LIBINPUT_DEVICE_CAP_TOUCH,
};
for (unsigned i = 0; i < G_N_ELEMENTS(device_caps); i++)
if (libinput_device_has_capability(device, device_caps[i]))
return !!libinput_device_config_rotation_is_available(device);