summaryrefslogtreecommitdiffstats
path: root/uwac/libuwac/uwac-input.c
blob: b11b1a422f815546576cc35cf93289e13b344f68 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
/*
 * Copyright © 2014-2015 David FORT <contact@hardening-consulting.com>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */
#include "uwac-priv.h"
#include "uwac-utils.h"

#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>

#include "uwac-os.h"
#include "wayland-cursor.h"
#include "wayland-client-protocol.h"

static struct wl_buffer* create_pointer_buffer(UwacSeat* seat, const void* src, size_t size)
{
	struct wl_buffer* buffer = NULL;
	int fd = 0;
	void* data = NULL;
	struct wl_shm_pool* pool = NULL;

	assert(seat);

	fd = uwac_create_anonymous_file(size);

	if (fd < 0)
		return buffer;

	data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

	if (data == MAP_FAILED)
	{
		goto error_mmap;
	}
	memcpy(data, src, size);

	pool = wl_shm_create_pool(seat->display->shm, fd, size);

	if (!pool)
	{
		munmap(data, size);
		goto error_mmap;
	}

	buffer =
	    wl_shm_pool_create_buffer(pool, 0, seat->pointer_image->width, seat->pointer_image->height,
	                              seat->pointer_image->width * 4, WL_SHM_FORMAT_ARGB8888);
	wl_shm_pool_destroy(pool);

	if (munmap(data, size) < 0)
		fprintf(stderr, "%s: munmap(%p, %zu) failed with [%d] %s\n", __func__, data, size, errno,
		        strerror(errno));

error_mmap:
	close(fd);
	return buffer;
}

static void on_buffer_release(void* data, struct wl_buffer* wl_buffer)
{
	(void)data;
	wl_buffer_destroy(wl_buffer);
}

static const struct wl_buffer_listener buffer_release_listener = { on_buffer_release };

static UwacReturnCode set_cursor_image(UwacSeat* seat, uint32_t serial)
{
	struct wl_buffer* buffer = NULL;
	struct wl_cursor* cursor = NULL;
	struct wl_cursor_image* image = NULL;
	struct wl_surface* surface = NULL;
	int32_t x = 0;
	int32_t y = 0;

	if (!seat || !seat->display || !seat->default_cursor || !seat->default_cursor->images)
		return UWAC_ERROR_INTERNAL;

	int scale = 1;
	if (seat->pointer_focus)
		scale = seat->pointer_focus->display->actual_scale;

	switch (seat->pointer_type)
	{
		case 2: /* Custom poiner */
			image = seat->pointer_image;
			buffer = create_pointer_buffer(seat, seat->pointer_data, seat->pointer_size);
			if (!buffer)
				return UWAC_ERROR_INTERNAL;
			if (wl_buffer_add_listener(buffer, &buffer_release_listener, seat) < 0)
				return UWAC_ERROR_INTERNAL;

			surface = seat->pointer_surface;
			x = image->hotspot_x / scale;
			y = image->hotspot_y / scale;
			break;
		case 1: /* NULL pointer */
			break;
		default: /* Default system pointer */
			cursor = seat->default_cursor;
			if (!cursor)
				return UWAC_ERROR_INTERNAL;
			image = cursor->images[0];
			if (!image)
				return UWAC_ERROR_INTERNAL;
			x = image->hotspot_x;
			y = image->hotspot_y;
			buffer = wl_cursor_image_get_buffer(image);
			if (!buffer)
				return UWAC_ERROR_INTERNAL;
			surface = seat->pointer_surface;
			break;
	}

	if (surface && buffer)
	{
		wl_surface_set_buffer_scale(surface, scale);
		wl_surface_attach(surface, buffer, 0, 0);
		wl_surface_damage(surface, 0, 0, image->width, image->height);
		wl_surface_commit(surface);
	}

	wl_pointer_set_cursor(seat->pointer, serial, surface, x, y);

	return UWAC_SUCCESS;
}

static void keyboard_repeat_func(UwacTask* task, uint32_t events)
{
	UwacSeat* input = container_of(task, UwacSeat, repeat_task);
	assert(input);
	UwacWindow* window = input->keyboard_focus;
	uint64_t exp = 0;

	if (read(input->repeat_timer_fd, &exp, sizeof exp) != sizeof exp)
		/* If we change the timer between the fd becoming
		 * readable and getting here, there'll be nothing to
		 * read and we get EAGAIN. */
		return;

	if (window)
	{
		UwacKeyEvent* key = NULL;

		key = (UwacKeyEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_KEY);
		if (!key)
			return;

		key->window = window;
		key->sym = input->repeat_sym;
		key->raw_key = input->repeat_key;
		key->pressed = true;
		key->repeated = true;
	}
}

static void keyboard_handle_keymap(void* data, struct wl_keyboard* keyboard, uint32_t format,
                                   int fd, uint32_t size)
{
	UwacSeat* input = data;
	struct xkb_keymap* keymap = NULL;
	struct xkb_state* state = NULL;
	char* map_str = NULL;
	int mapFlags = MAP_SHARED;

	if (!data)
	{
		close(fd);
		return;
	}

	if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
	{
		close(fd);
		return;
	}

	if (input->seat_version >= 7)
		mapFlags = MAP_PRIVATE;

	map_str = mmap(NULL, size, PROT_READ, mapFlags, fd, 0);
	if (map_str == MAP_FAILED)
	{
		close(fd);
		return;
	}

	keymap = xkb_keymap_new_from_string(input->xkb_context, map_str, XKB_KEYMAP_FORMAT_TEXT_V1, 0);
	munmap(map_str, size);
	close(fd);

	if (!keymap)
	{
		assert(uwacErrorHandler(input->display, UWAC_ERROR_INTERNAL, "failed to compile keymap\n"));
		return;
	}

	state = xkb_state_new(keymap);
	if (!state)
	{
		assert(
		    uwacErrorHandler(input->display, UWAC_ERROR_NOMEMORY, "failed to create XKB state\n"));
		xkb_keymap_unref(keymap);
		return;
	}

	xkb_keymap_unref(input->xkb.keymap);
	xkb_state_unref(input->xkb.state);
	input->xkb.keymap = keymap;
	input->xkb.state = state;

	input->xkb.control_mask = 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Control");
	input->xkb.alt_mask = 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod1");
	input->xkb.shift_mask = 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Shift");
	input->xkb.caps_mask = 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Lock");
	input->xkb.num_mask = 1 << xkb_keymap_mod_get_index(input->xkb.keymap, "Mod2");
}

static void keyboard_handle_key(void* data, struct wl_keyboard* keyboard, uint32_t serial,
                                uint32_t time, uint32_t key, uint32_t state_w);

static void keyboard_handle_enter(void* data, struct wl_keyboard* keyboard, uint32_t serial,
                                  struct wl_surface* surface, struct wl_array* keys)
{
	UwacSeat* input = (UwacSeat*)data;
	assert(input);

	UwacKeyboardEnterLeaveEvent* event = (UwacKeyboardEnterLeaveEvent*)UwacDisplayNewEvent(
	    input->display, UWAC_EVENT_KEYBOARD_ENTER);
	if (!event)
		return;

	event->window = input->keyboard_focus = (UwacWindow*)wl_surface_get_user_data(surface);
	event->seat = input;

	/* we may have the keys in the `keys` array, but as this function is called only
	 * when the window gets focus, so there may be keys from other unrelated windows, eg.
	 * this was leading to problems like passing CTRL+D to freerdp from closing terminal window
	 * if it was closing very fast and the keys was still pressed by the user while the freerdp
	 * gets focus
	 *
	 * currently just ignore this, as further key presses will be handled correctly anyway
	 */
}

static void keyboard_handle_leave(void* data, struct wl_keyboard* keyboard, uint32_t serial,
                                  struct wl_surface* surface)
{
	struct itimerspec its = { 0 };
	uint32_t* pressedKey = NULL;
	size_t i = 0;

	UwacSeat* input = (UwacSeat*)data;
	assert(input);

	its.it_interval.tv_sec = 0;
	its.it_interval.tv_nsec = 0;
	its.it_value.tv_sec = 0;
	its.it_value.tv_nsec = 0;
	timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);

	UwacPointerEnterLeaveEvent* event =
	    (UwacPointerEnterLeaveEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_POINTER_LEAVE);
	if (!event)
		return;

	event->window = input->keyboard_focus;

	/* we are currently loosing input focus of the main window:
	 * check if we currently have some keys pressed and release them as if we enter the window again
	 * it will be still "virtually" pressed in remote even if in reality the key has been released
	 */
	for (pressedKey = input->pressed_keys.data, i = 0; i < input->pressed_keys.size;
	     i += sizeof(uint32_t))
	{
		keyboard_handle_key(data, keyboard, serial, 0, *pressedKey, WL_KEYBOARD_KEY_STATE_RELEASED);
		pressedKey++;
	}
}

static int update_key_pressed(UwacSeat* seat, uint32_t key)
{
	uint32_t* keyPtr = NULL;
	assert(seat);

	/* check if the key is not already pressed */
	wl_array_for_each(keyPtr, &seat->pressed_keys)
	{
		if (*keyPtr == key)
			return 1;
	}

	keyPtr = wl_array_add(&seat->pressed_keys, sizeof(uint32_t));
	if (!keyPtr)
		return -1;

	*keyPtr = key;
	return 0;
}

static int update_key_released(UwacSeat* seat, uint32_t key)
{
	size_t toMove = 0;
	bool found = false;

	assert(seat);

	size_t i = 0;
	uint32_t* keyPtr = seat->pressed_keys.data;
	for (; i < seat->pressed_keys.size; i++, keyPtr++)
	{
		if (*keyPtr == key)
		{
			found = true;
			break;
		}
	}

	if (found)
	{
		toMove = seat->pressed_keys.size - ((i + 1) * sizeof(uint32_t));
		if (toMove)
			memmove(keyPtr, keyPtr + 1, toMove);

		seat->pressed_keys.size -= sizeof(uint32_t);
	}
	return 1;
}

static void keyboard_handle_key(void* data, struct wl_keyboard* keyboard, uint32_t serial,
                                uint32_t time, uint32_t key, uint32_t state_w)
{
	UwacSeat* input = (UwacSeat*)data;
	assert(input);

	UwacWindow* window = input->keyboard_focus;
	UwacKeyEvent* keyEvent = NULL;

	uint32_t code = 0;
	uint32_t num_syms = 0;
	enum wl_keyboard_key_state state = state_w;
	const xkb_keysym_t* syms = NULL;
	xkb_keysym_t sym = 0;
	struct itimerspec its;

	if (state_w == WL_KEYBOARD_KEY_STATE_PRESSED)
		update_key_pressed(input, key);
	else
		update_key_released(input, key);

	input->display->serial = serial;
	code = key + 8;
	if (!window || !input->xkb.state)
		return;

	/* We only use input grabs for pointer events for now, so just
	 * ignore key presses if a grab is active.  We expand the key
	 * event delivery mechanism to route events to widgets to
	 * properly handle key grabs.  In the meantime, this prevents
	 * key event delivery while a grab is active. */
	/*if (input->grab && input->grab_button == 0)
	    return;*/

	num_syms = xkb_state_key_get_syms(input->xkb.state, code, &syms);

	sym = XKB_KEY_NoSymbol;
	if (num_syms == 1)
		sym = syms[0];

	if (state == WL_KEYBOARD_KEY_STATE_RELEASED && key == input->repeat_key)
	{
		its.it_interval.tv_sec = 0;
		its.it_interval.tv_nsec = 0;
		its.it_value.tv_sec = 0;
		its.it_value.tv_nsec = 0;
		timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
	}
	else if (state == WL_KEYBOARD_KEY_STATE_PRESSED &&
	         xkb_keymap_key_repeats(input->xkb.keymap, code))
	{
		input->repeat_sym = sym;
		input->repeat_key = key;
		input->repeat_time = time;
		its.it_interval.tv_sec = input->repeat_rate_sec;
		its.it_interval.tv_nsec = input->repeat_rate_nsec;
		its.it_value.tv_sec = input->repeat_delay_sec;
		its.it_value.tv_nsec = input->repeat_delay_nsec;
		timerfd_settime(input->repeat_timer_fd, 0, &its, NULL);
	}

	keyEvent = (UwacKeyEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_KEY);
	if (!keyEvent)
		return;

	keyEvent->window = window;
	keyEvent->sym = sym;
	keyEvent->raw_key = key;
	keyEvent->pressed = (state == WL_KEYBOARD_KEY_STATE_PRESSED);
	keyEvent->repeated = false;
}

static void keyboard_handle_modifiers(void* data, struct wl_keyboard* keyboard, uint32_t serial,
                                      uint32_t mods_depressed, uint32_t mods_latched,
                                      uint32_t mods_locked, uint32_t group)
{
	UwacSeat* input = data;
	assert(input);

	UwacKeyboardModifiersEvent* event = NULL;
	xkb_mod_mask_t mask = 0;

	/* If we're not using a keymap, then we don't handle PC-style modifiers */
	if (!input->xkb.keymap)
		return;

	xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
	mask = xkb_state_serialize_mods(input->xkb.state, XKB_STATE_MODS_DEPRESSED |
	                                                      XKB_STATE_MODS_LATCHED |
	                                                      XKB_STATE_MODS_LOCKED);
	input->modifiers = 0;
	if (mask & input->xkb.control_mask)
		input->modifiers |= UWAC_MOD_CONTROL_MASK;
	if (mask & input->xkb.alt_mask)
		input->modifiers |= UWAC_MOD_ALT_MASK;
	if (mask & input->xkb.shift_mask)
		input->modifiers |= UWAC_MOD_SHIFT_MASK;
	if (mask & input->xkb.caps_mask)
		input->modifiers |= UWAC_MOD_CAPS_MASK;
	if (mask & input->xkb.num_mask)
		input->modifiers |= UWAC_MOD_NUM_MASK;

	event = (UwacKeyboardModifiersEvent*)UwacDisplayNewEvent(input->display,
	                                                         UWAC_EVENT_KEYBOARD_MODIFIERS);
	if (!event)
		return;

	event->modifiers = input->modifiers;
}

static void set_repeat_info(UwacSeat* input, int32_t rate, int32_t delay)
{
	assert(input);

	input->repeat_rate_sec = input->repeat_rate_nsec = 0;
	input->repeat_delay_sec = input->repeat_delay_nsec = 0;

	/* a rate of zero disables any repeating, regardless of the delay's
	 * value */
	if (rate == 0)
		return;

	if (rate == 1)
		input->repeat_rate_sec = 1;
	else
		input->repeat_rate_nsec = 1000000000 / rate;

	input->repeat_delay_sec = delay / 1000;
	delay -= (input->repeat_delay_sec * 1000);
	input->repeat_delay_nsec = delay * 1000 * 1000;
}

static void keyboard_handle_repeat_info(void* data, struct wl_keyboard* keyboard, int32_t rate,
                                        int32_t delay)
{
	UwacSeat* input = data;
	assert(input);

	set_repeat_info(input, rate, delay);
}

static const struct wl_keyboard_listener keyboard_listener = {
	keyboard_handle_keymap, keyboard_handle_enter,     keyboard_handle_leave,
	keyboard_handle_key,    keyboard_handle_modifiers, keyboard_handle_repeat_info
};

static bool touch_send_start_frame(UwacSeat* seat)
{
	assert(seat);

	UwacTouchFrameBegin* ev =
	    (UwacTouchFrameBegin*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_FRAME_BEGIN);
	if (!ev)
		return false;

	seat->touch_frame_started = true;
	return true;
}

static void 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_w,
                              wl_fixed_t y_w)
{
	UwacSeat* seat = data;
	UwacTouchDown* tdata = NULL;

	assert(seat);
	assert(seat->display);

	seat->display->serial = serial;
	if (!seat->touch_frame_started && !touch_send_start_frame(seat))
		return;

	tdata = (UwacTouchDown*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_DOWN);
	if (!tdata)
		return;

	tdata->seat = seat;
	tdata->id = id;

	float sx = wl_fixed_to_double(x_w);
	float sy = wl_fixed_to_double(y_w);

	tdata->x = sx;
	tdata->y = sy;

#if 0
	struct widget *widget;
	float sx = wl_fixed_to_double(x);
	float sy = wl_fixed_to_double(y);


	input->touch_focus = wl_surface_get_user_data(surface);
	if (!input->touch_focus) {
		DBG("Failed to find to touch focus for surface %p\n", (void*) surface);
		return;
	}

	if (surface != input->touch_focus->main_surface->surface) {
		DBG("Ignoring input event from subsurface %p\n", (void*) surface);
		input->touch_focus = NULL;
		return;
	}

	if (input->grab)
		widget = input->grab;
	else
		widget = window_find_widget(input->touch_focus,
					    wl_fixed_to_double(x),
					    wl_fixed_to_double(y));
	if (widget) {
		struct touch_point *tp = xmalloc(sizeof *tp);
		if (tp) {
			tp->id = id;
			tp->widget = widget;
			tp->x = sx;
			tp->y = sy;
			wl_list_insert(&input->touch_point_list, &tp->link);

			if (widget->touch_down_handler)
				(*widget->touch_down_handler)(widget, input,
							      serial, time, id,
							      sx, sy,
							      widget->user_data);
		}
	}
#endif
}

static void touch_handle_up(void* data, struct wl_touch* wl_touch, uint32_t serial, uint32_t time,
                            int32_t id)
{
	UwacSeat* seat = data;
	UwacTouchUp* tdata = NULL;

	assert(seat);

	if (!seat->touch_frame_started && !touch_send_start_frame(seat))
		return;

	tdata = (UwacTouchUp*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_UP);
	if (!tdata)
		return;

	tdata->seat = seat;
	tdata->id = id;

#if 0
	struct touch_point *tp, *tmp;

	if (!input->touch_focus) {
		DBG("No touch focus found for touch up event!\n");
		return;
	}

	wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
		if (tp->id != id)
			continue;

		if (tp->widget->touch_up_handler)
			(*tp->widget->touch_up_handler)(tp->widget, input, serial,
							time, id,
							tp->widget->user_data);

		wl_list_remove(&tp->link);
		free(tp);

		return;
	}
#endif
}

static void touch_handle_motion(void* data, struct wl_touch* wl_touch, uint32_t time, int32_t id,
                                wl_fixed_t x_w, wl_fixed_t y_w)
{
	UwacSeat* seat = data;
	assert(seat);

	UwacTouchMotion* tdata = NULL;

	if (!seat->touch_frame_started && !touch_send_start_frame(seat))
		return;

	tdata = (UwacTouchMotion*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_MOTION);
	if (!tdata)
		return;

	tdata->seat = seat;
	tdata->id = id;

	float sx = wl_fixed_to_double(x_w);
	float sy = wl_fixed_to_double(y_w);

	tdata->x = sx;
	tdata->y = sy;

#if 0
	struct touch_point *tp;
	float sx = wl_fixed_to_double(x);
	float sy = wl_fixed_to_double(y);

	DBG("touch_handle_motion: %i %i\n", id, wl_list_length(&seat->touch_point_list));

	if (!seat->touch_focus) {
		DBG("No touch focus found for touch motion event!\n");
		return;
	}

	wl_list_for_each(tp, &seat->touch_point_list, link) {
		if (tp->id != id)
			continue;

		tp->x = sx;
		tp->y = sy;
		if (tp->widget->touch_motion_handler)
			(*tp->widget->touch_motion_handler)(tp->widget, seat, time,
							    id, sx, sy,
							    tp->widget->user_data);
		return;
	}
#endif
}

static void touch_handle_frame(void* data, struct wl_touch* wl_touch)
{
	UwacSeat* seat = data;
	assert(seat);

	UwacTouchFrameEnd* ev =
	    (UwacTouchFrameEnd*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_FRAME_END);
	if (!ev)
		return;

	ev->seat = seat;
	seat->touch_frame_started = false;
}

static void touch_handle_cancel(void* data, struct wl_touch* wl_touch)
{
	UwacSeat* seat = data;
	assert(seat);

	UwacTouchCancel* ev =
	    (UwacTouchCancel*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_TOUCH_CANCEL);
	if (!ev)
		return;

	ev->seat = seat;
	seat->touch_frame_started = false;

#if 0
	struct touch_point *tp, *tmp;

	DBG("touch_handle_cancel\n");

	if (!input->touch_focus) {
		DBG("No touch focus found for touch cancel event!\n");
		return;
	}

	wl_list_for_each_safe(tp, tmp, &input->touch_point_list, link) {
		if (tp->widget->touch_cancel_handler)
			(*tp->widget->touch_cancel_handler)(tp->widget, input,
							    tp->widget->user_data);

		wl_list_remove(&tp->link);
		free(tp);
	}
#endif
}

static void touch_handle_shape(void* data, struct wl_touch* wl_touch, int32_t id, wl_fixed_t major,
                               wl_fixed_t minor)
{
	UwacSeat* seat = data;
	assert(seat);

	// TODO
}

static void touch_handle_orientation(void* data, struct wl_touch* wl_touch, int32_t id,
                                     wl_fixed_t orientation)
{
	UwacSeat* seat = data;
	assert(seat);

	// TODO
}

static const struct wl_touch_listener touch_listener = {
	touch_handle_down,   touch_handle_up,    touch_handle_motion,     touch_handle_frame,
	touch_handle_cancel, touch_handle_shape, touch_handle_orientation
};

static void pointer_handle_enter(void* data, struct wl_pointer* pointer, uint32_t serial,
                                 struct wl_surface* surface, wl_fixed_t sx_w, wl_fixed_t sy_w)
{
	UwacSeat* input = data;
	UwacWindow* window = NULL;
	UwacPointerEnterLeaveEvent* event = NULL;

	assert(input);

	float sx = wl_fixed_to_double(sx_w);
	float sy = wl_fixed_to_double(sy_w);

	if (!surface)
	{
		/* enter event for a window we've just destroyed */
		return;
	}

	input->display->serial = serial;
	input->display->pointer_focus_serial = serial;
	window = wl_surface_get_user_data(surface);
	if (window)
		window->pointer_enter_serial = serial;
	input->pointer_focus = window;
	input->sx = sx;
	input->sy = sy;

	event =
	    (UwacPointerEnterLeaveEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_POINTER_ENTER);
	if (!event)
		return;

	event->seat = input;
	event->window = window;
	event->x = sx;
	event->y = sy;

	/* Apply cursor theme */
	set_cursor_image(input, serial);
}

static void pointer_handle_leave(void* data, struct wl_pointer* pointer, uint32_t serial,
                                 struct wl_surface* surface)
{
	UwacPointerEnterLeaveEvent* event = NULL;
	UwacWindow* window = NULL;
	UwacSeat* input = data;
	assert(input);

	input->display->serial = serial;

	event =
	    (UwacPointerEnterLeaveEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_POINTER_LEAVE);
	if (!event)
		return;

	window = wl_surface_get_user_data(surface);

	event->seat = input;
	event->window = window;
}

static void pointer_handle_motion(void* data, struct wl_pointer* pointer, uint32_t time,
                                  wl_fixed_t sx_w, wl_fixed_t sy_w)
{
	UwacPointerMotionEvent* motion_event = NULL;
	UwacSeat* input = data;
	assert(input);

	UwacWindow* window = input->pointer_focus;
	if (!window || !window->display)
		return;

	int scale = window->display->actual_scale;
	int sx_i = wl_fixed_to_int(sx_w) * scale;
	int sy_i = wl_fixed_to_int(sy_w) * scale;
	double sx_d = wl_fixed_to_double(sx_w) * scale;
	double sy_d = wl_fixed_to_double(sy_w) * scale;

	if ((sx_i < 0) || (sy_i < 0))
		return;

	input->sx = sx_d;
	input->sy = sy_d;

	motion_event =
	    (UwacPointerMotionEvent*)UwacDisplayNewEvent(input->display, UWAC_EVENT_POINTER_MOTION);
	if (!motion_event)
		return;

	motion_event->seat = input;
	motion_event->window = window;
	motion_event->x = sx_i;
	motion_event->y = sy_i;
}

static void pointer_handle_button(void* data, struct wl_pointer* pointer, uint32_t serial,
                                  uint32_t time, uint32_t button, uint32_t state_w)
{
	UwacPointerButtonEvent* event = NULL;
	UwacSeat* seat = data;
	assert(seat);

	UwacWindow* window = seat->pointer_focus;

	seat->display->serial = serial;

	event = (UwacPointerButtonEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_BUTTONS);
	if (!event)
		return;

	event->seat = seat;
	event->window = window;
	event->x = seat->sx;
	event->y = seat->sy;
	event->button = button;
	event->state = (enum wl_pointer_button_state)state_w;
}

static void pointer_handle_axis(void* data, struct wl_pointer* pointer, uint32_t time,
                                uint32_t axis, wl_fixed_t value)
{
	UwacPointerAxisEvent* event = NULL;
	UwacSeat* seat = data;
	assert(seat);

	UwacWindow* window = seat->pointer_focus;

	if (!window)
		return;

	event = (UwacPointerAxisEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_AXIS);
	if (!event)
		return;

	event->seat = seat;
	event->window = window;
	event->x = seat->sx;
	event->y = seat->sy;
	event->axis = axis;
	event->value = value;
}

static void pointer_frame(void* data, struct wl_pointer* wl_pointer)
{
	UwacPointerFrameEvent* event = NULL;
	UwacSeat* seat = data;
	assert(seat);

	UwacWindow* window = seat->pointer_focus;

	if (!window)
		return;

	event = (UwacPointerFrameEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_FRAME);
	if (!event)
		return;

	event->seat = seat;
	event->window = window;
}

static void pointer_axis_source(void* data, struct wl_pointer* wl_pointer, uint32_t axis_source)
{
	UwacPointerSourceEvent* event = NULL;
	UwacSeat* seat = data;
	assert(seat);

	UwacWindow* window = seat->pointer_focus;

	if (!window)
		return;

	event = (UwacPointerSourceEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_SOURCE);
	if (!event)
		return;

	event->seat = seat;
	event->window = window;
	event->axis_source = axis_source;
}

static void pointer_axis_stop(void* data, struct wl_pointer* wl_pointer, uint32_t time,
                              uint32_t axis)
{
	UwacSeat* seat = data;
	assert(seat);
}

static void pointer_axis_discrete(void* data, struct wl_pointer* wl_pointer, uint32_t axis,
                                  int32_t discrete)
{
	/*UwacSeat *seat = data;*/
	UwacPointerAxisEvent* event = NULL;
	UwacSeat* seat = data;
	assert(seat);

	UwacWindow* window = seat->pointer_focus;

	if (!window)
		return;

	event =
	    (UwacPointerAxisEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_AXIS_DISCRETE);
	if (!event)
		return;

	event->seat = seat;
	event->window = window;
	event->x = seat->sx;
	event->y = seat->sy;
	event->axis = axis;
	event->value = discrete;
}

static void pointer_axis_value120(void* data, struct wl_pointer* wl_pointer, uint32_t axis,
                                  int32_t value120)
{
	/*UwacSeat *seat = data;*/
	UwacPointerAxisEvent* event = NULL;
	UwacSeat* seat = data;
	assert(seat);

	UwacWindow* window = seat->pointer_focus;

	if (!window)
		return;

	event =
	    (UwacPointerAxisEvent*)UwacDisplayNewEvent(seat->display, UWAC_EVENT_POINTER_AXIS_DISCRETE);
	if (!event)
		return;

	event->seat = seat;
	event->window = window;
	event->x = seat->sx;
	event->y = seat->sy;
	event->axis = axis;
	event->value = value120 / 120;
}

static const struct wl_pointer_listener pointer_listener = {
	pointer_handle_enter,  pointer_handle_leave, pointer_handle_motion, pointer_handle_button,
	pointer_handle_axis,   pointer_frame,        pointer_axis_source,   pointer_axis_stop,
	pointer_axis_discrete, pointer_axis_value120
};

static void seat_handle_capabilities(void* data, struct wl_seat* seat, enum wl_seat_capability caps)
{
	UwacSeat* input = data;
	assert(input);

	if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer)
	{
		input->pointer = wl_seat_get_pointer(seat);
		wl_pointer_set_user_data(input->pointer, input);
		wl_pointer_add_listener(input->pointer, &pointer_listener, input);

		input->cursor_theme = wl_cursor_theme_load(NULL, 32, input->display->shm);
		if (!input->cursor_theme)
		{
			assert(uwacErrorHandler(input->display, UWAC_ERROR_NOMEMORY,
			                        "unable to get wayland cursor theme\n"));
			return;
		}

		input->default_cursor = wl_cursor_theme_get_cursor(input->cursor_theme, "left_ptr");
		if (!input->default_cursor)
		{
			assert(uwacErrorHandler(input->display, UWAC_ERROR_NOMEMORY,
			                        "unable to get wayland cursor left_ptr\n"));
			return;
		}
	}
	else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer)
	{
#ifdef WL_POINTER_RELEASE_SINCE_VERSION
		if (input->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
			wl_pointer_release(input->pointer);
		else
#endif
			wl_pointer_destroy(input->pointer);
		if (input->cursor_theme)
			wl_cursor_theme_destroy(input->cursor_theme);

		input->default_cursor = NULL;
		input->cursor_theme = NULL;
		input->pointer = NULL;
	}

	if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard)
	{
		input->keyboard = wl_seat_get_keyboard(seat);
		wl_keyboard_set_user_data(input->keyboard, input);
		wl_keyboard_add_listener(input->keyboard, &keyboard_listener, input);
	}
	else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard)
	{
#ifdef WL_KEYBOARD_RELEASE_SINCE_VERSION
		if (input->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
			wl_keyboard_release(input->keyboard);
		else
#endif
			wl_keyboard_destroy(input->keyboard);
		input->keyboard = NULL;
	}

	if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch)
	{
		input->touch = wl_seat_get_touch(seat);
		wl_touch_set_user_data(input->touch, input);
		wl_touch_add_listener(input->touch, &touch_listener, input);
	}
	else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch)
	{
#ifdef WL_TOUCH_RELEASE_SINCE_VERSION
		if (input->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
			wl_touch_release(input->touch);
		else
#endif
			wl_touch_destroy(input->touch);
		input->touch = NULL;
	}
}

static void seat_handle_name(void* data, struct wl_seat* seat, const char* name)
{
	UwacSeat* input = data;
	assert(input);

	if (input->name)
		free(input->name);

	input->name = strdup(name);
	if (!input->name)
		assert(uwacErrorHandler(input->display, UWAC_ERROR_NOMEMORY,
		                        "unable to strdup seat's name\n"));
}

static const struct wl_seat_listener seat_listener = { seat_handle_capabilities, seat_handle_name };

UwacSeat* UwacSeatNew(UwacDisplay* d, uint32_t id, uint32_t version)
{
	UwacSeat* ret = xzalloc(sizeof(UwacSeat));
	if (!ret)
		return NULL;

	ret->display = d;
	ret->seat_id = id;
	ret->seat_version = version;

	wl_array_init(&ret->pressed_keys);
	ret->xkb_context = xkb_context_new(0);
	if (!ret->xkb_context)
	{
		fprintf(stderr, "%s: unable to allocate a xkb_context\n", __func__);
		goto fail;
	}

	ret->seat = wl_registry_bind(d->registry, id, &wl_seat_interface, version);
	wl_seat_add_listener(ret->seat, &seat_listener, ret);
	wl_seat_set_user_data(ret->seat, ret);

	ret->repeat_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
	if (ret->repeat_timer_fd < 0)
	{
		fprintf(stderr, "%s: error creating repeat timer\n", __func__);
		goto fail;
	}
	ret->repeat_task.run = keyboard_repeat_func;
	if (UwacDisplayWatchFd(d, ret->repeat_timer_fd, EPOLLIN, &ret->repeat_task) < 0)
	{
		fprintf(stderr, "%s: error polling repeat timer\n", __func__);
		goto fail;
	}

	wl_list_insert(d->seats.prev, &ret->link);
	return ret;

fail:
	UwacSeatDestroy(ret);
	return NULL;
}

