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.61 by cebix, 2001-07-14T15:02:47Z vs.
Revision 1.69 by gbeauche, 2003-05-22T22:13:56Z

# 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-2002 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 71 | Line 67 | using std::sort;
67   #include "debug.h"
68  
69  
70 + // Supported video modes
71 + static vector<video_mode> VideoModes;
72 +
73   // Display types
74   enum {
75          DISPLAY_WINDOW, // X11 window, using MIT SHM extensions if possible
# Line 97 | Line 96 | static uint32 the_buffer_size;                                         // S
96  
97   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
98   #ifdef HAVE_PTHREADS
99 + static pthread_attr_t redraw_thread_attr;                       // Redraw thread attributes
100   static volatile bool redraw_thread_cancel;                      // Flag: Cancel Redraw thread
101   static pthread_t redraw_thread;                                         // Redraw thread
102   #endif
# Line 139 | Line 139 | static int rshift, rloss, gshift, gloss,
139  
140   static Colormap cmap[2] = {0, 0};                                       // Colormaps for indexed modes (DGA needs two of them)
141  
142 < static XColor palette[256];                                                     // Color palette to be used as CLUT and gamma table
143 < static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
142 > static XColor x_palette[256];                                                   // Color palette to be used as CLUT and gamma table
143 > static bool x_palette_changed = false;                          // Flag: Palette changed, redraw thread must set new colors
144  
145   #ifdef ENABLE_FBDEV_DGA
146   static int fbdev_fd = -1;
# Line 153 | Line 153 | static int num_x_video_modes;
153  
154   // Mutex to protect palette
155   #ifdef HAVE_PTHREADS
156 < static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;
157 < #define LOCK_PALETTE pthread_mutex_lock(&palette_lock)
158 < #define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock)
156 > static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER;
157 > #define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock)
158 > #define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock)
159   #else
160   #define LOCK_PALETTE
161   #define UNLOCK_PALETTE
# Line 194 | Line 194 | extern void SysMountFirstFloppy(void);
194  
195  
196   /*
197 + *  monitor_desc subclass for X11 display
198 + */
199 +
200 + class X11_monitor_desc : public monitor_desc {
201 + public:
202 +        X11_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
203 +        ~X11_monitor_desc() {}
204 +
205 +        virtual void switch_to_current_mode(void);
206 +        virtual void set_palette(uint8 *pal, int num);
207 +
208 +        bool video_open(void);
209 +        void video_close(void);
210 + };
211 +
212 +
213 + /*
214   *  Utility functions
215   */
216  
217 + // Map video_mode depth ID to numerical depth value
218 + static inline int depth_of_video_mode(video_mode const & mode)
219 + {
220 +        int depth = -1;
221 +        switch (mode.depth) {
222 +        case VDEPTH_1BIT:
223 +                depth = 1;
224 +                break;
225 +        case VDEPTH_2BIT:
226 +                depth = 2;
227 +                break;
228 +        case VDEPTH_4BIT:
229 +                depth = 4;
230 +                break;
231 +        case VDEPTH_8BIT:
232 +                depth = 8;
233 +                break;
234 +        case VDEPTH_16BIT:
235 +                depth = 16;
236 +                break;
237 +        case VDEPTH_32BIT:
238 +                depth = 32;
239 +                break;
240 +        default:
241 +                abort();
242 +        }
243 +        return depth;
244 + }
245 +
246   // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
247   static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
248   {
# Line 331 | Line 377 | static void add_window_modes(video_depth
377   }
378  
379   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
380 < static void set_mac_frame_buffer(video_depth depth, bool native_byte_order)
380 > static void set_mac_frame_buffer(X11_monitor_desc &monitor, video_depth depth, bool native_byte_order)
381   {
382   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
383          int layout = FLAYOUT_DIRECT;
# Line 343 | Line 389 | static void set_mac_frame_buffer(video_d
389                  MacFrameLayout = layout;
390          else
391                  MacFrameLayout = FLAYOUT_DIRECT;
392 <        VideoMonitor.mac_frame_base = MacFrameBaseMac;
392 >        monitor.set_mac_frame_base(MacFrameBaseMac);
393  
394          // Set variables used by UAE memory banking
395 +        const video_mode &mode = monitor.get_current_mode();
396          MacFrameBaseHost = the_buffer;
397 <        MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
397 >        MacFrameSize = mode.bytes_per_row * mode.y;
398          InitFrameBufferMapping();
399   #else
400 <        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
400 >        monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
401   #endif
402 <        D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base));
402 >        D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
403   }
404  
405   // Set window name and class
# Line 430 | Line 477 | static int error_handler(Display *d, XEr
477  
478   class driver_base {
479   public:
480 <        driver_base();
480 >        driver_base(X11_monitor_desc &m);
481          virtual ~driver_base();
482  
483          virtual void update_palette(void);
# Line 446 | Line 493 | public:
493          virtual void ungrab_mouse(void) {}
494  
495   public:
496 +        X11_monitor_desc &monitor; // Associated video monitor
497 +        const video_mode &mode;    // Video mode handled by the driver
498 +
499          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
500          Window w;               // The window we draw into
501  
# Line 463 | Line 513 | class driver_window : public driver_base
513          friend void update_display_static(driver_window *drv);
514  
515   public:
516 <        driver_window(const video_mode &mode);
516 >        driver_window(X11_monitor_desc &monitor);
517          ~driver_window();
518  
519          void toggle_mouse_grab(void);
# Line 488 | Line 538 | static driver_base *drv = NULL;        // Point
538   # include "video_vosf.h"
539   #endif
540  
541 < driver_base::driver_base()
542 < : init_ok(false), w(0)
541 > driver_base::driver_base(X11_monitor_desc &m)
542 > : monitor(m), mode(m.get_current_mode()), init_ok(false), w(0)
543   {
544          the_buffer = NULL;
545          the_buffer_copy = NULL;
# Line 523 | Line 573 | driver_base::~driver_base()
573          }
574   #ifdef ENABLE_VOSF
575          else {
576 +                // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
577 +                if (the_buffer != VM_MAP_FAILED) {
578 +                        D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
579 +                        vm_release(the_buffer, the_buffer_size);
580 +                        the_buffer = NULL;
581 +                }
582                  if (the_host_buffer) {
583                          D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
584                          free(the_host_buffer);
585                          the_host_buffer = NULL;
586                  }
531                if (the_buffer) {
532                        D(bug(" freeing the_buffer at %p\n", the_buffer));
533                        free(the_buffer);
534                        the_buffer = NULL;
535                }
587                  if (the_buffer_copy) {
588                          D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
589                          free(the_buffer_copy);
# Line 547 | Line 598 | void driver_base::update_palette(void)
598   {
599          if (color_class == PseudoColor || color_class == DirectColor) {
600                  int num = vis->map_entries;
601 <                if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor)
601 >                if (!IsDirectMode(monitor.get_current_mode()) && color_class == DirectColor)
602                          return; // Indexed mode on true color screen, don't set CLUT
603 <                XStoreColors(x_display, cmap[0], palette, num);
604 <                XStoreColors(x_display, cmap[1], palette, num);
603 >                XStoreColors(x_display, cmap[0], x_palette, num);
604 >                XStoreColors(x_display, cmap[1], x_palette, num);
605          }
606          XSync(x_display, false);
607   }
# Line 573 | Line 624 | void driver_base::restore_mouse_accel(vo
624   */
625  
626   // Open display
627 < driver_window::driver_window(const video_mode &mode)
628 < : gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
627 > driver_window::driver_window(X11_monitor_desc &m)
628 > : driver_base(m), gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
629   {
630          int width = mode.x, height = mode.y;
631          int aligned_width = (width + 15) & ~15;
# Line 583 | Line 634 | driver_window::driver_window(const video
634          // Set absolute mouse mode
635          ADBSetRelMouseMode(mouse_grabbed);
636  
637 <        // Create window (setting backround_pixel, border_pixel and colormap is
637 >        // Create window (setting background_pixel, border_pixel and colormap is
638          // mandatory when using a non-default visual; in 1-bit mode we use the
639          // default visual, so we can also use the default colormap)
640          XSetWindowAttributes wattr;
# Line 675 | Line 726 | driver_window::driver_window(const video
726          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
727          the_host_buffer = the_buffer_copy;
728          the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
678        the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
729          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
730 +        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
731          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
732   #else
733          // Allocate memory for frame buffer
# Line 703 | Line 754 | driver_window::driver_window(const video
754          native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
755   #endif
756   #ifdef ENABLE_VOSF
757 <        Screen_blitter_init(&visualInfo, native_byte_order, mode.depth);
757 >        Screen_blitter_init(&visualInfo, native_byte_order, depth_of_video_mode(mode));
758   #endif
759  
760 <        // Set VideoMonitor
761 <        VideoMonitor.mode = mode;
711 <        set_mac_frame_buffer(mode.depth, native_byte_order);
760 >        // Set frame buffer base
761 >        set_mac_frame_buffer(monitor, mode.depth, native_byte_order);
762  
763          // Everything went well
764          init_ok = true;
# Line 725 | Line 775 | driver_window::~driver_window()
775                  the_buffer_copy = NULL; // don't free() in driver_base dtor
776   #endif
777          }
728 #ifdef ENABLE_VOSF
729        if (use_vosf) {
730                // don't free() memory mapped buffers in driver_base dtor
731                if (the_buffer != VM_MAP_FAILED) {
732                        D(bug(" releasing the_buffer at %p\n", the_buffer));
733                        vm_release(the_buffer, the_buffer_size);
734                        the_buffer = NULL;
735                }
736                if (the_buffer_copy != VM_MAP_FAILED) {
737                        D(bug(" releasing the_buffer_copy at %p\n", the_buffer_copy));
738                        vm_release(the_buffer_copy, the_buffer_size);
739                        the_buffer_copy = NULL;
740                }
741        }
742 #endif
778          if (img) {
779                  if (!have_shm)
780                          img->data = NULL;
# Line 803 | Line 838 | void driver_window::mouse_moved(int x, i
838          // Warped mouse motion (this code is taken from SDL)
839  
840          // Post first mouse event
841 <        int width = VideoMonitor.mode.x, height = VideoMonitor.mode.y;
841 >        int width = monitor.get_current_mode().x, height = monitor.get_current_mode().y;
842          int delta_x = x - mouse_last_x, delta_y = y - mouse_last_y;
843          mouse_last_x = x; mouse_last_y = y;
844          ADBMouseMoved(delta_x, delta_y);
# Line 840 | Line 875 | void driver_window::mouse_moved(int x, i
875  
876   class driver_dga : public driver_base {
877   public:
878 <        driver_dga();
878 >        driver_dga(X11_monitor_desc &monitor);
879          ~driver_dga();
880  
881          void suspend(void);
# Line 851 | Line 886 | private:
886          void *fb_save;                  // Saved frame buffer for suspend/resume
887   };
888  
889 < driver_dga::driver_dga()
890 < : suspend_win(0), fb_save(NULL)
889 > driver_dga::driver_dga(X11_monitor_desc &m)
890 > : driver_base(m), suspend_win(0), fb_save(NULL)
891   {
892   }
893  
# Line 873 | Line 908 | void driver_dga::suspend(void)
908          LOCK_FRAME_BUFFER;
909  
910          // Save frame buffer
911 <        fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
911 >        fb_save = malloc(mode.y * mode.bytes_per_row);
912          if (fb_save)
913 <                memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
913 >                memcpy(fb_save, the_buffer, mode.y * mode.bytes_per_row);
914  
915          // Close full screen display
916   #ifdef ENABLE_XF86_DGA
# Line 928 | Line 963 | void driver_dga::resume(void)
963                  LOCK_VOSF;
964                  PFLAG_SET_ALL;
965                  UNLOCK_VOSF;
966 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
966 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
967          }
968   #endif
969          
# Line 938 | Line 973 | void driver_dga::resume(void)
973                  // Don't copy fb_save to the temporary frame buffer in VOSF mode
974                  if (!use_vosf)
975   #endif
976 <                memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
976 >                memcpy(the_buffer, fb_save, mode.y * mode.bytes_per_row);
977                  free(fb_save);
978                  fb_save = NULL;
979          }
# Line 960 | Line 995 | const char FBDEVICE_FILE_NAME[] = "/dev/
995  
996   class driver_fbdev : public driver_dga {
997   public:
998 <        driver_fbdev(const video_mode &mode);
998 >        driver_fbdev(X11_monitor_desc &monitor);
999          ~driver_fbdev();
1000   };
1001  
1002   // Open display
1003 < driver_fbdev::driver_fbdev(const video_mode &mode)
1003 > driver_fbdev::driver_fbdev(X11_monitor_desc &m) : driver_dga(m)
1004   {
1005          int width = mode.x, height = mode.y;
1006  
# Line 1013 | Line 1048 | driver_fbdev::driver_fbdev(const video_m
1048                  if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
1049                          continue;
1050                  
1051 <                if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
1051 >                if ((sscanf(line, "%19s %d %x", fb_name, &fb_depth, &fb_offset) == 3)
1052                   && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) {
1053                          device_found = true;
1054                          break;
# Line 1086 | Line 1121 | driver_fbdev::driver_fbdev(const video_m
1121            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1122            the_host_buffer = the_buffer;
1123            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1124 <          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
1124 >          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1125            the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1126          }
1127   #else
# Line 1094 | Line 1129 | driver_fbdev::driver_fbdev(const video_m
1129   #endif
1130   #endif
1131          
1132 <        // Set VideoMonitor
1133 <        VideoModes[0].bytes_per_row = bytes_per_row;
1134 <        VideoModes[0].depth = DepthModeForPixelDepth(fb_depth);
1135 <        VideoMonitor.mode = mode;
1101 <        set_mac_frame_buffer(mode.depth, true);
1132 >        // Set frame buffer base
1133 >        const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1134 >        const_cast<video_mode *>(&mode)->depth = DepthModeForPixelDepth(fb_depth);
1135 >        set_mac_frame_buffer(monitor, mode.depth, true);
1136  
1137          // Everything went well
1138          init_ok = true;
# Line 1121 | Line 1155 | driver_fbdev::~driver_fbdev()
1155                          munmap(the_host_buffer, the_buffer_size);
1156                          the_host_buffer = NULL;
1157                  }
1124                if (the_buffer_copy != VM_MAP_FAILED) {
1125                        vm_release(the_buffer_copy, the_buffer_size);
1126                        the_buffer_copy = NULL;
1127                }
1128                if (the_buffer != VM_MAP_FAILED) {
1129                        vm_release(the_buffer, the_buffer_size);
1130                        the_buffer = NULL;
1131                }
1158          }
1159   #endif
1160   }
# Line 1142 | Line 1168 | driver_fbdev::~driver_fbdev()
1168  
1169   class driver_xf86dga : public driver_dga {
1170   public:
1171 <        driver_xf86dga(const video_mode &mode);
1171 >        driver_xf86dga(X11_monitor_desc &monitor);
1172          ~driver_xf86dga();
1173  
1174          void update_palette(void);
# Line 1153 | Line 1179 | private:
1179   };
1180  
1181   // Open display
1182 < driver_xf86dga::driver_xf86dga(const video_mode &mode)
1183 < : current_dga_cmap(0)
1182 > driver_xf86dga::driver_xf86dga(X11_monitor_desc &m)
1183 > : driver_dga(m), current_dga_cmap(0)
1184   {
1185          int width = mode.x, height = mode.y;
1186  
# Line 1181 | Line 1207 | driver_xf86dga::driver_xf86dga(const vid
1207          XSetWindowAttributes wattr;
1208          wattr.event_mask = eventmask = dga_eventmask;
1209          wattr.override_redirect = True;
1210 +        wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
1211  
1212          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
1213 <                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
1213 >                InputOutput, vis, CWEventMask | CWOverrideRedirect |
1214 >                (color_class == DirectColor ? CWColormap : 0), &wattr);
1215  
1216          // Set window name/class
1217          set_window_name(w, STR_WINDOW_TITLE);
# Line 1217 | Line 1245 | driver_xf86dga::driver_xf86dga(const vid
1245  
1246          // Init blitting routines
1247          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
1248 < #if VIDEO_VOSF
1248 > #if ENABLE_VOSF
1249 >        bool native_byte_order;
1250 > #ifdef WORDS_BIGENDIAN
1251 >        native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
1252 > #else
1253 >        native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
1254 > #endif
1255   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1256          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1257          // i.e. the framebuffer update function is not Blit_Copy_Raw
1258 <        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1258 >        use_vosf = Screen_blitter_init(&visualInfo, native_byte_order, depth_of_video_mode(mode));
1259          
1260          if (use_vosf) {
1261            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1262            the_host_buffer = the_buffer;
1263            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1264 <          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
1264 >          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1265            the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1266          }
1267   #else
# Line 1235 | Line 1269 | driver_xf86dga::driver_xf86dga(const vid
1269   #endif
1270   #endif
1271          
1272 <        // Set VideoMonitor
1272 >        // Set frame buffer base
1273          const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1274 <        VideoMonitor.mode = mode;
1241 <        set_mac_frame_buffer(mode.depth, true);
1274 >        set_mac_frame_buffer(monitor, mode.depth, true);
1275  
1276          // Everything went well
1277          init_ok = true;
# Line 1256 | Line 1289 | driver_xf86dga::~driver_xf86dga()
1289          else {
1290                  // don't free() the screen buffer in driver_base dtor
1291                  the_host_buffer = NULL;
1259                
1260                if (the_buffer_copy != VM_MAP_FAILED) {
1261                        vm_release(the_buffer_copy, the_buffer_size);
1262                        the_buffer_copy = NULL;
1263                }
1264                if (the_buffer != VM_MAP_FAILED) {
1265                        vm_release(the_buffer, the_buffer_size);
1266                        the_buffer = NULL;
1267                }
1292          }
1293   #endif
1294   #ifdef ENABLE_XF86_VIDMODE
# Line 1278 | Line 1302 | void driver_xf86dga::update_palette(void
1302   {
1303          driver_dga::update_palette();
1304          current_dga_cmap ^= 1;
1305 <        if (!IsDirectMode(VideoMonitor.mode) && cmap[current_dga_cmap])
1305 >        if (!IsDirectMode(monitor.get_current_mode()) && cmap[current_dga_cmap])
1306                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1307   }
1308  
# Line 1286 | Line 1310 | void driver_xf86dga::update_palette(void
1310   void driver_xf86dga::resume(void)
1311   {
1312          driver_dga::resume();
1313 <        if (!IsDirectMode(VideoMonitor.mode))
1313 >        if (!IsDirectMode(monitor.get_current_mode()))
1314                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1315   }
1316   #endif
# Line 1361 | Line 1385 | static void keycode_init(void)
1385          }
1386   }
1387  
1388 < // Open display for specified mode
1389 < static bool video_open(const video_mode &mode)
1388 > // Open display for current mode
1389 > bool X11_monitor_desc::video_open(void)
1390   {
1391          D(bug("video_open()\n"));
1392 +        const video_mode &mode = get_current_mode();
1393  
1394          // Find best available X visual
1395          if (!find_visual_for_depth(mode.depth)) {
# Line 1405 | Line 1430 | static bool video_open(const video_mode
1430                  int num = vis->map_entries;
1431                  for (int i=0; i<num; i++) {
1432                          int c = (i * 256) / num;
1433 <                        palette[i].pixel = map_rgb(c, c, c);
1434 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1433 >                        x_palette[i].pixel = map_rgb(c, c, c);
1434 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1435                  }
1436          } else if (color_class == PseudoColor) {
1437                  for (int i=0; i<256; i++) {
1438 <                        palette[i].pixel = i;
1439 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1438 >                        x_palette[i].pixel = i;
1439 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1440                  }
1441          }
1442  
# Line 1419 | Line 1444 | static bool video_open(const video_mode
1444          int num = (color_class == DirectColor ? vis->map_entries : 256);
1445          for (int i=0; i<num; i++) {
1446                  int c = (i * 256) / num;
1447 <                palette[i].red = c * 0x0101;
1448 <                palette[i].green = c * 0x0101;
1449 <                palette[i].blue = c * 0x0101;
1447 >                x_palette[i].red = c * 0x0101;
1448 >                x_palette[i].green = c * 0x0101;
1449 >                x_palette[i].blue = c * 0x0101;
1450          }
1451          if (color_class == PseudoColor || color_class == DirectColor) {
1452 <                XStoreColors(x_display, cmap[0], palette, num);
1453 <                XStoreColors(x_display, cmap[1], palette, num);
1452 >                XStoreColors(x_display, cmap[0], x_palette, num);
1453 >                XStoreColors(x_display, cmap[1], x_palette, num);
1454          }
1455  
1456   #ifdef ENABLE_VOSF
# Line 1438 | Line 1463 | static bool video_open(const video_mode
1463          // Create display driver object of requested type
1464          switch (display_type) {
1465                  case DISPLAY_WINDOW:
1466 <                        drv = new driver_window(mode);
1466 >                        drv = new driver_window(*this);
1467                          break;
1468   #ifdef ENABLE_FBDEV_DGA
1469                  case DISPLAY_DGA:
1470 <                        drv = new driver_fbdev(mode);
1470 >                        drv = new driver_fbdev(*this);
1471                          break;
1472   #endif
1473   #ifdef ENABLE_XF86_DGA
1474                  case DISPLAY_DGA:
1475 <                        drv = new driver_xf86dga(mode);
1475 >                        drv = new driver_xf86dga(*this);
1476                          break;
1477   #endif
1478          }
# Line 1462 | Line 1487 | static bool video_open(const video_mode
1487   #ifdef ENABLE_VOSF
1488          if (use_vosf) {
1489                  // Initialize the VOSF system
1490 <                if (!video_vosf_init()) {
1490 >                if (!video_vosf_init(*this)) {
1491                          ErrorAlert(STR_VOSF_INIT_ERR);
1492                  return false;
1493                  }
# Line 1479 | Line 1504 | static bool video_open(const video_mode
1504          // Start redraw/input thread
1505   #ifdef HAVE_PTHREADS
1506          redraw_thread_cancel = false;
1507 <        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1507 >        Set_pthread_attr(&redraw_thread_attr, 0);
1508 >        redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1509          if (!redraw_thread_active) {
1510                  printf("FATAL: cannot create redraw thread\n");
1511                  return false;
# Line 1523 | Line 1549 | bool VideoInit(bool classic)
1549                  ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1550                  return false;
1551          }
1552 <        sort(avail_depths, avail_depths + num_depths);
1552 >        std::sort(avail_depths, avail_depths + num_depths);
1553          
1554   #ifdef ENABLE_FBDEV_DGA
1555          // Frame buffer name
# Line 1626 | Line 1652 | bool VideoInit(bool classic)
1652                  ErrorAlert(STR_NO_XVISUAL_ERR);
1653                  return false;
1654          }
1655 <        video_init_depth_list();
1655 >
1656 >        // Find requested default mode with specified dimensions
1657 >        uint32 default_id;
1658 >        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1659 >        for (i = VideoModes.begin(); i != end; ++i) {
1660 >                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1661 >                        default_id = i->resolution_id;
1662 >                        break;
1663 >                }
1664 >        }
1665 >        if (i == end) { // not found, use first available mode
1666 >                default_depth = VideoModes[0].depth;
1667 >                default_id = VideoModes[0].resolution_id;
1668 >        }
1669  
1670   #if DEBUG
1671          D(bug("Available video modes:\n"));
1672 <        vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
1634 <        while (i != end) {
1672 >        for (i = VideoModes.begin(); i != end; ++i) {
1673                  int bits = 1 << i->depth;
1674                  if (bits == 16)
1675                          bits = 15;
1676                  else if (bits == 32)
1677                          bits = 24;
1678                  D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1641                ++i;
1679          }
1680   #endif
1681  
1682 <        // Find requested default mode and open display
1683 <        if (VideoModes.size() == 1)
1684 <                return video_open(VideoModes[0]);
1685 <        else {
1686 <                // Find mode with specified dimensions
1687 <                std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1651 <                for (i = VideoModes.begin(); i != end; ++i) {
1652 <                        if (i->x == default_width && i->y == default_height && i->depth == default_depth)
1653 <                                return video_open(*i);
1654 <                }
1655 <                return video_open(VideoModes[0]);
1656 <        }
1682 >        // Create X11_monitor_desc for this (the only) display
1683 >        X11_monitor_desc *monitor = new X11_monitor_desc(VideoModes, default_depth, default_id);
1684 >        VideoMonitors.push_back(monitor);
1685 >
1686 >        // Open display
1687 >        return monitor->video_open();
1688   }
1689  
1690  
# Line 1662 | Line 1693 | bool VideoInit(bool classic)
1693   */
1694  
1695   // Close display
1696 < static void video_close(void)
1696 > void X11_monitor_desc::video_close(void)
1697   {
1698          D(bug("video_close()\n"));
1699  
# Line 1707 | Line 1738 | static void video_close(void)
1738  
1739   void VideoExit(void)
1740   {
1741 <        // Close display
1742 <        video_close();
1741 >        // Close displays
1742 >        vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
1743 >        for (i = VideoMonitors.begin(); i != end; ++i)
1744 >                dynamic_cast<X11_monitor_desc *>(*i)->video_close();
1745  
1746   #ifdef ENABLE_XF86_VIDMODE
1747          // Free video mode list
# Line 1766 | Line 1799 | void VideoInterrupt(void)
1799   *  Set palette
1800   */
1801  
1802 < void video_set_palette(uint8 *pal, int num_in)
1802 > void X11_monitor_desc::set_palette(uint8 *pal, int num_in)
1803   {
1804 +        const video_mode &mode = get_current_mode();
1805 +
1806          LOCK_PALETTE;
1807  
1808          // Convert colors to XColor array
1809          int num_out = 256;
1810          bool stretch = false;
1811 <        if (IsDirectMode(VideoMonitor.mode)) {
1811 >        if (IsDirectMode(mode)) {
1812                  // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1813                  num_out = vis->map_entries;
1814                  stretch = true;
1815          }
1816 <        XColor *p = palette;
1816 >        XColor *p = x_palette;
1817          for (int i=0; i<num_out; i++) {
1818                  int c = (stretch ? (i * num_in) / num_out : i);
1819                  p->red = pal[c*3 + 0] * 0x0101;
# Line 1789 | Line 1824 | void video_set_palette(uint8 *pal, int n
1824  
1825   #ifdef ENABLE_VOSF
1826          // Recalculate pixel color expansion map
1827 <        if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) {
1827 >        if (!IsDirectMode(mode) && xdepth > 8) {
1828                  for (int i=0; i<256; i++) {
1829                          int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1830                          ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
# Line 1799 | Line 1834 | void video_set_palette(uint8 *pal, int n
1834                  LOCK_VOSF;
1835                  PFLAG_SET_ALL;
1836                  UNLOCK_VOSF;
1837 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1837 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1838          }
1839   #endif
1840  
1841          // Tell redraw thread to change palette
1842 <        palette_changed = true;
1842 >        x_palette_changed = true;
1843  
1844          UNLOCK_PALETTE;
1845   }
# Line 1814 | Line 1849 | void video_set_palette(uint8 *pal, int n
1849   *  Switch video mode
1850   */
1851  
1852 < void video_switch_to_mode(const video_mode &mode)
1852 > void X11_monitor_desc::switch_to_current_mode(void)
1853   {
1854          // Close and reopen display
1855          video_close();
1856 <        video_open(mode);
1856 >        video_open();
1857  
1858          if (drv == NULL) {
1859                  ErrorAlert(STR_OPEN_WINDOW_ERR);
# Line 2088 | Line 2123 | static void handle_events(void)
2123                          // Hidden parts exposed, force complete refresh of window
2124                          case Expose:
2125                                  if (display_type == DISPLAY_WINDOW) {
2126 +                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
2127   #ifdef ENABLE_VOSF
2128                                          if (use_vosf) {                 // VOSF refresh
2129                                                  LOCK_VOSF;
2130                                                  PFLAG_SET_ALL;
2131                                                  UNLOCK_VOSF;
2132 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2132 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2133                                          }
2134                                          else
2135   #endif
# Line 2104 | Line 2140 | static void handle_events(void)
2140                                                          updt_box[x1][y1] = true;
2141                                                  nr_boxes = 16 * 16;
2142                                          } else                                  // Static refresh
2143 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2143 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2144                                  }
2145                                  break;
2146  
# Line 2130 | Line 2166 | static void update_display_dynamic(int t
2166          int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
2167          int xil = 0;
2168          int rxm = 0, rxmo = 0;
2169 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2170 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2171 <        int rx = VideoMonitor.mode.bytes_per_row / 16;
2172 <        int ry = VideoMonitor.mode.y / 16;
2169 >        const video_mode &mode = drv->monitor.get_current_mode();
2170 >        int bytes_per_row = mode.bytes_per_row;
2171 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2172 >        int rx = mode.bytes_per_row / 16;
2173 >        int ry = mode.y / 16;
2174          int max_box;
2175  
2176          y2s = sm_uptd[ticker % 8];
# Line 2187 | Line 2224 | static void update_display_dynamic(int t
2224                                          i = (yi * bytes_per_row) + xi;
2225                                          for (y2=0; y2 < yil; y2++, i += bytes_per_row)
2226                                                  memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
2227 <                                        if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2227 >                                        if (mode.depth == VDEPTH_1BIT) {
2228                                                  if (drv->have_shm)
2229                                                          XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
2230                                                  else
# Line 2219 | Line 2256 | static void update_display_static(driver
2256   {
2257          // Incremental update code
2258          unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
2259 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2260 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2259 >        const video_mode &mode = drv->monitor.get_current_mode();
2260 >        int bytes_per_row = mode.bytes_per_row;
2261 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2262          uint8 *p, *p2;
2263  
2264          // Check for first line from top and first line from bottom that have changed
2265          y1 = 0;
2266 <        for (j=0; j<VideoMonitor.mode.y; j++) {
2266 >        for (j=0; j<mode.y; j++) {
2267                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2268                          y1 = j;
2269                          break;
2270                  }
2271          }
2272          y2 = y1 - 1;
2273 <        for (j=VideoMonitor.mode.y-1; j>=y1; j--) {
2273 >        for (j=mode.y-1; j>=y1; j--) {
2274                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2275                          y2 = j;
2276                          break;
# Line 2242 | Line 2280 | static void update_display_static(driver
2280  
2281          // Check for first column from left and first column from right that have changed
2282          if (high) {
2283 <                if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2284 <                        x1 = VideoMonitor.mode.x - 1;
2283 >                if (mode.depth == VDEPTH_1BIT) {
2284 >                        x1 = mode.x - 1;
2285                          for (j=y1; j<=y2; j++) {
2286                                  p = &the_buffer[j * bytes_per_row];
2287                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2261 | Line 2299 | static void update_display_static(driver
2299                                  p2 = &the_buffer_copy[j * bytes_per_row];
2300                                  p += bytes_per_row;
2301                                  p2 += bytes_per_row;
2302 <                                for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) {
2302 >                                for (i=(mode.x>>3); i>(x2>>3); i--) {
2303                                          p--; p2--;
2304                                          if (*p != *p2) {
2305                                                  x2 = (i << 3) + 7;
# Line 2280 | Line 2318 | static void update_display_static(driver
2318                          }
2319  
2320                  } else {
2321 <                        x1 = VideoMonitor.mode.x;
2321 >                        x1 = mode.x;
2322                          for (j=y1; j<=y2; j++) {
2323                                  p = &the_buffer[j * bytes_per_row];
2324                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2298 | Line 2336 | static void update_display_static(driver
2336                                  p2 = &the_buffer_copy[j * bytes_per_row];
2337                                  p += bytes_per_row;
2338                                  p2 += bytes_per_row;
2339 <                                for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2339 >                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2340                                          p--;
2341                                          p2--;
2342                                          if (*p != *p2) {
# Line 2365 | Line 2403 | static inline void handle_palette_change
2403   {
2404          LOCK_PALETTE;
2405  
2406 <        if (palette_changed) {
2407 <                palette_changed = false;
2406 >        if (x_palette_changed) {
2407 >                x_palette_changed = false;
2408                  drv->update_palette();
2409          }
2410  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines