ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
(Generate patch)

Comparing BasiliskII/src/Unix/video_x.cpp (file contents):
Revision 1.55 by cebix, 2001-07-06T22:37:23Z vs.
Revision 1.60 by cebix, 2001-07-11T19:26:14Z

# Line 80 | Line 80 | enum {
80   // Constants
81   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
82  
83 < static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | StructureNotifyMask;
83 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
84   static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
85  
86  
# Line 93 | Line 93 | static int display_type = DISPLAY_WINDOW
93   static bool local_X11;                                                          // Flag: X server running on local machine?
94   static uint8 *the_buffer = NULL;                                        // Mac frame buffer (where MacOS draws into)
95   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer (for refreshed modes)
96 + static uint32 the_buffer_size;                                          // Size of allocated the_buffer
97  
98   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
99   #ifdef HAVE_PTHREADS
# Line 183 | Line 184 | static void (*video_refresh)(void);
184  
185   // Prototypes
186   static void *redraw_func(void *arg);
186 static int event2keycode(XKeyEvent &ev);
187  
188   // From main_unix.cpp
189   extern char *x_display_name;
# Line 212 | Line 212 | static bool find_visual_for_depth(video_
212          // Calculate minimum and maximum supported X depth
213          int min_depth = 1, max_depth = 32;
214          switch (depth) {
215 <                case VDEPTH_1BIT:       // 1-bit works always
216 <                        min_depth = 1;
217 <                        max_depth = 32;
215 >                case VDEPTH_1BIT:       // 1-bit works always and uses default visual
216 >                        min_depth = max_depth = DefaultDepth(x_display, screen);
217                          break;
218   #ifdef ENABLE_VOSF
219                  case VDEPTH_2BIT:
# Line 293 | Line 292 | static bool find_visual_for_depth(video_
292                  case DirectColor: D(bug("DirectColor\n")); break;
293          }
294   #endif
295 +        return true;
296   }
297  
298   // Add mode to list of supported modes
# Line 428 | Line 428 | public:
428          virtual void toggle_mouse_grab(void) {}
429          virtual void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
430  
431 +        void disable_mouse_accel(void);
432 +        void restore_mouse_accel(void);
433 +
434          virtual void grab_mouse(void) {}
435          virtual void ungrab_mouse(void) {}
436  
437   public:
438          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
439          Window w;               // The window we draw into
440 +
441 +        int orig_accel_numer, orig_accel_denom, orig_threshold; // Original mouse acceleration
442   };
443  
444   class driver_window;
# Line 477 | Line 482 | driver_base::driver_base()
482   {
483          the_buffer = NULL;
484          the_buffer_copy = NULL;
485 +        XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
486   }
487  
488   driver_base::~driver_base()
489   {
490          ungrab_mouse();
491 +        restore_mouse_accel();
492  
493          if (w) {
494                  XUnmapWindow(x_display, w);
# Line 509 | Line 516 | driver_base::~driver_base()
516                          free(the_host_buffer);
517                          the_host_buffer = NULL;
518                  }
519 <                if (the_buffer != (uint8 *)VM_MAP_FAILED) {
520 <                        vm_release(the_buffer, the_buffer_size);
519 >                if (the_buffer) {
520 >                        free(the_buffer);
521                          the_buffer = NULL;
522                  }
523 <                if (the_buffer_copy != (uint8 *)VM_MAP_FAILED) {
524 <                        vm_release(the_buffer_copy, the_buffer_size);
523 >                if (the_buffer_copy) {
524 >                        free(the_buffer_copy);
525                          the_buffer_copy = NULL;
526                  }
527          }
# Line 524 | Line 531 | driver_base::~driver_base()
531   // Palette has changed
532   void driver_base::update_palette(void)
533   {
534 <        if (cmap[0] && cmap[1]) {
534 >        if (color_class == PseudoColor || color_class == DirectColor) {
535                  int num = vis->map_entries;
536                  if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor)
537                          return; // Indexed mode on true color screen, don't set CLUT
# Line 534 | Line 541 | void driver_base::update_palette(void)
541          XSync(x_display, false);
542   }
543  
544 + // Disable mouse acceleration
545 + void driver_base::disable_mouse_accel(void)
546 + {
547 +        XChangePointerControl(x_display, True, False, 1, 1, 0);
548 + }
549 +
550 + // Restore mouse acceleration to original value
551 + void driver_base::restore_mouse_accel(void)
552 + {
553 +        XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold);
554 + }
555 +
556  
557   /*
558   *  Windowed display driver
# Line 541 | Line 560 | void driver_base::update_palette(void)
560  
561   // Open display
562   driver_window::driver_window(const video_mode &mode)
563 < : gc(0), img(NULL), have_shm(false), mouse_grabbed(false), mac_cursor(0)
563 > : gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
564   {
565          int width = mode.x, height = mode.y;
566          int aligned_width = (width + 15) & ~15;
# Line 550 | Line 569 | driver_window::driver_window(const video
569          // Set absolute mouse mode
570          ADBSetRelMouseMode(mouse_grabbed);
571  
572 <        // Create window
572 >        // Create window (setting backround_pixel, border_pixel and colormap is
573 >        // mandatory when using a non-default visual; in 1-bit mode we use the
574 >        // default visual, so we can also use the default colormap)
575          XSetWindowAttributes wattr;
576          wattr.event_mask = eventmask = win_eventmask;
577 <        wattr.background_pixel = black_pixel;
578 <        wattr.colormap = (mode.depth == VDEPTH_1BIT && color_class == PseudoColor ? DefaultColormap(x_display, screen) : cmap[0]);
577 >        wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
578 >        wattr.border_pixel = 0;
579 >        wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
580          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
581 <                InputOutput, vis, CWEventMask | CWBackPixel | (color_class == PseudoColor || color_class == DirectColor ? CWColormap : 0), &wattr);
581 >                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &wattr);
582  
583          // Set window name/class
584          set_window_name(w, STR_WINDOW_TITLE);
# Line 629 | Line 651 | driver_window::driver_window(const video
651          }
652  
653   #ifdef ENABLE_VOSF
654 +        use_vosf = true;
655          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
656          the_host_buffer = the_buffer_copy;
657          the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
# Line 682 | Line 705 | driver_window::~driver_window()
705                  the_buffer_copy = NULL; // don't free() in driver_base dtor
706   #endif
707          }
708 + #ifdef ENABLE_VOSF
709 +        if (use_vosf) {
710 +                // don't free() memory mapped buffers in driver_base dtor
711 +                if (the_buffer != VM_MAP_FAILED) {
712 +                        vm_release(the_buffer, the_buffer_size);
713 +                        the_buffer = NULL;
714 +                }
715 +                if (the_buffer_copy != VM_MAP_FAILED) {
716 +                        vm_release(the_buffer_copy, the_buffer_size);
717 +                        the_buffer_copy = NULL;
718 +                }
719 +        }
720 + #endif
721          if (img)
722                  XDestroyImage(img);
723          if (have_shm) {
# Line 713 | Line 749 | void driver_window::grab_mouse(void)
749                  Delay_usec(100000);
750          }
751          if (result == GrabSuccess) {
716                ADBSetRelMouseMode(mouse_grabbed = true);
752                  XStoreName(x_display, w, GetString(STR_WINDOW_TITLE_GRABBED));
753 <                XSync(x_display, false);
753 >                ADBSetRelMouseMode(mouse_grabbed = true);
754 >                disable_mouse_accel();
755          }
756   }
757  
# Line 726 | Line 762 | void driver_window::ungrab_mouse(void)
762                  XUngrabPointer(x_display, CurrentTime);
763                  XStoreName(x_display, w, GetString(STR_WINDOW_TITLE));
764                  ADBSetRelMouseMode(mouse_grabbed = false);
765 +                restore_mouse_accel();
766          }
767   }
768  
# Line 821 | Line 858 | void driver_dga::suspend(void)
858   #endif
859          XUngrabPointer(x_display, CurrentTime);
860          XUngrabKeyboard(x_display, CurrentTime);
861 +        restore_mouse_accel();
862          XUnmapWindow(x_display, w);
863          wait_unmapped(w);
864  
# Line 850 | Line 888 | void driver_dga::resume(void)
888          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
889          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
890          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
891 +        disable_mouse_accel();
892   #ifdef ENABLE_XF86_DGA
893          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
894          XF86DGASetViewPort(x_display, screen, 0, 0);
# Line 996 | Line 1035 | driver_fbdev::driver_fbdev(const video_m
1035          XGrabPointer(x_display, w, True,
1036                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1037                  GrabModeAsync, GrabModeAsync, w, None, CurrentTime);
1038 +        disable_mouse_accel();
1039          
1040          // Calculate bytes per row
1041          int bytes_per_row = TrivialBytesPerRow(mode.x, mode.depth);
1042          
1043          // Map frame buffer
1044 <        if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1045 <                if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1044 >        the_buffer_size = height * bytes_per_row;
1045 >        if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1046 >                if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1047                          char str[256];
1048                          sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
1049                          ErrorAlert(str);
# Line 1041 | Line 1082 | driver_fbdev::driver_fbdev(const video_m
1082   // Close display
1083   driver_fbdev::~driver_fbdev()
1084   {
1085 +        if (!use_vosf) {
1086 +                if (the_buffer != MAP_FAILED) {
1087 +                        // don't free() the screen buffer in driver_base dtor
1088 +                        munmap(the_buffer, the_buffer_size);
1089 +                        the_buffer = NULL;
1090 +                }
1091 +        }
1092 + #ifdef ENABLE_VOSF
1093 +        else {
1094 +                if (the_host_buffer != MAP_FAILED) {
1095 +                        // don't free() the screen buffer in driver_base dtor
1096 +                        munmap(the_host_buffer, the_buffer_size);
1097 +                        the_host_buffer = NULL;
1098 +                }
1099 +                if (the_buffer_copy != VM_MAP_FAILED) {
1100 +                        vm_release(the_buffer_copy, the_buffer_size);
1101 +                        the_buffer_copy = NULL;
1102 +                }
1103 +                if (the_buffer != VM_MAP_FAILED) {
1104 +                        vm_release(the_buffer, the_buffer_size);
1105 +                        the_buffer = NULL;
1106 +                }
1107 +        }
1108 + #endif
1109   }
1110   #endif
1111  
# Line 1110 | Line 1175 | driver_xf86dga::driver_xf86dga(const vid
1175          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1176          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1177          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1178 +        disable_mouse_accel();
1179  
1180          int v_width, v_bank, v_size;
1181          XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
# Line 1126 | Line 1192 | driver_xf86dga::driver_xf86dga(const vid
1192  
1193          // Init blitting routines
1194          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
1195 < #ifdef VIDEO_VOSF
1195 > #if VIDEO_VOSF
1196   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1197          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1198          // i.e. the framebuffer update function is not Blit_Copy_Raw
# Line 1157 | Line 1223 | driver_xf86dga::driver_xf86dga(const vid
1223   driver_xf86dga::~driver_xf86dga()
1224   {
1225          XF86DGADirectVideo(x_display, screen, 0);
1226 +        if (!use_vosf) {
1227 +                // don't free() the screen buffer in driver_base dtor
1228 +                the_buffer = NULL;
1229 +        }
1230 + #ifdef ENABLE_VOSF
1231 +        else {
1232 +                // don't free() the screen buffer in driver_base dtor
1233 +                the_host_buffer = NULL;
1234 +                
1235 +                if (the_buffer_copy != VM_MAP_FAILED) {
1236 +                        vm_release(the_buffer_copy, the_buffer_size);
1237 +                        the_buffer_copy = NULL;
1238 +                }
1239 +                if (the_buffer != VM_MAP_FAILED) {
1240 +                        vm_release(the_buffer, the_buffer_size);
1241 +                        the_buffer = NULL;
1242 +                }
1243 +        }
1244 + #endif
1245   #ifdef ENABLE_XF86_VIDMODE
1246          if (has_vidmode)
1247                  XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
# Line 1264 | Line 1349 | static bool video_open(const video_mode
1349          if (color_class == PseudoColor || color_class == DirectColor) {
1350                  cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1351                  cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1352 +        } else {
1353 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
1354 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
1355          }
1356  
1357          // Find pixel format of direct modes
# Line 1285 | Line 1373 | static bool video_open(const video_mode
1373                          --bloss;
1374          }
1375  
1376 <        // Preset palette pixel values for gamma table
1376 >        // Preset palette pixel values for CLUT or gamma table
1377          if (color_class == DirectColor) {
1378                  int num = vis->map_entries;
1379                  for (int i=0; i<num; i++) {
1380                          int c = (i * 256) / num;
1381                          palette[i].pixel = map_rgb(c, c, c);
1382 +                        palette[i].flags = DoRed | DoGreen | DoBlue;
1383 +                }
1384 +        } else if (color_class == PseudoColor) {
1385 +                for (int i=0; i<256; i++) {
1386 +                        palette[i].pixel = i;
1387 +                        palette[i].flags = DoRed | DoGreen | DoBlue;
1388                  }
1389          }
1390  
# Line 1301 | Line 1395 | static bool video_open(const video_mode
1395                  palette[i].red = c * 0x0101;
1396                  palette[i].green = c * 0x0101;
1397                  palette[i].blue = c * 0x0101;
1304                if (color_class == PseudoColor)
1305                        palette[i].pixel = i;
1306                palette[i].flags = DoRed | DoGreen | DoBlue;
1398          }
1399 <        if (cmap[0] && cmap[1]) {
1399 >        if (color_class == PseudoColor || color_class == DirectColor) {
1400                  XStoreColors(x_display, cmap[0], palette, num);
1401                  XStoreColors(x_display, cmap[1], palette, num);
1402          }
1403  
1404   #ifdef ENABLE_VOSF
1405          // Load gray ramp to 8->16/32 expand map
1406 <        if (!IsDirectMode(mode) && (color_class == TrueColor || color_class == DirectColor))
1406 >        if (!IsDirectMode(mode) && xdepth > 8)
1407                  for (int i=0; i<256; i++)
1408                          ExpandMap[i] = map_rgb(i, i, i);
1409   #endif
# Line 1343 | Line 1434 | static bool video_open(const video_mode
1434  
1435   #ifdef ENABLE_VOSF
1436          if (use_vosf) {
1437 <                // Initialize the mainBuffer structure
1438 <                if (!video_init_buffer()) {
1437 >                // Initialize the VOSF system
1438 >                if (!video_vosf_init()) {
1439                          ErrorAlert(STR_VOSF_INIT_ERR);
1440                  return false;
1441                  }
1351
1352                // Initialize the handler for SIGSEGV
1353                if (!sigsegv_install_handler(screen_fault_handler)) {
1354                        ErrorAlert("Could not initialize Video on SEGV signals");
1355                        return false;
1356                }
1442          }
1443   #endif
1444          
# Line 1569 | Line 1654 | static void video_close(void)
1654          XSync(x_display, false);
1655  
1656   #ifdef ENABLE_VOSF
1572        // Deinitialize VOSF
1657          if (use_vosf) {
1658 <                if (mainBuffer.pageInfo) {
1659 <                        free(mainBuffer.pageInfo);
1576 <                        mainBuffer.pageInfo = NULL;
1577 <                }
1578 <                if (mainBuffer.dirtyPages) {
1579 <                        free(mainBuffer.dirtyPages);
1580 <                        mainBuffer.dirtyPages = NULL;
1581 <                }
1658 >                // Deinitialize VOSF
1659 >                video_vosf_exit();
1660          }
1661   #endif
1662  
# Line 1676 | Line 1754 | void video_set_palette(uint8 *pal, int n
1754                  p->red = pal[c*3 + 0] * 0x0101;
1755                  p->green = pal[c*3 + 1] * 0x0101;
1756                  p->blue = pal[c*3 + 2] * 0x0101;
1679                if (color_class == PseudoColor)
1680                        p->pixel = i;
1681                p->flags = DoRed | DoGreen | DoBlue;
1757                  p++;
1758          }
1759  
1760   #ifdef ENABLE_VOSF
1761          // Recalculate pixel color expansion map
1762 <        if (!IsDirectMode(VideoMonitor.mode) && (color_class == TrueColor || color_class == DirectColor)) {
1762 >        if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) {
1763                  for (int i=0; i<256; i++) {
1764                          int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1765                          ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
# Line 1897 | Line 1972 | static void handle_events(void)
1972                  XNextEvent(x_display, &event);
1973  
1974                  switch (event.type) {
1975 +
1976                          // Mouse button
1977                          case ButtonPress: {
1978                                  unsigned int button = event.xbutton.button;
# Line 1929 | Line 2005 | static void handle_events(void)
2005                                  drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2006                                  break;
2007  
2008 +                        // Mouse entered window
2009 +                        case EnterNotify:
2010 +                                if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
2011 +                                        drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2012 +                                break;
2013 +
2014                          // Keyboard
2015                          case KeyPress: {
2016                                  int code = -1;
# Line 2106 | Line 2188 | static void update_display_dynamic(int t
2188   static void update_display_static(driver_window *drv)
2189   {
2190          // Incremental update code
2191 <        int wide = 0, high = 0, x1, x2, y1, y2, i, j;
2191 >        unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
2192          int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2193          int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2194          uint8 *p, *p2;
# Line 2265 | Line 2347 | static void video_refresh_dga(void)
2347   {
2348          // Quit DGA mode if requested
2349          possibly_quit_dga_mode();
2268        
2269        // Handle X events
2270        handle_events();
2271        
2272        // Handle palette changes
2273        handle_palette_changes();
2350   }
2351  
2352   #ifdef ENABLE_VOSF
# Line 2280 | Line 2356 | static void video_refresh_dga_vosf(void)
2356          // Quit DGA mode if requested
2357          possibly_quit_dga_mode();
2358          
2283        // Handle X events
2284        handle_events();
2285        
2286        // Handle palette changes
2287        handle_palette_changes();
2288        
2359          // Update display (VOSF variant)
2360          static int tick_counter = 0;
2361          if (++tick_counter >= frame_skip) {
# Line 2304 | Line 2374 | static void video_refresh_window_vosf(vo
2374          // Ungrab mouse if requested
2375          possibly_ungrab_mouse();
2376          
2307        // Handle X events
2308        handle_events();
2309        
2310        // Handle palette changes
2311        handle_palette_changes();
2312        
2377          // Update display (VOSF variant)
2378          static int tick_counter = 0;
2379          if (++tick_counter >= frame_skip) {
# Line 2329 | Line 2393 | static void video_refresh_window_static(
2393          // Ungrab mouse if requested
2394          possibly_ungrab_mouse();
2395  
2332        // Handle X events
2333        handle_events();
2334        
2335        // Handle_palette changes
2336        handle_palette_changes();
2337        
2396          // Update display (static variant)
2397          static int tick_counter = 0;
2398          if (++tick_counter >= frame_skip) {
# Line 2348 | Line 2406 | static void video_refresh_window_dynamic
2406          // Ungrab mouse if requested
2407          possibly_ungrab_mouse();
2408  
2351        // Handle X events
2352        handle_events();
2353        
2354        // Handle_palette changes
2355        handle_palette_changes();
2356        
2409          // Update display (dynamic variant)
2410          static int tick_counter = 0;
2411          tick_counter++;
# Line 2389 | Line 2441 | static void VideoRefreshInit(void)
2441          }
2442   }
2443  
2444 + // This function is called on non-threaded platforms from a timer interrupt
2445   void VideoRefresh(void)
2446   {
2447          // We need to check redraw_thread_active to inhibit refreshed during
2448          // mode changes on non-threaded platforms
2449 <        if (redraw_thread_active)
2450 <                video_refresh();
2449 >        if (!redraw_thread_active)
2450 >                return;
2451 >
2452 >        // Handle X events
2453 >        handle_events();
2454 >
2455 >        // Handle palette changes
2456 >        handle_palette_changes();
2457 >
2458 >        // Update display
2459 >        video_refresh();
2460   }
2461  
2462 + const int VIDEO_REFRESH_HZ = 60;
2463 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2464 +
2465   #ifdef HAVE_PTHREADS
2466   static void *redraw_func(void *arg)
2467   {
2468 +        int fd = ConnectionNumber(x_display);
2469 +
2470          uint64 start = GetTicks_usec();
2471          int64 ticks = 0;
2472 <        uint64 next = GetTicks_usec();
2472 >        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2473 >
2474          while (!redraw_thread_cancel) {
2475 <                video_refresh();
2408 <                next += 16667;
2475 >
2476                  int64 delay = next - GetTicks_usec();
2477 <                if (delay > 0)
2478 <                        Delay_usec(delay);
2479 <                else if (delay < -16667)
2477 >                if (delay < -VIDEO_REFRESH_DELAY) {
2478 >
2479 >                        // We are lagging far behind, so we reset the delay mechanism
2480                          next = GetTicks_usec();
2481 <                ticks++;
2481 >
2482 >                } else if (delay <= 0) {
2483 >
2484 >                        // Delay expired, refresh display
2485 >                        handle_events();
2486 >                        handle_palette_changes();
2487 >                        video_refresh();
2488 >                        next += VIDEO_REFRESH_DELAY;
2489 >                        ticks++;
2490 >
2491 >                } else {
2492 >
2493 >                        // No display refresh pending, check for X events
2494 >                        fd_set readfds;
2495 >                        FD_ZERO(&readfds);
2496 >                        FD_SET(fd, &readfds);
2497 >                        struct timeval timeout;
2498 >                        timeout.tv_sec = 0;
2499 >                        timeout.tv_usec = delay;
2500 >                        if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2501 >                                handle_events();
2502 >                }
2503          }
2504 +
2505          uint64 end = GetTicks_usec();
2506 <        // printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
2506 >        D(bug("%Ld refreshes in %Ld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2507          return NULL;
2508   }
2509   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines