commit 8cfc76a
Devine Lu Linvega
·
2026-01-08 17:39:15 +0000 UTC
parent 9fa81fc
Storing resources together
1 files changed,
+50,
-41
+50,
-41
1@@ -24,15 +24,22 @@ cc -DNDEBUG -O2 -g0 -s src/uxn11.c -lX11 -lutil -o bin/uxn11
2
3 typedef unsigned char Uint8;
4 typedef unsigned short Uint16;
5-
6 typedef void (*deo_handler)(void);
7-static deo_handler deo_handlers[256];
8 typedef Uint8 (*dei_handler)(void);
9-static dei_handler dei_handlers[256];
10
11-static XImage *ximage;
12-static Display *display;
13-static Window window;
14+struct emu {
15+ Display *dpy;
16+ Window win;
17+ XImage *img;
18+ int fd_x11;
19+ int fd_timer;
20+};
21+
22+static deo_handler deo_handlers[256];
23+static dei_handler dei_handlers[256];
24+static struct emu emu = {0};
25+static Uint8 *ram, dev[0x100], ptr[2], stk[2][0x100], emu_dei(const Uint8 port);
26+static void emu_deo(const Uint8 port, const Uint8 value);
27
28 /* clang-format off */
29
30@@ -46,8 +53,6 @@ static Window window;
31 #define PEEK2(d) (*(d) << 8 | (d)[1])
32 #define NEXT if(--cycles) goto step; else return 0;
33
34-Uint8 *ram, dev[0x100], ptr[2], stk[2][0x100], emu_dei(const Uint8 port);
35-void emu_deo(const Uint8 port, const Uint8 value);
36
37 #define REM *p -= 1 + _2;
38 #define DEC s[--(*p)]
39@@ -1131,10 +1136,12 @@ emu_resize(void)
40 {
41 const int width = screen_width * screen_zoom;
42 const int height = screen_height * screen_zoom;
43- if(ximage)
44- free(ximage);
45- ximage = XCreateImage(display, DefaultVisual(display, 0), DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)screen_pixels, width, height, 32, 0);
46- XResizeWindow(display, window, width, height);
47+ if(emu.img) {
48+ emu.img->data = NULL;
49+ XDestroyImage(emu.img);
50+ }
51+ emu.img = XCreateImage(emu.dpy, DefaultVisual(emu.dpy, 0), DefaultDepth(emu.dpy, DefaultScreen(emu.dpy)), ZPixmap, 0, (char *)screen_pixels, width, height, 32, 0);
52+ XResizeWindow(emu.dpy, emu.win, width, height);
53 }
54
55 void
56@@ -1144,7 +1151,7 @@ emu_redraw(void)
57 const int ay = screen_y1 * screen_zoom;
58 const int aw = screen_x2 * screen_zoom - ax;
59 const int ah = screen_y2 * screen_zoom - ay;
60- XPutImage(display, window, DefaultGC(display, 0), ximage, ax, ay, ax, ay, aw, ah);
61+ XPutImage(emu.dpy, emu.win, DefaultGC(emu.dpy, 0), emu.img, ax, ay, ax, ay, aw, ah);
62 }
63
64 static void
65@@ -1174,10 +1181,12 @@ get_button(KeySym sym)
66 static void
67 display_center(void)
68 {
69- Screen *screen = ScreenOfDisplay(display, 0);
70- int desktop_width = screen->width, desktop_height = screen->height;
71- int x = desktop_width / 2 - screen_width / 2, y = desktop_height / 2 - screen_height / 2;
72- window = XCreateSimpleWindow(display, RootWindow(display, 0), x, y, screen_width, screen_height, 1, 0, 0);
73+ Screen *screen = ScreenOfDisplay(emu.dpy, 0);
74+ int w = screen_width * screen_zoom;
75+ int h = screen_height * screen_zoom;
76+ int x = screen->width / 2 - w / 2;
77+ int y = screen->height / 2 - h / 2;
78+ emu.win = XCreateSimpleWindow(emu.dpy, RootWindow(emu.dpy, 0), x, y, w, h, 1, 0, 0);
79 }
80
81 static void
82@@ -1185,26 +1194,26 @@ display_hidecursor(void)
83 {
84 XColor black = {0};
85 char empty[] = {0};
86- Pixmap bitmap = XCreateBitmapFromData(display, window, empty, 1, 1);
87- Cursor blank = XCreatePixmapCursor(display, bitmap, bitmap, &black, &black, 0, 0);
88- XDefineCursor(display, window, blank);
89- XFreeCursor(display, blank);
90- XFreePixmap(display, bitmap);
91+ Pixmap bitmap = XCreateBitmapFromData(emu.dpy, emu.win, empty, 1, 1);
92+ Cursor blank = XCreatePixmapCursor(emu.dpy, bitmap, bitmap, &black, &black, 0, 0);
93+ XDefineCursor(emu.dpy, emu.win, blank);
94+ XFreeCursor(emu.dpy, blank);
95+ XFreePixmap(emu.dpy, bitmap);
96 }
97
98 static void
99 display_floating(void)
100 {
101- Atom wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True);
102- Atom wmType = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
103- Atom wmDialog = XInternAtom(display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
104+ Atom wmDelete = XInternAtom(emu.dpy, "WM_DELETE_WINDOW", True);
105+ Atom wmType = XInternAtom(emu.dpy, "_NET_WM_WINDOW_TYPE", False);
106+ Atom wmDialog = XInternAtom(emu.dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
107 XClassHint class = {"uxn11", "Uxn"};
108- XSetWMProtocols(display, window, &wmDelete, 1);
109- XChangeProperty(display, window, wmType, 4, 32, PropModeReplace, (Uint8 *)&wmDialog, 1);
110- XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask | LeaveWindowMask);
111- XStoreName(display, window, "uxn11");
112- XSetClassHint(display, window, &class);
113- XMapWindow(display, window);
114+ XSetWMProtocols(emu.dpy, emu.win, &wmDelete, 1);
115+ XChangeProperty(emu.dpy, emu.win, wmType, 4, 32, PropModeReplace, (Uint8 *)&wmDialog, 1);
116+ XSelectInput(emu.dpy, emu.win, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask | LeaveWindowMask);
117+ XStoreName(emu.dpy, emu.win, "uxn11");
118+ XSetClassHint(emu.dpy, emu.win, &class);
119+ XMapWindow(emu.dpy, emu.win);
120 }
121
122 static void
123@@ -1212,7 +1221,7 @@ emu_event(void)
124 {
125 char buf[7];
126 XEvent ev;
127- XNextEvent(display, &ev);
128+ XNextEvent(emu.dpy, &ev);
129 switch(ev.type) {
130 case Expose:
131 screen_reqdraw = 1;
132@@ -1264,8 +1273,8 @@ emu_event(void)
133 static int
134 emu_init(void)
135 {
136- display = XOpenDisplay(NULL);
137- if(!display) return !fprintf(stderr, "Display: failed\n");
138+ emu.dpy = XOpenDisplay(NULL);
139+ if(!emu.dpy) return !fprintf(stderr, "Display: failed\n");
140 display_center();
141 display_hidecursor();
142 display_floating();
143@@ -1280,7 +1289,7 @@ emu_run(void)
144 static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}};
145 struct pollfd fds[3];
146 /* timer */
147- fds[0].fd = XConnectionNumber(display);
148+ fds[0].fd = XConnectionNumber(emu.dpy);
149 fds[1].fd = timerfd_create(CLOCK_MONOTONIC, 0);
150 timerfd_settime(fds[1].fd, 0, &screen_tspec, NULL);
151 fds[2].fd = STDIN_FILENO;
152@@ -1289,7 +1298,7 @@ emu_run(void)
153 while(!dev[0x0f]) {
154 if(poll(fds, 3, 1000) <= 0)
155 continue;
156- while(XPending(display))
157+ while(XPending(emu.dpy))
158 emu_event();
159 if(fds[1].revents & POLLIN) {
160 read(fds[1].fd, expirations, 8);
161@@ -1305,11 +1314,11 @@ emu_run(void)
162 console_input(0, CONSOLE_END), has_sent = 0;
163 }
164 }
165- if(ximage)
166- XDestroyImage(ximage);
167- if(window)
168- XDestroyWindow(display, window);
169- XCloseDisplay(display);
170+ if(emu.img)
171+ XDestroyImage(emu.img);
172+ if(emu.win)
173+ XDestroyWindow(emu.dpy, emu.win);
174+ XCloseDisplay(emu.dpy);
175 console_close();
176 }
177