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.43 by gbeauche, 2001-06-28T22:06:18Z vs.
Revision 1.44 by cebix, 2001-06-30T17:21:54Z

# Line 72 | Line 72 | enum {
72  
73   // Constants
74   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
75 < const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices";
75 >
76 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
77 > static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
78  
79  
80   // Global variables
81   static int32 frame_skip;                                                        // Prefs items
82 < static int16 mouse_wheel_mode = 1;
83 < static int16 mouse_wheel_lines = 3;
82 > static int16 mouse_wheel_mode;
83 > static int16 mouse_wheel_lines;
84  
85   static int display_type = DISPLAY_WINDOW;                       // See enum above
86   static bool local_X11;                                                          // Flag: X server running on local machine?
87 < static uint8 *the_buffer;                                                       // Mac frame buffer
87 > static uint8 *the_buffer = NULL;                                        // Mac frame buffer (where MacOS draws into)
88 > static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer (for refreshed modes)
89  
90   #ifdef HAVE_PTHREADS
91   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
# Line 93 | Line 96 | static pthread_t redraw_thread;                                                //
96   static bool has_dga = false;                                            // Flag: Video DGA capable
97   static bool has_vidmode = false;                                        // Flag: VidMode extension available
98  
99 + #ifdef ENABLE_VOSF
100 + static bool use_vosf = true;                                            // Flag: VOSF enabled
101 + #else
102 + static const bool use_vosf = false;                                     // VOSF not possible
103 + #endif
104 +
105   static bool ctrl_down = false;                                          // Flag: Ctrl key pressed
106   static bool caps_on = false;                                            // Flag: Caps Lock on
107   static bool quit_full_screen = false;                           // Flag: DGA close requested from redraw thread
# Line 107 | Line 116 | static int keycode_table[256];                                         // X
116   // X11 variables
117   static int screen;                                                                      // Screen number
118   static int xdepth;                                                                      // Depth of X screen
119 < static Window rootwin, the_win;                                         // Root window and our window
119 > static Window rootwin;                                                          // Root window and our window
120   static XVisualInfo visualInfo;
121   static Visual *vis;
122 < static Colormap cmap[2];                                                        // Two colormaps (DGA) for 8-bit mode
114 < static bool cmap_allocated = false;
122 > static Colormap cmap[2] = {0, 0};                                       // Colormaps for indexed modes (DGA needs two of them)
123   static XColor black, white;
124   static unsigned long black_pixel, white_pixel;
125   static int eventmask;
118 static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
119 static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
120 static Atom WM_DELETE_WINDOW = (Atom)0;
126  
127 < static XColor palette[256];                                                     // Color palette for 8-bit mode
127 > static XColor palette[256];                                                     // Color palette for indexed modes
128   static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
129 +
130 + #ifdef ENABLE_FBDEV_DGA
131 + static int fbdev_fd = -1;
132 + #endif
133 +
134 + #ifdef ENABLE_XF86_VIDMODE
135 + static XF86VidModeModeInfo **x_video_modes = NULL;      // Array of all available modes
136 + static int num_x_video_modes;
137 + #endif
138 +
139 + // Mutex to protect palette
140   #ifdef HAVE_PTHREADS
141 < static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;        // Mutex to protect palette
141 > static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;
142   #define LOCK_PALETTE pthread_mutex_lock(&palette_lock)
143   #define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock)
144   #else
# Line 130 | Line 146 | static pthread_mutex_t palette_lock = PT
146   #define UNLOCK_PALETTE
147   #endif
148  
149 < // Variables for window mode
134 < static GC the_gc;
135 < static XImage *img = NULL;
136 < static XShmSegmentInfo shminfo;
137 < static Cursor mac_cursor;
138 < static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer
139 < static bool have_shm = false;                                           // Flag: SHM extensions available
140 < static bool updt_box[17][17];                                           // Flag for Update
141 < static int nr_boxes;
142 < static const int sm_uptd[] = {4,1,6,3,0,5,2,7};
143 < static int sm_no_boxes[] = {1,8,32,64,128,300};
144 <
145 < // Variables for XF86 DGA mode
146 < static int current_dga_cmap;                                            // Number (0 or 1) of currently installed DGA colormap
147 < static Window suspend_win;                                                      // "Suspend" window
148 < static void *fb_save = NULL;                                            // Saved frame buffer for suspend
149 > // Mutex to protect frame buffer
150   #ifdef HAVE_PTHREADS
151 < static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer
151 > static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
152   #define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock);
153   #define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock);
154   #else
# Line 155 | Line 156 | static pthread_mutex_t frame_buffer_lock
156   #define UNLOCK_FRAME_BUFFER
157   #endif
158  
159 < // Variables for fbdev DGA mode
160 < const char FBDEVICE_FILE_NAME[] = "/dev/fb";
161 < static int fbdev_fd;
162 <
163 < #ifdef ENABLE_XF86_VIDMODE
163 < // Variables for XF86 VidMode support
164 < static XF86VidModeModeInfo **x_video_modes;                     // Array of all available modes
165 < static int num_x_video_modes;
166 < #endif
167 <
168 < #ifdef ENABLE_VOSF
169 < static bool use_vosf = true;                                            // Flag: VOSF enabled
170 < #else
171 < static const bool use_vosf = false;                                     // Flag: VOSF enabled
172 < #endif
159 > // Variables for non-VOSF incremental refresh
160 > static const int sm_uptd[] = {4,1,6,3,0,5,2,7};
161 > static int sm_no_boxes[] = {1,8,32,64,128,300};
162 > static bool updt_box[17][17];
163 > static int nr_boxes;
164  
165 < // VideoRefresh function
165 > // Video refresh function
166   static void VideoRefreshInit(void);
167   static void (*video_refresh)(void);
168  
169 +
170   // Prototypes
171   static void *redraw_func(void *arg);
172   static int event2keycode(XKeyEvent &ev);
# Line 187 | Line 179 | extern Display *x_display;
179   extern void SysMountFirstFloppy(void);
180  
181  
190 #ifdef ENABLE_VOSF
191 # include "video_vosf.h"
192 #endif
193
194
182   /*
183 < *  Initialization
183 > *  Utility functions
184   */
185  
186   // Add mode to list of supported modes
# Line 222 | Line 209 | static void set_mac_frame_buffer(video_d
209          else
210                  MacFrameLayout = FLAYOUT_DIRECT;
211          VideoMonitor.mac_frame_base = MacFrameBaseMac;
212 +
213 +        // Set variables used by UAE memory banking
214 +        MacFrameBaseHost = the_buffer;
215 +        MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
216 +        InitFrameBufferMapping();
217   #else
218          VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
219          D(bug("Host frame buffer = %p, ", the_buffer));
# Line 260 | Line 252 | static void set_window_focus(Window w)
252   }
253  
254   // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
255 + static Atom WM_DELETE_WINDOW = (Atom)0;
256   static void set_window_delete_protocol(Window w)
257   {
258          WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
# Line 296 | Line 289 | static int error_handler(Display *d, XEr
289                  return old_error_handler(d, e);
290   }
291  
292 < // Init window mode
293 < static bool init_window(const video_mode &mode)
292 >
293 > /*
294 > *  Display "driver" classes
295 > */
296 >
297 > class driver_base {
298 > public:
299 >        driver_base();
300 >        virtual ~driver_base();
301 >
302 >        virtual void update_palette(void);
303 >        virtual void suspend(void) {}
304 >        virtual void resume(void) {}
305 >
306 > public:
307 >        bool init_ok;   // Initialization succeeded (we can't use exceptions because of -fomit-frame-pointer)
308 >        Window w;               // The window we draw into
309 > };
310 >
311 > class driver_window;
312 > static void update_display_window_vosf(driver_window *drv);
313 > static void update_display_dynamic(int ticker, driver_window *drv);
314 > static void update_display_static(driver_window *drv);
315 >
316 > class driver_window : public driver_base {
317 >        friend void update_display_window_vosf(driver_window *drv);
318 >        friend void update_display_dynamic(int ticker, driver_window *drv);
319 >        friend void update_display_static(driver_window *drv);
320 >
321 > public:
322 >        driver_window(const video_mode &mode);
323 >        ~driver_window();
324 >
325 > private:
326 >        GC gc;
327 >        XImage *img;
328 >        bool have_shm;                          // Flag: SHM extensions available
329 >        XShmSegmentInfo shminfo;
330 >        Cursor mac_cursor;
331 > };
332 >
333 > static driver_base *drv = NULL; // Pointer to currently used driver object
334 >
335 > #ifdef ENABLE_VOSF
336 > # include "video_vosf.h"
337 > #endif
338 >
339 > driver_base::driver_base()
340 > : init_ok(false), w(0)
341 > {
342 >        the_buffer = NULL;
343 >        the_buffer_copy = NULL;
344 > }
345 >
346 > driver_base::~driver_base()
347 > {
348 >        XFlush(x_display);
349 >        XSync(x_display, false);
350 >
351 >        if (w) {
352 >                XUnmapWindow(x_display, w);
353 >                wait_unmapped(w);
354 >                XDestroyWindow(x_display, w);
355 >        }
356 >
357 >        // Free frame buffer(s)
358 >        if (!use_vosf) {
359 >                if (the_buffer) {
360 >                        free(the_buffer);
361 >                        the_buffer = NULL;
362 >                }
363 >                if (the_buffer_copy) {
364 >                        free(the_buffer_copy);
365 >                        the_buffer_copy = NULL;
366 >                }
367 >        }
368 > #ifdef ENABLE_VOSF
369 >        else {
370 >                if (the_buffer != (uint8 *)VM_MAP_FAILED) {
371 >                        vm_release(the_buffer, the_buffer_size);
372 >                        the_buffer = NULL;
373 >                }
374 >                if (the_buffer_copy != (uint8 *)VM_MAP_FAILED) {
375 >                        vm_release(the_buffer_copy, the_buffer_size);
376 >                        the_buffer_copy = NULL;
377 >                }
378 >        }
379 > #endif
380 > }
381 >
382 > // Palette has changed
383 > void driver_base::update_palette(void)
384 > {
385 >        if (cmap[0] && cmap[1]) {
386 >                int num = 256;
387 >                if (xdepth == 15)
388 >                        num = 32;
389 >                else if (xdepth == 16)
390 >                        num = 64;
391 >                XStoreColors(x_display, cmap[0], palette, num);
392 >                XStoreColors(x_display, cmap[1], palette, num);
393 >        }
394 >        XSync(x_display, false);
395 > }
396 >
397 >
398 > /*
399 > *  Windowed display driver
400 > */
401 >
402 > // Open display
403 > driver_window::driver_window(const video_mode &mode)
404 > : gc(0), img(NULL), have_shm(false), mac_cursor(0)
405   {
406          int width = mode.x, height = mode.y;
407          int aligned_width = (width + 15) & ~15;
# Line 306 | Line 410 | static bool init_window(const video_mode
410          // Set absolute mouse mode
411          ADBSetRelMouseMode(false);
412  
309        // Read frame skip prefs
310        frame_skip = PrefsFindInt32("frameskip");
311
413          // Create window
414          XSetWindowAttributes wattr;
415          wattr.event_mask = eventmask = win_eventmask;
416          wattr.background_pixel = black_pixel;
417          wattr.colormap = cmap[0];
418 <
419 <        the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
319 <                InputOutput, vis, CWEventMask | CWBackPixel | ((IsDirectMode(mode) || mode.depth == VDEPTH_1BIT) ? 0 : CWColormap), &wattr);
418 >        w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
419 >                InputOutput, vis, CWEventMask | CWBackPixel | ((mode.depth == VDEPTH_1BIT || cmap[0] == 0) ? 0 : CWColormap), &wattr);
420  
421          // Set window name/class
422 <        set_window_name(the_win, STR_WINDOW_TITLE);
422 >        set_window_name(w, STR_WINDOW_TITLE);
423  
424          // Indicate that we want keyboard input
425 <        set_window_focus(the_win);
425 >        set_window_focus(w);
426  
427          // Set delete protocol property
428 <        set_window_delete_protocol(the_win);
428 >        set_window_delete_protocol(w);
429  
430          // Make window unresizable
431          {
# Line 336 | Line 436 | static bool init_window(const video_mode
436                          hints->min_height = height;
437                          hints->max_height = height;
438                          hints->flags = PMinSize | PMaxSize;
439 <                        XSetWMNormalHints(x_display, the_win, hints);
439 >                        XSetWMNormalHints(x_display, w, hints);
440                          XFree(hints);
441                  }
442          }
443          
444          // Show window
445 <        XMapWindow(x_display, the_win);
446 <        wait_mapped(the_win);
445 >        XMapWindow(x_display, w);
446 >        wait_mapped(w);
447  
448          // Try to create and attach SHM image
449 <        have_shm = false;
350 <        if (mode.depth != VDEPTH_1BIT && local_X11 && XShmQueryExtension(x_display)) {
449 >        if (local_X11 && mode.depth != VDEPTH_1BIT && XShmQueryExtension(x_display)) {
450  
451                  // Create SHM image ("height + 2" for safety)
452                  img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
# Line 397 | Line 496 | static bool init_window(const video_mode
496   #endif
497  
498          // Create GC
499 <        the_gc = XCreateGC(x_display, the_win, 0, 0);
500 <        XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
499 >        gc = XCreateGC(x_display, w, 0, 0);
500 >        XSetState(x_display, gc, black_pixel, white_pixel, GXcopy, AllPlanes);
501  
502          // Create no_cursor
503          mac_cursor = XCreatePixmapCursor(x_display,
504 <           XCreatePixmap(x_display, the_win, 1, 1, 1),
505 <           XCreatePixmap(x_display, the_win, 1, 1, 1),
504 >           XCreatePixmap(x_display, w, 1, 1, 1),
505 >           XCreatePixmap(x_display, w, 1, 1, 1),
506             &black, &white, 0, 0);
507 <        XDefineCursor(x_display, the_win, mac_cursor);
507 >        XDefineCursor(x_display, w, mac_cursor);
508  
509 <        // Add resolution and set VideoMonitor
509 >        // Init blitting routines
510          bool native_byte_order;
511   #ifdef WORDS_BIGENDIAN
512          native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
# Line 417 | Line 516 | static bool init_window(const video_mode
516   #ifdef ENABLE_VOSF
517          Screen_blitter_init(&visualInfo, native_byte_order);
518   #endif
519 +
520 +        // Set VideoMonitor
521          VideoMonitor.mode = mode;
522          set_mac_frame_buffer(mode.depth, native_byte_order);
523 <        return true;
523 >
524 >        // Everything went well
525 >        init_ok = true;
526 > }
527 >
528 > // Close display
529 > driver_window::~driver_window()
530 > {
531 >        if (img)
532 >                XDestroyImage(img);
533 >        if (have_shm) {
534 >                XShmDetach(x_display, &shminfo);
535 >                the_buffer_copy = NULL; // don't free() in driver_base dtor
536 >        }
537 >        if (gc)
538 >                XFreeGC(x_display, gc);
539   }
540  
541 < // Init fbdev DGA display
542 < static bool init_fbdev_dga(const video_mode &mode)
541 >
542 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
543 > /*
544 > *  DGA display driver base class
545 > */
546 >
547 > class driver_dga : public driver_base {
548 > public:
549 >        driver_dga();
550 >        ~driver_dga();
551 >
552 >        void suspend(void);
553 >        void resume(void);
554 >
555 > private:
556 >        Window suspend_win;             // "Suspend" information window
557 >        void *fb_save;                  // Saved frame buffer for suspend/resume
558 > };
559 >
560 > driver_dga::driver_dga()
561 > : suspend_win(0), fb_save(NULL)
562 > {
563 > }
564 >
565 > driver_dga::~driver_dga()
566 > {
567 >        XUngrabPointer(x_display, CurrentTime);
568 >        XUngrabKeyboard(x_display, CurrentTime);
569 > }
570 >
571 > // Suspend emulation
572 > void driver_dga::suspend(void)
573 > {
574 >        // Release ctrl key
575 >        ADBKeyUp(0x36);
576 >        ctrl_down = false;
577 >
578 >        // Lock frame buffer (this will stop the MacOS thread)
579 >        LOCK_FRAME_BUFFER;
580 >
581 >        // Save frame buffer
582 >        fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
583 >        if (fb_save)
584 >                memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
585 >
586 >        // Close full screen display
587 > #ifdef ENABLE_XF86_DGA
588 >        XF86DGADirectVideo(x_display, screen, 0);
589 > #endif
590 >        XUngrabPointer(x_display, CurrentTime);
591 >        XUngrabKeyboard(x_display, CurrentTime);
592 >        XUnmapWindow(x_display, w);
593 >        wait_unmapped(w);
594 >
595 >        // Open "suspend" window
596 >        XSetWindowAttributes wattr;
597 >        wattr.event_mask = KeyPressMask;
598 >        wattr.background_pixel = black_pixel;
599 >                
600 >        suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
601 >                InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
602 >        set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
603 >        set_window_focus(suspend_win);
604 >        XMapWindow(x_display, suspend_win);
605 >        emul_suspended = true;
606 > }
607 >
608 > // Resume emulation
609 > void driver_dga::resume(void)
610   {
611 +        // Close "suspend" window
612 +        XDestroyWindow(x_display, suspend_win);
613 +        XSync(x_display, false);
614 +
615 +        // Reopen full screen display
616 +        XMapRaised(x_display, w);
617 +        wait_mapped(w);
618 +        XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
619 +        XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
620 +        XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
621 + #ifdef ENABLE_XF86_DGA
622 +        XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
623 +        XF86DGASetViewPort(x_display, screen, 0, 0);
624 + #endif
625 +        XSync(x_display, false);
626 +        
627 +        // the_buffer already contains the data to restore. i.e. since a temporary
628 +        // frame buffer is used when VOSF is actually used, fb_save is therefore
629 +        // not necessary.
630 + #ifdef ENABLE_VOSF
631 +        if (use_vosf) {
632 +                LOCK_VOSF;
633 +                PFLAG_SET_ALL;
634 +                UNLOCK_VOSF;
635 +                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
636 +        }
637 + #endif
638 +        
639 +        // Restore frame buffer
640 +        if (fb_save) {
641 + #ifdef ENABLE_VOSF
642 +                // Don't copy fb_save to the temporary frame buffer in VOSF mode
643 +                if (!use_vosf)
644 + #endif
645 +                memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
646 +                free(fb_save);
647 +                fb_save = NULL;
648 +        }
649 +        
650 +        // Unlock frame buffer (and continue MacOS thread)
651 +        UNLOCK_FRAME_BUFFER;
652 +        emul_suspended = false;
653 + }
654 + #endif
655 +
656 +
657   #ifdef ENABLE_FBDEV_DGA
658 + /*
659 + *  fbdev DGA display driver
660 + */
661 +
662 + class driver_fbdev : public driver_dga {
663 + public:
664 +        driver_fbdev(const video_mode &mode);
665 +        ~driver_fbdev();
666 +
667 + private:
668 +        const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices";
669 +        const char FBDEVICE_FILE_NAME[] = "/dev/fb";
670 + };
671 +
672 + // Open display
673 + driver_fbdev::driver_fbdev(const video_mode &mode)
674 + {
675          int width = mode.x, height = mode.y;
676  
677 +        // Set absolute mouse mode
678 +        ADBSetRelMouseMode(false);
679 +        
680          // Find the maximum depth available
681          int ndepths, max_depth(0);
682          int *depths = XListDepths(x_display, screen, &ndepths);
683          if (depths == NULL) {
684                  printf("FATAL: Could not determine the maximal depth available\n");
685 <                return false;
685 >                return;
686          } else {
687                  while (ndepths-- > 0) {
688                          if (depths[ndepths] > max_depth)
# Line 450 | Line 699 | static bool init_fbdev_dga(const video_m
699                  char str[256];
700                  sprintf(str, GetString(STR_NO_FBDEVICE_FILE_ERR), fbd_path ? fbd_path : FBDEVICES_FILE_NAME, strerror(errno));
701                  ErrorAlert(str);
702 <                return false;
702 >                return;
703          }
704          
705          int fb_depth;           // supported depth
# Line 470 | Line 719 | static bool init_fbdev_dga(const video_m
719                          continue;
720                  
721                  if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
722 <                && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) {
722 >                 && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) {
723                          device_found = true;
724                          break;
725                  }
# Line 484 | Line 733 | static bool init_fbdev_dga(const video_m
733                  char str[256];
734                  sprintf(str, GetString(STR_FBDEV_NAME_ERR), fb_name, max_depth);
735                  ErrorAlert(str);
736 <                return false;
736 >                return;
737          }
738          
490        depth = fb_depth;
491        
492        // Set relative mouse mode
493        ADBSetRelMouseMode(false);
494        
739          // Create window
740          XSetWindowAttributes wattr;
741          wattr.event_mask = eventmask = dga_eventmask;
# Line 499 | Line 743 | static bool init_fbdev_dga(const video_m
743          wattr.override_redirect = True;
744          wattr.colormap = cmap[0];
745          
746 <        the_win = XCreateWindow(x_display, rootwin,
746 >        w = XCreateWindow(x_display, rootwin,
747                  0, 0, width, height,
748                  0, xdepth, InputOutput, vis,
749 <                CWEventMask | CWBackPixel | CWOverrideRedirect | (depth == 8 ? CWColormap : 0),
749 >                CWEventMask | CWBackPixel | CWOverrideRedirect | (fb_depth <= 8 ? CWColormap : 0),
750                  &wattr);
751  
752          // Set window name/class
753 <        set_window_name(the_win, STR_WINDOW_TITLE);
753 >        set_window_name(w, STR_WINDOW_TITLE);
754  
755          // Indicate that we want keyboard input
756 <        set_window_focus(the_win);
756 >        set_window_focus(w);
757  
758          // Show window
759 <        XMapRaised(x_display, the_win);
760 <        wait_mapped(the_win);
759 >        XMapRaised(x_display, w);
760 >        wait_mapped(w);
761          
762          // Grab mouse and keyboard
763 <        XGrabKeyboard(x_display, the_win, True,
763 >        XGrabKeyboard(x_display, w, True,
764                  GrabModeAsync, GrabModeAsync, CurrentTime);
765 <        XGrabPointer(x_display, the_win, True,
765 >        XGrabPointer(x_display, w, True,
766                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
767 <                GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
767 >                GrabModeAsync, GrabModeAsync, w, None, CurrentTime);
768          
769          // Calculate bytes per row
770          int bytes_per_row = TrivialBytesPerRow(mode.x, mode.depth);
# Line 531 | Line 775 | static bool init_fbdev_dga(const video_m
775                          char str[256];
776                          sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
777                          ErrorAlert(str);
778 <                        return false;
778 >                        return;
779                  }
780          }
781          
# Line 554 | Line 798 | static bool init_fbdev_dga(const video_m
798   #endif
799          
800          // Set VideoMonitor
801 +        VideoModes[0].bytes_per_row = bytes_per_row;
802 +        VideoModes[0].depth = DepthModeForPixelDepth();
803          VideoMonitor.mode = mode;
804          set_mac_frame_buffer(mode.depth, true);
805 <        return true;
806 < #else
807 <        ErrorAlert("Basilisk II has been compiled with fbdev DGA support disabled.");
562 <        return false;
563 < #endif
805 >
806 >        // Everything went well
807 >        init_ok = true;
808   }
809  
810 < // Init XF86 DGA display
811 < static bool init_xf86_dga(const video_mode &mode)
810 > // Close display
811 > driver_fbdev::~driver_fbdev()
812   {
813 <        int width = mode.x, height = mode.y;
813 > }
814 > #endif
815 >
816  
817   #ifdef ENABLE_XF86_DGA
818 + /*
819 + *  XFree86 DGA display driver
820 + */
821 +
822 + class driver_xf86dga : public driver_dga {
823 + public:
824 +        driver_xf86dga(const video_mode &mode);
825 +        ~driver_xf86dga();
826 +
827 +        void update_palette(void);
828 +        void resume(void);
829 +
830 + private:
831 +        int current_dga_cmap;                                   // Number (0 or 1) of currently installed DGA colormap
832 + };
833 +
834 + // Open display
835 + driver_xf86dga::driver_xf86dga(const video_mode &mode)
836 + : current_dga_cmap(0)
837 + {
838 +        int width = mode.x, height = mode.y;
839 +
840          // Set relative mouse mode
841          ADBSetRelMouseMode(true);
842  
# Line 593 | Line 861 | static bool init_xf86_dga(const video_mo
861          wattr.event_mask = eventmask = dga_eventmask;
862          wattr.override_redirect = True;
863  
864 <        the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
864 >        w = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
865                  InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
866  
867          // Set window name/class
868 <        set_window_name(the_win, STR_WINDOW_TITLE);
868 >        set_window_name(w, STR_WINDOW_TITLE);
869  
870          // Indicate that we want keyboard input
871 <        set_window_focus(the_win);
871 >        set_window_focus(w);
872  
873          // Show window
874 <        XMapRaised(x_display, the_win);
875 <        wait_mapped(the_win);
874 >        XMapRaised(x_display, w);
875 >        wait_mapped(w);
876  
877          // Establish direct screen connection
878 <        XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
878 >        XMoveResizeWindow(x_display, w, 0, 0, width, height);
879          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
880          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
881          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
# Line 620 | Line 888 | static bool init_xf86_dga(const video_mo
888  
889          // Set colormap
890          if (!IsDirectMode(mode)) {
891 <                XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
891 >                XSetWindowColormap(x_display, w, cmap[current_dga_cmap = 0]);
892                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
893          }
894          XSync(x_display, false);
895  
896 <        // Set VideoMonitor
896 >        // Init blitting routines
897          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
630
898   #ifdef VIDEO_VOSF
899   #if REAL_ADDRESSING || DIRECT_ADDRESSING
900          // Screen_blitter_init() returns TRUE if VOSF is mandatory
# Line 646 | Line 913 | static bool init_xf86_dga(const video_mo
913   #endif
914   #endif
915          
916 +        // Set VideoMonitor
917          const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
918          VideoMonitor.mode = mode;
919          set_mac_frame_buffer(mode.depth, true);
920 <        return true;
921 < #else
922 <        ErrorAlert("Basilisk II has been compiled with XF86 DGA support disabled.");
923 <        return false;
920 >
921 >        // Everything went well
922 >        init_ok = true;
923 > }
924 >
925 > // Close display
926 > driver_xf86dga::~driver_xf86dga()
927 > {
928 >        XF86DGADirectVideo(x_display, screen, 0);
929 > #ifdef ENABLE_XF86_VIDMODE
930 >        if (has_vidmode)
931 >                XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
932   #endif
933   }
934  
935 + // Palette has changed
936 + void driver_xf86dga::update_palette(void)
937 + {
938 +        driver_dga::update_palette();
939 +        current_dga_cmap ^= 1;
940 +        if (!IsDirectMode(VideoMonitor.mode) && cmap[current_dga_cmap])
941 +                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
942 + }
943 +
944 + // Resume emulation
945 + void driver_xf86dga::resume(void)
946 + {
947 +        driver_dga::resume();
948 +        if (!IsDirectMode(VideoMonitor.mode))
949 +                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
950 + }
951 + #endif
952 +
953 +
954 + /*
955 + *  Initialization
956 + */
957 +
958   // Init keycode translation table
959   static void keycode_init(void)
960   {
# Line 724 | Line 1023 | static void keycode_init(void)
1023   // Open display for specified mode
1024   static bool video_open(const video_mode &mode)
1025   {
1026 <        // Create color maps for 8 bit mode
728 <        if (!IsDirectMode(mode)) {
729 <                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
730 <                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
731 <                cmap_allocated = true;
732 <                XInstallColormap(x_display, cmap[0]);
733 <                XInstallColormap(x_display, cmap[1]);
734 <        }
735 <
736 <        // Initialize according to display type
1026 >        // Create display driver object of requested type
1027          switch (display_type) {
1028                  case DISPLAY_WINDOW:
1029 <                        if (!init_window(mode))
740 <                                return false;
1029 >                        drv = new driver_window(mode);
1030                          break;
742                case DISPLAY_DGA:
1031   #ifdef ENABLE_FBDEV_DGA
1032 <                        if (!init_fbdev_dga(mode))
1033 < #else
1034 <                        if (!init_xf86_dga(mode))
1032 >                case DISPLAY_DGA:
1033 >                        drv = new driver_fbdev(mode);
1034 >                        break;
1035   #endif
1036 <                                return false;
1036 > #ifdef ENABLE_XF86_DGA
1037 >                case DISPLAY_DGA:
1038 >                        drv = new driver_xf86dga(mode);
1039                          break;
750        }
751
752        // Lock down frame buffer
753        LOCK_FRAME_BUFFER;
754
755 #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
756        // Set variables for UAE memory mapping
757        MacFrameBaseHost = the_buffer;
758        MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
759
760        // No special frame buffer in Classic mode (frame buffer is in Mac RAM)
761        if (classic)
762                MacFrameLayout = FLAYOUT_NONE;
1040   #endif
1041 <
1042 <        InitFrameBufferMapping();
1041 >        }
1042 >        if (drv == NULL)
1043 >                return false;
1044 >        if (!drv->init_ok) {
1045 >                delete drv;
1046 >                drv = NULL;
1047 >                return false;
1048 >        }
1049  
1050   #ifdef ENABLE_VOSF
1051          if (use_vosf) {
1052                  // Initialize the mainBuffer structure
1053                  if (!video_init_buffer()) {
1054 <                        ErrorAlert(GetString(STR_VOSF_INIT_ERR));
1054 >                        ErrorAlert(STR_VOSF_INIT_ERR);
1055                  return false;
1056                  }
1057  
# Line 782 | Line 1065 | static bool video_open(const video_mode
1065          
1066          // Initialize VideoRefresh function
1067          VideoRefreshInit();
1068 <        
1068 >
1069 >        // Lock down frame buffer
1070          XSync(x_display, false);
1071 +        LOCK_FRAME_BUFFER;
1072  
1073   #ifdef HAVE_PTHREADS
1074          // Start redraw/input thread
# Line 804 | Line 1089 | bool VideoInit(bool classic)
1089  
1090   #ifdef ENABLE_VOSF
1091          // Zero the mainBuffer structure
1092 <        mainBuffer.dirtyPages = 0;
1093 <        mainBuffer.pageInfo = 0;
1092 >        mainBuffer.dirtyPages = NULL;
1093 >        mainBuffer.pageInfo = NULL;
1094   #endif
1095          
1096          // Check if X server runs on local machine
# Line 816 | Line 1101 | bool VideoInit(bool classic)
1101          keycode_init();
1102  
1103          // Read prefs
1104 +        frame_skip = PrefsFindInt32("frameskip");
1105          mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1106          mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1107  
# Line 830 | Line 1116 | bool VideoInit(bool classic)
1116          // Frame buffer name
1117          char fb_name[20];
1118          
1119 <        // Could do fbdev dga ?
1119 >        // Could do fbdev DGA?
1120          if ((fbdev_fd = open(FBDEVICE_FILE_NAME, O_RDWR)) != -1)
1121                  has_dga = true;
1122          else
# Line 876 | Line 1162 | bool VideoInit(bool classic)
1162                  case 15:
1163                  case 16:
1164                  case 24:
1165 <                case 32:
1166 <                        color_class = TrueColor;
1165 >                case 32: // Try DirectColor first, as this will allow gamma correction
1166 >                        if (!XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo))
1167 >                                color_class = TrueColor;
1168                          break;
1169                  default:
1170 <                        ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
1170 >                        ErrorAlert(STR_UNSUPP_DEPTH_ERR);
1171                          return false;
1172          }
1173          if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
1174 <                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
1174 >                ErrorAlert(STR_NO_XVISUAL_ERR);
1175                  return false;
1176          }
1177          if (visualInfo.depth != xdepth) {
1178 <                ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
1178 >                ErrorAlert(STR_NO_XVISUAL_ERR);
1179                  return false;
1180          }
1181          vis = visualInfo.visual;
1182  
1183 +        // Create color maps
1184 +        if (color_class == PseudoColor || color_class == DirectColor) {
1185 +                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1186 +                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1187 +        }
1188 +
1189          // Get screen mode from preferences
1190          const char *mode_str;
1191          if (classic_mode)
# Line 909 | Line 1202 | bool VideoInit(bool classic)
1202   #ifdef ENABLE_FBDEV_DGA
1203                  } else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
1204                          display_type = DISPLAY_DGA;
1205 <                        default_width = -1; default_height = -1;
1206 < #else
1205 >                        default_width = -1; default_height = -1; // use entire screen
1206 > #endif
1207 > #ifdef ENABLE_XF86_DGA
1208                  } else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2) {
1209                          display_type = DISPLAY_DGA;
1210   #endif
# Line 980 | Line 1274 | static void video_close(void)
1274  
1275          // Unlock frame buffer
1276          UNLOCK_FRAME_BUFFER;
1277 +        XSync(x_display, false);
1278  
984        // Close window and server connection
985        if (x_display != NULL) {
986                XSync(x_display, false);
987
988 #ifdef ENABLE_XF86_DGA
989                if (display_type == DISPLAY_DGA) {
990                        XF86DGADirectVideo(x_display, screen, 0);
991                        XUngrabPointer(x_display, CurrentTime);
992                        XUngrabKeyboard(x_display, CurrentTime);
993                }
994 #endif
995
996 #ifdef ENABLE_XF86_VIDMODE
997                if (has_vidmode && display_type == DISPLAY_DGA)
998                        XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
999 #endif
1000
1001 #ifdef ENABLE_FBDEV_DGA
1002                if (display_type == DISPLAY_DGA) {
1003                        XUngrabPointer(x_display, CurrentTime);
1004                        XUngrabKeyboard(x_display, CurrentTime);
1005                        close(fbdev_fd);
1006                }
1007 #endif
1008
1009                XFlush(x_display);
1010                XSync(x_display, false);
1011                XUnmapWindow(x_display, the_win);
1012                wait_unmapped(the_win);
1013                XDestroyWindow(x_display, the_win);
1014
1015                if (have_shm) {
1016                        XDestroyImage(img);
1017                        XShmDetach(x_display, &shminfo);
1018                        have_shm = false;
1019                } else {
1020                        //!! free img
1021                }
1022                //!! free the_gc
1023
1024                if (cmap_allocated) {
1025                        XFreeColormap(x_display, cmap[0]);
1026                        XFreeColormap(x_display, cmap[1]);
1027                        cmap_allocated = false;
1028                }
1029                
1030                if (!use_vosf) {
1031                        if (the_buffer) {
1032                                free(the_buffer);
1033                                the_buffer = NULL;
1034                        }
1035
1036                        if (!have_shm && the_buffer_copy) {
1037                                free(the_buffer_copy);
1038                                the_buffer_copy = NULL;
1039                        }
1040                }
1041 #ifdef ENABLE_VOSF
1042                else {
1043                        //!! uninstall SEGV handler?
1044
1045                        if (the_buffer != (uint8 *)VM_MAP_FAILED) {
1046                                vm_release(the_buffer, the_buffer_size);
1047                                the_buffer = 0;
1048                        }
1049                        
1050                        if (the_buffer_copy != (uint8 *)VM_MAP_FAILED) {
1051                                vm_release(the_buffer_copy, the_buffer_size);
1052                                the_buffer_copy = 0;
1053                        }
1054                }
1055 #endif
1056        }
1057        
1279   #ifdef ENABLE_VOSF
1280 +        // Deinitialize VOSF
1281          if (use_vosf) {
1060                // Clear mainBuffer data
1282                  if (mainBuffer.pageInfo) {
1283                          free(mainBuffer.pageInfo);
1284 <                        mainBuffer.pageInfo = 0;
1284 >                        mainBuffer.pageInfo = NULL;
1285                  }
1065
1286                  if (mainBuffer.dirtyPages) {
1287                          free(mainBuffer.dirtyPages);
1288 <                        mainBuffer.dirtyPages = 0;
1288 >                        mainBuffer.dirtyPages = NULL;
1289                  }
1290          }
1291   #endif
1292 +
1293 +        // Close display
1294 +        delete drv;
1295 +        drv = NULL;
1296   }
1297  
1298   void VideoExit(void)
1299   {
1300 +        // Close display
1301          video_close();
1302 +
1303 +        // Free colormaps
1304 +        if (cmap[0]) {
1305 +                XFreeColormap(x_display, cmap[0]);
1306 +                cmap[0] = 0;
1307 +        }
1308 +        if (cmap[1]) {
1309 +                XFreeColormap(x_display, cmap[1]);
1310 +                cmap[1] = 0;
1311 +        }
1312 +
1313 + #ifdef ENABLE_XF86_VIDMODE
1314 +        // Free video mode list
1315 +        if (x_video_modes) {
1316 +                XFree(x_video_modes);
1317 +                x_video_modes = NULL;
1318 +        }
1319 + #endif
1320 +
1321 + #ifdef ENABLE_FBDEV_DGA
1322 +        // Close framebuffer device
1323 +        if (fbdev_fd >= 0) {
1324 +                close(fbdev_fd);
1325 +                fbdev_fd = -1;
1326 +        }
1327 + #endif
1328   }
1329  
1330  
# Line 1084 | Line 1335 | void VideoExit(void)
1335   void VideoQuitFullScreen(void)
1336   {
1337          D(bug("VideoQuitFullScreen()\n"));
1338 <        if (display_type == DISPLAY_DGA)
1088 <                quit_full_screen = true;
1338 >        quit_full_screen = true;
1339   }
1340  
1341  
# Line 1116 | Line 1366 | void video_set_palette(uint8 *pal)
1366  
1367          // Convert colors to XColor array
1368          for (int i=0; i<256; i++) {
1119                palette[i].pixel = i;
1369                  palette[i].red = pal[i*3] * 0x0101;
1370                  palette[i].green = pal[i*3+1] * 0x0101;
1371                  palette[i].blue = pal[i*3+2] * 0x0101;
1372 +                palette[i].pixel = i;
1373                  palette[i].flags = DoRed | DoGreen | DoBlue;
1374          }
1375  
# Line 1136 | Line 1386 | void video_set_palette(uint8 *pal)
1386  
1387   void video_switch_to_mode(const video_mode &mode)
1388   {
1389 +        // Close and reopen display
1390          video_close();
1391          video_open(mode);
1141 }
1142
1143
1144 /*
1145 *  Suspend/resume emulator
1146 */
1147
1148 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1149 static void suspend_emul(void)
1150 {
1151        if (display_type == DISPLAY_DGA) {
1152                // Release ctrl key
1153                ADBKeyUp(0x36);
1154                ctrl_down = false;
1155
1156                // Lock frame buffer (this will stop the MacOS thread)
1157                LOCK_FRAME_BUFFER;
1158
1159                // Save frame buffer
1160                fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
1161                if (fb_save)
1162                        memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
1163
1164                // Close full screen display
1165 #ifdef ENABLE_XF86_DGA
1166                XF86DGADirectVideo(x_display, screen, 0);
1167 #endif
1168                XUngrabPointer(x_display, CurrentTime);
1169                XUngrabKeyboard(x_display, CurrentTime);
1170                XUnmapWindow(x_display, the_win);
1171                wait_unmapped(the_win);
1172
1173                // Open "suspend" window
1174                XSetWindowAttributes wattr;
1175                wattr.event_mask = KeyPressMask;
1176                wattr.background_pixel = black_pixel;
1177                
1178                suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1179                        InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
1180                set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
1181                set_window_focus(suspend_win);
1182                XMapWindow(x_display, suspend_win);
1183                emul_suspended = true;
1184        }
1185 }
1186
1187 static void resume_emul(void)
1188 {
1189        // Close "suspend" window
1190        XDestroyWindow(x_display, suspend_win);
1191        XSync(x_display, false);
1392  
1393 <        // Reopen full screen display
1394 <        XMapRaised(x_display, the_win);
1395 <        wait_mapped(the_win);
1196 <        XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1197 <        XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1198 <        XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1199 < #ifdef ENABLE_XF86_DGA
1200 <        XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1201 <        XF86DGASetViewPort(x_display, screen, 0, 0);
1202 < #endif
1203 <        XSync(x_display, false);
1204 <        
1205 <        // the_buffer already contains the data to restore. i.e. since a temporary
1206 <        // frame buffer is used when VOSF is actually used, fb_save is therefore
1207 <        // not necessary.
1208 < #ifdef ENABLE_VOSF
1209 <        if (use_vosf) {
1210 <                LOCK_VOSF;
1211 <                PFLAG_SET_ALL;
1212 <                UNLOCK_VOSF;
1213 <                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1214 <        }
1215 < #endif
1216 <        
1217 <        // Restore frame buffer
1218 <        if (fb_save) {
1219 < #ifdef ENABLE_VOSF
1220 <                // Don't copy fb_save to the temporary frame buffer in VOSF mode
1221 <                if (!use_vosf)
1222 < #endif
1223 <                memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
1224 <                free(fb_save);
1225 <                fb_save = NULL;
1393 >        if (drv == NULL) {
1394 >                ErrorAlert(STR_OPEN_WINDOW_ERR);
1395 >                QuitEmulator();
1396          }
1227        
1228 #ifdef ENABLE_XF86_DGA
1229        if (!IsDirectMode(VideoMonitor.mode))
1230                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1231 #endif
1232
1233        // Unlock frame buffer (and continue MacOS thread)
1234        UNLOCK_FRAME_BUFFER;
1235        emul_suspended = false;
1397   }
1237 #endif
1398  
1399  
1400   /*
# Line 1295 | Line 1455 | static int kc_decode(KeySym ks)
1455                  case XK_slash: case XK_question: return 0x2c;
1456  
1457   #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1458 <                case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1458 >                case XK_Tab: if (ctrl_down) {drv->suspend(); return -1;} else return 0x30;
1459   #else
1460                  case XK_Tab: return 0x30;
1461   #endif
# Line 1470 | Line 1630 | static void handle_events(void)
1630                                                  if (code == 0x36)
1631                                                          ctrl_down = true;
1632                                          } else {
1473 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1633                                                  if (code == 0x31)
1634 <                                                        resume_emul();  // Space wakes us up
1476 < #endif
1634 >                                                        drv->resume();  // Space wakes us up
1635                                          }
1636                                  }
1637                                  break;
# Line 1533 | Line 1691 | static void handle_events(void)
1691   */
1692  
1693   // Dynamic display update (variable frame rate for each box)
1694 < static void update_display_dynamic(int ticker)
1694 > static void update_display_dynamic(int ticker, driver_window *drv)
1695   {
1696          int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1697          int xil = 0;
# Line 1596 | Line 1754 | static void update_display_dynamic(int t
1754                                          for (y2=0; y2 < yil; y2++, i += bytes_per_row)
1755                                                  memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
1756                                          if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
1757 <                                                if (have_shm)
1758 <                                                        XShmPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
1757 >                                                if (drv->have_shm)
1758 >                                                        XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
1759                                                  else
1760 <                                                        XPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil);
1760 >                                                        XPutImage(x_display, drv->w, drv->gc, drv->img, xi * 8, yi, xi * 8, yi, xil * 8, yil);
1761                                          } else {
1762 <                                                if (have_shm)
1763 <                                                        XShmPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0);
1762 >                                                if (drv->have_shm)
1763 >                                                        XShmPutImage(x_display, drv->w, drv->gc, drv->img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0);
1764                                                  else
1765 <                                                        XPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil);
1765 >                                                        XPutImage(x_display, drv->w, drv->gc, drv->img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil);
1766                                          }
1767                                          xil = 0;
1768                                  }
# Line 1623 | Line 1781 | static void update_display_dynamic(int t
1781   }
1782  
1783   // Static display update (fixed frame rate, but incremental)
1784 < static void update_display_static(void)
1784 > static void update_display_static(driver_window *drv)
1785   {
1786          // Incremental update code
1787          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
# Line 1729 | Line 1887 | static void update_display_static(void)
1887  
1888          // Refresh display
1889          if (high && wide) {
1890 <                if (have_shm)
1891 <                        XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1890 >                if (drv->have_shm)
1891 >                        XShmPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high, 0);
1892                  else
1893 <                        XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1893 >                        XPutImage(x_display, drv->w, drv->gc, drv->img, x1, y1, x1, y1, wide, high);
1894          }
1895   }
1896  
# Line 1749 | Line 1907 | static void update_display_static(void)
1907  
1908   static inline void possibly_quit_dga_mode()
1909   {
1752 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1910          // Quit DGA mode if requested
1911          if (quit_full_screen) {
1912                  quit_full_screen = false;
1913 < #ifdef ENABLE_XF86_DGA
1914 <                XF86DGADirectVideo(x_display, screen, 0);
1758 < #endif
1759 <                XUngrabPointer(x_display, CurrentTime);
1760 <                XUngrabKeyboard(x_display, CurrentTime);
1761 <                XUnmapWindow(x_display, the_win);
1762 <                wait_unmapped(the_win);
1913 >                delete drv;
1914 >                drv = NULL;
1915          }
1764 #endif
1916   }
1917  
1918 < static inline void handle_palette_changes(int display_type)
1918 > static inline void handle_palette_changes(void)
1919   {
1920          LOCK_PALETTE;
1921  
1922          if (palette_changed) {
1923                  palette_changed = false;
1924 <                if (!IsDirectMode(VideoMonitor.mode)) {
1774 <                        XStoreColors(x_display, cmap[0], palette, 256);
1775 <                        XStoreColors(x_display, cmap[1], palette, 256);
1776 <                        XSync(x_display, false);
1777 <                                
1778 < #ifdef ENABLE_XF86_DGA
1779 <                        if (display_type == DISPLAY_DGA) {
1780 <                                current_dga_cmap ^= 1;
1781 <                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1782 <                        }
1783 < #endif
1784 <                }
1924 >                drv->update_palette();
1925          }
1926  
1927          UNLOCK_PALETTE;
# Line 1796 | Line 1936 | static void video_refresh_dga(void)
1936          handle_events();
1937          
1938          // Handle palette changes
1939 <        handle_palette_changes(DISPLAY_DGA);
1939 >        handle_palette_changes();
1940   }
1941  
1942   #ifdef ENABLE_VOSF
# Line 1810 | Line 1950 | static void video_refresh_dga_vosf(void)
1950          handle_events();
1951          
1952          // Handle palette changes
1953 <        handle_palette_changes(DISPLAY_DGA);
1953 >        handle_palette_changes();
1954          
1955          // Update display (VOSF variant)
1956          static int tick_counter = 0;
# Line 1834 | Line 1974 | static void video_refresh_window_vosf(vo
1974          handle_events();
1975          
1976          // Handle palette changes
1977 <        handle_palette_changes(DISPLAY_WINDOW);
1977 >        handle_palette_changes();
1978          
1979          // Update display (VOSF variant)
1980          static int tick_counter = 0;
# Line 1842 | Line 1982 | static void video_refresh_window_vosf(vo
1982                  tick_counter = 0;
1983                  if (mainBuffer.dirty) {
1984                          LOCK_VOSF;
1985 <                        update_display_window_vosf();
1985 >                        update_display_window_vosf(static_cast<driver_window *>(drv));
1986                          UNLOCK_VOSF;
1987                          XSync(x_display, false); // Let the server catch up
1988                  }
# Line 1856 | Line 1996 | static void video_refresh_window_static(
1996          handle_events();
1997          
1998          // Handle_palette changes
1999 <        handle_palette_changes(DISPLAY_WINDOW);
1999 >        handle_palette_changes();
2000          
2001          // Update display (static variant)
2002          static int tick_counter = 0;
2003          if (++tick_counter >= frame_skip) {
2004                  tick_counter = 0;
2005 <                update_display_static();
2005 >                update_display_static(static_cast<driver_window *>(drv));
2006          }
2007   }
2008  
# Line 1872 | Line 2012 | static void video_refresh_window_dynamic
2012          handle_events();
2013          
2014          // Handle_palette changes
2015 <        handle_palette_changes(DISPLAY_WINDOW);
2015 >        handle_palette_changes();
2016          
2017          // Update display (dynamic variant)
2018          static int tick_counter = 0;
2019          tick_counter++;
2020 <        update_display_dynamic(tick_counter);
2020 >        update_display_dynamic(tick_counter, static_cast<driver_window *>(drv));
2021   }
2022  
2023  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines