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.14 by cebix, 2000-04-10T18:53:10Z vs.
Revision 1.28 by cebix, 2000-11-03T12:23:49Z

# 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 <unistd.h>
57 + # include <signal.h>
58 + # include <fcntl.h>
59 + # include <sys/mman.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_XF86_VIDMODE
56 #include <X11/extensions/xf86vmode.h>
57 #endif
58
59 #if ENABLE_FBDEV_DGA
60 #include <sys/mman.h>
61 #endif
62
63
73  
74   // Display types
75   enum {
# Line 79 | Line 88 | 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
# Line 109 | 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;
125 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | FocusChangeMask | ExposureMask;
126   static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
127  
115 static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;        // Mutex to protect palette
128   static XColor palette[256];                                                     // Color palette for 8-bit mode
129   static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
130 + #ifdef HAVE_PTHREADS
131 + static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;        // Mutex to protect palette
132 + #endif
133  
134   // Variables for window mode
135   static GC the_gc;
136   static XImage *img = NULL;
137   static XShmSegmentInfo shminfo;
123 static XImage *cursor_image, *cursor_mask_image;
124 static Pixmap cursor_map, cursor_mask_map;
138   static Cursor mac_cursor;
126 static GC cursor_gc, cursor_mask_gc;
139   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer
128 static uint8 the_cursor[64];                                            // Cursor image data
140   static bool have_shm = false;                                           // Flag: SHM extensions available
141   static bool updt_box[17][17];                                           // Flag for Update
142   static int nr_boxes;
# Line 136 | Line 147 | static int sm_no_boxes[] = {1,8,32,64,12
147   static int current_dga_cmap;                                            // Number (0 or 1) of currently installed DGA colormap
148   static Window suspend_win;                                                      // "Suspend" window
149   static void *fb_save = NULL;                                            // Saved frame buffer for suspend
150 + #ifdef HAVE_PTHREADS
151   static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer
152 + #endif
153  
154   // Variables for fbdev DGA mode
155   const char FBDEVICE_FILE_NAME[] = "/dev/fb";
156   static int fbdev_fd;
157  
158 < #if ENABLE_XF86_VIDMODE
158 > #ifdef ENABLE_XF86_VIDMODE
159   // Variables for XF86 VidMode support
160   static XF86VidModeModeInfo **x_video_modes;                     // Array of all available modes
161   static int num_x_video_modes;
162   #endif
163  
164 + #ifdef ENABLE_VOSF
165 + static bool use_vosf = true;                                            // Flag: VOSF enabled
166 + #else
167 + static const bool use_vosf = false;                                     // Flag: VOSF enabled
168 + #endif
169 +
170 + #ifdef ENABLE_VOSF
171 + static uint8 * the_host_buffer;                                         // Host frame buffer in VOSF mode
172 + static uint32 the_buffer_size;                                          // Size of allocated the_buffer
173 + #endif
174 +
175 + #ifdef ENABLE_VOSF
176 + // Variables for Video on SEGV support (taken from the Win32 port)
177 + struct ScreenPageInfo {
178 +    int top, bottom;                    // Mapping between this virtual page and Mac scanlines
179 + };
180 +
181 + struct ScreenInfo {
182 +    uint32 memBase;                             // Real start address
183 +    uint32 memStart;                    // Start address aligned to page boundary
184 +    uint32 memEnd;                              // Address of one-past-the-end of the screen
185 +    uint32 memLength;                   // Length of the memory addressed by the screen pages
186 +    
187 +    uint32 pageSize;                    // Size of a page
188 +    int pageBits;                               // Shift count to get the page number
189 +    uint32 pageCount;                   // Number of pages allocated to the screen
190 +    
191 +    uint8 * dirtyPages;                 // Table of flags set if page was altered
192 +    ScreenPageInfo * pageInfo;  // Table of mappings page -> Mac scanlines
193 + };
194 +
195 + static ScreenInfo mainBuffer;
196 +
197 + #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = 1
198 + #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = 0
199 + #define PFLAG_ISSET(page)               mainBuffer.dirtyPages[page]
200 + #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] == 0)
201 + #ifdef UNALIGNED_PROFITABLE
202 + # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + page)) == 0)
203 + #else
204 + # define PFLAG_ISCLEAR_4(page)  \
205 +                (mainBuffer.dirtyPages[page  ] == 0) \
206 +        &&      (mainBuffer.dirtyPages[page+1] == 0) \
207 +        &&      (mainBuffer.dirtyPages[page+2] == 0) \
208 +        &&      (mainBuffer.dirtyPages[page+3] == 0)
209 + #endif
210 + #define PFLAG_CLEAR_ALL                 memset(mainBuffer.dirtyPages, 0, mainBuffer.pageCount)
211 + #define PFLAG_SET_ALL                   memset(mainBuffer.dirtyPages, 1, mainBuffer.pageCount)
212 +
213 + static int zero_fd = -1;
214 + static bool Screen_fault_handler_init();
215 + static struct sigaction vosf_sa;
216 +
217 + #ifdef HAVE_PTHREADS
218 + static pthread_mutex_t Screen_draw_lock = PTHREAD_MUTEX_INITIALIZER;    // Mutex to protect frame buffer (dirtyPages in fact)
219 + #endif
220 +
221 + static int log_base_2(uint32 x)
222 + {
223 +        uint32 mask = 0x80000000;
224 +        int l = 31;
225 +        while (l >= 0 && (x & mask) == 0) {
226 +                mask >>= 1;
227 +                l--;
228 +        }
229 +        return l;
230 + }
231 +
232 + #endif
233 +
234 + // VideoRefresh function
235 + void VideoRefreshInit(void);
236 + static void (*video_refresh)(void);
237  
238   // Prototypes
239   static void *redraw_func(void *arg);
# Line 155 | Line 241 | static int event2keycode(XKeyEvent *ev);
241  
242  
243   // From main_unix.cpp
244 + extern char *x_display_name;
245   extern Display *x_display;
246  
247   // From sys_unix.cpp
248   extern void SysMountFirstFloppy(void);
249  
250 + #ifdef ENABLE_VOSF
251 + # include "video_vosf.h"
252 + #endif
253 +
254  
255   /*
256   *  Initialization
# Line 168 | Line 259 | extern void SysMountFirstFloppy(void);
259   // Set VideoMonitor according to video mode
260   void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order)
261   {
262 + #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
263          int layout = FLAYOUT_DIRECT;
264          switch (depth) {
265                  case 1:
266                          layout = FLAYOUT_DIRECT;
175                        VideoMonitor.mode = VMODE_1BIT;
267                          break;
268                  case 8:
269                          layout = FLAYOUT_DIRECT;
179                        VideoMonitor.mode = VMODE_8BIT;
270                          break;
271                  case 15:
272                          layout = FLAYOUT_HOST_555;
183                        VideoMonitor.mode = VMODE_16BIT;
273                          break;
274                  case 16:
275                          layout = FLAYOUT_HOST_565;
187                        VideoMonitor.mode = VMODE_16BIT;
276                          break;
277                  case 24:
278                  case 32:
279                          layout = FLAYOUT_HOST_888;
192                        VideoMonitor.mode = VMODE_32BIT;
280                          break;
281          }
195        VideoMonitor.x = width;
196        VideoMonitor.y = height;
197        VideoMonitor.bytes_per_row = bytes_per_row;
282          if (native_byte_order)
283                  MacFrameLayout = layout;
284          else
285                  MacFrameLayout = FLAYOUT_DIRECT;
286 + #endif
287 +        switch (depth) {
288 +                case 1:
289 +                        VideoMonitor.mode = VMODE_1BIT;
290 +                        break;
291 +                case 8:
292 +                        VideoMonitor.mode = VMODE_8BIT;
293 +                        break;
294 +                case 15:
295 +                        VideoMonitor.mode = VMODE_16BIT;
296 +                        break;
297 +                case 16:
298 +                        VideoMonitor.mode = VMODE_16BIT;
299 +                        break;
300 +                case 24:
301 +                case 32:
302 +                        VideoMonitor.mode = VMODE_32BIT;
303 +                        break;
304 +        }
305 +        VideoMonitor.x = width;
306 +        VideoMonitor.y = height;
307 +        VideoMonitor.bytes_per_row = bytes_per_row;
308   }
309  
310   // Trap SHM errors
# Line 225 | Line 331 | static bool init_window(int width, int h
331  
332          // Read frame skip prefs
333          frame_skip = PrefsFindInt32("frameskip");
228        if (frame_skip == 0)
229                frame_skip = 1;
334  
335          // Create window
336          XSetWindowAttributes wattr;
337          wattr.event_mask = eventmask = win_eventmask;
338          wattr.background_pixel = black_pixel;
235        wattr.border_pixel = black_pixel;
236        wattr.backing_store = NotUseful;
237        wattr.save_under = false;
238        wattr.backing_planes = xdepth;
339  
340          XSync(x_display, false);
341          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
342 <                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
243 <                CWBackingStore | CWBackingPlanes, &wattr);
244 <        XSync(x_display, false);
245 <        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
246 <        XMapRaised(x_display, the_win);
247 <        XSync(x_display, false);
342 >                InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
343  
344 <        // Set colormap
345 <        if (depth == 8) {
346 <                XSetWindowColormap(x_display, the_win, cmap[0]);
347 <                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
344 >        // Indicate that we want keyboard input
345 >        {
346 >                XWMHints *hints = XAllocWMHints();
347 >                if (hints) {
348 >                        hints->input = True;
349 >                        hints->flags = InputHint;
350 >                        XSetWMHints(x_display, the_win, hints);
351 >                        XFree((char *)hints);
352 >                }
353          }
354  
355          // Make window unresizable
356 <        XSizeHints *hints;
357 <        if ((hints = XAllocSizeHints()) != NULL) {
358 <                hints->min_width = width;
359 <                hints->max_width = width;
360 <                hints->min_height = height;
361 <                hints->max_height = height;
362 <                hints->flags = PMinSize | PMaxSize;
363 <                XSetWMNormalHints(x_display, the_win, hints);
364 <                XFree((char *)hints);
356 >        {
357 >                XSizeHints *hints = XAllocSizeHints();
358 >                if (hints) {
359 >                        hints->min_width = width;
360 >                        hints->max_width = width;
361 >                        hints->min_height = height;
362 >                        hints->max_height = height;
363 >                        hints->flags = PMinSize | PMaxSize;
364 >                        XSetWMNormalHints(x_display, the_win, hints);
365 >                        XFree((char *)hints);
366 >                }
367          }
368          
369 +        // Set window title
370 +        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
371 +
372 +        // Set window class
373 +        {
374 +                XClassHint *hints;
375 +                hints = XAllocClassHint();
376 +                if (hints) {
377 +                        hints->res_name = "BasiliskII";
378 +                        hints->res_class = "BasiliskII";
379 +                        XSetClassHint(x_display, the_win, hints);
380 +                        XFree((char *)hints);
381 +                }
382 +        }
383 +
384 +        // Show window
385 +        XSync(x_display, false);
386 +        XMapRaised(x_display, the_win);
387 +        XFlush(x_display);
388 +
389 +        // Set colormap
390 +        if (depth == 8)
391 +                XSetWindowColormap(x_display, the_win, cmap[0]);
392 +
393          // Try to create and attach SHM image
394          have_shm = false;
395 <        if (depth != 1 && XShmQueryExtension(x_display)) {
395 >        if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
396  
397                  // Create SHM image ("height + 2" for safety)
398                  img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
# Line 317 | Line 443 | static bool init_window(int width, int h
443                  img->bitmap_bit_order = MSBFirst;
444          }
445  
446 <        // Allocate memory for frame buffer copy
446 > #ifdef ENABLE_VOSF
447 >        // Allocate a page-aligned chunk of memory for frame buffer
448 >        the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line);
449 >        the_host_buffer = the_buffer_copy;
450 >        
451 >        the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size);
452 >        memset(the_buffer_copy, 0, the_buffer_size);
453 >        
454 >        the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
455 >        memset(the_buffer, 0, the_buffer_size);
456 > #else
457 >        // Allocate memory for frame buffer
458          the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
459 + #endif
460  
461          // Create GC
462          the_gc = XCreateGC(x_display, the_win, 0, 0);
463          XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
464  
465          // Create no_cursor
466 <        mac_cursor = XCreatePixmapCursor (x_display,
467 <                                           XCreatePixmap (x_display, the_win, 1, 1, 1),
468 <                                           XCreatePixmap (x_display, the_win, 1, 1, 1),
469 <                                           &black, &white, 0, 0);
470 <        XDefineCursor (x_display, the_win, mac_cursor);
466 >        mac_cursor = XCreatePixmapCursor(x_display,
467 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
468 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
469 >           &black, &white, 0, 0);
470 >        XDefineCursor(x_display, the_win, mac_cursor);
471  
472          // Set VideoMonitor
473 +        bool native_byte_order;
474   #ifdef WORDS_BIGENDIAN
475 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst);
475 >        native_byte_order = (img->bitmap_bit_order == MSBFirst);
476   #else
477 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst);
477 >        native_byte_order = (img->bitmap_bit_order == LSBFirst);
478 > #endif
479 > #ifdef ENABLE_VOSF
480 >        do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
481   #endif
482 +        set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
483          
484 < #if REAL_ADDRESSING
485 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
343 <        MacFrameLayout = FLAYOUT_DIRECT;
484 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
485 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
486   #else
487          VideoMonitor.mac_frame_base = MacFrameBaseMac;
488   #endif
# Line 350 | Line 492 | static bool init_window(int width, int h
492   // Init fbdev DGA display
493   static bool init_fbdev_dga(char *in_fb_name)
494   {
495 < #if ENABLE_FBDEV_DGA
495 > #ifdef ENABLE_FBDEV_DGA
496          // Find the maximum depth available
497          int ndepths, max_depth(0);
498          int *depths = XListDepths(x_display, screen, &ndepths);
# Line 419 | Line 561 | static bool init_fbdev_dga(char *in_fb_n
561          
562          // Create window
563          XSetWindowAttributes wattr;
422        wattr.override_redirect = True;
423        wattr.backing_store             = NotUseful;
424        wattr.background_pixel  = white_pixel;
425        wattr.border_pixel              = black_pixel;
564          wattr.event_mask                = eventmask = dga_eventmask;
565 +        wattr.background_pixel  = white_pixel;
566 +        wattr.override_redirect = True;
567          
568          XSync(x_display, false);
569          the_win = XCreateWindow(x_display, rootwin,
570                  0, 0, width, height,
571                  0, xdepth, InputOutput, vis,
572 <                CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore,
572 >                CWEventMask | CWBackPixel | CWOverrideRedirect,
573                  &wattr);
574          XSync(x_display, false);
575          XMapRaised(x_display, the_win);
# Line 443 | Line 583 | static bool init_fbdev_dga(char *in_fb_n
583                  GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
584          
585          // Set colormap
586 <        if (depth == 8) {
586 >        if (depth == 8)
587                  XSetWindowColormap(x_display, the_win, cmap[0]);
448                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
449        }
588          
589          // Set VideoMonitor
590          int bytes_per_row = width;
# Line 473 | Line 611 | static bool init_fbdev_dga(char *in_fb_n
611                  }
612          }
613          
614 + #if ENABLE_VOSF
615 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
616 +        // If the blit function is null, i.e. just a copy of the buffer,
617 +        // we first try to avoid the allocation of a temporary frame buffer
618 +        use_vosf = true;
619 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
620 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
621 +                use_vosf = false;
622 +        
623 +        if (use_vosf) {
624 +                the_host_buffer = the_buffer;
625 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
626 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
627 +                memset(the_buffer_copy, 0, the_buffer_size);
628 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
629 +                memset(the_buffer, 0, the_buffer_size);
630 +        }
631 + #else
632 +        use_vosf = false;
633 + #endif
634 + #endif
635 +        
636          set_video_monitor(width, height, bytes_per_row, true);
637 < #if REAL_ADDRESSING
638 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
479 <        MacFrameLayout = FLAYOUT_DIRECT;
637 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
638 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
639   #else
640          VideoMonitor.mac_frame_base = MacFrameBaseMac;
641   #endif
# Line 490 | Line 649 | static bool init_fbdev_dga(char *in_fb_n
649   // Init XF86 DGA display
650   static bool init_xf86_dga(int width, int height)
651   {
652 < #if ENABLE_XF86_DGA
652 > #ifdef ENABLE_XF86_DGA
653          // Set relative mouse mode
654          ADBSetRelMouseMode(true);
655  
656 < #if ENABLE_XF86_VIDMODE
656 > #ifdef ENABLE_XF86_VIDMODE
657          // Switch to best mode
658          if (has_vidmode) {
659                  int best = 0;
# Line 512 | Line 671 | static bool init_xf86_dga(int width, int
671          // Create window
672          XSetWindowAttributes wattr;
673          wattr.event_mask = eventmask = dga_eventmask;
515        wattr.border_pixel = black_pixel;
674          wattr.override_redirect = True;
675  
676          XSync(x_display, false);
677          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
678 <                InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr);
678 >                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
679          XSync(x_display, false);
680          XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
681          XMapRaised(x_display, the_win);
# Line 538 | Line 696 | static bool init_xf86_dga(int width, int
696          // Set colormap
697          if (depth == 8) {
698                  XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
541                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
699                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
700          }
701  
# Line 557 | Line 714 | static bool init_xf86_dga(int width, int
714                          bytes_per_row *= 4;
715                          break;
716          }
717 +        
718 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
719 +        // If the blit function is null, i.e. just a copy of the buffer,
720 +        // we first try to avoid the allocation of a temporary frame buffer
721 +        use_vosf = true;
722 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
723 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
724 +                use_vosf = false;
725 +        
726 +        if (use_vosf) {
727 +                the_host_buffer = the_buffer;
728 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
729 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
730 +                memset(the_buffer_copy, 0, the_buffer_size);
731 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
732 +                memset(the_buffer, 0, the_buffer_size);
733 +        }
734 + #elif defined(ENABLE_VOSF)
735 +        // The UAE memory handlers will already handle color conversion, if needed.
736 +        use_vosf = false;
737 + #endif
738 +        
739          set_video_monitor(width, height, bytes_per_row, true);
740 < #if REAL_ADDRESSING
741 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
742 <        MacFrameLayout = FLAYOUT_DIRECT;
740 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
741 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
742 > //      MacFrameLayout = FLAYOUT_DIRECT;
743   #else
744          VideoMonitor.mac_frame_base = MacFrameBaseMac;
745   #endif
# Line 636 | Line 815 | static void keycode_init(void)
815          }
816   }
817  
818 + bool VideoInitBuffer()
819 + {
820 + #ifdef ENABLE_VOSF
821 +        if (use_vosf) {
822 +                const uint32 page_size  = getpagesize();
823 +                const uint32 page_mask  = page_size - 1;
824 +                
825 +                mainBuffer.memBase      = (uint32) the_buffer;
826 +                // Align the frame buffer on page boundary
827 +                mainBuffer.memStart             = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
828 +                mainBuffer.memLength    = the_buffer_size;
829 +                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
830 +
831 +                mainBuffer.pageSize     = page_size;
832 +                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
833 +                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
834 +
835 +                if (mainBuffer.dirtyPages != 0)
836 +                        free(mainBuffer.dirtyPages);
837 +
838 +                mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
839 +
840 +                if (mainBuffer.pageInfo != 0)
841 +                        free(mainBuffer.pageInfo);
842 +
843 +                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
844 +
845 +                if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
846 +                        return false;
847 +
848 +                PFLAG_CLEAR_ALL;
849 +
850 +                uint32 a = 0;
851 +                for (int i = 0; i < mainBuffer.pageCount; i++) {
852 +                        int y1 = a / VideoMonitor.bytes_per_row;
853 +                        if (y1 >= VideoMonitor.y)
854 +                                y1 = VideoMonitor.y - 1;
855 +
856 +                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
857 +                        if (y2 >= VideoMonitor.y)
858 +                                y2 = VideoMonitor.y - 1;
859 +
860 +                        mainBuffer.pageInfo[i].top = y1;
861 +                        mainBuffer.pageInfo[i].bottom = y2;
862 +
863 +                        a += mainBuffer.pageSize;
864 +                        if (a > mainBuffer.memLength)
865 +                                a = mainBuffer.memLength;
866 +                }
867 +                
868 +                // We can now write-protect the frame buffer
869 +                if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0)
870 +                        return false;
871 +        }
872 + #endif
873 +        return true;
874 + }
875 +
876   bool VideoInit(bool classic)
877   {
878 + #ifdef ENABLE_VOSF
879 +        // Open /dev/zero
880 +        zero_fd = open("/dev/zero", O_RDWR);
881 +        if (zero_fd < 0) {
882 +        char str[256];
883 +                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
884 +                ErrorAlert(str);
885 +        return false;
886 +        }
887 +        
888 +        // Zero the mainBuffer structure
889 +        mainBuffer.dirtyPages = 0;
890 +        mainBuffer.pageInfo = 0;
891 + #endif
892 +        
893 +        // Check if X server runs on local machine
894 +        local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
895 +                 || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
896 +    
897          // Init keycode translation
898          keycode_init();
899  
# Line 652 | Line 908 | bool VideoInit(bool classic)
908          // Get screen depth
909          xdepth = DefaultDepth(x_display, screen);
910          
911 < #if ENABLE_FBDEV_DGA
911 > #ifdef ENABLE_FBDEV_DGA
912          // Frame buffer name
913          char fb_name[20];
914          
# Line 663 | Line 919 | bool VideoInit(bool classic)
919                  has_dga = false;
920   #endif
921  
922 < #if ENABLE_XF86_DGA
922 > #ifdef ENABLE_XF86_DGA
923          // DGA available?
924          int dga_event_base, dga_error_base;
925 <        if (XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
925 >        if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
926                  int dga_flags = 0;
927                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
928                  has_dga = dga_flags & XF86DGADirectPresent;
# Line 674 | Line 930 | bool VideoInit(bool classic)
930                  has_dga = false;
931   #endif
932  
933 < #if ENABLE_XF86_VIDMODE
933 > #ifdef ENABLE_XF86_VIDMODE
934          // VidMode available?
935          int vm_event_base, vm_error_base;
936          has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
# Line 747 | Line 1003 | bool VideoInit(bool classic)
1003          if (mode_str) {
1004                  if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
1005                          display_type = DISPLAY_WINDOW;
1006 < #if ENABLE_FBDEV_DGA
1006 > #ifdef ENABLE_FBDEV_DGA
1007                  else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
1008   #else
1009                  else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
# Line 771 | Line 1027 | bool VideoInit(bool classic)
1027                                  return false;
1028                          break;
1029                  case DISPLAY_DGA:
1030 < #if ENABLE_FBDEV_DGA
1030 > #ifdef ENABLE_FBDEV_DGA
1031                          if (!init_fbdev_dga(fb_name))
1032   #else
1033                          if (!init_xf86_dga(width, height))
# Line 780 | Line 1036 | bool VideoInit(bool classic)
1036                          break;
1037          }
1038  
1039 + #ifdef HAVE_PTHREADS
1040          // Lock down frame buffer
1041          pthread_mutex_lock(&frame_buffer_lock);
1042 + #endif
1043  
1044 < #if !REAL_ADDRESSING
1044 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1045          // Set variables for UAE memory mapping
1046          MacFrameBaseHost = the_buffer;
1047          MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
# Line 793 | Line 1051 | bool VideoInit(bool classic)
1051                  MacFrameLayout = FLAYOUT_NONE;
1052   #endif
1053  
1054 <        // Start redraw/input thread
1054 > #ifdef ENABLE_VOSF
1055 >        if (use_vosf) {
1056 >                // Initialize the mainBuffer structure
1057 >                if (!VideoInitBuffer()) {
1058 >                        // TODO: STR_VOSF_INIT_ERR ?
1059 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1060 >                return false;
1061 >                }
1062 >
1063 >                // Initialize the handler for SIGSEGV
1064 >                if (!Screen_fault_handler_init()) {
1065 >                        // TODO: STR_VOSF_INIT_ERR ?
1066 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1067 >                        return false;
1068 >                }
1069 >        }
1070 > #endif
1071 >        
1072 >        // Initialize VideoRefresh function
1073 >        VideoRefreshInit();
1074 >        
1075          XSync(x_display, false);
1076 +
1077 + #ifdef HAVE_PTHREADS
1078 +        // Start redraw/input thread
1079          redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1080 <        if (!redraw_thread_active)
1080 >        if (!redraw_thread_active) {
1081                  printf("FATAL: cannot create redraw thread\n");
1082 <        return redraw_thread_active;
1082 >                return false;
1083 >        }
1084 > #endif
1085 >        return true;
1086   }
1087  
1088  
# Line 808 | Line 1092 | bool VideoInit(bool classic)
1092  
1093   void VideoExit(void)
1094   {
1095 + #ifdef HAVE_PTHREADS
1096          // Stop redraw thread
1097          if (redraw_thread_active) {
1098                  redraw_thread_cancel = true;
# Line 817 | Line 1102 | void VideoExit(void)
1102                  pthread_join(redraw_thread, NULL);
1103                  redraw_thread_active = false;
1104          }
1105 + #endif
1106  
1107 + #ifdef HAVE_PTHREADS
1108          // Unlock frame buffer
1109          pthread_mutex_unlock(&frame_buffer_lock);
1110 + #endif
1111  
1112          // Close window and server connection
1113          if (x_display != NULL) {
1114                  XSync(x_display, false);
1115  
1116 < #if ENABLE_XF86_DGA
1116 > #ifdef ENABLE_XF86_DGA
1117                  if (display_type == DISPLAY_DGA) {
1118                          XF86DGADirectVideo(x_display, screen, 0);
1119                          XUngrabPointer(x_display, CurrentTime);
# Line 833 | Line 1121 | void VideoExit(void)
1121                  }
1122   #endif
1123  
1124 < #if ENABLE_XF86_VIDMODE
1124 > #ifdef ENABLE_XF86_VIDMODE
1125                  if (has_vidmode && display_type == DISPLAY_DGA)
1126                          XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1127   #endif
1128  
1129 < #if ENABLE_FBDEV_DGA
1129 > #ifdef ENABLE_FBDEV_DGA
1130                  if (display_type == DISPLAY_DGA) {
1131                          XUngrabPointer(x_display, CurrentTime);
1132                          XUngrabKeyboard(x_display, CurrentTime);
# Line 852 | Line 1140 | void VideoExit(void)
1140                          XFreeColormap(x_display, cmap[0]);
1141                          XFreeColormap(x_display, cmap[1]);
1142                  }
1143 +                
1144 +                if (!use_vosf) {
1145 +                        if (the_buffer) {
1146 +                                free(the_buffer);
1147 +                                the_buffer = NULL;
1148 +                        }
1149  
1150 <                if (the_buffer) {
1151 <                        free(the_buffer);
1152 <                        the_buffer = NULL;
1150 >                        if (!have_shm && the_buffer_copy) {
1151 >                                free(the_buffer_copy);
1152 >                                the_buffer_copy = NULL;
1153 >                        }
1154 >                }
1155 > #ifdef ENABLE_VOSF
1156 >                else {
1157 >                        if (the_buffer != (uint8 *)MAP_FAILED) {
1158 >                                munmap((caddr_t)the_buffer, the_buffer_size);
1159 >                                the_buffer = 0;
1160 >                        }
1161 >                        
1162 >                        if (the_buffer_copy != (uint8 *)MAP_FAILED) {
1163 >                                munmap((caddr_t)the_buffer_copy, the_buffer_size);
1164 >                                the_buffer_copy = 0;
1165 >                        }
1166 >                }
1167 > #endif
1168 >        }
1169 >        
1170 > #ifdef ENABLE_VOSF
1171 >        if (use_vosf) {
1172 >                // Clear mainBuffer data
1173 >                if (mainBuffer.pageInfo) {
1174 >                        free(mainBuffer.pageInfo);
1175 >                        mainBuffer.pageInfo = 0;
1176                  }
1177  
1178 <                if (!have_shm && the_buffer_copy) {
1179 <                        free(the_buffer_copy);
1180 <                        the_buffer_copy = NULL;
1178 >                if (mainBuffer.dirtyPages) {
1179 >                        free(mainBuffer.dirtyPages);
1180 >                        mainBuffer.dirtyPages = 0;
1181                  }
1182          }
1183 +
1184 +        // Close /dev/zero
1185 +        if (zero_fd > 0)
1186 +                close(zero_fd);
1187 + #endif
1188   }
1189  
1190  
# Line 888 | Line 1210 | void VideoInterrupt(void)
1210          if (emerg_quit)
1211                  QuitEmulator();
1212  
1213 + #ifdef HAVE_PTHREADS
1214          // Temporarily give up frame buffer lock (this is the point where
1215          // we are suspended when the user presses Ctrl-Tab)
1216          pthread_mutex_unlock(&frame_buffer_lock);
1217          pthread_mutex_lock(&frame_buffer_lock);
1218 + #endif
1219   }
1220  
1221  
# Line 901 | Line 1225 | void VideoInterrupt(void)
1225  
1226   void video_set_palette(uint8 *pal)
1227   {
1228 + #ifdef HAVE_PTHREADS
1229          pthread_mutex_lock(&palette_lock);
1230 + #endif
1231  
1232          // Convert colors to XColor array
1233          for (int i=0; i<256; i++) {
# Line 915 | Line 1241 | void video_set_palette(uint8 *pal)
1241          // Tell redraw thread to change palette
1242          palette_changed = true;
1243  
1244 + #ifdef HAVE_PTHREADS
1245          pthread_mutex_unlock(&palette_lock);
1246 + #endif
1247   }
1248  
1249  
# Line 923 | Line 1251 | void video_set_palette(uint8 *pal)
1251   *  Suspend/resume emulator
1252   */
1253  
1254 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1254 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1255   static void suspend_emul(void)
1256   {
1257          if (display_type == DISPLAY_DGA) {
# Line 931 | Line 1259 | static void suspend_emul(void)
1259                  ADBKeyUp(0x36);
1260                  ctrl_down = false;
1261  
1262 + #ifdef HAVE_PTHREADS
1263                  // Lock frame buffer (this will stop the MacOS thread)
1264                  pthread_mutex_lock(&frame_buffer_lock);
1265 + #endif
1266  
1267                  // Save frame buffer
1268                  fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
# Line 940 | Line 1270 | static void suspend_emul(void)
1270                          memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
1271  
1272                  // Close full screen display
1273 < #if ENABLE_XF86_DGA
1273 > #ifdef ENABLE_XF86_DGA
1274                  XF86DGADirectVideo(x_display, screen, 0);
1275   #endif
1276                  XUngrabPointer(x_display, CurrentTime);
# Line 952 | Line 1282 | static void suspend_emul(void)
1282                  XSetWindowAttributes wattr;
1283                  wattr.event_mask = KeyPressMask;
1284                  wattr.background_pixel = black_pixel;
1285 <                wattr.border_pixel = black_pixel;
956 <                wattr.backing_store = Always;
1285 >                wattr.backing_store = WhenMapped;
1286                  wattr.backing_planes = xdepth;
1287                  wattr.colormap = DefaultColormap(x_display, screen);
1288                  
1289                  XSync(x_display, false);
1290                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1291 <                        InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1292 <                        CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1291 >                        InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore |
1292 >                        CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1293                  XSync(x_display, false);
1294                  XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1295                  XMapRaised(x_display, suspend_win);
# Line 981 | Line 1310 | static void resume_emul(void)
1310          XSync(x_display, false);
1311          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1312          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1313 < #if ENABLE_XF86_DGA
1313 > #ifdef ENABLE_XF86_DGA
1314          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1315          XF86DGASetViewPort(x_display, screen, 0, 0);
1316   #endif
1317          XSync(x_display, false);
1318 <
1318 >        
1319 >        // the_buffer already contains the data to restore. i.e. since a temporary
1320 >        // frame buffer is used when VOSF is actually used, fb_save is therefore
1321 >        // not necessary.
1322 > #ifdef ENABLE_VOSF
1323 >        if (use_vosf) {
1324 > #ifdef HAVE_PTHREADS
1325 >                pthread_mutex_lock(&Screen_draw_lock);
1326 > #endif
1327 >                PFLAG_SET_ALL;
1328 > #ifdef HAVE_PTHREADS
1329 >                pthread_mutex_unlock(&Screen_draw_lock);
1330 > #endif
1331 >                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1332 >        }
1333 > #endif
1334 >        
1335          // Restore frame buffer
1336          if (fb_save) {
1337 + #ifdef ENABLE_VOSF
1338 +                // Don't copy fb_save to the temporary frame buffer in VOSF mode
1339 +                if (!use_vosf)
1340 + #endif
1341                  memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1342                  free(fb_save);
1343                  fb_save = NULL;
1344          }
1345          
1346 + #ifdef ENABLE_XF86_DGA
1347          if (depth == 8)
998 #if ENABLE_XF86_DGA
1348                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1349   #endif
1350  
1351 + #ifdef HAVE_PTHREADS
1352          // Unlock frame buffer (and continue MacOS thread)
1353          pthread_mutex_unlock(&frame_buffer_lock);
1354          emul_suspended = false;
1355 + #endif
1356   }
1357   #endif
1358  
# Line 1063 | Line 1414 | static int kc_decode(KeySym ks)
1414                  case XK_period: case XK_greater: return 0x2f;
1415                  case XK_slash: case XK_question: return 0x2c;
1416  
1417 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1417 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1418                  case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1419   #else
1420                  case XK_Tab: return 0x30;
# Line 1242 | Line 1593 | static void handle_events(void)
1593                                                  if (code == 0x36)
1594                                                          ctrl_down = true;
1595                                          } else {
1596 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1596 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1597                                                  if (code == 0x31)
1598                                                          resume_emul();  // Space wakes us up
1599   #endif
# Line 1268 | Line 1619 | static void handle_events(void)
1619                          // Hidden parts exposed, force complete refresh of window
1620                          case Expose:
1621                                  if (display_type == DISPLAY_WINDOW) {
1622 <                                        int x1, y1;
1623 <                                        for (y1=0; y1<16; y1++)
1624 <                                        for (x1=0; x1<16; x1++)
1625 <                                                updt_box[x1][y1] = true;
1626 <                                        nr_boxes = 16 * 16;
1622 > #ifdef ENABLE_VOSF
1623 >                                        if (use_vosf) {                 // VOSF refresh
1624 > #ifdef HAVE_PTHREADS
1625 >                                                pthread_mutex_lock(&Screen_draw_lock);
1626 > #endif
1627 >                                                PFLAG_SET_ALL;
1628 > #ifdef HAVE_PTHREADS
1629 >                                                pthread_mutex_unlock(&Screen_draw_lock);
1630 > #endif
1631 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1632 >                                        }
1633 >                                        else
1634 > #endif
1635 >                                        if (frame_skip == 0) {  // Dynamic refresh
1636 >                                                int x1, y1;
1637 >                                                for (y1=0; y1<16; y1++)
1638 >                                                for (x1=0; x1<16; x1++)
1639 >                                                        updt_box[x1][y1] = true;
1640 >                                                nr_boxes = 16 * 16;
1641 >                                        } else                                  // Static refresh
1642 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1643                                  }
1644                                  break;
1645 +
1646 +                        case FocusIn:
1647 +                        case FocusOut:
1648 +                                break;
1649                  }
1650          }
1651   }
# Line 1284 | Line 1655 | static void handle_events(void)
1655   *  Window display update
1656   */
1657  
1658 < static void update_display(int ticker)
1658 > // Dynamic display update (variable frame rate for each box)
1659 > static void update_display_dynamic(int ticker)
1660   {
1661 <        int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xic, xicl, xi;
1661 >        int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1662          int xil = 0;
1663          int rxm = 0, rxmo = 0;
1664          int bytes_per_row = VideoMonitor.bytes_per_row;
# Line 1373 | Line 1745 | static void update_display(int ticker)
1745          }
1746   }
1747  
1748 + // Static display update (fixed frame rate, but incremental)
1749 + static void update_display_static(void)
1750 + {
1751 +        // Incremental update code
1752 +        int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1753 +        int bytes_per_row = VideoMonitor.bytes_per_row;
1754 +        int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1755 +        uint8 *p, *p2;
1756 +
1757 +        // Check for first line from top and first line from bottom that have changed
1758 +        y1 = 0;
1759 +        for (j=0; j<VideoMonitor.y; j++) {
1760 +                if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1761 +                        y1 = j;
1762 +                        break;
1763 +                }
1764 +        }
1765 +        y2 = y1 - 1;
1766 +        for (j=VideoMonitor.y-1; j>=y1; j--) {
1767 +                if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1768 +                        y2 = j;
1769 +                        break;
1770 +                }
1771 +        }
1772 +        high = y2 - y1 + 1;
1773 +
1774 +        // Check for first column from left and first column from right that have changed
1775 +        if (high) {
1776 +                if (depth == 1) {
1777 +                        x1 = VideoMonitor.x - 1;
1778 +                        for (j=y1; j<=y2; j++) {
1779 +                                p = &the_buffer[j * bytes_per_row];
1780 +                                p2 = &the_buffer_copy[j * bytes_per_row];
1781 +                                for (i=0; i<(x1>>3); i++) {
1782 +                                        if (*p != *p2) {
1783 +                                                x1 = i << 3;
1784 +                                                break;
1785 +                                        }
1786 +                                        p++; p2++;
1787 +                                }
1788 +                        }
1789 +                        x2 = x1;
1790 +                        for (j=y1; j<=y2; j++) {
1791 +                                p = &the_buffer[j * bytes_per_row];
1792 +                                p2 = &the_buffer_copy[j * bytes_per_row];
1793 +                                p += bytes_per_row;
1794 +                                p2 += bytes_per_row;
1795 +                                for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1796 +                                        p--; p2--;
1797 +                                        if (*p != *p2) {
1798 +                                                x2 = (i << 3) + 7;
1799 +                                                break;
1800 +                                        }
1801 +                                }
1802 +                        }
1803 +                        wide = x2 - x1 + 1;
1804 +
1805 +                        // Update copy of the_buffer
1806 +                        if (high && wide) {
1807 +                                for (j=y1; j<=y2; j++) {
1808 +                                        i = j * bytes_per_row + (x1 >> 3);
1809 +                                        memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3);
1810 +                                }
1811 +                        }
1812 +
1813 +                } else {
1814 +                        x1 = VideoMonitor.x;
1815 +                        for (j=y1; j<=y2; j++) {
1816 +                                p = &the_buffer[j * bytes_per_row];
1817 +                                p2 = &the_buffer_copy[j * bytes_per_row];
1818 +                                for (i=0; i<x1*bytes_per_pixel; i++) {
1819 +                                        if (*p != *p2) {
1820 +                                                x1 = i / bytes_per_pixel;
1821 +                                                break;
1822 +                                        }
1823 +                                        p++; p2++;
1824 +                                }
1825 +                        }
1826 +                        x2 = x1;
1827 +                        for (j=y1; j<=y2; j++) {
1828 +                                p = &the_buffer[j * bytes_per_row];
1829 +                                p2 = &the_buffer_copy[j * bytes_per_row];
1830 +                                p += bytes_per_row;
1831 +                                p2 += bytes_per_row;
1832 +                                for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1833 +                                        p--;
1834 +                                        p2--;
1835 +                                        if (*p != *p2) {
1836 +                                                x2 = i / bytes_per_pixel;
1837 +                                                break;
1838 +                                        }
1839 +                                }
1840 +                        }
1841 +                        wide = x2 - x1;
1842 +
1843 +                        // Update copy of the_buffer
1844 +                        if (high && wide) {
1845 +                                for (j=y1; j<=y2; j++) {
1846 +                                        i = j * bytes_per_row + x1 * bytes_per_pixel;
1847 +                                        memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1848 +                                }
1849 +                        }
1850 +                }
1851 +        }
1852 +
1853 +        // Refresh display
1854 +        if (high && wide) {
1855 +                if (have_shm)
1856 +                        XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1857 +                else
1858 +                        XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1859 +        }
1860 + }
1861 +
1862  
1863   /*
1864 < *  Thread for screen refresh, input handling etc.
1864 > *      Screen refresh functions
1865   */
1866  
1867 < static void *redraw_func(void *arg)
1868 < {
1869 <        int tick_counter = 0;
1870 <
1871 <        while (!redraw_thread_cancel) {
1867 > // The specialisations hereunder are meant to enable VOSF with DGA in direct
1868 > // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1869 > // not get mapped correctly with respect to the predetermined host frame
1870 > // buffer base address.
1871 > //
1872 > // Hmm, in other words, when in direct addressing mode and DGA is requested,
1873 > // we first try to "triple allocate" the address spaces according to the real
1874 > // host frame buffer address. Then, if it fails, we will use a temporary
1875 > // frame buffer thus making the real host frame buffer updated when pages
1876 > // of the temp frame buffer are altered.
1877 > //
1878 > // As a side effect, a little speed gain in screen updates could be noticed
1879 > // for other modes than DGA.
1880 > //
1881 > // The following two functions below are inline so that a clever compiler
1882 > // could specialise the code according to the current screen depth and
1883 > // display type. A more clever compiler would the job by itself though...
1884 > // (update_display_vosf is inlined as well)
1885  
1886 <                // Wait
1887 < #ifdef HAVE_NANOSLEEP
1888 <                struct timespec req = {0, 16666667};
1889 <                nanosleep(&req, NULL);
1890 < #else
1891 <                usleep(16667);
1886 > static inline void possibly_quit_dga_mode()
1887 > {
1888 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1889 >        // Quit DGA mode if requested
1890 >        if (quit_full_screen) {
1891 >                quit_full_screen = false;
1892 > #ifdef ENABLE_XF86_DGA
1893 >                XF86DGADirectVideo(x_display, screen, 0);
1894 > #endif
1895 >                XUngrabPointer(x_display, CurrentTime);
1896 >                XUngrabKeyboard(x_display, CurrentTime);
1897 >                XUnmapWindow(x_display, the_win);
1898 >                XSync(x_display, false);
1899 >        }
1900   #endif
1901 + }
1902  
1903 < #if ENABLE_XF86_DGA
1904 <                // Quit DGA mode if requested
1905 <                if (quit_full_screen) {
1906 <                        quit_full_screen = false;
1903 > static inline void handle_palette_changes(int depth, int display_type)
1904 > {
1905 > #ifdef HAVE_PTHREADS
1906 >        pthread_mutex_lock(&palette_lock);
1907 > #endif
1908 >        if (palette_changed) {
1909 >                palette_changed = false;
1910 >                if (depth == 8) {
1911 >                        XStoreColors(x_display, cmap[0], palette, 256);
1912 >                        XStoreColors(x_display, cmap[1], palette, 256);
1913 >                                
1914 > #ifdef ENABLE_XF86_DGA
1915                          if (display_type == DISPLAY_DGA) {
1916 <                                XF86DGADirectVideo(x_display, screen, 0);
1917 <                                XUngrabPointer(x_display, CurrentTime);
1402 <                                XUngrabKeyboard(x_display, CurrentTime);
1403 <                                XUnmapWindow(x_display, the_win);
1404 <                                XSync(x_display, false);
1916 >                                current_dga_cmap ^= 1;
1917 >                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1918                          }
1919 + #endif
1920                  }
1921 +        }
1922 + #ifdef HAVE_PTHREADS
1923 +        pthread_mutex_unlock(&palette_lock);
1924   #endif
1925 + }
1926  
1927 < #if ENABLE_FBDEV_DGA
1928 <                // Quit DGA mode if requested
1929 <                if (quit_full_screen) {
1930 <                        quit_full_screen = false;
1931 <                        if (display_type == DISPLAY_DGA) {
1932 <                                XUngrabPointer(x_display, CurrentTime);
1933 <                                XUngrabKeyboard(x_display, CurrentTime);
1934 <                                XUnmapWindow(x_display, the_win);
1935 <                                XSync(x_display, false);
1936 <                        }
1927 > static void video_refresh_dga(void)
1928 > {
1929 >        // Quit DGA mode if requested
1930 >        possibly_quit_dga_mode();
1931 >        
1932 >        // Handle X events
1933 >        handle_events();
1934 >        
1935 >        // Handle palette changes
1936 >        handle_palette_changes(depth, DISPLAY_DGA);
1937 > }
1938 >
1939 > #ifdef ENABLE_VOSF
1940 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
1941 > static void video_refresh_dga_vosf(void)
1942 > {
1943 >        // Quit DGA mode if requested
1944 >        possibly_quit_dga_mode();
1945 >        
1946 >        // Handle X events
1947 >        handle_events();
1948 >        
1949 >        // Handle palette changes
1950 >        handle_palette_changes(depth, DISPLAY_DGA);
1951 >        
1952 >        // Update display (VOSF variant)
1953 >        static int tick_counter = 0;
1954 >        if (++tick_counter >= frame_skip) {
1955 >                tick_counter = 0;
1956 > #ifdef HAVE_PTHREADS
1957 >                pthread_mutex_lock(&Screen_draw_lock);
1958 > #endif
1959 >                update_display_dga_vosf();
1960 > #ifdef HAVE_PTHREADS
1961 >                pthread_mutex_unlock(&Screen_draw_lock);
1962 > #endif
1963 >        }
1964 > }
1965 > #endif
1966 >
1967 > static void video_refresh_window_vosf(void)
1968 > {
1969 >        // Quit DGA mode if requested
1970 >        possibly_quit_dga_mode();
1971 >        
1972 >        // Handle X events
1973 >        handle_events();
1974 >        
1975 >        // Handle palette changes
1976 >        handle_palette_changes(depth, DISPLAY_WINDOW);
1977 >        
1978 >        // Update display (VOSF variant)
1979 >        static int tick_counter = 0;
1980 >        if (++tick_counter >= frame_skip) {
1981 >                tick_counter = 0;
1982 > #ifdef HAVE_PTHREADS
1983 >                pthread_mutex_lock(&Screen_draw_lock);
1984 > #endif
1985 >                update_display_window_vosf();
1986 > #ifdef HAVE_PTHREADS
1987 >                pthread_mutex_unlock(&Screen_draw_lock);
1988 > #endif
1989 >        }
1990 > }
1991 > #endif // def ENABLE_VOSF
1992 >
1993 > static void video_refresh_window_static(void)
1994 > {
1995 >        // Handle X events
1996 >        handle_events();
1997 >        
1998 >        // Handle_palette changes
1999 >        handle_palette_changes(depth, DISPLAY_WINDOW);
2000 >        
2001 >        // Update display (static variant)
2002 >        static int tick_counter = 0;
2003 >        if (++tick_counter >= frame_skip) {
2004 >                tick_counter = 0;
2005 >                update_display_static();
2006 >        }
2007 > }
2008 >
2009 > static void video_refresh_window_dynamic(void)
2010 > {
2011 >        // Handle X events
2012 >        handle_events();
2013 >        
2014 >        // Handle_palette changes
2015 >        handle_palette_changes(depth, DISPLAY_WINDOW);
2016 >        
2017 >        // Update display (dynamic variant)
2018 >        static int tick_counter = 0;
2019 >        tick_counter++;
2020 >        update_display_dynamic(tick_counter);
2021 > }
2022 >
2023 >
2024 > /*
2025 > *  Thread for screen refresh, input handling etc.
2026 > */
2027 >
2028 > void VideoRefreshInit(void)
2029 > {
2030 >        // TODO: set up specialised 8bpp VideoRefresh handlers ?
2031 >        if (display_type == DISPLAY_DGA) {
2032 > #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2033 >                if (use_vosf)
2034 >                        video_refresh = video_refresh_dga_vosf;
2035 >                else
2036 > #endif
2037 >                        video_refresh = video_refresh_dga;
2038 >        }
2039 >        else {
2040 > #ifdef ENABLE_VOSF
2041 >                if (use_vosf)
2042 >                        video_refresh = video_refresh_window_vosf;
2043 >                else
2044 > #endif
2045 >                if (frame_skip == 0)
2046 >                        video_refresh = video_refresh_window_dynamic;
2047 >                else
2048 >                        video_refresh = video_refresh_window_static;
2049 >        }
2050 > }
2051 >
2052 > void VideoRefresh(void)
2053 > {
2054 >        // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2055 >        video_refresh();
2056 > }
2057 >
2058 > #if 0
2059 > void VideoRefresh(void)
2060 > {
2061 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2062 >        // Quit DGA mode if requested
2063 >        if (quit_full_screen) {
2064 >                quit_full_screen = false;
2065 >                if (display_type == DISPLAY_DGA) {
2066 > #ifdef ENABLE_XF86_DGA
2067 >                        XF86DGADirectVideo(x_display, screen, 0);
2068 > #endif
2069 >                        XUngrabPointer(x_display, CurrentTime);
2070 >                        XUngrabKeyboard(x_display, CurrentTime);
2071 >                        XUnmapWindow(x_display, the_win);
2072 >                        XSync(x_display, false);
2073                  }
2074 +        }
2075   #endif
1421                // Handle X events
1422                handle_events();
2076  
2077 <                // Handle palette changes
2078 <                pthread_mutex_lock(&palette_lock);
2079 <                if (palette_changed) {
2080 <                        palette_changed = false;
2081 <                        if (depth == 8) {
2082 <                                XStoreColors(x_display, cmap[0], palette, 256);
1430 <                                XStoreColors(x_display, cmap[1], palette, 256);
1431 <                                
1432 < #if ENABLE_XF86_DGA
1433 <                                if (display_type == DISPLAY_DGA) {
1434 <                                        current_dga_cmap ^= 1;
1435 <                                        XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1436 <                                }
2077 >        // Handle X events
2078 >        handle_events();
2079 >
2080 >        // Handle palette changes
2081 > #ifdef HAVE_PTHREADS
2082 >        pthread_mutex_lock(&palette_lock);
2083   #endif
2084 +        if (palette_changed) {
2085 +                palette_changed = false;
2086 +                if (depth == 8) {
2087 +                        XStoreColors(x_display, cmap[0], palette, 256);
2088 +                        XStoreColors(x_display, cmap[1], palette, 256);
2089 +                                
2090 + #ifdef ENABLE_XF86_DGA
2091 +                        if (display_type == DISPLAY_DGA) {
2092 +                                current_dga_cmap ^= 1;
2093 +                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2094                          }
2095 + #endif
2096                  }
2097 <                pthread_mutex_unlock(&palette_lock);
2097 >        }
2098 > #ifdef HAVE_PTHREADS
2099 >        pthread_mutex_unlock(&palette_lock);
2100 > #endif
2101  
2102 <                // In window mode, update display
2103 <                if (display_type == DISPLAY_WINDOW) {
2104 <                        tick_counter++;
2105 <                        update_display(tick_counter);
2102 >        // In window mode, update display
2103 >        static int tick_counter = 0;
2104 >        if (display_type == DISPLAY_WINDOW) {
2105 >                tick_counter++;
2106 >                if (frame_skip == 0)
2107 >                        update_display_dynamic(tick_counter);
2108 >                else if (tick_counter >= frame_skip) {
2109 >                        tick_counter = 0;
2110 >                        update_display_static();
2111                  }
2112          }
2113 + }
2114 + #endif
2115 +
2116 + #ifdef HAVE_PTHREADS
2117 + static void *redraw_func(void *arg)
2118 + {
2119 +        uint64 start = GetTicks_usec();
2120 +        int64 ticks = 0;
2121 +        uint64 next = GetTicks_usec();
2122 +        while (!redraw_thread_cancel) {
2123 + //              VideoRefresh();
2124 +                video_refresh();
2125 +                next += 16667;
2126 +                int64 delay = next - GetTicks_usec();
2127 +                if (delay > 0)
2128 +                        Delay_usec(delay);
2129 +                else if (delay < -16667)
2130 +                        next = GetTicks_usec();
2131 +                ticks++;
2132 +        }
2133 +        uint64 end = GetTicks_usec();
2134 +        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2135          return NULL;
2136   }
2137 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines