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.12 by cebix, 1999-11-03T21:04:23Z vs.
Revision 1.25 by cebix, 2000-10-13T16:47:52Z

# 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-2000 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 <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 68 | Line 77 | enum {
77          DISPLAY_DGA             // DGA fullscreen display
78   };
79  
71
80   // Constants
81   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
82   const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices";
# Line 80 | 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 113 | Line 125 | 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;
127  
116 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;
124 static XImage *cursor_image, *cursor_mask_image;
125 static Pixmap cursor_map, cursor_mask_map;
138   static Cursor mac_cursor;
127 static GC cursor_gc, cursor_mask_gc;
139   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer
129 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;
143 + static const int sm_uptd[] = {4,1,6,3,0,5,2,7};
144 + static int sm_no_boxes[] = {1,8,32,64,128,300};
145  
146   // Variables for XF86 DGA mode
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 152 | 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 165 | 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;
172                        VideoMonitor.mode = VMODE_1BIT;
267                          break;
268                  case 8:
269                          layout = FLAYOUT_DIRECT;
176                        VideoMonitor.mode = VMODE_8BIT;
270                          break;
271                  case 15:
272                          layout = FLAYOUT_HOST_555;
180                        VideoMonitor.mode = VMODE_16BIT;
273                          break;
274                  case 16:
275                          layout = FLAYOUT_HOST_565;
184                        VideoMonitor.mode = VMODE_16BIT;
276                          break;
277                  case 24:
278                  case 32:
279                          layout = FLAYOUT_HOST_888;
189                        VideoMonitor.mode = VMODE_32BIT;
280                          break;
281          }
192        VideoMonitor.x = width;
193        VideoMonitor.y = height;
194        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 214 | Line 323 | static int error_handler(Display *d, XEr
323   // Init window mode
324   static bool init_window(int width, int height)
325   {
326 +        int aligned_width = (width + 15) & ~15;
327 +        int aligned_height = (height + 15) & ~15;
328 +
329          // Set absolute mouse mode
330          ADBSetRelMouseMode(false);
331  
332          // Read frame skip prefs
333          frame_skip = PrefsFindInt32("frameskip");
222        if (frame_skip == 0)
223                frame_skip = 1;
334  
335          // Create window
336          XSetWindowAttributes wattr;
337          wattr.event_mask = eventmask = win_eventmask;
338          wattr.background_pixel = black_pixel;
339          wattr.border_pixel = black_pixel;
340 <        wattr.backing_store = Always;
340 >        wattr.backing_store = NotUseful;
341 >        wattr.save_under = false;
342          wattr.backing_planes = xdepth;
343  
344          XSync(x_display, false);
# Line 259 | Line 370 | static bool init_window(int width, int h
370          
371          // Try to create and attach SHM image
372          have_shm = false;
373 <        if (depth != 1 && XShmQueryExtension(x_display)) {
373 >        if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
374  
375                  // Create SHM image ("height + 2" for safety)
376                  img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
377 <                shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
378 <                the_buffer = (uint8 *)shmat(shminfo.shmid, 0, 0);
379 <                shminfo.shmaddr = img->data = (char *)the_buffer;
377 >                shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
378 >                the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
379 >                shminfo.shmaddr = img->data = (char *)the_buffer_copy;
380                  shminfo.readOnly = False;
381  
382                  // Try to attach SHM image, catching errors
# Line 286 | Line 397 | static bool init_window(int width, int h
397          
398          // Create normal X image if SHM doesn't work ("height + 2" for safety)
399          if (!have_shm) {
400 <                int bytes_per_row = width;
400 >                int bytes_per_row = aligned_width;
401                  switch (depth) {
402                          case 1:
403                                  bytes_per_row /= 8;
# Line 300 | Line 411 | static bool init_window(int width, int h
411                                  bytes_per_row *= 4;
412                                  break;
413                  }
414 <                the_buffer = (uint8 *)malloc((height + 2) * bytes_per_row);
415 <                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer, width, height, 32, bytes_per_row);
414 >                the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
415 >                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
416          }
417  
418          // 1-Bit mode is big-endian
# Line 310 | Line 421 | static bool init_window(int width, int h
421                  img->bitmap_bit_order = MSBFirst;
422          }
423  
424 <        // Allocate memory for frame buffer copy
425 <        the_buffer_copy = (uint8 *)malloc((height + 2) * img->bytes_per_line);
424 > #ifdef ENABLE_VOSF
425 >        // Allocate a page-aligned chunk of memory for frame buffer
426 >        the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line);
427 >        the_host_buffer = the_buffer_copy;
428 >        
429 >        the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size);
430 >        memset(the_buffer_copy, 0, the_buffer_size);
431 >        
432 >        the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
433 >        memset(the_buffer, 0, the_buffer_size);
434 > #else
435 >        // Allocate memory for frame buffer
436 >        the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
437 > #endif
438  
439          // Create GC
440          the_gc = XCreateGC(x_display, the_win, 0, 0);
441          XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
442  
443 <        // Create cursor
444 <        cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor, 16, 16, 16, 2);
445 <        cursor_image->byte_order = MSBFirst;
446 <        cursor_image->bitmap_bit_order = MSBFirst;
447 <        cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor+32, 16, 16, 16, 2);
448 <        cursor_mask_image->byte_order = MSBFirst;
326 <        cursor_mask_image->bitmap_bit_order = MSBFirst;
327 <        cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
328 <        cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
329 <        cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
330 <        cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
331 <        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
443 >        // Create no_cursor
444 >        mac_cursor = XCreatePixmapCursor(x_display,
445 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
446 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
447 >           &black, &white, 0, 0);
448 >        XDefineCursor(x_display, the_win, mac_cursor);
449  
450          // Set VideoMonitor
451 +        bool native_byte_order;
452   #ifdef WORDS_BIGENDIAN
453 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst);
453 >        native_byte_order = (img->bitmap_bit_order == MSBFirst);
454   #else
455 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst);
455 >        native_byte_order = (img->bitmap_bit_order == LSBFirst);
456 > #endif
457 > #ifdef ENABLE_VOSF
458 >        do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
459   #endif
460 +        set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
461          
462 < #if REAL_ADDRESSING
463 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
342 <        MacFrameLayout = FLAYOUT_DIRECT;
462 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
463 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
464   #else
465          VideoMonitor.mac_frame_base = MacFrameBaseMac;
466   #endif
# Line 349 | Line 470 | static bool init_window(int width, int h
470   // Init fbdev DGA display
471   static bool init_fbdev_dga(char *in_fb_name)
472   {
473 < #if ENABLE_FBDEV_DGA
473 > #ifdef ENABLE_FBDEV_DGA
474          // Find the maximum depth available
475          int ndepths, max_depth(0);
476          int *depths = XListDepths(x_display, screen, &ndepths);
# Line 472 | Line 593 | static bool init_fbdev_dga(char *in_fb_n
593                  }
594          }
595          
596 + #if ENABLE_VOSF
597 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
598 +        // If the blit function is null, i.e. just a copy of the buffer,
599 +        // we first try to avoid the allocation of a temporary frame buffer
600 +        use_vosf = true;
601 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
602 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
603 +                use_vosf = false;
604 +        
605 +        if (use_vosf) {
606 +                the_host_buffer = the_buffer;
607 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
608 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
609 +                memset(the_buffer_copy, 0, the_buffer_size);
610 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
611 +                memset(the_buffer, 0, the_buffer_size);
612 +        }
613 + #else
614 +        use_vosf = false;
615 + #endif
616 + #endif
617 +        
618          set_video_monitor(width, height, bytes_per_row, true);
619 < #if REAL_ADDRESSING
620 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
478 <        MacFrameLayout = FLAYOUT_DIRECT;
619 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
620 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
621   #else
622          VideoMonitor.mac_frame_base = MacFrameBaseMac;
623   #endif
# Line 489 | Line 631 | static bool init_fbdev_dga(char *in_fb_n
631   // Init XF86 DGA display
632   static bool init_xf86_dga(int width, int height)
633   {
634 < #if ENABLE_XF86_DGA
634 > #ifdef ENABLE_XF86_DGA
635          // Set relative mouse mode
636          ADBSetRelMouseMode(true);
637  
638 < #if ENABLE_XF86_VIDMODE
638 > #ifdef ENABLE_XF86_VIDMODE
639          // Switch to best mode
640          if (has_vidmode) {
641                  int best = 0;
# Line 556 | Line 698 | static bool init_xf86_dga(int width, int
698                          bytes_per_row *= 4;
699                          break;
700          }
701 +        
702 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
703 +        // If the blit function is null, i.e. just a copy of the buffer,
704 +        // we first try to avoid the allocation of a temporary frame buffer
705 +        use_vosf = true;
706 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
707 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
708 +                use_vosf = false;
709 +        
710 +        if (use_vosf) {
711 +                the_host_buffer = the_buffer;
712 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
713 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
714 +                memset(the_buffer_copy, 0, the_buffer_size);
715 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
716 +                memset(the_buffer, 0, the_buffer_size);
717 +        }
718 + #elif defined(ENABLE_VOSF)
719 +        // The UAE memory handlers will already handle color conversion, if needed.
720 +        use_vosf = false;
721 + #endif
722 +        
723          set_video_monitor(width, height, bytes_per_row, true);
724 < #if REAL_ADDRESSING
725 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
726 <        MacFrameLayout = FLAYOUT_DIRECT;
724 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
725 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
726 > //      MacFrameLayout = FLAYOUT_DIRECT;
727   #else
728          VideoMonitor.mac_frame_base = MacFrameBaseMac;
729   #endif
# Line 635 | Line 799 | static void keycode_init(void)
799          }
800   }
801  
802 + bool VideoInitBuffer()
803 + {
804 + #ifdef ENABLE_VOSF
805 +        if (use_vosf) {
806 +                const uint32 page_size  = getpagesize();
807 +                const uint32 page_mask  = page_size - 1;
808 +                
809 +                mainBuffer.memBase      = (uint32) the_buffer;
810 +                // Align the frame buffer on page boundary
811 +                mainBuffer.memStart             = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
812 +                mainBuffer.memLength    = the_buffer_size;
813 +                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
814 +
815 +                mainBuffer.pageSize     = page_size;
816 +                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
817 +                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
818 +
819 +                if (mainBuffer.dirtyPages != 0)
820 +                        free(mainBuffer.dirtyPages);
821 +
822 +                mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
823 +
824 +                if (mainBuffer.pageInfo != 0)
825 +                        free(mainBuffer.pageInfo);
826 +
827 +                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
828 +
829 +                if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
830 +                        return false;
831 +
832 +                PFLAG_CLEAR_ALL;
833 +
834 +                uint32 a = 0;
835 +                for (int i = 0; i < mainBuffer.pageCount; i++) {
836 +                        int y1 = a / VideoMonitor.bytes_per_row;
837 +                        if (y1 >= VideoMonitor.y)
838 +                                y1 = VideoMonitor.y - 1;
839 +
840 +                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
841 +                        if (y2 >= VideoMonitor.y)
842 +                                y2 = VideoMonitor.y - 1;
843 +
844 +                        mainBuffer.pageInfo[i].top = y1;
845 +                        mainBuffer.pageInfo[i].bottom = y2;
846 +
847 +                        a += mainBuffer.pageSize;
848 +                        if (a > mainBuffer.memLength)
849 +                                a = mainBuffer.memLength;
850 +                }
851 +                
852 +                // We can now write-protect the frame buffer
853 +                if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0)
854 +                        return false;
855 +        }
856 + #endif
857 +        return true;
858 + }
859 +
860   bool VideoInit(bool classic)
861   {
862 + #ifdef ENABLE_VOSF
863 +        // Open /dev/zero
864 +        zero_fd = open("/dev/zero", O_RDWR);
865 +        if (zero_fd < 0) {
866 +        char str[256];
867 +                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
868 +                ErrorAlert(str);
869 +        return false;
870 +        }
871 +        
872 +        // Zero the mainBuffer structure
873 +        mainBuffer.dirtyPages = 0;
874 +        mainBuffer.pageInfo = 0;
875 + #endif
876 +        
877 +        // Check if X server runs on local machine
878 +        local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
879 +                 || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
880 +    
881          // Init keycode translation
882          keycode_init();
883  
# Line 651 | Line 892 | bool VideoInit(bool classic)
892          // Get screen depth
893          xdepth = DefaultDepth(x_display, screen);
894          
895 < #if ENABLE_FBDEV_DGA
895 > #ifdef ENABLE_FBDEV_DGA
896          // Frame buffer name
897          char fb_name[20];
898          
# Line 662 | Line 903 | bool VideoInit(bool classic)
903                  has_dga = false;
904   #endif
905  
906 < #if ENABLE_XF86_DGA
906 > #ifdef ENABLE_XF86_DGA
907          // DGA available?
908          int dga_event_base, dga_error_base;
909 <        if (XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
909 >        if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
910                  int dga_flags = 0;
911                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
912                  has_dga = dga_flags & XF86DGADirectPresent;
# Line 673 | Line 914 | bool VideoInit(bool classic)
914                  has_dga = false;
915   #endif
916  
917 < #if ENABLE_XF86_VIDMODE
917 > #ifdef ENABLE_XF86_VIDMODE
918          // VidMode available?
919          int vm_event_base, vm_error_base;
920          has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
# Line 746 | Line 987 | bool VideoInit(bool classic)
987          if (mode_str) {
988                  if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
989                          display_type = DISPLAY_WINDOW;
990 < #if ENABLE_FBDEV_DGA
990 > #ifdef ENABLE_FBDEV_DGA
991                  else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
992   #else
993                  else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
# Line 770 | Line 1011 | bool VideoInit(bool classic)
1011                                  return false;
1012                          break;
1013                  case DISPLAY_DGA:
1014 < #if ENABLE_FBDEV_DGA
1014 > #ifdef ENABLE_FBDEV_DGA
1015                          if (!init_fbdev_dga(fb_name))
1016   #else
1017                          if (!init_xf86_dga(width, height))
# Line 779 | Line 1020 | bool VideoInit(bool classic)
1020                          break;
1021          }
1022  
1023 + #ifdef HAVE_PTHREADS
1024          // Lock down frame buffer
1025          pthread_mutex_lock(&frame_buffer_lock);
1026 + #endif
1027  
1028 < #if !REAL_ADDRESSING
1028 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1029          // Set variables for UAE memory mapping
1030          MacFrameBaseHost = the_buffer;
1031          MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
# Line 792 | Line 1035 | bool VideoInit(bool classic)
1035                  MacFrameLayout = FLAYOUT_NONE;
1036   #endif
1037  
1038 <        // Start redraw/input thread
1038 > #ifdef ENABLE_VOSF
1039 >        if (use_vosf) {
1040 >                // Initialize the mainBuffer structure
1041 >                if (!VideoInitBuffer()) {
1042 >                        // TODO: STR_VOSF_INIT_ERR ?
1043 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1044 >                return false;
1045 >                }
1046 >
1047 >                // Initialize the handler for SIGSEGV
1048 >                if (!Screen_fault_handler_init()) {
1049 >                        // TODO: STR_VOSF_INIT_ERR ?
1050 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1051 >                        return false;
1052 >                }
1053 >        }
1054 > #endif
1055 >        
1056 >        // Initialize VideoRefresh function
1057 >        VideoRefreshInit();
1058 >        
1059          XSync(x_display, false);
1060 +
1061 + #ifdef HAVE_PTHREADS
1062 +        // Start redraw/input thread
1063          redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1064 <        if (!redraw_thread_active)
1064 >        if (!redraw_thread_active) {
1065                  printf("FATAL: cannot create redraw thread\n");
1066 <        return redraw_thread_active;
1066 >                return false;
1067 >        }
1068 > #endif
1069 >        return true;
1070   }
1071  
1072  
# Line 807 | Line 1076 | bool VideoInit(bool classic)
1076  
1077   void VideoExit(void)
1078   {
1079 + #ifdef HAVE_PTHREADS
1080          // Stop redraw thread
1081          if (redraw_thread_active) {
1082                  redraw_thread_cancel = true;
# Line 816 | Line 1086 | void VideoExit(void)
1086                  pthread_join(redraw_thread, NULL);
1087                  redraw_thread_active = false;
1088          }
1089 + #endif
1090  
1091 + #ifdef HAVE_PTHREADS
1092          // Unlock frame buffer
1093          pthread_mutex_unlock(&frame_buffer_lock);
1094 + #endif
1095  
1096          // Close window and server connection
1097          if (x_display != NULL) {
1098                  XSync(x_display, false);
1099  
1100 < #if ENABLE_XF86_DGA
1100 > #ifdef ENABLE_XF86_DGA
1101                  if (display_type == DISPLAY_DGA) {
1102                          XF86DGADirectVideo(x_display, screen, 0);
1103                          XUngrabPointer(x_display, CurrentTime);
# Line 832 | Line 1105 | void VideoExit(void)
1105                  }
1106   #endif
1107  
1108 < #if ENABLE_XF86_VIDMODE
1108 > #ifdef ENABLE_XF86_VIDMODE
1109                  if (has_vidmode && display_type == DISPLAY_DGA)
1110                          XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1111   #endif
1112  
1113 < #if ENABLE_FBDEV_DGA
1113 > #ifdef ENABLE_FBDEV_DGA
1114                  if (display_type == DISPLAY_DGA) {
1115                          XUngrabPointer(x_display, CurrentTime);
1116                          XUngrabKeyboard(x_display, CurrentTime);
1117                          close(fbdev_fd);
1118                  }
1119   #endif
847                
848                if (the_buffer_copy) {
849                        free(the_buffer_copy);
850                        the_buffer_copy = NULL;
851                }
1120  
1121                  XFlush(x_display);
1122                  XSync(x_display, false);
# Line 856 | Line 1124 | void VideoExit(void)
1124                          XFreeColormap(x_display, cmap[0]);
1125                          XFreeColormap(x_display, cmap[1]);
1126                  }
1127 +                
1128 +                if (!use_vosf) {
1129 +                        if (the_buffer) {
1130 +                                free(the_buffer);
1131 +                                the_buffer = NULL;
1132 +                        }
1133 +
1134 +                        if (!have_shm && the_buffer_copy) {
1135 +                                free(the_buffer_copy);
1136 +                                the_buffer_copy = NULL;
1137 +                        }
1138 +                }
1139 + #ifdef ENABLE_VOSF
1140 +                else {
1141 +                        if (the_buffer != (uint8 *)MAP_FAILED) {
1142 +                                munmap((caddr_t)the_buffer, the_buffer_size);
1143 +                                the_buffer = 0;
1144 +                        }
1145 +                        
1146 +                        if (the_buffer_copy != (uint8 *)MAP_FAILED) {
1147 +                                munmap((caddr_t)the_buffer_copy, the_buffer_size);
1148 +                                the_buffer_copy = 0;
1149 +                        }
1150 +                }
1151 + #endif
1152          }
1153 +        
1154 + #ifdef ENABLE_VOSF
1155 +        if (use_vosf) {
1156 +                // Clear mainBuffer data
1157 +                if (mainBuffer.pageInfo) {
1158 +                        free(mainBuffer.pageInfo);
1159 +                        mainBuffer.pageInfo = 0;
1160 +                }
1161 +
1162 +                if (mainBuffer.dirtyPages) {
1163 +                        free(mainBuffer.dirtyPages);
1164 +                        mainBuffer.dirtyPages = 0;
1165 +                }
1166 +        }
1167 +
1168 +        // Close /dev/zero
1169 +        if (zero_fd > 0)
1170 +                close(zero_fd);
1171 + #endif
1172   }
1173  
1174  
# Line 882 | Line 1194 | void VideoInterrupt(void)
1194          if (emerg_quit)
1195                  QuitEmulator();
1196  
1197 + #ifdef HAVE_PTHREADS
1198          // Temporarily give up frame buffer lock (this is the point where
1199          // we are suspended when the user presses Ctrl-Tab)
1200          pthread_mutex_unlock(&frame_buffer_lock);
1201          pthread_mutex_lock(&frame_buffer_lock);
1202 + #endif
1203   }
1204  
1205  
# Line 895 | Line 1209 | void VideoInterrupt(void)
1209  
1210   void video_set_palette(uint8 *pal)
1211   {
1212 + #ifdef HAVE_PTHREDS
1213          pthread_mutex_lock(&palette_lock);
1214 + #endif
1215  
1216          // Convert colors to XColor array
1217          for (int i=0; i<256; i++) {
# Line 909 | Line 1225 | void video_set_palette(uint8 *pal)
1225          // Tell redraw thread to change palette
1226          palette_changed = true;
1227  
1228 + #ifdef HAVE_PTHREADS
1229          pthread_mutex_unlock(&palette_lock);
1230 + #endif
1231   }
1232  
1233  
# Line 917 | Line 1235 | void video_set_palette(uint8 *pal)
1235   *  Suspend/resume emulator
1236   */
1237  
1238 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1238 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1239   static void suspend_emul(void)
1240   {
1241          if (display_type == DISPLAY_DGA) {
# Line 925 | Line 1243 | static void suspend_emul(void)
1243                  ADBKeyUp(0x36);
1244                  ctrl_down = false;
1245  
1246 + #ifdef HAVE_PTHREADS
1247                  // Lock frame buffer (this will stop the MacOS thread)
1248                  pthread_mutex_lock(&frame_buffer_lock);
1249 + #endif
1250  
1251                  // Save frame buffer
1252                  fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
# Line 934 | Line 1254 | static void suspend_emul(void)
1254                          memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
1255  
1256                  // Close full screen display
1257 < #if ENABLE_XF86_DGA
1257 > #ifdef ENABLE_XF86_DGA
1258                  XF86DGADirectVideo(x_display, screen, 0);
1259   #endif
1260                  XUngrabPointer(x_display, CurrentTime);
# Line 975 | Line 1295 | static void resume_emul(void)
1295          XSync(x_display, false);
1296          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1297          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1298 < #if ENABLE_XF86_DGA
1298 > #ifdef ENABLE_XF86_DGA
1299          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1300          XF86DGASetViewPort(x_display, screen, 0, 0);
1301   #endif
1302          XSync(x_display, false);
1303 <
1303 >        
1304 >        // the_buffer already contains the data to restore. i.e. since a temporary
1305 >        // frame buffer is used when VOSF is actually used, fb_save is therefore
1306 >        // not necessary.
1307 > #ifdef ENABLE_VOSF
1308 >        if (use_vosf) {
1309 > #ifdef HAVE_PTHREADS
1310 >                pthread_mutex_lock(&Screen_draw_lock);
1311 > #endif
1312 >                PFLAG_SET_ALL;
1313 > #ifdef HAVE_PTHREADS
1314 >                pthread_mutex_unlock(&Screen_draw_lock);
1315 > #endif
1316 >                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1317 >        }
1318 > #endif
1319 >        
1320          // Restore frame buffer
1321          if (fb_save) {
1322 + #ifdef ENABLE_VOSF
1323 +                // Don't copy fb_save to the temporary frame buffer in VOSF mode
1324 +                if (!use_vosf)
1325 + #endif
1326                  memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1327                  free(fb_save);
1328                  fb_save = NULL;
1329          }
1330          
1331 + #ifdef ENABLE_XF86_DGA
1332          if (depth == 8)
992 #if ENABLE_XF86_DGA
1333                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1334   #endif
1335  
1336 + #ifdef HAVE_PTHREADS
1337          // Unlock frame buffer (and continue MacOS thread)
1338          pthread_mutex_unlock(&frame_buffer_lock);
1339          emul_suspended = false;
1340 + #endif
1341   }
1342   #endif
1343  
# Line 1057 | Line 1399 | static int kc_decode(KeySym ks)
1399                  case XK_period: case XK_greater: return 0x2f;
1400                  case XK_slash: case XK_question: return 0x2c;
1401  
1402 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1402 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1403                  case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1404   #else
1405                  case XK_Tab: return 0x30;
# Line 1236 | Line 1578 | static void handle_events(void)
1578                                                  if (code == 0x36)
1579                                                          ctrl_down = true;
1580                                          } else {
1581 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1581 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1582                                                  if (code == 0x31)
1583                                                          resume_emul();  // Space wakes us up
1584   #endif
# Line 1261 | Line 1603 | static void handle_events(void)
1603  
1604                          // Hidden parts exposed, force complete refresh of window
1605                          case Expose:
1606 <                                if (display_type == DISPLAY_WINDOW)
1607 <                                        memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1606 >                                if (display_type == DISPLAY_WINDOW) {
1607 > #ifdef ENABLE_VOSF
1608 >                                        if (use_vosf) {                 // VOSF refresh
1609 > #ifdef HAVE_PTHREADS
1610 >                                                pthread_mutex_lock(&Screen_draw_lock);
1611 > #endif
1612 >                                                PFLAG_SET_ALL;
1613 > #ifdef HAVE_PTHREADS
1614 >                                                pthread_mutex_unlock(&Screen_draw_lock);
1615 > #endif
1616 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1617 >                                        }
1618 >                                        else
1619 > #endif
1620 >                                        if (frame_skip == 0) {  // Dynamic refresh
1621 >                                                int x1, y1;
1622 >                                                for (y1=0; y1<16; y1++)
1623 >                                                for (x1=0; x1<16; x1++)
1624 >                                                        updt_box[x1][y1] = true;
1625 >                                                nr_boxes = 16 * 16;
1626 >                                        } else                                  // Static refresh
1627 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1628 >                                }
1629                                  break;
1630                  }
1631          }
# Line 1273 | Line 1636 | static void handle_events(void)
1636   *  Window display update
1637   */
1638  
1639 < static void update_display(void)
1639 > // Dynamic display update (variable frame rate for each box)
1640 > static void update_display_dynamic(int ticker)
1641 > {
1642 >        int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1643 >        int xil = 0;
1644 >        int rxm = 0, rxmo = 0;
1645 >        int bytes_per_row = VideoMonitor.bytes_per_row;
1646 >        int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1647 >        int rx = VideoMonitor.bytes_per_row / 16;
1648 >        int ry = VideoMonitor.y / 16;
1649 >        int max_box;
1650 >
1651 >        y2s = sm_uptd[ticker % 8];
1652 >        y2a = 8;
1653 >        for (i = 0; i < 6; i++)
1654 >                if (ticker % (2 << i))
1655 >                        break;
1656 >        max_box = sm_no_boxes[i];
1657 >
1658 >        if (y2a) {
1659 >                for (y1=0; y1<16; y1++) {
1660 >                        for (y2=y2s; y2 < ry; y2 += y2a) {
1661 >                                i = ((y1 * ry) + y2) * bytes_per_row;
1662 >                                for (x1=0; x1<16; x1++, i += rx) {
1663 >                                        if (updt_box[x1][y1] == false) {
1664 >                                                if (memcmp(&the_buffer_copy[i], &the_buffer[i], rx)) {
1665 >                                                        updt_box[x1][y1] = true;
1666 >                                                        nr_boxes++;
1667 >                                                }
1668 >                                        }
1669 >                                }
1670 >                        }
1671 >                }
1672 >        }
1673 >
1674 >        if ((nr_boxes <= max_box) && (nr_boxes)) {
1675 >                for (y1=0; y1<16; y1++) {
1676 >                        for (x1=0; x1<16; x1++) {
1677 >                                if (updt_box[x1][y1] == true) {
1678 >                                        if (rxm == 0)
1679 >                                                xm = x1;
1680 >                                        rxm += rx;
1681 >                                        updt_box[x1][y1] = false;
1682 >                                }
1683 >                                if (((updt_box[x1+1][y1] == false) || (x1 == 15)) && (rxm)) {
1684 >                                        if ((rxmo != rxm) || (xmo != xm) || (yo != y1 - 1)) {
1685 >                                                if (rxmo) {
1686 >                                                        xi = xmo * rx;
1687 >                                                        yi = ymo * ry;
1688 >                                                        xil = rxmo;
1689 >                                                        yil = (yo - ymo +1) * ry;
1690 >                                                }
1691 >                                                rxmo = rxm;
1692 >                                                xmo = xm;
1693 >                                                ymo = y1;
1694 >                                        }
1695 >                                        rxm = 0;
1696 >                                        yo = y1;
1697 >                                }      
1698 >                                if (xil) {
1699 >                                        i = (yi * bytes_per_row) + xi;
1700 >                                        for (y2=0; y2 < yil; y2++, i += bytes_per_row)
1701 >                                                memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
1702 >                                        if (depth == 1) {
1703 >                                                if (have_shm)
1704 >                                                        XShmPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
1705 >                                                else
1706 >                                                        XPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil);
1707 >                                        } else {
1708 >                                                if (have_shm)
1709 >                                                        XShmPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0);
1710 >                                                else
1711 >                                                        XPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil);
1712 >                                        }
1713 >                                        xil = 0;
1714 >                                }
1715 >                                if ((x1 == 15) && (y1 == 15) && (rxmo)) {
1716 >                                        x1--;
1717 >                                        xi = xmo * rx;
1718 >                                        yi = ymo * ry;
1719 >                                        xil = rxmo;
1720 >                                        yil = (yo - ymo +1) * ry;
1721 >                                        rxmo = 0;
1722 >                                }
1723 >                        }
1724 >                }
1725 >                nr_boxes = 0;
1726 >        }
1727 > }
1728 >
1729 > // Static display update (fixed frame rate, but incremental)
1730 > static void update_display_static(void)
1731   {
1278        // In classic mode, copy the frame buffer from Mac RAM
1279        if (classic_mode)
1280                Mac2Host_memcpy(the_buffer, 0x3fa700, VideoMonitor.bytes_per_row * VideoMonitor.y);
1281        
1732          // Incremental update code
1733          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1734          int bytes_per_row = VideoMonitor.bytes_per_row;
# Line 1305 | Line 1755 | static void update_display(void)
1755          // Check for first column from left and first column from right that have changed
1756          if (high) {
1757                  if (depth == 1) {
1758 <                        x1 = VideoMonitor.x;
1758 >                        x1 = VideoMonitor.x - 1;
1759                          for (j=y1; j<=y2; j++) {
1760                                  p = &the_buffer[j * bytes_per_row];
1761                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1314 | Line 1764 | static void update_display(void)
1764                                                  x1 = i << 3;
1765                                                  break;
1766                                          }
1767 <                                        p++;
1318 <                                        p2++;
1767 >                                        p++; p2++;
1768                                  }
1769                          }
1770                          x2 = x1;
# Line 1325 | Line 1774 | static void update_display(void)
1774                                  p += bytes_per_row;
1775                                  p2 += bytes_per_row;
1776                                  for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1777 <                                        p--;
1329 <                                        p2--;
1777 >                                        p--; p2--;
1778                                          if (*p != *p2) {
1779 <                                                x2 = i << 3;
1779 >                                                x2 = (i << 3) + 7;
1780                                                  break;
1781                                          }
1782                                  }
1783                          }
1784 <                        wide = x2 - x1;
1784 >                        wide = x2 - x1 + 1;
1785  
1786                          // Update copy of the_buffer
1787                          if (high && wide) {
1788                                  for (j=y1; j<=y2; j++) {
1789                                          i = j * bytes_per_row + (x1 >> 3);
1790 <                                        memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
1790 >                                        memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3);
1791                                  }
1792                          }
1793  
# Line 1348 | Line 1796 | static void update_display(void)
1796                          for (j=y1; j<=y2; j++) {
1797                                  p = &the_buffer[j * bytes_per_row];
1798                                  p2 = &the_buffer_copy[j * bytes_per_row];
1799 <                                for (i=0; i<x1; i++) {
1800 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1801 <                                                x1 = i;
1799 >                                for (i=0; i<x1*bytes_per_pixel; i++) {
1800 >                                        if (*p != *p2) {
1801 >                                                x1 = i / bytes_per_pixel;
1802                                                  break;
1803                                          }
1804 <                                        p += bytes_per_pixel;
1357 <                                        p2 += bytes_per_pixel;
1804 >                                        p++; p2++;
1805                                  }
1806                          }
1807                          x2 = x1;
# Line 1363 | Line 1810 | static void update_display(void)
1810                                  p2 = &the_buffer_copy[j * bytes_per_row];
1811                                  p += bytes_per_row;
1812                                  p2 += bytes_per_row;
1813 <                                for (i=VideoMonitor.x; i>x2; i--) {
1814 <                                        p -= bytes_per_pixel;
1815 <                                        p2 -= bytes_per_pixel;
1816 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1817 <                                                x2 = i;
1813 >                                for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1814 >                                        p--;
1815 >                                        p2--;
1816 >                                        if (*p != *p2) {
1817 >                                                x2 = i / bytes_per_pixel;
1818                                                  break;
1819                                          }
1820                                  }
# Line 1378 | Line 1825 | static void update_display(void)
1825                          if (high && wide) {
1826                                  for (j=y1; j<=y2; j++) {
1827                                          i = j * bytes_per_row + x1 * bytes_per_pixel;
1828 <                                        memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
1828 >                                        memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1829                                  }
1830                          }
1831                  }
# Line 1391 | Line 1838 | static void update_display(void)
1838                  else
1839                          XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1840          }
1841 + }
1842 +
1843  
1844 <        // Has the Mac started? (cursor data is not valid otherwise)
1845 <        if (HasMacStarted()) {
1844 > /*
1845 > *      Screen refresh functions
1846 > */
1847 >
1848 > // The specialisations hereunder are meant to enable VOSF with DGA in direct
1849 > // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1850 > // not get mapped correctly with respect to the predetermined host frame
1851 > // buffer base address.
1852 > //
1853 > // Hmm, in other words, when in direct addressing mode and DGA is requested,
1854 > // we first try to "triple allocate" the address spaces according to the real
1855 > // host frame buffer address. Then, if it fails, we will use a temporary
1856 > // frame buffer thus making the real host frame buffer updated when pages
1857 > // of the temp frame buffer are altered.
1858 > //
1859 > // As a side effect, a little speed gain in screen updates could be noticed
1860 > // for other modes than DGA.
1861 > //
1862 > // The following two functions below are inline so that a clever compiler
1863 > // could specialise the code according to the current screen depth and
1864 > // display type. A more clever compiler would the job by itself though...
1865 > // (update_display_vosf is inlined as well)
1866 >
1867 > static inline void possibly_quit_dga_mode()
1868 > {
1869 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1870 >        // Quit DGA mode if requested
1871 >        if (quit_full_screen) {
1872 >                quit_full_screen = false;
1873 > #ifdef ENABLE_XF86_DGA
1874 >                XF86DGADirectVideo(x_display, screen, 0);
1875 > #endif
1876 >                XUngrabPointer(x_display, CurrentTime);
1877 >                XUngrabKeyboard(x_display, CurrentTime);
1878 >                XUnmapWindow(x_display, the_win);
1879 >                XSync(x_display, false);
1880 >        }
1881 > #endif
1882 > }
1883  
1884 <                // Set new cursor image if it was changed
1885 <                if (memcmp(the_cursor, Mac2HostAddr(0x844), 64)) {
1886 <                        Mac2Host_memcpy(the_cursor, 0x844, 64);
1887 <                        memcpy(cursor_image->data, the_cursor, 32);
1888 <                        memcpy(cursor_mask_image->data, the_cursor+32, 32);
1889 <                        XFreeCursor(x_display, mac_cursor);
1890 <                        XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
1891 <                        XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
1892 <                        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, ReadMacInt8(0x885), ReadMacInt8(0x887));
1893 <                        XDefineCursor(x_display, the_win, mac_cursor);
1884 > static inline void handle_palette_changes(int depth, int display_type)
1885 > {
1886 > #ifdef HAVE_PTHREADS
1887 >        pthread_mutex_lock(&palette_lock);
1888 > #endif
1889 >        if (palette_changed) {
1890 >                palette_changed = false;
1891 >                if (depth == 8) {
1892 >                        XStoreColors(x_display, cmap[0], palette, 256);
1893 >                        XStoreColors(x_display, cmap[1], palette, 256);
1894 >                                
1895 > #ifdef ENABLE_XF86_DGA
1896 >                        if (display_type == DISPLAY_DGA) {
1897 >                                current_dga_cmap ^= 1;
1898 >                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1899 >                        }
1900 > #endif
1901                  }
1902          }
1903 + #ifdef HAVE_PTHREADS
1904 +        pthread_mutex_unlock(&palette_lock);
1905 + #endif
1906 + }
1907 +
1908 + static void video_refresh_dga(void)
1909 + {
1910 +        // Quit DGA mode if requested
1911 +        possibly_quit_dga_mode();
1912 +        
1913 +        // Handle X events
1914 +        handle_events();
1915 +        
1916 +        // Handle palette changes
1917 +        handle_palette_changes(depth, DISPLAY_DGA);
1918 + }
1919 +
1920 + #ifdef ENABLE_VOSF
1921 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
1922 + static void video_refresh_dga_vosf(void)
1923 + {
1924 +        // Quit DGA mode if requested
1925 +        possibly_quit_dga_mode();
1926 +        
1927 +        // Handle X events
1928 +        handle_events();
1929 +        
1930 +        // Handle palette changes
1931 +        handle_palette_changes(depth, DISPLAY_DGA);
1932 +        
1933 +        // Update display (VOSF variant)
1934 +        static int tick_counter = 0;
1935 +        if (++tick_counter >= frame_skip) {
1936 +                tick_counter = 0;
1937 + #ifdef HAVE_PTHREADS
1938 +                pthread_mutex_lock(&Screen_draw_lock);
1939 + #endif
1940 +                update_display_dga_vosf();
1941 + #ifdef HAVE_PTHREADS
1942 +                pthread_mutex_unlock(&Screen_draw_lock);
1943 + #endif
1944 +        }
1945 + }
1946 + #endif
1947 +
1948 + static void video_refresh_window_vosf(void)
1949 + {
1950 +        // Quit DGA mode if requested
1951 +        possibly_quit_dga_mode();
1952 +        
1953 +        // Handle X events
1954 +        handle_events();
1955 +        
1956 +        // Handle palette changes
1957 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1958 +        
1959 +        // Update display (VOSF variant)
1960 +        static int tick_counter = 0;
1961 +        if (++tick_counter >= frame_skip) {
1962 +                tick_counter = 0;
1963 + #ifdef HAVE_PTHREADS
1964 +                pthread_mutex_lock(&Screen_draw_lock);
1965 + #endif
1966 +                update_display_window_vosf();
1967 + #ifdef HAVE_PTHREADS
1968 +                pthread_mutex_unlock(&Screen_draw_lock);
1969 + #endif
1970 +        }
1971 + }
1972 + #endif // def ENABLE_VOSF
1973 +
1974 + static void video_refresh_window_static(void)
1975 + {
1976 +        // Handle X events
1977 +        handle_events();
1978 +        
1979 +        // Handle_palette changes
1980 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1981 +        
1982 +        // Update display (static variant)
1983 +        static int tick_counter = 0;
1984 +        if (++tick_counter >= frame_skip) {
1985 +                tick_counter = 0;
1986 +                update_display_static();
1987 +        }
1988 + }
1989 +
1990 + static void video_refresh_window_dynamic(void)
1991 + {
1992 +        // Handle X events
1993 +        handle_events();
1994 +        
1995 +        // Handle_palette changes
1996 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1997 +        
1998 +        // Update display (dynamic variant)
1999 +        static int tick_counter = 0;
2000 +        tick_counter++;
2001 +        update_display_dynamic(tick_counter);
2002   }
2003  
2004  
# Line 1414 | Line 2006 | static void update_display(void)
2006   *  Thread for screen refresh, input handling etc.
2007   */
2008  
2009 < static void *redraw_func(void *arg)
2009 > void VideoRefreshInit(void)
2010   {
2011 <        int tick_counter = 0;
2011 >        // TODO: set up specialised 8bpp VideoRefresh handlers ?
2012 >        if (display_type == DISPLAY_DGA) {
2013 > #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2014 >                if (use_vosf)
2015 >                        video_refresh = video_refresh_dga_vosf;
2016 >                else
2017 > #endif
2018 >                        video_refresh = video_refresh_dga;
2019 >        }
2020 >        else {
2021 > #ifdef ENABLE_VOSF
2022 >                if (use_vosf)
2023 >                        video_refresh = video_refresh_window_vosf;
2024 >                else
2025 > #endif
2026 >                if (frame_skip == 0)
2027 >                        video_refresh = video_refresh_window_dynamic;
2028 >                else
2029 >                        video_refresh = video_refresh_window_static;
2030 >        }
2031 > }
2032  
2033 <        while (!redraw_thread_cancel) {
2033 > void VideoRefresh(void)
2034 > {
2035 >        // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2036 >        video_refresh();
2037 > }
2038  
2039 <                // Wait
2040 < #ifdef HAVE_NANOSLEEP
2041 <                struct timespec req = {0, 16666667};
2042 <                nanosleep(&req, NULL);
2043 < #else
2044 <                usleep(16667);
2039 > #if 0
2040 > void VideoRefresh(void)
2041 > {
2042 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2043 >        // Quit DGA mode if requested
2044 >        if (quit_full_screen) {
2045 >                quit_full_screen = false;
2046 >                if (display_type == DISPLAY_DGA) {
2047 > #ifdef ENABLE_XF86_DGA
2048 >                        XF86DGADirectVideo(x_display, screen, 0);
2049   #endif
2050 <
2051 < #if ENABLE_XF86_DGA
2052 <                // Quit DGA mode if requested
2053 <                if (quit_full_screen) {
1434 <                        quit_full_screen = false;
1435 <                        if (display_type == DISPLAY_DGA) {
1436 <                                XF86DGADirectVideo(x_display, screen, 0);
1437 <                                XUngrabPointer(x_display, CurrentTime);
1438 <                                XUngrabKeyboard(x_display, CurrentTime);
1439 <                                XUnmapWindow(x_display, the_win);
1440 <                                XSync(x_display, false);
1441 <                        }
2050 >                        XUngrabPointer(x_display, CurrentTime);
2051 >                        XUngrabKeyboard(x_display, CurrentTime);
2052 >                        XUnmapWindow(x_display, the_win);
2053 >                        XSync(x_display, false);
2054                  }
2055 +        }
2056   #endif
2057  
2058 < #if ENABLE_FBDEV_DGA
2059 <                // Quit DGA mode if requested
2060 <                if (quit_full_screen) {
2061 <                        quit_full_screen = false;
2058 >        // Handle X events
2059 >        handle_events();
2060 >
2061 >        // Handle palette changes
2062 > #ifdef HAVE_PTHREADS
2063 >        pthread_mutex_lock(&palette_lock);
2064 > #endif
2065 >        if (palette_changed) {
2066 >                palette_changed = false;
2067 >                if (depth == 8) {
2068 >                        XStoreColors(x_display, cmap[0], palette, 256);
2069 >                        XStoreColors(x_display, cmap[1], palette, 256);
2070 >                                
2071 > #ifdef ENABLE_XF86_DGA
2072                          if (display_type == DISPLAY_DGA) {
2073 <                                XUngrabPointer(x_display, CurrentTime);
2074 <                                XUngrabKeyboard(x_display, CurrentTime);
1452 <                                XUnmapWindow(x_display, the_win);
1453 <                                XSync(x_display, false);
2073 >                                current_dga_cmap ^= 1;
2074 >                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2075                          }
2076 + #endif
2077                  }
2078 +        }
2079 + #ifdef HAVE_PTHREADS
2080 +        pthread_mutex_unlock(&palette_lock);
2081   #endif
1457                // Handle X events
1458                handle_events();
2082  
2083 <                // Handle palette changes
2084 <                pthread_mutex_lock(&palette_lock);
2085 <                if (palette_changed) {
2086 <                        palette_changed = false;
2087 <                        if (depth == 8) {
2088 <                                XStoreColors(x_display, cmap[0], palette, 256);
2089 <                                XStoreColors(x_display, cmap[1], palette, 256);
2090 <                                
2091 < #if ENABLE_XF86_DGA
1469 <                                if (display_type == DISPLAY_DGA) {
1470 <                                        current_dga_cmap ^= 1;
1471 <                                        XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1472 <                                }
1473 < #endif
1474 <                        }
2083 >        // In window mode, update display
2084 >        static int tick_counter = 0;
2085 >        if (display_type == DISPLAY_WINDOW) {
2086 >                tick_counter++;
2087 >                if (frame_skip == 0)
2088 >                        update_display_dynamic(tick_counter);
2089 >                else if (tick_counter >= frame_skip) {
2090 >                        tick_counter = 0;
2091 >                        update_display_static();
2092                  }
2093 <                pthread_mutex_unlock(&palette_lock);
2093 >        }
2094 > }
2095 > #endif
2096  
2097 <                // In window mode, update display and mouse pointer
2098 <                if (display_type == DISPLAY_WINDOW) {
2099 <                        tick_counter++;
2100 <                        if (tick_counter >= frame_skip) {
2101 <                                tick_counter = 0;
2102 <                                update_display();
2103 <                        }
2104 <                }
2097 > #ifdef HAVE_PTHREADS
2098 > static void *redraw_func(void *arg)
2099 > {
2100 >        uint64 start = GetTicks_usec();
2101 >        int64 ticks = 0;
2102 >        uint64 next = GetTicks_usec();
2103 >        while (!redraw_thread_cancel) {
2104 > //              VideoRefresh();
2105 >                video_refresh();
2106 >                next += 16667;
2107 >                int64 delay = next - GetTicks_usec();
2108 >                if (delay > 0)
2109 >                        Delay_usec(delay);
2110 >                else if (delay < -16667)
2111 >                        next = GetTicks_usec();
2112 >                ticks++;
2113          }
2114 +        uint64 end = GetTicks_usec();
2115 +        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2116          return NULL;
2117   }
2118 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines