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.9 by cebix, 1999-10-21T16:40:49Z vs.
Revision 1.20 by gbeauche, 2000-09-25T21:49:19Z

# 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 <math.h> // log()
57 + # include <unistd.h>
58 + # include <signal.h>
59 + # include <fcntl.h>
60 + # include <sys/mman.h>
61 + #endif
62 +
63   #include "cpu_emulation.h"
64   #include "main.h"
65   #include "adb.h"
# Line 45 | Line 68
68   #include "user_strings.h"
69   #include "video.h"
70  
71 < #define DEBUG 1
71 > #define DEBUG 0
72   #include "debug.h"
73  
51 #if ENABLE_XF86_DGA
52 #include <X11/extensions/xf86dga.h>
53 #endif
54
55 #if ENABLE_FBDEV_DGA
56 #include <sys/mman.h>
57 #endif
58
59
74  
75   // Display types
76   enum {
# Line 64 | Line 78 | enum {
78          DISPLAY_DGA             // DGA fullscreen display
79   };
80  
67
81   // Constants
82   const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
83   const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices";
84  
85  
86   // Global variables
87 < static int32 frame_skip;
87 > static int32 frame_skip;                                                        // Prefs items
88 > static int16 mouse_wheel_mode = 1;
89 > static int16 mouse_wheel_lines = 3;
90 >
91   static int display_type = DISPLAY_WINDOW;                       // See enum above
92 + static bool local_X11;                                                          // Flag: X server running on local machine?
93   static uint8 *the_buffer;                                                       // Mac frame buffer
94 +
95 + #ifdef HAVE_PTHREADS
96   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
97   static volatile bool redraw_thread_cancel = false;      // Flag: Cancel Redraw thread
98   static pthread_t redraw_thread;                                         // Redraw thread
99 + #endif
100  
101   static bool has_dga = false;                                            // Flag: Video DGA capable
102 + static bool has_vidmode = false;                                        // Flag: VidMode extension available
103  
104   static bool ctrl_down = false;                                          // Flag: Ctrl key pressed
105   static bool caps_on = false;                                            // Flag: Caps Lock on
# Line 105 | Line 126 | static int eventmask;
126   static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask;
127   static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
128  
108 static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;        // Mutex to protect palette
129   static XColor palette[256];                                                     // Color palette for 8-bit mode
130   static bool palette_changed = false;                            // Flag: Palette changed, redraw thread must set new colors
131 + #ifdef HAVE_PTHREADS
132 + static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER;        // Mutex to protect palette
133 + #endif
134  
135   // Variables for window mode
136   static GC the_gc;
137   static XImage *img = NULL;
138   static XShmSegmentInfo shminfo;
116 static XImage *cursor_image, *cursor_mask_image;
117 static Pixmap cursor_map, cursor_mask_map;
139   static Cursor mac_cursor;
119 static GC cursor_gc, cursor_mask_gc;
140   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer
121 static uint8 the_cursor[64];                                            // Cursor image data
141   static bool have_shm = false;                                           // Flag: SHM extensions available
142 + static bool updt_box[17][17];                                           // Flag for Update
143 + static int nr_boxes;
144 + static const int sm_uptd[] = {4,1,6,3,0,5,2,7};
145 + static int sm_no_boxes[] = {1,8,32,64,128,300};
146  
147   // Variables for XF86 DGA mode
148   static int current_dga_cmap;                                            // Number (0 or 1) of currently installed DGA colormap
149   static Window suspend_win;                                                      // "Suspend" window
150   static void *fb_save = NULL;                                            // Saved frame buffer for suspend
151 + #ifdef HAVE_PTHREADS
152   static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER;   // Mutex to protect frame buffer
153 + #endif
154  
155   // Variables for fbdev DGA mode
156   const char FBDEVICE_FILE_NAME[] = "/dev/fb";
157   static int fbdev_fd;
158  
159 + #ifdef ENABLE_XF86_VIDMODE
160 + // Variables for XF86 VidMode support
161 + static XF86VidModeModeInfo **x_video_modes;                     // Array of all available modes
162 + static int num_x_video_modes;
163 + #endif
164 +
165 + #ifdef ENABLE_VOSF
166 + static bool use_vosf = true;                                            // Flag: VOSF enabled
167 + #else
168 + static const bool use_vosf = false;                                     // Flag: VOSF enabled
169 + #endif
170 +
171 + #ifdef ENABLE_VOSF
172 + static uint8 * the_host_buffer;                                         // Host frame buffer in VOSF mode
173 + static uint32 the_buffer_size;                                          // Size of allocated the_buffer
174 + #endif
175 +
176 + #ifdef ENABLE_VOSF
177 + // Variables for Video on SEGV support (taken from the Win32 port)
178 + struct ScreenPageInfo {
179 +    int top, bottom;                    // Mapping between this virtual page and Mac scanlines
180 + };
181 +
182 + struct ScreenInfo {
183 +    uint32 memBase;                             // Real start address
184 +    uint32 memStart;                    // Start address aligned to page boundary
185 +    uint32 memEnd;                              // Address of one-past-the-end of the screen
186 +    uint32 memLength;                   // Length of the memory addressed by the screen pages
187 +    
188 +    uint32 pageSize;                    // Size of a page
189 +    int pageBits;                               // Shift count to get the page number
190 +    uint32 pageCount;                   // Number of pages allocated to the screen
191 +    
192 +    uint8 * dirtyPages;                 // Table of flags set if page was altered
193 +    ScreenPageInfo * pageInfo;  // Table of mappings page -> Mac scanlines
194 + };
195 +
196 + static ScreenInfo mainBuffer;
197 +
198 + #define PFLAG_SET(page)                 mainBuffer.dirtyPages[page] = 1
199 + #define PFLAG_CLEAR(page)               mainBuffer.dirtyPages[page] = 0
200 + #define PFLAG_ISSET(page)               mainBuffer.dirtyPages[page]
201 + #define PFLAG_ISCLEAR(page)             (mainBuffer.dirtyPages[page] == 0)
202 + #ifdef UNALIGNED_PROFITABLE
203 + # define PFLAG_ISCLEAR_4(page)  (*((uint32 *)(mainBuffer.dirtyPages + page)) == 0)
204 + #else
205 + # define PFLAG_ISCLEAR_4(page)  \
206 +                (mainBuffer.dirtyPages[page  ] == 0) \
207 +        &&      (mainBuffer.dirtyPages[page+1] == 0) \
208 +        &&      (mainBuffer.dirtyPages[page+2] == 0) \
209 +        &&      (mainBuffer.dirtyPages[page+3] == 0)
210 + #endif
211 + #define PFLAG_CLEAR_ALL                 memset(mainBuffer.dirtyPages, 0, mainBuffer.pageCount)
212 + #define PFLAG_SET_ALL                   memset(mainBuffer.dirtyPages, 1, mainBuffer.pageCount)
213 +
214 + static int zero_fd = -1;
215 + static bool Screen_fault_handler_init();
216 + static struct sigaction vosf_sa;
217 +
218 + #ifdef HAVE_PTHREADS
219 + static pthread_mutex_t Screen_draw_lock = PTHREAD_MUTEX_INITIALIZER;    // Mutex to protect frame buffer (dirtyPages in fact)
220 + #endif
221 +
222 + #endif
223 +
224 + // VideoRefresh function
225 + void VideoRefreshInit(void);
226 + static void (*video_refresh)(void);
227  
228   // Prototypes
229   static void *redraw_func(void *arg);
# Line 138 | Line 231 | static int event2keycode(XKeyEvent *ev);
231  
232  
233   // From main_unix.cpp
234 + extern char *x_display_name;
235   extern Display *x_display;
236  
237   // From sys_unix.cpp
238   extern void SysMountFirstFloppy(void);
239  
240 + #ifdef ENABLE_VOSF
241 + # include "video_vosf.h"
242 + #endif
243 +
244  
245   /*
246   *  Initialization
# Line 151 | Line 249 | extern void SysMountFirstFloppy(void);
249   // Set VideoMonitor according to video mode
250   void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order)
251   {
252 + #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
253          int layout = FLAYOUT_DIRECT;
254          switch (depth) {
255                  case 1:
256                          layout = FLAYOUT_DIRECT;
158                        VideoMonitor.mode = VMODE_1BIT;
257                          break;
258                  case 8:
259                          layout = FLAYOUT_DIRECT;
162                        VideoMonitor.mode = VMODE_8BIT;
260                          break;
261                  case 15:
262                          layout = FLAYOUT_HOST_555;
166                        VideoMonitor.mode = VMODE_16BIT;
263                          break;
264                  case 16:
265                          layout = FLAYOUT_HOST_565;
170                        VideoMonitor.mode = VMODE_16BIT;
266                          break;
267                  case 24:
268                  case 32:
269                          layout = FLAYOUT_HOST_888;
175                        VideoMonitor.mode = VMODE_32BIT;
270                          break;
271          }
178        VideoMonitor.x = width;
179        VideoMonitor.y = height;
180        VideoMonitor.bytes_per_row = bytes_per_row;
272          if (native_byte_order)
273                  MacFrameLayout = layout;
274          else
275                  MacFrameLayout = FLAYOUT_DIRECT;
276 + #endif
277 +        switch (depth) {
278 +                case 1:
279 +                        VideoMonitor.mode = VMODE_1BIT;
280 +                        break;
281 +                case 8:
282 +                        VideoMonitor.mode = VMODE_8BIT;
283 +                        break;
284 +                case 15:
285 +                        VideoMonitor.mode = VMODE_16BIT;
286 +                        break;
287 +                case 16:
288 +                        VideoMonitor.mode = VMODE_16BIT;
289 +                        break;
290 +                case 24:
291 +                case 32:
292 +                        VideoMonitor.mode = VMODE_32BIT;
293 +                        break;
294 +        }
295 +        VideoMonitor.x = width;
296 +        VideoMonitor.y = height;
297 +        VideoMonitor.bytes_per_row = bytes_per_row;
298   }
299  
300   // Trap SHM errors
# Line 200 | Line 313 | static int error_handler(Display *d, XEr
313   // Init window mode
314   static bool init_window(int width, int height)
315   {
316 +        int aligned_width = (width + 15) & ~15;
317 +        int aligned_height = (height + 15) & ~15;
318 +
319          // Set absolute mouse mode
320          ADBSetRelMouseMode(false);
321  
322          // Read frame skip prefs
323          frame_skip = PrefsFindInt32("frameskip");
208        if (frame_skip == 0)
209                frame_skip = 1;
324  
325          // Create window
326          XSetWindowAttributes wattr;
327          wattr.event_mask = eventmask = win_eventmask;
328          wattr.background_pixel = black_pixel;
329          wattr.border_pixel = black_pixel;
330 <        wattr.backing_store = Always;
330 >        wattr.backing_store = NotUseful;
331 >        wattr.save_under = false;
332          wattr.backing_planes = xdepth;
333  
334          XSync(x_display, false);
# Line 245 | Line 360 | static bool init_window(int width, int h
360          
361          // Try to create and attach SHM image
362          have_shm = false;
363 <        if (depth != 1 && XShmQueryExtension(x_display)) {
363 >        if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
364  
365                  // Create SHM image ("height + 2" for safety)
366                  img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
367 <                shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
368 <                the_buffer = (uint8 *)shmat(shminfo.shmid, 0, 0);
369 <                shminfo.shmaddr = img->data = (char *)the_buffer;
367 >                shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
368 >                the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
369 >                shminfo.shmaddr = img->data = (char *)the_buffer_copy;
370                  shminfo.readOnly = False;
371  
372                  // Try to attach SHM image, catching errors
# Line 272 | Line 387 | static bool init_window(int width, int h
387          
388          // Create normal X image if SHM doesn't work ("height + 2" for safety)
389          if (!have_shm) {
390 <                int bytes_per_row = width;
390 >                int bytes_per_row = aligned_width;
391                  switch (depth) {
392                          case 1:
393                                  bytes_per_row /= 8;
# Line 286 | Line 401 | static bool init_window(int width, int h
401                                  bytes_per_row *= 4;
402                                  break;
403                  }
404 <                the_buffer = (uint8 *)malloc((height + 2) * bytes_per_row);
405 <                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer, width, height, 32, bytes_per_row);
404 >                the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
405 >                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
406          }
407  
408          // 1-Bit mode is big-endian
# Line 296 | Line 411 | static bool init_window(int width, int h
411                  img->bitmap_bit_order = MSBFirst;
412          }
413  
414 <        // Allocate memory for frame buffer copy
415 <        the_buffer_copy = (uint8 *)malloc((height + 2) * img->bytes_per_line);
414 > #ifdef ENABLE_VOSF
415 >        // Allocate a page-aligned chunk of memory for frame buffer
416 >        the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line);
417 >        the_host_buffer = the_buffer_copy;
418 >        
419 >        the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size);
420 >        memset(the_buffer_copy, 0, the_buffer_size);
421 >        
422 >        the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
423 >        memset(the_buffer, 0, the_buffer_size);
424 > #else
425 >        // Allocate memory for frame buffer
426 >        the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
427 > #endif
428  
429          // Create GC
430          the_gc = XCreateGC(x_display, the_win, 0, 0);
431          XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
432  
433 <        // Create cursor
434 <        cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor, 16, 16, 16, 2);
435 <        cursor_image->byte_order = MSBFirst;
436 <        cursor_image->bitmap_bit_order = MSBFirst;
437 <        cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor+32, 16, 16, 16, 2);
438 <        cursor_mask_image->byte_order = MSBFirst;
312 <        cursor_mask_image->bitmap_bit_order = MSBFirst;
313 <        cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
314 <        cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
315 <        cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
316 <        cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
317 <        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
433 >        // Create no_cursor
434 >        mac_cursor = XCreatePixmapCursor(x_display,
435 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
436 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
437 >           &black, &white, 0, 0);
438 >        XDefineCursor(x_display, the_win, mac_cursor);
439  
440          // Set VideoMonitor
441 +        bool native_byte_order;
442   #ifdef WORDS_BIGENDIAN
443 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst);
443 >        native_byte_order = (img->bitmap_bit_order == MSBFirst);
444   #else
445 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst);
445 >        native_byte_order = (img->bitmap_bit_order == LSBFirst);
446 > #endif
447 > #ifdef ENABLE_VOSF
448 >        do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
449   #endif
450 +        set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
451          
452 < #if REAL_ADDRESSING
453 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
328 <        MacFrameLayout = FLAYOUT_DIRECT;
452 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
453 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
454   #else
455          VideoMonitor.mac_frame_base = MacFrameBaseMac;
456   #endif
# Line 335 | Line 460 | static bool init_window(int width, int h
460   // Init fbdev DGA display
461   static bool init_fbdev_dga(char *in_fb_name)
462   {
463 < #if ENABLE_FBDEV_DGA
463 > #ifdef ENABLE_FBDEV_DGA
464          // Find the maximum depth available
465          int ndepths, max_depth(0);
466          int *depths = XListDepths(x_display, screen, &ndepths);
# Line 458 | Line 583 | static bool init_fbdev_dga(char *in_fb_n
583                  }
584          }
585          
586 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
587 +        // If the blit function is null, i.e. just a copy of the buffer,
588 +        // we first try to avoid the allocation of a temporary frame buffer
589 +        use_vosf = true;
590 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
591 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
592 +                use_vosf = false;
593 +        
594 +        if (use_vosf) {
595 +                the_host_buffer = the_buffer;
596 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
597 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
598 +                memset(the_buffer_copy, 0, the_buffer_size);
599 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
600 +                memset(the_buffer, 0, the_buffer_size);
601 +        }
602 + #elif ENABLE_VOSF
603 +        use_vosf = false;
604 + #endif
605 +        
606          set_video_monitor(width, height, bytes_per_row, true);
607 < #if REAL_ADDRESSING
608 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
464 <        MacFrameLayout = FLAYOUT_DIRECT;
607 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
608 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
609   #else
610          VideoMonitor.mac_frame_base = MacFrameBaseMac;
611   #endif
# Line 475 | Line 619 | static bool init_fbdev_dga(char *in_fb_n
619   // Init XF86 DGA display
620   static bool init_xf86_dga(int width, int height)
621   {
622 < #if ENABLE_XF86_DGA
622 > #ifdef ENABLE_XF86_DGA
623          // Set relative mouse mode
624          ADBSetRelMouseMode(true);
625  
626 + #ifdef ENABLE_XF86_VIDMODE
627 +        // Switch to best mode
628 +        if (has_vidmode) {
629 +                int best = 0;
630 +                for (int i=1; i<num_x_video_modes; i++) {
631 +                        if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
632 +                                x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
633 +                                best = i;
634 +                        }
635 +                }
636 +                XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
637 +                XF86VidModeSetViewPort(x_display, screen, 0, 0);
638 +        }
639 + #endif
640 +
641          // Create window
642          XSetWindowAttributes wattr;
643          wattr.event_mask = eventmask = dga_eventmask;
# Line 527 | Line 686 | static bool init_xf86_dga(int width, int
686                          bytes_per_row *= 4;
687                          break;
688          }
689 +        
690 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
691 +        // If the blit function is null, i.e. just a copy of the buffer,
692 +        // we first try to avoid the allocation of a temporary frame buffer
693 +        use_vosf = true;
694 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
695 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
696 +                use_vosf = false;
697 +        
698 +        if (use_vosf) {
699 +                the_host_buffer = the_buffer;
700 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
701 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
702 +                memset(the_buffer_copy, 0, the_buffer_size);
703 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
704 +                memset(the_buffer, 0, the_buffer_size);
705 +        }
706 + #elif ENABLE_VOSF
707 +        // The UAE memory handlers will already handle color conversion, if needed.
708 +        use_vosf = false;
709 + #endif
710 +        
711          set_video_monitor(width, height, bytes_per_row, true);
712 < #if REAL_ADDRESSING
713 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
714 <        MacFrameLayout = FLAYOUT_DIRECT;
712 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
713 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
714 > //      MacFrameLayout = FLAYOUT_DIRECT;
715   #else
716          VideoMonitor.mac_frame_base = MacFrameBaseMac;
717   #endif
# Line 606 | Line 787 | static void keycode_init(void)
787          }
788   }
789  
790 + bool VideoInitBuffer()
791 + {
792 + #ifdef ENABLE_VOSF
793 +        if (use_vosf) {
794 +                const uint32 page_size  = getpagesize();
795 +                const uint32 page_mask  = page_size - 1;
796 +                
797 +                mainBuffer.memBase      = (uint32) the_buffer;
798 +                // Align the frame buffer on page boundary
799 +                mainBuffer.memStart             = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
800 +                mainBuffer.memLength    = the_buffer_size;
801 +                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
802 +
803 +                mainBuffer.pageSize     = page_size;
804 +                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
805 +                mainBuffer.pageBits     = int( log(mainBuffer.pageSize) / log(2.0) );
806 +
807 +                if (mainBuffer.dirtyPages != 0)
808 +                        free(mainBuffer.dirtyPages);
809 +
810 +                mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
811 +
812 +                if (mainBuffer.pageInfo != 0)
813 +                        free(mainBuffer.pageInfo);
814 +
815 +                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
816 +
817 +                if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
818 +                        return false;
819 +
820 +                PFLAG_CLEAR_ALL;
821 +
822 +                uint32 a = 0;
823 +                for (int i = 0; i < mainBuffer.pageCount; i++) {
824 +                        int y1 = a / VideoMonitor.bytes_per_row;
825 +                        if (y1 >= VideoMonitor.y)
826 +                                y1 = VideoMonitor.y - 1;
827 +
828 +                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
829 +                        if (y2 >= VideoMonitor.y)
830 +                                y2 = VideoMonitor.y - 1;
831 +
832 +                        mainBuffer.pageInfo[i].top = y1;
833 +                        mainBuffer.pageInfo[i].bottom = y2;
834 +
835 +                        a += mainBuffer.pageSize;
836 +                        if (a > mainBuffer.memLength)
837 +                                a = mainBuffer.memLength;
838 +                }
839 +                
840 +                // We can now write-protect the frame buffer
841 +                if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0)
842 +                        return false;
843 +        }
844 + #endif
845 +        return true;
846 + }
847 +
848   bool VideoInit(bool classic)
849   {
850 + #ifdef ENABLE_VOSF
851 +        // Open /dev/zero
852 +        zero_fd = open("/dev/zero", O_RDWR);
853 +        if (zero_fd < 0) {
854 +        char str[256];
855 +                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
856 +                ErrorAlert(str);
857 +        return false;
858 +        }
859 +        
860 +        // Zero the mainBuffer structure
861 +        mainBuffer.dirtyPages = 0;
862 +        mainBuffer.pageInfo = 0;
863 + #endif
864 +        
865 +        // Check if X server runs on local machine
866 +        local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
867 +                 || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
868 +    
869          // Init keycode translation
870          keycode_init();
871  
872 +        // Read prefs
873 +        mouse_wheel_mode = PrefsFindInt16("mousewheelmode");
874 +        mouse_wheel_lines = PrefsFindInt16("mousewheellines");
875 +
876          // Find screen and root window
877          screen = XDefaultScreen(x_display);
878          rootwin = XRootWindow(x_display, screen);
# Line 618 | Line 880 | bool VideoInit(bool classic)
880          // Get screen depth
881          xdepth = DefaultDepth(x_display, screen);
882          
883 < #if ENABLE_FBDEV_DGA
883 > #ifdef ENABLE_FBDEV_DGA
884          // Frame buffer name
885          char fb_name[20];
886          
# Line 628 | Line 890 | bool VideoInit(bool classic)
890          else
891                  has_dga = false;
892   #endif
893 <        
894 < #if ENABLE_XF86_DGA
893 >
894 > #ifdef ENABLE_XF86_DGA
895          // DGA available?
896 <        int event_base, error_base;
897 <        if (XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
896 >        int dga_event_base, dga_error_base;
897 >        if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
898                  int dga_flags = 0;
899                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
900                  has_dga = dga_flags & XF86DGADirectPresent;
# Line 640 | Line 902 | bool VideoInit(bool classic)
902                  has_dga = false;
903   #endif
904  
905 + #ifdef ENABLE_XF86_VIDMODE
906 +        // VidMode available?
907 +        int vm_event_base, vm_error_base;
908 +        has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
909 +        if (has_vidmode)
910 +                XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
911 + #endif
912 +        
913          // Find black and white colors
914          XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
915          XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
# Line 705 | Line 975 | bool VideoInit(bool classic)
975          if (mode_str) {
976                  if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
977                          display_type = DISPLAY_WINDOW;
978 < #if ENABLE_FBDEV_DGA
978 > #ifdef ENABLE_FBDEV_DGA
979                  else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
980   #else
981                  else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
# Line 729 | Line 999 | bool VideoInit(bool classic)
999                                  return false;
1000                          break;
1001                  case DISPLAY_DGA:
1002 < #if ENABLE_FBDEV_DGA
1002 > #ifdef ENABLE_FBDEV_DGA
1003                          if (!init_fbdev_dga(fb_name))
1004   #else
1005                          if (!init_xf86_dga(width, height))
# Line 738 | Line 1008 | bool VideoInit(bool classic)
1008                          break;
1009          }
1010  
1011 + #ifdef HAVE_PTHREADS
1012          // Lock down frame buffer
1013          pthread_mutex_lock(&frame_buffer_lock);
1014 + #endif
1015  
1016 < #if !REAL_ADDRESSING
1016 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1017          // Set variables for UAE memory mapping
1018          MacFrameBaseHost = the_buffer;
1019          MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
# Line 751 | Line 1023 | bool VideoInit(bool classic)
1023                  MacFrameLayout = FLAYOUT_NONE;
1024   #endif
1025  
1026 <        // Start redraw/input thread
1026 > #ifdef ENABLE_VOSF
1027 >        if (use_vosf) {
1028 >                // Initialize the mainBuffer structure
1029 >                if (!VideoInitBuffer()) {
1030 >                        // TODO: STR_VOSF_INIT_ERR ?
1031 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1032 >                return false;
1033 >                }
1034 >
1035 >                // Initialize the handler for SIGSEGV
1036 >                if (!Screen_fault_handler_init()) {
1037 >                        // TODO: STR_VOSF_INIT_ERR ?
1038 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1039 >                        return false;
1040 >                }
1041 >        }
1042 > #endif
1043 >        
1044 >        // Initialize VideoRefresh function
1045 >        VideoRefreshInit();
1046 >        
1047          XSync(x_display, false);
1048 +
1049 + #ifdef HAVE_PTHREADS
1050 +        // Start redraw/input thread
1051          redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1052 <        if (!redraw_thread_active)
1052 >        if (!redraw_thread_active) {
1053                  printf("FATAL: cannot create redraw thread\n");
1054 <        return redraw_thread_active;
1054 >                return false;
1055 >        }
1056 > #endif
1057 >        return true;
1058   }
1059  
1060  
# Line 766 | Line 1064 | bool VideoInit(bool classic)
1064  
1065   void VideoExit(void)
1066   {
1067 + #ifdef HAVE_PTHREADS
1068          // Stop redraw thread
1069          if (redraw_thread_active) {
1070                  redraw_thread_cancel = true;
# Line 775 | Line 1074 | void VideoExit(void)
1074                  pthread_join(redraw_thread, NULL);
1075                  redraw_thread_active = false;
1076          }
1077 + #endif
1078  
1079 + #ifdef HAVE_PTHREADS
1080          // Unlock frame buffer
1081          pthread_mutex_unlock(&frame_buffer_lock);
1082 + #endif
1083  
1084          // Close window and server connection
1085          if (x_display != NULL) {
1086                  XSync(x_display, false);
1087  
1088 < #if ENABLE_XF86_DGA
1088 > #ifdef ENABLE_XF86_DGA
1089                  if (display_type == DISPLAY_DGA) {
1090                          XF86DGADirectVideo(x_display, screen, 0);
1091                          XUngrabPointer(x_display, CurrentTime);
# Line 791 | Line 1093 | void VideoExit(void)
1093                  }
1094   #endif
1095  
1096 < #if ENABLE_FBDEV_DGA
1096 > #ifdef ENABLE_XF86_VIDMODE
1097 >                if (has_vidmode && display_type == DISPLAY_DGA)
1098 >                        XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1099 > #endif
1100 >
1101 > #ifdef ENABLE_FBDEV_DGA
1102                  if (display_type == DISPLAY_DGA) {
1103                          XUngrabPointer(x_display, CurrentTime);
1104                          XUngrabKeyboard(x_display, CurrentTime);
1105                          close(fbdev_fd);
1106                  }
1107   #endif
801                
802                if (the_buffer_copy) {
803                        free(the_buffer_copy);
804                        the_buffer_copy = NULL;
805                }
1108  
1109                  XFlush(x_display);
1110                  XSync(x_display, false);
# Line 810 | Line 1112 | void VideoExit(void)
1112                          XFreeColormap(x_display, cmap[0]);
1113                          XFreeColormap(x_display, cmap[1]);
1114                  }
1115 +                
1116 +                if (!use_vosf) {
1117 +                        if (the_buffer) {
1118 +                                free(the_buffer);
1119 +                                the_buffer = NULL;
1120 +                        }
1121 +
1122 +                        if (!have_shm && the_buffer_copy) {
1123 +                                free(the_buffer_copy);
1124 +                                the_buffer_copy = NULL;
1125 +                        }
1126 +                }
1127 + #ifdef ENABLE_VOSF
1128 +                else {
1129 +                        if (the_buffer != (uint8 *)MAP_FAILED) {
1130 +                                munmap((caddr_t)the_buffer, the_buffer_size);
1131 +                                the_buffer = 0;
1132 +                        }
1133 +                        
1134 +                        if (the_buffer_copy != (uint8 *)MAP_FAILED) {
1135 +                                munmap((caddr_t)the_buffer_copy, the_buffer_size);
1136 +                                the_buffer_copy = 0;
1137 +                        }
1138 +                }
1139 + #endif
1140 +        }
1141 +        
1142 + #ifdef ENABLE_VOSF
1143 +        if (use_vosf) {
1144 +                // Clear mainBuffer data
1145 +                if (mainBuffer.pageInfo) {
1146 +                        free(mainBuffer.pageInfo);
1147 +                        mainBuffer.pageInfo = 0;
1148 +                }
1149 +
1150 +                if (mainBuffer.dirtyPages) {
1151 +                        free(mainBuffer.dirtyPages);
1152 +                        mainBuffer.dirtyPages = 0;
1153 +                }
1154          }
1155 +
1156 +        // Close /dev/zero
1157 +        if (zero_fd > 0)
1158 +                close(zero_fd);
1159 + #endif
1160   }
1161  
1162  
# Line 836 | Line 1182 | void VideoInterrupt(void)
1182          if (emerg_quit)
1183                  QuitEmulator();
1184  
1185 + #ifdef HAVE_PTHREADS
1186          // Temporarily give up frame buffer lock (this is the point where
1187          // we are suspended when the user presses Ctrl-Tab)
1188          pthread_mutex_unlock(&frame_buffer_lock);
1189          pthread_mutex_lock(&frame_buffer_lock);
1190 + #endif
1191   }
1192  
1193  
# Line 849 | Line 1197 | void VideoInterrupt(void)
1197  
1198   void video_set_palette(uint8 *pal)
1199   {
1200 + #ifdef HAVE_PTHREDS
1201          pthread_mutex_lock(&palette_lock);
1202 + #endif
1203  
1204          // Convert colors to XColor array
1205          for (int i=0; i<256; i++) {
# Line 863 | Line 1213 | void video_set_palette(uint8 *pal)
1213          // Tell redraw thread to change palette
1214          palette_changed = true;
1215  
1216 + #ifdef HAVE_PTHREADS
1217          pthread_mutex_unlock(&palette_lock);
1218 + #endif
1219   }
1220  
1221  
# Line 871 | Line 1223 | void video_set_palette(uint8 *pal)
1223   *  Suspend/resume emulator
1224   */
1225  
1226 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1226 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1227   static void suspend_emul(void)
1228   {
1229          if (display_type == DISPLAY_DGA) {
# Line 879 | Line 1231 | static void suspend_emul(void)
1231                  ADBKeyUp(0x36);
1232                  ctrl_down = false;
1233  
1234 + #ifdef HAVE_PTHREADS
1235                  // Lock frame buffer (this will stop the MacOS thread)
1236                  pthread_mutex_lock(&frame_buffer_lock);
1237 + #endif
1238  
1239                  // Save frame buffer
1240                  fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
# Line 888 | Line 1242 | static void suspend_emul(void)
1242                          memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
1243  
1244                  // Close full screen display
1245 < #if ENABLE_XF86_DGA
1245 > #ifdef ENABLE_XF86_DGA
1246                  XF86DGADirectVideo(x_display, screen, 0);
1247   #endif
1248                  XUngrabPointer(x_display, CurrentTime);
# Line 929 | Line 1283 | static void resume_emul(void)
1283          XSync(x_display, false);
1284          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1285          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1286 < #if ENABLE_XF86_DGA
1286 > #ifdef ENABLE_XF86_DGA
1287          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1288          XF86DGASetViewPort(x_display, screen, 0, 0);
1289   #endif
# Line 937 | Line 1291 | static void resume_emul(void)
1291  
1292          // Restore frame buffer
1293          if (fb_save) {
1294 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
1295 +                if (use_vosf)
1296 +                        mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ|PROT_WRITE);
1297 + #endif
1298                  memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1299 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
1300 +                if (use_vosf) {
1301 +                        mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ);
1302 +                        do_update_framebuffer(the_host_buffer, the_buffer, VideoMonitor.x * VideoMonitor.bytes_per_row);
1303 + #ifdef HAVE_PTHREADS
1304 +                        pthread_mutex_lock(&Screen_draw_lock);
1305 + #endif
1306 +                        PFLAG_CLEAR_ALL;
1307 + #ifdef HAVE_PTHREADS
1308 +                        pthread_mutex_unlock(&Screen_draw_lock);
1309 + #endif
1310 +                }
1311 + #endif
1312                  free(fb_save);
1313                  fb_save = NULL;
1314          }
1315          
1316 + #ifdef ENABLE_XF86_DGA
1317          if (depth == 8)
946 #if ENABLE_XF86_DGA
1318                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1319   #endif
1320  
1321 + #ifdef HAVE_PTHREADS
1322          // Unlock frame buffer (and continue MacOS thread)
1323          pthread_mutex_unlock(&frame_buffer_lock);
1324          emul_suspended = false;
1325 + #endif
1326   }
1327   #endif
1328  
# Line 1011 | Line 1384 | static int kc_decode(KeySym ks)
1384                  case XK_period: case XK_greater: return 0x2f;
1385                  case XK_slash: case XK_question: return 0x2c;
1386  
1387 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1387 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1388                  case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1389   #else
1390                  case XK_Tab: return 0x30;
# Line 1137 | Line 1510 | static void handle_events(void)
1510                                  unsigned int button = ((XButtonEvent *)&event)->button;
1511                                  if (button < 4)
1512                                          ADBMouseDown(button - 1);
1513 +                                else if (button < 6) {  // Wheel mouse
1514 +                                        if (mouse_wheel_mode == 0) {
1515 +                                                int key = (button == 5) ? 0x79 : 0x74;  // Page up/down
1516 +                                                ADBKeyDown(key);
1517 +                                                ADBKeyUp(key);
1518 +                                        } else {
1519 +                                                int key = (button == 5) ? 0x3d : 0x3e;  // Cursor up/down
1520 +                                                for(int i=0; i<mouse_wheel_lines; i++) {
1521 +                                                        ADBKeyDown(key);
1522 +                                                        ADBKeyUp(key);
1523 +                                                }
1524 +                                        }
1525 +                                }
1526                                  break;
1527                          }
1528                          case ButtonRelease: {
# Line 1177 | Line 1563 | static void handle_events(void)
1563                                                  if (code == 0x36)
1564                                                          ctrl_down = true;
1565                                          } else {
1566 < #if ENABLE_XF86_DGA || ENABLE_FBDEV_DGA
1566 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1567                                                  if (code == 0x31)
1568                                                          resume_emul();  // Space wakes us up
1569   #endif
# Line 1202 | Line 1588 | static void handle_events(void)
1588  
1589                          // Hidden parts exposed, force complete refresh of window
1590                          case Expose:
1591 <                                if (display_type == DISPLAY_WINDOW)
1592 <                                        memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1591 >                                if (display_type == DISPLAY_WINDOW) {
1592 >                                        if (frame_skip == 0) {  // Dynamic refresh
1593 >                                                int x1, y1;
1594 >                                                for (y1=0; y1<16; y1++)
1595 >                                                for (x1=0; x1<16; x1++)
1596 >                                                        updt_box[x1][y1] = true;
1597 >                                                nr_boxes = 16 * 16;
1598 >                                        } else {
1599 > #ifdef ENABLE_VOSF
1600 > #ifdef HAVE_PTHREADS
1601 >                                                pthread_mutex_lock(&Screen_draw_lock);
1602 > #endif
1603 >                                                PFLAG_SET_ALL;
1604 > #ifdef HAVE_PTHREADS
1605 >                                                pthread_mutex_unlock(&Screen_draw_lock);
1606 > #endif
1607 > #endif
1608 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1609 >                                        }
1610 >                                }
1611                                  break;
1612                  }
1613          }
# Line 1214 | Line 1618 | static void handle_events(void)
1618   *  Window display update
1619   */
1620  
1621 < static void update_display(void)
1621 > // Dynamic display update (variable frame rate for each box)
1622 > static void update_display_dynamic(int ticker)
1623 > {
1624 >        int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1625 >        int xil = 0;
1626 >        int rxm = 0, rxmo = 0;
1627 >        int bytes_per_row = VideoMonitor.bytes_per_row;
1628 >        int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1629 >        int rx = VideoMonitor.bytes_per_row / 16;
1630 >        int ry = VideoMonitor.y / 16;
1631 >        int max_box;
1632 >
1633 >        y2s = sm_uptd[ticker % 8];
1634 >        y2a = 8;
1635 >        for (i = 0; i < 6; i++)
1636 >                if (ticker % (2 << i))
1637 >                        break;
1638 >        max_box = sm_no_boxes[i];
1639 >
1640 >        if (y2a) {
1641 >                for (y1=0; y1<16; y1++) {
1642 >                        for (y2=y2s; y2 < ry; y2 += y2a) {
1643 >                                i = ((y1 * ry) + y2) * bytes_per_row;
1644 >                                for (x1=0; x1<16; x1++, i += rx) {
1645 >                                        if (updt_box[x1][y1] == false) {
1646 >                                                if (memcmp(&the_buffer_copy[i], &the_buffer[i], rx)) {
1647 >                                                        updt_box[x1][y1] = true;
1648 >                                                        nr_boxes++;
1649 >                                                }
1650 >                                        }
1651 >                                }
1652 >                        }
1653 >                }
1654 >        }
1655 >
1656 >        if ((nr_boxes <= max_box) && (nr_boxes)) {
1657 >                for (y1=0; y1<16; y1++) {
1658 >                        for (x1=0; x1<16; x1++) {
1659 >                                if (updt_box[x1][y1] == true) {
1660 >                                        if (rxm == 0)
1661 >                                                xm = x1;
1662 >                                        rxm += rx;
1663 >                                        updt_box[x1][y1] = false;
1664 >                                }
1665 >                                if (((updt_box[x1+1][y1] == false) || (x1 == 15)) && (rxm)) {
1666 >                                        if ((rxmo != rxm) || (xmo != xm) || (yo != y1 - 1)) {
1667 >                                                if (rxmo) {
1668 >                                                        xi = xmo * rx;
1669 >                                                        yi = ymo * ry;
1670 >                                                        xil = rxmo;
1671 >                                                        yil = (yo - ymo +1) * ry;
1672 >                                                }
1673 >                                                rxmo = rxm;
1674 >                                                xmo = xm;
1675 >                                                ymo = y1;
1676 >                                        }
1677 >                                        rxm = 0;
1678 >                                        yo = y1;
1679 >                                }      
1680 >                                if (xil) {
1681 >                                        i = (yi * bytes_per_row) + xi;
1682 >                                        for (y2=0; y2 < yil; y2++, i += bytes_per_row)
1683 >                                                memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
1684 >                                        if (depth == 1) {
1685 >                                                if (have_shm)
1686 >                                                        XShmPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
1687 >                                                else
1688 >                                                        XPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil);
1689 >                                        } else {
1690 >                                                if (have_shm)
1691 >                                                        XShmPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0);
1692 >                                                else
1693 >                                                        XPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil);
1694 >                                        }
1695 >                                        xil = 0;
1696 >                                }
1697 >                                if ((x1 == 15) && (y1 == 15) && (rxmo)) {
1698 >                                        x1--;
1699 >                                        xi = xmo * rx;
1700 >                                        yi = ymo * ry;
1701 >                                        xil = rxmo;
1702 >                                        yil = (yo - ymo +1) * ry;
1703 >                                        rxmo = 0;
1704 >                                }
1705 >                        }
1706 >                }
1707 >                nr_boxes = 0;
1708 >        }
1709 > }
1710 >
1711 > // Static display update (fixed frame rate, but incremental)
1712 > static void update_display_static(void)
1713   {
1219        // In classic mode, copy the frame buffer from Mac RAM
1220        if (classic_mode)
1221                memcpy(the_buffer, Mac2HostAddr(0x3fa700), VideoMonitor.bytes_per_row * VideoMonitor.y);
1222        
1714          // Incremental update code
1715          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1716          int bytes_per_row = VideoMonitor.bytes_per_row;
# Line 1255 | Line 1746 | static void update_display(void)
1746                                                  x1 = i << 3;
1747                                                  break;
1748                                          }
1749 <                                        p++;
1259 <                                        p2++;
1749 >                                        p++; p2++;
1750                                  }
1751                          }
1752                          x2 = x1;
# Line 1266 | Line 1756 | static void update_display(void)
1756                                  p += bytes_per_row;
1757                                  p2 += bytes_per_row;
1758                                  for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1759 <                                        p--;
1270 <                                        p2--;
1759 >                                        p--; p2--;
1760                                          if (*p != *p2) {
1761                                                  x2 = i << 3;
1762                                                  break;
# Line 1280 | Line 1769 | static void update_display(void)
1769                          if (high && wide) {
1770                                  for (j=y1; j<=y2; j++) {
1771                                          i = j * bytes_per_row + (x1 >> 3);
1772 <                                        memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
1772 >                                        memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3);
1773                                  }
1774                          }
1775  
# Line 1289 | Line 1778 | static void update_display(void)
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; i++) {
1782 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1783 <                                                x1 = i;
1781 >                                for (i=0; i<x1*bytes_per_pixel; i++) {
1782 >                                        if (*p != *p2) {
1783 >                                                x1 = i / bytes_per_pixel;
1784                                                  break;
1785                                          }
1786 <                                        p += bytes_per_pixel;
1298 <                                        p2 += bytes_per_pixel;
1786 >                                        p++; p2++;
1787                                  }
1788                          }
1789                          x2 = x1;
# Line 1304 | Line 1792 | static void update_display(void)
1792                                  p2 = &the_buffer_copy[j * bytes_per_row];
1793                                  p += bytes_per_row;
1794                                  p2 += bytes_per_row;
1795 <                                for (i=VideoMonitor.x; i>x2; i--) {
1796 <                                        p -= bytes_per_pixel;
1797 <                                        p2 -= bytes_per_pixel;
1798 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1799 <                                                x2 = i;
1795 >                                for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1796 >                                        p--;
1797 >                                        p2--;
1798 >                                        if (*p != *p2) {
1799 >                                                x2 = i / bytes_per_pixel;
1800                                                  break;
1801                                          }
1802                                  }
# Line 1319 | Line 1807 | static void update_display(void)
1807                          if (high && wide) {
1808                                  for (j=y1; j<=y2; j++) {
1809                                          i = j * bytes_per_row + x1 * bytes_per_pixel;
1810 <                                        memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
1810 >                                        memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1811                                  }
1812                          }
1813                  }
# Line 1332 | Line 1820 | static void update_display(void)
1820                  else
1821                          XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1822          }
1823 + }
1824 +
1825 +
1826 + /*
1827 + *      Screen refresh functions
1828 + */
1829  
1830 <        // Has the Mac started? (cursor data is not valid otherwise)
1831 <        if (HasMacStarted()) {
1830 > // The specialisations hereunder are meant to enable VOSF with DGA in direct
1831 > // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1832 > // not get mapped correctly with respect to the predetermined host frame
1833 > // buffer base address.
1834 > //
1835 > // Hmm, in other words, when in direct addressing mode and DGA is requested,
1836 > // we first try to "triple allocate" the address spaces according to the real
1837 > // host frame buffer address. Then, if it fails, we will use a temporary
1838 > // frame buffer thus making the real host frame buffer updated when pages
1839 > // of the temp frame buffer are altered.
1840 > //
1841 > // As a side effect, a little speed gain in screen updates could be noticed
1842 > // for other modes than DGA.
1843 > //
1844 > // The following two functions below are inline so that a clever compiler
1845 > // could specialise the code according to the current screen depth and
1846 > // display type. A more clever compiler would the job by itself though...
1847 > // (update_display_vosf is inlined as well)
1848  
1849 <                // Set new cursor image if it was changed
1850 <                if (memcmp(the_cursor, Mac2HostAddr(0x844), 64)) {
1851 <                        memcpy(the_cursor, Mac2HostAddr(0x844), 64);
1852 <                        memcpy(cursor_image->data, the_cursor, 32);
1853 <                        memcpy(cursor_mask_image->data, the_cursor+32, 32);
1854 <                        XFreeCursor(x_display, mac_cursor);
1855 <                        XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
1856 <                        XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
1857 <                        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, ReadMacInt8(0x885), ReadMacInt8(0x887));
1858 <                        XDefineCursor(x_display, the_win, mac_cursor);
1849 > static inline void possibly_quit_dga_mode()
1850 > {
1851 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1852 >        // Quit DGA mode if requested
1853 >        if (quit_full_screen) {
1854 >                quit_full_screen = false;
1855 > #ifdef ENABLE_XF86_DGA
1856 >                XF86DGADirectVideo(x_display, screen, 0);
1857 > #endif
1858 >                XUngrabPointer(x_display, CurrentTime);
1859 >                XUngrabKeyboard(x_display, CurrentTime);
1860 >                XUnmapWindow(x_display, the_win);
1861 >                XSync(x_display, false);
1862 >        }
1863 > #endif
1864 > }
1865 >
1866 > static inline void handle_palette_changes(int depth, int display_type)
1867 > {
1868 > #ifdef HAVE_PTHREADS
1869 >        pthread_mutex_lock(&palette_lock);
1870 > #endif
1871 >        if (palette_changed) {
1872 >                palette_changed = false;
1873 >                if (depth == 8) {
1874 >                        XStoreColors(x_display, cmap[0], palette, 256);
1875 >                        XStoreColors(x_display, cmap[1], palette, 256);
1876 >                                
1877 > #ifdef ENABLE_XF86_DGA
1878 >                        if (display_type == DISPLAY_DGA) {
1879 >                                current_dga_cmap ^= 1;
1880 >                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1881 >                        }
1882 > #endif
1883                  }
1884          }
1885 + #ifdef HAVE_PTHREADS
1886 +        pthread_mutex_unlock(&palette_lock);
1887 + #endif
1888 + }
1889 +
1890 + static void video_refresh_dga(void)
1891 + {
1892 +        // Quit DGA mode if requested
1893 +        possibly_quit_dga_mode();
1894 +        
1895 +        // Handle X events
1896 +        handle_events();
1897 +        
1898 +        // Handle palette changes
1899 +        handle_palette_changes(depth, DISPLAY_DGA);
1900 + }
1901 +
1902 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
1903 + static void video_refresh_dga_vosf(void)
1904 + {
1905 +        // Quit DGA mode if requested
1906 +        possibly_quit_dga_mode();
1907 +        
1908 +        // Handle X events
1909 +        handle_events();
1910 +        
1911 +        // Handle palette changes
1912 +        handle_palette_changes(depth, DISPLAY_DGA);
1913 +        
1914 +        // Update display (VOSF variant)
1915 +        static int tick_counter = 0;
1916 +        if (++tick_counter >= frame_skip) {
1917 +                tick_counter = 0;
1918 + #ifdef HAVE_PTHREADS
1919 +                pthread_mutex_lock(&Screen_draw_lock);
1920 + #endif
1921 +                update_display_dga_vosf();
1922 + #ifdef HAVE_PTHREADS
1923 +                pthread_mutex_unlock(&Screen_draw_lock);
1924 + #endif
1925 +        }
1926 + }
1927 + #endif
1928 +
1929 + #ifdef ENABLE_VOSF
1930 + static void video_refresh_window_vosf(void)
1931 + {
1932 +        // Quit DGA mode if requested
1933 +        possibly_quit_dga_mode();
1934 +        
1935 +        // Handle X events
1936 +        handle_events();
1937 +        
1938 +        // Handle palette changes
1939 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1940 +        
1941 +        // Update display (VOSF variant)
1942 +        static int tick_counter = 0;
1943 +        if (++tick_counter >= frame_skip) {
1944 +                tick_counter = 0;
1945 + #ifdef HAVE_PTHREADS
1946 +                pthread_mutex_lock(&Screen_draw_lock);
1947 + #endif
1948 +                update_display_window_vosf();
1949 + #ifdef HAVE_PTHREADS
1950 +                pthread_mutex_unlock(&Screen_draw_lock);
1951 + #endif
1952 +        }
1953 + }
1954 + #endif
1955 +
1956 + static void video_refresh_window_static(void)
1957 + {
1958 +        // Handle X events
1959 +        handle_events();
1960 +        
1961 +        // Handle_palette changes
1962 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1963 +        
1964 +        // Update display (static variant)
1965 +        static int tick_counter = 0;
1966 +        if (++tick_counter >= frame_skip) {
1967 +                tick_counter = 0;
1968 +                update_display_static();
1969 +        }
1970 + }
1971 +
1972 + static void video_refresh_window_dynamic(void)
1973 + {
1974 +        // Handle X events
1975 +        handle_events();
1976 +        
1977 +        // Handle_palette changes
1978 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1979 +        
1980 +        // Update display (dynamic variant)
1981 +        static int tick_counter = 0;
1982 +        tick_counter++;
1983 +        update_display_dynamic(tick_counter);
1984   }
1985  
1986  
# Line 1355 | Line 1988 | static void update_display(void)
1988   *  Thread for screen refresh, input handling etc.
1989   */
1990  
1991 < static void *redraw_func(void *arg)
1991 > void VideoRefreshInit(void)
1992   {
1993 <        int tick_counter = 0;
1993 >        // TODO: set up specialised 8bpp VideoRefresh handlers ?
1994 >        if (display_type == DISPLAY_DGA) {
1995 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
1996 >                if (use_vosf)
1997 >                        video_refresh = video_refresh_dga_vosf;
1998 >                else
1999 > #endif
2000 >                        video_refresh = video_refresh_dga;
2001 >        }
2002 >        else {
2003 > #ifdef ENABLE_VOSF
2004 >                if (use_vosf)
2005 >                        video_refresh = video_refresh_window_vosf;
2006 >                else
2007 > #endif
2008 >                if (frame_skip == 0)
2009 >                        video_refresh = video_refresh_window_dynamic;
2010 >                else
2011 >                        video_refresh = video_refresh_window_static;
2012 >        }
2013 > }
2014  
2015 <        while (!redraw_thread_cancel) {
2015 > void VideoRefresh(void)
2016 > {
2017 >        // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2018 >        video_refresh();
2019 > }
2020  
2021 <                // Wait
2022 < #ifdef HAVE_NANOSLEEP
2023 <                struct timespec req = {0, 16666667};
2024 <                nanosleep(&req, NULL);
2025 < #else
2026 <                usleep(16667);
2021 > #if 0
2022 > void VideoRefresh(void)
2023 > {
2024 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2025 >        // Quit DGA mode if requested
2026 >        if (quit_full_screen) {
2027 >                quit_full_screen = false;
2028 >                if (display_type == DISPLAY_DGA) {
2029 > #ifdef ENABLE_XF86_DGA
2030 >                        XF86DGADirectVideo(x_display, screen, 0);
2031   #endif
2032 <
2033 < #if ENABLE_XF86_DGA
2034 <                // Quit DGA mode if requested
2035 <                if (quit_full_screen) {
1375 <                        quit_full_screen = false;
1376 <                        if (display_type == DISPLAY_DGA) {
1377 <                                XF86DGADirectVideo(x_display, screen, 0);
1378 <                                XUngrabPointer(x_display, CurrentTime);
1379 <                                XUngrabKeyboard(x_display, CurrentTime);
1380 <                                XUnmapWindow(x_display, the_win);
1381 <                                XSync(x_display, false);
1382 <                        }
2032 >                        XUngrabPointer(x_display, CurrentTime);
2033 >                        XUngrabKeyboard(x_display, CurrentTime);
2034 >                        XUnmapWindow(x_display, the_win);
2035 >                        XSync(x_display, false);
2036                  }
2037 +        }
2038   #endif
2039  
2040 < #if ENABLE_FBDEV_DGA
2041 <                // Quit DGA mode if requested
2042 <                if (quit_full_screen) {
2043 <                        quit_full_screen = false;
2040 >        // Handle X events
2041 >        handle_events();
2042 >
2043 >        // Handle palette changes
2044 > #ifdef HAVE_PTHREADS
2045 >        pthread_mutex_lock(&palette_lock);
2046 > #endif
2047 >        if (palette_changed) {
2048 >                palette_changed = false;
2049 >                if (depth == 8) {
2050 >                        XStoreColors(x_display, cmap[0], palette, 256);
2051 >                        XStoreColors(x_display, cmap[1], palette, 256);
2052 >                                
2053 > #ifdef ENABLE_XF86_DGA
2054                          if (display_type == DISPLAY_DGA) {
2055 <                                XUngrabPointer(x_display, CurrentTime);
2056 <                                XUngrabKeyboard(x_display, CurrentTime);
1393 <                                XUnmapWindow(x_display, the_win);
1394 <                                XSync(x_display, false);
2055 >                                current_dga_cmap ^= 1;
2056 >                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2057                          }
2058 + #endif
2059                  }
2060 +        }
2061 + #ifdef HAVE_PTHREADS
2062 +        pthread_mutex_unlock(&palette_lock);
2063   #endif
1398                // Handle X events
1399                handle_events();
2064  
2065 <                // Handle palette changes
2066 <                pthread_mutex_lock(&palette_lock);
2067 <                if (palette_changed) {
2068 <                        palette_changed = false;
2069 <                        if (depth == 8) {
2070 <                                XStoreColors(x_display, cmap[0], palette, 256);
2071 <                                XStoreColors(x_display, cmap[1], palette, 256);
2072 <                                
2073 < #if ENABLE_XF86_DGA
1410 <                                if (display_type == DISPLAY_DGA) {
1411 <                                        current_dga_cmap ^= 1;
1412 <                                        XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1413 <                                }
1414 < #endif
1415 <                        }
2065 >        // In window mode, update display
2066 >        static int tick_counter = 0;
2067 >        if (display_type == DISPLAY_WINDOW) {
2068 >                tick_counter++;
2069 >                if (frame_skip == 0)
2070 >                        update_display_dynamic(tick_counter);
2071 >                else if (tick_counter >= frame_skip) {
2072 >                        tick_counter = 0;
2073 >                        update_display_static();
2074                  }
2075 <                pthread_mutex_unlock(&palette_lock);
2075 >        }
2076 > }
2077 > #endif
2078  
2079 <                // In window mode, update display and mouse pointer
2080 <                if (display_type == DISPLAY_WINDOW) {
2081 <                        tick_counter++;
2082 <                        if (tick_counter >= frame_skip) {
2083 <                                tick_counter = 0;
2084 <                                update_display();
2085 <                        }
2086 <                }
2079 > #ifdef HAVE_PTHREADS
2080 > static void *redraw_func(void *arg)
2081 > {
2082 >        uint64 start = GetTicks_usec();
2083 >        int64 ticks = 0;
2084 >        uint64 next = GetTicks_usec();
2085 >        while (!redraw_thread_cancel) {
2086 > //              VideoRefresh();
2087 >                video_refresh();
2088 >                next += 16667;
2089 >                int64 delay = next - GetTicks_usec();
2090 >                if (delay > 0)
2091 >                        Delay_usec(delay);
2092 >                else if (delay < -16667)
2093 >                        next = GetTicks_usec();
2094 >                ticks++;
2095          }
2096 +        uint64 end = GetTicks_usec();
2097 +        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2098          return NULL;
2099   }
2100 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines