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.62 by gbeauche, 2001-07-14T18:41:05Z vs.
Revision 1.75 by gbeauche, 2004-12-18T19:28:33Z

# 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 188 | Line 192 | static void *redraw_func(void *arg);
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 331 | 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 343 | 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 430 | 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 446 | 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 463 | 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 488 | 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 548 | 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 574 | 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), mac_cursor(0), mouse_grabbed(false)
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 676 | 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 = (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
# Line 704 | 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;
712 <        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 789 | 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 826 | 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 837 | 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 859 | 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 914 | 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 924 | 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 946 | 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 999 | 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 1066 | 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 *)malloc(the_buffer_size);
1134 <          the_buffer = (uint8 *)vm_acquire(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;
1087 <        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 1120 | 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 1131 | 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 1159 | 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 1195 | 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 *)malloc(the_buffer_size);
1274 <          the_buffer = (uint8 *)vm_acquire(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;
1219 <        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 1247 | 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 1255 | 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 1289 | Line 1353 | static void keycode_init(void)
1353  
1354                  // Search for server vendor string, then read keycodes
1355                  const char *vendor = ServerVendor(x_display);
1356 +                // Force use of MacX mappings on MacOS X with Apple's X server
1357 +                int dummy;
1358 +                if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy))
1359 +                        vendor = "MacX";
1360                  bool vendor_found = false;
1361                  char line[256];
1362                  while (fgets(line, 255, f)) {
# Line 1330 | Line 1398 | static void keycode_init(void)
1398          }
1399   }
1400  
1401 < // Open display for specified mode
1402 < static bool video_open(const video_mode &mode)
1401 > // Open display for current mode
1402 > bool X11_monitor_desc::video_open(void)
1403   {
1404          D(bug("video_open()\n"));
1405 +        const video_mode &mode = get_current_mode();
1406  
1407          // Find best available X visual
1408          if (!find_visual_for_depth(mode.depth)) {
# Line 1341 | Line 1410 | static bool video_open(const video_mode
1410                  return false;
1411          }
1412  
1413 +        // Build up visualFormat structure
1414 +        visualFormat.depth = visualInfo.depth;
1415 +        visualFormat.Rmask = visualInfo.red_mask;
1416 +        visualFormat.Gmask = visualInfo.green_mask;
1417 +        visualFormat.Bmask = visualInfo.blue_mask;
1418 +
1419          // Create color maps
1420          if (color_class == PseudoColor || color_class == DirectColor) {
1421                  cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
# Line 1374 | Line 1449 | static bool video_open(const video_mode
1449                  int num = vis->map_entries;
1450                  for (int i=0; i<num; i++) {
1451                          int c = (i * 256) / num;
1452 <                        palette[i].pixel = map_rgb(c, c, c);
1453 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1452 >                        x_palette[i].pixel = map_rgb(c, c, c);
1453 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1454                  }
1455          } else if (color_class == PseudoColor) {
1456                  for (int i=0; i<256; i++) {
1457 <                        palette[i].pixel = i;
1458 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1457 >                        x_palette[i].pixel = i;
1458 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1459                  }
1460          }
1461  
# Line 1388 | Line 1463 | static bool video_open(const video_mode
1463          int num = (color_class == DirectColor ? vis->map_entries : 256);
1464          for (int i=0; i<num; i++) {
1465                  int c = (i * 256) / num;
1466 <                palette[i].red = c * 0x0101;
1467 <                palette[i].green = c * 0x0101;
1468 <                palette[i].blue = c * 0x0101;
1466 >                x_palette[i].red = c * 0x0101;
1467 >                x_palette[i].green = c * 0x0101;
1468 >                x_palette[i].blue = c * 0x0101;
1469          }
1470          if (color_class == PseudoColor || color_class == DirectColor) {
1471 <                XStoreColors(x_display, cmap[0], palette, num);
1472 <                XStoreColors(x_display, cmap[1], palette, num);
1471 >                XStoreColors(x_display, cmap[0], x_palette, num);
1472 >                XStoreColors(x_display, cmap[1], x_palette, num);
1473          }
1474  
1475   #ifdef ENABLE_VOSF
# Line 1407 | Line 1482 | static bool video_open(const video_mode
1482          // Create display driver object of requested type
1483          switch (display_type) {
1484                  case DISPLAY_WINDOW:
1485 <                        drv = new driver_window(mode);
1485 >                        drv = new driver_window(*this);
1486                          break;
1487   #ifdef ENABLE_FBDEV_DGA
1488                  case DISPLAY_DGA:
1489 <                        drv = new driver_fbdev(mode);
1489 >                        drv = new driver_fbdev(*this);
1490                          break;
1491   #endif
1492   #ifdef ENABLE_XF86_DGA
1493                  case DISPLAY_DGA:
1494 <                        drv = new driver_xf86dga(mode);
1494 >                        drv = new driver_xf86dga(*this);
1495                          break;
1496   #endif
1497          }
# Line 1431 | Line 1506 | static bool video_open(const video_mode
1506   #ifdef ENABLE_VOSF
1507          if (use_vosf) {
1508                  // Initialize the VOSF system
1509 <                if (!video_vosf_init()) {
1509 >                if (!video_vosf_init(*this)) {
1510                          ErrorAlert(STR_VOSF_INIT_ERR);
1511                  return false;
1512                  }
# Line 1448 | Line 1523 | static bool video_open(const video_mode
1523          // Start redraw/input thread
1524   #ifdef HAVE_PTHREADS
1525          redraw_thread_cancel = false;
1526 <        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1526 >        Set_pthread_attr(&redraw_thread_attr, 0);
1527 >        redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1528          if (!redraw_thread_active) {
1529                  printf("FATAL: cannot create redraw thread\n");
1530                  return false;
# Line 1492 | Line 1568 | bool VideoInit(bool classic)
1568                  ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1569                  return false;
1570          }
1571 <        sort(avail_depths, avail_depths + num_depths);
1571 >        std::sort(avail_depths, avail_depths + num_depths);
1572          
1573   #ifdef ENABLE_FBDEV_DGA
1574          // Frame buffer name
# Line 1595 | Line 1671 | bool VideoInit(bool classic)
1671                  ErrorAlert(STR_NO_XVISUAL_ERR);
1672                  return false;
1673          }
1674 <        video_init_depth_list();
1674 >
1675 >        // Find requested default mode with specified dimensions
1676 >        uint32 default_id;
1677 >        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1678 >        for (i = VideoModes.begin(); i != end; ++i) {
1679 >                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1680 >                        default_id = i->resolution_id;
1681 >                        break;
1682 >                }
1683 >        }
1684 >        if (i == end) { // not found, use first available mode
1685 >                default_depth = VideoModes[0].depth;
1686 >                default_id = VideoModes[0].resolution_id;
1687 >        }
1688  
1689   #if DEBUG
1690          D(bug("Available video modes:\n"));
1691 <        vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
1603 <        while (i != end) {
1691 >        for (i = VideoModes.begin(); i != end; ++i) {
1692                  int bits = 1 << i->depth;
1693                  if (bits == 16)
1694                          bits = 15;
1695                  else if (bits == 32)
1696                          bits = 24;
1697                  D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1610                ++i;
1698          }
1699   #endif
1700  
1701 <        // Find requested default mode and open display
1702 <        if (VideoModes.size() == 1)
1703 <                return video_open(VideoModes[0]);
1704 <        else {
1705 <                // Find mode with specified dimensions
1706 <                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 <        }
1701 >        // Create X11_monitor_desc for this (the only) display
1702 >        X11_monitor_desc *monitor = new X11_monitor_desc(VideoModes, default_depth, default_id);
1703 >        VideoMonitors.push_back(monitor);
1704 >
1705 >        // Open display
1706 >        return monitor->video_open();
1707   }
1708  
1709  
# Line 1631 | Line 1712 | bool VideoInit(bool classic)
1712   */
1713  
1714   // Close display
1715 < static void video_close(void)
1715 > void X11_monitor_desc::video_close(void)
1716   {
1717          D(bug("video_close()\n"));
1718  
# Line 1676 | Line 1757 | static void video_close(void)
1757  
1758   void VideoExit(void)
1759   {
1760 <        // Close display
1761 <        video_close();
1760 >        // Close displays
1761 >        vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
1762 >        for (i = VideoMonitors.begin(); i != end; ++i)
1763 >                dynamic_cast<X11_monitor_desc *>(*i)->video_close();
1764  
1765   #ifdef ENABLE_XF86_VIDMODE
1766          // Free video mode list
# Line 1735 | Line 1818 | void VideoInterrupt(void)
1818   *  Set palette
1819   */
1820  
1821 < void video_set_palette(uint8 *pal, int num_in)
1821 > void X11_monitor_desc::set_palette(uint8 *pal, int num_in)
1822   {
1823 +        const video_mode &mode = get_current_mode();
1824 +
1825          LOCK_PALETTE;
1826  
1827          // Convert colors to XColor array
1828          int num_out = 256;
1829          bool stretch = false;
1830 <        if (IsDirectMode(VideoMonitor.mode)) {
1830 >        if (IsDirectMode(mode)) {
1831                  // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1832                  num_out = vis->map_entries;
1833                  stretch = true;
1834          }
1835 <        XColor *p = palette;
1835 >        XColor *p = x_palette;
1836          for (int i=0; i<num_out; i++) {
1837                  int c = (stretch ? (i * num_in) / num_out : i);
1838                  p->red = pal[c*3 + 0] * 0x0101;
# Line 1758 | Line 1843 | void video_set_palette(uint8 *pal, int n
1843  
1844   #ifdef ENABLE_VOSF
1845          // Recalculate pixel color expansion map
1846 <        if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) {
1846 >        if (!IsDirectMode(mode) && xdepth > 8) {
1847                  for (int i=0; i<256; i++) {
1848                          int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1849                          ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
# Line 1768 | Line 1853 | void video_set_palette(uint8 *pal, int n
1853                  LOCK_VOSF;
1854                  PFLAG_SET_ALL;
1855                  UNLOCK_VOSF;
1856 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1856 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1857          }
1858   #endif
1859  
1860          // Tell redraw thread to change palette
1861 <        palette_changed = true;
1861 >        x_palette_changed = true;
1862  
1863          UNLOCK_PALETTE;
1864   }
# Line 1783 | Line 1868 | void video_set_palette(uint8 *pal, int n
1868   *  Switch video mode
1869   */
1870  
1871 < void video_switch_to_mode(const video_mode &mode)
1871 > void X11_monitor_desc::switch_to_current_mode(void)
1872   {
1873          // Close and reopen display
1874          video_close();
1875 <        video_open(mode);
1875 >        video_open();
1876  
1877          if (drv == NULL) {
1878                  ErrorAlert(STR_OPEN_WINDOW_ERR);
# Line 1966 | Line 2051 | static int event2keycode(XKeyEvent &ev,
2051  
2052   static void handle_events(void)
2053   {
2054 <        while (XPending(x_display)) {
2054 >        for (;;) {
2055                  XEvent event;
2056 <                XNextEvent(x_display, &event);
2056 >                XDisplayLock();
2057 >
2058 >                if (!XCheckMaskEvent(x_display, eventmask, &event)) {
2059 >                        // Handle clipboard events
2060 >                        if (XCheckTypedEvent(x_display, SelectionRequest, &event))
2061 >                                ClipboardSelectionRequest(&event.xselectionrequest);
2062 >                        else if (XCheckTypedEvent(x_display, SelectionClear, &event))
2063 >                                ClipboardSelectionClear(&event.xselectionclear);
2064  
2065 +                        // Window "close" widget clicked
2066 +                        else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
2067 +                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
2068 +                                        ADBKeyDown(0x7f);       // Power key
2069 +                                        ADBKeyUp(0x7f);
2070 +                                }
2071 +                        }
2072 +                        XDisplayUnlock();
2073 +                        break;
2074 +                }
2075 +                
2076                  switch (event.type) {
2077  
2078                          // Mouse button
# Line 2057 | Line 2160 | static void handle_events(void)
2160                          // Hidden parts exposed, force complete refresh of window
2161                          case Expose:
2162                                  if (display_type == DISPLAY_WINDOW) {
2163 +                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
2164   #ifdef ENABLE_VOSF
2165                                          if (use_vosf) {                 // VOSF refresh
2166                                                  LOCK_VOSF;
2167                                                  PFLAG_SET_ALL;
2168                                                  UNLOCK_VOSF;
2169 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2169 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2170                                          }
2171                                          else
2172   #endif
# Line 2073 | Line 2177 | static void handle_events(void)
2177                                                          updt_box[x1][y1] = true;
2178                                                  nr_boxes = 16 * 16;
2179                                          } else                                  // Static refresh
2180 <                                                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);
2180 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2181                                  }
2182                                  break;
2183                  }
2184 +
2185 +                XDisplayUnlock();
2186          }
2187   }
2188  
# Line 2099 | Line 2197 | static void update_display_dynamic(int t
2197          int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
2198          int xil = 0;
2199          int rxm = 0, rxmo = 0;
2200 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2201 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2202 <        int rx = VideoMonitor.mode.bytes_per_row / 16;
2203 <        int ry = VideoMonitor.mode.y / 16;
2200 >        const video_mode &mode = drv->monitor.get_current_mode();
2201 >        int bytes_per_row = mode.bytes_per_row;
2202 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2203 >        int rx = mode.bytes_per_row / 16;
2204 >        int ry = mode.y / 16;
2205          int max_box;
2206  
2207          y2s = sm_uptd[ticker % 8];
# Line 2128 | Line 2227 | static void update_display_dynamic(int t
2227                  }
2228          }
2229  
2230 +        XDisplayLock();
2231          if ((nr_boxes <= max_box) && (nr_boxes)) {
2232                  for (y1=0; y1<16; y1++) {
2233                          for (x1=0; x1<16; x1++) {
# Line 2156 | Line 2256 | static void update_display_dynamic(int t
2256                                          i = (yi * bytes_per_row) + xi;
2257                                          for (y2=0; y2 < yil; y2++, i += bytes_per_row)
2258                                                  memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
2259 <                                        if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2259 >                                        if (mode.depth == VDEPTH_1BIT) {
2260                                                  if (drv->have_shm)
2261                                                          XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
2262                                                  else
# Line 2181 | Line 2281 | static void update_display_dynamic(int t
2281                  }
2282                  nr_boxes = 0;
2283          }
2284 +        XDisplayUnlock();
2285   }
2286  
2287   // Static display update (fixed frame rate, but incremental)
# Line 2188 | Line 2289 | static void update_display_static(driver
2289   {
2290          // Incremental update code
2291          unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
2292 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2293 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2292 >        const video_mode &mode = drv->monitor.get_current_mode();
2293 >        int bytes_per_row = mode.bytes_per_row;
2294 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2295          uint8 *p, *p2;
2296  
2297          // Check for first line from top and first line from bottom that have changed
2298          y1 = 0;
2299 <        for (j=0; j<VideoMonitor.mode.y; j++) {
2299 >        for (j=0; j<mode.y; j++) {
2300                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2301                          y1 = j;
2302                          break;
2303                  }
2304          }
2305          y2 = y1 - 1;
2306 <        for (j=VideoMonitor.mode.y-1; j>=y1; j--) {
2306 >        for (j=mode.y-1; j>=y1; j--) {
2307                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2308                          y2 = j;
2309                          break;
# Line 2211 | Line 2313 | static void update_display_static(driver
2313  
2314          // Check for first column from left and first column from right that have changed
2315          if (high) {
2316 <                if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2317 <                        x1 = VideoMonitor.mode.x - 1;
2316 >                if (mode.depth == VDEPTH_1BIT) {
2317 >                        x1 = mode.x - 1;
2318                          for (j=y1; j<=y2; j++) {
2319                                  p = &the_buffer[j * bytes_per_row];
2320                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2230 | Line 2332 | static void update_display_static(driver
2332                                  p2 = &the_buffer_copy[j * bytes_per_row];
2333                                  p += bytes_per_row;
2334                                  p2 += bytes_per_row;
2335 <                                for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) {
2335 >                                for (i=(mode.x>>3); i>(x2>>3); i--) {
2336                                          p--; p2--;
2337                                          if (*p != *p2) {
2338                                                  x2 = (i << 3) + 7;
# Line 2249 | Line 2351 | static void update_display_static(driver
2351                          }
2352  
2353                  } else {
2354 <                        x1 = VideoMonitor.mode.x;
2354 >                        x1 = mode.x;
2355                          for (j=y1; j<=y2; j++) {
2356                                  p = &the_buffer[j * bytes_per_row];
2357                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2267 | Line 2369 | static void update_display_static(driver
2369                                  p2 = &the_buffer_copy[j * bytes_per_row];
2370                                  p += bytes_per_row;
2371                                  p2 += bytes_per_row;
2372 <                                for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2372 >                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2373                                          p--;
2374                                          p2--;
2375                                          if (*p != *p2) {
# Line 2289 | Line 2391 | static void update_display_static(driver
2391          }
2392  
2393          // Refresh display
2394 +        XDisplayLock();
2395          if (high && wide) {
2396                  if (drv->have_shm)
2397                          XShmPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high, 0);
2398                  else
2399                          XPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high);
2400          }
2401 +        XDisplayUnlock();
2402   }
2403  
2404  
# Line 2334 | Line 2438 | static inline void handle_palette_change
2438   {
2439          LOCK_PALETTE;
2440  
2441 <        if (palette_changed) {
2442 <                palette_changed = false;
2441 >        if (x_palette_changed) {
2442 >                x_palette_changed = false;
2443 >                XDisplayLock();
2444                  drv->update_palette();
2445 +                XDisplayUnlock();
2446          }
2447  
2448          UNLOCK_PALETTE;
# Line 2378 | Line 2484 | static void video_refresh_window_vosf(vo
2484          if (++tick_counter >= frame_skip) {
2485                  tick_counter = 0;
2486                  if (mainBuffer.dirty) {
2487 +                        XDisplayLock();
2488                          LOCK_VOSF;
2489                          update_display_window_vosf(static_cast<driver_window *>(drv));
2490                          UNLOCK_VOSF;
2491                          XSync(x_display, false); // Let the server catch up
2492 +                        XDisplayUnlock();
2493                  }
2494          }
2495   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines