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.53 by cebix, 2001-07-06T20:49:53Z 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 80 | Line 79 | enum {
79   // Constants
80   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
81  
82 < static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | StructureNotifyMask;
82 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
83   static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
84  
85  
# Line 93 | Line 92 | static int display_type = DISPLAY_WINDOW
92   static bool local_X11;                                                          // Flag: X server running on local machine?
93   static uint8 *the_buffer = NULL;                                        // Mac frame buffer (where MacOS draws into)
94   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer (for refreshed modes)
95 + static uint32 the_buffer_size;                                          // Size of allocated the_buffer
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 138 | 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 152 | 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 183 | Line 184 | static void (*video_refresh)(void);
184  
185   // Prototypes
186   static void *redraw_func(void *arg);
186 static int event2keycode(XKeyEvent &ev);
187  
188   // From main_unix.cpp
189   extern char *x_display_name;
# 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
216                        min_depth = 1;
217                        max_depth = 32;
218                        break;
246   #ifdef ENABLE_VOSF
247                  case VDEPTH_2BIT:
248 <                case VDEPTH_4BIT:       // VOSF blitters can convert 2/4-bit -> 16/32-bit
249 <                        min_depth = 15;
223 <                        max_depth = 32;
224 <                        break;
225 <                case VDEPTH_8BIT:       // VOSF blitters can convert 8-bit -> 16/32-bit
248 >                case VDEPTH_4BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
249 >                case VDEPTH_8BIT:
250                          min_depth = 8;
251                          max_depth = 32;
252                          break;
# Line 296 | Line 320 | static bool find_visual_for_depth(video_
320                  case DirectColor: D(bug("DirectColor\n")); break;
321          }
322   #endif
323 +        return true;
324   }
325  
326   // Add mode to list of supported modes
# Line 323 | 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 335 | 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);
346 <        D(bug("Host frame buffer = %p, ", 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 423 | 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 432 | Line 457 | public:
457          virtual void toggle_mouse_grab(void) {}
458          virtual void mouse_moved(int x, int y) { ADBMouseMoved(x, y); }
459  
460 +        void disable_mouse_accel(void);
461 +        void restore_mouse_accel(void);
462 +
463          virtual void grab_mouse(void) {}
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 +
473 +        int orig_accel_numer, orig_accel_denom, orig_threshold; // Original mouse acceleration
474   };
475  
476   class driver_window;
# Line 451 | 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 476 | 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;
517 +        XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold);
518   }
519  
520   driver_base::~driver_base()
521   {
522          ungrab_mouse();
523 +        restore_mouse_accel();
524  
525          if (w) {
526                  XUnmapWindow(x_display, w);
# Line 509 | Line 544 | driver_base::~driver_base()
544          }
545   #ifdef ENABLE_VOSF
546          else {
547 <                if (the_buffer != (uint8 *)VM_MAP_FAILED) {
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_buffer_copy != (uint8 *)VM_MAP_FAILED) {
554 <                        vm_release(the_buffer_copy, the_buffer_size);
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 >                }
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                  }
563          }
# Line 524 | Line 567 | driver_base::~driver_base()
567   // Palette has changed
568   void driver_base::update_palette(void)
569   {
570 <        if (cmap[0] && cmap[1]) {
571 <                int num = 256;
572 <                if (IsDirectMode(VideoMonitor.mode))
530 <                        num = vis->map_entries; // Palette is gamma table
531 <                else if (color_class == DirectColor)
570 >        if (color_class == PseudoColor || color_class == DirectColor) {
571 >                int num = vis->map_entries;
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   }
579  
580 + // Disable mouse acceleration
581 + void driver_base::disable_mouse_accel(void)
582 + {
583 +        XChangePointerControl(x_display, True, False, 1, 1, 0);
584 + }
585 +
586 + // Restore mouse acceleration to original value
587 + void driver_base::restore_mouse_accel(void)
588 + {
589 +        XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold);
590 + }
591 +
592  
593   /*
594   *  Windowed display driver
595   */
596  
597   // Open display
598 < driver_window::driver_window(const video_mode &mode)
599 < : gc(0), img(NULL), have_shm(false), mouse_grabbed(false), mac_cursor(0)
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 552 | Line 605 | driver_window::driver_window(const video
605          // Set absolute mouse mode
606          ADBSetRelMouseMode(mouse_grabbed);
607  
608 <        // Create window
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;
612          wattr.event_mask = eventmask = win_eventmask;
613 <        wattr.background_pixel = black_pixel;
614 <        wattr.colormap = (mode.depth == VDEPTH_1BIT && color_class == PseudoColor ? DefaultColormap(x_display, screen) : cmap[0]);
613 >        wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
614 >        wattr.border_pixel = 0;
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 | (color_class == PseudoColor || color_class == DirectColor ? CWColormap : 0), &wattr);
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 582 | 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 596 | 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 610 | Line 670 | driver_window::driver_window(const video
670                  if (shm_error) {
671                          shmdt(shminfo.shmaddr);
672                          XDestroyImage(img);
673 +                        img = NULL;
674                          shminfo.shmid = -1;
675                  } else {
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 622 | 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 630 | Line 693 | driver_window::driver_window(const video
693          }
694  
695   #ifdef ENABLE_VOSF
696 +        use_vosf = true;
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);
636        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
705          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
706 +        D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
707   #endif
708  
709          // Create GC
# Line 662 | 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;
667 <        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 673 | Line 738 | driver_window::driver_window(const video
738   // Close display
739   driver_window::~driver_window()
740   {
676        if (img)
677                XDestroyImage(img);
741          if (have_shm) {
742                  XShmDetach(x_display, &shminfo);
743 + #ifdef ENABLE_VOSF
744 +                the_host_buffer = NULL; // don't free() in driver_base dtor
745 + #else
746                  the_buffer_copy = NULL; // don't free() in driver_base dtor
747 + #endif
748 +        }
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);
757          }
758          if (gc)
759                  XFreeGC(x_display, gc);
# Line 704 | Line 780 | void driver_window::grab_mouse(void)
780                  Delay_usec(100000);
781          }
782          if (result == GrabSuccess) {
707                ADBSetRelMouseMode(mouse_grabbed = true);
783                  XStoreName(x_display, w, GetString(STR_WINDOW_TITLE_GRABBED));
784 <                XSync(x_display, false);
784 >                ADBSetRelMouseMode(mouse_grabbed = true);
785 >                disable_mouse_accel();
786          }
787   }
788  
# Line 717 | Line 793 | void driver_window::ungrab_mouse(void)
793                  XUngrabPointer(x_display, CurrentTime);
794                  XStoreName(x_display, w, GetString(STR_WINDOW_TITLE));
795                  ADBSetRelMouseMode(mouse_grabbed = false);
796 +                restore_mouse_accel();
797          }
798   }
799  
# Line 732 | 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 769 | 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 780 | 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 802 | 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 812 | Line 889 | void driver_dga::suspend(void)
889   #endif
890          XUngrabPointer(x_display, CurrentTime);
891          XUngrabKeyboard(x_display, CurrentTime);
892 +        restore_mouse_accel();
893          XUnmapWindow(x_display, w);
894          wait_unmapped(w);
895  
# Line 841 | Line 919 | void driver_dga::resume(void)
919          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
920          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
921          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
922 +        disable_mouse_accel();
923   #ifdef ENABLE_XF86_DGA
924          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
925          XF86DGASetViewPort(x_display, screen, 0, 0);
# Line 855 | 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 865 | 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 887 | 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 940 | 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 987 | Line 1066 | driver_fbdev::driver_fbdev(const video_m
1066          XGrabPointer(x_display, w, True,
1067                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1068                  GrabModeAsync, GrabModeAsync, w, None, CurrentTime);
1069 +        disable_mouse_accel();
1070          
1071          // Calculate bytes per row
1072          int bytes_per_row = TrivialBytesPerRow(mode.x, mode.depth);
1073          
1074          // Map frame buffer
1075 <        if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1076 <                if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1075 >        the_buffer_size = height * bytes_per_row;
1076 >        if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
1077 >                if ((the_buffer = (uint8 *) mmap(NULL, the_buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
1078                          char str[256];
1079                          sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
1080                          ErrorAlert(str);
# Line 1011 | 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 1019 | 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;
1026 <        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 1032 | Line 1112 | driver_fbdev::driver_fbdev(const video_m
1112   // Close display
1113   driver_fbdev::~driver_fbdev()
1114   {
1115 +        if (!use_vosf) {
1116 +                if (the_buffer != MAP_FAILED) {
1117 +                        // don't free() the screen buffer in driver_base dtor
1118 +                        munmap(the_buffer, the_buffer_size);
1119 +                        the_buffer = NULL;
1120 +                }
1121 +        }
1122 + #ifdef ENABLE_VOSF
1123 +        else {
1124 +                if (the_host_buffer != MAP_FAILED) {
1125 +                        // don't free() the screen buffer in driver_base dtor
1126 +                        munmap(the_host_buffer, the_buffer_size);
1127 +                        the_host_buffer = NULL;
1128 +                }
1129 +        }
1130 + #endif
1131   }
1132   #endif
1133  
# Line 1043 | 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 1054 | 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 1082 | 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 1101 | Line 1199 | driver_xf86dga::driver_xf86dga(const vid
1199          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1200          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
1201          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1202 +        disable_mouse_accel();
1203  
1204          int v_width, v_bank, v_size;
1205          XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
# Line 1117 | 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 < #ifdef 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 1135 | 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;
1141 <        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 1148 | Line 1252 | driver_xf86dga::driver_xf86dga(const vid
1252   driver_xf86dga::~driver_xf86dga()
1253   {
1254          XF86DGADirectVideo(x_display, screen, 0);
1255 +        if (!use_vosf) {
1256 +                // don't free() the screen buffer in driver_base dtor
1257 +                the_buffer = NULL;
1258 +        }
1259 + #ifdef ENABLE_VOSF
1260 +        else {
1261 +                // don't free() the screen buffer in driver_base dtor
1262 +                the_host_buffer = NULL;
1263 +        }
1264 + #endif
1265   #ifdef ENABLE_XF86_VIDMODE
1266          if (has_vidmode)
1267                  XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
# Line 1159 | 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 1167 | 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 1242 | 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 1255 | Line 1372 | static bool video_open(const video_mode
1372          if (color_class == PseudoColor || color_class == DirectColor) {
1373                  cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1374                  cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1375 +        } else {
1376 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
1377 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
1378          }
1379  
1380          // Find pixel format of direct modes
# Line 1276 | Line 1396 | static bool video_open(const video_mode
1396                          --bloss;
1397          }
1398  
1399 <        // Preset palette pixel values for gamma table
1399 >        // Preset palette pixel values for CLUT or gamma table
1400          if (color_class == DirectColor) {
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);
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 >                        x_palette[i].pixel = i;
1410 >                        x_palette[i].flags = DoRed | DoGreen | DoBlue;
1411                  }
1412          }
1413  
# Line 1289 | 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;
1421 <                if (color_class == PseudoColor)
1422 <                        palette[i].pixel = i;
1423 <                palette[i].flags = DoRed | DoGreen | DoBlue;
1424 <        }
1299 <        if (cmap[0] && cmap[1]) {
1300 <                XStoreColors(x_display, cmap[0], palette, num);
1301 <                XStoreColors(x_display, cmap[1], palette, num);
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], x_palette, num);
1424 >                XStoreColors(x_display, cmap[1], x_palette, num);
1425          }
1426  
1427   #ifdef ENABLE_VOSF
1428          // Load gray ramp to 8->16/32 expand map
1429 <        if (!IsDirectMode(mode) && (color_class == TrueColor || color_class == DirectColor))
1429 >        if (!IsDirectMode(mode) && xdepth > 8)
1430                  for (int i=0; i<256; i++)
1431                          ExpandMap[i] = map_rgb(i, i, i);
1432   #endif
# Line 1311 | 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 1334 | Line 1457 | static bool video_open(const video_mode
1457  
1458   #ifdef ENABLE_VOSF
1459          if (use_vosf) {
1460 <                // Initialize the mainBuffer structure
1461 <                if (!video_init_buffer()) {
1460 >                // Initialize the VOSF system
1461 >                if (!video_vosf_init(*this)) {
1462                          ErrorAlert(STR_VOSF_INIT_ERR);
1463                  return false;
1464                  }
1342
1343                // Initialize the handler for SIGSEGV
1344                if (!sigsegv_install_handler(screen_fault_handler)) {
1345                        ErrorAlert("Could not initialize Video on SEGV signals");
1346                        return false;
1347                }
1465          }
1466   #endif
1467          
# Line 1358 | 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 1402 | 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 1505 | 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();
1513 <        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));
1520                ++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();
1530 <                for (i = VideoModes.begin(); i != end; ++i) {
1531 <                        if (i->x == default_width && i->y == default_height && i->depth == default_depth)
1532 <                                return video_open(*i);
1533 <                }
1534 <                return video_open(VideoModes[0]);
1535 <        }
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 1541 | 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 1558 | 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
1563        // Deinitialize VOSF
1689          if (use_vosf) {
1690 <                if (mainBuffer.pageInfo) {
1691 <                        free(mainBuffer.pageInfo);
1567 <                        mainBuffer.pageInfo = NULL;
1568 <                }
1569 <                if (mainBuffer.dirtyPages) {
1570 <                        free(mainBuffer.dirtyPages);
1571 <                        mainBuffer.dirtyPages = NULL;
1572 <                }
1690 >                // Deinitialize VOSF
1691 >                video_vosf_exit();
1692          }
1693   #endif
1694  
# Line 1590 | 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 1649 | 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 <        if (IsDirectMode(VideoMonitor.mode)) {
1781 >        bool stretch = false;
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 = (i * num_in) / num_out;
1789 >                int c = (stretch ? (i * num_in) / num_out : i);
1790                  p->red = pal[c*3 + 0] * 0x0101;
1791                  p->green = pal[c*3 + 1] * 0x0101;
1792                  p->blue = pal[c*3 + 2] * 0x0101;
1668                if (color_class == PseudoColor)
1669                        p->pixel = i;
1670                p->flags = DoRed | DoGreen | DoBlue;
1793                  p++;
1794          }
1795  
1796   #ifdef ENABLE_VOSF
1797          // Recalculate pixel color expansion map
1798 <        if (!IsDirectMode(VideoMonitor.mode) && (color_class == TrueColor || color_class == DirectColor)) {
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 1683 | 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 1698 | 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 1886 | Line 2008 | static void handle_events(void)
2008                  XNextEvent(x_display, &event);
2009  
2010                  switch (event.type) {
2011 +
2012                          // Mouse button
2013                          case ButtonPress: {
2014                                  unsigned int button = event.xbutton.button;
# Line 1918 | Line 2041 | static void handle_events(void)
2041                                  drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2042                                  break;
2043  
2044 +                        // Mouse entered window
2045 +                        case EnterNotify:
2046 +                                if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
2047 +                                        drv->mouse_moved(event.xmotion.x, event.xmotion.y);
2048 +                                break;
2049 +
2050                          // Keyboard
2051                          case KeyPress: {
2052                                  int code = -1;
# Line 1965 | 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 1981 | 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 2007 | 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 2064 | 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 2095 | Line 2226 | static void update_display_dynamic(int t
2226   static void update_display_static(driver_window *drv)
2227   {
2228          // Incremental update code
2229 <        int 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;
2229 >        unsigned wide = 0, high = 0, x1, x2, y1, y2, i, j;
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 2119 | 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 2138 | 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 2157 | 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 2175 | 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 2242 | 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  
# Line 2254 | Line 2386 | static void video_refresh_dga(void)
2386   {
2387          // Quit DGA mode if requested
2388          possibly_quit_dga_mode();
2257        
2258        // Handle X events
2259        handle_events();
2260        
2261        // Handle palette changes
2262        handle_palette_changes();
2389   }
2390  
2391   #ifdef ENABLE_VOSF
# Line 2269 | Line 2395 | static void video_refresh_dga_vosf(void)
2395          // Quit DGA mode if requested
2396          possibly_quit_dga_mode();
2397          
2272        // Handle X events
2273        handle_events();
2274        
2275        // Handle palette changes
2276        handle_palette_changes();
2277        
2398          // Update display (VOSF variant)
2399          static int tick_counter = 0;
2400          if (++tick_counter >= frame_skip) {
# Line 2293 | Line 2413 | static void video_refresh_window_vosf(vo
2413          // Ungrab mouse if requested
2414          possibly_ungrab_mouse();
2415          
2296        // Handle X events
2297        handle_events();
2298        
2299        // Handle palette changes
2300        handle_palette_changes();
2301        
2416          // Update display (VOSF variant)
2417          static int tick_counter = 0;
2418          if (++tick_counter >= frame_skip) {
# Line 2318 | Line 2432 | static void video_refresh_window_static(
2432          // Ungrab mouse if requested
2433          possibly_ungrab_mouse();
2434  
2321        // Handle X events
2322        handle_events();
2323        
2324        // Handle_palette changes
2325        handle_palette_changes();
2326        
2435          // Update display (static variant)
2436          static int tick_counter = 0;
2437          if (++tick_counter >= frame_skip) {
# Line 2337 | Line 2445 | static void video_refresh_window_dynamic
2445          // Ungrab mouse if requested
2446          possibly_ungrab_mouse();
2447  
2340        // Handle X events
2341        handle_events();
2342        
2343        // Handle_palette changes
2344        handle_palette_changes();
2345        
2448          // Update display (dynamic variant)
2449          static int tick_counter = 0;
2450          tick_counter++;
# Line 2378 | Line 2480 | static void VideoRefreshInit(void)
2480          }
2481   }
2482  
2483 + // This function is called on non-threaded platforms from a timer interrupt
2484   void VideoRefresh(void)
2485   {
2486          // We need to check redraw_thread_active to inhibit refreshed during
2487          // mode changes on non-threaded platforms
2488 <        if (redraw_thread_active)
2489 <                video_refresh();
2488 >        if (!redraw_thread_active)
2489 >                return;
2490 >
2491 >        // Handle X events
2492 >        handle_events();
2493 >
2494 >        // Handle palette changes
2495 >        handle_palette_changes();
2496 >
2497 >        // Update display
2498 >        video_refresh();
2499   }
2500  
2501 + const int VIDEO_REFRESH_HZ = 60;
2502 + const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2503 +
2504   #ifdef HAVE_PTHREADS
2505   static void *redraw_func(void *arg)
2506   {
2507 +        int fd = ConnectionNumber(x_display);
2508 +
2509          uint64 start = GetTicks_usec();
2510          int64 ticks = 0;
2511 <        uint64 next = GetTicks_usec();
2511 >        uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2512 >
2513          while (!redraw_thread_cancel) {
2514 <                video_refresh();
2397 <                next += 16667;
2514 >
2515                  int64 delay = next - GetTicks_usec();
2516 <                if (delay > 0)
2517 <                        Delay_usec(delay);
2518 <                else if (delay < -16667)
2516 >                if (delay < -VIDEO_REFRESH_DELAY) {
2517 >
2518 >                        // We are lagging far behind, so we reset the delay mechanism
2519                          next = GetTicks_usec();
2520 <                ticks++;
2520 >
2521 >                } else if (delay <= 0) {
2522 >
2523 >                        // Delay expired, refresh display
2524 >                        handle_events();
2525 >                        handle_palette_changes();
2526 >                        video_refresh();
2527 >                        next += VIDEO_REFRESH_DELAY;
2528 >                        ticks++;
2529 >
2530 >                } else {
2531 >
2532 >                        // No display refresh pending, check for X events
2533 >                        fd_set readfds;
2534 >                        FD_ZERO(&readfds);
2535 >                        FD_SET(fd, &readfds);
2536 >                        struct timeval timeout;
2537 >                        timeout.tv_sec = 0;
2538 >                        timeout.tv_usec = delay;
2539 >                        if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2540 >                                handle_events();
2541 >                }
2542          }
2543 +
2544          uint64 end = GetTicks_usec();
2545 <        // printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
2545 >        D(bug("%Ld refreshes in %Ld usec = %f refreshes/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
2546          return NULL;
2547   }
2548   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines