--- BasiliskII/src/Unix/video_x.cpp 2000/10/27 17:01:40 1.26 +++ BasiliskII/src/Unix/video_x.cpp 2001/07/01 19:57:55 1.49 @@ -1,7 +1,7 @@ /* * video_x.cpp - Video/graphics emulation, X11 specific stuff * - * Basilisk II (C) 1997-2000 Christian Bauer + * Basilisk II (C) 1997-2001 Christian Bauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -52,13 +52,6 @@ # include #endif -#ifdef ENABLE_VOSF -# include -# include -# include -# include -#endif - #include "cpu_emulation.h" #include "main.h" #include "adb.h" @@ -79,27 +72,36 @@ enum { // Constants const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes"; -const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices"; + +static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask; +static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask; // Global variables static int32 frame_skip; // Prefs items -static int16 mouse_wheel_mode = 1; -static int16 mouse_wheel_lines = 3; +static int16 mouse_wheel_mode; +static int16 mouse_wheel_lines; static int display_type = DISPLAY_WINDOW; // See enum above static bool local_X11; // Flag: X server running on local machine? -static uint8 *the_buffer; // Mac frame buffer +static uint8 *the_buffer = NULL; // Mac frame buffer (where MacOS draws into) +static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer (for refreshed modes) -#ifdef HAVE_PTHREADS static bool redraw_thread_active = false; // Flag: Redraw thread installed -static volatile bool redraw_thread_cancel = false; // Flag: Cancel Redraw thread +#ifdef HAVE_PTHREADS +static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread static pthread_t redraw_thread; // Redraw thread #endif static bool has_dga = false; // Flag: Video DGA capable static bool has_vidmode = false; // Flag: VidMode extension available +#ifdef ENABLE_VOSF +static bool use_vosf = true; // Flag: VOSF enabled +#else +static const bool use_vosf = false; // VOSF not possible +#endif + static bool ctrl_down = false; // Flag: Ctrl key pressed static bool caps_on = false; // Flag: Caps Lock on static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread @@ -114,131 +116,62 @@ static int keycode_table[256]; // X // X11 variables static int screen; // Screen number static int xdepth; // Depth of X screen -static int depth; // Depth of Mac frame buffer -static Window rootwin, the_win; // Root window and our window +static Window rootwin; // Root window and our window static XVisualInfo visualInfo; static Visual *vis; -static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode +static Colormap cmap[2] = {0, 0}; // Colormaps for indexed modes (DGA needs two of them) static XColor black, white; static unsigned long black_pixel, white_pixel; static int eventmask; -static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | FocusChangeMask | ExposureMask; -static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask; -static XColor palette[256]; // Color palette for 8-bit mode -static bool palette_changed = false; // Flag: Palette changed, redraw thread must set new colors -#ifdef HAVE_PTHREADS -static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect palette -#endif +static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes -// Variables for window mode -static GC the_gc; -static XImage *img = NULL; -static XShmSegmentInfo shminfo; -static Cursor mac_cursor; -static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer -static bool have_shm = false; // Flag: SHM extensions available -static bool updt_box[17][17]; // Flag for Update -static int nr_boxes; -static const int sm_uptd[] = {4,1,6,3,0,5,2,7}; -static int sm_no_boxes[] = {1,8,32,64,128,300}; +static XColor palette[256]; // Color palette to be used as CLUT and gamma table +static bool palette_changed = false; // Flag: Palette changed, redraw thread must set new colors -// Variables for XF86 DGA mode -static int current_dga_cmap; // Number (0 or 1) of currently installed DGA colormap -static Window suspend_win; // "Suspend" window -static void *fb_save = NULL; // Saved frame buffer for suspend -#ifdef HAVE_PTHREADS -static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer +#ifdef ENABLE_FBDEV_DGA +static int fbdev_fd = -1; #endif -// Variables for fbdev DGA mode -const char FBDEVICE_FILE_NAME[] = "/dev/fb"; -static int fbdev_fd; - #ifdef ENABLE_XF86_VIDMODE -// Variables for XF86 VidMode support -static XF86VidModeModeInfo **x_video_modes; // Array of all available modes +static XF86VidModeModeInfo **x_video_modes = NULL; // Array of all available modes static int num_x_video_modes; #endif -#ifdef ENABLE_VOSF -static bool use_vosf = true; // Flag: VOSF enabled +// Mutex to protect palette +#ifdef HAVE_PTHREADS +static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER; +#define LOCK_PALETTE pthread_mutex_lock(&palette_lock) +#define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock) #else -static const bool use_vosf = false; // Flag: VOSF enabled +#define LOCK_PALETTE +#define UNLOCK_PALETTE #endif -#ifdef ENABLE_VOSF -static uint8 * the_host_buffer; // Host frame buffer in VOSF mode -static uint32 the_buffer_size; // Size of allocated the_buffer -#endif - -#ifdef ENABLE_VOSF -// Variables for Video on SEGV support (taken from the Win32 port) -struct ScreenPageInfo { - int top, bottom; // Mapping between this virtual page and Mac scanlines -}; - -struct ScreenInfo { - uint32 memBase; // Real start address - uint32 memStart; // Start address aligned to page boundary - uint32 memEnd; // Address of one-past-the-end of the screen - uint32 memLength; // Length of the memory addressed by the screen pages - - uint32 pageSize; // Size of a page - int pageBits; // Shift count to get the page number - uint32 pageCount; // Number of pages allocated to the screen - - uint8 * dirtyPages; // Table of flags set if page was altered - ScreenPageInfo * pageInfo; // Table of mappings page -> Mac scanlines -}; - -static ScreenInfo mainBuffer; - -#define PFLAG_SET(page) mainBuffer.dirtyPages[page] = 1 -#define PFLAG_CLEAR(page) mainBuffer.dirtyPages[page] = 0 -#define PFLAG_ISSET(page) mainBuffer.dirtyPages[page] -#define PFLAG_ISCLEAR(page) (mainBuffer.dirtyPages[page] == 0) -#ifdef UNALIGNED_PROFITABLE -# define PFLAG_ISCLEAR_4(page) (*((uint32 *)(mainBuffer.dirtyPages + page)) == 0) -#else -# define PFLAG_ISCLEAR_4(page) \ - (mainBuffer.dirtyPages[page ] == 0) \ - && (mainBuffer.dirtyPages[page+1] == 0) \ - && (mainBuffer.dirtyPages[page+2] == 0) \ - && (mainBuffer.dirtyPages[page+3] == 0) -#endif -#define PFLAG_CLEAR_ALL memset(mainBuffer.dirtyPages, 0, mainBuffer.pageCount) -#define PFLAG_SET_ALL memset(mainBuffer.dirtyPages, 1, mainBuffer.pageCount) - -static int zero_fd = -1; -static bool Screen_fault_handler_init(); -static struct sigaction vosf_sa; - +// Mutex to protect frame buffer #ifdef HAVE_PTHREADS -static pthread_mutex_t Screen_draw_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer (dirtyPages in fact) +static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER; +#define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock); +#define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock); +#else +#define LOCK_FRAME_BUFFER +#define UNLOCK_FRAME_BUFFER #endif -static int log_base_2(uint32 x) -{ - uint32 mask = 0x80000000; - int l = 31; - while (l >= 0 && (x & mask) == 0) { - mask >>= 1; - l--; - } - return l; -} - -#endif +// Variables for non-VOSF incremental refresh +static const int sm_uptd[] = {4,1,6,3,0,5,2,7}; +static int sm_no_boxes[] = {1,8,32,64,128,300}; +static bool updt_box[17][17]; +static int nr_boxes; -// VideoRefresh function -void VideoRefreshInit(void); +// Video refresh function +static void VideoRefreshInit(void); static void (*video_refresh)(void); + // Prototypes static void *redraw_func(void *arg); -static int event2keycode(XKeyEvent *ev); - +static int event2keycode(XKeyEvent &ev); // From main_unix.cpp extern char *x_display_name; @@ -247,64 +180,108 @@ extern Display *x_display; // From sys_unix.cpp extern void SysMountFirstFloppy(void); -#ifdef ENABLE_VOSF -# include "video_vosf.h" -#endif - /* - * Initialization + * Utility functions */ -// Set VideoMonitor according to video mode -void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order) +// Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals) +static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue) +{ + return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift); +} + +// Add mode to list of supported modes +static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth) +{ + video_mode mode; + mode.x = width; + mode.y = height; + mode.resolution_id = resolution_id; + mode.bytes_per_row = bytes_per_row; + mode.depth = depth; + VideoModes.push_back(mode); +} + +// Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac) +static void set_mac_frame_buffer(video_depth depth, bool native_byte_order) { #if !REAL_ADDRESSING && !DIRECT_ADDRESSING int layout = FLAYOUT_DIRECT; - switch (depth) { - case 1: - layout = FLAYOUT_DIRECT; - break; - case 8: - layout = FLAYOUT_DIRECT; - break; - case 15: - layout = FLAYOUT_HOST_555; - break; - case 16: - layout = FLAYOUT_HOST_565; - break; - case 24: - case 32: - layout = FLAYOUT_HOST_888; - break; - } + if (depth == VDEPTH_16BIT) + layout = (xdepth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565; + else if (depth == VDEPTH_32BIT) + layout = (xdepth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT; if (native_byte_order) MacFrameLayout = layout; else MacFrameLayout = FLAYOUT_DIRECT; + VideoMonitor.mac_frame_base = MacFrameBaseMac; + + // Set variables used by UAE memory banking + MacFrameBaseHost = the_buffer; + MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y; + InitFrameBufferMapping(); +#else + VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer); + D(bug("Host frame buffer = %p, ", the_buffer)); #endif - switch (depth) { - case 1: - VideoMonitor.mode = VMODE_1BIT; - break; - case 8: - VideoMonitor.mode = VMODE_8BIT; - break; - case 15: - VideoMonitor.mode = VMODE_16BIT; - break; - case 16: - VideoMonitor.mode = VMODE_16BIT; - break; - case 24: - case 32: - VideoMonitor.mode = VMODE_32BIT; - break; + D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base)); +} + +// Set window name and class +static void set_window_name(Window w, int name) +{ + const char *str = GetString(name); + XStoreName(x_display, w, str); + XSetIconName(x_display, w, str); + + XClassHint *hints; + hints = XAllocClassHint(); + if (hints) { + hints->res_name = "BasiliskII"; + hints->res_class = "BasiliskII"; + XSetClassHint(x_display, w, hints); + XFree(hints); } - VideoMonitor.x = width; - VideoMonitor.y = height; - VideoMonitor.bytes_per_row = bytes_per_row; +} + +// Set window input focus flag +static void set_window_focus(Window w) +{ + XWMHints *hints = XAllocWMHints(); + if (hints) { + hints->input = True; + hints->initial_state = NormalState; + hints->flags = InputHint | StateHint; + XSetWMHints(x_display, w, hints); + XFree(hints); + } +} + +// Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget) +static Atom WM_DELETE_WINDOW = (Atom)0; +static void set_window_delete_protocol(Window w) +{ + WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false); + XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1); +} + +// Wait until window is mapped/unmapped +void wait_mapped(Window w) +{ + XEvent e; + do { + XMaskEvent(x_display, StructureNotifyMask, &e); + } while ((e.type != MapNotify) || (e.xmap.event != w)); +} + +void wait_unmapped(Window w) +{ + XEvent e; + do { + XMaskEvent(x_display, StructureNotifyMask, &e); + } while ((e.type != UnmapNotify) || (e.xmap.event != w)); } // Trap SHM errors @@ -320,42 +297,143 @@ static int error_handler(Display *d, XEr return old_error_handler(d, e); } -// Init window mode -static bool init_window(int width, int height) + +/* + * Display "driver" classes + */ + +class driver_base { +public: + driver_base(); + virtual ~driver_base(); + + virtual void update_palette(void); + virtual void suspend(void) {} + virtual void resume(void) {} + +public: + bool init_ok; // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer) + Window w; // The window we draw into +}; + +class driver_window; +static void update_display_window_vosf(driver_window *drv); +static void update_display_dynamic(int ticker, driver_window *drv); +static void update_display_static(driver_window *drv); + +class driver_window : public driver_base { + friend void update_display_window_vosf(driver_window *drv); + friend void update_display_dynamic(int ticker, driver_window *drv); + friend void update_display_static(driver_window *drv); + +public: + driver_window(const video_mode &mode); + ~driver_window(); + +private: + GC gc; + XImage *img; + bool have_shm; // Flag: SHM extensions available + XShmSegmentInfo shminfo; + Cursor mac_cursor; +}; + +static driver_base *drv = NULL; // Pointer to currently used driver object + +#ifdef ENABLE_VOSF +# include "video_vosf.h" +#endif + +driver_base::driver_base() + : init_ok(false), w(0) { + the_buffer = NULL; + the_buffer_copy = NULL; +} + +driver_base::~driver_base() +{ + if (w) { + XUnmapWindow(x_display, w); + wait_unmapped(w); + XDestroyWindow(x_display, w); + } + + XFlush(x_display); + XSync(x_display, false); + + // Free frame buffer(s) + if (!use_vosf) { + if (the_buffer) { + free(the_buffer); + the_buffer = NULL; + } + if (the_buffer_copy) { + free(the_buffer_copy); + the_buffer_copy = NULL; + } + } +#ifdef ENABLE_VOSF + else { + if (the_buffer != (uint8 *)VM_MAP_FAILED) { + vm_release(the_buffer, the_buffer_size); + the_buffer = NULL; + } + if (the_buffer_copy != (uint8 *)VM_MAP_FAILED) { + vm_release(the_buffer_copy, the_buffer_size); + the_buffer_copy = NULL; + } + } +#endif +} + +// Palette has changed +void driver_base::update_palette(void) +{ + if (cmap[0] && cmap[1]) { + int num = 256; + if (IsDirectMode(VideoMonitor.mode)) + num = vis->map_entries; // Palette is gamma table + else if (vis->c_class == DirectColor) + return; // Indexed mode on true color screen, don't set CLUT + XStoreColors(x_display, cmap[0], palette, num); + XStoreColors(x_display, cmap[1], palette, num); + } + XSync(x_display, false); +} + + +/* + * Windowed display driver + */ + +// Open display +driver_window::driver_window(const video_mode &mode) + : gc(0), img(NULL), have_shm(false), mac_cursor(0) +{ + int width = mode.x, height = mode.y; int aligned_width = (width + 15) & ~15; int aligned_height = (height + 15) & ~15; // Set absolute mouse mode ADBSetRelMouseMode(false); - // Read frame skip prefs - frame_skip = PrefsFindInt32("frameskip"); - // Create window XSetWindowAttributes wattr; wattr.event_mask = eventmask = win_eventmask; wattr.background_pixel = black_pixel; - wattr.border_pixel = black_pixel; - wattr.backing_store = NotUseful; - wattr.save_under = false; - wattr.backing_planes = xdepth; + wattr.colormap = (mode.depth == VDEPTH_1BIT && vis->c_class == PseudoColor ? DefaultColormap(x_display, screen) : cmap[0]); + w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, + InputOutput, vis, CWEventMask | CWBackPixel | (vis->c_class == PseudoColor || vis->c_class == DirectColor ? CWColormap : 0), &wattr); - XSync(x_display, false); - the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, - InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | - CWBackingStore | CWBackingPlanes, &wattr); + // Set window name/class + set_window_name(w, STR_WINDOW_TITLE); // Indicate that we want keyboard input - { - XWMHints *hints = XAllocWMHints(); - if (hints) { - hints->input = True; - hints->flags = InputHint; - XSetWMHints(x_display, the_win, hints); - XFree((char *)hints); - } - } + set_window_focus(w); + + // Set delete protocol property + set_window_delete_protocol(w); // Make window unresizable { @@ -366,47 +444,24 @@ static bool init_window(int width, int h hints->min_height = height; hints->max_height = height; hints->flags = PMinSize | PMaxSize; - XSetWMNormalHints(x_display, the_win, hints); - XFree((char *)hints); + XSetWMNormalHints(x_display, w, hints); + XFree(hints); } } - // Set window title - { - XTextProperty title_prop; - const char *title = GetString(STR_WINDOW_TITLE); - XStringListToTextProperty((char **)&title, 1, &title_prop); - XSetWMName(x_display, the_win, &title_prop); - XFree(title_prop.value); - } - - // Set window class - { - XClassHint *hints; - hints = XAllocClassHint(); - if (hints) { - hints->res_name = "BasiliskII"; - hints->res_class = "BasiliskII"; - XSetClassHint(x_display, the_win, hints); - XFree((char *)hints); - } - } - // Show window - XSync(x_display, false); - XMapRaised(x_display, the_win); - XFlush(x_display); + XMapWindow(x_display, w); + wait_mapped(w); - // Set colormap - if (depth == 8) - XSetWindowColormap(x_display, the_win, cmap[0]); + // 1-bit mode is big-endian; if the X server is little-endian, we can't + // use SHM because that doesn't allow changing the image byte order + bool need_msb_image = (mode.depth == VDEPTH_1BIT && XImageByteOrder(x_display) == LSBFirst); // Try to create and attach SHM image - have_shm = false; - if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) { + if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) { // Create SHM image ("height + 2" for safety) - img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height); + img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height); shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777); the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0); shminfo.shmaddr = img->data = (char *)the_buffer_copy; @@ -430,86 +485,214 @@ static bool init_window(int width, int h // Create normal X image if SHM doesn't work ("height + 2" for safety) if (!have_shm) { - int bytes_per_row = aligned_width; - switch (depth) { - case 1: - bytes_per_row /= 8; - break; - case 15: - case 16: - bytes_per_row *= 2; - break; - case 24: - case 32: - bytes_per_row *= 4; - break; - } + int bytes_per_row = (mode.depth == VDEPTH_1BIT ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth))); the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row); - img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row); + img = XCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row); } - // 1-Bit mode is big-endian - if (depth == 1) { + if (need_msb_image) { img->byte_order = MSBFirst; img->bitmap_bit_order = MSBFirst; } #ifdef ENABLE_VOSF - // Allocate a page-aligned chunk of memory for frame buffer - the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line); + // Allocate memory for frame buffer (SIZE is extended to page-boundary) the_host_buffer = the_buffer_copy; - - the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size); - memset(the_buffer_copy, 0, the_buffer_size); - - the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size); - memset(the_buffer, 0, the_buffer_size); + the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line); + the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size); + the_buffer = (uint8 *)vm_acquire(the_buffer_size); #else // Allocate memory for frame buffer the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line); #endif // Create GC - the_gc = XCreateGC(x_display, the_win, 0, 0); - XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes); + gc = XCreateGC(x_display, w, 0, 0); + XSetState(x_display, gc, black_pixel, white_pixel, GXcopy, AllPlanes); // Create no_cursor mac_cursor = XCreatePixmapCursor(x_display, - XCreatePixmap(x_display, the_win, 1, 1, 1), - XCreatePixmap(x_display, the_win, 1, 1, 1), + XCreatePixmap(x_display, w, 1, 1, 1), + XCreatePixmap(x_display, w, 1, 1, 1), &black, &white, 0, 0); - XDefineCursor(x_display, the_win, mac_cursor); + XDefineCursor(x_display, w, mac_cursor); - // Set VideoMonitor + // Init blitting routines bool native_byte_order; #ifdef WORDS_BIGENDIAN - native_byte_order = (img->bitmap_bit_order == MSBFirst); + native_byte_order = (XImageByteOrder(x_display) == MSBFirst); #else - native_byte_order = (img->bitmap_bit_order == LSBFirst); + native_byte_order = (XImageByteOrder(x_display) == LSBFirst); #endif #ifdef ENABLE_VOSF - do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW); + Screen_blitter_init(&visualInfo, native_byte_order, mode.depth); #endif - set_video_monitor(width, height, img->bytes_per_line, native_byte_order); - -#if REAL_ADDRESSING || DIRECT_ADDRESSING - VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer); -#else - VideoMonitor.mac_frame_base = MacFrameBaseMac; + + // Set VideoMonitor + VideoMonitor.mode = mode; + set_mac_frame_buffer(mode.depth, native_byte_order); + + // Everything went well + init_ok = true; +} + +// Close display +driver_window::~driver_window() +{ + if (img) + XDestroyImage(img); + if (have_shm) { + XShmDetach(x_display, &shminfo); + the_buffer_copy = NULL; // don't free() in driver_base dtor + } + if (gc) + XFreeGC(x_display, gc); +} + + +#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) +/* + * DGA display driver base class + */ + +class driver_dga : public driver_base { +public: + driver_dga(); + ~driver_dga(); + + void suspend(void); + void resume(void); + +private: + Window suspend_win; // "Suspend" information window + void *fb_save; // Saved frame buffer for suspend/resume +}; + +driver_dga::driver_dga() + : suspend_win(0), fb_save(NULL) +{ +} + +driver_dga::~driver_dga() +{ + XUngrabPointer(x_display, CurrentTime); + XUngrabKeyboard(x_display, CurrentTime); +} + +// Suspend emulation +void driver_dga::suspend(void) +{ + // Release ctrl key + ADBKeyUp(0x36); + ctrl_down = false; + + // Lock frame buffer (this will stop the MacOS thread) + LOCK_FRAME_BUFFER; + + // Save frame buffer + fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row); + if (fb_save) + memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row); + + // Close full screen display +#ifdef ENABLE_XF86_DGA + XF86DGADirectVideo(x_display, screen, 0); #endif - return true; + XUngrabPointer(x_display, CurrentTime); + XUngrabKeyboard(x_display, CurrentTime); + XUnmapWindow(x_display, w); + wait_unmapped(w); + + // Open "suspend" window + XSetWindowAttributes wattr; + wattr.event_mask = KeyPressMask; + wattr.background_pixel = black_pixel; + + suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth, + InputOutput, vis, CWEventMask | CWBackPixel, &wattr); + set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE); + set_window_focus(suspend_win); + XMapWindow(x_display, suspend_win); + emul_suspended = true; } -// Init fbdev DGA display -static bool init_fbdev_dga(char *in_fb_name) +// Resume emulation +void driver_dga::resume(void) { + // Close "suspend" window + XDestroyWindow(x_display, suspend_win); + XSync(x_display, false); + + // Reopen full screen display + XMapRaised(x_display, w); + wait_mapped(w); + XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); + XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime); + XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); +#ifdef ENABLE_XF86_DGA + XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse); + XF86DGASetViewPort(x_display, screen, 0, 0); +#endif + XSync(x_display, false); + + // the_buffer already contains the data to restore. i.e. since a temporary + // frame buffer is used when VOSF is actually used, fb_save is therefore + // not necessary. +#ifdef ENABLE_VOSF + if (use_vosf) { + LOCK_VOSF; + PFLAG_SET_ALL; + UNLOCK_VOSF; + memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); + } +#endif + + // Restore frame buffer + if (fb_save) { +#ifdef ENABLE_VOSF + // Don't copy fb_save to the temporary frame buffer in VOSF mode + if (!use_vosf) +#endif + memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row); + free(fb_save); + fb_save = NULL; + } + + // Unlock frame buffer (and continue MacOS thread) + UNLOCK_FRAME_BUFFER; + emul_suspended = false; +} +#endif + + #ifdef ENABLE_FBDEV_DGA +/* + * fbdev DGA display driver + */ + +const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices"; +const char FBDEVICE_FILE_NAME[] = "/dev/fb"; + +class driver_fbdev : public driver_dga { +public: + driver_fbdev(const video_mode &mode); + ~driver_fbdev(); +}; + +// Open display +driver_fbdev::driver_fbdev(const video_mode &mode) +{ + int width = mode.x, height = mode.y; + + // Set absolute mouse mode + ADBSetRelMouseMode(false); + // Find the maximum depth available int ndepths, max_depth(0); int *depths = XListDepths(x_display, screen, &ndepths); if (depths == NULL) { printf("FATAL: Could not determine the maximal depth available\n"); - return false; + return; } else { while (ndepths-- > 0) { if (depths[ndepths] > max_depth) @@ -526,7 +709,7 @@ static bool init_fbdev_dga(char *in_fb_n char str[256]; sprintf(str, GetString(STR_NO_FBDEVICE_FILE_ERR), fbd_path ? fbd_path : FBDEVICES_FILE_NAME, strerror(errno)); ErrorAlert(str); - return false; + return; } int fb_depth; // supported depth @@ -546,7 +729,7 @@ static bool init_fbdev_dga(char *in_fb_n continue; if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3) - && (strcmp(fb_name, in_fb_name) == 0) && (fb_depth == max_depth)) { + && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) { device_found = true; break; } @@ -558,111 +741,112 @@ static bool init_fbdev_dga(char *in_fb_n // Frame buffer name not found ? Then, display warning if (!device_found) { char str[256]; - sprintf(str, GetString(STR_FBDEV_NAME_ERR), in_fb_name, max_depth); + sprintf(str, GetString(STR_FBDEV_NAME_ERR), fb_name, max_depth); ErrorAlert(str); - return false; + return; } - int width = DisplayWidth(x_display, screen); - int height = DisplayHeight(x_display, screen); - depth = fb_depth; // max_depth - - // Set relative mouse mode - ADBSetRelMouseMode(false); - // Create window XSetWindowAttributes wattr; - wattr.override_redirect = True; - wattr.backing_store = NotUseful; - wattr.background_pixel = white_pixel; - wattr.border_pixel = black_pixel; - wattr.event_mask = eventmask = dga_eventmask; + wattr.event_mask = eventmask = dga_eventmask; + wattr.background_pixel = white_pixel; + wattr.override_redirect = True; + wattr.colormap = cmap[0]; - XSync(x_display, false); - the_win = XCreateWindow(x_display, rootwin, + w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, InputOutput, vis, - CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore, + CWEventMask | CWBackPixel | CWOverrideRedirect | (fb_depth <= 8 ? CWColormap : 0), &wattr); - XSync(x_display, false); - XMapRaised(x_display, the_win); - XSync(x_display, false); + + // Set window name/class + set_window_name(w, STR_WINDOW_TITLE); + + // Indicate that we want keyboard input + set_window_focus(w); + + // Show window + XMapRaised(x_display, w); + wait_mapped(w); // Grab mouse and keyboard - XGrabKeyboard(x_display, the_win, True, + XGrabKeyboard(x_display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime); - XGrabPointer(x_display, the_win, True, + XGrabPointer(x_display, w, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, - GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime); + GrabModeAsync, GrabModeAsync, w, None, CurrentTime); - // Set colormap - if (depth == 8) - XSetWindowColormap(x_display, the_win, cmap[0]); - - // Set VideoMonitor - int bytes_per_row = width; - switch (depth) { - case 1: - bytes_per_row = ((width | 7) & ~7) >> 3; - break; - case 15: - case 16: - bytes_per_row *= 2; - break; - case 24: - case 32: - bytes_per_row *= 4; - break; - } + // Calculate bytes per row + int bytes_per_row = TrivialBytesPerRow(mode.x, mode.depth); + // Map frame buffer if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) { if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) { char str[256]; sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno)); ErrorAlert(str); - return false; + return; } } #if ENABLE_VOSF #if REAL_ADDRESSING || DIRECT_ADDRESSING - // If the blit function is null, i.e. just a copy of the buffer, - // we first try to avoid the allocation of a temporary frame buffer - use_vosf = true; - do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA); - if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw)) - use_vosf = false; + // Screen_blitter_init() returns TRUE if VOSF is mandatory + // i.e. the framebuffer update function is not Blit_Copy_Raw + use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth); if (use_vosf) { - the_host_buffer = the_buffer; - the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row); - the_buffer_copy = (uint8 *)malloc(the_buffer_size); - memset(the_buffer_copy, 0, the_buffer_size); - the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size); - memset(the_buffer, 0, the_buffer_size); + // Allocate memory for frame buffer (SIZE is extended to page-boundary) + the_host_buffer = the_buffer; + the_buffer_size = page_extend((height + 2) * bytes_per_row); + the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size); + the_buffer = (uint8 *)vm_acquire(the_buffer_size); } #else use_vosf = false; #endif #endif - set_video_monitor(width, height, bytes_per_row, true); -#if REAL_ADDRESSING || DIRECT_ADDRESSING - VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer); -#else - VideoMonitor.mac_frame_base = MacFrameBaseMac; -#endif - return true; -#else - ErrorAlert("Basilisk II has been compiled with fbdev DGA support disabled."); - return false; -#endif + // Set VideoMonitor + VideoModes[0].bytes_per_row = bytes_per_row; + VideoModes[0].depth = DepthModeForPixelDepth(fb_depth); + VideoMonitor.mode = mode; + set_mac_frame_buffer(mode.depth, true); + + // Everything went well + init_ok = true; } -// Init XF86 DGA display -static bool init_xf86_dga(int width, int height) +// Close display +driver_fbdev::~driver_fbdev() { +} +#endif + + #ifdef ENABLE_XF86_DGA +/* + * XFree86 DGA display driver + */ + +class driver_xf86dga : public driver_dga { +public: + driver_xf86dga(const video_mode &mode); + ~driver_xf86dga(); + + void update_palette(void); + void resume(void); + +private: + int current_dga_cmap; // Number (0 or 1) of currently installed DGA colormap +}; + +// Open display +driver_xf86dga::driver_xf86dga(const video_mode &mode) + : current_dga_cmap(0) +{ + int width = mode.x, height = mode.y; + // Set relative mouse mode ADBSetRelMouseMode(true); @@ -678,25 +862,30 @@ static bool init_xf86_dga(int width, int } XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]); XF86VidModeSetViewPort(x_display, screen, 0, 0); + XSync(x_display, false); } #endif // Create window XSetWindowAttributes wattr; wattr.event_mask = eventmask = dga_eventmask; - wattr.border_pixel = black_pixel; wattr.override_redirect = True; - XSync(x_display, false); - the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, - InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr); - XSync(x_display, false); - XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); - XMapRaised(x_display, the_win); - XSync(x_display, false); + w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, + InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr); + + // Set window name/class + set_window_name(w, STR_WINDOW_TITLE); + + // Indicate that we want keyboard input + set_window_focus(w); + + // Show window + XMapRaised(x_display, w); + wait_mapped(w); // Establish direct screen connection - XMoveResizeWindow(x_display, the_win, 0, 0, width, height); + XMoveResizeWindow(x_display, w, 0, 0, width, height); XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime); XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); @@ -708,62 +897,74 @@ static bool init_xf86_dga(int width, int XF86DGASetVidPage(x_display, screen, 0); // Set colormap - if (depth == 8) { - XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]); + if (!IsDirectMode(mode)) { + XSetWindowColormap(x_display, w, cmap[current_dga_cmap = 0]); XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); } + XSync(x_display, false); - // Set VideoMonitor - int bytes_per_row = (v_width + 7) & ~7; - switch (depth) { - case 1: - bytes_per_row /= 8; - break; - case 15: - case 16: - bytes_per_row *= 2; - break; - case 24: - case 32: - bytes_per_row *= 4; - break; - } - + // Init blitting routines + int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth); +#ifdef VIDEO_VOSF #if REAL_ADDRESSING || DIRECT_ADDRESSING - // If the blit function is null, i.e. just a copy of the buffer, - // we first try to avoid the allocation of a temporary frame buffer - use_vosf = true; - do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA); - if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw)) - use_vosf = false; + // Screen_blitter_init() returns TRUE if VOSF is mandatory + // i.e. the framebuffer update function is not Blit_Copy_Raw + use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth); if (use_vosf) { - the_host_buffer = the_buffer; - the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row); - the_buffer_copy = (uint8 *)malloc(the_buffer_size); - memset(the_buffer_copy, 0, the_buffer_size); - the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size); - memset(the_buffer, 0, the_buffer_size); + // Allocate memory for frame buffer (SIZE is extended to page-boundary) + the_host_buffer = the_buffer; + the_buffer_size = page_extend((height + 2) * bytes_per_row); + the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size); + the_buffer = (uint8 *)vm_acquire(the_buffer_size); } -#elif defined(ENABLE_VOSF) - // The UAE memory handlers will already handle color conversion, if needed. +#else use_vosf = false; #endif - - set_video_monitor(width, height, bytes_per_row, true); -#if REAL_ADDRESSING || DIRECT_ADDRESSING - VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer); -// MacFrameLayout = FLAYOUT_DIRECT; -#else - VideoMonitor.mac_frame_base = MacFrameBaseMac; #endif - return true; -#else - ErrorAlert("Basilisk II has been compiled with XF86 DGA support disabled."); - return false; + + // Set VideoMonitor + const_cast(&mode)->bytes_per_row = bytes_per_row; + VideoMonitor.mode = mode; + set_mac_frame_buffer(mode.depth, true); + + // Everything went well + init_ok = true; +} + +// Close display +driver_xf86dga::~driver_xf86dga() +{ + XF86DGADirectVideo(x_display, screen, 0); +#ifdef ENABLE_XF86_VIDMODE + if (has_vidmode) + XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]); #endif } +// Palette has changed +void driver_xf86dga::update_palette(void) +{ + driver_dga::update_palette(); + current_dga_cmap ^= 1; + if (!IsDirectMode(VideoMonitor.mode) && cmap[current_dga_cmap]) + XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); +} + +// Resume emulation +void driver_xf86dga::resume(void) +{ + driver_dga::resume(); + if (!IsDirectMode(VideoMonitor.mode)) + XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); +} +#endif + + +/* + * Initialization + */ + // Init keycode translation table static void keycode_init(void) { @@ -829,79 +1030,102 @@ static void keycode_init(void) } } -bool VideoInitBuffer() +// Open display for specified mode +static bool video_open(const video_mode &mode) { -#ifdef ENABLE_VOSF - if (use_vosf) { - const uint32 page_size = getpagesize(); - const uint32 page_mask = page_size - 1; - - mainBuffer.memBase = (uint32) the_buffer; - // Align the frame buffer on page boundary - mainBuffer.memStart = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask); - mainBuffer.memLength = the_buffer_size; - mainBuffer.memEnd = mainBuffer.memStart + mainBuffer.memLength; - - mainBuffer.pageSize = page_size; - mainBuffer.pageCount = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize; - mainBuffer.pageBits = log_base_2(mainBuffer.pageSize); - - if (mainBuffer.dirtyPages != 0) - free(mainBuffer.dirtyPages); + // Load gray ramp to color map + int num = (vis->c_class == DirectColor ? vis->map_entries : 256); + for (int i=0; ic_class == PseudoColor) + palette[i].pixel = i; + palette[i].flags = DoRed | DoGreen | DoBlue; + } + if (cmap[0] && cmap[1]) { + XStoreColors(x_display, cmap[0], palette, num); + XStoreColors(x_display, cmap[1], palette, num); + } - mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount); +#ifdef ENABLE_VOSF + // Load gray ramp to 8->16/32 expand map + if (!IsDirectMode(mode) && (vis->c_class == TrueColor || vis->c_class == DirectColor)) + for (int i=0; i<256; i++) + ExpandMap[i] = map_rgb(i, i, i); +#endif - if (mainBuffer.pageInfo != 0) - free(mainBuffer.pageInfo); + // Create display driver object of requested type + switch (display_type) { + case DISPLAY_WINDOW: + drv = new driver_window(mode); + break; +#ifdef ENABLE_FBDEV_DGA + case DISPLAY_DGA: + drv = new driver_fbdev(mode); + break; +#endif +#ifdef ENABLE_XF86_DGA + case DISPLAY_DGA: + drv = new driver_xf86dga(mode); + break; +#endif + } + if (drv == NULL) + return false; + if (!drv->init_ok) { + delete drv; + drv = NULL; + return false; + } - mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo)); +#ifdef ENABLE_VOSF + if (use_vosf) { + // Initialize the mainBuffer structure + if (!video_init_buffer()) { + ErrorAlert(STR_VOSF_INIT_ERR); + return false; + } - if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0)) + // Initialize the handler for SIGSEGV + if (!sigsegv_install_handler(screen_fault_handler)) { + ErrorAlert("Could not initialize Video on SEGV signals"); return false; + } + } +#endif + + // Initialize VideoRefresh function + VideoRefreshInit(); - PFLAG_CLEAR_ALL; + // Lock down frame buffer + XSync(x_display, false); + LOCK_FRAME_BUFFER; - uint32 a = 0; - for (int i = 0; i < mainBuffer.pageCount; i++) { - int y1 = a / VideoMonitor.bytes_per_row; - if (y1 >= VideoMonitor.y) - y1 = VideoMonitor.y - 1; - - int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row; - if (y2 >= VideoMonitor.y) - y2 = VideoMonitor.y - 1; - - mainBuffer.pageInfo[i].top = y1; - mainBuffer.pageInfo[i].bottom = y2; - - a += mainBuffer.pageSize; - if (a > mainBuffer.memLength) - a = mainBuffer.memLength; - } - - // We can now write-protect the frame buffer - if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0) - return false; + // Start redraw/input thread +#ifdef HAVE_PTHREADS + redraw_thread_cancel = false; + redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0); + if (!redraw_thread_active) { + printf("FATAL: cannot create redraw thread\n"); + return false; } +#else + redraw_thread_active = true; #endif + return true; } bool VideoInit(bool classic) { + classic_mode = classic; + #ifdef ENABLE_VOSF - // Open /dev/zero - zero_fd = open("/dev/zero", O_RDWR); - if (zero_fd < 0) { - char str[256]; - sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno)); - ErrorAlert(str); - return false; - } - // Zero the mainBuffer structure - mainBuffer.dirtyPages = 0; - mainBuffer.pageInfo = 0; + mainBuffer.dirtyPages = NULL; + mainBuffer.pageInfo = NULL; #endif // Check if X server runs on local machine @@ -912,8 +1136,9 @@ bool VideoInit(bool classic) keycode_init(); // Read prefs - mouse_wheel_mode = PrefsFindInt16("mousewheelmode"); - mouse_wheel_lines = PrefsFindInt16("mousewheellines"); + frame_skip = PrefsFindInt32("frameskip"); + mouse_wheel_mode = PrefsFindInt32("mousewheelmode"); + mouse_wheel_lines = PrefsFindInt32("mousewheellines"); // Find screen and root window screen = XDefaultScreen(x_display); @@ -926,7 +1151,7 @@ bool VideoInit(bool classic) // Frame buffer name char fb_name[20]; - // Could do fbdev dga ? + // Could do fbdev DGA? if ((fbdev_fd = open(FBDEVICE_FILE_NAME, O_RDWR)) != -1) has_dga = true; else @@ -972,131 +1197,143 @@ bool VideoInit(bool classic) case 15: case 16: case 24: - case 32: - color_class = TrueColor; + case 32: // Try DirectColor first, as this will allow gamma correction + color_class = DirectColor; + if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) + color_class = TrueColor; break; default: - ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR)); + ErrorAlert(STR_UNSUPP_DEPTH_ERR); return false; } if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) { - ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); + ErrorAlert(STR_NO_XVISUAL_ERR); return false; } if (visualInfo.depth != xdepth) { - ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); + ErrorAlert(STR_NO_XVISUAL_ERR); return false; } vis = visualInfo.visual; - // Mac screen depth is always 1 bit in Classic mode, but follows X depth otherwise - classic_mode = classic; - if (classic) - depth = 1; - else - depth = xdepth; - - // Create color maps for 8 bit mode - if (depth == 8) { + // Create color maps + if (color_class == PseudoColor || color_class == DirectColor) { cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll); cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll); - XInstallColormap(x_display, cmap[0]); - XInstallColormap(x_display, cmap[1]); + } + + // Find pixel format of direct modes + if (color_class == DirectColor || color_class == TrueColor) { + rshift = gshift = bshift = 0; + rloss = gloss = bloss = 8; + uint32 mask; + for (mask=vis->red_mask; !(mask&1); mask>>=1) + ++rshift; + for (; mask&1; mask>>=1) + --rloss; + for (mask=vis->green_mask; !(mask&1); mask>>=1) + ++gshift; + for (; mask&1; mask>>=1) + --gloss; + for (mask=vis->blue_mask; !(mask&1); mask>>=1) + ++bshift; + for (; mask&1; mask>>=1) + --bloss; + } + + // Preset palette pixel values for gamma table + if (color_class == DirectColor) { + int num = vis->map_entries; + for (int i=0; i DisplayWidth(x_display, screen)) - width = DisplayWidth(x_display, screen); - if (height > DisplayHeight(x_display, screen)) - height = DisplayHeight(x_display, screen); - } - if (width <= 0) - width = DisplayWidth(x_display, screen); - if (height <= 0) - height = DisplayHeight(x_display, screen); - } - - // Initialize according to display type - switch (display_type) { - case DISPLAY_WINDOW: - if (!init_window(width, height)) - return false; - break; - case DISPLAY_DGA: -#ifdef ENABLE_FBDEV_DGA - if (!init_fbdev_dga(fb_name)) -#else - if (!init_xf86_dga(width, height)) #endif - return false; - break; + } } + if (default_width <= 0) + default_width = DisplayWidth(x_display, screen); + else if (default_width > DisplayWidth(x_display, screen)) + default_width = DisplayWidth(x_display, screen); + if (default_height <= 0) + default_height = DisplayHeight(x_display, screen); + else if (default_height > DisplayHeight(x_display, screen)) + default_height = DisplayHeight(x_display, screen); -#ifdef HAVE_PTHREADS - // Lock down frame buffer - pthread_mutex_lock(&frame_buffer_lock); -#endif - -#if !REAL_ADDRESSING && !DIRECT_ADDRESSING - // Set variables for UAE memory mapping - MacFrameBaseHost = the_buffer; - MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y; - - // No special frame buffer in Classic mode (frame buffer is in Mac RAM) - if (classic) - MacFrameLayout = FLAYOUT_NONE; -#endif + // Mac screen depth follows X depth + video_depth default_depth = DepthModeForPixelDepth(xdepth); + // Construct list of supported modes + if (display_type == DISPLAY_WINDOW) { + if (classic) + add_mode(512, 342, 0x80, 64, VDEPTH_1BIT); + else { + if (default_depth != VDEPTH_1BIT) { // 1-bit modes are always available + add_mode(512, 384, 0x80, TrivialBytesPerRow(512, VDEPTH_1BIT), VDEPTH_1BIT); + add_mode(640, 480, 0x81, TrivialBytesPerRow(640, VDEPTH_1BIT), VDEPTH_1BIT); + add_mode(800, 600, 0x82, TrivialBytesPerRow(800, VDEPTH_1BIT), VDEPTH_1BIT); + add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, VDEPTH_1BIT), VDEPTH_1BIT); + add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, VDEPTH_1BIT), VDEPTH_1BIT); + add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, VDEPTH_1BIT), VDEPTH_1BIT); + add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, VDEPTH_1BIT), VDEPTH_1BIT); + } #ifdef ENABLE_VOSF - if (use_vosf) { - // Initialize the mainBuffer structure - if (!VideoInitBuffer()) { - // TODO: STR_VOSF_INIT_ERR ? - ErrorAlert("Could not initialize Video on SEGV signals"); - return false; + if (default_depth > VDEPTH_8BIT) { // 8-bit modes are also possible on 16/32-bit screens with VOSF blitters + add_mode(512, 384, 0x80, TrivialBytesPerRow(512, VDEPTH_8BIT), VDEPTH_8BIT); + add_mode(640, 480, 0x81, TrivialBytesPerRow(640, VDEPTH_8BIT), VDEPTH_8BIT); + add_mode(800, 600, 0x82, TrivialBytesPerRow(800, VDEPTH_8BIT), VDEPTH_8BIT); + add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, VDEPTH_8BIT), VDEPTH_8BIT); + add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, VDEPTH_8BIT), VDEPTH_8BIT); + add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, VDEPTH_8BIT), VDEPTH_8BIT); + add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, VDEPTH_8BIT), VDEPTH_8BIT); + } +#endif + add_mode(512, 384, 0x80, TrivialBytesPerRow(512, default_depth), default_depth); + add_mode(640, 480, 0x81, TrivialBytesPerRow(640, default_depth), default_depth); + add_mode(800, 600, 0x82, TrivialBytesPerRow(800, default_depth), default_depth); + add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, default_depth), default_depth); + add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, default_depth), default_depth); + add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, default_depth), default_depth); + add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, default_depth), default_depth); } + } else + add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth); - // Initialize the handler for SIGSEGV - if (!Screen_fault_handler_init()) { - // TODO: STR_VOSF_INIT_ERR ? - ErrorAlert("Could not initialize Video on SEGV signals"); - return false; + // Find requested default mode and open display + if (VideoModes.size() == 1) + return video_open(VideoModes[0]); + else { + // Find mode with specified dimensions + std::vector::const_iterator i, end = VideoModes.end(); + for (i = VideoModes.begin(); i != end; ++i) { + if (i->x == default_width && i->y == default_height && i->depth == default_depth) + return video_open(*i); } + return video_open(VideoModes[0]); } -#endif - - // Initialize VideoRefresh function - VideoRefreshInit(); - - XSync(x_display, false); - -#ifdef HAVE_PTHREADS - // Start redraw/input thread - redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0); - if (!redraw_thread_active) { - printf("FATAL: cannot create redraw thread\n"); - return false; - } -#endif - return true; } @@ -1104,100 +1341,73 @@ bool VideoInit(bool classic) * Deinitialization */ -void VideoExit(void) +// Close display +static void video_close(void) { -#ifdef HAVE_PTHREADS // Stop redraw thread +#ifdef HAVE_PTHREADS if (redraw_thread_active) { redraw_thread_cancel = true; #ifdef HAVE_PTHREAD_CANCEL pthread_cancel(redraw_thread); #endif pthread_join(redraw_thread, NULL); - redraw_thread_active = false; } #endif + redraw_thread_active = false; -#ifdef HAVE_PTHREADS // Unlock frame buffer - pthread_mutex_unlock(&frame_buffer_lock); -#endif - - // Close window and server connection - if (x_display != NULL) { - XSync(x_display, false); - -#ifdef ENABLE_XF86_DGA - if (display_type == DISPLAY_DGA) { - XF86DGADirectVideo(x_display, screen, 0); - XUngrabPointer(x_display, CurrentTime); - XUngrabKeyboard(x_display, CurrentTime); - } -#endif - -#ifdef ENABLE_XF86_VIDMODE - if (has_vidmode && display_type == DISPLAY_DGA) - XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]); -#endif - -#ifdef ENABLE_FBDEV_DGA - if (display_type == DISPLAY_DGA) { - XUngrabPointer(x_display, CurrentTime); - XUngrabKeyboard(x_display, CurrentTime); - close(fbdev_fd); - } -#endif - - XFlush(x_display); - XSync(x_display, false); - if (depth == 8) { - XFreeColormap(x_display, cmap[0]); - XFreeColormap(x_display, cmap[1]); - } - - if (!use_vosf) { - if (the_buffer) { - free(the_buffer); - the_buffer = NULL; - } + UNLOCK_FRAME_BUFFER; + XSync(x_display, false); - if (!have_shm && the_buffer_copy) { - free(the_buffer_copy); - the_buffer_copy = NULL; - } - } -#ifdef ENABLE_VOSF - else { - if (the_buffer != (uint8 *)MAP_FAILED) { - munmap((caddr_t)the_buffer, the_buffer_size); - the_buffer = 0; - } - - if (the_buffer_copy != (uint8 *)MAP_FAILED) { - munmap((caddr_t)the_buffer_copy, the_buffer_size); - the_buffer_copy = 0; - } - } -#endif - } - #ifdef ENABLE_VOSF + // Deinitialize VOSF if (use_vosf) { - // Clear mainBuffer data if (mainBuffer.pageInfo) { free(mainBuffer.pageInfo); - mainBuffer.pageInfo = 0; + mainBuffer.pageInfo = NULL; } - if (mainBuffer.dirtyPages) { free(mainBuffer.dirtyPages); - mainBuffer.dirtyPages = 0; + mainBuffer.dirtyPages = NULL; } } +#endif + + // Close display + delete drv; + drv = NULL; +} + +void VideoExit(void) +{ + // Close display + video_close(); + + // Free colormaps + if (cmap[0]) { + XFreeColormap(x_display, cmap[0]); + cmap[0] = 0; + } + if (cmap[1]) { + XFreeColormap(x_display, cmap[1]); + cmap[1] = 0; + } - // Close /dev/zero - if (zero_fd > 0) - close(zero_fd); +#ifdef ENABLE_XF86_VIDMODE + // Free video mode list + if (x_video_modes) { + XFree(x_video_modes); + x_video_modes = NULL; + } +#endif + +#ifdef ENABLE_FBDEV_DGA + // Close framebuffer device + if (fbdev_fd >= 0) { + close(fbdev_fd); + fbdev_fd = -1; + } #endif } @@ -1209,8 +1419,7 @@ void VideoExit(void) void VideoQuitFullScreen(void) { D(bug("VideoQuitFullScreen()\n")); - if (display_type == DISPLAY_DGA) - quit_full_screen = true; + quit_full_screen = true; } @@ -1224,12 +1433,10 @@ void VideoInterrupt(void) if (emerg_quit) QuitEmulator(); -#ifdef HAVE_PTHREADS // Temporarily give up frame buffer lock (this is the point where // we are suspended when the user presses Ctrl-Tab) - pthread_mutex_unlock(&frame_buffer_lock); - pthread_mutex_lock(&frame_buffer_lock); -#endif + UNLOCK_FRAME_BUFFER; + LOCK_FRAME_BUFFER; } @@ -1239,137 +1446,64 @@ void VideoInterrupt(void) void video_set_palette(uint8 *pal) { -#ifdef HAVE_PTHREDS - pthread_mutex_lock(&palette_lock); -#endif + LOCK_PALETTE; // Convert colors to XColor array - for (int i=0; i<256; i++) { - palette[i].pixel = i; - palette[i].red = pal[i*3] * 0x0101; - palette[i].green = pal[i*3+1] * 0x0101; - palette[i].blue = pal[i*3+2] * 0x0101; - palette[i].flags = DoRed | DoGreen | DoBlue; + int num_in = 256, num_out = 256; + if (VideoMonitor.mode.depth == VDEPTH_16BIT) + num_in = 32; + if (IsDirectMode(VideoMonitor.mode)) { + // If X is in 565 mode we have to stretch the palette from 32 to 64 entries + num_out = vis->map_entries; + } + XColor *p = palette; + for (int i=0; ired = pal[c*3 + 0] * 0x0101; + p->green = pal[c*3 + 1] * 0x0101; + p->blue = pal[c*3 + 2] * 0x0101; + if (vis->c_class == PseudoColor) + p->pixel = i; + p->flags = DoRed | DoGreen | DoBlue; + p++; } +#ifdef ENABLE_VOSF + // Recalculate pixel color expansion map + if (!IsDirectMode(VideoMonitor.mode) && (vis->c_class == TrueColor || vis->c_class == DirectColor)) { + for (int i=0; i<256; i++) + ExpandMap[i] = map_rgb(pal[i*3+0], pal[i*3+1], pal[i*3+2]); + + // We have to redraw everything because the interpretation of pixel values changed + LOCK_VOSF; + PFLAG_SET_ALL; + UNLOCK_VOSF; + memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); + } +#endif + // Tell redraw thread to change palette palette_changed = true; -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&palette_lock); -#endif + UNLOCK_PALETTE; } /* - * Suspend/resume emulator + * Switch video mode */ -#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) -static void suspend_emul(void) +void video_switch_to_mode(const video_mode &mode) { - if (display_type == DISPLAY_DGA) { - // Release ctrl key - ADBKeyUp(0x36); - ctrl_down = false; - -#ifdef HAVE_PTHREADS - // Lock frame buffer (this will stop the MacOS thread) - pthread_mutex_lock(&frame_buffer_lock); -#endif - - // Save frame buffer - fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row); - if (fb_save) - memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row); - - // Close full screen display -#ifdef ENABLE_XF86_DGA - XF86DGADirectVideo(x_display, screen, 0); -#endif - XUngrabPointer(x_display, CurrentTime); - XUngrabKeyboard(x_display, CurrentTime); - XUnmapWindow(x_display, the_win); - XSync(x_display, false); + // Close and reopen display + video_close(); + video_open(mode); - // Open "suspend" window - XSetWindowAttributes wattr; - wattr.event_mask = KeyPressMask; - wattr.background_pixel = black_pixel; - wattr.border_pixel = black_pixel; - wattr.backing_store = Always; - wattr.backing_planes = xdepth; - wattr.colormap = DefaultColormap(x_display, screen); - - XSync(x_display, false); - suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth, - InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | - CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr); - XSync(x_display, false); - XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE)); - XMapRaised(x_display, suspend_win); - XSync(x_display, false); - emul_suspended = true; - } -} - -static void resume_emul(void) -{ - // Close "suspend" window - XDestroyWindow(x_display, suspend_win); - XSync(x_display, false); - - // Reopen full screen display - XMapRaised(x_display, the_win); - XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); - XSync(x_display, false); - XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime); - XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); -#ifdef ENABLE_XF86_DGA - XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse); - XF86DGASetViewPort(x_display, screen, 0, 0); -#endif - XSync(x_display, false); - - // the_buffer already contains the data to restore. i.e. since a temporary - // frame buffer is used when VOSF is actually used, fb_save is therefore - // not necessary. -#ifdef ENABLE_VOSF - if (use_vosf) { -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&Screen_draw_lock); -#endif - PFLAG_SET_ALL; -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&Screen_draw_lock); -#endif - memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); - } -#endif - - // Restore frame buffer - if (fb_save) { -#ifdef ENABLE_VOSF - // Don't copy fb_save to the temporary frame buffer in VOSF mode - if (!use_vosf) -#endif - memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row); - free(fb_save); - fb_save = NULL; + if (drv == NULL) { + ErrorAlert(STR_OPEN_WINDOW_ERR); + QuitEmulator(); } - -#ifdef ENABLE_XF86_DGA - if (depth == 8) - XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); -#endif - -#ifdef HAVE_PTHREADS - // Unlock frame buffer (and continue MacOS thread) - pthread_mutex_unlock(&frame_buffer_lock); - emul_suspended = false; -#endif } -#endif /* @@ -1430,7 +1564,7 @@ static int kc_decode(KeySym ks) case XK_slash: case XK_question: return 0x2c; #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) - case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30; + case XK_Tab: if (ctrl_down) {drv->suspend(); return -1;} else return 0x30; #else case XK_Tab: return 0x30; #endif @@ -1521,14 +1655,14 @@ static int kc_decode(KeySym ks) return -1; } -static int event2keycode(XKeyEvent *ev) +static int event2keycode(XKeyEvent &ev) { KeySym ks; int as; int i = 0; do { - ks = XLookupKeysym(ev, i++); + ks = XLookupKeysym(&ev, i++); as = kc_decode(ks); if (as != -1) return as; @@ -1544,15 +1678,14 @@ static int event2keycode(XKeyEvent *ev) static void handle_events(void) { - XEvent event; - for (;;) { - if (!XCheckMaskEvent(x_display, eventmask, &event)) - break; + while (XPending(x_display)) { + XEvent event; + XNextEvent(x_display, &event); switch (event.type) { // Mouse button case ButtonPress: { - unsigned int button = ((XButtonEvent *)&event)->button; + unsigned int button = event.xbutton.button; if (button < 4) ADBMouseDown(button - 1); else if (button < 6) { // Wheel mouse @@ -1571,7 +1704,7 @@ static void handle_events(void) break; } case ButtonRelease: { - unsigned int button = ((XButtonEvent *)&event)->button; + unsigned int button = event.xbutton.button; if (button < 4) ADBMouseUp(button - 1); break; @@ -1579,20 +1712,18 @@ static void handle_events(void) // Mouse moved case EnterNotify: - ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); - break; case MotionNotify: - ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); + ADBMouseMoved(event.xmotion.x, event.xmotion.y); break; // Keyboard case KeyPress: { int code; if (use_keycodes) { - event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys - code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff]; + event2keycode(event.xkey); // This is called to process the hotkeys + code = keycode_table[event.xkey.keycode & 0xff]; } else - code = event2keycode((XKeyEvent *)&event); + code = event2keycode(event.xkey); if (code != -1) { if (!emul_suspended) { if (code == 0x39) { // Caps Lock pressed @@ -1608,10 +1739,8 @@ static void handle_events(void) if (code == 0x36) ctrl_down = true; } else { -#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) if (code == 0x31) - resume_emul(); // Space wakes us up -#endif + drv->resume(); // Space wakes us up } } break; @@ -1619,10 +1748,10 @@ static void handle_events(void) case KeyRelease: { int code; if (use_keycodes) { - event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys - code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff]; + event2keycode(event.xkey); // This is called to process the hotkeys + code = keycode_table[event.xkey.keycode & 0xff]; } else - code = event2keycode((XKeyEvent *)&event); + code = event2keycode(event.xkey); if (code != -1 && code != 0x39) { // Don't propagate Caps Lock releases ADBKeyUp(code); if (code == 0x36) @@ -1636,14 +1765,10 @@ static void handle_events(void) if (display_type == DISPLAY_WINDOW) { #ifdef ENABLE_VOSF if (use_vosf) { // VOSF refresh -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&Screen_draw_lock); -#endif + LOCK_VOSF; PFLAG_SET_ALL; -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&Screen_draw_lock); -#endif - memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); + UNLOCK_VOSF; + memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); } else #endif @@ -1654,12 +1779,16 @@ static void handle_events(void) updt_box[x1][y1] = true; nr_boxes = 16 * 16; } else // Static refresh - memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); + memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); } break; - case FocusIn: - case FocusOut: + // Window "close" widget clicked + case ClientMessage: + if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) { + ADBKeyDown(0x7f); // Power key + ADBKeyUp(0x7f); + } break; } } @@ -1671,15 +1800,15 @@ static void handle_events(void) */ // Dynamic display update (variable frame rate for each box) -static void update_display_dynamic(int ticker) +static void update_display_dynamic(int ticker, driver_window *drv) { int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi; int xil = 0; int rxm = 0, rxmo = 0; - int bytes_per_row = VideoMonitor.bytes_per_row; - int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x; - int rx = VideoMonitor.bytes_per_row / 16; - int ry = VideoMonitor.y / 16; + int bytes_per_row = VideoMonitor.mode.bytes_per_row; + int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x; + int rx = VideoMonitor.mode.bytes_per_row / 16; + int ry = VideoMonitor.mode.y / 16; int max_box; y2s = sm_uptd[ticker % 8]; @@ -1733,16 +1862,16 @@ static void update_display_dynamic(int t i = (yi * bytes_per_row) + xi; for (y2=0; y2 < yil; y2++, i += bytes_per_row) memcpy(&the_buffer_copy[i], &the_buffer[i], xil); - if (depth == 1) { - if (have_shm) - XShmPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0); + if (VideoMonitor.mode.depth == VDEPTH_1BIT) { + if (drv->have_shm) + XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0); else - XPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil); + XPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil); } else { - if (have_shm) - XShmPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0); + if (drv->have_shm) + XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0); else - XPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil); + XPutImage(x_display, drv->w, drv->gc, drv->img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil); } xil = 0; } @@ -1761,24 +1890,24 @@ static void update_display_dynamic(int t } // Static display update (fixed frame rate, but incremental) -static void update_display_static(void) +static void update_display_static(driver_window *drv) { // Incremental update code int wide = 0, high = 0, x1, x2, y1, y2, i, j; - int bytes_per_row = VideoMonitor.bytes_per_row; - int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x; + int bytes_per_row = VideoMonitor.mode.bytes_per_row; + int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x; uint8 *p, *p2; // Check for first line from top and first line from bottom that have changed y1 = 0; - for (j=0; j=y1; j--) { + for (j=VideoMonitor.mode.y-1; j>=y1; j--) { if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) { y2 = j; break; @@ -1788,8 +1917,8 @@ static void update_display_static(void) // Check for first column from left and first column from right that have changed if (high) { - if (depth == 1) { - x1 = VideoMonitor.x - 1; + if (VideoMonitor.mode.depth == VDEPTH_1BIT) { + x1 = VideoMonitor.mode.x - 1; for (j=y1; j<=y2; j++) { p = &the_buffer[j * bytes_per_row]; p2 = &the_buffer_copy[j * bytes_per_row]; @@ -1807,7 +1936,7 @@ static void update_display_static(void) p2 = &the_buffer_copy[j * bytes_per_row]; p += bytes_per_row; p2 += bytes_per_row; - for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) { + for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) { p--; p2--; if (*p != *p2) { x2 = (i << 3) + 7; @@ -1826,7 +1955,7 @@ static void update_display_static(void) } } else { - x1 = VideoMonitor.x; + x1 = VideoMonitor.mode.x; for (j=y1; j<=y2; j++) { p = &the_buffer[j * bytes_per_row]; p2 = &the_buffer_copy[j * bytes_per_row]; @@ -1844,7 +1973,7 @@ static void update_display_static(void) p2 = &the_buffer_copy[j * bytes_per_row]; p += bytes_per_row; p2 += bytes_per_row; - for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) { + for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) { p--; p2--; if (*p != *p2) { @@ -1867,10 +1996,10 @@ static void update_display_static(void) // Refresh display if (high && wide) { - if (have_shm) - XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0); + if (drv->have_shm) + XShmPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high, 0); else - XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high); + XPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high); } } @@ -1879,64 +2008,32 @@ static void update_display_static(void) * Screen refresh functions */ -// The specialisations hereunder are meant to enable VOSF with DGA in direct -// addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could -// not get mapped correctly with respect to the predetermined host frame -// buffer base address. -// -// Hmm, in other words, when in direct addressing mode and DGA is requested, -// we first try to "triple allocate" the address spaces according to the real -// host frame buffer address. Then, if it fails, we will use a temporary -// frame buffer thus making the real host frame buffer updated when pages -// of the temp frame buffer are altered. -// -// As a side effect, a little speed gain in screen updates could be noticed -// for other modes than DGA. -// -// The following two functions below are inline so that a clever compiler -// could specialise the code according to the current screen depth and -// display type. A more clever compiler would the job by itself though... -// (update_display_vosf is inlined as well) +// We suggest the compiler to inline the next two functions so that it +// may specialise the code according to the current screen depth and +// display type. A clever compiler would do that job by itself though... + +// NOTE: update_display_vosf is inlined too static inline void possibly_quit_dga_mode() { -#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) // Quit DGA mode if requested if (quit_full_screen) { quit_full_screen = false; -#ifdef ENABLE_XF86_DGA - XF86DGADirectVideo(x_display, screen, 0); -#endif - XUngrabPointer(x_display, CurrentTime); - XUngrabKeyboard(x_display, CurrentTime); - XUnmapWindow(x_display, the_win); - XSync(x_display, false); + delete drv; + drv = NULL; } -#endif } -static inline void handle_palette_changes(int depth, int display_type) +static inline void handle_palette_changes(void) { -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&palette_lock); -#endif + LOCK_PALETTE; + if (palette_changed) { palette_changed = false; - if (depth == 8) { - XStoreColors(x_display, cmap[0], palette, 256); - XStoreColors(x_display, cmap[1], palette, 256); - -#ifdef ENABLE_XF86_DGA - if (display_type == DISPLAY_DGA) { - current_dga_cmap ^= 1; - XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); - } -#endif - } + drv->update_palette(); } -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&palette_lock); -#endif + + UNLOCK_PALETTE; } static void video_refresh_dga(void) @@ -1948,7 +2045,7 @@ static void video_refresh_dga(void) handle_events(); // Handle palette changes - handle_palette_changes(depth, DISPLAY_DGA); + handle_palette_changes(); } #ifdef ENABLE_VOSF @@ -1962,19 +2059,17 @@ static void video_refresh_dga_vosf(void) handle_events(); // Handle palette changes - handle_palette_changes(depth, DISPLAY_DGA); + handle_palette_changes(); // Update display (VOSF variant) static int tick_counter = 0; if (++tick_counter >= frame_skip) { tick_counter = 0; -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&Screen_draw_lock); -#endif - update_display_dga_vosf(); -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&Screen_draw_lock); -#endif + if (mainBuffer.dirty) { + LOCK_VOSF; + update_display_dga_vosf(); + UNLOCK_VOSF; + } } } #endif @@ -1988,19 +2083,18 @@ static void video_refresh_window_vosf(vo handle_events(); // Handle palette changes - handle_palette_changes(depth, DISPLAY_WINDOW); + handle_palette_changes(); // Update display (VOSF variant) static int tick_counter = 0; if (++tick_counter >= frame_skip) { tick_counter = 0; -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&Screen_draw_lock); -#endif - update_display_window_vosf(); -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&Screen_draw_lock); -#endif + if (mainBuffer.dirty) { + LOCK_VOSF; + update_display_window_vosf(static_cast(drv)); + UNLOCK_VOSF; + XSync(x_display, false); // Let the server catch up + } } } #endif // def ENABLE_VOSF @@ -2011,13 +2105,13 @@ static void video_refresh_window_static( handle_events(); // Handle_palette changes - handle_palette_changes(depth, DISPLAY_WINDOW); + handle_palette_changes(); // Update display (static variant) static int tick_counter = 0; if (++tick_counter >= frame_skip) { tick_counter = 0; - update_display_static(); + update_display_static(static_cast(drv)); } } @@ -2027,12 +2121,12 @@ static void video_refresh_window_dynamic handle_events(); // Handle_palette changes - handle_palette_changes(depth, DISPLAY_WINDOW); + handle_palette_changes(); // Update display (dynamic variant) static int tick_counter = 0; tick_counter++; - update_display_dynamic(tick_counter); + update_display_dynamic(tick_counter, static_cast(drv)); } @@ -2040,7 +2134,7 @@ static void video_refresh_window_dynamic * Thread for screen refresh, input handling etc. */ -void VideoRefreshInit(void) +static void VideoRefreshInit(void) { // TODO: set up specialised 8bpp VideoRefresh handlers ? if (display_type == DISPLAY_DGA) { @@ -2066,67 +2160,11 @@ void VideoRefreshInit(void) void VideoRefresh(void) { - // TODO: make main_unix/VideoRefresh call directly video_refresh() ? - video_refresh(); -} - -#if 0 -void VideoRefresh(void) -{ -#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) - // Quit DGA mode if requested - if (quit_full_screen) { - quit_full_screen = false; - if (display_type == DISPLAY_DGA) { -#ifdef ENABLE_XF86_DGA - XF86DGADirectVideo(x_display, screen, 0); -#endif - XUngrabPointer(x_display, CurrentTime); - XUngrabKeyboard(x_display, CurrentTime); - XUnmapWindow(x_display, the_win); - XSync(x_display, false); - } - } -#endif - - // Handle X events - handle_events(); - - // Handle palette changes -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&palette_lock); -#endif - if (palette_changed) { - palette_changed = false; - if (depth == 8) { - XStoreColors(x_display, cmap[0], palette, 256); - XStoreColors(x_display, cmap[1], palette, 256); - -#ifdef ENABLE_XF86_DGA - if (display_type == DISPLAY_DGA) { - current_dga_cmap ^= 1; - XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); - } -#endif - } - } -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&palette_lock); -#endif - - // In window mode, update display - static int tick_counter = 0; - if (display_type == DISPLAY_WINDOW) { - tick_counter++; - if (frame_skip == 0) - update_display_dynamic(tick_counter); - else if (tick_counter >= frame_skip) { - tick_counter = 0; - update_display_static(); - } - } + // We need to check redraw_thread_active to inhibit refreshed during + // mode changes on non-threaded platforms + if (redraw_thread_active) + video_refresh(); } -#endif #ifdef HAVE_PTHREADS static void *redraw_func(void *arg) @@ -2135,7 +2173,6 @@ static void *redraw_func(void *arg) int64 ticks = 0; uint64 next = GetTicks_usec(); while (!redraw_thread_cancel) { -// VideoRefresh(); video_refresh(); next += 16667; int64 delay = next - GetTicks_usec(); @@ -2146,7 +2183,7 @@ static void *redraw_func(void *arg) ticks++; } uint64 end = GetTicks_usec(); - printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks); + // printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start)); return NULL; } #endif