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.2 by cebix, 1999-10-03T16:20:08Z vs.
Revision 1.28 by cebix, 2000-11-03T12:23:49Z

# 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_DGA
52 #include <X11/extensions/xf86dga.h>
53 #endif
54
73  
74   // Display types
75   enum {
# Line 59 | Line 77 | enum {
77          DISPLAY_DGA             // DGA fullscreen display
78   };
79  
62
80   // Constants
81 < const char KEYCODE_FILE_NAME[] = SHAREDIR "keycodes";
81 > const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
82 > const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices";
83  
84  
85   // Global variables
86 < static int32 frame_skip;
86 > static int32 frame_skip;                                                        // Prefs items
87 > static int16 mouse_wheel_mode = 1;
88 > static int16 mouse_wheel_lines = 3;
89 >
90   static int display_type = DISPLAY_WINDOW;                       // See enum above
91 + static bool local_X11;                                                          // Flag: X server running on local machine?
92   static uint8 *the_buffer;                                                       // Mac frame buffer
93 +
94 + #ifdef HAVE_PTHREADS
95   static bool redraw_thread_active = false;                       // Flag: Redraw thread installed
96   static volatile bool redraw_thread_cancel = false;      // Flag: Cancel Redraw thread
97   static pthread_t redraw_thread;                                         // Redraw thread
98 + #endif
99  
100   static bool has_dga = false;                                            // Flag: Video DGA capable
101 + static bool has_vidmode = false;                                        // Flag: VidMode extension available
102  
103   static bool ctrl_down = false;                                          // Flag: Ctrl key pressed
104 + static bool caps_on = false;                                            // Flag: Caps Lock on
105   static bool quit_full_screen = false;                           // Flag: DGA close requested from redraw thread
106   static bool emerg_quit = false;                                         // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
107   static bool emul_suspended = false;                                     // Flag: Emulator suspended
# Line 95 | Line 122 | static Colormap cmap[2];                                                       // Two co
122   static XColor black, white;
123   static unsigned long black_pixel, white_pixel;
124   static int eventmask;
125 < static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask;
125 > static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | FocusChangeMask | ExposureMask;
126   static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
127  
101 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;
109 static XImage *cursor_image, *cursor_mask_image;
110 static Pixmap cursor_map, cursor_mask_map;
138   static Cursor mac_cursor;
112 static GC cursor_gc, cursor_mask_gc;
139   static uint8 *the_buffer_copy = NULL;                           // Copy of Mac frame buffer
114 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 DGA mode
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 <
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 + #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 128 | 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 141 | 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 <        int layout;
262 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
263 >        int layout = FLAYOUT_DIRECT;
264          switch (depth) {
265                  case 1:
266                          layout = FLAYOUT_DIRECT;
148                        VideoMonitor.mode = VMODE_1BIT;
267                          break;
268                  case 8:
269                          layout = FLAYOUT_DIRECT;
152                        VideoMonitor.mode = VMODE_8BIT;
270                          break;
271                  case 15:
272                          layout = FLAYOUT_HOST_555;
156                        VideoMonitor.mode = VMODE_16BIT;
273                          break;
274                  case 16:
275                          layout = FLAYOUT_HOST_565;
160                        VideoMonitor.mode = VMODE_16BIT;
276                          break;
277                  case 24:
278                  case 32:
279                          layout = FLAYOUT_HOST_888;
165                        VideoMonitor.mode = VMODE_32BIT;
280                          break;
281          }
168        VideoMonitor.x = width;
169        VideoMonitor.y = height;
170        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 190 | 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");
198        if (frame_skip == 0)
199                frame_skip = 1;
334  
335          // Create window
336          XSetWindowAttributes wattr;
337          wattr.event_mask = eventmask = win_eventmask;
338          wattr.background_pixel = black_pixel;
205        wattr.border_pixel = black_pixel;
206        wattr.backing_store = Always;
207        wattr.backing_planes = xdepth;
339  
340          XSync(x_display, false);
341          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
342 <                InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
212 <                CWBackingStore | CWBackingPlanes, &wattr);
213 <        XSync(x_display, false);
214 <        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
215 <        XMapRaised(x_display, the_win);
216 <        XSync(x_display, false);
342 >                InputOutput, vis, CWEventMask | CWBackPixel, &wattr);
343  
344 <        // Set colormap
345 <        if (depth == 8) {
346 <                XSetWindowColormap(x_display, the_win, cmap[0]);
347 <                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
344 >        // Indicate that we want keyboard input
345 >        {
346 >                XWMHints *hints = XAllocWMHints();
347 >                if (hints) {
348 >                        hints->input = True;
349 >                        hints->flags = InputHint;
350 >                        XSetWMHints(x_display, the_win, hints);
351 >                        XFree((char *)hints);
352 >                }
353          }
354  
355          // Make window unresizable
356 <        XSizeHints *hints;
357 <        if ((hints = XAllocSizeHints()) != NULL) {
358 <                hints->min_width = width;
359 <                hints->max_width = width;
360 <                hints->min_height = height;
361 <                hints->max_height = height;
362 <                hints->flags = PMinSize | PMaxSize;
363 <                XSetWMNormalHints(x_display, the_win, hints);
364 <                XFree((char *)hints);
356 >        {
357 >                XSizeHints *hints = XAllocSizeHints();
358 >                if (hints) {
359 >                        hints->min_width = width;
360 >                        hints->max_width = width;
361 >                        hints->min_height = height;
362 >                        hints->max_height = height;
363 >                        hints->flags = PMinSize | PMaxSize;
364 >                        XSetWMNormalHints(x_display, the_win, hints);
365 >                        XFree((char *)hints);
366 >                }
367          }
368 +        
369 +        // Set window title
370 +        XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
371 +
372 +        // Set window class
373 +        {
374 +                XClassHint *hints;
375 +                hints = XAllocClassHint();
376 +                if (hints) {
377 +                        hints->res_name = "BasiliskII";
378 +                        hints->res_class = "BasiliskII";
379 +                        XSetClassHint(x_display, the_win, hints);
380 +                        XFree((char *)hints);
381 +                }
382 +        }
383 +
384 +        // Show window
385 +        XSync(x_display, false);
386 +        XMapRaised(x_display, the_win);
387 +        XFlush(x_display);
388 +
389 +        // Set colormap
390 +        if (depth == 8)
391 +                XSetWindowColormap(x_display, the_win, cmap[0]);
392  
393          // Try to create and attach SHM image
394          have_shm = false;
395 <        if (depth != 1 && XShmQueryExtension(x_display)) {
395 >        if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
396  
397                  // Create SHM image ("height + 2" for safety)
398                  img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
399 <                shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
400 <                the_buffer = (uint8 *)shmat(shminfo.shmid, 0, 0);
401 <                shminfo.shmaddr = img->data = (char *)the_buffer;
399 >                shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
400 >                the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
401 >                shminfo.shmaddr = img->data = (char *)the_buffer_copy;
402                  shminfo.readOnly = False;
403  
404                  // Try to attach SHM image, catching errors
# Line 259 | Line 416 | static bool init_window(int width, int h
416                          shmctl(shminfo.shmid, IPC_RMID, 0);
417                  }
418          }
419 <
419 >        
420          // Create normal X image if SHM doesn't work ("height + 2" for safety)
421          if (!have_shm) {
422 <                int bytes_per_row = width;
422 >                int bytes_per_row = aligned_width;
423                  switch (depth) {
424                          case 1:
425                                  bytes_per_row /= 8;
# Line 276 | Line 433 | static bool init_window(int width, int h
433                                  bytes_per_row *= 4;
434                                  break;
435                  }
436 <                the_buffer = (uint8 *)malloc((height + 2) * bytes_per_row);
437 <                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer, width, height, 32, bytes_per_row);
436 >                the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
437 >                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
438          }
439  
440          // 1-Bit mode is big-endian
# Line 286 | Line 443 | static bool init_window(int width, int h
443                  img->bitmap_bit_order = MSBFirst;
444          }
445  
446 <        // Allocate memory for frame buffer copy
447 <        the_buffer_copy = (uint8 *)malloc((height + 2) * img->bytes_per_line);
446 > #ifdef ENABLE_VOSF
447 >        // Allocate a page-aligned chunk of memory for frame buffer
448 >        the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line);
449 >        the_host_buffer = the_buffer_copy;
450 >        
451 >        the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size);
452 >        memset(the_buffer_copy, 0, the_buffer_size);
453 >        
454 >        the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
455 >        memset(the_buffer, 0, the_buffer_size);
456 > #else
457 >        // Allocate memory for frame buffer
458 >        the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
459 > #endif
460  
461          // Create GC
462          the_gc = XCreateGC(x_display, the_win, 0, 0);
463          XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
464  
465 <        // Create cursor
466 <        cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor, 16, 16, 16, 2);
467 <        cursor_image->byte_order = MSBFirst;
468 <        cursor_image->bitmap_bit_order = MSBFirst;
469 <        cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor+32, 16, 16, 16, 2);
470 <        cursor_mask_image->byte_order = MSBFirst;
302 <        cursor_mask_image->bitmap_bit_order = MSBFirst;
303 <        cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
304 <        cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
305 <        cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
306 <        cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
307 <        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
465 >        // Create no_cursor
466 >        mac_cursor = XCreatePixmapCursor(x_display,
467 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
468 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
469 >           &black, &white, 0, 0);
470 >        XDefineCursor(x_display, the_win, mac_cursor);
471  
472          // Set VideoMonitor
473 +        bool native_byte_order;
474   #ifdef WORDS_BIGENDIAN
475 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst);
475 >        native_byte_order = (img->bitmap_bit_order == MSBFirst);
476 > #else
477 >        native_byte_order = (img->bitmap_bit_order == LSBFirst);
478 > #endif
479 > #ifdef ENABLE_VOSF
480 >        do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
481 > #endif
482 >        set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
483 >        
484 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
485 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
486 > #else
487 >        VideoMonitor.mac_frame_base = MacFrameBaseMac;
488 > #endif
489 >        return true;
490 > }
491 >
492 > // Init fbdev DGA display
493 > static bool init_fbdev_dga(char *in_fb_name)
494 > {
495 > #ifdef ENABLE_FBDEV_DGA
496 >        // Find the maximum depth available
497 >        int ndepths, max_depth(0);
498 >        int *depths = XListDepths(x_display, screen, &ndepths);
499 >        if (depths == NULL) {
500 >                printf("FATAL: Could not determine the maximal depth available\n");
501 >                return false;
502 >        } else {
503 >                while (ndepths-- > 0) {
504 >                        if (depths[ndepths] > max_depth)
505 >                                max_depth = depths[ndepths];
506 >                }
507 >        }
508 >        
509 >        // Get fbdevices file path from preferences
510 >        const char *fbd_path = PrefsFindString("fbdevicefile");
511 >        
512 >        // Open fbdevices file
513 >        FILE *fp = fopen(fbd_path ? fbd_path : FBDEVICES_FILE_NAME, "r");
514 >        if (fp == NULL) {
515 >                char str[256];
516 >                sprintf(str, GetString(STR_NO_FBDEVICE_FILE_ERR), fbd_path ? fbd_path : FBDEVICES_FILE_NAME, strerror(errno));
517 >                ErrorAlert(str);
518 >                return false;
519 >        }
520 >        
521 >        int fb_depth;           // supported depth
522 >        uint32 fb_offset;       // offset used for mmap(2)
523 >        char fb_name[20];
524 >        char line[256];
525 >        bool device_found = false;
526 >        while (fgets(line, 255, fp)) {
527 >                // Read line
528 >                int len = strlen(line);
529 >                if (len == 0)
530 >                        continue;
531 >                line[len - 1] = '\0';
532 >                
533 >                // Comments begin with "#" or ";"
534 >                if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
535 >                        continue;
536 >                
537 >                if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
538 >                && (strcmp(fb_name, in_fb_name) == 0) && (fb_depth == max_depth)) {
539 >                        device_found = true;
540 >                        break;
541 >                }
542 >        }
543 >        
544 >        // fbdevices file completely read
545 >        fclose(fp);
546 >        
547 >        // Frame buffer name not found ? Then, display warning
548 >        if (!device_found) {
549 >                char str[256];
550 >                sprintf(str, GetString(STR_FBDEV_NAME_ERR), in_fb_name, max_depth);
551 >                ErrorAlert(str);
552 >                return false;
553 >        }
554 >        
555 >        int width = DisplayWidth(x_display, screen);
556 >        int height = DisplayHeight(x_display, screen);
557 >        depth = fb_depth; // max_depth
558 >        
559 >        // Set relative mouse mode
560 >        ADBSetRelMouseMode(false);
561 >        
562 >        // Create window
563 >        XSetWindowAttributes wattr;
564 >        wattr.event_mask                = eventmask = dga_eventmask;
565 >        wattr.background_pixel  = white_pixel;
566 >        wattr.override_redirect = True;
567 >        
568 >        XSync(x_display, false);
569 >        the_win = XCreateWindow(x_display, rootwin,
570 >                0, 0, width, height,
571 >                0, xdepth, InputOutput, vis,
572 >                CWEventMask | CWBackPixel | CWOverrideRedirect,
573 >                &wattr);
574 >        XSync(x_display, false);
575 >        XMapRaised(x_display, the_win);
576 >        XSync(x_display, false);
577 >        
578 >        // Grab mouse and keyboard
579 >        XGrabKeyboard(x_display, the_win, True,
580 >                GrabModeAsync, GrabModeAsync, CurrentTime);
581 >        XGrabPointer(x_display, the_win, True,
582 >                PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
583 >                GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
584 >        
585 >        // Set colormap
586 >        if (depth == 8)
587 >                XSetWindowColormap(x_display, the_win, cmap[0]);
588 >        
589 >        // Set VideoMonitor
590 >        int bytes_per_row = width;
591 >        switch (depth) {
592 >                case 1:
593 >                        bytes_per_row = ((width | 7) & ~7) >> 3;
594 >                        break;
595 >                case 15:
596 >                case 16:
597 >                        bytes_per_row *= 2;
598 >                        break;
599 >                case 24:
600 >                case 32:
601 >                        bytes_per_row *= 4;
602 >                        break;
603 >        }
604 >        
605 >        if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
606 >                if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
607 >                        char str[256];
608 >                        sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
609 >                        ErrorAlert(str);
610 >                        return false;
611 >                }
612 >        }
613 >        
614 > #if ENABLE_VOSF
615 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
616 >        // If the blit function is null, i.e. just a copy of the buffer,
617 >        // we first try to avoid the allocation of a temporary frame buffer
618 >        use_vosf = true;
619 >        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
620 >        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
621 >                use_vosf = false;
622 >        
623 >        if (use_vosf) {
624 >                the_host_buffer = the_buffer;
625 >                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
626 >                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
627 >                memset(the_buffer_copy, 0, the_buffer_size);
628 >                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
629 >                memset(the_buffer, 0, the_buffer_size);
630 >        }
631   #else
632 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst);
632 >        use_vosf = false;
633   #endif
634 < #if REAL_ADDRESSING
635 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
636 <        MacFrameLayout = FLAYOUT_DIRECT;
634 > #endif
635 >        
636 >        set_video_monitor(width, height, bytes_per_row, true);
637 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
638 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
639   #else
640          VideoMonitor.mac_frame_base = MacFrameBaseMac;
641   #endif
642          return true;
643 + #else
644 +        ErrorAlert("Basilisk II has been compiled with fbdev DGA support disabled.");
645 +        return false;
646 + #endif
647   }
648  
649 < // Init DGA display
650 < static bool init_dga(int width, int height)
649 > // Init XF86 DGA display
650 > static bool init_xf86_dga(int width, int height)
651   {
652 < #if ENABLE_DGA
652 > #ifdef ENABLE_XF86_DGA
653          // Set relative mouse mode
654          ADBSetRelMouseMode(true);
655  
656 + #ifdef ENABLE_XF86_VIDMODE
657 +        // Switch to best mode
658 +        if (has_vidmode) {
659 +                int best = 0;
660 +                for (int i=1; i<num_x_video_modes; i++) {
661 +                        if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
662 +                                x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
663 +                                best = i;
664 +                        }
665 +                }
666 +                XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
667 +                XF86VidModeSetViewPort(x_display, screen, 0, 0);
668 +        }
669 + #endif
670 +
671          // Create window
672          XSetWindowAttributes wattr;
673          wattr.event_mask = eventmask = dga_eventmask;
334        wattr.border_pixel = black_pixel;
674          wattr.override_redirect = True;
675  
676          XSync(x_display, false);
677          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
678 <                InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr);
678 >                InputOutput, vis, CWEventMask | CWOverrideRedirect, &wattr);
679          XSync(x_display, false);
680          XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
681          XMapRaised(x_display, the_win);
# Line 357 | Line 696 | static bool init_dga(int width, int heig
696          // Set colormap
697          if (depth == 8) {
698                  XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
360                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
699                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
700          }
701  
# Line 376 | Line 714 | static bool init_dga(int width, int heig
714                          bytes_per_row *= 4;
715                          break;
716          }
717 +        
718 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
719 +        // If the blit function is null, i.e. just a copy of the buffer,
720 +        // we first try to avoid the allocation of a temporary frame buffer
721 +        use_vosf = true;
722 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
723 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
724 +                use_vosf = false;
725 +        
726 +        if (use_vosf) {
727 +                the_host_buffer = the_buffer;
728 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
729 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
730 +                memset(the_buffer_copy, 0, the_buffer_size);
731 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
732 +                memset(the_buffer, 0, the_buffer_size);
733 +        }
734 + #elif defined(ENABLE_VOSF)
735 +        // The UAE memory handlers will already handle color conversion, if needed.
736 +        use_vosf = false;
737 + #endif
738 +        
739          set_video_monitor(width, height, bytes_per_row, true);
740 < #if REAL_ADDRESSING
741 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
742 <        MacFrameLayout = FLAYOUT_DIRECT;
740 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
741 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
742 > //      MacFrameLayout = FLAYOUT_DIRECT;
743   #else
744          VideoMonitor.mac_frame_base = MacFrameBaseMac;
745   #endif
746          return true;
747   #else
748 <        ErrorAlert("Basilisk II has been compiled with DGA support disabled.");
748 >        ErrorAlert("Basilisk II has been compiled with XF86 DGA support disabled.");
749          return false;
750   #endif
751   }
# Line 455 | Line 815 | static void keycode_init(void)
815          }
816   }
817  
818 + bool VideoInitBuffer()
819 + {
820 + #ifdef ENABLE_VOSF
821 +        if (use_vosf) {
822 +                const uint32 page_size  = getpagesize();
823 +                const uint32 page_mask  = page_size - 1;
824 +                
825 +                mainBuffer.memBase      = (uint32) the_buffer;
826 +                // Align the frame buffer on page boundary
827 +                mainBuffer.memStart             = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
828 +                mainBuffer.memLength    = the_buffer_size;
829 +                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
830 +
831 +                mainBuffer.pageSize     = page_size;
832 +                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
833 +                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
834 +
835 +                if (mainBuffer.dirtyPages != 0)
836 +                        free(mainBuffer.dirtyPages);
837 +
838 +                mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
839 +
840 +                if (mainBuffer.pageInfo != 0)
841 +                        free(mainBuffer.pageInfo);
842 +
843 +                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
844 +
845 +                if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
846 +                        return false;
847 +
848 +                PFLAG_CLEAR_ALL;
849 +
850 +                uint32 a = 0;
851 +                for (int i = 0; i < mainBuffer.pageCount; i++) {
852 +                        int y1 = a / VideoMonitor.bytes_per_row;
853 +                        if (y1 >= VideoMonitor.y)
854 +                                y1 = VideoMonitor.y - 1;
855 +
856 +                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
857 +                        if (y2 >= VideoMonitor.y)
858 +                                y2 = VideoMonitor.y - 1;
859 +
860 +                        mainBuffer.pageInfo[i].top = y1;
861 +                        mainBuffer.pageInfo[i].bottom = y2;
862 +
863 +                        a += mainBuffer.pageSize;
864 +                        if (a > mainBuffer.memLength)
865 +                                a = mainBuffer.memLength;
866 +                }
867 +                
868 +                // We can now write-protect the frame buffer
869 +                if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0)
870 +                        return false;
871 +        }
872 + #endif
873 +        return true;
874 + }
875 +
876   bool VideoInit(bool classic)
877   {
878 + #ifdef ENABLE_VOSF
879 +        // Open /dev/zero
880 +        zero_fd = open("/dev/zero", O_RDWR);
881 +        if (zero_fd < 0) {
882 +        char str[256];
883 +                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
884 +                ErrorAlert(str);
885 +        return false;
886 +        }
887 +        
888 +        // Zero the mainBuffer structure
889 +        mainBuffer.dirtyPages = 0;
890 +        mainBuffer.pageInfo = 0;
891 + #endif
892 +        
893 +        // Check if X server runs on local machine
894 +        local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
895 +                 || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
896 +    
897          // Init keycode translation
898          keycode_init();
899  
900 +        // Read prefs
901 +        mouse_wheel_mode = PrefsFindInt16("mousewheelmode");
902 +        mouse_wheel_lines = PrefsFindInt16("mousewheellines");
903 +
904          // Find screen and root window
905          screen = XDefaultScreen(x_display);
906          rootwin = XRootWindow(x_display, screen);
907 <
907 >        
908          // Get screen depth
909          xdepth = DefaultDepth(x_display, screen);
910 +        
911 + #ifdef ENABLE_FBDEV_DGA
912 +        // Frame buffer name
913 +        char fb_name[20];
914 +        
915 +        // Could do fbdev dga ?
916 +        if ((fbdev_fd = open(FBDEVICE_FILE_NAME, O_RDWR)) != -1)
917 +                has_dga = true;
918 +        else
919 +                has_dga = false;
920 + #endif
921  
922 < #if ENABLE_DGA
922 > #ifdef ENABLE_XF86_DGA
923          // DGA available?
924 <        int dga_flags = 0;
925 <        XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
926 <        has_dga = dga_flags & XF86DGADirectPresent;
924 >        int dga_event_base, dga_error_base;
925 >        if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
926 >                int dga_flags = 0;
927 >                XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
928 >                has_dga = dga_flags & XF86DGADirectPresent;
929 >        } else
930 >                has_dga = false;
931   #endif
932  
933 + #ifdef ENABLE_XF86_VIDMODE
934 +        // VidMode available?
935 +        int vm_event_base, vm_error_base;
936 +        has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
937 +        if (has_vidmode)
938 +                XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
939 + #endif
940 +        
941          // Find black and white colors
942          XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
943          XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
# Line 539 | Line 1003 | bool VideoInit(bool classic)
1003          if (mode_str) {
1004                  if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
1005                          display_type = DISPLAY_WINDOW;
1006 <                else if (has_dga && strcmp(mode_str, "dga") == 0) {
1006 > #ifdef ENABLE_FBDEV_DGA
1007 >                else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
1008 > #else
1009 >                else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
1010 > #endif
1011                          display_type = DISPLAY_DGA;
1012 +                        if (width > DisplayWidth(x_display, screen))
1013 +                                width = DisplayWidth(x_display, screen);
1014 +                        if (height > DisplayHeight(x_display, screen))
1015 +                                height = DisplayHeight(x_display, screen);
1016 +                }
1017 +                if (width <= 0)
1018                          width = DisplayWidth(x_display, screen);
1019 +                if (height <= 0)
1020                          height = DisplayHeight(x_display, screen);
546                }
1021          }
1022  
1023          // Initialize according to display type
# Line 553 | Line 1027 | bool VideoInit(bool classic)
1027                                  return false;
1028                          break;
1029                  case DISPLAY_DGA:
1030 <                        if (!init_dga(width, height))
1030 > #ifdef ENABLE_FBDEV_DGA
1031 >                        if (!init_fbdev_dga(fb_name))
1032 > #else
1033 >                        if (!init_xf86_dga(width, height))
1034 > #endif
1035                                  return false;
1036                          break;
1037          }
1038  
1039 + #ifdef HAVE_PTHREADS
1040          // Lock down frame buffer
1041          pthread_mutex_lock(&frame_buffer_lock);
1042 + #endif
1043  
1044 < #if !REAL_ADDRESSING
1044 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1045          // Set variables for UAE memory mapping
1046          MacFrameBaseHost = the_buffer;
1047          MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
# Line 571 | Line 1051 | bool VideoInit(bool classic)
1051                  MacFrameLayout = FLAYOUT_NONE;
1052   #endif
1053  
1054 <        // Start redraw/input thread
1054 > #ifdef ENABLE_VOSF
1055 >        if (use_vosf) {
1056 >                // Initialize the mainBuffer structure
1057 >                if (!VideoInitBuffer()) {
1058 >                        // TODO: STR_VOSF_INIT_ERR ?
1059 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1060 >                return false;
1061 >                }
1062 >
1063 >                // Initialize the handler for SIGSEGV
1064 >                if (!Screen_fault_handler_init()) {
1065 >                        // TODO: STR_VOSF_INIT_ERR ?
1066 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1067 >                        return false;
1068 >                }
1069 >        }
1070 > #endif
1071 >        
1072 >        // Initialize VideoRefresh function
1073 >        VideoRefreshInit();
1074 >        
1075          XSync(x_display, false);
1076 +
1077 + #ifdef HAVE_PTHREADS
1078 +        // Start redraw/input thread
1079          redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1080 <        if (!redraw_thread_active)
1080 >        if (!redraw_thread_active) {
1081                  printf("FATAL: cannot create redraw thread\n");
1082 <        return redraw_thread_active;
1082 >                return false;
1083 >        }
1084 > #endif
1085 >        return true;
1086   }
1087  
1088  
# Line 586 | Line 1092 | bool VideoInit(bool classic)
1092  
1093   void VideoExit(void)
1094   {
1095 + #ifdef HAVE_PTHREADS
1096          // Stop redraw thread
1097          if (redraw_thread_active) {
1098                  redraw_thread_cancel = true;
# Line 595 | Line 1102 | void VideoExit(void)
1102                  pthread_join(redraw_thread, NULL);
1103                  redraw_thread_active = false;
1104          }
1105 + #endif
1106  
1107 + #ifdef HAVE_PTHREADS
1108          // Unlock frame buffer
1109          pthread_mutex_unlock(&frame_buffer_lock);
1110 + #endif
1111  
1112          // Close window and server connection
1113          if (x_display != NULL) {
1114                  XSync(x_display, false);
1115  
1116 < #if ENABLE_DGA
1116 > #ifdef ENABLE_XF86_DGA
1117                  if (display_type == DISPLAY_DGA) {
1118                          XF86DGADirectVideo(x_display, screen, 0);
1119                          XUngrabPointer(x_display, CurrentTime);
# Line 611 | Line 1121 | void VideoExit(void)
1121                  }
1122   #endif
1123  
1124 <                if (the_buffer_copy) {
1125 <                        free(the_buffer_copy);
1126 <                        the_buffer_copy = NULL;
1124 > #ifdef ENABLE_XF86_VIDMODE
1125 >                if (has_vidmode && display_type == DISPLAY_DGA)
1126 >                        XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1127 > #endif
1128 >
1129 > #ifdef ENABLE_FBDEV_DGA
1130 >                if (display_type == DISPLAY_DGA) {
1131 >                        XUngrabPointer(x_display, CurrentTime);
1132 >                        XUngrabKeyboard(x_display, CurrentTime);
1133 >                        close(fbdev_fd);
1134                  }
1135 + #endif
1136  
1137                  XFlush(x_display);
1138                  XSync(x_display, false);
# Line 622 | Line 1140 | void VideoExit(void)
1140                          XFreeColormap(x_display, cmap[0]);
1141                          XFreeColormap(x_display, cmap[1]);
1142                  }
1143 +                
1144 +                if (!use_vosf) {
1145 +                        if (the_buffer) {
1146 +                                free(the_buffer);
1147 +                                the_buffer = NULL;
1148 +                        }
1149 +
1150 +                        if (!have_shm && the_buffer_copy) {
1151 +                                free(the_buffer_copy);
1152 +                                the_buffer_copy = NULL;
1153 +                        }
1154 +                }
1155 + #ifdef ENABLE_VOSF
1156 +                else {
1157 +                        if (the_buffer != (uint8 *)MAP_FAILED) {
1158 +                                munmap((caddr_t)the_buffer, the_buffer_size);
1159 +                                the_buffer = 0;
1160 +                        }
1161 +                        
1162 +                        if (the_buffer_copy != (uint8 *)MAP_FAILED) {
1163 +                                munmap((caddr_t)the_buffer_copy, the_buffer_size);
1164 +                                the_buffer_copy = 0;
1165 +                        }
1166 +                }
1167 + #endif
1168          }
1169 +        
1170 + #ifdef ENABLE_VOSF
1171 +        if (use_vosf) {
1172 +                // Clear mainBuffer data
1173 +                if (mainBuffer.pageInfo) {
1174 +                        free(mainBuffer.pageInfo);
1175 +                        mainBuffer.pageInfo = 0;
1176 +                }
1177 +
1178 +                if (mainBuffer.dirtyPages) {
1179 +                        free(mainBuffer.dirtyPages);
1180 +                        mainBuffer.dirtyPages = 0;
1181 +                }
1182 +        }
1183 +
1184 +        // Close /dev/zero
1185 +        if (zero_fd > 0)
1186 +                close(zero_fd);
1187 + #endif
1188   }
1189  
1190  
# Line 648 | Line 1210 | void VideoInterrupt(void)
1210          if (emerg_quit)
1211                  QuitEmulator();
1212  
1213 + #ifdef HAVE_PTHREADS
1214          // Temporarily give up frame buffer lock (this is the point where
1215          // we are suspended when the user presses Ctrl-Tab)
1216          pthread_mutex_unlock(&frame_buffer_lock);
1217          pthread_mutex_lock(&frame_buffer_lock);
1218 + #endif
1219   }
1220  
1221  
# Line 661 | Line 1225 | void VideoInterrupt(void)
1225  
1226   void video_set_palette(uint8 *pal)
1227   {
1228 + #ifdef HAVE_PTHREADS
1229          pthread_mutex_lock(&palette_lock);
1230 + #endif
1231  
1232          // Convert colors to XColor array
1233          for (int i=0; i<256; i++) {
# Line 675 | Line 1241 | void video_set_palette(uint8 *pal)
1241          // Tell redraw thread to change palette
1242          palette_changed = true;
1243  
1244 + #ifdef HAVE_PTHREADS
1245          pthread_mutex_unlock(&palette_lock);
1246 + #endif
1247   }
1248  
1249  
# Line 683 | Line 1251 | void video_set_palette(uint8 *pal)
1251   *  Suspend/resume emulator
1252   */
1253  
1254 < #if ENABLE_DGA
1254 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1255   static void suspend_emul(void)
1256   {
1257          if (display_type == DISPLAY_DGA) {
# Line 691 | Line 1259 | static void suspend_emul(void)
1259                  ADBKeyUp(0x36);
1260                  ctrl_down = false;
1261  
1262 + #ifdef HAVE_PTHREADS
1263                  // Lock frame buffer (this will stop the MacOS thread)
1264                  pthread_mutex_lock(&frame_buffer_lock);
1265 + #endif
1266  
1267                  // Save frame buffer
1268                  fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
# Line 700 | Line 1270 | static void suspend_emul(void)
1270                          memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
1271  
1272                  // Close full screen display
1273 + #ifdef ENABLE_XF86_DGA
1274                  XF86DGADirectVideo(x_display, screen, 0);
1275 + #endif
1276                  XUngrabPointer(x_display, CurrentTime);
1277                  XUngrabKeyboard(x_display, CurrentTime);
1278                  XUnmapWindow(x_display, the_win);
# Line 710 | Line 1282 | static void suspend_emul(void)
1282                  XSetWindowAttributes wattr;
1283                  wattr.event_mask = KeyPressMask;
1284                  wattr.background_pixel = black_pixel;
1285 <                wattr.border_pixel = black_pixel;
714 <                wattr.backing_store = Always;
1285 >                wattr.backing_store = WhenMapped;
1286                  wattr.backing_planes = xdepth;
1287                  wattr.colormap = DefaultColormap(x_display, screen);
1288 +                
1289                  XSync(x_display, false);
1290                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1291 <                        InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1292 <                        CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1291 >                        InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore |
1292 >                        CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1293                  XSync(x_display, false);
1294                  XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1295                  XMapRaised(x_display, suspend_win);
# Line 738 | Line 1310 | static void resume_emul(void)
1310          XSync(x_display, false);
1311          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1312          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1313 + #ifdef ENABLE_XF86_DGA
1314          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1315          XF86DGASetViewPort(x_display, screen, 0, 0);
1316 + #endif
1317          XSync(x_display, false);
1318 <
1318 >        
1319 >        // the_buffer already contains the data to restore. i.e. since a temporary
1320 >        // frame buffer is used when VOSF is actually used, fb_save is therefore
1321 >        // not necessary.
1322 > #ifdef ENABLE_VOSF
1323 >        if (use_vosf) {
1324 > #ifdef HAVE_PTHREADS
1325 >                pthread_mutex_lock(&Screen_draw_lock);
1326 > #endif
1327 >                PFLAG_SET_ALL;
1328 > #ifdef HAVE_PTHREADS
1329 >                pthread_mutex_unlock(&Screen_draw_lock);
1330 > #endif
1331 >                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1332 >        }
1333 > #endif
1334 >        
1335          // Restore frame buffer
1336          if (fb_save) {
1337 + #ifdef ENABLE_VOSF
1338 +                // Don't copy fb_save to the temporary frame buffer in VOSF mode
1339 +                if (!use_vosf)
1340 + #endif
1341                  memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1342                  free(fb_save);
1343                  fb_save = NULL;
1344          }
1345 +        
1346 + #ifdef ENABLE_XF86_DGA
1347          if (depth == 8)
1348                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1349 + #endif
1350  
1351 + #ifdef HAVE_PTHREADS
1352          // Unlock frame buffer (and continue MacOS thread)
1353          pthread_mutex_unlock(&frame_buffer_lock);
1354          emul_suspended = false;
1355 + #endif
1356   }
1357   #endif
1358  
# Line 815 | Line 1414 | static int kc_decode(KeySym ks)
1414                  case XK_period: case XK_greater: return 0x2f;
1415                  case XK_slash: case XK_question: return 0x2c;
1416  
1417 < #if ENABLE_DGA
1417 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1418                  case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1419   #else
1420                  case XK_Tab: return 0x30;
# Line 941 | Line 1540 | static void handle_events(void)
1540                                  unsigned int button = ((XButtonEvent *)&event)->button;
1541                                  if (button < 4)
1542                                          ADBMouseDown(button - 1);
1543 +                                else if (button < 6) {  // Wheel mouse
1544 +                                        if (mouse_wheel_mode == 0) {
1545 +                                                int key = (button == 5) ? 0x79 : 0x74;  // Page up/down
1546 +                                                ADBKeyDown(key);
1547 +                                                ADBKeyUp(key);
1548 +                                        } else {
1549 +                                                int key = (button == 5) ? 0x3d : 0x3e;  // Cursor up/down
1550 +                                                for(int i=0; i<mouse_wheel_lines; i++) {
1551 +                                                        ADBKeyDown(key);
1552 +                                                        ADBKeyUp(key);
1553 +                                                }
1554 +                                        }
1555 +                                }
1556                                  break;
1557                          }
1558                          case ButtonRelease: {
# Line 968 | Line 1580 | static void handle_events(void)
1580                                          code = event2keycode((XKeyEvent *)&event);
1581                                  if (code != -1) {
1582                                          if (!emul_suspended) {
1583 <                                                ADBKeyDown(code);
1583 >                                                if (code == 0x39) {     // Caps Lock pressed
1584 >                                                        if (caps_on) {
1585 >                                                                ADBKeyUp(code);
1586 >                                                                caps_on = false;
1587 >                                                        } else {
1588 >                                                                ADBKeyDown(code);
1589 >                                                                caps_on = true;
1590 >                                                        }
1591 >                                                } else
1592 >                                                        ADBKeyDown(code);
1593                                                  if (code == 0x36)
1594                                                          ctrl_down = true;
1595                                          } else {
1596 < #if ENABLE_DGA
1596 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1597                                                  if (code == 0x31)
1598                                                          resume_emul();  // Space wakes us up
1599   #endif
# Line 987 | Line 1608 | static void handle_events(void)
1608                                          code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1609                                  } else
1610                                          code = event2keycode((XKeyEvent *)&event);
1611 <                                if (code != -1) {
1611 >                                if (code != -1 && code != 0x39) {       // Don't propagate Caps Lock releases
1612                                          ADBKeyUp(code);
1613                                          if (code == 0x36)
1614                                                  ctrl_down = false;
# Line 997 | Line 1618 | static void handle_events(void)
1618  
1619                          // Hidden parts exposed, force complete refresh of window
1620                          case Expose:
1621 <                                if (display_type == DISPLAY_WINDOW)
1622 <                                        memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1621 >                                if (display_type == DISPLAY_WINDOW) {
1622 > #ifdef ENABLE_VOSF
1623 >                                        if (use_vosf) {                 // VOSF refresh
1624 > #ifdef HAVE_PTHREADS
1625 >                                                pthread_mutex_lock(&Screen_draw_lock);
1626 > #endif
1627 >                                                PFLAG_SET_ALL;
1628 > #ifdef HAVE_PTHREADS
1629 >                                                pthread_mutex_unlock(&Screen_draw_lock);
1630 > #endif
1631 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1632 >                                        }
1633 >                                        else
1634 > #endif
1635 >                                        if (frame_skip == 0) {  // Dynamic refresh
1636 >                                                int x1, y1;
1637 >                                                for (y1=0; y1<16; y1++)
1638 >                                                for (x1=0; x1<16; x1++)
1639 >                                                        updt_box[x1][y1] = true;
1640 >                                                nr_boxes = 16 * 16;
1641 >                                        } else                                  // Static refresh
1642 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1643 >                                }
1644 >                                break;
1645 >
1646 >                        case FocusIn:
1647 >                        case FocusOut:
1648                                  break;
1649                  }
1650          }
# Line 1009 | Line 1655 | static void handle_events(void)
1655   *  Window display update
1656   */
1657  
1658 < static void update_display(void)
1658 > // Dynamic display update (variable frame rate for each box)
1659 > static void update_display_dynamic(int ticker)
1660   {
1661 <        // In classic mode, copy the frame buffer from Mac RAM
1662 <        if (classic_mode)
1663 <                memcpy(the_buffer, Mac2HostAddr(0x3fa700), VideoMonitor.bytes_per_row * VideoMonitor.y);
1661 >        int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1662 >        int xil = 0;
1663 >        int rxm = 0, rxmo = 0;
1664 >        int bytes_per_row = VideoMonitor.bytes_per_row;
1665 >        int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1666 >        int rx = VideoMonitor.bytes_per_row / 16;
1667 >        int ry = VideoMonitor.y / 16;
1668 >        int max_box;
1669 >
1670 >        y2s = sm_uptd[ticker % 8];
1671 >        y2a = 8;
1672 >        for (i = 0; i < 6; i++)
1673 >                if (ticker % (2 << i))
1674 >                        break;
1675 >        max_box = sm_no_boxes[i];
1676  
1677 +        if (y2a) {
1678 +                for (y1=0; y1<16; y1++) {
1679 +                        for (y2=y2s; y2 < ry; y2 += y2a) {
1680 +                                i = ((y1 * ry) + y2) * bytes_per_row;
1681 +                                for (x1=0; x1<16; x1++, i += rx) {
1682 +                                        if (updt_box[x1][y1] == false) {
1683 +                                                if (memcmp(&the_buffer_copy[i], &the_buffer[i], rx)) {
1684 +                                                        updt_box[x1][y1] = true;
1685 +                                                        nr_boxes++;
1686 +                                                }
1687 +                                        }
1688 +                                }
1689 +                        }
1690 +                }
1691 +        }
1692 +
1693 +        if ((nr_boxes <= max_box) && (nr_boxes)) {
1694 +                for (y1=0; y1<16; y1++) {
1695 +                        for (x1=0; x1<16; x1++) {
1696 +                                if (updt_box[x1][y1] == true) {
1697 +                                        if (rxm == 0)
1698 +                                                xm = x1;
1699 +                                        rxm += rx;
1700 +                                        updt_box[x1][y1] = false;
1701 +                                }
1702 +                                if (((updt_box[x1+1][y1] == false) || (x1 == 15)) && (rxm)) {
1703 +                                        if ((rxmo != rxm) || (xmo != xm) || (yo != y1 - 1)) {
1704 +                                                if (rxmo) {
1705 +                                                        xi = xmo * rx;
1706 +                                                        yi = ymo * ry;
1707 +                                                        xil = rxmo;
1708 +                                                        yil = (yo - ymo +1) * ry;
1709 +                                                }
1710 +                                                rxmo = rxm;
1711 +                                                xmo = xm;
1712 +                                                ymo = y1;
1713 +                                        }
1714 +                                        rxm = 0;
1715 +                                        yo = y1;
1716 +                                }      
1717 +                                if (xil) {
1718 +                                        i = (yi * bytes_per_row) + xi;
1719 +                                        for (y2=0; y2 < yil; y2++, i += bytes_per_row)
1720 +                                                memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
1721 +                                        if (depth == 1) {
1722 +                                                if (have_shm)
1723 +                                                        XShmPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
1724 +                                                else
1725 +                                                        XPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil);
1726 +                                        } else {
1727 +                                                if (have_shm)
1728 +                                                        XShmPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0);
1729 +                                                else
1730 +                                                        XPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil);
1731 +                                        }
1732 +                                        xil = 0;
1733 +                                }
1734 +                                if ((x1 == 15) && (y1 == 15) && (rxmo)) {
1735 +                                        x1--;
1736 +                                        xi = xmo * rx;
1737 +                                        yi = ymo * ry;
1738 +                                        xil = rxmo;
1739 +                                        yil = (yo - ymo +1) * ry;
1740 +                                        rxmo = 0;
1741 +                                }
1742 +                        }
1743 +                }
1744 +                nr_boxes = 0;
1745 +        }
1746 + }
1747 +
1748 + // Static display update (fixed frame rate, but incremental)
1749 + static void update_display_static(void)
1750 + {
1751          // Incremental update code
1752          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1753          int bytes_per_row = VideoMonitor.bytes_per_row;
# Line 1041 | Line 1774 | static void update_display(void)
1774          // Check for first column from left and first column from right that have changed
1775          if (high) {
1776                  if (depth == 1) {
1777 <                        x1 = VideoMonitor.x;
1777 >                        x1 = VideoMonitor.x - 1;
1778                          for (j=y1; j<=y2; j++) {
1779                                  p = &the_buffer[j * bytes_per_row];
1780                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1050 | Line 1783 | static void update_display(void)
1783                                                  x1 = i << 3;
1784                                                  break;
1785                                          }
1786 <                                        p++;
1054 <                                        p2++;
1786 >                                        p++; p2++;
1787                                  }
1788                          }
1789                          x2 = x1;
# Line 1061 | Line 1793 | static void update_display(void)
1793                                  p += bytes_per_row;
1794                                  p2 += bytes_per_row;
1795                                  for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1796 <                                        p--;
1065 <                                        p2--;
1796 >                                        p--; p2--;
1797                                          if (*p != *p2) {
1798 <                                                x2 = i << 3;
1798 >                                                x2 = (i << 3) + 7;
1799                                                  break;
1800                                          }
1801                                  }
1802                          }
1803 <                        wide = x2 - x1;
1803 >                        wide = x2 - x1 + 1;
1804  
1805                          // Update copy of the_buffer
1806                          if (high && wide) {
1807                                  for (j=y1; j<=y2; j++) {
1808                                          i = j * bytes_per_row + (x1 >> 3);
1809 <                                        memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
1809 >                                        memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3);
1810                                  }
1811                          }
1812  
# Line 1084 | Line 1815 | static void update_display(void)
1815                          for (j=y1; j<=y2; j++) {
1816                                  p = &the_buffer[j * bytes_per_row];
1817                                  p2 = &the_buffer_copy[j * bytes_per_row];
1818 <                                for (i=0; i<x1; i++) {
1819 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1820 <                                                x1 = i;
1818 >                                for (i=0; i<x1*bytes_per_pixel; i++) {
1819 >                                        if (*p != *p2) {
1820 >                                                x1 = i / bytes_per_pixel;
1821                                                  break;
1822                                          }
1823 <                                        p += bytes_per_pixel;
1093 <                                        p2 += bytes_per_pixel;
1823 >                                        p++; p2++;
1824                                  }
1825                          }
1826                          x2 = x1;
# Line 1099 | Line 1829 | static void update_display(void)
1829                                  p2 = &the_buffer_copy[j * bytes_per_row];
1830                                  p += bytes_per_row;
1831                                  p2 += bytes_per_row;
1832 <                                for (i=VideoMonitor.x; i>x2; i--) {
1833 <                                        p -= bytes_per_pixel;
1834 <                                        p2 -= bytes_per_pixel;
1835 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1836 <                                                x2 = i;
1832 >                                for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1833 >                                        p--;
1834 >                                        p2--;
1835 >                                        if (*p != *p2) {
1836 >                                                x2 = i / bytes_per_pixel;
1837                                                  break;
1838                                          }
1839                                  }
# Line 1114 | Line 1844 | static void update_display(void)
1844                          if (high && wide) {
1845                                  for (j=y1; j<=y2; j++) {
1846                                          i = j * bytes_per_row + x1 * bytes_per_pixel;
1847 <                                        memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
1847 >                                        memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1848                                  }
1849                          }
1850                  }
# Line 1127 | Line 1857 | static void update_display(void)
1857                  else
1858                          XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1859          }
1860 + }
1861 +
1862 +
1863 + /*
1864 + *      Screen refresh functions
1865 + */
1866  
1867 <        // Has the Mac started? (cursor data is not valid otherwise)
1868 <        if (HasMacStarted()) {
1867 > // The specialisations hereunder are meant to enable VOSF with DGA in direct
1868 > // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1869 > // not get mapped correctly with respect to the predetermined host frame
1870 > // buffer base address.
1871 > //
1872 > // Hmm, in other words, when in direct addressing mode and DGA is requested,
1873 > // we first try to "triple allocate" the address spaces according to the real
1874 > // host frame buffer address. Then, if it fails, we will use a temporary
1875 > // frame buffer thus making the real host frame buffer updated when pages
1876 > // of the temp frame buffer are altered.
1877 > //
1878 > // As a side effect, a little speed gain in screen updates could be noticed
1879 > // for other modes than DGA.
1880 > //
1881 > // The following two functions below are inline so that a clever compiler
1882 > // could specialise the code according to the current screen depth and
1883 > // display type. A more clever compiler would the job by itself though...
1884 > // (update_display_vosf is inlined as well)
1885  
1886 <                // Set new cursor image if it was changed
1887 <                if (memcmp(the_cursor, Mac2HostAddr(0x844), 64)) {
1888 <                        memcpy(the_cursor, Mac2HostAddr(0x844), 64);
1889 <                        memcpy(cursor_image->data, the_cursor, 32);
1890 <                        memcpy(cursor_mask_image->data, the_cursor+32, 32);
1891 <                        XFreeCursor(x_display, mac_cursor);
1892 <                        XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
1893 <                        XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
1894 <                        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, ReadMacInt8(0x885), ReadMacInt8(0x887));
1895 <                        XDefineCursor(x_display, the_win, mac_cursor);
1886 > static inline void possibly_quit_dga_mode()
1887 > {
1888 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1889 >        // Quit DGA mode if requested
1890 >        if (quit_full_screen) {
1891 >                quit_full_screen = false;
1892 > #ifdef ENABLE_XF86_DGA
1893 >                XF86DGADirectVideo(x_display, screen, 0);
1894 > #endif
1895 >                XUngrabPointer(x_display, CurrentTime);
1896 >                XUngrabKeyboard(x_display, CurrentTime);
1897 >                XUnmapWindow(x_display, the_win);
1898 >                XSync(x_display, false);
1899 >        }
1900 > #endif
1901 > }
1902 >
1903 > static inline void handle_palette_changes(int depth, int display_type)
1904 > {
1905 > #ifdef HAVE_PTHREADS
1906 >        pthread_mutex_lock(&palette_lock);
1907 > #endif
1908 >        if (palette_changed) {
1909 >                palette_changed = false;
1910 >                if (depth == 8) {
1911 >                        XStoreColors(x_display, cmap[0], palette, 256);
1912 >                        XStoreColors(x_display, cmap[1], palette, 256);
1913 >                                
1914 > #ifdef ENABLE_XF86_DGA
1915 >                        if (display_type == DISPLAY_DGA) {
1916 >                                current_dga_cmap ^= 1;
1917 >                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1918 >                        }
1919 > #endif
1920                  }
1921          }
1922 + #ifdef HAVE_PTHREADS
1923 +        pthread_mutex_unlock(&palette_lock);
1924 + #endif
1925 + }
1926 +
1927 + static void video_refresh_dga(void)
1928 + {
1929 +        // Quit DGA mode if requested
1930 +        possibly_quit_dga_mode();
1931 +        
1932 +        // Handle X events
1933 +        handle_events();
1934 +        
1935 +        // Handle palette changes
1936 +        handle_palette_changes(depth, DISPLAY_DGA);
1937 + }
1938 +
1939 + #ifdef ENABLE_VOSF
1940 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
1941 + static void video_refresh_dga_vosf(void)
1942 + {
1943 +        // Quit DGA mode if requested
1944 +        possibly_quit_dga_mode();
1945 +        
1946 +        // Handle X events
1947 +        handle_events();
1948 +        
1949 +        // Handle palette changes
1950 +        handle_palette_changes(depth, DISPLAY_DGA);
1951 +        
1952 +        // Update display (VOSF variant)
1953 +        static int tick_counter = 0;
1954 +        if (++tick_counter >= frame_skip) {
1955 +                tick_counter = 0;
1956 + #ifdef HAVE_PTHREADS
1957 +                pthread_mutex_lock(&Screen_draw_lock);
1958 + #endif
1959 +                update_display_dga_vosf();
1960 + #ifdef HAVE_PTHREADS
1961 +                pthread_mutex_unlock(&Screen_draw_lock);
1962 + #endif
1963 +        }
1964 + }
1965 + #endif
1966 +
1967 + static void video_refresh_window_vosf(void)
1968 + {
1969 +        // Quit DGA mode if requested
1970 +        possibly_quit_dga_mode();
1971 +        
1972 +        // Handle X events
1973 +        handle_events();
1974 +        
1975 +        // Handle palette changes
1976 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1977 +        
1978 +        // Update display (VOSF variant)
1979 +        static int tick_counter = 0;
1980 +        if (++tick_counter >= frame_skip) {
1981 +                tick_counter = 0;
1982 + #ifdef HAVE_PTHREADS
1983 +                pthread_mutex_lock(&Screen_draw_lock);
1984 + #endif
1985 +                update_display_window_vosf();
1986 + #ifdef HAVE_PTHREADS
1987 +                pthread_mutex_unlock(&Screen_draw_lock);
1988 + #endif
1989 +        }
1990 + }
1991 + #endif // def ENABLE_VOSF
1992 +
1993 + static void video_refresh_window_static(void)
1994 + {
1995 +        // Handle X events
1996 +        handle_events();
1997 +        
1998 +        // Handle_palette changes
1999 +        handle_palette_changes(depth, DISPLAY_WINDOW);
2000 +        
2001 +        // Update display (static variant)
2002 +        static int tick_counter = 0;
2003 +        if (++tick_counter >= frame_skip) {
2004 +                tick_counter = 0;
2005 +                update_display_static();
2006 +        }
2007 + }
2008 +
2009 + static void video_refresh_window_dynamic(void)
2010 + {
2011 +        // Handle X events
2012 +        handle_events();
2013 +        
2014 +        // Handle_palette changes
2015 +        handle_palette_changes(depth, DISPLAY_WINDOW);
2016 +        
2017 +        // Update display (dynamic variant)
2018 +        static int tick_counter = 0;
2019 +        tick_counter++;
2020 +        update_display_dynamic(tick_counter);
2021   }
2022  
2023  
# Line 1150 | Line 2025 | static void update_display(void)
2025   *  Thread for screen refresh, input handling etc.
2026   */
2027  
2028 < static void *redraw_func(void *arg)
2028 > void VideoRefreshInit(void)
2029   {
2030 <        int tick_counter = 0;
2030 >        // TODO: set up specialised 8bpp VideoRefresh handlers ?
2031 >        if (display_type == DISPLAY_DGA) {
2032 > #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2033 >                if (use_vosf)
2034 >                        video_refresh = video_refresh_dga_vosf;
2035 >                else
2036 > #endif
2037 >                        video_refresh = video_refresh_dga;
2038 >        }
2039 >        else {
2040 > #ifdef ENABLE_VOSF
2041 >                if (use_vosf)
2042 >                        video_refresh = video_refresh_window_vosf;
2043 >                else
2044 > #endif
2045 >                if (frame_skip == 0)
2046 >                        video_refresh = video_refresh_window_dynamic;
2047 >                else
2048 >                        video_refresh = video_refresh_window_static;
2049 >        }
2050 > }
2051  
2052 <        while (!redraw_thread_cancel) {
2052 > void VideoRefresh(void)
2053 > {
2054 >        // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2055 >        video_refresh();
2056 > }
2057  
2058 <                // Wait
2059 < #ifdef HAVE_NANOSLEEP
2060 <                struct timespec req = {0, 16666667};
2061 <                nanosleep(&req, NULL);
2062 < #else
2063 <                usleep(16667);
2058 > #if 0
2059 > void VideoRefresh(void)
2060 > {
2061 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2062 >        // Quit DGA mode if requested
2063 >        if (quit_full_screen) {
2064 >                quit_full_screen = false;
2065 >                if (display_type == DISPLAY_DGA) {
2066 > #ifdef ENABLE_XF86_DGA
2067 >                        XF86DGADirectVideo(x_display, screen, 0);
2068   #endif
2069 <
2070 < #if ENABLE_DGA
2071 <                // Quit DGA mode if requested
2072 <                if (quit_full_screen) {
1170 <                        quit_full_screen = false;
1171 <                        if (display_type == DISPLAY_DGA) {
1172 <                                XF86DGADirectVideo(x_display, screen, 0);
1173 <                                XUngrabPointer(x_display, CurrentTime);
1174 <                                XUngrabKeyboard(x_display, CurrentTime);
1175 <                                XUnmapWindow(x_display, the_win);
1176 <                                XSync(x_display, false);
1177 <                        }
2069 >                        XUngrabPointer(x_display, CurrentTime);
2070 >                        XUngrabKeyboard(x_display, CurrentTime);
2071 >                        XUnmapWindow(x_display, the_win);
2072 >                        XSync(x_display, false);
2073                  }
2074 +        }
2075   #endif
2076  
2077 <                // Handle X events
2078 <                handle_events();
2077 >        // Handle X events
2078 >        handle_events();
2079  
2080 <                // Handle palette changes
2081 <                pthread_mutex_lock(&palette_lock);
2082 <                if (palette_changed) {
1187 <                        palette_changed = false;
1188 <                        if (depth == 8) {
1189 <                                XStoreColors(x_display, cmap[0], palette, 256);
1190 <                                XStoreColors(x_display, cmap[1], palette, 256);
1191 < #if ENABLE_DGA
1192 <                                if (display_type == DISPLAY_DGA) {
1193 <                                        current_dga_cmap ^= 1;
1194 <                                        XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1195 <                                }
2080 >        // Handle palette changes
2081 > #ifdef HAVE_PTHREADS
2082 >        pthread_mutex_lock(&palette_lock);
2083   #endif
2084 +        if (palette_changed) {
2085 +                palette_changed = false;
2086 +                if (depth == 8) {
2087 +                        XStoreColors(x_display, cmap[0], palette, 256);
2088 +                        XStoreColors(x_display, cmap[1], palette, 256);
2089 +                                
2090 + #ifdef ENABLE_XF86_DGA
2091 +                        if (display_type == DISPLAY_DGA) {
2092 +                                current_dga_cmap ^= 1;
2093 +                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2094                          }
2095 + #endif
2096                  }
2097 <                pthread_mutex_unlock(&palette_lock);
2097 >        }
2098 > #ifdef HAVE_PTHREADS
2099 >        pthread_mutex_unlock(&palette_lock);
2100 > #endif
2101  
2102 <                // In window mode, update display and mouse pointer
2103 <                if (display_type == DISPLAY_WINDOW) {
2104 <                        tick_counter++;
2105 <                        if (tick_counter >= frame_skip) {
2106 <                                tick_counter = 0;
2107 <                                update_display();
2108 <                        }
2102 >        // In window mode, update display
2103 >        static int tick_counter = 0;
2104 >        if (display_type == DISPLAY_WINDOW) {
2105 >                tick_counter++;
2106 >                if (frame_skip == 0)
2107 >                        update_display_dynamic(tick_counter);
2108 >                else if (tick_counter >= frame_skip) {
2109 >                        tick_counter = 0;
2110 >                        update_display_static();
2111                  }
2112          }
2113 + }
2114 + #endif
2115 +
2116 + #ifdef HAVE_PTHREADS
2117 + static void *redraw_func(void *arg)
2118 + {
2119 +        uint64 start = GetTicks_usec();
2120 +        int64 ticks = 0;
2121 +        uint64 next = GetTicks_usec();
2122 +        while (!redraw_thread_cancel) {
2123 + //              VideoRefresh();
2124 +                video_refresh();
2125 +                next += 16667;
2126 +                int64 delay = next - GetTicks_usec();
2127 +                if (delay > 0)
2128 +                        Delay_usec(delay);
2129 +                else if (delay < -16667)
2130 +                        next = GetTicks_usec();
2131 +                ticks++;
2132 +        }
2133 +        uint64 end = GetTicks_usec();
2134 +        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2135          return NULL;
2136   }
2137 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines