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.36 by gbeauche, 2001-01-28T14:05:19Z vs.
Revision 1.43 by gbeauche, 2001-06-28T22:06:18Z

# Line 52 | Line 52
52   # include <sys/mman.h>
53   #endif
54  
55 #ifdef ENABLE_VOSF
56 # include <unistd.h>
57 # include <signal.h>
58 # include <fcntl.h>
59 # include <sys/mman.h>
60 #endif
61
55   #include "cpu_emulation.h"
56   #include "main.h"
57   #include "adb.h"
# Line 93 | Line 86 | static uint8 *the_buffer;                                                      // Mac f
86  
87   #ifdef HAVE_PTHREADS
88   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
89 < static volatile bool redraw_thread_cancel = false;      // Flag: Cancel Redraw thread
89 > static volatile bool redraw_thread_cancel;                      // Flag: Cancel Redraw thread
90   static pthread_t redraw_thread;                                         // Redraw thread
91   #endif
92  
# Line 114 | Line 107 | static int keycode_table[256];                                         // X
107   // X11 variables
108   static int screen;                                                                      // Screen number
109   static int xdepth;                                                                      // Depth of X screen
117 static int depth;                                                                       // Depth of Mac frame buffer
110   static Window rootwin, the_win;                                         // Root window and our window
111   static XVisualInfo visualInfo;
112   static Visual *vis;
113   static Colormap cmap[2];                                                        // Two colormaps (DGA) for 8-bit mode
114 + static bool cmap_allocated = false;
115   static XColor black, white;
116   static unsigned long black_pixel, white_pixel;
117   static int eventmask;
# Line 178 | Line 171 | static bool use_vosf = true;                                           // Fla
171   static const bool use_vosf = false;                                     // Flag: VOSF enabled
172   #endif
173  
181 #ifdef ENABLE_VOSF
182 // Variables for Video on SEGV support (taken from the Win32 port)
183 static uint8 *the_host_buffer;                                          // Host frame buffer in VOSF mode
184 static uint32 the_buffer_size;                                          // Size of allocated the_buffer
185
186 struct ScreenPageInfo {
187    int top, bottom;                    // Mapping between this virtual page and Mac scanlines
188 };
189
190 struct ScreenInfo {
191    uint32 memBase;                             // Real start address
192    uint32 memStart;                    // Start address aligned to page boundary
193    uint32 memEnd;                              // Address of one-past-the-end of the screen
194    uint32 memLength;                   // Length of the memory addressed by the screen pages
195    
196    uint32 pageSize;                    // Size of a page
197    int pageBits;                               // Shift count to get the page number
198    uint32 pageCount;                   // Number of pages allocated to the screen
199    
200        bool dirty;                                     // Flag: set if the frame buffer was touched
201    char * dirtyPages;                  // Table of flags set if page was altered
202    ScreenPageInfo * pageInfo;  // Table of mappings page -> Mac scanlines
203 };
204
205 static ScreenInfo mainBuffer;
206
207 #define PFLAG_SET_VALUE                 0x00
208 #define PFLAG_CLEAR_VALUE               0x01
209 #define PFLAG_SET_VALUE_4               0x00000000
210 #define PFLAG_CLEAR_VALUE_4             0x01010101
211 #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = PFLAG_SET_VALUE
212 #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = PFLAG_CLEAR_VALUE
213 #define PFLAG_ISSET(page)               (mainBuffer.dirtyPages[page] == PFLAG_SET_VALUE)
214 #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] != PFLAG_SET_VALUE)
215
216 #ifdef UNALIGNED_PROFITABLE
217 # define PFLAG_ISSET_4(page)    (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_SET_VALUE_4)
218 # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + (page))) == PFLAG_CLEAR_VALUE_4)
219 #else
220 # define PFLAG_ISSET_4(page) \
221                PFLAG_ISSET(page  ) && PFLAG_ISSET(page+1) \
222        &&      PFLAG_ISSET(page+2) && PFLAG_ISSET(page+3)
223 # define PFLAG_ISCLEAR_4(page) \
224                PFLAG_ISCLEAR(page  ) && PFLAG_ISCLEAR(page+1) \
225        &&      PFLAG_ISCLEAR(page+2) && PFLAG_ISCLEAR(page+3)
226 #endif
227
228 // Set the selected page range [ first_page, last_page [ into the SET state
229 #define PFLAG_SET_RANGE(first_page, last_page) \
230        memset(mainBuffer.dirtyPages + (first_page), PFLAG_SET_VALUE, \
231                (last_page) - (first_page))
232
233 // Set the selected page range [ first_page, last_page [ into the CLEAR state
234 #define PFLAG_CLEAR_RANGE(first_page, last_page) \
235        memset(mainBuffer.dirtyPages + (first_page), PFLAG_CLEAR_VALUE, \
236                (last_page) - (first_page))
237
238 #define PFLAG_SET_ALL do { \
239        PFLAG_SET_RANGE(0, mainBuffer.pageCount); \
240        mainBuffer.dirty = true; \
241 } while (0)
242
243 #define PFLAG_CLEAR_ALL do { \
244        PFLAG_CLEAR_RANGE(0, mainBuffer.pageCount); \
245        mainBuffer.dirty = false; \
246 } while (0)
247
248 // Set the following macro definition to 1 if your system
249 // provides a really fast strchr() implementation
250 //#define HAVE_FAST_STRCHR 0
251
252 static inline int find_next_page_set(int page)
253 {
254 #if HAVE_FAST_STRCHR
255        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_SET_VALUE);
256        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
257 #else
258        while (PFLAG_ISCLEAR_4(page))
259                page += 4;
260        while (PFLAG_ISCLEAR(page))
261                page++;
262        return page;
263 #endif
264 }
265
266 static inline int find_next_page_clear(int page)
267 {
268 #if HAVE_FAST_STRCHR
269        char *match = strchr(mainBuffer.dirtyPages + page, PFLAG_CLEAR_VALUE);
270        return match ? match - mainBuffer.dirtyPages : mainBuffer.pageCount;
271 #else
272        while (PFLAG_ISSET_4(page))
273                page += 4;
274        while (PFLAG_ISSET(page))
275                page++;
276        return page;
277 #endif
278 }
279
280 static int zero_fd = -1;
281 static bool Screen_fault_handler_init();
282 static struct sigaction vosf_sa;
283
284 #ifdef HAVE_PTHREADS
285 static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer (dirtyPages in fact)
286 #define LOCK_VOSF pthread_mutex_lock(&vosf_lock);
287 #define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock);
288 #else
289 #define LOCK_VOSF
290 #define UNLOCK_VOSF
291 #endif
292
293 static int log_base_2(uint32 x)
294 {
295        uint32 mask = 0x80000000;
296        int l = 31;
297        while (l >= 0 && (x & mask) == 0) {
298                mask >>= 1;
299                l--;
300        }
301        return l;
302 }
303 #endif /* ENABLE_VOSF */
304
174   // VideoRefresh function
175 < void VideoRefreshInit(void);
175 > static void VideoRefreshInit(void);
176   static void (*video_refresh)(void);
177  
178   // Prototypes
179   static void *redraw_func(void *arg);
180   static int event2keycode(XKeyEvent &ev);
181  
313
182   // From main_unix.cpp
183   extern char *x_display_name;
184   extern Display *x_display;
# Line 318 | Line 186 | extern Display *x_display;
186   // From sys_unix.cpp
187   extern void SysMountFirstFloppy(void);
188  
189 +
190   #ifdef ENABLE_VOSF
191   # include "video_vosf.h"
192   #endif
# Line 327 | Line 196 | extern void SysMountFirstFloppy(void);
196   *  Initialization
197   */
198  
199 < // Set VideoMonitor according to video mode
200 < void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order)
199 > // Add mode to list of supported modes
200 > static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
201 > {
202 >        video_mode mode;
203 >        mode.x = width;
204 >        mode.y = height;
205 >        mode.resolution_id = resolution_id;
206 >        mode.bytes_per_row = bytes_per_row;
207 >        mode.depth = depth;
208 >        VideoModes.push_back(mode);
209 > }
210 >
211 > // Set Mac frame layout and base address (uses the_buffer/MacFrameBaseMac)
212 > static void set_mac_frame_buffer(video_depth depth, bool native_byte_order)
213   {
214   #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
215          int layout = FLAYOUT_DIRECT;
216 <        switch (depth) {
217 <                case 1:
218 <                        layout = FLAYOUT_DIRECT;
219 <                        break;
339 <                case 8:
340 <                        layout = FLAYOUT_DIRECT;
341 <                        break;
342 <                case 15:
343 <                        layout = FLAYOUT_HOST_555;
344 <                        break;
345 <                case 16:
346 <                        layout = FLAYOUT_HOST_565;
347 <                        break;
348 <                case 24:
349 <                case 32:
350 <                        layout = FLAYOUT_HOST_888;
351 <                        break;
352 <        }
216 >        if (depth == VDEPTH_16BIT)
217 >                layout = (xdepth == 15) ? FLAYOUT_HOST_555 : FLAYOUT_HOST_565;
218 >        else if (depth == VDEPTH_32BIT)
219 >                layour = (xdepth == 24) ? FLAYOUT_HOST_888 : FLAYOUT_DIRECT;
220          if (native_byte_order)
221                  MacFrameLayout = layout;
222          else
223                  MacFrameLayout = FLAYOUT_DIRECT;
224 +        VideoMonitor.mac_frame_base = MacFrameBaseMac;
225 + #else
226 +        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
227 +        D(bug("Host frame buffer = %p, ", the_buffer));
228   #endif
229 <        switch (depth) {
359 <                case 1:
360 <                        VideoMonitor.mode = VMODE_1BIT;
361 <                        break;
362 <                case 8:
363 <                        VideoMonitor.mode = VMODE_8BIT;
364 <                        break;
365 <                case 15:
366 <                        VideoMonitor.mode = VMODE_16BIT;
367 <                        break;
368 <                case 16:
369 <                        VideoMonitor.mode = VMODE_16BIT;
370 <                        break;
371 <                case 24:
372 <                case 32:
373 <                        VideoMonitor.mode = VMODE_32BIT;
374 <                        break;
375 <        }
376 <        VideoMonitor.x = width;
377 <        VideoMonitor.y = height;
378 <        VideoMonitor.bytes_per_row = bytes_per_row;
229 >        D(bug("VideoMonitor.mac_frame_base = %08x\n", VideoMonitor.mac_frame_base));
230   }
231  
232   // Set window name and class
# Line 446 | Line 297 | static int error_handler(Display *d, XEr
297   }
298  
299   // Init window mode
300 < static bool init_window(int width, int height)
300 > static bool init_window(const video_mode &mode)
301   {
302 +        int width = mode.x, height = mode.y;
303          int aligned_width = (width + 15) & ~15;
304          int aligned_height = (height + 15) & ~15;
305  
# Line 464 | Line 316 | static bool init_window(int width, int h
316          wattr.colormap = cmap[0];
317  
318          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
319 <                InputOutput, vis, CWEventMask | CWBackPixel | (depth == 8 ? CWColormap : 0), &wattr);
319 >                InputOutput, vis, CWEventMask | CWBackPixel | ((IsDirectMode(mode) || mode.depth == VDEPTH_1BIT) ? 0 : CWColormap), &wattr);
320  
321          // Set window name/class
322          set_window_name(the_win, STR_WINDOW_TITLE);
# Line 495 | Line 347 | static bool init_window(int width, int h
347  
348          // Try to create and attach SHM image
349          have_shm = false;
350 <        if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
350 >        if (mode.depth != VDEPTH_1BIT && local_X11 && XShmQueryExtension(x_display)) {
351  
352                  // Create SHM image ("height + 2" for safety)
353 <                img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
353 >                img = XShmCreateImage(x_display, vis, mode.depth == VDEPTH_1BIT ? 1 : xdepth, mode.depth == VDEPTH_1BIT ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
354                  shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
355                  the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
356                  shminfo.shmaddr = img->data = (char *)the_buffer_copy;
# Line 522 | Line 374 | static bool init_window(int width, int h
374          
375          // Create normal X image if SHM doesn't work ("height + 2" for safety)
376          if (!have_shm) {
377 <                int bytes_per_row = aligned_width;
526 <                switch (depth) {
527 <                        case 1:
528 <                                bytes_per_row /= 8;
529 <                                break;
530 <                        case 15:
531 <                        case 16:
532 <                                bytes_per_row *= 2;
533 <                                break;
534 <                        case 24:
535 <                        case 32:
536 <                                bytes_per_row *= 4;
537 <                                break;
538 <                }
377 >                int bytes_per_row = TrivialBytesPerRow(aligned_width, mode.depth);
378                  the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
379 <                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
379 >                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);
380          }
381  
382          // 1-Bit mode is big-endian
383 <        if (depth == 1) {
383 >        if (mode.depth == VDEPTH_1BIT) {
384                  img->byte_order = MSBFirst;
385                  img->bitmap_bit_order = MSBFirst;
386          }
387  
388   #ifdef ENABLE_VOSF
389 <        // Allocate a page-aligned chunk of memory for frame buffer
551 <        the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line);
389 >        // Allocate memory for frame buffer (SIZE is extended to page-boundary)
390          the_host_buffer = the_buffer_copy;
391 <        
392 <        the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size);
393 <        memset(the_buffer_copy, 0, the_buffer_size);
556 <        
557 <        the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
558 <        memset(the_buffer, 0, the_buffer_size);
391 >        the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
392 >        the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
393 >        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
394   #else
395          // Allocate memory for frame buffer
396          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
# Line 572 | Line 407 | static bool init_window(int width, int h
407             &black, &white, 0, 0);
408          XDefineCursor(x_display, the_win, mac_cursor);
409  
410 <        // Set VideoMonitor
410 >        // Add resolution and set VideoMonitor
411          bool native_byte_order;
412   #ifdef WORDS_BIGENDIAN
413          native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
# Line 582 | Line 417 | static bool init_window(int width, int h
417   #ifdef ENABLE_VOSF
418          Screen_blitter_init(&visualInfo, native_byte_order);
419   #endif
420 <        set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
421 <        
587 < #if REAL_ADDRESSING || DIRECT_ADDRESSING
588 <        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
589 < #else
590 <        VideoMonitor.mac_frame_base = MacFrameBaseMac;
591 < #endif
420 >        VideoMonitor.mode = mode;
421 >        set_mac_frame_buffer(mode.depth, native_byte_order);
422          return true;
423   }
424  
425   // Init fbdev DGA display
426 < static bool init_fbdev_dga(char *in_fb_name)
426 > static bool init_fbdev_dga(const video_mode &mode)
427   {
428   #ifdef ENABLE_FBDEV_DGA
429 +        int width = mode.x, height = mode.y;
430 +
431          // Find the maximum depth available
432          int ndepths, max_depth(0);
433          int *depths = XListDepths(x_display, screen, &ndepths);
# Line 638 | Line 470 | static bool init_fbdev_dga(char *in_fb_n
470                          continue;
471                  
472                  if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
473 <                && (strcmp(fb_name, in_fb_name) == 0) && (fb_depth == max_depth)) {
473 >                && (strcmp(fb_name, fb_name) == 0) && (fb_depth == max_depth)) {
474                          device_found = true;
475                          break;
476                  }
# Line 650 | Line 482 | static bool init_fbdev_dga(char *in_fb_n
482          // Frame buffer name not found ? Then, display warning
483          if (!device_found) {
484                  char str[256];
485 <                sprintf(str, GetString(STR_FBDEV_NAME_ERR), in_fb_name, max_depth);
485 >                sprintf(str, GetString(STR_FBDEV_NAME_ERR), fb_name, max_depth);
486                  ErrorAlert(str);
487                  return false;
488          }
489          
490 <        int width = DisplayWidth(x_display, screen);
659 <        int height = DisplayHeight(x_display, screen);
660 <        depth = fb_depth; // max_depth
490 >        depth = fb_depth;
491          
492          // Set relative mouse mode
493          ADBSetRelMouseMode(false);
# Line 692 | Line 522 | static bool init_fbdev_dga(char *in_fb_n
522                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
523                  GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
524          
525 <        // Set VideoMonitor
526 <        int bytes_per_row = width;
697 <        switch (depth) {
698 <                case 1:
699 <                        bytes_per_row = ((width | 7) & ~7) >> 3;
700 <                        break;
701 <                case 15:
702 <                case 16:
703 <                        bytes_per_row *= 2;
704 <                        break;
705 <                case 24:
706 <                case 32:
707 <                        bytes_per_row *= 4;
708 <                        break;
709 <        }
525 >        // Calculate bytes per row
526 >        int bytes_per_row = TrivialBytesPerRow(mode.x, mode.depth);
527          
528 +        // Map frame buffer
529          if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
530                  if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
531                          char str[256];
# Line 724 | Line 542 | static bool init_fbdev_dga(char *in_fb_n
542          use_vosf = Screen_blitter_init(&visualInfo, true);
543          
544          if (use_vosf) {
545 <                the_host_buffer = the_buffer;
546 <                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
547 <                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
548 <                memset(the_buffer_copy, 0, the_buffer_size);
549 <                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
732 <                memset(the_buffer, 0, the_buffer_size);
545 >          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
546 >          the_host_buffer = the_buffer;
547 >          the_buffer_size = page_extend((height + 2) * bytes_per_row);
548 >          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
549 >          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
550          }
551   #else
552          use_vosf = false;
553   #endif
554   #endif
555          
556 <        set_video_monitor(width, height, bytes_per_row, true);
557 < #if REAL_ADDRESSING || DIRECT_ADDRESSING
558 <        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
742 < #else
743 <        VideoMonitor.mac_frame_base = MacFrameBaseMac;
744 < #endif
556 >        // Set VideoMonitor
557 >        VideoMonitor.mode = mode;
558 >        set_mac_frame_buffer(mode.depth, true);
559          return true;
560   #else
561          ErrorAlert("Basilisk II has been compiled with fbdev DGA support disabled.");
# Line 750 | Line 564 | static bool init_fbdev_dga(char *in_fb_n
564   }
565  
566   // Init XF86 DGA display
567 < static bool init_xf86_dga(int width, int height)
567 > static bool init_xf86_dga(const video_mode &mode)
568   {
569 +        int width = mode.x, height = mode.y;
570 +
571   #ifdef ENABLE_XF86_DGA
572          // Set relative mouse mode
573          ADBSetRelMouseMode(true);
# Line 803 | Line 619 | static bool init_xf86_dga(int width, int
619          XF86DGASetVidPage(x_display, screen, 0);
620  
621          // Set colormap
622 <        if (depth == 8) {
622 >        if (!IsDirectMode(mode)) {
623                  XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
624                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
625          }
626          XSync(x_display, false);
627  
628          // Set VideoMonitor
629 <        int bytes_per_row = (v_width + 7) & ~7;
630 <        switch (depth) {
631 <                case 1:
816 <                        bytes_per_row /= 8;
817 <                        break;
818 <                case 15:
819 <                case 16:
820 <                        bytes_per_row *= 2;
821 <                        break;
822 <                case 24:
823 <                case 32:
824 <                        bytes_per_row *= 4;
825 <                        break;
826 <        }
827 <        
629 >        int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, mode.depth);
630 >
631 > #ifdef VIDEO_VOSF
632   #if REAL_ADDRESSING || DIRECT_ADDRESSING
633          // Screen_blitter_init() returns TRUE if VOSF is mandatory
634          // i.e. the framebuffer update function is not Blit_Copy_Raw
635          use_vosf = Screen_blitter_init(&visualInfo, true);
636          
637          if (use_vosf) {
638 <                the_host_buffer = the_buffer;
639 <                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
640 <                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
641 <                memset(the_buffer_copy, 0, the_buffer_size);
642 <                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
839 <                memset(the_buffer, 0, the_buffer_size);
638 >          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
639 >          the_host_buffer = the_buffer;
640 >          the_buffer_size = page_extend((height + 2) * bytes_per_row);
641 >          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
642 >          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
643          }
644 < #elif defined(ENABLE_VOSF)
842 <        // The UAE memory handlers will already handle color conversion, if needed.
644 > #else
645          use_vosf = false;
646   #endif
845        
846        set_video_monitor(width, height, bytes_per_row, true);
847 #if REAL_ADDRESSING || DIRECT_ADDRESSING
848        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
849 //      MacFrameLayout = FLAYOUT_DIRECT;
850 #else
851        VideoMonitor.mac_frame_base = MacFrameBaseMac;
647   #endif
648 +        
649 +        const_cast<video_mode *>(&mode)->bytes_per_row = bytes_per_row;
650 +        VideoMonitor.mode = mode;
651 +        set_mac_frame_buffer(mode.depth, true);
652          return true;
653   #else
654          ErrorAlert("Basilisk II has been compiled with XF86 DGA support disabled.");
# Line 922 | Line 721 | static void keycode_init(void)
721          }
722   }
723  
724 < bool VideoInitBuffer()
724 > // Open display for specified mode
725 > static bool video_open(const video_mode &mode)
726   {
727 < #ifdef ENABLE_VOSF
728 <        if (use_vosf) {
729 <                const uint32 page_size  = getpagesize();
730 <                const uint32 page_mask  = page_size - 1;
731 <                
732 <                mainBuffer.memBase      = (uint32) the_buffer;
733 <                // Align the frame buffer on page boundary
734 <                mainBuffer.memStart             = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
935 <                mainBuffer.memLength    = the_buffer_size;
936 <                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
937 <
938 <                mainBuffer.pageSize     = page_size;
939 <                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
940 <                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
727 >        // 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 <                if (mainBuffer.dirtyPages != 0)
737 <                        free(mainBuffer.dirtyPages);
736 >        // Initialize according to display type
737 >        switch (display_type) {
738 >                case DISPLAY_WINDOW:
739 >                        if (!init_window(mode))
740 >                                return false;
741 >                        break;
742 >                case DISPLAY_DGA:
743 > #ifdef ENABLE_FBDEV_DGA
744 >                        if (!init_fbdev_dga(mode))
745 > #else
746 >                        if (!init_xf86_dga(mode))
747 > #endif
748 >                                return false;
749 >                        break;
750 >        }
751  
752 <                mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
752 >        // Lock down frame buffer
753 >        LOCK_FRAME_BUFFER;
754  
755 <                if (mainBuffer.pageInfo != 0)
756 <                        free(mainBuffer.pageInfo);
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 <                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
760 >        // No special frame buffer in Classic mode (frame buffer is in Mac RAM)
761 >        if (classic)
762 >                MacFrameLayout = FLAYOUT_NONE;
763 > #endif
764  
765 <                if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
953 <                        return false;
954 <                
955 <                mainBuffer.dirty = false;
765 >        InitFrameBufferMapping();
766  
767 <                PFLAG_CLEAR_ALL;
768 <                // Safety net to insure the loops in the update routines will terminate
769 <                // See a discussion in <video_vosf.h> for further details
770 <                PFLAG_CLEAR(mainBuffer.pageCount);
771 <                PFLAG_SET(mainBuffer.pageCount+1);
772 <
963 <                uint32 a = 0;
964 <                for (int i = 0; i < mainBuffer.pageCount; i++) {
965 <                        int y1 = a / VideoMonitor.bytes_per_row;
966 <                        if (y1 >= VideoMonitor.y)
967 <                                y1 = VideoMonitor.y - 1;
968 <
969 <                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
970 <                        if (y2 >= VideoMonitor.y)
971 <                                y2 = VideoMonitor.y - 1;
972 <
973 <                        mainBuffer.pageInfo[i].top = y1;
974 <                        mainBuffer.pageInfo[i].bottom = y2;
975 <
976 <                        a += mainBuffer.pageSize;
977 <                        if (a > mainBuffer.memLength)
978 <                                a = mainBuffer.memLength;
767 > #ifdef ENABLE_VOSF
768 >        if (use_vosf) {
769 >                // Initialize the mainBuffer structure
770 >                if (!video_init_buffer()) {
771 >                        ErrorAlert(GetString(STR_VOSF_INIT_ERR));
772 >                return false;
773                  }
774 <                
775 <                // We can now write-protect the frame buffer
776 <                if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0)
774 >
775 >                // Initialize the handler for SIGSEGV
776 >                if (!sigsegv_install_handler(screen_fault_handler)) {
777 >                        ErrorAlert("Could not initialize Video on SEGV signals");
778                          return false;
779 +                }
780          }
781   #endif
782 +        
783 +        // Initialize VideoRefresh function
784 +        VideoRefreshInit();
785 +        
786 +        XSync(x_display, false);
787 +
788 + #ifdef HAVE_PTHREADS
789 +        // Start redraw/input thread
790 +        redraw_thread_cancel = false;
791 +        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
792 +        if (!redraw_thread_active) {
793 +                printf("FATAL: cannot create redraw thread\n");
794 +                return false;
795 +        }
796 + #endif
797 +
798          return true;
799   }
800  
801   bool VideoInit(bool classic)
802   {
803 +        classic_mode = classic;
804 +
805   #ifdef ENABLE_VOSF
992        // Open /dev/zero
993        zero_fd = open("/dev/zero", O_RDWR);
994        if (zero_fd < 0) {
995        char str[256];
996                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
997                ErrorAlert(str);
998        return false;
999        }
1000        
806          // Zero the mainBuffer structure
807          mainBuffer.dirtyPages = 0;
808          mainBuffer.pageInfo = 0;
# Line 1088 | Line 893 | bool VideoInit(bool classic)
893          }
894          vis = visualInfo.visual;
895  
1091        // Mac screen depth is always 1 bit in Classic mode, but follows X depth otherwise
1092        classic_mode = classic;
1093        if (classic)
1094                depth = 1;
1095        else
1096                depth = xdepth;
1097
1098        // Create color maps for 8 bit mode
1099        if (depth == 8) {
1100                cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1101                cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1102                XInstallColormap(x_display, cmap[0]);
1103                XInstallColormap(x_display, cmap[1]);
1104        }
1105
896          // Get screen mode from preferences
897          const char *mode_str;
898 <        if (classic)
898 >        if (classic_mode)
899                  mode_str = "win/512/342";
900          else
901                  mode_str = PrefsFindString("screen");
902  
903 <        // Determine type and mode
904 <        int width = 512, height = 384;
903 >        // Determine display type and default dimensions
904 >        int default_width = 512, default_height = 384;
905          display_type = DISPLAY_WINDOW;
906          if (mode_str) {
907 <                if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
907 >                if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2) {
908                          display_type = DISPLAY_WINDOW;
909   #ifdef ENABLE_FBDEV_DGA
910 <                else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
1121 < #else
1122 <                else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
1123 < #endif
910 >                } else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
911                          display_type = DISPLAY_DGA;
912 <                        if (width > DisplayWidth(x_display, screen))
1126 <                                width = DisplayWidth(x_display, screen);
1127 <                        if (height > DisplayHeight(x_display, screen))
1128 <                                height = DisplayHeight(x_display, screen);
1129 <                }
1130 <                if (width <= 0)
1131 <                        width = DisplayWidth(x_display, screen);
1132 <                if (height <= 0)
1133 <                        height = DisplayHeight(x_display, screen);
1134 <        }
1135 <
1136 <        // Initialize according to display type
1137 <        switch (display_type) {
1138 <                case DISPLAY_WINDOW:
1139 <                        if (!init_window(width, height))
1140 <                                return false;
1141 <                        break;
1142 <                case DISPLAY_DGA:
1143 < #ifdef ENABLE_FBDEV_DGA
1144 <                        if (!init_fbdev_dga(fb_name))
912 >                        default_width = -1; default_height = -1;
913   #else
914 <                        if (!init_xf86_dga(width, height))
914 >                } else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2) {
915 >                        display_type = DISPLAY_DGA;
916   #endif
917 <                                return false;
1149 <                        break;
917 >                }
918          }
919 +        if (default_width <= 0)
920 +                default_width = DisplayWidth(x_display, screen);
921 +        else if (default_width > DisplayWidth(x_display, screen))
922 +                default_width = DisplayWidth(x_display, screen);
923 +        if (default_height <= 0)
924 +                default_height = DisplayHeight(x_display, screen);
925 +        else if (default_height > DisplayHeight(x_display, screen))
926 +                default_height = DisplayHeight(x_display, screen);
927  
928 <        // Lock down frame buffer
929 <        LOCK_FRAME_BUFFER;
930 <
1155 < #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1156 <        // Set variables for UAE memory mapping
1157 <        MacFrameBaseHost = the_buffer;
1158 <        MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
1159 <
1160 <        // No special frame buffer in Classic mode (frame buffer is in Mac RAM)
1161 <        if (classic)
1162 <                MacFrameLayout = FLAYOUT_NONE;
1163 < #endif
928 >        // Mac screen depth is always 1 bit in Classic mode, but follows X depth otherwise
929 >        int depth = (classic_mode ? 1 : xdepth);
930 >        video_depth depth_mode = DepthModeForPixelDepth(depth);
931  
932 < #ifdef ENABLE_VOSF
933 <        if (use_vosf) {
934 <                // Initialize the mainBuffer structure
935 <                if (!VideoInitBuffer()) {
936 <                        // TODO: STR_VOSF_INIT_ERR ?
937 <                        ErrorAlert("Could not initialize Video on SEGV signals");
938 <                return false;
932 >        // Construct list of supported modes
933 >        if (display_type == DISPLAY_WINDOW) {
934 >                if (classic)
935 >                        add_mode(512, 342, 0x80, 64, depth_mode);
936 >                else {
937 >                        add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth_mode), depth_mode);
938 >                        add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth_mode), depth_mode);
939 >                        add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth_mode), depth_mode);
940 >                        add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth_mode), depth_mode);
941 >                        add_mode(1280, 1024, 0x84, TrivialBytesPerRow(1280, depth_mode), depth_mode);
942                  }
943 +        } else
944 +                add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, depth_mode), depth_mode);
945  
946 <                // Initialize the handler for SIGSEGV
947 <                if (!Screen_fault_handler_init()) {
948 <                        // TODO: STR_VOSF_INIT_ERR ?
949 <                        ErrorAlert("Could not initialize Video on SEGV signals");
950 <                        return false;
946 >        // Find requested default mode and open display
947 >        if (VideoModes.size() == 1)
948 >                return video_open(VideoModes[0]);
949 >        else {
950 >                // Find mode with specified dimensions
951 >                std::vector<video_mode>::const_iterator i = VideoModes.begin(), end = VideoModes.end();
952 >                while (i != end) {
953 >                        if (i->x == default_width && i->y == default_height)
954 >                                return video_open(*i);
955 >                        ++i;
956                  }
957 +                return video_open(VideoModes[0]);
958          }
1181 #endif
1182        
1183        // Initialize VideoRefresh function
1184        VideoRefreshInit();
1185        
1186        XSync(x_display, false);
1187
1188 #ifdef HAVE_PTHREADS
1189        // Start redraw/input thread
1190        redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1191        if (!redraw_thread_active) {
1192                printf("FATAL: cannot create redraw thread\n");
1193                return false;
1194        }
1195 #endif
1196        return true;
959   }
960  
961  
# Line 1201 | Line 963 | bool VideoInit(bool classic)
963   *  Deinitialization
964   */
965  
966 < void VideoExit(void)
966 > // Close display
967 > static void video_close(void)
968   {
969   #ifdef HAVE_PTHREADS
970          // Stop redraw thread
# Line 1245 | Line 1008 | void VideoExit(void)
1008  
1009                  XFlush(x_display);
1010                  XSync(x_display, false);
1011 <                if (depth == 8) {
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) {
# Line 1263 | Line 1040 | void VideoExit(void)
1040                  }
1041   #ifdef ENABLE_VOSF
1042                  else {
1043 <                        if (the_buffer != (uint8 *)MAP_FAILED) {
1044 <                                munmap((caddr_t)the_buffer, the_buffer_size);
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 *)MAP_FAILED) {
1051 <                                munmap((caddr_t)the_buffer_copy, the_buffer_size);
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                  }
# Line 1289 | Line 1068 | void VideoExit(void)
1068                          mainBuffer.dirtyPages = 0;
1069                  }
1070          }
1292
1293        // Close /dev/zero
1294        if (zero_fd > 0)
1295                close(zero_fd);
1071   #endif
1072   }
1073  
1074 + void VideoExit(void)
1075 + {
1076 +        video_close();
1077 + }
1078 +
1079  
1080   /*
1081   *  Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
# Line 1351 | Line 1131 | void video_set_palette(uint8 *pal)
1131  
1132  
1133   /*
1134 + *  Switch video mode
1135 + */
1136 +
1137 + void video_switch_to_mode(const video_mode &mode)
1138 + {
1139 +        video_close();
1140 +        video_open(mode);
1141 + }
1142 +
1143 +
1144 + /*
1145   *  Suspend/resume emulator
1146   */
1147  
# Line 1366 | Line 1157 | static void suspend_emul(void)
1157                  LOCK_FRAME_BUFFER;
1158  
1159                  // Save frame buffer
1160 <                fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
1160 >                fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
1161                  if (fb_save)
1162 <                        memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
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
# Line 1419 | Line 1210 | static void resume_emul(void)
1210                  LOCK_VOSF;
1211                  PFLAG_SET_ALL;
1212                  UNLOCK_VOSF;
1213 <                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1213 >                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1214          }
1215   #endif
1216          
# Line 1429 | Line 1220 | static void resume_emul(void)
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.y * VideoMonitor.bytes_per_row);
1223 >                memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
1224                  free(fb_save);
1225                  fb_save = NULL;
1226          }
1227          
1228   #ifdef ENABLE_XF86_DGA
1229 <        if (depth == 8)
1229 >        if (!IsDirectMode(VideoMonitor.mode))
1230                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1231   #endif
1232  
# Line 1710 | Line 1501 | static void handle_events(void)
1501                                                  LOCK_VOSF;
1502                                                  PFLAG_SET_ALL;
1503                                                  UNLOCK_VOSF;
1504 <                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1504 >                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1505                                          }
1506                                          else
1507   #endif
# Line 1721 | Line 1512 | static void handle_events(void)
1512                                                          updt_box[x1][y1] = true;
1513                                                  nr_boxes = 16 * 16;
1514                                          } else                                  // Static refresh
1515 <                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1515 >                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1516                                  }
1517                                  break;
1518  
# Line 1747 | Line 1538 | static void update_display_dynamic(int t
1538          int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1539          int xil = 0;
1540          int rxm = 0, rxmo = 0;
1541 <        int bytes_per_row = VideoMonitor.bytes_per_row;
1542 <        int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1543 <        int rx = VideoMonitor.bytes_per_row / 16;
1544 <        int ry = VideoMonitor.y / 16;
1541 >        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
1542 >        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
1543 >        int rx = VideoMonitor.mode.bytes_per_row / 16;
1544 >        int ry = VideoMonitor.mode.y / 16;
1545          int max_box;
1546  
1547          y2s = sm_uptd[ticker % 8];
# Line 1804 | Line 1595 | static void update_display_dynamic(int t
1595                                          i = (yi * bytes_per_row) + xi;
1596                                          for (y2=0; y2 < yil; y2++, i += bytes_per_row)
1597                                                  memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
1598 <                                        if (depth == 1) {
1598 >                                        if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
1599                                                  if (have_shm)
1600                                                          XShmPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
1601                                                  else
# Line 1836 | Line 1627 | static void update_display_static(void)
1627   {
1628          // Incremental update code
1629          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1630 <        int bytes_per_row = VideoMonitor.bytes_per_row;
1631 <        int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1630 >        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
1631 >        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
1632          uint8 *p, *p2;
1633  
1634          // Check for first line from top and first line from bottom that have changed
1635          y1 = 0;
1636 <        for (j=0; j<VideoMonitor.y; j++) {
1636 >        for (j=0; j<VideoMonitor.mode.y; j++) {
1637                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1638                          y1 = j;
1639                          break;
1640                  }
1641          }
1642          y2 = y1 - 1;
1643 <        for (j=VideoMonitor.y-1; j>=y1; j--) {
1643 >        for (j=VideoMonitor.mode.y-1; j>=y1; j--) {
1644                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1645                          y2 = j;
1646                          break;
# Line 1859 | Line 1650 | static void update_display_static(void)
1650  
1651          // Check for first column from left and first column from right that have changed
1652          if (high) {
1653 <                if (depth == 1) {
1654 <                        x1 = VideoMonitor.x - 1;
1653 >                if (VideoMonitor.mode.depth == VDEPTH_1BIT) {
1654 >                        x1 = VideoMonitor.mode.x - 1;
1655                          for (j=y1; j<=y2; j++) {
1656                                  p = &the_buffer[j * bytes_per_row];
1657                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1878 | Line 1669 | static void update_display_static(void)
1669                                  p2 = &the_buffer_copy[j * bytes_per_row];
1670                                  p += bytes_per_row;
1671                                  p2 += bytes_per_row;
1672 <                                for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1672 >                                for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) {
1673                                          p--; p2--;
1674                                          if (*p != *p2) {
1675                                                  x2 = (i << 3) + 7;
# Line 1897 | Line 1688 | static void update_display_static(void)
1688                          }
1689  
1690                  } else {
1691 <                        x1 = VideoMonitor.x;
1691 >                        x1 = VideoMonitor.mode.x;
1692                          for (j=y1; j<=y2; j++) {
1693                                  p = &the_buffer[j * bytes_per_row];
1694                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1915 | Line 1706 | static void update_display_static(void)
1706                                  p2 = &the_buffer_copy[j * bytes_per_row];
1707                                  p += bytes_per_row;
1708                                  p2 += bytes_per_row;
1709 <                                for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1709 >                                for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1710                                          p--;
1711                                          p2--;
1712                                          if (*p != *p2) {
# Line 1968 | Line 1759 | static inline void possibly_quit_dga_mod
1759                  XUngrabPointer(x_display, CurrentTime);
1760                  XUngrabKeyboard(x_display, CurrentTime);
1761                  XUnmapWindow(x_display, the_win);
1762 <                XSync(x_display, false);
1762 >                wait_unmapped(the_win);
1763          }
1764   #endif
1765   }
1766  
1767 < static inline void handle_palette_changes(int depth, int display_type)
1767 > static inline void handle_palette_changes(int display_type)
1768   {
1769          LOCK_PALETTE;
1770  
1771          if (palette_changed) {
1772                  palette_changed = false;
1773 <                if (depth == 8) {
1773 >                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);
# Line 2005 | Line 1796 | static void video_refresh_dga(void)
1796          handle_events();
1797          
1798          // Handle palette changes
1799 <        handle_palette_changes(depth, DISPLAY_DGA);
1799 >        handle_palette_changes(DISPLAY_DGA);
1800   }
1801  
1802   #ifdef ENABLE_VOSF
# Line 2019 | Line 1810 | static void video_refresh_dga_vosf(void)
1810          handle_events();
1811          
1812          // Handle palette changes
1813 <        handle_palette_changes(depth, DISPLAY_DGA);
1813 >        handle_palette_changes(DISPLAY_DGA);
1814          
1815          // Update display (VOSF variant)
1816          static int tick_counter = 0;
# Line 2043 | Line 1834 | static void video_refresh_window_vosf(vo
1834          handle_events();
1835          
1836          // Handle palette changes
1837 <        handle_palette_changes(depth, DISPLAY_WINDOW);
1837 >        handle_palette_changes(DISPLAY_WINDOW);
1838          
1839          // Update display (VOSF variant)
1840          static int tick_counter = 0;
# Line 2053 | Line 1844 | static void video_refresh_window_vosf(vo
1844                          LOCK_VOSF;
1845                          update_display_window_vosf();
1846                          UNLOCK_VOSF;
1847 +                        XSync(x_display, false); // Let the server catch up
1848                  }
1849          }
1850   }
# Line 2064 | Line 1856 | static void video_refresh_window_static(
1856          handle_events();
1857          
1858          // Handle_palette changes
1859 <        handle_palette_changes(depth, DISPLAY_WINDOW);
1859 >        handle_palette_changes(DISPLAY_WINDOW);
1860          
1861          // Update display (static variant)
1862          static int tick_counter = 0;
# Line 2080 | Line 1872 | static void video_refresh_window_dynamic
1872          handle_events();
1873          
1874          // Handle_palette changes
1875 <        handle_palette_changes(depth, DISPLAY_WINDOW);
1875 >        handle_palette_changes(DISPLAY_WINDOW);
1876          
1877          // Update display (dynamic variant)
1878          static int tick_counter = 0;
# Line 2093 | Line 1885 | static void video_refresh_window_dynamic
1885   *  Thread for screen refresh, input handling etc.
1886   */
1887  
1888 < void VideoRefreshInit(void)
1888 > static void VideoRefreshInit(void)
1889   {
1890          // TODO: set up specialised 8bpp VideoRefresh handlers ?
1891          if (display_type == DISPLAY_DGA) {
# Line 2140 | Line 1932 | static void *redraw_func(void *arg)
1932                  ticks++;
1933          }
1934          uint64 end = GetTicks_usec();
1935 <        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
1935 >        // printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
1936          return NULL;
1937   }
1938   #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines