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.63 by cebix, 2002-01-15T14:58:37Z vs.
Revision 1.85 by asvitkine, 2009-02-19T07:09:35Z

# Line 1 | Line 1
1   /*
2   *  video_x.cpp - Video/graphics emulation, X11 specific stuff
3   *
4 < *  Basilisk II (C) 1997-2002 Christian Bauer
4 > *  Basilisk II (C) 1997-2008 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 39 | Line 39
39  
40   #include <algorithm>
41  
42 #ifndef NO_STD_NAMESPACE
43 using std::sort;
44 #endif
45
42   #ifdef HAVE_PTHREADS
43   # include <pthread.h>
44   #endif
# Line 66 | Line 62 | using std::sort;
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 97 | Line 97 | static uint32 the_buffer_size;                                         // S
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 volatile bool redraw_thread_cancel_ack;          // Flag: Acknowledge for redraw thread cancellation
103   static pthread_t redraw_thread;                                         // Redraw thread
104   #endif
105  
# Line 122 | Line 124 | static bool use_keycodes = false;                                      //
124   static int keycode_table[256];                                          // X keycode -> Mac keycode translation table
125  
126   // X11 variables
127 + char *x_display_name = NULL;                                            // X11 display name
128 + Display *x_display = NULL;                                                      // X11 display handle
129   static int screen;                                                                      // Screen number
130   static Window rootwin;                                                          // Root window and our window
131   static int num_depths = 0;                                                      // Number of available X depths
# Line 131 | Line 135 | static unsigned long black_pixel, white_
135   static int eventmask;
136  
137   static int xdepth;                                                                      // Depth of X screen
138 + static VisualFormat visualFormat;
139   static XVisualInfo visualInfo;
140   static Visual *vis;
141   static int color_class;
142  
143 + static bool x_native_byte_order;                                                // XImage has native byte order?
144   static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
145  
146   static Colormap cmap[2] = {0, 0};                                       // Colormaps for indexed modes (DGA needs two of them)
147  
148 < static XColor palette[256];                                                     // Color palette to be used as CLUT and gamma table
149 < static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
148 > static XColor x_palette[256];                                           // Color palette to be used as CLUT and gamma table
149 > static bool x_palette_changed = false;                          // Flag: Palette changed, redraw thread must set new colors
150  
151   #ifdef ENABLE_FBDEV_DGA
152   static int fbdev_fd = -1;
# Line 153 | Line 159 | static int num_x_video_modes;
159  
160   // Mutex to protect palette
161   #ifdef HAVE_PTHREADS
162 < static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;
163 < #define LOCK_PALETTE pthread_mutex_lock(&palette_lock)
164 < #define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock)
162 > static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER;
163 > #define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock)
164 > #define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock)
165   #else
166   #define LOCK_PALETTE
167   #define UNLOCK_PALETTE
# Line 192 | Line 198 | extern Display *x_display;
198   // From sys_unix.cpp
199   extern void SysMountFirstFloppy(void);
200  
201 + // From clip_unix.cpp
202 + extern void ClipboardSelectionClear(XSelectionClearEvent *);
203 + extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
204 +
205 +
206 + /*
207 + *  monitor_desc subclass for X11 display
208 + */
209 +
210 + class X11_monitor_desc : public monitor_desc {
211 + public:
212 +        X11_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
213 +        ~X11_monitor_desc() {}
214 +
215 +        virtual void switch_to_current_mode(void);
216 +        virtual void set_palette(uint8 *pal, int num);
217 +
218 +        bool video_open(void);
219 +        void video_close(void);
220 + };
221 +
222  
223   /*
224   *  Utility functions
225   */
226  
227 + // Map video_mode depth ID to numerical depth value
228 + static inline int depth_of_video_mode(video_mode const & mode)
229 + {
230 +        int depth = -1;
231 +        switch (mode.depth) {
232 +        case VDEPTH_1BIT:
233 +                depth = 1;
234 +                break;
235 +        case VDEPTH_2BIT:
236 +                depth = 2;
237 +                break;
238 +        case VDEPTH_4BIT:
239 +                depth = 4;
240 +                break;
241 +        case VDEPTH_8BIT:
242 +                depth = 8;
243 +                break;
244 +        case VDEPTH_16BIT:
245 +                depth = 16;
246 +                break;
247 +        case VDEPTH_32BIT:
248 +                depth = 32;
249 +                break;
250 +        default:
251 +                abort();
252 +        }
253 +        return depth;
254 + }
255 +
256   // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
257 < static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
257 > static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue, bool fix_byte_order = false)
258   {
259 <        return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
259 >        uint32 val = ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
260 >        if (fix_byte_order && !x_native_byte_order) {
261 >                // We have to fix byte order in the ExpandMap[]
262 >                // NOTE: this is only an optimization since Screen_blitter_init()
263 >                // could be arranged to choose an NBO or OBO (with
264 >                // byteswapping) Blit_Expand_X_To_Y() function
265 >                switch (visualFormat.depth) {
266 >                case 15: case 16:
267 >                        val = do_byteswap_16(val);
268 >                        break;
269 >                case 24: case 32:
270 >                        val = do_byteswap_32(val);
271 >                        break;
272 >                }
273 >        }
274 >        return val;
275   }
276  
277   // Do we have a visual for handling the specified Mac depth? If so, set the
# Line 331 | Line 402 | static void add_window_modes(video_depth
402   }
403  
404   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
405 < static void set_mac_frame_buffer(video_depth depth, bool native_byte_order)
405 > static void set_mac_frame_buffer(X11_monitor_desc &monitor, video_depth depth, bool native_byte_order)
406   {
407   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
408          int layout = FLAYOUT_DIRECT;
# Line 343 | Line 414 | static void set_mac_frame_buffer(video_d
414                  MacFrameLayout = layout;
415          else
416                  MacFrameLayout = FLAYOUT_DIRECT;
417 <        VideoMonitor.mac_frame_base = MacFrameBaseMac;
417 >        monitor.set_mac_frame_base(MacFrameBaseMac);
418  
419          // Set variables used by UAE memory banking
420 +        const video_mode &mode = monitor.get_current_mode();
421          MacFrameBaseHost = the_buffer;
422 <        MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
422 >        MacFrameSize = mode.bytes_per_row * mode.y;
423          InitFrameBufferMapping();
424   #else
425 <        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
425 >        monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
426   #endif
427 <        D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base));
427 >        D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
428   }
429  
430   // Set window name and class
# Line 425 | Line 497 | static int error_handler(Display *d, XEr
497  
498  
499   /*
500 + *  Framebuffer allocation routines
501 + */
502 +
503 + #ifdef ENABLE_VOSF
504 + #include "vm_alloc.h"
505 +
506 + static void *vm_acquire_framebuffer(uint32 size)
507 + {
508 +        // always try to allocate framebuffer at the same address
509 +        static void *fb = VM_MAP_FAILED;
510 +        if (fb != VM_MAP_FAILED) {
511 +                if (vm_acquire_fixed(fb, size) < 0)
512 +                        fb = VM_MAP_FAILED;
513 +        }
514 +        if (fb == VM_MAP_FAILED)
515 +                fb = vm_acquire(size, VM_MAP_DEFAULT | VM_MAP_32BIT);
516 +        return fb;
517 + }
518 +
519 + static inline void vm_release_framebuffer(void *fb, uint32 size)
520 + {
521 +        vm_release(fb, size);
522 + }
523 + #endif
524 +
525 +
526 + /*
527   *  Display "driver" classes
528   */
529  
530   class driver_base {
531   public:
532 <        driver_base();
532 >        driver_base(X11_monitor_desc &m);
533          virtual ~driver_base();
534  
535          virtual void update_palette(void);
# Line 446 | Line 545 | public:
545          virtual void ungrab_mouse(void) {}
546  
547   public:
548 +        X11_monitor_desc &monitor; // Associated video monitor
549 +        const video_mode &mode;    // Video mode handled by the driver
550 +
551          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
552          Window w;               // The window we draw into
553  
# Line 463 | Line 565 | class driver_window : public driver_base
565          friend void update_display_static(driver_window *drv);
566  
567   public:
568 <        driver_window(const video_mode &mode);
568 >        driver_window(X11_monitor_desc &monitor);
569          ~driver_window();
570  
571          void toggle_mouse_grab(void);
# Line 482 | Line 584 | private:
584          int mouse_last_x, mouse_last_y; // Last mouse position (for relative mode)
585   };
586  
587 + class driver_dga;
588 + static void update_display_dga_vosf(driver_dga *drv);
589 +
590 + class driver_dga : public driver_base {
591 +        friend void update_display_dga_vosf(driver_dga *drv);
592 +
593 + public:
594 +        driver_dga(X11_monitor_desc &monitor);
595 +        ~driver_dga();
596 +
597 +        void suspend(void);
598 +        void resume(void);
599 +
600 + protected:
601 +        struct FakeXImage {
602 +                int width, height;              // size of image
603 +                int depth;                              // depth of image
604 +                int bytes_per_line;             // accelerator to next line
605 +
606 +                FakeXImage(int w, int h, int d)
607 +                        : width(w), height(h), depth(d)
608 +                        { bytes_per_line = TrivialBytesPerRow(width, DepthModeForPixelDepth(depth)); }
609 +        };
610 +        FakeXImage *img;
611 +
612 + private:
613 +        Window suspend_win;             // "Suspend" information window
614 +        void *fb_save;                  // Saved frame buffer for suspend/resume
615 + };
616 +
617   static driver_base *drv = NULL; // Pointer to currently used driver object
618  
619   #ifdef ENABLE_VOSF
620   # include "video_vosf.h"
621   #endif
622  
623 < driver_base::driver_base()
624 < : init_ok(false), w(0)
623 > driver_base::driver_base(X11_monitor_desc &m)
624 > : monitor(m), mode(m.get_current_mode()), init_ok(false), w(0)
625   {
626          the_buffer = NULL;
627          the_buffer_copy = NULL;
# Line 526 | Line 658 | driver_base::~driver_base()
658                  // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
659                  if (the_buffer != VM_MAP_FAILED) {
660                          D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
661 <                        vm_release(the_buffer, the_buffer_size);
661 >                        vm_release_framebuffer(the_buffer, the_buffer_size);
662                          the_buffer = NULL;
663                  }
664                  if (the_host_buffer) {
# Line 548 | Line 680 | void driver_base::update_palette(void)
680   {
681          if (color_class == PseudoColor || color_class == DirectColor) {
682                  int num = vis->map_entries;
683 <                if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor)
683 >                if (!IsDirectMode(monitor.get_current_mode()) && color_class == DirectColor)
684                          return; // Indexed mode on true color screen, don't set CLUT
685 <                XStoreColors(x_display, cmap[0], palette, num);
686 <                XStoreColors(x_display, cmap[1], palette, num);
685 >                XStoreColors(x_display, cmap[0], x_palette, num);
686 >                XStoreColors(x_display, cmap[1], x_palette, num);
687          }
688          XSync(x_display, false);
689   }
# Line 574 | Line 706 | void driver_base::restore_mouse_accel(vo
706   */
707  
708   // Open display
709 < driver_window::driver_window(const video_mode &mode)
710 < : gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
709 > driver_window::driver_window(X11_monitor_desc &m)
710 > : driver_base(m), gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
711   {
712          int width = mode.x, height = mode.y;
713          int aligned_width = (width + 15) & ~15;
# Line 676 | Line 808 | driver_window::driver_window(const video
808          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
809          the_host_buffer = the_buffer_copy;
810          the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
811 <        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
811 >        the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
812          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
813          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
814   #else
# Line 697 | Line 829 | driver_window::driver_window(const video
829          XDefineCursor(x_display, w, mac_cursor);
830  
831          // Init blitting routines
700        bool native_byte_order;
701 #ifdef WORDS_BIGENDIAN
702        native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
703 #else
704        native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
705 #endif
832   #ifdef ENABLE_VOSF
833 <        Screen_blitter_init(&visualInfo, native_byte_order, mode.depth);
833 >        Screen_blitter_init(visualFormat, x_native_byte_order, depth_of_video_mode(mode));
834   #endif
835  
836 <        // Set VideoMonitor
837 <        VideoMonitor.mode = mode;
712 <        set_mac_frame_buffer(mode.depth, native_byte_order);
836 >        // Set frame buffer base
837 >        set_mac_frame_buffer(monitor, mode.depth, x_native_byte_order);
838  
839          // Everything went well
840          init_ok = true;
# Line 789 | Line 914 | void driver_window::mouse_moved(int x, i
914          // Warped mouse motion (this code is taken from SDL)
915  
916          // Post first mouse event
917 <        int width = VideoMonitor.mode.x, height = VideoMonitor.mode.y;
917 >        int width = monitor.get_current_mode().x, height = monitor.get_current_mode().y;
918          int delta_x = x - mouse_last_x, delta_y = y - mouse_last_y;
919          mouse_last_x = x; mouse_last_y = y;
920          ADBMouseMoved(delta_x, delta_y);
# Line 824 | Line 949 | void driver_window::mouse_moved(int x, i
949   *  DGA display driver base class
950   */
951  
952 < class driver_dga : public driver_base {
953 < public:
829 <        driver_dga();
830 <        ~driver_dga();
831 <
832 <        void suspend(void);
833 <        void resume(void);
834 <
835 < private:
836 <        Window suspend_win;             // "Suspend" information window
837 <        void *fb_save;                  // Saved frame buffer for suspend/resume
838 < };
839 <
840 < driver_dga::driver_dga()
841 < : suspend_win(0), fb_save(NULL)
952 > driver_dga::driver_dga(X11_monitor_desc &m)
953 >        : driver_base(m), suspend_win(0), fb_save(NULL), img(NULL)
954   {
955   }
956  
# Line 846 | Line 958 | driver_dga::~driver_dga()
958   {
959          XUngrabPointer(x_display, CurrentTime);
960          XUngrabKeyboard(x_display, CurrentTime);
961 +
962 +        if (img)
963 +                delete img;
964   }
965  
966   // Suspend emulation
# Line 859 | Line 974 | void driver_dga::suspend(void)
974          LOCK_FRAME_BUFFER;
975  
976          // Save frame buffer
977 <        fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
977 >        fb_save = malloc(mode.y * mode.bytes_per_row);
978          if (fb_save)
979 <                memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
979 >                memcpy(fb_save, the_buffer, mode.y * mode.bytes_per_row);
980  
981          // Close full screen display
982   #ifdef ENABLE_XF86_DGA
# Line 914 | Line 1029 | void driver_dga::resume(void)
1029                  LOCK_VOSF;
1030                  PFLAG_SET_ALL;
1031                  UNLOCK_VOSF;
1032 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1032 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1033          }
1034   #endif
1035          
# Line 924 | Line 1039 | void driver_dga::resume(void)
1039                  // Don't copy fb_save to the temporary frame buffer in VOSF mode
1040                  if (!use_vosf)
1041   #endif
1042 <                memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
1042 >                memcpy(the_buffer, fb_save, mode.y * mode.bytes_per_row);
1043                  free(fb_save);
1044                  fb_save = NULL;
1045          }
# Line 946 | Line 1061 | const char FBDEVICE_FILE_NAME[] = "/dev/
1061  
1062   class driver_fbdev : public driver_dga {
1063   public:
1064 <        driver_fbdev(const video_mode &mode);
1064 >        driver_fbdev(X11_monitor_desc &monitor);
1065          ~driver_fbdev();
1066   };
1067  
1068   // Open display
1069 < driver_fbdev::driver_fbdev(const video_mode &mode)
1069 > driver_fbdev::driver_fbdev(X11_monitor_desc &m) : driver_dga(m)
1070   {
1071          int width = mode.x, height = mode.y;
1072  
# Line 999 | Line 1114 | driver_fbdev::driver_fbdev(const video_m
1114                  if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
1115                          continue;
1116                  
1117 <                if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
1117 >                if ((sscanf(line, "%19s %d %x", fb_name, &fb_depth, &fb_offset) == 3)
1118                   && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) {
1119                          device_found = true;
1120                          break;
# Line 1066 | Line 1181 | driver_fbdev::driver_fbdev(const video_m
1181   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1182          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1183          // i.e. the framebuffer update function is not Blit_Copy_Raw
1184 <        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1184 >        use_vosf = Screen_blitter_init(visualFormat, true, mode.depth);
1185          
1186          if (use_vosf) {
1187            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1188            the_host_buffer = the_buffer;
1189            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1190            the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1191 <          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1191 >          the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
1192 >
1193 >          // Fake image for DGA/VOSF mode to know about display bounds
1194 >          img = new FakeXImage(width, height, depth_of_video_mode(mode));
1195          }
1196   #else
1197          use_vosf = false;
1198   #endif
1199   #endif
1200          
1201 <        // Set VideoMonitor
1202 <        VideoModes[0].bytes_per_row = bytes_per_row;
1203 <        VideoModes[0].depth = DepthModeForPixelDepth(fb_depth);
1204 <        VideoMonitor.mode = mode;
1087 <        set_mac_frame_buffer(mode.depth, true);
1201 >        // Set frame buffer base
1202 >        const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1203 >        const_cast<video_mode *>(&mode)->depth = DepthModeForPixelDepth(fb_depth);
1204 >        set_mac_frame_buffer(monitor, mode.depth, true);
1205  
1206          // Everything went well
1207          init_ok = true;
# Line 1120 | Line 1237 | driver_fbdev::~driver_fbdev()
1237  
1238   class driver_xf86dga : public driver_dga {
1239   public:
1240 <        driver_xf86dga(const video_mode &mode);
1240 >        driver_xf86dga(X11_monitor_desc &monitor);
1241          ~driver_xf86dga();
1242  
1243          void update_palette(void);
# Line 1131 | Line 1248 | private:
1248   };
1249  
1250   // Open display
1251 < driver_xf86dga::driver_xf86dga(const video_mode &mode)
1252 < : current_dga_cmap(0)
1251 > driver_xf86dga::driver_xf86dga(X11_monitor_desc &m)
1252 > : driver_dga(m), current_dga_cmap(0)
1253   {
1254          int width = mode.x, height = mode.y;
1255  
# Line 1159 | Line 1276 | driver_xf86dga::driver_xf86dga(const vid
1276          XSetWindowAttributes wattr;
1277          wattr.event_mask = eventmask = dga_eventmask;
1278          wattr.override_redirect = True;
1279 +        wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
1280  
1281          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
1282 <                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
1282 >                InputOutput, vis, CWEventMask | CWOverrideRedirect |
1283 >                (color_class == DirectColor ? CWColormap : 0), &wattr);
1284  
1285          // Set window name/class
1286          set_window_name(w, STR_WINDOW_TITLE);
# Line 1195 | Line 1314 | driver_xf86dga::driver_xf86dga(const vid
1314  
1315          // Init blitting routines
1316          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
1317 < #if VIDEO_VOSF
1317 > #if ENABLE_VOSF
1318   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1319          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1320          // i.e. the framebuffer update function is not Blit_Copy_Raw
1321 <        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1321 >        use_vosf = Screen_blitter_init(visualFormat, x_native_byte_order, depth_of_video_mode(mode));
1322          
1323          if (use_vosf) {
1324            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1325            the_host_buffer = the_buffer;
1326            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1327            the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1328 <          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1328 >          the_buffer = (uint8 *)vm_acquire_framebuffer(the_buffer_size);
1329 >
1330 >          // Fake image for DGA/VOSF mode to know about display bounds
1331 >          img = new FakeXImage((v_width + 7) & ~7, height, depth_of_video_mode(mode));
1332          }
1333   #else
1334          use_vosf = false;
1335   #endif
1336   #endif
1337          
1338 <        // Set VideoMonitor
1338 >        // Set frame buffer base
1339          const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1340 <        VideoMonitor.mode = mode;
1219 <        set_mac_frame_buffer(mode.depth, true);
1340 >        set_mac_frame_buffer(monitor, mode.depth, true);
1341  
1342          // Everything went well
1343          init_ok = true;
# Line 1247 | Line 1368 | void driver_xf86dga::update_palette(void
1368   {
1369          driver_dga::update_palette();
1370          current_dga_cmap ^= 1;
1371 <        if (!IsDirectMode(VideoMonitor.mode) && cmap[current_dga_cmap])
1371 >        if (!IsDirectMode(monitor.get_current_mode()) && cmap[current_dga_cmap])
1372                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1373   }
1374  
# Line 1255 | Line 1376 | void driver_xf86dga::update_palette(void
1376   void driver_xf86dga::resume(void)
1377   {
1378          driver_dga::resume();
1379 <        if (!IsDirectMode(VideoMonitor.mode))
1379 >        if (!IsDirectMode(monitor.get_current_mode()))
1380                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1381   }
1382   #endif
# Line 1289 | Line 1410 | static void keycode_init(void)
1410  
1411                  // Search for server vendor string, then read keycodes
1412                  const char *vendor = ServerVendor(x_display);
1413 +                // Force use of MacX mappings on MacOS X with Apple's X server
1414 +                int dummy;
1415 +                if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy))
1416 +                        vendor = "MacX";
1417                  bool vendor_found = false;
1418                  char line[256];
1419                  while (fgets(line, 255, f)) {
# Line 1330 | Line 1455 | static void keycode_init(void)
1455          }
1456   }
1457  
1458 < // Open display for specified mode
1459 < static bool video_open(const video_mode &mode)
1458 > // Open display for current mode
1459 > bool X11_monitor_desc::video_open(void)
1460   {
1461          D(bug("video_open()\n"));
1462 +        const video_mode &mode = get_current_mode();
1463  
1464          // Find best available X visual
1465          if (!find_visual_for_depth(mode.depth)) {
# Line 1341 | Line 1467 | static bool video_open(const video_mode
1467                  return false;
1468          }
1469  
1470 +        // Determine the byte order of an XImage content
1471 + #ifdef WORDS_BIGENDIAN
1472 +        x_native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
1473 + #else
1474 +        x_native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
1475 + #endif
1476 +
1477 +        // Build up visualFormat structure
1478 +        visualFormat.fullscreen = (display_type == DISPLAY_DGA);
1479 +        visualFormat.depth = visualInfo.depth;
1480 +        visualFormat.Rmask = visualInfo.red_mask;
1481 +        visualFormat.Gmask = visualInfo.green_mask;
1482 +        visualFormat.Bmask = visualInfo.blue_mask;
1483 +
1484          // Create color maps
1485          if (color_class == PseudoColor || color_class == DirectColor) {
1486                  cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
# Line 1374 | Line 1514 | static bool video_open(const video_mode
1514                  int num = vis->map_entries;
1515                  for (int i=0; i<num; i++) {
1516                          int c = (i * 256) / num;
1517 <                        palette[i].pixel = map_rgb(c, c, c);
1518 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1517 >                        x_palette[i].pixel = map_rgb(c, c, c);
1518 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1519                  }
1520          } else if (color_class == PseudoColor) {
1521                  for (int i=0; i<256; i++) {
1522 <                        palette[i].pixel = i;
1523 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1522 >                        x_palette[i].pixel = i;
1523 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1524                  }
1525          }
1526  
# Line 1388 | Line 1528 | static bool video_open(const video_mode
1528          int num = (color_class == DirectColor ? vis->map_entries : 256);
1529          for (int i=0; i<num; i++) {
1530                  int c = (i * 256) / num;
1531 <                palette[i].red = c * 0x0101;
1532 <                palette[i].green = c * 0x0101;
1533 <                palette[i].blue = c * 0x0101;
1531 >                x_palette[i].red = c * 0x0101;
1532 >                x_palette[i].green = c * 0x0101;
1533 >                x_palette[i].blue = c * 0x0101;
1534          }
1535          if (color_class == PseudoColor || color_class == DirectColor) {
1536 <                XStoreColors(x_display, cmap[0], palette, num);
1537 <                XStoreColors(x_display, cmap[1], palette, num);
1536 >                XStoreColors(x_display, cmap[0], x_palette, num);
1537 >                XStoreColors(x_display, cmap[1], x_palette, num);
1538          }
1539  
1540   #ifdef ENABLE_VOSF
1541          // Load gray ramp to 8->16/32 expand map
1542          if (!IsDirectMode(mode) && xdepth > 8)
1543                  for (int i=0; i<256; i++)
1544 <                        ExpandMap[i] = map_rgb(i, i, i);
1544 >                        ExpandMap[i] = map_rgb(i, i, i, true);
1545   #endif
1546  
1547          // Create display driver object of requested type
1548          switch (display_type) {
1549                  case DISPLAY_WINDOW:
1550 <                        drv = new driver_window(mode);
1550 >                        drv = new driver_window(*this);
1551                          break;
1552   #ifdef ENABLE_FBDEV_DGA
1553                  case DISPLAY_DGA:
1554 <                        drv = new driver_fbdev(mode);
1554 >                        drv = new driver_fbdev(*this);
1555                          break;
1556   #endif
1557   #ifdef ENABLE_XF86_DGA
1558                  case DISPLAY_DGA:
1559 <                        drv = new driver_xf86dga(mode);
1559 >                        drv = new driver_xf86dga(*this);
1560                          break;
1561   #endif
1562          }
# Line 1431 | Line 1571 | static bool video_open(const video_mode
1571   #ifdef ENABLE_VOSF
1572          if (use_vosf) {
1573                  // Initialize the VOSF system
1574 <                if (!video_vosf_init()) {
1574 >                if (!video_vosf_init(*this)) {
1575                          ErrorAlert(STR_VOSF_INIT_ERR);
1576                  return false;
1577                  }
# Line 1446 | Line 1586 | static bool video_open(const video_mode
1586          LOCK_FRAME_BUFFER;
1587  
1588          // Start redraw/input thread
1589 < #ifdef HAVE_PTHREADS
1589 > #ifdef USE_PTHREADS_SERVICES
1590          redraw_thread_cancel = false;
1591 <        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1591 >        Set_pthread_attr(&redraw_thread_attr, 0);
1592 >        redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1593          if (!redraw_thread_active) {
1594                  printf("FATAL: cannot create redraw thread\n");
1595                  return false;
# Line 1472 | Line 1613 | bool VideoInit(bool classic)
1613          
1614          // Check if X server runs on local machine
1615          local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
1616 +                 || (strncmp(XDisplayName(x_display_name), "/", 1) == 0)
1617                   || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
1618      
1619          // Init keycode translation
# Line 1492 | Line 1634 | bool VideoInit(bool classic)
1634                  ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1635                  return false;
1636          }
1637 <        sort(avail_depths, avail_depths + num_depths);
1637 >        std::sort(avail_depths, avail_depths + num_depths);
1638          
1639   #ifdef ENABLE_FBDEV_DGA
1640          // Frame buffer name
# Line 1595 | Line 1737 | bool VideoInit(bool classic)
1737                  ErrorAlert(STR_NO_XVISUAL_ERR);
1738                  return false;
1739          }
1740 <        video_init_depth_list();
1740 >
1741 >        // Find requested default mode with specified dimensions
1742 >        uint32 default_id;
1743 >        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1744 >        for (i = VideoModes.begin(); i != end; ++i) {
1745 >                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1746 >                        default_id = i->resolution_id;
1747 >                        break;
1748 >                }
1749 >        }
1750 >        if (i == end) { // not found, use first available mode
1751 >                default_depth = VideoModes[0].depth;
1752 >                default_id = VideoModes[0].resolution_id;
1753 >        }
1754  
1755   #if DEBUG
1756          D(bug("Available video modes:\n"));
1757 <        vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
1603 <        while (i != end) {
1757 >        for (i = VideoModes.begin(); i != end; ++i) {
1758                  int bits = 1 << i->depth;
1759                  if (bits == 16)
1760                          bits = 15;
1761                  else if (bits == 32)
1762                          bits = 24;
1763                  D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1610                ++i;
1764          }
1765   #endif
1766  
1767 <        // Find requested default mode and open display
1768 <        if (VideoModes.size() == 1)
1769 <                return video_open(VideoModes[0]);
1770 <        else {
1771 <                // Find mode with specified dimensions
1772 <                std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1620 <                for (i = VideoModes.begin(); i != end; ++i) {
1621 <                        if (i->x == default_width && i->y == default_height && i->depth == default_depth)
1622 <                                return video_open(*i);
1623 <                }
1624 <                return video_open(VideoModes[0]);
1625 <        }
1767 >        // Create X11_monitor_desc for this (the only) display
1768 >        X11_monitor_desc *monitor = new X11_monitor_desc(VideoModes, default_depth, default_id);
1769 >        VideoMonitors.push_back(monitor);
1770 >
1771 >        // Open display
1772 >        return monitor->video_open();
1773   }
1774  
1775  
# Line 1631 | Line 1778 | bool VideoInit(bool classic)
1778   */
1779  
1780   // Close display
1781 < static void video_close(void)
1781 > void X11_monitor_desc::video_close(void)
1782   {
1783          D(bug("video_close()\n"));
1784  
1785          // Stop redraw thread
1786 < #ifdef HAVE_PTHREADS
1786 > #ifdef USE_PTHREADS_SERVICES
1787          if (redraw_thread_active) {
1788                  redraw_thread_cancel = true;
1789 < #ifdef HAVE_PTHREAD_CANCEL
1643 <                pthread_cancel(redraw_thread);
1644 < #endif
1789 >                redraw_thread_cancel_ack = false;
1790                  pthread_join(redraw_thread, NULL);
1791 +                while (!redraw_thread_cancel_ack) ;
1792          }
1793   #endif
1794          redraw_thread_active = false;
# Line 1676 | Line 1822 | static void video_close(void)
1822  
1823   void VideoExit(void)
1824   {
1825 <        // Close display
1826 <        video_close();
1825 >        // Close displays
1826 >        vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
1827 >        for (i = VideoMonitors.begin(); i != end; ++i)
1828 >                dynamic_cast<X11_monitor_desc *>(*i)->video_close();
1829  
1830   #ifdef ENABLE_XF86_VIDMODE
1831          // Free video mode list
# Line 1735 | Line 1883 | void VideoInterrupt(void)
1883   *  Set palette
1884   */
1885  
1886 < void video_set_palette(uint8 *pal, int num_in)
1886 > void X11_monitor_desc::set_palette(uint8 *pal, int num_in)
1887   {
1888 +        const video_mode &mode = get_current_mode();
1889 +
1890          LOCK_PALETTE;
1891  
1892          // Convert colors to XColor array
1893          int num_out = 256;
1894          bool stretch = false;
1895 <        if (IsDirectMode(VideoMonitor.mode)) {
1895 >        if (IsDirectMode(mode)) {
1896                  // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1897                  num_out = vis->map_entries;
1898                  stretch = true;
1899          }
1900 <        XColor *p = palette;
1900 >        XColor *p = x_palette;
1901          for (int i=0; i<num_out; i++) {
1902                  int c = (stretch ? (i * num_in) / num_out : i);
1903                  p->red = pal[c*3 + 0] * 0x0101;
# Line 1758 | Line 1908 | void video_set_palette(uint8 *pal, int n
1908  
1909   #ifdef ENABLE_VOSF
1910          // Recalculate pixel color expansion map
1911 <        if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) {
1911 >        if (!IsDirectMode(mode) && xdepth > 8) {
1912                  for (int i=0; i<256; i++) {
1913                          int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1914 <                        ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
1914 >                        ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2], true);
1915                  }
1916  
1917                  // We have to redraw everything because the interpretation of pixel values changed
1918                  LOCK_VOSF;
1919                  PFLAG_SET_ALL;
1920                  UNLOCK_VOSF;
1921 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1921 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1922          }
1923   #endif
1924  
1925          // Tell redraw thread to change palette
1926 <        palette_changed = true;
1926 >        x_palette_changed = true;
1927  
1928          UNLOCK_PALETTE;
1929   }
# Line 1783 | Line 1933 | void video_set_palette(uint8 *pal, int n
1933   *  Switch video mode
1934   */
1935  
1936 < void video_switch_to_mode(const video_mode &mode)
1936 > void X11_monitor_desc::switch_to_current_mode(void)
1937   {
1938          // Close and reopen display
1939          video_close();
1940 <        video_open(mode);
1940 >        video_open();
1941  
1942          if (drv == NULL) {
1943                  ErrorAlert(STR_OPEN_WINDOW_ERR);
# Line 1966 | Line 2116 | static int event2keycode(XKeyEvent &ev,
2116  
2117   static void handle_events(void)
2118   {
2119 <        while (XPending(x_display)) {
2119 >        for (;;) {
2120                  XEvent event;
2121 <                XNextEvent(x_display, &event);
2121 >                XDisplayLock();
2122 >
2123 >                if (!XCheckMaskEvent(x_display, eventmask, &event)) {
2124 >                        // Handle clipboard events
2125 >                        if (XCheckTypedEvent(x_display, SelectionRequest, &event))
2126 >                                ClipboardSelectionRequest(&event.xselectionrequest);
2127 >                        else if (XCheckTypedEvent(x_display, SelectionClear, &event))
2128 >                                ClipboardSelectionClear(&event.xselectionclear);
2129  
2130 +                        // Window "close" widget clicked
2131 +                        else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
2132 +                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
2133 +                                        ADBKeyDown(0x7f);       // Power key
2134 +                                        ADBKeyUp(0x7f);
2135 +                                }
2136 +                        }
2137 +                        XDisplayUnlock();
2138 +                        break;
2139 +                }
2140 +                
2141                  switch (event.type) {
2142  
2143                          // Mouse button
# Line 2057 | Line 2225 | static void handle_events(void)
2225                          // Hidden parts exposed, force complete refresh of window
2226                          case Expose:
2227                                  if (display_type == DISPLAY_WINDOW) {
2228 +                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
2229   #ifdef ENABLE_VOSF
2230                                          if (use_vosf) {                 // VOSF refresh
2231                                                  LOCK_VOSF;
2232                                                  PFLAG_SET_ALL;
2233                                                  UNLOCK_VOSF;
2234 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2234 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2235                                          }
2236                                          else
2237   #endif
# Line 2073 | Line 2242 | static void handle_events(void)
2242                                                          updt_box[x1][y1] = true;
2243                                                  nr_boxes = 16 * 16;
2244                                          } else                                  // Static refresh
2245 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2077 <                                }
2078 <                                break;
2079 <
2080 <                        // Window "close" widget clicked
2081 <                        case ClientMessage:
2082 <                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
2083 <                                        ADBKeyDown(0x7f);       // Power key
2084 <                                        ADBKeyUp(0x7f);
2245 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2246                                  }
2247                                  break;
2248                  }
2249 +
2250 +                XDisplayUnlock();
2251          }
2252   }
2253  
# Line 2099 | Line 2262 | static void update_display_dynamic(int t
2262          int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
2263          int xil = 0;
2264          int rxm = 0, rxmo = 0;
2265 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2266 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2267 <        int rx = VideoMonitor.mode.bytes_per_row / 16;
2268 <        int ry = VideoMonitor.mode.y / 16;
2265 >        const video_mode &mode = drv->monitor.get_current_mode();
2266 >        int bytes_per_row = mode.bytes_per_row;
2267 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2268 >        int rx = mode.bytes_per_row / 16;
2269 >        int ry = mode.y / 16;
2270          int max_box;
2271  
2272          y2s = sm_uptd[ticker % 8];
# Line 2128 | Line 2292 | static void update_display_dynamic(int t
2292                  }
2293          }
2294  
2295 +        XDisplayLock();
2296          if ((nr_boxes <= max_box) && (nr_boxes)) {
2297                  for (y1=0; y1<16; y1++) {
2298                          for (x1=0; x1<16; x1++) {
# Line 2156 | Line 2321 | static void update_display_dynamic(int t
2321                                          i = (yi * bytes_per_row) + xi;
2322                                          for (y2=0; y2 < yil; y2++, i += bytes_per_row)
2323                                                  memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
2324 <                                        if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2324 >                                        if (mode.depth == VDEPTH_1BIT) {
2325                                                  if (drv->have_shm)
2326                                                          XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
2327                                                  else
# Line 2181 | Line 2346 | static void update_display_dynamic(int t
2346                  }
2347                  nr_boxes = 0;
2348          }
2349 +        XDisplayUnlock();
2350   }
2351  
2352   // Static display update (fixed frame rate, but incremental)
# Line 2188 | Line 2354 | static void update_display_static(driver
2354   {
2355          // Incremental update code
2356          unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
2357 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2358 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2357 >        const video_mode &mode = drv->monitor.get_current_mode();
2358 >        int bytes_per_row = mode.bytes_per_row;
2359 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2360          uint8 *p, *p2;
2361  
2362          // Check for first line from top and first line from bottom that have changed
2363          y1 = 0;
2364 <        for (j=0; j<VideoMonitor.mode.y; j++) {
2364 >        for (j=0; j<mode.y; j++) {
2365                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2366                          y1 = j;
2367                          break;
2368                  }
2369          }
2370          y2 = y1 - 1;
2371 <        for (j=VideoMonitor.mode.y-1; j>=y1; j--) {
2371 >        for (j=mode.y-1; j>=y1; j--) {
2372                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2373                          y2 = j;
2374                          break;
# Line 2211 | Line 2378 | static void update_display_static(driver
2378  
2379          // Check for first column from left and first column from right that have changed
2380          if (high) {
2381 <                if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2382 <                        x1 = VideoMonitor.mode.x - 1;
2381 >                if (mode.depth == VDEPTH_1BIT) {
2382 >                        x1 = mode.x - 1;
2383                          for (j=y1; j<=y2; j++) {
2384                                  p = &the_buffer[j * bytes_per_row];
2385                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2230 | Line 2397 | static void update_display_static(driver
2397                                  p2 = &the_buffer_copy[j * bytes_per_row];
2398                                  p += bytes_per_row;
2399                                  p2 += bytes_per_row;
2400 <                                for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) {
2400 >                                for (i=(mode.x>>3); i>(x2>>3); i--) {
2401                                          p--; p2--;
2402                                          if (*p != *p2) {
2403                                                  x2 = (i << 3) + 7;
# Line 2249 | Line 2416 | static void update_display_static(driver
2416                          }
2417  
2418                  } else {
2419 <                        x1 = VideoMonitor.mode.x;
2419 >                        x1 = mode.x;
2420                          for (j=y1; j<=y2; j++) {
2421                                  p = &the_buffer[j * bytes_per_row];
2422                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2267 | Line 2434 | static void update_display_static(driver
2434                                  p2 = &the_buffer_copy[j * bytes_per_row];
2435                                  p += bytes_per_row;
2436                                  p2 += bytes_per_row;
2437 <                                for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2437 >                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2438                                          p--;
2439                                          p2--;
2440                                          if (*p != *p2) {
# Line 2289 | Line 2456 | static void update_display_static(driver
2456          }
2457  
2458          // Refresh display
2459 +        XDisplayLock();
2460          if (high && wide) {
2461                  if (drv->have_shm)
2462                          XShmPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high, 0);
2463                  else
2464                          XPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high);
2465          }
2466 +        XDisplayUnlock();
2467   }
2468  
2469  
# Line 2334 | Line 2503 | static inline void handle_palette_change
2503   {
2504          LOCK_PALETTE;
2505  
2506 <        if (palette_changed) {
2507 <                palette_changed = false;
2506 >        if (x_palette_changed) {
2507 >                x_palette_changed = false;
2508 >                XDisplayLock();
2509                  drv->update_palette();
2510 +                XDisplayUnlock();
2511          }
2512  
2513          UNLOCK_PALETTE;
# Line 2361 | Line 2532 | static void video_refresh_dga_vosf(void)
2532                  tick_counter = 0;
2533                  if (mainBuffer.dirty) {
2534                          LOCK_VOSF;
2535 <                        update_display_dga_vosf();
2535 >                        update_display_dga_vosf(static_cast<driver_dga *>(drv));
2536                          UNLOCK_VOSF;
2537                  }
2538          }
# Line 2378 | Line 2549 | static void video_refresh_window_vosf(vo
2549          if (++tick_counter >= frame_skip) {
2550                  tick_counter = 0;
2551                  if (mainBuffer.dirty) {
2552 +                        XDisplayLock();
2553                          LOCK_VOSF;
2554                          update_display_window_vosf(static_cast<driver_window *>(drv));
2555                          UNLOCK_VOSF;
2556                          XSync(x_display, false); // Let the server catch up
2557 +                        XDisplayUnlock();
2558                  }
2559          }
2560   }
# Line 2461 | Line 2634 | void VideoRefresh(void)
2634   const int VIDEO_REFRESH_HZ = 60;
2635   const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2636  
2637 < #ifdef HAVE_PTHREADS
2637 > #ifdef USE_PTHREADS_SERVICES
2638   static void *redraw_func(void *arg)
2639   {
2640          int fd = ConnectionNumber(x_display);
# Line 2502 | Line 2675 | static void *redraw_func(void *arg)
2675          }
2676  
2677          uint64 end = GetTicks_usec();
2678 <        D(bug("%Ld refreshes in %Ld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2678 >        D(bug("%lld refreshes in %lld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2679 >
2680 >        redraw_thread_cancel_ack = true;
2681          return NULL;
2682   }
2683   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines