summaryrefslogtreecommitdiff
path: root/vidtime.c
blob: 41a9f2584dbf47858aa6bcf028716a5a380c2982 (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
/*
 * Copyright © 2018 Keith Packard <keithp@keithp.com>
 *
 * This program 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 Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 */

#include <X11/Xlib.h>
#include <X11/extensions/Xpresent.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <time.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>

struct monitor {
	int	fd;
};

#define BILLION 1000000000ULL
#define MILLION 1000000ULL

static uint64_t vid_clock(void)
{
	struct timespec	ts;
	clock_gettime(CLOCK_MONOTONIC, &ts);
	return ((uint64_t) ts.tv_sec * BILLION + (uint64_t) ts.tv_nsec);
}

static void flush(int fd)
{
	char	buf[1024];
	int	n;
	int	avail;
	for (;;) {
		n = ioctl(fd, FIONREAD, &avail);
		if (n < 0 || avail <= 0)
			break;
		read(fd, buf, avail);
	}
}

static uint64_t		vid_time[2];
static int		vid_value;

static pthread_mutex_t	time_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t	time_cond = PTHREAD_COND_INITIALIZER;


static void *monitor_device(void *arg)
{
	struct monitor	*m = arg;
	int	fd = m->fd;
	char	buf[256];
	int	n;
	struct termios	termios;
	uint64_t	t;
	int	v;

	tcgetattr(fd, &termios);
	cfmakeraw(&termios);
	cfsetospeed(&termios, B9600);
	cfsetispeed(&termios, B9600);
	tcsetattr(fd, TCSAFLUSH, &termios);
	flush(fd);
	write(fd, "E 0\nV 0\n", 8);
	sleep(1);
	flush(fd);
	write(fd, "V 1\n", 4);
	for (;;) {
		n = read(fd, buf, sizeof(buf));
		if (n < 0) {
			perror("read");
			return NULL;
		}
		t = vid_clock();
		switch(buf[0]) {
		case '1':
		case '0':
			v = buf[0] - '0';
			pthread_mutex_lock(&time_mutex);
			vid_value = v;
			vid_time[v] = t;
			pthread_mutex_unlock(&time_mutex);
			pthread_cond_signal(&time_cond);
			break;
		}
	}
}

#define W	512
#define H	512
#define DEV	"/dev/ttyACM0"

int main(int argc, char **argv)
{
	Display	*dpy;
	int	scr;
	Window	root;
	Window	win;
	GC	gc;
	Pixmap	pix;
	int	v = 0;
	struct timespec	ts = { .tv_sec = 0, .tv_nsec = 100 * MILLION };
	pthread_t	monitor_thread;
	struct monitor	monitor;
	uint64_t	put, show, got;
	uint32_t	serial = 0;
	XEvent		ev;
	int		ge_event_base, ge_error_base;
	int		present_opcode, present_event_base, present_error_base;
	bool		got_event, exposed;
	XSetWindowAttributes	swa = {
		.event_mask = ExposureMask
	};

	monitor.fd = open(DEV, O_RDWR);
	if (monitor.fd < 0) {
		perror(DEV);
		exit(1);
	}
	pthread_create(&monitor_thread, NULL, monitor_device, &monitor);

	dpy = XOpenDisplay(NULL);
	scr = DefaultScreen(dpy);
	root = RootWindow(dpy, scr);
	win = XCreateWindow(dpy, root,
			    0, 0, W, H, 0, DefaultDepth(dpy, scr),
			    InputOutput, CopyFromParent,
			    CWEventMask, &swa);
	pix = XCreatePixmap(dpy, win, W, H, DefaultDepth(dpy, scr));
	gc = XCreateGC(dpy, win, 0, NULL);
	XMapWindow(dpy, win);
	XPresentQueryExtension(dpy, &present_opcode, &present_event_base, &present_error_base);
	XPresentSelectInput(dpy, win, PresentCompleteNotifyMask);
	for (;;) {
		XSetForeground(dpy, gc, v ? WhitePixel(dpy, scr) : BlackPixel(dpy, scr));
		XFillRectangle(dpy, pix, gc, 0, 0, W, H);
		XPresentPixmap(dpy, win, pix, ++serial,
			       None, None, 0, 0, None, None, None,
			       PresentOptionNone,
			       0,
			       0, 0, NULL, 0);
		put = vid_clock();
		XFlush(dpy);
		got_event = false;
		exposed = false;
		while (!got_event) {
			XGenericEventCookie *cookie = (void *) &ev;
			XNextEvent(dpy, &ev);
			if (XGetEventData(dpy, cookie)) {
				if (cookie->extension == present_opcode &&
				    cookie->evtype == PresentCompleteNotify) {
					XPresentCompleteNotifyEvent	*ce = cookie->data;
					if (ce->serial_number == serial) {
						show = ce->ust * 1000;
						got_event = true;
					}
				}
				XFreeEventData(dpy, cookie);
			} else {
				switch (ev.type) {
				case Expose:
					if (ev.xexpose.count == 0)
						exposed = true;
					break;
				}
			}
		}
		if (!exposed) {
			pthread_mutex_lock(&time_mutex);
			for (;;) {
				if (vid_value == v) {
					got = vid_time[v];
					break;
				}
				pthread_cond_wait (&time_cond, &time_mutex);
			}
			pthread_mutex_unlock(&time_mutex);
			if (show >= put && got >= show) {
				printf("%d delay put->show %f show->got %f\n", v,
				       (show - put) / 1e9,
				       (got - show) / 1e9);
			} else {
				printf("%d invalid\n", v);
			}
		}
		v = 1-v;
		nanosleep(&ts, NULL);
	}
}