summaryrefslogtreecommitdiffstats
path: root/uwac/libuwac/uwac-display.c
blob: 1b26e219283a8c2a97a47a0ae869a3a9d4f2012d (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
/*
 * Copyright © 2014 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 <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/epoll.h>

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

#define TARGET_COMPOSITOR_INTERFACE 3U
#define TARGET_SHM_INTERFACE 1U
#define TARGET_SHELL_INTERFACE 1U
#define TARGET_DDM_INTERFACE 1U
#define TARGET_SEAT_INTERFACE 5U
#define TARGET_XDG_VERSION 5U /* The version of xdg-shell that we implement */

#if !defined(NDEBUG)
static const char* event_names[] = {
	"new seat",      "removed seat",      "new output",      "configure",    "pointer enter",
	"pointer leave", "pointer motion",    "pointer buttons", "pointer axis", "keyboard enter",
	"key",           "touch frame begin", "touch up",        "touch down",   "touch motion",
	"touch cancel",  "touch frame end",   "frame done",      "close",        NULL
};
#endif

static bool uwac_default_error_handler(UwacDisplay* display, UwacReturnCode code, const char* msg,
                                       ...)
{
	va_list args;
	va_start(args, msg);
	vfprintf(stderr, "%s", args);
	va_end(args);
	return false;
}

UwacErrorHandler uwacErrorHandler = uwac_default_error_handler;

void UwacInstallErrorHandler(UwacErrorHandler handler)
{
	if (handler)
		uwacErrorHandler = handler;
	else
		uwacErrorHandler = uwac_default_error_handler;
}

static void cb_shm_format(void* data, struct wl_shm* wl_shm, uint32_t format)
{
	UwacDisplay* d = data;

	if (format == WL_SHM_FORMAT_RGB565)
		d->has_rgb565 = true;

	d->shm_formats_nb++;
	d->shm_formats =
	    xrealloc((void*)d->shm_formats, sizeof(enum wl_shm_format) * d->shm_formats_nb);
	d->shm_formats[d->shm_formats_nb - 1] = format;
}

static struct wl_shm_listener shm_listener = { cb_shm_format };

static void xdg_shell_ping(void* data, struct xdg_wm_base* xdg_wm_base, uint32_t serial)
{
	xdg_wm_base_pong(xdg_wm_base, serial);
}

static const struct xdg_wm_base_listener xdg_wm_base_listener = {
	xdg_shell_ping,
};

#ifdef BUILD_FULLSCREEN_SHELL
static void fullscreen_capability(void* data,
                                  struct zwp_fullscreen_shell_v1* zwp_fullscreen_shell_v1,
                                  uint32_t capability)
{
}

static const struct zwp_fullscreen_shell_v1_listener fullscreen_shell_listener = {
	fullscreen_capability,
};
#endif

static void display_destroy_seat(UwacDisplay* d, uint32_t name)
{
	UwacSeat* seat = NULL;
	UwacSeat* tmp = NULL;
	wl_list_for_each_safe(seat, tmp, &d->seats, link)
	{
		if (seat->seat_id == name)
		{
			UwacSeatDestroy(seat);
		}
	}
}

static void UwacSeatRegisterDDM(UwacSeat* seat)
{
	UwacDisplay* d = seat->display;
	if (!d->data_device_manager)
		return;

	if (!seat->data_device)
		seat->data_device =
		    wl_data_device_manager_get_data_device(d->data_device_manager, seat->seat);
}

static void UwacRegisterCursor(UwacSeat* seat)
{
	if (!seat || !seat->display || !seat->display->compositor)
		return;

	seat->pointer_surface = wl_compositor_create_surface(seat->display->compositor);
}

static void registry_handle_global(void* data, struct wl_registry* registry, uint32_t id,
                                   const char* interface, uint32_t version)
{
	UwacDisplay* d = data;
	UwacGlobal* global = NULL;
	global = xzalloc(sizeof *global);
	global->name = id;
	global->interface = xstrdup(interface);
	global->version = version;
	wl_list_insert(d->globals.prev, &global->link);

	if (strcmp(interface, "wl_compositor") == 0)
	{
		d->compositor = wl_registry_bind(registry, id, &wl_compositor_interface,
		                                 min(TARGET_COMPOSITOR_INTERFACE, version));
	}
	else if (strcmp(interface, "wl_shm") == 0)
	{
		d->shm =
		    wl_registry_bind(registry, id, &wl_shm_interface, min(TARGET_SHM_INTERFACE, version));
		wl_shm_add_listener(d->shm, &shm_listener, d);
	}
	else if (strcmp(interface, "wl_output") == 0)
	{
		UwacOutput* output = NULL;
		UwacOutputNewEvent* ev = NULL;
		output = UwacCreateOutput(d, id, version);

		if (!output)
		{
			assert(uwacErrorHandler(d, UWAC_ERROR_NOMEMORY, "unable to create output\n"));
			return;
		}

		ev = (UwacOutputNewEvent*)UwacDisplayNewEvent(d, UWAC_EVENT_NEW_OUTPUT);

		if (ev)
			ev->output = output;
	}
	else if (strcmp(interface, "wl_seat") == 0)
	{
		UwacSeatNewEvent* ev = NULL;
		UwacSeat* seat = NULL;
		seat = UwacSeatNew(d, id, min(version, TARGET_SEAT_INTERFACE));

		if (!seat)
		{
			assert(uwacErrorHandler(d, UWAC_ERROR_NOMEMORY, "unable to create new seat\n"));
			return;
		}

		UwacSeatRegisterDDM(seat);
		UwacSeatRegisterClipboard(seat);
		UwacRegisterCursor(seat);
		ev = (UwacSeatNewEvent*)UwacDisplayNewEvent(d, UWAC_EVENT_NEW_SEAT);

		if (!ev)
		{
			assert(uwacErrorHandler(d, UWAC_ERROR_NOMEMORY, "unable to create new seat event\n"));
			return;
		}

		ev->seat = seat;
	}
	else if (strcmp(interface, "wl_data_device_manager") == 0)
	{
		UwacSeat* seat = NULL;
		UwacSeat* tmp = NULL;

		d->data_device_manager = wl_registry_bind(registry, id, &wl_data_device_manager_interface,
		                                          min(TARGET_DDM_INTERFACE, version));

		wl_list_for_each_safe(seat, tmp, &d->seats, link)
		{
			UwacSeatRegisterDDM(seat);
			UwacSeatRegisterClipboard(seat);
			UwacRegisterCursor(seat);
		}
	}
	else if (strcmp(interface, "wl_shell") == 0)
	{
		d->shell = wl_registry_bind(registry, id, &wl_shell_interface,
		                            min(TARGET_SHELL_INTERFACE, version));
	}
	else if (strcmp(interface, "xdg_wm_base") == 0)
	{
		d->xdg_base = wl_registry_bind(registry, id, &xdg_wm_base_interface, 1);
		xdg_wm_base_add_listener(d->xdg_base, &xdg_wm_base_listener, d);
	}
	else if (strcmp(interface, "wp_viewporter") == 0)
	{
		d->viewporter = wl_registry_bind(registry, id, &wp_viewporter_interface, 1);
	}
	else if (strcmp(interface, "zwp_keyboard_shortcuts_inhibit_manager_v1") == 0)
	{
		d->keyboard_inhibit_manager =
		    wl_registry_bind(registry, id, &zwp_keyboard_shortcuts_inhibit_manager_v1_interface, 1);
	}
	else if (strcmp(interface, "zxdg_decoration_manager_v1") == 0)
	{
		d->deco_manager = wl_registry_bind(registry, id, &zxdg_decoration_manager_v1_interface, 1);
	}
	else if (strcmp(interface, "org_kde_kwin_server_decoration_manager") == 0)
	{
		d->kde_deco_manager =
		    wl_registry_bind(registry, id, &org_kde_kwin_server_decoration_manager_interface, 1);
	}
#if BUILD_IVI
	else if (strcmp(interface, "ivi_application") == 0)
	{
		d->ivi_application = wl_registry_bind(registry, id, &ivi_application_interface, 1);
	}
#endif
#if BUILD_FULLSCREEN_SHELL
	else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0)
	{
		d->fullscreen_shell = wl_registry_bind(registry, id, &zwp_fullscreen_shell_v1_interface, 1);
		zwp_fullscreen_shell_v1_add_listener(d->fullscreen_shell, &fullscreen_shell_listener, d);
	}
#endif
#if 0
	else if (strcmp(interface, "text_cursor_position") == 0)
	{
		d->text_cursor_position = wl_registry_bind(registry, id, &text_cursor_position_interface, 1);
	}
	else if (strcmp(interface, "workspace_manager") == 0)
	{
		//init_workspace_manager(d, id);
	}
	else if (strcmp(interface, "wl_subcompositor") == 0)
	{
		d->subcompositor = wl_registry_bind(registry, id, &wl_subcompositor_interface, 1);
#endif
}

static void registry_handle_global_remove(void* data, struct wl_registry* registry, uint32_t name)
{
	UwacDisplay* d = data;
	UwacGlobal* global = NULL;
	UwacGlobal* tmp = NULL;
	wl_list_for_each_safe(global, tmp, &d->globals, link)
	{
		if (global->name != name)
			continue;

#if 0

		if (strcmp(global->interface, "wl_output") == 0)
			display_destroy_output(d, name);

#endif

		if (strcmp(global->interface, "wl_seat") == 0)
		{
			UwacSeatRemovedEvent* ev = NULL;
			display_destroy_seat(d, name);
			ev = (UwacSeatRemovedEvent*)UwacDisplayNewEvent(d, UWAC_EVENT_REMOVED_SEAT);

			if (ev)
				ev->id = name;
		}

		wl_list_remove(&global->link);
		free(global->interface);
		free(global);
	}
}

static void UwacDestroyGlobal(UwacGlobal* global)
{
	free(global->interface);
	wl_list_remove(&global->link);
	free(global);
}

static void* display_bind(UwacDisplay* display, uint32_t name, const struct wl_interface* interface,
                          uint32_t version)
{
	return wl_registry_bind(display->registry, name, interface, version);
}

static const struct wl_registry_listener registry_listener = { registry_handle_global,
	                                                           registry_handle_global_remove };

int UwacDisplayWatchFd(UwacDisplay* display, int fd, uint32_t events, UwacTask* task)
{
	struct epoll_event ep;
	ep.events = events;
	ep.data.ptr = task;
	return epoll_ctl(display->epoll_fd, EPOLL_CTL_ADD, fd, &ep);
}

static void UwacDisplayUnwatchFd(UwacDisplay* display, int fd)
{
	epoll_ctl(display->epoll_fd, EPOLL_CTL_DEL, fd, NULL);
}

static void display_exit(UwacDisplay* display)
{
	display->running = false;
}

static void display_dispatch_events(UwacTask* task, uint32_t events)
{
	UwacDisplay* display = container_of(task, UwacDisplay, dispatch_fd_task);
	struct epoll_event ep;
	int ret = 0;
	display->display_fd_events = events;

	if ((events & EPOLLERR) || (events & EPOLLHUP))
	{
		display_exit(display);
		return;
	}

	if (events & EPOLLIN)
	{
		ret = wl_display_dispatch(display->display);

		if (ret == -1)
		{
			display_exit(display);
			return;
		}
	}

	if (events & EPOLLOUT)
	{
		ret = wl_display_flush(display->display);

		if (ret == 0)
		{
			ep.events = EPOLLIN | EPOLLERR | EPOLLHUP;
			ep.data.ptr = &display->dispatch_fd_task;
			epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD, display->display_fd, &ep);
		}
		else if (ret == -1 && errno != EAGAIN)
		{
			display_exit(display);
			return;
		}
	}
}

UwacDisplay* UwacOpenDisplay(const char* name, UwacReturnCode* err)
{
	UwacDisplay* ret = NULL;
	ret = (UwacDisplay*)xzalloc(sizeof(*ret));

	if (!ret)
	{
		*err = UWAC_ERROR_NOMEMORY;
		return NULL;
	}

	wl_list_init(&ret->globals);
	wl_list_init(&ret->seats);
	wl_list_init(&ret->outputs);
	wl_list_init(&ret->windows);
	ret->display = wl_display_connect(name);

	if (ret->display == NULL)
	{
		fprintf(stderr, "failed to connect to Wayland display %s: %m\n", name);
		*err = UWAC_ERROR_UNABLE_TO_CONNECT;
		goto out_free;
	}

	ret->epoll_fd = uwac_os_epoll_create_cloexec();

	if (ret->epoll_fd < 0)
	{
		*err = UWAC_NOT_ENOUGH_RESOURCES;
		goto out_disconnect;
	}

	ret->display_fd = wl_display_get_fd(ret->display);
	ret->registry = wl_display_get_registry(ret->display);

	if (!ret->registry)
	{
		*err = UWAC_ERROR_NOMEMORY;
		goto out_close_epoll;
	}

	wl_registry_add_listener(ret->registry, &registry_listener, ret);

	if ((wl_display_roundtrip(ret->display) < 0) || (wl_display_roundtrip(ret->display) < 0))
	{
		uwacErrorHandler(ret, UWAC_ERROR_UNABLE_TO_CONNECT,
		                 "Failed to process Wayland connection: %m\n");
		*err = UWAC_ERROR_UNABLE_TO_CONNECT;
		goto out_free_registry;
	}

	ret->dispatch_fd_task.run = display_dispatch_events;

	if (UwacDisplayWatchFd(ret, ret->display_fd, EPOLLIN | EPOLLERR | EPOLLHUP,
	                       &ret->dispatch_fd_task) < 0)
	{
		uwacErrorHandler(ret, UWAC_ERROR_INTERNAL, "unable to watch display fd: %m\n");
		*err = UWAC_ERROR_INTERNAL;
		goto out_free_registry;
	}

	ret->running = true;
	ret->last_error = *err = UWAC_SUCCESS;
	return ret;
out_free_registry:
	wl_registry_destroy(ret->registry);
out_close_epoll:
	close(ret->epoll_fd);
out_disconnect:
	wl_display_disconnect(ret->display);
out_free:
	free(ret);
	return NULL;
}

int UwacDisplayDispatch(UwacDisplay* display, int timeout)
{
	int ret = 0;
	int count = 0;
	UwacTask* task = NULL;
	struct epoll_event ep[16];
	wl_display_dispatch_pending(display->display);

	if (!display->running)
		return 0;

	ret = wl_display_flush(display->display);

	if (ret < 0 && errno == EAGAIN)
	{
		ep[0].events = (EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP);
		ep[0].data.ptr = &display->dispatch_fd_task;
		epoll_ctl(display->epoll_fd, EPOLL_CTL_MOD, display->display_fd, &ep[0]);
	}
	else if (ret < 0)
	{
		return -1;
	}

	count = epoll_wait(display->epoll_fd, ep, ARRAY_LENGTH(ep), timeout);

	for (int i = 0; i < count; i++)
	{
		task = ep[i].data.ptr;
		task->run(task, ep[i].events);
	}

	return 1;
}

UwacReturnCode UwacDisplayGetLastError(const UwacDisplay* display)
{
	return display->last_error;
}

UwacReturnCode UwacCloseDisplay(UwacDisplay** pdisplay)
{
	UwacDisplay* display = NULL;
	UwacSeat* seat = NULL;
	UwacSeat* tmpSeat = NULL;
	UwacWindow* window = NULL;
	UwacWindow* tmpWindow = NULL;
	UwacOutput* output = NULL;
	UwacOutput* tmpOutput = NULL;
	UwacGlobal* global = NULL;
	UwacGlobal* tmpGlobal = NULL;
	assert(pdisplay);
	display = *pdisplay;

	if (!display)
		return UWAC_ERROR_INVALID_DISPLAY;

	/* destroy windows */
	wl_list_for_each_safe(window, tmpWindow, &display->windows, link)
	{
		UwacDestroyWindow(&window);
	}
	/* destroy seats */
	wl_list_for_each_safe(seat, tmpSeat, &display->seats, link)
	{
		UwacSeatDestroy(seat);
	}
	/* destroy output */
	wl_list_for_each_safe(output, tmpOutput, &display->outputs, link)
	{
		UwacDestroyOutput(output);
	}
	/* destroy globals */
	wl_list_for_each_safe(global, tmpGlobal, &display->globals, link)
	{
		UwacDestroyGlobal(global);
	}

	if (display->compositor)
		wl_compositor_destroy(display->compositor);

	if (display->keyboard_inhibit_manager)
		zwp_keyboard_shortcuts_inhibit_manager_v1_destroy(display->keyboard_inhibit_manager);

	if (display->deco_manager)
		zxdg_decoration_manager_v1_destroy(display->deco_manager);

	if (display->kde_deco_manager)
		org_kde_kwin_server_decoration_manager_destroy(display->kde_deco_manager);

#ifdef BUILD_FULLSCREEN_SHELL

	if (display->fullscreen_shell)
		zwp_fullscreen_shell_v1_destroy(display->fullscreen_shell);

#endif
#ifdef BUILD_IVI

	if (display->ivi_application)
		ivi_application_destroy(display->ivi_application);

#endif

	if (display->xdg_toplevel)
		xdg_toplevel_destroy(display->xdg_toplevel);

	if (display->xdg_base)
		xdg_wm_base_destroy(display->xdg_base);

	if (display->shell)
		wl_shell_destroy(display->shell);

	if (display->shm)
		wl_shm_destroy(display->shm);

	if (display->viewporter)
		wp_viewporter_destroy(display->viewporter);

	if (display->subcompositor)
		wl_subcompositor_destroy(display->subcompositor);

	if (display->data_device_manager)
		wl_data_device_manager_destroy(display->data_device_manager);

	free(display->shm_formats);
	wl_registry_destroy(display->registry);
	close(display->epoll_fd);
	wl_display_disconnect(display->display);

	/* cleanup the event queue */
	while (display->push_queue)
	{
		UwacEventListItem* item = display->push_queue;
		display->push_queue = item->tail;
		free(item);
	}

	free(display);
	*pdisplay = NULL;
	return UWAC_SUCCESS;
}

int UwacDisplayGetFd(UwacDisplay* display)
{
	return display->epoll_fd;
}

static const char* errorStrings[] = {
	"success",
	"out of memory error",
	"unable to connect to wayland display",
	"invalid UWAC display",
	"not enough resources",
	"timed out",
	"not found",
	"closed connection",

	"internal error",
};

const char* UwacErrorString(UwacReturnCode error)
{
	if (error < UWAC_SUCCESS || error >= UWAC_ERROR_LAST)
		return "invalid error code";

	return errorStrings[error];
}

UwacReturnCode UwacDisplayQueryInterfaceVersion(const UwacDisplay* display, const char* name,
                                                uint32_t* version)
{
	const UwacGlobal* global = NULL;
	const UwacGlobal* tmp = NULL;

	if (!display)
		return UWAC_ERROR_INVALID_DISPLAY;

	wl_list_for_each_safe(global, tmp, &display->globals, link)
	{
		if (strcmp(global->interface, name) == 0)
		{
			if (version)
				*version = global->version;

			return UWAC_SUCCESS;
		}
	}
	return UWAC_NOT_FOUND;
}

uint32_t UwacDisplayQueryGetNbShmFormats(UwacDisplay* display)
{
	if (!display)
	{
		return 0;
	}

	if (!display->shm)
	{
		display->last_error = UWAC_NOT_FOUND;
		return 0;
	}

	display->last_error = UWAC_SUCCESS;
	return display->shm_formats_nb;
}

UwacReturnCode UwacDisplayQueryShmFormats(const UwacDisplay* display, enum wl_shm_format* formats,
                                          int formats_size, int* filled)
{
	if (!display)
		return UWAC_ERROR_INVALID_DISPLAY;

	*filled = min((int64_t)display->shm_formats_nb, formats_size);
	memcpy(formats, (const void*)display->shm_formats, *filled * sizeof(enum wl_shm_format));
	return UWAC_SUCCESS;
}

uint32_t UwacDisplayGetNbOutputs(const UwacDisplay* display)
{
	return wl_list_length(&display->outputs);
}

const UwacOutput* UwacDisplayGetOutput(UwacDisplay* display, int index)
{
	int i = 0;
	int display_count = 0;
	UwacOutput* ret = NULL;

	if (!display)
		return NULL;

	display_count = wl_list_length(&display->outputs);
	if (display_count <= index)
		return NULL;

	wl_list_for_each(ret, &display->outputs, link)
	{
		if (i == index)
			break;
		i++;
	}

	if (!ret)
	{
		display->last_error = UWAC_NOT_FOUND;
		return NULL;
	}

	display->last_error = UWAC_SUCCESS;
	return ret;
}

UwacReturnCode UwacOutputGetResolution(const UwacOutput* output, UwacSize* resolution)
{
	if ((output->resolution.height <= 0) || (output->resolution.width <= 0))
		return UWAC_ERROR_INTERNAL;

	*resolution = output->resolution;
	return UWAC_SUCCESS;
}

UwacReturnCode UwacOutputGetPosition(const UwacOutput* output, UwacPosition* pos)
{
	*pos = output->position;
	return UWAC_SUCCESS;
}

UwacEvent* UwacDisplayNewEvent(UwacDisplay* display, int type)
{
	UwacEventListItem* ret = NULL;

	if (!display)
	{
		return 0;
	}

	ret = xzalloc(sizeof(UwacEventListItem));

	if (!ret)
	{
		assert(uwacErrorHandler(display, UWAC_ERROR_NOMEMORY, "unable to allocate a '%s' event",
		                        event_names[type]));
		display->last_error = UWAC_ERROR_NOMEMORY;
		return 0;
	}

	ret->event.type = type;
	ret->tail = display->push_queue;

	if (ret->tail)
		ret->tail->head = ret;
	else
		display->pop_queue = ret;

	display->push_queue = ret;
	return &ret->event;
}

bool UwacHasEvent(UwacDisplay* display)
{
	return display->pop_queue != NULL;
}

UwacReturnCode UwacNextEvent(UwacDisplay* display, UwacEvent* event)
{
	UwacEventListItem* prevItem = NULL;
	int ret = 0;

	if (!display)
		return UWAC_ERROR_INVALID_DISPLAY;

	while (!display->pop_queue)
	{
		ret = UwacDisplayDispatch(display, 1 * 1000);

		if (ret < 0)
			return UWAC_ERROR_INTERNAL;
		else if (ret == 0)
			return UWAC_ERROR_CLOSED;
	}

	prevItem = display->pop_queue->head;
	*event = display->pop_queue->event;
	free(display->pop_queue);
	display->pop_queue = prevItem;

	if (prevItem)
		prevItem->tail = NULL;
	else
		display->push_queue = NULL;

	return UWAC_SUCCESS;
}