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.51 by cebix, 2001-07-03T15:59:47Z vs.
Revision 1.72 by gbeauche, 2004-11-08T21:07:07Z

# Line 1 | Line 1
1   /*
2   *  video_x.cpp - Video/graphics emulation, X11 specific stuff
3   *
4 < *  Basilisk II (C) 1997-2001 Christian Bauer
4 > *  Basilisk II (C) 1997-2004 Christian Bauer
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 37 | Line 37
37   #include <sys/shm.h>
38   #include <errno.h>
39  
40 + #include <algorithm>
41 +
42   #ifdef HAVE_PTHREADS
43   # include <pthread.h>
44   #endif
# Line 60 | Line 62
62   #include "prefs.h"
63   #include "user_strings.h"
64   #include "video.h"
65 + #include "video_blit.h"
66  
67   #define DEBUG 0
68   #include "debug.h"
69  
70  
71 + // Supported video modes
72 + static vector<video_mode> VideoModes;
73 +
74   // Display types
75   enum {
76          DISPLAY_WINDOW, // X11 window, using MIT SHM extensions if possible
# Line 74 | 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 87 | 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
100 + static pthread_attr_t redraw_thread_attr;                       // Redraw thread attributes
101   static volatile bool redraw_thread_cancel;                      // Flag: Cancel Redraw thread
102   static pthread_t redraw_thread;                                         // Redraw thread
103   #endif
# Line 115 | Line 123 | static bool use_keycodes = false;                                      //
123   static int keycode_table[256];                                          // X keycode -> Mac keycode translation table
124  
125   // X11 variables
126 + char *x_display_name = NULL;                                            // X11 display name
127 + Display *x_display = NULL;                                                      // X11 display handle
128   static int screen;                                                                      // Screen number
119 static int xdepth;                                                                      // Depth of X screen
129   static Window rootwin;                                                          // Root window and our window
130 < static XVisualInfo visualInfo;
131 < static Visual *vis;
123 < static Colormap cmap[2] = {0, 0};                                       // Colormaps for indexed modes (DGA needs two of them)
130 > static int num_depths = 0;                                                      // Number of available X depths
131 > static int *avail_depths = NULL;                                        // List of available X depths
132   static XColor black, white;
133   static unsigned long black_pixel, white_pixel;
134   static int eventmask;
135  
136 + static int xdepth;                                                                      // Depth of X screen
137 + static VisualFormat visualFormat;
138 + static XVisualInfo visualInfo;
139 + static Visual *vis;
140 + static int color_class;
141 +
142   static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
143  
144 < static XColor palette[256];                                                     // Color palette to be used as CLUT and gamma table
145 < static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
144 > static Colormap cmap[2] = {0, 0};                                       // Colormaps for indexed modes (DGA needs two of them)
145 >
146 > static XColor x_palette[256];                                                   // Color palette to be used as CLUT and gamma table
147 > static bool x_palette_changed = false;                          // Flag: Palette changed, redraw thread must set new colors
148  
149   #ifdef ENABLE_FBDEV_DGA
150   static int fbdev_fd = -1;
# Line 141 | Line 157 | static int num_x_video_modes;
157  
158   // Mutex to protect palette
159   #ifdef HAVE_PTHREADS
160 < static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;
161 < #define LOCK_PALETTE pthread_mutex_lock(&palette_lock)
162 < #define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock)
160 > static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER;
161 > #define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock)
162 > #define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock)
163   #else
164   #define LOCK_PALETTE
165   #define UNLOCK_PALETTE
# Line 172 | Line 188 | static void (*video_refresh)(void);
188  
189   // Prototypes
190   static void *redraw_func(void *arg);
175 static int event2keycode(XKeyEvent &ev);
191  
192   // From main_unix.cpp
193   extern char *x_display_name;
194   extern Display *x_display;
195 + extern void *vm_acquire_mac(size_t size);
196  
197   // From sys_unix.cpp
198   extern void SysMountFirstFloppy(void);
199  
200  
201   /*
202 + *  monitor_desc subclass for X11 display
203 + */
204 +
205 + class X11_monitor_desc : public monitor_desc {
206 + public:
207 +        X11_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
208 +        ~X11_monitor_desc() {}
209 +
210 +        virtual void switch_to_current_mode(void);
211 +        virtual void set_palette(uint8 *pal, int num);
212 +
213 +        bool video_open(void);
214 +        void video_close(void);
215 + };
216 +
217 +
218 + /*
219   *  Utility functions
220   */
221  
222 + // Map video_mode depth ID to numerical depth value
223 + static inline int depth_of_video_mode(video_mode const & mode)
224 + {
225 +        int depth = -1;
226 +        switch (mode.depth) {
227 +        case VDEPTH_1BIT:
228 +                depth = 1;
229 +                break;
230 +        case VDEPTH_2BIT:
231 +                depth = 2;
232 +                break;
233 +        case VDEPTH_4BIT:
234 +                depth = 4;
235 +                break;
236 +        case VDEPTH_8BIT:
237 +                depth = 8;
238 +                break;
239 +        case VDEPTH_16BIT:
240 +                depth = 16;
241 +                break;
242 +        case VDEPTH_32BIT:
243 +                depth = 32;
244 +                break;
245 +        default:
246 +                abort();
247 +        }
248 +        return depth;
249 + }
250 +
251   // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
252   static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
253   {
254          return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
255   }
256  
257 + // Do we have a visual for handling the specified Mac depth? If so, set the
258 + // global variables "xdepth", "visualInfo", "vis" and "color_class".
259 + static bool find_visual_for_depth(video_depth depth)
260 + {
261 +        D(bug("have_visual_for_depth(%d)\n", 1 << depth));
262 +
263 +        // 1-bit works always and uses default visual
264 +        if (depth == VDEPTH_1BIT) {
265 +                vis = DefaultVisual(x_display, screen);
266 +                visualInfo.visualid = XVisualIDFromVisual(vis);
267 +                int num = 0;
268 +                XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
269 +                visualInfo = vi[0];
270 +                XFree(vi);
271 +                xdepth = visualInfo.depth;
272 +                color_class = visualInfo.c_class;
273 +                D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
274 +                return true;
275 +        }
276 +
277 +        // Calculate minimum and maximum supported X depth
278 +        int min_depth = 1, max_depth = 32;
279 +        switch (depth) {
280 + #ifdef ENABLE_VOSF
281 +                case VDEPTH_2BIT:
282 +                case VDEPTH_4BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
283 +                case VDEPTH_8BIT:
284 +                        min_depth = 8;
285 +                        max_depth = 32;
286 +                        break;
287 + #else
288 +                case VDEPTH_2BIT:
289 +                case VDEPTH_4BIT:       // 2/4-bit requires VOSF blitters
290 +                        return false;
291 +                case VDEPTH_8BIT:       // 8-bit without VOSF requires an 8-bit visual
292 +                        min_depth = 8;
293 +                        max_depth = 8;
294 +                        break;
295 + #endif
296 +                case VDEPTH_16BIT:      // 16-bit requires a 15/16-bit visual
297 +                        min_depth = 15;
298 +                        max_depth = 16;
299 +                        break;
300 +                case VDEPTH_32BIT:      // 32-bit requires a 24/32-bit visual
301 +                        min_depth = 24;
302 +                        max_depth = 32;
303 +                        break;
304 +        }
305 +        D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth));
306 +
307 +        // Try to find a visual for one of the color depths
308 +        bool visual_found = false;
309 +        for (int i=0; i<num_depths && !visual_found; i++) {
310 +
311 +                xdepth = avail_depths[i];
312 +                D(bug(" trying to find visual for depth %d\n", xdepth));
313 +                if (xdepth < min_depth || xdepth > max_depth)
314 +                        continue;
315 +
316 +                // Determine best color class for this depth
317 +                switch (xdepth) {
318 +                        case 1: // Try StaticGray or StaticColor
319 +                                if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo)
320 +                                 || XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo))
321 +                                        visual_found = true;
322 +                                break;
323 +                        case 8: // Need PseudoColor
324 +                                if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo))
325 +                                        visual_found = true;
326 +                                break;
327 +                        case 15:
328 +                        case 16:
329 +                        case 24:
330 +                        case 32: // Try DirectColor first, as this will allow gamma correction
331 +                                if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo)
332 +                                 || XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo))
333 +                                        visual_found = true;
334 +                                break;
335 +                        default:
336 +                                D(bug("  not a supported depth\n"));
337 +                                break;
338 +                }
339 +        }
340 +        if (!visual_found)
341 +                return false;
342 +
343 +        // Visual was found
344 +        vis = visualInfo.visual;
345 +        color_class = visualInfo.c_class;
346 +        D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth));
347 + #if DEBUG
348 +        switch (color_class) {
349 +                case StaticGray: D(bug("StaticGray\n")); break;
350 +                case GrayScale: D(bug("GrayScale\n")); break;
351 +                case StaticColor: D(bug("StaticColor\n")); break;
352 +                case PseudoColor: D(bug("PseudoColor\n")); break;
353 +                case TrueColor: D(bug("TrueColor\n")); break;
354 +                case DirectColor: D(bug("DirectColor\n")); break;
355 +        }
356 + #endif
357 +        return true;
358 + }
359 +
360   // Add mode to list of supported modes
361   static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
362   {
# Line 217 | Line 382 | static void add_window_modes(video_depth
382   }
383  
384   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
385 < static void set_mac_frame_buffer(video_depth depth, bool native_byte_order)
385 > static void set_mac_frame_buffer(X11_monitor_desc &monitor, video_depth depth, bool native_byte_order)
386   {
387   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
388          int layout = FLAYOUT_DIRECT;
# Line 229 | Line 394 | static void set_mac_frame_buffer(video_d
394                  MacFrameLayout = layout;
395          else
396                  MacFrameLayout = FLAYOUT_DIRECT;
397 <        VideoMonitor.mac_frame_base = MacFrameBaseMac;
397 >        monitor.set_mac_frame_base(MacFrameBaseMac);
398  
399          // Set variables used by UAE memory banking
400 +        const video_mode &mode = monitor.get_current_mode();
401          MacFrameBaseHost = the_buffer;
402 <        MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
402 >        MacFrameSize = mode.bytes_per_row * mode.y;
403          InitFrameBufferMapping();
404   #else
405 <        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
240 <        D(bug("Host frame buffer = %p, ", the_buffer));
405 >        monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
406   #endif
407 <        D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base));
407 >        D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
408   }
409  
410   // Set window name and class
# Line 317 | Line 482 | static int error_handler(Display *d, XEr
482  
483   class driver_base {
484   public:
485 <        driver_base();
485 >        driver_base(X11_monitor_desc &m);
486          virtual ~driver_base();
487  
488          virtual void update_palette(void);
# Line 326 | Line 491 | public:
491          virtual void toggle_mouse_grab(void) {}
492          virtual void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
493  
494 +        void disable_mouse_accel(void);
495 +        void restore_mouse_accel(void);
496 +
497          virtual void grab_mouse(void) {}
498          virtual void ungrab_mouse(void) {}
499  
500   public:
501 +        X11_monitor_desc &monitor; // Associated video monitor
502 +        const video_mode &mode;    // Video mode handled by the driver
503 +
504          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
505          Window w;               // The window we draw into
506 +
507 +        int orig_accel_numer, orig_accel_denom, orig_threshold; // Original mouse acceleration
508   };
509  
510   class driver_window;
# Line 345 | Line 518 | class driver_window : public driver_base
518          friend void update_display_static(driver_window *drv);
519  
520   public:
521 <        driver_window(const video_mode &mode);
521 >        driver_window(X11_monitor_desc &monitor);
522          ~driver_window();
523  
524          void toggle_mouse_grab(void);
# Line 370 | Line 543 | static driver_base *drv = NULL;        // Point
543   # include "video_vosf.h"
544   #endif
545  
546 < driver_base::driver_base()
547 < : init_ok(false), w(0)
546 > driver_base::driver_base(X11_monitor_desc &m)
547 > : monitor(m), mode(m.get_current_mode()), init_ok(false), w(0)
548   {
549          the_buffer = NULL;
550          the_buffer_copy = NULL;
551 +        XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
552   }
553  
554   driver_base::~driver_base()
555   {
556          ungrab_mouse();
557 +        restore_mouse_accel();
558  
559          if (w) {
560                  XUnmapWindow(x_display, w);
# Line 403 | Line 578 | driver_base::~driver_base()
578          }
579   #ifdef ENABLE_VOSF
580          else {
581 <                if (the_buffer != (uint8 *)VM_MAP_FAILED) {
581 >                // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
582 >                if (the_buffer != VM_MAP_FAILED) {
583 >                        D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
584                          vm_release(the_buffer, the_buffer_size);
585                          the_buffer = NULL;
586                  }
587 <                if (the_buffer_copy != (uint8 *)VM_MAP_FAILED) {
588 <                        vm_release(the_buffer_copy, the_buffer_size);
587 >                if (the_host_buffer) {
588 >                        D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
589 >                        free(the_host_buffer);
590 >                        the_host_buffer = NULL;
591 >                }
592 >                if (the_buffer_copy) {
593 >                        D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
594 >                        free(the_buffer_copy);
595                          the_buffer_copy = NULL;
596                  }
597          }
# Line 418 | Line 601 | driver_base::~driver_base()
601   // Palette has changed
602   void driver_base::update_palette(void)
603   {
604 <        if (cmap[0] && cmap[1]) {
605 <                int num = 256;
606 <                if (IsDirectMode(VideoMonitor.mode))
424 <                        num = vis->map_entries; // Palette is gamma table
425 <                else if (vis->c_class == DirectColor)
604 >        if (color_class == PseudoColor || color_class == DirectColor) {
605 >                int num = vis->map_entries;
606 >                if (!IsDirectMode(monitor.get_current_mode()) && color_class == DirectColor)
607                          return; // Indexed mode on true color screen, don't set CLUT
608 <                XStoreColors(x_display, cmap[0], palette, num);
609 <                XStoreColors(x_display, cmap[1], palette, num);
608 >                XStoreColors(x_display, cmap[0], x_palette, num);
609 >                XStoreColors(x_display, cmap[1], x_palette, num);
610          }
611          XSync(x_display, false);
612   }
613  
614 + // Disable mouse acceleration
615 + void driver_base::disable_mouse_accel(void)
616 + {
617 +        XChangePointerControl(x_display, True, False, 1, 1, 0);
618 + }
619 +
620 + // Restore mouse acceleration to original value
621 + void driver_base::restore_mouse_accel(void)
622 + {
623 +        XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold);
624 + }
625 +
626  
627   /*
628   *  Windowed display driver
629   */
630  
631   // Open display
632 < driver_window::driver_window(const video_mode &mode)
633 < : gc(0), img(NULL), have_shm(false), mouse_grabbed(false), mac_cursor(0)
632 > driver_window::driver_window(X11_monitor_desc &m)
633 > : driver_base(m), gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
634   {
635          int width = mode.x, height = mode.y;
636          int aligned_width = (width + 15) & ~15;
# Line 446 | Line 639 | driver_window::driver_window(const video
639          // Set absolute mouse mode
640          ADBSetRelMouseMode(mouse_grabbed);
641  
642 <        // Create window
642 >        // Create window (setting background_pixel, border_pixel and colormap is
643 >        // mandatory when using a non-default visual; in 1-bit mode we use the
644 >        // default visual, so we can also use the default colormap)
645          XSetWindowAttributes wattr;
646          wattr.event_mask = eventmask = win_eventmask;
647 <        wattr.background_pixel = black_pixel;
648 <        wattr.colormap = (mode.depth == VDEPTH_1BIT && vis->c_class == PseudoColor ? DefaultColormap(x_display, screen) : cmap[0]);
647 >        wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
648 >        wattr.border_pixel = 0;
649 >        wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
650          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
651 <                InputOutput, vis, CWEventMask | CWBackPixel | (vis->c_class == PseudoColor || vis->c_class == DirectColor ? CWColormap : 0), &wattr);
651 >                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &wattr);
652 >        D(bug(" window created\n"));
653  
654          // Set window name/class
655          set_window_name(w, STR_WINDOW_TITLE);
# Line 476 | Line 673 | driver_window::driver_window(const video
673                          XFree(hints);
674                  }
675          }
676 +        D(bug(" window attributes set\n"));
677          
678          // Show window
679          XMapWindow(x_display, w);
680          wait_mapped(w);
681 +        D(bug(" window mapped\n"));
682  
683          // 1-bit mode is big-endian; if the X server is little-endian, we can't
684          // use SHM because that doesn't allow changing the image byte order
# Line 490 | Line 689 | driver_window::driver_window(const video
689  
690                  // Create SHM image ("height + 2" for safety)
691                  img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
692 +                D(bug(" shm image created\n"));
693                  shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
694                  the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
695                  shminfo.shmaddr = img->data = (char *)the_buffer_copy;
# Line 504 | Line 704 | driver_window::driver_window(const video
704                  if (shm_error) {
705                          shmdt(shminfo.shmaddr);
706                          XDestroyImage(img);
707 +                        img = NULL;
708                          shminfo.shmid = -1;
709                  } else {
710                          have_shm = true;
711                          shmctl(shminfo.shmid, IPC_RMID, 0);
712                  }
713 +                D(bug(" shm image attached\n"));
714          }
715          
716          // Create normal X image if SHM doesn't work ("height + 2" for safety)
# Line 516 | Line 718 | driver_window::driver_window(const video
718                  int bytes_per_row = (mode.depth == VDEPTH_1BIT ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth)));
719                  the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
720                  img = XCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
721 +                D(bug(" X image created\n"));
722          }
723  
724          if (need_msb_image) {
# Line 524 | Line 727 | driver_window::driver_window(const video
727          }
728  
729   #ifdef ENABLE_VOSF
730 +        use_vosf = true;
731          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
732          the_host_buffer = the_buffer_copy;
733          the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
734 <        the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
735 <        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
734 >        the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size);
735 >        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
736 >        D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
737   #else
738          // Allocate memory for frame buffer
739          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
740 +        D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
741   #endif
742  
743          // Create GC
# Line 553 | Line 759 | driver_window::driver_window(const video
759          native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
760   #endif
761   #ifdef ENABLE_VOSF
762 <        Screen_blitter_init(&visualInfo, native_byte_order, mode.depth);
762 >        Screen_blitter_init(visualFormat, native_byte_order, depth_of_video_mode(mode));
763   #endif
764  
765 <        // Set VideoMonitor
766 <        VideoMonitor.mode = mode;
561 <        set_mac_frame_buffer(mode.depth, native_byte_order);
765 >        // Set frame buffer base
766 >        set_mac_frame_buffer(monitor, mode.depth, native_byte_order);
767  
768          // Everything went well
769          init_ok = true;
# Line 567 | Line 772 | driver_window::driver_window(const video
772   // Close display
773   driver_window::~driver_window()
774   {
570        if (img)
571                XDestroyImage(img);
775          if (have_shm) {
776                  XShmDetach(x_display, &shminfo);
777 + #ifdef ENABLE_VOSF
778 +                the_host_buffer = NULL; // don't free() in driver_base dtor
779 + #else
780                  the_buffer_copy = NULL; // don't free() in driver_base dtor
781 + #endif
782 +        }
783 +        if (img) {
784 +                if (!have_shm)
785 +                        img->data = NULL;
786 +                XDestroyImage(img);
787 +        }
788 +        if (have_shm) {
789 +                shmdt(shminfo.shmaddr);
790 +                shmctl(shminfo.shmid, IPC_RMID, 0);
791          }
792          if (gc)
793                  XFreeGC(x_display, gc);
# Line 598 | Line 814 | void driver_window::grab_mouse(void)
814                  Delay_usec(100000);
815          }
816          if (result == GrabSuccess) {
601                ADBSetRelMouseMode(mouse_grabbed = true);
817                  XStoreName(x_display, w, GetString(STR_WINDOW_TITLE_GRABBED));
818 <                XSync(x_display, false);
818 >                ADBSetRelMouseMode(mouse_grabbed = true);
819 >                disable_mouse_accel();
820          }
821   }
822  
# Line 611 | Line 827 | void driver_window::ungrab_mouse(void)
827                  XUngrabPointer(x_display, CurrentTime);
828                  XStoreName(x_display, w, GetString(STR_WINDOW_TITLE));
829                  ADBSetRelMouseMode(mouse_grabbed = false);
830 +                restore_mouse_accel();
831          }
832   }
833  
# Line 626 | Line 843 | void driver_window::mouse_moved(int x, i
843          // Warped mouse motion (this code is taken from SDL)
844  
845          // Post first mouse event
846 <        int width = VideoMonitor.mode.x, height = VideoMonitor.mode.y;
846 >        int width = monitor.get_current_mode().x, height = monitor.get_current_mode().y;
847          int delta_x = x - mouse_last_x, delta_y = y - mouse_last_y;
848          mouse_last_x = x; mouse_last_y = y;
849          ADBMouseMoved(delta_x, delta_y);
# Line 663 | Line 880 | void driver_window::mouse_moved(int x, i
880  
881   class driver_dga : public driver_base {
882   public:
883 <        driver_dga();
883 >        driver_dga(X11_monitor_desc &monitor);
884          ~driver_dga();
885  
886          void suspend(void);
# Line 674 | Line 891 | private:
891          void *fb_save;                  // Saved frame buffer for suspend/resume
892   };
893  
894 < driver_dga::driver_dga()
895 < : suspend_win(0), fb_save(NULL)
894 > driver_dga::driver_dga(X11_monitor_desc &m)
895 > : driver_base(m), suspend_win(0), fb_save(NULL)
896   {
897   }
898  
# Line 696 | Line 913 | void driver_dga::suspend(void)
913          LOCK_FRAME_BUFFER;
914  
915          // Save frame buffer
916 <        fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
916 >        fb_save = malloc(mode.y * mode.bytes_per_row);
917          if (fb_save)
918 <                memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
918 >                memcpy(fb_save, the_buffer, mode.y * mode.bytes_per_row);
919  
920          // Close full screen display
921   #ifdef ENABLE_XF86_DGA
# Line 706 | Line 923 | void driver_dga::suspend(void)
923   #endif
924          XUngrabPointer(x_display, CurrentTime);
925          XUngrabKeyboard(x_display, CurrentTime);
926 +        restore_mouse_accel();
927          XUnmapWindow(x_display, w);
928          wait_unmapped(w);
929  
# Line 735 | Line 953 | void driver_dga::resume(void)
953          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
954          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
955          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
956 +        disable_mouse_accel();
957   #ifdef ENABLE_XF86_DGA
958          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
959          XF86DGASetViewPort(x_display, screen, 0, 0);
# Line 749 | Line 968 | void driver_dga::resume(void)
968                  LOCK_VOSF;
969                  PFLAG_SET_ALL;
970                  UNLOCK_VOSF;
971 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
971 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
972          }
973   #endif
974          
# Line 759 | Line 978 | void driver_dga::resume(void)
978                  // Don't copy fb_save to the temporary frame buffer in VOSF mode
979                  if (!use_vosf)
980   #endif
981 <                memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
981 >                memcpy(the_buffer, fb_save, mode.y * mode.bytes_per_row);
982                  free(fb_save);
983                  fb_save = NULL;
984          }
# Line 781 | Line 1000 | const char FBDEVICE_FILE_NAME[] = "/dev/
1000  
1001   class driver_fbdev : public driver_dga {
1002   public:
1003 <        driver_fbdev(const video_mode &mode);
1003 >        driver_fbdev(X11_monitor_desc &monitor);
1004          ~driver_fbdev();
1005   };
1006  
1007   // Open display
1008 < driver_fbdev::driver_fbdev(const video_mode &mode)
1008 > driver_fbdev::driver_fbdev(X11_monitor_desc &m) : driver_dga(m)
1009   {
1010          int width = mode.x, height = mode.y;
1011  
# Line 834 | Line 1053 | driver_fbdev::driver_fbdev(const video_m
1053                  if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
1054                          continue;
1055                  
1056 <                if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
1056 >                if ((sscanf(line, "%19s %d %x", fb_name, &fb_depth, &fb_offset) == 3)
1057                   && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) {
1058                          device_found = true;
1059                          break;
# Line 881 | Line 1100 | driver_fbdev::driver_fbdev(const video_m
1100          XGrabPointer(x_display, w, True,
1101                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1102                  GrabModeAsync, GrabModeAsync, w, None, CurrentTime);
1103 +        disable_mouse_accel();
1104          
1105          // Calculate bytes per row
1106          int bytes_per_row = TrivialBytesPerRow(mode.x, mode.depth);
1107          
1108          // Map frame buffer
1109 <        if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1110 <                if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1109 >        the_buffer_size = height * bytes_per_row;
1110 >        if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1111 >                if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1112                          char str[256];
1113                          sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
1114                          ErrorAlert(str);
# Line 899 | Line 1120 | driver_fbdev::driver_fbdev(const video_m
1120   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1121          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1122          // i.e. the framebuffer update function is not Blit_Copy_Raw
1123 <        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1123 >        use_vosf = Screen_blitter_init(visualFormat, true, mode.depth);
1124          
1125          if (use_vosf) {
1126            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1127            the_host_buffer = the_buffer;
1128            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1129 <          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
1130 <          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1129 >          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1130 >          the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size);
1131          }
1132   #else
1133          use_vosf = false;
1134   #endif
1135   #endif
1136          
1137 <        // Set VideoMonitor
1138 <        VideoModes[0].bytes_per_row = bytes_per_row;
1139 <        VideoModes[0].depth = DepthModeForPixelDepth(fb_depth);
1140 <        VideoMonitor.mode = mode;
920 <        set_mac_frame_buffer(mode.depth, true);
1137 >        // Set frame buffer base
1138 >        const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1139 >        const_cast<video_mode *>(&mode)->depth = DepthModeForPixelDepth(fb_depth);
1140 >        set_mac_frame_buffer(monitor, mode.depth, true);
1141  
1142          // Everything went well
1143          init_ok = true;
# Line 926 | Line 1146 | driver_fbdev::driver_fbdev(const video_m
1146   // Close display
1147   driver_fbdev::~driver_fbdev()
1148   {
1149 +        if (!use_vosf) {
1150 +                if (the_buffer != MAP_FAILED) {
1151 +                        // don't free() the screen buffer in driver_base dtor
1152 +                        munmap(the_buffer, the_buffer_size);
1153 +                        the_buffer = NULL;
1154 +                }
1155 +        }
1156 + #ifdef ENABLE_VOSF
1157 +        else {
1158 +                if (the_host_buffer != MAP_FAILED) {
1159 +                        // don't free() the screen buffer in driver_base dtor
1160 +                        munmap(the_host_buffer, the_buffer_size);
1161 +                        the_host_buffer = NULL;
1162 +                }
1163 +        }
1164 + #endif
1165   }
1166   #endif
1167  
# Line 937 | Line 1173 | driver_fbdev::~driver_fbdev()
1173  
1174   class driver_xf86dga : public driver_dga {
1175   public:
1176 <        driver_xf86dga(const video_mode &mode);
1176 >        driver_xf86dga(X11_monitor_desc &monitor);
1177          ~driver_xf86dga();
1178  
1179          void update_palette(void);
# Line 948 | Line 1184 | private:
1184   };
1185  
1186   // Open display
1187 < driver_xf86dga::driver_xf86dga(const video_mode &mode)
1188 < : current_dga_cmap(0)
1187 > driver_xf86dga::driver_xf86dga(X11_monitor_desc &m)
1188 > : driver_dga(m), current_dga_cmap(0)
1189   {
1190          int width = mode.x, height = mode.y;
1191  
# Line 976 | Line 1212 | driver_xf86dga::driver_xf86dga(const vid
1212          XSetWindowAttributes wattr;
1213          wattr.event_mask = eventmask = dga_eventmask;
1214          wattr.override_redirect = True;
1215 +        wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
1216  
1217          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
1218 <                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
1218 >                InputOutput, vis, CWEventMask | CWOverrideRedirect |
1219 >                (color_class == DirectColor ? CWColormap : 0), &wattr);
1220  
1221          // Set window name/class
1222          set_window_name(w, STR_WINDOW_TITLE);
# Line 995 | Line 1233 | driver_xf86dga::driver_xf86dga(const vid
1233          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1234          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1235          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1236 +        disable_mouse_accel();
1237  
1238          int v_width, v_bank, v_size;
1239          XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
# Line 1011 | Line 1250 | driver_xf86dga::driver_xf86dga(const vid
1250  
1251          // Init blitting routines
1252          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
1253 < #ifdef VIDEO_VOSF
1253 > #if ENABLE_VOSF
1254 >        bool native_byte_order;
1255 > #ifdef WORDS_BIGENDIAN
1256 >        native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
1257 > #else
1258 >        native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
1259 > #endif
1260   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1261          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1262          // i.e. the framebuffer update function is not Blit_Copy_Raw
1263 <        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1263 >        use_vosf = Screen_blitter_init(visualFormat, native_byte_order, depth_of_video_mode(mode));
1264          
1265          if (use_vosf) {
1266            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1267            the_host_buffer = the_buffer;
1268            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1269 <          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
1270 <          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1269 >          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1270 >          the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size);
1271          }
1272   #else
1273          use_vosf = false;
1274   #endif
1275   #endif
1276          
1277 <        // Set VideoMonitor
1277 >        // Set frame buffer base
1278          const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1279 <        VideoMonitor.mode = mode;
1035 <        set_mac_frame_buffer(mode.depth, true);
1279 >        set_mac_frame_buffer(monitor, mode.depth, true);
1280  
1281          // Everything went well
1282          init_ok = true;
# Line 1042 | Line 1286 | driver_xf86dga::driver_xf86dga(const vid
1286   driver_xf86dga::~driver_xf86dga()
1287   {
1288          XF86DGADirectVideo(x_display, screen, 0);
1289 +        if (!use_vosf) {
1290 +                // don't free() the screen buffer in driver_base dtor
1291 +                the_buffer = NULL;
1292 +        }
1293 + #ifdef ENABLE_VOSF
1294 +        else {
1295 +                // don't free() the screen buffer in driver_base dtor
1296 +                the_host_buffer = NULL;
1297 +        }
1298 + #endif
1299   #ifdef ENABLE_XF86_VIDMODE
1300          if (has_vidmode)
1301                  XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
# Line 1053 | Line 1307 | void driver_xf86dga::update_palette(void
1307   {
1308          driver_dga::update_palette();
1309          current_dga_cmap ^= 1;
1310 <        if (!IsDirectMode(VideoMonitor.mode) && cmap[current_dga_cmap])
1310 >        if (!IsDirectMode(monitor.get_current_mode()) && cmap[current_dga_cmap])
1311                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1312   }
1313  
# Line 1061 | Line 1315 | void driver_xf86dga::update_palette(void
1315   void driver_xf86dga::resume(void)
1316   {
1317          driver_dga::resume();
1318 <        if (!IsDirectMode(VideoMonitor.mode))
1318 >        if (!IsDirectMode(monitor.get_current_mode()))
1319                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1320   }
1321   #endif
# Line 1136 | Line 1390 | static void keycode_init(void)
1390          }
1391   }
1392  
1393 < // Open display for specified mode
1394 < static bool video_open(const video_mode &mode)
1393 > // Open display for current mode
1394 > bool X11_monitor_desc::video_open(void)
1395   {
1396 +        D(bug("video_open()\n"));
1397 +        const video_mode &mode = get_current_mode();
1398 +
1399 +        // Find best available X visual
1400 +        if (!find_visual_for_depth(mode.depth)) {
1401 +                ErrorAlert(STR_NO_XVISUAL_ERR);
1402 +                return false;
1403 +        }
1404 +
1405 +        // Build up visualFormat structure
1406 +        visualFormat.depth = visualInfo.depth;
1407 +        visualFormat.Rmask = visualInfo.red_mask;
1408 +        visualFormat.Gmask = visualInfo.green_mask;
1409 +        visualFormat.Bmask = visualInfo.blue_mask;
1410 +
1411 +        // Create color maps
1412 +        if (color_class == PseudoColor || color_class == DirectColor) {
1413 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1414 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1415 +        } else {
1416 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
1417 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
1418 +        }
1419 +
1420 +        // Find pixel format of direct modes
1421 +        if (color_class == DirectColor || color_class == TrueColor) {
1422 +                rshift = gshift = bshift = 0;
1423 +                rloss = gloss = bloss = 8;
1424 +                uint32 mask;
1425 +                for (mask=vis->red_mask; !(mask&1); mask>>=1)
1426 +                        ++rshift;
1427 +                for (; mask&1; mask>>=1)
1428 +                        --rloss;
1429 +                for (mask=vis->green_mask; !(mask&1); mask>>=1)
1430 +                        ++gshift;
1431 +                for (; mask&1; mask>>=1)
1432 +                        --gloss;
1433 +                for (mask=vis->blue_mask; !(mask&1); mask>>=1)
1434 +                        ++bshift;
1435 +                for (; mask&1; mask>>=1)
1436 +                        --bloss;
1437 +        }
1438 +
1439 +        // Preset palette pixel values for CLUT or gamma table
1440 +        if (color_class == DirectColor) {
1441 +                int num = vis->map_entries;
1442 +                for (int i=0; i<num; i++) {
1443 +                        int c = (i * 256) / num;
1444 +                        x_palette[i].pixel = map_rgb(c, c, c);
1445 +                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1446 +                }
1447 +        } else if (color_class == PseudoColor) {
1448 +                for (int i=0; i<256; i++) {
1449 +                        x_palette[i].pixel = i;
1450 +                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1451 +                }
1452 +        }
1453 +
1454          // Load gray ramp to color map
1455 <        int num = (vis->c_class == DirectColor ? vis->map_entries : 256);
1455 >        int num = (color_class == DirectColor ? vis->map_entries : 256);
1456          for (int i=0; i<num; i++) {
1457                  int c = (i * 256) / num;
1458 <                palette[i].red = c * 0x0101;
1459 <                palette[i].green = c * 0x0101;
1460 <                palette[i].blue = c * 0x0101;
1461 <                if (vis->c_class == PseudoColor)
1462 <                        palette[i].pixel = i;
1463 <                palette[i].flags = DoRed | DoGreen | DoBlue;
1464 <        }
1153 <        if (cmap[0] && cmap[1]) {
1154 <                XStoreColors(x_display, cmap[0], palette, num);
1155 <                XStoreColors(x_display, cmap[1], palette, num);
1458 >                x_palette[i].red = c * 0x0101;
1459 >                x_palette[i].green = c * 0x0101;
1460 >                x_palette[i].blue = c * 0x0101;
1461 >        }
1462 >        if (color_class == PseudoColor || color_class == DirectColor) {
1463 >                XStoreColors(x_display, cmap[0], x_palette, num);
1464 >                XStoreColors(x_display, cmap[1], x_palette, num);
1465          }
1466  
1467   #ifdef ENABLE_VOSF
1468          // Load gray ramp to 8->16/32 expand map
1469 <        if (!IsDirectMode(mode) && (vis->c_class == TrueColor || vis->c_class == DirectColor))
1469 >        if (!IsDirectMode(mode) && xdepth > 8)
1470                  for (int i=0; i<256; i++)
1471                          ExpandMap[i] = map_rgb(i, i, i);
1472   #endif
# Line 1165 | Line 1474 | static bool video_open(const video_mode
1474          // Create display driver object of requested type
1475          switch (display_type) {
1476                  case DISPLAY_WINDOW:
1477 <                        drv = new driver_window(mode);
1477 >                        drv = new driver_window(*this);
1478                          break;
1479   #ifdef ENABLE_FBDEV_DGA
1480                  case DISPLAY_DGA:
1481 <                        drv = new driver_fbdev(mode);
1481 >                        drv = new driver_fbdev(*this);
1482                          break;
1483   #endif
1484   #ifdef ENABLE_XF86_DGA
1485                  case DISPLAY_DGA:
1486 <                        drv = new driver_xf86dga(mode);
1486 >                        drv = new driver_xf86dga(*this);
1487                          break;
1488   #endif
1489          }
# Line 1188 | Line 1497 | static bool video_open(const video_mode
1497  
1498   #ifdef ENABLE_VOSF
1499          if (use_vosf) {
1500 <                // Initialize the mainBuffer structure
1501 <                if (!video_init_buffer()) {
1500 >                // Initialize the VOSF system
1501 >                if (!video_vosf_init(*this)) {
1502                          ErrorAlert(STR_VOSF_INIT_ERR);
1503                  return false;
1504                  }
1196
1197                // Initialize the handler for SIGSEGV
1198                if (!sigsegv_install_handler(screen_fault_handler)) {
1199                        ErrorAlert("Could not initialize Video on SEGV signals");
1200                        return false;
1201                }
1505          }
1506   #endif
1507          
# Line 1212 | Line 1515 | static bool video_open(const video_mode
1515          // Start redraw/input thread
1516   #ifdef HAVE_PTHREADS
1517          redraw_thread_cancel = false;
1518 <        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1518 >        Set_pthread_attr(&redraw_thread_attr, 0);
1519 >        redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1520          if (!redraw_thread_active) {
1521                  printf("FATAL: cannot create redraw thread\n");
1522                  return false;
# Line 1249 | Line 1553 | bool VideoInit(bool classic)
1553          // Find screen and root window
1554          screen = XDefaultScreen(x_display);
1555          rootwin = XRootWindow(x_display, screen);
1556 <        
1557 <        // Get screen depth
1558 <        xdepth = DefaultDepth(x_display, screen);
1556 >
1557 >        // Get sorted list of available depths
1558 >        avail_depths = XListDepths(x_display, screen, &num_depths);
1559 >        if (avail_depths == NULL) {
1560 >                ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1561 >                return false;
1562 >        }
1563 >        std::sort(avail_depths, avail_depths + num_depths);
1564          
1565   #ifdef ENABLE_FBDEV_DGA
1566          // Frame buffer name
# Line 1291 | Line 1600 | bool VideoInit(bool classic)
1600          black_pixel = BlackPixel(x_display, screen);
1601          white_pixel = WhitePixel(x_display, screen);
1602  
1294        // Get appropriate visual
1295        int color_class;
1296        switch (xdepth) {
1297                case 1:
1298                        color_class = StaticGray;
1299                        break;
1300                case 8:
1301                        color_class = PseudoColor;
1302                        break;
1303                case 15:
1304                case 16:
1305                case 24:
1306                case 32: // Try DirectColor first, as this will allow gamma correction
1307                        color_class = DirectColor;
1308                        if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo))
1309                                color_class = TrueColor;
1310                        break;
1311                default:
1312                        ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1313                        return false;
1314        }
1315        if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
1316                ErrorAlert(STR_NO_XVISUAL_ERR);
1317                return false;
1318        }
1319        if (visualInfo.depth != xdepth) {
1320                ErrorAlert(STR_NO_XVISUAL_ERR);
1321                return false;
1322        }
1323        vis = visualInfo.visual;
1324
1325        // Create color maps
1326        if (color_class == PseudoColor || color_class == DirectColor) {
1327                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1328                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1329        }
1330
1331        // Find pixel format of direct modes
1332        if (color_class == DirectColor || color_class == TrueColor) {
1333                rshift = gshift = bshift = 0;
1334                rloss = gloss = bloss = 8;
1335                uint32 mask;
1336                for (mask=vis->red_mask; !(mask&1); mask>>=1)
1337                        ++rshift;
1338                for (; mask&1; mask>>=1)
1339                        --rloss;
1340                for (mask=vis->green_mask; !(mask&1); mask>>=1)
1341                        ++gshift;
1342                for (; mask&1; mask>>=1)
1343                        --gloss;
1344                for (mask=vis->blue_mask; !(mask&1); mask>>=1)
1345                        ++bshift;
1346                for (; mask&1; mask>>=1)
1347                        --bloss;
1348        }
1349
1350        // Preset palette pixel values for gamma table
1351        if (color_class == DirectColor) {
1352                int num = vis->map_entries;
1353                for (int i=0; i<num; i++) {
1354                        int c = (i * 256) / num;
1355                        palette[i].pixel = map_rgb(c, c, c);
1356                }
1357        }
1358
1603          // Get screen mode from preferences
1604          const char *mode_str;
1605          if (classic_mode)
# Line 1390 | Line 1634 | bool VideoInit(bool classic)
1634                  default_height = DisplayHeight(x_display, screen);
1635  
1636          // Mac screen depth follows X depth
1637 <        video_depth default_depth = DepthModeForPixelDepth(xdepth);
1637 >        video_depth default_depth = VDEPTH_1BIT;
1638 >        switch (DefaultDepth(x_display, screen)) {
1639 >                case 8:
1640 >                        default_depth = VDEPTH_8BIT;
1641 >                        break;
1642 >                case 15: case 16:
1643 >                        default_depth = VDEPTH_16BIT;
1644 >                        break;
1645 >                case 24: case 32:
1646 >                        default_depth = VDEPTH_32BIT;
1647 >                        break;
1648 >        }
1649  
1650          // Construct list of supported modes
1651          if (display_type == DISPLAY_WINDOW) {
1652                  if (classic)
1653                          add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
1654                  else {
1655 <                        if (default_depth != VDEPTH_1BIT)
1656 <                                add_window_modes(VDEPTH_1BIT);  // 1-bit modes are always available
1657 < #ifdef ENABLE_VOSF
1403 <                        if (default_depth > VDEPTH_8BIT) {
1404 <                                add_window_modes(VDEPTH_2BIT);  // 2, 4 and 8-bit modes are also possible on 16/32-bit screens with VOSF blitters
1405 <                                add_window_modes(VDEPTH_4BIT);
1406 <                                add_window_modes(VDEPTH_8BIT);
1655 >                        for (unsigned d=VDEPTH_1BIT; d<=VDEPTH_32BIT; d++) {
1656 >                                if (find_visual_for_depth(video_depth(d)))
1657 >                                        add_window_modes(video_depth(d));
1658                          }
1408 #endif
1409                        add_window_modes(default_depth);
1659                  }
1660          } else
1661                  add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
1662 +        if (VideoModes.empty()) {
1663 +                ErrorAlert(STR_NO_XVISUAL_ERR);
1664 +                return false;
1665 +        }
1666  
1667 <        // Find requested default mode and open display
1668 <        if (VideoModes.size() == 1)
1669 <                return video_open(VideoModes[0]);
1670 <        else {
1671 <                // Find mode with specified dimensions
1672 <                std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1673 <                for (i = VideoModes.begin(); i != end; ++i) {
1421 <                        if (i->x == default_width && i->y == default_height && i->depth == default_depth)
1422 <                                return video_open(*i);
1667 >        // Find requested default mode with specified dimensions
1668 >        uint32 default_id;
1669 >        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1670 >        for (i = VideoModes.begin(); i != end; ++i) {
1671 >                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1672 >                        default_id = i->resolution_id;
1673 >                        break;
1674                  }
1424                return video_open(VideoModes[0]);
1675          }
1676 +        if (i == end) { // not found, use first available mode
1677 +                default_depth = VideoModes[0].depth;
1678 +                default_id = VideoModes[0].resolution_id;
1679 +        }
1680 +
1681 + #if DEBUG
1682 +        D(bug("Available video modes:\n"));
1683 +        for (i = VideoModes.begin(); i != end; ++i) {
1684 +                int bits = 1 << i->depth;
1685 +                if (bits == 16)
1686 +                        bits = 15;
1687 +                else if (bits == 32)
1688 +                        bits = 24;
1689 +                D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1690 +        }
1691 + #endif
1692 +
1693 +        // Create X11_monitor_desc for this (the only) display
1694 +        X11_monitor_desc *monitor = new X11_monitor_desc(VideoModes, default_depth, default_id);
1695 +        VideoMonitors.push_back(monitor);
1696 +
1697 +        // Open display
1698 +        return monitor->video_open();
1699   }
1700  
1701  
# Line 1431 | Line 1704 | bool VideoInit(bool classic)
1704   */
1705  
1706   // Close display
1707 < static void video_close(void)
1707 > void X11_monitor_desc::video_close(void)
1708   {
1709 +        D(bug("video_close()\n"));
1710 +
1711          // Stop redraw thread
1712   #ifdef HAVE_PTHREADS
1713          if (redraw_thread_active) {
# Line 1448 | Line 1723 | static void video_close(void)
1723          // Unlock frame buffer
1724          UNLOCK_FRAME_BUFFER;
1725          XSync(x_display, false);
1726 +        D(bug(" frame buffer unlocked\n"));
1727  
1728   #ifdef ENABLE_VOSF
1453        // Deinitialize VOSF
1729          if (use_vosf) {
1730 <                if (mainBuffer.pageInfo) {
1731 <                        free(mainBuffer.pageInfo);
1457 <                        mainBuffer.pageInfo = NULL;
1458 <                }
1459 <                if (mainBuffer.dirtyPages) {
1460 <                        free(mainBuffer.dirtyPages);
1461 <                        mainBuffer.dirtyPages = NULL;
1462 <                }
1730 >                // Deinitialize VOSF
1731 >                video_vosf_exit();
1732          }
1733   #endif
1734  
1735          // Close display
1736          delete drv;
1737          drv = NULL;
1469 }
1470
1471 void VideoExit(void)
1472 {
1473        // Close display
1474        video_close();
1738  
1739          // Free colormaps
1740          if (cmap[0]) {
# Line 1482 | Line 1745 | void VideoExit(void)
1745                  XFreeColormap(x_display, cmap[1]);
1746                  cmap[1] = 0;
1747          }
1748 + }
1749 +
1750 + void VideoExit(void)
1751 + {
1752 +        // Close displays
1753 +        vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
1754 +        for (i = VideoMonitors.begin(); i != end; ++i)
1755 +                dynamic_cast<X11_monitor_desc *>(*i)->video_close();
1756  
1757   #ifdef ENABLE_XF86_VIDMODE
1758          // Free video mode list
# Line 1498 | Line 1769 | void VideoExit(void)
1769                  fbdev_fd = -1;
1770          }
1771   #endif
1772 +
1773 +        // Free depth list
1774 +        if (avail_depths) {
1775 +                XFree(avail_depths);
1776 +                avail_depths = NULL;
1777 +        }
1778   }
1779  
1780  
# Line 1533 | Line 1810 | void VideoInterrupt(void)
1810   *  Set palette
1811   */
1812  
1813 < void video_set_palette(uint8 *pal, int num_in)
1813 > void X11_monitor_desc::set_palette(uint8 *pal, int num_in)
1814   {
1815 +        const video_mode &mode = get_current_mode();
1816 +
1817          LOCK_PALETTE;
1818  
1819          // Convert colors to XColor array
1820          int num_out = 256;
1821 <        if (IsDirectMode(VideoMonitor.mode)) {
1821 >        bool stretch = false;
1822 >        if (IsDirectMode(mode)) {
1823                  // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1824                  num_out = vis->map_entries;
1825 +                stretch = true;
1826          }
1827 <        XColor *p = palette;
1827 >        XColor *p = x_palette;
1828          for (int i=0; i<num_out; i++) {
1829 <                int c = (i * num_in) / num_out;
1829 >                int c = (stretch ? (i * num_in) / num_out : i);
1830                  p->red = pal[c*3 + 0] * 0x0101;
1831                  p->green = pal[c*3 + 1] * 0x0101;
1832                  p->blue = pal[c*3 + 2] * 0x0101;
1552                if (vis->c_class == PseudoColor)
1553                        p->pixel = i;
1554                p->flags = DoRed | DoGreen | DoBlue;
1833                  p++;
1834          }
1835  
1836   #ifdef ENABLE_VOSF
1837          // Recalculate pixel color expansion map
1838 <        if (!IsDirectMode(VideoMonitor.mode) && (vis->c_class == TrueColor || vis->c_class == DirectColor)) {
1838 >        if (!IsDirectMode(mode) && xdepth > 8) {
1839                  for (int i=0; i<256; i++) {
1840                          int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1841                          ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
# Line 1567 | Line 1845 | void video_set_palette(uint8 *pal, int n
1845                  LOCK_VOSF;
1846                  PFLAG_SET_ALL;
1847                  UNLOCK_VOSF;
1848 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1848 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1849          }
1850   #endif
1851  
1852          // Tell redraw thread to change palette
1853 <        palette_changed = true;
1853 >        x_palette_changed = true;
1854  
1855          UNLOCK_PALETTE;
1856   }
# Line 1582 | Line 1860 | void video_set_palette(uint8 *pal, int n
1860   *  Switch video mode
1861   */
1862  
1863 < void video_switch_to_mode(const video_mode &mode)
1863 > void X11_monitor_desc::switch_to_current_mode(void)
1864   {
1865          // Close and reopen display
1866          video_close();
1867 <        video_open(mode);
1867 >        video_open();
1868  
1869          if (drv == NULL) {
1870                  ErrorAlert(STR_OPEN_WINDOW_ERR);
# Line 1770 | Line 2048 | static void handle_events(void)
2048                  XNextEvent(x_display, &event);
2049  
2050                  switch (event.type) {
2051 +
2052                          // Mouse button
2053                          case ButtonPress: {
2054                                  unsigned int button = event.xbutton.button;
# Line 1802 | Line 2081 | static void handle_events(void)
2081                                  drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2082                                  break;
2083  
2084 +                        // Mouse entered window
2085 +                        case EnterNotify:
2086 +                                if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
2087 +                                        drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2088 +                                break;
2089 +
2090                          // Keyboard
2091                          case KeyPress: {
2092                                  int code = -1;
# Line 1849 | Line 2134 | static void handle_events(void)
2134                          // Hidden parts exposed, force complete refresh of window
2135                          case Expose:
2136                                  if (display_type == DISPLAY_WINDOW) {
2137 +                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
2138   #ifdef ENABLE_VOSF
2139                                          if (use_vosf) {                 // VOSF refresh
2140                                                  LOCK_VOSF;
2141                                                  PFLAG_SET_ALL;
2142                                                  UNLOCK_VOSF;
2143 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2143 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2144                                          }
2145                                          else
2146   #endif
# Line 1865 | Line 2151 | static void handle_events(void)
2151                                                          updt_box[x1][y1] = true;
2152                                                  nr_boxes = 16 * 16;
2153                                          } else                                  // Static refresh
2154 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2154 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2155                                  }
2156                                  break;
2157  
# Line 1891 | Line 2177 | static void update_display_dynamic(int t
2177          int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
2178          int xil = 0;
2179          int rxm = 0, rxmo = 0;
2180 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2181 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2182 <        int rx = VideoMonitor.mode.bytes_per_row / 16;
2183 <        int ry = VideoMonitor.mode.y / 16;
2180 >        const video_mode &mode = drv->monitor.get_current_mode();
2181 >        int bytes_per_row = mode.bytes_per_row;
2182 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2183 >        int rx = mode.bytes_per_row / 16;
2184 >        int ry = mode.y / 16;
2185          int max_box;
2186  
2187          y2s = sm_uptd[ticker % 8];
# Line 1948 | Line 2235 | static void update_display_dynamic(int t
2235                                          i = (yi * bytes_per_row) + xi;
2236                                          for (y2=0; y2 < yil; y2++, i += bytes_per_row)
2237                                                  memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
2238 <                                        if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2238 >                                        if (mode.depth == VDEPTH_1BIT) {
2239                                                  if (drv->have_shm)
2240                                                          XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
2241                                                  else
# Line 1979 | Line 2266 | static void update_display_dynamic(int t
2266   static void update_display_static(driver_window *drv)
2267   {
2268          // Incremental update code
2269 <        int wide = 0, high = 0, x1, x2, y1, y2, i, j;
2270 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2271 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2269 >        unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
2270 >        const video_mode &mode = drv->monitor.get_current_mode();
2271 >        int bytes_per_row = mode.bytes_per_row;
2272 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2273          uint8 *p, *p2;
2274  
2275          // Check for first line from top and first line from bottom that have changed
2276          y1 = 0;
2277 <        for (j=0; j<VideoMonitor.mode.y; j++) {
2277 >        for (j=0; j<mode.y; j++) {
2278                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2279                          y1 = j;
2280                          break;
2281                  }
2282          }
2283          y2 = y1 - 1;
2284 <        for (j=VideoMonitor.mode.y-1; j>=y1; j--) {
2284 >        for (j=mode.y-1; j>=y1; j--) {
2285                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2286                          y2 = j;
2287                          break;
# Line 2003 | Line 2291 | static void update_display_static(driver
2291  
2292          // Check for first column from left and first column from right that have changed
2293          if (high) {
2294 <                if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2295 <                        x1 = VideoMonitor.mode.x - 1;
2294 >                if (mode.depth == VDEPTH_1BIT) {
2295 >                        x1 = mode.x - 1;
2296                          for (j=y1; j<=y2; j++) {
2297                                  p = &the_buffer[j * bytes_per_row];
2298                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2022 | Line 2310 | static void update_display_static(driver
2310                                  p2 = &the_buffer_copy[j * bytes_per_row];
2311                                  p += bytes_per_row;
2312                                  p2 += bytes_per_row;
2313 <                                for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) {
2313 >                                for (i=(mode.x>>3); i>(x2>>3); i--) {
2314                                          p--; p2--;
2315                                          if (*p != *p2) {
2316                                                  x2 = (i << 3) + 7;
# Line 2041 | Line 2329 | static void update_display_static(driver
2329                          }
2330  
2331                  } else {
2332 <                        x1 = VideoMonitor.mode.x;
2332 >                        x1 = mode.x;
2333                          for (j=y1; j<=y2; j++) {
2334                                  p = &the_buffer[j * bytes_per_row];
2335                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2059 | Line 2347 | static void update_display_static(driver
2347                                  p2 = &the_buffer_copy[j * bytes_per_row];
2348                                  p += bytes_per_row;
2349                                  p2 += bytes_per_row;
2350 <                                for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2350 >                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2351                                          p--;
2352                                          p2--;
2353                                          if (*p != *p2) {
# Line 2126 | Line 2414 | static inline void handle_palette_change
2414   {
2415          LOCK_PALETTE;
2416  
2417 <        if (palette_changed) {
2418 <                palette_changed = false;
2417 >        if (x_palette_changed) {
2418 >                x_palette_changed = false;
2419                  drv->update_palette();
2420          }
2421  
# Line 2138 | Line 2426 | static void video_refresh_dga(void)
2426   {
2427          // Quit DGA mode if requested
2428          possibly_quit_dga_mode();
2141        
2142        // Handle X events
2143        handle_events();
2144        
2145        // Handle palette changes
2146        handle_palette_changes();
2429   }
2430  
2431   #ifdef ENABLE_VOSF
# Line 2153 | Line 2435 | static void video_refresh_dga_vosf(void)
2435          // Quit DGA mode if requested
2436          possibly_quit_dga_mode();
2437          
2156        // Handle X events
2157        handle_events();
2158        
2159        // Handle palette changes
2160        handle_palette_changes();
2161        
2438          // Update display (VOSF variant)
2439          static int tick_counter = 0;
2440          if (++tick_counter >= frame_skip) {
# Line 2177 | Line 2453 | static void video_refresh_window_vosf(vo
2453          // Ungrab mouse if requested
2454          possibly_ungrab_mouse();
2455          
2180        // Handle X events
2181        handle_events();
2182        
2183        // Handle palette changes
2184        handle_palette_changes();
2185        
2456          // Update display (VOSF variant)
2457          static int tick_counter = 0;
2458          if (++tick_counter >= frame_skip) {
# Line 2202 | Line 2472 | static void video_refresh_window_static(
2472          // Ungrab mouse if requested
2473          possibly_ungrab_mouse();
2474  
2205        // Handle X events
2206        handle_events();
2207        
2208        // Handle_palette changes
2209        handle_palette_changes();
2210        
2475          // Update display (static variant)
2476          static int tick_counter = 0;
2477          if (++tick_counter >= frame_skip) {
# Line 2221 | Line 2485 | static void video_refresh_window_dynamic
2485          // Ungrab mouse if requested
2486          possibly_ungrab_mouse();
2487  
2224        // Handle X events
2225        handle_events();
2226        
2227        // Handle_palette changes
2228        handle_palette_changes();
2229        
2488          // Update display (dynamic variant)
2489          static int tick_counter = 0;
2490          tick_counter++;
# Line 2262 | Line 2520 | static void VideoRefreshInit(void)
2520          }
2521   }
2522  
2523 + // This function is called on non-threaded platforms from a timer interrupt
2524   void VideoRefresh(void)
2525   {
2526          // We need to check redraw_thread_active to inhibit refreshed during
2527          // mode changes on non-threaded platforms
2528 <        if (redraw_thread_active)
2529 <                video_refresh();
2528 >        if (!redraw_thread_active)
2529 >                return;
2530 >
2531 >        // Handle X events
2532 >        handle_events();
2533 >
2534 >        // Handle palette changes
2535 >        handle_palette_changes();
2536 >
2537 >        // Update display
2538 >        video_refresh();
2539   }
2540  
2541 + const int VIDEO_REFRESH_HZ = 60;
2542 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2543 +
2544   #ifdef HAVE_PTHREADS
2545   static void *redraw_func(void *arg)
2546   {
2547 +        int fd = ConnectionNumber(x_display);
2548 +
2549          uint64 start = GetTicks_usec();
2550          int64 ticks = 0;
2551 <        uint64 next = GetTicks_usec();
2551 >        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2552 >
2553          while (!redraw_thread_cancel) {
2554 <                video_refresh();
2281 <                next += 16667;
2554 >
2555                  int64 delay = next - GetTicks_usec();
2556 <                if (delay > 0)
2557 <                        Delay_usec(delay);
2558 <                else if (delay < -16667)
2556 >                if (delay < -VIDEO_REFRESH_DELAY) {
2557 >
2558 >                        // We are lagging far behind, so we reset the delay mechanism
2559                          next = GetTicks_usec();
2560 <                ticks++;
2560 >
2561 >                } else if (delay <= 0) {
2562 >
2563 >                        // Delay expired, refresh display
2564 >                        handle_events();
2565 >                        handle_palette_changes();
2566 >                        video_refresh();
2567 >                        next += VIDEO_REFRESH_DELAY;
2568 >                        ticks++;
2569 >
2570 >                } else {
2571 >
2572 >                        // No display refresh pending, check for X events
2573 >                        fd_set readfds;
2574 >                        FD_ZERO(&readfds);
2575 >                        FD_SET(fd, &readfds);
2576 >                        struct timeval timeout;
2577 >                        timeout.tv_sec = 0;
2578 >                        timeout.tv_usec = delay;
2579 >                        if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2580 >                                handle_events();
2581 >                }
2582          }
2583 +
2584          uint64 end = GetTicks_usec();
2585 <        // printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
2585 >        D(bug("%Ld refreshes in %Ld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2586          return NULL;
2587   }
2588   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines