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.8 by cebix, 1999-10-21T16:07:36Z vs.
Revision 1.40 by cebix, 2001-06-27T19:03:37Z

# Line 1 | Line 1
1   /*
2   *  video_x.cpp - Video/graphics emulation, X11 specific stuff
3   *
4 < *  Basilisk II (C) 1997-1999 Christian Bauer
4 > *  Basilisk II (C) 1997-2001 Christian Bauer
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 34 | Line 34
34   #include <X11/extensions/XShm.h>
35   #include <sys/ipc.h>
36   #include <sys/shm.h>
37 #include <pthread.h>
37   #include <errno.h>
38  
39 + #ifdef HAVE_PTHREADS
40 + # include <pthread.h>
41 + #endif
42 +
43 + #ifdef ENABLE_XF86_DGA
44 + # include <X11/extensions/xf86dga.h>
45 + #endif
46 +
47 + #ifdef ENABLE_XF86_VIDMODE
48 + # include <X11/extensions/xf86vmode.h>
49 + #endif
50 +
51 + #ifdef ENABLE_FBDEV_DGA
52 + # include <sys/mman.h>
53 + #endif
54 +
55 + #ifdef ENABLE_VOSF
56 + # include <fcntl.h>
57 + # include <sys/mman.h>
58 + # include "sigsegv.h"
59 + # include "vm_alloc.h"
60 + #endif
61 +
62   #include "cpu_emulation.h"
63   #include "main.h"
64   #include "adb.h"
# Line 45 | Line 67
67   #include "user_strings.h"
68   #include "video.h"
69  
70 < #define DEBUG 1
70 > #define DEBUG 0
71   #include "debug.h"
72  
51 #if ENABLE_XF86_DGA
52 #include <X11/extensions/xf86dga.h>
53 #endif
54
55 #if ENABLE_FBDEV_DGA
56 #include <sys/mman.h>
57 #endif
58
59
73  
74   // Display types
75   enum {
# Line 64 | Line 77 | enum {
77          DISPLAY_DGA             // DGA fullscreen display
78   };
79  
67
80   // Constants
81   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
82   const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices";
83  
84  
85   // Global variables
86 < static int32 frame_skip;
86 > static int32 frame_skip;                                                        // Prefs items
87 > static int16 mouse_wheel_mode = 1;
88 > static int16 mouse_wheel_lines = 3;
89 >
90   static int display_type = DISPLAY_WINDOW;                       // See enum above
91 + static bool local_X11;                                                          // Flag: X server running on local machine?
92   static uint8 *the_buffer;                                                       // Mac frame buffer
93 +
94 + #ifdef HAVE_PTHREADS
95   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
96   static volatile bool redraw_thread_cancel = false;      // Flag: Cancel Redraw thread
97   static pthread_t redraw_thread;                                         // Redraw thread
98 + #endif
99  
100   static bool has_dga = false;                                            // Flag: Video DGA capable
101 + static bool has_vidmode = false;                                        // Flag: VidMode extension available
102  
103   static bool ctrl_down = false;                                          // Flag: Ctrl key pressed
104   static bool caps_on = false;                                            // Flag: Caps Lock on
# Line 102 | Line 122 | static Colormap cmap[2];                                                       // Two co
122   static XColor black, white;
123   static unsigned long black_pixel, white_pixel;
124   static int eventmask;
125 < static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask;
126 < static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
125 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
126 > static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
127 > static Atom WM_DELETE_WINDOW = (Atom)0;
128  
108 static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;        // Mutex to protect palette
129   static XColor palette[256];                                                     // Color palette for 8-bit mode
130   static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
131 + #ifdef HAVE_PTHREADS
132 + static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;        // Mutex to protect palette
133 + #define LOCK_PALETTE pthread_mutex_lock(&palette_lock)
134 + #define UNLOCK_PALETTE pthread_mutex_unlock(&palette_lock)
135 + #else
136 + #define LOCK_PALETTE
137 + #define UNLOCK_PALETTE
138 + #endif
139  
140   // Variables for window mode
141   static GC the_gc;
142   static XImage *img = NULL;
143   static XShmSegmentInfo shminfo;
116 static XImage *cursor_image, *cursor_mask_image;
117 static Pixmap cursor_map, cursor_mask_map;
144   static Cursor mac_cursor;
119 static GC cursor_gc, cursor_mask_gc;
145   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer
121 static uint8 the_cursor[64];                                            // Cursor image data
146   static bool have_shm = false;                                           // Flag: SHM extensions available
147 + static bool updt_box[17][17];                                           // Flag for Update
148 + static int nr_boxes;
149 + static const int sm_uptd[] = {4,1,6,3,0,5,2,7};
150 + static int sm_no_boxes[] = {1,8,32,64,128,300};
151  
152   // Variables for XF86 DGA mode
153   static int current_dga_cmap;                                            // Number (0 or 1) of currently installed DGA colormap
154   static Window suspend_win;                                                      // "Suspend" window
155   static void *fb_save = NULL;                                            // Saved frame buffer for suspend
156 + #ifdef HAVE_PTHREADS
157   static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer
158 + #define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock);
159 + #define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock);
160 + #else
161 + #define LOCK_FRAME_BUFFER
162 + #define UNLOCK_FRAME_BUFFER
163 + #endif
164  
165   // Variables for fbdev DGA mode
166   const char FBDEVICE_FILE_NAME[] = "/dev/fb";
167   static int fbdev_fd;
168  
169 + #ifdef ENABLE_XF86_VIDMODE
170 + // Variables for XF86 VidMode support
171 + static XF86VidModeModeInfo **x_video_modes;                     // Array of all available modes
172 + static int num_x_video_modes;
173 + #endif
174 +
175 + #ifdef ENABLE_VOSF
176 + static bool use_vosf = true;                                            // Flag: VOSF enabled
177 + #else
178 + static const bool use_vosf = false;                                     // Flag: VOSF enabled
179 + #endif
180 +
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 +    uintptr memBase;                    // Real start address
192 +    uintptr memStart;                   // Start address aligned to page boundary
193 +    uintptr 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 +
282 + #ifdef HAVE_PTHREADS
283 + static pthread_mutex_t vosf_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer (dirtyPages in fact)
284 + #define LOCK_VOSF pthread_mutex_lock(&vosf_lock);
285 + #define UNLOCK_VOSF pthread_mutex_unlock(&vosf_lock);
286 + #else
287 + #define LOCK_VOSF
288 + #define UNLOCK_VOSF
289 + #endif
290 +
291 + static int log_base_2(uint32 x)
292 + {
293 +        uint32 mask = 0x80000000;
294 +        int l = 31;
295 +        while (l >= 0 && (x & mask) == 0) {
296 +                mask >>= 1;
297 +                l--;
298 +        }
299 +        return l;
300 + }
301 + #endif /* ENABLE_VOSF */
302 +
303 + // VideoRefresh function
304 + void VideoRefreshInit(void);
305 + static void (*video_refresh)(void);
306  
307   // Prototypes
308   static void *redraw_func(void *arg);
309 < static int event2keycode(XKeyEvent *ev);
309 > static int event2keycode(XKeyEvent &ev);
310  
311  
312   // From main_unix.cpp
313 + extern char *x_display_name;
314   extern Display *x_display;
315  
316   // From sys_unix.cpp
317   extern void SysMountFirstFloppy(void);
318  
319 + #ifdef ENABLE_VOSF
320 + # include "video_vosf.h"
321 + #endif
322 +
323  
324   /*
325   *  Initialization
326   */
327  
328 < // Set VideoMonitor according to video mode
329 < void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order)
328 > // Add resolution to list of supported modes and set VideoMonitor
329 > static void set_video_monitor(uint32 width, uint32 height, uint32 bytes_per_row, bool native_byte_order)
330   {
331 +        video_mode mode;
332 + #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
333          int layout = FLAYOUT_DIRECT;
334          switch (depth) {
335                  case 1:
336                          layout = FLAYOUT_DIRECT;
158                        VideoMonitor.mode = VMODE_1BIT;
337                          break;
338                  case 8:
339                          layout = FLAYOUT_DIRECT;
162                        VideoMonitor.mode = VMODE_8BIT;
340                          break;
341                  case 15:
342                          layout = FLAYOUT_HOST_555;
166                        VideoMonitor.mode = VMODE_16BIT;
343                          break;
344                  case 16:
345                          layout = FLAYOUT_HOST_565;
170                        VideoMonitor.mode = VMODE_16BIT;
346                          break;
347                  case 24:
348                  case 32:
349                          layout = FLAYOUT_HOST_888;
175                        VideoMonitor.mode = VMODE_32BIT;
350                          break;
351          }
178        VideoMonitor.x = width;
179        VideoMonitor.y = height;
180        VideoMonitor.bytes_per_row = bytes_per_row;
352          if (native_byte_order)
353                  MacFrameLayout = layout;
354          else
355                  MacFrameLayout = FLAYOUT_DIRECT;
356 + #endif
357 +
358 +        mode.x = width;
359 +        mode.y = height;
360 +        mode.resolution_id = 0x80;
361 +        mode.bytes_per_row = bytes_per_row;
362 +
363 +        switch (depth) {
364 +                case 1:
365 +                        mode.depth = VDEPTH_1BIT;
366 +                        break;
367 +                case 2:
368 +                        mode.depth = VDEPTH_2BIT;
369 +                        break;
370 +                case 4:
371 +                        mode.depth = VDEPTH_4BIT;
372 +                        break;
373 +                case 8:
374 +                        mode.depth = VDEPTH_8BIT;
375 +                        break;
376 +                case 15:
377 +                        mode.depth = VDEPTH_16BIT;
378 +                        break;
379 +                case 16:
380 +                        mode.depth = VDEPTH_16BIT;
381 +                        break;
382 +                case 24:
383 +                case 32:
384 +                        mode.depth = VDEPTH_32BIT;
385 +                        break;
386 +        }
387 +
388 +        VideoModes.push_back(mode);
389 +        VideoMonitor.mode = mode;
390 + }
391 +
392 + // Set window name and class
393 + static void set_window_name(Window w, int name)
394 + {
395 +        const char *str = GetString(name);
396 +        XStoreName(x_display, w, str);
397 +        XSetIconName(x_display, w, str);
398 +
399 +        XClassHint *hints;
400 +        hints = XAllocClassHint();
401 +        if (hints) {
402 +                hints->res_name = "BasiliskII";
403 +                hints->res_class = "BasiliskII";
404 +                XSetClassHint(x_display, w, hints);
405 +                XFree(hints);
406 +        }
407 + }
408 +
409 + // Set window input focus flag
410 + static void set_window_focus(Window w)
411 + {
412 +        XWMHints *hints = XAllocWMHints();
413 +        if (hints) {
414 +                hints->input = True;
415 +                hints->initial_state = NormalState;
416 +                hints->flags = InputHint | StateHint;
417 +                XSetWMHints(x_display, w, hints);
418 +                XFree(hints);
419 +        }
420 + }
421 +
422 + // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
423 + static void set_window_delete_protocol(Window w)
424 + {
425 +        WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
426 +        XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
427 + }
428 +
429 + // Wait until window is mapped/unmapped
430 + void wait_mapped(Window w)
431 + {
432 +        XEvent e;
433 +        do {
434 +                XMaskEvent(x_display, StructureNotifyMask, &e);
435 +        } while ((e.type != MapNotify) || (e.xmap.event != w));
436 + }
437 +
438 + void wait_unmapped(Window w)
439 + {
440 +        XEvent e;
441 +        do {
442 +                XMaskEvent(x_display, StructureNotifyMask, &e);
443 +        } while ((e.type != UnmapNotify) || (e.xmap.event != w));
444   }
445  
446   // Trap SHM errors
# Line 200 | Line 459 | static int error_handler(Display *d, XEr
459   // Init window mode
460   static bool init_window(int width, int height)
461   {
462 +        int aligned_width = (width + 15) & ~15;
463 +        int aligned_height = (height + 15) & ~15;
464 +
465          // Set absolute mouse mode
466          ADBSetRelMouseMode(false);
467  
468          // Read frame skip prefs
469          frame_skip = PrefsFindInt32("frameskip");
208        if (frame_skip == 0)
209                frame_skip = 1;
470  
471          // Create window
472          XSetWindowAttributes wattr;
473          wattr.event_mask = eventmask = win_eventmask;
474          wattr.background_pixel = black_pixel;
475 <        wattr.border_pixel = black_pixel;
216 <        wattr.backing_store = Always;
217 <        wattr.backing_planes = xdepth;
475 >        wattr.colormap = cmap[0];
476  
219        XSync(x_display, false);
477          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
478 <                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
222 <                CWBackingStore | CWBackingPlanes, &wattr);
223 <        XSync(x_display, false);
224 <        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
225 <        XMapRaised(x_display, the_win);
226 <        XSync(x_display, false);
478 >                InputOutput, vis, CWEventMask | CWBackPixel | (depth == 8 ? CWColormap : 0), &wattr);
479  
480 <        // Set colormap
481 <        if (depth == 8) {
482 <                XSetWindowColormap(x_display, the_win, cmap[0]);
483 <                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
484 <        }
480 >        // Set window name/class
481 >        set_window_name(the_win, STR_WINDOW_TITLE);
482 >
483 >        // Indicate that we want keyboard input
484 >        set_window_focus(the_win);
485 >
486 >        // Set delete protocol property
487 >        set_window_delete_protocol(the_win);
488  
489          // Make window unresizable
490 <        XSizeHints *hints;
491 <        if ((hints = XAllocSizeHints()) != NULL) {
492 <                hints->min_width = width;
493 <                hints->max_width = width;
494 <                hints->min_height = height;
495 <                hints->max_height = height;
496 <                hints->flags = PMinSize | PMaxSize;
497 <                XSetWMNormalHints(x_display, the_win, hints);
498 <                XFree((char *)hints);
490 >        {
491 >                XSizeHints *hints = XAllocSizeHints();
492 >                if (hints) {
493 >                        hints->min_width = width;
494 >                        hints->max_width = width;
495 >                        hints->min_height = height;
496 >                        hints->max_height = height;
497 >                        hints->flags = PMinSize | PMaxSize;
498 >                        XSetWMNormalHints(x_display, the_win, hints);
499 >                        XFree(hints);
500 >                }
501          }
502          
503 +        // Show window
504 +        XMapWindow(x_display, the_win);
505 +        wait_mapped(the_win);
506 +
507          // Try to create and attach SHM image
508          have_shm = false;
509 <        if (depth != 1 && XShmQueryExtension(x_display)) {
509 >        if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
510  
511                  // Create SHM image ("height + 2" for safety)
512                  img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
513 <                shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
514 <                the_buffer = (uint8 *)shmat(shminfo.shmid, 0, 0);
515 <                shminfo.shmaddr = img->data = (char *)the_buffer;
513 >                shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
514 >                the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
515 >                shminfo.shmaddr = img->data = (char *)the_buffer_copy;
516                  shminfo.readOnly = False;
517  
518                  // Try to attach SHM image, catching errors
# Line 272 | Line 533 | static bool init_window(int width, int h
533          
534          // Create normal X image if SHM doesn't work ("height + 2" for safety)
535          if (!have_shm) {
536 <                int bytes_per_row = width;
536 >                int bytes_per_row = aligned_width;
537                  switch (depth) {
538                          case 1:
539                                  bytes_per_row /= 8;
# Line 286 | Line 547 | static bool init_window(int width, int h
547                                  bytes_per_row *= 4;
548                                  break;
549                  }
550 <                the_buffer = (uint8 *)malloc((height + 2) * bytes_per_row);
551 <                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer, width, height, 32, bytes_per_row);
550 >                the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
551 >                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
552          }
553  
554          // 1-Bit mode is big-endian
# Line 296 | Line 557 | static bool init_window(int width, int h
557                  img->bitmap_bit_order = MSBFirst;
558          }
559  
560 <        // Allocate memory for frame buffer copy
561 <        the_buffer_copy = (uint8 *)malloc((height + 2) * img->bytes_per_line);
560 > #ifdef ENABLE_VOSF
561 >        // Allocate memory for frame buffer (SIZE is extended to page-boundary)
562 >        the_host_buffer = the_buffer_copy;
563 >        the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
564 >        the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
565 >        the_buffer = (uint8 *)vm_acquire(the_buffer_size);
566 > #else
567 >        // Allocate memory for frame buffer
568 >        the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
569 > #endif
570  
571          // Create GC
572          the_gc = XCreateGC(x_display, the_win, 0, 0);
573          XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
574  
575 <        // Create cursor
576 <        cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor, 16, 16, 16, 2);
577 <        cursor_image->byte_order = MSBFirst;
578 <        cursor_image->bitmap_bit_order = MSBFirst;
579 <        cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor+32, 16, 16, 16, 2);
580 <        cursor_mask_image->byte_order = MSBFirst;
312 <        cursor_mask_image->bitmap_bit_order = MSBFirst;
313 <        cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
314 <        cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
315 <        cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
316 <        cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
317 <        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
575 >        // Create no_cursor
576 >        mac_cursor = XCreatePixmapCursor(x_display,
577 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
578 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
579 >           &black, &white, 0, 0);
580 >        XDefineCursor(x_display, the_win, mac_cursor);
581  
582 <        // Set VideoMonitor
582 >        // Add resolution and set VideoMonitor
583 >        bool native_byte_order;
584   #ifdef WORDS_BIGENDIAN
585 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst);
585 >        native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
586   #else
587 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst);
587 >        native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
588 > #endif
589 > #ifdef ENABLE_VOSF
590 >        Screen_blitter_init(&visualInfo, native_byte_order);
591   #endif
592 +        set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
593          
594 < #if REAL_ADDRESSING
595 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
328 <        MacFrameLayout = FLAYOUT_DIRECT;
594 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
595 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
596   #else
597          VideoMonitor.mac_frame_base = MacFrameBaseMac;
598   #endif
# Line 335 | Line 602 | static bool init_window(int width, int h
602   // Init fbdev DGA display
603   static bool init_fbdev_dga(char *in_fb_name)
604   {
605 < #if ENABLE_FBDEV_DGA
605 > #ifdef ENABLE_FBDEV_DGA
606          // Find the maximum depth available
607          int ndepths, max_depth(0);
608          int *depths = XListDepths(x_display, screen, &ndepths);
609          if (depths == NULL) {
610 <                fprintf(stderr, "Error: could not determine the maximal depth available\n");
610 >                printf("FATAL: Could not determine the maximal depth available\n");
611                  return false;
612          } else {
613                  while (ndepths-- > 0) {
# Line 404 | Line 671 | static bool init_fbdev_dga(char *in_fb_n
671          
672          // Create window
673          XSetWindowAttributes wattr;
674 <        wattr.override_redirect = True;
675 <        wattr.backing_store             = NotUseful;
676 <        wattr.background_pixel  = white_pixel;
677 <        wattr.border_pixel              = black_pixel;
411 <        wattr.event_mask                = eventmask = dga_eventmask;
674 >        wattr.event_mask = eventmask = dga_eventmask;
675 >        wattr.background_pixel = white_pixel;
676 >        wattr.override_redirect = True;
677 >        wattr.colormap = cmap[0];
678          
413        XSync(x_display, false);
679          the_win = XCreateWindow(x_display, rootwin,
680                  0, 0, width, height,
681                  0, xdepth, InputOutput, vis,
682 <                CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore,
682 >                CWEventMask | CWBackPixel | CWOverrideRedirect | (depth == 8 ? CWColormap : 0),
683                  &wattr);
684 <        XSync(x_display, false);
684 >
685 >        // Set window name/class
686 >        set_window_name(the_win, STR_WINDOW_TITLE);
687 >
688 >        // Indicate that we want keyboard input
689 >        set_window_focus(the_win);
690 >
691 >        // Show window
692          XMapRaised(x_display, the_win);
693 <        XSync(x_display, false);
693 >        wait_mapped(the_win);
694          
695          // Grab mouse and keyboard
696          XGrabKeyboard(x_display, the_win, True,
# Line 427 | Line 699 | static bool init_fbdev_dga(char *in_fb_n
699                  PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
700                  GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
701          
430        // Set colormap
431        if (depth == 8) {
432                XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
433                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
434        }
435        
702          // Set VideoMonitor
703          int bytes_per_row = width;
704          switch (depth) {
# Line 458 | Line 724 | static bool init_fbdev_dga(char *in_fb_n
724                  }
725          }
726          
727 + #if ENABLE_VOSF
728 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
729 +        // Screen_blitter_init() returns TRUE if VOSF is mandatory
730 +        // i.e. the framebuffer update function is not Blit_Copy_Raw
731 +        use_vosf = Screen_blitter_init(&visualInfo, true);
732 +        
733 +        if (use_vosf) {
734 +          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
735 +          the_host_buffer = the_buffer;
736 +          the_buffer_size = page_extend((height + 2) * bytes_per_row);
737 +          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
738 +          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
739 +        }
740 + #else
741 +        use_vosf = false;
742 + #endif
743 + #endif
744 +        
745          set_video_monitor(width, height, bytes_per_row, true);
746 < #if REAL_ADDRESSING
747 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
464 <        MacFrameLayout = FLAYOUT_DIRECT;
746 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
747 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
748   #else
749          VideoMonitor.mac_frame_base = MacFrameBaseMac;
750   #endif
468        
469        printf("FbDev DGA with %s in %d-bit mode enabled\n", fb_name, fb_depth);
751          return true;
752   #else
753          ErrorAlert("Basilisk II has been compiled with fbdev DGA support disabled.");
# Line 477 | Line 758 | static bool init_fbdev_dga(char *in_fb_n
758   // Init XF86 DGA display
759   static bool init_xf86_dga(int width, int height)
760   {
761 < #if ENABLE_XF86_DGA
761 > #ifdef ENABLE_XF86_DGA
762          // Set relative mouse mode
763          ADBSetRelMouseMode(true);
764  
765 + #ifdef ENABLE_XF86_VIDMODE
766 +        // Switch to best mode
767 +        if (has_vidmode) {
768 +                int best = 0;
769 +                for (int i=1; i<num_x_video_modes; i++) {
770 +                        if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
771 +                                x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
772 +                                best = i;
773 +                        }
774 +                }
775 +                XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
776 +                XF86VidModeSetViewPort(x_display, screen, 0, 0);
777 +                XSync(x_display, false);
778 +        }
779 + #endif
780 +
781          // Create window
782          XSetWindowAttributes wattr;
783          wattr.event_mask = eventmask = dga_eventmask;
487        wattr.border_pixel = black_pixel;
784          wattr.override_redirect = True;
785  
490        XSync(x_display, false);
786          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
787 <                InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr);
788 <        XSync(x_display, false);
789 <        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
787 >                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
788 >
789 >        // Set window name/class
790 >        set_window_name(the_win, STR_WINDOW_TITLE);
791 >
792 >        // Indicate that we want keyboard input
793 >        set_window_focus(the_win);
794 >
795 >        // Show window
796          XMapRaised(x_display, the_win);
797 <        XSync(x_display, false);
797 >        wait_mapped(the_win);
798  
799          // Establish direct screen connection
800          XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
# Line 510 | Line 811 | static bool init_xf86_dga(int width, int
811          // Set colormap
812          if (depth == 8) {
813                  XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
513                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
814                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
815          }
816 +        XSync(x_display, false);
817  
818          // Set VideoMonitor
819          int bytes_per_row = (v_width + 7) & ~7;
# Line 529 | Line 830 | static bool init_xf86_dga(int width, int
830                          bytes_per_row *= 4;
831                          break;
832          }
833 +
834 + #ifdef VIDEO_VOSF
835 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
836 +        // Screen_blitter_init() returns TRUE if VOSF is mandatory
837 +        // i.e. the framebuffer update function is not Blit_Copy_Raw
838 +        use_vosf = Screen_blitter_init(&visualInfo, true);
839 +        
840 +        if (use_vosf) {
841 +          // Allocate memory for frame buffer (SIZE is extended to page-boundary)
842 +          the_host_buffer = the_buffer;
843 +          the_buffer_size = page_extend((height + 2) * bytes_per_row);
844 +          the_buffer_copy = (uint8 *)vm_acquire(the_buffer_size);
845 +          the_buffer = (uint8 *)vm_acquire(the_buffer_size);
846 +        }
847 + #else
848 +        use_vosf = false;
849 + #endif
850 + #endif
851 +        
852          set_video_monitor(width, height, bytes_per_row, true);
853 < #if REAL_ADDRESSING
854 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
855 <        MacFrameLayout = FLAYOUT_DIRECT;
853 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
854 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
855 > //      MacFrameLayout = FLAYOUT_DIRECT;
856   #else
857          VideoMonitor.mac_frame_base = MacFrameBaseMac;
858   #endif
# Line 608 | Line 928 | static void keycode_init(void)
928          }
929   }
930  
931 + bool VideoInitBuffer()
932 + {
933 + #ifdef ENABLE_VOSF
934 +        if (use_vosf) {
935 +                const uint32 page_size  = getpagesize();
936 +                const uint32 page_mask  = page_size - 1;
937 +                
938 +                mainBuffer.memBase      = (uintptr) the_buffer;
939 +                // Align the frame buffer on page boundary
940 +                mainBuffer.memStart             = (uintptr)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
941 +                mainBuffer.memLength    = the_buffer_size;
942 +                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
943 +
944 +                mainBuffer.pageSize     = page_size;
945 +                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
946 +                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
947 +
948 +                if (mainBuffer.dirtyPages != 0)
949 +                        free(mainBuffer.dirtyPages);
950 +
951 +                mainBuffer.dirtyPages = (char *) malloc(mainBuffer.pageCount + 2);
952 +
953 +                if (mainBuffer.pageInfo != 0)
954 +                        free(mainBuffer.pageInfo);
955 +
956 +                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
957 +
958 +                if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
959 +                        return false;
960 +                
961 +                mainBuffer.dirty = false;
962 +
963 +                PFLAG_CLEAR_ALL;
964 +                // Safety net to insure the loops in the update routines will terminate
965 +                // See a discussion in <video_vosf.h> for further details
966 +                PFLAG_CLEAR(mainBuffer.pageCount);
967 +                PFLAG_SET(mainBuffer.pageCount+1);
968 +
969 +                uint32 a = 0;
970 +                for (int i = 0; i < mainBuffer.pageCount; i++) {
971 +                        int y1 = a / VideoMonitor.mode.bytes_per_row;
972 +                        if (y1 >= VideoMonitor.mode.y)
973 +                                y1 = VideoMonitor.mode.y - 1;
974 +
975 +                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.mode.bytes_per_row;
976 +                        if (y2 >= VideoMonitor.mode.y)
977 +                                y2 = VideoMonitor.mode.y - 1;
978 +
979 +                        mainBuffer.pageInfo[i].top = y1;
980 +                        mainBuffer.pageInfo[i].bottom = y2;
981 +
982 +                        a += mainBuffer.pageSize;
983 +                        if (a > mainBuffer.memLength)
984 +                                a = mainBuffer.memLength;
985 +                }
986 +                
987 +                // We can now write-protect the frame buffer
988 +                if (vm_protect((char *)mainBuffer.memStart, mainBuffer.memLength, VM_PAGE_READ) != 0)
989 +                        return false;
990 +        }
991 + #endif
992 +        return true;
993 + }
994 +
995   bool VideoInit(bool classic)
996   {
997 + #ifdef ENABLE_VOSF
998 +        // Open /dev/zero
999 +        zero_fd = open("/dev/zero", O_RDWR);
1000 +        if (zero_fd < 0) {
1001 +        char str[256];
1002 +                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
1003 +                ErrorAlert(str);
1004 +        return false;
1005 +        }
1006 +        
1007 +        // Zero the mainBuffer structure
1008 +        mainBuffer.dirtyPages = 0;
1009 +        mainBuffer.pageInfo = 0;
1010 + #endif
1011 +        
1012 +        // Check if X server runs on local machine
1013 +        local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
1014 +                 || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
1015 +    
1016          // Init keycode translation
1017          keycode_init();
1018  
1019 +        // Read prefs
1020 +        mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1021 +        mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1022 +
1023          // Find screen and root window
1024          screen = XDefaultScreen(x_display);
1025          rootwin = XRootWindow(x_display, screen);
# Line 620 | Line 1027 | bool VideoInit(bool classic)
1027          // Get screen depth
1028          xdepth = DefaultDepth(x_display, screen);
1029          
1030 < #if ENABLE_FBDEV_DGA
1030 > #ifdef ENABLE_FBDEV_DGA
1031          // Frame buffer name
1032          char fb_name[20];
1033          
# Line 630 | Line 1037 | bool VideoInit(bool classic)
1037          else
1038                  has_dga = false;
1039   #endif
1040 <        
1041 < #if ENABLE_XF86_DGA
1040 >
1041 > #ifdef ENABLE_XF86_DGA
1042          // DGA available?
1043 <        int event_base, error_base;
1044 <        if (XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1043 >        int dga_event_base, dga_error_base;
1044 >        if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
1045                  int dga_flags = 0;
1046                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1047                  has_dga = dga_flags & XF86DGADirectPresent;
# Line 642 | Line 1049 | bool VideoInit(bool classic)
1049                  has_dga = false;
1050   #endif
1051  
1052 + #ifdef ENABLE_XF86_VIDMODE
1053 +        // VidMode available?
1054 +        int vm_event_base, vm_error_base;
1055 +        has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
1056 +        if (has_vidmode)
1057 +                XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1058 + #endif
1059 +        
1060          // Find black and white colors
1061          XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1062          XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
# Line 707 | Line 1122 | bool VideoInit(bool classic)
1122          if (mode_str) {
1123                  if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
1124                          display_type = DISPLAY_WINDOW;
1125 < #if ENABLE_FBDEV_DGA
1125 > #ifdef ENABLE_FBDEV_DGA
1126                  else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
1127   #else
1128                  else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
# Line 731 | Line 1146 | bool VideoInit(bool classic)
1146                                  return false;
1147                          break;
1148                  case DISPLAY_DGA:
1149 < #if ENABLE_FBDEV_DGA
1149 > #ifdef ENABLE_FBDEV_DGA
1150                          if (!init_fbdev_dga(fb_name))
1151   #else
1152                          if (!init_xf86_dga(width, height))
# Line 741 | Line 1156 | bool VideoInit(bool classic)
1156          }
1157  
1158          // Lock down frame buffer
1159 <        pthread_mutex_lock(&frame_buffer_lock);
1159 >        LOCK_FRAME_BUFFER;
1160  
1161 < #if !REAL_ADDRESSING
1161 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1162          // Set variables for UAE memory mapping
1163          MacFrameBaseHost = the_buffer;
1164 <        MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
1164 >        MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
1165  
1166          // No special frame buffer in Classic mode (frame buffer is in Mac RAM)
1167          if (classic)
1168                  MacFrameLayout = FLAYOUT_NONE;
1169   #endif
1170  
1171 <        // Start redraw/input thread
1171 > #ifdef ENABLE_VOSF
1172 >        if (use_vosf) {
1173 >                // Initialize the mainBuffer structure
1174 >                if (!VideoInitBuffer()) {
1175 >                        // TODO: STR_VOSF_INIT_ERR ?
1176 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1177 >                return false;
1178 >                }
1179 >
1180 >                // Initialize the handler for SIGSEGV
1181 >                if (!sigsegv_install_handler(screen_fault_handler)) {
1182 >                        // TODO: STR_VOSF_INIT_ERR ?
1183 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1184 >                        return false;
1185 >                }
1186 >        }
1187 > #endif
1188 >        
1189 >        // Initialize VideoRefresh function
1190 >        VideoRefreshInit();
1191 >        
1192          XSync(x_display, false);
1193 +
1194 + #ifdef HAVE_PTHREADS
1195 +        // Start redraw/input thread
1196          redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1197 <        if (!redraw_thread_active)
1197 >        if (!redraw_thread_active) {
1198                  printf("FATAL: cannot create redraw thread\n");
1199 <        return redraw_thread_active;
1199 >                return false;
1200 >        }
1201 > #endif
1202 >        return true;
1203   }
1204  
1205  
# Line 768 | Line 1209 | bool VideoInit(bool classic)
1209  
1210   void VideoExit(void)
1211   {
1212 + #ifdef HAVE_PTHREADS
1213          // Stop redraw thread
1214          if (redraw_thread_active) {
1215                  redraw_thread_cancel = true;
# Line 777 | Line 1219 | void VideoExit(void)
1219                  pthread_join(redraw_thread, NULL);
1220                  redraw_thread_active = false;
1221          }
1222 + #endif
1223  
1224          // Unlock frame buffer
1225 <        pthread_mutex_unlock(&frame_buffer_lock);
1225 >        UNLOCK_FRAME_BUFFER;
1226  
1227          // Close window and server connection
1228          if (x_display != NULL) {
1229                  XSync(x_display, false);
1230  
1231 < #if ENABLE_XF86_DGA
1231 > #ifdef ENABLE_XF86_DGA
1232                  if (display_type == DISPLAY_DGA) {
1233                          XF86DGADirectVideo(x_display, screen, 0);
1234                          XUngrabPointer(x_display, CurrentTime);
# Line 793 | Line 1236 | void VideoExit(void)
1236                  }
1237   #endif
1238  
1239 < #if ENABLE_FBDEV_DGA
1239 > #ifdef ENABLE_XF86_VIDMODE
1240 >                if (has_vidmode && display_type == DISPLAY_DGA)
1241 >                        XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1242 > #endif
1243 >
1244 > #ifdef ENABLE_FBDEV_DGA
1245                  if (display_type == DISPLAY_DGA) {
1246                          XUngrabPointer(x_display, CurrentTime);
1247                          XUngrabKeyboard(x_display, CurrentTime);
1248                          close(fbdev_fd);
1249                  }
1250   #endif
803                
804                if (the_buffer_copy) {
805                        free(the_buffer_copy);
806                        the_buffer_copy = NULL;
807                }
1251  
1252                  XFlush(x_display);
1253                  XSync(x_display, false);
# Line 812 | Line 1255 | void VideoExit(void)
1255                          XFreeColormap(x_display, cmap[0]);
1256                          XFreeColormap(x_display, cmap[1]);
1257                  }
1258 +                
1259 +                if (!use_vosf) {
1260 +                        if (the_buffer) {
1261 +                                free(the_buffer);
1262 +                                the_buffer = NULL;
1263 +                        }
1264 +
1265 +                        if (!have_shm && the_buffer_copy) {
1266 +                                free(the_buffer_copy);
1267 +                                the_buffer_copy = NULL;
1268 +                        }
1269 +                }
1270 + #ifdef ENABLE_VOSF
1271 +                else {
1272 +                        if (the_buffer != (uint8 *)VM_MAP_FAILED) {
1273 +                                vm_release(the_buffer, the_buffer_size);
1274 +                                the_buffer = 0;
1275 +                        }
1276 +                        
1277 +                        if (the_buffer_copy != (uint8 *)VM_MAP_FAILED) {
1278 +                                vm_release(the_buffer_copy, the_buffer_size);
1279 +                                the_buffer_copy = 0;
1280 +                        }
1281 +                }
1282 + #endif
1283 +        }
1284 +        
1285 + #ifdef ENABLE_VOSF
1286 +        if (use_vosf) {
1287 +                // Clear mainBuffer data
1288 +                if (mainBuffer.pageInfo) {
1289 +                        free(mainBuffer.pageInfo);
1290 +                        mainBuffer.pageInfo = 0;
1291 +                }
1292 +
1293 +                if (mainBuffer.dirtyPages) {
1294 +                        free(mainBuffer.dirtyPages);
1295 +                        mainBuffer.dirtyPages = 0;
1296 +                }
1297          }
1298 +
1299 +        // Close /dev/zero
1300 +        if (zero_fd > 0)
1301 +                close(zero_fd);
1302 + #endif
1303   }
1304  
1305  
# Line 840 | Line 1327 | void VideoInterrupt(void)
1327  
1328          // Temporarily give up frame buffer lock (this is the point where
1329          // we are suspended when the user presses Ctrl-Tab)
1330 <        pthread_mutex_unlock(&frame_buffer_lock);
1331 <        pthread_mutex_lock(&frame_buffer_lock);
1330 >        UNLOCK_FRAME_BUFFER;
1331 >        LOCK_FRAME_BUFFER;
1332   }
1333  
1334  
# Line 851 | Line 1338 | void VideoInterrupt(void)
1338  
1339   void video_set_palette(uint8 *pal)
1340   {
1341 <        pthread_mutex_lock(&palette_lock);
1341 >        LOCK_PALETTE;
1342  
1343          // Convert colors to XColor array
1344          for (int i=0; i<256; i++) {
# Line 865 | Line 1352 | void video_set_palette(uint8 *pal)
1352          // Tell redraw thread to change palette
1353          palette_changed = true;
1354  
1355 <        pthread_mutex_unlock(&palette_lock);
1355 >        UNLOCK_PALETTE;
1356   }
1357  
1358  
# Line 873 | Line 1360 | void video_set_palette(uint8 *pal)
1360   *  Suspend/resume emulator
1361   */
1362  
1363 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1363 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1364   static void suspend_emul(void)
1365   {
1366          if (display_type == DISPLAY_DGA) {
# Line 882 | Line 1369 | static void suspend_emul(void)
1369                  ctrl_down = false;
1370  
1371                  // Lock frame buffer (this will stop the MacOS thread)
1372 <                pthread_mutex_lock(&frame_buffer_lock);
1372 >                LOCK_FRAME_BUFFER;
1373  
1374                  // Save frame buffer
1375 <                fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
1375 >                fb_save = malloc(VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
1376                  if (fb_save)
1377 <                        memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
1377 >                        memcpy(fb_save, the_buffer, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
1378  
1379                  // Close full screen display
1380 < #if ENABLE_XF86_DGA
1380 > #ifdef ENABLE_XF86_DGA
1381                  XF86DGADirectVideo(x_display, screen, 0);
1382   #endif
1383                  XUngrabPointer(x_display, CurrentTime);
1384                  XUngrabKeyboard(x_display, CurrentTime);
1385                  XUnmapWindow(x_display, the_win);
1386 <                XSync(x_display, false);
1386 >                wait_unmapped(the_win);
1387  
1388                  // Open "suspend" window
1389                  XSetWindowAttributes wattr;
1390                  wattr.event_mask = KeyPressMask;
1391                  wattr.background_pixel = black_pixel;
905                wattr.border_pixel = black_pixel;
906                wattr.backing_store = Always;
907                wattr.backing_planes = xdepth;
908                wattr.colormap = DefaultColormap(x_display, screen);
1392                  
910                XSync(x_display, false);
1393                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1394 <                        InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1395 <                        CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1396 <                XSync(x_display, false);
1397 <                XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
916 <                XMapRaised(x_display, suspend_win);
917 <                XSync(x_display, false);
1394 >                        InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
1395 >                set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE);
1396 >                set_window_focus(suspend_win);
1397 >                XMapWindow(x_display, suspend_win);
1398                  emul_suspended = true;
1399          }
1400   }
# Line 927 | Line 1407 | static void resume_emul(void)
1407  
1408          // Reopen full screen display
1409          XMapRaised(x_display, the_win);
1410 +        wait_mapped(the_win);
1411          XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
931        XSync(x_display, false);
1412          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1413          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1414 < #if ENABLE_XF86_DGA
1414 > #ifdef ENABLE_XF86_DGA
1415          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1416          XF86DGASetViewPort(x_display, screen, 0, 0);
1417   #endif
1418          XSync(x_display, false);
1419 <
1419 >        
1420 >        // the_buffer already contains the data to restore. i.e. since a temporary
1421 >        // frame buffer is used when VOSF is actually used, fb_save is therefore
1422 >        // not necessary.
1423 > #ifdef ENABLE_VOSF
1424 >        if (use_vosf) {
1425 >                LOCK_VOSF;
1426 >                PFLAG_SET_ALL;
1427 >                UNLOCK_VOSF;
1428 >                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1429 >        }
1430 > #endif
1431 >        
1432          // Restore frame buffer
1433          if (fb_save) {
1434 <                memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1434 > #ifdef ENABLE_VOSF
1435 >                // Don't copy fb_save to the temporary frame buffer in VOSF mode
1436 >                if (!use_vosf)
1437 > #endif
1438 >                memcpy(the_buffer, fb_save, VideoMonitor.mode.y * VideoMonitor.mode.bytes_per_row);
1439                  free(fb_save);
1440                  fb_save = NULL;
1441          }
1442          
1443 + #ifdef ENABLE_XF86_DGA
1444          if (depth == 8)
948 #if ENABLE_XF86_DGA
1445                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1446   #endif
951 #if ENABLE_FBDEV_DGA
952                XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap]);
953 #endif
1447  
1448          // Unlock frame buffer (and continue MacOS thread)
1449 <        pthread_mutex_unlock(&frame_buffer_lock);
1449 >        UNLOCK_FRAME_BUFFER;
1450          emul_suspended = false;
1451   }
1452   #endif
# Line 1016 | Line 1509 | static int kc_decode(KeySym ks)
1509                  case XK_period: case XK_greater: return 0x2f;
1510                  case XK_slash: case XK_question: return 0x2c;
1511  
1512 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1512 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1513                  case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1514   #else
1515                  case XK_Tab: return 0x30;
# Line 1108 | Line 1601 | static int kc_decode(KeySym ks)
1601          return -1;
1602   }
1603  
1604 < static int event2keycode(XKeyEvent *ev)
1604 > static int event2keycode(XKeyEvent &ev)
1605   {
1606          KeySym ks;
1607          int as;
1608          int i = 0;
1609  
1610          do {
1611 <                ks = XLookupKeysym(ev, i++);
1611 >                ks = XLookupKeysym(&ev, i++);
1612                  as = kc_decode(ks);
1613                  if (as != -1)
1614                          return as;
# Line 1131 | Line 1624 | static int event2keycode(XKeyEvent *ev)
1624  
1625   static void handle_events(void)
1626   {
1627 <        XEvent event;
1628 <        for (;;) {
1629 <                if (!XCheckMaskEvent(x_display, eventmask, &event))
1137 <                        break;
1627 >        while (XPending(x_display)) {
1628 >                XEvent event;
1629 >                XNextEvent(x_display, &event);
1630  
1631                  switch (event.type) {
1632                          // Mouse button
1633                          case ButtonPress: {
1634 <                                unsigned int button = ((XButtonEvent *)&event)->button;
1634 >                                unsigned int button = event.xbutton.button;
1635                                  if (button < 4)
1636                                          ADBMouseDown(button - 1);
1637 +                                else if (button < 6) {  // Wheel mouse
1638 +                                        if (mouse_wheel_mode == 0) {
1639 +                                                int key = (button == 5) ? 0x79 : 0x74;  // Page up/down
1640 +                                                ADBKeyDown(key);
1641 +                                                ADBKeyUp(key);
1642 +                                        } else {
1643 +                                                int key = (button == 5) ? 0x3d : 0x3e;  // Cursor up/down
1644 +                                                for(int i=0; i<mouse_wheel_lines; i++) {
1645 +                                                        ADBKeyDown(key);
1646 +                                                        ADBKeyUp(key);
1647 +                                                }
1648 +                                        }
1649 +                                }
1650                                  break;
1651                          }
1652                          case ButtonRelease: {
1653 <                                unsigned int button = ((XButtonEvent *)&event)->button;
1653 >                                unsigned int button = event.xbutton.button;
1654                                  if (button < 4)
1655                                          ADBMouseUp(button - 1);
1656                                  break;
# Line 1153 | Line 1658 | static void handle_events(void)
1658  
1659                          // Mouse moved
1660                          case EnterNotify:
1156                                ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1157                                break;
1661                          case MotionNotify:
1662 <                                ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1662 >                                ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1663                                  break;
1664  
1665                          // Keyboard
1666                          case KeyPress: {
1667                                  int code;
1668                                  if (use_keycodes) {
1669 <                                        event2keycode((XKeyEvent *)&event);     // This is called to process the hotkeys
1670 <                                        code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1669 >                                        event2keycode(event.xkey);      // This is called to process the hotkeys
1670 >                                        code = keycode_table[event.xkey.keycode & 0xff];
1671                                  } else
1672 <                                        code = event2keycode((XKeyEvent *)&event);
1672 >                                        code = event2keycode(event.xkey);
1673                                  if (code != -1) {
1674                                          if (!emul_suspended) {
1675                                                  if (code == 0x39) {     // Caps Lock pressed
# Line 1182 | Line 1685 | static void handle_events(void)
1685                                                  if (code == 0x36)
1686                                                          ctrl_down = true;
1687                                          } else {
1688 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1688 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1689                                                  if (code == 0x31)
1690                                                          resume_emul();  // Space wakes us up
1691   #endif
# Line 1193 | Line 1696 | static void handle_events(void)
1696                          case KeyRelease: {
1697                                  int code;
1698                                  if (use_keycodes) {
1699 <                                        event2keycode((XKeyEvent *)&event);     // This is called to process the hotkeys
1700 <                                        code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1699 >                                        event2keycode(event.xkey);      // This is called to process the hotkeys
1700 >                                        code = keycode_table[event.xkey.keycode & 0xff];
1701                                  } else
1702 <                                        code = event2keycode((XKeyEvent *)&event);
1702 >                                        code = event2keycode(event.xkey);
1703                                  if (code != -1 && code != 0x39) {       // Don't propagate Caps Lock releases
1704                                          ADBKeyUp(code);
1705                                          if (code == 0x36)
# Line 1207 | Line 1710 | static void handle_events(void)
1710  
1711                          // Hidden parts exposed, force complete refresh of window
1712                          case Expose:
1713 <                                if (display_type == DISPLAY_WINDOW)
1714 <                                        memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1713 >                                if (display_type == DISPLAY_WINDOW) {
1714 > #ifdef ENABLE_VOSF
1715 >                                        if (use_vosf) {                 // VOSF refresh
1716 >                                                LOCK_VOSF;
1717 >                                                PFLAG_SET_ALL;
1718 >                                                UNLOCK_VOSF;
1719 >                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1720 >                                        }
1721 >                                        else
1722 > #endif
1723 >                                        if (frame_skip == 0) {  // Dynamic refresh
1724 >                                                int x1, y1;
1725 >                                                for (y1=0; y1<16; y1++)
1726 >                                                for (x1=0; x1<16; x1++)
1727 >                                                        updt_box[x1][y1] = true;
1728 >                                                nr_boxes = 16 * 16;
1729 >                                        } else                                  // Static refresh
1730 >                                                memset(the_buffer_copy, 0, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
1731 >                                }
1732 >                                break;
1733 >
1734 >                        // Window "close" widget clicked
1735 >                        case ClientMessage:
1736 >                                if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1737 >                                        ADBKeyDown(0x7f);       // Power key
1738 >                                        ADBKeyUp(0x7f);
1739 >                                }
1740                                  break;
1741                  }
1742          }
# Line 1219 | Line 1747 | static void handle_events(void)
1747   *  Window display update
1748   */
1749  
1750 < static void update_display(void)
1750 > // Dynamic display update (variable frame rate for each box)
1751 > static void update_display_dynamic(int ticker)
1752 > {
1753 >        int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1754 >        int xil = 0;
1755 >        int rxm = 0, rxmo = 0;
1756 >        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
1757 >        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
1758 >        int rx = VideoMonitor.mode.bytes_per_row / 16;
1759 >        int ry = VideoMonitor.mode.y / 16;
1760 >        int max_box;
1761 >
1762 >        y2s = sm_uptd[ticker % 8];
1763 >        y2a = 8;
1764 >        for (i = 0; i < 6; i++)
1765 >                if (ticker % (2 << i))
1766 >                        break;
1767 >        max_box = sm_no_boxes[i];
1768 >
1769 >        if (y2a) {
1770 >                for (y1=0; y1<16; y1++) {
1771 >                        for (y2=y2s; y2 < ry; y2 += y2a) {
1772 >                                i = ((y1 * ry) + y2) * bytes_per_row;
1773 >                                for (x1=0; x1<16; x1++, i += rx) {
1774 >                                        if (updt_box[x1][y1] == false) {
1775 >                                                if (memcmp(&the_buffer_copy[i], &the_buffer[i], rx)) {
1776 >                                                        updt_box[x1][y1] = true;
1777 >                                                        nr_boxes++;
1778 >                                                }
1779 >                                        }
1780 >                                }
1781 >                        }
1782 >                }
1783 >        }
1784 >
1785 >        if ((nr_boxes <= max_box) && (nr_boxes)) {
1786 >                for (y1=0; y1<16; y1++) {
1787 >                        for (x1=0; x1<16; x1++) {
1788 >                                if (updt_box[x1][y1] == true) {
1789 >                                        if (rxm == 0)
1790 >                                                xm = x1;
1791 >                                        rxm += rx;
1792 >                                        updt_box[x1][y1] = false;
1793 >                                }
1794 >                                if (((updt_box[x1+1][y1] == false) || (x1 == 15)) && (rxm)) {
1795 >                                        if ((rxmo != rxm) || (xmo != xm) || (yo != y1 - 1)) {
1796 >                                                if (rxmo) {
1797 >                                                        xi = xmo * rx;
1798 >                                                        yi = ymo * ry;
1799 >                                                        xil = rxmo;
1800 >                                                        yil = (yo - ymo +1) * ry;
1801 >                                                }
1802 >                                                rxmo = rxm;
1803 >                                                xmo = xm;
1804 >                                                ymo = y1;
1805 >                                        }
1806 >                                        rxm = 0;
1807 >                                        yo = y1;
1808 >                                }      
1809 >                                if (xil) {
1810 >                                        i = (yi * bytes_per_row) + xi;
1811 >                                        for (y2=0; y2 < yil; y2++, i += bytes_per_row)
1812 >                                                memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
1813 >                                        if (depth == 1) {
1814 >                                                if (have_shm)
1815 >                                                        XShmPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
1816 >                                                else
1817 >                                                        XPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil);
1818 >                                        } else {
1819 >                                                if (have_shm)
1820 >                                                        XShmPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0);
1821 >                                                else
1822 >                                                        XPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil);
1823 >                                        }
1824 >                                        xil = 0;
1825 >                                }
1826 >                                if ((x1 == 15) && (y1 == 15) && (rxmo)) {
1827 >                                        x1--;
1828 >                                        xi = xmo * rx;
1829 >                                        yi = ymo * ry;
1830 >                                        xil = rxmo;
1831 >                                        yil = (yo - ymo +1) * ry;
1832 >                                        rxmo = 0;
1833 >                                }
1834 >                        }
1835 >                }
1836 >                nr_boxes = 0;
1837 >        }
1838 > }
1839 >
1840 > // Static display update (fixed frame rate, but incremental)
1841 > static void update_display_static(void)
1842   {
1224        // In classic mode, copy the frame buffer from Mac RAM
1225        if (classic_mode)
1226                memcpy(the_buffer, Mac2HostAddr(0x3fa700), VideoMonitor.bytes_per_row * VideoMonitor.y);
1227        
1843          // Incremental update code
1844          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1845 <        int bytes_per_row = VideoMonitor.bytes_per_row;
1846 <        int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1845 >        int bytes_per_row = VideoMonitor.mode.bytes_per_row;
1846 >        int bytes_per_pixel = VideoMonitor.mode.bytes_per_row / VideoMonitor.mode.x;
1847          uint8 *p, *p2;
1848  
1849          // Check for first line from top and first line from bottom that have changed
1850          y1 = 0;
1851 <        for (j=0; j<VideoMonitor.y; j++) {
1851 >        for (j=0; j<VideoMonitor.mode.y; j++) {
1852                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1853                          y1 = j;
1854                          break;
1855                  }
1856          }
1857          y2 = y1 - 1;
1858 <        for (j=VideoMonitor.y-1; j>=y1; j--) {
1858 >        for (j=VideoMonitor.mode.y-1; j>=y1; j--) {
1859                  if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1860                          y2 = j;
1861                          break;
# Line 1251 | Line 1866 | static void update_display(void)
1866          // Check for first column from left and first column from right that have changed
1867          if (high) {
1868                  if (depth == 1) {
1869 <                        x1 = VideoMonitor.x;
1869 >                        x1 = VideoMonitor.mode.x - 1;
1870                          for (j=y1; j<=y2; j++) {
1871                                  p = &the_buffer[j * bytes_per_row];
1872                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1260 | Line 1875 | static void update_display(void)
1875                                                  x1 = i << 3;
1876                                                  break;
1877                                          }
1878 <                                        p++;
1264 <                                        p2++;
1878 >                                        p++; p2++;
1879                                  }
1880                          }
1881                          x2 = x1;
# Line 1270 | Line 1884 | static void update_display(void)
1884                                  p2 = &the_buffer_copy[j * bytes_per_row];
1885                                  p += bytes_per_row;
1886                                  p2 += bytes_per_row;
1887 <                                for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1888 <                                        p--;
1275 <                                        p2--;
1887 >                                for (i=(VideoMonitor.mode.x>>3); i>(x2>>3); i--) {
1888 >                                        p--; p2--;
1889                                          if (*p != *p2) {
1890 <                                                x2 = i << 3;
1890 >                                                x2 = (i << 3) + 7;
1891                                                  break;
1892                                          }
1893                                  }
1894                          }
1895 <                        wide = x2 - x1;
1895 >                        wide = x2 - x1 + 1;
1896  
1897                          // Update copy of the_buffer
1898                          if (high && wide) {
1899                                  for (j=y1; j<=y2; j++) {
1900                                          i = j * bytes_per_row + (x1 >> 3);
1901 <                                        memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
1901 >                                        memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3);
1902                                  }
1903                          }
1904  
1905                  } else {
1906 <                        x1 = VideoMonitor.x;
1906 >                        x1 = VideoMonitor.mode.x;
1907                          for (j=y1; j<=y2; j++) {
1908                                  p = &the_buffer[j * bytes_per_row];
1909                                  p2 = &the_buffer_copy[j * bytes_per_row];
1910 <                                for (i=0; i<x1; i++) {
1911 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1912 <                                                x1 = i;
1910 >                                for (i=0; i<x1*bytes_per_pixel; i++) {
1911 >                                        if (*p != *p2) {
1912 >                                                x1 = i / bytes_per_pixel;
1913                                                  break;
1914                                          }
1915 <                                        p += bytes_per_pixel;
1303 <                                        p2 += bytes_per_pixel;
1915 >                                        p++; p2++;
1916                                  }
1917                          }
1918                          x2 = x1;
# Line 1309 | Line 1921 | static void update_display(void)
1921                                  p2 = &the_buffer_copy[j * bytes_per_row];
1922                                  p += bytes_per_row;
1923                                  p2 += bytes_per_row;
1924 <                                for (i=VideoMonitor.x; i>x2; i--) {
1925 <                                        p -= bytes_per_pixel;
1926 <                                        p2 -= bytes_per_pixel;
1927 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1928 <                                                x2 = i;
1924 >                                for (i=VideoMonitor.mode.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1925 >                                        p--;
1926 >                                        p2--;
1927 >                                        if (*p != *p2) {
1928 >                                                x2 = i / bytes_per_pixel;
1929                                                  break;
1930                                          }
1931                                  }
# Line 1324 | Line 1936 | static void update_display(void)
1936                          if (high && wide) {
1937                                  for (j=y1; j<=y2; j++) {
1938                                          i = j * bytes_per_row + x1 * bytes_per_pixel;
1939 <                                        memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
1939 >                                        memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1940                                  }
1941                          }
1942                  }
# Line 1337 | Line 1949 | static void update_display(void)
1949                  else
1950                          XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1951          }
1340
1341        // Has the Mac started? (cursor data is not valid otherwise)
1342        if (HasMacStarted()) {
1343
1344                // Set new cursor image if it was changed
1345                if (memcmp(the_cursor, Mac2HostAddr(0x844), 64)) {
1346                        memcpy(the_cursor, Mac2HostAddr(0x844), 64);
1347                        memcpy(cursor_image->data, the_cursor, 32);
1348                        memcpy(cursor_mask_image->data, the_cursor+32, 32);
1349                        XFreeCursor(x_display, mac_cursor);
1350                        XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
1351                        XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
1352                        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, ReadMacInt8(0x885), ReadMacInt8(0x887));
1353                        XDefineCursor(x_display, the_win, mac_cursor);
1354                }
1355        }
1952   }
1953  
1954  
1955   /*
1956 < *  Thread for screen refresh, input handling etc.
1956 > *      Screen refresh functions
1957   */
1958  
1959 < static void *redraw_func(void *arg)
1960 < {
1961 <        int tick_counter = 0;
1959 > // We suggest the compiler to inline the next two functions so that it
1960 > // may specialise the code according to the current screen depth and
1961 > // display type. A clever compiler would do that job by itself though...
1962  
1963 <        while (!redraw_thread_cancel) {
1963 > // NOTE: update_display_vosf is inlined too
1964  
1965 <                // Wait
1966 < #ifdef HAVE_NANOSLEEP
1967 <                struct timespec req = {0, 16666667};
1968 <                nanosleep(&req, NULL);
1969 < #else
1970 <                usleep(16667);
1965 > static inline void possibly_quit_dga_mode()
1966 > {
1967 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1968 >        // Quit DGA mode if requested
1969 >        if (quit_full_screen) {
1970 >                quit_full_screen = false;
1971 > #ifdef ENABLE_XF86_DGA
1972 >                XF86DGADirectVideo(x_display, screen, 0);
1973   #endif
1974 +                XUngrabPointer(x_display, CurrentTime);
1975 +                XUngrabKeyboard(x_display, CurrentTime);
1976 +                XUnmapWindow(x_display, the_win);
1977 +                XSync(x_display, false);
1978 +        }
1979 + #endif
1980 + }
1981  
1982 < #if ENABLE_XF86_DGA
1983 <                // Quit DGA mode if requested
1984 <                if (quit_full_screen) {
1985 <                        quit_full_screen = false;
1982 > static inline void handle_palette_changes(int depth, int display_type)
1983 > {
1984 >        LOCK_PALETTE;
1985 >
1986 >        if (palette_changed) {
1987 >                palette_changed = false;
1988 >                if (depth == 8) {
1989 >                        XStoreColors(x_display, cmap[0], palette, 256);
1990 >                        XStoreColors(x_display, cmap[1], palette, 256);
1991 >                        XSync(x_display, false);
1992 >                                
1993 > #ifdef ENABLE_XF86_DGA
1994                          if (display_type == DISPLAY_DGA) {
1995 <                                XF86DGADirectVideo(x_display, screen, 0);
1996 <                                XUngrabPointer(x_display, CurrentTime);
1384 <                                XUngrabKeyboard(x_display, CurrentTime);
1385 <                                XUnmapWindow(x_display, the_win);
1386 <                                XSync(x_display, false);
1995 >                                current_dga_cmap ^= 1;
1996 >                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1997                          }
1388                }
1998   #endif
1999 +                }
2000 +        }
2001  
2002 < #if ENABLE_FBDEV_DGA
2003 <                // Quit DGA mode if requested
2004 <                if (quit_full_screen) {
2005 <                        quit_full_screen = false;
2006 <                        if (display_type == DISPLAY_DGA) {
2007 <                                XUngrabPointer(x_display, CurrentTime);
2008 <                                XUngrabKeyboard(x_display, CurrentTime);
2009 <                                XUnmapWindow(x_display, the_win);
2010 <                                XSync(x_display, false);
2011 <                        }
2002 >        UNLOCK_PALETTE;
2003 > }
2004 >
2005 > static void video_refresh_dga(void)
2006 > {
2007 >        // Quit DGA mode if requested
2008 >        possibly_quit_dga_mode();
2009 >        
2010 >        // Handle X events
2011 >        handle_events();
2012 >        
2013 >        // Handle palette changes
2014 >        handle_palette_changes(depth, DISPLAY_DGA);
2015 > }
2016 >
2017 > #ifdef ENABLE_VOSF
2018 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
2019 > static void video_refresh_dga_vosf(void)
2020 > {
2021 >        // Quit DGA mode if requested
2022 >        possibly_quit_dga_mode();
2023 >        
2024 >        // Handle X events
2025 >        handle_events();
2026 >        
2027 >        // Handle palette changes
2028 >        handle_palette_changes(depth, DISPLAY_DGA);
2029 >        
2030 >        // Update display (VOSF variant)
2031 >        static int tick_counter = 0;
2032 >        if (++tick_counter >= frame_skip) {
2033 >                tick_counter = 0;
2034 >                if (mainBuffer.dirty) {
2035 >                        LOCK_VOSF;
2036 >                        update_display_dga_vosf();
2037 >                        UNLOCK_VOSF;
2038                  }
2039 +        }
2040 + }
2041   #endif
1403                // Handle X events
1404                handle_events();
2042  
2043 <                // Handle palette changes
2044 <                pthread_mutex_lock(&palette_lock);
2045 <                if (palette_changed) {
2046 <                        palette_changed = false;
2047 <                        if (depth == 8) {
2048 <                                XStoreColors(x_display, cmap[0], palette, 256);
2049 <                                XStoreColors(x_display, cmap[1], palette, 256);
2050 <                                
2051 < #if ENABLE_XF86_DGA
2052 <                                if (display_type == DISPLAY_DGA) {
2053 <                                        current_dga_cmap ^= 1;
2054 <                                        XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2055 <                                }
2043 > static void video_refresh_window_vosf(void)
2044 > {
2045 >        // Quit DGA mode if requested
2046 >        possibly_quit_dga_mode();
2047 >        
2048 >        // Handle X events
2049 >        handle_events();
2050 >        
2051 >        // Handle palette changes
2052 >        handle_palette_changes(depth, DISPLAY_WINDOW);
2053 >        
2054 >        // Update display (VOSF variant)
2055 >        static int tick_counter = 0;
2056 >        if (++tick_counter >= frame_skip) {
2057 >                tick_counter = 0;
2058 >                if (mainBuffer.dirty) {
2059 >                        LOCK_VOSF;
2060 >                        update_display_window_vosf();
2061 >                        UNLOCK_VOSF;
2062 >                        XSync(x_display, false); // Let the server catch up
2063 >                }
2064 >        }
2065 > }
2066 > #endif // def ENABLE_VOSF
2067 >
2068 > static void video_refresh_window_static(void)
2069 > {
2070 >        // Handle X events
2071 >        handle_events();
2072 >        
2073 >        // Handle_palette changes
2074 >        handle_palette_changes(depth, DISPLAY_WINDOW);
2075 >        
2076 >        // Update display (static variant)
2077 >        static int tick_counter = 0;
2078 >        if (++tick_counter >= frame_skip) {
2079 >                tick_counter = 0;
2080 >                update_display_static();
2081 >        }
2082 > }
2083 >
2084 > static void video_refresh_window_dynamic(void)
2085 > {
2086 >        // Handle X events
2087 >        handle_events();
2088 >        
2089 >        // Handle_palette changes
2090 >        handle_palette_changes(depth, DISPLAY_WINDOW);
2091 >        
2092 >        // Update display (dynamic variant)
2093 >        static int tick_counter = 0;
2094 >        tick_counter++;
2095 >        update_display_dynamic(tick_counter);
2096 > }
2097 >
2098 >
2099 > /*
2100 > *  Thread for screen refresh, input handling etc.
2101 > */
2102 >
2103 > void VideoRefreshInit(void)
2104 > {
2105 >        // TODO: set up specialised 8bpp VideoRefresh handlers ?
2106 >        if (display_type == DISPLAY_DGA) {
2107 > #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2108 >                if (use_vosf)
2109 >                        video_refresh = video_refresh_dga_vosf;
2110 >                else
2111   #endif
2112 <                                
2113 < #if ENABLE_FBDEV_DGA
2114 <                                if (display_type == DISPLAY_DGA)
2115 <                                        XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap]);
2112 >                        video_refresh = video_refresh_dga;
2113 >        }
2114 >        else {
2115 > #ifdef ENABLE_VOSF
2116 >                if (use_vosf)
2117 >                        video_refresh = video_refresh_window_vosf;
2118 >                else
2119   #endif
2120 <                        }
2121 <                }
2122 <                pthread_mutex_unlock(&palette_lock);
2120 >                if (frame_skip == 0)
2121 >                        video_refresh = video_refresh_window_dynamic;
2122 >                else
2123 >                        video_refresh = video_refresh_window_static;
2124 >        }
2125 > }
2126  
2127 <                // In window mode, update display and mouse pointer
2128 <                if (display_type == DISPLAY_WINDOW) {
2129 <                        tick_counter++;
2130 <                        if (tick_counter >= frame_skip) {
2131 <                                tick_counter = 0;
2132 <                                update_display();
2133 <                        }
2134 <                }
2127 > void VideoRefresh(void)
2128 > {
2129 >        // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2130 >        video_refresh();
2131 > }
2132 >
2133 > #ifdef HAVE_PTHREADS
2134 > static void *redraw_func(void *arg)
2135 > {
2136 >        uint64 start = GetTicks_usec();
2137 >        int64 ticks = 0;
2138 >        uint64 next = GetTicks_usec();
2139 >        while (!redraw_thread_cancel) {
2140 >                video_refresh();
2141 >                next += 16667;
2142 >                int64 delay = next - GetTicks_usec();
2143 >                if (delay > 0)
2144 >                        Delay_usec(delay);
2145 >                else if (delay < -16667)
2146 >                        next = GetTicks_usec();
2147 >                ticks++;
2148          }
2149 +        uint64 end = GetTicks_usec();
2150 +        // printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, ticks * 1000000 / (end - start));
2151          return NULL;
2152   }
2153 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines