--- BasiliskII/src/Unix/video_x.cpp 1999/10/03 16:20:08 1.2 +++ BasiliskII/src/Unix/video_x.cpp 2000/09/22 17:16:31 1.19 @@ -1,7 +1,7 @@ /* * video_x.cpp - Video/graphics emulation, X11 specific stuff * - * Basilisk II (C) 1997-1999 Christian Bauer + * Basilisk II (C) 1997-2000 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 @@ -34,9 +34,32 @@ #include #include #include -#include #include +#ifdef HAVE_PTHREADS +# include +#endif + +#ifdef ENABLE_XF86_DGA +# include +#endif + +#ifdef ENABLE_XF86_VIDMODE +# include +#endif + +#ifdef ENABLE_FBDEV_DGA +# include +#endif + +#ifdef ENABLE_VOSF +# include // log() +# include +# include +# include +# include +#endif + #include "cpu_emulation.h" #include "main.h" #include "adb.h" @@ -45,13 +68,9 @@ #include "user_strings.h" #include "video.h" -#define DEBUG 1 +#define DEBUG 0 #include "debug.h" -#if ENABLE_DGA -#include -#endif - // Display types enum { @@ -59,22 +78,31 @@ enum { DISPLAY_DGA // DGA fullscreen display }; - // Constants -const char KEYCODE_FILE_NAME[] = SHAREDIR "keycodes"; +const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes"; +const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices"; // Global variables -static int32 frame_skip; +static int32 frame_skip; // Prefs items +static int16 mouse_wheel_mode = 1; +static int16 mouse_wheel_lines = 3; + 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 + +#ifdef HAVE_PTHREADS static bool redraw_thread_active = false; // Flag: Redraw thread installed static volatile bool redraw_thread_cancel = false; // 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 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 static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread static bool emul_suspended = false; // Flag: Emulator suspended @@ -98,29 +126,104 @@ static int eventmask; static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask; static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask; -static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect palette 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 // Variables for window mode static GC the_gc; static XImage *img = NULL; static XShmSegmentInfo shminfo; -static XImage *cursor_image, *cursor_mask_image; -static Pixmap cursor_map, cursor_mask_map; static Cursor mac_cursor; -static GC cursor_gc, cursor_mask_gc; static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer -static uint8 the_cursor[64]; // Cursor image data 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}; -// Variables for DGA mode +// 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 +#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 int num_x_video_modes; +#endif + +#ifdef ENABLE_VOSF +static bool use_vosf = true; // Flag: VOSF enabled +#else +static const bool use_vosf = false; // Flag: VOSF enabled +#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; + +#ifdef HAVE_PTHREADS +static pthread_mutex_t Screen_draw_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer (dirtyPages in fact) +#endif + +#endif + +// VideoRefresh function +void VideoRefreshInit(void); +static void (*video_refresh)(void); // Prototypes static void *redraw_func(void *arg); @@ -128,11 +231,16 @@ static int event2keycode(XKeyEvent *ev); // From main_unix.cpp +extern char *x_display_name; extern Display *x_display; // From sys_unix.cpp extern void SysMountFirstFloppy(void); +#ifdef ENABLE_VOSF +# include "video_vosf.h" +#endif + /* * Initialization @@ -141,37 +249,52 @@ extern void SysMountFirstFloppy(void); // Set VideoMonitor according to video mode void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order) { - int layout; +#if !REAL_ADDRESSING && !DIRECT_ADDRESSING + int layout = FLAYOUT_DIRECT; switch (depth) { case 1: layout = FLAYOUT_DIRECT; - VideoMonitor.mode = VMODE_1BIT; break; case 8: layout = FLAYOUT_DIRECT; - VideoMonitor.mode = VMODE_8BIT; break; case 15: layout = FLAYOUT_HOST_555; - VideoMonitor.mode = VMODE_16BIT; break; case 16: layout = FLAYOUT_HOST_565; - VideoMonitor.mode = VMODE_16BIT; break; case 24: case 32: layout = FLAYOUT_HOST_888; - VideoMonitor.mode = VMODE_32BIT; break; } - VideoMonitor.x = width; - VideoMonitor.y = height; - VideoMonitor.bytes_per_row = bytes_per_row; if (native_byte_order) MacFrameLayout = layout; else MacFrameLayout = FLAYOUT_DIRECT; +#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; + } + VideoMonitor.x = width; + VideoMonitor.y = height; + VideoMonitor.bytes_per_row = bytes_per_row; } // Trap SHM errors @@ -190,20 +313,22 @@ static int error_handler(Display *d, XEr // Init window mode static bool init_window(int width, int height) { + 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"); - if (frame_skip == 0) - frame_skip = 1; // Create window XSetWindowAttributes wattr; wattr.event_mask = eventmask = win_eventmask; wattr.background_pixel = black_pixel; wattr.border_pixel = black_pixel; - wattr.backing_store = Always; + wattr.backing_store = NotUseful; + wattr.save_under = false; wattr.backing_planes = xdepth; XSync(x_display, false); @@ -232,16 +357,16 @@ static bool init_window(int width, int h XSetWMNormalHints(x_display, the_win, hints); XFree((char *)hints); } - + // Try to create and attach SHM image have_shm = false; - if (depth != 1 && XShmQueryExtension(x_display)) { + if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) { // Create SHM image ("height + 2" for safety) img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height); - shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777); - the_buffer = (uint8 *)shmat(shminfo.shmid, 0, 0); - shminfo.shmaddr = img->data = (char *)the_buffer; + 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; shminfo.readOnly = False; // Try to attach SHM image, catching errors @@ -259,10 +384,10 @@ static bool init_window(int width, int h shmctl(shminfo.shmid, IPC_RMID, 0); } } - + // Create normal X image if SHM doesn't work ("height + 2" for safety) if (!have_shm) { - int bytes_per_row = width; + int bytes_per_row = aligned_width; switch (depth) { case 1: bytes_per_row /= 8; @@ -276,8 +401,8 @@ static bool init_window(int width, int h bytes_per_row *= 4; break; } - the_buffer = (uint8 *)malloc((height + 2) * bytes_per_row); - img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer, width, height, 32, bytes_per_row); + 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); } // 1-Bit mode is big-endian @@ -286,48 +411,233 @@ static bool init_window(int width, int h img->bitmap_bit_order = MSBFirst; } - // Allocate memory for frame buffer copy - the_buffer_copy = (uint8 *)malloc((height + 2) * img->bytes_per_line); +#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); + 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); +#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); - // Create cursor - cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor, 16, 16, 16, 2); - cursor_image->byte_order = MSBFirst; - cursor_image->bitmap_bit_order = MSBFirst; - cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor+32, 16, 16, 16, 2); - cursor_mask_image->byte_order = MSBFirst; - cursor_mask_image->bitmap_bit_order = MSBFirst; - cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1); - cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1); - cursor_gc = XCreateGC(x_display, cursor_map, 0, 0); - cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0); - mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0); + // 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); // Set VideoMonitor + bool native_byte_order; #ifdef WORDS_BIGENDIAN - set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst); + native_byte_order = (img->bitmap_bit_order == MSBFirst); +#else + native_byte_order = (img->bitmap_bit_order == LSBFirst); +#endif +#ifdef ENABLE_VOSF + do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW); +#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 - set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst); + VideoMonitor.mac_frame_base = MacFrameBaseMac; +#endif + return true; +} + +// Init fbdev DGA display +static bool init_fbdev_dga(char *in_fb_name) +{ +#ifdef ENABLE_FBDEV_DGA + // 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; + } else { + while (ndepths-- > 0) { + if (depths[ndepths] > max_depth) + max_depth = depths[ndepths]; + } + } + + // Get fbdevices file path from preferences + const char *fbd_path = PrefsFindString("fbdevicefile"); + + // Open fbdevices file + FILE *fp = fopen(fbd_path ? fbd_path : FBDEVICES_FILE_NAME, "r"); + if (fp == NULL) { + char str[256]; + sprintf(str, GetString(STR_NO_FBDEVICE_FILE_ERR), fbd_path ? fbd_path : FBDEVICES_FILE_NAME, strerror(errno)); + ErrorAlert(str); + return false; + } + + int fb_depth; // supported depth + uint32 fb_offset; // offset used for mmap(2) + char fb_name[20]; + char line[256]; + bool device_found = false; + while (fgets(line, 255, fp)) { + // Read line + int len = strlen(line); + if (len == 0) + continue; + line[len - 1] = '\0'; + + // Comments begin with "#" or ";" + if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0')) + 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)) { + device_found = true; + break; + } + } + + // fbdevices file completely read + fclose(fp); + + // 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); + ErrorAlert(str); + return false; + } + + 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; + + XSync(x_display, false); + the_win = XCreateWindow(x_display, rootwin, + 0, 0, width, height, + 0, xdepth, InputOutput, vis, + CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore, + &wattr); + XSync(x_display, false); + XMapRaised(x_display, the_win); + XSync(x_display, false); + + // 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); + + // Set colormap + if (depth == 8) { + XSetWindowColormap(x_display, the_win, cmap[0]); + XSetWMColormapWindows(x_display, the_win, &the_win, 1); + } + + // 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; + } + + 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; + } + } + +#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; + + 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); + } +#elif ENABLE_VOSF + use_vosf = false; #endif -#if REAL_ADDRESSING - VideoMonitor.mac_frame_base = (uint32)the_buffer; - MacFrameLayout = FLAYOUT_DIRECT; + + 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 } -// Init DGA display -static bool init_dga(int width, int height) +// Init XF86 DGA display +static bool init_xf86_dga(int width, int height) { -#if ENABLE_DGA +#ifdef ENABLE_XF86_DGA // Set relative mouse mode ADBSetRelMouseMode(true); +#ifdef ENABLE_XF86_VIDMODE + // Switch to best mode + if (has_vidmode) { + int best = 0; + for (int i=1; ihdisplay >= width && x_video_modes[i]->vdisplay >= height && + x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) { + best = i; + } + } + XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]); + XF86VidModeSetViewPort(x_display, screen, 0, 0); + } +#endif + // Create window XSetWindowAttributes wattr; wattr.event_mask = eventmask = dga_eventmask; @@ -376,16 +686,38 @@ static bool init_dga(int width, int heig bytes_per_row *= 4; break; } + +#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; + + 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); + } +#elif ENABLE_VOSF + // The UAE memory handlers will already color conversion, if needed. + use_vosf = false; +#endif + set_video_monitor(width, height, bytes_per_row, true); -#if REAL_ADDRESSING - VideoMonitor.mac_frame_base = (uint32)the_buffer; - MacFrameLayout = FLAYOUT_DIRECT; +#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 DGA support disabled."); + ErrorAlert("Basilisk II has been compiled with XF86 DGA support disabled."); return false; #endif } @@ -455,25 +787,129 @@ static void keycode_init(void) } } +bool VideoInitBuffer() +{ +#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 = int( log(mainBuffer.pageSize) / log(2.0) ); + + if (mainBuffer.dirtyPages != 0) + free(mainBuffer.dirtyPages); + + mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount); + + if (mainBuffer.pageInfo != 0) + free(mainBuffer.pageInfo); + + mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo)); + + if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0)) + return false; + + PFLAG_CLEAR_ALL; + + 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; + } +#endif + return true; +} + bool VideoInit(bool 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; +#endif + + // Check if X server runs on local machine + local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0) + || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0); + // Init keycode translation keycode_init(); + // Read prefs + mouse_wheel_mode = PrefsFindInt16("mousewheelmode"); + mouse_wheel_lines = PrefsFindInt16("mousewheellines"); + // Find screen and root window screen = XDefaultScreen(x_display); rootwin = XRootWindow(x_display, screen); - + // Get screen depth xdepth = DefaultDepth(x_display, screen); + +#ifdef ENABLE_FBDEV_DGA + // Frame buffer name + char fb_name[20]; + + // Could do fbdev dga ? + if ((fbdev_fd = open(FBDEVICE_FILE_NAME, O_RDWR)) != -1) + has_dga = true; + else + has_dga = false; +#endif -#if ENABLE_DGA +#ifdef ENABLE_XF86_DGA // DGA available? - int dga_flags = 0; - XF86DGAQueryDirectVideo(x_display, screen, &dga_flags); - has_dga = dga_flags & XF86DGADirectPresent; + int dga_event_base, dga_error_base; + if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) { + int dga_flags = 0; + XF86DGAQueryDirectVideo(x_display, screen, &dga_flags); + has_dga = dga_flags & XF86DGADirectPresent; + } else + has_dga = false; #endif +#ifdef ENABLE_XF86_VIDMODE + // VidMode available? + int vm_event_base, vm_error_base; + has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base); + if (has_vidmode) + XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes); +#endif + // Find black and white colors XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black); XAllocColor(x_display, DefaultColormap(x_display, screen), &black); @@ -539,11 +975,21 @@ bool VideoInit(bool classic) if (mode_str) { if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2) display_type = DISPLAY_WINDOW; - else if (has_dga && strcmp(mode_str, "dga") == 0) { +#ifdef ENABLE_FBDEV_DGA + else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) { +#else + else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) { +#endif display_type = DISPLAY_DGA; + if (width > 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 @@ -553,15 +999,21 @@ bool VideoInit(bool classic) return false; break; case DISPLAY_DGA: - if (!init_dga(width, height)) +#ifdef ENABLE_FBDEV_DGA + if (!init_fbdev_dga(fb_name)) +#else + if (!init_xf86_dga(width, height)) +#endif return false; break; } +#ifdef HAVE_PTHREADS // Lock down frame buffer pthread_mutex_lock(&frame_buffer_lock); +#endif -#if !REAL_ADDRESSING +#if !REAL_ADDRESSING && !DIRECT_ADDRESSING // Set variables for UAE memory mapping MacFrameBaseHost = the_buffer; MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y; @@ -571,12 +1023,38 @@ bool VideoInit(bool classic) MacFrameLayout = FLAYOUT_NONE; #endif - // Start redraw/input thread +#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; + } + + // 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; + } + } +#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) + if (!redraw_thread_active) { printf("FATAL: cannot create redraw thread\n"); - return redraw_thread_active; + return false; + } +#endif + return true; } @@ -586,6 +1064,7 @@ bool VideoInit(bool classic) void VideoExit(void) { +#ifdef HAVE_PTHREADS // Stop redraw thread if (redraw_thread_active) { redraw_thread_cancel = true; @@ -595,15 +1074,18 @@ void VideoExit(void) pthread_join(redraw_thread, NULL); redraw_thread_active = false; } +#endif +#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); -#if ENABLE_DGA +#ifdef ENABLE_XF86_DGA if (display_type == DISPLAY_DGA) { XF86DGADirectVideo(x_display, screen, 0); XUngrabPointer(x_display, CurrentTime); @@ -611,10 +1093,18 @@ void VideoExit(void) } #endif - if (the_buffer_copy) { - free(the_buffer_copy); - the_buffer_copy = NULL; +#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); @@ -622,7 +1112,51 @@ void VideoExit(void) XFreeColormap(x_display, cmap[0]); XFreeColormap(x_display, cmap[1]); } + + if (!use_vosf) { + if (the_buffer) { + free(the_buffer); + the_buffer = NULL; + } + + 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 + if (use_vosf) { + // Clear mainBuffer data + if (mainBuffer.pageInfo) { + free(mainBuffer.pageInfo); + mainBuffer.pageInfo = 0; + } + + if (mainBuffer.dirtyPages) { + free(mainBuffer.dirtyPages); + mainBuffer.dirtyPages = 0; + } + } + + // Close /dev/zero + if (zero_fd > 0) + close(zero_fd); +#endif } @@ -648,10 +1182,12 @@ 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 } @@ -661,7 +1197,9 @@ void VideoInterrupt(void) void video_set_palette(uint8 *pal) { +#ifdef HAVE_PTHREDS pthread_mutex_lock(&palette_lock); +#endif // Convert colors to XColor array for (int i=0; i<256; i++) { @@ -675,7 +1213,9 @@ void video_set_palette(uint8 *pal) // Tell redraw thread to change palette palette_changed = true; +#ifdef HAVE_PTHREADS pthread_mutex_unlock(&palette_lock); +#endif } @@ -683,7 +1223,7 @@ void video_set_palette(uint8 *pal) * Suspend/resume emulator */ -#if ENABLE_DGA +#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) static void suspend_emul(void) { if (display_type == DISPLAY_DGA) { @@ -691,8 +1231,10 @@ static void suspend_emul(void) 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); @@ -700,7 +1242,9 @@ static void suspend_emul(void) 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); @@ -714,6 +1258,7 @@ static void suspend_emul(void) 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 | @@ -738,22 +1283,46 @@ static void resume_emul(void) 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); // Restore frame buffer if (fb_save) { +#if REAL_ADDRESSING || DIRECT_ADDRESSING + if (use_vosf) + mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ|PROT_WRITE); +#endif memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row); +#if REAL_ADDRESSING || DIRECT_ADDRESSING + if (use_vosf) { + mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ); + do_update_framebuffer(the_host_buffer, the_buffer, VideoMonitor.x * VideoMonitor.bytes_per_row); +#ifdef HAVE_PTHREADS + pthread_mutex_lock(&Screen_draw_lock); +#endif + PFLAG_CLEAR_ALL; +#ifdef HAVE_PTHREADS + pthread_mutex_unlock(&Screen_draw_lock); +#endif + } +#endif free(fb_save); fb_save = NULL; } + +#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 @@ -815,7 +1384,7 @@ static int kc_decode(KeySym ks) case XK_period: case XK_greater: return 0x2f; case XK_slash: case XK_question: return 0x2c; -#if ENABLE_DGA +#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30; #else case XK_Tab: return 0x30; @@ -941,6 +1510,19 @@ static void handle_events(void) unsigned int button = ((XButtonEvent *)&event)->button; if (button < 4) ADBMouseDown(button - 1); + else if (button < 6) { // Wheel mouse + if (mouse_wheel_mode == 0) { + int key = (button == 5) ? 0x79 : 0x74; // Page up/down + ADBKeyDown(key); + ADBKeyUp(key); + } else { + int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down + for(int i=0; ikeycode & 0xff]; } else code = event2keycode((XKeyEvent *)&event); - if (code != -1) { + if (code != -1 && code != 0x39) { // Don't propagate Caps Lock releases ADBKeyUp(code); if (code == 0x36) ctrl_down = false; @@ -997,8 +1588,36 @@ static void handle_events(void) // Hidden parts exposed, force complete refresh of window case Expose: - if (display_type == DISPLAY_WINDOW) - memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); + if (display_type == DISPLAY_WINDOW) { +#ifdef ENABLE_VOSF + const XExposeEvent & xev = event.xexpose; + const uint32 start = xev.y * VideoMonitor.bytes_per_row + xev.x; + const uint32 end = (xev.y + xev.height - 1) * VideoMonitor.bytes_per_row + (xev.x + xev.width); + int page = start >> mainBuffer.pageBits; + uint32 addr = start; + while (addr < end) { + memset(the_buffer_copy + addr, 0, mainBuffer.pageSize); + addr += mainBuffer.pageSize; +#ifdef HAVE_PTHREADS + pthread_mutex_lock(&Screen_draw_lock); +#endif + PFLAG_SET(page); +#ifdef HAVE_PTHREADS + pthread_mutex_unlock(&Screen_draw_lock); +#endif + ++page; + } +#else + if (frame_skip == 0) { // Dynamic refresh + int x1, y1; + for (y1=0; y1<16; y1++) + for (x1=0; x1<16; x1++) + updt_box[x1][y1] = true; + nr_boxes = 16 * 16; + } else + memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); +#endif /* ENABLE_VOSF */ + } break; } } @@ -1009,12 +1628,99 @@ static void handle_events(void) * Window display update */ -static void update_display(void) +// Dynamic display update (variable frame rate for each box) +static void update_display_dynamic(int ticker) { - // In classic mode, copy the frame buffer from Mac RAM - if (classic_mode) - memcpy(the_buffer, Mac2HostAddr(0x3fa700), VideoMonitor.bytes_per_row * VideoMonitor.y); + 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 max_box; + + y2s = sm_uptd[ticker % 8]; + y2a = 8; + for (i = 0; i < 6; i++) + if (ticker % (2 << i)) + break; + max_box = sm_no_boxes[i]; + + if (y2a) { + for (y1=0; y1<16; y1++) { + for (y2=y2s; y2 < ry; y2 += y2a) { + i = ((y1 * ry) + y2) * bytes_per_row; + for (x1=0; x1<16; x1++, i += rx) { + if (updt_box[x1][y1] == false) { + if (memcmp(&the_buffer_copy[i], &the_buffer[i], rx)) { + updt_box[x1][y1] = true; + nr_boxes++; + } + } + } + } + } + } + + if ((nr_boxes <= max_box) && (nr_boxes)) { + for (y1=0; y1<16; y1++) { + for (x1=0; x1<16; x1++) { + if (updt_box[x1][y1] == true) { + if (rxm == 0) + xm = x1; + rxm += rx; + updt_box[x1][y1] = false; + } + if (((updt_box[x1+1][y1] == false) || (x1 == 15)) && (rxm)) { + if ((rxmo != rxm) || (xmo != xm) || (yo != y1 - 1)) { + if (rxmo) { + xi = xmo * rx; + yi = ymo * ry; + xil = rxmo; + yil = (yo - ymo +1) * ry; + } + rxmo = rxm; + xmo = xm; + ymo = y1; + } + rxm = 0; + yo = y1; + } + if (xil) { + 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); + else + XPutImage(x_display, the_win, the_gc, 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); + else + XPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil); + } + xil = 0; + } + if ((x1 == 15) && (y1 == 15) && (rxmo)) { + x1--; + xi = xmo * rx; + yi = ymo * ry; + xil = rxmo; + yil = (yo - ymo +1) * ry; + rxmo = 0; + } + } + } + nr_boxes = 0; + } +} +// Static display update (fixed frame rate, but incremental) +static void update_display_static(void) +{ // Incremental update code int wide = 0, high = 0, x1, x2, y1, y2, i, j; int bytes_per_row = VideoMonitor.bytes_per_row; @@ -1050,8 +1756,7 @@ static void update_display(void) x1 = i << 3; break; } - p++; - p2++; + p++; p2++; } } x2 = x1; @@ -1061,8 +1766,7 @@ static void update_display(void) p += bytes_per_row; p2 += bytes_per_row; for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) { - p--; - p2--; + p--; p2--; if (*p != *p2) { x2 = i << 3; break; @@ -1075,7 +1779,7 @@ static void update_display(void) if (high && wide) { for (j=y1; j<=y2; j++) { i = j * bytes_per_row + (x1 >> 3); - memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3); + memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3); } } @@ -1084,13 +1788,12 @@ static void update_display(void) for (j=y1; j<=y2; j++) { p = &the_buffer[j * bytes_per_row]; p2 = &the_buffer_copy[j * bytes_per_row]; - for (i=0; ix2; i--) { - p -= bytes_per_pixel; - p2 -= bytes_per_pixel; - if (memcmp(p, p2, bytes_per_pixel)) { - x2 = i; + for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) { + p--; + p2--; + if (*p != *p2) { + x2 = i / bytes_per_pixel; break; } } @@ -1114,7 +1817,7 @@ static void update_display(void) if (high && wide) { for (j=y1; j<=y2; j++) { i = j * bytes_per_row + x1 * bytes_per_pixel; - memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide); + memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide); } } } @@ -1127,22 +1830,167 @@ static void update_display(void) else XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high); } +} + + +/* + * 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) - // Has the Mac started? (cursor data is not valid otherwise) - if (HasMacStarted()) { +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); + } +#endif +} - // Set new cursor image if it was changed - if (memcmp(the_cursor, Mac2HostAddr(0x844), 64)) { - memcpy(the_cursor, Mac2HostAddr(0x844), 64); - memcpy(cursor_image->data, the_cursor, 32); - memcpy(cursor_mask_image->data, the_cursor+32, 32); - XFreeCursor(x_display, mac_cursor); - XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16); - XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16); - mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, ReadMacInt8(0x885), ReadMacInt8(0x887)); - XDefineCursor(x_display, the_win, mac_cursor); +static inline void handle_palette_changes(int depth, int display_type) +{ +#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 +} + +static void video_refresh_dga(void) +{ + // Quit DGA mode if requested + possibly_quit_dga_mode(); + + // Handle X events + handle_events(); + + // Handle palette changes + handle_palette_changes(depth, DISPLAY_DGA); +} + +#if REAL_ADDRESSING || DIRECT_ADDRESSING +static void video_refresh_dga_vosf(void) +{ + // Quit DGA mode if requested + possibly_quit_dga_mode(); + + // Handle X events + handle_events(); + + // Handle palette changes + handle_palette_changes(depth, DISPLAY_DGA); + + // 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 + } +} +#endif + +#ifdef ENABLE_VOSF +static void video_refresh_window_vosf(void) +{ + // Quit DGA mode if requested + possibly_quit_dga_mode(); + + // Handle X events + handle_events(); + + // Handle palette changes + handle_palette_changes(depth, DISPLAY_WINDOW); + + // 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 + } +} +#endif + +static void video_refresh_window_static(void) +{ + // Handle X events + handle_events(); + + // Handle_palette changes + handle_palette_changes(depth, DISPLAY_WINDOW); + + // Update display (static variant) + static int tick_counter = 0; + if (++tick_counter >= frame_skip) { + tick_counter = 0; + update_display_static(); + } +} + +static void video_refresh_window_dynamic(void) +{ + // Handle X events + handle_events(); + + // Handle_palette changes + handle_palette_changes(depth, DISPLAY_WINDOW); + + // Update display (dynamic variant) + static int tick_counter = 0; + tick_counter++; + update_display_dynamic(tick_counter); } @@ -1150,62 +1998,113 @@ static void update_display(void) * Thread for screen refresh, input handling etc. */ -static void *redraw_func(void *arg) +void VideoRefreshInit(void) { - int tick_counter = 0; + // TODO: set up specialised 8bpp VideoRefresh handlers ? + if (display_type == DISPLAY_DGA) { +#if REAL_ADDRESSING || DIRECT_ADDRESSING + if (use_vosf) + video_refresh = video_refresh_dga_vosf; + else +#endif + video_refresh = video_refresh_dga; + } + else { +#ifdef ENABLE_VOSF + if (use_vosf) + video_refresh = video_refresh_window_vosf; + else +#endif + if (frame_skip == 0) + video_refresh = video_refresh_window_dynamic; + else + video_refresh = video_refresh_window_static; + } +} - while (!redraw_thread_cancel) { +void VideoRefresh(void) +{ + // TODO: make main_unix/VideoRefresh call directly video_refresh() ? + video_refresh(); +} - // Wait -#ifdef HAVE_NANOSLEEP - struct timespec req = {0, 16666667}; - nanosleep(&req, NULL); -#else - usleep(16667); +#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 - -#if ENABLE_DGA - // Quit DGA mode if requested - if (quit_full_screen) { - quit_full_screen = false; - if (display_type == DISPLAY_DGA) { - XF86DGADirectVideo(x_display, screen, 0); - XUngrabPointer(x_display, CurrentTime); - XUngrabKeyboard(x_display, CurrentTime); - XUnmapWindow(x_display, the_win); - XSync(x_display, false); - } + XUngrabPointer(x_display, CurrentTime); + XUngrabKeyboard(x_display, CurrentTime); + XUnmapWindow(x_display, the_win); + XSync(x_display, false); } + } #endif - // Handle X events - handle_events(); + // Handle X events + handle_events(); - // Handle palette changes - pthread_mutex_lock(&palette_lock); - if (palette_changed) { - palette_changed = false; - if (depth == 8) { - XStoreColors(x_display, cmap[0], palette, 256); - XStoreColors(x_display, cmap[1], palette, 256); -#if ENABLE_DGA - if (display_type == DISPLAY_DGA) { - current_dga_cmap ^= 1; - XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); - } + // 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 } - pthread_mutex_unlock(&palette_lock); + } +#ifdef HAVE_PTHREADS + pthread_mutex_unlock(&palette_lock); +#endif - // In window mode, update display and mouse pointer - if (display_type == DISPLAY_WINDOW) { - tick_counter++; - if (tick_counter >= frame_skip) { - tick_counter = 0; - update_display(); - } + // 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(); } } +} +#endif + +#ifdef HAVE_PTHREADS +static void *redraw_func(void *arg) +{ + uint64 start = GetTicks_usec(); + int64 ticks = 0; + uint64 next = GetTicks_usec(); + while (!redraw_thread_cancel) { +// VideoRefresh(); + video_refresh(); + next += 16667; + int64 delay = next - GetTicks_usec(); + if (delay > 0) + Delay_usec(delay); + else if (delay < -16667) + next = GetTicks_usec(); + ticks++; + } + uint64 end = GetTicks_usec(); + printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks); return NULL; } +#endif