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.60 by cebix, 2001-07-11T19:26:14Z vs.
Revision 1.68 by gbeauche, 2002-09-28T12:42:39Z

# 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  
# Line 209 | Line 226 | static bool find_visual_for_depth(video_
226   {
227          D(bug("have_visual_for_depth(%d)\n", 1 << depth));
228  
229 +        // 1-bit works always and uses default visual
230 +        if (depth == VDEPTH_1BIT) {
231 +                vis = DefaultVisual(x_display, screen);
232 +                visualInfo.visualid = XVisualIDFromVisual(vis);
233 +                int num = 0;
234 +                XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
235 +                visualInfo = vi[0];
236 +                XFree(vi);
237 +                xdepth = visualInfo.depth;
238 +                color_class = visualInfo.c_class;
239 +                D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
240 +                return true;
241 +        }
242 +
243          // Calculate minimum and maximum supported X depth
244          int min_depth = 1, max_depth = 32;
245          switch (depth) {
215                case VDEPTH_1BIT:       // 1-bit works always and uses default visual
216                        min_depth = max_depth = DefaultDepth(x_display, screen);
217                        break;
246   #ifdef ENABLE_VOSF
247                  case VDEPTH_2BIT:
248                  case VDEPTH_4BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
# Line 320 | Line 348 | static void add_window_modes(video_depth
348   }
349  
350   // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
351 < static void set_mac_frame_buffer(video_depth depth, bool native_byte_order)
351 > static void set_mac_frame_buffer(X11_monitor_desc &monitor, video_depth depth, bool native_byte_order)
352   {
353   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
354          int layout = FLAYOUT_DIRECT;
# Line 332 | Line 360 | static void set_mac_frame_buffer(video_d
360                  MacFrameLayout = layout;
361          else
362                  MacFrameLayout = FLAYOUT_DIRECT;
363 <        VideoMonitor.mac_frame_base = MacFrameBaseMac;
363 >        monitor.set_mac_frame_base(MacFrameBaseMac);
364  
365          // Set variables used by UAE memory banking
366 +        const video_mode &mode = monitor.get_current_mode();
367          MacFrameBaseHost = the_buffer;
368 <        MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
368 >        MacFrameSize = mode.bytes_per_row * mode.y;
369          InitFrameBufferMapping();
370   #else
371 <        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
371 >        monitor.set_mac_frame_base(Host2MacAddr(the_buffer));
372   #endif
373 <        D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base));
373 >        D(bug("monitor.mac_frame_base = %08x\n", monitor.get_mac_frame_base()));
374   }
375  
376   // Set window name and class
# Line 419 | Line 448 | static int error_handler(Display *d, XEr
448  
449   class driver_base {
450   public:
451 <        driver_base();
451 >        driver_base(X11_monitor_desc &m);
452          virtual ~driver_base();
453  
454          virtual void update_palette(void);
# Line 435 | Line 464 | public:
464          virtual void ungrab_mouse(void) {}
465  
466   public:
467 +        X11_monitor_desc &monitor; // Associated video monitor
468 +        const video_mode &mode;    // Video mode handled by the driver
469 +
470          bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
471          Window w;               // The window we draw into
472  
# Line 452 | Line 484 | class driver_window : public driver_base
484          friend void update_display_static(driver_window *drv);
485  
486   public:
487 <        driver_window(const video_mode &mode);
487 >        driver_window(X11_monitor_desc &monitor);
488          ~driver_window();
489  
490          void toggle_mouse_grab(void);
# Line 477 | Line 509 | static driver_base *drv = NULL;        // Point
509   # include "video_vosf.h"
510   #endif
511  
512 < driver_base::driver_base()
513 < : init_ok(false), w(0)
512 > driver_base::driver_base(X11_monitor_desc &m)
513 > : monitor(m), mode(m.get_current_mode()), init_ok(false), w(0)
514   {
515          the_buffer = NULL;
516          the_buffer_copy = NULL;
# Line 512 | Line 544 | driver_base::~driver_base()
544          }
545   #ifdef ENABLE_VOSF
546          else {
547 +                // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
548 +                if (the_buffer != VM_MAP_FAILED) {
549 +                        D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
550 +                        vm_release(the_buffer, the_buffer_size);
551 +                        the_buffer = NULL;
552 +                }
553                  if (the_host_buffer) {
554 +                        D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
555                          free(the_host_buffer);
556                          the_host_buffer = NULL;
557                  }
519                if (the_buffer) {
520                        free(the_buffer);
521                        the_buffer = NULL;
522                }
558                  if (the_buffer_copy) {
559 +                        D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
560                          free(the_buffer_copy);
561                          the_buffer_copy = NULL;
562                  }
# Line 533 | Line 569 | void driver_base::update_palette(void)
569   {
570          if (color_class == PseudoColor || color_class == DirectColor) {
571                  int num = vis->map_entries;
572 <                if (!IsDirectMode(VideoMonitor.mode) && color_class == DirectColor)
572 >                if (!IsDirectMode(monitor.get_current_mode()) && color_class == DirectColor)
573                          return; // Indexed mode on true color screen, don't set CLUT
574 <                XStoreColors(x_display, cmap[0], palette, num);
575 <                XStoreColors(x_display, cmap[1], palette, num);
574 >                XStoreColors(x_display, cmap[0], x_palette, num);
575 >                XStoreColors(x_display, cmap[1], x_palette, num);
576          }
577          XSync(x_display, false);
578   }
# Line 559 | Line 595 | void driver_base::restore_mouse_accel(vo
595   */
596  
597   // Open display
598 < driver_window::driver_window(const video_mode &mode)
599 < : gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
598 > driver_window::driver_window(X11_monitor_desc &m)
599 > : driver_base(m), gc(0), img(NULL), have_shm(false), mac_cursor(0), mouse_grabbed(false)
600   {
601          int width = mode.x, height = mode.y;
602          int aligned_width = (width + 15) & ~15;
# Line 569 | Line 605 | driver_window::driver_window(const video
605          // Set absolute mouse mode
606          ADBSetRelMouseMode(mouse_grabbed);
607  
608 <        // Create window (setting backround_pixel, border_pixel and colormap is
608 >        // Create window (setting background_pixel, border_pixel and colormap is
609          // mandatory when using a non-default visual; in 1-bit mode we use the
610          // default visual, so we can also use the default colormap)
611          XSetWindowAttributes wattr;
# Line 579 | Line 615 | driver_window::driver_window(const video
615          wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
616          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
617                  InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWColormap, &wattr);
618 +        D(bug(" window created\n"));
619  
620          // Set window name/class
621          set_window_name(w, STR_WINDOW_TITLE);
# Line 602 | Line 639 | driver_window::driver_window(const video
639                          XFree(hints);
640                  }
641          }
642 +        D(bug(" window attributes set\n"));
643          
644          // Show window
645          XMapWindow(x_display, w);
646          wait_mapped(w);
647 +        D(bug(" window mapped\n"));
648  
649          // 1-bit mode is big-endian; if the X server is little-endian, we can't
650          // use SHM because that doesn't allow changing the image byte order
# Line 616 | Line 655 | driver_window::driver_window(const video
655  
656                  // Create SHM image ("height + 2" for safety)
657                  img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
658 +                D(bug(" shm image created\n"));
659                  shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
660                  the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
661                  shminfo.shmaddr = img->data = (char *)the_buffer_copy;
# Line 636 | Line 676 | driver_window::driver_window(const video
676                          have_shm = true;
677                          shmctl(shminfo.shmid, IPC_RMID, 0);
678                  }
679 +                D(bug(" shm image attached\n"));
680          }
681          
682          // Create normal X image if SHM doesn't work ("height + 2" for safety)
# Line 643 | Line 684 | driver_window::driver_window(const video
684                  int bytes_per_row = (mode.depth == VDEPTH_1BIT ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth)));
685                  the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
686                  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);
687 +                D(bug(" X image created\n"));
688          }
689  
690          if (need_msb_image) {
# Line 655 | Line 697 | driver_window::driver_window(const video
697          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
698          the_host_buffer = the_buffer_copy;
699          the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
658        the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
700          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
701 +        the_buffer_copy = (uint8 *)malloc(the_buffer_size);
702          D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
703   #else
704          // Allocate memory for frame buffer
# Line 686 | Line 728 | driver_window::driver_window(const video
728          Screen_blitter_init(&visualInfo, native_byte_order, mode.depth);
729   #endif
730  
731 <        // Set VideoMonitor
732 <        VideoMonitor.mode = mode;
691 <        set_mac_frame_buffer(mode.depth, native_byte_order);
731 >        // Set frame buffer base
732 >        set_mac_frame_buffer(monitor, mode.depth, native_byte_order);
733  
734          // Everything went well
735          init_ok = true;
# Line 705 | Line 746 | driver_window::~driver_window()
746                  the_buffer_copy = NULL; // don't free() in driver_base dtor
747   #endif
748          }
749 < #ifdef ENABLE_VOSF
750 <        if (use_vosf) {
751 <                // don't free() memory mapped buffers in driver_base dtor
711 <                if (the_buffer != VM_MAP_FAILED) {
712 <                        vm_release(the_buffer, the_buffer_size);
713 <                        the_buffer = NULL;
714 <                }
715 <                if (the_buffer_copy != VM_MAP_FAILED) {
716 <                        vm_release(the_buffer_copy, the_buffer_size);
717 <                        the_buffer_copy = NULL;
718 <                }
719 <        }
720 < #endif
721 <        if (img)
749 >        if (img) {
750 >                if (!have_shm)
751 >                        img->data = NULL;
752                  XDestroyImage(img);
753 +        }
754          if (have_shm) {
755                  shmdt(shminfo.shmaddr);
756                  shmctl(shminfo.shmid, IPC_RMID, 0);
# Line 778 | Line 809 | void driver_window::mouse_moved(int x, i
809          // Warped mouse motion (this code is taken from SDL)
810  
811          // Post first mouse event
812 <        int width = VideoMonitor.mode.x, height = VideoMonitor.mode.y;
812 >        int width = monitor.get_current_mode().x, height = monitor.get_current_mode().y;
813          int delta_x = x - mouse_last_x, delta_y = y - mouse_last_y;
814          mouse_last_x = x; mouse_last_y = y;
815          ADBMouseMoved(delta_x, delta_y);
# Line 815 | Line 846 | void driver_window::mouse_moved(int x, i
846  
847   class driver_dga : public driver_base {
848   public:
849 <        driver_dga();
849 >        driver_dga(X11_monitor_desc &monitor);
850          ~driver_dga();
851  
852          void suspend(void);
# Line 826 | Line 857 | private:
857          void *fb_save;                  // Saved frame buffer for suspend/resume
858   };
859  
860 < driver_dga::driver_dga()
861 < : suspend_win(0), fb_save(NULL)
860 > driver_dga::driver_dga(X11_monitor_desc &m)
861 > : driver_base(m), suspend_win(0), fb_save(NULL)
862   {
863   }
864  
# Line 848 | Line 879 | void driver_dga::suspend(void)
879          LOCK_FRAME_BUFFER;
880  
881          // Save frame buffer
882 <        fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
882 >        fb_save = malloc(mode.y * mode.bytes_per_row);
883          if (fb_save)
884 <                memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
884 >                memcpy(fb_save, the_buffer, mode.y * mode.bytes_per_row);
885  
886          // Close full screen display
887   #ifdef ENABLE_XF86_DGA
# Line 903 | Line 934 | void driver_dga::resume(void)
934                  LOCK_VOSF;
935                  PFLAG_SET_ALL;
936                  UNLOCK_VOSF;
937 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
937 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
938          }
939   #endif
940          
# Line 913 | Line 944 | void driver_dga::resume(void)
944                  // Don't copy fb_save to the temporary frame buffer in VOSF mode
945                  if (!use_vosf)
946   #endif
947 <                memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
947 >                memcpy(the_buffer, fb_save, mode.y * mode.bytes_per_row);
948                  free(fb_save);
949                  fb_save = NULL;
950          }
# Line 935 | Line 966 | const char FBDEVICE_FILE_NAME[] = "/dev/
966  
967   class driver_fbdev : public driver_dga {
968   public:
969 <        driver_fbdev(const video_mode &mode);
969 >        driver_fbdev(X11_monitor_desc &monitor);
970          ~driver_fbdev();
971   };
972  
973   // Open display
974 < driver_fbdev::driver_fbdev(const video_mode &mode)
974 > driver_fbdev::driver_fbdev(X11_monitor_desc &m) : driver_dga(m)
975   {
976          int width = mode.x, height = mode.y;
977  
# Line 988 | Line 1019 | driver_fbdev::driver_fbdev(const video_m
1019                  if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
1020                          continue;
1021                  
1022 <                if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
1022 >                if ((sscanf(line, "%19s %d %x", fb_name, &fb_depth, &fb_offset) == 3)
1023                   && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) {
1024                          device_found = true;
1025                          break;
# Line 1061 | Line 1092 | driver_fbdev::driver_fbdev(const video_m
1092            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1093            the_host_buffer = the_buffer;
1094            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1095 <          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
1095 >          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1096            the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1097          }
1098   #else
# Line 1069 | Line 1100 | driver_fbdev::driver_fbdev(const video_m
1100   #endif
1101   #endif
1102          
1103 <        // Set VideoMonitor
1104 <        VideoModes[0].bytes_per_row = bytes_per_row;
1105 <        VideoModes[0].depth = DepthModeForPixelDepth(fb_depth);
1106 <        VideoMonitor.mode = mode;
1076 <        set_mac_frame_buffer(mode.depth, true);
1103 >        // Set frame buffer base
1104 >        const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1105 >        const_cast<video_mode *>(&mode)->depth = DepthModeForPixelDepth(fb_depth);
1106 >        set_mac_frame_buffer(monitor, mode.depth, true);
1107  
1108          // Everything went well
1109          init_ok = true;
# Line 1096 | Line 1126 | driver_fbdev::~driver_fbdev()
1126                          munmap(the_host_buffer, the_buffer_size);
1127                          the_host_buffer = NULL;
1128                  }
1099                if (the_buffer_copy != VM_MAP_FAILED) {
1100                        vm_release(the_buffer_copy, the_buffer_size);
1101                        the_buffer_copy = NULL;
1102                }
1103                if (the_buffer != VM_MAP_FAILED) {
1104                        vm_release(the_buffer, the_buffer_size);
1105                        the_buffer = NULL;
1106                }
1129          }
1130   #endif
1131   }
# Line 1117 | Line 1139 | driver_fbdev::~driver_fbdev()
1139  
1140   class driver_xf86dga : public driver_dga {
1141   public:
1142 <        driver_xf86dga(const video_mode &mode);
1142 >        driver_xf86dga(X11_monitor_desc &monitor);
1143          ~driver_xf86dga();
1144  
1145          void update_palette(void);
# Line 1128 | Line 1150 | private:
1150   };
1151  
1152   // Open display
1153 < driver_xf86dga::driver_xf86dga(const video_mode &mode)
1154 < : current_dga_cmap(0)
1153 > driver_xf86dga::driver_xf86dga(X11_monitor_desc &m)
1154 > : driver_dga(m), current_dga_cmap(0)
1155   {
1156          int width = mode.x, height = mode.y;
1157  
# Line 1156 | Line 1178 | driver_xf86dga::driver_xf86dga(const vid
1178          XSetWindowAttributes wattr;
1179          wattr.event_mask = eventmask = dga_eventmask;
1180          wattr.override_redirect = True;
1181 +        wattr.colormap = (mode.depth == VDEPTH_1BIT ? DefaultColormap(x_display, screen) : cmap[0]);
1182  
1183          w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
1184 <                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
1184 >                InputOutput, vis, CWEventMask | CWOverrideRedirect |
1185 >                (color_class == DirectColor ? CWColormap : 0), &wattr);
1186  
1187          // Set window name/class
1188          set_window_name(w, STR_WINDOW_TITLE);
# Line 1192 | Line 1216 | driver_xf86dga::driver_xf86dga(const vid
1216  
1217          // Init blitting routines
1218          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
1219 < #if VIDEO_VOSF
1219 > #if ENABLE_VOSF
1220 >        bool native_byte_order;
1221 > #ifdef WORDS_BIGENDIAN
1222 >        native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
1223 > #else
1224 >        native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
1225 > #endif
1226   #if REAL_ADDRESSING || DIRECT_ADDRESSING
1227          // Screen_blitter_init() returns TRUE if VOSF is mandatory
1228          // i.e. the framebuffer update function is not Blit_Copy_Raw
1229 <        use_vosf = Screen_blitter_init(&visualInfo, true, mode.depth);
1229 >        use_vosf = Screen_blitter_init(&visualInfo, native_byte_order, mode.depth);
1230          
1231          if (use_vosf) {
1232            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
1233            the_host_buffer = the_buffer;
1234            the_buffer_size = page_extend((height + 2) * bytes_per_row);
1235 <          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
1235 >          the_buffer_copy = (uint8 *)malloc(the_buffer_size);
1236            the_buffer = (uint8 *)vm_acquire(the_buffer_size);
1237          }
1238   #else
# Line 1210 | Line 1240 | driver_xf86dga::driver_xf86dga(const vid
1240   #endif
1241   #endif
1242          
1243 <        // Set VideoMonitor
1243 >        // Set frame buffer base
1244          const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
1245 <        VideoMonitor.mode = mode;
1216 <        set_mac_frame_buffer(mode.depth, true);
1245 >        set_mac_frame_buffer(monitor, mode.depth, true);
1246  
1247          // Everything went well
1248          init_ok = true;
# Line 1231 | Line 1260 | driver_xf86dga::~driver_xf86dga()
1260          else {
1261                  // don't free() the screen buffer in driver_base dtor
1262                  the_host_buffer = NULL;
1234                
1235                if (the_buffer_copy != VM_MAP_FAILED) {
1236                        vm_release(the_buffer_copy, the_buffer_size);
1237                        the_buffer_copy = NULL;
1238                }
1239                if (the_buffer != VM_MAP_FAILED) {
1240                        vm_release(the_buffer, the_buffer_size);
1241                        the_buffer = NULL;
1242                }
1263          }
1264   #endif
1265   #ifdef ENABLE_XF86_VIDMODE
# Line 1253 | Line 1273 | void driver_xf86dga::update_palette(void
1273   {
1274          driver_dga::update_palette();
1275          current_dga_cmap ^= 1;
1276 <        if (!IsDirectMode(VideoMonitor.mode) && cmap[current_dga_cmap])
1276 >        if (!IsDirectMode(monitor.get_current_mode()) && cmap[current_dga_cmap])
1277                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1278   }
1279  
# Line 1261 | Line 1281 | void driver_xf86dga::update_palette(void
1281   void driver_xf86dga::resume(void)
1282   {
1283          driver_dga::resume();
1284 <        if (!IsDirectMode(VideoMonitor.mode))
1284 >        if (!IsDirectMode(monitor.get_current_mode()))
1285                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1286   }
1287   #endif
# Line 1336 | Line 1356 | static void keycode_init(void)
1356          }
1357   }
1358  
1359 < // Open display for specified mode
1360 < static bool video_open(const video_mode &mode)
1359 > // Open display for current mode
1360 > bool X11_monitor_desc::video_open(void)
1361   {
1362 +        D(bug("video_open()\n"));
1363 +        const video_mode &mode = get_current_mode();
1364 +
1365          // Find best available X visual
1366          if (!find_visual_for_depth(mode.depth)) {
1367                  ErrorAlert(STR_NO_XVISUAL_ERR);
# Line 1378 | Line 1401 | static bool video_open(const video_mode
1401                  int num = vis->map_entries;
1402                  for (int i=0; i<num; i++) {
1403                          int c = (i * 256) / num;
1404 <                        palette[i].pixel = map_rgb(c, c, c);
1405 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1404 >                        x_palette[i].pixel = map_rgb(c, c, c);
1405 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1406                  }
1407          } else if (color_class == PseudoColor) {
1408                  for (int i=0; i<256; i++) {
1409 <                        palette[i].pixel = i;
1410 <                        palette[i].flags = DoRed | DoGreen | DoBlue;
1409 >                        x_palette[i].pixel = i;
1410 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1411                  }
1412          }
1413  
# Line 1392 | Line 1415 | static bool video_open(const video_mode
1415          int num = (color_class == DirectColor ? vis->map_entries : 256);
1416          for (int i=0; i<num; i++) {
1417                  int c = (i * 256) / num;
1418 <                palette[i].red = c * 0x0101;
1419 <                palette[i].green = c * 0x0101;
1420 <                palette[i].blue = c * 0x0101;
1418 >                x_palette[i].red = c * 0x0101;
1419 >                x_palette[i].green = c * 0x0101;
1420 >                x_palette[i].blue = c * 0x0101;
1421          }
1422          if (color_class == PseudoColor || color_class == DirectColor) {
1423 <                XStoreColors(x_display, cmap[0], palette, num);
1424 <                XStoreColors(x_display, cmap[1], palette, num);
1423 >                XStoreColors(x_display, cmap[0], x_palette, num);
1424 >                XStoreColors(x_display, cmap[1], x_palette, num);
1425          }
1426  
1427   #ifdef ENABLE_VOSF
# Line 1411 | Line 1434 | static bool video_open(const video_mode
1434          // Create display driver object of requested type
1435          switch (display_type) {
1436                  case DISPLAY_WINDOW:
1437 <                        drv = new driver_window(mode);
1437 >                        drv = new driver_window(*this);
1438                          break;
1439   #ifdef ENABLE_FBDEV_DGA
1440                  case DISPLAY_DGA:
1441 <                        drv = new driver_fbdev(mode);
1441 >                        drv = new driver_fbdev(*this);
1442                          break;
1443   #endif
1444   #ifdef ENABLE_XF86_DGA
1445                  case DISPLAY_DGA:
1446 <                        drv = new driver_xf86dga(mode);
1446 >                        drv = new driver_xf86dga(*this);
1447                          break;
1448   #endif
1449          }
# Line 1435 | Line 1458 | static bool video_open(const video_mode
1458   #ifdef ENABLE_VOSF
1459          if (use_vosf) {
1460                  // Initialize the VOSF system
1461 <                if (!video_vosf_init()) {
1461 >                if (!video_vosf_init(*this)) {
1462                          ErrorAlert(STR_VOSF_INIT_ERR);
1463                  return false;
1464                  }
# Line 1452 | Line 1475 | static bool video_open(const video_mode
1475          // Start redraw/input thread
1476   #ifdef HAVE_PTHREADS
1477          redraw_thread_cancel = false;
1478 <        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1478 >        Set_pthread_attr(&redraw_thread_attr, 0);
1479 >        redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1480          if (!redraw_thread_active) {
1481                  printf("FATAL: cannot create redraw thread\n");
1482                  return false;
# Line 1496 | Line 1520 | bool VideoInit(bool classic)
1520                  ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1521                  return false;
1522          }
1523 <        sort(avail_depths, avail_depths + num_depths);
1523 >        std::sort(avail_depths, avail_depths + num_depths);
1524          
1525   #ifdef ENABLE_FBDEV_DGA
1526          // Frame buffer name
# Line 1599 | Line 1623 | bool VideoInit(bool classic)
1623                  ErrorAlert(STR_NO_XVISUAL_ERR);
1624                  return false;
1625          }
1626 <        video_init_depth_list();
1626 >
1627 >        // Find requested default mode with specified dimensions
1628 >        uint32 default_id;
1629 >        std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1630 >        for (i = VideoModes.begin(); i != end; ++i) {
1631 >                if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
1632 >                        default_id = i->resolution_id;
1633 >                        break;
1634 >                }
1635 >        }
1636 >        if (i == end) { // not found, use first available mode
1637 >                default_depth = VideoModes[0].depth;
1638 >                default_id = VideoModes[0].resolution_id;
1639 >        }
1640  
1641   #if DEBUG
1642          D(bug("Available video modes:\n"));
1643 <        vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
1607 <        while (i != end) {
1643 >        for (i = VideoModes.begin(); i != end; ++i) {
1644                  int bits = 1 << i->depth;
1645                  if (bits == 16)
1646                          bits = 15;
1647                  else if (bits == 32)
1648                          bits = 24;
1649                  D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
1614                ++i;
1650          }
1651   #endif
1652  
1653 <        // Find requested default mode and open display
1654 <        if (VideoModes.size() == 1)
1655 <                return video_open(VideoModes[0]);
1656 <        else {
1657 <                // Find mode with specified dimensions
1658 <                std::vector<video_mode>::const_iterator i, end = VideoModes.end();
1624 <                for (i = VideoModes.begin(); i != end; ++i) {
1625 <                        if (i->x == default_width && i->y == default_height && i->depth == default_depth)
1626 <                                return video_open(*i);
1627 <                }
1628 <                return video_open(VideoModes[0]);
1629 <        }
1653 >        // Create X11_monitor_desc for this (the only) display
1654 >        X11_monitor_desc *monitor = new X11_monitor_desc(VideoModes, default_depth, default_id);
1655 >        VideoMonitors.push_back(monitor);
1656 >
1657 >        // Open display
1658 >        return monitor->video_open();
1659   }
1660  
1661  
# Line 1635 | Line 1664 | bool VideoInit(bool classic)
1664   */
1665  
1666   // Close display
1667 < static void video_close(void)
1667 > void X11_monitor_desc::video_close(void)
1668   {
1669 +        D(bug("video_close()\n"));
1670 +
1671          // Stop redraw thread
1672   #ifdef HAVE_PTHREADS
1673          if (redraw_thread_active) {
# Line 1652 | Line 1683 | static void video_close(void)
1683          // Unlock frame buffer
1684          UNLOCK_FRAME_BUFFER;
1685          XSync(x_display, false);
1686 +        D(bug(" frame buffer unlocked\n"));
1687  
1688   #ifdef ENABLE_VOSF
1689          if (use_vosf) {
# Line 1677 | Line 1709 | static void video_close(void)
1709  
1710   void VideoExit(void)
1711   {
1712 <        // Close display
1713 <        video_close();
1712 >        // Close displays
1713 >        vector<monitor_desc *>::iterator i, end = VideoMonitors.end();
1714 >        for (i = VideoMonitors.begin(); i != end; ++i)
1715 >                dynamic_cast<X11_monitor_desc *>(*i)->video_close();
1716  
1717   #ifdef ENABLE_XF86_VIDMODE
1718          // Free video mode list
# Line 1736 | Line 1770 | void VideoInterrupt(void)
1770   *  Set palette
1771   */
1772  
1773 < void video_set_palette(uint8 *pal, int num_in)
1773 > void X11_monitor_desc::set_palette(uint8 *pal, int num_in)
1774   {
1775 +        const video_mode &mode = get_current_mode();
1776 +
1777          LOCK_PALETTE;
1778  
1779          // Convert colors to XColor array
1780          int num_out = 256;
1781          bool stretch = false;
1782 <        if (IsDirectMode(VideoMonitor.mode)) {
1782 >        if (IsDirectMode(mode)) {
1783                  // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1784                  num_out = vis->map_entries;
1785                  stretch = true;
1786          }
1787 <        XColor *p = palette;
1787 >        XColor *p = x_palette;
1788          for (int i=0; i<num_out; i++) {
1789                  int c = (stretch ? (i * num_in) / num_out : i);
1790                  p->red = pal[c*3 + 0] * 0x0101;
# Line 1759 | Line 1795 | void video_set_palette(uint8 *pal, int n
1795  
1796   #ifdef ENABLE_VOSF
1797          // Recalculate pixel color expansion map
1798 <        if (!IsDirectMode(VideoMonitor.mode) && xdepth > 8) {
1798 >        if (!IsDirectMode(mode) && xdepth > 8) {
1799                  for (int i=0; i<256; i++) {
1800                          int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1801                          ExpandMap[i] = map_rgb(pal[c*3+0], pal[c*3+1], pal[c*3+2]);
# Line 1769 | Line 1805 | void video_set_palette(uint8 *pal, int n
1805                  LOCK_VOSF;
1806                  PFLAG_SET_ALL;
1807                  UNLOCK_VOSF;
1808 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1808 >                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
1809          }
1810   #endif
1811  
1812          // Tell redraw thread to change palette
1813 <        palette_changed = true;
1813 >        x_palette_changed = true;
1814  
1815          UNLOCK_PALETTE;
1816   }
# Line 1784 | Line 1820 | void video_set_palette(uint8 *pal, int n
1820   *  Switch video mode
1821   */
1822  
1823 < void video_switch_to_mode(const video_mode &mode)
1823 > void X11_monitor_desc::switch_to_current_mode(void)
1824   {
1825          // Close and reopen display
1826          video_close();
1827 <        video_open(mode);
1827 >        video_open();
1828  
1829          if (drv == NULL) {
1830                  ErrorAlert(STR_OPEN_WINDOW_ERR);
# Line 2058 | Line 2094 | static void handle_events(void)
2094                          // Hidden parts exposed, force complete refresh of window
2095                          case Expose:
2096                                  if (display_type == DISPLAY_WINDOW) {
2097 +                                        const video_mode &mode = VideoMonitors[0]->get_current_mode();
2098   #ifdef ENABLE_VOSF
2099                                          if (use_vosf) {                 // VOSF refresh
2100                                                  LOCK_VOSF;
2101                                                  PFLAG_SET_ALL;
2102                                                  UNLOCK_VOSF;
2103 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2103 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2104                                          }
2105                                          else
2106   #endif
# Line 2074 | Line 2111 | static void handle_events(void)
2111                                                          updt_box[x1][y1] = true;
2112                                                  nr_boxes = 16 * 16;
2113                                          } else                                  // Static refresh
2114 <                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
2114 >                                                memset(the_buffer_copy, 0, mode.bytes_per_row * mode.y);
2115                                  }
2116                                  break;
2117  
# Line 2100 | Line 2137 | static void update_display_dynamic(int t
2137          int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
2138          int xil = 0;
2139          int rxm = 0, rxmo = 0;
2140 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2141 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2142 <        int rx = VideoMonitor.mode.bytes_per_row / 16;
2143 <        int ry = VideoMonitor.mode.y / 16;
2140 >        const video_mode &mode = drv->monitor.get_current_mode();
2141 >        int bytes_per_row = mode.bytes_per_row;
2142 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2143 >        int rx = mode.bytes_per_row / 16;
2144 >        int ry = mode.y / 16;
2145          int max_box;
2146  
2147          y2s = sm_uptd[ticker % 8];
# Line 2157 | Line 2195 | static void update_display_dynamic(int t
2195                                          i = (yi * bytes_per_row) + xi;
2196                                          for (y2=0; y2 < yil; y2++, i += bytes_per_row)
2197                                                  memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
2198 <                                        if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2198 >                                        if (mode.depth == VDEPTH_1BIT) {
2199                                                  if (drv->have_shm)
2200                                                          XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
2201                                                  else
# Line 2189 | Line 2227 | static void update_display_static(driver
2227   {
2228          // Incremental update code
2229          unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
2230 <        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
2231 <        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
2230 >        const video_mode &mode = drv->monitor.get_current_mode();
2231 >        int bytes_per_row = mode.bytes_per_row;
2232 >        int bytes_per_pixel = mode.bytes_per_row / mode.x;
2233          uint8 *p, *p2;
2234  
2235          // Check for first line from top and first line from bottom that have changed
2236          y1 = 0;
2237 <        for (j=0; j<VideoMonitor.mode.y; j++) {
2237 >        for (j=0; j<mode.y; j++) {
2238                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2239                          y1 = j;
2240                          break;
2241                  }
2242          }
2243          y2 = y1 - 1;
2244 <        for (j=VideoMonitor.mode.y-1; j>=y1; j--) {
2244 >        for (j=mode.y-1; j>=y1; j--) {
2245                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2246                          y2 = j;
2247                          break;
# Line 2212 | Line 2251 | static void update_display_static(driver
2251  
2252          // Check for first column from left and first column from right that have changed
2253          if (high) {
2254 <                if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
2255 <                        x1 = VideoMonitor.mode.x - 1;
2254 >                if (mode.depth == VDEPTH_1BIT) {
2255 >                        x1 = mode.x - 1;
2256                          for (j=y1; j<=y2; j++) {
2257                                  p = &the_buffer[j * bytes_per_row];
2258                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2231 | Line 2270 | static void update_display_static(driver
2270                                  p2 = &the_buffer_copy[j * bytes_per_row];
2271                                  p += bytes_per_row;
2272                                  p2 += bytes_per_row;
2273 <                                for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) {
2273 >                                for (i=(mode.x>>3); i>(x2>>3); i--) {
2274                                          p--; p2--;
2275                                          if (*p != *p2) {
2276                                                  x2 = (i << 3) + 7;
# Line 2250 | Line 2289 | static void update_display_static(driver
2289                          }
2290  
2291                  } else {
2292 <                        x1 = VideoMonitor.mode.x;
2292 >                        x1 = mode.x;
2293                          for (j=y1; j<=y2; j++) {
2294                                  p = &the_buffer[j * bytes_per_row];
2295                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 2268 | Line 2307 | static void update_display_static(driver
2307                                  p2 = &the_buffer_copy[j * bytes_per_row];
2308                                  p += bytes_per_row;
2309                                  p2 += bytes_per_row;
2310 <                                for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2310 >                                for (i=mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
2311                                          p--;
2312                                          p2--;
2313                                          if (*p != *p2) {
# Line 2335 | Line 2374 | static inline void handle_palette_change
2374   {
2375          LOCK_PALETTE;
2376  
2377 <        if (palette_changed) {
2378 <                palette_changed = false;
2377 >        if (x_palette_changed) {
2378 >                x_palette_changed = false;
2379                  drv->update_palette();
2380          }
2381  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines