/* SDL3 window backend for the host browser harness. * * SDL3 selects the Wayland driver on this desktop (and falls back to X11 if * it cannot connect); the browser still owns a plain RGB565 canvas and only * sees the generic win_* ABI through host/syscall.c. */ #include #include #include #include #include #include "window.h" #include "cursor_abi.h" #define KEYQ_MAX 256 #define EVQ_MAX 64 static SDL_Window *win; static SDL_Renderer *ren; static SDL_Texture *tex; static unsigned short *fb; static int win_w, win_h; static int inited; static int focused; static int inside; static int close_sent; static int mouse_x, mouse_y; static unsigned mouse_buttons; static int scroll_x, scroll_y; static char backend[32] = "none"; static int keyq[KEYQ_MAX]; static unsigned int key_head, key_tail; static struct win_event evq[EVQ_MAX]; static unsigned int ev_head, ev_tail; static void push_key(int k) { unsigned int next = (key_head + 1) % KEYQ_MAX; if (next == key_tail) return; keyq[key_head] = k; key_head = next; } static int pop_key(void) { int k; if (key_tail == key_head) return 0; k = keyq[key_tail]; key_tail = (key_tail + 1) % KEYQ_MAX; return k; } static void push_event(unsigned int type, int x, int y, unsigned int button) { unsigned int next = (ev_head + 1) % EVQ_MAX; struct win_event *ev; if (next == ev_tail) return; ev = &evq[ev_head]; ev->type = type; ev->x = x; ev->y = y; ev->buttons = mouse_buttons; ev->button = button; ev_head = next; } static unsigned int neb_buttons(Uint32 state) { unsigned int b = 0; if (state & SDL_BUTTON_LMASK) b |= 1; if (state & SDL_BUTTON_RMASK) b |= 2; if (state & SDL_BUTTON_MMASK) b |= 4; return b; } static void set_mouse(float x, float y, Uint32 state) { mouse_x = (int)x; mouse_y = (int)y; mouse_buttons = neb_buttons(state); inside = mouse_x >= 0 && mouse_y >= 0 && mouse_x < win_w && mouse_y < win_h; } static void key_down(const SDL_KeyboardEvent *e) { SDL_Keycode k = e->key; SDL_Keymod mod = e->mod; int ctrl = (mod & SDL_KMOD_CTRL) != 0; /* Ctrl+letter -> ASCII control code (Ctrl+L = 0x0C, Ctrl+R = 0x12). */ if (ctrl && k >= SDLK_A && k <= SDLK_Z) { push_key((int)(k - SDLK_A + 1)); return; } switch (k) { case SDLK_ESCAPE: push_key(0x1B); return; case SDLK_BACKSPACE: push_key(ctrl ? 0x10C : 0x08); return; case SDLK_TAB: push_key(0x09); return; case SDLK_RETURN: case SDLK_KP_ENTER: push_key('\n'); return; case SDLK_DELETE: push_key(ctrl ? 0x10B : 0x108); return; case SDLK_LEFT: push_key(ctrl ? 0x109 : 0x100); return; case SDLK_RIGHT: push_key(ctrl ? 0x10A : 0x101); return; case SDLK_UP: push_key(0x102); return; case SDLK_DOWN: push_key(0x103); return; case SDLK_PAGEUP: push_key(0x104); return; case SDLK_PAGEDOWN: push_key(0x105); return; case SDLK_HOME: push_key(0x106); return; case SDLK_END: push_key(0x107); return; default: break; } /* Printable characters arrive through SDL_EVENT_TEXT_INPUT, which * honours the keyboard layout and key repeat. */ } static void text_utf8(const char *s) { const unsigned char *p = (const unsigned char *)s; while (p && *p) { unsigned int cp; int n; if (*p < 0x80) { cp = *p++; n = 0; } else if ((*p & 0xE0) == 0xC0) { cp = *p++ & 0x1F; n = 1; } else if ((*p & 0xF0) == 0xE0) { cp = *p++ & 0x0F; n = 2; } else if ((*p & 0xF8) == 0xF0) { cp = *p++ & 0x07; n = 3; } else { p++; continue; } while (n-- > 0) { if ((*p & 0xC0) != 0x80) { cp = 0xFFFD; break; } cp = (cp << 6) | (*p++ & 0x3F); } if (cp == '\r' || cp == '\n') push_key('\n'); else if (cp >= 0x20 && cp <= 0xFF) push_key((int)cp); else if (cp > 0xFF) push_key('?'); } } static void handle_event(SDL_Event *e) { switch (e->type) { case SDL_EVENT_QUIT: case SDL_EVENT_WINDOW_CLOSE_REQUESTED: if (!close_sent) { close_sent = 1; push_key(0x1B); } break; case SDL_EVENT_WINDOW_FOCUS_GAINED: focused = 1; break; case SDL_EVENT_WINDOW_FOCUS_LOST: focused = 0; break; case SDL_EVENT_WINDOW_MOUSE_ENTER: inside = 1; break; case SDL_EVENT_WINDOW_MOUSE_LEAVE: inside = 0; break; case SDL_EVENT_MOUSE_MOTION: set_mouse(e->motion.x, e->motion.y, e->motion.state); break; case SDL_EVENT_MOUSE_BUTTON_DOWN: case SDL_EVENT_MOUSE_BUTTON_UP: { unsigned int bit = 0; if (e->button.button == SDL_BUTTON_LEFT) bit = 1; else if (e->button.button == SDL_BUTTON_RIGHT) bit = 2; else if (e->button.button == SDL_BUTTON_MIDDLE) bit = 4; mouse_x = (int)e->button.x; mouse_y = (int)e->button.y; inside = mouse_x >= 0 && mouse_y >= 0 && mouse_x < win_w && mouse_y < win_h; if (bit) { if (e->type == SDL_EVENT_MOUSE_BUTTON_DOWN) mouse_buttons |= bit; else mouse_buttons &= ~bit; push_event(e->type == SDL_EVENT_MOUSE_BUTTON_DOWN ? WIN_EV_POINTER_DOWN : WIN_EV_POINTER_UP, mouse_x, mouse_y, bit); } break; } case SDL_EVENT_MOUSE_WHEEL: /* SDL's y is positive away from the user; the browser wants * +dy = later content. */ scroll_x += (int)(-e->wheel.x * 48.0f); scroll_y += (int)(-e->wheel.y * 48.0f); break; case SDL_EVENT_KEY_DOWN: key_down(&e->key); break; case SDL_EVENT_TEXT_INPUT: text_utf8(e->text.text); break; default: break; } } int host_sdl_open(unsigned int w, unsigned int h, const char *title) { const char *drv; if (inited) return (win_w == (int)w && win_h == (int)h) ? 0 : -1; if (!SDL_Init(SDL_INIT_VIDEO)) { fprintf(stderr, "browser-host: SDL_Init: %s\n", SDL_GetError()); return -1; } drv = SDL_GetCurrentVideoDriver(); snprintf(backend, sizeof(backend), "%s", drv ? drv : "?"); win = SDL_CreateWindow(title ? title : "Browser", (int)w, (int)h, 0); if (!win) { fprintf(stderr, "browser-host: SDL_CreateWindow: %s\n", SDL_GetError()); SDL_Quit(); return -1; } ren = SDL_CreateRenderer(win, 0); if (!ren) ren = SDL_CreateRenderer(win, "software"); if (!ren) { fprintf(stderr, "browser-host: SDL_CreateRenderer: %s\n", SDL_GetError()); SDL_DestroyWindow(win); win = 0; SDL_Quit(); return -1; } SDL_SetRenderVSync(ren, 0); tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING, (int)w, (int)h); if (!tex) { fprintf(stderr, "browser-host: SDL_CreateTexture: %s\n", SDL_GetError()); SDL_DestroyRenderer(ren); ren = 0; SDL_DestroyWindow(win); win = 0; SDL_Quit(); return -1; } SDL_SetTextureScaleMode(tex, SDL_SCALEMODE_NEAREST); SDL_SetWindowResizable(win, false); SDL_SetWindowMinimumSize(win, (int)w, (int)h); SDL_SetWindowMaximumSize(win, (int)w, (int)h); SDL_StartTextInput(win); fb = (unsigned short *)calloc((size_t)w * h, sizeof(unsigned short)); if (!fb) { SDL_DestroyTexture(tex); tex = 0; SDL_DestroyRenderer(ren); ren = 0; SDL_DestroyWindow(win); win = 0; SDL_Quit(); return -1; } win_w = (int)w; win_h = (int)h; focused = 1; inside = 1; inited = 1; fprintf(stderr, "browser-host: video driver=%s window=%ux%u\n", backend, w, h); return 0; } void host_sdl_close(void) { if (!inited) return; SDL_StopTextInput(win); free(fb); fb = 0; SDL_DestroyTexture(tex); tex = 0; SDL_DestroyRenderer(ren); ren = 0; SDL_DestroyWindow(win); win = 0; SDL_Quit(); inited = 0; } void *host_sdl_fb(void) { return fb; } unsigned int host_sdl_width(void) { return (unsigned int)win_w; } unsigned int host_sdl_height(void) { return (unsigned int)win_h; } void host_sdl_present(void) { if (!inited || !tex) return; if (!SDL_UpdateTexture(tex, 0, fb, win_w * (int)sizeof(unsigned short))) return; SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); SDL_RenderClear(ren); SDL_RenderTexture(ren, tex, 0, 0); SDL_RenderPresent(ren); } void host_sdl_pump(unsigned int timeout_ms) { SDL_Event e; if (!inited) { if (timeout_ms) usleep(timeout_ms * 1000u); return; } if (timeout_ms) { if (SDL_WaitEventTimeout(&e, (Sint32)timeout_ms)) handle_event(&e); } else { SDL_PumpEvents(); } while (SDL_PollEvent(&e)) handle_event(&e); } int host_sdl_pointer(struct win_pointer *out) { SDL_WindowFlags flags; if (!inited) return -1; flags = SDL_GetWindowFlags(win); focused = (flags & SDL_WINDOW_INPUT_FOCUS) ? 1 : 0; out->x = mouse_x; out->y = mouse_y; out->buttons = mouse_buttons; out->inside = inside ? 1u : 0u; out->focused = focused ? 1u : 0u; return 0; } int host_sdl_event(struct win_event *out) { if (ev_tail == ev_head) return 0; *out = evq[ev_tail]; ev_tail = (ev_tail + 1) % EVQ_MAX; return 1; } int host_sdl_scroll(struct win_scroll *out) { if (!scroll_x && !scroll_y) return 0; out->dx = scroll_x; out->dy = scroll_y; scroll_x = scroll_y = 0; return 1; } int host_sdl_key(void) { return pop_key(); } /* NebOS cursor shapes on the desktop's own cursor theme. SDL has no * help/zoom/copy/... system cursors: those fall back to the arrow. */ void host_sdl_set_cursor(int shape) { static SDL_Cursor *cache[CURSOR_COUNT]; static int current = -1; SDL_SystemCursor sc = SDL_SYSTEM_CURSOR_DEFAULT; if (shape < 0 || shape >= CURSOR_COUNT || shape == current) return; switch (shape) { case CURSOR_POINTER: sc = SDL_SYSTEM_CURSOR_POINTER; break; case CURSOR_TEXT: case CURSOR_VERTICAL_TEXT: sc = SDL_SYSTEM_CURSOR_TEXT; break; case CURSOR_WAIT: sc = SDL_SYSTEM_CURSOR_WAIT; break; case CURSOR_PROGRESS: sc = SDL_SYSTEM_CURSOR_PROGRESS; break; case CURSOR_CROSSHAIR: case CURSOR_CELL: sc = SDL_SYSTEM_CURSOR_CROSSHAIR; break; case CURSOR_MOVE: case CURSOR_ALL_SCROLL: case CURSOR_ALL_RESIZE: sc = SDL_SYSTEM_CURSOR_MOVE; break; case CURSOR_NOT_ALLOWED: case CURSOR_NO_DROP: sc = SDL_SYSTEM_CURSOR_NOT_ALLOWED; break; case CURSOR_N_RESIZE: sc = SDL_SYSTEM_CURSOR_N_RESIZE; break; case CURSOR_E_RESIZE: sc = SDL_SYSTEM_CURSOR_E_RESIZE; break; case CURSOR_S_RESIZE: sc = SDL_SYSTEM_CURSOR_S_RESIZE; break; case CURSOR_W_RESIZE: sc = SDL_SYSTEM_CURSOR_W_RESIZE; break; case CURSOR_NE_RESIZE: sc = SDL_SYSTEM_CURSOR_NE_RESIZE; break; case CURSOR_NW_RESIZE: sc = SDL_SYSTEM_CURSOR_NW_RESIZE; break; case CURSOR_SE_RESIZE: sc = SDL_SYSTEM_CURSOR_SE_RESIZE; break; case CURSOR_SW_RESIZE: sc = SDL_SYSTEM_CURSOR_SW_RESIZE; break; case CURSOR_EW_RESIZE: case CURSOR_COL_RESIZE: sc = SDL_SYSTEM_CURSOR_EW_RESIZE; break; case CURSOR_NS_RESIZE: case CURSOR_ROW_RESIZE: sc = SDL_SYSTEM_CURSOR_NS_RESIZE; break; case CURSOR_NESW_RESIZE: sc = SDL_SYSTEM_CURSOR_NESW_RESIZE; break; case CURSOR_NWSE_RESIZE: sc = SDL_SYSTEM_CURSOR_NWSE_RESIZE; break; default: break; } if (!cache[shape]) cache[shape] = SDL_CreateSystemCursor(sc); if (cache[shape]) SDL_SetCursor(cache[shape]); current = shape; } int host_sdl_screen(unsigned int *w, unsigned int *h) { SDL_Rect r; if (SDL_GetDisplayBounds(SDL_GetPrimaryDisplay(), &r)) { *w = (unsigned int)r.w; *h = (unsigned int)r.h; } else { *w = (unsigned int)win_w; *h = (unsigned int)win_h; } return 0; } int host_sdl_key_wait(unsigned int timeout_ms) { int k = pop_key(); if (k) return k; host_sdl_pump(timeout_ms); return pop_key(); } const char *host_sdl_backend(void) { return backend; }