summaryrefslogtreecommitdiff
path: root/src/wfdevent.c
blob: 48686849276e91a1963534d3e918b6217d5e4a79 (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
/*
 * Copyright (c) 2011 Benjamin Franzke
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial eventions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#define _GNU_SOURCE

#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

#include <WF/wfd.h>
#include <WF/wfdext.h>

#include "wflist.h"
#include "wfhandle.h"
#include "wfdregistry.h"
#include "wfdevent.h"
#include "wfdport.h"
#include "wfdpipeline.h"

#include <xf86drm.h>
#include <libudev.h>

#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>

struct bind_event {
	WFDint pipeline_id;
	WFDSource source;
	WFDint time;

	struct wf_list link;
};

#define EGL_EGLEXT_PROTOTYPES
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <pthread.h>

struct wfd_event {
	uint32_t start_time_msec;
	int epoll_fd;
	uint32_t filter;

	struct wfd_device *device;

	/* source bind complete */
	int drm_fd;
	struct bind_event bind;

	/* port attach */
	struct udev_monitor *udev_monitor;
	int udev_monitor_fd;
	WFDint attach_port_id;
	WFDboolean attach_state;

	int source_bind_event_queue_fd;
	struct wf_list bind_event_queue;

	WFDEventType current_event_type;

	EGLDisplay egl_display;
	EGLSyncKHR egl_sync;
	pthread_t thread;
	int thread_destroy_fd;
};

static int
create_port_attach_event_fd(struct wfd_event *event)
{
	struct wfd_registry *registry = wfd_get_registry();

	event->udev_monitor =
		udev_monitor_new_from_netlink(registry->udev, "udev");
	if (event->udev_monitor == NULL) {
		fprintf(stderr, "failed to create udev monitor\n");
		return -1;
	}

	udev_monitor_filter_add_match_subsystem_devtype(event->udev_monitor,
							"drm", NULL);

	if (udev_monitor_enable_receiving(event->udev_monitor) < 0) {
		fprintf(stderr, "failed to enable udev-monitor receiving\n");
		udev_monitor_unref(event->udev_monitor);
		return -1;
	}

	return udev_monitor_get_fd(event->udev_monitor);
}

enum wfd_event_type_mask {
	WFD_EVENT_NONE_MASK				= (1 << 0),
	WFD_EVENT_DESTROYED_MASK			= (1 << 1),
	WFD_EVENT_PORT_ATTACH_DETACH_MASK		= (1 << 2),
	WFD_EVENT_PORT_PROTECTION_FAILURE_MASK		= (1 << 3),
	WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE_MASK	= (1 << 4),
	WFD_EVENT_PIPELINE_BIND_MASK_COMPLETE_MASK	= (1 << 5),

	WFD_EVENT_ALL_MASK 				= 0x3f,
};

static int
wfd_event_update(struct wfd_device *device,
		 struct wfd_event *event,
		 uint32_t filter)
{
	struct epoll_event ep;
	uint32_t additions, removals;
	int ret;

	additions =  filter & ~event->filter;
	removals  = ~filter &  event->filter;

	if (additions & WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE_MASK) {
		memset(&ep, 0, sizeof ep);
		ep.events = EPOLLIN;
		event->drm_fd = fcntl(wfd_device_get_fd(device),
				      F_DUPFD_CLOEXEC, 0);
		if (event->drm_fd < 0) {
			fprintf(stderr, "failed to dup drm fd: %m\n");
			return -1;
		}
		ep.data.fd = event->drm_fd;
		ret = epoll_ctl(event->epoll_fd, EPOLL_CTL_ADD,
				ep.data.fd, &ep);
		if (ret == -1) {
			fprintf(stderr, "failed to add fd to epoll: %m\n");
			close(event->drm_fd);
			return -1;
		}

		event->filter |= WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE_MASK;
	} else if (removals & WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE_MASK) {
		ret = epoll_ctl(event->epoll_fd, EPOLL_CTL_DEL,
				event->drm_fd, NULL);
		if (ret) {
			fprintf(stderr, "failed to del fd form epoll: %m\n");
			return -1;
		}

		close (event->drm_fd);
		event->filter &= ~WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE_MASK;
	}

	if (additions & WFD_EVENT_PORT_ATTACH_DETACH) {
		memset(&ep, 0, sizeof ep);
		ep.events = EPOLLIN;
		event->udev_monitor_fd = create_port_attach_event_fd(event);
		if (event->udev_monitor_fd < 0) {
			fprintf(stderr, "failed to create udev monitor fd\n");
			return -1;
		}
		ep.data.fd = event->udev_monitor_fd;
		if (ep.data.fd >= 0) {
			ret = epoll_ctl(event->epoll_fd, EPOLL_CTL_ADD,
					ep.data.fd, &ep);
			if (ret == -1) {
				fprintf(stderr, "failed to add fd to epoll: %m\n");
				return -1;
			}
		}

		memset(&ep, 0, sizeof ep);
		event->source_bind_event_queue_fd =
			eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE);
		ep.events = EPOLLIN;
		ep.data.fd = event->source_bind_event_queue_fd;
		ret = epoll_ctl(event->epoll_fd, EPOLL_CTL_ADD,
				ep.data.fd, &ep);
		if (ret == -1) {
			fprintf(stderr, "failed to add fd to epoll: %m\n");
			return -1;
		}

		wf_list_init(&event->bind_event_queue);
		event->filter |= WFD_EVENT_PORT_ATTACH_DETACH;
	} else if (removals & WFD_EVENT_PORT_ATTACH_DETACH) {
		ret = epoll_ctl(event->epoll_fd, EPOLL_CTL_DEL,
				event->udev_monitor_fd, NULL);
		if (ret) {
			fprintf(stderr, "failed to del fd form epoll: %m\n");
			return -1;
		}

		udev_monitor_unref(event->udev_monitor);
		event->udev_monitor_fd = -1;

		ret = epoll_ctl(event->epoll_fd, EPOLL_CTL_DEL,
				event->source_bind_event_queue_fd, NULL);
		if (ret) {
			fprintf(stderr, "failed to del fd form epoll: %m\n");
			return -1;
		}

		close(event->source_bind_event_queue_fd);
		
		struct bind_event *entry, *next;
		wf_list_for_each_safe(entry, next,
				      &event->bind_event_queue, link) {
			wf_list_remove(&entry->link);
			free(entry);

		}

		event->filter &= ~WFD_EVENT_PORT_ATTACH_DETACH;
	}

	return 0;
}

void
wfd_event_set_filter(struct wfd_device *device,
		     struct wfd_event *event,
		     const WFDEventType *filter_list)
{
	uint32_t filter;
	int i;

	/* These events cant be disabled */
	filter = WFD_EVENT_NONE_MASK | WFD_EVENT_DESTROYED_MASK;

	if (filter_list == NULL)
		filter |= WFD_EVENT_ALL_MASK;

	for (i = 0; filter_list != NULL && filter_list[i] != WFD_NONE; ++i) {
		switch (filter_list[i]) {
		case WFD_EVENT_NONE:
		case WFD_EVENT_DESTROYED:
		case WFD_EVENT_INVALID:
			break;
		case WFD_EVENT_PORT_ATTACH_DETACH:
			filter |= WFD_EVENT_PORT_ATTACH_DETACH_MASK;
			break;
		case WFD_EVENT_PORT_PROTECTION_FAILURE:
			filter |= WFD_EVENT_PORT_PROTECTION_FAILURE_MASK;
			break;
		case WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE:
			filter |= WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE_MASK;
			break;
		case WFD_EVENT_PIPELINE_BIND_MASK_COMPLETE:
			filter |= WFD_EVENT_PIPELINE_BIND_MASK_COMPLETE_MASK;
			break;
		default:
			wfd_device_set_error(device,
					     WFD_ERROR_ILLEGAL_ARGUMENT);
			return;
		}
	}

	wfd_event_update(device, event, filter);
}

void
wfd_event_destroy(struct wfd_device *device,
		  struct wfd_event *event)
{
	if (event->thread_destroy_fd >= 0) {
		int64_t value = 1;
		write(event->thread_destroy_fd, &value, sizeof value);
		pthread_join(event->thread, NULL);
		close(event->thread_destroy_fd);
		event->thread_destroy_fd = -1;
	}

	wfd_event_update(device, event, 0);

	close(event->epoll_fd);
	free(event);
}

struct wfd_event *
wfd_create_event(struct wfd_device *device,
		 const WFDint *attrib_list)
{
	struct wfd_event *event;
	struct timeval tv;

	event = calloc(1, sizeof *event);
	if (event == NULL)
		return NULL;

	gettimeofday(&tv, NULL);
	event->start_time_msec = tv.tv_sec * 1e3 + tv.tv_usec / 1e3;

	event->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
	if (event->epoll_fd == -1) {
		fprintf(stderr, "failed to crate epoll instance: %m\n");
		free(event);
		return NULL;
	}

	event->device = device;
	event->current_event_type = WFD_EVENT_NONE;
	event->filter = 0;

	event->thread_destroy_fd = -1;

	if (wfd_event_update(device, event, WFD_EVENT_ALL_MASK) < 0) {
		wfd_event_destroy(device, event);
		return NULL;
	}

	return event;
}

WFDint
wfd_event_get_attribi(struct wfd_device *device,
		      struct wfd_event *event,
		      WFDEventAttrib attribute)
{
	switch (attribute) {
	case WFD_EVENT_TYPE:
		return event->current_event_type;
	case WFD_EVENT_PIPELINE_BIND_QUEUE_SIZE:
	case WFD_EVENT_PORT_ATTACH_PORT_ID:
	case WFD_EVENT_PORT_ATTACH_STATE:
	case WFD_EVENT_PORT_PROTECTION_PORT_ID:
	case WFD_EVENT_PIPELINE_BIND_PIPELINE_ID:
	case WFD_EVENT_PIPELINE_BIND_SOURCE:
	case WFD_EVENT_PIPELINE_BIND_MASK:
	case WFD_EVENT_PIPELINE_BIND_QUEUE_OVERFLOW:
	case WFD_EVENT_PIPELINE_BIND_TIME_EXT:
		break;
	default:
		wfd_device_set_error(device, WFD_ERROR_BAD_ATTRIBUTE);
		return 0;
	}

	switch (event->current_event_type) {
	case WFD_EVENT_PORT_ATTACH_DETACH:
		switch (attribute) {
		case WFD_EVENT_PORT_ATTACH_PORT_ID:
			return event->attach_port_id;
		case WFD_EVENT_PORT_ATTACH_STATE:
			return event->attach_state;
		default:
			wfd_device_set_error(device,
					     WFD_ERROR_ILLEGAL_ARGUMENT);
			return 0;
		}

	case WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE:
		switch (attribute) {
		case WFD_EVENT_PIPELINE_BIND_PIPELINE_ID:
			return event->bind.pipeline_id;
		case WFD_EVENT_PIPELINE_BIND_TIME_EXT:
			return event->bind.time;
		case WFD_EVENT_PIPELINE_BIND_SOURCE:
			return event->bind.source;
		default:
			wfd_device_set_error(device,
					     WFD_ERROR_ILLEGAL_ARGUMENT);
			return 0;
		}
	default:
		wfd_device_set_error(device, WFD_ERROR_ILLEGAL_ARGUMENT);
		return 0;
	}
	
	return 0;
}


static struct bind_event global_bind_event;
struct wfd_event *global_event;

static void
page_flip_handler(int fd, unsigned int frame,
		  unsigned int sec, unsigned int usec, void *data)
{
	struct wfd_pipeline *pipeline = data;
	struct bind_event bind;

	wf_list_init(&bind.link);
	bind.source = wf_handle_get(wfd_pipeline_get_previous_source(pipeline),
				    SOURCE_HANDLE);
	bind.pipeline_id = wfd_pipeline_get_id(pipeline);
	bind.time = (sec * 1000 + usec / 1000) -
		global_event->start_time_msec;

	wfd_pipeline_destroy_pending_fb(pipeline);

	if (global_bind_event.pipeline_id == WFD_INVALID_PIPELINE_ID) {
		global_bind_event = bind;
	} else {
		/* queue up */
		int64_t add = 1;
		struct bind_event *queue_entry;

		queue_entry = malloc(sizeof *queue_entry);
		if (queue_entry) {
			*queue_entry = bind;

			wf_list_push_tail(&global_event->bind_event_queue,
					  queue_entry, link);

			if (write(global_event->source_bind_event_queue_fd,
				  &add, sizeof add) < 0) {
				printf("overflow\n");
			}
		}
	}
}

static int
udev_event_is_hotplug_for_device(struct udev_device *u_event,
				 struct wfd_device *device)
{
	struct udev_list_entry *list, *hotplug_entry;
	
	list = udev_device_get_properties_list_entry(u_event);

	hotplug_entry = udev_list_entry_get_by_name(list, "HOTPLUG");
	if (hotplug_entry == NULL)
		return 0;

	if (strcmp(udev_list_entry_get_value(hotplug_entry), "1") != 0)
		return 0;

	if (udev_device_get_devnum(u_event) != 
	    udev_device_get_devnum(wfd_device_get_udev_device(device)))
		return 0;

	return 1;
}

WFDEventType
wfd_event_wait(struct wfd_device *device,
	       struct wfd_event *event,
	       WFDtime time)
{
	struct epoll_event ep;
	WFDEventType event_type = WFD_EVENT_NONE;
	int num;
	int timeout;

	timeout = time / 1e6;

	num = epoll_wait(event->epoll_fd, &ep, 1, timeout);
	if (num == -1) {
		fprintf(stderr, "epoll_wait failed: %m\n");
		return WFD_EVENT_NONE;
	} else if (num == 0) {
		return WFD_EVENT_NONE;
	}

	global_event = event;

	if (ep.data.fd == event->source_bind_event_queue_fd) {
		int64_t id;
		struct bind_event *queue_entry;

		if (read(event->source_bind_event_queue_fd, &id,
			 sizeof id) < 0) {
			fprintf(stderr, "error reading from event queue\n");
			return WFD_EVENT_NONE;
		}

		wf_list_pop_head(&event->bind_event_queue,
				 queue_entry, link);

		event->bind = *queue_entry;
		free(queue_entry);
		event_type = WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE;
	} else if (ep.data.fd == event->drm_fd) {
		drmEventContext drm_evctx;

		memset(&drm_evctx, 0, sizeof drm_evctx);
		drm_evctx.version = DRM_EVENT_CONTEXT_VERSION;
		drm_evctx.page_flip_handler = page_flip_handler;

		global_bind_event.pipeline_id = WFD_INVALID_PIPELINE_ID;
		drmHandleEvent(event->drm_fd, &drm_evctx);
		if (global_bind_event.pipeline_id != WFD_INVALID_PIPELINE_ID) {
			event->bind = global_bind_event;
			event_type = WFD_EVENT_PIPELINE_BIND_SOURCE_COMPLETE;
		}
	} else if (ep.data.fd == event->udev_monitor_fd) {
		struct udev_device *u_event;

		u_event = udev_monitor_receive_device(event->udev_monitor);

		if (udev_event_is_hotplug_for_device(u_event, device)) {
			event_type = wfd_device_find_attach_detach(device,
				&event->attach_port_id, &event->attach_state);
		}

		udev_device_unref(u_event);
	}

	event->current_event_type = event_type;

	return event_type;
}

int
wfd_event_get_fd(struct wfd_device *device,
		 struct wfd_event *event)
{
	return event->epoll_fd;
}

static void *
signalling_thread(void *_event)
{
	struct wfd_event *event = _event;
	struct epoll_event ep[16];
	int i, num;

	while (1) {
		num = epoll_wait(event->epoll_fd, ep, 16, -1);
		if (num < 0)
			continue;
		for (i = 0; i < num; ++i) {
			if (ep[i].data.fd == event->thread_destroy_fd)
				return NULL;
			else
				eglSignalSyncKHR(event->egl_display,
						 event->egl_sync,
						 EGL_SIGNALED_KHR);
		}
	}

	return NULL;
}

int
wfd_event_async(struct wfd_device *device,
		struct wfd_event *event,
		WFDEGLDisplay *egl_display,
		WFDEGLSync *egl_sync)
{
	struct epoll_event ep;

	if (event->egl_sync != NULL)
		return -1;

	event->egl_display = (EGLDisplay) egl_display;
	event->egl_sync = (EGLSyncKHR) egl_sync;

	event->thread_destroy_fd =
		eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE);

	memset(&ep, 0, sizeof ep);
	ep.events = EPOLLIN;
	ep.data.fd = event->thread_destroy_fd;
	epoll_ctl(event->epoll_fd, EPOLL_CTL_ADD, ep.data.fd, &ep);

	pthread_create(&event->thread, NULL, signalling_thread, event);

	return 0;
}