void UwacSeatDestroy(UwacSeat* s)
{
	if (!s)
		return;

	UwacSeatInhibitShortcuts(s, false);
	if (s->seat)
	{
#ifdef WL_SEAT_RELEASE_SINCE_VERSION
		if (s->seat_version >= WL_SEAT_RELEASE_SINCE_VERSION)
			wl_seat_release(s->seat);
		else
#endif
			wl_seat_destroy(s->seat);
	}
	s->seat = NULL;

	free(s->name);
	wl_array_release(&s->pressed_keys);

	xkb_state_unref(s->xkb.state);
	xkb_context_unref(s->xkb_context);

	if (s->pointer)
	{
#ifdef WL_POINTER_RELEASE_SINCE_VERSION
		if (s->seat_version >= WL_POINTER_RELEASE_SINCE_VERSION)
			wl_pointer_release(s->pointer);
		else
#endif
			wl_pointer_destroy(s->pointer);
	}

	if (s->touch)
	{
#ifdef WL_TOUCH_RELEASE_SINCE_VERSION
		if (s->seat_version >= WL_TOUCH_RELEASE_SINCE_VERSION)
			wl_touch_release(s->touch);
		else
#endif
			wl_touch_destroy(s->touch);
	}

	if (s->keyboard)
	{
#ifdef WL_KEYBOARD_RELEASE_SINCE_VERSION
		if (s->seat_version >= WL_KEYBOARD_RELEASE_SINCE_VERSION)
			wl_keyboard_release(s->keyboard);
		else
#endif
			wl_keyboard_destroy(s->keyboard);
	}

	if (s->data_device)
		wl_data_device_destroy(s->data_device);

	if (s->data_source)
		wl_data_source_destroy(s->data_source);

	if (s->pointer_surface)
		wl_surface_destroy(s->pointer_surface);

	free(s->pointer_image);
	free(s->pointer_data);

	wl_list_remove(&s->link);
	free(s);
}

const char* UwacSeatGetName(const UwacSeat* seat)
{
	assert(seat);
	return seat->name;
}

UwacSeatId UwacSeatGetId(const UwacSeat* seat)
{
	assert(seat);
	return seat->seat_id;
}

UwacReturnCode UwacSeatInhibitShortcuts(UwacSeat* s, bool inhibit)
{
	if (!s)
		return UWAC_ERROR_CLOSED;

	if (s->keyboard_inhibitor)
	{
		zwp_keyboard_shortcuts_inhibitor_v1_destroy(s->keyboard_inhibitor);
		s->keyboard_inhibitor = NULL;
	}
	if (inhibit && s->display && s->display->keyboard_inhibit_manager)
		s->keyboard_inhibitor = zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(
		    s->display->keyboard_inhibit_manager, s->keyboard_focus->surface, s->seat);

	if (inhibit && !s->keyboard_inhibitor)
		return UWAC_ERROR_INTERNAL;
	return UWAC_SUCCESS;
}

UwacReturnCode UwacSeatSetMouseCursor(UwacSeat* seat, const void* data, size_t length, size_t width,
                                      size_t height, size_t hot_x, size_t hot_y)
{
	if (!seat)
		return UWAC_ERROR_CLOSED;

	free(seat->pointer_image);
	seat->pointer_image = NULL;

	free(seat->pointer_data);
	seat->pointer_data = NULL;
	seat->pointer_size = 0;

	/* There is a cursor provided */
	if ((data != NULL) && (length != 0))
	{
		seat->pointer_image = xzalloc(sizeof(struct wl_cursor_image));
		if (!seat->pointer_image)
			return UWAC_ERROR_NOMEMORY;
		seat->pointer_image->width = width;
		seat->pointer_image->height = height;
		seat->pointer_image->hotspot_x = hot_x;
		seat->pointer_image->hotspot_y = hot_y;

		free(seat->pointer_data);
		seat->pointer_data = xmalloc(length);
		memcpy(seat->pointer_data, data, length);
		seat->pointer_size = length;

		seat->pointer_type = 2;
	}
	/* We want to use the system cursor */
	else if (length != 0)
	{
		seat->pointer_type = 0;
	}
	/* Hide the cursor */
	else
	{
		seat->pointer_type = 1;
	}
	if (seat && !seat->default_cursor)
		return UWAC_SUCCESS;
	return set_cursor_image(seat, seat->display->pointer_focus_serial);
}