--- BasiliskII/src/Unix/video_x.cpp 2000/10/11 17:55:06 1.24 +++ BasiliskII/src/Unix/video_x.cpp 2001/01/11 16:38:48 1.34 @@ -53,7 +53,6 @@ #endif #ifdef ENABLE_VOSF -# include // log() # include # include # include @@ -123,13 +122,19 @@ static Colormap cmap[2]; // Two co static XColor black, white; static unsigned long black_pixel, white_pixel; static int eventmask; -static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask; -static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask; +static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask; +static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask; +static Atom WM_DELETE_WINDOW = (Atom)0; 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 +#define LOCK_PALETTE pthread_mutex_lock(&palette_lock) +#define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock) +#else +#define LOCK_PALETTE +#define UNLOCK_PALETTE #endif // Variables for window mode @@ -150,6 +155,11 @@ static Window suspend_win; // "Sus 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 +#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 // Variables for fbdev DGA mode @@ -169,12 +179,10 @@ static const bool use_vosf = false; #endif #ifdef ENABLE_VOSF -static uint8 * the_host_buffer; // Host frame buffer in VOSF mode +// Variables for Video on SEGV support (taken from the Win32 port) +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 }; @@ -189,37 +197,107 @@ struct ScreenInfo { 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 + char * 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) +#define PFLAG_SET_VALUE 0x00 +#define PFLAG_CLEAR_VALUE 0x01 +#define PFLAG_SET_VALUE_4 0x00000000 +#define PFLAG_CLEAR_VALUE_4 0x01010101 +#define PFLAG_SET(page) mainBuffer.dirtyPages[page] = PFLAG_SET_VALUE +#define PFLAG_CLEAR(page) mainBuffer.dirtyPages[page] = PFLAG_CLEAR_VALUE +#define PFLAG_ISSET(page) (mainBuffer.dirtyPages[page] == PFLAG_SET_VALUE) +#define PFLAG_ISCLEAR(page) (mainBuffer.dirtyPages[page] != PFLAG_SET_VALUE) + #ifdef UNALIGNED_PROFITABLE -# define PFLAG_ISCLEAR_4(page) (*((uint32 *)(mainBuffer.dirtyPages + page)) == 0) +# define PFLAG_ISSET_4(page) (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_SET_VALUE_4) +# define PFLAG_ISCLEAR_4(page) (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_CLEAR_VALUE_4) +#else +# define PFLAG_ISSET_4(page) \ + PFLAG_ISSET(page ) && PFLAG_ISSET(page+1) \ + && PFLAG_ISSET(page+2) && PFLAG_ISSET(page+3) +# define PFLAG_ISCLEAR_4(page) \ + PFLAG_ISCLEAR(page ) && PFLAG_ISCLEAR(page+1) \ + && PFLAG_ISCLEAR(page+2) && PFLAG_ISCLEAR(page+3) +#endif + +// Set the selected page range [ first_page, last_page [ into the SET state +#define PFLAG_SET_RANGE(first_page, last_page) \ + memset(mainBuffer.dirtyPages + (first_page), PFLAG_SET_VALUE, \ + (last_page) - (first_page)) + +// Set the selected page range [ first_page, last_page [ into the CLEAR state +#define PFLAG_CLEAR_RANGE(first_page, last_page) \ + memset(mainBuffer.dirtyPages + (first_page), PFLAG_CLEAR_VALUE, \ + (last_page) - (first_page)) + +#define PFLAG_SET_ALL \ + PFLAG_SET_RANGE(0, mainBuffer.pageCount) + +#define PFLAG_CLEAR_ALL \ + PFLAG_CLEAR_RANGE(0, mainBuffer.pageCount) + +// Set the following macro definition to 1 if your system +// provides a really fast strchr() implementation +//#define HAVE_FAST_STRCHR 0 + +static inline int find_next_page_set(int page) +{ +#if HAVE_FAST_STRCHR + char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_SET_VALUE); + return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount; #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) + while (PFLAG_ISCLEAR_4(page)) + page += 4; + while (PFLAG_ISCLEAR(page)) + page++; + return page; #endif -#define PFLAG_CLEAR_ALL memset(mainBuffer.dirtyPages, 0, mainBuffer.pageCount) -#define PFLAG_SET_ALL memset(mainBuffer.dirtyPages, 1, mainBuffer.pageCount) +} + +static inline int find_next_page_clear(int page) +{ +#if HAVE_FAST_STRCHR + char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_CLEAR_VALUE); + return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount; +#else + // NOTE: the loop is bound to terminate because the last + // page in mainBuffer.dirtyPages[] shall be set to CLEAR + while (PFLAG_ISSET_4(page)) + page += 4; + while (PFLAG_ISSET(page)) + page++; + return page; +#endif +} 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) +static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer (dirtyPages in fact) +#define LOCK_VOSF pthread_mutex_lock(&vosf_lock); +#define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock); +#else +#define LOCK_VOSF +#define UNLOCK_VOSF #endif -#endif +static int log_base_2(uint32 x) +{ + uint32 mask = 0x80000000; + int l = 31; + while (l >= 0 && (x & mask) == 0) { + mask >>= 1; + l--; + } + return l; +} +#endif /* ENABLE_VOSF */ // VideoRefresh function void VideoRefreshInit(void); @@ -227,7 +305,7 @@ static void (*video_refresh)(void); // Prototypes static void *redraw_func(void *arg); -static int event2keycode(XKeyEvent *ev); +static int event2keycode(XKeyEvent &ev); // From main_unix.cpp @@ -297,6 +375,60 @@ void set_video_monitor(int width, int he VideoMonitor.bytes_per_row = bytes_per_row; } +// Set window name and class +static void set_window_name(Window w, int name) +{ + const char *str = GetString(name); + XStoreName(x_display, w, str); + XSetIconName(x_display, w, str); + + XClassHint *hints; + hints = XAllocClassHint(); + if (hints) { + hints->res_name = "BasiliskII"; + hints->res_class = "BasiliskII"; + XSetClassHint(x_display, w, hints); + XFree(hints); + } +} + +// 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 void set_window_delete_protocol(Window w) +{ + WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false); + XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1); +} + +// Wait until window is mapped/unmapped +void wait_mapped(Window w) +{ + XEvent e; + do { + XMaskEvent(x_display, StructureNotifyMask, &e); + } while ((e.type != MapNotify) || (e.xmap.event != w)); +} + +void wait_unmapped(Window w) +{ + XEvent e; + do { + XMaskEvent(x_display, StructureNotifyMask, &e); + } while ((e.type != UnmapNotify) || (e.xmap.event != w)); +} + // Trap SHM errors static bool shm_error = false; static int (*old_error_handler)(Display *, XErrorEvent *); @@ -326,38 +458,38 @@ static bool init_window(int width, int h XSetWindowAttributes wattr; wattr.event_mask = eventmask = win_eventmask; wattr.background_pixel = black_pixel; - wattr.border_pixel = black_pixel; - wattr.backing_store = NotUseful; - wattr.save_under = false; - wattr.backing_planes = xdepth; + wattr.colormap = cmap[0]; - XSync(x_display, false); the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, - InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | - CWBackingStore | CWBackingPlanes, &wattr); - XSync(x_display, false); - XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); - XMapRaised(x_display, the_win); - XSync(x_display, false); + InputOutput, vis, CWEventMask | CWBackPixel | (depth == 8 ? CWColormap : 0), &wattr); - // Set colormap - if (depth == 8) { - XSetWindowColormap(x_display, the_win, cmap[0]); - XSetWMColormapWindows(x_display, the_win, &the_win, 1); - } + // 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); // Make window unresizable - XSizeHints *hints; - if ((hints = XAllocSizeHints()) != NULL) { - hints->min_width = width; - hints->max_width = width; - hints->min_height = height; - hints->max_height = height; - hints->flags = PMinSize | PMaxSize; - XSetWMNormalHints(x_display, the_win, hints); - XFree((char *)hints); + { + XSizeHints *hints = XAllocSizeHints(); + if (hints) { + hints->min_width = width; + hints->max_width = width; + hints->min_height = height; + hints->max_height = height; + hints->flags = PMinSize | PMaxSize; + XSetWMNormalHints(x_display, the_win, hints); + XFree(hints); + } } + // Show window + XMapWindow(x_display, the_win); + wait_mapped(the_win); + // Try to create and attach SHM image have_shm = false; if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) { @@ -440,9 +572,9 @@ static bool init_window(int width, int h // Set VideoMonitor bool native_byte_order; #ifdef WORDS_BIGENDIAN - native_byte_order = (img->bitmap_bit_order == MSBFirst); + native_byte_order = (XImageByteOrder(x_display) == MSBFirst); #else - native_byte_order = (img->bitmap_bit_order == LSBFirst); + native_byte_order = (XImageByteOrder(x_display) == LSBFirst); #endif #ifdef ENABLE_VOSF do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW); @@ -529,21 +661,26 @@ static bool init_fbdev_dga(char *in_fb_n // Create window XSetWindowAttributes wattr; - wattr.override_redirect = True; - wattr.backing_store = NotUseful; - wattr.background_pixel = white_pixel; - wattr.border_pixel = black_pixel; - wattr.event_mask = eventmask = dga_eventmask; + wattr.event_mask = eventmask = dga_eventmask; + wattr.background_pixel = white_pixel; + wattr.override_redirect = True; + wattr.colormap = cmap[0]; - XSync(x_display, false); the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, InputOutput, vis, - CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore, + CWEventMask | CWBackPixel | CWOverrideRedirect | (depth == 8 ? CWColormap : 0), &wattr); - XSync(x_display, false); + + // Set window name/class + set_window_name(the_win, STR_WINDOW_TITLE); + + // Indicate that we want keyboard input + set_window_focus(the_win); + + // Show window XMapRaised(x_display, the_win); - XSync(x_display, false); + wait_mapped(the_win); // Grab mouse and keyboard XGrabKeyboard(x_display, the_win, True, @@ -552,12 +689,6 @@ static bool init_fbdev_dga(char *in_fb_n 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) { @@ -637,22 +768,27 @@ static bool init_xf86_dga(int width, int } XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]); XF86VidModeSetViewPort(x_display, screen, 0, 0); + XSync(x_display, false); } #endif // Create window XSetWindowAttributes wattr; wattr.event_mask = eventmask = dga_eventmask; - wattr.border_pixel = black_pixel; wattr.override_redirect = True; - XSync(x_display, false); the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, - InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr); - XSync(x_display, false); - XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); + InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr); + + // Set window name/class + set_window_name(the_win, STR_WINDOW_TITLE); + + // Indicate that we want keyboard input + set_window_focus(the_win); + + // Show window XMapRaised(x_display, the_win); - XSync(x_display, false); + wait_mapped(the_win); // Establish direct screen connection XMoveResizeWindow(x_display, the_win, 0, 0, width, height); @@ -669,9 +805,9 @@ static bool init_xf86_dga(int width, int // Set colormap if (depth == 8) { XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]); - XSetWMColormapWindows(x_display, the_win, &the_win, 1); XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); } + XSync(x_display, false); // Set VideoMonitor int bytes_per_row = (v_width + 7) & ~7; @@ -804,12 +940,12 @@ bool VideoInitBuffer() mainBuffer.pageSize = page_size; mainBuffer.pageCount = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize; - mainBuffer.pageBits = int( log(mainBuffer.pageSize) / log(2.0) ); + mainBuffer.pageBits = log_base_2(mainBuffer.pageSize); if (mainBuffer.dirtyPages != 0) free(mainBuffer.dirtyPages); - mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount); + mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 1); if (mainBuffer.pageInfo != 0) free(mainBuffer.pageInfo); @@ -820,6 +956,12 @@ bool VideoInitBuffer() return false; PFLAG_CLEAR_ALL; + + // Make sure there is at least one page marked, so the + // loops in the update routine will terminate + // gb-- Set the last page as cleared because the update + // routine finally searches for a page that was not touched + PFLAG_CLEAR(mainBuffer.pageCount); uint32 a = 0; for (int i = 0; i < mainBuffer.pageCount; i++) { @@ -872,8 +1014,8 @@ bool VideoInit(bool classic) keycode_init(); // Read prefs - mouse_wheel_mode = PrefsFindInt16("mousewheelmode"); - mouse_wheel_lines = PrefsFindInt16("mousewheellines"); + mouse_wheel_mode = PrefsFindInt32("mousewheelmode"); + mouse_wheel_lines = PrefsFindInt32("mousewheellines"); // Find screen and root window screen = XDefaultScreen(x_display); @@ -1010,10 +1152,8 @@ bool VideoInit(bool classic) break; } -#ifdef HAVE_PTHREADS // Lock down frame buffer - pthread_mutex_lock(&frame_buffer_lock); -#endif + LOCK_FRAME_BUFFER; #if !REAL_ADDRESSING && !DIRECT_ADDRESSING // Set variables for UAE memory mapping @@ -1078,10 +1218,8 @@ void VideoExit(void) } #endif -#ifdef HAVE_PTHREADS // Unlock frame buffer - pthread_mutex_unlock(&frame_buffer_lock); -#endif + UNLOCK_FRAME_BUFFER; // Close window and server connection if (x_display != NULL) { @@ -1184,12 +1322,10 @@ void VideoInterrupt(void) if (emerg_quit) QuitEmulator(); -#ifdef HAVE_PTHREADS // Temporarily give up frame buffer lock (this is the point where // we are suspended when the user presses Ctrl-Tab) - pthread_mutex_unlock(&frame_buffer_lock); - pthread_mutex_lock(&frame_buffer_lock); -#endif + UNLOCK_FRAME_BUFFER; + LOCK_FRAME_BUFFER; } @@ -1199,9 +1335,7 @@ void VideoInterrupt(void) void video_set_palette(uint8 *pal) { -#ifdef HAVE_PTHREDS - pthread_mutex_lock(&palette_lock); -#endif + LOCK_PALETTE; // Convert colors to XColor array for (int i=0; i<256; i++) { @@ -1215,9 +1349,7 @@ void video_set_palette(uint8 *pal) // Tell redraw thread to change palette palette_changed = true; -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&palette_lock); -#endif + UNLOCK_PALETTE; } @@ -1233,10 +1365,8 @@ 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 + LOCK_FRAME_BUFFER; // Save frame buffer fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row); @@ -1250,25 +1380,18 @@ static void suspend_emul(void) XUngrabPointer(x_display, CurrentTime); XUngrabKeyboard(x_display, CurrentTime); XUnmapWindow(x_display, the_win); - XSync(x_display, false); + wait_unmapped(the_win); // Open "suspend" window XSetWindowAttributes wattr; wattr.event_mask = KeyPressMask; wattr.background_pixel = black_pixel; - wattr.border_pixel = black_pixel; - wattr.backing_store = Always; - wattr.backing_planes = xdepth; - wattr.colormap = DefaultColormap(x_display, screen); - XSync(x_display, false); suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth, - InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | - CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr); - XSync(x_display, false); - XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE)); - XMapRaised(x_display, suspend_win); - XSync(x_display, false); + InputOutput, vis, CWEventMask | CWBackPixel, &wattr); + set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE); + set_window_focus(suspend_win); + XMapWindow(x_display, suspend_win); emul_suspended = true; } } @@ -1281,8 +1404,8 @@ static void resume_emul(void) // Reopen full screen display XMapRaised(x_display, the_win); + wait_mapped(the_win); XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); - XSync(x_display, false); XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime); XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); #ifdef ENABLE_XF86_DGA @@ -1296,13 +1419,9 @@ static void resume_emul(void) // not necessary. #ifdef ENABLE_VOSF if (use_vosf) { -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&Screen_draw_lock); -#endif + LOCK_VOSF; PFLAG_SET_ALL; -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&Screen_draw_lock); -#endif + UNLOCK_VOSF; memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); } #endif @@ -1323,11 +1442,9 @@ static void resume_emul(void) 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); + UNLOCK_FRAME_BUFFER; emul_suspended = false; -#endif } #endif @@ -1481,14 +1598,14 @@ static int kc_decode(KeySym ks) return -1; } -static int event2keycode(XKeyEvent *ev) +static int event2keycode(XKeyEvent &ev) { KeySym ks; int as; int i = 0; do { - ks = XLookupKeysym(ev, i++); + ks = XLookupKeysym(&ev, i++); as = kc_decode(ks); if (as != -1) return as; @@ -1504,15 +1621,14 @@ static int event2keycode(XKeyEvent *ev) static void handle_events(void) { - XEvent event; - for (;;) { - if (!XCheckMaskEvent(x_display, eventmask, &event)) - break; + while (XPending(x_display)) { + XEvent event; + XNextEvent(x_display, &event); switch (event.type) { // Mouse button case ButtonPress: { - unsigned int button = ((XButtonEvent *)&event)->button; + unsigned int button = event.xbutton.button; if (button < 4) ADBMouseDown(button - 1); else if (button < 6) { // Wheel mouse @@ -1531,7 +1647,7 @@ static void handle_events(void) break; } case ButtonRelease: { - unsigned int button = ((XButtonEvent *)&event)->button; + unsigned int button = event.xbutton.button; if (button < 4) ADBMouseUp(button - 1); break; @@ -1539,20 +1655,18 @@ static void handle_events(void) // Mouse moved case EnterNotify: - ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); - break; case MotionNotify: - ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); + ADBMouseMoved(event.xmotion.x, event.xmotion.y); break; // Keyboard case KeyPress: { int code; if (use_keycodes) { - event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys - code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff]; + event2keycode(event.xkey); // This is called to process the hotkeys + code = keycode_table[event.xkey.keycode & 0xff]; } else - code = event2keycode((XKeyEvent *)&event); + code = event2keycode(event.xkey); if (code != -1) { if (!emul_suspended) { if (code == 0x39) { // Caps Lock pressed @@ -1579,10 +1693,10 @@ static void handle_events(void) case KeyRelease: { int code; if (use_keycodes) { - event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys - code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff]; + event2keycode(event.xkey); // This is called to process the hotkeys + code = keycode_table[event.xkey.keycode & 0xff]; } else - code = event2keycode((XKeyEvent *)&event); + code = event2keycode(event.xkey); if (code != -1 && code != 0x39) { // Don't propagate Caps Lock releases ADBKeyUp(code); if (code == 0x36) @@ -1596,13 +1710,9 @@ static void handle_events(void) if (display_type == DISPLAY_WINDOW) { #ifdef ENABLE_VOSF if (use_vosf) { // VOSF refresh -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&Screen_draw_lock); -#endif + LOCK_VOSF; PFLAG_SET_ALL; -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&Screen_draw_lock); -#endif + UNLOCK_VOSF; memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); } else @@ -1617,6 +1727,14 @@ static void handle_events(void) memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.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); + } + break; } } } @@ -1766,12 +1884,12 @@ static void update_display_static(void) for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) { p--; p2--; if (*p != *p2) { - x2 = i << 3; + x2 = (i << 3) + 7; break; } } } - wide = x2 - x1; + wide = x2 - x1 + 1; // Update copy of the_buffer if (high && wide) { @@ -1835,24 +1953,11 @@ static void update_display_static(void) * Screen refresh functions */ -// The specialisations hereunder are meant to enable VOSF with DGA in direct -// addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could -// not get mapped correctly with respect to the predetermined host frame -// buffer base address. -// -// Hmm, in other words, when in direct addressing mode and DGA is requested, -// we first try to "triple allocate" the address spaces according to the real -// host frame buffer address. Then, if it fails, we will use a temporary -// frame buffer thus making the real host frame buffer updated when pages -// of the temp frame buffer are altered. -// -// As a side effect, a little speed gain in screen updates could be noticed -// for other modes than DGA. -// -// The following two functions below are inline so that a clever compiler -// could specialise the code according to the current screen depth and -// display type. A more clever compiler would the job by itself though... -// (update_display_vosf is inlined as well) +// We suggest the compiler to inline the next two functions so that it +// may specialise the code according to the current screen depth and +// display type. A clever compiler would that job by itself though... + +// NOTE: update_display_vosf is inlined too static inline void possibly_quit_dga_mode() { @@ -1873,14 +1978,14 @@ static inline void possibly_quit_dga_mod static inline void handle_palette_changes(int depth, int display_type) { -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&palette_lock); -#endif + LOCK_PALETTE; + if (palette_changed) { palette_changed = false; if (depth == 8) { XStoreColors(x_display, cmap[0], palette, 256); XStoreColors(x_display, cmap[1], palette, 256); + XSync(x_display, false); #ifdef ENABLE_XF86_DGA if (display_type == DISPLAY_DGA) { @@ -1890,9 +1995,8 @@ static inline void handle_palette_change #endif } } -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&palette_lock); -#endif + + UNLOCK_PALETTE; } static void video_refresh_dga(void) @@ -1924,13 +2028,9 @@ static void video_refresh_dga_vosf(void) static int tick_counter = 0; if (++tick_counter >= frame_skip) { tick_counter = 0; -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&Screen_draw_lock); -#endif + LOCK_VOSF; update_display_dga_vosf(); -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&Screen_draw_lock); -#endif + UNLOCK_VOSF; } } #endif @@ -1950,13 +2050,9 @@ static void video_refresh_window_vosf(vo static int tick_counter = 0; if (++tick_counter >= frame_skip) { tick_counter = 0; -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&Screen_draw_lock); -#endif + LOCK_VOSF; update_display_window_vosf(); -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&Screen_draw_lock); -#endif + UNLOCK_VOSF; } } #endif // def ENABLE_VOSF @@ -2026,64 +2122,6 @@ void VideoRefresh(void) video_refresh(); } -#if 0 -void VideoRefresh(void) -{ -#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) - // Quit DGA mode if requested - if (quit_full_screen) { - quit_full_screen = false; - if (display_type == DISPLAY_DGA) { -#ifdef ENABLE_XF86_DGA - XF86DGADirectVideo(x_display, screen, 0); -#endif - XUngrabPointer(x_display, CurrentTime); - XUngrabKeyboard(x_display, CurrentTime); - XUnmapWindow(x_display, the_win); - XSync(x_display, false); - } - } -#endif - - // Handle X events - handle_events(); - - // Handle palette changes -#ifdef HAVE_PTHREADS - pthread_mutex_lock(&palette_lock); -#endif - if (palette_changed) { - palette_changed = false; - if (depth == 8) { - XStoreColors(x_display, cmap[0], palette, 256); - XStoreColors(x_display, cmap[1], palette, 256); - -#ifdef ENABLE_XF86_DGA - if (display_type == DISPLAY_DGA) { - current_dga_cmap ^= 1; - XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); - } -#endif - } - } -#ifdef HAVE_PTHREADS - pthread_mutex_unlock(&palette_lock); -#endif - - // In window mode, update display - static int tick_counter = 0; - if (display_type == DISPLAY_WINDOW) { - tick_counter++; - if (frame_skip == 0) - update_display_dynamic(tick_counter); - else if (tick_counter >= frame_skip) { - tick_counter = 0; - update_display_static(); - } - } -} -#endif - #ifdef HAVE_PTHREADS static void *redraw_func(void *arg) { @@ -2091,7 +2129,6 @@ static void *redraw_func(void *arg) int64 ticks = 0; uint64 next = GetTicks_usec(); while (!redraw_thread_cancel) { -// VideoRefresh(); video_refresh(); next += 16667; int64 delay = next - GetTicks_usec(); @@ -2102,7 +2139,7 @@ static void *redraw_func(void *arg) ticks++; } uint64 end = GetTicks_usec(); - printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks); + printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start)); return NULL; } #endif