commit bbb187a

Devine Lu Linvega  ·  2026-01-08 17:43:04 +0000 UTC
parent 8cfc76a
Store resources in struct
1 files changed,  +17, -8
+17, -8
 1@@ -1273,11 +1273,20 @@ emu_event(void)
 2 static int
 3 emu_init(void)
 4 {
 5+	static const struct itimerspec screen_tspec = {
 6+		{0, 16666666},
 7+		{0, 16666666}};
 8 	emu.dpy = XOpenDisplay(NULL);
 9-	if(!emu.dpy) return !fprintf(stderr, "Display: failed\n");
10+	if(!emu.dpy)
11+		return !fprintf(stderr, "Display: failed\n");
12 	display_center();
13 	display_hidecursor();
14 	display_floating();
15+	emu.fd_x11 = XConnectionNumber(emu.dpy);
16+	emu.fd_timer = timerfd_create(CLOCK_MONOTONIC, 0);
17+	if(emu.fd_timer < 0)
18+		return !fprintf(stderr, "timerfd_create: failed\n");
19+	timerfd_settime(emu.fd_timer, 0, &screen_tspec, NULL);
20 	return 1;
21 }
22 
23@@ -1286,12 +1295,11 @@ emu_run(void)
24 {
25 	int has_input, has_eof, has_sent = 1;
26 	char expirations[8], coninp[CONINBUFSIZE];
27-	static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}};
28 	struct pollfd fds[3];
29 	/* timer */
30-	fds[0].fd = XConnectionNumber(emu.dpy);
31-	fds[1].fd = timerfd_create(CLOCK_MONOTONIC, 0);
32-	timerfd_settime(fds[1].fd, 0, &screen_tspec, NULL);
33+	fds[0].fd = emu.fd_x11;
34+	fds[1].fd = emu.fd_timer;
35+	fds[2].fd = STDIN_FILENO;
36 	fds[2].fd = STDIN_FILENO;
37 	fds[0].events = fds[1].events = fds[2].events = POLLIN;
38 	/* main loop */
39@@ -1314,11 +1322,12 @@ emu_run(void)
40 				console_input(0, CONSOLE_END), has_sent = 0;
41 		}
42 	}
43-	if(emu.img)
44-		XDestroyImage(emu.img);
45+	if(emu.fd_timer > 0)
46+		close(emu.fd_timer);
47 	if(emu.win)
48 		XDestroyWindow(emu.dpy, emu.win);
49-	XCloseDisplay(emu.dpy);
50+	if(emu.dpy)
51+		XCloseDisplay(emu.dpy);
52 	console_close();
53 }
54