From 8cf471bec9483e3735ecd3100fc998922c0c6d16 Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 19 Oct 2021 14:11:45 +0200 Subject: [PATCH 01/68] Add prototypical SDL support. Build with cmake. This will create a new backend (ldesdl). - Resolution can only be set by editing the variables in sdl.c. - Key repeat does not work. - Still problems with keysyms that implicitly contain modifiers. - The entire screen is bitblted onto the SDL display every frame. Support keyboard, and work on mouse. Kind of working... Fix display resolution problems. --- CMakeLists.txt | 43 +++++- bin/makefile-tail | 3 + src/initdsp.c | 11 +- src/initkbd.c | 11 +- src/ldeboot.c | 10 +- src/main.c | 4 +- src/sdl.c | 353 ++++++++++++++++++++++++++++++++++++++++++++++ src/xc.c | 6 +- 8 files changed, 433 insertions(+), 8 deletions(-) create mode 100644 src/sdl.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 957bc00..8729a39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,7 @@ SET(MAIKO_DEFINITIONS "-DRELEASE=351" ) -OPTION(MAIKO_DISPLAY_X11 "Use X11 for display." ON) +OPTION(MAIKO_DISPLAY_X11 "Use X11 for display." OFF) IF(MAIKO_DISPLAY_X11) FIND_PACKAGE(X11 REQUIRED) SET(MAIKO_DISPLAY_X11_DEFINITIONS @@ -91,6 +91,19 @@ ELSEIF(MAIKO_NETWORK_TYPE STREQUAL "SUN_NIT") MESSAGE("-- Configured for (SunOS) NIT networking") ELSEIF(NOT MAIKO_NETWORK_TYPE STREQUAL "NONE") MESSAGE(WARNING "Invalid option given for MAIKO_NETWORK_TYPE, must be one of:\nNONE, SUN_DLPI, SUN_NIT, NETHUB") + +OPTION(MAIKO_DISPLAY_SDL "Use SDL for display." ON) +IF(MAIKO_DISPLAY_SDL) + FIND_PACKAGE(SDL2 REQUIRED) + SET(MAIKO_DISPLAY_SDL_DEFINITIONS + "-DSDL" + ) + SET(MAIKO_DISPLAY_SDL_LIBRARIES SDL2::SDL2) + SET(MAIKO_DISPLAY_SDL_SRCS + src/sdl.c + ) + SET(MAIKO_DISPLAY_SDL_HDRS + ) ENDIF() IF(CMAKE_SYSTEM_NAME STREQUAL "SunOS") @@ -413,6 +426,13 @@ IF(MAIKO_DISPLAY_X11) TARGET_LINK_LIBRARIES(lde X11::X11) ENDIF() +IF(MAIKO_DISPLAY_SDL) + # Tell it that the SDL launcher is available. + TARGET_COMPILE_DEFINITIONS(lde PUBLIC ${MAIKO_DISPLAY_SDL_DEFINITIONS}) + # This is needed so that it can call XOpenDisplay. + TARGET_LINK_LIBRARIES(lde SDL2::SDL2) +ENDIF() + ADD_EXECUTABLE(ldeether src/ldeether.c src/dlpi.c) TARGET_COMPILE_DEFINITIONS(ldeether PUBLIC ${MAIKO_DEFINITIONS}) TARGET_INCLUDE_DIRECTORIES(ldeether PUBLIC inc) @@ -431,6 +451,27 @@ IF(MAIKO_DISPLAY_X11) TARGET_LINK_LIBRARIES(ldex ${MAIKO_LIBRARIES} ${MAIKO_DISPLAY_X11_LIBRARIES}) ENDIF() +<<<<<<< HEAD +======= +IF(MAIKO_DISPLAY_SDL) + ADD_EXECUTABLE(ldesdl + src/main.c + vdate.c + ${MAIKO_SRCS} + ${MAIKO_HDRS} + ${MAIKO_DISPLAY_SDL_SRCS} + ${MAIKO_DISPLAY_SDL_HDRS} + ) + TARGET_COMPILE_DEFINITIONS(ldesdl PUBLIC ${MAIKO_DEFINITIONS} ${MAIKO_DISPLAY_SDL_DEFINITIONS}) + TARGET_INCLUDE_DIRECTORIES(ldesdl PUBLIC inc) + TARGET_LINK_LIBRARIES(ldesdl ${MAIKO_LIBRARIES} ${MAIKO_DISPLAY_SDL_LIBRARIES}) +ENDIF() + +ADD_EXECUTABLE(mkvdate src/mkvdate.c) +TARGET_COMPILE_DEFINITIONS(mkvdate PUBLIC ${MAIKO_DEFINITIONS}) +TARGET_INCLUDE_DIRECTORIES(mkvdate PUBLIC inc) + +>>>>>>> f58abe3 (Add prototypical SDL support.) ADD_EXECUTABLE(setsout src/setsout.c src/byteswap.c) TARGET_COMPILE_DEFINITIONS(setsout PUBLIC ${MAIKO_DEFINITIONS}) TARGET_INCLUDE_DIRECTORIES(setsout PUBLIC inc) diff --git a/bin/makefile-tail b/bin/makefile-tail index ad64c19..8566abf 100644 --- a/bin/makefile-tail +++ b/bin/makefile-tail @@ -996,6 +996,9 @@ $(OBJECTDIR)xwinman.o: $(SRCDIR)xwinman.c $(REQUIRED-INCS) \ $(INCDIR)xlspwindefs.h $(INCDIR)xscrolldefs.h $(CC) $(RFLAGS) $(SRCDIR)xwinman.c -o $(OBJECTDIR)xwinman.o +$(OBJECTDIR)sdl.o: $(SRCDIR)sdl.c $(REQUIRED-INCS) + $(CC) $(RFLAGS) $(SRCDIR)sdl.c -o $(OBJECTDIR)sdl.o + $(OBJECTDIR)foreign.o: $(SRCDIR)foreign.c $(REQUIRED-INCS) \ $(INCDIR)/foreigndefs.h $(CC) $(RFLAGS) $(SRCDIR)foreign.c -o $(OBJECTDIR)foreign.o diff --git a/src/initdsp.c b/src/initdsp.c index 86c0335..4551ccc 100644 --- a/src/initdsp.c +++ b/src/initdsp.c @@ -58,6 +58,8 @@ int FrameBufferFd = -1; extern unsigned displaywidth, displayheight, DisplayRasterWidth, DisplayType, DisplayByteSize; unsigned displaywidth, displayheight, DisplayRasterWidth, DisplayType, DisplayByteSize; +extern int sdl_displaywidth, sdl_displayheight; +int sdl_displaywidth, sdl_displayheight; DLword *DisplayRegion68k; /* 68k addr of #{}22,0 */ #ifdef DISPLAYBUFFER @@ -169,7 +171,10 @@ void init_display2(DLword *display_addr, unsigned display_max) displaywidth = currentdsp->Display.width; displayheight = currentdsp->Display.height; #endif /* XWINDOW */ - +#if (defined(SDL)) + displaywidth = sdl_displaywidth; + displayheight = sdl_displayheight; +#endif /* SDL */ DisplayRasterWidth = displaywidth / BITSPER_DLWORD; if ((displaywidth * displayheight) > display_max) { displayheight = display_max / displaywidth; } @@ -183,7 +188,9 @@ void init_display2(DLword *display_addr, unsigned display_max) DisplayType = SUN2BW; DisplayRegion68k_end_addr = DisplayRegion68k + DisplayRasterWidth * displayheight; #endif /* XWINDOW */ - +#ifdef SDL + DisplayType = SUN2BW; +#endif /* SDL */ init_cursor(); DisplayByteSize = ((displaywidth * displayheight / 8 + ((unsigned)getpagesize() - 1)) & (unsigned)-getpagesize()); diff --git a/src/initkbd.c b/src/initkbd.c index d8af8b1..3b891cd 100644 --- a/src/initkbd.c +++ b/src/initkbd.c @@ -283,6 +283,7 @@ void set_kbd_iopointers(void) { #define KB_HP9000 (10 + MIN_KEYTYPE) /* TODO: Can we remove this? */ #define KB_X (11 + MIN_KEYTYPE) #define KB_DOS (12 + MIN_KEYTYPE) +#define KB_SDL (13 + MIN_KEYTYPE) /* KB_SUN4 not defined in older OS versions */ #ifndef KB_SUN4 @@ -442,6 +443,8 @@ void keyboardtype(int fd) type = KB_X; #elif DOS type = KB_DOS; +#elif SDL + type = KB_SDL; #endif /* XWINDOW */ } /* if end */ else { @@ -457,6 +460,8 @@ void keyboardtype(int fd) type = KB_X; else if (strcmp("x", key) == 0) type = KB_X; + else if (strcmp("sdl", key) == 0) + type = KB_SDL; else type = KB_SUN3; /* default */ } @@ -493,7 +498,11 @@ void keyboardtype(int fd) InterfacePage->devconfig |= KB_SUN3 - MIN_KEYTYPE; /* 10 */ break; #endif /* XWINDOW */ - +#ifdef SDL + case KB_SDL: + InterfacePage->devconfig |= KB_SUN3 - MIN_KEYTYPE; /* 10 */ + break; +#endif /* SDL */ #ifdef DOS case KB_DOS: SUNLispKeyMap = DOSLispKeyMap_101; diff --git a/src/ldeboot.c b/src/ldeboot.c index a01f612..780d336 100644 --- a/src/ldeboot.c +++ b/src/ldeboot.c @@ -42,6 +42,14 @@ #define LDEX "ldex" #endif /* XWINDOW */ +#ifdef SDL +#define LDESDL "ldesdl" +#endif +#define LDEMONO "ldesingle" +#define LDECOLOR "ldemulti" +#define LDETRUECOLOR "ldetruecolor" + +#define FBTYPE_SUNFAST_COLOR 12 /************************************************************************/ @@ -188,7 +196,7 @@ int main(int argc, char *argv[]) fork_Unix(); /* start ldemono or ldecolor */ - + strcpy(filetorun,LDESDL); if (filetorun[0] == '\0') { fprintf(stderr, "Unable to determine what display program to run.\n"); exit(1); diff --git a/src/main.c b/src/main.c index 36d2a44..40144eb 100644 --- a/src/main.c +++ b/src/main.c @@ -598,7 +598,9 @@ int main(int argc, char *argv[]) #if defined(DOS) || defined(XWINDOW) make_dsp_instance(currentdsp, 0, 0, 0, 1); /* All defaults the first time */ #endif /* DOS || XWINDOW */ - +#if defined(SDL) + init_SDL(); +#endif /* SDL */ /* Load sysout to VM space and returns real sysout_size(not 0) */ sysout_size = sysout_loader(sysout_name, sysout_size); diff --git a/src/sdl.c b/src/sdl.c new file mode 100644 index 0000000..b35f6d2 --- /dev/null +++ b/src/sdl.c @@ -0,0 +1,353 @@ +#include +#include +#include "lispemul.h" +#include "miscstat.h" +#include "keyboard.h" +static SDL_Window *sdl_window = NULL; +static SDL_Surface *sdl_screenSurface = NULL; +static SDL_Renderer *sdl_renderer = NULL; +extern int KBDEventFlg; +int keymap[] = { + 0, SDLK_5, + 1, SDLK_4, + 2, SDLK_6, + 3, SDLK_e, + 4, SDLK_7, + 5, SDLK_d, + 6, SDLK_u, + 7, SDLK_v, + 8, SDLK_RIGHTPAREN, + 8, SDLK_0, + 9, SDLK_k, + 10, SDLK_MINUS, + 11, SDLK_p, + 12, SDLK_SLASH, + 13, SDLK_KP_PERIOD, + 14, SDLK_SCROLLLOCK, + 15, SDLK_BACKSPACE, + 16, SDLK_3, + 17, SDLK_2, + 18, SDLK_w, + 19, SDLK_q, + 20, SDLK_s, + 21, SDLK_a, + 22, SDLK_LEFTPAREN, + 22, SDLK_9, + 23, SDLK_i, + 24, SDLK_x, + 25, SDLK_o, + 26, SDLK_l, + 27, SDLK_COMMA, + 28, SDLK_QUOTE, + 29, SDLK_RIGHTBRACKET, + 31, SDLK_LALT, /* Meta, Sun-4 usual key */ + 32, SDLK_1, + 33, SDLK_ESCAPE, + 34, SDLK_TAB, + 35, SDLK_f, + 36, SDLK_LCTRL, + 37, SDLK_c, + 38, SDLK_j, + 39, SDLK_b, + 40, SDLK_z, + 41, SDLK_LSHIFT, + 42, SDLK_PERIOD, + 43, SDLK_SEMICOLON, + 43, SDLK_COLON, + 44, SDLK_RETURN, + 45, SDLK_BACKQUOTE, + 47, SDLK_RCTRL, + 48, SDLK_r, + 49, SDLK_t, + 50, SDLK_g, + 51, SDLK_y, + 52, SDLK_h, + 53, SDLK_8, + 54, SDLK_n, + 55, SDLK_m, + 56, SDLK_CAPSLOCK, + 57, SDLK_SPACE, + 58, SDLK_LEFTBRACKET, + 59, SDLK_EQUALS, + 60, SDLK_RSHIFT, + 61, SDLK_F11, + 61, SDLK_PAUSE, + 62, SDLK_HOME, + 63, SDLK_PAGEUP, + 64, SDLK_KP_EQUALS, + 65, SDLK_KP_DIVIDE, + 66, SDLK_F7, + 67, SDLK_F4, + 68, SDLK_F5, + 69, SDLK_KP_2, + 70, SDLK_KP_3, + // 71, XK_Linefeed, + // 73, XK_Numlock, + 76, SDLK_KP_ENTER, + 80, SDLK_F9, + 81, SDLK_KP_7, + 82, SDLK_KP_8, + 83, SDLK_KP_9, + 84, SDLK_KP_4, + 85, SDLK_KP_5, + 86, SDLK_LALT, /* (sun left-diamond key) */ + 87, SDLK_KP_6, + 89, SDLK_INSERT, + 90, SDLK_END, + 91, SDLK_F12, + 92, SDLK_PRINTSCREEN, // is this XK_Print?? + 93, SDLK_MODE, // is this XK_Mode_switch + 94, SDLK_KP_1, + 95, SDLK_KP_MULTIPLY, + 96, SDLK_KP_MINUS, + 97, SDLK_HELP, + 98, SDLK_KP_0, + 99, SDLK_F2, + 100, SDLK_F3, + 101, SDLK_F6, + 102, SDLK_KP_PLUS, + 104, SDLK_F8, + 105, SDLK_BACKSLASH, + 106, SDLK_F10, + 107, SDLK_F11, + 108, SDLK_F12, + -1, -1 +}; +int sdl_displaywidth = 1600; +int sdl_displayheight = 1024; +extern char *DisplayRegion68k; + +void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel) +{ + if(x >= surface->w) + return; + if(y >= surface->h) + return; + Uint32 * const target_pixel = (Uint32 *) ((Uint8 *) surface->pixels + + y * surface->pitch + + x * surface->format->BytesPerPixel); + *target_pixel = pixel; +} + +extern DLword *EmKbdAd068K, *EmKbdAd168K, *EmKbdAd268K, *EmKbdAd368K, *EmKbdAd468K, *EmKbdAd568K, + *EmRealUtilin68K; +extern DLword *CTopKeyevent; +extern int URaid_req; +extern LispPTR *KEYBUFFERING68k; + +void DoRing() { + DLword w, r; + KBEVENT *kbevent; + + do_ring: + /* DEL is not generally present on a Mac X keyboard, Ctrl-shift-ESC would be 18496 */ + if (((*EmKbdAd268K) & 2113) == 0) { /*Ctrl-shift-NEXT*/ + error("****** EMERGENCY Interrupt ******"); + *EmKbdAd268K = KB_ALLUP; /*reset*/ + ((RING *)CTopKeyevent)->read = 0; /* reset queue */ + ((RING *)CTopKeyevent)->write = MINKEYEVENT; + /*return(0);*/ + } else if (((*EmKbdAd268K) & 2114) == 0) { /* Ctrl-Shift-DEL */ + *EmKbdAd268K = KB_ALLUP; /*reset*/ + URaid_req = T; + ((RING *)CTopKeyevent)->read = 0; /* reset queue */ + ((RING *)CTopKeyevent)->write = MINKEYEVENT; + /*return(0);*/ + } + +#ifdef OS4_TYPE4BUG + else if (((*EmKbdAd268K) & 2120) == 0) { /* Ctrl-Shift-Return */ + *EmKbdAd268K = KB_ALLUP; /*reset*/ + URaid_req = T; + ((RING *)CTopKeyevent)->read = 0; /* reset queue */ + ((RING *)CTopKeyevent)->write = MINKEYEVENT; + } +#endif + + r = RING_READ(CTopKeyevent); + w = RING_WRITE(CTopKeyevent); + + if (r == w) /* event queue FULL */ + goto KBnext; + + kbevent = (KBEVENT *)(CTopKeyevent + w); + /* RCLK(kbevent->time); */ + kbevent->W0 = *EmKbdAd068K; + kbevent->W1 = *EmKbdAd168K; + kbevent->W2 = *EmKbdAd268K; + kbevent->W3 = *EmKbdAd368K; + kbevent->W4 = *EmKbdAd468K; + kbevent->W5 = *EmKbdAd568K; + kbevent->WU = *EmRealUtilin68K; + + if (r == 0) /* Queue was empty */ + ((RING *)CTopKeyevent)->read = w; + if (w >= MAXKEYEVENT) + ((RING *)CTopKeyevent)->write = MINKEYEVENT; + else + ((RING *)CTopKeyevent)->write = w + KEYEVENTSIZE; + + KBnext: + if (*KEYBUFFERING68k == NIL) *KEYBUFFERING68k = ATOM_T; +} + +void sdl_bitblt_to_screen(uint32_t *source) { + int width = sdl_displaywidth; + int height = sdl_displayheight; + int bpw = 32; + for(int y = 0; y < height; y++) { + for(int x = 0; x < width / bpw; x++) { + for(int b = 0; b < bpw; b++) { + //printf("%d/%d %d\n", x, y, b); + int px = 0; + if(source[y*(sdl_displaywidth/bpw) + x] & (1 << (bpw - 1 - b))) { + px = 0xff000000; + } else { + px = 0xffffffff; + } + //printf("px is %x\n", px); + set_pixel(sdl_screenSurface, x * bpw + b, y, px); + } + } + } +} +int map_key(SDL_Keycode k) { + for(int i = 0; keymap[i] != -1; i+= 2) { + if(keymap[i+1] == k) + return keymap[i]; + } + return -1; +} +#define KEYCODE_OFFSET 0 +void handle_keydown(SDL_Keycode k, unsigned short mod) { + int lk = map_key(k); + if(lk == -1) { + printf("No mapping for key %s\n", SDL_GetKeyName(k)); + } else { + printf("dn %s -> lisp keycode %d (0x%x)\n", SDL_GetKeyName(k), lk, mod); + kb_trans(lk - KEYCODE_OFFSET, FALSE); + DoRing(); + if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; + } +} +void handle_keyup(SDL_Keycode k, unsigned short mod) { + int lk = map_key(k); + if(lk == -1) { + printf("No mapping for key %s\n", SDL_GetKeyName(k)); + } else { + printf("up %s -> lisp keycode %d (0x%x)\n", SDL_GetKeyName(k), lk, mod); + kb_trans(lk - KEYCODE_OFFSET, TRUE); + DoRing(); + if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; + } +} +extern DLword *EmCursorX68K, *EmCursorY68K; +extern DLword *EmMouseX68K, *EmMouseY68K, *EmKbdAd068K, *EmRealUtilin68K; +extern LispPTR *CLastUserActionCell68k; +extern MISCSTATS *MiscStats; + +/* bits within the EmRealUtilin word */ +#define KEYSET_LEFT 8 +#define KEYSET_LEFTMIDDLE 9 +#define KEYSET_MIDDLE 10 +#define KEYSET_RIGHTMIDDLE 11 +#define KEYSET_RIGHT 12 +/* Mouse buttons */ +#define MOUSE_LEFT 13 +#define MOUSE_RIGHT 14 +#define MOUSE_MIDDLE 15 + +void process_SDLevents() { + SDL_Event event; + while(SDL_PollEvent(&event)) { + switch(event.type) { + case SDL_QUIT: + printf("quitting\n"); exit(0); + break; + case SDL_WINDOWEVENT: + switch(event.window.event) { + case SDL_WINDOWEVENT_RESIZED: sdl_screenSurface = SDL_GetWindowSurface(sdl_window); break; + } + break; + case SDL_KEYDOWN: + // printf("type: %x, state: %x, repeat: %x, scancode: %x, sym: %x, mod: %x\n", event.key.type, event.key.state, event.key.repeat, event.key.keysym.scancode, event.key.keysym.sym, event.key.keysym.mod); + handle_keydown(event.key.keysym.sym, event.key.keysym.mod); + break; + case SDL_KEYUP: + handle_keyup(event.key.keysym.sym, event.key.keysym.mod); + break; + case SDL_MOUSEMOTION: { + int x, y; + SDL_GetMouseState(&x, &y); + *CLastUserActionCell68k = MiscStats->secondstmp; + *EmCursorX68K = (*((DLword *)EmMouseX68K)) = + (short)(x & 0xFFFF); + *EmCursorY68K = (*((DLword *)EmMouseY68K)) = + (short)(y & 0xFFFF); + DoRing(); + if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; + } + break; + case SDL_MOUSEBUTTONDOWN: { + int button = event.button.button; + switch(button) { + case 1: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_LEFT, FALSE); break; + case 2: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_MIDDLE, FALSE); break; + case 3: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_RIGHT, FALSE); break; + case 4: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFT, FALSE); break; + case 5: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFTMIDDLE, FALSE); break; + case 6: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHT, FALSE); break; + case 7: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHTMIDDLE, FALSE); break; + } + DoRing(); + if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; + } + break; + case SDL_MOUSEBUTTONUP: { + int button = event.button.button; + switch(button) { + case 1: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_LEFT, TRUE); break; + case 2: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_MIDDLE, TRUE); break; + case 3: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_RIGHT, TRUE); break; + case 4: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFT, TRUE); break; + case 5: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFTMIDDLE, TRUE); break; + case 6: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHT, TRUE); break; + case 7: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHTMIDDLE, TRUE); break; + } + DoRing(); + if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; + } + break; + /* case SDL_KEYMAPCHANGED: */ + /* printf("SDL_KEYMAPCHANGED\n"); break; */ + /* case SDL_TEXTINPUT: */ + /* printf("SDL_TEXTINPUT\n"); break; */ + default: + printf("other event type: %d\n", event.type); + } + } + SDL_SetRenderDrawColor(sdl_renderer, 50, 50, 50, 255); + sdl_bitblt_to_screen(DisplayRegion68k); + SDL_UpdateWindowSurface(sdl_window); +} +int init_SDL() { + int width = sdl_displaywidth; + int height = sdl_displayheight; + printf("requested width: %d, height: %d\n", width, height); + if(SDL_Init(SDL_INIT_VIDEO) < 0) { + printf("SDL could not be initialized. SDL_Error: %s\n", SDL_GetError()); + return 1; + } + printf("initialised\n"); + sdl_window = SDL_CreateWindow("Maiko", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 0); + printf("Window created\n"); + if(sdl_window == NULL) { + printf("Window could not be created. SDL_Error: %s\n", SDL_GetError()); + return 2; + } + sdl_screenSurface = SDL_GetWindowSurface(sdl_window); + printf("surface: %dx%d, pitch %d\n", sdl_screenSurface->w, sdl_screenSurface->h, sdl_screenSurface->pitch); + printf(" format %d, bitspp %d, bytespp %d\n", sdl_screenSurface->format->format, sdl_screenSurface->format->BitsPerPixel, sdl_screenSurface->format->BytesPerPixel); + printf("SDL initialised\n"); + return 0; +} diff --git a/src/xc.c b/src/xc.c index 1e9c800..02e9031 100644 --- a/src/xc.c +++ b/src/xc.c @@ -1131,10 +1131,12 @@ check_interrupt: * If the system is configured with SIGIO handling we have a hint * that allows us to cheaply skip if there's nothing to do */ -#ifndef INIT +#ifdef XWINDOW process_Xevents(currentdsp); #endif - +#ifdef SDL + process_SDLevents(); +#endif if (IO_Signalled) { IO_Signalled = FALSE; process_io_events(); From a722e01a4f4fbe08b1b00987d4a2f22d1ac17f24 Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 20 Oct 2021 13:14:34 +0200 Subject: [PATCH 02/68] Change SDL backend to accelerated renderer, support pixel scaling. Edit main.c, init_SDL(1600, 1024, 1) means lisp display size 1600x1024, pixel scaling 1. Try init_SDL(800, 512, 2) or even init_SDL(608, 1023, 3). --- src/initdsp.c | 4 +- src/main.c | 2 +- src/sdl.c | 132 ++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 105 insertions(+), 33 deletions(-) diff --git a/src/initdsp.c b/src/initdsp.c index 4551ccc..017c16c 100644 --- a/src/initdsp.c +++ b/src/initdsp.c @@ -56,10 +56,10 @@ extern DspInterface currentdsp; int FrameBufferFd = -1; +extern int sdl_displaywidth, sdl_displayheight, sdl_pixelscale; +int sdl_displaywidth, sdl_displayheight, sdl_pixelscale; extern unsigned displaywidth, displayheight, DisplayRasterWidth, DisplayType, DisplayByteSize; unsigned displaywidth, displayheight, DisplayRasterWidth, DisplayType, DisplayByteSize; -extern int sdl_displaywidth, sdl_displayheight; -int sdl_displaywidth, sdl_displayheight; DLword *DisplayRegion68k; /* 68k addr of #{}22,0 */ #ifdef DISPLAYBUFFER diff --git a/src/main.c b/src/main.c index 40144eb..d7acc81 100644 --- a/src/main.c +++ b/src/main.c @@ -599,7 +599,7 @@ int main(int argc, char *argv[]) make_dsp_instance(currentdsp, 0, 0, 0, 1); /* All defaults the first time */ #endif /* DOS || XWINDOW */ #if defined(SDL) - init_SDL(); + init_SDL(1600, 1024, 1); #endif /* SDL */ /* Load sysout to VM space and returns real sysout_size(not 0) */ sysout_size = sysout_loader(sysout_name, sysout_size); diff --git a/src/sdl.c b/src/sdl.c index b35f6d2..a0198ec 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -4,8 +4,10 @@ #include "miscstat.h" #include "keyboard.h" static SDL_Window *sdl_window = NULL; -static SDL_Surface *sdl_screenSurface = NULL; static SDL_Renderer *sdl_renderer = NULL; +static SDL_Texture *sdl_texture = NULL; +static Uint32 *buffer = NULL; + extern int KBDEventFlg; int keymap[] = { 0, SDLK_5, @@ -113,21 +115,18 @@ int keymap[] = { 108, SDLK_F12, -1, -1 }; -int sdl_displaywidth = 1600; -int sdl_displayheight = 1024; +// all of the following are overwritten, the values here are irrelevant defaults! +// actual size of the lisp display in pixels. +int sdl_displaywidth = 0; +int sdl_displayheight = 0; +// current size of the window, in pixels +int sdl_windowwidth = 0; +int sdl_windowheight = 0; +// each pixel is shown as this many pixels +int sdl_pixelscale = 0; + extern char *DisplayRegion68k; -void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel) -{ - if(x >= surface->w) - return; - if(y >= surface->h) - return; - Uint32 * const target_pixel = (Uint32 *) ((Uint8 *) surface->pixels - + y * surface->pitch - + x * surface->format->BytesPerPixel); - *target_pixel = pixel; -} extern DLword *EmKbdAd068K, *EmKbdAd168K, *EmKbdAd268K, *EmKbdAd368K, *EmKbdAd468K, *EmKbdAd568K, *EmRealUtilin68K; @@ -191,22 +190,35 @@ void DoRing() { if (*KEYBUFFERING68k == NIL) *KEYBUFFERING68k = ATOM_T; } +void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel) +{ + if(x >= surface->w) + return; + if(y >= surface->h) + return; + Uint32 * const target_pixel = (Uint32 *) ((Uint8 *) surface->pixels + + y * surface->pitch + + x * surface->format->BytesPerPixel); + *target_pixel = pixel; +} void sdl_bitblt_to_screen(uint32_t *source) { int width = sdl_displaywidth; int height = sdl_displayheight; int bpw = 32; for(int y = 0; y < height; y++) { for(int x = 0; x < width / bpw; x++) { + int w = source[y*(sdl_displaywidth/bpw) + x]; for(int b = 0; b < bpw; b++) { //printf("%d/%d %d\n", x, y, b); int px = 0; - if(source[y*(sdl_displaywidth/bpw) + x] & (1 << (bpw - 1 - b))) { + if(w & (1 << (bpw - 1 - b))) { px = 0xff000000; } else { px = 0xffffffff; } //printf("px is %x\n", px); - set_pixel(sdl_screenSurface, x * bpw + b, y, px); + int xx = x * bpw + b; + buffer[y * sdl_displaywidth + xx] = px; } } } @@ -256,7 +268,26 @@ extern MISCSTATS *MiscStats; #define MOUSE_LEFT 13 #define MOUSE_RIGHT 14 #define MOUSE_MIDDLE 15 - +void sdl_update_viewport(int width, int height) { + int w = width / 32 * 32; + if(w > sdl_displaywidth * sdl_pixelscale) + w = sdl_displaywidth * sdl_pixelscale; + int h = height / 32 * 32; + if(h > sdl_displayheight * sdl_pixelscale) + h = sdl_displayheight * sdl_pixelscale; + SDL_Rect r; + r.x = 0; + r.y = 0; + r.w = w; + r.h = h; + SDL_RenderSetViewport(sdl_renderer, &r); + printf("new viewport: %d / %d\n", w, h); +} +static int min(int a, int b) { + if(a < b) + return a; + return b; +} void process_SDLevents() { SDL_Event event; while(SDL_PollEvent(&event)) { @@ -266,19 +297,27 @@ void process_SDLevents() { break; case SDL_WINDOWEVENT: switch(event.window.event) { - case SDL_WINDOWEVENT_RESIZED: sdl_screenSurface = SDL_GetWindowSurface(sdl_window); break; + case SDL_WINDOWEVENT_RESIZED: { + sdl_windowwidth = event.window.data1; + sdl_windowheight = event.window.data2; + sdl_update_viewport(sdl_windowwidth, sdl_windowheight); + } + break; } break; case SDL_KEYDOWN: - // printf("type: %x, state: %x, repeat: %x, scancode: %x, sym: %x, mod: %x\n", event.key.type, event.key.state, event.key.repeat, event.key.keysym.scancode, event.key.keysym.sym, event.key.keysym.mod); + printf("dn ts: %x, type: %x, state: %x, repeat: %x, scancode: %x, sym: %x <%s>, mod: %x\n", event.key.timestamp, event.key.type, event.key.state, event.key.repeat, event.key.keysym.scancode, event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.mod); handle_keydown(event.key.keysym.sym, event.key.keysym.mod); break; case SDL_KEYUP: + printf("up ts: %x, type: %x, state: %x, repeat: %x, scancode: %x, sym: %x <%s>, mod: %x\n", event.key.timestamp, event.key.type, event.key.state, event.key.repeat, event.key.keysym.scancode, event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.mod); handle_keyup(event.key.keysym.sym, event.key.keysym.mod); break; case SDL_MOUSEMOTION: { int x, y; SDL_GetMouseState(&x, &y); + x /= sdl_pixelscale; + y /= sdl_pixelscale; *CLastUserActionCell68k = MiscStats->secondstmp; *EmCursorX68K = (*((DLword *)EmMouseX68K)) = (short)(x & 0xFFFF); @@ -318,19 +357,43 @@ void process_SDLevents() { if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; } break; - /* case SDL_KEYMAPCHANGED: */ - /* printf("SDL_KEYMAPCHANGED\n"); break; */ - /* case SDL_TEXTINPUT: */ - /* printf("SDL_TEXTINPUT\n"); break; */ + /* case SDL_KEYMAPCHANGED: */ + /* printf("SDL_KEYMAPCHANGED\n"); break; */ + /* case SDL_TEXTINPUT: */ + /* printf("SDL_TEXTINPUT\n"); break; */ default: printf("other event type: %d\n", event.type); } } - SDL_SetRenderDrawColor(sdl_renderer, 50, 50, 50, 255); + int before = SDL_GetTicks(); sdl_bitblt_to_screen(DisplayRegion68k); - SDL_UpdateWindowSurface(sdl_window); + int after = SDL_GetTicks(); + // printf("blitting took %dms\n", after - before); + SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); + SDL_RenderClear(sdl_renderer); + SDL_Rect r; + r.x = 0; + r.y = 0; + r.w = min(sdl_windowwidth / sdl_pixelscale, sdl_displaywidth); + r.h = min(sdl_windowheight / sdl_pixelscale, sdl_displayheight); + SDL_Rect s; + s.x = 0; + s.y = 0; + s.w = min(sdl_windowwidth / sdl_pixelscale * sdl_pixelscale, sdl_displaywidth * sdl_pixelscale); + s.h = min(sdl_windowheight / sdl_pixelscale * sdl_pixelscale, sdl_displayheight * sdl_pixelscale); + SDL_RenderCopy(sdl_renderer, sdl_texture, &r, &s); + SDL_RenderPresent(sdl_renderer); + SDL_PumpEvents(); } -int init_SDL() { +int init_SDL(int w, int h, int s) { + sdl_pixelscale = s; + // must be multiples of 32 + w = w / 32 * 32; + h = h / 32 * 32; + sdl_displaywidth = w; + sdl_displayheight = h; + sdl_windowwidth = w * s; + sdl_windowheight = h * s; int width = sdl_displaywidth; int height = sdl_displayheight; printf("requested width: %d, height: %d\n", width, height); @@ -339,15 +402,24 @@ int init_SDL() { return 1; } printf("initialised\n"); - sdl_window = SDL_CreateWindow("Maiko", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, 0); + sdl_window = SDL_CreateWindow("Maiko", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sdl_windowwidth, sdl_windowheight, 0); printf("Window created\n"); if(sdl_window == NULL) { printf("Window could not be created. SDL_Error: %s\n", SDL_GetError()); return 2; } - sdl_screenSurface = SDL_GetWindowSurface(sdl_window); - printf("surface: %dx%d, pitch %d\n", sdl_screenSurface->w, sdl_screenSurface->h, sdl_screenSurface->pitch); - printf(" format %d, bitspp %d, bytespp %d\n", sdl_screenSurface->format->format, sdl_screenSurface->format->BitsPerPixel, sdl_screenSurface->format->BytesPerPixel); + printf("Creating renderer...\n"); + sdl_renderer = SDL_CreateRenderer(sdl_window, -1, SDL_RENDERER_ACCELERATED); + if(NULL == sdl_renderer) { + printf("SDL Error: %s\n", SDL_GetError()); + return 3; + } + SDL_SetRenderDrawColor(sdl_renderer, 50, 50, 50, 255); + SDL_RenderSetScale(sdl_renderer, 1.0, 1.0); + printf("Creating texture...\n"); + sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height); + printf("-> %x\n", sdl_texture); + buffer = malloc(width * height * sizeof(Uint32)); printf("SDL initialised\n"); return 0; } From e39b650f4d026e5bfcef9ec1c45629e9fe5ed055 Mon Sep 17 00:00:00 2001 From: Peter Date: Wed, 20 Oct 2021 23:45:55 +0200 Subject: [PATCH 03/68] Only bitblt on damage. Also add an alternative for key handling, which does not work any better... --- inc/display.h | 4 +++ src/bbtsub.c | 19 +++++++++++++ src/initdsp.c | 20 +++++++++++--- src/sdl.c | 76 +++++++++++++++++++++++++++++++++++---------------- 4 files changed, 91 insertions(+), 28 deletions(-) diff --git a/inc/display.h b/inc/display.h index 5cee6ff..59c0509 100755 --- a/inc/display.h +++ b/inc/display.h @@ -43,6 +43,10 @@ extern DLword *DISP_MAX_Address; #define DISPLAYBUFFER #endif /* XWINDOW */ +#ifdef SDL +#define DISPLAYBUFFER +#endif /* SDL */ + #ifdef DOS #define DISPLAYBUFFER #endif /* DOS */ diff --git a/src/bbtsub.c b/src/bbtsub.c index f8c5d12..e32c901 100644 --- a/src/bbtsub.c +++ b/src/bbtsub.c @@ -470,6 +470,10 @@ do_it_now: if (in_display_segment(dstbase)) flush_display_region(dx, dty, w, h); #endif /* XWINDOW */ +#ifdef SDL + if (in_display_segment(dstbase)) flush_display_region(dx, dty, w, h); +#endif /* XWINDOW */ + #ifdef DOS /* Copy the changed section of display bank to the frame buffer */ if (in_display_segment(dstbase)) { @@ -823,6 +827,10 @@ do_it_now: if (in_display_segment(dstbase)) flush_display_region(dlx, dty, width, height); #endif /* XWINDOW */ +#ifdef SDL + if (in_display_segment(dstbase)) flush_display_region(dlx, dty, width, height); +#endif /* SDL */ + #ifdef DOS /* Copy the changed section of display bank to the frame buffer */ if (in_display_segment(dstbase)) { @@ -1070,6 +1078,10 @@ do_it_now: if (in_display_segment(dstbase)) flush_display_region(left, dty, width, height); #endif /* XWINDOW */ +#ifdef SDL + if (in_display_segment(dstbase)) flush_display_region(left, dty, width, height); +#endif /* SDL */ + #ifdef DOS /* Copy the changed section of display bank to the frame buffer */ if (in_display_segment(dstbase)) { @@ -1199,6 +1211,10 @@ void bltchar(LispPTR *args) if (in_display_segment(dstbase)) flush_display_lineregion(dx, dstbase, w, h); #endif /* XWINDOW */ +#ifdef SDL + if (in_display_segment(dstbase)) flush_display_lineregion(dx, dstbase, w, h); +#endif /* SDL */ + #ifdef DOS if (in_display_segment(dstbase)) flush_display_lineregion(dx, dstbase, w, h); #endif /* DOS */ @@ -1415,6 +1431,9 @@ void newbltchar(LispPTR *args) { #ifdef XWINDOW if (in_display_segment(dstbase)) flush_display_lineregion(dx, dstbase, w, h); #endif /* XWINDOW */ +#ifdef SDL + if (in_display_segment(dstbase)) flush_display_lineregion(dx, dstbase, w, h); +#endif /* SDL */ #ifdef DOS if (in_display_segment(dstbase)) flush_display_lineregion(dx, dstbase, w, h); #endif /* DOS */ diff --git a/src/initdsp.c b/src/initdsp.c index 017c16c..6a7b056 100644 --- a/src/initdsp.c +++ b/src/initdsp.c @@ -261,8 +261,11 @@ in_display_segment(baseaddr) /* */ /************************************************************************/ +<<<<<<< HEAD void flush_display_buffer(void) { - +#ifdef SDL + sdl_bitblt_to_screen(0, 0, sdl_displaywidth, sdl_displayheight); +#endif #ifdef XWINDOW (currentdsp->bitblt_to_screen)(currentdsp, DisplayRegion68k, currentdsp->Visible.x, currentdsp->Visible.y, currentdsp->Visible.width, @@ -290,7 +293,10 @@ void flush_display_buffer(void) { /************************************************************************/ void flush_display_region(int x, int y, int w, int h) { - + // printf("flush_display_region %d %d %d %d\n", x, y, w, h); +#ifdef SDL + sdl_bitblt_to_screen(x, y, w, h); +#endif #if (defined(XWINDOW) || defined(DOS)) TPRINT(("Enter flush_display_region x=%d, y=%d, w=%d, h=%d\n", x, y, w, h)); (currentdsp->bitblt_to_screen)(currentdsp, DisplayRegion68k, x, y, w, h); @@ -333,7 +339,10 @@ void flush_display_lineregion(UNSIGNED x, DLword *ybase, int w, int h) { int y; y = ((DLword *)ybase - DisplayRegion68k) / DLWORD_PERLINE; - + // printf("flush_display_lineregion %d %d %d %d\n", x, y, w, h); +#ifdef SDL + sdl_bitblt_to_screen(x, y, w, h); +#endif #if (defined(XWINDOW) || defined(DOS)) TPRINT(("Enter flush_display_lineregion x=%p, y=%d, w=%d, h=%d\n", (void *)x, y, w, h)); (currentdsp->bitblt_to_screen)(currentdsp, DisplayRegion68k, x, y, w, h); @@ -364,7 +373,10 @@ void flush_display_ptrregion(DLword *ybase, UNSIGNED bitoffset, int w, int h) baseoffset = (((DLword *)ybase) - DisplayRegion68k); y = baseoffset / DLWORD_PERLINE; x = bitoffset + (BITSPERWORD * (baseoffset - (DLWORD_PERLINE * y))); - + // printf("flush_display_ptrregion %d %d %d %d\n", x, y, w, h); +#ifdef SDL + sdl_bitblt_to_screen(x, y, w, h); +#endif #if (defined(XWINDOW) || defined(DOS)) TPRINT(("Enter flush_display_ptrregion\n x=%d, y=%d, w=%d, h=%d\n", x, y, w, h)); (currentdsp->bitblt_to_screen)(currentdsp, DisplayRegion68k, x, y, w, h); diff --git a/src/sdl.c b/src/sdl.c index a0198ec..9af3731 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -125,7 +125,7 @@ int sdl_windowheight = 0; // each pixel is shown as this many pixels int sdl_pixelscale = 0; -extern char *DisplayRegion68k; +extern Uint32 *DisplayRegion68k; extern DLword *EmKbdAd068K, *EmKbdAd168K, *EmKbdAd268K, *EmKbdAd368K, *EmKbdAd468K, *EmKbdAd568K, @@ -201,13 +201,14 @@ void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel) + x * surface->format->BytesPerPixel); *target_pixel = pixel; } -void sdl_bitblt_to_screen(uint32_t *source) { +void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { + // printf("bitblting\n"); int width = sdl_displaywidth; int height = sdl_displayheight; int bpw = 32; - for(int y = 0; y < height; y++) { - for(int x = 0; x < width / bpw; x++) { - int w = source[y*(sdl_displaywidth/bpw) + x]; + for(int y = _y; y < _y + _h; y++) { + for(int x = _x / bpw; x < (_x + _w + bpw - 1) / bpw; x++) { + int w = DisplayRegion68k[y*(sdl_displaywidth/bpw) + x]; for(int b = 0; b < bpw; b++) { //printf("%d/%d %d\n", x, y, b); int px = 0; @@ -222,6 +223,7 @@ void sdl_bitblt_to_screen(uint32_t *source) { } } } + SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); } int map_key(SDL_Keycode k) { for(int i = 0; keymap[i] != -1; i+= 2) { @@ -288,6 +290,28 @@ static int min(int a, int b) { return a; return b; } +static int last_draw = 0; +static int last_keystate[512] = { 0 }; +/* void handle_keyboard() { */ +/* SDL_PumpEvents(); */ +/* int numkeys; */ +/* const Uint8 *keystates = SDL_GetKeyboardState(&numkeys); */ +/* for(int i = 0; keymap[i] != -1; i+= 2) { */ +/* int keycode = keymap[i+1]; */ +/* int lk = keymap[i]; */ +/* int scancode = SDL_GetScancodeFromKey(keycode); */ +/* if(scancode < numkeys) { */ +/* int state = keystates[scancode]; */ +/* if(state != last_keystate[scancode]) { */ +/* printf("lk %d scancode %d %s <%s>\n", lk, scancode, state ? "pressed" : "released", SDL_GetKeyName(keycode)); */ +/* kb_trans(lk - KEYCODE_OFFSET, state ? FALSE : TRUE); */ +/* DoRing(); */ +/* if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; */ +/* } */ +/* last_keystate[scancode] = state; */ +/* } */ +/* } */ +/* } */ void process_SDLevents() { SDL_Event event; while(SDL_PollEvent(&event)) { @@ -365,25 +389,29 @@ void process_SDLevents() { printf("other event type: %d\n", event.type); } } - int before = SDL_GetTicks(); - sdl_bitblt_to_screen(DisplayRegion68k); - int after = SDL_GetTicks(); - // printf("blitting took %dms\n", after - before); - SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); - SDL_RenderClear(sdl_renderer); - SDL_Rect r; - r.x = 0; - r.y = 0; - r.w = min(sdl_windowwidth / sdl_pixelscale, sdl_displaywidth); - r.h = min(sdl_windowheight / sdl_pixelscale, sdl_displayheight); - SDL_Rect s; - s.x = 0; - s.y = 0; - s.w = min(sdl_windowwidth / sdl_pixelscale * sdl_pixelscale, sdl_displaywidth * sdl_pixelscale); - s.h = min(sdl_windowheight / sdl_pixelscale * sdl_pixelscale, sdl_displayheight * sdl_pixelscale); - SDL_RenderCopy(sdl_renderer, sdl_texture, &r, &s); - SDL_RenderPresent(sdl_renderer); - SDL_PumpEvents(); + // handle_keyboard(); + /* int before = SDL_GetTicks(); */ + /* sdl_bitblt_to_screen(); */ + /* int after = SDL_GetTicks(); */ + // printf("blitting took %dms\n", after - before); + // SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); + int this_draw = SDL_GetTicks(); + if(this_draw - last_draw > 16) { + SDL_RenderClear(sdl_renderer); + SDL_Rect r; + r.x = 0; + r.y = 0; + r.w = min(sdl_windowwidth / sdl_pixelscale, sdl_displaywidth); + r.h = min(sdl_windowheight / sdl_pixelscale, sdl_displayheight); + SDL_Rect s; + s.x = 0; + s.y = 0; + s.w = min(sdl_windowwidth / sdl_pixelscale * sdl_pixelscale, sdl_displaywidth * sdl_pixelscale); + s.h = min(sdl_windowheight / sdl_pixelscale * sdl_pixelscale, sdl_displayheight * sdl_pixelscale); + SDL_RenderCopy(sdl_renderer, sdl_texture, &r, &s); + SDL_RenderPresent(sdl_renderer); + last_draw = this_draw; + } } int init_SDL(int w, int h, int s) { sdl_pixelscale = s; From ffacda1278b03d3309cbfe5c6bb0b50ec9dd6022 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 08:25:09 +0200 Subject: [PATCH 04/68] Refactor, don't update texture on every bitblt. --- src/sdl.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 9af3731..eb05392 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -203,6 +203,7 @@ void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel) } void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { // printf("bitblting\n"); + int before = SDL_GetTicks(); int width = sdl_displaywidth; int height = sdl_displayheight; int bpw = 32; @@ -223,7 +224,12 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { } } } - SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); + int after = SDL_GetTicks(); + // printf("bitblting took %dms\n", after - before); + /* before = SDL_GetTicks(); */ + /* SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); */ + /* after = SDL_GetTicks(); */ + /* printf("UpdateTexture took %dms\n", after - before); */ } int map_key(SDL_Keycode k) { for(int i = 0; keymap[i] != -1; i+= 2) { @@ -312,7 +318,10 @@ static int last_keystate[512] = { 0 }; /* } */ /* } */ /* } */ +int process_events_time = 0; void process_SDLevents() { + // printf("processing events delta %dms\n", SDL_GetTicks() - process_events_time); + process_events_time = SDL_GetTicks(); SDL_Event event; while(SDL_PollEvent(&event)) { switch(event.type) { @@ -390,12 +399,15 @@ void process_SDLevents() { } } // handle_keyboard(); - /* int before = SDL_GetTicks(); */ /* sdl_bitblt_to_screen(); */ - /* int after = SDL_GetTicks(); */ - // printf("blitting took %dms\n", after - before); // SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); + int before = SDL_GetTicks(); + SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); + int after = SDL_GetTicks(); + // printf("UpdateTexture took %dms\n", after - before); int this_draw = SDL_GetTicks(); + before = SDL_GetTicks(); + // printf("processing events took %dms\n", before - process_events_time); if(this_draw - last_draw > 16) { SDL_RenderClear(sdl_renderer); SDL_Rect r; @@ -411,6 +423,8 @@ void process_SDLevents() { SDL_RenderCopy(sdl_renderer, sdl_texture, &r, &s); SDL_RenderPresent(sdl_renderer); last_draw = this_draw; + after = SDL_GetTicks(); + // printf("rendering took %dms\n", after - before); } } int init_SDL(int w, int h, int s) { From 948db16ed9b7b89848c5a8da60c63b24d3fc714c Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 08:48:33 +0200 Subject: [PATCH 05/68] Refactor, only bitblt once per "frame". --- src/initdsp.c | 8 ++++---- src/main.c | 6 ++++-- src/sdl.c | 32 +++++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/initdsp.c b/src/initdsp.c index 6a7b056..a98f771 100644 --- a/src/initdsp.c +++ b/src/initdsp.c @@ -264,7 +264,7 @@ in_display_segment(baseaddr) <<<<<<< HEAD void flush_display_buffer(void) { #ifdef SDL - sdl_bitblt_to_screen(0, 0, sdl_displaywidth, sdl_displayheight); + // sdl_bitblt_to_screen(0, 0, sdl_displaywidth, sdl_displayheight); #endif #ifdef XWINDOW (currentdsp->bitblt_to_screen)(currentdsp, DisplayRegion68k, currentdsp->Visible.x, @@ -295,7 +295,7 @@ void flush_display_region(int x, int y, int w, int h) { // printf("flush_display_region %d %d %d %d\n", x, y, w, h); #ifdef SDL - sdl_bitblt_to_screen(x, y, w, h); + // sdl_bitblt_to_screen(x, y, w, h); #endif #if (defined(XWINDOW) || defined(DOS)) TPRINT(("Enter flush_display_region x=%d, y=%d, w=%d, h=%d\n", x, y, w, h)); @@ -341,7 +341,7 @@ void flush_display_lineregion(UNSIGNED x, DLword *ybase, int w, int h) y = ((DLword *)ybase - DisplayRegion68k) / DLWORD_PERLINE; // printf("flush_display_lineregion %d %d %d %d\n", x, y, w, h); #ifdef SDL - sdl_bitblt_to_screen(x, y, w, h); + // sdl_bitblt_to_screen(x, y, w, h); #endif #if (defined(XWINDOW) || defined(DOS)) TPRINT(("Enter flush_display_lineregion x=%p, y=%d, w=%d, h=%d\n", (void *)x, y, w, h)); @@ -375,7 +375,7 @@ void flush_display_ptrregion(DLword *ybase, UNSIGNED bitoffset, int w, int h) x = bitoffset + (BITSPERWORD * (baseoffset - (DLWORD_PERLINE * y))); // printf("flush_display_ptrregion %d %d %d %d\n", x, y, w, h); #ifdef SDL - sdl_bitblt_to_screen(x, y, w, h); + // sdl_bitblt_to_screen(x, y, w, h); #endif #if (defined(XWINDOW) || defined(DOS)) TPRINT(("Enter flush_display_ptrregion\n x=%d, y=%d, w=%d, h=%d\n", x, y, w, h)); diff --git a/src/main.c b/src/main.c index d7acc81..89afeb4 100644 --- a/src/main.c +++ b/src/main.c @@ -238,7 +238,9 @@ int flushing = FALSE; /* see dbprint.h if set, all debug/trace printing will cal #include "devif.h" extern DspInterface currentdsp; #endif /* DOS || XWINDOW */ - +#ifdef SDL +extern int init_SDL(int, int, int); +#endif extern const time_t MDate; extern int nokbdflag; extern int nomouseflag; @@ -599,7 +601,7 @@ int main(int argc, char *argv[]) make_dsp_instance(currentdsp, 0, 0, 0, 1); /* All defaults the first time */ #endif /* DOS || XWINDOW */ #if defined(SDL) - init_SDL(1600, 1024, 1); + init_SDL(1888, 1110, 1); #endif /* SDL */ /* Load sysout to VM space and returns real sysout_size(not 0) */ sysout_size = sysout_loader(sysout_name, sysout_size); diff --git a/src/sdl.c b/src/sdl.c index eb05392..29ea2b5 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -201,15 +201,21 @@ void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel) + x * surface->format->BytesPerPixel); *target_pixel = pixel; } +int should_update_texture = 0; + void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { // printf("bitblting\n"); int before = SDL_GetTicks(); int width = sdl_displaywidth; int height = sdl_displayheight; int bpw = 32; - for(int y = _y; y < _y + _h; y++) { - for(int x = _x / bpw; x < (_x + _w + bpw - 1) / bpw; x++) { - int w = DisplayRegion68k[y*(sdl_displaywidth/bpw) + x]; + int pitch = sdl_displaywidth / bpw; + int xlimit = (_x + _w + bpw - 1) / bpw; + int ylimit = _y + _h; + for(int y = _y; y < ylimit; y++) { + for(int x = _x / bpw; x < xlimit; x++) { + int w = DisplayRegion68k[y * pitch + x]; + int thex = x * bpw; for(int b = 0; b < bpw; b++) { //printf("%d/%d %d\n", x, y, b); int px = 0; @@ -219,11 +225,12 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { px = 0xffffffff; } //printf("px is %x\n", px); - int xx = x * bpw + b; + int xx = thex + b; buffer[y * sdl_displaywidth + xx] = px; } } } + should_update_texture = 1; int after = SDL_GetTicks(); // printf("bitblting took %dms\n", after - before); /* before = SDL_GetTicks(); */ @@ -401,14 +408,25 @@ void process_SDLevents() { // handle_keyboard(); /* sdl_bitblt_to_screen(); */ // SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); - int before = SDL_GetTicks(); - SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); - int after = SDL_GetTicks(); + int before = 0; + int after = 0; + /* if(should_update_texture) { */ + /* before = SDL_GetTicks(); */ + /* SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); */ + /* after = SDL_GetTicks(); */ + /* should_update_texture = 0; */ + /* } */ // printf("UpdateTexture took %dms\n", after - before); int this_draw = SDL_GetTicks(); before = SDL_GetTicks(); // printf("processing events took %dms\n", before - process_events_time); if(this_draw - last_draw > 16) { + before = SDL_GetTicks(); + SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); + after = SDL_GetTicks(); + should_update_texture = 0; + + sdl_bitblt_to_screen(0, 0, sdl_displaywidth, sdl_displayheight); SDL_RenderClear(sdl_renderer); SDL_Rect r; r.x = 0; From 3b6b203e6c3318a73b7790eea7e8d0a3675089a8 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 08:50:06 +0200 Subject: [PATCH 06/68] Remove extra directory from includes for SDL. --- src/sdl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 29ea2b5..90e513e 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include "lispemul.h" #include "miscstat.h" #include "keyboard.h" From 085e99a7c07dac8bbd3baff75a65c8e06e755300 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 09:59:59 +0200 Subject: [PATCH 07/68] Add more #ifdef XWINDOW --- src/keyevent.c | 2 ++ src/xc.c | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/keyevent.c b/src/keyevent.c index b57e2ee..8386687 100644 --- a/src/keyevent.c +++ b/src/keyevent.c @@ -55,7 +55,9 @@ void Mouse_hndlr(void); /* Fields mouse events from driver */ #include "keyeventdefs.h" #include "osmsgdefs.h" +#ifdef XWINDOW #include "xwinmandefs.h" +#endif #if defined(MAIKO_ENABLE_ETHERNET) || defined(MAIKO_ENABLE_NETHUB) #include "etherdefs.h" diff --git a/src/xc.c b/src/xc.c index 02e9031..3b820c4 100644 --- a/src/xc.c +++ b/src/xc.c @@ -95,7 +95,9 @@ #include "ubf3defs.h" #include "unwinddefs.h" #include "vars3defs.h" +#ifdef XWINDOW #include "xwinmandefs.h" +#endif #include "z2defs.h" #ifdef DOS @@ -103,7 +105,7 @@ extern KbdInterface currentkbd; extern DspInterface currentdsp; extern MouseInterface currentmouse; -#else +#elif defined(XWINDOW) extern DspInterface currentdsp; #endif /* DOS */ From 8038a959c502abf6f3702ea56e5584bb803091c0 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 13:58:55 +0200 Subject: [PATCH 08/68] Update CMakeLists.txt for cross-compiling. --- CMakeLists.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8729a39..8004db4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,10 +23,12 @@ find_program( ) IF (CLANG_TIDY_EXE) + IF (NOT CMAKE_CROSSCOMPILING) # There are many many warnings for strcpy instances to deal with, # but suppress it for now so that other issues are more obvious # - SET(CMAKE_C_CLANG_TIDY ${CLANG_TIDY_EXE} -checks=-*,cert-*,clang-analyzer-security.*,-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-security.insecureAPI.bzero -header-filter=.*) + SET(CMAKE_C_CLANG_TIDY ${CLANG_TIDY_EXE} -checks=-*,cert-*,clang-analyzer-security.*,-clang-analyzer-security.insecureAPI.strcpy,-clang-analyzer-security.insecureAPI.bzero -header-filter=.*) + ENDIF() ENDIF() INCLUDE(CheckLibraryExists) @@ -98,7 +100,7 @@ IF(MAIKO_DISPLAY_SDL) SET(MAIKO_DISPLAY_SDL_DEFINITIONS "-DSDL" ) - SET(MAIKO_DISPLAY_SDL_LIBRARIES SDL2::SDL2) + SET(MAIKO_DISPLAY_SDL_LIBRARIES SDL2) SET(MAIKO_DISPLAY_SDL_SRCS src/sdl.c ) @@ -430,7 +432,7 @@ IF(MAIKO_DISPLAY_SDL) # Tell it that the SDL launcher is available. TARGET_COMPILE_DEFINITIONS(lde PUBLIC ${MAIKO_DISPLAY_SDL_DEFINITIONS}) # This is needed so that it can call XOpenDisplay. - TARGET_LINK_LIBRARIES(lde SDL2::SDL2) + TARGET_LINK_LIBRARIES(lde SDL2) ENDIF() ADD_EXECUTABLE(ldeether src/ldeether.c src/dlpi.c) From b77f64f31e3b788d8e98b371711d6cc5ee538862 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 18:18:28 +0200 Subject: [PATCH 09/68] Add command line parameters -sc WxH and -pixelscale n for SDL. --- src/main.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 89afeb4..5556382 100644 --- a/src/main.c +++ b/src/main.c @@ -276,7 +276,16 @@ const char *helpstring = -bw The Medley screen borderwidth\n\ -g[eometry] ] The Medley screen geometry\n\ -sc[reen] x] The Medley screen geometry\n"; -#else /* not DOS, not XWINDOW */ +#elif SDL +const char *helpstring = + "\n\ + either setenv LDESRCESYSOUT or do:\n\ + medley [] []\n\ + -info Print general info about the system\n\ + -help Print this message\n\ + -pixelscale The amount of pixels to show for one Medley screen pixel.\n\ + -sc[reen] x] The Medley screen geometry\n"; +#else /* not DOS, not XWINDOW, not SDL */ const char *helpstring = "\n\ either setenv LDESRCESYSOUT or do:\n\ @@ -315,6 +324,9 @@ int main(int argc, char *argv[]) extern int TIMER_INTERVAL; extern fd_set LispReadFds; long tmpint; + int width = 1024, height = 768; + int pixelscale = 1; + #ifdef MAIKO_ENABLE_FOREIGN_FUNCTION_INTERFACE if (dld_find_executable(argv[0]) == 0) { perror("Name of executable not found."); @@ -442,7 +454,31 @@ int main(int argc, char *argv[]) } #endif /* DOS */ - +#ifdef SDL + else if ((strcmp(argv[i], "-sc") == 0) || (strcmp(argv[i], "-SC") == 0)) { + if (argc > ++i) { + int read = sscanf(argv[i], "%dx%d", &width, &height); + if(read != 2) { + fprintf(stderr, "Could not parse -sc argument %s\n", argv[i]); + exit(1); + } + } else { + fprintf(stderr, "Missing argument after -m\n"); + exit(1); + } + } else if ((strcmp(argv[i], "-pixelscale") == 0) || (strcmp(argv[i], "-PIXELSCALE") == 0)) { + if (argc > ++i) { + int read = sscanf(argv[i], "%d", &pixelscale); + if(read != 1) { + fprintf(stderr, "Could not parse -pixelscale argument %s\n", argv[i]); + exit(1); + } + } else { + fprintf(stderr, "Missing argument after -m\n"); + exit(1); + } + } +#endif /* SDL */ /* Can only do this under SUNOs, for now */ else if (!strcmp(argv[i], "-E")) { /**** ethernet info ****/ #ifdef MAIKO_ENABLE_ETHERNET @@ -601,7 +637,7 @@ int main(int argc, char *argv[]) make_dsp_instance(currentdsp, 0, 0, 0, 1); /* All defaults the first time */ #endif /* DOS || XWINDOW */ #if defined(SDL) - init_SDL(1888, 1110, 1); + init_SDL(width, height, pixelscale); #endif /* SDL */ /* Load sysout to VM space and returns real sysout_size(not 0) */ sysout_size = sysout_loader(sysout_name, sysout_size); From 8c34ca31061e3df28315383a3aee5d843f9f030d Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 18:22:01 +0200 Subject: [PATCH 10/68] Try to get CI running again ;) --- src/ldeboot.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ldeboot.c b/src/ldeboot.c index 780d336..2f870cc 100644 --- a/src/ldeboot.c +++ b/src/ldeboot.c @@ -142,7 +142,9 @@ int main(int argc, char *argv[]) } /* end if */ } #endif /* XWINDOW */ - +#ifdef SDL + strcpy(filetorun,LDESDL); +#endif /* SDL */ #ifdef USESUNSCREEN if ((FrameBufferFd = open("/dev/fb", O_RDWR)) < 0) { fprintf(stderr, "ldeboot: can't open FrameBuffer\n"); @@ -196,7 +198,6 @@ int main(int argc, char *argv[]) fork_Unix(); /* start ldemono or ldecolor */ - strcpy(filetorun,LDESDL); if (filetorun[0] == '\0') { fprintf(stderr, "Unable to determine what display program to run.\n"); exit(1); From e2e6aa536657b393c7b99061da4acf36742c9173 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 18:28:23 +0200 Subject: [PATCH 11/68] Also install SDL2 on ubuntu runners. --- .github/workflows/build.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bae50f2..b65eb76 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -45,6 +45,9 @@ jobs: - name: Install X11 dependencies on MacOS if: ${{ runner.os == 'macOS'}} run: brew install --cask xquartz + - name: Install SDL dependency on Ubuntu + if: ${{ runner.os == 'Linux'}} + run: apt install libsdl2-dev - name: Create Build Environment run: cmake -E make_directory ${{github.workspace}}/build - name: Configure CMake From 79903e311ee8681e3d7bcc638236cec5200a1da3 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 18:29:41 +0200 Subject: [PATCH 12/68] Also *correctly* install SDL2 on ubuntu runners. --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b65eb76..6ec5cd5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,7 +47,7 @@ jobs: run: brew install --cask xquartz - name: Install SDL dependency on Ubuntu if: ${{ runner.os == 'Linux'}} - run: apt install libsdl2-dev + run: sudo apt-get install -y libsdl2-dev - name: Create Build Environment run: cmake -E make_directory ${{github.workspace}}/build - name: Configure CMake From 0187073ae020a30baf00423bf8e06c16346833e7 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 18:31:54 +0200 Subject: [PATCH 13/68] Revert CMakeLists.txt change until better understanding. --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8004db4..d211b42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,7 +100,7 @@ IF(MAIKO_DISPLAY_SDL) SET(MAIKO_DISPLAY_SDL_DEFINITIONS "-DSDL" ) - SET(MAIKO_DISPLAY_SDL_LIBRARIES SDL2) + SET(MAIKO_DISPLAY_SDL_LIBRARIES SDL2::SDL2) SET(MAIKO_DISPLAY_SDL_SRCS src/sdl.c ) @@ -432,7 +432,7 @@ IF(MAIKO_DISPLAY_SDL) # Tell it that the SDL launcher is available. TARGET_COMPILE_DEFINITIONS(lde PUBLIC ${MAIKO_DISPLAY_SDL_DEFINITIONS}) # This is needed so that it can call XOpenDisplay. - TARGET_LINK_LIBRARIES(lde SDL2) + TARGET_LINK_LIBRARIES(lde SDL2::SDL2) ENDIF() ADD_EXECUTABLE(ldeether src/ldeether.c src/dlpi.c) From 6604b3dd06429b5562b9f3ca9fe90f12ef3b9908 Mon Sep 17 00:00:00 2001 From: Peter Date: Thu, 21 Oct 2021 18:44:31 +0200 Subject: [PATCH 14/68] Add support for -t / -title. --- src/main.c | 22 +++++++++++++++++----- src/sdl.c | 4 ++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main.c b/src/main.c index 5556382..4d23107 100644 --- a/src/main.c +++ b/src/main.c @@ -239,7 +239,7 @@ int flushing = FALSE; /* see dbprint.h if set, all debug/trace printing will cal extern DspInterface currentdsp; #endif /* DOS || XWINDOW */ #ifdef SDL -extern int init_SDL(int, int, int); +extern int init_SDL(char*, int, int, int); #endif extern const time_t MDate; extern int nokbdflag; @@ -284,7 +284,9 @@ const char *helpstring = -info Print general info about the system\n\ -help Print this message\n\ -pixelscale The amount of pixels to show for one Medley screen pixel.\n\ - -sc[reen] x] The Medley screen geometry\n"; + -sc[reen] x] The Medley screen geometry\n\ + -t The window title\n\ + -title <title> The window title\n"; #else /* not DOS, not XWINDOW, not SDL */ const char *helpstring = "\n\ @@ -326,6 +328,7 @@ int main(int argc, char *argv[]) long tmpint; int width = 1024, height = 768; int pixelscale = 1; + char *windowtitle = "Medley"; #ifdef MAIKO_ENABLE_FOREIGN_FUNCTION_INTERFACE if (dld_find_executable(argv[0]) == 0) { @@ -463,7 +466,7 @@ int main(int argc, char *argv[]) exit(1); } } else { - fprintf(stderr, "Missing argument after -m\n"); + fprintf(stderr, "Missing argument after -sc\n"); exit(1); } } else if ((strcmp(argv[i], "-pixelscale") == 0) || (strcmp(argv[i], "-PIXELSCALE") == 0)) { @@ -474,10 +477,19 @@ int main(int argc, char *argv[]) exit(1); } } else { - fprintf(stderr, "Missing argument after -m\n"); + fprintf(stderr, "Missing argument after -pixelscale\n"); + exit(1); + } + } else if ((strcmp(argv[i], "-t") == 0) || (strcmp(argv[i], "-T") == 0) + || (strcmp(argv[i], "-title") == 0) || (strcmp(argv[i], "-TITLE") == 0)) { + if (argc > ++i) { + windowtitle = argv[i]; + } else { + fprintf(stderr, "Missing argument after -title\n"); exit(1); } } + #endif /* SDL */ /* Can only do this under SUNOs, for now */ else if (!strcmp(argv[i], "-E")) { /**** ethernet info ****/ @@ -637,7 +649,7 @@ int main(int argc, char *argv[]) make_dsp_instance(currentdsp, 0, 0, 0, 1); /* All defaults the first time */ #endif /* DOS || XWINDOW */ #if defined(SDL) - init_SDL(width, height, pixelscale); + init_SDL(windowtitle, width, height, pixelscale); #endif /* SDL */ /* Load sysout to VM space and returns real sysout_size(not 0) */ sysout_size = sysout_loader(sysout_name, sysout_size); diff --git a/src/sdl.c b/src/sdl.c index 90e513e..bbff60b 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -445,7 +445,7 @@ void process_SDLevents() { // printf("rendering took %dms\n", after - before); } } -int init_SDL(int w, int h, int s) { +int init_SDL(char *windowtitle, int w, int h, int s) { sdl_pixelscale = s; // must be multiples of 32 w = w / 32 * 32; @@ -462,7 +462,7 @@ int init_SDL(int w, int h, int s) { return 1; } printf("initialised\n"); - sdl_window = SDL_CreateWindow("Maiko", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sdl_windowwidth, sdl_windowheight, 0); + sdl_window = SDL_CreateWindow(windowtitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sdl_windowwidth, sdl_windowheight, 0); printf("Window created\n"); if(sdl_window == NULL) { printf("Window could not be created. SDL_Error: %s\n", SDL_GetError()); From 5e28babfa5a9e238d78787abc427b92f1cdb27b1 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 18:45:39 +0200 Subject: [PATCH 15/68] Run apt-get update before trying to install. --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6ec5cd5..17b1ed6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,7 +47,7 @@ jobs: run: brew install --cask xquartz - name: Install SDL dependency on Ubuntu if: ${{ runner.os == 'Linux'}} - run: sudo apt-get install -y libsdl2-dev + run: sudo apt-get update && sudo apt-get install -y libsdl2-dev - name: Create Build Environment run: cmake -E make_directory ${{github.workspace}}/build - name: Configure CMake From 40b24ec65551a73b6a3fd0c053f3a2805cdf72b8 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 18:51:41 +0200 Subject: [PATCH 16/68] Try to make builds pass again. --- CMakeLists.txt | 4 ++-- src/sdl.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d211b42..8004db4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,7 +100,7 @@ IF(MAIKO_DISPLAY_SDL) SET(MAIKO_DISPLAY_SDL_DEFINITIONS "-DSDL" ) - SET(MAIKO_DISPLAY_SDL_LIBRARIES SDL2::SDL2) + SET(MAIKO_DISPLAY_SDL_LIBRARIES SDL2) SET(MAIKO_DISPLAY_SDL_SRCS src/sdl.c ) @@ -432,7 +432,7 @@ IF(MAIKO_DISPLAY_SDL) # Tell it that the SDL launcher is available. TARGET_COMPILE_DEFINITIONS(lde PUBLIC ${MAIKO_DISPLAY_SDL_DEFINITIONS}) # This is needed so that it can call XOpenDisplay. - TARGET_LINK_LIBRARIES(lde SDL2::SDL2) + TARGET_LINK_LIBRARIES(lde SDL2) ENDIF() ADD_EXECUTABLE(ldeether src/ldeether.c src/dlpi.c) diff --git a/src/sdl.c b/src/sdl.c index bbff60b..e9a6f20 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -1,5 +1,5 @@ -#include <SDL.h> -#include <SDL_keycode.h> +#include <SDL2/SDL.h> +#include <SDL2/SDL_keycode.h> #include "lispemul.h" #include "miscstat.h" #include "keyboard.h" From 81e17c09559ab60be15eea48131213c77634230a Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 19:13:46 +0200 Subject: [PATCH 17/68] Show cmake version before building. --- .github/workflows/build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 17b1ed6..3b8f435 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -42,6 +42,8 @@ jobs: BUILD_TYPE: Release steps: - uses: actions/checkout@v3 + - name: Show CMake version + run: cmake --version - name: Install X11 dependencies on MacOS if: ${{ runner.os == 'macOS'}} run: brew install --cask xquartz From d046d260034fd2ba64b47551b9cef1db056dff61 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 19:17:59 +0200 Subject: [PATCH 18/68] Add SDL2 dependency to macos build. --- .github/workflows/build.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3b8f435..a89425e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,6 +47,9 @@ jobs: - name: Install X11 dependencies on MacOS if: ${{ runner.os == 'macOS'}} run: brew install --cask xquartz + - name: Install SDL2 dependencies on MacOS + if: ${{ runner.os == 'macOS'}} + run: brew install --cask sdl2 - name: Install SDL dependency on Ubuntu if: ${{ runner.os == 'Linux'}} run: sudo apt-get update && sudo apt-get install -y libsdl2-dev From 71d2d0a5b08504cf1ade069bbcc1fae38085021e Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 19:20:13 +0200 Subject: [PATCH 19/68] Apparently there is no sdl2 cask for macos :-/ --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a89425e..5c24d78 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -49,7 +49,7 @@ jobs: run: brew install --cask xquartz - name: Install SDL2 dependencies on MacOS if: ${{ runner.os == 'macOS'}} - run: brew install --cask sdl2 + run: brew install sdl2 - name: Install SDL dependency on Ubuntu if: ${{ runner.os == 'Linux'}} run: sudo apt-get update && sudo apt-get install -y libsdl2-dev From 292e5dd9b2dd67f8035c75b83bfc51786b88c9cb Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 19:24:32 +0200 Subject: [PATCH 20/68] Try to placate the macos build. --- src/xc.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/xc.c b/src/xc.c index 3b820c4..5140042 100644 --- a/src/xc.c +++ b/src/xc.c @@ -109,6 +109,10 @@ extern MouseInterface currentmouse; extern DspInterface currentdsp; #endif /* DOS */ +#ifdef SDL +extern void process_SDLevents(); +#endif + typedef struct conspage ConsPage; typedef ByteCode *InstPtr; From 74d47b07ca0cc65cf421d2960cc75740ca68fc33 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 19:31:42 +0200 Subject: [PATCH 21/68] Try SDL2::SDL2 instead of SDL2 in CMakeLists.txt. --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8004db4..d211b42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,7 +100,7 @@ IF(MAIKO_DISPLAY_SDL) SET(MAIKO_DISPLAY_SDL_DEFINITIONS "-DSDL" ) - SET(MAIKO_DISPLAY_SDL_LIBRARIES SDL2) + SET(MAIKO_DISPLAY_SDL_LIBRARIES SDL2::SDL2) SET(MAIKO_DISPLAY_SDL_SRCS src/sdl.c ) @@ -432,7 +432,7 @@ IF(MAIKO_DISPLAY_SDL) # Tell it that the SDL launcher is available. TARGET_COMPILE_DEFINITIONS(lde PUBLIC ${MAIKO_DISPLAY_SDL_DEFINITIONS}) # This is needed so that it can call XOpenDisplay. - TARGET_LINK_LIBRARIES(lde SDL2) + TARGET_LINK_LIBRARIES(lde SDL2::SDL2) ENDIF() ADD_EXECUTABLE(ldeether src/ldeether.c src/dlpi.c) From 2f633f2f500b4f2697a85e843c06186875d8dbb2 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 19:42:28 +0200 Subject: [PATCH 22/68] Try to unify cmake file for different versions of SDL2. --- CMakeLists.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d211b42..494dcfd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,10 +97,15 @@ ELSEIF(NOT MAIKO_NETWORK_TYPE STREQUAL "NONE") OPTION(MAIKO_DISPLAY_SDL "Use SDL for display." ON) IF(MAIKO_DISPLAY_SDL) FIND_PACKAGE(SDL2 REQUIRED) + if(TARGET SDL2::SDL2) + set(SDLNAME SDL2::SDL2) + elseif(target SDL) + set(SDLNAME SDL2) + endif() SET(MAIKO_DISPLAY_SDL_DEFINITIONS "-DSDL" ) - SET(MAIKO_DISPLAY_SDL_LIBRARIES SDL2::SDL2) + SET(MAIKO_DISPLAY_SDL_LIBRARIES ${SDLNAME}) SET(MAIKO_DISPLAY_SDL_SRCS src/sdl.c ) @@ -432,7 +437,7 @@ IF(MAIKO_DISPLAY_SDL) # Tell it that the SDL launcher is available. TARGET_COMPILE_DEFINITIONS(lde PUBLIC ${MAIKO_DISPLAY_SDL_DEFINITIONS}) # This is needed so that it can call XOpenDisplay. - TARGET_LINK_LIBRARIES(lde SDL2::SDL2) + TARGET_LINK_LIBRARIES(lde ${SDLNAME}) ENDIF() ADD_EXECUTABLE(ldeether src/ldeether.c src/dlpi.c) From af6c6e3e1dd934f476bb4459692a020cc1c8dd35 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 19:44:11 +0200 Subject: [PATCH 23/68] Fix Caso. --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 494dcfd..14a988d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,7 +99,7 @@ IF(MAIKO_DISPLAY_SDL) FIND_PACKAGE(SDL2 REQUIRED) if(TARGET SDL2::SDL2) set(SDLNAME SDL2::SDL2) - elseif(target SDL) + elseif(TARGET SDL) set(SDLNAME SDL2) endif() SET(MAIKO_DISPLAY_SDL_DEFINITIONS From 13d367e7664c156b58c43be02006dd39fb5efb6f Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 20:12:07 +0200 Subject: [PATCH 24/68] Remove SDL2 directory from include SDL2/SDL.h. --- src/sdl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index e9a6f20..bbff60b 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -1,5 +1,5 @@ -#include <SDL2/SDL.h> -#include <SDL2/SDL_keycode.h> +#include <SDL.h> +#include <SDL_keycode.h> #include "lispemul.h" #include "miscstat.h" #include "keyboard.h" From 083d153d43ad620970cdaac247952de96591fe3d Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 21:49:30 +0200 Subject: [PATCH 25/68] Maybe fix cmake for SDL2. --- CMakeLists.txt | 10 ++-------- src/sdl.c | 5 +++++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 14a988d..c0d81a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -97,15 +97,10 @@ ELSEIF(NOT MAIKO_NETWORK_TYPE STREQUAL "NONE") OPTION(MAIKO_DISPLAY_SDL "Use SDL for display." ON) IF(MAIKO_DISPLAY_SDL) FIND_PACKAGE(SDL2 REQUIRED) - if(TARGET SDL2::SDL2) - set(SDLNAME SDL2::SDL2) - elseif(TARGET SDL) - set(SDLNAME SDL2) - endif() SET(MAIKO_DISPLAY_SDL_DEFINITIONS "-DSDL" ) - SET(MAIKO_DISPLAY_SDL_LIBRARIES ${SDLNAME}) + SET(MAIKO_DISPLAY_SDL_LIBRARIES ${SDL2_LIBRARIES}) SET(MAIKO_DISPLAY_SDL_SRCS src/sdl.c ) @@ -436,8 +431,6 @@ ENDIF() IF(MAIKO_DISPLAY_SDL) # Tell it that the SDL launcher is available. TARGET_COMPILE_DEFINITIONS(lde PUBLIC ${MAIKO_DISPLAY_SDL_DEFINITIONS}) - # This is needed so that it can call XOpenDisplay. - TARGET_LINK_LIBRARIES(lde ${SDLNAME}) ENDIF() ADD_EXECUTABLE(ldeether src/ldeether.c src/dlpi.c) @@ -471,6 +464,7 @@ IF(MAIKO_DISPLAY_SDL) ) TARGET_COMPILE_DEFINITIONS(ldesdl PUBLIC ${MAIKO_DEFINITIONS} ${MAIKO_DISPLAY_SDL_DEFINITIONS}) TARGET_INCLUDE_DIRECTORIES(ldesdl PUBLIC inc) + TARGET_INCLUDE_DIRECTORIES(ldesdl PRIVATE ${SDL2_INCLUDE_DIRS}) TARGET_LINK_LIBRARIES(ldesdl ${MAIKO_LIBRARIES} ${MAIKO_DISPLAY_SDL_LIBRARIES}) ENDIF() diff --git a/src/sdl.c b/src/sdl.c index bbff60b..89c87ac 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -347,6 +347,11 @@ void process_SDLevents() { break; case SDL_KEYDOWN: printf("dn ts: %x, type: %x, state: %x, repeat: %x, scancode: %x, sym: %x <%s>, mod: %x\n", event.key.timestamp, event.key.type, event.key.state, event.key.repeat, event.key.keysym.scancode, event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.mod); + // TODO: this produces *double* keys for me, need to fix that before using it + /* if (event.key.repeat) { */ + /* /\* Lisp needs to see the UP transition before the DOWN transition *\/ */ + /* handle_keyup(event.key.keysym.sym, event.key.keysym.mod); */ + /* } */ handle_keydown(event.key.keysym.sym, event.key.keysym.mod); break; case SDL_KEYUP: From 7573151be8a31dbe76207118ca4cac89ecebca90 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 21:55:52 +0200 Subject: [PATCH 26/68] For now, some more extern functions declarations. --- src/sdl.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/sdl.c b/src/sdl.c index 89c87ac..356add2 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -3,11 +3,15 @@ #include "lispemul.h" #include "miscstat.h" #include "keyboard.h" + static SDL_Window *sdl_window = NULL; static SDL_Renderer *sdl_renderer = NULL; static SDL_Texture *sdl_texture = NULL; static Uint32 *buffer = NULL; +extern void kb_trans(u_short keycode, u_short upflg); +extern int error(const char *s); + extern int KBDEventFlg; int keymap[] = { 0, SDLK_5, From 6f0ca7ad1c524833c28a2e5f5895c1cc8e1ac088 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Thu, 21 Oct 2021 23:12:39 +0200 Subject: [PATCH 27/68] Add support for key repeating. --- src/sdl.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 356add2..6ef8103 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -351,11 +351,10 @@ void process_SDLevents() { break; case SDL_KEYDOWN: printf("dn ts: %x, type: %x, state: %x, repeat: %x, scancode: %x, sym: %x <%s>, mod: %x\n", event.key.timestamp, event.key.type, event.key.state, event.key.repeat, event.key.keysym.scancode, event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.mod); - // TODO: this produces *double* keys for me, need to fix that before using it - /* if (event.key.repeat) { */ - /* /\* Lisp needs to see the UP transition before the DOWN transition *\/ */ - /* handle_keyup(event.key.keysym.sym, event.key.keysym.mod); */ - /* } */ + if (event.key.repeat) { + /* Lisp needs to see the UP transition before the DOWN transition */ + handle_keyup(event.key.keysym.sym, event.key.keysym.mod); + } handle_keydown(event.key.keysym.sym, event.key.keysym.mod); break; case SDL_KEYUP: From 380416fa2dce64b8c32e9dfb2614ded99a99ebf2 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Fri, 22 Oct 2021 07:34:03 +0200 Subject: [PATCH 28/68] Only update texture on damage. --- src/initdsp.c | 12 ++++++++---- src/sdl.c | 30 ++++++++++++++++-------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/initdsp.c b/src/initdsp.c index a98f771..285450e 100644 --- a/src/initdsp.c +++ b/src/initdsp.c @@ -80,6 +80,10 @@ extern DLword *ColorDisplayRegion68k; extern int MonoOrColor; #endif /* COLOR */ +#ifdef SDL +extern void sdl_notify_damage(int, int, int, int); +#endif /* SDL */ + #ifdef XWINDOW DLword *DisplayRegion68k_end_addr; extern int *Xdisplay; /* DAANGER -jarl nilsson 27-apr-92 */ @@ -264,7 +268,7 @@ in_display_segment(baseaddr) <<<<<<< HEAD void flush_display_buffer(void) { #ifdef SDL - // sdl_bitblt_to_screen(0, 0, sdl_displaywidth, sdl_displayheight); + sdl_notify_damage(0, 0, sdl_displaywidth, sdl_displayheight); #endif #ifdef XWINDOW (currentdsp->bitblt_to_screen)(currentdsp, DisplayRegion68k, currentdsp->Visible.x, @@ -295,7 +299,7 @@ void flush_display_region(int x, int y, int w, int h) { // printf("flush_display_region %d %d %d %d\n", x, y, w, h); #ifdef SDL - // sdl_bitblt_to_screen(x, y, w, h); + sdl_notify_damage(x, y, w, h); #endif #if (defined(XWINDOW) || defined(DOS)) TPRINT(("Enter flush_display_region x=%d, y=%d, w=%d, h=%d\n", x, y, w, h)); @@ -341,7 +345,7 @@ void flush_display_lineregion(UNSIGNED x, DLword *ybase, int w, int h) y = ((DLword *)ybase - DisplayRegion68k) / DLWORD_PERLINE; // printf("flush_display_lineregion %d %d %d %d\n", x, y, w, h); #ifdef SDL - // sdl_bitblt_to_screen(x, y, w, h); + sdl_notify_damage(x, y, w, h); #endif #if (defined(XWINDOW) || defined(DOS)) TPRINT(("Enter flush_display_lineregion x=%p, y=%d, w=%d, h=%d\n", (void *)x, y, w, h)); @@ -375,7 +379,7 @@ void flush_display_ptrregion(DLword *ybase, UNSIGNED bitoffset, int w, int h) x = bitoffset + (BITSPERWORD * (baseoffset - (DLWORD_PERLINE * y))); // printf("flush_display_ptrregion %d %d %d %d\n", x, y, w, h); #ifdef SDL - // sdl_bitblt_to_screen(x, y, w, h); + sdl_notify_damage(x, y, w, h); #endif #if (defined(XWINDOW) || defined(DOS)) TPRINT(("Enter flush_display_ptrregion\n x=%d, y=%d, w=%d, h=%d\n", x, y, w, h)); diff --git a/src/sdl.c b/src/sdl.c index 6ef8103..b321080 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -207,8 +207,12 @@ void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel) } int should_update_texture = 0; +void sdl_notify_damage(int x, int y, int w, int h) { + should_update_texture = 1; +} + void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { - // printf("bitblting\n"); + //printf("bitblting\n"); int before = SDL_GetTicks(); int width = sdl_displaywidth; int height = sdl_displayheight; @@ -234,7 +238,7 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { } } } - should_update_texture = 1; + //should_update_texture = 1; int after = SDL_GetTicks(); // printf("bitblting took %dms\n", after - before); /* before = SDL_GetTicks(); */ @@ -414,27 +418,25 @@ void process_SDLevents() { } } // handle_keyboard(); - /* sdl_bitblt_to_screen(); */ - // SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); int before = 0; int after = 0; - /* if(should_update_texture) { */ - /* before = SDL_GetTicks(); */ - /* SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); */ - /* after = SDL_GetTicks(); */ - /* should_update_texture = 0; */ - /* } */ - // printf("UpdateTexture took %dms\n", after - before); + if(should_update_texture) { + before = SDL_GetTicks(); + sdl_bitblt_to_screen(0, 0, sdl_displaywidth, sdl_displayheight); + SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); + after = SDL_GetTicks(); + // printf("UpdateTexture took %dms\n", after - before); + should_update_texture = 0; + } int this_draw = SDL_GetTicks(); before = SDL_GetTicks(); // printf("processing events took %dms\n", before - process_events_time); if(this_draw - last_draw > 16) { before = SDL_GetTicks(); - SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); + /* SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); */ after = SDL_GetTicks(); - should_update_texture = 0; + // should_update_texture = 0; - sdl_bitblt_to_screen(0, 0, sdl_displaywidth, sdl_displayheight); SDL_RenderClear(sdl_renderer); SDL_Rect r; r.x = 0; From cf8ecc1dd4be26b558f3a4281f3cdaa3750aabc2 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Fri, 22 Oct 2021 07:37:15 +0200 Subject: [PATCH 29/68] Add mouse wheel diagnostics. --- src/sdl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sdl.c b/src/sdl.c index b321080..02ba7bb 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -409,6 +409,9 @@ void process_SDLevents() { if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; } break; + case SDL_MOUSEWHEEL: + printf("mousewheel mouse %d x %d y %d direction %s\n", event.wheel.which, event.wheel.x, event.wheel.y, event.wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped"); + break; /* case SDL_KEYMAPCHANGED: */ /* printf("SDL_KEYMAPCHANGED\n"); break; */ /* case SDL_TEXTINPUT: */ @@ -488,7 +491,6 @@ int init_SDL(char *windowtitle, int w, int h, int s) { SDL_RenderSetScale(sdl_renderer, 1.0, 1.0); printf("Creating texture...\n"); sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height); - printf("-> %x\n", sdl_texture); buffer = malloc(width * height * sizeof(Uint32)); printf("SDL initialised\n"); return 0; From dbba06d9e16f1bf22b806b45425d60e50e4580df Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Mon, 25 Oct 2021 21:11:00 +0200 Subject: [PATCH 30/68] Add support for inverting video and setting mouse position. Use (IL:VIDEOCOLOR T/NIL) to change inversion. --- src/dspsubrs.c | 12 ++++++++++++ src/sdl.c | 12 +++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/dspsubrs.c b/src/dspsubrs.c index 7fdfe8a..bf3e48b 100644 --- a/src/dspsubrs.c +++ b/src/dspsubrs.c @@ -69,6 +69,13 @@ LispPTR DSP_VideoColor(LispPTR *args) /* args[0] : black flag */ return ATOM_T; else return NIL; +#elif defined(SDL) + invert = args[0] & 0xFFFF; + sdl_set_invert(invert); + if (invert) + return ATOM_T; + else + return NIL; #else return NIL; #endif @@ -114,6 +121,11 @@ void DSP_SetMousePos(LispPTR *args) if (Mouse_Included) set_Xmouseposition((int)(GetSmalldata(args[0])), (int)(GetSmalldata(args[1]))); #endif /* XWINDOW */ +#ifdef SDL + int x = (int)(GetSmalldata(args[0])); + int y = (int)(GetSmalldata(args[1])); + sdl_setMousePosition(x, y); +#endif /* SDL */ } /**************************************************** diff --git a/src/sdl.c b/src/sdl.c index 02ba7bb..ef5d635 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -210,7 +210,7 @@ int should_update_texture = 0; void sdl_notify_damage(int x, int y, int w, int h) { should_update_texture = 1; } - +int do_invert = 0; void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { //printf("bitblting\n"); int before = SDL_GetTicks(); @@ -228,9 +228,9 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { //printf("%d/%d %d\n", x, y, b); int px = 0; if(w & (1 << (bpw - 1 - b))) { - px = 0xff000000; + px = do_invert ? 0xffffffff : 0xff000000; } else { - px = 0xffffffff; + px = do_invert ? 0xff000000 : 0xffffffff; } //printf("px is %x\n", px); int xx = thex + b; @@ -333,6 +333,12 @@ static int last_keystate[512] = { 0 }; /* } */ /* } */ /* } */ +void sdl_set_invert(int flag) { + do_invert = flag; +} +void sdl_setMousePosition(int x, int y) { + SDL_WarpMouseInWindow(sdl_window, x, y); +} int process_events_time = 0; void process_SDLevents() { // printf("processing events delta %dms\n", SDL_GetTicks() - process_events_time); From 1db44b1f761597cfb8b54fec397ac6325dd9b722 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Sun, 7 Nov 2021 19:35:52 +0100 Subject: [PATCH 31/68] Add support for setting the mouse cursor. Not finished! Currently this just allocates a new X cursor each time, should be amended to cache the cursor, just like X does at the moment. --- CMakeLists.txt | 1 + src/dspsubrs.c | 8 ++++++-- src/sdl.c | 37 +++++++++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c0d81a8..2285b48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.15) SET(CMAKE_EXPORT_COMPILE_COMMANDS ON) SET(CMAKE_C_STANDARD 99) +# SET(CMAKE_C_EXTENSIONS OFF) # actually use c99, *not* gnu99 IF(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_COMPILER_ID MATCHES "GNU") IF(UNIX AND CMAKE_GENERATOR STREQUAL "Ninja") diff --git a/src/dspsubrs.c b/src/dspsubrs.c index bf3e48b..ba6eaf9 100644 --- a/src/dspsubrs.c +++ b/src/dspsubrs.c @@ -99,9 +99,11 @@ void DSP_Cursor(LispPTR *args, int argnum) extern int LastCursorX, LastCursorY; -#ifdef XWINDOW +#if defined(XWINDOW) /* For X-Windows, set the cursor to the given location. */ Set_XCursor((int)(args[0] & 0xFFFF), (int)(args[1] & 0xFFFF)); +#elif defined(SDL) + sdl_setCursor((int)(args[0] & 0xFFFF), (int)(args[1] & 0xFFFF)); #endif /* XWINDOW */ } @@ -186,8 +188,10 @@ void flip_cursor(void) { #endif -#ifdef XWINDOW +#if defined(XWINDOW) /* JDS 011213: 15- cur y, as function does same! */ Set_XCursor(Current_Hot_X, 15 - Current_Hot_Y); +#elif defined(SDL) + sdl_setCursor(0, 0); // TODO: keep track of the current hot_x and hot_y #endif /* XWINDOW */ } diff --git a/src/sdl.c b/src/sdl.c index ef5d635..83a5bb0 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -3,6 +3,11 @@ #include "lispemul.h" #include "miscstat.h" #include "keyboard.h" +#include "lspglob.h" // for IOPage +#include "display.h" // for CURSORHEIGHT, DisplayRegion68k + + + static SDL_Window *sdl_window = NULL; static SDL_Renderer *sdl_renderer = NULL; @@ -129,9 +134,6 @@ int sdl_windowheight = 0; // each pixel is shown as this many pixels int sdl_pixelscale = 0; -extern Uint32 *DisplayRegion68k; - - extern DLword *EmKbdAd068K, *EmKbdAd168K, *EmKbdAd268K, *EmKbdAd368K, *EmKbdAd468K, *EmKbdAd568K, *EmRealUtilin68K; extern DLword *CTopKeyevent; @@ -210,8 +212,35 @@ int should_update_texture = 0; void sdl_notify_damage(int x, int y, int w, int h) { should_update_texture = 1; } + +void sdl_setCursor(int hot_x, int hot_y) { + extern const unsigned char reversedbits[]; /* extern inside a function? Insanity! */ + DLword *newbm = ((DLword *)(IOPage->dlcursorbitmap)); + Uint8 cursor[32]; + printf("setCursor: bm %p, x %d, y %d\n", newbm, hot_x, hot_y); + for(int i = 0; i < CURSORHEIGHT; i++) { + printf("%04x ", newbm[i]); + // cursor[i] = newbm[i % 2 == 1 ? i - 1 : i + 1]; + } + printf("\n"); + Uint8 *bitmap = newbm; + for (int i = 0; i < 32; i++) cursor[i] = bitmap[i ^ 3]; + // TODO: actually keep track of the cursors, don't just allocate a new one every time! + for(int i = 0; i < CURSORHEIGHT * 2; i++) { + printf("%02x ", cursor[i]); + // cursor[i] = newbm[i % 2 == 1 ? i - 1 : i + 1]; + } + printf("\n"); + hot_x = 0; hot_y = 0; + SDL_Cursor *c = SDL_CreateCursor(cursor, cursor, 16, 16, hot_x, hot_y); + if(c == NULL) + printf("ERROR creating cursor: %s\n", SDL_GetError()); + SDL_SetCursor(c); + +} int do_invert = 0; void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { + Uint32 *dr = (Uint32*)DisplayRegion68k; //printf("bitblting\n"); int before = SDL_GetTicks(); int width = sdl_displaywidth; @@ -222,7 +251,7 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { int ylimit = _y + _h; for(int y = _y; y < ylimit; y++) { for(int x = _x / bpw; x < xlimit; x++) { - int w = DisplayRegion68k[y * pitch + x]; + int w = dr[y * pitch + x]; int thex = x * bpw; for(int b = 0; b < bpw; b++) { //printf("%d/%d %d\n", x, y, b); From 64e0419af37e0bf9a6027c2a1ae0f58ea7526548 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Sun, 7 Nov 2021 11:26:46 -0800 Subject: [PATCH 32/68] Fix "error: implicit declaration of function" from dspsubrs Creates an sdldefs.h, declares a few more functions in sdl.c static, and includes sdldefs.h in dspsubrs.c when necessary. --- inc/sdldefs.h | 11 +++++++++++ src/dspsubrs.c | 4 +++- src/sdl.c | 9 +++++---- 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 inc/sdldefs.h diff --git a/inc/sdldefs.h b/inc/sdldefs.h new file mode 100644 index 0000000..7af26ad --- /dev/null +++ b/inc/sdldefs.h @@ -0,0 +1,11 @@ +#ifndef SDLDEFS_H +#define SDLDEFS_H 1 + +void sdl_notify_damage(int x, int y, int w, int h); +void sdl_setCursor(int hot_x, int hot_y); +void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h); +void sdl_set_invert(int flag); +void sdl_setMousePosition(int x, int y); +void process_SDLevents(); +int init_SDL(char *windowtitle, int w, int h, int s); +#endif diff --git a/src/dspsubrs.c b/src/dspsubrs.c index ba6eaf9..7aa604e 100644 --- a/src/dspsubrs.c +++ b/src/dspsubrs.c @@ -18,9 +18,11 @@ #include "lispemul.h" // for LispPTR, DLword, ATOM_T, NIL #include "lispmap.h" // for S_POSITIVE #include "lsptypes.h" // for GETWORD -#ifdef XWINDOW +#if defined(XWINDOW) #include "xcursordefs.h" // for Set_XCursor #include "xlspwindefs.h" // for lisp_Xvideocolor, set_Xmouseposition +#elif defined(SDL) +#include "sdldefs.h" #endif extern int DebugDSP; diff --git a/src/sdl.c b/src/sdl.c index 83a5bb0..7f5a22c 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -1,5 +1,6 @@ #include <SDL.h> #include <SDL_keycode.h> +#include "sdldefs.h" #include "lispemul.h" #include "miscstat.h" #include "keyboard.h" @@ -275,7 +276,7 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { /* after = SDL_GetTicks(); */ /* printf("UpdateTexture took %dms\n", after - before); */ } -int map_key(SDL_Keycode k) { +static int map_key(SDL_Keycode k) { for(int i = 0; keymap[i] != -1; i+= 2) { if(keymap[i+1] == k) return keymap[i]; @@ -283,7 +284,7 @@ int map_key(SDL_Keycode k) { return -1; } #define KEYCODE_OFFSET 0 -void handle_keydown(SDL_Keycode k, unsigned short mod) { +static void handle_keydown(SDL_Keycode k, unsigned short mod) { int lk = map_key(k); if(lk == -1) { printf("No mapping for key %s\n", SDL_GetKeyName(k)); @@ -294,7 +295,7 @@ void handle_keydown(SDL_Keycode k, unsigned short mod) { if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; } } -void handle_keyup(SDL_Keycode k, unsigned short mod) { +static void handle_keyup(SDL_Keycode k, unsigned short mod) { int lk = map_key(k); if(lk == -1) { printf("No mapping for key %s\n", SDL_GetKeyName(k)); @@ -320,7 +321,7 @@ extern MISCSTATS *MiscStats; #define MOUSE_LEFT 13 #define MOUSE_RIGHT 14 #define MOUSE_MIDDLE 15 -void sdl_update_viewport(int width, int height) { +static void sdl_update_viewport(int width, int height) { int w = width / 32 * 32; if(w > sdl_displaywidth * sdl_pixelscale) w = sdl_displaywidth * sdl_pixelscale; From 0ed77193435e2d46a8dec7fbb06969e628ab6e45 Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Mon, 8 Nov 2021 20:03:28 +0100 Subject: [PATCH 33/68] Change Pixelformat to 332 (one byte) instead of 8888 (four bytes). --- src/sdl.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 7f5a22c..680a782 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -13,7 +13,7 @@ static SDL_Window *sdl_window = NULL; static SDL_Renderer *sdl_renderer = NULL; static SDL_Texture *sdl_texture = NULL; -static Uint32 *buffer = NULL; +static char *buffer = NULL; extern void kb_trans(u_short keycode, u_short upflg); extern int error(const char *s); @@ -197,17 +197,6 @@ void DoRing() { if (*KEYBUFFERING68k == NIL) *KEYBUFFERING68k = ATOM_T; } -void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel) -{ - if(x >= surface->w) - return; - if(y >= surface->h) - return; - Uint32 * const target_pixel = (Uint32 *) ((Uint8 *) surface->pixels - + y * surface->pitch - + x * surface->format->BytesPerPixel); - *target_pixel = pixel; -} int should_update_texture = 0; void sdl_notify_damage(int x, int y, int w, int h) { @@ -246,7 +235,7 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { int before = SDL_GetTicks(); int width = sdl_displaywidth; int height = sdl_displayheight; - int bpw = 32; + int bpw = 8 * sizeof(Uint32); int pitch = sdl_displaywidth / bpw; int xlimit = (_x + _w + bpw - 1) / bpw; int ylimit = _y + _h; @@ -258,9 +247,9 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { //printf("%d/%d %d\n", x, y, b); int px = 0; if(w & (1 << (bpw - 1 - b))) { - px = do_invert ? 0xffffffff : 0xff000000; + px = do_invert ? 0xff : 0x00; } else { - px = do_invert ? 0xff000000 : 0xffffffff; + px = do_invert ? 0x00 : 0xff; } //printf("px is %x\n", px); int xx = thex + b; @@ -462,7 +451,7 @@ void process_SDLevents() { if(should_update_texture) { before = SDL_GetTicks(); sdl_bitblt_to_screen(0, 0, sdl_displaywidth, sdl_displayheight); - SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); + SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(char)); after = SDL_GetTicks(); // printf("UpdateTexture took %dms\n", after - before); should_update_texture = 0; @@ -526,8 +515,8 @@ int init_SDL(char *windowtitle, int w, int h, int s) { SDL_SetRenderDrawColor(sdl_renderer, 50, 50, 50, 255); SDL_RenderSetScale(sdl_renderer, 1.0, 1.0); printf("Creating texture...\n"); - sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height); - buffer = malloc(width * height * sizeof(Uint32)); + sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING, width, height); + buffer = malloc(width * height * sizeof(char)); printf("SDL initialised\n"); return 0; } From 0e731ce02dee935b972b76d272f90afcb560fe3d Mon Sep 17 00:00:00 2001 From: Peter <craven@gmx.net> Date: Mon, 8 Nov 2021 21:10:23 +0100 Subject: [PATCH 34/68] Cache cursors for SDL. --- inc/sdldefs.h | 2 +- src/sdl.c | 116 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 94 insertions(+), 24 deletions(-) diff --git a/inc/sdldefs.h b/inc/sdldefs.h index 7af26ad..e1a0c18 100644 --- a/inc/sdldefs.h +++ b/inc/sdldefs.h @@ -3,7 +3,7 @@ void sdl_notify_damage(int x, int y, int w, int h); void sdl_setCursor(int hot_x, int hot_y); -void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h); +void sdl_bitblt_to_screen(int x, int y, int w, int h); void sdl_set_invert(int flag); void sdl_setMousePosition(int x, int y); void process_SDLevents(); diff --git a/src/sdl.c b/src/sdl.c index 680a782..2d9ff06 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -134,6 +134,8 @@ int sdl_windowwidth = 0; int sdl_windowheight = 0; // each pixel is shown as this many pixels int sdl_pixelscale = 0; +// if set, invert video (white on black) +static int do_invert = 0; extern DLword *EmKbdAd068K, *EmKbdAd168K, *EmKbdAd268K, *EmKbdAd368K, *EmKbdAd468K, *EmKbdAd568K, *EmRealUtilin68K; @@ -197,41 +199,102 @@ void DoRing() { if (*KEYBUFFERING68k == NIL) *KEYBUFFERING68k = ATOM_T; } -int should_update_texture = 0; +static int should_update_texture = 0; +static int min_x = 0; +static int min_y = 0; +static int max_x = 0; +static int max_y = 0; void sdl_notify_damage(int x, int y, int w, int h) { + if(x < min_x) + min_x = x; + if(y < min_y) + min_y = y; + if(x + w > max_x) + max_x = x + w; + if(y + h > max_y) + max_y = y + h; should_update_texture = 1; } +/* a simple linked list to remember generated cursors + * because cursors don't have any identifying information + * except for the actual bitmap in Lisp, just cache that. + * 16 Uint16, to give a 16x16 bitmap cursor. + */ +struct CachedCursor { + struct CachedCursor *next; + DLword bitmap[CURSORHEIGHT]; + SDL_Cursor *cursor; +} *sdl_cursorlist = NULL; + +static int cursor_equal_p(DLword *a, DLword *b) { + for(int i = 0; i < CURSORHEIGHT; i++) + if(a[i] != b[i]) return FALSE; + return TRUE; +} + +/* + * Try to find cursor CURSOR on the sdl_cursorlist, if it isn't there, add it. + * Return an SDL_Cursor that can be used directly. + */ +static SDL_Cursor *sdl_getOrAllocateCursor(Uint8 cursor[32], int hot_x, int hot_y) { + hot_x = 0; + hot_y = 0; + int i = 0; + /* try to find the cursor by checking the full bitmap */ + DLword *bitmap = (DLword*)cursor; + struct CachedCursor *clp = sdl_cursorlist; + for(; clp != NULL; clp = clp->next) { + if(cursor_equal_p(clp->bitmap, bitmap) == TRUE) break; + } + if(clp == NULL) { /* it isn't there, push on a new one to the front of the list */ + clp = (struct CachedCursor *)malloc(sizeof(struct CachedCursor)); + for(int i = 0; i < CURSORHEIGHT; i++) clp->bitmap[i] = bitmap[i]; + SDL_Cursor *c = SDL_CreateCursor(cursor, cursor, 16, 16, hot_x, hot_y); + if(c == NULL) + printf("ERROR creating cursor: %s\n", SDL_GetError()); + clp->cursor = c; + clp->next = sdl_cursorlist; + sdl_cursorlist = clp; + // printf("Creating new cursor, using it\n"); + return clp->cursor; + } else { + /* TODO: move to front of list */ + // printf("Found cursor, using it\n"); + return clp->cursor; + } +} + +/* + * Read a cursor bitmap from lisp. Try to find a cached cursor, then use that. + * Use HOT_X and HOT_Y as the cursor hotspot. + */ void sdl_setCursor(int hot_x, int hot_y) { - extern const unsigned char reversedbits[]; /* extern inside a function? Insanity! */ DLword *newbm = ((DLword *)(IOPage->dlcursorbitmap)); Uint8 cursor[32]; - printf("setCursor: bm %p, x %d, y %d\n", newbm, hot_x, hot_y); - for(int i = 0; i < CURSORHEIGHT; i++) { - printf("%04x ", newbm[i]); - // cursor[i] = newbm[i % 2 == 1 ? i - 1 : i + 1]; - } - printf("\n"); - Uint8 *bitmap = newbm; + // printf("setCursor: bm %p, x %d, y %d\n", newbm, hot_x, hot_y); + /* for(int i = 0; i < CURSORHEIGHT; i++) { */ + /* printf("%04x ", newbm[i]); */ + /* // cursor[i] = newbm[i % 2 == 1 ? i - 1 : i + 1]; */ + /* } */ + /* printf("\n"); */ + Uint8 *bitmap = (Uint8*)newbm; for (int i = 0; i < 32; i++) cursor[i] = bitmap[i ^ 3]; // TODO: actually keep track of the cursors, don't just allocate a new one every time! - for(int i = 0; i < CURSORHEIGHT * 2; i++) { - printf("%02x ", cursor[i]); - // cursor[i] = newbm[i % 2 == 1 ? i - 1 : i + 1]; - } - printf("\n"); - hot_x = 0; hot_y = 0; - SDL_Cursor *c = SDL_CreateCursor(cursor, cursor, 16, 16, hot_x, hot_y); - if(c == NULL) - printf("ERROR creating cursor: %s\n", SDL_GetError()); + /* for(int i = 0; i < CURSORHEIGHT * 2; i++) { */ + /* printf("%02x ", cursor[i]); */ + /* // cursor[i] = newbm[i % 2 == 1 ? i - 1 : i + 1]; */ + /* } */ + /* printf("\n"); */ + SDL_Cursor *c = sdl_getOrAllocateCursor(cursor, hot_x, hot_y); SDL_SetCursor(c); - } -int do_invert = 0; + void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { + // printf("bitblt(%d, %d, %d, %d)\n", _x, _y, _w, _h); Uint32 *dr = (Uint32*)DisplayRegion68k; - //printf("bitblting\n"); + int before = SDL_GetTicks(); int width = sdl_displaywidth; int height = sdl_displayheight; @@ -450,11 +513,14 @@ void process_SDLevents() { int after = 0; if(should_update_texture) { before = SDL_GetTicks(); - sdl_bitblt_to_screen(0, 0, sdl_displaywidth, sdl_displayheight); + sdl_bitblt_to_screen(min_x, min_y, max_x - min_x, max_y - min_y); SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(char)); after = SDL_GetTicks(); - // printf("UpdateTexture took %dms\n", after - before); + // printf("UpdateTexture took %dms\n", after - before); should_update_texture = 0; + min_x = sdl_displaywidth; + min_y = sdl_displayheight; + max_x = max_y = 0; } int this_draw = SDL_GetTicks(); before = SDL_GetTicks(); @@ -492,6 +558,10 @@ int init_SDL(char *windowtitle, int w, int h, int s) { sdl_displayheight = h; sdl_windowwidth = w * s; sdl_windowheight = h * s; + min_x = 0; + max_x = sdl_displaywidth; + min_y = 0; + max_y = sdl_displayheight; int width = sdl_displaywidth; int height = sdl_displayheight; printf("requested width: %d, height: %d\n", width, height); From a2f8f23152301a3d346ffa1df8e685b970396bbb Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 8 Nov 2021 18:16:14 -0800 Subject: [PATCH 35/68] Keep damage notification boundaries within the current screen On startup, bitblt calls may be made for the original screen size and if the new screen size is smaller than that, we must ensure that damage notifications are contained within the new screen. Move definition of min() so we can use it in sdl_notify_damage() --- src/sdl.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 2d9ff06..5e5fe16 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -199,6 +199,12 @@ void DoRing() { if (*KEYBUFFERING68k == NIL) *KEYBUFFERING68k = ATOM_T; } +static int min(int a, int b) { + if(a < b) + return a; + return b; +} + static int should_update_texture = 0; static int min_x = 0; @@ -211,9 +217,9 @@ void sdl_notify_damage(int x, int y, int w, int h) { if(y < min_y) min_y = y; if(x + w > max_x) - max_x = x + w; + max_x = min(x + w, sdl_displaywidth - 1); if(y + h > max_y) - max_y = y + h; + max_y = min(y + h, sdl_displayheight - 1); should_update_texture = 1; } @@ -388,11 +394,6 @@ static void sdl_update_viewport(int width, int height) { SDL_RenderSetViewport(sdl_renderer, &r); printf("new viewport: %d / %d\n", w, h); } -static int min(int a, int b) { - if(a < b) - return a; - return b; -} static int last_draw = 0; static int last_keystate[512] = { 0 }; /* void handle_keyboard() { */ From fab10fc71fcffbc6c186e6614b007a06094b1226 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 8 Nov 2021 20:40:30 -0800 Subject: [PATCH 36/68] Bounds check indexing into intermediate screen buffer When building with -DCMAKE_BUILD_TYPE=Debug, we assert() that the index into the intermediate screen buffer is in bounds. --- src/sdl.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 5e5fe16..a216078 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -1,5 +1,6 @@ #include <SDL.h> #include <SDL_keycode.h> +#include <assert.h> #include "sdldefs.h" #include "lispemul.h" #include "miscstat.h" @@ -13,6 +14,7 @@ static SDL_Window *sdl_window = NULL; static SDL_Renderer *sdl_renderer = NULL; static SDL_Texture *sdl_texture = NULL; +static int buffer_size = 0; static char *buffer = NULL; extern void kb_trans(u_short keycode, u_short upflg); @@ -321,8 +323,9 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { px = do_invert ? 0x00 : 0xff; } //printf("px is %x\n", px); - int xx = thex + b; - buffer[y * sdl_displaywidth + xx] = px; + int pxindex = (y * sdl_displaywidth) + thex + b; + assert(pxindex >= 0 && pxindex < buffer_size); + buffer[pxindex] = px; } } } @@ -587,7 +590,8 @@ int init_SDL(char *windowtitle, int w, int h, int s) { SDL_RenderSetScale(sdl_renderer, 1.0, 1.0); printf("Creating texture...\n"); sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING, width, height); - buffer = malloc(width * height * sizeof(char)); + buffer_size = width * height * sizeof(char); + buffer = malloc(buffer_size); printf("SDL initialised\n"); return 0; } From e5b8248465a49287f90dcdeb4c34ef2b86131bb0 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 8 Nov 2021 21:28:06 -0800 Subject: [PATCH 37/68] Width, but not height, must be multiple of 32 --- src/sdl.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index a216078..447f5f1 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -555,9 +555,8 @@ void process_SDLevents() { } int init_SDL(char *windowtitle, int w, int h, int s) { sdl_pixelscale = s; - // must be multiples of 32 - w = w / 32 * 32; - h = h / 32 * 32; + // width must be multiple of 32 + w = (w + 31) / 32 * 32; sdl_displaywidth = w; sdl_displayheight = h; sdl_windowwidth = w * s; From 2b957aa8f3a2b09a5f9c3409c964c7a50ab61aeb Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 8 Nov 2021 22:02:43 -0800 Subject: [PATCH 38/68] Move display update into separate procedure --- src/sdl.c | 79 +++++++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 447f5f1..f2fddb3 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -425,6 +425,44 @@ void sdl_set_invert(int flag) { void sdl_setMousePosition(int x, int y) { SDL_WarpMouseInWindow(sdl_window, x, y); } +void sdl_update_display_rendering() { + int before = 0; + int after = 0; + + if(should_update_texture) { + before = SDL_GetTicks(); + sdl_bitblt_to_screen(min_x, min_y, max_x - min_x, max_y - min_y); + SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(char)); + after = SDL_GetTicks(); + // printf("UpdateTexture took %dms\n", after - before); + should_update_texture = 0; + min_x = sdl_displaywidth; + min_y = sdl_displayheight; + max_x = max_y = 0; + } + int this_draw = SDL_GetTicks(); + before = SDL_GetTicks(); + + if(this_draw - last_draw > 16) { + before = SDL_GetTicks(); + SDL_RenderClear(sdl_renderer); + SDL_Rect r; + r.x = 0; + r.y = 0; + r.w = min(sdl_windowwidth / sdl_pixelscale, sdl_displaywidth); + r.h = min(sdl_windowheight / sdl_pixelscale, sdl_displayheight); + SDL_Rect s; + s.x = 0; + s.y = 0; + s.w = min(sdl_windowwidth / sdl_pixelscale * sdl_pixelscale, sdl_displaywidth * sdl_pixelscale); + s.h = min(sdl_windowheight / sdl_pixelscale * sdl_pixelscale, sdl_displayheight * sdl_pixelscale); + SDL_RenderCopy(sdl_renderer, sdl_texture, &r, &s); + SDL_RenderPresent(sdl_renderer); + last_draw = this_draw; + after = SDL_GetTicks(); + // printf("rendering took %dms\n", after - before); + } +} int process_events_time = 0; void process_SDLevents() { // printf("processing events delta %dms\n", SDL_GetTicks() - process_events_time); @@ -512,46 +550,7 @@ void process_SDLevents() { printf("other event type: %d\n", event.type); } } - // handle_keyboard(); - int before = 0; - int after = 0; - if(should_update_texture) { - before = SDL_GetTicks(); - sdl_bitblt_to_screen(min_x, min_y, max_x - min_x, max_y - min_y); - SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(char)); - after = SDL_GetTicks(); - // printf("UpdateTexture took %dms\n", after - before); - should_update_texture = 0; - min_x = sdl_displaywidth; - min_y = sdl_displayheight; - max_x = max_y = 0; - } - int this_draw = SDL_GetTicks(); - before = SDL_GetTicks(); - // printf("processing events took %dms\n", before - process_events_time); - if(this_draw - last_draw > 16) { - before = SDL_GetTicks(); - /* SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); */ - after = SDL_GetTicks(); - // should_update_texture = 0; - - SDL_RenderClear(sdl_renderer); - SDL_Rect r; - r.x = 0; - r.y = 0; - r.w = min(sdl_windowwidth / sdl_pixelscale, sdl_displaywidth); - r.h = min(sdl_windowheight / sdl_pixelscale, sdl_displayheight); - SDL_Rect s; - s.x = 0; - s.y = 0; - s.w = min(sdl_windowwidth / sdl_pixelscale * sdl_pixelscale, sdl_displaywidth * sdl_pixelscale); - s.h = min(sdl_windowheight / sdl_pixelscale * sdl_pixelscale, sdl_displayheight * sdl_pixelscale); - SDL_RenderCopy(sdl_renderer, sdl_texture, &r, &s); - SDL_RenderPresent(sdl_renderer); - last_draw = this_draw; - after = SDL_GetTicks(); - // printf("rendering took %dms\n", after - before); - } + sdl_update_display_rendering(); } int init_SDL(char *windowtitle, int w, int h, int s) { sdl_pixelscale = s; From 74da70d0b43f53ad928091f5a912dfa46a415b43 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 8 Nov 2021 22:18:08 -0800 Subject: [PATCH 39/68] Introduce alternative method for updating window from Lisp bitmap If the preprocessor symbol SDLRENDERING is defined the code operates as it previously did. In the absence of that, we process the Lisp bitmap into an intermediate form (as before) but then use SDL bitblt to redraw the intermediate form on the window's surface. --- src/sdl.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index f2fddb3..bd45e0b 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -12,8 +12,13 @@ static SDL_Window *sdl_window = NULL; +#if defined(SDLRENDERING) static SDL_Renderer *sdl_renderer = NULL; static SDL_Texture *sdl_texture = NULL; +#else +static SDL_Surface *sdl_windowsurface = NULL; +static SDL_Surface *sdl_buffersurface = NULL; +#endif static int buffer_size = 0; static char *buffer = NULL; @@ -383,6 +388,7 @@ extern MISCSTATS *MiscStats; #define MOUSE_RIGHT 14 #define MOUSE_MIDDLE 15 static void sdl_update_viewport(int width, int height) { + /* XXX: needs work */ int w = width / 32 * 32; if(w > sdl_displaywidth * sdl_pixelscale) w = sdl_displaywidth * sdl_pixelscale; @@ -394,7 +400,9 @@ static void sdl_update_viewport(int width, int height) { r.y = 0; r.w = w; r.h = h; +#if defined(SDLRENDERING) SDL_RenderSetViewport(sdl_renderer, &r); +#endif printf("new viewport: %d / %d\n", w, h); } static int last_draw = 0; @@ -425,6 +433,7 @@ void sdl_set_invert(int flag) { void sdl_setMousePosition(int x, int y) { SDL_WarpMouseInWindow(sdl_window, x, y); } +#if defined(SDLRENDERING) void sdl_update_display_rendering() { int before = 0; int after = 0; @@ -463,6 +472,45 @@ void sdl_update_display_rendering() { // printf("rendering took %dms\n", after - before); } } +#else +void sdl_update_display_surfaces() { + int before = 0; + int after = 0; + int this_draw = SDL_GetTicks(); + + /* check if there's anything to do and sufficient time has elapsed */ + if (!should_update_texture || this_draw - last_draw <= 16) { + return; + } + before = SDL_GetTicks(); + sdl_bitblt_to_screen(min_x, min_y, max_x - min_x, max_y - min_y); + after = SDL_GetTicks(); + SDL_Rect r; + r.x = min_x; + r.y = min_y; + r.w = max_x - min_x; + r.h = max_y - min_y; + if (sdl_pixelscale == 1) { + SDL_BlitSurface(sdl_buffersurface, &r, sdl_windowsurface, &r); + SDL_UpdateWindowSurfaceRects(sdl_window, &r, 1); + } else { + SDL_Rect s; + s.x = r.x * sdl_pixelscale; + s.y = r.y * sdl_pixelscale; + s.w = r.w * sdl_pixelscale; + s.h = r.h * sdl_pixelscale; + SDL_BlitScaled(sdl_buffersurface, &r, sdl_windowsurface, &s); + SDL_UpdateWindowSurfaceRects(sdl_window, &s, 1); + } + min_x = 0; + max_x = sdl_displaywidth; + min_y = 0; + max_y = sdl_displayheight; + after = SDL_GetTicks(); + last_draw = this_draw; + // printf("surface update took %dms\n", after - before); +} +#endif int process_events_time = 0; void process_SDLevents() { // printf("processing events delta %dms\n", SDL_GetTicks() - process_events_time); @@ -476,6 +524,7 @@ void process_SDLevents() { case SDL_WINDOWEVENT: switch(event.window.event) { case SDL_WINDOWEVENT_RESIZED: { + /* XXX: what about integer multiple of 32 requirements here? */ sdl_windowwidth = event.window.data1; sdl_windowheight = event.window.data2; sdl_update_viewport(sdl_windowwidth, sdl_windowheight); @@ -550,8 +599,13 @@ void process_SDLevents() { printf("other event type: %d\n", event.type); } } +#if defined(SDLRENDERING) sdl_update_display_rendering(); +#else + sdl_update_display_surfaces(); +#endif } + int init_SDL(char *windowtitle, int w, int h, int s) { sdl_pixelscale = s; // width must be multiple of 32 @@ -578,6 +632,9 @@ int init_SDL(char *windowtitle, int w, int h, int s) { printf("Window could not be created. SDL_Error: %s\n", SDL_GetError()); return 2; } + buffer_size = width * height * sizeof(char); + buffer = malloc(buffer_size); +#if defined(SDLRENDERING) printf("Creating renderer...\n"); sdl_renderer = SDL_CreateRenderer(sdl_window, -1, SDL_RENDERER_ACCELERATED); if(NULL == sdl_renderer) { @@ -588,8 +645,12 @@ int init_SDL(char *windowtitle, int w, int h, int s) { SDL_RenderSetScale(sdl_renderer, 1.0, 1.0); printf("Creating texture...\n"); sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING, width, height); - buffer_size = width * height * sizeof(char); - buffer = malloc(buffer_size); +#else + printf("Creating window surface and buffer surface\n"); + sdl_windowsurface = SDL_GetWindowSurface(sdl_window); + sdl_buffersurface = SDL_CreateRGBSurfaceWithFormatFrom(buffer, sdl_displaywidth, sdl_displayheight, 8, + sdl_displaywidth, SDL_PIXELFORMAT_RGB332); +#endif printf("SDL initialised\n"); return 0; } From 4104219ee5faad31f2652effb1dac1c15ca811b1 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Tue, 9 Nov 2021 13:57:25 -0800 Subject: [PATCH 40/68] Avoid intermediate buffer and bitblt Lisp screen changes directly into the texture pixels Using SDL_LockTexture/SDL_UnlockTexture we can gain more direct access to the pixels of the texture and update them directly from the Lisp screen bitmap. At the same time, for both the rendering case and the display surface case, update the pixel format used to be either the first (presumably preferred?) format for a texture, or the surface format of the window. Use the SDL routines to pick out the pixel value for Black and White based on the destination it will be placed in. --- src/sdl.c | 102 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 22 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index bd45e0b..37215ce 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -8,19 +8,23 @@ #include "lspglob.h" // for IOPage #include "display.h" // for CURSORHEIGHT, DisplayRegion68k - - +#define SDLRENDERING 1 static SDL_Window *sdl_window = NULL; #if defined(SDLRENDERING) static SDL_Renderer *sdl_renderer = NULL; +static SDL_RendererInfo sdl_rendererinfo = {0}; static SDL_Texture *sdl_texture = NULL; #else static SDL_Surface *sdl_windowsurface = NULL; static SDL_Surface *sdl_buffersurface = NULL; #endif +static Uint32 sdl_white; +static Uint32 sdl_black; +static int sdl_bytesperpixel; +static SDL_PixelFormat *sdl_pixelformat; static int buffer_size = 0; -static char *buffer = NULL; +static void *buffer = NULL; extern void kb_trans(u_short keycode, u_short upflg); extern int error(const char *s); @@ -303,7 +307,51 @@ void sdl_setCursor(int hot_x, int hot_y) { SDL_Cursor *c = sdl_getOrAllocateCursor(cursor, hot_x, hot_y); SDL_SetCursor(c); } - +#if defined(SDLRENDERING) +void sdl_bitblt_to_texture(int _x, int _y, int _w, int _h) { + // printf("bitblt(%d, %d, %d, %d)\n", _x, _y, _w, _h); + Uint32 *dr = (Uint32*)DisplayRegion68k; + void *tbuffer; + int tpitch; + int before = SDL_GetTicks(); + int width = sdl_displaywidth; + int height = sdl_displayheight; + int bpw = 8 * sizeof(Uint32); + int pitch = sdl_displaywidth / bpw; + int xlimit = (_x + _w + bpw - 1) / bpw; + int ylimit = _y + _h; + SDL_Rect r; + // if we are to avoid dealing with partial words in the update we must calculate the + // rounded down x starting bit position and the rounded up x ending bit position and + // lock that region + r.x = (_x / bpw) * bpw; + r.w = ((_x + _w + bpw - 1) / bpw * bpw) - r.x; + r.y = _y; + r.h = _h; + // printf("locking %d %d %d %d\n", r.x, r.y, r.w, r.h); + SDL_LockTexture(sdl_texture, &r, &tbuffer, &tpitch); + for(int y = _y; y < ylimit; y++) { // for each line + for(int x = _x / bpw; x < xlimit; x++) { // for each word within line + int w = dr[y * pitch + x]; + int thex = x * bpw; + for(int b = 0; b < bpw; b++) { // for each bit within word + // printf("%d/%d %d\n", x, y, b); + uint32_t px = 0; + if(w & (1 << (bpw - 1 - b))) { + px = do_invert ? sdl_white : sdl_black; + } else { + px = do_invert ? sdl_black : sdl_white; + } + //printf("px is %x\n", px); + int pxindex = ((y - _y) * (tpitch / sdl_bytesperpixel)) + ((x - (_x / bpw)) * bpw) + b; + ((Uint32 *)tbuffer)[pxindex] = px; + } + } + } + SDL_UnlockTexture(sdl_texture); + int after = SDL_GetTicks(); +} +#else void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { // printf("bitblt(%d, %d, %d, %d)\n", _x, _y, _w, _h); Uint32 *dr = (Uint32*)DisplayRegion68k; @@ -321,27 +369,23 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { int thex = x * bpw; for(int b = 0; b < bpw; b++) { //printf("%d/%d %d\n", x, y, b); - int px = 0; + uint32_t px = 0; if(w & (1 << (bpw - 1 - b))) { - px = do_invert ? 0xff : 0x00; + px = do_invert ? sdl_white : sdl_black; } else { - px = do_invert ? 0x00 : 0xff; + px = do_invert ? sdl_black : sdl_white; } //printf("px is %x\n", px); int pxindex = (y * sdl_displaywidth) + thex + b; assert(pxindex >= 0 && pxindex < buffer_size); - buffer[pxindex] = px; + ((Uint32 *)buffer)[pxindex] = px; } } } - //should_update_texture = 1; int after = SDL_GetTicks(); // printf("bitblting took %dms\n", after - before); - /* before = SDL_GetTicks(); */ - /* SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(Uint32)); */ - /* after = SDL_GetTicks(); */ - /* printf("UpdateTexture took %dms\n", after - before); */ } +#endif static int map_key(SDL_Keycode k) { for(int i = 0; keymap[i] != -1; i+= 2) { if(keymap[i+1] == k) @@ -440,8 +484,8 @@ void sdl_update_display_rendering() { if(should_update_texture) { before = SDL_GetTicks(); - sdl_bitblt_to_screen(min_x, min_y, max_x - min_x, max_y - min_y); - SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sizeof(char)); + sdl_bitblt_to_texture(min_x, min_y, max_x - min_x, max_y - min_y); + // SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sdl_bytesperpixel); after = SDL_GetTicks(); // printf("UpdateTexture took %dms\n", after - before); should_update_texture = 0; @@ -452,9 +496,10 @@ void sdl_update_display_rendering() { int this_draw = SDL_GetTicks(); before = SDL_GetTicks(); + if(this_draw - last_draw > 16) { before = SDL_GetTicks(); - SDL_RenderClear(sdl_renderer); + //SDL_RenderClear(sdl_renderer); SDL_Rect r; r.x = 0; r.y = 0; @@ -466,6 +511,7 @@ void sdl_update_display_rendering() { s.w = min(sdl_windowwidth / sdl_pixelscale * sdl_pixelscale, sdl_displaywidth * sdl_pixelscale); s.h = min(sdl_windowheight / sdl_pixelscale * sdl_pixelscale, sdl_displayheight * sdl_pixelscale); SDL_RenderCopy(sdl_renderer, sdl_texture, &r, &s); + //SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL); SDL_RenderPresent(sdl_renderer); last_draw = this_draw; after = SDL_GetTicks(); @@ -632,8 +678,6 @@ int init_SDL(char *windowtitle, int w, int h, int s) { printf("Window could not be created. SDL_Error: %s\n", SDL_GetError()); return 2; } - buffer_size = width * height * sizeof(char); - buffer = malloc(buffer_size); #if defined(SDLRENDERING) printf("Creating renderer...\n"); sdl_renderer = SDL_CreateRenderer(sdl_window, -1, SDL_RENDERER_ACCELERATED); @@ -641,15 +685,29 @@ int init_SDL(char *windowtitle, int w, int h, int s) { printf("SDL Error: %s\n", SDL_GetError()); return 3; } - SDL_SetRenderDrawColor(sdl_renderer, 50, 50, 50, 255); + SDL_RenderClear(sdl_renderer); + SDL_GetRendererInfo(sdl_renderer, &sdl_rendererinfo); + // SDL_SetRenderDrawColor(sdl_renderer, 50, 50, 50, 255); SDL_RenderSetScale(sdl_renderer, 1.0, 1.0); printf("Creating texture...\n"); - sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING, width, height); + sdl_pixelformat = SDL_AllocFormat(sdl_rendererinfo.texture_formats[0]); + sdl_texture = SDL_CreateTexture(sdl_renderer, sdl_pixelformat->format, SDL_TEXTUREACCESS_STREAMING, width, height); + sdl_black = SDL_MapRGB(sdl_pixelformat, 0, 0, 0); + sdl_white = SDL_MapRGB(sdl_pixelformat, 255, 255, 255); + sdl_bytesperpixel = sdl_pixelformat->BytesPerPixel; #else printf("Creating window surface and buffer surface\n"); sdl_windowsurface = SDL_GetWindowSurface(sdl_window); - sdl_buffersurface = SDL_CreateRGBSurfaceWithFormatFrom(buffer, sdl_displaywidth, sdl_displayheight, 8, - sdl_displaywidth, SDL_PIXELFORMAT_RGB332); + sdl_pixelformat = sdl_windowsurface->format; + sdl_black = SDL_MapRGB(sdl_pixelformat, 0, 0, 0); + sdl_white = SDL_MapRGB(sdl_pixelformat, 255, 255, 255); + sdl_bytesperpixel = sdl_pixelformat->BytesPerPixel; + buffer_size = width * height * sdl_bytesperpixel; + buffer = malloc(buffer_size); + sdl_buffersurface = SDL_CreateRGBSurfaceWithFormatFrom(buffer, sdl_displaywidth, sdl_displayheight, + sdl_bytesperpixel * 8, + sdl_displaywidth * sdl_bytesperpixel, + sdl_pixelformat->format); #endif printf("SDL initialised\n"); return 0; From 16e4062b8fc3297a99cc057d7b865a817297e8a9 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Tue, 9 Nov 2021 20:15:00 -0800 Subject: [PATCH 41/68] When updating the window surface directly, at scale 1, avoid the intermediate buffer Adds an sdl_blt_to_window_surface() that goes directly from the Lisp bitmap to the window surface avoiding the intermediate copy. This is only coded for a scale factor of 1. Uses the intermediate buffer if the scale is not 1. Corrects an error where the damage rectangle was not properly reset. --- src/sdl.c | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 37215ce..6fd6649 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -385,6 +385,39 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { int after = SDL_GetTicks(); // printf("bitblting took %dms\n", after - before); } +void sdl_bitblt_to_window_surface(int _x, int _y, int _w, int _h) { + // printf("bitblt(%d, %d, %d, %d)\n", _x, _y, _w, _h); + Uint32 *dr = (Uint32*)DisplayRegion68k; + + int before = SDL_GetTicks(); + int width = sdl_displaywidth; + int height = sdl_displayheight; + int bpw = 8 * sizeof(Uint32); + int pitch = sdl_displaywidth / bpw; + int xlimit = (_x + _w + bpw - 1) / bpw; + int ylimit = _y + _h; + for(int y = _y; y < ylimit; y++) { + for(int x = _x / bpw; x < xlimit; x++) { + int w = dr[y * pitch + x]; + int thex = x * bpw; + for(int b = 0; b < bpw; b++) { + //printf("%d/%d %d\n", x, y, b); + uint32_t px = 0; + if(w & (1 << (bpw - 1 - b))) { + px = do_invert ? sdl_white : sdl_black; + } else { + px = do_invert ? sdl_black : sdl_white; + } + //printf("px is %x\n", px); + int pxindex = (y * sdl_displaywidth) + thex + b; + assert(pxindex >= 0 && pxindex < buffer_size); + ((Uint32 *)sdl_windowsurface->pixels)[pxindex] = px; + } + } + } + int after = SDL_GetTicks(); + // printf("bitblting took %dms\n", after - before); +} #endif static int map_key(SDL_Keycode k) { for(int i = 0; keymap[i] != -1; i+= 2) { @@ -528,16 +561,13 @@ void sdl_update_display_surfaces() { if (!should_update_texture || this_draw - last_draw <= 16) { return; } - before = SDL_GetTicks(); - sdl_bitblt_to_screen(min_x, min_y, max_x - min_x, max_y - min_y); - after = SDL_GetTicks(); SDL_Rect r; r.x = min_x; r.y = min_y; r.w = max_x - min_x; r.h = max_y - min_y; if (sdl_pixelscale == 1) { - SDL_BlitSurface(sdl_buffersurface, &r, sdl_windowsurface, &r); + sdl_bitblt_to_window_surface(r.x, r.y, r.w, r.h); SDL_UpdateWindowSurfaceRects(sdl_window, &r, 1); } else { SDL_Rect s; @@ -545,13 +575,15 @@ void sdl_update_display_surfaces() { s.y = r.y * sdl_pixelscale; s.w = r.w * sdl_pixelscale; s.h = r.h * sdl_pixelscale; + sdl_bitblt_to_screen(r.x, r.y, r.w, r.h); SDL_BlitScaled(sdl_buffersurface, &r, sdl_windowsurface, &s); SDL_UpdateWindowSurfaceRects(sdl_window, &s, 1); } - min_x = 0; - max_x = sdl_displaywidth; - min_y = 0; - max_y = sdl_displayheight; + min_x = sdl_displaywidth; + max_x = 0; + min_y = sdl_displayheight; + max_y = 0; + should_update_texture = 0; after = SDL_GetTicks(); last_draw = this_draw; // printf("surface update took %dms\n", after - before); From ca7f09bb6d04043c22056b692de0f56b7e01b738 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 15 Nov 2021 18:34:01 -0800 Subject: [PATCH 42/68] Minor changes to add missing file for SDL and set no extensions for C compiler Sets CMAKE_C_EXTENSIONS to OFF as we wish to use c99 *not* gnu99 Adds inc/sdldefs.h to SDL header files list --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2285b48..618adf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.15) SET(CMAKE_EXPORT_COMPILE_COMMANDS ON) SET(CMAKE_C_STANDARD 99) -# SET(CMAKE_C_EXTENSIONS OFF) # actually use c99, *not* gnu99 +SET(CMAKE_C_EXTENSIONS OFF) # actually use c99, *not* gnu99 IF(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_COMPILER_ID MATCHES "GNU") IF(UNIX AND CMAKE_GENERATOR STREQUAL "Ninja") @@ -106,6 +106,7 @@ IF(MAIKO_DISPLAY_SDL) src/sdl.c ) SET(MAIKO_DISPLAY_SDL_HDRS + inc/sdldefs.h ) ENDIF() From 0600a034b64a805cf63f69c219d0631278816caa Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 15 Nov 2021 18:38:16 -0800 Subject: [PATCH 43/68] Speedups in texture based display path Instead of checking "do_invert" at each pixel assignment, assign the foreground and background colors appropriately when responding to the (VIDEOCOLOR x) call. Modify sdl_bitblt_to_texture() so that it does less arithmetic in the inner loop, including using a table of masks rather than computing 1<<n on each pixel. Modify sdl_bitblt_to_texture() so that it accesses the Lisp display region 16-bits at a time with the correct ordering for whether we are on a byte-swapped system or not. --- src/sdl.c | 111 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 62 insertions(+), 49 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 6fd6649..6dc897c 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -3,6 +3,7 @@ #include <assert.h> #include "sdldefs.h" #include "lispemul.h" +#include "lsptypes.h" #include "miscstat.h" #include "keyboard.h" #include "lspglob.h" // for IOPage @@ -21,6 +22,8 @@ static SDL_Surface *sdl_buffersurface = NULL; #endif static Uint32 sdl_white; static Uint32 sdl_black; +static Uint32 sdl_foreground; +static Uint32 sdl_background; static int sdl_bytesperpixel; static SDL_PixelFormat *sdl_pixelformat; static int buffer_size = 0; @@ -145,9 +148,6 @@ int sdl_windowwidth = 0; int sdl_windowheight = 0; // each pixel is shown as this many pixels int sdl_pixelscale = 0; -// if set, invert video (white on black) -static int do_invert = 0; - extern DLword *EmKbdAd068K, *EmKbdAd168K, *EmKbdAd268K, *EmKbdAd368K, *EmKbdAd468K, *EmKbdAd568K, *EmRealUtilin68K; extern DLword *CTopKeyevent; @@ -310,46 +310,46 @@ void sdl_setCursor(int hot_x, int hot_y) { #if defined(SDLRENDERING) void sdl_bitblt_to_texture(int _x, int _y, int _w, int _h) { // printf("bitblt(%d, %d, %d, %d)\n", _x, _y, _w, _h); - Uint32 *dr = (Uint32*)DisplayRegion68k; - void *tbuffer; - int tpitch; - int before = SDL_GetTicks(); - int width = sdl_displaywidth; - int height = sdl_displayheight; - int bpw = 8 * sizeof(Uint32); - int pitch = sdl_displaywidth / bpw; - int xlimit = (_x + _w + bpw - 1) / bpw; - int ylimit = _y + _h; - SDL_Rect r; - // if we are to avoid dealing with partial words in the update we must calculate the - // rounded down x starting bit position and the rounded up x ending bit position and - // lock that region - r.x = (_x / bpw) * bpw; - r.w = ((_x + _w + bpw - 1) / bpw * bpw) - r.x; - r.y = _y; - r.h = _h; - // printf("locking %d %d %d %d\n", r.x, r.y, r.w, r.h); - SDL_LockTexture(sdl_texture, &r, &tbuffer, &tpitch); - for(int y = _y; y < ylimit; y++) { // for each line - for(int x = _x / bpw; x < xlimit; x++) { // for each word within line - int w = dr[y * pitch + x]; - int thex = x * bpw; - for(int b = 0; b < bpw; b++) { // for each bit within word - // printf("%d/%d %d\n", x, y, b); - uint32_t px = 0; - if(w & (1 << (bpw - 1 - b))) { - px = do_invert ? sdl_white : sdl_black; - } else { - px = do_invert ? sdl_black : sdl_white; - } - //printf("px is %x\n", px); - int pxindex = ((y - _y) * (tpitch / sdl_bytesperpixel)) + ((x - (_x / bpw)) * bpw) + b; - ((Uint32 *)tbuffer)[pxindex] = px; + // Uint32 *dr = (Uint32 *)DisplayRegion68k; + static const DLword mask[16] = {1<<15, 1<<14, 1<<13, 1<<12, 1<<11, 1<<10, 1<<9, 1<<8, + 1<<7, 1<<6, 1<<5, 1<<4, 1<<3, 1<<2, 1<<1, 1<<0}; + DLword *dr = DisplayRegion68k; + void *destbuffer; + int destpitchbytes; + int destpitchpixels; + // const int bitsperword = 8 * sizeof(Uint32); + const int bitsperword = 8 * sizeof(DLword); + int sourcepitchwords = sdl_displaywidth / bitsperword; + int xstart = _x / bitsperword; + int xlimit = (_x + _w + bitsperword - 1) / bitsperword; + int ystart = _y * sourcepitchwords; + int ylimit = (_y + _h) * sourcepitchwords; + SDL_Rect destrect; + /* printf("_x %d _y %d _w %d _h %d; xstart %d xlimit %d ystart %d ylimit %d sourcepitchwords %d\n", + _x, _y, _w, _h, xstart, xlimit, ystart, ylimit, sourcepitchwords); */ + // Avoid dealing with partial words in the update by stretching the source rectangle + // left and right to cover complete units (32-bit words) and lock the corresponding + // region in the texture + destrect.x = (_x / bitsperword) * bitsperword; + destrect.w = ((_x + _w + bitsperword - 1) / bitsperword * bitsperword) - destrect.x; + destrect.y = _y; + destrect.h = _h; + SDL_LockTexture(sdl_texture, &destrect, &destbuffer, &destpitchbytes); + destpitchpixels = destpitchbytes / sdl_bytesperpixel; + int dy = 0; + // for each line in the source image + for(int sy = ystart; sy < ylimit ; sy += sourcepitchwords, dy += destpitchpixels) { + // for each word in the line + int dx = 0; + for(int sx = xstart; sx < xlimit; sx++, dx += bitsperword) { + int w = GETBASEWORD(dr, sy + sx); + // for each bit in the word + for(int b = 0; b < bitsperword; b++) { + ((Uint32 *)destbuffer)[dy + dx + b] = (w & mask[b]) ? sdl_foreground : sdl_background; } } } SDL_UnlockTexture(sdl_texture); - int after = SDL_GetTicks(); } #else void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { @@ -364,6 +364,7 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { int xlimit = (_x + _w + bpw - 1) / bpw; int ylimit = _y + _h; for(int y = _y; y < ylimit; y++) { + int they = y * sdl_displaywidth; for(int x = _x / bpw; x < xlimit; x++) { int w = dr[y * pitch + x]; int thex = x * bpw; @@ -371,12 +372,12 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { //printf("%d/%d %d\n", x, y, b); uint32_t px = 0; if(w & (1 << (bpw - 1 - b))) { - px = do_invert ? sdl_white : sdl_black; + px = sdl_foreground; } else { - px = do_invert ? sdl_black : sdl_white; + px = sdl_background; } //printf("px is %x\n", px); - int pxindex = (y * sdl_displaywidth) + thex + b; + int pxindex = they + thex + b; assert(pxindex >= 0 && pxindex < buffer_size); ((Uint32 *)buffer)[pxindex] = px; } @@ -397,6 +398,7 @@ void sdl_bitblt_to_window_surface(int _x, int _y, int _w, int _h) { int xlimit = (_x + _w + bpw - 1) / bpw; int ylimit = _y + _h; for(int y = _y; y < ylimit; y++) { + int they = y * sdl_displaywidth; for(int x = _x / bpw; x < xlimit; x++) { int w = dr[y * pitch + x]; int thex = x * bpw; @@ -404,12 +406,12 @@ void sdl_bitblt_to_window_surface(int _x, int _y, int _w, int _h) { //printf("%d/%d %d\n", x, y, b); uint32_t px = 0; if(w & (1 << (bpw - 1 - b))) { - px = do_invert ? sdl_white : sdl_black; + px = sdl_foreground; } else { - px = do_invert ? sdl_black : sdl_white; + px = sdl_background; } //printf("px is %x\n", px); - int pxindex = (y * sdl_displaywidth) + thex + b; + int pxindex = they + thex + b; assert(pxindex >= 0 && pxindex < buffer_size); ((Uint32 *)sdl_windowsurface->pixels)[pxindex] = px; } @@ -505,7 +507,14 @@ static int last_keystate[512] = { 0 }; /* } */ /* } */ void sdl_set_invert(int flag) { - do_invert = flag; + if (flag) { + sdl_foreground = sdl_white; + sdl_background = sdl_black; + } else { + sdl_foreground = sdl_black; + sdl_background = sdl_white; + } + sdl_notify_damage(0, 0, sdl_displaywidth, sdl_displayheight); } void sdl_setMousePosition(int x, int y) { SDL_WarpMouseInWindow(sdl_window, x, y); @@ -518,7 +527,6 @@ void sdl_update_display_rendering() { if(should_update_texture) { before = SDL_GetTicks(); sdl_bitblt_to_texture(min_x, min_y, max_x - min_x, max_y - min_y); - // SDL_UpdateTexture(sdl_texture, NULL, buffer, sdl_displaywidth * sdl_bytesperpixel); after = SDL_GetTicks(); // printf("UpdateTexture took %dms\n", after - before); should_update_texture = 0; @@ -530,7 +538,7 @@ void sdl_update_display_rendering() { before = SDL_GetTicks(); - if(this_draw - last_draw > 16) { + if(this_draw - last_draw > 10) { before = SDL_GetTicks(); //SDL_RenderClear(sdl_renderer); SDL_Rect r; @@ -717,15 +725,18 @@ int init_SDL(char *windowtitle, int w, int h, int s) { printf("SDL Error: %s\n", SDL_GetError()); return 3; } + SDL_SetRenderDrawColor(sdl_renderer, 127, 127, 127, 255); SDL_RenderClear(sdl_renderer); + SDL_RenderPresent(sdl_renderer); SDL_GetRendererInfo(sdl_renderer, &sdl_rendererinfo); - // SDL_SetRenderDrawColor(sdl_renderer, 50, 50, 50, 255); SDL_RenderSetScale(sdl_renderer, 1.0, 1.0); printf("Creating texture...\n"); sdl_pixelformat = SDL_AllocFormat(sdl_rendererinfo.texture_formats[0]); sdl_texture = SDL_CreateTexture(sdl_renderer, sdl_pixelformat->format, SDL_TEXTUREACCESS_STREAMING, width, height); sdl_black = SDL_MapRGB(sdl_pixelformat, 0, 0, 0); sdl_white = SDL_MapRGB(sdl_pixelformat, 255, 255, 255); + sdl_foreground = sdl_black; + sdl_background = sdl_white; sdl_bytesperpixel = sdl_pixelformat->BytesPerPixel; #else printf("Creating window surface and buffer surface\n"); @@ -733,6 +744,8 @@ int init_SDL(char *windowtitle, int w, int h, int s) { sdl_pixelformat = sdl_windowsurface->format; sdl_black = SDL_MapRGB(sdl_pixelformat, 0, 0, 0); sdl_white = SDL_MapRGB(sdl_pixelformat, 255, 255, 255); + sdl_foreground = sdl_black; + sdl_background = sdl_white; sdl_bytesperpixel = sdl_pixelformat->BytesPerPixel; buffer_size = width * height * sdl_bytesperpixel; buffer = malloc(buffer_size); From d2700aa6370a06c2d5881ab6ac8b7d39f00e349d Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 15 Nov 2021 18:57:51 -0800 Subject: [PATCH 44/68] Some systems require C99 extensions to get sigset utilities. --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 618adf2..8c81460 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,6 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.15) SET(CMAKE_EXPORT_COMPILE_COMMANDS ON) SET(CMAKE_C_STANDARD 99) -SET(CMAKE_C_EXTENSIONS OFF) # actually use c99, *not* gnu99 IF(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_COMPILER_ID MATCHES "GNU") IF(UNIX AND CMAKE_GENERATOR STREQUAL "Ninja") From 78bcb188a86611f3c8419ba9f0ab61d58fa7c17c Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Wed, 17 Nov 2021 12:27:13 -0800 Subject: [PATCH 45/68] Take pixelscale into account when repositioning cursor --- src/sdl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl.c b/src/sdl.c index 6dc897c..e957006 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -517,7 +517,7 @@ void sdl_set_invert(int flag) { sdl_notify_damage(0, 0, sdl_displaywidth, sdl_displayheight); } void sdl_setMousePosition(int x, int y) { - SDL_WarpMouseInWindow(sdl_window, x, y); + SDL_WarpMouseInWindow(sdl_window, x * sdl_pixelscale, y * sdl_pixelscale); } #if defined(SDLRENDERING) void sdl_update_display_rendering() { From 878168f4f5870cb13ae06b85d0544bf6c77a4e4f Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Sat, 20 Nov 2021 18:26:50 -0800 Subject: [PATCH 46/68] Implement bitblt to texture at bit level rather than word level, some cleanup sdl_bitblt_to_texture2 is an experiment, parallel to sdl_bitblt_to_texture that only moves as many bits as are required, rather than rounding down(up) to the nearest (16-bit) word boundary for the start(end) of the line. Introduces some name changes to make things a little clearer. --- src/sdl.c | 176 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 101 insertions(+), 75 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index e957006..d639709 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -19,6 +19,8 @@ static SDL_Texture *sdl_texture = NULL; #else static SDL_Surface *sdl_windowsurface = NULL; static SDL_Surface *sdl_buffersurface = NULL; +static int buffer_size = 0; +static void *buffer = NULL; #endif static Uint32 sdl_white; static Uint32 sdl_black; @@ -26,8 +28,6 @@ static Uint32 sdl_foreground; static Uint32 sdl_background; static int sdl_bytesperpixel; static SDL_PixelFormat *sdl_pixelformat; -static int buffer_size = 0; -static void *buffer = NULL; extern void kb_trans(u_short keycode, u_short upflg); extern int error(const char *s); @@ -216,7 +216,7 @@ static int min(int a, int b) { return b; } -static int should_update_texture = 0; +static int display_update_needed = 0; static int min_x = 0; static int min_y = 0; @@ -231,7 +231,7 @@ void sdl_notify_damage(int x, int y, int w, int h) { max_x = min(x + w, sdl_displaywidth - 1); if(y + h > max_y) max_y = min(y + h, sdl_displayheight - 1); - should_update_texture = 1; + display_update_needed = 1; } /* a simple linked list to remember generated cursors @@ -286,6 +286,7 @@ static SDL_Cursor *sdl_getOrAllocateCursor(Uint8 cursor[32], int hot_x, int hot_ /* * Read a cursor bitmap from lisp. Try to find a cached cursor, then use that. * Use HOT_X and HOT_Y as the cursor hotspot. + * XXX: needs to deal with sdl_pixelscale > 1, and where is the hotspot? */ void sdl_setCursor(int hot_x, int hot_y) { DLword *newbm = ((DLword *)(IOPage->dlcursorbitmap)); @@ -310,51 +311,86 @@ void sdl_setCursor(int hot_x, int hot_y) { #if defined(SDLRENDERING) void sdl_bitblt_to_texture(int _x, int _y, int _w, int _h) { // printf("bitblt(%d, %d, %d, %d)\n", _x, _y, _w, _h); - // Uint32 *dr = (Uint32 *)DisplayRegion68k; + // Uint32 *src = (Uint32 *)DisplayRegion68k; static const DLword mask[16] = {1<<15, 1<<14, 1<<13, 1<<12, 1<<11, 1<<10, 1<<9, 1<<8, 1<<7, 1<<6, 1<<5, 1<<4, 1<<3, 1<<2, 1<<1, 1<<0}; - DLword *dr = DisplayRegion68k; - void *destbuffer; - int destpitchbytes; - int destpitchpixels; - // const int bitsperword = 8 * sizeof(Uint32); + DLword *src = DisplayRegion68k; + void *dst; + int dstpitchbytes; + int dstpitchpixels; const int bitsperword = 8 * sizeof(DLword); int sourcepitchwords = sdl_displaywidth / bitsperword; int xstart = _x / bitsperword; int xlimit = (_x + _w + bitsperword - 1) / bitsperword; int ystart = _y * sourcepitchwords; int ylimit = (_y + _h) * sourcepitchwords; - SDL_Rect destrect; + SDL_Rect dstrect; /* printf("_x %d _y %d _w %d _h %d; xstart %d xlimit %d ystart %d ylimit %d sourcepitchwords %d\n", _x, _y, _w, _h, xstart, xlimit, ystart, ylimit, sourcepitchwords); */ // Avoid dealing with partial words in the update by stretching the source rectangle - // left and right to cover complete units (32-bit words) and lock the corresponding + // left and right to cover complete units and lock the corresponding // region in the texture - destrect.x = (_x / bitsperword) * bitsperword; - destrect.w = ((_x + _w + bitsperword - 1) / bitsperword * bitsperword) - destrect.x; - destrect.y = _y; - destrect.h = _h; - SDL_LockTexture(sdl_texture, &destrect, &destbuffer, &destpitchbytes); - destpitchpixels = destpitchbytes / sdl_bytesperpixel; + dstrect.x = (_x / bitsperword) * bitsperword; + dstrect.w = ((_x + _w + bitsperword - 1) / bitsperword * bitsperword) - dstrect.x; + dstrect.y = _y; + dstrect.h = _h; + SDL_LockTexture(sdl_texture, &dstrect, &dst, &dstpitchbytes); + dstpitchpixels = dstpitchbytes / sdl_bytesperpixel; int dy = 0; // for each line in the source image - for(int sy = ystart; sy < ylimit ; sy += sourcepitchwords, dy += destpitchpixels) { + for(int sy = ystart; sy < ylimit ; sy += sourcepitchwords, dy += dstpitchpixels) { // for each word in the line int dx = 0; for(int sx = xstart; sx < xlimit; sx++, dx += bitsperword) { - int w = GETBASEWORD(dr, sy + sx); + int srcw = GETBASEWORD(src, sy + sx); // for each bit in the word for(int b = 0; b < bitsperword; b++) { - ((Uint32 *)destbuffer)[dy + dx + b] = (w & mask[b]) ? sdl_foreground : sdl_background; + ((Uint32 *)dst)[dy + dx + b] = (srcw & mask[b]) ? sdl_foreground : sdl_background; + } + } + } + SDL_UnlockTexture(sdl_texture); +} +void sdl_bitblt_to_texture2(int _x, int _y, int _w, int _h) { + static const int mask[16] = {1<<15, 1<<14, 1<<13, 1<<12, 1<<11, 1<<10, 1<<9, 1<<8, + 1<<7, 1<<6, 1<<5, 1<<4, 1<<3, 1<<2, 1<<1, 1<<0}; + DLword *src = DisplayRegion68k; + void *dst; + int dstpitchbytes; + int dstpitchpixels; + const int bitsperword = 8 * sizeof(DLword); + int sourcepitchwords = sdl_displaywidth / bitsperword; + int xstart = _x / bitsperword; // "word" index of first accessed word in line + int xstartb = _x % bitsperword; // bit within word + int xlimit = (_x + _w + bitsperword - 1) / bitsperword; // word index + int ystart = _y * sourcepitchwords; + int ylimit = (_y + _h) * sourcepitchwords; + SDL_Rect dstrect = {.x = _x, .y = _y, .w = _w, .h = _h}; + SDL_LockTexture(sdl_texture, &dstrect, &dst, &dstpitchbytes); + dstpitchpixels = dstpitchbytes / sdl_bytesperpixel; + int dy = 0; + // for each line in the source image + for(int sy = ystart; sy < ylimit ; sy += sourcepitchwords, dy += dstpitchpixels) { + int dx = 0; + int sx = xstart; + int b = xstartb; + int srcw = GETBASEWORD(src, sy + sx); + // for each pixel within the dstination region line + for (int dx = 0; dx < _w; dx++) { + ((Uint32 *)dst)[dy + dx] = (srcw & mask[b]) ? sdl_foreground : sdl_background; + if (++b == bitsperword) { + b = 0; + sx++; + srcw = GETBASEWORD(src, sy + sx); } } } SDL_UnlockTexture(sdl_texture); } #else -void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { +void sdl_bitblt_to_buffer(int _x, int _y, int _w, int _h) { // printf("bitblt(%d, %d, %d, %d)\n", _x, _y, _w, _h); - Uint32 *dr = (Uint32*)DisplayRegion68k; + Uint32 *src = (Uint32*)DisplayRegion68k; int before = SDL_GetTicks(); int width = sdl_displaywidth; @@ -366,12 +402,12 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { for(int y = _y; y < ylimit; y++) { int they = y * sdl_displaywidth; for(int x = _x / bpw; x < xlimit; x++) { - int w = dr[y * pitch + x]; + int srcw = src[y * pitch + x]; int thex = x * bpw; for(int b = 0; b < bpw; b++) { //printf("%d/%d %d\n", x, y, b); uint32_t px = 0; - if(w & (1 << (bpw - 1 - b))) { + if(srcw & (1 << (bpw - 1 - b))) { px = sdl_foreground; } else { px = sdl_background; @@ -387,37 +423,31 @@ void sdl_bitblt_to_screen(int _x, int _y, int _w, int _h) { // printf("bitblting took %dms\n", after - before); } void sdl_bitblt_to_window_surface(int _x, int _y, int _w, int _h) { - // printf("bitblt(%d, %d, %d, %d)\n", _x, _y, _w, _h); - Uint32 *dr = (Uint32*)DisplayRegion68k; - - int before = SDL_GetTicks(); - int width = sdl_displaywidth; - int height = sdl_displayheight; - int bpw = 8 * sizeof(Uint32); - int pitch = sdl_displaywidth / bpw; - int xlimit = (_x + _w + bpw - 1) / bpw; - int ylimit = _y + _h; - for(int y = _y; y < ylimit; y++) { - int they = y * sdl_displaywidth; - for(int x = _x / bpw; x < xlimit; x++) { - int w = dr[y * pitch + x]; - int thex = x * bpw; - for(int b = 0; b < bpw; b++) { - //printf("%d/%d %d\n", x, y, b); - uint32_t px = 0; - if(w & (1 << (bpw - 1 - b))) { - px = sdl_foreground; - } else { - px = sdl_background; - } - //printf("px is %x\n", px); - int pxindex = they + thex + b; - assert(pxindex >= 0 && pxindex < buffer_size); - ((Uint32 *)sdl_windowsurface->pixels)[pxindex] = px; + static const DLword mask[16] = {1<<15, 1<<14, 1<<13, 1<<12, 1<<11, 1<<10, 1<<9, 1<<8, + 1<<7, 1<<6, 1<<5, 1<<4, 1<<3, 1<<2, 1<<1, 1<<0}; + DLword *src = DisplayRegion68k; + Uint32 *dst = (Uint32 *)sdl_windowsurface->pixels; + int dstpitchbytes = sdl_windowsurface->pitch; + int dstpitchpixels = dstpitchbytes / sdl_bytesperpixel; + const int bitsperword = 8 * sizeof(DLword); + int sourcepitchwords = sdl_displaywidth / bitsperword; + int xstart = _x / bitsperword; + int xlimit = (_x + _w + bitsperword - 1) / bitsperword; + int ystart = _y * sourcepitchwords; + int ylimit = (_y + _h) * sourcepitchwords; + int dy = _y * dstpitchpixels; + // for each line in the source image + for(int sy = ystart; sy < ylimit ; sy += sourcepitchwords, dy += dstpitchpixels) { + // for each word in the line + int dx = (_x / bitsperword) * bitsperword; + for(int sx = xstart; sx < xlimit; sx++, dx+=bitsperword) { + int srcw = GETBASEWORD(src, sy + sx); + // for each bit in the word + for(int b = 0; b < bitsperword; b++) { + ((Uint32 *)dst)[dy + dx + b] = (srcw & mask[b]) ? sdl_foreground : sdl_background; } } } - int after = SDL_GetTicks(); // printf("bitblting took %dms\n", after - before); } #endif @@ -520,25 +550,24 @@ void sdl_setMousePosition(int x, int y) { SDL_WarpMouseInWindow(sdl_window, x * sdl_pixelscale, y * sdl_pixelscale); } #if defined(SDLRENDERING) -void sdl_update_display_rendering() { +void sdl_update_display() { int before = 0; int after = 0; - if(should_update_texture) { - before = SDL_GetTicks(); - sdl_bitblt_to_texture(min_x, min_y, max_x - min_x, max_y - min_y); - after = SDL_GetTicks(); - // printf("UpdateTexture took %dms\n", after - before); - should_update_texture = 0; - min_x = sdl_displaywidth; - min_y = sdl_displayheight; - max_x = max_y = 0; - } int this_draw = SDL_GetTicks(); + if(this_draw - last_draw <= 16) return; + + sdl_bitblt_to_texture2(min_x, min_y, max_x - min_x, max_y - min_y); + after = SDL_GetTicks(); + // printf("UpdateTexture took %dms\n", after - before); + display_update_needed = 0; + min_x = sdl_displaywidth; + min_y = sdl_displayheight; + max_x = max_y = 0; + before = SDL_GetTicks(); - if(this_draw - last_draw > 10) { before = SDL_GetTicks(); //SDL_RenderClear(sdl_renderer); SDL_Rect r; @@ -557,16 +586,15 @@ void sdl_update_display_rendering() { last_draw = this_draw; after = SDL_GetTicks(); // printf("rendering took %dms\n", after - before); - } } #else -void sdl_update_display_surfaces() { +void sdl_update_display() { int before = 0; int after = 0; int this_draw = SDL_GetTicks(); /* check if there's anything to do and sufficient time has elapsed */ - if (!should_update_texture || this_draw - last_draw <= 16) { + if (!display_update_needed || this_draw - last_draw <= 16) { return; } SDL_Rect r; @@ -583,7 +611,7 @@ void sdl_update_display_surfaces() { s.y = r.y * sdl_pixelscale; s.w = r.w * sdl_pixelscale; s.h = r.h * sdl_pixelscale; - sdl_bitblt_to_screen(r.x, r.y, r.w, r.h); + sdl_bitblt_to_buffer(r.x, r.y, r.w, r.h); SDL_BlitScaled(sdl_buffersurface, &r, sdl_windowsurface, &s); SDL_UpdateWindowSurfaceRects(sdl_window, &s, 1); } @@ -591,7 +619,7 @@ void sdl_update_display_surfaces() { max_x = 0; min_y = sdl_displayheight; max_y = 0; - should_update_texture = 0; + display_update_needed = 0; after = SDL_GetTicks(); last_draw = this_draw; // printf("surface update took %dms\n", after - before); @@ -685,11 +713,9 @@ void process_SDLevents() { printf("other event type: %d\n", event.type); } } -#if defined(SDLRENDERING) - sdl_update_display_rendering(); -#else - sdl_update_display_surfaces(); -#endif + if (display_update_needed) { + sdl_update_display(); + } } int init_SDL(char *windowtitle, int w, int h, int s) { From b166d36ec482095202d5210a655a5d343311434a Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Tue, 23 Nov 2021 17:45:01 -0800 Subject: [PATCH 47/68] Cleanup, strip some debug/performance code in display update, minor formatting --- src/sdl.c | 134 ++++++++++-------------------------------------------- 1 file changed, 23 insertions(+), 111 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index d639709..632aea2 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -1,6 +1,7 @@ #include <SDL.h> #include <SDL_keycode.h> #include <assert.h> +#include <limits.h> #include "sdldefs.h" #include "lispemul.h" #include "lsptypes.h" @@ -9,7 +10,7 @@ #include "lspglob.h" // for IOPage #include "display.h" // for CURSORHEIGHT, DisplayRegion68k -#define SDLRENDERING 1 +// #define SDLRENDERING 1 static SDL_Window *sdl_window = NULL; #if defined(SDLRENDERING) @@ -139,6 +140,8 @@ int keymap[] = { 108, SDLK_F12, -1, -1 }; +static const DLword bitmask[16] = {1<<15, 1<<14, 1<<13, 1<<12, 1<<11, 1<<10, 1<<9, 1<<8, + 1<<7, 1<<6, 1<<5, 1<<4, 1<<3, 1<<2, 1<<1, 1<<0}; // all of the following are overwritten, the values here are irrelevant defaults! // actual size of the lisp display in pixels. int sdl_displaywidth = 0; @@ -218,8 +221,8 @@ static int min(int a, int b) { static int display_update_needed = 0; -static int min_x = 0; -static int min_y = 0; +static int min_x = INT_MAX; +static int min_y = INT_MAX; static int max_x = 0; static int max_y = 0; void sdl_notify_damage(int x, int y, int w, int h) { @@ -310,10 +313,6 @@ void sdl_setCursor(int hot_x, int hot_y) { } #if defined(SDLRENDERING) void sdl_bitblt_to_texture(int _x, int _y, int _w, int _h) { - // printf("bitblt(%d, %d, %d, %d)\n", _x, _y, _w, _h); - // Uint32 *src = (Uint32 *)DisplayRegion68k; - static const DLword mask[16] = {1<<15, 1<<14, 1<<13, 1<<12, 1<<11, 1<<10, 1<<9, 1<<8, - 1<<7, 1<<6, 1<<5, 1<<4, 1<<3, 1<<2, 1<<1, 1<<0}; DLword *src = DisplayRegion68k; void *dst; int dstpitchbytes; @@ -325,13 +324,11 @@ void sdl_bitblt_to_texture(int _x, int _y, int _w, int _h) { int ystart = _y * sourcepitchwords; int ylimit = (_y + _h) * sourcepitchwords; SDL_Rect dstrect; - /* printf("_x %d _y %d _w %d _h %d; xstart %d xlimit %d ystart %d ylimit %d sourcepitchwords %d\n", - _x, _y, _w, _h, xstart, xlimit, ystart, ylimit, sourcepitchwords); */ // Avoid dealing with partial words in the update by stretching the source rectangle // left and right to cover complete units and lock the corresponding // region in the texture - dstrect.x = (_x / bitsperword) * bitsperword; - dstrect.w = ((_x + _w + bitsperword - 1) / bitsperword * bitsperword) - dstrect.x; + dstrect.x = xstart * bitsperword; + dstrect.w = (xlimit * bitsperword) - dstrect.x; dstrect.y = _y; dstrect.h = _h; SDL_LockTexture(sdl_texture, &dstrect, &dst, &dstpitchbytes); @@ -345,15 +342,13 @@ void sdl_bitblt_to_texture(int _x, int _y, int _w, int _h) { int srcw = GETBASEWORD(src, sy + sx); // for each bit in the word for(int b = 0; b < bitsperword; b++) { - ((Uint32 *)dst)[dy + dx + b] = (srcw & mask[b]) ? sdl_foreground : sdl_background; + ((Uint32 *)dst)[dy + dx + b] = (srcw & bitmask[b]) ? sdl_foreground : sdl_background; } } } SDL_UnlockTexture(sdl_texture); } void sdl_bitblt_to_texture2(int _x, int _y, int _w, int _h) { - static const int mask[16] = {1<<15, 1<<14, 1<<13, 1<<12, 1<<11, 1<<10, 1<<9, 1<<8, - 1<<7, 1<<6, 1<<5, 1<<4, 1<<3, 1<<2, 1<<1, 1<<0}; DLword *src = DisplayRegion68k; void *dst; int dstpitchbytes; @@ -377,7 +372,7 @@ void sdl_bitblt_to_texture2(int _x, int _y, int _w, int _h) { int srcw = GETBASEWORD(src, sy + sx); // for each pixel within the dstination region line for (int dx = 0; dx < _w; dx++) { - ((Uint32 *)dst)[dy + dx] = (srcw & mask[b]) ? sdl_foreground : sdl_background; + ((Uint32 *)dst)[dy + dx] = (srcw & bitmask[b]) ? sdl_foreground : sdl_background; if (++b == bitsperword) { b = 0; sx++; @@ -389,10 +384,7 @@ void sdl_bitblt_to_texture2(int _x, int _y, int _w, int _h) { } #else void sdl_bitblt_to_buffer(int _x, int _y, int _w, int _h) { - // printf("bitblt(%d, %d, %d, %d)\n", _x, _y, _w, _h); Uint32 *src = (Uint32*)DisplayRegion68k; - - int before = SDL_GetTicks(); int width = sdl_displaywidth; int height = sdl_displayheight; int bpw = 8 * sizeof(Uint32); @@ -405,26 +397,20 @@ void sdl_bitblt_to_buffer(int _x, int _y, int _w, int _h) { int srcw = src[y * pitch + x]; int thex = x * bpw; for(int b = 0; b < bpw; b++) { - //printf("%d/%d %d\n", x, y, b); uint32_t px = 0; if(srcw & (1 << (bpw - 1 - b))) { px = sdl_foreground; } else { px = sdl_background; } - //printf("px is %x\n", px); int pxindex = they + thex + b; assert(pxindex >= 0 && pxindex < buffer_size); ((Uint32 *)buffer)[pxindex] = px; } } } - int after = SDL_GetTicks(); - // printf("bitblting took %dms\n", after - before); } void sdl_bitblt_to_window_surface(int _x, int _y, int _w, int _h) { - static const DLword mask[16] = {1<<15, 1<<14, 1<<13, 1<<12, 1<<11, 1<<10, 1<<9, 1<<8, - 1<<7, 1<<6, 1<<5, 1<<4, 1<<3, 1<<2, 1<<1, 1<<0}; DLword *src = DisplayRegion68k; Uint32 *dst = (Uint32 *)sdl_windowsurface->pixels; int dstpitchbytes = sdl_windowsurface->pitch; @@ -444,11 +430,10 @@ void sdl_bitblt_to_window_surface(int _x, int _y, int _w, int _h) { int srcw = GETBASEWORD(src, sy + sx); // for each bit in the word for(int b = 0; b < bitsperword; b++) { - ((Uint32 *)dst)[dy + dx + b] = (srcw & mask[b]) ? sdl_foreground : sdl_background; + ((Uint32 *)dst)[dy + dx + b] = (srcw & bitmask[b]) ? sdl_foreground : sdl_background; } } } - // printf("bitblting took %dms\n", after - before); } #endif static int map_key(SDL_Keycode k) { @@ -514,28 +499,7 @@ static void sdl_update_viewport(int width, int height) { #endif printf("new viewport: %d / %d\n", w, h); } -static int last_draw = 0; static int last_keystate[512] = { 0 }; -/* void handle_keyboard() { */ -/* SDL_PumpEvents(); */ -/* int numkeys; */ -/* const Uint8 *keystates = SDL_GetKeyboardState(&numkeys); */ -/* for(int i = 0; keymap[i] != -1; i+= 2) { */ -/* int keycode = keymap[i+1]; */ -/* int lk = keymap[i]; */ -/* int scancode = SDL_GetScancodeFromKey(keycode); */ -/* if(scancode < numkeys) { */ -/* int state = keystates[scancode]; */ -/* if(state != last_keystate[scancode]) { */ -/* printf("lk %d scancode %d %s <%s>\n", lk, scancode, state ? "pressed" : "released", SDL_GetKeyName(keycode)); */ -/* kb_trans(lk - KEYCODE_OFFSET, state ? FALSE : TRUE); */ -/* DoRing(); */ -/* if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; */ -/* } */ -/* last_keystate[scancode] = state; */ -/* } */ -/* } */ -/* } */ void sdl_set_invert(int flag) { if (flag) { sdl_foreground = sdl_white; @@ -551,53 +515,14 @@ void sdl_setMousePosition(int x, int y) { } #if defined(SDLRENDERING) void sdl_update_display() { - int before = 0; - int after = 0; - - int this_draw = SDL_GetTicks(); - if(this_draw - last_draw <= 16) return; - - sdl_bitblt_to_texture2(min_x, min_y, max_x - min_x, max_y - min_y); - after = SDL_GetTicks(); - // printf("UpdateTexture took %dms\n", after - before); - display_update_needed = 0; - min_x = sdl_displaywidth; - min_y = sdl_displayheight; - max_x = max_y = 0; - - before = SDL_GetTicks(); - - - before = SDL_GetTicks(); - //SDL_RenderClear(sdl_renderer); - SDL_Rect r; - r.x = 0; - r.y = 0; - r.w = min(sdl_windowwidth / sdl_pixelscale, sdl_displaywidth); - r.h = min(sdl_windowheight / sdl_pixelscale, sdl_displayheight); - SDL_Rect s; - s.x = 0; - s.y = 0; - s.w = min(sdl_windowwidth / sdl_pixelscale * sdl_pixelscale, sdl_displaywidth * sdl_pixelscale); - s.h = min(sdl_windowheight / sdl_pixelscale * sdl_pixelscale, sdl_displayheight * sdl_pixelscale); - SDL_RenderCopy(sdl_renderer, sdl_texture, &r, &s); - //SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL); - SDL_RenderPresent(sdl_renderer); - last_draw = this_draw; - after = SDL_GetTicks(); - // printf("rendering took %dms\n", after - before); + sdl_bitblt_to_texture(min_x, min_y, max_x - min_x, max_y - min_y); + SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL); + SDL_RenderPresent(sdl_renderer); } #else void sdl_update_display() { - int before = 0; - int after = 0; - int this_draw = SDL_GetTicks(); + SDL_Rect r, s; - /* check if there's anything to do and sufficient time has elapsed */ - if (!display_update_needed || this_draw - last_draw <= 16) { - return; - } - SDL_Rect r; r.x = min_x; r.y = min_y; r.w = max_x - min_x; @@ -606,7 +531,6 @@ void sdl_update_display() { sdl_bitblt_to_window_surface(r.x, r.y, r.w, r.h); SDL_UpdateWindowSurfaceRects(sdl_window, &r, 1); } else { - SDL_Rect s; s.x = r.x * sdl_pixelscale; s.y = r.y * sdl_pixelscale; s.w = r.w * sdl_pixelscale; @@ -615,20 +539,10 @@ void sdl_update_display() { SDL_BlitScaled(sdl_buffersurface, &r, sdl_windowsurface, &s); SDL_UpdateWindowSurfaceRects(sdl_window, &s, 1); } - min_x = sdl_displaywidth; - max_x = 0; - min_y = sdl_displayheight; - max_y = 0; - display_update_needed = 0; - after = SDL_GetTicks(); - last_draw = this_draw; - // printf("surface update took %dms\n", after - before); } #endif int process_events_time = 0; void process_SDLevents() { - // printf("processing events delta %dms\n", SDL_GetTicks() - process_events_time); - process_events_time = SDL_GetTicks(); SDL_Event event; while(SDL_PollEvent(&event)) { switch(event.type) { @@ -637,12 +551,11 @@ void process_SDLevents() { break; case SDL_WINDOWEVENT: switch(event.window.event) { - case SDL_WINDOWEVENT_RESIZED: { + case SDL_WINDOWEVENT_RESIZED: /* XXX: what about integer multiple of 32 requirements here? */ sdl_windowwidth = event.window.data1; sdl_windowheight = event.window.data2; sdl_update_viewport(sdl_windowwidth, sdl_windowheight); - } break; } break; @@ -670,8 +583,8 @@ void process_SDLevents() { (short)(y & 0xFFFF); DoRing(); if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; - } break; + } case SDL_MOUSEBUTTONDOWN: { int button = event.button.button; switch(button) { @@ -685,8 +598,8 @@ void process_SDLevents() { } DoRing(); if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; - } break; + } case SDL_MOUSEBUTTONUP: { int button = event.button.button; switch(button) { @@ -700,8 +613,8 @@ void process_SDLevents() { } DoRing(); if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; - } break; + } case SDL_MOUSEWHEEL: printf("mousewheel mouse %d x %d y %d direction %s\n", event.wheel.which, event.wheel.x, event.wheel.y, event.wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped"); break; @@ -715,6 +628,9 @@ void process_SDLevents() { } if (display_update_needed) { sdl_update_display(); + display_update_needed = 0; + min_x = min_y = INT_MAX; + max_x = max_y = 0; } } @@ -726,10 +642,6 @@ int init_SDL(char *windowtitle, int w, int h, int s) { sdl_displayheight = h; sdl_windowwidth = w * s; sdl_windowheight = h * s; - min_x = 0; - max_x = sdl_displaywidth; - min_y = 0; - max_y = sdl_displayheight; int width = sdl_displaywidth; int height = sdl_displayheight; printf("requested width: %d, height: %d\n", width, height); @@ -751,10 +663,10 @@ int init_SDL(char *windowtitle, int w, int h, int s) { printf("SDL Error: %s\n", SDL_GetError()); return 3; } + SDL_GetRendererInfo(sdl_renderer, &sdl_rendererinfo); SDL_SetRenderDrawColor(sdl_renderer, 127, 127, 127, 255); SDL_RenderClear(sdl_renderer); SDL_RenderPresent(sdl_renderer); - SDL_GetRendererInfo(sdl_renderer, &sdl_rendererinfo); SDL_RenderSetScale(sdl_renderer, 1.0, 1.0); printf("Creating texture...\n"); sdl_pixelformat = SDL_AllocFormat(sdl_rendererinfo.texture_formats[0]); From 355e6dfb51410ccaa356bf041388594c0ad0d726 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Tue, 23 Nov 2021 17:51:39 -0800 Subject: [PATCH 48/68] Reformat to standard with clang-format --- src/sdl.c | 277 +++++++++++++++++++++++++++--------------------------- 1 file changed, 139 insertions(+), 138 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 632aea2..806d15f 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -7,10 +7,10 @@ #include "lsptypes.h" #include "miscstat.h" #include "keyboard.h" -#include "lspglob.h" // for IOPage -#include "display.h" // for CURSORHEIGHT, DisplayRegion68k +#include "lspglob.h" // for IOPage +#include "display.h" // for CURSORHEIGHT, DisplayRegion68k -// #define SDLRENDERING 1 +#define SDLRENDERING 1 static SDL_Window *sdl_window = NULL; #if defined(SDLRENDERING) @@ -34,6 +34,7 @@ extern void kb_trans(u_short keycode, u_short upflg); extern int error(const char *s); extern int KBDEventFlg; +/* clang-format off */ int keymap[] = { 0, SDLK_5, 1, SDLK_4, @@ -140,8 +141,10 @@ int keymap[] = { 108, SDLK_F12, -1, -1 }; -static const DLword bitmask[16] = {1<<15, 1<<14, 1<<13, 1<<12, 1<<11, 1<<10, 1<<9, 1<<8, - 1<<7, 1<<6, 1<<5, 1<<4, 1<<3, 1<<2, 1<<1, 1<<0}; +/* clang-format on */ +static const DLword bitmask[16] = {1 << 15, 1 << 14, 1 << 13, 1 << 12, 1 << 11, 1 << 10, + 1 << 9, 1 << 8, 1 << 7, 1 << 6, 1 << 5, 1 << 4, + 1 << 3, 1 << 2, 1 << 1, 1 << 0}; // all of the following are overwritten, the values here are irrelevant defaults! // actual size of the lisp display in pixels. int sdl_displaywidth = 0; @@ -152,7 +155,7 @@ int sdl_windowheight = 0; // each pixel is shown as this many pixels int sdl_pixelscale = 0; extern DLword *EmKbdAd068K, *EmKbdAd168K, *EmKbdAd268K, *EmKbdAd368K, *EmKbdAd468K, *EmKbdAd568K, - *EmRealUtilin68K; + *EmRealUtilin68K; extern DLword *CTopKeyevent; extern int URaid_req; extern LispPTR *KEYBUFFERING68k; @@ -161,7 +164,7 @@ void DoRing() { DLword w, r; KBEVENT *kbevent; - do_ring: +do_ring: /* DEL is not generally present on a Mac X keyboard, Ctrl-shift-ESC would be 18496 */ if (((*EmKbdAd268K) & 2113) == 0) { /*Ctrl-shift-NEXT*/ error("****** EMERGENCY Interrupt ******"); @@ -209,13 +212,12 @@ void DoRing() { else ((RING *)CTopKeyevent)->write = w + KEYEVENTSIZE; - KBnext: +KBnext: if (*KEYBUFFERING68k == NIL) *KEYBUFFERING68k = ATOM_T; } static int min(int a, int b) { - if(a < b) - return a; + if (a < b) return a; return b; } @@ -226,14 +228,10 @@ static int min_y = INT_MAX; static int max_x = 0; static int max_y = 0; void sdl_notify_damage(int x, int y, int w, int h) { - if(x < min_x) - min_x = x; - if(y < min_y) - min_y = y; - if(x + w > max_x) - max_x = min(x + w, sdl_displaywidth - 1); - if(y + h > max_y) - max_y = min(y + h, sdl_displayheight - 1); + if (x < min_x) min_x = x; + if (y < min_y) min_y = y; + if (x + w > max_x) max_x = min(x + w, sdl_displaywidth - 1); + if (y + h > max_y) max_y = min(y + h, sdl_displayheight - 1); display_update_needed = 1; } @@ -249,8 +247,8 @@ struct CachedCursor { } *sdl_cursorlist = NULL; static int cursor_equal_p(DLword *a, DLword *b) { - for(int i = 0; i < CURSORHEIGHT; i++) - if(a[i] != b[i]) return FALSE; + for (int i = 0; i < CURSORHEIGHT; i++) + if (a[i] != b[i]) return FALSE; return TRUE; } @@ -263,17 +261,16 @@ static SDL_Cursor *sdl_getOrAllocateCursor(Uint8 cursor[32], int hot_x, int hot_ hot_y = 0; int i = 0; /* try to find the cursor by checking the full bitmap */ - DLword *bitmap = (DLword*)cursor; + DLword *bitmap = (DLword *)cursor; struct CachedCursor *clp = sdl_cursorlist; - for(; clp != NULL; clp = clp->next) { - if(cursor_equal_p(clp->bitmap, bitmap) == TRUE) break; + for (; clp != NULL; clp = clp->next) { + if (cursor_equal_p(clp->bitmap, bitmap) == TRUE) break; } - if(clp == NULL) { /* it isn't there, push on a new one to the front of the list */ + if (clp == NULL) { /* it isn't there, push on a new one to the front of the list */ clp = (struct CachedCursor *)malloc(sizeof(struct CachedCursor)); - for(int i = 0; i < CURSORHEIGHT; i++) clp->bitmap[i] = bitmap[i]; + for (int i = 0; i < CURSORHEIGHT; i++) clp->bitmap[i] = bitmap[i]; SDL_Cursor *c = SDL_CreateCursor(cursor, cursor, 16, 16, hot_x, hot_y); - if(c == NULL) - printf("ERROR creating cursor: %s\n", SDL_GetError()); + if (c == NULL) printf("ERROR creating cursor: %s\n", SDL_GetError()); clp->cursor = c; clp->next = sdl_cursorlist; sdl_cursorlist = clp; @@ -300,7 +297,7 @@ void sdl_setCursor(int hot_x, int hot_y) { /* // cursor[i] = newbm[i % 2 == 1 ? i - 1 : i + 1]; */ /* } */ /* printf("\n"); */ - Uint8 *bitmap = (Uint8*)newbm; + Uint8 *bitmap = (Uint8 *)newbm; for (int i = 0; i < 32; i++) cursor[i] = bitmap[i ^ 3]; // TODO: actually keep track of the cursors, don't just allocate a new one every time! /* for(int i = 0; i < CURSORHEIGHT * 2; i++) { */ @@ -335,13 +332,13 @@ void sdl_bitblt_to_texture(int _x, int _y, int _w, int _h) { dstpitchpixels = dstpitchbytes / sdl_bytesperpixel; int dy = 0; // for each line in the source image - for(int sy = ystart; sy < ylimit ; sy += sourcepitchwords, dy += dstpitchpixels) { + for (int sy = ystart; sy < ylimit; sy += sourcepitchwords, dy += dstpitchpixels) { // for each word in the line int dx = 0; - for(int sx = xstart; sx < xlimit; sx++, dx += bitsperword) { + for (int sx = xstart; sx < xlimit; sx++, dx += bitsperword) { int srcw = GETBASEWORD(src, sy + sx); // for each bit in the word - for(int b = 0; b < bitsperword; b++) { + for (int b = 0; b < bitsperword; b++) { ((Uint32 *)dst)[dy + dx + b] = (srcw & bitmask[b]) ? sdl_foreground : sdl_background; } } @@ -355,8 +352,8 @@ void sdl_bitblt_to_texture2(int _x, int _y, int _w, int _h) { int dstpitchpixels; const int bitsperword = 8 * sizeof(DLword); int sourcepitchwords = sdl_displaywidth / bitsperword; - int xstart = _x / bitsperword; // "word" index of first accessed word in line - int xstartb = _x % bitsperword; // bit within word + int xstart = _x / bitsperword; // "word" index of first accessed word in line + int xstartb = _x % bitsperword; // bit within word int xlimit = (_x + _w + bitsperword - 1) / bitsperword; // word index int ystart = _y * sourcepitchwords; int ylimit = (_y + _h) * sourcepitchwords; @@ -365,7 +362,7 @@ void sdl_bitblt_to_texture2(int _x, int _y, int _w, int _h) { dstpitchpixels = dstpitchbytes / sdl_bytesperpixel; int dy = 0; // for each line in the source image - for(int sy = ystart; sy < ylimit ; sy += sourcepitchwords, dy += dstpitchpixels) { + for (int sy = ystart; sy < ylimit; sy += sourcepitchwords, dy += dstpitchpixels) { int dx = 0; int sx = xstart; int b = xstartb; @@ -384,21 +381,21 @@ void sdl_bitblt_to_texture2(int _x, int _y, int _w, int _h) { } #else void sdl_bitblt_to_buffer(int _x, int _y, int _w, int _h) { - Uint32 *src = (Uint32*)DisplayRegion68k; + Uint32 *src = (Uint32 *)DisplayRegion68k; int width = sdl_displaywidth; int height = sdl_displayheight; int bpw = 8 * sizeof(Uint32); int pitch = sdl_displaywidth / bpw; int xlimit = (_x + _w + bpw - 1) / bpw; int ylimit = _y + _h; - for(int y = _y; y < ylimit; y++) { + for (int y = _y; y < ylimit; y++) { int they = y * sdl_displaywidth; - for(int x = _x / bpw; x < xlimit; x++) { + for (int x = _x / bpw; x < xlimit; x++) { int srcw = src[y * pitch + x]; int thex = x * bpw; - for(int b = 0; b < bpw; b++) { + for (int b = 0; b < bpw; b++) { uint32_t px = 0; - if(srcw & (1 << (bpw - 1 - b))) { + if (srcw & (1 << (bpw - 1 - b))) { px = sdl_foreground; } else { px = sdl_background; @@ -423,13 +420,13 @@ void sdl_bitblt_to_window_surface(int _x, int _y, int _w, int _h) { int ylimit = (_y + _h) * sourcepitchwords; int dy = _y * dstpitchpixels; // for each line in the source image - for(int sy = ystart; sy < ylimit ; sy += sourcepitchwords, dy += dstpitchpixels) { + for (int sy = ystart; sy < ylimit; sy += sourcepitchwords, dy += dstpitchpixels) { // for each word in the line int dx = (_x / bitsperword) * bitsperword; - for(int sx = xstart; sx < xlimit; sx++, dx+=bitsperword) { + for (int sx = xstart; sx < xlimit; sx++, dx += bitsperword) { int srcw = GETBASEWORD(src, sy + sx); // for each bit in the word - for(int b = 0; b < bitsperword; b++) { + for (int b = 0; b < bitsperword; b++) { ((Uint32 *)dst)[dy + dx + b] = (srcw & bitmask[b]) ? sdl_foreground : sdl_background; } } @@ -437,16 +434,15 @@ void sdl_bitblt_to_window_surface(int _x, int _y, int _w, int _h) { } #endif static int map_key(SDL_Keycode k) { - for(int i = 0; keymap[i] != -1; i+= 2) { - if(keymap[i+1] == k) - return keymap[i]; + for (int i = 0; keymap[i] != -1; i += 2) { + if (keymap[i + 1] == k) return keymap[i]; } return -1; } #define KEYCODE_OFFSET 0 static void handle_keydown(SDL_Keycode k, unsigned short mod) { int lk = map_key(k); - if(lk == -1) { + if (lk == -1) { printf("No mapping for key %s\n", SDL_GetKeyName(k)); } else { printf("dn %s -> lisp keycode %d (0x%x)\n", SDL_GetKeyName(k), lk, mod); @@ -457,7 +453,7 @@ static void handle_keydown(SDL_Keycode k, unsigned short mod) { } static void handle_keyup(SDL_Keycode k, unsigned short mod) { int lk = map_key(k); - if(lk == -1) { + if (lk == -1) { printf("No mapping for key %s\n", SDL_GetKeyName(k)); } else { printf("up %s -> lisp keycode %d (0x%x)\n", SDL_GetKeyName(k), lk, mod); @@ -474,7 +470,7 @@ extern MISCSTATS *MiscStats; /* bits within the EmRealUtilin word */ #define KEYSET_LEFT 8 #define KEYSET_LEFTMIDDLE 9 -#define KEYSET_MIDDLE 10 +#define KEYSET_MIDDLE 10 #define KEYSET_RIGHTMIDDLE 11 #define KEYSET_RIGHT 12 /* Mouse buttons */ @@ -484,11 +480,9 @@ extern MISCSTATS *MiscStats; static void sdl_update_viewport(int width, int height) { /* XXX: needs work */ int w = width / 32 * 32; - if(w > sdl_displaywidth * sdl_pixelscale) - w = sdl_displaywidth * sdl_pixelscale; + if (w > sdl_displaywidth * sdl_pixelscale) w = sdl_displaywidth * sdl_pixelscale; int h = height / 32 * 32; - if(h > sdl_displayheight * sdl_pixelscale) - h = sdl_displayheight * sdl_pixelscale; + if (h > sdl_displayheight * sdl_pixelscale) h = sdl_displayheight * sdl_pixelscale; SDL_Rect r; r.x = 0; r.y = 0; @@ -499,7 +493,7 @@ static void sdl_update_viewport(int width, int height) { #endif printf("new viewport: %d / %d\n", w, h); } -static int last_keystate[512] = { 0 }; +static int last_keystate[512] = {0}; void sdl_set_invert(int flag) { if (flag) { sdl_foreground = sdl_white; @@ -544,86 +538,92 @@ void sdl_update_display() { int process_events_time = 0; void process_SDLevents() { SDL_Event event; - while(SDL_PollEvent(&event)) { - switch(event.type) { - case SDL_QUIT: - printf("quitting\n"); exit(0); - break; - case SDL_WINDOWEVENT: - switch(event.window.event) { - case SDL_WINDOWEVENT_RESIZED: - /* XXX: what about integer multiple of 32 requirements here? */ - sdl_windowwidth = event.window.data1; - sdl_windowheight = event.window.data2; - sdl_update_viewport(sdl_windowwidth, sdl_windowheight); + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + printf("quitting\n"); + exit(0); + break; + case SDL_WINDOWEVENT: + switch (event.window.event) { + case SDL_WINDOWEVENT_RESIZED: + /* XXX: what about integer multiple of 32 requirements here? */ + sdl_windowwidth = event.window.data1; + sdl_windowheight = event.window.data2; + sdl_update_viewport(sdl_windowwidth, sdl_windowheight); + break; + } + break; + case SDL_KEYDOWN: + printf("dn ts: %x, type: %x, state: %x, repeat: %x, scancode: %x, sym: %x <%s>, mod: %x\n", + event.key.timestamp, event.key.type, event.key.state, event.key.repeat, + event.key.keysym.scancode, event.key.keysym.sym, + SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.mod); + if (event.key.repeat) { + /* Lisp needs to see the UP transition before the DOWN transition */ + handle_keyup(event.key.keysym.sym, event.key.keysym.mod); + } + handle_keydown(event.key.keysym.sym, event.key.keysym.mod); + break; + case SDL_KEYUP: + printf("up ts: %x, type: %x, state: %x, repeat: %x, scancode: %x, sym: %x <%s>, mod: %x\n", + event.key.timestamp, event.key.type, event.key.state, event.key.repeat, + event.key.keysym.scancode, event.key.keysym.sym, + SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.mod); + handle_keyup(event.key.keysym.sym, event.key.keysym.mod); + break; + case SDL_MOUSEMOTION: { + int x, y; + SDL_GetMouseState(&x, &y); + x /= sdl_pixelscale; + y /= sdl_pixelscale; + *CLastUserActionCell68k = MiscStats->secondstmp; + *EmCursorX68K = (*((DLword *)EmMouseX68K)) = (short)(x & 0xFFFF); + *EmCursorY68K = (*((DLword *)EmMouseY68K)) = (short)(y & 0xFFFF); + DoRing(); + if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; break; } - break; - case SDL_KEYDOWN: - printf("dn ts: %x, type: %x, state: %x, repeat: %x, scancode: %x, sym: %x <%s>, mod: %x\n", event.key.timestamp, event.key.type, event.key.state, event.key.repeat, event.key.keysym.scancode, event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.mod); - if (event.key.repeat) { - /* Lisp needs to see the UP transition before the DOWN transition */ - handle_keyup(event.key.keysym.sym, event.key.keysym.mod); + case SDL_MOUSEBUTTONDOWN: { + int button = event.button.button; + switch (button) { + case 1: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_LEFT, FALSE); break; + case 2: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_MIDDLE, FALSE); break; + case 3: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_RIGHT, FALSE); break; + case 4: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFT, FALSE); break; + case 5: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFTMIDDLE, FALSE); break; + case 6: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHT, FALSE); break; + case 7: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHTMIDDLE, FALSE); break; + } + DoRing(); + if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; + break; } - handle_keydown(event.key.keysym.sym, event.key.keysym.mod); - break; - case SDL_KEYUP: - printf("up ts: %x, type: %x, state: %x, repeat: %x, scancode: %x, sym: %x <%s>, mod: %x\n", event.key.timestamp, event.key.type, event.key.state, event.key.repeat, event.key.keysym.scancode, event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.mod); - handle_keyup(event.key.keysym.sym, event.key.keysym.mod); - break; - case SDL_MOUSEMOTION: { - int x, y; - SDL_GetMouseState(&x, &y); - x /= sdl_pixelscale; - y /= sdl_pixelscale; - *CLastUserActionCell68k = MiscStats->secondstmp; - *EmCursorX68K = (*((DLword *)EmMouseX68K)) = - (short)(x & 0xFFFF); - *EmCursorY68K = (*((DLword *)EmMouseY68K)) = - (short)(y & 0xFFFF); - DoRing(); - if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; - break; - } - case SDL_MOUSEBUTTONDOWN: { - int button = event.button.button; - switch(button) { - case 1: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_LEFT, FALSE); break; - case 2: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_MIDDLE, FALSE); break; - case 3: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_RIGHT, FALSE); break; - case 4: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFT, FALSE); break; - case 5: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFTMIDDLE, FALSE); break; - case 6: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHT, FALSE); break; - case 7: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHTMIDDLE, FALSE); break; + case SDL_MOUSEBUTTONUP: { + int button = event.button.button; + switch (button) { + case 1: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_LEFT, TRUE); break; + case 2: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_MIDDLE, TRUE); break; + case 3: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_RIGHT, TRUE); break; + case 4: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFT, TRUE); break; + case 5: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFTMIDDLE, TRUE); break; + case 6: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHT, TRUE); break; + case 7: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHTMIDDLE, TRUE); break; + } + DoRing(); + if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; + break; } - DoRing(); - if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; - break; - } - case SDL_MOUSEBUTTONUP: { - int button = event.button.button; - switch(button) { - case 1: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_LEFT, TRUE); break; - case 2: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_MIDDLE, TRUE); break; - case 3: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_RIGHT, TRUE); break; - case 4: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFT, TRUE); break; - case 5: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFTMIDDLE, TRUE); break; - case 6: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHT, TRUE); break; - case 7: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHTMIDDLE, TRUE); break; - } - DoRing(); - if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; - break; - } - case SDL_MOUSEWHEEL: - printf("mousewheel mouse %d x %d y %d direction %s\n", event.wheel.which, event.wheel.x, event.wheel.y, event.wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped"); - break; - /* case SDL_KEYMAPCHANGED: */ - /* printf("SDL_KEYMAPCHANGED\n"); break; */ - /* case SDL_TEXTINPUT: */ - /* printf("SDL_TEXTINPUT\n"); break; */ - default: - printf("other event type: %d\n", event.type); + case SDL_MOUSEWHEEL: + printf("mousewheel mouse %d x %d y %d direction %s\n", event.wheel.which, event.wheel.x, + event.wheel.y, + event.wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped"); + break; + /* case SDL_KEYMAPCHANGED: */ + /* printf("SDL_KEYMAPCHANGED\n"); break; */ + /* case SDL_TEXTINPUT: */ + /* printf("SDL_TEXTINPUT\n"); break; */ + default: printf("other event type: %d\n", event.type); } } if (display_update_needed) { @@ -645,21 +645,22 @@ int init_SDL(char *windowtitle, int w, int h, int s) { int width = sdl_displaywidth; int height = sdl_displayheight; printf("requested width: %d, height: %d\n", width, height); - if(SDL_Init(SDL_INIT_VIDEO) < 0) { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL could not be initialized. SDL_Error: %s\n", SDL_GetError()); return 1; } printf("initialised\n"); - sdl_window = SDL_CreateWindow(windowtitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, sdl_windowwidth, sdl_windowheight, 0); + sdl_window = SDL_CreateWindow(windowtitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + sdl_windowwidth, sdl_windowheight, 0); printf("Window created\n"); - if(sdl_window == NULL) { + if (sdl_window == NULL) { printf("Window could not be created. SDL_Error: %s\n", SDL_GetError()); return 2; } #if defined(SDLRENDERING) printf("Creating renderer...\n"); sdl_renderer = SDL_CreateRenderer(sdl_window, -1, SDL_RENDERER_ACCELERATED); - if(NULL == sdl_renderer) { + if (NULL == sdl_renderer) { printf("SDL Error: %s\n", SDL_GetError()); return 3; } @@ -670,7 +671,8 @@ int init_SDL(char *windowtitle, int w, int h, int s) { SDL_RenderSetScale(sdl_renderer, 1.0, 1.0); printf("Creating texture...\n"); sdl_pixelformat = SDL_AllocFormat(sdl_rendererinfo.texture_formats[0]); - sdl_texture = SDL_CreateTexture(sdl_renderer, sdl_pixelformat->format, SDL_TEXTUREACCESS_STREAMING, width, height); + sdl_texture = SDL_CreateTexture(sdl_renderer, sdl_pixelformat->format, + SDL_TEXTUREACCESS_STREAMING, width, height); sdl_black = SDL_MapRGB(sdl_pixelformat, 0, 0, 0); sdl_white = SDL_MapRGB(sdl_pixelformat, 255, 255, 255); sdl_foreground = sdl_black; @@ -687,10 +689,9 @@ int init_SDL(char *windowtitle, int w, int h, int s) { sdl_bytesperpixel = sdl_pixelformat->BytesPerPixel; buffer_size = width * height * sdl_bytesperpixel; buffer = malloc(buffer_size); - sdl_buffersurface = SDL_CreateRGBSurfaceWithFormatFrom(buffer, sdl_displaywidth, sdl_displayheight, - sdl_bytesperpixel * 8, - sdl_displaywidth * sdl_bytesperpixel, - sdl_pixelformat->format); + sdl_buffersurface = SDL_CreateRGBSurfaceWithFormatFrom( + buffer, sdl_displaywidth, sdl_displayheight, sdl_bytesperpixel * 8, + sdl_displaywidth * sdl_bytesperpixel, sdl_pixelformat->format); #endif printf("SDL initialised\n"); return 0; From a5923f42f9186c94a23953094100f4304945baa5 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Fri, 26 Nov 2021 18:02:21 -0800 Subject: [PATCH 49/68] Clean up cursor code Use the Lisp type DLword where referring to data stored by Lisp Access the extern EmCursorBitMap68K directly where appropriate. Use GETBYTE() macro to correct for endian-ness of host system when accessing the cursor bitmaps. Cached cursors can use an ordering strategy that only promotes the found item if it is third or later in the list since in normal operation Medley is highly likely to flip back-and-forth between two cursors. --- src/sdl.c | 57 +++++++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index 806d15f..f141963 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -3,6 +3,7 @@ #include <assert.h> #include <limits.h> #include "sdldefs.h" +#include "byteswapdefs.h" #include "lispemul.h" #include "lsptypes.h" #include "miscstat.h" @@ -156,6 +157,7 @@ int sdl_windowheight = 0; int sdl_pixelscale = 0; extern DLword *EmKbdAd068K, *EmKbdAd168K, *EmKbdAd268K, *EmKbdAd368K, *EmKbdAd468K, *EmKbdAd568K, *EmRealUtilin68K; +extern DLword *EmCursorBitMap68K; extern DLword *CTopKeyevent; extern int URaid_req; extern LispPTR *KEYBUFFERING68k; @@ -238,11 +240,11 @@ void sdl_notify_damage(int x, int y, int w, int h) { /* a simple linked list to remember generated cursors * because cursors don't have any identifying information * except for the actual bitmap in Lisp, just cache that. - * 16 Uint16, to give a 16x16 bitmap cursor. + * 16 DLwords, to give a 16x16 bitmap cursor. */ struct CachedCursor { struct CachedCursor *next; - DLword bitmap[CURSORHEIGHT]; + DLword EmCursorBitMap[CURSORHEIGHT]; SDL_Cursor *cursor; } *sdl_cursorlist = NULL; @@ -256,31 +258,40 @@ static int cursor_equal_p(DLword *a, DLword *b) { * Try to find cursor CURSOR on the sdl_cursorlist, if it isn't there, add it. * Return an SDL_Cursor that can be used directly. */ -static SDL_Cursor *sdl_getOrAllocateCursor(Uint8 cursor[32], int hot_x, int hot_y) { +static SDL_Cursor *sdl_getOrAllocateCursor(DLword cursor[16], int hot_x, int hot_y) { hot_x = 0; hot_y = 0; - int i = 0; + Uint8 sdl_cursor_data[32]; /* try to find the cursor by checking the full bitmap */ - DLword *bitmap = (DLword *)cursor; + struct CachedCursor *pclp = NULL; struct CachedCursor *clp = sdl_cursorlist; - for (; clp != NULL; clp = clp->next) { - if (cursor_equal_p(clp->bitmap, bitmap) == TRUE) break; + while (clp != NULL) { + if (cursor_equal_p(clp->EmCursorBitMap, cursor) == TRUE) break; + pclp = clp; + clp = clp->next; } if (clp == NULL) { /* it isn't there, push on a new one to the front of the list */ clp = (struct CachedCursor *)malloc(sizeof(struct CachedCursor)); - for (int i = 0; i < CURSORHEIGHT; i++) clp->bitmap[i] = bitmap[i]; - SDL_Cursor *c = SDL_CreateCursor(cursor, cursor, 16, 16, hot_x, hot_y); + memcpy(clp->EmCursorBitMap, cursor, sizeof(clp->EmCursorBitMap)); + for (int i = 0; i < 32; i++) sdl_cursor_data[i] = GETBYTE(((Uint8 *)cursor) + i); + SDL_Cursor *c = SDL_CreateCursor(sdl_cursor_data, sdl_cursor_data, 16, 16, hot_x, hot_y); if (c == NULL) printf("ERROR creating cursor: %s\n", SDL_GetError()); clp->cursor = c; clp->next = sdl_cursorlist; sdl_cursorlist = clp; - // printf("Creating new cursor, using it\n"); - return clp->cursor; - } else { - /* TODO: move to front of list */ - // printf("Found cursor, using it\n"); return clp->cursor; } + /* if it's in the first two elements of the list, leave the order alone. + * There is a high probability of flipping back and forth between two + */ + if (clp == sdl_cursorlist || pclp == sdl_cursorlist) { + return clp->cursor; + } + /* otherwise unlink the found item and reinsert at the front */ + pclp->next = clp->next; + clp->next = sdl_cursorlist; + sdl_cursorlist = clp; + return clp->cursor; } /* @@ -289,23 +300,7 @@ static SDL_Cursor *sdl_getOrAllocateCursor(Uint8 cursor[32], int hot_x, int hot_ * XXX: needs to deal with sdl_pixelscale > 1, and where is the hotspot? */ void sdl_setCursor(int hot_x, int hot_y) { - DLword *newbm = ((DLword *)(IOPage->dlcursorbitmap)); - Uint8 cursor[32]; - // printf("setCursor: bm %p, x %d, y %d\n", newbm, hot_x, hot_y); - /* for(int i = 0; i < CURSORHEIGHT; i++) { */ - /* printf("%04x ", newbm[i]); */ - /* // cursor[i] = newbm[i % 2 == 1 ? i - 1 : i + 1]; */ - /* } */ - /* printf("\n"); */ - Uint8 *bitmap = (Uint8 *)newbm; - for (int i = 0; i < 32; i++) cursor[i] = bitmap[i ^ 3]; - // TODO: actually keep track of the cursors, don't just allocate a new one every time! - /* for(int i = 0; i < CURSORHEIGHT * 2; i++) { */ - /* printf("%02x ", cursor[i]); */ - /* // cursor[i] = newbm[i % 2 == 1 ? i - 1 : i + 1]; */ - /* } */ - /* printf("\n"); */ - SDL_Cursor *c = sdl_getOrAllocateCursor(cursor, hot_x, hot_y); + SDL_Cursor *c = sdl_getOrAllocateCursor(EmCursorBitMap68K, hot_x, hot_y); SDL_SetCursor(c); } #if defined(SDLRENDERING) From 7048f7dd8d28527f7350b176021328c5be7b0472 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 6 Dec 2021 19:21:10 -0800 Subject: [PATCH 50/68] Handle pixel scaling when creating the cursors --- src/sdl.c | 76 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index f141963..126183b 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -248,6 +248,26 @@ struct CachedCursor { SDL_Cursor *cursor; } *sdl_cursorlist = NULL; +/* + * given a 16-bit value and a repeat count modify an array + * of bytes to contain the same bit pattern with each bit + * repeated "reps" times consecutively in the output + */ +static void replicate_bits(int bits, int reps, Uint8 *out) { + int dbyte = 0; + int dbit = 7; + for (int ibit = 15; ibit >= 0; --ibit) { + for (int r = 0; r < reps; r++) { + if (bits & (1 << ibit)) + out[dbyte] |= 1 << dbit; + if (--dbit < 0) { + dbyte++; + dbit = 7; + } + } + } +} + static int cursor_equal_p(DLword *a, DLword *b) { for (int i = 0; i < CURSORHEIGHT; i++) if (a[i] != b[i]) return FALSE; @@ -261,34 +281,52 @@ static int cursor_equal_p(DLword *a, DLword *b) { static SDL_Cursor *sdl_getOrAllocateCursor(DLword cursor[16], int hot_x, int hot_y) { hot_x = 0; hot_y = 0; - Uint8 sdl_cursor_data[32]; /* try to find the cursor by checking the full bitmap */ struct CachedCursor *pclp = NULL; struct CachedCursor *clp = sdl_cursorlist; + SDL_Cursor *c; while (clp != NULL) { - if (cursor_equal_p(clp->EmCursorBitMap, cursor) == TRUE) break; + if (cursor_equal_p(clp->EmCursorBitMap, cursor) == TRUE) { + /* if it's in the first two elements of the list, leave the order alone. + * There is a high probability of flipping back and forth between two + */ + if (clp == sdl_cursorlist || pclp == sdl_cursorlist) { + return clp->cursor; + } + /* otherwise unlink the found item and reinsert at the front */ + pclp->next = clp->next; + clp->next = sdl_cursorlist; + sdl_cursorlist = clp; + return clp->cursor; + } pclp = clp; clp = clp->next; } - if (clp == NULL) { /* it isn't there, push on a new one to the front of the list */ - clp = (struct CachedCursor *)malloc(sizeof(struct CachedCursor)); - memcpy(clp->EmCursorBitMap, cursor, sizeof(clp->EmCursorBitMap)); + /* It isn't there, so build a new one */ + clp = (struct CachedCursor *)malloc(sizeof(struct CachedCursor)); + memcpy(clp->EmCursorBitMap, cursor, sizeof(clp->EmCursorBitMap)); + /* no scaling is an easy case, scale > 1 is harder */ + if (sdl_pixelscale == 1) { + Uint8 sdl_cursor_data[32]; for (int i = 0; i < 32; i++) sdl_cursor_data[i] = GETBYTE(((Uint8 *)cursor) + i); - SDL_Cursor *c = SDL_CreateCursor(sdl_cursor_data, sdl_cursor_data, 16, 16, hot_x, hot_y); - if (c == NULL) printf("ERROR creating cursor: %s\n", SDL_GetError()); - clp->cursor = c; - clp->next = sdl_cursorlist; - sdl_cursorlist = clp; - return clp->cursor; + c = SDL_CreateCursor(sdl_cursor_data, sdl_cursor_data, 16, 16, hot_x, hot_y); + } else { + Uint8 *sdl_cursor_data = calloc(sdl_pixelscale * sdl_pixelscale, 32); + /* fill in the cursor data expanded */ + for (int i = 0; i < 32; i += 2) { + int v = GETBYTE(((Uint8 *)cursor) + i) << 8 | GETBYTE(((Uint8 *)cursor) + i + 1); + int db = i * sdl_pixelscale * sdl_pixelscale; + /* spread the bits out for the first copy of the row */ + replicate_bits(v, sdl_pixelscale, &sdl_cursor_data[db]); + /* and then copy the replicated bits for the copies of the row */ + for (int j = 1; j < sdl_pixelscale; j++) { + memcpy(&sdl_cursor_data[db + (j * 2 * sdl_pixelscale)], &sdl_cursor_data[db], 2 * sdl_pixelscale); + } + } + c = SDL_CreateCursor(sdl_cursor_data, sdl_cursor_data, 16 * sdl_pixelscale, 16 * sdl_pixelscale, hot_x, hot_y); } - /* if it's in the first two elements of the list, leave the order alone. - * There is a high probability of flipping back and forth between two - */ - if (clp == sdl_cursorlist || pclp == sdl_cursorlist) { - return clp->cursor; - } - /* otherwise unlink the found item and reinsert at the front */ - pclp->next = clp->next; + if (c == NULL) printf("ERROR creating cursor: %s\n", SDL_GetError()); + clp->cursor = c; clp->next = sdl_cursorlist; sdl_cursorlist = clp; return clp->cursor; From 376b045c332b4738bee0c2ba42c30b1b2f55fb7e Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Fri, 10 Dec 2021 16:35:19 -0800 Subject: [PATCH 51/68] Use SDL_LowerBlitScaled() rather than SDL_BlitScaled() --- src/sdl.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/sdl.c b/src/sdl.c index 126183b..2245c72 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -11,6 +11,10 @@ #include "lspglob.h" // for IOPage #include "display.h" // for CURSORHEIGHT, DisplayRegion68k +/* if SDLRENDERING is defined, render to a texture rather than + * using the window surface + */ + #define SDLRENDERING 1 static SDL_Window *sdl_window = NULL; @@ -563,7 +567,7 @@ void sdl_update_display() { s.w = r.w * sdl_pixelscale; s.h = r.h * sdl_pixelscale; sdl_bitblt_to_buffer(r.x, r.y, r.w, r.h); - SDL_BlitScaled(sdl_buffersurface, &r, sdl_windowsurface, &s); + SDL_LowerBlitScaled(sdl_buffersurface, &r, sdl_windowsurface, &s); SDL_UpdateWindowSurfaceRects(sdl_window, &s, 1); } } From a3c462d211970112c0880a57b9e0fb934ebfbecf Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 20 Dec 2021 12:56:11 -0800 Subject: [PATCH 52/68] Rename sdl_bitblt_to_texture2 to sdl_bitblt_to_texture_exact to be more descriptive --- src/sdl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl.c b/src/sdl.c index 2245c72..3b86d2a 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -382,7 +382,7 @@ void sdl_bitblt_to_texture(int _x, int _y, int _w, int _h) { } SDL_UnlockTexture(sdl_texture); } -void sdl_bitblt_to_texture2(int _x, int _y, int _w, int _h) { +void sdl_bitblt_to_texture_exact(int _x, int _y, int _w, int _h) { DLword *src = DisplayRegion68k; void *dst; int dstpitchbytes; From 2bc704c5e34be7139a385ece2ce60cb5b7693829 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Sat, 12 Nov 2022 18:33:26 -0800 Subject: [PATCH 53/68] Minor cleanups after rebase onto master Fix merge edit mistake in CMakeLists.txt Add missing includes in draw.c, keyevent.c --- CMakeLists.txt | 4 +--- src/draw.c | 1 + src/keyevent.c | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c81460..959e145 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,6 +93,7 @@ ELSEIF(MAIKO_NETWORK_TYPE STREQUAL "SUN_NIT") MESSAGE("-- Configured for (SunOS) NIT networking") ELSEIF(NOT MAIKO_NETWORK_TYPE STREQUAL "NONE") MESSAGE(WARNING "Invalid option given for MAIKO_NETWORK_TYPE, must be one of:\nNONE, SUN_DLPI, SUN_NIT, NETHUB") +ENDIF() OPTION(MAIKO_DISPLAY_SDL "Use SDL for display." ON) IF(MAIKO_DISPLAY_SDL) @@ -452,8 +453,6 @@ IF(MAIKO_DISPLAY_X11) TARGET_LINK_LIBRARIES(ldex ${MAIKO_LIBRARIES} ${MAIKO_DISPLAY_X11_LIBRARIES}) ENDIF() -<<<<<<< HEAD -======= IF(MAIKO_DISPLAY_SDL) ADD_EXECUTABLE(ldesdl src/main.c @@ -473,7 +472,6 @@ ADD_EXECUTABLE(mkvdate src/mkvdate.c) TARGET_COMPILE_DEFINITIONS(mkvdate PUBLIC ${MAIKO_DEFINITIONS}) TARGET_INCLUDE_DIRECTORIES(mkvdate PUBLIC inc) ->>>>>>> f58abe3 (Add prototypical SDL support.) ADD_EXECUTABLE(setsout src/setsout.c src/byteswap.c) TARGET_COMPILE_DEFINITIONS(setsout PUBLIC ${MAIKO_DEFINITIONS}) TARGET_INCLUDE_DIRECTORIES(setsout PUBLIC inc) diff --git a/src/draw.c b/src/draw.c index 09b314f..1b5fdac 100644 --- a/src/draw.c +++ b/src/draw.c @@ -20,6 +20,7 @@ #include <stddef.h> // for ptrdiff_t #include <stdlib.h> // for abs #include "adr68k.h" // for NativeAligned2FromLAddr +#include "bitblt.h" // for MOUSEXR, MOUSEYH #include "display.h" // for DISPLAYBUFFER, DisplayRegion68k, in_display... #include "drawdefs.h" // for N_OP_drawline #include "emlglob.h" diff --git a/src/keyevent.c b/src/keyevent.c index 8386687..ecc60b3 100644 --- a/src/keyevent.c +++ b/src/keyevent.c @@ -45,6 +45,7 @@ void Mouse_hndlr(void); /* Fields mouse events from driver */ #include "adr68k.h" #include "address.h" #include "stack.h" +#include "initdspdefs.h" #include "keyboard.h" #include "display.h" #include "lsptypes.h" From 8c490df964e79655b6ee747616adfb8f6e27e852 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Sat, 12 Nov 2022 20:42:23 -0800 Subject: [PATCH 54/68] Instruct git to ignore SDL object directories for x86_64 builds --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a5a3139..288345e 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,7 @@ build/** *.sparc-x/** *.sparc/** *.x86_64-x/** +*.x86_64-sdl/** *.x86_64/** *.armv7l-x/** *.armv7l/** From 4ae1987c33b2df7bf602ec5a0e9c5436f58ac14b Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Sat, 12 Nov 2022 20:44:16 -0800 Subject: [PATCH 55/68] Teach makeright to compile for SDL display on MacOS on x86_64 CPU --- bin/makefile-darwin.x86_64-sdl | 19 +++++++++++++++++++ bin/makeright | 3 +++ 2 files changed, 22 insertions(+) create mode 100644 bin/makefile-darwin.x86_64-sdl diff --git a/bin/makefile-darwin.x86_64-sdl b/bin/makefile-darwin.x86_64-sdl new file mode 100644 index 0000000..b15e07b --- /dev/null +++ b/bin/makefile-darwin.x86_64-sdl @@ -0,0 +1,19 @@ +# Options for MacOS, x86 processor, X windows + +CC = clang -m64 $(CLANG_CFLAGS) + +XFILES = $(OBJECTDIR)sdl.o + +SDLFLAGS = -DSDL -I/Library/Frameworks/SDL2.framework/Headers + +# OPTFLAGS is normally -O2. +OPTFLAGS = -O1 -g +DEBUGFLAGS = # -DDEBUG -DOPTRACE +DFLAGS = $(DEBUGFLAGS) $(SDLFLAGS) -DRELEASE=351 + +LDFLAGS = -F/Library/Frameworks -framework SDL2 +LDELDFLAGS = + +OBJECTDIR = ../$(RELEASENAME)/ + +default : ../$(OSARCHNAME)/lde ../$(OSARCHNAME)/ldesdl diff --git a/bin/makeright b/bin/makeright index 9f2ab0c..1fde522 100755 --- a/bin/makeright +++ b/bin/makeright @@ -71,6 +71,9 @@ case "$display" in x) releasename=${osversion}.${architecture}-${display} ldename=ldex ;; + sdl) releasename=${osversion}.${architecture}-${display} + ldename=ldesdl + ;; *) echo "display-option: $display is not supported." exit ;; From 7d8a7a66688b24f46e8c50d802fe19cb79c1fe6c Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Mon, 21 Nov 2022 17:18:47 -0800 Subject: [PATCH 56/68] Clean up SDL infrastructure, add makefiles for more system/cpu combinations Rationalize the SDL.h include file references so that they work on all systems Use the correct macOS Framework options to get include path and library search path Add makefile fragments for SDL on macOS on Apple Silicon, plus FreeBSD and Linux on same cpus as X11 Switch to gcc rather than clang as the default compiler for Linux systems --- bin/makefile-cygwin.x86_64-x | 2 +- bin/makefile-darwin.aarch64-sdl | 19 +++++++++++++++++++ bin/makefile-darwin.x86_64-sdl | 8 ++++---- bin/makefile-freebsd.386-sdl | 18 ++++++++++++++++++ bin/makefile-freebsd.aarch64-sdl | 18 ++++++++++++++++++ bin/makefile-freebsd.x86_64-sdl | 18 ++++++++++++++++++ bin/makefile-linux.386-sdl | 19 +++++++++++++++++++ bin/makefile-linux.386-x | 6 +++--- bin/makefile-linux.aarch64-sdl | 19 +++++++++++++++++++ bin/makefile-linux.armv7l-sdl | 19 +++++++++++++++++++ bin/makefile-linux.x86_64-sdl | 19 +++++++++++++++++++ bin/makefile-linux.x86_64-x | 4 ++-- src/sdl.c | 14 +++++++++----- 13 files changed, 168 insertions(+), 15 deletions(-) mode change 100755 => 100644 bin/makefile-cygwin.x86_64-x create mode 100644 bin/makefile-darwin.aarch64-sdl create mode 100644 bin/makefile-freebsd.386-sdl create mode 100644 bin/makefile-freebsd.aarch64-sdl create mode 100644 bin/makefile-freebsd.x86_64-sdl create mode 100644 bin/makefile-linux.386-sdl create mode 100644 bin/makefile-linux.aarch64-sdl create mode 100644 bin/makefile-linux.armv7l-sdl create mode 100644 bin/makefile-linux.x86_64-sdl diff --git a/bin/makefile-cygwin.x86_64-x b/bin/makefile-cygwin.x86_64-x old mode 100755 new mode 100644 index cce7326..f280cba --- a/bin/makefile-cygwin.x86_64-x +++ b/bin/makefile-cygwin.x86_64-x @@ -1,7 +1,7 @@ # Options for Linux, Intel x86_64 and X-Window #CC = gcc -m64 $(GCC_CFLAGS) -CC = clang -m64 $(CLANG_CFLAGS) -DMAIKO_ENABLE_NETHUB +CC = clang -m64 $(CLANG_CFLAGS) XFILES = $(OBJECTDIR)xmkicon.o \ $(OBJECTDIR)xbbt.o \ diff --git a/bin/makefile-darwin.aarch64-sdl b/bin/makefile-darwin.aarch64-sdl new file mode 100644 index 0000000..dc56a02 --- /dev/null +++ b/bin/makefile-darwin.aarch64-sdl @@ -0,0 +1,19 @@ +# Options for MacOS, x86 processor, SDL + +CC = clang $(CLANG_CFLAGS) + +XFILES = $(OBJECTDIR)sdl.o + +SDLFLAGS = -DSDL -F /Library/Frameworks + +# OPTFLAGS is normally -O2. +OPTFLAGS = -O2 -g +DEBUGFLAGS = # -DDEBUG -DOPTRACE +DFLAGS = $(DEBUGFLAGS) $(SDLFLAGS) -DRELEASE=351 + +LDFLAGS = -F /Library/Frameworks -framework SDL2 +LDELDFLAGS = + +OBJECTDIR = ../$(RELEASENAME)/ + +default : ../$(OSARCHNAME)/lde ../$(OSARCHNAME)/ldesdl diff --git a/bin/makefile-darwin.x86_64-sdl b/bin/makefile-darwin.x86_64-sdl index b15e07b..94604b0 100644 --- a/bin/makefile-darwin.x86_64-sdl +++ b/bin/makefile-darwin.x86_64-sdl @@ -1,17 +1,17 @@ -# Options for MacOS, x86 processor, X windows +# Options for MacOS, x86 processor, SDL CC = clang -m64 $(CLANG_CFLAGS) XFILES = $(OBJECTDIR)sdl.o -SDLFLAGS = -DSDL -I/Library/Frameworks/SDL2.framework/Headers +SDLFLAGS = -DSDL -F /Library/Frameworks # OPTFLAGS is normally -O2. -OPTFLAGS = -O1 -g +OPTFLAGS = -O2 -g DEBUGFLAGS = # -DDEBUG -DOPTRACE DFLAGS = $(DEBUGFLAGS) $(SDLFLAGS) -DRELEASE=351 -LDFLAGS = -F/Library/Frameworks -framework SDL2 +LDFLAGS = -F /Library/Frameworks -framework SDL2 LDELDFLAGS = OBJECTDIR = ../$(RELEASENAME)/ diff --git a/bin/makefile-freebsd.386-sdl b/bin/makefile-freebsd.386-sdl new file mode 100644 index 0000000..8a843a6 --- /dev/null +++ b/bin/makefile-freebsd.386-sdl @@ -0,0 +1,18 @@ +# Options for FreeBSD, Intel 386/486 and SDL + +CC = clang -m32 $(CLANG_CFLAGS) + +XFILES = $(OBJECTDIR)sdl.o + +SDLFLAGS = -DSDL -I/usr/local/include + +# OPTFLAGS is normally -O2. +OPTFLAGS = -O2 -g +DFLAGS = $(SDLFLAGS) -DRELEASE=351 + +LDFLAGS = -L/usr/local/lib -lSDL2 -lm +LDELDFLAGS = + +OBJECTDIR = ../$(RELEASENAME)/ + +default : ../$(OSARCHNAME)/lde ../$(OSARCHNAME)/ldesdl diff --git a/bin/makefile-freebsd.aarch64-sdl b/bin/makefile-freebsd.aarch64-sdl new file mode 100644 index 0000000..01f967b --- /dev/null +++ b/bin/makefile-freebsd.aarch64-sdl @@ -0,0 +1,18 @@ +# Options for FreeBSD, ARM64 and SDL + +CC = clang -m64 $(CLANG_CFLAGS) + +XFILES = $(OBJECTDIR)sdl.o + +SDLFLAGS = -DSDL -I/usr/local/include + +# OPTFLAGS is normally -O2. +OPTFLAGS = -O2 -g +DFLAGS = $(SDLFLAGS) -DRELEASE=351 + +LDFLAGS = -L/usr/local/lib -lSDL2 -lm +LDELDFLAGS = + +OBJECTDIR = ../$(RELEASENAME)/ + +default : ../$(OSARCHNAME)/lde ../$(OSARCHNAME)/ldesdl diff --git a/bin/makefile-freebsd.x86_64-sdl b/bin/makefile-freebsd.x86_64-sdl new file mode 100644 index 0000000..8087150 --- /dev/null +++ b/bin/makefile-freebsd.x86_64-sdl @@ -0,0 +1,18 @@ +# Options for FreeBSD, Intel x86_64 and X-Windows + +CC = clang -m64 $(CLANG_CFLAGS) + +XFILES = $(OBJECTDIR)sdl.o + +SDLFLAGS = -DSDL -I/usr/local/include + +# OPTFLAGS is normally -O2. +OPTFLAGS = -O2 -g +DFLAGS = $(SDLFLAGS) -DRELEASE=351 + +LDFLAGS = -L/usr/local/lib -lSDL2 -lm +LDELDFLAGS = + +OBJECTDIR = ../$(RELEASENAME)/ + +default : ../$(OSARCHNAME)/lde ../$(OSARCHNAME)/ldesdl diff --git a/bin/makefile-linux.386-sdl b/bin/makefile-linux.386-sdl new file mode 100644 index 0000000..da04d80 --- /dev/null +++ b/bin/makefile-linux.386-sdl @@ -0,0 +1,19 @@ +# Options for Linux, Intel 386/486 and SDL + +CC = gcc -m32 $(GCC_CFLAGS) +# CC = clang $(CLANG_CFLAGS) + +XFILES = $(OBJECTDIR)sdl.o + +XFLAGS = -DSDL + +# OPTFLAGS is normally -O2. +OPTFLAGS = -O2 -g3 +DFLAGS = $(XFLAGS) -DRELEASE=351 + +LDFLAGS = -lm -lSDL2 +LDELDFLAGS = + +OBJECTDIR = ../$(RELEASENAME)/ + +default : ../$(OSARCHNAME)/lde ../$(OSARCHNAME)/ldesdl diff --git a/bin/makefile-linux.386-x b/bin/makefile-linux.386-x index b25198d..5250683 100644 --- a/bin/makefile-linux.386-x +++ b/bin/makefile-linux.386-x @@ -1,7 +1,8 @@ # Options for Linux, Intel 386/486 and X-Window -#CC = gcc -m32 $(GCC_CFLAGS) -CC = clang -m32 $(CLANG_CFLAGS) +CC = gcc -m32 $(GCC_CFLAGS) +# CC = clang -m32 $(CLANG_CFLAGS) + XFILES = $(OBJECTDIR)xmkicon.o \ $(OBJECTDIR)xbbt.o \ $(OBJECTDIR)dspif.o \ @@ -12,7 +13,6 @@ XFILES = $(OBJECTDIR)xmkicon.o \ $(OBJECTDIR)xrdopt.o \ $(OBJECTDIR)xwinman.o - XFLAGS = -DXWINDOW # OPTFLAGS is normally -O2. diff --git a/bin/makefile-linux.aarch64-sdl b/bin/makefile-linux.aarch64-sdl new file mode 100644 index 0000000..337b14d --- /dev/null +++ b/bin/makefile-linux.aarch64-sdl @@ -0,0 +1,19 @@ +# Options for Linux, ARM64 and SDL + +CC = gcc $(GCC_CFLAGS) +#CC = clang $(CLANG_CFLAGS) + +XFILES = $(OBJECTDIR)sdl.o + +SDLFLAGS = -DSDL + +# OPTFLAGS is normally -O2. +OPTFLAGS = -O2 -g3 +DFLAGS = $(SDLFLAGS) -DRELEASE=351 + +LDFLAGS = -lSDL2 -lm +LDELDFLAGS = + +OBJECTDIR = ../$(RELEASENAME)/ + +default : ../$(OSARCHNAME)/lde ../$(OSARCHNAME)/ldesdl diff --git a/bin/makefile-linux.armv7l-sdl b/bin/makefile-linux.armv7l-sdl new file mode 100644 index 0000000..f2b844c --- /dev/null +++ b/bin/makefile-linux.armv7l-sdl @@ -0,0 +1,19 @@ +# Options for Linux, ARMv7 and SDL + +CC = gcc $(GCC_CFLAGS) +#CC = clang $(CLANG_CFLAGS) + +XFILES = $(OBJECTDIR)sdl.o + +XFLAGS = -DSDL + +# OPTFLAGS is normally -O2. +OPTFLAGS = -O2 -g3 +DFLAGS = $(XFLAGS) -DRELEASE=351 + +LDFLAGS = -lm -lSDL2 +LDELDFLAGS = + +OBJECTDIR = ../$(RELEASENAME)/ + +default : ../$(OSARCHNAME)/lde ../$(OSARCHNAME)/ldesdl diff --git a/bin/makefile-linux.x86_64-sdl b/bin/makefile-linux.x86_64-sdl new file mode 100644 index 0000000..64819dc --- /dev/null +++ b/bin/makefile-linux.x86_64-sdl @@ -0,0 +1,19 @@ +# Options for Linux, Intel x86_64 and SDL + +CC = gcc -m64 $(GCC_CFLAGS) +# CC = clang -m64 $(CLANG_CFLAGS) + +XFILES = $(OBJECTDIR)sdl.o + +XFLAGS = -DSDL + +# OPTFLAGS is normally -O2. +OPTFLAGS = -O2 -g3 +DFLAGS = $(XFLAGS) -DRELEASE=351 + +LDFLAGS = -lm -lSDL2 +LDELDFLAGS = + +OBJECTDIR = ../$(RELEASENAME)/ + +default : ../$(OSARCHNAME)/lde ../$(OSARCHNAME)/ldesdl diff --git a/bin/makefile-linux.x86_64-x b/bin/makefile-linux.x86_64-x index cce7326..64c3eb0 100644 --- a/bin/makefile-linux.x86_64-x +++ b/bin/makefile-linux.x86_64-x @@ -1,7 +1,7 @@ # Options for Linux, Intel x86_64 and X-Window -#CC = gcc -m64 $(GCC_CFLAGS) -CC = clang -m64 $(CLANG_CFLAGS) -DMAIKO_ENABLE_NETHUB +CC = gcc -m64 $(GCC_CFLAGS) +# CC = clang -m64 $(CLANG_CFLAGS) XFILES = $(OBJECTDIR)xmkicon.o \ $(OBJECTDIR)xbbt.o \ diff --git a/src/sdl.c b/src/sdl.c index 3b86d2a..c0f8c52 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -1,5 +1,5 @@ -#include <SDL.h> -#include <SDL_keycode.h> +#include <SDL2/SDL.h> +#include <SDL2/SDL_keycode.h> #include <assert.h> #include <limits.h> #include "sdldefs.h" @@ -482,7 +482,7 @@ static void handle_keydown(SDL_Keycode k, unsigned short mod) { if (lk == -1) { printf("No mapping for key %s\n", SDL_GetKeyName(k)); } else { - printf("dn %s -> lisp keycode %d (0x%x)\n", SDL_GetKeyName(k), lk, mod); + // printf("dn %s -> lisp keycode %d (0x%x)\n", SDL_GetKeyName(k), lk, mod); kb_trans(lk - KEYCODE_OFFSET, FALSE); DoRing(); if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; @@ -493,7 +493,7 @@ static void handle_keyup(SDL_Keycode k, unsigned short mod) { if (lk == -1) { printf("No mapping for key %s\n", SDL_GetKeyName(k)); } else { - printf("up %s -> lisp keycode %d (0x%x)\n", SDL_GetKeyName(k), lk, mod); + // printf("up %s -> lisp keycode %d (0x%x)\n", SDL_GetKeyName(k), lk, mod); kb_trans(lk - KEYCODE_OFFSET, TRUE); DoRing(); if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; @@ -592,10 +592,12 @@ void process_SDLevents() { } break; case SDL_KEYDOWN: +#if 0 printf("dn ts: %x, type: %x, state: %x, repeat: %x, scancode: %x, sym: %x <%s>, mod: %x\n", event.key.timestamp, event.key.type, event.key.state, event.key.repeat, event.key.keysym.scancode, event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.mod); +#endif if (event.key.repeat) { /* Lisp needs to see the UP transition before the DOWN transition */ handle_keyup(event.key.keysym.sym, event.key.keysym.mod); @@ -603,10 +605,12 @@ void process_SDLevents() { handle_keydown(event.key.keysym.sym, event.key.keysym.mod); break; case SDL_KEYUP: +#if 0 printf("up ts: %x, type: %x, state: %x, repeat: %x, scancode: %x, sym: %x <%s>, mod: %x\n", event.key.timestamp, event.key.type, event.key.state, event.key.repeat, event.key.keysym.scancode, event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym), event.key.keysym.mod); +#endif handle_keyup(event.key.keysym.sym, event.key.keysym.mod); break; case SDL_MOUSEMOTION: { @@ -660,7 +664,7 @@ void process_SDLevents() { /* printf("SDL_KEYMAPCHANGED\n"); break; */ /* case SDL_TEXTINPUT: */ /* printf("SDL_TEXTINPUT\n"); break; */ - default: printf("other event type: %d\n", event.type); + default: /* printf("other event type: %d\n", event.type); */ break; } } if (display_update_needed) { From d63b933dc834e1794ea081618d0220ef1e38a49e Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Wed, 7 Dec 2022 18:32:04 -0800 Subject: [PATCH 57/68] Clean up various aspects of ldeboot.c (lde) Handle compilation with both SDL and XWINDOW defines If both are defined then the -d/-display will accept SDL as a display name and invoke ldesdl rather than ldex. Improve error messages when display can't be opened Use exit status 1 for all failures (vs. mixed 1, -1) --- src/ldeboot.c | 248 +++++++++++++++++++++++--------------------------- 1 file changed, 115 insertions(+), 133 deletions(-) diff --git a/src/ldeboot.c b/src/ldeboot.c index 2f870cc..1fd356d 100644 --- a/src/ldeboot.c +++ b/src/ldeboot.c @@ -18,6 +18,8 @@ #include <sys/ioctl.h> #include <unistd.h> +#include "unixfork.h" + #if defined(sun) && !defined(OS5) #define USESUNSCREEN #else @@ -26,16 +28,16 @@ #ifdef USESUNSCREEN #include <sys/fbio.h> +#ifndef FBTYPE_SUNFAST_COLOR +#define FBTYPE_SUNFAST_COLOR 12 +#endif #define LDEMONO "ldesingle" #define LDECOLOR "ldemulti" #define LDETRUECOLOR "ldetruecolor" -#define FBTYPE_SUNFAST_COLOR 12 #endif /* USESUNSCREEN */ -#include "unixfork.h" - #ifdef XWINDOW #include <X11/Xlib.h> #include <X11/Xutil.h> @@ -45,12 +47,6 @@ #ifdef SDL #define LDESDL "ldesdl" #endif -#define LDEMONO "ldesingle" -#define LDECOLOR "ldemulti" -#define LDETRUECOLOR "ldetruecolor" - -#define FBTYPE_SUNFAST_COLOR 12 - /************************************************************************/ /* */ @@ -60,10 +56,11 @@ /* */ /************************************************************************/ -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { int i; - char filetorun[30] = {0}; + char *filetorun = NULL; + char *displayName = (char *)NULL; + int doFork = 1; #ifdef USESUNSCREEN int FrameBufferFd; struct fbtype my_screen; @@ -71,143 +68,128 @@ int main(int argc, char *argv[]) struct fbgattr FBattr; #endif /* USESUNSCREEN */ -/* Kickstart program for the Lisp Development Environment (LDE). - Display Device emulator - CG3, CG6 lde.multi - BW2, CG2, CG4, CG9 lde.single + /* Kickstart program for the Lisp Development Environment (LDE). + Display Device emulator + CG3, CG6 lde.multi + BW2, CG2, CG4, CG9 lde.single - FB-TYPE REAL-TYPE - BW2 2 x - CG2 3 3 - CG3 8 6 - CG4 2 8 - CG6 8 12 - CG8 6 7 - CG9(GP1) 4 4 ;gpconfig -f -b - CG9(GP1) 2 13 ;gpconfig gpone0 -f -b cgtwo0 - ;We assume This config for GXP model + FB-TYPE REAL-TYPE + BW2 2 x + CG2 3 3 + CG3 8 6 + CG4 2 8 + CG6 8 12 + CG8 6 7 + CG9(GP1) 4 4 ;gpconfig -f -b + CG9(GP1) 2 13 ;gpconfig gpone0 -f -b cgtwo0 + ;We assume This config for GXP model + */ + + /* look for a -display argument that could tell us X11 vs SDL for display + */ + for (i = 1; i < argc; i++) { + if ((strcmp(argv[i], "-d") == 0) || (strcmp(argv[i], "-display") == 0)) { + if (i == argc - 1) { + fprintf(stderr, "Missing argument to -display option.\n"); + exit(1); + } + displayName = argv[++i]; + } + } + + /* Unless prevented by -NF option, fork the process, while small, to handle + * process communications and subsequent forks */ -#ifdef XWINDOW - /* If X-Server exists on the host specified in -display option - or environment variable DISPLAY, ldex is started. Otherwise - ldesingle or ldemulti. - */ - { - char *Display_Name = (char *)NULL; - Display *Xdisplay = (Display *)NULL; - char *pos; - - for (i = 1; i < argc; i++) { - if ((strcmp(argv[i], "-d") == 0) || (strcmp(argv[i], "-display") == 0)) { - if (i == argc) break; - pos = (char *)strchr(argv[++i], ':'); - if (pos != NULL) { Display_Name = argv[i]; } - continue; - } + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-NF") == 0) { + doFork = 0; + break; } + } + if (doFork) fork_Unix(); - if ((Xdisplay = XOpenDisplay(Display_Name)) != (Display *)NULL) { - /* success to connect X-server */ +#ifdef SDL +#ifdef XWINDOW + /* if we have SDL *and* XWINDOW we only do SDL if requested */ + if (displayName && (0 == strcmp(displayName, "SDL"))) { +#else + /* otherwise SDL is it */ + { +#endif + filetorun = LDESDL; + goto run; + } +#endif /* SDL */ + +#ifdef XWINDOW + /* If an X server exists as specified in the -display option + * or environment variable DISPLAY, ldex is started. + */ + { + Display *Xdisplay = XOpenDisplay(displayName); + + if (Xdisplay) { + /* success connecting to X server. Close it now, it will be reopened by ldex */ XCloseDisplay(Xdisplay); - strcpy(filetorun, LDEX); - - /* JRB - call fork_Unix here, while we're REALLY small, unless -NF is - specified, of course... */ - for (i = 1; i < argc; i++) - if (!strcmp(argv[i], "-NF")) break; - if (i == argc) /* -NF not in arguments */ - fork_Unix(); - - argv[0] = filetorun; - execvp(filetorun, argv); - perror(filetorun); + filetorun = LDEX; + goto run; + } else { + fprintf(stderr, "Unable to open X11 display %s\n", + displayName ? displayName : "from DISPLAY"); exit(1); - } else { /* failed to connect X-server */ -#define NAME_LEN 100 - char host_name[NAME_LEN]; - gethostname(host_name, NAME_LEN); - if (Display_Name == NULL) { - if ((Display_Name = getenv("DISPLAY")) != NULL) { - if (strncmp(Display_Name, host_name, strlen(host_name)) == 0) { - fprintf(stderr, "ldeboot: can't find X-Server\n"); - exit(-1); - } /* end if */ - } - /* end if */ - } else { - fprintf(stderr, "ldeboot: can't find X-Server\n"); - exit(-1); - } /* end if */ - } /* end if */ + } } #endif /* XWINDOW */ -#ifdef SDL - strcpy(filetorun,LDESDL); -#endif /* SDL */ + #ifdef USESUNSCREEN if ((FrameBufferFd = open("/dev/fb", O_RDWR)) < 0) { - fprintf(stderr, "ldeboot: can't open FrameBuffer\n"); - exit(-1); + fprintf(stderr, "lde: can't open FrameBuffer\n"); + exit(1); } if (ioctl(FrameBufferFd, FBIOGTYPE, &my_screen) < 0) { - perror("initdisplay0:"); - exit(-1); - } - - if (my_screen.fb_type == FBTYPE_SUN4COLOR) { /* cg3 or cg6 */ - if (ioctl(FrameBufferFd, FBIOGATTR, &FBattr) >= 0) { - if (FBattr.real_type == FBTYPE_SUN3COLOR || /* cg3 */ - FBattr.real_type == FBTYPE_SUNFAST_COLOR) /* cg6 */ - { - strcpy(filetorun, LDECOLOR); - } - } else { /* if( ioctl... */ - perror("lde: This Display Model does not supported\n"); - exit(-1); - } - } else if (my_screen.fb_type == FBTYPE_SUN2BW) { /* bw2, cg4 or cg9 */ - strcpy(filetorun, LDEMONO); - } else if (my_screen.fb_type == FBTYPE_SUN3COLOR) { - if (ioctl(FrameBufferFd, FBIOGATTR, &FBattr) >= 0) { - if (FBattr.real_type == FBTYPE_MEMCOLOR) /* cg8 */ - { - strcpy(filetorun, LDETRUECOLOR); - } - } else { /* if( ioctl... */ - perror("lde: This Display Model does not supported\n"); - exit(-1); - } - - } else if (my_screen.fb_type == FBTYPE_SUN2COLOR) { /* cg2 */ - strcpy(filetorun, LDEMONO); - } else { - perror("lde: This Display Model does not supported\n"); - exit(-1); - } /* endif( my_screen... */ - - close(FrameBufferFd); - -#endif /* USESUNSCREEN */ - - /* JRB - call fork_Unix here, while we're REALLY small, unless -NF is - specified, of course... */ - for (i = 1; i < argc; i++) - if (!strcmp(argv[i], "-NF")) break; - if (i == argc) /* -NF not in arguments */ - fork_Unix(); - - /* start ldemono or ldecolor */ - if (filetorun[0] == '\0') { - fprintf(stderr, "Unable to determine what display program to run.\n"); + perror(argv[0]); exit(1); } - argv[0] = filetorun; /* or whatever... */ - - /* then execve the LDE executable */ - execvp(filetorun, argv); - perror(filetorun); + switch (my_screen.fb_type) { + case FBTYPE_SUN4COLOR: /* cg3 or cg6 */ + if (ioctl(FrameBufferFd, FBIOGATTR, &FBattr) >= 0) { + if (FBattr.real_type == FBTYPE_SUN3COLOR || /* cg3 */ + FBattr.real_type == FBTYPE_SUNFAST_COLOR) /* cg6 */ + { + filetorun = LDECOLOR; + } + } + break; + case FBTYPE_SUN2BW: /* bw2, cg4 or cg9 */ + filetorun = LDEMONO; + break; + case FBTYPE_SUN3COLOR: /* cg8 */ + if (ioctl(FrameBufferFd, FBIOGATTR, &FBattr) >= 0) { + if (FBattr.real_type == FBTYPE_MEMCOLOR) { filetorun = LDETRUECOLOR; } + } + break; + case FBTYPE_SUN2COLOR: /* cg2 */ + filetorun = LDEMONO; + break; + default: break; + } + if (filetorun == (char *)NULL) { + perror("lde: This Display Model is not supported\n"); + exit(1); + } + close(FrameBufferFd); +#endif /* USESUNSCREEN */ + run: + if (filetorun == NULL) { + fprintf(stderr, "Unable to determine what display program to run.\n"); + exit(1); + } + argv[0] = filetorun; + execvp(argv[0], argv); + perror(argv[0]); exit(1); + } From 11c7df58130cfe66d084a2563cdc11fb624a99dc Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Thu, 8 Dec 2022 13:03:12 -0800 Subject: [PATCH 58/68] Note display subsystem configuration in CMake output. --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 959e145..c088906 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,7 @@ IF(MAIKO_DISPLAY_X11) inc/xscroll.h inc/xwinmandefs.h ) + MESSAGE("-- Configured for X11 display") ENDIF() # according to: https://cmake.org/pipermail/cmake/2016-October/064342.html @@ -108,6 +109,7 @@ IF(MAIKO_DISPLAY_SDL) SET(MAIKO_DISPLAY_SDL_HDRS inc/sdldefs.h ) + MESSAGE("-- Configured for SDL display") ENDIF() IF(CMAKE_SYSTEM_NAME STREQUAL "SunOS") From 2474619e83efbbea737abe90660e921acf14da84 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Sun, 19 Feb 2023 10:43:55 -0800 Subject: [PATCH 59/68] Cleanup errors introduced resolving rebase conflicts. --- src/initdsp.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/initdsp.c b/src/initdsp.c index 285450e..05466af 100644 --- a/src/initdsp.c +++ b/src/initdsp.c @@ -57,7 +57,6 @@ extern DspInterface currentdsp; int FrameBufferFd = -1; extern int sdl_displaywidth, sdl_displayheight, sdl_pixelscale; -int sdl_displaywidth, sdl_displayheight, sdl_pixelscale; extern unsigned displaywidth, displayheight, DisplayRasterWidth, DisplayType, DisplayByteSize; unsigned displaywidth, displayheight, DisplayRasterWidth, DisplayType, DisplayByteSize; DLword *DisplayRegion68k; /* 68k addr of #{}22,0 */ @@ -265,7 +264,6 @@ in_display_segment(baseaddr) /* */ /************************************************************************/ -<<<<<<< HEAD void flush_display_buffer(void) { #ifdef SDL sdl_notify_damage(0, 0, sdl_displaywidth, sdl_displayheight); From 68d63cc080b7b012d17a81cebab92bcba4e093fc Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Wed, 3 May 2023 12:49:31 -0700 Subject: [PATCH 60/68] CMake warning: CMAKE_MINIMUM_REQUIRED must preceed PROJECT --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c088906..ba4beb4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ -PROJECT(maiko C) CMAKE_MINIMUM_REQUIRED(VERSION 3.15) +PROJECT(maiko C) SET(CMAKE_EXPORT_COMPILE_COMMANDS ON) From 6ce6aa25e175da4dbe0d9f55da736168145a3281 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Thu, 4 May 2023 12:40:21 -0700 Subject: [PATCH 61/68] Make X11 default display method for CMake builds when SDL branch merged --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba4beb4..32442da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,7 @@ SET(MAIKO_DEFINITIONS "-DRELEASE=351" ) -OPTION(MAIKO_DISPLAY_X11 "Use X11 for display." OFF) +OPTION(MAIKO_DISPLAY_X11 "Use X11 for display." ON) IF(MAIKO_DISPLAY_X11) FIND_PACKAGE(X11 REQUIRED) SET(MAIKO_DISPLAY_X11_DEFINITIONS @@ -96,7 +96,7 @@ ELSEIF(NOT MAIKO_NETWORK_TYPE STREQUAL "NONE") MESSAGE(WARNING "Invalid option given for MAIKO_NETWORK_TYPE, must be one of:\nNONE, SUN_DLPI, SUN_NIT, NETHUB") ENDIF() -OPTION(MAIKO_DISPLAY_SDL "Use SDL for display." ON) +OPTION(MAIKO_DISPLAY_SDL "Use SDL for display." OFF) IF(MAIKO_DISPLAY_SDL) FIND_PACKAGE(SDL2 REQUIRED) SET(MAIKO_DISPLAY_SDL_DEFINITIONS From d14d6183da967be1cd664bb375b377f560467145 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Thu, 4 May 2023 12:51:29 -0700 Subject: [PATCH 62/68] Comment out debug printf for mousewheel(trackpad) handling. --- src/sdl.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sdl.c b/src/sdl.c index c0f8c52..a82aaa3 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -656,9 +656,11 @@ void process_SDLevents() { break; } case SDL_MOUSEWHEEL: - printf("mousewheel mouse %d x %d y %d direction %s\n", event.wheel.which, event.wheel.x, + /* + printf("mousewheel mouse %d x %d y %d direction %s\n", event.wheel.which, event.wheel.x, event.wheel.y, event.wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped"); + */ break; /* case SDL_KEYMAPCHANGED: */ /* printf("SDL_KEYMAPCHANGED\n"); break; */ From a511feff39cebbd2e3dc2f9fbb6dd4a57953f522 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Thu, 4 May 2023 18:11:56 -0700 Subject: [PATCH 63/68] Resolve some warnings noted by clang-tidy Remove unused #include "miscstat.h" Make parameters of cursor_equal_p both const Remove unnecessary extern declarations for EmKbdAd068K, EmRealUtilin68K, MiscStats --- src/sdl.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index a82aaa3..a06fd99 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -6,7 +6,6 @@ #include "byteswapdefs.h" #include "lispemul.h" #include "lsptypes.h" -#include "miscstat.h" #include "keyboard.h" #include "lspglob.h" // for IOPage #include "display.h" // for CURSORHEIGHT, DisplayRegion68k @@ -272,7 +271,7 @@ static void replicate_bits(int bits, int reps, Uint8 *out) { } } -static int cursor_equal_p(DLword *a, DLword *b) { +static int cursor_equal_p(const DLword *a, const DLword *b) { for (int i = 0; i < CURSORHEIGHT; i++) if (a[i] != b[i]) return FALSE; return TRUE; @@ -400,7 +399,6 @@ void sdl_bitblt_to_texture_exact(int _x, int _y, int _w, int _h) { int dy = 0; // for each line in the source image for (int sy = ystart; sy < ylimit; sy += sourcepitchwords, dy += dstpitchpixels) { - int dx = 0; int sx = xstart; int b = xstartb; int srcw = GETBASEWORD(src, sy + sx); @@ -500,9 +498,8 @@ static void handle_keyup(SDL_Keycode k, unsigned short mod) { } } extern DLword *EmCursorX68K, *EmCursorY68K; -extern DLword *EmMouseX68K, *EmMouseY68K, *EmKbdAd068K, *EmRealUtilin68K; +extern DLword *EmMouseX68K, *EmMouseY68K; extern LispPTR *CLastUserActionCell68k; -extern MISCSTATS *MiscStats; /* bits within the EmRealUtilin word */ #define KEYSET_LEFT 8 From 45494170a3230335a63c2f303f4f0010b1e0a052 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Thu, 4 May 2023 18:47:34 -0700 Subject: [PATCH 64/68] Use SDL defined symbolic constants for mouse button events --- src/sdl.c | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/sdl.c b/src/sdl.c index a06fd99..367fd4e 100644 --- a/src/sdl.c +++ b/src/sdl.c @@ -623,30 +623,20 @@ void process_SDLevents() { break; } case SDL_MOUSEBUTTONDOWN: { - int button = event.button.button; - switch (button) { - case 1: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_LEFT, FALSE); break; - case 2: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_MIDDLE, FALSE); break; - case 3: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_RIGHT, FALSE); break; - case 4: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFT, FALSE); break; - case 5: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFTMIDDLE, FALSE); break; - case 6: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHT, FALSE); break; - case 7: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHTMIDDLE, FALSE); break; + switch (event.button.button) { + case SDL_BUTTON_LEFT: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_LEFT, FALSE); break; + case SDL_BUTTON_MIDDLE: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_MIDDLE, FALSE); break; + case SDL_BUTTON_RIGHT: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_RIGHT, FALSE); break; } DoRing(); if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; break; } case SDL_MOUSEBUTTONUP: { - int button = event.button.button; - switch (button) { - case 1: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_LEFT, TRUE); break; - case 2: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_MIDDLE, TRUE); break; - case 3: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_RIGHT, TRUE); break; - case 4: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFT, TRUE); break; - case 5: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFTMIDDLE, TRUE); break; - case 6: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHT, TRUE); break; - case 7: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHTMIDDLE, TRUE); break; + switch (event.button.button) { + case SDL_BUTTON_LEFT: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_LEFT, TRUE); break; + case SDL_BUTTON_MIDDLE: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_MIDDLE, TRUE); break; + case SDL_BUTTON_RIGHT: PUTBASEBIT68K(EmRealUtilin68K, MOUSE_RIGHT, TRUE); break; } DoRing(); if ((KBDEventFlg += 1) > 0) Irq_Stk_End = Irq_Stk_Check = 0; @@ -657,6 +647,15 @@ void process_SDLevents() { printf("mousewheel mouse %d x %d y %d direction %s\n", event.wheel.which, event.wheel.x, event.wheel.y, event.wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped"); + + these are the 4 key bits for mouse wheel/trackpad scrolling - which unlike X11 are + *not* presented as mouse button down/up events for each scroll action + + case 4: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFT, FALSE); break; + case 5: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_LEFTMIDDLE, FALSE); break; + case 6: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHT, FALSE); break; + case 7: PUTBASEBIT68K(EmRealUtilin68K, KEYSET_RIGHTMIDDLE, FALSE); break; + */ break; /* case SDL_KEYMAPCHANGED: */ From b265d3a917fe4aa7603dad68a5d01928caa82d39 Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Sat, 6 May 2023 13:36:00 -0700 Subject: [PATCH 65/68] Reorganize CMakeLists.txt to put display option selections together. --- CMakeLists.txt | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 32442da..2426353 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,8 @@ SET(MAIKO_DEFINITIONS ) OPTION(MAIKO_DISPLAY_X11 "Use X11 for display." ON) +OPTION(MAIKO_DISPLAY_SDL "Use SDL for display." OFF) + IF(MAIKO_DISPLAY_X11) FIND_PACKAGE(X11 REQUIRED) SET(MAIKO_DISPLAY_X11_DEFINITIONS @@ -75,6 +77,21 @@ IF(MAIKO_DISPLAY_X11) MESSAGE("-- Configured for X11 display") ENDIF() +IF(MAIKO_DISPLAY_SDL) + FIND_PACKAGE(SDL2 REQUIRED) + SET(MAIKO_DISPLAY_SDL_DEFINITIONS + "-DSDL" + ) + SET(MAIKO_DISPLAY_SDL_LIBRARIES ${SDL2_LIBRARIES}) + SET(MAIKO_DISPLAY_SDL_SRCS + src/sdl.c + ) + SET(MAIKO_DISPLAY_SDL_HDRS + inc/sdldefs.h + ) + MESSAGE("-- Configured for SDL display") +ENDIF() + # according to: https://cmake.org/pipermail/cmake/2016-October/064342.html # the following 2 lines should produce a dropdown-box in the cmake-gui # but this will happen only after running the command line version of cmake, @@ -96,21 +113,6 @@ ELSEIF(NOT MAIKO_NETWORK_TYPE STREQUAL "NONE") MESSAGE(WARNING "Invalid option given for MAIKO_NETWORK_TYPE, must be one of:\nNONE, SUN_DLPI, SUN_NIT, NETHUB") ENDIF() -OPTION(MAIKO_DISPLAY_SDL "Use SDL for display." OFF) -IF(MAIKO_DISPLAY_SDL) - FIND_PACKAGE(SDL2 REQUIRED) - SET(MAIKO_DISPLAY_SDL_DEFINITIONS - "-DSDL" - ) - SET(MAIKO_DISPLAY_SDL_LIBRARIES ${SDL2_LIBRARIES}) - SET(MAIKO_DISPLAY_SDL_SRCS - src/sdl.c - ) - SET(MAIKO_DISPLAY_SDL_HDRS - inc/sdldefs.h - ) - MESSAGE("-- Configured for SDL display") -ENDIF() IF(CMAKE_SYSTEM_NAME STREQUAL "SunOS") LIST(APPEND MAIKO_DEFINITIONS From 41ae0162da439bcd1912a9cb65aae5d655fe95cd Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Sun, 7 May 2023 15:10:01 -0700 Subject: [PATCH 66/68] Construct path for invoking display executable to match original path of lde The pathname for the display program is constructed to parallel the path for "lde", which makes it more likely that the correct display program will get run rather than (a) not being found because "lde" wasn't found from the PATH, or (b) a different version of the display program which was found from the PATH --- src/ldeboot.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/ldeboot.c b/src/ldeboot.c index 1fd356d..6e18927 100644 --- a/src/ldeboot.c +++ b/src/ldeboot.c @@ -12,6 +12,7 @@ #include <ctype.h> #include <fcntl.h> +#include <limits.h> // for PATH_MAX #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -59,6 +60,8 @@ int main(int argc, char *argv[]) { int i; char *filetorun = NULL; + char filetorunpath[PATH_MAX]; + char *dirsepp = NULL; char *displayName = (char *)NULL; int doFork = 1; #ifdef USESUNSCREEN @@ -187,7 +190,21 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Unable to determine what display program to run.\n"); exit(1); } - argv[0] = filetorun; + + /* construct invocation path of display program parallel to this one, + * so that it will have higher probability of finding a corresponding + * display program if there are multiple versions findable via PATH + */ + dirsepp = strrchr(argv[0], '/'); + if (dirsepp == NULL) { + argv[0] = filetorun; + } else { + /* copy up to and including the final "/" in the path */ + dirsepp = stpncpy(filetorunpath, argv[0], dirsepp + 1 - argv[0]); + /* dirsepp now points to the trailing null in the copy */ + strncpy(dirsepp, filetorun, PATH_MAX - (dirsepp - filetorunpath)); + argv[0] = filetorunpath; + } execvp(argv[0], argv); perror(argv[0]); exit(1); From e8daed41ea47760743f35ef5c928695c84cc6e7e Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Tue, 16 May 2023 18:52:23 -0700 Subject: [PATCH 67/68] UNIX-GETPARM(DISPLAY) should return "SDL" for SDL-based display, as it returns "X" for X11 --- src/uutils.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/uutils.c b/src/uutils.c index 5a6607b..e3a58b2 100644 --- a/src/uutils.c +++ b/src/uutils.c @@ -219,6 +219,8 @@ LispPTR unix_getparm(LispPTR *args) { } else if (strcmp(envname, "DISPLAY") == 0) { #if defined(XWINDOW) envvalue = "X"; +#elif defined(SDL) + envvalue = "SDL"; #elif defined(DISPLAYBUFFER) envvalue = "BUFFERED"; #else From a6d645ce9b16e56ef09b407ee1ebac7ca49716cd Mon Sep 17 00:00:00 2001 From: Nick Briggs <nicholas.h.briggs@gmail.com> Date: Tue, 16 May 2023 21:18:15 -0700 Subject: [PATCH 68/68] SunOS bug avoidances should only execute when on SunOS --- src/initkbd.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/initkbd.c b/src/initkbd.c index 3b891cd..0cf55b0 100644 --- a/src/initkbd.c +++ b/src/initkbd.c @@ -425,8 +425,10 @@ void keyboardtype(int fd) int i; char *key; +#if defined(OS4) for (i = 0; i < 5000; i++) { /* IDLE LOOP */ } /* because of a SunOS bug */ +#endif /* clear the keyboard field in devconfig */ InterfacePage->devconfig &= 0xfff8;