commit ea1ce67

neauoire  ·  2022-03-27 16:11:14 +0000 UTC
parent 7d42770
Cleanup
2 files changed,  +52, -55
+3, -3
 1@@ -22,11 +22,11 @@ cc src/devices/datetime.c src/devices/system.c src/devices/file.c src/uxn.c -DND
 2 
 3 - `00` system
 4 - `10` console(partial)
 5-- `20` screen
 6+- `20` screen(partial/vector)
 7 - `30` audio(missing)
 8 - `70` midi(missing)
 9-- `80` controller(partial)
10-- `90` mouse
11+- `80` controller(partial/key)
12+- `90` mouse(partial/scroll)
13 - `a0` file(missing)
14 - `c0` datetime
15 
+49, -52
  1@@ -1,13 +1,6 @@
  2 #include <stdio.h>
  3-#include <time.h>
  4-#include <unistd.h>
  5-
  6-#include <X11/Xlib.h>
  7 #include <stdlib.h>
  8-#include <string.h>
  9-
 10-#define WIDTH 64 * 8
 11-#define HEIGHT 40 * 8
 12+#include <X11/Xlib.h>
 13 
 14 #include "uxn.h"
 15 #include "devices/system.h"
 16@@ -16,8 +9,15 @@
 17 #include "devices/mouse.h"
 18 #include "devices/datetime.h"
 19 
 20-static Device *devscreen, *devctrl, *devmouse;
 21 static XImage *ximage;
 22+static Display *display;
 23+static Visual *visual;
 24+static Window window;
 25+
 26+static Device *devscreen, *devctrl, *devmouse;
 27+
 28+#define WIDTH 64 * 8
 29+#define HEIGHT 40 * 8
 30 
 31 static int
 32 error(char *msg, const char *err)
 33@@ -70,8 +70,8 @@ load(Uxn *u, char *filepath)
 34 	return 1;
 35 }
 36 
 37-void
 38-redraw(Display *display, Visual *visual, Window window)
 39+static void
 40+redraw(void)
 41 {
 42 	screen_redraw(&uxn_screen, uxn_screen.pixels);
 43 	XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, 0, 0, uxn_screen.width, uxn_screen.height);
 44@@ -80,26 +80,30 @@ redraw(Display *display, Visual *visual, Window window)
 45 /* /usr/include/X11/keysymdef.h */
 46 
 47 #define XK_Escape 0xff1b
 48-
 49 #define XK_Left 0xff51
 50 #define XK_Up 0xff52
 51 #define XK_Right 0xff53
 52 #define XK_Down 0xff54
 53-
 54 #define XK_Home 0xff50
 55 #define XK_Shift 0xffe1
 56 #define XK_Control 0xffe3
 57 #define XK_Alt 0xffe9
 58 
 59-void
 60-processEvent(Display *display, Visual *visual, Window window)
 61+static void
 62+processEvent(void)
 63 {
 64 	XEvent ev;
 65 	XNextEvent(display, &ev);
 66 	switch(ev.type) {
 67 	case Expose:
 68-		redraw(display, visual, window);
 69+		redraw();
 70 		break;
 71+	case ClientMessage: {
 72+		XDestroyImage(ximage);
 73+		XDestroyWindow(display, window);
 74+		XCloseDisplay(display);
 75+		exit(0);
 76+	} break;
 77 	case KeyPress: {
 78 		XKeyPressedEvent *e = (XKeyPressedEvent *)&ev;
 79 		if(e->keycode == XKeysymToKeycode(display, XK_Escape)) exit(0);
 80@@ -131,29 +135,20 @@ processEvent(Display *display, Visual *visual, Window window)
 81 		XButtonPressedEvent *e = (XButtonPressedEvent *)&ev;
 82 		mouse_up(devmouse, e->button);
 83 	} break;
 84-
 85 	case MotionNotify: {
 86 		XMotionEvent *e = (XMotionEvent *)&ev;
 87 		mouse_pos(devmouse, e->x, e->y);
 88 	} break;
 89-	case ClientMessage: {
 90-		XClientMessageEvent *e = (XClientMessageEvent *)&ev;
 91-		XDestroyImage(ximage);
 92-		XDestroyWindow(display, window);
 93-		XCloseDisplay(display);
 94-		exit(0);
 95-	} break;
 96-	}
 97-	if(uxn_screen.fg.changed || uxn_screen.bg.changed) {
 98-		redraw(display, visual, window);
 99 	}
100 }
101 
102 static int
103-start(Uxn *u)
104+start(Uxn *u, char *rom)
105 {
106 	if(!uxn_boot(u, (Uint8 *)calloc(0x10000, sizeof(Uint8))))
107 		return error("Boot", "Failed");
108+	if(!load(u, rom))
109+		return error("Load", "Failed");
110 	/* system   */ uxn_port(u, 0x0, system_dei, system_deo);
111 	/* console  */ uxn_port(u, 0x1, nil_dei, console_deo);
112 	/* screen   */ devscreen = uxn_port(u, 0x2, screen_dei, screen_deo);
113@@ -170,6 +165,26 @@ start(Uxn *u)
114 	/* empty    */ uxn_port(u, 0xd, nil_dei, nil_deo);
115 	/* empty    */ uxn_port(u, 0xe, nil_dei, nil_deo);
116 	/* empty    */ uxn_port(u, 0xf, nil_dei, nil_deo);
117+	screen_resize(&uxn_screen, WIDTH, HEIGHT);
118+	if(!uxn_eval(u, PAGE_PROGRAM))
119+		return error("Boot", "Failed to start rom.");
120+	return 1;
121+}
122+
123+static int
124+init(void)
125+{
126+	Atom wmDelete;
127+	display = XOpenDisplay(NULL);
128+	visual = DefaultVisual(display, 0);
129+	window = XCreateSimpleWindow(display, RootWindow(display, 0), 0, 0, uxn_screen.width, uxn_screen.height, 1, 0, 0);
130+	if(visual->class != TrueColor)
131+		return error("Init", "True-color visual failed");
132+	XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask);
133+	wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True);
134+	XSetWMProtocols(display, window, &wmDelete, 1);
135+	XMapWindow(display, window);
136+	ximage = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, uxn_screen.width, uxn_screen.height, 32, 0);
137 	return 1;
138 }
139 
140@@ -177,35 +192,17 @@ int
141 main(int argc, char **argv)
142 {
143 	Uxn u;
144-
145 	if(argc < 2)
146 		return error("Usage", "uxncli game.rom args");
147-	if(!start(&u))
148+	if(!start(&u, argv[1]))
149 		return error("Start", "Failed");
150-	if(!load(&u, argv[1]))
151-		return error("Load", "Failed");
152-
153-	screen_resize(&uxn_screen, WIDTH, HEIGHT);
154-
155-	if(!uxn_eval(&u, PAGE_PROGRAM))
156-		return error("Boot", "Failed to start rom.");
157-
158-	Display *display = XOpenDisplay(NULL);
159-	Visual *visual = DefaultVisual(display, 0);
160-	Window window = XCreateSimpleWindow(display, RootWindow(display, 0), 0, 0, uxn_screen.width, uxn_screen.height, 1, 0, 0);
161-	if(visual->class != TrueColor) {
162-		fprintf(stderr, "Cannot handle non true color visual ...\n");
163-		exit(1);
164-	}
165-
166-	XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask);
167-	Atom wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True);
168-	XSetWMProtocols(display, window, &wmDelete, 1);
169-	XMapWindow(display, window);
170-	ximage = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, uxn_screen.width, uxn_screen.height, 32, 0);
171+	if(!init())
172+		return error("Init", "Failed");
173 	while(1) {
174-		processEvent(display, visual, window);
175+		processEvent();
176 		uxn_eval(&u, GETVECTOR(devscreen));
177+		if(uxn_screen.fg.changed || uxn_screen.bg.changed)
178+			redraw();
179 		/* sleep(0.01); */
180 	}
181 	XDestroyImage(ximage);