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.72 by gbeauche, 2004-11-08T21:07:07Z

# Line 1 | Line 1
1   /*
2   *  video_x.cpp - Video/graphics emulation, X11 specific stuff
3   *
4 < *  Basilisk II (C) 1997-2001 Christian Bauer
4 > *  Basilisk II (C) 1997-2004 Christian Bauer
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 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  
201   /*
202 + *  monitor_desc subclass for X11 display
203 + */
204 +
205 + class X11_monitor_desc : public monitor_desc {
206 + public:
207 +        X11_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
208 +        ~X11_monitor_desc() {}
209 +
210 +        virtual void switch_to_current_mode(void);
211 +        virtual void set_palette(uint8 *pal, int num);
212 +
213 +        bool video_open(void);
214 +        void video_close(void);
215 + };
216 +
217 +
218 + /*
219   *  Utility functions
220   */
221  
222 + // Map video_mode depth ID to numerical depth value
223 + static inline int depth_of_video_mode(video_mode const & mode)
224 + {
225 +        int depth = -1;
226 +        switch (mode.depth) {
227 +        case VDEPTH_1BIT:
228 +                depth = 1;
229 +                break;
230 +        case VDEPTH_2BIT:
231 +                depth = 2;
232 +                break;
233 +        case VDEPTH_4BIT:
234 +                depth = 4;
235 +                break;
236 +        case VDEPTH_8BIT:
237 +                depth = 8;
238 +                break;
239 +        case VDEPTH_16BIT:
240 +                depth = 16;
241 +                break;
242 +        case VDEPTH_32BIT:
243 +                depth = 32;
244 +                break;
245 +        default:
246 +                abort();
247 +        }
248 +        return depth;
249 + }
250 +
251   // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
252   static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
253   {
# Line 210 | Line 260 | static bool find_visual_for_depth(video_
260   {
261          D(bug("have_visual_for_depth(%d)\n", 1 << depth));
262  
263 +        // 1-bit works always and uses default visual
264 +        if (depth == VDEPTH_1BIT) {
265 +                vis = DefaultVisual(x_display, screen);
266 +                visualInfo.visualid = XVisualIDFromVisual(vis);
267 +                int num = 0;
268 +                XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
269 +                visualInfo = vi[0];
270 +                XFree(vi);
271 +                xdepth = visualInfo.depth;
272 +                color_class = visualInfo.c_class;
273 +                D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
274 +                return true;
275 +        }
276 +
277          // Calculate minimum and maximum supported X depth
278          int min_depth = 1, max_depth = 32;
279          switch (depth) {
216                case VDEPTH_1BIT:       // 1-bit works always and uses default visual
217                        min_depth = max_depth = DefaultDepth(x_display, screen);
218                        break;
280   #ifdef ENABLE_VOSF
281                  case VDEPTH_2BIT:
282                  case VDEPTH_4BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
# Line 321 | Line 382 | static void add_window_modes(video_depth
382   }
383  
384   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
385 < static void set_mac_frame_buffer(video_depth depth, bool native_byte_order)
385 > static void set_mac_frame_buffer(X11_monitor_desc &monitor, video_depth depth, bool native_byte_order)
386   {
387   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
388          int layout = FLAYOUT_DIRECT;
# Line 333 | Line 394 | static void set_mac_frame_buffer(video_d
394                  MacFrameLayout = layout;
395          else
396                  MacFrameLayout = FLAYOUT_DIRECT;
397 <        VideoMonitor.mac_frame_base = MacFrameBaseMac;
397 >        monitor.set_mac_frame_base(MacFrameBaseMac);
398  
399          // Set variables used by UAE memory banking
400 +        const video_mode &mode = monitor.get_current_mode();
401          MacFrameBaseHost = the_buffer;
402 <        MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
402 >        MacFrameSize = mode.bytes_per_row * mode.y;
403          InitFrameBufferMapping();
404   #else
405 <        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
405 >        monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
406   #endif
407 <        D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base));
407 >        D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
408   }
409  
410   // Set window name and class
# Line 420 | Line 482 | static int error_handler(Display *d, XEr
482  
483   class driver_base {
484   public:
485 <        driver_base();
485 >        driver_base(X11_monitor_desc &m);
486          virtual ~driver_base();
487  
488          virtual void update_palette(void);
# Line 436 | Line 498 | public:
498          virtual void ungrab_mouse(void) {}
499  
500   public:
501 +        X11_monitor_desc &monitor; // Associated video monitor
502 +        const video_mode &mode;    // Video mode handled by the driver
503 +
504          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
505          Window w;               // The window we draw into
506  
# Line 453 | Line 518 | class driver_window : public driver_base
518          friend void update_display_static(driver_window *drv);
519  
520   public:
521 <        driver_window(const video_mode &mode);
521 >        driver_window(X11_monitor_desc &monitor);
522          ~driver_window();
523  
524          void toggle_mouse_grab(void);
# Line 478 | Line 543 | static driver_base *drv = NULL;        // Point
543   # include "video_vosf.h"
544   #endif
545  
546 < driver_base::driver_base()
547 < : init_ok(false), w(0)
546 > driver_base::driver_base(X11_monitor_desc &m)
547 > : monitor(m), mode(m.get_current_mode()), init_ok(false), w(0)
548   {
549          the_buffer = NULL;
550          the_buffer_copy = NULL;
# Line 513 | Line 578 | driver_base::~driver_base()
578          }
579   #ifdef ENABLE_VOSF
580          else {
581 +                // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
582 +                if (the_buffer != VM_MAP_FAILED) {
583 +                        D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
584 +                        vm_release(the_buffer, the_buffer_size);
585 +                        the_buffer = NULL;
586 +                }
587                  if (the_host_buffer) {
588 +                        D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
589                          free(the_host_buffer);
590                          the_host_buffer = NULL;
591                  }
520                if (the_buffer) {
521                        free(the_buffer);
522                        the_buffer = NULL;
523                }
592                  if (the_buffer_copy) {
593 +                        D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
594                          free(the_buffer_copy);
595                          the_buffer_copy = NULL;
596                  }
# Line 534 | Line 603 | void driver_base::update_palette(void)
603   {
604          if (color_class == PseudoColor || color_class == DirectColor) {
605                  int num = vis->map_entries;
606 <                if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor)
606 >                if (!IsDirectMode(monitor.get_current_mode()) && color_class == DirectColor)
607                          return; // Indexed mode on true color screen, don't set CLUT
608 <                XStoreColors(x_display, cmap[0], palette, num);
609 <                XStoreColors(x_display, cmap[1], palette, num);
608 >                XStoreColors(x_display, cmap[0], x_palette, num);
609 >                XStoreColors(x_display, cmap[1], x_palette, num);
610          }
611          XSync(x_display, false);
612   }
# Line 560 | Line 629 | void driver_base::restore_mouse_accel(vo
629   */
630  
631   // Open display
632 < driver_window::driver_window(const video_mode &mode)
633 < : gc(0), img(NULL), have_shm(false), mouse_grabbed(false), mac_cursor(0)
632 > driver_window::driver_window(X11_monitor_desc &m)
633 > : driver_base(m), gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
634   {
635          int width = mode.x, height = mode.y;
636          int aligned_width = (width + 15) & ~15;
# Line 570 | Line 639 | driver_window::driver_window(const video
639          // Set absolute mouse mode
640          ADBSetRelMouseMode(mouse_grabbed);
641  
642 <        // Create window (setting backround_pixel, border_pixel and colormap is
642 >        // Create window (setting background_pixel, border_pixel and colormap is
643          // mandatory when using a non-default visual; in 1-bit mode we use the
644          // default visual, so we can also use the default colormap)
645          XSetWindowAttributes wattr;
# Line 580 | Line 649 | driver_window::driver_window(const video
649          wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
650          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
651                  InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &wattr);
652 +        D(bug(" window created\n"));
653  
654          // Set window name/class
655          set_window_name(w, STR_WINDOW_TITLE);
# Line 603 | Line 673 | driver_window::driver_window(const video
673                          XFree(hints);
674                  }
675          }
676 +        D(bug(" window attributes set\n"));
677          
678          // Show window
679          XMapWindow(x_display, w);
680          wait_mapped(w);
681 +        D(bug(" window mapped\n"));
682  
683          // 1-bit mode is big-endian; if the X server is little-endian, we can't
684          // use SHM because that doesn't allow changing the image byte order
# Line 617 | Line 689 | driver_window::driver_window(const video
689  
690                  // Create SHM image ("height + 2" for safety)
691                  img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
692 +                D(bug(" shm image created\n"));
693                  shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
694                  the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
695                  shminfo.shmaddr = img->data = (char *)the_buffer_copy;
# Line 637 | Line 710 | driver_window::driver_window(const video
710                          have_shm = true;
711                          shmctl(shminfo.shmid, IPC_RMID, 0);
712                  }
713 +                D(bug(" shm image attached\n"));
714          }
715          
716          // Create normal X image if SHM doesn't work ("height + 2" for safety)
# Line 644 | Line 718 | driver_window::driver_window(const video
718                  int bytes_per_row = (mode.depth == VDEPTH_1BIT ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth)));
719                  the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
720                  img = XCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
721 +                D(bug(" X image created\n"));
722          }
723  
724          if (need_msb_image) {
# Line 656 | Line 731 | driver_window::driver_window(const video
731          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
732          the_host_buffer = the_buffer_copy;
733          the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
734 <        the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
735 <        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
734 >        the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size);
735 >        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
736          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
737   #else
738          // Allocate memory for frame buffer
# Line 684 | Line 759 | driver_window::driver_window(const video
759          native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
760   #endif
761   #ifdef ENABLE_VOSF
762 <        Screen_blitter_init(&visualInfo, native_byte_order, mode.depth);
762 >        Screen_blitter_init(visualFormat, native_byte_order, depth_of_video_mode(mode));
763   #endif
764  
765 <        // Set VideoMonitor
766 <        VideoMonitor.mode = mode;
692 <        set_mac_frame_buffer(mode.depth, native_byte_order);
765 >        // Set frame buffer base
766 >        set_mac_frame_buffer(monitor, mode.depth, native_byte_order);
767  
768          // Everything went well
769          init_ok = true;
# Line 706 | Line 780 | driver_window::~driver_window()
780                  the_buffer_copy = NULL; // don't free() in driver_base dtor
781   #endif
782          }
783 < #ifdef ENABLE_VOSF
784 <        if (use_vosf) {
785 <                // 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)
783 >        if (img) {
784 >                if (!have_shm)
785 >                        img->data = NULL;
786                  XDestroyImage(img);
787 +        }
788          if (have_shm) {
789                  shmdt(shminfo.shmaddr);
790                  shmctl(shminfo.shmid, IPC_RMID, 0);
# Line 779 | Line 843 | void driver_window::mouse_moved(int x, i
843          // Warped mouse motion (this code is taken from SDL)
844  
845          // Post first mouse event
846 <        int width = VideoMonitor.mode.x, height = VideoMonitor.mode.y;
846 >        int width = monitor.get_current_mode().x, height = monitor.get_current_mode().y;
847          int delta_x = x - mouse_last_x, delta_y = y - mouse_last_y;
848          mouse_last_x = x; mouse_last_y = y;
849          ADBMouseMoved(delta_x, delta_y);
# Line 816 | Line 880 | void driver_window::mouse_moved(int x, i
880  
881   class driver_dga : public driver_base {
882   public:
883 <        driver_dga();
883 >        driver_dga(X11_monitor_desc &monitor);
884          ~driver_dga();
885  
886          void suspend(void);
# Line 827 | Line 891 | private:
891          void *fb_save;                  // Saved frame buffer for suspend/resume
892   };
893  
894 < driver_dga::driver_dga()
895 < : suspend_win(0), fb_save(NULL)
894 > driver_dga::driver_dga(X11_monitor_desc &m)
895 > : driver_base(m), suspend_win(0), fb_save(NULL)
896   {
897   }
898  
# Line 849 | Line 913 | void driver_dga::suspend(void)
913          LOCK_FRAME_BUFFER;
914  
915          // Save frame buffer
916 <        fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
916 >        fb_save = malloc(mode.y * mode.bytes_per_row);
917          if (fb_save)
918 <                memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
918 >                memcpy(fb_save, the_buffer, mode.y * mode.bytes_per_row);
919  
920          // Close full screen display
921   #ifdef ENABLE_XF86_DGA
# Line 904 | Line 968 | void driver_dga::resume(void)
968                  LOCK_VOSF;
969                  PFLAG_SET_ALL;
970                  UNLOCK_VOSF;
971 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
971 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
972          }
973   #endif
974          
# Line 914 | Line 978 | void driver_dga::resume(void)
978                  // Don't copy fb_save to the temporary frame buffer in VOSF mode
979                  if (!use_vosf)
980   #endif
981 <                memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
981 >                memcpy(the_buffer, fb_save, mode.y * mode.bytes_per_row);
982                  free(fb_save);
983                  fb_save = NULL;
984          }
# Line 936 | Line 1000 | const char FBDEVICE_FILE_NAME[] = "/dev/
1000  
1001   class driver_fbdev : public driver_dga {
1002   public:
1003 <        driver_fbdev(const video_mode &mode);
1003 >        driver_fbdev(X11_monitor_desc &monitor);
1004          ~driver_fbdev();
1005   };
1006  
1007   // Open display
1008 < driver_fbdev::driver_fbdev(const video_mode &mode)
1008 > driver_fbdev::driver_fbdev(X11_monitor_desc &m) : driver_dga(m)
1009   {
1010          int width = mode.x, height = mode.y;
1011  
# Line 989 | Line 1053 | driver_fbdev::driver_fbdev(const video_m
1053                  if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
1054                          continue;
1055                  
1056 <                if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
1056 >                if ((sscanf(line, "%19s %d %x", fb_name, &fb_depth, &fb_offset) == 3)
1057                   && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) {
1058                          device_found = true;
1059                          break;
# Line 1056 | Line 1120 | driver_fbdev::driver_fbdev(const video_m
1120   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1121          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1122          // i.e. the framebuffer update function is not Blit_Copy_Raw
1123 <        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1123 >        use_vosf = Screen_blitter_init(visualFormat, true, mode.depth);
1124          
1125          if (use_vosf) {
1126            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1127            the_host_buffer = the_buffer;
1128            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1129 <          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
1130 <          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1129 >          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1130 >          the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size);
1131          }
1132   #else
1133          use_vosf = false;
1134   #endif
1135   #endif
1136          
1137 <        // Set VideoMonitor
1138 <        VideoModes[0].bytes_per_row = bytes_per_row;
1139 <        VideoModes[0].depth = DepthModeForPixelDepth(fb_depth);
1140 <        VideoMonitor.mode = mode;
1077 <        set_mac_frame_buffer(mode.depth, true);
1137 >        // Set frame buffer base
1138 >        const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1139 >        const_cast<video_mode *>(&mode)->depth = DepthModeForPixelDepth(fb_depth);
1140 >        set_mac_frame_buffer(monitor, mode.depth, true);
1141  
1142          // Everything went well
1143          init_ok = true;
# Line 1097 | Line 1160 | driver_fbdev::~driver_fbdev()
1160                          munmap(the_host_buffer, the_buffer_size);
1161                          the_host_buffer = NULL;
1162                  }
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                }
1163          }
1164   #endif
1165   }
# Line 1118 | Line 1173 | driver_fbdev::~driver_fbdev()
1173  
1174   class driver_xf86dga : public driver_dga {
1175   public:
1176 <        driver_xf86dga(const video_mode &mode);
1176 >        driver_xf86dga(X11_monitor_desc &monitor);
1177          ~driver_xf86dga();
1178  
1179          void update_palette(void);
# Line 1129 | Line 1184 | private:
1184   };
1185  
1186   // Open display
1187 < driver_xf86dga::driver_xf86dga(const video_mode &mode)
1188 < : current_dga_cmap(0)
1187 > driver_xf86dga::driver_xf86dga(X11_monitor_desc &m)
1188 > : driver_dga(m), current_dga_cmap(0)
1189   {
1190          int width = mode.x, height = mode.y;
1191  
# Line 1157 | Line 1212 | driver_xf86dga::driver_xf86dga(const vid
1212          XSetWindowAttributes wattr;
1213          wattr.event_mask = eventmask = dga_eventmask;
1214          wattr.override_redirect = True;
1215 +        wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
1216  
1217          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
1218 <                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
1218 >                InputOutput, vis, CWEventMask | CWOverrideRedirect |
1219 >                (color_class == DirectColor ? CWColormap : 0), &wattr);
1220  
1221          // Set window name/class
1222          set_window_name(w, STR_WINDOW_TITLE);
# Line 1193 | Line 1250 | driver_xf86dga::driver_xf86dga(const vid
1250  
1251          // Init blitting routines
1252          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
1253 < #if VIDEO_VOSF
1253 > #if ENABLE_VOSF
1254 >        bool native_byte_order;
1255 > #ifdef WORDS_BIGENDIAN
1256 >        native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
1257 > #else
1258 >        native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
1259 > #endif
1260   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1261          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1262          // i.e. the framebuffer update function is not Blit_Copy_Raw
1263 <        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1263 >        use_vosf = Screen_blitter_init(visualFormat, native_byte_order, depth_of_video_mode(mode));
1264          
1265          if (use_vosf) {
1266            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1267            the_host_buffer = the_buffer;
1268            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1269 <          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
1270 <          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1269 >          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1270 >          the_buffer = (uint8 *)vm_acquire_mac(the_buffer_size);
1271          }
1272   #else
1273          use_vosf = false;
1274   #endif
1275   #endif
1276          
1277 <        // Set VideoMonitor
1277 >        // Set frame buffer base
1278          const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1279 <        VideoMonitor.mode = mode;
1217 <        set_mac_frame_buffer(mode.depth, true);
1279 >        set_mac_frame_buffer(monitor, mode.depth, true);
1280  
1281          // Everything went well
1282          init_ok = true;
# Line 1232 | Line 1294 | driver_xf86dga::~driver_xf86dga()
1294          else {
1295                  // don't free() the screen buffer in driver_base dtor
1296                  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                }
1297          }
1298   #endif
1299   #ifdef ENABLE_XF86_VIDMODE
# Line 1254 | Line 1307 | void driver_xf86dga::update_palette(void
1307   {
1308          driver_dga::update_palette();
1309          current_dga_cmap ^= 1;
1310 <        if (!IsDirectMode(VideoMonitor.mode) && cmap[current_dga_cmap])
1310 >        if (!IsDirectMode(monitor.get_current_mode()) && cmap[current_dga_cmap])
1311                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1312   }
1313  
# Line 1262 | Line 1315 | void driver_xf86dga::update_palette(void
1315   void driver_xf86dga::resume(void)
1316   {
1317          driver_dga::resume();
1318 <        if (!IsDirectMode(VideoMonitor.mode))
1318 >        if (!IsDirectMode(monitor.get_current_mode()))
1319                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1320   }
1321   #endif
# Line 1337 | Line 1390 | static void keycode_init(void)
1390          }
1391   }
1392  
1393 < // Open display for specified mode
1394 < static bool video_open(const video_mode &mode)
1393 > // Open display for current mode
1394 > bool X11_monitor_desc::video_open(void)
1395   {
1396 +        D(bug("video_open()\n"));
1397 +        const video_mode &mode = get_current_mode();
1398 +
1399          // Find best available X visual
1400          if (!find_visual_for_depth(mode.depth)) {
1401                  ErrorAlert(STR_NO_XVISUAL_ERR);
1402                  return false;
1403          }
1404  
1405 +        // Build up visualFormat structure
1406 +        visualFormat.depth = visualInfo.depth;
1407 +        visualFormat.Rmask = visualInfo.red_mask;
1408 +        visualFormat.Gmask = visualInfo.green_mask;
1409 +        visualFormat.Bmask = visualInfo.blue_mask;
1410 +
1411          // Create color maps
1412          if (color_class == PseudoColor || color_class == DirectColor) {
1413                  cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
# Line 1379 | Line 1441 | static bool video_open(const video_mode
1441                  int num = vis->map_entries;
1442                  for (int i=0; i<num; i++) {
1443                          int c = (i * 256) / num;
1444 <                        palette[i].pixel = map_rgb(c, c, c);
1445 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1444 >                        x_palette[i].pixel = map_rgb(c, c, c);
1445 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1446                  }
1447          } else if (color_class == PseudoColor) {
1448                  for (int i=0; i<256; i++) {
1449 <                        palette[i].pixel = i;
1450 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1449 >                        x_palette[i].pixel = i;
1450 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1451                  }
1452          }
1453  
# Line 1393 | Line 1455 | static bool video_open(const video_mode
1455          int num = (color_class == DirectColor ? vis->map_entries : 256);
1456          for (int i=0; i<num; i++) {
1457                  int c = (i * 256) / num;
1458 <                palette[i].red = c * 0x0101;
1459 <                palette[i].green = c * 0x0101;
1460 <                palette[i].blue = c * 0x0101;
1458 >                x_palette[i].red = c * 0x0101;
1459 >                x_palette[i].green = c * 0x0101;
1460 >                x_palette[i].blue = c * 0x0101;
1461          }
1462          if (color_class == PseudoColor || color_class == DirectColor) {
1463 <                XStoreColors(x_display, cmap[0], palette, num);
1464 <                XStoreColors(x_display, cmap[1], palette, num);
1463 >                XStoreColors(x_display, cmap[0], x_palette, num);
1464 >                XStoreColors(x_display, cmap[1], x_palette, num);
1465          }
1466  
1467   #ifdef ENABLE_VOSF
# Line 1412 | Line 1474 | static bool video_open(const video_mode
1474          // Create display driver object of requested type
1475          switch (display_type) {
1476                  case DISPLAY_WINDOW:
1477 <                        drv = new driver_window(mode);
1477 >                        drv = new driver_window(*this);
1478                          break;
1479   #ifdef ENABLE_FBDEV_DGA
1480                  case DISPLAY_DGA:
1481 <                        drv = new driver_fbdev(mode);
1481 >                        drv = new driver_fbdev(*this);
1482                          break;
1483   #endif
1484   #ifdef ENABLE_XF86_DGA
1485                  case DISPLAY_DGA:
1486 <                        drv = new driver_xf86dga(mode);
1486 >                        drv = new driver_xf86dga(*this);
1487                          break;
1488   #endif
1489          }
# Line 1436 | Line 1498 | static bool video_open(const video_mode
1498   #ifdef ENABLE_VOSF
1499          if (use_vosf) {
1500                  // Initialize the VOSF system
1501 <                if (!video_vosf_init()) {
1501 >                if (!video_vosf_init(*this)) {
1502                          ErrorAlert(STR_VOSF_INIT_ERR);
1503                  return false;
1504                  }
# Line 1453 | Line 1515 | static bool video_open(const video_mode
1515          // Start redraw/input thread
1516   #ifdef HAVE_PTHREADS
1517          redraw_thread_cancel = false;
1518 <        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1518 >        Set_pthread_attr(&redraw_thread_attr, 0);
1519 >        redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1520          if (!redraw_thread_active) {
1521                  printf("FATAL: cannot create redraw thread\n");
1522                  return false;
# Line 1497 | Line 1560 | bool VideoInit(bool classic)
1560                  ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1561                  return false;
1562          }
1563 <        sort(avail_depths, avail_depths + num_depths);
1563 >        std::sort(avail_depths, avail_depths + num_depths);
1564          
1565   #ifdef ENABLE_FBDEV_DGA
1566          // Frame buffer name
# Line 1600 | Line 1663 | bool VideoInit(bool classic)
1663                  ErrorAlert(STR_NO_XVISUAL_ERR);
1664                  return false;
1665          }
1666 <        video_init_depth_list();
1666 >
1667 >        // Find requested default mode with specified dimensions
1668 >        uint32 default_id;
1669 >        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1670 >        for (i = VideoModes.begin(); i != end; ++i) {
1671 >                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1672 >                        default_id = i->resolution_id;
1673 >                        break;
1674 >                }
1675 >        }
1676 >        if (i == end) { // not found, use first available mode
1677 >                default_depth = VideoModes[0].depth;
1678 >                default_id = VideoModes[0].resolution_id;
1679 >        }
1680  
1681   #if DEBUG
1682          D(bug("Available video modes:\n"));
1683 <        vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
1608 <        while (i != end) {
1683 >        for (i = VideoModes.begin(); i != end; ++i) {
1684                  int bits = 1 << i->depth;
1685                  if (bits == 16)
1686                          bits = 15;
1687                  else if (bits == 32)
1688                          bits = 24;
1689                  D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1615                ++i;
1690          }
1691   #endif
1692  
1693 <        // Find requested default mode and open display
1694 <        if (VideoModes.size() == 1)
1695 <                return video_open(VideoModes[0]);
1696 <        else {
1697 <                // Find mode with specified dimensions
1698 <                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 <        }
1693 >        // Create X11_monitor_desc for this (the only) display
1694 >        X11_monitor_desc *monitor = new X11_monitor_desc(VideoModes, default_depth, default_id);
1695 >        VideoMonitors.push_back(monitor);
1696 >
1697 >        // Open display
1698 >        return monitor->video_open();
1699   }
1700  
1701  
# Line 1636 | Line 1704 | bool VideoInit(bool classic)
1704   */
1705  
1706   // Close display
1707 < static void video_close(void)
1707 > void X11_monitor_desc::video_close(void)
1708   {
1709 +        D(bug("video_close()\n"));
1710 +
1711          // Stop redraw thread
1712   #ifdef HAVE_PTHREADS
1713          if (redraw_thread_active) {
# Line 1653 | Line 1723 | static void video_close(void)
1723          // Unlock frame buffer
1724          UNLOCK_FRAME_BUFFER;
1725          XSync(x_display, false);
1726 +        D(bug(" frame buffer unlocked\n"));
1727  
1728   #ifdef ENABLE_VOSF
1729          if (use_vosf) {
# Line 1678 | Line 1749 | static void video_close(void)
1749  
1750   void VideoExit(void)
1751   {
1752 <        // Close display
1753 <        video_close();
1752 >        // Close displays
1753 >        vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
1754 >        for (i = VideoMonitors.begin(); i != end; ++i)
1755 >                dynamic_cast<X11_monitor_desc *>(*i)->video_close();
1756  
1757   #ifdef ENABLE_XF86_VIDMODE
1758          // Free video mode list
# Line 1737 | Line 1810 | void VideoInterrupt(void)
1810   *  Set palette
1811   */
1812  
1813 < void video_set_palette(uint8 *pal, int num_in)
1813 > void X11_monitor_desc::set_palette(uint8 *pal, int num_in)
1814   {
1815 +        const video_mode &mode = get_current_mode();
1816 +
1817          LOCK_PALETTE;
1818  
1819          // Convert colors to XColor array
1820          int num_out = 256;
1821          bool stretch = false;
1822 <        if (IsDirectMode(VideoMonitor.mode)) {
1822 >        if (IsDirectMode(mode)) {
1823                  // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1824                  num_out = vis->map_entries;
1825                  stretch = true;
1826          }
1827 <        XColor *p = palette;
1827 >        XColor *p = x_palette;
1828          for (int i=0; i<num_out; i++) {
1829                  int c = (stretch ? (i * num_in) / num_out : i);
1830                  p->red = pal[c*3 + 0] * 0x0101;
# Line 1760 | Line 1835 | void video_set_palette(uint8 *pal, int n
1835  
1836   #ifdef ENABLE_VOSF
1837          // Recalculate pixel color expansion map
1838 <        if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) {
1838 >        if (!IsDirectMode(mode) && xdepth > 8) {
1839                  for (int i=0; i<256; i++) {
1840                          int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1841                          ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
# Line 1770 | Line 1845 | void video_set_palette(uint8 *pal, int n
1845                  LOCK_VOSF;
1846                  PFLAG_SET_ALL;
1847                  UNLOCK_VOSF;
1848 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1848 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1849          }
1850   #endif
1851  
1852          // Tell redraw thread to change palette
1853 <        palette_changed = true;
1853 >        x_palette_changed = true;
1854  
1855          UNLOCK_PALETTE;
1856   }
# Line 1785 | Line 1860 | void video_set_palette(uint8 *pal, int n
1860   *  Switch video mode
1861   */
1862  
1863 < void video_switch_to_mode(const video_mode &mode)
1863 > void X11_monitor_desc::switch_to_current_mode(void)
1864   {
1865          // Close and reopen display
1866          video_close();
1867 <        video_open(mode);
1867 >        video_open();
1868  
1869          if (drv == NULL) {
1870                  ErrorAlert(STR_OPEN_WINDOW_ERR);
# Line 2059 | Line 2134 | static void handle_events(void)
2134                          // Hidden parts exposed, force complete refresh of window
2135                          case Expose:
2136                                  if (display_type == DISPLAY_WINDOW) {
2137 +                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
2138   #ifdef ENABLE_VOSF
2139                                          if (use_vosf) {                 // VOSF refresh
2140                                                  LOCK_VOSF;
2141                                                  PFLAG_SET_ALL;
2142                                                  UNLOCK_VOSF;
2143 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2143 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2144                                          }
2145                                          else
2146   #endif
# Line 2075 | Line 2151 | static void handle_events(void)
2151                                                          updt_box[x1][y1] = true;
2152                                                  nr_boxes = 16 * 16;
2153                                          } else                                  // Static refresh
2154 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2154 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2155                                  }
2156                                  break;
2157  
# Line 2101 | Line 2177 | static void update_display_dynamic(int t
2177          int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
2178          int xil = 0;
2179          int rxm = 0, rxmo = 0;
2180 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2181 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2182 <        int rx = VideoMonitor.mode.bytes_per_row / 16;
2183 <        int ry = VideoMonitor.mode.y / 16;
2180 >        const video_mode &mode = drv->monitor.get_current_mode();
2181 >        int bytes_per_row = mode.bytes_per_row;
2182 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2183 >        int rx = mode.bytes_per_row / 16;
2184 >        int ry = mode.y / 16;
2185          int max_box;
2186  
2187          y2s = sm_uptd[ticker % 8];
# Line 2158 | Line 2235 | static void update_display_dynamic(int t
2235                                          i = (yi * bytes_per_row) + xi;
2236                                          for (y2=0; y2 < yil; y2++, i += bytes_per_row)
2237                                                  memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
2238 <                                        if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2238 >                                        if (mode.depth == VDEPTH_1BIT) {
2239                                                  if (drv->have_shm)
2240                                                          XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
2241                                                  else
# Line 2189 | Line 2266 | static void update_display_dynamic(int t
2266   static void update_display_static(driver_window *drv)
2267   {
2268          // Incremental update code
2269 <        int wide = 0, high = 0, x1, x2, y1, y2, i, j;
2270 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2271 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2269 >        unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
2270 >        const video_mode &mode = drv->monitor.get_current_mode();
2271 >        int bytes_per_row = mode.bytes_per_row;
2272 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2273          uint8 *p, *p2;
2274  
2275          // Check for first line from top and first line from bottom that have changed
2276          y1 = 0;
2277 <        for (j=0; j<VideoMonitor.mode.y; j++) {
2277 >        for (j=0; j<mode.y; j++) {
2278                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2279                          y1 = j;
2280                          break;
2281                  }
2282          }
2283          y2 = y1 - 1;
2284 <        for (j=VideoMonitor.mode.y-1; j>=y1; j--) {
2284 >        for (j=mode.y-1; j>=y1; j--) {
2285                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2286                          y2 = j;
2287                          break;
# Line 2213 | Line 2291 | static void update_display_static(driver
2291  
2292          // Check for first column from left and first column from right that have changed
2293          if (high) {
2294 <                if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2295 <                        x1 = VideoMonitor.mode.x - 1;
2294 >                if (mode.depth == VDEPTH_1BIT) {
2295 >                        x1 = mode.x - 1;
2296                          for (j=y1; j<=y2; j++) {
2297                                  p = &the_buffer[j * bytes_per_row];
2298                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2232 | Line 2310 | static void update_display_static(driver
2310                                  p2 = &the_buffer_copy[j * bytes_per_row];
2311                                  p += bytes_per_row;
2312                                  p2 += bytes_per_row;
2313 <                                for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) {
2313 >                                for (i=(mode.x>>3); i>(x2>>3); i--) {
2314                                          p--; p2--;
2315                                          if (*p != *p2) {
2316                                                  x2 = (i << 3) + 7;
# Line 2251 | Line 2329 | static void update_display_static(driver
2329                          }
2330  
2331                  } else {
2332 <                        x1 = VideoMonitor.mode.x;
2332 >                        x1 = mode.x;
2333                          for (j=y1; j<=y2; j++) {
2334                                  p = &the_buffer[j * bytes_per_row];
2335                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2269 | Line 2347 | static void update_display_static(driver
2347                                  p2 = &the_buffer_copy[j * bytes_per_row];
2348                                  p += bytes_per_row;
2349                                  p2 += bytes_per_row;
2350 <                                for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2350 >                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2351                                          p--;
2352                                          p2--;
2353                                          if (*p != *p2) {
# Line 2336 | Line 2414 | static inline void handle_palette_change
2414   {
2415          LOCK_PALETTE;
2416  
2417 <        if (palette_changed) {
2418 <                palette_changed = false;
2417 >        if (x_palette_changed) {
2418 >                x_palette_changed = false;
2419                  drv->update_palette();
2420          }
2421  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines