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.53 by cebix, 2001-07-06T20:49:53Z vs.
Revision 1.57 by cebix, 2001-07-09T11:22:00Z

# 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 218 | Line 219 | static bool find_visual_for_depth(video_
219                          break;
220   #ifdef ENABLE_VOSF
221                  case VDEPTH_2BIT:
222 <                case VDEPTH_4BIT:       // VOSF blitters can convert 2/4-bit -> 16/32-bit
223 <                        min_depth = 15;
223 <                        max_depth = 32;
224 <                        break;
225 <                case VDEPTH_8BIT:       // VOSF blitters can convert 8-bit -> 16/32-bit
222 >                case VDEPTH_4BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
223 >                case VDEPTH_8BIT:
224                          min_depth = 8;
225                          max_depth = 32;
226                          break;
# Line 343 | Line 341 | static void set_mac_frame_buffer(video_d
341          InitFrameBufferMapping();
342   #else
343          VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
346        D(bug("Host frame buffer = %p, ", the_buffer));
344   #endif
345          D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base));
346   }
# Line 432 | Line 429 | public:
429          virtual void toggle_mouse_grab(void) {}
430          virtual void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
431  
432 +        void disable_mouse_accel(void);
433 +        void restore_mouse_accel(void);
434 +
435          virtual void grab_mouse(void) {}
436          virtual void ungrab_mouse(void) {}
437  
438   public:
439          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
440          Window w;               // The window we draw into
441 +
442 +        int orig_accel_numer, orig_accel_denom, orig_threshold; // Original mouse acceleration
443   };
444  
445   class driver_window;
# Line 481 | Line 483 | driver_base::driver_base()
483   {
484          the_buffer = NULL;
485          the_buffer_copy = NULL;
486 +        XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
487   }
488  
489   driver_base::~driver_base()
490   {
491          ungrab_mouse();
492 +        restore_mouse_accel();
493  
494          if (w) {
495                  XUnmapWindow(x_display, w);
# Line 509 | Line 513 | driver_base::~driver_base()
513          }
514   #ifdef ENABLE_VOSF
515          else {
516 <                if (the_buffer != (uint8 *)VM_MAP_FAILED) {
517 <                        vm_release(the_buffer, the_buffer_size);
516 >                if (the_host_buffer) {
517 >                        free(the_host_buffer);
518 >                        the_host_buffer = NULL;
519 >                }
520 >                if (the_buffer) {
521 >                        free(the_buffer);
522                          the_buffer = NULL;
523                  }
524 <                if (the_buffer_copy != (uint8 *)VM_MAP_FAILED) {
525 <                        vm_release(the_buffer_copy, the_buffer_size);
524 >                if (the_buffer_copy) {
525 >                        free(the_buffer_copy);
526                          the_buffer_copy = NULL;
527                  }
528          }
# Line 525 | Line 533 | driver_base::~driver_base()
533   void driver_base::update_palette(void)
534   {
535          if (cmap[0] && cmap[1]) {
536 <                int num = 256;
537 <                if (IsDirectMode(VideoMonitor.mode))
530 <                        num = vis->map_entries; // Palette is gamma table
531 <                else if (color_class == DirectColor)
536 >                int num = vis->map_entries;
537 >                if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor)
538                          return; // Indexed mode on true color screen, don't set CLUT
539                  XStoreColors(x_display, cmap[0], palette, num);
540                  XStoreColors(x_display, cmap[1], palette, num);
# Line 536 | Line 542 | void driver_base::update_palette(void)
542          XSync(x_display, false);
543   }
544  
545 + // Disable mouse acceleration
546 + void driver_base::disable_mouse_accel(void)
547 + {
548 +        XChangePointerControl(x_display, True, False, 1, 1, 0);
549 + }
550 +
551 + // Restore mouse acceleration to original value
552 + void driver_base::restore_mouse_accel(void)
553 + {
554 +        XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold);
555 + }
556 +
557  
558   /*
559   *  Windowed display driver
# Line 610 | Line 628 | driver_window::driver_window(const video
628                  if (shm_error) {
629                          shmdt(shminfo.shmaddr);
630                          XDestroyImage(img);
631 +                        img = NULL;
632                          shminfo.shmid = -1;
633                  } else {
634                          have_shm = true;
# Line 630 | Line 649 | driver_window::driver_window(const video
649          }
650  
651   #ifdef ENABLE_VOSF
652 +        use_vosf = true;
653          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
654          the_host_buffer = the_buffer_copy;
655          the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
656          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
657          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
658 +        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
659   #else
660          // Allocate memory for frame buffer
661          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
662 +        D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
663   #endif
664  
665          // Create GC
# Line 673 | Line 695 | driver_window::driver_window(const video
695   // Close display
696   driver_window::~driver_window()
697   {
676        if (img)
677                XDestroyImage(img);
698          if (have_shm) {
699                  XShmDetach(x_display, &shminfo);
700 + #ifdef ENABLE_VOSF
701 +                the_host_buffer = NULL; // don't free() in driver_base dtor
702 + #else
703                  the_buffer_copy = NULL; // don't free() in driver_base dtor
704 + #endif
705 +        }
706 + #ifdef ENABLE_VOSF
707 +        if (use_vosf) {
708 +                // don't free() memory mapped buffers in driver_base dtor
709 +                if (the_buffer != VM_MAP_FAILED) {
710 +                        vm_release(the_buffer, the_buffer_size);
711 +                        the_buffer = NULL;
712 +                }
713 +                if (the_buffer_copy != VM_MAP_FAILED) {
714 +                        vm_release(the_buffer_copy, the_buffer_size);
715 +                        the_buffer_copy = NULL;
716 +                }
717 +        }
718 + #endif
719 +        if (img)
720 +                XDestroyImage(img);
721 +        if (have_shm) {
722 +                shmdt(shminfo.shmaddr);
723 +                shmctl(shminfo.shmid, IPC_RMID, 0);
724          }
725          if (gc)
726                  XFreeGC(x_display, gc);
# Line 704 | Line 747 | void driver_window::grab_mouse(void)
747                  Delay_usec(100000);
748          }
749          if (result == GrabSuccess) {
707                ADBSetRelMouseMode(mouse_grabbed = true);
750                  XStoreName(x_display, w, GetString(STR_WINDOW_TITLE_GRABBED));
751 <                XSync(x_display, false);
751 >                ADBSetRelMouseMode(mouse_grabbed = true);
752 >                disable_mouse_accel();
753          }
754   }
755  
# Line 717 | Line 760 | void driver_window::ungrab_mouse(void)
760                  XUngrabPointer(x_display, CurrentTime);
761                  XStoreName(x_display, w, GetString(STR_WINDOW_TITLE));
762                  ADBSetRelMouseMode(mouse_grabbed = false);
763 +                restore_mouse_accel();
764          }
765   }
766  
# Line 812 | Line 856 | void driver_dga::suspend(void)
856   #endif
857          XUngrabPointer(x_display, CurrentTime);
858          XUngrabKeyboard(x_display, CurrentTime);
859 +        restore_mouse_accel();
860          XUnmapWindow(x_display, w);
861          wait_unmapped(w);
862  
# Line 841 | Line 886 | void driver_dga::resume(void)
886          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
887          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
888          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
889 +        disable_mouse_accel();
890   #ifdef ENABLE_XF86_DGA
891          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
892          XF86DGASetViewPort(x_display, screen, 0, 0);
# Line 987 | Line 1033 | driver_fbdev::driver_fbdev(const video_m
1033          XGrabPointer(x_display, w, True,
1034                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1035                  GrabModeAsync, GrabModeAsync, w, None, CurrentTime);
1036 +        disable_mouse_accel();
1037          
1038          // Calculate bytes per row
1039          int bytes_per_row = TrivialBytesPerRow(mode.x, mode.depth);
1040          
1041          // Map frame buffer
1042 <        if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1043 <                if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1042 >        the_buffer_size = height * bytes_per_row;
1043 >        if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1044 >                if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1045                          char str[256];
1046                          sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
1047                          ErrorAlert(str);
# Line 1032 | Line 1080 | driver_fbdev::driver_fbdev(const video_m
1080   // Close display
1081   driver_fbdev::~driver_fbdev()
1082   {
1083 +        if (!use_vosf) {
1084 +                if (the_buffer != MAP_FAILED) {
1085 +                        // don't free() the screen buffer in driver_base dtor
1086 +                        munmap(the_buffer, the_buffer_size);
1087 +                        the_buffer = NULL;
1088 +                }
1089 +        }
1090 + #ifdef ENABLE_VOSF
1091 +        else {
1092 +                if (the_host_buffer != MAP_FAILED) {
1093 +                        // don't free() the screen buffer in driver_base dtor
1094 +                        munmap(the_host_buffer, the_buffer_size);
1095 +                        the_host_buffer = NULL;
1096 +                }
1097 +                if (the_buffer_copy != VM_MAP_FAILED) {
1098 +                        vm_release(the_buffer_copy, the_buffer_size);
1099 +                        the_buffer_copy = NULL;
1100 +                }
1101 +                if (the_buffer != VM_MAP_FAILED) {
1102 +                        vm_release(the_buffer, the_buffer_size);
1103 +                        the_buffer = NULL;
1104 +                }
1105 +        }
1106 + #endif
1107   }
1108   #endif
1109  
# Line 1101 | Line 1173 | driver_xf86dga::driver_xf86dga(const vid
1173          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1174          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1175          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1176 +        disable_mouse_accel();
1177  
1178          int v_width, v_bank, v_size;
1179          XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
# Line 1117 | Line 1190 | driver_xf86dga::driver_xf86dga(const vid
1190  
1191          // Init blitting routines
1192          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
1193 < #ifdef VIDEO_VOSF
1193 > #if VIDEO_VOSF
1194   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1195          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1196          // i.e. the framebuffer update function is not Blit_Copy_Raw
# Line 1148 | Line 1221 | driver_xf86dga::driver_xf86dga(const vid
1221   driver_xf86dga::~driver_xf86dga()
1222   {
1223          XF86DGADirectVideo(x_display, screen, 0);
1224 +        if (!use_vosf) {
1225 +                // don't free() the screen buffer in driver_base dtor
1226 +                the_buffer = NULL;
1227 +        }
1228 + #ifdef ENABLE_VOSF
1229 +        else {
1230 +                // don't free() the screen buffer in driver_base dtor
1231 +                the_host_buffer = NULL;
1232 +                
1233 +                if (the_buffer_copy != VM_MAP_FAILED) {
1234 +                        vm_release(the_buffer_copy, the_buffer_size);
1235 +                        the_buffer_copy = NULL;
1236 +                }
1237 +                if (the_buffer != VM_MAP_FAILED) {
1238 +                        vm_release(the_buffer, the_buffer_size);
1239 +                        the_buffer = NULL;
1240 +                }
1241 +        }
1242 + #endif
1243   #ifdef ENABLE_XF86_VIDMODE
1244          if (has_vidmode)
1245                  XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
# Line 1276 | Line 1368 | static bool video_open(const video_mode
1368                          --bloss;
1369          }
1370  
1371 <        // Preset palette pixel values for gamma table
1371 >        // Preset palette pixel values for CLUT or gamma table
1372          if (color_class == DirectColor) {
1373                  int num = vis->map_entries;
1374                  for (int i=0; i<num; i++) {
1375                          int c = (i * 256) / num;
1376                          palette[i].pixel = map_rgb(c, c, c);
1377 +                        palette[i].flags = DoRed | DoGreen | DoBlue;
1378 +                }
1379 +        } else if (color_class == PseudoColor) {
1380 +                for (int i=0; i<256; i++) {
1381 +                        palette[i].pixel = i;
1382 +                        palette[i].flags = DoRed | DoGreen | DoBlue;
1383                  }
1384          }
1385  
# Line 1292 | Line 1390 | static bool video_open(const video_mode
1390                  palette[i].red = c * 0x0101;
1391                  palette[i].green = c * 0x0101;
1392                  palette[i].blue = c * 0x0101;
1295                if (color_class == PseudoColor)
1296                        palette[i].pixel = i;
1297                palette[i].flags = DoRed | DoGreen | DoBlue;
1393          }
1394          if (cmap[0] && cmap[1]) {
1395                  XStoreColors(x_display, cmap[0], palette, num);
# Line 1303 | Line 1398 | static bool video_open(const video_mode
1398  
1399   #ifdef ENABLE_VOSF
1400          // Load gray ramp to 8->16/32 expand map
1401 <        if (!IsDirectMode(mode) && (color_class == TrueColor || color_class == DirectColor))
1401 >        if (!IsDirectMode(mode) && xdepth > 8)
1402                  for (int i=0; i<256; i++)
1403                          ExpandMap[i] = map_rgb(i, i, i);
1404   #endif
# Line 1334 | Line 1429 | static bool video_open(const video_mode
1429  
1430   #ifdef ENABLE_VOSF
1431          if (use_vosf) {
1432 <                // Initialize the mainBuffer structure
1433 <                if (!video_init_buffer()) {
1432 >                // Initialize the VOSF system
1433 >                if (!video_vosf_init()) {
1434                          ErrorAlert(STR_VOSF_INIT_ERR);
1435                  return false;
1436                  }
1342
1343                // Initialize the handler for SIGSEGV
1344                if (!sigsegv_install_handler(screen_fault_handler)) {
1345                        ErrorAlert("Could not initialize Video on SEGV signals");
1346                        return false;
1347                }
1437          }
1438   #endif
1439          
# Line 1560 | Line 1649 | static void video_close(void)
1649          XSync(x_display, false);
1650  
1651   #ifdef ENABLE_VOSF
1563        // Deinitialize VOSF
1652          if (use_vosf) {
1653 <                if (mainBuffer.pageInfo) {
1654 <                        free(mainBuffer.pageInfo);
1567 <                        mainBuffer.pageInfo = NULL;
1568 <                }
1569 <                if (mainBuffer.dirtyPages) {
1570 <                        free(mainBuffer.dirtyPages);
1571 <                        mainBuffer.dirtyPages = NULL;
1572 <                }
1653 >                // Deinitialize VOSF
1654 >                video_vosf_exit();
1655          }
1656   #endif
1657  
# Line 1655 | Line 1737 | void video_set_palette(uint8 *pal, int n
1737  
1738          // Convert colors to XColor array
1739          int num_out = 256;
1740 +        bool stretch = false;
1741          if (IsDirectMode(VideoMonitor.mode)) {
1742                  // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1743                  num_out = vis->map_entries;
1744 +                stretch = true;
1745          }
1746          XColor *p = palette;
1747          for (int i=0; i<num_out; i++) {
1748 <                int c = (i * num_in) / num_out;
1748 >                int c = (stretch ? (i * num_in) / num_out : i);
1749                  p->red = pal[c*3 + 0] * 0x0101;
1750                  p->green = pal[c*3 + 1] * 0x0101;
1751                  p->blue = pal[c*3 + 2] * 0x0101;
1668                if (color_class == PseudoColor)
1669                        p->pixel = i;
1670                p->flags = DoRed | DoGreen | DoBlue;
1752                  p++;
1753          }
1754  
1755   #ifdef ENABLE_VOSF
1756          // Recalculate pixel color expansion map
1757 <        if (!IsDirectMode(VideoMonitor.mode) && (color_class == TrueColor || color_class == DirectColor)) {
1757 >        if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) {
1758                  for (int i=0; i<256; i++) {
1759                          int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1760                          ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
# Line 1886 | Line 1967 | static void handle_events(void)
1967                  XNextEvent(x_display, &event);
1968  
1969                  switch (event.type) {
1970 +
1971                          // Mouse button
1972                          case ButtonPress: {
1973                                  unsigned int button = event.xbutton.button;
# Line 1918 | Line 2000 | static void handle_events(void)
2000                                  drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2001                                  break;
2002  
2003 +                        // Mouse entered window
2004 +                        case EnterNotify:
2005 +                                if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
2006 +                                        drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2007 +                                break;
2008 +
2009                          // Keyboard
2010                          case KeyPress: {
2011                                  int code = -1;
# Line 2254 | Line 2342 | static void video_refresh_dga(void)
2342   {
2343          // Quit DGA mode if requested
2344          possibly_quit_dga_mode();
2257        
2258        // Handle X events
2259        handle_events();
2260        
2261        // Handle palette changes
2262        handle_palette_changes();
2345   }
2346  
2347   #ifdef ENABLE_VOSF
# Line 2269 | Line 2351 | static void video_refresh_dga_vosf(void)
2351          // Quit DGA mode if requested
2352          possibly_quit_dga_mode();
2353          
2272        // Handle X events
2273        handle_events();
2274        
2275        // Handle palette changes
2276        handle_palette_changes();
2277        
2354          // Update display (VOSF variant)
2355          static int tick_counter = 0;
2356          if (++tick_counter >= frame_skip) {
# Line 2293 | Line 2369 | static void video_refresh_window_vosf(vo
2369          // Ungrab mouse if requested
2370          possibly_ungrab_mouse();
2371          
2296        // Handle X events
2297        handle_events();
2298        
2299        // Handle palette changes
2300        handle_palette_changes();
2301        
2372          // Update display (VOSF variant)
2373          static int tick_counter = 0;
2374          if (++tick_counter >= frame_skip) {
# Line 2318 | Line 2388 | static void video_refresh_window_static(
2388          // Ungrab mouse if requested
2389          possibly_ungrab_mouse();
2390  
2321        // Handle X events
2322        handle_events();
2323        
2324        // Handle_palette changes
2325        handle_palette_changes();
2326        
2391          // Update display (static variant)
2392          static int tick_counter = 0;
2393          if (++tick_counter >= frame_skip) {
# Line 2337 | Line 2401 | static void video_refresh_window_dynamic
2401          // Ungrab mouse if requested
2402          possibly_ungrab_mouse();
2403  
2340        // Handle X events
2341        handle_events();
2342        
2343        // Handle_palette changes
2344        handle_palette_changes();
2345        
2404          // Update display (dynamic variant)
2405          static int tick_counter = 0;
2406          tick_counter++;
# Line 2378 | Line 2436 | static void VideoRefreshInit(void)
2436          }
2437   }
2438  
2439 + // This function is called on non-threaded platforms from a timer interrupt
2440   void VideoRefresh(void)
2441   {
2442          // We need to check redraw_thread_active to inhibit refreshed during
2443          // mode changes on non-threaded platforms
2444 <        if (redraw_thread_active)
2445 <                video_refresh();
2444 >        if (!redraw_thread_active)
2445 >                return;
2446 >
2447 >        // Handle X events
2448 >        handle_events();
2449 >
2450 >        // Handle palette changes
2451 >        handle_palette_changes();
2452 >
2453 >        // Update display
2454 >        video_refresh();
2455   }
2456  
2457 + const int VIDEO_REFRESH_HZ = 60;
2458 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2459 +
2460   #ifdef HAVE_PTHREADS
2461   static void *redraw_func(void *arg)
2462   {
2463 +        int fd = ConnectionNumber(x_display);
2464 +
2465          uint64 start = GetTicks_usec();
2466          int64 ticks = 0;
2467 <        uint64 next = GetTicks_usec();
2467 >        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2468 >
2469          while (!redraw_thread_cancel) {
2470 <                video_refresh();
2397 <                next += 16667;
2470 >
2471                  int64 delay = next - GetTicks_usec();
2472 <                if (delay > 0)
2473 <                        Delay_usec(delay);
2474 <                else if (delay < -16667)
2472 >                if (delay < -VIDEO_REFRESH_DELAY) {
2473 >
2474 >                        // We are lagging far behind, so we reset the delay mechanism
2475                          next = GetTicks_usec();
2476 <                ticks++;
2476 >
2477 >                } else if (delay <= 0) {
2478 >
2479 >                        // Delay expired, refresh display
2480 >                        handle_events();
2481 >                        handle_palette_changes();
2482 >                        video_refresh();
2483 >                        next += VIDEO_REFRESH_DELAY;
2484 >                        ticks++;
2485 >
2486 >                } else {
2487 >
2488 >                        // No display refresh pending, check for X events
2489 >                        fd_set readfds;
2490 >                        FD_ZERO(&readfds);
2491 >                        FD_SET(fd, &readfds);
2492 >                        struct timeval timeout;
2493 >                        timeout.tv_sec = 0;
2494 >                        timeout.tv_usec = delay;
2495 >                        if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2496 >                                handle_events();
2497 >                }
2498          }
2499 +
2500          uint64 end = GetTicks_usec();
2501 <        // printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
2501 >        D(bug("%Ld refreshes in %Ld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2502          return NULL;
2503   }
2504   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines