--- BasiliskII/src/Unix/video_x.cpp 2001/07/11 19:26:14 1.60 +++ BasiliskII/src/Unix/video_x.cpp 2006/01/03 22:03:27 1.82 @@ -1,7 +1,7 @@ /* * video_x.cpp - Video/graphics emulation, X11 specific stuff * - * Basilisk II (C) 1997-2001 Christian Bauer + * Basilisk II (C) 1997-2005 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 @@ -39,10 +39,6 @@ #include -#ifndef NO_STD_NAMESPACE -using std::sort; -#endif - #ifdef HAVE_PTHREADS # include #endif @@ -66,11 +62,15 @@ using std::sort; #include "prefs.h" #include "user_strings.h" #include "video.h" +#include "video_blit.h" #define DEBUG 0 #include "debug.h" +// Supported video modes +static vector VideoModes; + // Display types enum { DISPLAY_WINDOW, // X11 window, using MIT SHM extensions if possible @@ -97,7 +97,9 @@ static uint32 the_buffer_size; // S static bool redraw_thread_active = false; // Flag: Redraw thread installed #ifdef HAVE_PTHREADS +static pthread_attr_t redraw_thread_attr; // Redraw thread attributes static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread +static volatile bool redraw_thread_cancel_ack; // Flag: Acknowledge for redraw thread cancellation static pthread_t redraw_thread; // Redraw thread #endif @@ -122,6 +124,8 @@ static bool use_keycodes = false; // static int keycode_table[256]; // X keycode -> Mac keycode translation table // X11 variables +char *x_display_name = NULL; // X11 display name +Display *x_display = NULL; // X11 display handle static int screen; // Screen number static Window rootwin; // Root window and our window static int num_depths = 0; // Number of available X depths @@ -131,16 +135,18 @@ static unsigned long black_pixel, white_ static int eventmask; static int xdepth; // Depth of X screen +static VisualFormat visualFormat; static XVisualInfo visualInfo; static Visual *vis; static int color_class; +static bool x_native_byte_order; // XImage has native byte order? static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes static Colormap cmap[2] = {0, 0}; // Colormaps for indexed modes (DGA needs two of them) -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 +static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table +static bool x_palette_changed = false; // Flag: Palette changed, redraw thread must set new colors #ifdef ENABLE_FBDEV_DGA static int fbdev_fd = -1; @@ -153,9 +159,9 @@ static int num_x_video_modes; // 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) +static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER; +#define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock) +#define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock) #else #define LOCK_PALETTE #define UNLOCK_PALETTE @@ -188,19 +194,85 @@ static void *redraw_func(void *arg); // From main_unix.cpp extern char *x_display_name; extern Display *x_display; +extern void *vm_acquire_mac(size_t size); // From sys_unix.cpp extern void SysMountFirstFloppy(void); +// From clip_unix.cpp +extern void ClipboardSelectionClear(XSelectionClearEvent *); +extern void ClipboardSelectionRequest(XSelectionRequestEvent *); + + +/* + * monitor_desc subclass for X11 display + */ + +class X11_monitor_desc : public monitor_desc { +public: + X11_monitor_desc(const vector &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {} + ~X11_monitor_desc() {} + + virtual void switch_to_current_mode(void); + virtual void set_palette(uint8 *pal, int num); + + bool video_open(void); + void video_close(void); +}; + /* * Utility functions */ +// Map video_mode depth ID to numerical depth value +static inline int depth_of_video_mode(video_mode const & mode) +{ + int depth = -1; + switch (mode.depth) { + case VDEPTH_1BIT: + depth = 1; + break; + case VDEPTH_2BIT: + depth = 2; + break; + case VDEPTH_4BIT: + depth = 4; + break; + case VDEPTH_8BIT: + depth = 8; + break; + case VDEPTH_16BIT: + depth = 16; + break; + case VDEPTH_32BIT: + depth = 32; + break; + default: + abort(); + } + return depth; +} + // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals) -static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue) +static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue, bool fix_byte_order = false) { - return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift); + uint32 val = ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift); + if (fix_byte_order && !x_native_byte_order) { + // We have to fix byte order in the ExpandMap[] + // NOTE: this is only an optimization since Screen_blitter_init() + // could be arranged to choose an NBO or OBO (with + // byteswapping) Blit_Expand_X_To_Y() function + switch (visualFormat.depth) { + case 15: case 16: + val = do_byteswap_16(val); + break; + case 24: case 32: + val = do_byteswap_32(val); + break; + } + } + return val; } // Do we have a visual for handling the specified Mac depth? If so, set the @@ -209,12 +281,23 @@ static bool find_visual_for_depth(video_ { D(bug("have_visual_for_depth(%d)\n", 1 << depth)); + // 1-bit works always and uses default visual + if (depth == VDEPTH_1BIT) { + vis = DefaultVisual(x_display, screen); + visualInfo.visualid = XVisualIDFromVisual(vis); + int num = 0; + XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num); + visualInfo = vi[0]; + XFree(vi); + xdepth = visualInfo.depth; + color_class = visualInfo.c_class; + D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth)); + return true; + } + // Calculate minimum and maximum supported X depth int min_depth = 1, max_depth = 32; switch (depth) { - case VDEPTH_1BIT: // 1-bit works always and uses default visual - min_depth = max_depth = DefaultDepth(x_display, screen); - break; #ifdef ENABLE_VOSF case VDEPTH_2BIT: case VDEPTH_4BIT: // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit @@ -320,7 +403,7 @@ static void add_window_modes(video_depth } // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac) -static void set_mac_frame_buffer(video_depth depth, bool native_byte_order) +static void set_mac_frame_buffer(X11_monitor_desc &monitor, video_depth depth, bool native_byte_order) { #if !REAL_ADDRESSING && !DIRECT_ADDRESSING int layout = FLAYOUT_DIRECT; @@ -332,16 +415,17 @@ static void set_mac_frame_buffer(video_d MacFrameLayout = layout; else MacFrameLayout = FLAYOUT_DIRECT; - VideoMonitor.mac_frame_base = MacFrameBaseMac; + monitor.set_mac_frame_base(MacFrameBaseMac); // Set variables used by UAE memory banking + const video_mode &mode = monitor.get_current_mode(); MacFrameBaseHost = the_buffer; - MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y; + MacFrameSize = mode.bytes_per_row * mode.y; InitFrameBufferMapping(); #else - VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer); + monitor.set_mac_frame_base(Host2MacAddr(the_buffer)); #endif - D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base)); + D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base())); } // Set window name and class @@ -419,7 +503,7 @@ static int error_handler(Display *d, XEr class driver_base { public: - driver_base(); + driver_base(X11_monitor_desc &m); virtual ~driver_base(); virtual void update_palette(void); @@ -435,6 +519,9 @@ public: virtual void ungrab_mouse(void) {} public: + X11_monitor_desc &monitor; // Associated video monitor + const video_mode &mode; // Video mode handled by the driver + bool init_ok; // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer) Window w; // The window we draw into @@ -452,7 +539,7 @@ class driver_window : public driver_base friend void update_display_static(driver_window *drv); public: - driver_window(const video_mode &mode); + driver_window(X11_monitor_desc &monitor); ~driver_window(); void toggle_mouse_grab(void); @@ -471,14 +558,44 @@ private: int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode) }; +class driver_dga; +static void update_display_dga_vosf(driver_dga *drv); + +class driver_dga : public driver_base { + friend void update_display_dga_vosf(driver_dga *drv); + +public: + driver_dga(X11_monitor_desc &monitor); + ~driver_dga(); + + void suspend(void); + void resume(void); + +protected: + struct FakeXImage { + int width, height; // size of image + int depth; // depth of image + int bytes_per_line; // accelerator to next line + + FakeXImage(int w, int h, int d) + : width(w), height(h), depth(d) + { bytes_per_line = TrivialBytesPerRow(width, DepthModeForPixelDepth(depth)); } + }; + FakeXImage *img; + +private: + Window suspend_win; // "Suspend" information window + void *fb_save; // Saved frame buffer for suspend/resume +}; + 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) +driver_base::driver_base(X11_monitor_desc &m) + : monitor(m), mode(m.get_current_mode()), init_ok(false), w(0) { the_buffer = NULL; the_buffer_copy = NULL; @@ -512,15 +629,19 @@ driver_base::~driver_base() } #ifdef ENABLE_VOSF else { + // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will + if (the_buffer != VM_MAP_FAILED) { + D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size)); + vm_release(the_buffer, the_buffer_size); + the_buffer = NULL; + } if (the_host_buffer) { + D(bug(" freeing the_host_buffer at %p\n", the_host_buffer)); free(the_host_buffer); the_host_buffer = NULL; } - if (the_buffer) { - free(the_buffer); - the_buffer = NULL; - } if (the_buffer_copy) { + D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy)); free(the_buffer_copy); the_buffer_copy = NULL; } @@ -533,10 +654,10 @@ void driver_base::update_palette(void) { if (color_class == PseudoColor || color_class == DirectColor) { int num = vis->map_entries; - if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor) + if (!IsDirectMode(monitor.get_current_mode()) && color_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); + XStoreColors(x_display, cmap[0], x_palette, num); + XStoreColors(x_display, cmap[1], x_palette, num); } XSync(x_display, false); } @@ -559,8 +680,8 @@ void driver_base::restore_mouse_accel(vo */ // Open display -driver_window::driver_window(const video_mode &mode) - : gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false) +driver_window::driver_window(X11_monitor_desc &m) + : driver_base(m), gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false) { int width = mode.x, height = mode.y; int aligned_width = (width + 15) & ~15; @@ -569,7 +690,7 @@ driver_window::driver_window(const video // Set absolute mouse mode ADBSetRelMouseMode(mouse_grabbed); - // Create window (setting backround_pixel, border_pixel and colormap is + // Create window (setting background_pixel, border_pixel and colormap is // mandatory when using a non-default visual; in 1-bit mode we use the // default visual, so we can also use the default colormap) XSetWindowAttributes wattr; @@ -579,6 +700,7 @@ driver_window::driver_window(const video wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]); w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &wattr); + D(bug(" window created\n")); // Set window name/class set_window_name(w, STR_WINDOW_TITLE); @@ -602,10 +724,12 @@ driver_window::driver_window(const video XFree(hints); } } + D(bug(" window attributes set\n")); // Show window XMapWindow(x_display, w); wait_mapped(w); + D(bug(" window mapped\n")); // 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 @@ -616,6 +740,7 @@ driver_window::driver_window(const video // Create SHM image ("height + 2" for safety) img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height); + D(bug(" shm image created\n")); 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; @@ -636,6 +761,7 @@ driver_window::driver_window(const video have_shm = true; shmctl(shminfo.shmid, IPC_RMID, 0); } + D(bug(" shm image attached\n")); } // Create normal X image if SHM doesn't work ("height + 2" for safety) @@ -643,6 +769,7 @@ driver_window::driver_window(const video 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, 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); + D(bug(" X image created\n")); } if (need_msb_image) { @@ -655,8 +782,8 @@ driver_window::driver_window(const video // Allocate memory for frame buffer (SIZE is extended to page-boundary) the_host_buffer = the_buffer_copy; 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); + the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size); + the_buffer_copy = (uint8 *)malloc(the_buffer_size); D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer)); #else // Allocate memory for frame buffer @@ -676,19 +803,12 @@ driver_window::driver_window(const video XDefineCursor(x_display, w, mac_cursor); // Init blitting routines - bool native_byte_order; -#ifdef WORDS_BIGENDIAN - native_byte_order = (XImageByteOrder(x_display) == MSBFirst); -#else - native_byte_order = (XImageByteOrder(x_display) == LSBFirst); -#endif #ifdef ENABLE_VOSF - Screen_blitter_init(&visualInfo, native_byte_order, mode.depth); + Screen_blitter_init(visualFormat, x_native_byte_order, depth_of_video_mode(mode)); #endif - // Set VideoMonitor - VideoMonitor.mode = mode; - set_mac_frame_buffer(mode.depth, native_byte_order); + // Set frame buffer base + set_mac_frame_buffer(monitor, mode.depth, x_native_byte_order); // Everything went well init_ok = true; @@ -705,21 +825,11 @@ driver_window::~driver_window() the_buffer_copy = NULL; // don't free() in driver_base dtor #endif } -#ifdef ENABLE_VOSF - if (use_vosf) { - // don't free() memory mapped buffers in driver_base dtor - if (the_buffer != VM_MAP_FAILED) { - vm_release(the_buffer, the_buffer_size); - the_buffer = NULL; - } - if (the_buffer_copy != VM_MAP_FAILED) { - vm_release(the_buffer_copy, the_buffer_size); - the_buffer_copy = NULL; - } - } -#endif - if (img) + if (img) { + if (!have_shm) + img->data = NULL; XDestroyImage(img); + } if (have_shm) { shmdt(shminfo.shmaddr); shmctl(shminfo.shmid, IPC_RMID, 0); @@ -778,7 +888,7 @@ void driver_window::mouse_moved(int x, i // Warped mouse motion (this code is taken from SDL) // Post first mouse event - int width = VideoMonitor.mode.x, height = VideoMonitor.mode.y; + int width = monitor.get_current_mode().x, height = monitor.get_current_mode().y; int delta_x = x - mouse_last_x, delta_y = y - mouse_last_y; mouse_last_x = x; mouse_last_y = y; ADBMouseMoved(delta_x, delta_y); @@ -813,21 +923,8 @@ void driver_window::mouse_moved(int x, i * 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(X11_monitor_desc &m) + : driver_base(m), suspend_win(0), fb_save(NULL), img(NULL) { } @@ -835,6 +932,9 @@ driver_dga::~driver_dga() { XUngrabPointer(x_display, CurrentTime); XUngrabKeyboard(x_display, CurrentTime); + + if (img) + delete img; } // Suspend emulation @@ -848,9 +948,9 @@ void driver_dga::suspend(void) LOCK_FRAME_BUFFER; // Save frame buffer - fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row); + fb_save = malloc(mode.y * mode.bytes_per_row); if (fb_save) - memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row); + memcpy(fb_save, the_buffer, mode.y * mode.bytes_per_row); // Close full screen display #ifdef ENABLE_XF86_DGA @@ -903,7 +1003,7 @@ void driver_dga::resume(void) LOCK_VOSF; PFLAG_SET_ALL; UNLOCK_VOSF; - memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); + memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y); } #endif @@ -913,7 +1013,7 @@ void driver_dga::resume(void) // 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); + memcpy(the_buffer, fb_save, mode.y * mode.bytes_per_row); free(fb_save); fb_save = NULL; } @@ -935,12 +1035,12 @@ const char FBDEVICE_FILE_NAME[] = "/dev/ class driver_fbdev : public driver_dga { public: - driver_fbdev(const video_mode &mode); + driver_fbdev(X11_monitor_desc &monitor); ~driver_fbdev(); }; // Open display -driver_fbdev::driver_fbdev(const video_mode &mode) +driver_fbdev::driver_fbdev(X11_monitor_desc &m) : driver_dga(m) { int width = mode.x, height = mode.y; @@ -988,7 +1088,7 @@ driver_fbdev::driver_fbdev(const video_m if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0')) continue; - if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3) + if ((sscanf(line, "%19s %d %x", fb_name, &fb_depth, &fb_offset) == 3) && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) { device_found = true; break; @@ -1055,25 +1155,27 @@ driver_fbdev::driver_fbdev(const video_m #if REAL_ADDRESSING || DIRECT_ADDRESSING // 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); + use_vosf = Screen_blitter_init(visualFormat, true, mode.depth); if (use_vosf) { // 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); + the_buffer_copy = (uint8 *)malloc(the_buffer_size); + the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size); + + // Fake image for DGA/VOSF mode to know about display bounds + img = new FakeXImage(width, height, depth_of_video_mode(mode)); } #else use_vosf = false; #endif #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); + // Set frame buffer base + const_cast(&mode)->bytes_per_row = bytes_per_row; + const_cast(&mode)->depth = DepthModeForPixelDepth(fb_depth); + set_mac_frame_buffer(monitor, mode.depth, true); // Everything went well init_ok = true; @@ -1096,14 +1198,6 @@ driver_fbdev::~driver_fbdev() munmap(the_host_buffer, the_buffer_size); the_host_buffer = NULL; } - if (the_buffer_copy != VM_MAP_FAILED) { - vm_release(the_buffer_copy, the_buffer_size); - the_buffer_copy = NULL; - } - if (the_buffer != VM_MAP_FAILED) { - vm_release(the_buffer, the_buffer_size); - the_buffer = NULL; - } } #endif } @@ -1117,7 +1211,7 @@ driver_fbdev::~driver_fbdev() class driver_xf86dga : public driver_dga { public: - driver_xf86dga(const video_mode &mode); + driver_xf86dga(X11_monitor_desc &monitor); ~driver_xf86dga(); void update_palette(void); @@ -1128,8 +1222,8 @@ private: }; // Open display -driver_xf86dga::driver_xf86dga(const video_mode &mode) - : current_dga_cmap(0) +driver_xf86dga::driver_xf86dga(X11_monitor_desc &m) + : driver_dga(m), current_dga_cmap(0) { int width = mode.x, height = mode.y; @@ -1156,9 +1250,11 @@ driver_xf86dga::driver_xf86dga(const vid XSetWindowAttributes wattr; wattr.event_mask = eventmask = dga_eventmask; wattr.override_redirect = True; + wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]); w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, - InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr); + InputOutput, vis, CWEventMask | CWOverrideRedirect | + (color_class == DirectColor ? CWColormap : 0), &wattr); // Set window name/class set_window_name(w, STR_WINDOW_TITLE); @@ -1192,28 +1288,30 @@ driver_xf86dga::driver_xf86dga(const vid // Init blitting routines int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth); -#if VIDEO_VOSF +#if ENABLE_VOSF #if REAL_ADDRESSING || DIRECT_ADDRESSING // 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); + use_vosf = Screen_blitter_init(visualFormat, x_native_byte_order, depth_of_video_mode(mode)); if (use_vosf) { // 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); + the_buffer_copy = (uint8 *)malloc(the_buffer_size); + the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size); + + // Fake image for DGA/VOSF mode to know about display bounds + img = new FakeXImage((v_width + 7) & ~7, height, depth_of_video_mode(mode)); } #else use_vosf = false; #endif #endif - // Set VideoMonitor + // Set frame buffer base const_cast(&mode)->bytes_per_row = bytes_per_row; - VideoMonitor.mode = mode; - set_mac_frame_buffer(mode.depth, true); + set_mac_frame_buffer(monitor, mode.depth, true); // Everything went well init_ok = true; @@ -1231,15 +1329,6 @@ driver_xf86dga::~driver_xf86dga() else { // don't free() the screen buffer in driver_base dtor the_host_buffer = NULL; - - if (the_buffer_copy != VM_MAP_FAILED) { - vm_release(the_buffer_copy, the_buffer_size); - the_buffer_copy = NULL; - } - if (the_buffer != VM_MAP_FAILED) { - vm_release(the_buffer, the_buffer_size); - the_buffer = NULL; - } } #endif #ifdef ENABLE_XF86_VIDMODE @@ -1253,7 +1342,7 @@ void driver_xf86dga::update_palette(void { driver_dga::update_palette(); current_dga_cmap ^= 1; - if (!IsDirectMode(VideoMonitor.mode) && cmap[current_dga_cmap]) + if (!IsDirectMode(monitor.get_current_mode()) && cmap[current_dga_cmap]) XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); } @@ -1261,7 +1350,7 @@ void driver_xf86dga::update_palette(void void driver_xf86dga::resume(void) { driver_dga::resume(); - if (!IsDirectMode(VideoMonitor.mode)) + if (!IsDirectMode(monitor.get_current_mode())) XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); } #endif @@ -1295,6 +1384,10 @@ static void keycode_init(void) // Search for server vendor string, then read keycodes const char *vendor = ServerVendor(x_display); + // Force use of MacX mappings on MacOS X with Apple's X server + int dummy; + if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy)) + vendor = "MacX"; bool vendor_found = false; char line[256]; while (fgets(line, 255, f)) { @@ -1336,15 +1429,32 @@ static void keycode_init(void) } } -// Open display for specified mode -static bool video_open(const video_mode &mode) +// Open display for current mode +bool X11_monitor_desc::video_open(void) { + D(bug("video_open()\n")); + const video_mode &mode = get_current_mode(); + // Find best available X visual if (!find_visual_for_depth(mode.depth)) { ErrorAlert(STR_NO_XVISUAL_ERR); return false; } + // Determine the byte order of an XImage content +#ifdef WORDS_BIGENDIAN + x_native_byte_order = (XImageByteOrder(x_display) == MSBFirst); +#else + x_native_byte_order = (XImageByteOrder(x_display) == LSBFirst); +#endif + + // Build up visualFormat structure + visualFormat.fullscreen = (display_type == DISPLAY_DGA); + visualFormat.depth = visualInfo.depth; + visualFormat.Rmask = visualInfo.red_mask; + visualFormat.Gmask = visualInfo.green_mask; + visualFormat.Bmask = visualInfo.blue_mask; + // Create color maps if (color_class == PseudoColor || color_class == DirectColor) { cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll); @@ -1378,13 +1488,13 @@ static bool video_open(const video_mode int num = vis->map_entries; for (int i=0; imap_entries : 256); for (int i=0; i16/32 expand map if (!IsDirectMode(mode) && xdepth > 8) for (int i=0; i<256; i++) - ExpandMap[i] = map_rgb(i, i, i); + ExpandMap[i] = map_rgb(i, i, i, true); #endif // Create display driver object of requested type switch (display_type) { case DISPLAY_WINDOW: - drv = new driver_window(mode); + drv = new driver_window(*this); break; #ifdef ENABLE_FBDEV_DGA case DISPLAY_DGA: - drv = new driver_fbdev(mode); + drv = new driver_fbdev(*this); break; #endif #ifdef ENABLE_XF86_DGA case DISPLAY_DGA: - drv = new driver_xf86dga(mode); + drv = new driver_xf86dga(*this); break; #endif } @@ -1435,7 +1545,7 @@ static bool video_open(const video_mode #ifdef ENABLE_VOSF if (use_vosf) { // Initialize the VOSF system - if (!video_vosf_init()) { + if (!video_vosf_init(*this)) { ErrorAlert(STR_VOSF_INIT_ERR); return false; } @@ -1450,9 +1560,10 @@ static bool video_open(const video_mode LOCK_FRAME_BUFFER; // Start redraw/input thread -#ifdef HAVE_PTHREADS +#ifdef USE_PTHREADS_SERVICES redraw_thread_cancel = false; - redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0); + Set_pthread_attr(&redraw_thread_attr, 0); + redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0); if (!redraw_thread_active) { printf("FATAL: cannot create redraw thread\n"); return false; @@ -1496,7 +1607,7 @@ bool VideoInit(bool classic) ErrorAlert(STR_UNSUPP_DEPTH_ERR); return false; } - sort(avail_depths, avail_depths + num_depths); + std::sort(avail_depths, avail_depths + num_depths); #ifdef ENABLE_FBDEV_DGA // Frame buffer name @@ -1599,34 +1710,39 @@ bool VideoInit(bool classic) ErrorAlert(STR_NO_XVISUAL_ERR); return false; } - video_init_depth_list(); + + // Find requested default mode with specified dimensions + uint32 default_id; + 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) { + default_id = i->resolution_id; + break; + } + } + if (i == end) { // not found, use first available mode + default_depth = VideoModes[0].depth; + default_id = VideoModes[0].resolution_id; + } #if DEBUG D(bug("Available video modes:\n")); - vector::const_iterator i = VideoModes.begin(), end = VideoModes.end(); - while (i != end) { + for (i = VideoModes.begin(); i != end; ++i) { int bits = 1 << i->depth; if (bits == 16) bits = 15; else if (bits == 32) bits = 24; D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits)); - ++i; } #endif - // 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]); - } + // Create X11_monitor_desc for this (the only) display + X11_monitor_desc *monitor = new X11_monitor_desc(VideoModes, default_depth, default_id); + VideoMonitors.push_back(monitor); + + // Open display + return monitor->video_open(); } @@ -1635,16 +1751,17 @@ bool VideoInit(bool classic) */ // Close display -static void video_close(void) +void X11_monitor_desc::video_close(void) { + D(bug("video_close()\n")); + // Stop redraw thread -#ifdef HAVE_PTHREADS +#ifdef USE_PTHREADS_SERVICES if (redraw_thread_active) { redraw_thread_cancel = true; -#ifdef HAVE_PTHREAD_CANCEL - pthread_cancel(redraw_thread); -#endif + redraw_thread_cancel_ack = false; pthread_join(redraw_thread, NULL); + while (!redraw_thread_cancel_ack) ; } #endif redraw_thread_active = false; @@ -1652,6 +1769,7 @@ static void video_close(void) // Unlock frame buffer UNLOCK_FRAME_BUFFER; XSync(x_display, false); + D(bug(" frame buffer unlocked\n")); #ifdef ENABLE_VOSF if (use_vosf) { @@ -1677,8 +1795,10 @@ static void video_close(void) void VideoExit(void) { - // Close display - video_close(); + // Close displays + vector::iterator i, end = VideoMonitors.end(); + for (i = VideoMonitors.begin(); i != end; ++i) + dynamic_cast(*i)->video_close(); #ifdef ENABLE_XF86_VIDMODE // Free video mode list @@ -1736,19 +1856,21 @@ void VideoInterrupt(void) * Set palette */ -void video_set_palette(uint8 *pal, int num_in) +void X11_monitor_desc::set_palette(uint8 *pal, int num_in) { + const video_mode &mode = get_current_mode(); + LOCK_PALETTE; // Convert colors to XColor array int num_out = 256; bool stretch = false; - if (IsDirectMode(VideoMonitor.mode)) { + if (IsDirectMode(mode)) { // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries num_out = vis->map_entries; stretch = true; } - XColor *p = palette; + XColor *p = x_palette; for (int i=0; ired = pal[c*3 + 0] * 0x0101; @@ -1759,22 +1881,22 @@ void video_set_palette(uint8 *pal, int n #ifdef ENABLE_VOSF // Recalculate pixel color expansion map - if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) { + if (!IsDirectMode(mode) && xdepth > 8) { for (int i=0; i<256; i++) { int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier) - ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]); + ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2], true); } // 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); + memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y); } #endif // Tell redraw thread to change palette - palette_changed = true; + x_palette_changed = true; UNLOCK_PALETTE; } @@ -1784,11 +1906,11 @@ void video_set_palette(uint8 *pal, int n * Switch video mode */ -void video_switch_to_mode(const video_mode &mode) +void X11_monitor_desc::switch_to_current_mode(void) { // Close and reopen display video_close(); - video_open(mode); + video_open(); if (drv == NULL) { ErrorAlert(STR_OPEN_WINDOW_ERR); @@ -1967,10 +2089,28 @@ static int event2keycode(XKeyEvent &ev, static void handle_events(void) { - while (XPending(x_display)) { + for (;;) { XEvent event; - XNextEvent(x_display, &event); + XDisplayLock(); + + if (!XCheckMaskEvent(x_display, eventmask, &event)) { + // Handle clipboard events + if (XCheckTypedEvent(x_display, SelectionRequest, &event)) + ClipboardSelectionRequest(&event.xselectionrequest); + else if (XCheckTypedEvent(x_display, SelectionClear, &event)) + ClipboardSelectionClear(&event.xselectionclear); + // Window "close" widget clicked + else if (XCheckTypedEvent(x_display, ClientMessage, &event)) { + if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) { + ADBKeyDown(0x7f); // Power key + ADBKeyUp(0x7f); + } + } + XDisplayUnlock(); + break; + } + switch (event.type) { // Mouse button @@ -2058,12 +2198,13 @@ static void handle_events(void) // Hidden parts exposed, force complete refresh of window case Expose: if (display_type == DISPLAY_WINDOW) { + const video_mode &mode = VideoMonitors[0]->get_current_mode(); #ifdef ENABLE_VOSF if (use_vosf) { // VOSF refresh LOCK_VOSF; PFLAG_SET_ALL; UNLOCK_VOSF; - memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); + memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y); } else #endif @@ -2074,18 +2215,12 @@ static void handle_events(void) updt_box[x1][y1] = true; nr_boxes = 16 * 16; } else // Static refresh - memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y); - } - break; - - // 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); + memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y); } break; } + + XDisplayUnlock(); } } @@ -2100,10 +2235,11 @@ static void update_display_dynamic(int t 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.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; + const video_mode &mode = drv->monitor.get_current_mode(); + int bytes_per_row = mode.bytes_per_row; + int bytes_per_pixel = mode.bytes_per_row / mode.x; + int rx = mode.bytes_per_row / 16; + int ry = mode.y / 16; int max_box; y2s = sm_uptd[ticker % 8]; @@ -2129,6 +2265,7 @@ static void update_display_dynamic(int t } } + XDisplayLock(); if ((nr_boxes <= max_box) && (nr_boxes)) { for (y1=0; y1<16; y1++) { for (x1=0; x1<16; x1++) { @@ -2157,7 +2294,7 @@ 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 (VideoMonitor.mode.depth == VDEPTH_1BIT) { + if (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 @@ -2182,6 +2319,7 @@ static void update_display_dynamic(int t } nr_boxes = 0; } + XDisplayUnlock(); } // Static display update (fixed frame rate, but incremental) @@ -2189,20 +2327,21 @@ static void update_display_static(driver { // Incremental update code unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j; - int bytes_per_row = VideoMonitor.mode.bytes_per_row; - int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x; + const video_mode &mode = drv->monitor.get_current_mode(); + int bytes_per_row = mode.bytes_per_row; + int bytes_per_pixel = mode.bytes_per_row / 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=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; @@ -2212,8 +2351,8 @@ static void update_display_static(driver // Check for first column from left and first column from right that have changed if (high) { - if (VideoMonitor.mode.depth == VDEPTH_1BIT) { - x1 = VideoMonitor.mode.x - 1; + if (mode.depth == VDEPTH_1BIT) { + x1 = mode.x - 1; for (j=y1; j<=y2; j++) { p = &the_buffer[j * bytes_per_row]; p2 = &the_buffer_copy[j * bytes_per_row]; @@ -2231,7 +2370,7 @@ static void update_display_static(driver p2 = &the_buffer_copy[j * bytes_per_row]; p += bytes_per_row; p2 += bytes_per_row; - for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) { + for (i=(mode.x>>3); i>(x2>>3); i--) { p--; p2--; if (*p != *p2) { x2 = (i << 3) + 7; @@ -2250,7 +2389,7 @@ static void update_display_static(driver } } else { - x1 = VideoMonitor.mode.x; + x1 = mode.x; for (j=y1; j<=y2; j++) { p = &the_buffer[j * bytes_per_row]; p2 = &the_buffer_copy[j * bytes_per_row]; @@ -2268,7 +2407,7 @@ static void update_display_static(driver p2 = &the_buffer_copy[j * bytes_per_row]; p += bytes_per_row; p2 += bytes_per_row; - for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) { + for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) { p--; p2--; if (*p != *p2) { @@ -2290,12 +2429,14 @@ static void update_display_static(driver } // Refresh display + XDisplayLock(); if (high && wide) { if (drv->have_shm) XShmPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high, 0); else XPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high); } + XDisplayUnlock(); } @@ -2335,9 +2476,11 @@ static inline void handle_palette_change { LOCK_PALETTE; - if (palette_changed) { - palette_changed = false; + if (x_palette_changed) { + x_palette_changed = false; + XDisplayLock(); drv->update_palette(); + XDisplayUnlock(); } UNLOCK_PALETTE; @@ -2362,7 +2505,7 @@ static void video_refresh_dga_vosf(void) tick_counter = 0; if (mainBuffer.dirty) { LOCK_VOSF; - update_display_dga_vosf(); + update_display_dga_vosf(static_cast(drv)); UNLOCK_VOSF; } } @@ -2379,10 +2522,12 @@ static void video_refresh_window_vosf(vo if (++tick_counter >= frame_skip) { tick_counter = 0; if (mainBuffer.dirty) { + XDisplayLock(); LOCK_VOSF; update_display_window_vosf(static_cast(drv)); UNLOCK_VOSF; XSync(x_display, false); // Let the server catch up + XDisplayUnlock(); } } } @@ -2462,7 +2607,7 @@ void VideoRefresh(void) const int VIDEO_REFRESH_HZ = 60; const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ; -#ifdef HAVE_PTHREADS +#ifdef USE_PTHREADS_SERVICES static void *redraw_func(void *arg) { int fd = ConnectionNumber(x_display); @@ -2503,7 +2648,9 @@ static void *redraw_func(void *arg) } uint64 end = GetTicks_usec(); - D(bug("%Ld refreshes in %Ld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); + D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start))); + + redraw_thread_cancel_ack = true; return NULL; } #endif