From: Robert Pengelly Date: Sun, 25 Oct 2026 05:25:49 +0000 (+0000) Subject: Add native VGA output for Wayland, X11, Win32 and Cocoa X-Git-Url: https://git.candlhat.org/?a=commitdiff_plain;h=54431721917e20a28f6138d75e87f290ac6b0c30;p=dosemu.git Add native VGA output for Wayland, X11, Win32 and Cocoa --- diff --git a/Makefile.unix b/Makefile.unix index e60ea3b..4e06735 100755 --- a/Makefile.unix +++ b/Makefile.unix @@ -10,13 +10,17 @@ CFLAGS := -D_FILE_OFFSET_BITS=64 -O2 -Wall -Werror -Wextra CC_SRC := $(wildcard $(SRCDIR)/*.c) ifeq ($(OS), Windows_NT) -LIBS := -lm -lwinmm +LIBS := -lm -lwinmm -luser32 -lgdi32 else ifeq ($(shell uname -s), Darwin) -CFLAGS += -pthread -LIBS := -lm -framework AudioToolbox +CC_SRC += $(wildcard $(SRCDIR)/*.m) +LIBS := -lm -framework AudioToolbox -framework Cocoa else ifeq ($(shell uname -s), Linux) CFLAGS += -pthread LIBS := -lm -ldl +ifeq ($(shell pkg-config --exists x11 && echo yes),yes) +CFLAGS += -DHAVE_X11 +LIBS += $(shell pkg-config --libs x11) +endif else LIBS := -lm endif diff --git a/Makefile.w32 b/Makefile.w32 index a2e78d1..3839b97 100755 --- a/Makefile.w32 +++ b/Makefile.w32 @@ -13,7 +13,7 @@ all: dosemu.exe dosemu.exe: $(CC_SRC) - $(CC) $(CFLAGS) -o $@ $^ -lwinmm + $(CC) $(CFLAGS) -o $@ $^ -lwinmm -luser32 -lgdi32 clean: diff --git a/README.md b/README.md index c6efbf1..1e51531 100755 --- a/README.md +++ b/README.md @@ -44,11 +44,11 @@ ### Windows - set DOS_VER=xx.xx + set DOSEMU_VER=xx.xx ### Linux, macOS or BSD - export DOS_VER=xx.xx + export DOSEMU_VER=xx.xx Currently the only programs that have been tested are: diff --git a/interrupts.c b/interrupts.c index d7345ab..23c8785 100644 --- a/interrupts.c +++ b/interrupts.c @@ -36,6 +36,7 @@ #include "interrupts.h" #include "loader.h" #include "psp.h" +#include "video_native.h" #if defined (_WIN32) static int host_lstat (const char *path, struct stat *st) { @@ -2334,7 +2335,7 @@ void bios_keyboard_poll (CPU8086 *cpu) { uint8_t scan; /* Ordinary DOS and BIOS keyboard reads still use the existing input path. */ - if (!cpu->vga_graphics_mode || cpu->kbd_scancode_pending || cpu->kbd_release_scancode) { + if (!cpu->vga_graphics_mode || video_native_active () || cpu->kbd_scancode_pending || cpu->kbd_release_scancode) { return; } @@ -2383,7 +2384,7 @@ static uint16_t dos_get_version (void) { uint16_t ver = 0x0005; const char *env_p; - if ((env_p = getenv ("DOS_VER"))) { + if ((env_p = getenv ("DOSEMU_VER"))) { if (!strchr (env_p, '.') && env_p[0] == '0') { @@ -9086,7 +9087,7 @@ static void int_21 (CPU8086 *cpu) { uint32_t source = (uint32_t) parent_psp << 4; uint32_t destination = (uint32_t) child_psp << 4; - const char *version = getenv ("DOS_VER"); + const char *version = getenv ("DOSEMU_VER"); unsigned long major = 5; uint8_t contents[256]; @@ -9554,8 +9555,11 @@ static int bios_graphics_visible; static unsigned bios_graphics_columns, bios_graphics_rows; static uint64_t bios_graphics_last_refresh; +#ifndef _WIN32 static unsigned bios_graphics_refresh_checks; -static uint16_t bios_graphics_cells[100][160]; +#endif + +static uint32_t bios_graphics_cells[100][160][2]; static uint8_t bios_graphics_palette[256][3]; static uint8_t bios_graphics_dac_mask; @@ -9601,6 +9605,39 @@ static void bios_graphics_viewport (unsigned *columns, unsigned *rows) { } +static uint32_t bios_graphics_sample (CPU8086 *cpu, unsigned col, unsigned line, unsigned columns, unsigned lines) { + + unsigned x0 = col * 320, x1 = (col + 1) * 320; + unsigned y0 = line * 200, y1 = (line + 1) * 200; + unsigned red = 0, green = 0, blue = 0; + unsigned x, y; + + for (y = y0 / lines; y < (y1 + lines - 1) / lines; y++) { + + unsigned sy0 = y * lines, sy1 = (y + 1) * lines; + unsigned height = (sy1 < y1 ? sy1 : y1) - (sy0 > y0 ? sy0 : y0); + + for (x = x0 / columns; x < (x1 + columns - 1) / columns; x++) { + + unsigned sx0 = x * columns, sx1 = (x + 1) * columns; + unsigned width = (sx1 < x1 ? sx1 : x1) - (sx0 > x0 ? sx0 : x0); + unsigned weight = width * height; + + uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; + const uint8_t *rgb = cpu->vga_dac_palette[index]; + + red += weight * rgb[0]; + green += weight * rgb[1]; + blue += weight * rgb[2]; + + } + + } + + return (((red + 32000) / 64000) << 16) | (((green + 32000) / 64000) << 8) | ((blue + 32000) / 64000); + +} + static void bios_graphics_refresh (CPU8086 *cpu) { uint64_t milliseconds; @@ -9612,21 +9649,26 @@ static void bios_graphics_refresh (CPU8086 *cpu) { int output_started = 0; int color_valid = 0; - uint8_t last_top = 0, last_bottom = 0; + uint32_t last_top = 0, last_bottom = 0; #if defined (_WIN32) HANDLE handle = GetStdHandle (STD_OUTPUT_HANDLE); DWORD output_mode; - int vt_console = handle != NULL && handle != INVALID_HANDLE_VALUE && GetConsoleMode (handle, &output_mode) && (output_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING); + int vt_console = handle != NULL && handle != INVALID_HANDLE_VALUE && GetConsoleMode (handle, &output_mode) && (output_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING); +#else + struct timeval now; +#endif - if (handle == NULL || handle == INVALID_HANDLE_VALUE || !GetConsoleMode (handle, &output_mode)) { + if (video_native_present (cpu)) { return; } +#if defined (_WIN32) + if (handle == NULL || handle == INVALID_HANDLE_VALUE || !GetConsoleMode (handle, &output_mode)) { + return; + } #else - struct timeval now; - if (!isatty (STDOUT_FILENO)) { return; } @@ -9703,20 +9745,16 @@ static void bios_graphics_refresh (CPU8086 *cpu) { for (col = 0; col < columns; col++) { - unsigned x = (col * 320 + 160) / columns; - unsigned y = (row * 200 + 100) / rows; - - uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; - const uint8_t *rgb = cpu->vga_dac_palette[index]; + uint32_t rgb = bios_graphics_sample (cpu, col, row, columns, rows); unsigned best = 0, best_distance = ~0u; unsigned i; for (i = 0; i < 16; i++) { - int dr = (int) rgb[0] - vga16[i][0]; - int dg = (int) rgb[1] - vga16[i][1]; - int db = (int) rgb[2] - vga16[i][2]; + int dr = (int) ((rgb >> 16) & 0xFF) - vga16[i][0]; + int dg = (int) ((rgb >> 8) & 0xFF) - vga16[i][1]; + int db = (int) (rgb & 0xFF) - vga16[i][2]; unsigned distance = (unsigned) (dr * dr + dg * dg + db * db); @@ -9745,6 +9783,7 @@ static void bios_graphics_refresh (CPU8086 *cpu) { origin.Y = 0; region = info.srWindow; + region.Right = (SHORT) (region.Left + columns - 1); region.Bottom = (SHORT) (region.Top + rows - 1); @@ -9768,36 +9807,18 @@ static void bios_graphics_refresh (CPU8086 *cpu) { for (col = 0; col < columns; col++) { - unsigned x = (col * 320 + 160) / columns; - unsigned y_top = (row * 200 + rows / 2) / rows; - unsigned y_bottom = ((row * 2 + 1) * 200 + rows) / (rows * 2); - - uint8_t top, bottom; - const uint8_t *foreground, *background; + uint32_t top = bios_graphics_sample (cpu, col, row * 2, columns, rows * 2); + uint32_t bottom = bios_graphics_sample (cpu, col, row * 2 + 1, columns, rows * 2); - if (x >= 320) { - x = 319; - } - - if (y_top >= 200) { - y_top = 199; - } - - if (y_bottom >= 200) { - y_bottom = 199; - } - - top = mem_vga_pixel (cpu, x, y_top) & cpu->vga_dac_mask; - bottom = mem_vga_pixel (cpu, x, y_bottom) & cpu->vga_dac_mask; - - if (!full_redraw && bios_graphics_cells[row][col] == ((uint16_t) top | ((uint16_t) bottom << 8))) { + if (!full_redraw && bios_graphics_cells[row][col][0] == top && bios_graphics_cells[row][col][1] == bottom) { next_col = columns; continue; } - bios_graphics_cells[row][col] = (uint16_t) top | ((uint16_t) bottom << 8); + bios_graphics_cells[row][col][0] = top; + bios_graphics_cells[row][col][1] = bottom; if (!output_started) { @@ -9819,16 +9840,13 @@ static void bios_graphics_refresh (CPU8086 *cpu) { } - foreground = cpu->vga_dac_palette[top]; - background = cpu->vga_dac_palette[bottom]; - fprintf (stdout, "\033[38;2;%u;%u;%u;48;2;%u;%u;%um\342\226\200", - (unsigned) ((foreground[0] << 2) | (foreground[0] >> 4)), - (unsigned) ((foreground[1] << 2) | (foreground[1] >> 4)), - (unsigned) ((foreground[2] << 2) | (foreground[2] >> 4)), - (unsigned) ((background[0] << 2) | (background[0] >> 4)), - (unsigned) ((background[1] << 2) | (background[1] >> 4)), - (unsigned) ((background[2] << 2) | (background[2] >> 4))); + (unsigned) ((((top >> 16) & 0xFF) << 2) | (((top >> 16) & 0xFF) >> 4)), + (unsigned) ((((top >> 8) & 0xFF) << 2) | (((top >> 8) & 0xFF) >> 4)), + (unsigned) (((top & 0xFF) << 2) | ((top & 0xFF) >> 4)), + (unsigned) ((((bottom >> 16) & 0xFF) << 2) | (((bottom >> 16) & 0xFF) >> 4)), + (unsigned) ((((bottom >> 8) & 0xFF) << 2) | (((bottom >> 8) & 0xFF) >> 4)), + (unsigned) (((bottom & 0xFF) << 2) | ((bottom & 0xFF) >> 4))); last_bottom = bottom; last_top = top; @@ -10977,6 +10995,10 @@ void bios_video_refresh (CPU8086 *cpu) { } + if (video_native_active ()) { + video_native_shutdown (); + } + if (bios_graphics_visible) { bios_graphics_visible = 0; @@ -11393,8 +11415,8 @@ static void int_1A (CPU8086 *cpu) { */ void init_ivt (CPU8086 *cpu) { - const char *env_long_paths = getenv ("DOS_LONG_PATHS"); - const char *env_p = getenv ("DOS_FCB_START"); + const char *env_long_paths = getenv ("DOSEMU_LONG_PATHS"); + const char *env_p = getenv ("DOSEMU_ROOT"); uint32_t stub_base = (uint32_t) DOS_IVT_STUB_SEG << 4; unsigned int i; diff --git a/main.c b/main.c index ea43dbe..3634299 100644 --- a/main.c +++ b/main.c @@ -29,6 +29,7 @@ #include "memory.h" #include "psp.h" #include "speaker.h" +#include "video_native.h" #define CONVENTIONAL_MEM_SIZE (640UL << 10) #define EXE_LOAD_SEG 0x0060 @@ -509,6 +510,13 @@ int main (int argc, char **argv) { } + if (atexit (video_native_shutdown) != 0) { + + free (mem); + return 1; + + } + if (atexit (restore_terminal_cursor) != 0) { fprintf (stderr, "Failed to register terminal cursor cleanup\n"); @@ -554,7 +562,16 @@ int main (int argc, char **argv) { } if ((steps & 0x3FFF) == 0) { + + if (video_native_poll (&cpu) < 0) { + + cpu.halted = 1; + break; + + } + bios_keyboard_poll (&cpu); + } rc = cpu_step (&cpu); diff --git a/video_native.h b/video_native.h new file mode 100644 index 0000000..ce29af3 --- /dev/null +++ b/video_native.h @@ -0,0 +1,15 @@ +/****************************************************************************** + * @file video_native.h + *****************************************************************************/ +#ifndef VIDEO_NATIVE_H +#define VIDEO_NATIVE_H + +#include "cpu.h" + +int video_native_present (CPU8086 *cpu); +int video_native_active (void); +int video_native_poll (CPU8086 *cpu); + +void video_native_shutdown (void); + +#endif diff --git a/video_native_linux.c b/video_native_linux.c new file mode 100644 index 0000000..f602452 --- /dev/null +++ b/video_native_linux.c @@ -0,0 +1,1398 @@ +/****************************************************************************** + * @file video_native_linux.c + *****************************************************************************/ +#if defined (__linux__) && !defined (__ANDROID__) + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined (HAVE_X11) +# include +# include +# include +#endif + +#include + +#include "video_native.h" +#include "memory.h" + +#define VIDEO_WIDTH 640 +#define VIDEO_HEIGHT 400 +#define VIDEO_MAX_WIDTH 1920 +#define VIDEO_MAX_HEIGHT 1200 + +/* The Wayland core client ABI is loaded at runtime; no scanner or SDK is needed. */ +struct wl_proxy; +struct wl_display; + +struct wl_interface { + + const char *name; + + int version; + int method_count; + + const struct wl_message *methods; + int event_count; + + const struct wl_message *events; + +}; + +struct wl_message { + + const char *name; + const char *signature; + + const struct wl_interface **types; + +}; + +static const struct wl_message xdg_wm_base_requests[] = { + + { "destroy", "", NULL }, + { "create_positioner", "n", NULL }, + { "get_xdg_surface", "no", NULL }, + { "pong", "u", NULL } + +}; + +static const struct wl_message xdg_wm_base_events[] = { + { "ping", "u", NULL } +}; + +static const struct wl_message xdg_surface_requests[] = { + + { "destroy", "", NULL }, + { "get_toplevel", "n", NULL }, + { "get_popup", "noo", NULL }, + { "set_window_geometry", "iiii", NULL }, + { "ack_configure", "u", NULL } + +}; + +static const struct wl_message xdg_surface_events[] = { + { "configure", "u", NULL } +}; + +static const struct wl_message xdg_toplevel_requests[] = { + + { "destroy", "", NULL }, + { "set_parent", "?o", NULL }, + { "set_title", "s", NULL }, + { "set_app_id", "s", NULL }, + { "show_window_menu", "ouii", NULL }, + { "move", "ou", NULL }, + { "resize", "ouu", NULL }, + { "set_max_size", "ii", NULL }, + { "set_min_size", "ii", NULL }, + { "set_maximized", "", NULL }, + { "unset_maximized", "", NULL }, + { "set_fullscreen", "?o", NULL }, + { "unset_fullscreen", "", NULL }, + { "set_minimized", "", NULL } + +}; + +static const struct wl_message xdg_toplevel_events[] = { + + { "configure", "iia", NULL }, + { "close", "", NULL }, + { "configure_bounds", "2ii", NULL }, + { "wm_capabilities", "5a", NULL } + +}; + +static const struct wl_interface xdg_wm_base_interface = { + "xdg_wm_base", 1, 4, xdg_wm_base_requests, 1, xdg_wm_base_events +}; + +static const struct wl_interface xdg_surface_interface = { + "xdg_surface", 1, 5, xdg_surface_requests, 1, xdg_surface_events +}; + +static const struct wl_interface xdg_toplevel_interface = { + "xdg_toplevel", 1, 14, xdg_toplevel_requests, 2, xdg_toplevel_events +}; + +static struct { + + void *library; + + struct wl_display *display; + struct wl_proxy *registry, *compositor, *shm, *seat, *keyboard; + struct wl_proxy *surface, *wm_base, *xdg_surface, *toplevel; + struct wl_proxy *pool, *buffers[2]; + + unsigned char *pixels; + size_t buffer_size; + + int busy[2]; + int width, height, configured, closed; + int fd; + + const struct wl_interface *registry_interface, *compositor_interface; + const struct wl_interface *surface_interface, *shm_interface, *pool_interface; + const struct wl_interface *buffer_interface, *seat_interface, *keyboard_interface; + + struct wl_display *(*connect) (const char *); + void (*disconnect) (struct wl_display *); + + int (*roundtrip) (struct wl_display *); + int (*dispatch) (struct wl_display *); + int (*dispatch_pending) (struct wl_display *); + int (*flush) (struct wl_display *); + int (*get_fd) (struct wl_display *); + int (*add_listener) (struct wl_proxy *, void (**)(void), void *); + + void (*destroy) (struct wl_proxy *); + + uint32_t (*version) (struct wl_proxy *); + struct wl_proxy *(*marshal) (struct wl_proxy *, uint32_t, const struct wl_interface *, uint32_t, uint32_t, ...); + +} wayland; + +static struct { + +#if defined (HAVE_X11) + Display *display; + Window window; + Atom close_atom; + GC gc; + XImage *image; +#endif + int width, height, redraw; + +} x11; + +static int backend; +static int attempted; +static int closed; + +static uint64_t last_refresh; + +static uint8_t previous_palette[256][3]; +static uint8_t previous_mask; +static uint8_t key_queue[128]; + +static unsigned key_head, key_tail; + +static uint64_t video_milliseconds (void) { + + struct timeval now; + gettimeofday (&now, NULL); + + return (uint64_t) now.tv_sec * 1000 + now.tv_usec / 1000; + +} + +static void video_key (uint8_t scan, int pressed) { + + unsigned next = (key_tail + 1) % sizeof (key_queue); + + if (!scan || next == key_head) { + return; + } + + key_queue[key_tail] = pressed ? scan : (uint8_t) (scan | 0x80); + key_tail = next; + +} + +static uint8_t video_evdev_scan (uint32_t code) { + + if (code <= KEY_F10) { + return (uint8_t) code; + } + + switch (code) { + + case KEY_UP: + + return 0x48; + + case KEY_DOWN: + + return 0x50; + + case KEY_LEFT: + + return 0x4B; + + case KEY_RIGHT: + + return 0x4D; + + case KEY_HOME: + + return 0x47; + + case KEY_END: + + return 0x4F; + + case KEY_PAGEUP: + + return 0x49; + + case KEY_PAGEDOWN: + + return 0x51; + + case KEY_INSERT: + + return 0x52; + + case KEY_DELETE: + + return 0x53; + + case KEY_F11: + + return 0x57; + + case KEY_F12: + + return 0x58; + + case KEY_RIGHTCTRL: + + return 0x1D; + + case KEY_RIGHTALT: + + return 0x38; + + case KEY_KPENTER: + + return 0x1C; + + } + + return 0; + +} + +#if defined (HAVE_X11) +static uint8_t video_x11_scan (KeySym key) { + + if (key >= XK_a && key <= XK_z) { + + static const uint8_t letters[] = { + + 0x1E, 0x30, 0x2E, 0x20, 0x12, 0x21, 0x22, 0x23, 0x17, + 0x24, 0x25, 0x26, 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, + 0x1F, 0x14, 0x16, 0x2F, 0x11, 0x2D, 0x15, 0x2C + + }; + + return letters[key - XK_a]; + + } + + if (key >= XK_A && key <= XK_Z) { + return video_x11_scan (key - XK_A + XK_a); + } + + if (key >= XK_1 && key <= XK_9) { + return (uint8_t) (key - XK_1 + 2); + } + + switch (key) { + + case XK_0: + + return 0x0B; + + case XK_Escape: + + return 0x01; + + case XK_BackSpace: + + return 0x0E; + + case XK_Tab: + + return 0x0F; + + case XK_Return: case XK_KP_Enter: + + return 0x1C; + + case XK_space: + + return 0x39; + + case XK_Left: + + return 0x4B; + + case XK_Right: + + return 0x4D; + + case XK_Up: + + return 0x48; + + case XK_Down: + + return 0x50; + + case XK_Shift_L: + + return 0x2A; + + case XK_Shift_R: + + return 0x36; + + case XK_Control_L: case XK_Control_R: + + return 0x1D; + + case XK_Alt_L: case XK_Alt_R: + + return 0x38; + + case XK_F1: + + return 0x3B; + + case XK_F2: + + return 0x3C; + + case XK_F3: + + return 0x3D; + + case XK_F4: + + return 0x3E; + + case XK_F5: + + return 0x3F; + + case XK_F6: + + return 0x40; + + case XK_F7: + + return 0x41; + + case XK_F8: + + return 0x42; + + case XK_F9: + + return 0x43; + + case XK_F10: + + return 0x44; + + case XK_F11: + + return 0x57; + + case XK_F12: + + return 0x58; + + case XK_minus: + + return 0x0C; + + case XK_equal: + + return 0x0D; + + case XK_bracketleft: + + return 0x1A; + + case XK_bracketright: + + return 0x1B; + + case XK_semicolon: + + return 0x27; + + case XK_apostrophe: + + return 0x28; + + case XK_grave: + + return 0x29; + + case XK_backslash: + + return 0x2B; + + case XK_comma: + + return 0x33; + + case XK_period: + + return 0x34; + + case XK_slash: + + return 0x35; + + case XK_Delete: + + return 0x53; + + case XK_Home: + + return 0x47; + + case XK_End: + + return 0x4F; + + case XK_Page_Up: + + return 0x49; + + case XK_Page_Down: + + return 0x51; + + case XK_Insert: + + return 0x52; + + } + + return 0; + +} + +static int video_x11_initialize (void) { + + int screen; + + XSetWindowAttributes attrs; + XVisualInfo visual_info; + + if (!getenv ("DISPLAY") || !(x11.display = XOpenDisplay (NULL))) { + return 0; + } + + screen = DefaultScreen (x11.display); + + if (!XMatchVisualInfo (x11.display, screen, DefaultDepth (x11.display, screen), TrueColor, &visual_info)) { + + XCloseDisplay (x11.display); + + x11.display = NULL; + return 0; + + } + + attrs.background_pixel = BlackPixel (x11.display, screen); + attrs.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask | KeyReleaseMask; + + x11.window = XCreateWindow (x11.display, RootWindow (x11.display, screen), 0, 0, + VIDEO_WIDTH, VIDEO_HEIGHT, 0, visual_info.depth, InputOutput, visual_info.visual, + CWBackPixel | CWEventMask, &attrs); + + if (!x11.window) { + + XCloseDisplay (x11.display); + + x11.display = NULL; + return 0; + + } + + x11.close_atom = XInternAtom (x11.display, "WM_DELETE_WINDOW", False); + + XSetWMProtocols (x11.display, x11.window, &x11.close_atom, 1); + XStoreName (x11.display, x11.window, "dosemu - VGA"); + + x11.gc = XCreateGC (x11.display, x11.window, 0, NULL); + + XMapWindow (x11.display, x11.window); + XFlush (x11.display); + + x11.width = VIDEO_WIDTH; + x11.height = VIDEO_HEIGHT; + x11.redraw = 1; + + return 1; + +} + +static void video_x11_events (void) { + + XEvent event; + + while (XPending (x11.display)) { + + XNextEvent (x11.display, &event); + + switch (event.type) { + + case KeyPress: case KeyRelease: + + video_key (video_x11_scan (XLookupKeysym (&event.xkey, 0)), event.type == KeyPress); + break; + + case ConfigureNotify: + + if (event.xconfigure.width != x11.width || event.xconfigure.height != x11.height) { + + x11.width = event.xconfigure.width; + x11.height = event.xconfigure.height; + x11.redraw = 1; + + } + + break; + + case Expose: + + x11.redraw = 1; + break; + + case ClientMessage: + + if ((Atom) event.xclient.data.l[0] == x11.close_atom) { + closed = 1; + } + + break; + + case DestroyNotify: + + closed = 1; + break; + + } + + } + +} + +static void video_x11_draw (const uint32_t *pixels, int width, int height) { + + XImage *image; + size_t size; + + int x, y; + int depth; + + Visual *visual; + + width = width > VIDEO_MAX_WIDTH ? VIDEO_MAX_WIDTH : width; + height = height > VIDEO_MAX_HEIGHT ? VIDEO_MAX_HEIGHT : height; + + if (width < 1 || height < 1) { + return; + } + + if (x11.image && (x11.image->width != width || x11.image->height != height)) { + + XDestroyImage (x11.image); + x11.image = NULL; + + } + + if (!x11.image) { + + depth = DefaultDepth (x11.display, DefaultScreen (x11.display)); + visual = DefaultVisual (x11.display, DefaultScreen (x11.display)); + + size = (size_t) width * height * 4; + x11.image = XCreateImage (x11.display, visual, (unsigned) depth, ZPixmap, 0, (char *) calloc (1, size), (unsigned) width, (unsigned) height, 32, 0); + + if (!x11.image || !x11.image->data) { + + if (x11.image) { + + XDestroyImage (x11.image); + x11.image = NULL; + + } + + closed = 1; + return; + + } + + } + + image = x11.image; + + for (y = 0; y < height; y++) { + + for (x = 0; x < width; x++) { + + uint32_t rgb = pixels[(y * VIDEO_HEIGHT / height) * VIDEO_WIDTH + (x * VIDEO_WIDTH / width)]; + + if (image->bits_per_pixel == 32 && image->byte_order == LSBFirst && + image->red_mask == 0x00FF0000 && image->green_mask == 0x0000FF00 && + image->blue_mask == 0x000000FF) { + ((uint32_t *) (image->data + y * image->bytes_per_line))[x] = rgb; + } else { + XPutPixel (image, x, y, rgb); + } + + } + + } + + XPutImage (x11.display, x11.window, x11.gc, image, 0, 0, 0, 0, (unsigned) width, (unsigned) height); + XFlush (x11.display); + + x11.redraw = 0; + +} +#else +static int video_x11_initialize (void) { + return 0; +} +static void video_x11_events (void) {} + +static void video_x11_draw (const uint32_t *pixels, int width, int height) { + + (void) pixels; + (void) width; + (void) height; + +} +#endif + +static void video_wayland_global (void *data, struct wl_proxy *registry, uint32_t name, const char *interface, uint32_t version) { + + const struct wl_interface *type = NULL; + struct wl_proxy **target = NULL; + + (void) data; + + if (!strcmp (interface, "wl_compositor")) { + + target = &wayland.compositor; + type = wayland.compositor_interface; + + } else if (!strcmp (interface, "wl_shm")) { + + target = &wayland.shm; + type = wayland.shm_interface; + + } else if (!strcmp (interface, "xdg_wm_base")) { + + target = &wayland.wm_base; + type = &xdg_wm_base_interface; + + } else if (!strcmp (interface, "wl_seat")) { + + target = &wayland.seat; + type = wayland.seat_interface; + + } + + if (!target || *target || !type) { + return; + } + + if (version > (uint32_t) type->version) { + version = (uint32_t) type->version; + } + + /* wl_registry.bind(name, interface, version, new_id) */ + *target = wayland.marshal (registry, 0, type, wayland.version (registry), 0, + name, interface, version, NULL); + +} + +static void video_wayland_global_remove (void *data, struct wl_proxy *registry, uint32_t name) { + + (void) data; + (void) registry; + (void) name; + +} + +static void video_wayland_ping (void *data, struct wl_proxy *proxy, uint32_t serial) { + + (void) data; + wayland.marshal (proxy, 3, NULL, wayland.version (proxy), 0, serial); + +} + +static void video_wayland_configure (void *data, struct wl_proxy *proxy, uint32_t serial) { + + (void) data; + + wayland.marshal (proxy, 4, NULL, wayland.version (proxy), 0, serial); + wayland.configured = 1; + +} + +static void video_wayland_toplevel_configure (void *data, struct wl_proxy *proxy, int32_t width, int32_t height, void *states) { + + (void) data; + (void) proxy; + (void) width; + (void) height; + (void) states; + +} + +static void video_wayland_toplevel_close (void *data, struct wl_proxy *proxy) { + + (void) data; + (void) proxy; + + closed = 1; + +} + +static void video_wayland_buffer_release (void *data, struct wl_proxy *proxy) { + + int *busy = data; + + (void) proxy; + *busy = 0; + +} + +static void video_wayland_keyboard_keymap (void *data, struct wl_proxy *proxy, uint32_t format, int fd, uint32_t size) { + + (void) data; + (void) proxy; + (void) format; + (void) size; + + close (fd); + +} + +static void video_wayland_keyboard_enter (void *data, struct wl_proxy *proxy, uint32_t serial, struct wl_proxy *surface, void *keys) { + + (void) data; + (void) proxy; + (void) serial; + (void) surface; + (void) keys; + +} + +static void video_wayland_keyboard_leave (void *data, struct wl_proxy *proxy, uint32_t serial, struct wl_proxy *surface) { + + (void) data; + (void) proxy; + (void) serial; + (void) surface; + +} + +static void video_wayland_keyboard_key (void *data, struct wl_proxy *proxy, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) { + + (void) data; + (void) proxy; + (void) serial; + (void) time; + + video_key (video_evdev_scan (key), state != 0); + +} + +static void video_wayland_keyboard_modifiers (void *data, struct wl_proxy *proxy, uint32_t serial, uint32_t depressed, uint32_t latched, uint32_t locked, uint32_t group) { + + (void) data; + (void) proxy; + (void) serial; + (void) depressed; + (void) latched; + (void) locked; + (void) group; + +} + +static void video_wayland_keyboard_repeat (void *data, struct wl_proxy *proxy, int32_t rate, int32_t delay) { + + (void) data; + (void) proxy; + (void) rate; + (void) delay; + +} + +static void video_wayland_seat_capabilities (void *data, struct wl_proxy *proxy, uint32_t capabilities) { + + static void (*keyboard_listener[])(void) = { + + (void (*)(void)) video_wayland_keyboard_keymap, + (void (*)(void)) video_wayland_keyboard_enter, + (void (*)(void)) video_wayland_keyboard_leave, + (void (*)(void)) video_wayland_keyboard_key, + (void (*)(void)) video_wayland_keyboard_modifiers, + (void (*)(void)) video_wayland_keyboard_repeat + + }; + + (void) data; + + if ((capabilities & 2) && !wayland.keyboard) { + + wayland.keyboard = wayland.marshal (proxy, 1, wayland.keyboard_interface, wayland.version (proxy), 0, NULL); + + if (wayland.keyboard) { + wayland.add_listener (wayland.keyboard, keyboard_listener, NULL); + } + + } + + if (!(capabilities & 2) && wayland.keyboard) { + + if (wayland.version (wayland.keyboard) >= 3) { + wayland.marshal (wayland.keyboard, 0, NULL, wayland.version (wayland.keyboard), 1); + } else { + wayland.destroy (wayland.keyboard); + } + + wayland.keyboard = NULL; + + } + +} + +static void video_wayland_seat_name (void *data, struct wl_proxy *proxy, const char *name) { + + (void) data; + (void) proxy; + (void) name; + +} + +static int video_wayland_initialize (void) { + + static void (*registry_listener[])(void) = { + + (void (*)(void)) video_wayland_global, + (void (*)(void)) video_wayland_global_remove + + }; + + static void (*wm_listener[])(void) = { + (void (*)(void)) video_wayland_ping + }; + + static void (*surface_listener[])(void) = { + (void (*)(void)) video_wayland_configure + }; + + static void (*toplevel_listener[])(void) = { + + (void (*)(void)) video_wayland_toplevel_configure, + (void (*)(void)) video_wayland_toplevel_close + + }; + + static void (*seat_listener[])(void) = { + + (void (*)(void)) video_wayland_seat_capabilities, + (void (*)(void)) video_wayland_seat_name + + }; + + struct wl_proxy *proxy; + int i; + + if (!getenv ("WAYLAND_DISPLAY")) { + return 0; + } + + wayland.fd = -1; + + if (!(wayland.library = dlopen ("libwayland-client.so.0", RTLD_NOW | RTLD_LOCAL))) { + return 0; + } + +#define WAYLAND_LOAD(member, symbol) do { \ + *(void **) (&wayland.member) = dlsym (wayland.library, symbol); \ + if (!wayland.member) return 0; \ +} while (0) + WAYLAND_LOAD (connect, "wl_display_connect"); + WAYLAND_LOAD (disconnect, "wl_display_disconnect"); + WAYLAND_LOAD (roundtrip, "wl_display_roundtrip"); + WAYLAND_LOAD (dispatch, "wl_display_dispatch"); + WAYLAND_LOAD (dispatch_pending, "wl_display_dispatch_pending"); + WAYLAND_LOAD (flush, "wl_display_flush"); + WAYLAND_LOAD (get_fd, "wl_display_get_fd"); + WAYLAND_LOAD (add_listener, "wl_proxy_add_listener"); + WAYLAND_LOAD (destroy, "wl_proxy_destroy"); + WAYLAND_LOAD (version, "wl_proxy_get_version"); + WAYLAND_LOAD (marshal, "wl_proxy_marshal_flags"); + WAYLAND_LOAD (registry_interface, "wl_registry_interface"); + WAYLAND_LOAD (compositor_interface, "wl_compositor_interface"); + WAYLAND_LOAD (surface_interface, "wl_surface_interface"); + WAYLAND_LOAD (shm_interface, "wl_shm_interface"); + WAYLAND_LOAD (pool_interface, "wl_shm_pool_interface"); + WAYLAND_LOAD (buffer_interface, "wl_buffer_interface"); + WAYLAND_LOAD (seat_interface, "wl_seat_interface"); + WAYLAND_LOAD (keyboard_interface, "wl_keyboard_interface"); +#undef WAYLAND_LOAD + + wayland.display = wayland.connect (NULL); + + if (!wayland.display) { + return 0; + } + + wayland.registry = wayland.marshal ((struct wl_proxy *) wayland.display, 1, + wayland.registry_interface, wayland.version ((struct wl_proxy *) wayland.display), 0, NULL); + + if (!wayland.registry) { + return 0; + } + + wayland.add_listener (wayland.registry, registry_listener, NULL); + + if (wayland.roundtrip (wayland.display) < 0 || !wayland.compositor || !wayland.shm || !wayland.wm_base) { + return 0; + } + + wayland.add_listener (wayland.wm_base, wm_listener, NULL); + + if (wayland.seat) { + wayland.add_listener (wayland.seat, seat_listener, NULL); + } + + wayland.surface = wayland.marshal (wayland.compositor, 0, + wayland.surface_interface, wayland.version (wayland.compositor), 0, NULL); + + wayland.xdg_surface = wayland.marshal (wayland.wm_base, 2, + &xdg_surface_interface, wayland.version (wayland.wm_base), 0, NULL, wayland.surface); + + if (!wayland.surface || !wayland.xdg_surface) { + return 0; + } + + wayland.toplevel = wayland.marshal (wayland.xdg_surface, 1, + &xdg_toplevel_interface, wayland.version (wayland.xdg_surface), 0, NULL); + + if (!wayland.toplevel) { + return 0; + } + + wayland.add_listener (wayland.xdg_surface, surface_listener, NULL); + wayland.add_listener (wayland.toplevel, toplevel_listener, NULL); + + wayland.marshal (wayland.toplevel, 2, NULL, 1, 0, "dosemu - VGA"); + wayland.marshal (wayland.toplevel, 3, NULL, 1, 0, "dosemu"); + wayland.marshal (wayland.toplevel, 7, NULL, 1, 0, VIDEO_WIDTH, VIDEO_HEIGHT); + wayland.marshal (wayland.toplevel, 8, NULL, 1, 0, VIDEO_WIDTH, VIDEO_HEIGHT); + wayland.marshal (wayland.surface, 6, NULL, wayland.version (wayland.surface), 0); + + if (wayland.roundtrip (wayland.display) < 0 || !wayland.configured) { + return 0; + } + + /* Two buffers ensure the compositor never sees a buffer being rewritten. */ + wayland.width = VIDEO_WIDTH; + wayland.height = VIDEO_HEIGHT; + wayland.buffer_size = (size_t) wayland.width * wayland.height * 4; + + { + + char path[] = "/dev/shm/dosemu-XXXXXX"; + wayland.fd = mkstemp (path); + + if (wayland.fd < 0) { + + char fallback[] = "/tmp/dosemu-XXXXXX"; + wayland.fd = mkstemp (fallback); + + if (wayland.fd < 0) { + return 0; + } + + unlink (fallback); + + } else { + unlink (path); + } + + } + + if (ftruncate (wayland.fd, (off_t) (wayland.buffer_size * 2)) < 0) { + return 0; + } + + wayland.pixels = mmap (NULL, wayland.buffer_size * 2, PROT_READ | PROT_WRITE, MAP_SHARED, wayland.fd, 0); + + if (wayland.pixels == MAP_FAILED) { + + wayland.pixels = NULL; + return 0; + + } + + wayland.pool = wayland.marshal (wayland.shm, 0, wayland.pool_interface, + wayland.version (wayland.shm), 0, NULL, wayland.fd, (int32_t) (wayland.buffer_size * 2)); + + if (!wayland.pool) { + return 0; + } + + for (i = 0; i < 2; i++) { + + static void (*buffer_listener[])(void) = { + (void (*)(void)) video_wayland_buffer_release + }; + + proxy = wayland.marshal (wayland.pool, 0, wayland.buffer_interface, + wayland.version (wayland.pool), 0, NULL, (int32_t) (i * wayland.buffer_size), + wayland.width, wayland.height, wayland.width * 4, 1); + + if (!proxy) { + return 0; + } + + wayland.buffers[i] = proxy; + wayland.add_listener (proxy, buffer_listener, &wayland.busy[i]); + + } + + wayland.roundtrip (wayland.display); + return 1; + +} + +static void video_wayland_events (void) { + + struct pollfd fd; + + if (!wayland.display) { + return; + } + + if (wayland.dispatch_pending (wayland.display) < 0) { + + closed = 1; + return; + + } + + wayland.flush (wayland.display); + + fd.fd = wayland.get_fd (wayland.display); + fd.events = POLLIN; + fd.revents = 0; + + if (poll (&fd, 1, 0) > 0 && (fd.revents & POLLIN)) { + + if (wayland.dispatch (wayland.display) < 0) { + closed = 1; + } + + } + +} + +static int video_wayland_draw (const uint32_t *pixels) { + + int slot = wayland.busy[0] ? (wayland.busy[1] ? -1 : 1) : 0; + + if (slot >= 0) { + + memcpy (wayland.pixels + slot * wayland.buffer_size, pixels, wayland.buffer_size); + wayland.busy[slot] = 1; + + wayland.marshal (wayland.surface, 1, NULL, wayland.version (wayland.surface), 0, wayland.buffers[slot], 0, 0); + wayland.marshal (wayland.surface, 2, NULL, wayland.version (wayland.surface), 0, 0, 0, VIDEO_WIDTH, VIDEO_HEIGHT); + wayland.marshal (wayland.surface, 6, NULL, wayland.version (wayland.surface), 0); + wayland.flush (wayland.display); + + return 1; + + } + + return 0; + +} + +static void video_wayland_shutdown (void) { + + int i; + + if (wayland.display) { + + for (i = 0; i < 2; i++) { + + if (wayland.buffers[i]) { + wayland.marshal (wayland.buffers[i], 0, NULL, 1, 1); + } + + } + + if (wayland.pool) { + wayland.marshal (wayland.pool, 1, NULL, 1, 1); + } + + if (wayland.keyboard) { + + if (wayland.version (wayland.keyboard) >= 3) { + wayland.marshal (wayland.keyboard, 0, NULL, wayland.version (wayland.keyboard), 1); + } else { + wayland.destroy (wayland.keyboard); + } + + } + + if (wayland.seat) { + + if (wayland.version (wayland.seat) >= 5) { + wayland.marshal (wayland.seat, 3, NULL, wayland.version (wayland.seat), 1); + } else { + wayland.destroy (wayland.seat); + } + + } + + if (wayland.toplevel) { + wayland.marshal (wayland.toplevel, 0, NULL, 1, 1); + } + + if (wayland.xdg_surface) { + wayland.marshal (wayland.xdg_surface, 0, NULL, 1, 1); + } + + if (wayland.surface) { + wayland.marshal (wayland.surface, 0, NULL, wayland.version (wayland.surface), 1); + } + + if (wayland.wm_base) { + wayland.marshal (wayland.wm_base, 0, NULL, 1, 1); + } + + wayland.flush (wayland.display); + wayland.disconnect (wayland.display); + + } + + if (wayland.pixels) { + munmap (wayland.pixels, wayland.buffer_size * 2); + } + + if (wayland.fd >= 0) { + close (wayland.fd); + } + + if (wayland.library) { + dlclose (wayland.library); + } + + memset (&wayland, 0, sizeof (wayland)); + wayland.fd = -1; + +} + +static uint32_t video_framebuffer[VIDEO_WIDTH * VIDEO_HEIGHT]; + +static int video_initialize (void) { + + const char *requested = getenv ("DOSEMU_VIDEO"); + + if (requested && !strcmp (requested, "terminal")) { + return 0; + } + + if (!requested || !strcmp (requested, "wayland")) { + + if (video_wayland_initialize ()) { + return 2; + } + + if (wayland.library) { + video_wayland_shutdown (); + } + + } + + if ((!requested || !strcmp (requested, "x11")) && video_x11_initialize ()) { + return 1; + } + + return 0; + +} + +int video_native_present (CPU8086 *cpu) { + + int palette_changed; + + uint64_t now; + unsigned x, y; + + if (closed) { + return 1; + } + + if (!attempted) { + + attempted = 1; + backend = video_initialize (); + + } + + if (!backend) { + return 0; + } + + if (backend == 1) { + video_x11_events (); + } else { + video_wayland_events (); + } + + if (closed) { + return 1; + } + + palette_changed = memcmp (previous_palette, cpu->vga_dac_palette, sizeof (previous_palette)) || previous_mask != cpu->vga_dac_mask; + + if (!(mem_video_dirty & 4) && !palette_changed && (backend != 1 || !x11.redraw)) { + return 1; + } + + now = video_milliseconds (); + + if (last_refresh && now - last_refresh < 50) { + return 1; + } + + last_refresh = now; + + for (y = 0; y < 200; y++) { + + for (x = 0; x < 320; x++) { + + uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; + const uint8_t *rgb = cpu->vga_dac_palette[index]; + + uint32_t color = ((uint32_t) (rgb[0] * 255 + 31) / 63 << 16) | ((uint32_t) (rgb[1] * 255 + 31) / 63 << 8) | (uint32_t) (rgb[2] * 255 + 31) / 63; + + video_framebuffer[y * 2 * VIDEO_WIDTH + x * 2] = color; + video_framebuffer[y * 2 * VIDEO_WIDTH + x * 2 + 1] = color; + video_framebuffer[(y * 2 + 1) * VIDEO_WIDTH + x * 2] = color; + video_framebuffer[(y * 2 + 1) * VIDEO_WIDTH + x * 2 + 1] = color; + + } + + } + + if (backend == 1) { + video_x11_draw (video_framebuffer, x11.width, x11.height); + } else if (!video_wayland_draw (video_framebuffer)) { + return 1; + } + + memcpy (previous_palette, cpu->vga_dac_palette, sizeof (previous_palette)); + + previous_mask = cpu->vga_dac_mask; + mem_video_dirty &= ~4u; + + return 1; + +} + +int video_native_active (void) { + return backend != 0; +} + +int video_native_poll (CPU8086 *cpu) { + + if (backend == 1) { + video_x11_events (); + } + + if (backend == 2) { + video_wayland_events (); + } + + if (closed) { + return -1; + } + + if (!cpu->kbd_scancode_pending && key_head != key_tail) { + + cpu->kbd_release_scancode = 0; + cpu->kbd_scancode = key_queue[key_head]; + cpu->kbd_scancode_pending = 1; + cpu->pic[0].irr |= 2; + + key_head = (key_head + 1) % sizeof (key_queue); + + } + + return 0; + +} + +void video_native_shutdown (void) { + +#if defined (HAVE_X11) + if (backend == 1 && x11.display) { + + if (x11.image) { + XDestroyImage (x11.image); + } + + if (x11.gc) { + XFreeGC (x11.display, x11.gc); + } + + if (x11.window) { + XDestroyWindow (x11.display, x11.window); + } + + XCloseDisplay (x11.display); + + } +#endif + if (backend == 2) { + video_wayland_shutdown (); + } + + memset (&x11, 0, sizeof (x11)); + + backend = 0; + attempted = 0; + + closed = 0; + last_refresh = 0; + + key_head = key_tail = 0; + memset (previous_palette, 0, sizeof (previous_palette)); + + previous_mask = 0; + +} + +#endif diff --git a/video_native_macos.c b/video_native_macos.c new file mode 100644 index 0000000..4412da1 --- /dev/null +++ b/video_native_macos.c @@ -0,0 +1,537 @@ +/****************************************************************************** + * @file video_native_macos.c + *****************************************************************************/ +#if defined (__APPLE__) + +#import +#include +#include +#include + +#include "video_native.h" +#include "memory.h" + +static NSWindow *video_window; +static NSView *video_view; + +static uint32_t video_pixels[640 * 400]; + +static uint8_t video_palette[256][3]; +static uint8_t video_mask; +static uint8_t video_queue[128]; + +static unsigned video_head, video_tail; + +static NSTimeInterval video_last_refresh; +static int video_attempted, video_closed; + +static uint8_t video_macos_scan (unsigned code) { + + switch (code) { + + case 0: + + return 0x1E; + + case 1: + + return 0x1F; + + case 2: + + return 0x20; + + case 3: + + return 0x21; + + case 4: + + return 0x23; + + case 5: + + return 0x22; + + case 6: + + return 0x2C; + + case 7: + + return 0x2D; + + case 8: + + return 0x2E; + + case 9: + + return 0x2F; + + case 11: + + return 0x30; + + case 12: + + return 0x10; + + case 13: + + return 0x11; + + case 14: + + return 0x12; + + case 15: + + return 0x13; + + case 16: + + return 0x15; + + case 17: + + return 0x14; + + case 31: + + return 0x18; + + case 32: + + return 0x16; + + case 34: + + return 0x17; + + case 35: + + return 0x19; + + case 37: + + return 0x25; + + case 38: + + return 0x24; + + case 40: + + return 0x27; + + case 45: + + return 0x31; + + case 46: + + return 0x32; + + case 18: + + return 0x02; + + case 19: + + return 0x03; + + case 20: + + return 0x04; + + case 21: + + return 0x05; + + case 23: + + return 0x06; + + case 22: + + return 0x07; + + case 26: + + return 0x08; + + case 28: + + return 0x09; + + case 25: + + return 0x0A; + + case 29: + + return 0x0B; + + case 49: + + return 0x39; + + case 36: + + return 0x1C; + + case 51: + + return 0x0E; + + case 48: + + return 0x0F; + + case 53: + + return 0x01; + + case 123: + + return 0x4B; + + case 124: + + return 0x4D; + + case 125: + + return 0x50; + + case 126: + + return 0x48; + + case 122: + + return 0x3B; + + case 120: + + return 0x3C; + + case 99: + + return 0x3D; + + case 118: + + return 0x3E; + + case 96: + + return 0x3F; + + case 97: + + return 0x40; + + case 98: + + return 0x41; + + case 100: + + return 0x42; + + case 101: + + return 0x43; + + case 109: + + return 0x44; + + case 103: + + return 0x57; + + case 111: + + return 0x58; + + case 56: + + return 0x2A; + + case 60: + + return 0x36; + + case 59: + case 62: + + return 0x1D; + + case 58: + case 61: + + return 0x38; + + } + + return 0; + +} + +static void video_key (NSEvent *event, int pressed) { + + unsigned scan = video_macos_scan ([event keyCode]); + unsigned next = (video_tail + 1) % sizeof (video_queue); + + if (!scan || next == video_head) { + return; + } + + video_queue[video_tail] = (uint8_t) (scan | (pressed ? 0 : 0x80)); + video_tail = next; + +} + +@interface DosemuVideoView : NSView +@end + +@implementation DosemuVideoView + +- (BOOL) acceptsFirstResponder { + return YES; +} + +- (void) keyDown: (NSEvent *) event { + video_key (event, 1); +} + +- (void) keyUp: (NSEvent *) event { + video_key (event, 0); +} + +- (void) drawRect: (NSRect) rect { + + CGColorSpaceRef colorSpace; + CGContextRef context; + + CGImageRef image; + CGDataProviderRef provider; + + (void) rect; + + if (!(context = [[NSGraphicsContext currentContext] CGContext])) { + return; + } + + colorSpace = CGColorSpaceCreateDeviceRGB (); + provider = CGDataProviderCreateWithData (NULL, video_pixels, sizeof (video_pixels), NULL); + + image = CGImageCreate (640, 400, 8, 32, 640 * 4, colorSpace, + kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst, + provider, NULL, false, kCGRenderingIntentDefault); + + if (image) { + + CGContextSetInterpolationQuality (context, kCGInterpolationNone); + CGContextDrawImage (context, CGRectMake (0, 0, self.bounds.size.width, self.bounds.size.height), image); + CGImageRelease (image); + + } + + CGDataProviderRelease (provider); + CGColorSpaceRelease (colorSpace); + +} + +@end + +@interface DosemuVideoDelegate : NSObject +@end + +@implementation DosemuVideoDelegate + +- (void) windowWillClose: (NSNotification *) notification { + + (void) notification; + video_closed = 1; + +} + +@end + +static DosemuVideoDelegate *video_delegate; + +static int video_initialize (void) { + + NSRect area = NSMakeRect (0, 0, 640, 400); + + NSUInteger style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; + const char *requested = getenv ("DOSEMU_VIDEO"); + + if (requested && !strcmp (requested, "terminal")) { + return 0; + } + + [NSApplication sharedApplication]; + [NSApp setActivationPolicy: NSApplicationActivationPolicyRegular]; + [NSApp finishLaunching]; + + video_window = [[NSWindow alloc] initWithContentRect: area styleMask: style + backing: NSBackingStoreBuffered defer: NO]; + + if (!video_window) { + return 0; + } + + video_view = [[DosemuVideoView alloc] initWithFrame: area]; + video_delegate = [[DosemuVideoDelegate alloc] init]; + + [video_window setContentView: video_view]; + [video_window setDelegate: video_delegate]; + [video_window setTitle: @"dosemu - VGA"]; + [video_window center]; + [video_window makeKeyAndOrderFront: nil]; + [video_window makeFirstResponder: video_view]; + [NSApp activateIgnoringOtherApps: YES]; + + return 1; + +} + +int video_native_active (void) { + return video_window != nil; +} + +int video_native_poll (CPU8086 *cpu) { + + NSEvent *event; + + if (video_window) { + + while ((event = [NSApp nextEventMatchingMask: NSEventMaskAny untilDate: [NSDate distantPast] inMode: NSDefaultRunLoopMode dequeue: YES]) != nil) { + [NSApp sendEvent: event]; + } + + [NSApp updateWindows]; + + } + + if (video_closed) { + return -1; + } + + if (!cpu->kbd_scancode_pending && video_head != video_tail) { + + cpu->kbd_release_scancode = 0; + cpu->kbd_scancode = video_queue[video_head]; + cpu->kbd_scancode_pending = 1; + cpu->pic[0].irr |= 2; + + video_head = (video_head + 1) % sizeof (video_queue); + + } + + return 0; + +} + +int video_native_present (CPU8086 *cpu) { + + unsigned x, y; + NSTimeInterval now; + + int palette_changed; + + if (video_closed) { + return 1; + } + + if (!video_attempted) { + + video_attempted = 1; + video_initialize (); + + } + + if (!video_window) { + return 0; + } + + video_native_poll (cpu); + palette_changed = memcmp (video_palette, cpu->vga_dac_palette, sizeof (video_palette)) || video_mask != cpu->vga_dac_mask; + + if (!(mem_video_dirty & 4) && !palette_changed) { + return 1; + } + + now = [NSDate timeIntervalSinceReferenceDate]; + + if (video_last_refresh && now - video_last_refresh < 0.05) { + return 1; + } + + video_last_refresh = now; + + for (y = 0; y < 200; y++) { + + for (x = 0; x < 320; x++) { + + uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; + const uint8_t *rgb = cpu->vga_dac_palette[index]; + + uint32_t color = ((uint32_t) (rgb[0] * 255 + 31) / 63 << 16) | + ((uint32_t) (rgb[1] * 255 + 31) / 63 << 8) | (uint32_t) (rgb[2] * 255 + 31) / 63; + + video_pixels[y * 2 * 640 + x * 2] = color; + video_pixels[y * 2 * 640 + x * 2 + 1] = color; + video_pixels[(y * 2 + 1) * 640 + x * 2] = color; + video_pixels[(y * 2 + 1) * 640 + x * 2 + 1] = color; + + } + + } + + memcpy (video_palette, cpu->vga_dac_palette, sizeof (video_palette)); + + video_mask = cpu->vga_dac_mask; + mem_video_dirty &= ~4u; + + [video_view setNeedsDisplay: YES]; + [video_view displayIfNeeded]; + + return 1; + +} + +void video_native_shutdown (void) { + + if (video_window) { + + [video_window setDelegate: nil]; + [video_window close]; + [video_window release]; + + video_window = nil; + + } + + [video_view release]; + video_view = nil; + + [video_delegate release]; + video_delegate = nil; + + video_closed = 0; + video_attempted = 0; + video_last_refresh = 0; + video_head = video_tail = 0; + + memset (video_palette, 0, sizeof (video_palette)); + video_mask = 0; + +} + +#endif diff --git a/video_native_other.c b/video_native_other.c new file mode 100644 index 0000000..9285200 --- /dev/null +++ b/video_native_other.c @@ -0,0 +1,26 @@ +/****************************************************************************** + * @file video_native_other.c + *****************************************************************************/ +#if !defined (__linux__) && !defined (_WIN32) && !defined (__APPLE__) || defined (__ANDROID__) +#include "video_native.h" + +int video_native_present (CPU8086 *cpu) { + + (void) cpu; + return 0; + +} + +int video_native_active (void) { + return 0; +} + +int video_native_poll (CPU8086 *cpu) { + + (void) cpu; + return 0; + +} + +void video_native_shutdown (void) {} +#endif diff --git a/video_native_win32.c b/video_native_win32.c new file mode 100644 index 0000000..30d821c --- /dev/null +++ b/video_native_win32.c @@ -0,0 +1,267 @@ +/****************************************************************************** + * @file video_native_win32.c + *****************************************************************************/ +#if defined (_WIN32) + +#include +#include +#include +#include + +#include "video_native.h" +#include "memory.h" + +static HWND video_window; + +static int video_closed; +static int video_attempted; + +static DWORD video_last_refresh; +static uint32_t video_pixels[640 * 400]; + +static uint8_t video_palette[256][3]; +static uint8_t video_mask; +static uint8_t video_key_queue[128]; + +static unsigned video_key_head, video_key_tail; + +static void video_key (UINT key, int pressed) { + + unsigned scan = MapVirtualKeyA (key, MAPVK_VK_TO_VSC) & 0x7F; + unsigned next = (video_key_tail + 1) % sizeof (video_key_queue); + + if (!scan || next == video_key_head) { + return; + } + + video_key_queue[video_key_tail] = (uint8_t) (scan | (pressed ? 0 : 0x80)); + video_key_tail = next; + +} + +static LRESULT CALLBACK video_window_proc (HWND window, UINT message, WPARAM wparam, LPARAM lparam) { + + switch (message) { + + case WM_CLOSE: + + video_closed = 1; + + DestroyWindow (window); + return 0; + + case WM_DESTROY: + + video_window = NULL; + return 0; + + case WM_ERASEBKGND: + + return 1; + + case WM_KEYDOWN: case WM_SYSKEYDOWN: + + video_key ((UINT) wparam, 1); + return 0; + + case WM_KEYUP: case WM_SYSKEYUP: + + video_key ((UINT) wparam, 0); + return 0; + + case WM_PAINT: { + + PAINTSTRUCT paint; + BITMAPINFO bitmap; + + RECT area; + HDC dc = BeginPaint (window, &paint); + + memset (&bitmap, 0, sizeof (bitmap)); + + bitmap.bmiHeader.biSize = sizeof (bitmap.bmiHeader); + bitmap.bmiHeader.biWidth = 640; + bitmap.bmiHeader.biHeight = -400; + bitmap.bmiHeader.biPlanes = 1; + bitmap.bmiHeader.biBitCount = 32; + bitmap.bmiHeader.biCompression = BI_RGB; + + GetClientRect (window, &area); + SetStretchBltMode (dc, COLORONCOLOR); + + StretchDIBits (dc, 0, 0, area.right, area.bottom, 0, 0, 640, 400, video_pixels, &bitmap, DIB_RGB_COLORS, SRCCOPY); + EndPaint (window, &paint); + + return 0; + + } + + } + + return DefWindowProcA (window, message, wparam, lparam); + +} + +static int video_initialize (void) { + + WNDCLASSA cls; + RECT area = { 0, 0, 640, 400 }; + + const char *requested = getenv ("DOSEMU_VIDEO"); + DWORD style = WS_OVERLAPPEDWINDOW; + + if (requested && !strcmp (requested, "terminal")) { + return 0; + } + + memset (&cls, 0, sizeof (cls)); + + cls.lpfnWndProc = video_window_proc; + cls.hInstance = GetModuleHandleA (NULL); + cls.hCursor = LoadCursor (NULL, IDC_ARROW); + cls.lpszClassName = "dosemuVGA"; + + if (!RegisterClassA (&cls) && GetLastError () != ERROR_CLASS_ALREADY_EXISTS) { + return 0; + } + + AdjustWindowRect (&area, style, FALSE); + video_window = CreateWindowExA (0, cls.lpszClassName, "dosemu - VGA", style, + CW_USEDEFAULT, CW_USEDEFAULT, area.right - area.left, area.bottom - area.top, + NULL, NULL, cls.hInstance, NULL); + + if (!video_window) { + return 0; + } + + ShowWindow (video_window, SW_SHOW); + UpdateWindow (video_window); + + SetFocus (video_window); + return 1; + +} + +int video_native_active (void) { + return video_window != NULL; +} + +int video_native_poll (CPU8086 *cpu) { + + MSG message; + + while (video_window && PeekMessageA (&message, NULL, 0, 0, PM_REMOVE)) { + + TranslateMessage (&message); + DispatchMessageA (&message); + + } + + if (video_closed) { + return -1; + } + + if (!cpu->kbd_scancode_pending && video_key_head != video_key_tail) { + + cpu->kbd_release_scancode = 0; + cpu->kbd_scancode = video_key_queue[video_key_head]; + cpu->kbd_scancode_pending = 1; + + cpu->pic[0].irr |= 2; + video_key_head = (video_key_head + 1) % sizeof (video_key_queue); + + } + + return 0; + +} + +int video_native_present (CPU8086 *cpu) { + + unsigned x, y; + DWORD now; + + int palette_changed; + + if (video_closed) { + return 1; + } + + if (!video_attempted) { + + video_attempted = 1; + video_initialize (); + + } + + if (!video_window) { + return 0; + } + + video_native_poll (cpu); + palette_changed = memcmp (video_palette, cpu->vga_dac_palette, sizeof (video_palette)) || video_mask != cpu->vga_dac_mask; + + if (!(mem_video_dirty & 4) && !palette_changed) { + return 1; + } + + now = GetTickCount (); + + if (video_last_refresh && (DWORD) (now - video_last_refresh) < 50) { + return 1; + } + + video_last_refresh = now; + + for (y = 0; y < 200; y++) { + + for (x = 0; x < 320; x++) { + + uint8_t index = mem_vga_pixel (cpu, x, y) & cpu->vga_dac_mask; + const uint8_t *rgb = cpu->vga_dac_palette[index]; + + uint32_t color = ((uint32_t) (rgb[0] * 255 + 31) / 63 << 16) | + ((uint32_t) (rgb[1] * 255 + 31) / 63 << 8) | (uint32_t) (rgb[2] * 255 + 31) / 63; + + video_pixels[y * 2 * 640 + x * 2] = color; + video_pixels[y * 2 * 640 + x * 2 + 1] = color; + video_pixels[(y * 2 + 1) * 640 + x * 2] = color; + video_pixels[(y * 2 + 1) * 640 + x * 2 + 1] = color; + + } + + } + + memcpy (video_palette, cpu->vga_dac_palette, sizeof (video_palette)); + + video_mask = cpu->vga_dac_mask; + mem_video_dirty &= ~4u; + + InvalidateRect (video_window, NULL, FALSE); + UpdateWindow (video_window); + + return 1; + +} + +void video_native_shutdown (void) { + + if (video_window) { + + DestroyWindow (video_window); + video_window = NULL; + + } + + video_closed = 0; + video_attempted = 0; + + video_last_refresh = 0; + video_key_head = video_key_tail = 0; + + memset (video_palette, 0, sizeof (video_palette)); + video_mask = 0; + +} + +#endif