-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathwayland_common.c
More file actions
1202 lines (1046 loc) · 35.6 KB
/
wayland_common.c
File metadata and controls
1202 lines (1046 loc) · 35.6 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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2020 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
/* Needed for memfd_create */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* See feature_test_macros(7) */
#endif
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <poll.h>
#include <unistd.h>
#include <string/stdstring.h>
#ifdef HAVE_LIBDECOR_H
#include <libdecor.h>
#endif
#include "wayland_common.h"
#include "../input_keymaps.h"
#include "../../frontend/frontend_driver.h"
#include "../../verbosity.h"
#define DND_ACTION WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE
#define FILE_MIME "text/uri-list"
#define TEXT_MIME "text/plain;charset=utf-8"
#define PIPE_MS_TIMEOUT 10
#define IOR_READ 0x1
#define IOR_WRITE 0x2
#define IOR_NO_RETRY 0x4
#define SPLASH_SHM_NAME "retroarch-wayland-splash"
static void wl_keyboard_handle_keymap(void* data,
struct wl_keyboard* keyboard,
uint32_t format,
int fd,
uint32_t size)
{
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
{
close(fd);
return;
}
#ifdef HAVE_XKBCOMMON
init_xkb(fd, size);
#endif
close(fd);
}
static void wl_keyboard_handle_enter(void* data,
struct wl_keyboard* keyboard,
uint32_t serial,
struct wl_surface* surface,
struct wl_array* keys)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
wl->input.keyboard_focus = true;
}
static void wl_keyboard_handle_leave(void *data,
struct wl_keyboard *keyboard,
uint32_t serial,
struct wl_surface *surface)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
wl->input.keyboard_focus = false;
/* Release all keys */
memset(wl->input.key_state, 0, sizeof(wl->input.key_state));
}
static void wl_keyboard_handle_key(void *data,
struct wl_keyboard *keyboard,
uint32_t serial,
uint32_t time,
uint32_t key,
uint32_t state)
{
int value = 1;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
uint32_t keysym = key;
/* Handle 'duplicate' inputs that correspond
* to the same RETROK_* key */
switch (key)
{
case KEY_OK:
case KEY_SELECT:
keysym = KEY_ENTER;
case KEY_EXIT:
keysym = KEY_CLEAR;
default:
break;
}
if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
{
BIT_SET(wl->input.key_state, keysym);
value = 1;
}
else if (state == WL_KEYBOARD_KEY_STATE_RELEASED)
{
BIT_CLEAR(wl->input.key_state, keysym);
value = 0;
}
#ifdef HAVE_XKBCOMMON
if (handle_xkb(keysym, value) == 0)
return;
#endif
input_keyboard_event(value,
input_keymaps_translate_keysym_to_rk(keysym),
0, 0, RETRO_DEVICE_KEYBOARD);
}
static void wl_keyboard_handle_modifiers(void *data,
struct wl_keyboard *keyboard,
uint32_t serial,
uint32_t modsDepressed,
uint32_t modsLatched,
uint32_t modsLocked,
uint32_t group)
{
#ifdef HAVE_XKBCOMMON
handle_xkb_state_mask(modsDepressed, modsLatched, modsLocked, group);
#endif
}
static void wl_keyboard_handle_repeat_info(void *data,
struct wl_keyboard *wl_keyboard,
int32_t rate,
int32_t delay)
{
/* TODO: Seems like we'll need this to get
* repeat working. We'll have to do it on our own. */
}
void gfx_ctx_wl_show_mouse(void *data, bool state)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (!wl->wl_pointer)
return;
if (state)
if (wl->cursor_shape_device)
wp_cursor_shape_device_v1_set_shape(
wl->cursor_shape_device, wl->cursor.serial, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
else
{
struct wl_cursor_image *image = wl->cursor.default_cursor->images[0];
wl_pointer_set_cursor(wl->wl_pointer,
wl->cursor.serial, wl->cursor.surface,
image->hotspot_x, image->hotspot_y);
wl_surface_attach(wl->cursor.surface,
wl_cursor_image_get_buffer(image), 0, 0);
wl_surface_damage(wl->cursor.surface, 0, 0, image->width, image->height);
wl_surface_commit(wl->cursor.surface);
}
else
wl_pointer_set_cursor(wl->wl_pointer, wl->cursor.serial, NULL, 0, 0);
wl->cursor.visible = state;
}
static void wl_pointer_handle_enter(void *data,
struct wl_pointer *pointer,
uint32_t serial,
struct wl_surface *surface,
wl_fixed_t sx,
wl_fixed_t sy)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
wl->input.mouse.surface = surface;
wl->input.mouse.last_x = wl->fractional_scale ?
(int) FRACTIONAL_SCALE_MULT(wl_fixed_to_int(sx), wl->fractional_scale_num) :
wl_fixed_to_int(sx * (wl_fixed_t)wl->buffer_scale);
wl->input.mouse.last_y = wl->fractional_scale ?
(int) FRACTIONAL_SCALE_MULT(wl_fixed_to_int(sy), wl->fractional_scale_num) :
wl_fixed_to_int(sy * (wl_fixed_t)wl->buffer_scale);
wl->input.mouse.x = wl->input.mouse.last_x;
wl->input.mouse.y = wl->input.mouse.last_y;
wl->input.mouse.focus = true;
wl->cursor.serial = serial;
gfx_ctx_wl_show_mouse(data, wl->cursor.visible);
}
static void wl_pointer_handle_leave(void *data,
struct wl_pointer *pointer,
uint32_t serial,
struct wl_surface *surface)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
wl->input.mouse.focus = false;
wl->input.mouse.left = false;
wl->input.mouse.right = false;
wl->input.mouse.middle = false;
wl->input.mouse.side = false;
wl->input.mouse.extra = false;
if (wl->input.mouse.surface == surface)
wl->input.mouse.surface = NULL;
}
static void wl_pointer_handle_motion(void *data,
struct wl_pointer *pointer,
uint32_t time,
wl_fixed_t sx,
wl_fixed_t sy)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
wl->input.mouse.x = wl->fractional_scale ?
(int) FRACTIONAL_SCALE_MULT(wl_fixed_to_int(sx), wl->fractional_scale_num) :
wl_fixed_to_int((wl_fixed_t)wl->buffer_scale * sx);
wl->input.mouse.y = wl->fractional_scale ?
(int) FRACTIONAL_SCALE_MULT(wl_fixed_to_int(sy), wl->fractional_scale_num) :
wl_fixed_to_int((wl_fixed_t)wl->buffer_scale * sy);
}
static void wl_pointer_handle_button(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
uint32_t time,
uint32_t button,
uint32_t state)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (wl->input.mouse.surface != wl->surface)
return;
if (state == WL_POINTER_BUTTON_STATE_PRESSED)
{
switch (button)
{
case BTN_LEFT:
wl->input.mouse.left = true;
if (BIT_GET(wl->input.key_state, KEY_LEFTALT))
{
#ifdef HAVE_LIBDECOR_H
if (wl->libdecor)
wl->libdecor_frame_move(wl->libdecor_frame, wl->seat, serial);
else
#endif
{
xdg_toplevel_move(wl->xdg_toplevel, wl->seat, serial);
}
}
break;
case BTN_RIGHT:
wl->input.mouse.right = true;
break;
case BTN_MIDDLE:
wl->input.mouse.middle = true;
break;
case BTN_SIDE:
wl->input.mouse.side = true;
break;
case BTN_EXTRA:
wl->input.mouse.extra = true;
break;
}
}
else
{
switch (button)
{
case BTN_LEFT:
wl->input.mouse.left = false;
break;
case BTN_RIGHT:
wl->input.mouse.right = false;
break;
case BTN_MIDDLE:
wl->input.mouse.middle = false;
break;
case BTN_SIDE:
wl->input.mouse.side = false;
break;
case BTN_EXTRA:
wl->input.mouse.extra = false;
break;
}
}
}
static void wl_pointer_handle_axis(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
uint32_t axis,
wl_fixed_t value)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
double d_value = wl_fixed_to_double(value);
switch (axis)
{
case WL_POINTER_AXIS_VERTICAL_SCROLL:
if (d_value < 0)
wl->input.mouse.wu = true;
else if (d_value > 0)
wl->input.mouse.wd = true;
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
if (d_value < 0)
wl->input.mouse.wl = true;
else if (d_value > 0)
wl->input.mouse.wr = true;
break;
}
}
static void wl_touch_handle_down(void *data,
struct wl_touch *wl_touch,
uint32_t serial,
uint32_t time,
struct wl_surface *surface,
int32_t id,
wl_fixed_t x,
wl_fixed_t y)
{
int i;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (wl->num_active_touches < MAX_TOUCHES)
{
for (i = 0; i < MAX_TOUCHES; i++)
{
/* Use next empty slot */
if (!wl->active_touch_positions[i].active)
{
wl->active_touch_positions[wl->num_active_touches].active = true;
wl->active_touch_positions[wl->num_active_touches].id = id;
wl->active_touch_positions[wl->num_active_touches].x = wl->fractional_scale ?
FRACTIONAL_SCALE_MULT(wl_fixed_to_int(x), wl->fractional_scale_num) :
(unsigned) wl_fixed_to_int(x * (wl_fixed_t)wl->buffer_scale);
wl->active_touch_positions[wl->num_active_touches].y = wl->fractional_scale ?
FRACTIONAL_SCALE_MULT(wl_fixed_to_int(y), wl->fractional_scale_num) :
(unsigned) wl_fixed_to_int(y * (wl_fixed_t)wl->buffer_scale);
wl->num_active_touches++;
break;
}
}
}
}
static void wl_reorder_touches(gfx_ctx_wayland_data_t *wl)
{
int i, j;
if (wl->num_active_touches == 0)
return;
for (i = 0; i < MAX_TOUCHES; i++)
{
if (!wl->active_touch_positions[i].active)
{
for (j=i+1; j<MAX_TOUCHES; j++)
{
if (wl->active_touch_positions[j].active)
{
wl->active_touch_positions[i].active =
wl->active_touch_positions[j].active;
wl->active_touch_positions[i].id =
wl->active_touch_positions[j].id;
wl->active_touch_positions[i].x = wl->active_touch_positions[j].x;
wl->active_touch_positions[i].y = wl->active_touch_positions[j].y;
wl->active_touch_positions[j].active = false;
wl->active_touch_positions[j].id = -1;
wl->active_touch_positions[j].x = (unsigned) 0;
wl->active_touch_positions[j].y = (unsigned) 0;
break;
}
if (j == MAX_TOUCHES)
return;
}
}
}
}
static void wl_touch_handle_up(void *data,
struct wl_touch *wl_touch,
uint32_t serial,
uint32_t time,
int32_t id)
{
int i;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
for (i = 0; i < MAX_TOUCHES; i++)
{
if ( wl->active_touch_positions[i].active
&& wl->active_touch_positions[i].id == id)
{
wl->active_touch_positions[i].active = false;
wl->active_touch_positions[i].id = -1;
wl->active_touch_positions[i].x = (unsigned)0;
wl->active_touch_positions[i].y = (unsigned)0;
wl->num_active_touches--;
}
}
wl_reorder_touches(wl);
}
static void wl_touch_handle_motion(void *data,
struct wl_touch *wl_touch,
uint32_t time,
int32_t id,
wl_fixed_t x,
wl_fixed_t y)
{
int i;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
for (i = 0; i < MAX_TOUCHES; i++)
{
if ( wl->active_touch_positions[i].active &&
wl->active_touch_positions[i].id == id)
{
wl->active_touch_positions[i].x = wl->fractional_scale ?
FRACTIONAL_SCALE_MULT(wl_fixed_to_int(x), wl->fractional_scale_num) :
(unsigned) wl_fixed_to_int(x * (wl_fixed_t)wl->buffer_scale);
wl->active_touch_positions[i].y = wl->fractional_scale ?
FRACTIONAL_SCALE_MULT(wl_fixed_to_int(y), wl->fractional_scale_num) :
(unsigned) wl_fixed_to_int(y * (wl_fixed_t)wl->buffer_scale);
}
}
}
static void handle_relative_motion(void *data,
struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1,
uint32_t utime_hi, uint32_t utime_lo,
wl_fixed_t dx, wl_fixed_t dy,
wl_fixed_t dx_unaccel, wl_fixed_t dy_unaccel)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
wl->input.mouse.delta_x = wl_fixed_to_int(dx_unaccel);
wl->input.mouse.delta_y = wl_fixed_to_int(dy_unaccel);
if (wl->locked_pointer)
{
wl->input.mouse.x += wl->input.mouse.delta_x;
wl->input.mouse.y += wl->input.mouse.delta_y;
}
}
static void
locked_pointer_locked(void *data, struct zwp_locked_pointer_v1 *lockptr) { }
static void
locked_pointer_unlocked(void *data, struct zwp_locked_pointer_v1 *lockptr) { }
static void wl_touch_handle_frame(void *data, struct wl_touch *wl_touch) { }
static void wl_touch_handle_cancel(void *data, struct wl_touch *wl_touch)
{
/* If i understand the spec correctly we have to reset all touches here
* since they were not meant for us anyway */
int i;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
for (i = 0; i < MAX_TOUCHES; i++)
{
wl->active_touch_positions[i].active = false;
wl->active_touch_positions[i].id = -1;
wl->active_touch_positions[i].x = (unsigned) 0;
wl->active_touch_positions[i].y = (unsigned) 0;
}
wl->num_active_touches = 0;
}
static void wl_seat_handle_capabilities(void *data,
struct wl_seat *seat, unsigned caps)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !wl->wl_keyboard)
{
wl->wl_keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_add_listener(wl->wl_keyboard, &keyboard_listener, wl);
}
else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && wl->wl_keyboard)
{
wl_keyboard_destroy(wl->wl_keyboard);
wl->wl_keyboard = NULL;
}
if ((caps & WL_SEAT_CAPABILITY_POINTER) && !wl->wl_pointer)
{
wl->wl_pointer = wl_seat_get_pointer(seat);
wl_pointer_add_listener(wl->wl_pointer, &pointer_listener, wl);
if (wl->relative_pointer_manager)
{
wl->wl_relative_pointer =
zwp_relative_pointer_manager_v1_get_relative_pointer(
wl->relative_pointer_manager, wl->wl_pointer);
zwp_relative_pointer_v1_add_listener(wl->wl_relative_pointer,
&relative_pointer_listener, wl);
}
if (!wl->cursor_shape_device && wl->cursor_shape_manager)
{
wl->cursor_shape_device =
wp_cursor_shape_manager_v1_get_pointer(
wl->cursor_shape_manager, wl->wl_pointer);
}
}
else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && wl->wl_pointer)
{
wl_pointer_destroy(wl->wl_pointer);
wl->wl_pointer = NULL;
}
if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !wl->wl_touch)
{
wl->wl_touch = wl_seat_get_touch(seat);
wl_touch_add_listener(wl->wl_touch, &touch_listener, wl);
}
else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && wl->wl_touch)
{
wl_touch_destroy(wl->wl_touch);
wl->wl_touch = NULL;
}
}
static void wl_seat_handle_name(void *data,
struct wl_seat *seat, const char *name) { }
/* Surface callbacks. */
static bool wl_update_scale(gfx_ctx_wayland_data_t *wl)
{
surface_output_t *os;
output_info_t *new_output = NULL;
unsigned largest_scale = 0;
wl_list_for_each(os, &wl->current_outputs, link)
{
if (os->output->scale > largest_scale)
{
largest_scale = os->output->scale;
new_output = os->output;
}
};
if (new_output && wl->current_output != new_output)
{
wl->current_output = new_output;
wl->pending_buffer_scale = new_output->scale;
return true;
}
return false;
}
static bool wl_current_outputs_add(gfx_ctx_wayland_data_t *wl,
struct wl_output *output)
{
display_output_t *od;
surface_output_t *os;
output_info_t *oi_found = NULL;
wl_list_for_each(od, &wl->all_outputs, link)
{
if (od->output->output == output)
{
oi_found = od->output;
break;
}
};
if (oi_found)
{
surface_output_t *os = (surface_output_t*)
calloc(1, sizeof(surface_output_t));
os->output = oi_found;
wl_list_insert(&wl->current_outputs, &os->link);
return true;
}
return false;
}
static bool wl_current_outputs_remove(gfx_ctx_wayland_data_t *wl,
struct wl_output *output)
{
surface_output_t *os;
surface_output_t *os_found = NULL;
wl_list_for_each(os, &wl->current_outputs, link)
{
if (os->output->output == output)
{
os_found = os;
break;
}
};
if (os_found)
{
wl_list_remove(&os_found->link);
free(os_found);
return true;
}
return false;
}
static void wp_fractional_scale_v1_preferred_scale(void *data, struct wp_fractional_scale_v1 *fractional_scale,
uint32_t scale)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
wl->pending_fractional_scale_num = scale;
}
static void wl_surface_enter(void *data, struct wl_surface *wl_surface,
struct wl_output *output)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
output_info_t *oi;
wl->input.mouse.surface = wl_surface;
if (wl_current_outputs_add(wl, output))
wl_update_scale(wl);
}
static void wl_surface_leave(void *data, struct wl_surface *wl_surface, struct wl_output *output)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
output_info_t *oi;
if (wl_current_outputs_remove(wl, output))
wl_update_scale(wl);
}
/* Shell surface callbacks. */
static void xdg_shell_ping(
void *data, struct xdg_wm_base *shell, uint32_t serial)
{
xdg_wm_base_pong(shell, serial);
}
static void xdg_surface_handle_configure(
void *data, struct xdg_surface *surface,
uint32_t serial)
{
xdg_surface_ack_configure(surface, serial);
}
static void wl_output_handle_geometry(void *data,
struct wl_output *output,
int x, int y,
int physical_width, int physical_height,
int subpixel,
const char *make,
const char *model,
int transform)
{
output_info_t *oi = (output_info_t*)data;
oi->physical_width = physical_width;
oi->physical_height = physical_height;
oi->make = strdup(make);
oi->model = strdup(model);
}
static void wl_output_handle_mode(void *data,
struct wl_output *output,
uint32_t flags,
int width,
int height,
int refresh)
{
output_info_t *oi = (output_info_t*)data;
oi->width = width;
oi->height = height;
oi->refresh_rate = refresh;
}
static void wl_output_handle_done(void *data, struct wl_output *output) { }
static void wl_output_handle_scale(void *data,
struct wl_output *output,
int32_t factor)
{
output_info_t *oi = (output_info_t*)data;
oi->scale = factor;
}
static bool wl_setup_data_device(gfx_ctx_wayland_data_t *wl)
{
if (!wl->data_device && wl->data_device_manager && wl->seat)
{
wl->data_device = wl_data_device_manager_get_data_device(
wl->data_device_manager, wl->seat);
if (wl->data_device)
{
wl_data_device_add_listener(wl->data_device,
&data_device_listener, wl);
return true;
}
}
return false;
}
/* Registry callbacks. */
static void wl_registry_handle_global(void *data, struct wl_registry *reg,
uint32_t id, const char *interface, uint32_t version)
{
int found = 1;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
RARCH_DBG("[Wayland] Add global %u, interface %s, version %u.\n",
id, interface, version);
if (string_is_equal(interface, wl_compositor_interface.name) && found++)
wl->compositor = (struct wl_compositor*)wl_registry_bind(reg,
id, &wl_compositor_interface, MIN(version, 4));
else if (string_is_equal(interface, wp_viewporter_interface.name) && found++)
wl->viewporter = (struct wp_viewporter*)wl_registry_bind(reg,
id, &wp_viewporter_interface, MIN(version, 1));
else if (string_is_equal(interface, wp_fractional_scale_manager_v1_interface.name) && found++)
wl->fractional_scale_manager = (struct wp_fractional_scale_manager_v1*)
wl_registry_bind(reg, id, &wp_fractional_scale_manager_v1_interface, MIN(version, 1));
else if (string_is_equal(interface, wl_output_interface.name) && found++)
{
display_output_t *od = (display_output_t*)
calloc(1, sizeof(display_output_t));
output_info_t *oi = (output_info_t*)
calloc(1, sizeof(output_info_t));
od->output = oi;
oi->global_id = id;
oi->output = (struct wl_output*)wl_registry_bind(reg,
id, &wl_output_interface, MIN(version, 2));
wl_output_add_listener(oi->output, &output_listener, oi);
wl_list_insert(&wl->all_outputs, &od->link);
}
else if (string_is_equal(interface, xdg_wm_base_interface.name) && found++)
wl->xdg_shell = (struct xdg_wm_base*)
wl_registry_bind(reg, id, &xdg_wm_base_interface, MIN(version, 3));
else if (string_is_equal(interface, wl_shm_interface.name) && found++)
wl->shm = (struct wl_shm*)wl_registry_bind(reg, id, &wl_shm_interface, MIN(version, 1));
else if (string_is_equal(interface, wl_seat_interface.name) && found++)
{
wl->seat = (struct wl_seat*)wl_registry_bind(reg, id, &wl_seat_interface, MIN(version, 2));
wl_seat_add_listener(wl->seat, &seat_listener, wl);
wl_setup_data_device(wl);
}
else if (string_is_equal(interface, wl_data_device_manager_interface.name) && found++)
{
wl->data_device_manager = (struct wl_data_device_manager*)
wl_registry_bind(
reg, id, &wl_data_device_manager_interface, MIN(version, 3));
wl_setup_data_device(wl);
}
else if (string_is_equal(interface, zwp_idle_inhibit_manager_v1_interface.name) && found++)
wl->idle_inhibit_manager = (struct zwp_idle_inhibit_manager_v1*)
wl_registry_bind(
reg, id, &zwp_idle_inhibit_manager_v1_interface, MIN(version, 1));
else if (string_is_equal(interface, zxdg_decoration_manager_v1_interface.name) && found++)
wl->deco_manager = (struct zxdg_decoration_manager_v1*)
wl_registry_bind(
reg, id, &zxdg_decoration_manager_v1_interface, MIN(version, 1));
else if (string_is_equal(interface, zwp_pointer_constraints_v1_interface.name) && found++)
{
wl->pointer_constraints = (struct zwp_pointer_constraints_v1*)
wl_registry_bind(
reg, id, &zwp_pointer_constraints_v1_interface, MIN(version, 1));
wl->locked_pointer = NULL;
}
else if (string_is_equal(interface, zwp_relative_pointer_manager_v1_interface.name) && found++)
wl->relative_pointer_manager = (struct zwp_relative_pointer_manager_v1*)
wl_registry_bind(
reg, id, &zwp_relative_pointer_manager_v1_interface, MIN(version, 1));
else if (string_is_equal(interface, wp_cursor_shape_manager_v1_interface.name) && found++)
wl->cursor_shape_manager = (struct wp_cursor_shape_manager_v1*)
wl_registry_bind(
reg, id, &wp_cursor_shape_manager_v1_interface, MIN(version, 1));
else if (string_is_equal(interface, wp_content_type_manager_v1_interface.name) && found++)
wl->content_type_manager = (struct wp_content_type_manager_v1*)
wl_registry_bind(
reg, id, &wp_content_type_manager_v1_interface, MIN(version, 1));
else if (string_is_equal(interface, wp_single_pixel_buffer_manager_v1_interface.name) && found++)
wl->single_pixel_manager = (struct wp_single_pixel_buffer_manager_v1*)
wl_registry_bind(
reg, id, &wp_single_pixel_buffer_manager_v1_interface, MIN(version, 1));
else if (string_is_equal(interface, xdg_toplevel_icon_manager_v1_interface.name) && found++)
wl->xdg_toplevel_icon_manager = (struct xdg_toplevel_icon_manager_v1*)
wl_registry_bind(
reg, id, &xdg_toplevel_icon_manager_v1_interface, MIN(version, 1));
else if (string_is_equal(interface, xdg_toplevel_tag_manager_v1_interface.name) && found++)
wl->xdg_toplevel_tag_manager = (struct xdg_toplevel_tag_manager_v1*)
wl_registry_bind(
reg, id, &xdg_toplevel_tag_manager_v1_interface, MIN(version, 1));
if (found > 1)
RARCH_LOG("[Wayland] Registered interface %s at version %u.\n",
interface, version);
}
static void wl_registry_handle_global_remove(void *data,
struct wl_registry *registry, uint32_t id)
{
display_output_t *od, *tmp;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
bool surface_output_removed = false;
wl_list_for_each_safe(od, tmp, &wl->all_outputs, link)
{
if (od->output->global_id == id)
{
if (wl_current_outputs_remove(wl, od->output->output))
surface_output_removed = true;
wl_list_remove(&od->link);
free(od->output);
free(od);
break;
}
}
if (surface_output_removed)
wl_update_scale(wl);
}
static int wl_ioready(int fd, int flags, int timeoutMS)
{
int result;
do
{
struct pollfd info;
info.fd = fd;
info.events = 0;
if (flags & IOR_READ)
info.events |= POLLIN | POLLPRI;
if (flags & IOR_WRITE)
info.events |= POLLOUT;
result = poll(&info, 1, timeoutMS);
} while ( result < 0 && errno == EINTR && !(flags & IOR_NO_RETRY));
return result;
}
static ssize_t wl_read_pipe(int fd, void** buffer, size_t* total_length,
bool null_terminate)
{
char temp[PIPE_BUF];
void* output_buffer = NULL;
size_t new_buffer_length = 0;
ssize_t bytes_read = 0;
size_t pos = 0;
int ready = wl_ioready(fd, IOR_READ, PIPE_MS_TIMEOUT);
if (ready == 0) /* Pipe timeout? */
bytes_read = -1;
else if (ready < 0) /* Pipe select error? */
bytes_read = -1;
else
{
if ((bytes_read = read(fd, temp, sizeof(temp))) > 0)
{
pos = *total_length;
*total_length += bytes_read;
if (null_terminate)
new_buffer_length = *total_length + 1;
else
new_buffer_length = *total_length;
if (*buffer == NULL)
output_buffer = malloc(new_buffer_length);
else
output_buffer = realloc(*buffer, new_buffer_length);
if (output_buffer)
{
memcpy((uint8_t*)output_buffer + pos, temp, bytes_read);
if (null_terminate)
memset((uint8_t*)output_buffer + (new_buffer_length - 1), 0, 1);
*buffer = output_buffer;
}
}
}
return bytes_read;
}
static void *wayland_data_offer_receive(
struct wl_display *display, struct wl_data_offer *offer,
size_t *length,
const char* mime_type, bool null_terminate)
{
int pipefd[2];
void *buffer = NULL;
*length = 0;
if (!offer)
RARCH_WARN("[Wayland] Invalid data offer.\n");
else if (pipe2(pipefd, O_CLOEXEC|O_NONBLOCK) == -1)
RARCH_WARN("[Wayland] Could not read pipe.\n");
else
{
wl_data_offer_receive(offer, mime_type, pipefd[1]);
/* Wait for sending client to transfer */
wl_display_roundtrip(display);
close(pipefd[1]);
while (wl_read_pipe(pipefd[0], &buffer, length, null_terminate) > 0);
close(pipefd[0]);
}
return buffer;
}
static void wl_data_device_handle_data_offer(void *data,
struct wl_data_device *data_device, struct wl_data_offer *offer)
{
data_offer_ctx *offer_data = (data_offer_ctx*)calloc(1, sizeof *offer_data);
offer_data->offer = offer;
offer_data->data_device = data_device;
offer_data->dropped = false;
wl_data_offer_set_user_data(offer, offer_data);
wl_data_offer_add_listener(offer, &data_offer_listener, offer_data);
}
static void wl_data_device_handle_enter(void *data,
struct wl_data_device *data_device, uint32_t serial,
struct wl_surface *surface, wl_fixed_t x, wl_fixed_t y,
struct wl_data_offer *offer)
{
data_offer_ctx *offer_data;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
enum wl_data_device_manager_dnd_action dnd_action =
WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
if (!offer)
return;
offer_data = wl_data_offer_get_user_data(offer);
wl->current_drag_offer = offer_data;
wl_data_offer_accept(offer, serial,
offer_data->is_file_mime_type ? FILE_MIME : NULL);
if ( offer_data->is_file_mime_type
&& offer_data->supported_actions & DND_ACTION)
dnd_action = DND_ACTION;
if ( wl_data_offer_get_version(offer)
>= WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION)
wl_data_offer_set_actions(offer, dnd_action, dnd_action);
}
static void wl_data_device_handle_leave(void *data,
struct wl_data_device *data_device)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
data_offer_ctx *offer_data = wl->current_drag_offer;
if (offer_data && !offer_data->dropped)
{
wl->current_drag_offer = NULL;
wl_data_offer_destroy(offer_data->offer);
free(offer_data);
}
}
static void wl_data_device_handle_motion(void *data,
struct wl_data_device *data_device, uint32_t time,
wl_fixed_t x, wl_fixed_t y) { }
static void wl_data_device_handle_drop(void *data,