#include #include #include #include #include #include #include #include #include "uxn.h" #include "devices/system.h" #include "devices/screen.h" #include "devices/controller.h" #include "devices/mouse.h" #include "devices/file.h" #include "devices/datetime.h" static XImage *ximage; static Display *display; static Visual *visual; static Window window; #define WIDTH (64 * 8) #define HEIGHT (40 * 8) static int error(char *msg, const char *err) { fprintf(stderr, "Error %s: %s\n", msg, err); return 0; } void system_deo_special(Uint8 *d, Uint8 port) { if(port > 0x7 && port < 0xe) screen_palette(&uxn_screen, &d[0x8]); } static int console_input(Uxn *u, char c) { Uint8 *d = u->dev[1].dat; d[0x2] = c; return uxn_eval(u, GETVEC(d)); } static void console_deo(Uint8 *d, Uint8 port) { FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr : 0; if(fd) { fputc(d[port], fd); fflush(fd); } } static Uint8 uxn11_dei(struct Uxn *u, Uint8 addr) { Uint8 p = addr & 0x0f; Device *d = &u->dev[addr >> 4]; switch(addr & 0xf0) { case 0x20: return screen_dei(d->dat, p); break; case 0x80: return u->dpg[0x80 + p]; break; case 0x90: return u->dpg[0x90 + p]; break; case 0xa0: return file_dei(0, d->dat, p); break; case 0xb0: return file_dei(1, d->dat, p); break; case 0xc0: return datetime_dei(d->dat, p); break; } return d->dat[p]; } static void uxn11_deo(Uxn *u, Uint8 addr, Uint8 v) { Uint8 p = addr & 0x0f; Device *d = &u->dev[addr >> 4]; d->dat[p] = v; switch(addr & 0xf0) { case 0x00: system_deo(u, d->dat, p); break; case 0x10: console_deo(d->dat, p); break; case 0x20: screen_deo(u->ram, d->dat, p); break; case 0x80: u->dpg[0x80 + p] = v; break; case 0x90: u->dpg[0x90 + p] = v; break; case 0xa0: file_deo(0, u->ram, d, p); break; case 0xb0: file_deo(1, u->ram, d, p); break; } } static void redraw(void) { screen_redraw(&uxn_screen, uxn_screen.pixels); XPutImage(display, window, DefaultGC(display, 0), ximage, 0, 0, 0, 0, uxn_screen.width, uxn_screen.height); } static void hide_cursor(void) { Cursor blank; Pixmap bitmap; XColor black; static char empty[] = {0, 0, 0, 0, 0, 0, 0, 0}; black.red = black.green = black.blue = 0; bitmap = XCreateBitmapFromData(display, window, empty, 8, 8); blank = XCreatePixmapCursor(display, bitmap, bitmap, &black, &black, 0, 0); XDefineCursor(display, window, blank); XFreeCursor(display, blank); XFreePixmap(display, bitmap); } static Uint8 get_button(KeySym sym) { switch(sym) { case XK_Up: return 0x10; case XK_Down: return 0x20; case XK_Left: return 0x40; case XK_Right: return 0x80; case XK_Control_L: return 0x01; case XK_Alt_L: return 0x02; case XK_Shift_L: return 0x04; case XK_Home: return 0x08; } return 0x00; } static void processEvent(Uxn *u) { XEvent ev; XNextEvent(display, &ev); switch(ev.type) { case Expose: redraw(); break; case ClientMessage: { XDestroyImage(ximage); XDestroyWindow(display, window); XCloseDisplay(display); exit(0); } break; case KeyPress: { KeySym sym; char buf[7]; XLookupString((XKeyPressedEvent *)&ev, buf, 7, &sym, 0); controller_down(u, &u->dpg[0x80], get_button(sym)); controller_key(u, &u->dpg[0x80], sym < 0x80 ? sym : buf[0]); } break; case KeyRelease: { KeySym sym; char buf[7]; XLookupString((XKeyPressedEvent *)&ev, buf, 7, &sym, 0); controller_up(u, &u->dpg[0x80], get_button(sym)); } break; case ButtonPress: { XButtonPressedEvent *e = (XButtonPressedEvent *)&ev; mouse_down(u, &u->dpg[0x90], 0x1 << (e->button - 1)); } break; case ButtonRelease: { XButtonPressedEvent *e = (XButtonPressedEvent *)&ev; mouse_up(u, &u->dpg[0x90], 0x1 << (e->button - 1)); } break; case MotionNotify: { XMotionEvent *e = (XMotionEvent *)&ev; mouse_pos(u, &u->dpg[0x90], e->x, e->y); } break; } } static int start(Uxn *u, char *rom) { if(!uxn_boot(u, (Uint8 *)calloc(0x10300, sizeof(Uint8)))) return error("Boot", "Failed"); if(!load_rom(u, rom)) return error("Load", "Failed"); fprintf(stderr, "Loaded %s\n", rom); u->dei = uxn11_dei; u->deo = uxn11_deo; screen_resize(&uxn_screen, WIDTH, HEIGHT); if(!uxn_eval(u, PAGE_PROGRAM)) return error("Boot", "Failed to start rom."); return 1; } static int init(void) { Atom wmDelete; display = XOpenDisplay(NULL); visual = DefaultVisual(display, 0); window = XCreateSimpleWindow(display, RootWindow(display, 0), 0, 0, uxn_screen.width, uxn_screen.height, 1, 0, 0); if(visual->class != TrueColor) return error("Init", "True-color visual failed"); XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask); wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True); XSetWMProtocols(display, window, &wmDelete, 1); XMapWindow(display, window); ximage = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, uxn_screen.width, uxn_screen.height, 32, 0); hide_cursor(); return 1; } int main(int argc, char **argv) { Uxn u; int i; char expirations[8]; struct pollfd fds[2]; static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}}; if(argc < 2) return error("Usage", "uxncli game.rom args"); if(!start(&u, argv[1])) return error("Start", "Failed"); if(!init()) return error("Init", "Failed"); /* console vector */ for(i = 2; i < argc; i++) { char *p = argv[i]; while(*p) console_input(&u, *p++); console_input(&u, '\n'); } fds[0].fd = XConnectionNumber(display); fds[1].fd = timerfd_create(CLOCK_MONOTONIC, 0); timerfd_settime(fds[1].fd, 0, &screen_tspec, NULL); fds[0].events = fds[1].events = POLLIN; /* main loop */ while(1) { if(poll(fds, 2, 1000) <= 0) continue; while(XPending(display)) processEvent(&u); if(poll(&fds[1], 1, 0)) { read(fds[1].fd, expirations, 8); /* Indicate we handled the timer */ uxn_eval(&u, GETVEC(u.dev[0x2].dat)); /* Call the vector once, even if the timer fired multiple times */ } if(uxn_screen.fg.changed || uxn_screen.bg.changed) redraw(); } XDestroyImage(ximage); return 0; }