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.59 by cebix, 2001-07-11T17:04:41Z vs.
Revision 1.74 by gbeauche, 2004-11-15T23:40:23Z

# 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 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 pthread_t redraw_thread;                                         // Redraw thread
103   #endif
# Line 122 | 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
129   static Window rootwin;                                                          // Root window and our window
130   static int num_depths = 0;                                                      // Number of available X depths
# Line 131 | Line 134 | static unsigned long black_pixel, white_
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;
# Line 139 | Line 143 | static int rshift, rloss, gshift, gloss,
143  
144   static Colormap cmap[2] = {0, 0};                                       // Colormaps for indexed modes (DGA needs two of them)
145  
146 < static XColor palette[256];                                                     // Color palette to be used as CLUT and gamma table
147 < static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
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 153 | 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 184 | Line 188 | static void (*video_refresh)(void);
188  
189   // Prototypes
190   static void *redraw_func(void *arg);
187 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 + // From clip_unix.cpp
201 + extern void ClipboardSelectionClear(XSelectionClearEvent *);
202 + extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
203 +
204 +
205 + /*
206 + *  monitor_desc subclass for X11 display
207 + */
208 +
209 + class X11_monitor_desc : public monitor_desc {
210 + public:
211 +        X11_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
212 +        ~X11_monitor_desc() {}
213 +
214 +        virtual void switch_to_current_mode(void);
215 +        virtual void set_palette(uint8 *pal, int num);
216 +
217 +        bool video_open(void);
218 +        void video_close(void);
219 + };
220 +
221  
222   /*
223   *  Utility functions
224   */
225  
226 + // Map video_mode depth ID to numerical depth value
227 + static inline int depth_of_video_mode(video_mode const & mode)
228 + {
229 +        int depth = -1;
230 +        switch (mode.depth) {
231 +        case VDEPTH_1BIT:
232 +                depth = 1;
233 +                break;
234 +        case VDEPTH_2BIT:
235 +                depth = 2;
236 +                break;
237 +        case VDEPTH_4BIT:
238 +                depth = 4;
239 +                break;
240 +        case VDEPTH_8BIT:
241 +                depth = 8;
242 +                break;
243 +        case VDEPTH_16BIT:
244 +                depth = 16;
245 +                break;
246 +        case VDEPTH_32BIT:
247 +                depth = 32;
248 +                break;
249 +        default:
250 +                abort();
251 +        }
252 +        return depth;
253 + }
254 +
255   // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
256   static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
257   {
# Line 210 | Line 264 | static bool find_visual_for_depth(video_
264   {
265          D(bug("have_visual_for_depth(%d)\n", 1 << depth));
266  
267 +        // 1-bit works always and uses default visual
268 +        if (depth == VDEPTH_1BIT) {
269 +                vis = DefaultVisual(x_display, screen);
270 +                visualInfo.visualid = XVisualIDFromVisual(vis);
271 +                int num = 0;
272 +                XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
273 +                visualInfo = vi[0];
274 +                XFree(vi);
275 +                xdepth = visualInfo.depth;
276 +                color_class = visualInfo.c_class;
277 +                D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
278 +                return true;
279 +        }
280 +
281          // Calculate minimum and maximum supported X depth
282          int min_depth = 1, max_depth = 32;
283          switch (depth) {
216                case VDEPTH_1BIT:       // 1-bit works always and uses default visual
217                        min_depth = max_depth = DefaultDepth(x_display, screen);
218                        break;
284   #ifdef ENABLE_VOSF
285                  case VDEPTH_2BIT:
286                  case VDEPTH_4BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
# Line 321 | Line 386 | static void add_window_modes(video_depth
386   }
387  
388   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
389 < static void set_mac_frame_buffer(video_depth depth, bool native_byte_order)
389 > static void set_mac_frame_buffer(X11_monitor_desc &monitor, video_depth depth, bool native_byte_order)
390   {
391   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
392          int layout = FLAYOUT_DIRECT;
# Line 333 | Line 398 | static void set_mac_frame_buffer(video_d
398                  MacFrameLayout = layout;
399          else
400                  MacFrameLayout = FLAYOUT_DIRECT;
401 <        VideoMonitor.mac_frame_base = MacFrameBaseMac;
401 >        monitor.set_mac_frame_base(MacFrameBaseMac);
402  
403          // Set variables used by UAE memory banking
404 +        const video_mode &mode = monitor.get_current_mode();
405          MacFrameBaseHost = the_buffer;
406 <        MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
406 >        MacFrameSize = mode.bytes_per_row * mode.y;
407          InitFrameBufferMapping();
408   #else
409 <        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
409 >        monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
410   #endif
411 <        D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base));
411 >        D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
412   }
413  
414   // Set window name and class
# Line 420 | Line 486 | static int error_handler(Display *d, XEr
486  
487   class driver_base {
488   public:
489 <        driver_base();
489 >        driver_base(X11_monitor_desc &m);
490          virtual ~driver_base();
491  
492          virtual void update_palette(void);
# Line 436 | Line 502 | public:
502          virtual void ungrab_mouse(void) {}
503  
504   public:
505 +        X11_monitor_desc &monitor; // Associated video monitor
506 +        const video_mode &mode;    // Video mode handled by the driver
507 +
508          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
509          Window w;               // The window we draw into
510  
# Line 453 | Line 522 | class driver_window : public driver_base
522          friend void update_display_static(driver_window *drv);
523  
524   public:
525 <        driver_window(const video_mode &mode);
525 >        driver_window(X11_monitor_desc &monitor);
526          ~driver_window();
527  
528          void toggle_mouse_grab(void);
# Line 478 | Line 547 | static driver_base *drv = NULL;        // Point
547   # include "video_vosf.h"
548   #endif
549  
550 < driver_base::driver_base()
551 < : init_ok(false), w(0)
550 > driver_base::driver_base(X11_monitor_desc &m)
551 > : monitor(m), mode(m.get_current_mode()), init_ok(false), w(0)
552   {
553          the_buffer = NULL;
554          the_buffer_copy = NULL;
# Line 513 | Line 582 | driver_base::~driver_base()
582          }
583   #ifdef ENABLE_VOSF
584          else {
585 +                // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
586 +                if (the_buffer != VM_MAP_FAILED) {
587 +                        D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
588 +                        vm_release(the_buffer, the_buffer_size);
589 +                        the_buffer = NULL;
590 +                }
591                  if (the_host_buffer) {
592 +                        D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
593                          free(the_host_buffer);
594                          the_host_buffer = NULL;
595                  }
520                if (the_buffer) {
521                        free(the_buffer);
522                        the_buffer = NULL;
523                }
596                  if (the_buffer_copy) {
597 +                        D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
598                          free(the_buffer_copy);
599                          the_buffer_copy = NULL;
600                  }
# Line 534 | Line 607 | void driver_base::update_palette(void)
607   {
608          if (color_class == PseudoColor || color_class == DirectColor) {
609                  int num = vis->map_entries;
610 <                if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor)
610 >                if (!IsDirectMode(monitor.get_current_mode()) && color_class == DirectColor)
611                          return; // Indexed mode on true color screen, don't set CLUT
612 <                XStoreColors(x_display, cmap[0], palette, num);
613 <                XStoreColors(x_display, cmap[1], palette, num);
612 >                XStoreColors(x_display, cmap[0], x_palette, num);
613 >                XStoreColors(x_display, cmap[1], x_palette, num);
614          }
615          XSync(x_display, false);
616   }
# Line 560 | Line 633 | void driver_base::restore_mouse_accel(vo
633   */
634  
635   // Open display
636 < driver_window::driver_window(const video_mode &mode)
637 < : gc(0), img(NULL), have_shm(false), mouse_grabbed(false), mac_cursor(0)
636 > driver_window::driver_window(X11_monitor_desc &m)
637 > : driver_base(m), gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
638   {
639          int width = mode.x, height = mode.y;
640          int aligned_width = (width + 15) & ~15;
# Line 570 | Line 643 | driver_window::driver_window(const video
643          // Set absolute mouse mode
644          ADBSetRelMouseMode(mouse_grabbed);
645  
646 <        // Create window (setting backround_pixel, border_pixel and colormap is
646 >        // Create window (setting background_pixel, border_pixel and colormap is
647          // mandatory when using a non-default visual; in 1-bit mode we use the
648          // default visual, so we can also use the default colormap)
649          XSetWindowAttributes wattr;
# Line 580 | Line 653 | driver_window::driver_window(const video
653          wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
654          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
655                  InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &wattr);
656 +        D(bug(" window created\n"));
657  
658          // Set window name/class
659          set_window_name(w, STR_WINDOW_TITLE);
# Line 603 | Line 677 | driver_window::driver_window(const video
677                          XFree(hints);
678                  }
679          }
680 +        D(bug(" window attributes set\n"));
681          
682          // Show window
683          XMapWindow(x_display, w);
684          wait_mapped(w);
685 +        D(bug(" window mapped\n"));
686  
687          // 1-bit mode is big-endian; if the X server is little-endian, we can't
688          // use SHM because that doesn't allow changing the image byte order
# Line 617 | Line 693 | driver_window::driver_window(const video
693  
694                  // Create SHM image ("height + 2" for safety)
695                  img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
696 +                D(bug(" shm image created\n"));
697                  shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
698                  the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
699                  shminfo.shmaddr = img->data = (char *)the_buffer_copy;
# Line 637 | Line 714 | driver_window::driver_window(const video
714                          have_shm = true;
715                          shmctl(shminfo.shmid, IPC_RMID, 0);
716                  }
717 +                D(bug(" shm image attached\n"));
718          }
719          
720          // Create normal X image if SHM doesn't work ("height + 2" for safety)
# Line 644 | Line 722 | driver_window::driver_window(const video
722                  int bytes_per_row = (mode.depth == VDEPTH_1BIT ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth)));
723                  the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
724                  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);
725 +                D(bug(" X image created\n"));
726          }
727  
728          if (need_msb_image) {
# Line 656 | Line 735 | driver_window::driver_window(const video
735          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
736          the_host_buffer = the_buffer_copy;
737          the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
738 <        the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
739 <        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
738 >        the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size);
739 >        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
740          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
741   #else
742          // Allocate memory for frame buffer
# Line 684 | Line 763 | driver_window::driver_window(const video
763          native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
764   #endif
765   #ifdef ENABLE_VOSF
766 <        Screen_blitter_init(&visualInfo, native_byte_order, mode.depth);
766 >        Screen_blitter_init(visualFormat, native_byte_order, depth_of_video_mode(mode));
767   #endif
768  
769 <        // Set VideoMonitor
770 <        VideoMonitor.mode = mode;
692 <        set_mac_frame_buffer(mode.depth, native_byte_order);
769 >        // Set frame buffer base
770 >        set_mac_frame_buffer(monitor, mode.depth, native_byte_order);
771  
772          // Everything went well
773          init_ok = true;
# Line 706 | Line 784 | driver_window::~driver_window()
784                  the_buffer_copy = NULL; // don't free() in driver_base dtor
785   #endif
786          }
787 < #ifdef ENABLE_VOSF
788 <        if (use_vosf) {
789 <                // don't free() memory mapped buffers in driver_base dtor
712 <                if (the_buffer != VM_MAP_FAILED) {
713 <                        vm_release(the_buffer, the_buffer_size);
714 <                        the_buffer = NULL;
715 <                }
716 <                if (the_buffer_copy != VM_MAP_FAILED) {
717 <                        vm_release(the_buffer_copy, the_buffer_size);
718 <                        the_buffer_copy = NULL;
719 <                }
720 <        }
721 < #endif
722 <        if (img)
787 >        if (img) {
788 >                if (!have_shm)
789 >                        img->data = NULL;
790                  XDestroyImage(img);
791 +        }
792          if (have_shm) {
793                  shmdt(shminfo.shmaddr);
794                  shmctl(shminfo.shmid, IPC_RMID, 0);
# Line 779 | Line 847 | void driver_window::mouse_moved(int x, i
847          // Warped mouse motion (this code is taken from SDL)
848  
849          // Post first mouse event
850 <        int width = VideoMonitor.mode.x, height = VideoMonitor.mode.y;
850 >        int width = monitor.get_current_mode().x, height = monitor.get_current_mode().y;
851          int delta_x = x - mouse_last_x, delta_y = y - mouse_last_y;
852          mouse_last_x = x; mouse_last_y = y;
853          ADBMouseMoved(delta_x, delta_y);
# Line 816 | Line 884 | void driver_window::mouse_moved(int x, i
884  
885   class driver_dga : public driver_base {
886   public:
887 <        driver_dga();
887 >        driver_dga(X11_monitor_desc &monitor);
888          ~driver_dga();
889  
890          void suspend(void);
# Line 827 | Line 895 | private:
895          void *fb_save;                  // Saved frame buffer for suspend/resume
896   };
897  
898 < driver_dga::driver_dga()
899 < : suspend_win(0), fb_save(NULL)
898 > driver_dga::driver_dga(X11_monitor_desc &m)
899 > : driver_base(m), suspend_win(0), fb_save(NULL)
900   {
901   }
902  
# Line 849 | Line 917 | void driver_dga::suspend(void)
917          LOCK_FRAME_BUFFER;
918  
919          // Save frame buffer
920 <        fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
920 >        fb_save = malloc(mode.y * mode.bytes_per_row);
921          if (fb_save)
922 <                memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
922 >                memcpy(fb_save, the_buffer, mode.y * mode.bytes_per_row);
923  
924          // Close full screen display
925   #ifdef ENABLE_XF86_DGA
# Line 904 | Line 972 | void driver_dga::resume(void)
972                  LOCK_VOSF;
973                  PFLAG_SET_ALL;
974                  UNLOCK_VOSF;
975 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
975 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
976          }
977   #endif
978          
# Line 914 | Line 982 | void driver_dga::resume(void)
982                  // Don't copy fb_save to the temporary frame buffer in VOSF mode
983                  if (!use_vosf)
984   #endif
985 <                memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
985 >                memcpy(the_buffer, fb_save, mode.y * mode.bytes_per_row);
986                  free(fb_save);
987                  fb_save = NULL;
988          }
# Line 936 | Line 1004 | const char FBDEVICE_FILE_NAME[] = "/dev/
1004  
1005   class driver_fbdev : public driver_dga {
1006   public:
1007 <        driver_fbdev(const video_mode &mode);
1007 >        driver_fbdev(X11_monitor_desc &monitor);
1008          ~driver_fbdev();
1009   };
1010  
1011   // Open display
1012 < driver_fbdev::driver_fbdev(const video_mode &mode)
1012 > driver_fbdev::driver_fbdev(X11_monitor_desc &m) : driver_dga(m)
1013   {
1014          int width = mode.x, height = mode.y;
1015  
# Line 989 | Line 1057 | driver_fbdev::driver_fbdev(const video_m
1057                  if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
1058                          continue;
1059                  
1060 <                if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
1060 >                if ((sscanf(line, "%19s %d %x", fb_name, &fb_depth, &fb_offset) == 3)
1061                   && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) {
1062                          device_found = true;
1063                          break;
# Line 1056 | Line 1124 | driver_fbdev::driver_fbdev(const video_m
1124   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1125          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1126          // i.e. the framebuffer update function is not Blit_Copy_Raw
1127 <        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1127 >        use_vosf = Screen_blitter_init(visualFormat, true, mode.depth);
1128          
1129          if (use_vosf) {
1130            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1131            the_host_buffer = the_buffer;
1132            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1133 <          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
1134 <          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1133 >          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1134 >          the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size);
1135          }
1136   #else
1137          use_vosf = false;
1138   #endif
1139   #endif
1140          
1141 <        // Set VideoMonitor
1142 <        VideoModes[0].bytes_per_row = bytes_per_row;
1143 <        VideoModes[0].depth = DepthModeForPixelDepth(fb_depth);
1144 <        VideoMonitor.mode = mode;
1077 <        set_mac_frame_buffer(mode.depth, true);
1141 >        // Set frame buffer base
1142 >        const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1143 >        const_cast<video_mode *>(&mode)->depth = DepthModeForPixelDepth(fb_depth);
1144 >        set_mac_frame_buffer(monitor, mode.depth, true);
1145  
1146          // Everything went well
1147          init_ok = true;
# Line 1097 | Line 1164 | driver_fbdev::~driver_fbdev()
1164                          munmap(the_host_buffer, the_buffer_size);
1165                          the_host_buffer = NULL;
1166                  }
1100                if (the_buffer_copy != VM_MAP_FAILED) {
1101                        vm_release(the_buffer_copy, the_buffer_size);
1102                        the_buffer_copy = NULL;
1103                }
1104                if (the_buffer != VM_MAP_FAILED) {
1105                        vm_release(the_buffer, the_buffer_size);
1106                        the_buffer = NULL;
1107                }
1167          }
1168   #endif
1169   }
# Line 1118 | Line 1177 | driver_fbdev::~driver_fbdev()
1177  
1178   class driver_xf86dga : public driver_dga {
1179   public:
1180 <        driver_xf86dga(const video_mode &mode);
1180 >        driver_xf86dga(X11_monitor_desc &monitor);
1181          ~driver_xf86dga();
1182  
1183          void update_palette(void);
# Line 1129 | Line 1188 | private:
1188   };
1189  
1190   // Open display
1191 < driver_xf86dga::driver_xf86dga(const video_mode &mode)
1192 < : current_dga_cmap(0)
1191 > driver_xf86dga::driver_xf86dga(X11_monitor_desc &m)
1192 > : driver_dga(m), current_dga_cmap(0)
1193   {
1194          int width = mode.x, height = mode.y;
1195  
# Line 1157 | Line 1216 | driver_xf86dga::driver_xf86dga(const vid
1216          XSetWindowAttributes wattr;
1217          wattr.event_mask = eventmask = dga_eventmask;
1218          wattr.override_redirect = True;
1219 +        wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
1220  
1221          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
1222 <                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
1222 >                InputOutput, vis, CWEventMask | CWOverrideRedirect |
1223 >                (color_class == DirectColor ? CWColormap : 0), &wattr);
1224  
1225          // Set window name/class
1226          set_window_name(w, STR_WINDOW_TITLE);
# Line 1193 | Line 1254 | driver_xf86dga::driver_xf86dga(const vid
1254  
1255          // Init blitting routines
1256          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
1257 < #if VIDEO_VOSF
1257 > #if ENABLE_VOSF
1258 >        bool native_byte_order;
1259 > #ifdef WORDS_BIGENDIAN
1260 >        native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
1261 > #else
1262 >        native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
1263 > #endif
1264   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1265          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1266          // i.e. the framebuffer update function is not Blit_Copy_Raw
1267 <        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1267 >        use_vosf = Screen_blitter_init(visualFormat, native_byte_order, depth_of_video_mode(mode));
1268          
1269          if (use_vosf) {
1270            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1271            the_host_buffer = the_buffer;
1272            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1273 <          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
1274 <          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1273 >          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1274 >          the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size);
1275          }
1276   #else
1277          use_vosf = false;
1278   #endif
1279   #endif
1280          
1281 <        // Set VideoMonitor
1281 >        // Set frame buffer base
1282          const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1283 <        VideoMonitor.mode = mode;
1217 <        set_mac_frame_buffer(mode.depth, true);
1283 >        set_mac_frame_buffer(monitor, mode.depth, true);
1284  
1285          // Everything went well
1286          init_ok = true;
# Line 1232 | Line 1298 | driver_xf86dga::~driver_xf86dga()
1298          else {
1299                  // don't free() the screen buffer in driver_base dtor
1300                  the_host_buffer = NULL;
1235                
1236                if (the_buffer_copy != VM_MAP_FAILED) {
1237                        vm_release(the_buffer_copy, the_buffer_size);
1238                        the_buffer_copy = NULL;
1239                }
1240                if (the_buffer != VM_MAP_FAILED) {
1241                        vm_release(the_buffer, the_buffer_size);
1242                        the_buffer = NULL;
1243                }
1301          }
1302   #endif
1303   #ifdef ENABLE_XF86_VIDMODE
# Line 1254 | Line 1311 | void driver_xf86dga::update_palette(void
1311   {
1312          driver_dga::update_palette();
1313          current_dga_cmap ^= 1;
1314 <        if (!IsDirectMode(VideoMonitor.mode) && cmap[current_dga_cmap])
1314 >        if (!IsDirectMode(monitor.get_current_mode()) && cmap[current_dga_cmap])
1315                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1316   }
1317  
# Line 1262 | Line 1319 | void driver_xf86dga::update_palette(void
1319   void driver_xf86dga::resume(void)
1320   {
1321          driver_dga::resume();
1322 <        if (!IsDirectMode(VideoMonitor.mode))
1322 >        if (!IsDirectMode(monitor.get_current_mode()))
1323                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1324   }
1325   #endif
# Line 1337 | Line 1394 | static void keycode_init(void)
1394          }
1395   }
1396  
1397 < // Open display for specified mode
1398 < static bool video_open(const video_mode &mode)
1397 > // Open display for current mode
1398 > bool X11_monitor_desc::video_open(void)
1399   {
1400 +        D(bug("video_open()\n"));
1401 +        const video_mode &mode = get_current_mode();
1402 +
1403          // Find best available X visual
1404          if (!find_visual_for_depth(mode.depth)) {
1405                  ErrorAlert(STR_NO_XVISUAL_ERR);
1406                  return false;
1407          }
1408  
1409 +        // Build up visualFormat structure
1410 +        visualFormat.depth = visualInfo.depth;
1411 +        visualFormat.Rmask = visualInfo.red_mask;
1412 +        visualFormat.Gmask = visualInfo.green_mask;
1413 +        visualFormat.Bmask = visualInfo.blue_mask;
1414 +
1415          // Create color maps
1416          if (color_class == PseudoColor || color_class == DirectColor) {
1417                  cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
# Line 1379 | Line 1445 | static bool video_open(const video_mode
1445                  int num = vis->map_entries;
1446                  for (int i=0; i<num; i++) {
1447                          int c = (i * 256) / num;
1448 <                        palette[i].pixel = map_rgb(c, c, c);
1449 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1448 >                        x_palette[i].pixel = map_rgb(c, c, c);
1449 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1450                  }
1451          } else if (color_class == PseudoColor) {
1452                  for (int i=0; i<256; i++) {
1453 <                        palette[i].pixel = i;
1454 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1453 >                        x_palette[i].pixel = i;
1454 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1455                  }
1456          }
1457  
# Line 1393 | Line 1459 | static bool video_open(const video_mode
1459          int num = (color_class == DirectColor ? vis->map_entries : 256);
1460          for (int i=0; i<num; i++) {
1461                  int c = (i * 256) / num;
1462 <                palette[i].red = c * 0x0101;
1463 <                palette[i].green = c * 0x0101;
1464 <                palette[i].blue = c * 0x0101;
1462 >                x_palette[i].red = c * 0x0101;
1463 >                x_palette[i].green = c * 0x0101;
1464 >                x_palette[i].blue = c * 0x0101;
1465          }
1466          if (color_class == PseudoColor || color_class == DirectColor) {
1467 <                XStoreColors(x_display, cmap[0], palette, num);
1468 <                XStoreColors(x_display, cmap[1], palette, num);
1467 >                XStoreColors(x_display, cmap[0], x_palette, num);
1468 >                XStoreColors(x_display, cmap[1], x_palette, num);
1469          }
1470  
1471   #ifdef ENABLE_VOSF
# Line 1412 | Line 1478 | static bool video_open(const video_mode
1478          // Create display driver object of requested type
1479          switch (display_type) {
1480                  case DISPLAY_WINDOW:
1481 <                        drv = new driver_window(mode);
1481 >                        drv = new driver_window(*this);
1482                          break;
1483   #ifdef ENABLE_FBDEV_DGA
1484                  case DISPLAY_DGA:
1485 <                        drv = new driver_fbdev(mode);
1485 >                        drv = new driver_fbdev(*this);
1486                          break;
1487   #endif
1488   #ifdef ENABLE_XF86_DGA
1489                  case DISPLAY_DGA:
1490 <                        drv = new driver_xf86dga(mode);
1490 >                        drv = new driver_xf86dga(*this);
1491                          break;
1492   #endif
1493          }
# Line 1436 | Line 1502 | static bool video_open(const video_mode
1502   #ifdef ENABLE_VOSF
1503          if (use_vosf) {
1504                  // Initialize the VOSF system
1505 <                if (!video_vosf_init()) {
1505 >                if (!video_vosf_init(*this)) {
1506                          ErrorAlert(STR_VOSF_INIT_ERR);
1507                  return false;
1508                  }
# Line 1453 | Line 1519 | static bool video_open(const video_mode
1519          // Start redraw/input thread
1520   #ifdef HAVE_PTHREADS
1521          redraw_thread_cancel = false;
1522 <        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1522 >        Set_pthread_attr(&redraw_thread_attr, 0);
1523 >        redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1524          if (!redraw_thread_active) {
1525                  printf("FATAL: cannot create redraw thread\n");
1526                  return false;
# Line 1497 | Line 1564 | bool VideoInit(bool classic)
1564                  ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1565                  return false;
1566          }
1567 <        sort(avail_depths, avail_depths + num_depths);
1567 >        std::sort(avail_depths, avail_depths + num_depths);
1568          
1569   #ifdef ENABLE_FBDEV_DGA
1570          // Frame buffer name
# Line 1600 | Line 1667 | bool VideoInit(bool classic)
1667                  ErrorAlert(STR_NO_XVISUAL_ERR);
1668                  return false;
1669          }
1670 <        video_init_depth_list();
1670 >
1671 >        // Find requested default mode with specified dimensions
1672 >        uint32 default_id;
1673 >        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1674 >        for (i = VideoModes.begin(); i != end; ++i) {
1675 >                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1676 >                        default_id = i->resolution_id;
1677 >                        break;
1678 >                }
1679 >        }
1680 >        if (i == end) { // not found, use first available mode
1681 >                default_depth = VideoModes[0].depth;
1682 >                default_id = VideoModes[0].resolution_id;
1683 >        }
1684  
1685   #if DEBUG
1686          D(bug("Available video modes:\n"));
1687 <        vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
1608 <        while (i != end) {
1687 >        for (i = VideoModes.begin(); i != end; ++i) {
1688                  int bits = 1 << i->depth;
1689                  if (bits == 16)
1690                          bits = 15;
1691                  else if (bits == 32)
1692                          bits = 24;
1693                  D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1615                ++i;
1694          }
1695   #endif
1696  
1697 <        // Find requested default mode and open display
1698 <        if (VideoModes.size() == 1)
1699 <                return video_open(VideoModes[0]);
1700 <        else {
1701 <                // Find mode with specified dimensions
1702 <                std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1625 <                for (i = VideoModes.begin(); i != end; ++i) {
1626 <                        if (i->x == default_width && i->y == default_height && i->depth == default_depth)
1627 <                                return video_open(*i);
1628 <                }
1629 <                return video_open(VideoModes[0]);
1630 <        }
1697 >        // Create X11_monitor_desc for this (the only) display
1698 >        X11_monitor_desc *monitor = new X11_monitor_desc(VideoModes, default_depth, default_id);
1699 >        VideoMonitors.push_back(monitor);
1700 >
1701 >        // Open display
1702 >        return monitor->video_open();
1703   }
1704  
1705  
# Line 1636 | Line 1708 | bool VideoInit(bool classic)
1708   */
1709  
1710   // Close display
1711 < static void video_close(void)
1711 > void X11_monitor_desc::video_close(void)
1712   {
1713 +        D(bug("video_close()\n"));
1714 +
1715          // Stop redraw thread
1716   #ifdef HAVE_PTHREADS
1717          if (redraw_thread_active) {
# Line 1653 | Line 1727 | static void video_close(void)
1727          // Unlock frame buffer
1728          UNLOCK_FRAME_BUFFER;
1729          XSync(x_display, false);
1730 +        D(bug(" frame buffer unlocked\n"));
1731  
1732   #ifdef ENABLE_VOSF
1733          if (use_vosf) {
# Line 1678 | Line 1753 | static void video_close(void)
1753  
1754   void VideoExit(void)
1755   {
1756 <        // Close display
1757 <        video_close();
1756 >        // Close displays
1757 >        vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
1758 >        for (i = VideoMonitors.begin(); i != end; ++i)
1759 >                dynamic_cast<X11_monitor_desc *>(*i)->video_close();
1760  
1761   #ifdef ENABLE_XF86_VIDMODE
1762          // Free video mode list
# Line 1737 | Line 1814 | void VideoInterrupt(void)
1814   *  Set palette
1815   */
1816  
1817 < void video_set_palette(uint8 *pal, int num_in)
1817 > void X11_monitor_desc::set_palette(uint8 *pal, int num_in)
1818   {
1819 +        const video_mode &mode = get_current_mode();
1820 +
1821          LOCK_PALETTE;
1822  
1823          // Convert colors to XColor array
1824          int num_out = 256;
1825          bool stretch = false;
1826 <        if (IsDirectMode(VideoMonitor.mode)) {
1826 >        if (IsDirectMode(mode)) {
1827                  // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1828                  num_out = vis->map_entries;
1829                  stretch = true;
1830          }
1831 <        XColor *p = palette;
1831 >        XColor *p = x_palette;
1832          for (int i=0; i<num_out; i++) {
1833                  int c = (stretch ? (i * num_in) / num_out : i);
1834                  p->red = pal[c*3 + 0] * 0x0101;
# Line 1760 | Line 1839 | void video_set_palette(uint8 *pal, int n
1839  
1840   #ifdef ENABLE_VOSF
1841          // Recalculate pixel color expansion map
1842 <        if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) {
1842 >        if (!IsDirectMode(mode) && xdepth > 8) {
1843                  for (int i=0; i<256; i++) {
1844                          int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1845                          ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
# Line 1770 | Line 1849 | void video_set_palette(uint8 *pal, int n
1849                  LOCK_VOSF;
1850                  PFLAG_SET_ALL;
1851                  UNLOCK_VOSF;
1852 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1852 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1853          }
1854   #endif
1855  
1856          // Tell redraw thread to change palette
1857 <        palette_changed = true;
1857 >        x_palette_changed = true;
1858  
1859          UNLOCK_PALETTE;
1860   }
# Line 1785 | Line 1864 | void video_set_palette(uint8 *pal, int n
1864   *  Switch video mode
1865   */
1866  
1867 < void video_switch_to_mode(const video_mode &mode)
1867 > void X11_monitor_desc::switch_to_current_mode(void)
1868   {
1869          // Close and reopen display
1870          video_close();
1871 <        video_open(mode);
1871 >        video_open();
1872  
1873          if (drv == NULL) {
1874                  ErrorAlert(STR_OPEN_WINDOW_ERR);
# Line 1968 | Line 2047 | static int event2keycode(XKeyEvent &ev,
2047  
2048   static void handle_events(void)
2049   {
2050 <        while (XPending(x_display)) {
2050 >        for (;;) {
2051                  XEvent event;
2052 <                XNextEvent(x_display, &event);
2052 >                XDisplayLock();
2053 >
2054 >                if (!XCheckMaskEvent(x_display, eventmask, &event)) {
2055 >                        // Handle clipboard events
2056 >                        if (XCheckTypedEvent(x_display, SelectionRequest, &event))
2057 >                                ClipboardSelectionRequest(&event.xselectionrequest);
2058 >                        else if (XCheckTypedEvent(x_display, SelectionClear, &event))
2059 >                                ClipboardSelectionClear(&event.xselectionclear);
2060  
2061 +                        // Window "close" widget clicked
2062 +                        else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
2063 +                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
2064 +                                        ADBKeyDown(0x7f);       // Power key
2065 +                                        ADBKeyUp(0x7f);
2066 +                                }
2067 +                        }
2068 +                        XDisplayUnlock();
2069 +                        break;
2070 +                }
2071 +                
2072                  switch (event.type) {
2073  
2074                          // Mouse button
# Line 2059 | Line 2156 | static void handle_events(void)
2156                          // Hidden parts exposed, force complete refresh of window
2157                          case Expose:
2158                                  if (display_type == DISPLAY_WINDOW) {
2159 +                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
2160   #ifdef ENABLE_VOSF
2161                                          if (use_vosf) {                 // VOSF refresh
2162                                                  LOCK_VOSF;
2163                                                  PFLAG_SET_ALL;
2164                                                  UNLOCK_VOSF;
2165 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2165 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2166                                          }
2167                                          else
2168   #endif
# Line 2075 | Line 2173 | static void handle_events(void)
2173                                                          updt_box[x1][y1] = true;
2174                                                  nr_boxes = 16 * 16;
2175                                          } else                                  // Static refresh
2176 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2079 <                                }
2080 <                                break;
2081 <
2082 <                        // Window "close" widget clicked
2083 <                        case ClientMessage:
2084 <                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
2085 <                                        ADBKeyDown(0x7f);       // Power key
2086 <                                        ADBKeyUp(0x7f);
2176 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2177                                  }
2178                                  break;
2179                  }
2180 +
2181 +                XDisplayUnlock();
2182          }
2183   }
2184  
# Line 2101 | Line 2193 | static void update_display_dynamic(int t
2193          int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
2194          int xil = 0;
2195          int rxm = 0, rxmo = 0;
2196 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2197 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2198 <        int rx = VideoMonitor.mode.bytes_per_row / 16;
2199 <        int ry = VideoMonitor.mode.y / 16;
2196 >        const video_mode &mode = drv->monitor.get_current_mode();
2197 >        int bytes_per_row = mode.bytes_per_row;
2198 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2199 >        int rx = mode.bytes_per_row / 16;
2200 >        int ry = mode.y / 16;
2201          int max_box;
2202  
2203          y2s = sm_uptd[ticker % 8];
# Line 2130 | Line 2223 | static void update_display_dynamic(int t
2223                  }
2224          }
2225  
2226 +        XDisplayLock();
2227          if ((nr_boxes <= max_box) && (nr_boxes)) {
2228                  for (y1=0; y1<16; y1++) {
2229                          for (x1=0; x1<16; x1++) {
# Line 2158 | Line 2252 | static void update_display_dynamic(int t
2252                                          i = (yi * bytes_per_row) + xi;
2253                                          for (y2=0; y2 < yil; y2++, i += bytes_per_row)
2254                                                  memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
2255 <                                        if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2255 >                                        if (mode.depth == VDEPTH_1BIT) {
2256                                                  if (drv->have_shm)
2257                                                          XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
2258                                                  else
# Line 2183 | Line 2277 | static void update_display_dynamic(int t
2277                  }
2278                  nr_boxes = 0;
2279          }
2280 +        XDisplayUnlock();
2281   }
2282  
2283   // Static display update (fixed frame rate, but incremental)
2284   static void update_display_static(driver_window *drv)
2285   {
2286          // Incremental update code
2287 <        int wide = 0, high = 0, x1, x2, y1, y2, i, j;
2288 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2289 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2287 >        unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
2288 >        const video_mode &mode = drv->monitor.get_current_mode();
2289 >        int bytes_per_row = mode.bytes_per_row;
2290 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2291          uint8 *p, *p2;
2292  
2293          // Check for first line from top and first line from bottom that have changed
2294          y1 = 0;
2295 <        for (j=0; j<VideoMonitor.mode.y; j++) {
2295 >        for (j=0; j<mode.y; j++) {
2296                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2297                          y1 = j;
2298                          break;
2299                  }
2300          }
2301          y2 = y1 - 1;
2302 <        for (j=VideoMonitor.mode.y-1; j>=y1; j--) {
2302 >        for (j=mode.y-1; j>=y1; j--) {
2303                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2304                          y2 = j;
2305                          break;
# Line 2213 | Line 2309 | static void update_display_static(driver
2309  
2310          // Check for first column from left and first column from right that have changed
2311          if (high) {
2312 <                if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2313 <                        x1 = VideoMonitor.mode.x - 1;
2312 >                if (mode.depth == VDEPTH_1BIT) {
2313 >                        x1 = mode.x - 1;
2314                          for (j=y1; j<=y2; j++) {
2315                                  p = &the_buffer[j * bytes_per_row];
2316                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2232 | Line 2328 | static void update_display_static(driver
2328                                  p2 = &the_buffer_copy[j * bytes_per_row];
2329                                  p += bytes_per_row;
2330                                  p2 += bytes_per_row;
2331 <                                for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) {
2331 >                                for (i=(mode.x>>3); i>(x2>>3); i--) {
2332                                          p--; p2--;
2333                                          if (*p != *p2) {
2334                                                  x2 = (i << 3) + 7;
# Line 2251 | Line 2347 | static void update_display_static(driver
2347                          }
2348  
2349                  } else {
2350 <                        x1 = VideoMonitor.mode.x;
2350 >                        x1 = mode.x;
2351                          for (j=y1; j<=y2; j++) {
2352                                  p = &the_buffer[j * bytes_per_row];
2353                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2269 | Line 2365 | static void update_display_static(driver
2365                                  p2 = &the_buffer_copy[j * bytes_per_row];
2366                                  p += bytes_per_row;
2367                                  p2 += bytes_per_row;
2368 <                                for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2368 >                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2369                                          p--;
2370                                          p2--;
2371                                          if (*p != *p2) {
# Line 2291 | Line 2387 | static void update_display_static(driver
2387          }
2388  
2389          // Refresh display
2390 +        XDisplayLock();
2391          if (high && wide) {
2392                  if (drv->have_shm)
2393                          XShmPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high, 0);
2394                  else
2395                          XPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high);
2396          }
2397 +        XDisplayUnlock();
2398   }
2399  
2400  
# Line 2336 | Line 2434 | static inline void handle_palette_change
2434   {
2435          LOCK_PALETTE;
2436  
2437 <        if (palette_changed) {
2438 <                palette_changed = false;
2437 >        if (x_palette_changed) {
2438 >                x_palette_changed = false;
2439 >                XDisplayLock();
2440                  drv->update_palette();
2441 +                XDisplayUnlock();
2442          }
2443  
2444          UNLOCK_PALETTE;
# Line 2380 | Line 2480 | static void video_refresh_window_vosf(vo
2480          if (++tick_counter >= frame_skip) {
2481                  tick_counter = 0;
2482                  if (mainBuffer.dirty) {
2483 +                        XDisplayLock();
2484                          LOCK_VOSF;
2485                          update_display_window_vosf(static_cast<driver_window *>(drv));
2486                          UNLOCK_VOSF;
2487                          XSync(x_display, false); // Let the server catch up
2488 +                        XDisplayUnlock();
2489                  }
2490          }
2491   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines