commit b24ebcb

Devine Lu Linvega  ·  2026-01-08 19:00:22 +0000 UTC
parent 620bc7b
Removed struct
1 files changed,  +52, -58
+52, -58
  1@@ -27,18 +27,17 @@ typedef unsigned short Uint16;
  2 typedef void (*deo_handler)(void);
  3 typedef Uint8 (*dei_handler)(void);
  4 
  5-struct emu {
  6-	Display *dpy;
  7-	Window win;
  8-	XImage *img;
  9-	int fd_x11;
 10-	int fd_timer;
 11-};
 12+static Display *emu_dpy;
 13+static Window emu_win;
 14+static XImage *emu_img;
 15+static int emu_x11;
 16+static int emu_timer;
 17 
 18 static deo_handler deo_handlers[256];
 19 static dei_handler dei_handlers[256];
 20-static struct emu emu = {0};
 21-static Uint8 *ram, dev[0x100], ptr[2], stk[2][0x100], emu_dei(const Uint8 port);
 22+static Uint8 *ram, dev[0x100], ptr[2], stk[2][0x100];
 23+
 24+static Uint8 emu_dei(const Uint8 port);
 25 static void emu_deo(const Uint8 port, const Uint8 value);
 26 
 27 /* clang-format off */
 28@@ -51,8 +50,6 @@ static void emu_deo(const Uint8 port, const Uint8 value);
 29 #define CLAMP(v, a, b) { if(v < a) v = a; else if(v >= b) v = b; }
 30 #define TWOS(v) (v & 0x8000 ? (int)v - 0x10000 : (int)v)
 31 #define PEEK2(d) (*(d) << 8 | (d)[1])
 32-#define NEXT if(--cycles) goto step; else return 0;
 33-
 34 
 35 #define REM *p -= 1 + _2;
 36 #define DEC s[--(*p)]
 37@@ -571,15 +568,12 @@ screen_update(void)
 38 static void
 39 screen_deo_pixel(void)
 40 {
 41-	int layer_mask, color, px;
 42 	const int ctrl = dev[0x2e];
 43-	if(ctrl & 0x40)
 44-		layer_mask = 0x3, color = (ctrl & 0x3) << 2;
 45-	else
 46-		layer_mask = 0xc, color = ctrl & 0x3;
 47+	const int layer_mask = (ctrl & 0x40) ? 0x3 : 0xc;
 48+	const int color = (ctrl & 0x40) ? ((ctrl & 0x3) << 2) : (ctrl & 0x3);
 49 	/* fill mode */
 50 	if(ctrl & 0x80) {
 51-		int x1, y1, x2, y2, ay, by;
 52+		int x1, y1, x2, y2, ay, by, px;
 53 		if(ctrl & 0x10)
 54 			x1 = 0, x2 = rX;
 55 		else
 56@@ -1136,12 +1130,12 @@ emu_resize(void)
 57 {
 58 	const int width = screen_width * screen_zoom;
 59 	const int height = screen_height * screen_zoom;
 60-	if(emu.img) {
 61-		emu.img->data = NULL;
 62-		XDestroyImage(emu.img);
 63+	if(emu_img) {
 64+		emu_img->data = NULL;
 65+		XDestroyImage(emu_img);
 66 	}
 67-	emu.img = XCreateImage(emu.dpy, DefaultVisual(emu.dpy, 0), DefaultDepth(emu.dpy, DefaultScreen(emu.dpy)), ZPixmap, 0, (char *)screen_pixels, width, height, 32, 0);
 68-	XResizeWindow(emu.dpy, emu.win, width, height);
 69+	emu_img = XCreateImage(emu_dpy, DefaultVisual(emu_dpy, 0), DefaultDepth(emu_dpy, DefaultScreen(emu_dpy)), ZPixmap, 0, (char *)screen_pixels, width, height, 32, 0);
 70+	XResizeWindow(emu_dpy, emu_win, width, height);
 71 }
 72 
 73 void
 74@@ -1151,7 +1145,7 @@ emu_redraw(void)
 75 	const int ay = screen_y1 * screen_zoom;
 76 	const int aw = screen_x2 * screen_zoom - ax;
 77 	const int ah = screen_y2 * screen_zoom - ay;
 78-	XPutImage(emu.dpy, emu.win, DefaultGC(emu.dpy, 0), emu.img, ax, ay, ax, ay, aw, ah);
 79+	XPutImage(emu_dpy, emu_win, DefaultGC(emu_dpy, 0), emu_img, ax, ay, ax, ay, aw, ah);
 80 }
 81 
 82 static void
 83@@ -1181,12 +1175,12 @@ get_button(KeySym sym)
 84 static void
 85 display_center(void)
 86 {
 87-	Screen *screen = ScreenOfDisplay(emu.dpy, 0);
 88+	Screen *screen = ScreenOfDisplay(emu_dpy, 0);
 89 	int w = screen_width * screen_zoom;
 90 	int h = screen_height * screen_zoom;
 91 	int x = screen->width / 2 - w / 2;
 92 	int y = screen->height / 2 - h / 2;
 93-	emu.win = XCreateSimpleWindow(emu.dpy, RootWindow(emu.dpy, 0), x, y, w, h, 1, 0, 0);
 94+	emu_win = XCreateSimpleWindow(emu_dpy, RootWindow(emu_dpy, 0), x, y, w, h, 1, 0, 0);
 95 }
 96 
 97 static void
 98@@ -1194,26 +1188,26 @@ display_hidecursor(void)
 99 {
100 	XColor black = {0};
101 	char empty[] = {0};
102-	Pixmap bitmap = XCreateBitmapFromData(emu.dpy, emu.win, empty, 1, 1);
103-	Cursor blank = XCreatePixmapCursor(emu.dpy, bitmap, bitmap, &black, &black, 0, 0);
104-	XDefineCursor(emu.dpy, emu.win, blank);
105-	XFreeCursor(emu.dpy, blank);
106-	XFreePixmap(emu.dpy, bitmap);
107+	Pixmap bitmap = XCreateBitmapFromData(emu_dpy, emu_win, empty, 1, 1);
108+	Cursor blank = XCreatePixmapCursor(emu_dpy, bitmap, bitmap, &black, &black, 0, 0);
109+	XDefineCursor(emu_dpy, emu_win, blank);
110+	XFreeCursor(emu_dpy, blank);
111+	XFreePixmap(emu_dpy, bitmap);
112 }
113 
114 static void
115 display_floating(void)
116 {
117-	Atom wmDelete = XInternAtom(emu.dpy, "WM_DELETE_WINDOW", True);
118-	Atom wmType = XInternAtom(emu.dpy, "_NET_WM_WINDOW_TYPE", False);
119-	Atom wmDialog = XInternAtom(emu.dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
120+	Atom wmDelete = XInternAtom(emu_dpy, "WM_DELETE_WINDOW", True);
121+	Atom wmType = XInternAtom(emu_dpy, "_NET_WM_WINDOW_TYPE", False);
122+	Atom wmDialog = XInternAtom(emu_dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
123 	XClassHint class = {"uxn11", "Uxn"};
124-	XSetWMProtocols(emu.dpy, emu.win, &wmDelete, 1);
125-	XChangeProperty(emu.dpy, emu.win, wmType, 4, 32, PropModeReplace, (Uint8 *)&wmDialog, 1);
126-	XSelectInput(emu.dpy, emu.win, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask | LeaveWindowMask);
127-	XStoreName(emu.dpy, emu.win, "uxn11");
128-	XSetClassHint(emu.dpy, emu.win, &class);
129-	XMapWindow(emu.dpy, emu.win);
130+	XSetWMProtocols(emu_dpy, emu_win, &wmDelete, 1);
131+	XChangeProperty(emu_dpy, emu_win, wmType, 4, 32, PropModeReplace, (Uint8 *)&wmDialog, 1);
132+	XSelectInput(emu_dpy, emu_win, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask | LeaveWindowMask);
133+	XStoreName(emu_dpy, emu_win, "uxn11");
134+	XSetClassHint(emu_dpy, emu_win, &class);
135+	XMapWindow(emu_dpy, emu_win);
136 }
137 
138 static void
139@@ -1221,7 +1215,7 @@ emu_event(void)
140 {
141 	char buf[7];
142 	XEvent ev;
143-	XNextEvent(emu.dpy, &ev);
144+	XNextEvent(emu_dpy, &ev);
145 	switch(ev.type) {
146 	case Expose:
147 		screen_reqdraw = 1;
148@@ -1276,19 +1270,19 @@ emu_init(void)
149 	static const struct itimerspec screen_tspec = {
150 		{0, 16666666},
151 		{0, 16666666}};
152-	emu.dpy = XOpenDisplay(NULL);
153-	if(!emu.dpy)
154+	emu_dpy = XOpenDisplay(NULL);
155+	if(!emu_dpy)
156 		return !fprintf(stderr, "Display: failed\n");
157-	emu.fd_x11 = ConnectionNumber(emu.dpy);
158+	emu_x11 = ConnectionNumber(emu_dpy);
159 	display_center();
160 	display_hidecursor();
161 	display_floating();
162-	emu.fd_timer = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
163-	if(emu.fd_timer < 0)
164+	emu_timer = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
165+	if(emu_timer < 0)
166 		return !fprintf(stderr, "timerfd_create: failed\n");
167-	timerfd_settime(emu.fd_timer, 0, &screen_tspec, NULL);
168-	XMapWindow(emu.dpy, emu.win);
169-	XFlush(emu.dpy);
170+	timerfd_settime(emu_timer, 0, &screen_tspec, NULL);
171+	XMapWindow(emu_dpy, emu_win);
172+	XFlush(emu_dpy);
173 	return 1;
174 }
175 
176@@ -1298,18 +1292,18 @@ emu_run(void)
177 	int has_input, has_eof, has_sent = 1;
178 	char expirations[8], coninp[CONINBUFSIZE];
179 	struct pollfd fds[3];
180-	fds[0].fd = emu.fd_x11, fds[0].events = POLLIN;
181-	fds[1].fd = emu.fd_timer, fds[1].events = POLLIN;
182+	fds[0].fd = emu_x11, fds[0].events = POLLIN;
183+	fds[1].fd = emu_timer, fds[1].events = POLLIN;
184 	fds[2].fd = STDIN_FILENO, fds[2].events = POLLIN | POLLHUP;
185 	while(!dev[0x0f]) {
186 		if(poll(fds, 3, -1) <= 0)
187 			continue;
188 		if(fds[0].revents & POLLIN) {
189-			while(XPending(emu.dpy))
190+			while(XPending(emu_dpy))
191 				emu_event();
192 		}
193 		if(fds[1].revents & POLLIN) {
194-			read(emu.fd_timer, expirations, sizeof expirations);
195+			read(emu_timer, expirations, sizeof expirations);
196 			screen_update();
197 		}
198 		has_input = fds[2].revents & POLLIN;
199@@ -1326,12 +1320,12 @@ emu_run(void)
200 			}
201 		}
202 	}
203-	if(emu.fd_timer >= 0)
204-		close(emu.fd_timer);
205-	if(emu.win)
206-		XDestroyWindow(emu.dpy, emu.win);
207-	if(emu.dpy)
208-		XCloseDisplay(emu.dpy);
209+	if(emu_timer >= 0)
210+		close(emu_timer);
211+	if(emu_win)
212+		XDestroyWindow(emu_dpy, emu_win);
213+	if(emu_dpy)
214+		XCloseDisplay(emu_dpy);
215 	console_close();
216 }
217