--- SheepShaver/src/Unix/video_x.cpp 2004/12/18 18:34:56 1.34 +++ SheepShaver/src/Unix/video_x.cpp 2005/06/22 16:40:24 1.44 @@ -1,7 +1,7 @@ /* * video_x.cpp - Video/graphics emulation, X11 specific stuff * - * SheepShaver (C) 1997-2004 Marc Hellwig and Christian Bauer + * SheepShaver (C) 1997-2005 Marc Hellwig and 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 @@ -18,6 +18,15 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +/* + * NOTES: + * The Ctrl key works like a qualifier for special actions: + * Ctrl-Tab = suspend DGA mode + * Ctrl-Esc = emergency quit + * Ctrl-F1 = mount floppy + * Ctrl-F5 = grab mouse (in windowed mode) + */ + #include "sysdeps.h" #include @@ -31,6 +40,11 @@ #include +#ifdef ENABLE_FBDEV_DGA +# include +# include +#endif + #ifdef ENABLE_XF86_DGA # include #endif @@ -39,6 +53,10 @@ # include #endif +#ifdef ENABLE_FBDEV_DGA +# include +#endif + #include "main.h" #include "adb.h" #include "prefs.h" @@ -109,6 +127,7 @@ static int color_class; static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table +static int orig_accel_numer, orig_accel_denom, orig_threshold; // Original mouse acceleration static XColor black, white; static unsigned long black_pixel, white_pixel; @@ -131,8 +150,17 @@ static uint8 *the_buffer_copy = NULL; / static uint32 the_buffer_size; // Size of allocated the_buffer // Variables for DGA mode +static bool is_fbdev_dga_mode = false; // Flag: Use FBDev DGA mode? static int current_dga_cmap; +#ifdef ENABLE_FBDEV_DGA +static int fb_dev_fd = -1; // Handle to fb device name +static struct fb_fix_screeninfo fb_finfo; // Fixed info +static struct fb_var_screeninfo fb_vinfo; // Variable info +static struct fb_var_screeninfo fb_orig_vinfo; // Variable info to restore later +static struct fb_cmap fb_oldcmap; // Colormap to restore later +#endif + #ifdef ENABLE_XF86_VIDMODE // Variables for XF86 VidMode support static XF86VidModeModeInfo **x_video_modes; // Array of all available modes @@ -153,6 +181,20 @@ static pthread_mutex_t x_palette_lock = #define UNLOCK_PALETTE #endif +// Mutex to protect frame buffer +#ifdef HAVE_SPINLOCKS +static spinlock_t frame_buffer_lock = SPIN_LOCK_UNLOCKED; +#define LOCK_FRAME_BUFFER spin_lock(&frame_buffer_lock) +#define UNLOCK_FRAME_BUFFER spin_unlock(&frame_buffer_lock) +#elif defined(HAVE_PTHREADS) +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 + // Prototypes static void *redraw_func(void *arg); @@ -364,6 +406,36 @@ static bool find_visual_for_depth(int de * Open display (window or fullscreen) */ +// 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 = "SheepShaver"; + hints->res_class = "SheepShaver"; + XSetClassHint(x_display, w, hints); + XFree(hints); + } +} + +// 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) @@ -389,6 +461,18 @@ static void wait_unmapped(Window w) } while ((e.type != UnmapNotify) || (e.xmap.event != w)); } +// Disable mouse acceleration +static void disable_mouse_accel(void) +{ + XChangePointerControl(x_display, True, False, 1, 1, 0); +} + +// Restore mouse acceleration to original value +static void restore_mouse_accel(void) +{ + XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold); +} + // Trap SHM errors static bool shm_error = false; static int (*old_error_handler)(Display *, XErrorEvent *); @@ -421,8 +505,11 @@ static bool open_window(int width, int h the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr); - // Set window name - XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); + // Set window name/class + set_window_name(the_win, STR_WINDOW_TITLE); + + // Indicate that we want keyboard input + set_window_focus(the_win); // Set delete protocol property set_window_delete_protocol(the_win); @@ -550,9 +637,159 @@ static bool open_window(int width, int h return true; } -// Open DGA display (!! should use X11 VidMode extensions to set mode) -static bool open_dga(int width, int height) +// Open FBDev DGA display +static bool open_fbdev_dga(int width, int height) { +#ifdef ENABLE_FBDEV_DGA +#ifdef ENABLE_XF86_VIDMODE + // Switch to best mode + if (has_vidmode) { + int best = -1; + for (int i = 0; i < num_x_video_modes; i++) { + if (x_video_modes[i]->hdisplay == width && x_video_modes[i]->vdisplay == height) { + best = i; + break; + } + }; + assert(best != -1); + XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]); + XF86VidModeSetViewPort(x_display, screen, 0, 0); + D(bug("[fbdev] VideoMode %d: %d x %d @ %d\n", best, + x_video_modes[best]->hdisplay, x_video_modes[best]->vdisplay, + 1000 * x_video_modes[best]->dotclock / (x_video_modes[best]->htotal * x_video_modes[best]->vtotal))); + } +#endif + + if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo) != 0) { + D(bug("[fbdev] Can't get FSCREENINFO: %s\n", strerror(errno))); + return false; + } + D(bug("[fbdev] Device ID: %s\n", fb_finfo.id)); + D(bug("[fbdev] smem_start: %p [%d bytes]\n", fb_finfo.smem_start, fb_finfo.smem_len)); + + int fb_type = fb_finfo.type; + const char *fb_type_str = NULL; + switch (fb_type) { + case FB_TYPE_PACKED_PIXELS: fb_type_str = "Packed Pixels"; break; + case FB_TYPE_PLANES: fb_type_str = "Non interleaved planes"; break; + case FB_TYPE_INTERLEAVED_PLANES: fb_type_str = "Interleaved planes"; break; + case FB_TYPE_TEXT: fb_type_str = "Text/attributes"; break; + case FB_TYPE_VGA_PLANES: fb_type_str = "EGA/VGA planes"; break; + default: fb_type_str = ""; break; + } + D(bug("[fbdev] type: %s\n", fb_type_str)); + + if (fb_type != FB_TYPE_PACKED_PIXELS) { + D(bug("[fbdev] type '%s' not supported\n", fb_type_str)); + return false; + } + + int fb_visual = fb_finfo.visual; + const char *fb_visual_str; + switch (fb_visual) { + case FB_VISUAL_MONO01: fb_visual_str = "Monochrome 1=Black 0=White"; break; + case FB_VISUAL_MONO10: fb_visual_str = "Monochrome 1=While 0=Black"; break; + case FB_VISUAL_TRUECOLOR: fb_visual_str = "True color"; break; + case FB_VISUAL_PSEUDOCOLOR: fb_visual_str = "Pseudo color (like atari)"; break; + case FB_VISUAL_DIRECTCOLOR: fb_visual_str = "Direct color"; break; + case FB_VISUAL_STATIC_PSEUDOCOLOR: fb_visual_str = "Pseudo color readonly"; break; + default: fb_visual_str = ""; break; + } + D(bug("[fbdev] visual: %s\n", fb_visual_str)); + + if (fb_visual != FB_VISUAL_TRUECOLOR) { + D(bug("[fbdev] visual '%s' not supported\n", fb_visual_str)); + return false; + } + + // Map frame buffer + the_buffer = (uint8 *)mmap(NULL, fb_finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev_fd, 0); + if (the_buffer == MAP_FAILED) { + D(bug("[fbdev] Can't mmap /dev/fb0: %s\n", strerror(errno))); + return false; + } + + // Set absolute mouse mode + ADBSetRelMouseMode(false); + + // Create window + XSetWindowAttributes wattr; + wattr.event_mask = eventmask = dga_eventmask; + wattr.override_redirect = True; + the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, + InputOutput, DefaultVisual(x_display, screen), + CWEventMask | CWOverrideRedirect, &wattr); + + // Show window + XMapRaised(x_display, the_win); + wait_mapped(the_win); + + // Grab mouse and keyboard + XGrabKeyboard(x_display, the_win, True, + GrabModeAsync, GrabModeAsync, CurrentTime); + XGrabPointer(x_display, the_win, True, + PointerMotionMask | ButtonPressMask | ButtonReleaseMask, + GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime); + disable_mouse_accel(); + + // Create no_cursor + mac_cursor = XCreatePixmapCursor(x_display, + XCreatePixmap(x_display, the_win, 1, 1, 1), + XCreatePixmap(x_display, the_win, 1, 1, 1), + &black, &white, 0, 0); + XDefineCursor(x_display, the_win, mac_cursor); + + // Init blitting routines + int bytes_per_row = TrivialBytesPerRow((width + 7) & ~7, DepthModeForPixelDepth(depth)); +#if ENABLE_VOSF + // Extract current screen color masks (we are in True Color mode) + VisualFormat visualFormat; + visualFormat.depth = xdepth = DefaultDepth(x_display, screen); + XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo); + assert(visualFormat.depth == visualInfo.depth); + visualFormat.Rmask = visualInfo.red_mask; + visualFormat.Gmask = visualInfo.green_mask; + visualFormat.Bmask = visualInfo.blue_mask; + D(bug("[fbdev] %d bpp, (%08x,%08x,%08x)\n", + visualFormat.depth, + visualFormat.Rmask, visualFormat.Gmask, visualFormat.Bmask)); + D(bug("[fbdev] Mac depth %d bpp\n", depth)); + + // Screen_blitter_init() returns TRUE if VOSF is mandatory + // i.e. the framebuffer update function is not Blit_Copy_Raw +#ifdef WORDS_BIGENDIAN + const bool native_byte_order = (XImageByteOrder(x_display) == MSBFirst); +#else + const bool native_byte_order = (XImageByteOrder(x_display) == LSBFirst); +#endif + Screen_blitter_init(visualFormat, native_byte_order, depth); + + // Allocate memory for frame buffer (SIZE is extended to page-boundary) + use_vosf = true; + the_host_buffer = the_buffer; + the_buffer_size = page_extend((height + 2) * bytes_per_row); + the_buffer_copy = (uint8 *)malloc(the_buffer_size); + the_buffer = (uint8 *)vm_acquire(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)); +#endif + + // Set frame buffer base + D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf)); + screen_base = Host2MacAddr(the_buffer); + VModes[cur_mode].viRowBytes = bytes_per_row; + return true; +#else + ErrorAlert("SheepShaver has been compiled with DGA support disabled."); + return false; +#endif +} + +// Open XF86 DGA display (!! should use X11 VidMode extensions to set mode) +static bool open_xf86_dga(int width, int height) +{ + if (is_fbdev_dga_mode) + return false; + #ifdef ENABLE_XF86_DGA // Set relative mouse mode ADBSetRelMouseMode(true); @@ -642,11 +879,43 @@ static bool open_dga(int width, int heig #endif } +// Open DGA display +static bool open_dga(int width, int height) +{ + bool display_open; + + display_open = open_xf86_dga(width, height); +#ifdef ENABLE_FBDEV_DGA + // Try to fallback to FBDev DGA mode + if (!display_open) { + is_fbdev_dga_mode = true; + display_open = open_fbdev_dga(width, height); + } +#endif + + // Common DGA display initialization + if (display_open) { + + // Fake image to get display bounds in the refresh function + if ((img = (XImage *)malloc(sizeof(*img))) == NULL) + return false; + img->width = DisplayWidth(x_display, screen); + img->height = DisplayHeight(x_display, screen); + img->depth = is_fbdev_dga_mode ? xdepth : depth; + img->bytes_per_line = TrivialBytesPerRow(img->width, DepthModeForPixelDepth(img->depth)); + } + + return display_open; +} + static bool open_display(void) { D(bug("open_display()\n")); const VideoInfo &mode = VModes[cur_mode]; + // Get original mouse acceleration + XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold); + // Find best available X visual if (!find_visual_for_depth(mode.viAppleMode)) { ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); @@ -654,6 +923,7 @@ static bool open_display(void) } // Build up visualFormat structure + visualFormat.fullscreen = (display_type == DIS_SCREEN); visualFormat.depth = visualInfo.depth; visualFormat.Rmask = visualInfo.red_mask; visualFormat.Gmask = visualInfo.green_mask; @@ -726,22 +996,37 @@ static bool open_display(void) display_type = mode.viType; depth = depth_of_video_mode(mode.viAppleMode); - bool display_open = false; - if (display_type == DIS_SCREEN) + bool display_open; + switch (display_type) { + case DIS_SCREEN: display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize); - else if (display_type == DIS_WINDOW) + break; + case DIS_WINDOW: display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize); + break; + default: + display_open = false; + break; + } #ifdef ENABLE_VOSF if (use_vosf) { // Initialize the VOSF system + LOCK_VOSF; if (!video_vosf_init()) { ErrorAlert(GetString(STR_VOSF_INIT_ERR)); + UNLOCK_VOSF; return false; } + UNLOCK_VOSF; } #endif - + + // Zero screen buffers, viRowBytes is initialized at this stage + if (display_open) { + memset(the_buffer, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); + memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); + } return display_open; } @@ -777,20 +1062,49 @@ static void close_window(void) XSync(x_display, false); } -// Close DGA mode -static void close_dga(void) +// Close FBDev mode +static void close_fbdev_dga(void) +{ +#ifdef ENABLE_FBDEV_DGA + uint8 *fb_base; + if (!use_vosf) + fb_base = the_buffer; +#ifdef ENABLE_VOSF + else + fb_base = the_host_buffer; +#endif + munmap(fb_base, fb_finfo.smem_len); +#endif +} + +// Close XF86 DGA mode +static void close_xf86_dga(void) { #ifdef ENABLE_XF86_DGA XF86DGADirectVideo(x_display, screen, 0); +#endif +} + +// Close DGA mode +static void close_dga(void) +{ + if (is_fbdev_dga_mode) + close_fbdev_dga(); + else + close_xf86_dga(); + XUngrabPointer(x_display, CurrentTime); XUngrabKeyboard(x_display, CurrentTime); -#endif #ifdef ENABLE_XF86_VIDMODE if (has_vidmode) XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]); #endif + // Release fake image (it's not a normal XImage!) + free(img); + img = NULL; + if (!use_vosf) { // don't free() the screen buffer in driver_base dtor the_buffer = NULL; @@ -861,6 +1175,9 @@ static void close_display(void) } } #endif + + // Restore mouse acceleration + restore_mouse_accel(); } @@ -970,47 +1287,56 @@ static int find_mode(int apple_mode, int return -1; } +// Add custom video mode +static void add_custom_mode(VideoInfo *&p, int type, uint32 x, uint32 y, int apple_mode, int apple_id) +{ + p->viType = type; + p->viXsize = x; + p->viYsize = y; + p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode); + p->viAppleMode = apple_mode; + p->viAppleID = apple_id; + p++; +} + // Add mode to list of supported modes static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type) { if (allow & test) { - p->viType = type; + uint32 x = 0, y = 0; switch (apple_id) { case APPLE_W_640x480: case APPLE_640x480: - p->viXsize = 640; - p->viYsize = 480; + x = 640; + y = 480; break; case APPLE_W_800x600: case APPLE_800x600: - p->viXsize = 800; - p->viYsize = 600; + x = 800; + y = 600; break; case APPLE_1024x768: - p->viXsize = 1024; - p->viYsize = 768; + x = 1024; + y = 768; break; case APPLE_1152x768: - p->viXsize = 1152; - p->viYsize = 768; + x = 1152; + y = 768; break; case APPLE_1152x900: - p->viXsize = 1152; - p->viYsize = 900; + x = 1152; + y = 900; break; case APPLE_1280x1024: - p->viXsize = 1280; - p->viYsize = 1024; + x = 1280; + y = 1024; break; case APPLE_1600x1200: - p->viXsize = 1600; - p->viYsize = 1200; + x = 1600; + y = 1200; break; } - p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode); - p->viAppleMode = apple_mode; - p->viAppleID = apple_id; - p++; + add_custom_mode(p, type, x, y, apple_mode, apple_id); } } @@ -1024,13 +1350,14 @@ static void add_window_modes(VideoInfo * static bool has_mode(int x, int y) { #ifdef ENABLE_XF86_VIDMODE - for (int i=0; ihdisplay >= x && x_video_modes[i]->vdisplay >= y) - return true; - return false; -#else - return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y; + if (has_vidmode) { + for (int i=0; ihdisplay == x && x_video_modes[i]->vdisplay == y) + return true; + return false; + } #endif + return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y; } bool VideoInit(void) @@ -1079,10 +1406,16 @@ bool VideoInit(void) #ifdef ENABLE_XF86_DGA // DGA available? int event_base, error_base; + is_fbdev_dga_mode = false; if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) { int dga_flags = 0; XF86DGAQueryDirectVideo(x_display, screen, &dga_flags); has_dga = dga_flags & XF86DGADirectPresent; +#if defined(__linux__) + // Check r/w permission on /dev/mem for DGA mode to work + if (has_dga && access("/dev/mem", R_OK | W_OK) != 0) + has_dga = false; +#endif } else has_dga = false; #endif @@ -1091,8 +1424,33 @@ bool VideoInit(void) // VidMode available? int vm_event_base, vm_error_base; has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base); - if (has_vidmode) + if (has_vidmode) { + int vm_major_version, vm_minor_version; + XF86VidModeQueryVersion(x_display, &vm_major_version, &vm_minor_version); + D(bug("VidMode extension %d.%d available\n", vm_major_version, vm_minor_version)); XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes); + } +#endif + +#ifdef ENABLE_FBDEV_DGA + // FBDev available? + bool has_fbdev_dga = false; + if (local_X11) { + if ((fb_dev_fd = open("/dev/fb0", O_RDWR)) > 0) { + if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo) != 0) + close(fb_dev_fd); + else { + has_fbdev_dga = true; + if (!has_dga) { + // Fallback to FBDev DGA mode if XF86 DGA is not possible + has_dga = true; + is_fbdev_dga_mode = true; + } + fb_orig_vinfo = fb_vinfo; + D(bug("Frame buffer device initial resolution: %dx%dx%d\n", fb_vinfo.xres, fb_vinfo.yres, fb_vinfo.bits_per_pixel)); + } + } + } #endif // Find black and white colors @@ -1120,20 +1478,110 @@ bool VideoInit(void) break; } + // Get screen mode from preferences + const char *mode_str = PrefsFindString("screen"); + int default_width = 640, default_height = 480; + if (mode_str) { + display_type = DIS_INVALID; + if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2) + display_type = DIS_WINDOW; +#ifdef ENABLE_XF86_DGA + else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2) + display_type = DIS_SCREEN; +#endif +#ifdef ENABLE_FBDEV_DGA + else if (has_fbdev_dga && sscanf(mode_str, "fbdev/%d/%d", &default_width, &default_height) == 2) { + is_fbdev_dga_mode = true; + display_type = DIS_SCREEN; + } +#endif + if (display_type == DIS_INVALID) { + D(bug("Invalid screen mode specified, defaulting to old modes selection\n")); + mode_str = NULL; + } + else { + 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); + } + } + // Construct video mode table uint32 window_modes = PrefsFindInt32("windowmodes"); uint32 screen_modes = PrefsFindInt32("screenmodes"); if (!has_dga) screen_modes = 0; - if (window_modes == 0 && screen_modes == 0) + if (mode_str) + window_modes = screen_modes = 0; + else if (window_modes == 0 && screen_modes == 0) window_modes |= 3; // Allow at least 640x480 and 800x600 window modes VideoInfo *p = VModes; - for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) - if (find_visual_for_depth(d)) - add_window_modes(p, window_modes, d); + if (mode_str) { + if (display_type == DIS_WINDOW) { + for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) { + if (find_visual_for_depth(d)) { + if (default_width > 640 && default_height > 480) + add_mode(p, 3, 1, d, APPLE_W_640x480, DIS_WINDOW); + if (default_width > 800 && default_height > 600) + add_mode(p, 3, 2, d, APPLE_W_800x600, DIS_WINDOW); + add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM); + } + } +#ifdef ENABLE_VOSF + } else if (display_type == DIS_SCREEN && is_fbdev_dga_mode) { + for (unsigned int d = APPLE_1_BIT; d <= default_mode; d++) + if (find_visual_for_depth(d)) + add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM); +#endif + } else + add_custom_mode(p, display_type, default_width, default_height, default_mode, APPLE_CUSTOM); - if (has_vidmode) { + // Add extra VidMode capable modes + if (display_type == DIS_SCREEN) { + struct { + int w; + int h; + int apple_id; + } + video_modes[] = { + { 640, 480, APPLE_640x480 }, + { 800, 600, APPLE_800x600 }, + { 1024, 768, APPLE_1024x768 }, + { 1152, 768, APPLE_1152x768 }, + { 1152, 900, APPLE_1152x900 }, + { 1280, 1024, APPLE_1280x1024 }, + { 1600, 1200, APPLE_1600x1200 }, + { 0, } + }; + + for (int i = 0; video_modes[i].w != 0; i++) { + const int w = video_modes[i].w; + const int h = video_modes[i].h; + if (w >= default_width || h >= default_height) + continue; + if (has_mode(w, h)) { +#ifdef ENABLE_VOSF + if (is_fbdev_dga_mode) { + for (unsigned int d = APPLE_1_BIT; d <= default_mode; d++) + if (find_visual_for_depth(d)) + add_custom_mode(p, display_type, w, h, d, video_modes[i].apple_id); + } else +#endif + add_custom_mode(p, display_type, w, h, default_mode, video_modes[i].apple_id); + } + } + } + } else if (window_modes) { + for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) + if (find_visual_for_depth(d)) + add_window_modes(p, window_modes, d); + } else if (has_vidmode) { if (has_mode(640, 480)) add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN); if (has_mode(800, 600)) @@ -1174,7 +1622,9 @@ bool VideoInit(void) int apple_id = find_apple_resolution(screen_width, screen_height); if (apple_id != -1) cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN); - } + } else if (has_dga && mode_str) + cur_mode = find_mode(default_mode, APPLE_CUSTOM, DIS_SCREEN); + if (cur_mode == -1) { // pick up first windowed mode available for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) { @@ -1195,14 +1645,20 @@ bool VideoInit(void) #endif // Open window/screen - if (!open_display()) + if (!open_display()) { + ErrorAlert(GetString(STR_OPEN_WINDOW_ERR)); return false; + } #if 0 // Ignore errors from now on XSetErrorHandler(ignore_errors); #endif + // Lock down frame buffer + XSync(x_display, false); + LOCK_FRAME_BUFFER; + // Start periodic thread XSync(x_display, false); Set_pthread_attr(&redraw_thread_attr, 0); @@ -1227,6 +1683,11 @@ void VideoExit(void) redraw_thread_active = false; } + // Unlock frame buffer + UNLOCK_FRAME_BUFFER; + XSync(x_display, false); + D(bug(" frame buffer unlocked\n")); + #ifdef ENABLE_VOSF if (use_vosf) { // Deinitialize VOSF @@ -1241,6 +1702,14 @@ void VideoExit(void) XFlush(x_display); XSync(x_display, false); } + +#ifdef ENABLE_FBDEV_DGA + // Close framebuffer device + if (fb_dev_fd >= 0) { + close(fb_dev_fd); + fb_dev_fd = -1; + } +#endif } @@ -1248,9 +1717,6 @@ void VideoExit(void) * Suspend/resume emulator */ -extern void PauseEmulator(void); -extern void ResumeEmulator(void); - static void suspend_emul(void) { if (display_type == DIS_SCREEN) { @@ -1258,9 +1724,8 @@ static void suspend_emul(void) ADBKeyUp(0x36); ctrl_down = false; - // Pause MacOS thread - PauseEmulator(); - emul_suspended = true; + // Lock frame buffer (this will stop the MacOS thread) + LOCK_FRAME_BUFFER; // Save frame buffer fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes); @@ -1268,29 +1733,32 @@ static void suspend_emul(void) Mac2Host_memcpy(fb_save, screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes); // Close full screen display +#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) #ifdef ENABLE_XF86_DGA - XF86DGADirectVideo(x_display, screen, 0); + if (!is_fbdev_dga_mode) + XF86DGADirectVideo(x_display, screen, 0); +#endif XUngrabPointer(x_display, CurrentTime); XUngrabKeyboard(x_display, CurrentTime); #endif + restore_mouse_accel(); + XUnmapWindow(x_display, the_win); + wait_unmapped(the_win); XSync(x_display, false); // Open "suspend" window XSetWindowAttributes wattr; wattr.event_mask = KeyPressMask; - wattr.background_pixel = black_pixel; - wattr.border_pixel = black_pixel; + wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0); wattr.backing_store = Always; - wattr.backing_planes = xdepth; - wattr.colormap = DefaultColormap(x_display, screen); - XSync(x_display, false); + wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]); + 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); + InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore | CWColormap, &wattr); + set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE); + set_window_focus(suspend_win); + XMapWindow(x_display, suspend_win); + emul_suspended = true; } } @@ -1301,11 +1769,18 @@ static void resume_emul(void) XSync(x_display, false); // Reopen full screen display - XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime); - XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); + XMapRaised(x_display, the_win); + wait_mapped(the_win); + XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); + Window w = is_fbdev_dga_mode ? the_win : rootwin; + XGrabKeyboard(x_display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime); + XGrabPointer(x_display, w, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, is_fbdev_dga_mode ? w : None, None, CurrentTime); + disable_mouse_accel(); #ifdef ENABLE_XF86_DGA - XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse); - XF86DGASetViewPort(x_display, screen, 0, 0); + if (!is_fbdev_dga_mode) { + XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse); + XF86DGASetViewPort(x_display, screen, 0, 0); + } #endif XSync(x_display, false); @@ -1316,8 +1791,8 @@ static void resume_emul(void) if (use_vosf) { LOCK_VOSF; PFLAG_SET_ALL; - UNLOCK_VOSF; memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); + UNLOCK_VOSF; } #endif @@ -1331,12 +1806,10 @@ static void resume_emul(void) free(fb_save); fb_save = NULL; } - if (depth == 8) - palette_changed = true; - // Resume MacOS thread + // Unlock frame buffer (and continue MacOS thread) + UNLOCK_FRAME_BUFFER; emul_suspended = false; - ResumeEmulator(); } @@ -1633,10 +2106,12 @@ static void handle_events(void) if (use_vosf) { // VOSF refresh LOCK_VOSF; PFLAG_SET_ALL; + memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); UNLOCK_VOSF; } + else #endif - memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); + memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); break; } } @@ -1652,6 +2127,11 @@ void VideoVBL(void) if (emerg_quit) QuitEmulator(); + // Temporarily give up frame buffer lock (this is the point where + // we are suspended when the user presses Ctrl-Tab) + UNLOCK_FRAME_BUFFER; + LOCK_FRAME_BUFFER; + // Execute video VBL if (private_data != NULL && private_data->interruptsEnabled) VSLDoInterruptService(private_data->vslServiceID); @@ -1748,8 +2228,9 @@ void video_set_palette(void) // We have to redraw everything because the interpretation of pixel values changed LOCK_VOSF; PFLAG_SET_ALL; + if (display_type == DIS_SCREEN) + PFLAG_SET_VERY_DIRTY; UNLOCK_VOSF; - memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); } #endif @@ -1931,7 +2412,7 @@ static void handle_palette_changes(void) } #ifdef ENABLE_XF86_DGA - if (display_type == DIS_SCREEN) { + if (display_type == DIS_SCREEN && !is_fbdev_dga_mode) { current_dga_cmap ^= 1; if (!IsDirectMode(mode) && cmap[current_dga_cmap]) XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); @@ -1976,8 +2457,11 @@ static void *redraw_func(void *arg) quit_full_screen = false; if (display_type == DIS_SCREEN) { XDisplayLock(); +#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) #ifdef ENABLE_XF86_DGA - XF86DGADirectVideo(x_display, screen, 0); + if (!is_fbdev_dga_mode) + XF86DGADirectVideo(x_display, screen, 0); +#endif XUngrabPointer(x_display, CurrentTime); XUngrabKeyboard(x_display, CurrentTime); XUnmapWindow(x_display, the_win);