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.27 by cebix, 2000-11-02T14:45:16Z

# 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;
339          wattr.border_pixel = black_pixel;
340 <        wattr.backing_store = Always;
340 >        wattr.backing_store = NotUseful;
341 >        wattr.save_under = false;
342          wattr.backing_planes = xdepth;
343  
344          XSync(x_display, false);
345          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
346                  InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
347                  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);
348  
349 <        // Set colormap
350 <        if (depth == 8) {
351 <                XSetWindowColormap(x_display, the_win, cmap[0]);
352 <                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
349 >        // Indicate that we want keyboard input
350 >        {
351 >                XWMHints *hints = XAllocWMHints();
352 >                if (hints) {
353 >                        hints->input = True;
354 >                        hints->flags = InputHint;
355 >                        XSetWMHints(x_display, the_win, hints);
356 >                        XFree((char *)hints);
357 >                }
358          }
359  
360          // Make window unresizable
361 <        XSizeHints *hints;
362 <        if ((hints = XAllocSizeHints()) != NULL) {
363 <                hints->min_width = width;
364 <                hints->max_width = width;
365 <                hints->min_height = height;
366 <                hints->max_height = height;
367 <                hints->flags = PMinSize | PMaxSize;
368 <                XSetWMNormalHints(x_display, the_win, hints);
369 <                XFree((char *)hints);
361 >        {
362 >                XSizeHints *hints = XAllocSizeHints();
363 >                if (hints) {
364 >                        hints->min_width = width;
365 >                        hints->max_width = width;
366 >                        hints->min_height = height;
367 >                        hints->max_height = height;
368 >                        hints->flags = PMinSize | PMaxSize;
369 >                        XSetWMNormalHints(x_display, the_win, hints);
370 >                        XFree((char *)hints);
371 >                }
372          }
373 +        
374 +        // Set window title
375 +        {
376 +                XTextProperty title_prop;
377 +                const char *title = GetString(STR_WINDOW_TITLE);
378 +                XStringListToTextProperty((char **)&title, 1, &title_prop);
379 +                XSetWMName(x_display, the_win, &title_prop);
380 +                XFree(title_prop.value);
381 +        }
382 +
383 +        // Set window class
384 +        {
385 +                XClassHint *hints;
386 +                hints = XAllocClassHint();
387 +                if (hints) {
388 +                        hints->res_name = "BasiliskII";
389 +                        hints->res_class = "BasiliskII";
390 +                        XSetClassHint(x_display, the_win, hints);
391 +                        XFree((char *)hints);
392 +                }
393 +        }
394 +
395 +        // Show window
396 +        XSync(x_display, false);
397 +        XMapRaised(x_display, the_win);
398 +        XFlush(x_display);
399 +
400 +        // Set colormap
401 +        if (depth == 8)
402 +                XSetWindowColormap(x_display, the_win, cmap[0]);
403  
404          // Try to create and attach SHM image
405          have_shm = false;
406 <        if (depth != 1 && XShmQueryExtension(x_display)) {
406 >        if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
407  
408                  // Create SHM image ("height + 2" for safety)
409                  img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
410 <                shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
411 <                the_buffer = (uint8 *)shmat(shminfo.shmid, 0, 0);
412 <                shminfo.shmaddr = img->data = (char *)the_buffer;
410 >                shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
411 >                the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
412 >                shminfo.shmaddr = img->data = (char *)the_buffer_copy;
413                  shminfo.readOnly = False;
414  
415                  // Try to attach SHM image, catching errors
# Line 259 | Line 427 | static bool init_window(int width, int h
427                          shmctl(shminfo.shmid, IPC_RMID, 0);
428                  }
429          }
430 <
430 >        
431          // Create normal X image if SHM doesn't work ("height + 2" for safety)
432          if (!have_shm) {
433 <                int bytes_per_row = width;
433 >                int bytes_per_row = aligned_width;
434                  switch (depth) {
435                          case 1:
436                                  bytes_per_row /= 8;
# Line 276 | Line 444 | static bool init_window(int width, int h
444                                  bytes_per_row *= 4;
445                                  break;
446                  }
447 <                the_buffer = (uint8 *)malloc((height + 2) * bytes_per_row);
448 <                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer, width, height, 32, bytes_per_row);
447 >                the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
448 >                img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
449          }
450  
451          // 1-Bit mode is big-endian
# Line 286 | Line 454 | static bool init_window(int width, int h
454                  img->bitmap_bit_order = MSBFirst;
455          }
456  
457 <        // Allocate memory for frame buffer copy
458 <        the_buffer_copy = (uint8 *)malloc((height + 2) * img->bytes_per_line);
457 > #ifdef ENABLE_VOSF
458 >        // Allocate a page-aligned chunk of memory for frame buffer
459 >        the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line);
460 >        the_host_buffer = the_buffer_copy;
461 >        
462 >        the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size);
463 >        memset(the_buffer_copy, 0, the_buffer_size);
464 >        
465 >        the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
466 >        memset(the_buffer, 0, the_buffer_size);
467 > #else
468 >        // Allocate memory for frame buffer
469 >        the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
470 > #endif
471  
472          // Create GC
473          the_gc = XCreateGC(x_display, the_win, 0, 0);
474          XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
475  
476 <        // Create cursor
477 <        cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor, 16, 16, 16, 2);
478 <        cursor_image->byte_order = MSBFirst;
479 <        cursor_image->bitmap_bit_order = MSBFirst;
480 <        cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor+32, 16, 16, 16, 2);
481 <        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);
476 >        // Create no_cursor
477 >        mac_cursor = XCreatePixmapCursor(x_display,
478 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
479 >           XCreatePixmap(x_display, the_win, 1, 1, 1),
480 >           &black, &white, 0, 0);
481 >        XDefineCursor(x_display, the_win, mac_cursor);
482  
483          // Set VideoMonitor
484 +        bool native_byte_order;
485   #ifdef WORDS_BIGENDIAN
486 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst);
486 >        native_byte_order = (img->bitmap_bit_order == MSBFirst);
487   #else
488 <        set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst);
488 >        native_byte_order = (img->bitmap_bit_order == LSBFirst);
489 > #endif
490 > #ifdef ENABLE_VOSF
491 >        do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
492   #endif
493 < #if REAL_ADDRESSING
494 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
495 <        MacFrameLayout = FLAYOUT_DIRECT;
493 >        set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
494 >        
495 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
496 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
497   #else
498          VideoMonitor.mac_frame_base = MacFrameBaseMac;
499   #endif
500          return true;
501   }
502  
503 < // Init DGA display
504 < static bool init_dga(int width, int height)
503 > // Init fbdev DGA display
504 > static bool init_fbdev_dga(char *in_fb_name)
505   {
506 < #if ENABLE_DGA
506 > #ifdef ENABLE_FBDEV_DGA
507 >        // Find the maximum depth available
508 >        int ndepths, max_depth(0);
509 >        int *depths = XListDepths(x_display, screen, &ndepths);
510 >        if (depths == NULL) {
511 >                printf("FATAL: Could not determine the maximal depth available\n");
512 >                return false;
513 >        } else {
514 >                while (ndepths-- > 0) {
515 >                        if (depths[ndepths] > max_depth)
516 >                                max_depth = depths[ndepths];
517 >                }
518 >        }
519 >        
520 >        // Get fbdevices file path from preferences
521 >        const char *fbd_path = PrefsFindString("fbdevicefile");
522 >        
523 >        // Open fbdevices file
524 >        FILE *fp = fopen(fbd_path ? fbd_path : FBDEVICES_FILE_NAME, "r");
525 >        if (fp == NULL) {
526 >                char str[256];
527 >                sprintf(str, GetString(STR_NO_FBDEVICE_FILE_ERR), fbd_path ? fbd_path : FBDEVICES_FILE_NAME, strerror(errno));
528 >                ErrorAlert(str);
529 >                return false;
530 >        }
531 >        
532 >        int fb_depth;           // supported depth
533 >        uint32 fb_offset;       // offset used for mmap(2)
534 >        char fb_name[20];
535 >        char line[256];
536 >        bool device_found = false;
537 >        while (fgets(line, 255, fp)) {
538 >                // Read line
539 >                int len = strlen(line);
540 >                if (len == 0)
541 >                        continue;
542 >                line[len - 1] = '\0';
543 >                
544 >                // Comments begin with "#" or ";"
545 >                if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
546 >                        continue;
547 >                
548 >                if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
549 >                && (strcmp(fb_name, in_fb_name) == 0) && (fb_depth == max_depth)) {
550 >                        device_found = true;
551 >                        break;
552 >                }
553 >        }
554 >        
555 >        // fbdevices file completely read
556 >        fclose(fp);
557 >        
558 >        // Frame buffer name not found ? Then, display warning
559 >        if (!device_found) {
560 >                char str[256];
561 >                sprintf(str, GetString(STR_FBDEV_NAME_ERR), in_fb_name, max_depth);
562 >                ErrorAlert(str);
563 >                return false;
564 >        }
565 >        
566 >        int width = DisplayWidth(x_display, screen);
567 >        int height = DisplayHeight(x_display, screen);
568 >        depth = fb_depth; // max_depth
569 >        
570 >        // Set relative mouse mode
571 >        ADBSetRelMouseMode(false);
572 >        
573 >        // Create window
574 >        XSetWindowAttributes wattr;
575 >        wattr.override_redirect = True;
576 >        wattr.backing_store             = NotUseful;
577 >        wattr.background_pixel  = white_pixel;
578 >        wattr.border_pixel              = black_pixel;
579 >        wattr.event_mask                = eventmask = dga_eventmask;
580 >        
581 >        XSync(x_display, false);
582 >        the_win = XCreateWindow(x_display, rootwin,
583 >                0, 0, width, height,
584 >                0, xdepth, InputOutput, vis,
585 >                CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore,
586 >                &wattr);
587 >        XSync(x_display, false);
588 >        XMapRaised(x_display, the_win);
589 >        XSync(x_display, false);
590 >        
591 >        // Grab mouse and keyboard
592 >        XGrabKeyboard(x_display, the_win, True,
593 >                GrabModeAsync, GrabModeAsync, CurrentTime);
594 >        XGrabPointer(x_display, the_win, True,
595 >                PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
596 >                GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
597 >        
598 >        // Set colormap
599 >        if (depth == 8)
600 >                XSetWindowColormap(x_display, the_win, cmap[0]);
601 >        
602 >        // Set VideoMonitor
603 >        int bytes_per_row = width;
604 >        switch (depth) {
605 >                case 1:
606 >                        bytes_per_row = ((width | 7) & ~7) >> 3;
607 >                        break;
608 >                case 15:
609 >                case 16:
610 >                        bytes_per_row *= 2;
611 >                        break;
612 >                case 24:
613 >                case 32:
614 >                        bytes_per_row *= 4;
615 >                        break;
616 >        }
617 >        
618 >        if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
619 >                if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
620 >                        char str[256];
621 >                        sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
622 >                        ErrorAlert(str);
623 >                        return false;
624 >                }
625 >        }
626 >        
627 > #if ENABLE_VOSF
628 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
629 >        // If the blit function is null, i.e. just a copy of the buffer,
630 >        // we first try to avoid the allocation of a temporary frame buffer
631 >        use_vosf = true;
632 >        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
633 >        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
634 >                use_vosf = false;
635 >        
636 >        if (use_vosf) {
637 >                the_host_buffer = the_buffer;
638 >                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
639 >                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
640 >                memset(the_buffer_copy, 0, the_buffer_size);
641 >                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
642 >                memset(the_buffer, 0, the_buffer_size);
643 >        }
644 > #else
645 >        use_vosf = false;
646 > #endif
647 > #endif
648 >        
649 >        set_video_monitor(width, height, bytes_per_row, true);
650 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
651 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
652 > #else
653 >        VideoMonitor.mac_frame_base = MacFrameBaseMac;
654 > #endif
655 >        return true;
656 > #else
657 >        ErrorAlert("Basilisk II has been compiled with fbdev DGA support disabled.");
658 >        return false;
659 > #endif
660 > }
661 >
662 > // Init XF86 DGA display
663 > static bool init_xf86_dga(int width, int height)
664 > {
665 > #ifdef ENABLE_XF86_DGA
666          // Set relative mouse mode
667          ADBSetRelMouseMode(true);
668  
669 + #ifdef ENABLE_XF86_VIDMODE
670 +        // Switch to best mode
671 +        if (has_vidmode) {
672 +                int best = 0;
673 +                for (int i=1; i<num_x_video_modes; i++) {
674 +                        if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
675 +                                x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
676 +                                best = i;
677 +                        }
678 +                }
679 +                XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
680 +                XF86VidModeSetViewPort(x_display, screen, 0, 0);
681 +        }
682 + #endif
683 +
684          // Create window
685          XSetWindowAttributes wattr;
686          wattr.event_mask = eventmask = dga_eventmask;
# Line 357 | Line 710 | static bool init_dga(int width, int heig
710          // Set colormap
711          if (depth == 8) {
712                  XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
360                XSetWMColormapWindows(x_display, the_win, &the_win, 1);
713                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
714          }
715  
# Line 376 | Line 728 | static bool init_dga(int width, int heig
728                          bytes_per_row *= 4;
729                          break;
730          }
731 +        
732 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
733 +        // If the blit function is null, i.e. just a copy of the buffer,
734 +        // we first try to avoid the allocation of a temporary frame buffer
735 +        use_vosf = true;
736 +        do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
737 +        if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
738 +                use_vosf = false;
739 +        
740 +        if (use_vosf) {
741 +                the_host_buffer = the_buffer;
742 +                the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
743 +                the_buffer_copy = (uint8 *)malloc(the_buffer_size);
744 +                memset(the_buffer_copy, 0, the_buffer_size);
745 +                the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
746 +                memset(the_buffer, 0, the_buffer_size);
747 +        }
748 + #elif defined(ENABLE_VOSF)
749 +        // The UAE memory handlers will already handle color conversion, if needed.
750 +        use_vosf = false;
751 + #endif
752 +        
753          set_video_monitor(width, height, bytes_per_row, true);
754 < #if REAL_ADDRESSING
755 <        VideoMonitor.mac_frame_base = (uint32)the_buffer;
756 <        MacFrameLayout = FLAYOUT_DIRECT;
754 > #if REAL_ADDRESSING || DIRECT_ADDRESSING
755 >        VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
756 > //      MacFrameLayout = FLAYOUT_DIRECT;
757   #else
758          VideoMonitor.mac_frame_base = MacFrameBaseMac;
759   #endif
760          return true;
761   #else
762 <        ErrorAlert("Basilisk II has been compiled with DGA support disabled.");
762 >        ErrorAlert("Basilisk II has been compiled with XF86 DGA support disabled.");
763          return false;
764   #endif
765   }
# Line 455 | Line 829 | static void keycode_init(void)
829          }
830   }
831  
832 + bool VideoInitBuffer()
833 + {
834 + #ifdef ENABLE_VOSF
835 +        if (use_vosf) {
836 +                const uint32 page_size  = getpagesize();
837 +                const uint32 page_mask  = page_size - 1;
838 +                
839 +                mainBuffer.memBase      = (uint32) the_buffer;
840 +                // Align the frame buffer on page boundary
841 +                mainBuffer.memStart             = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
842 +                mainBuffer.memLength    = the_buffer_size;
843 +                mainBuffer.memEnd       = mainBuffer.memStart + mainBuffer.memLength;
844 +
845 +                mainBuffer.pageSize     = page_size;
846 +                mainBuffer.pageCount    = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
847 +                mainBuffer.pageBits     = log_base_2(mainBuffer.pageSize);
848 +
849 +                if (mainBuffer.dirtyPages != 0)
850 +                        free(mainBuffer.dirtyPages);
851 +
852 +                mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
853 +
854 +                if (mainBuffer.pageInfo != 0)
855 +                        free(mainBuffer.pageInfo);
856 +
857 +                mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
858 +
859 +                if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
860 +                        return false;
861 +
862 +                PFLAG_CLEAR_ALL;
863 +
864 +                uint32 a = 0;
865 +                for (int i = 0; i < mainBuffer.pageCount; i++) {
866 +                        int y1 = a / VideoMonitor.bytes_per_row;
867 +                        if (y1 >= VideoMonitor.y)
868 +                                y1 = VideoMonitor.y - 1;
869 +
870 +                        int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
871 +                        if (y2 >= VideoMonitor.y)
872 +                                y2 = VideoMonitor.y - 1;
873 +
874 +                        mainBuffer.pageInfo[i].top = y1;
875 +                        mainBuffer.pageInfo[i].bottom = y2;
876 +
877 +                        a += mainBuffer.pageSize;
878 +                        if (a > mainBuffer.memLength)
879 +                                a = mainBuffer.memLength;
880 +                }
881 +                
882 +                // We can now write-protect the frame buffer
883 +                if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0)
884 +                        return false;
885 +        }
886 + #endif
887 +        return true;
888 + }
889 +
890   bool VideoInit(bool classic)
891   {
892 + #ifdef ENABLE_VOSF
893 +        // Open /dev/zero
894 +        zero_fd = open("/dev/zero", O_RDWR);
895 +        if (zero_fd < 0) {
896 +        char str[256];
897 +                sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
898 +                ErrorAlert(str);
899 +        return false;
900 +        }
901 +        
902 +        // Zero the mainBuffer structure
903 +        mainBuffer.dirtyPages = 0;
904 +        mainBuffer.pageInfo = 0;
905 + #endif
906 +        
907 +        // Check if X server runs on local machine
908 +        local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
909 +                 || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
910 +    
911          // Init keycode translation
912          keycode_init();
913  
914 +        // Read prefs
915 +        mouse_wheel_mode = PrefsFindInt16("mousewheelmode");
916 +        mouse_wheel_lines = PrefsFindInt16("mousewheellines");
917 +
918          // Find screen and root window
919          screen = XDefaultScreen(x_display);
920          rootwin = XRootWindow(x_display, screen);
921 <
921 >        
922          // Get screen depth
923          xdepth = DefaultDepth(x_display, screen);
924 +        
925 + #ifdef ENABLE_FBDEV_DGA
926 +        // Frame buffer name
927 +        char fb_name[20];
928 +        
929 +        // Could do fbdev dga ?
930 +        if ((fbdev_fd = open(FBDEVICE_FILE_NAME, O_RDWR)) != -1)
931 +                has_dga = true;
932 +        else
933 +                has_dga = false;
934 + #endif
935  
936 < #if ENABLE_DGA
936 > #ifdef ENABLE_XF86_DGA
937          // DGA available?
938 <        int dga_flags = 0;
939 <        XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
940 <        has_dga = dga_flags & XF86DGADirectPresent;
938 >        int dga_event_base, dga_error_base;
939 >        if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
940 >                int dga_flags = 0;
941 >                XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
942 >                has_dga = dga_flags & XF86DGADirectPresent;
943 >        } else
944 >                has_dga = false;
945   #endif
946  
947 + #ifdef ENABLE_XF86_VIDMODE
948 +        // VidMode available?
949 +        int vm_event_base, vm_error_base;
950 +        has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
951 +        if (has_vidmode)
952 +                XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
953 + #endif
954 +        
955          // Find black and white colors
956          XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
957          XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
# Line 539 | Line 1017 | bool VideoInit(bool classic)
1017          if (mode_str) {
1018                  if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
1019                          display_type = DISPLAY_WINDOW;
1020 <                else if (has_dga && strcmp(mode_str, "dga") == 0) {
1020 > #ifdef ENABLE_FBDEV_DGA
1021 >                else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
1022 > #else
1023 >                else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
1024 > #endif
1025                          display_type = DISPLAY_DGA;
1026 +                        if (width > DisplayWidth(x_display, screen))
1027 +                                width = DisplayWidth(x_display, screen);
1028 +                        if (height > DisplayHeight(x_display, screen))
1029 +                                height = DisplayHeight(x_display, screen);
1030 +                }
1031 +                if (width <= 0)
1032                          width = DisplayWidth(x_display, screen);
1033 +                if (height <= 0)
1034                          height = DisplayHeight(x_display, screen);
546                }
1035          }
1036  
1037          // Initialize according to display type
# Line 553 | Line 1041 | bool VideoInit(bool classic)
1041                                  return false;
1042                          break;
1043                  case DISPLAY_DGA:
1044 <                        if (!init_dga(width, height))
1044 > #ifdef ENABLE_FBDEV_DGA
1045 >                        if (!init_fbdev_dga(fb_name))
1046 > #else
1047 >                        if (!init_xf86_dga(width, height))
1048 > #endif
1049                                  return false;
1050                          break;
1051          }
1052  
1053 + #ifdef HAVE_PTHREADS
1054          // Lock down frame buffer
1055          pthread_mutex_lock(&frame_buffer_lock);
1056 + #endif
1057  
1058 < #if !REAL_ADDRESSING
1058 > #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1059          // Set variables for UAE memory mapping
1060          MacFrameBaseHost = the_buffer;
1061          MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
# Line 571 | Line 1065 | bool VideoInit(bool classic)
1065                  MacFrameLayout = FLAYOUT_NONE;
1066   #endif
1067  
1068 <        // Start redraw/input thread
1068 > #ifdef ENABLE_VOSF
1069 >        if (use_vosf) {
1070 >                // Initialize the mainBuffer structure
1071 >                if (!VideoInitBuffer()) {
1072 >                        // TODO: STR_VOSF_INIT_ERR ?
1073 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1074 >                return false;
1075 >                }
1076 >
1077 >                // Initialize the handler for SIGSEGV
1078 >                if (!Screen_fault_handler_init()) {
1079 >                        // TODO: STR_VOSF_INIT_ERR ?
1080 >                        ErrorAlert("Could not initialize Video on SEGV signals");
1081 >                        return false;
1082 >                }
1083 >        }
1084 > #endif
1085 >        
1086 >        // Initialize VideoRefresh function
1087 >        VideoRefreshInit();
1088 >        
1089          XSync(x_display, false);
1090 +
1091 + #ifdef HAVE_PTHREADS
1092 +        // Start redraw/input thread
1093          redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1094 <        if (!redraw_thread_active)
1094 >        if (!redraw_thread_active) {
1095                  printf("FATAL: cannot create redraw thread\n");
1096 <        return redraw_thread_active;
1096 >                return false;
1097 >        }
1098 > #endif
1099 >        return true;
1100   }
1101  
1102  
# Line 586 | Line 1106 | bool VideoInit(bool classic)
1106  
1107   void VideoExit(void)
1108   {
1109 + #ifdef HAVE_PTHREADS
1110          // Stop redraw thread
1111          if (redraw_thread_active) {
1112                  redraw_thread_cancel = true;
# Line 595 | Line 1116 | void VideoExit(void)
1116                  pthread_join(redraw_thread, NULL);
1117                  redraw_thread_active = false;
1118          }
1119 + #endif
1120  
1121 + #ifdef HAVE_PTHREADS
1122          // Unlock frame buffer
1123          pthread_mutex_unlock(&frame_buffer_lock);
1124 + #endif
1125  
1126          // Close window and server connection
1127          if (x_display != NULL) {
1128                  XSync(x_display, false);
1129  
1130 < #if ENABLE_DGA
1130 > #ifdef ENABLE_XF86_DGA
1131                  if (display_type == DISPLAY_DGA) {
1132                          XF86DGADirectVideo(x_display, screen, 0);
1133                          XUngrabPointer(x_display, CurrentTime);
# Line 611 | Line 1135 | void VideoExit(void)
1135                  }
1136   #endif
1137  
1138 <                if (the_buffer_copy) {
1139 <                        free(the_buffer_copy);
1140 <                        the_buffer_copy = NULL;
1138 > #ifdef ENABLE_XF86_VIDMODE
1139 >                if (has_vidmode && display_type == DISPLAY_DGA)
1140 >                        XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1141 > #endif
1142 >
1143 > #ifdef ENABLE_FBDEV_DGA
1144 >                if (display_type == DISPLAY_DGA) {
1145 >                        XUngrabPointer(x_display, CurrentTime);
1146 >                        XUngrabKeyboard(x_display, CurrentTime);
1147 >                        close(fbdev_fd);
1148                  }
1149 + #endif
1150  
1151                  XFlush(x_display);
1152                  XSync(x_display, false);
# Line 622 | Line 1154 | void VideoExit(void)
1154                          XFreeColormap(x_display, cmap[0]);
1155                          XFreeColormap(x_display, cmap[1]);
1156                  }
1157 +                
1158 +                if (!use_vosf) {
1159 +                        if (the_buffer) {
1160 +                                free(the_buffer);
1161 +                                the_buffer = NULL;
1162 +                        }
1163 +
1164 +                        if (!have_shm && the_buffer_copy) {
1165 +                                free(the_buffer_copy);
1166 +                                the_buffer_copy = NULL;
1167 +                        }
1168 +                }
1169 + #ifdef ENABLE_VOSF
1170 +                else {
1171 +                        if (the_buffer != (uint8 *)MAP_FAILED) {
1172 +                                munmap((caddr_t)the_buffer, the_buffer_size);
1173 +                                the_buffer = 0;
1174 +                        }
1175 +                        
1176 +                        if (the_buffer_copy != (uint8 *)MAP_FAILED) {
1177 +                                munmap((caddr_t)the_buffer_copy, the_buffer_size);
1178 +                                the_buffer_copy = 0;
1179 +                        }
1180 +                }
1181 + #endif
1182          }
1183 +        
1184 + #ifdef ENABLE_VOSF
1185 +        if (use_vosf) {
1186 +                // Clear mainBuffer data
1187 +                if (mainBuffer.pageInfo) {
1188 +                        free(mainBuffer.pageInfo);
1189 +                        mainBuffer.pageInfo = 0;
1190 +                }
1191 +
1192 +                if (mainBuffer.dirtyPages) {
1193 +                        free(mainBuffer.dirtyPages);
1194 +                        mainBuffer.dirtyPages = 0;
1195 +                }
1196 +        }
1197 +
1198 +        // Close /dev/zero
1199 +        if (zero_fd > 0)
1200 +                close(zero_fd);
1201 + #endif
1202   }
1203  
1204  
# Line 648 | Line 1224 | void VideoInterrupt(void)
1224          if (emerg_quit)
1225                  QuitEmulator();
1226  
1227 + #ifdef HAVE_PTHREADS
1228          // Temporarily give up frame buffer lock (this is the point where
1229          // we are suspended when the user presses Ctrl-Tab)
1230          pthread_mutex_unlock(&frame_buffer_lock);
1231          pthread_mutex_lock(&frame_buffer_lock);
1232 + #endif
1233   }
1234  
1235  
# Line 661 | Line 1239 | void VideoInterrupt(void)
1239  
1240   void video_set_palette(uint8 *pal)
1241   {
1242 + #ifdef HAVE_PTHREADS
1243          pthread_mutex_lock(&palette_lock);
1244 + #endif
1245  
1246          // Convert colors to XColor array
1247          for (int i=0; i<256; i++) {
# Line 675 | Line 1255 | void video_set_palette(uint8 *pal)
1255          // Tell redraw thread to change palette
1256          palette_changed = true;
1257  
1258 + #ifdef HAVE_PTHREADS
1259          pthread_mutex_unlock(&palette_lock);
1260 + #endif
1261   }
1262  
1263  
# Line 683 | Line 1265 | void video_set_palette(uint8 *pal)
1265   *  Suspend/resume emulator
1266   */
1267  
1268 < #if ENABLE_DGA
1268 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1269   static void suspend_emul(void)
1270   {
1271          if (display_type == DISPLAY_DGA) {
# Line 691 | Line 1273 | static void suspend_emul(void)
1273                  ADBKeyUp(0x36);
1274                  ctrl_down = false;
1275  
1276 + #ifdef HAVE_PTHREADS
1277                  // Lock frame buffer (this will stop the MacOS thread)
1278                  pthread_mutex_lock(&frame_buffer_lock);
1279 + #endif
1280  
1281                  // Save frame buffer
1282                  fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
# Line 700 | Line 1284 | static void suspend_emul(void)
1284                          memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
1285  
1286                  // Close full screen display
1287 + #ifdef ENABLE_XF86_DGA
1288                  XF86DGADirectVideo(x_display, screen, 0);
1289 + #endif
1290                  XUngrabPointer(x_display, CurrentTime);
1291                  XUngrabKeyboard(x_display, CurrentTime);
1292                  XUnmapWindow(x_display, the_win);
# Line 714 | Line 1300 | static void suspend_emul(void)
1300                  wattr.backing_store = Always;
1301                  wattr.backing_planes = xdepth;
1302                  wattr.colormap = DefaultColormap(x_display, screen);
1303 +                
1304                  XSync(x_display, false);
1305                  suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1306                          InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
# Line 738 | Line 1325 | static void resume_emul(void)
1325          XSync(x_display, false);
1326          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1327          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1328 + #ifdef ENABLE_XF86_DGA
1329          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1330          XF86DGASetViewPort(x_display, screen, 0, 0);
1331 + #endif
1332          XSync(x_display, false);
1333 <
1333 >        
1334 >        // the_buffer already contains the data to restore. i.e. since a temporary
1335 >        // frame buffer is used when VOSF is actually used, fb_save is therefore
1336 >        // not necessary.
1337 > #ifdef ENABLE_VOSF
1338 >        if (use_vosf) {
1339 > #ifdef HAVE_PTHREADS
1340 >                pthread_mutex_lock(&Screen_draw_lock);
1341 > #endif
1342 >                PFLAG_SET_ALL;
1343 > #ifdef HAVE_PTHREADS
1344 >                pthread_mutex_unlock(&Screen_draw_lock);
1345 > #endif
1346 >                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1347 >        }
1348 > #endif
1349 >        
1350          // Restore frame buffer
1351          if (fb_save) {
1352 + #ifdef ENABLE_VOSF
1353 +                // Don't copy fb_save to the temporary frame buffer in VOSF mode
1354 +                if (!use_vosf)
1355 + #endif
1356                  memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1357                  free(fb_save);
1358                  fb_save = NULL;
1359          }
1360 +        
1361 + #ifdef ENABLE_XF86_DGA
1362          if (depth == 8)
1363                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1364 + #endif
1365  
1366 + #ifdef HAVE_PTHREADS
1367          // Unlock frame buffer (and continue MacOS thread)
1368          pthread_mutex_unlock(&frame_buffer_lock);
1369          emul_suspended = false;
1370 + #endif
1371   }
1372   #endif
1373  
# Line 815 | Line 1429 | static int kc_decode(KeySym ks)
1429                  case XK_period: case XK_greater: return 0x2f;
1430                  case XK_slash: case XK_question: return 0x2c;
1431  
1432 < #if ENABLE_DGA
1432 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1433                  case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1434   #else
1435                  case XK_Tab: return 0x30;
# Line 941 | Line 1555 | static void handle_events(void)
1555                                  unsigned int button = ((XButtonEvent *)&event)->button;
1556                                  if (button < 4)
1557                                          ADBMouseDown(button - 1);
1558 +                                else if (button < 6) {  // Wheel mouse
1559 +                                        if (mouse_wheel_mode == 0) {
1560 +                                                int key = (button == 5) ? 0x79 : 0x74;  // Page up/down
1561 +                                                ADBKeyDown(key);
1562 +                                                ADBKeyUp(key);
1563 +                                        } else {
1564 +                                                int key = (button == 5) ? 0x3d : 0x3e;  // Cursor up/down
1565 +                                                for(int i=0; i<mouse_wheel_lines; i++) {
1566 +                                                        ADBKeyDown(key);
1567 +                                                        ADBKeyUp(key);
1568 +                                                }
1569 +                                        }
1570 +                                }
1571                                  break;
1572                          }
1573                          case ButtonRelease: {
# Line 968 | Line 1595 | static void handle_events(void)
1595                                          code = event2keycode((XKeyEvent *)&event);
1596                                  if (code != -1) {
1597                                          if (!emul_suspended) {
1598 <                                                ADBKeyDown(code);
1598 >                                                if (code == 0x39) {     // Caps Lock pressed
1599 >                                                        if (caps_on) {
1600 >                                                                ADBKeyUp(code);
1601 >                                                                caps_on = false;
1602 >                                                        } else {
1603 >                                                                ADBKeyDown(code);
1604 >                                                                caps_on = true;
1605 >                                                        }
1606 >                                                } else
1607 >                                                        ADBKeyDown(code);
1608                                                  if (code == 0x36)
1609                                                          ctrl_down = true;
1610                                          } else {
1611 < #if ENABLE_DGA
1611 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1612                                                  if (code == 0x31)
1613                                                          resume_emul();  // Space wakes us up
1614   #endif
# Line 987 | Line 1623 | static void handle_events(void)
1623                                          code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1624                                  } else
1625                                          code = event2keycode((XKeyEvent *)&event);
1626 <                                if (code != -1) {
1626 >                                if (code != -1 && code != 0x39) {       // Don't propagate Caps Lock releases
1627                                          ADBKeyUp(code);
1628                                          if (code == 0x36)
1629                                                  ctrl_down = false;
# Line 997 | Line 1633 | static void handle_events(void)
1633  
1634                          // Hidden parts exposed, force complete refresh of window
1635                          case Expose:
1636 <                                if (display_type == DISPLAY_WINDOW)
1637 <                                        memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1636 >                                if (display_type == DISPLAY_WINDOW) {
1637 > #ifdef ENABLE_VOSF
1638 >                                        if (use_vosf) {                 // VOSF refresh
1639 > #ifdef HAVE_PTHREADS
1640 >                                                pthread_mutex_lock(&Screen_draw_lock);
1641 > #endif
1642 >                                                PFLAG_SET_ALL;
1643 > #ifdef HAVE_PTHREADS
1644 >                                                pthread_mutex_unlock(&Screen_draw_lock);
1645 > #endif
1646 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1647 >                                        }
1648 >                                        else
1649 > #endif
1650 >                                        if (frame_skip == 0) {  // Dynamic refresh
1651 >                                                int x1, y1;
1652 >                                                for (y1=0; y1<16; y1++)
1653 >                                                for (x1=0; x1<16; x1++)
1654 >                                                        updt_box[x1][y1] = true;
1655 >                                                nr_boxes = 16 * 16;
1656 >                                        } else                                  // Static refresh
1657 >                                                memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1658 >                                }
1659 >                                break;
1660 >
1661 >                        case FocusIn:
1662 >                        case FocusOut:
1663                                  break;
1664                  }
1665          }
# Line 1009 | Line 1670 | static void handle_events(void)
1670   *  Window display update
1671   */
1672  
1673 < static void update_display(void)
1673 > // Dynamic display update (variable frame rate for each box)
1674 > static void update_display_dynamic(int ticker)
1675   {
1676 <        // In classic mode, copy the frame buffer from Mac RAM
1677 <        if (classic_mode)
1678 <                memcpy(the_buffer, Mac2HostAddr(0x3fa700), VideoMonitor.bytes_per_row * VideoMonitor.y);
1676 >        int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1677 >        int xil = 0;
1678 >        int rxm = 0, rxmo = 0;
1679 >        int bytes_per_row = VideoMonitor.bytes_per_row;
1680 >        int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1681 >        int rx = VideoMonitor.bytes_per_row / 16;
1682 >        int ry = VideoMonitor.y / 16;
1683 >        int max_box;
1684 >
1685 >        y2s = sm_uptd[ticker % 8];
1686 >        y2a = 8;
1687 >        for (i = 0; i < 6; i++)
1688 >                if (ticker % (2 << i))
1689 >                        break;
1690 >        max_box = sm_no_boxes[i];
1691  
1692 +        if (y2a) {
1693 +                for (y1=0; y1<16; y1++) {
1694 +                        for (y2=y2s; y2 < ry; y2 += y2a) {
1695 +                                i = ((y1 * ry) + y2) * bytes_per_row;
1696 +                                for (x1=0; x1<16; x1++, i += rx) {
1697 +                                        if (updt_box[x1][y1] == false) {
1698 +                                                if (memcmp(&the_buffer_copy[i], &the_buffer[i], rx)) {
1699 +                                                        updt_box[x1][y1] = true;
1700 +                                                        nr_boxes++;
1701 +                                                }
1702 +                                        }
1703 +                                }
1704 +                        }
1705 +                }
1706 +        }
1707 +
1708 +        if ((nr_boxes <= max_box) && (nr_boxes)) {
1709 +                for (y1=0; y1<16; y1++) {
1710 +                        for (x1=0; x1<16; x1++) {
1711 +                                if (updt_box[x1][y1] == true) {
1712 +                                        if (rxm == 0)
1713 +                                                xm = x1;
1714 +                                        rxm += rx;
1715 +                                        updt_box[x1][y1] = false;
1716 +                                }
1717 +                                if (((updt_box[x1+1][y1] == false) || (x1 == 15)) && (rxm)) {
1718 +                                        if ((rxmo != rxm) || (xmo != xm) || (yo != y1 - 1)) {
1719 +                                                if (rxmo) {
1720 +                                                        xi = xmo * rx;
1721 +                                                        yi = ymo * ry;
1722 +                                                        xil = rxmo;
1723 +                                                        yil = (yo - ymo +1) * ry;
1724 +                                                }
1725 +                                                rxmo = rxm;
1726 +                                                xmo = xm;
1727 +                                                ymo = y1;
1728 +                                        }
1729 +                                        rxm = 0;
1730 +                                        yo = y1;
1731 +                                }      
1732 +                                if (xil) {
1733 +                                        i = (yi * bytes_per_row) + xi;
1734 +                                        for (y2=0; y2 < yil; y2++, i += bytes_per_row)
1735 +                                                memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
1736 +                                        if (depth == 1) {
1737 +                                                if (have_shm)
1738 +                                                        XShmPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
1739 +                                                else
1740 +                                                        XPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil);
1741 +                                        } else {
1742 +                                                if (have_shm)
1743 +                                                        XShmPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0);
1744 +                                                else
1745 +                                                        XPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil);
1746 +                                        }
1747 +                                        xil = 0;
1748 +                                }
1749 +                                if ((x1 == 15) && (y1 == 15) && (rxmo)) {
1750 +                                        x1--;
1751 +                                        xi = xmo * rx;
1752 +                                        yi = ymo * ry;
1753 +                                        xil = rxmo;
1754 +                                        yil = (yo - ymo +1) * ry;
1755 +                                        rxmo = 0;
1756 +                                }
1757 +                        }
1758 +                }
1759 +                nr_boxes = 0;
1760 +        }
1761 + }
1762 +
1763 + // Static display update (fixed frame rate, but incremental)
1764 + static void update_display_static(void)
1765 + {
1766          // Incremental update code
1767          int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1768          int bytes_per_row = VideoMonitor.bytes_per_row;
# Line 1041 | Line 1789 | static void update_display(void)
1789          // Check for first column from left and first column from right that have changed
1790          if (high) {
1791                  if (depth == 1) {
1792 <                        x1 = VideoMonitor.x;
1792 >                        x1 = VideoMonitor.x - 1;
1793                          for (j=y1; j<=y2; j++) {
1794                                  p = &the_buffer[j * bytes_per_row];
1795                                  p2 = &the_buffer_copy[j * bytes_per_row];
# Line 1050 | Line 1798 | static void update_display(void)
1798                                                  x1 = i << 3;
1799                                                  break;
1800                                          }
1801 <                                        p++;
1054 <                                        p2++;
1801 >                                        p++; p2++;
1802                                  }
1803                          }
1804                          x2 = x1;
# Line 1061 | Line 1808 | static void update_display(void)
1808                                  p += bytes_per_row;
1809                                  p2 += bytes_per_row;
1810                                  for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1811 <                                        p--;
1065 <                                        p2--;
1811 >                                        p--; p2--;
1812                                          if (*p != *p2) {
1813 <                                                x2 = i << 3;
1813 >                                                x2 = (i << 3) + 7;
1814                                                  break;
1815                                          }
1816                                  }
1817                          }
1818 <                        wide = x2 - x1;
1818 >                        wide = x2 - x1 + 1;
1819  
1820                          // Update copy of the_buffer
1821                          if (high && wide) {
1822                                  for (j=y1; j<=y2; j++) {
1823                                          i = j * bytes_per_row + (x1 >> 3);
1824 <                                        memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
1824 >                                        memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3);
1825                                  }
1826                          }
1827  
# Line 1084 | Line 1830 | static void update_display(void)
1830                          for (j=y1; j<=y2; j++) {
1831                                  p = &the_buffer[j * bytes_per_row];
1832                                  p2 = &the_buffer_copy[j * bytes_per_row];
1833 <                                for (i=0; i<x1; i++) {
1834 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1835 <                                                x1 = i;
1833 >                                for (i=0; i<x1*bytes_per_pixel; i++) {
1834 >                                        if (*p != *p2) {
1835 >                                                x1 = i / bytes_per_pixel;
1836                                                  break;
1837                                          }
1838 <                                        p += bytes_per_pixel;
1093 <                                        p2 += bytes_per_pixel;
1838 >                                        p++; p2++;
1839                                  }
1840                          }
1841                          x2 = x1;
# Line 1099 | Line 1844 | static void update_display(void)
1844                                  p2 = &the_buffer_copy[j * bytes_per_row];
1845                                  p += bytes_per_row;
1846                                  p2 += bytes_per_row;
1847 <                                for (i=VideoMonitor.x; i>x2; i--) {
1848 <                                        p -= bytes_per_pixel;
1849 <                                        p2 -= bytes_per_pixel;
1850 <                                        if (memcmp(p, p2, bytes_per_pixel)) {
1851 <                                                x2 = i;
1847 >                                for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1848 >                                        p--;
1849 >                                        p2--;
1850 >                                        if (*p != *p2) {
1851 >                                                x2 = i / bytes_per_pixel;
1852                                                  break;
1853                                          }
1854                                  }
# Line 1114 | Line 1859 | static void update_display(void)
1859                          if (high && wide) {
1860                                  for (j=y1; j<=y2; j++) {
1861                                          i = j * bytes_per_row + x1 * bytes_per_pixel;
1862 <                                        memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
1862 >                                        memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1863                                  }
1864                          }
1865                  }
# Line 1127 | Line 1872 | static void update_display(void)
1872                  else
1873                          XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1874          }
1875 + }
1876 +
1877 +
1878 + /*
1879 + *      Screen refresh functions
1880 + */
1881  
1882 <        // Has the Mac started? (cursor data is not valid otherwise)
1883 <        if (HasMacStarted()) {
1882 > // The specialisations hereunder are meant to enable VOSF with DGA in direct
1883 > // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1884 > // not get mapped correctly with respect to the predetermined host frame
1885 > // buffer base address.
1886 > //
1887 > // Hmm, in other words, when in direct addressing mode and DGA is requested,
1888 > // we first try to "triple allocate" the address spaces according to the real
1889 > // host frame buffer address. Then, if it fails, we will use a temporary
1890 > // frame buffer thus making the real host frame buffer updated when pages
1891 > // of the temp frame buffer are altered.
1892 > //
1893 > // As a side effect, a little speed gain in screen updates could be noticed
1894 > // for other modes than DGA.
1895 > //
1896 > // The following two functions below are inline so that a clever compiler
1897 > // could specialise the code according to the current screen depth and
1898 > // display type. A more clever compiler would the job by itself though...
1899 > // (update_display_vosf is inlined as well)
1900  
1901 <                // Set new cursor image if it was changed
1902 <                if (memcmp(the_cursor, Mac2HostAddr(0x844), 64)) {
1903 <                        memcpy(the_cursor, Mac2HostAddr(0x844), 64);
1904 <                        memcpy(cursor_image->data, the_cursor, 32);
1905 <                        memcpy(cursor_mask_image->data, the_cursor+32, 32);
1906 <                        XFreeCursor(x_display, mac_cursor);
1907 <                        XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
1908 <                        XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
1909 <                        mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, ReadMacInt8(0x885), ReadMacInt8(0x887));
1910 <                        XDefineCursor(x_display, the_win, mac_cursor);
1901 > static inline void possibly_quit_dga_mode()
1902 > {
1903 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1904 >        // Quit DGA mode if requested
1905 >        if (quit_full_screen) {
1906 >                quit_full_screen = false;
1907 > #ifdef ENABLE_XF86_DGA
1908 >                XF86DGADirectVideo(x_display, screen, 0);
1909 > #endif
1910 >                XUngrabPointer(x_display, CurrentTime);
1911 >                XUngrabKeyboard(x_display, CurrentTime);
1912 >                XUnmapWindow(x_display, the_win);
1913 >                XSync(x_display, false);
1914 >        }
1915 > #endif
1916 > }
1917 >
1918 > static inline void handle_palette_changes(int depth, int display_type)
1919 > {
1920 > #ifdef HAVE_PTHREADS
1921 >        pthread_mutex_lock(&palette_lock);
1922 > #endif
1923 >        if (palette_changed) {
1924 >                palette_changed = false;
1925 >                if (depth == 8) {
1926 >                        XStoreColors(x_display, cmap[0], palette, 256);
1927 >                        XStoreColors(x_display, cmap[1], palette, 256);
1928 >                                
1929 > #ifdef ENABLE_XF86_DGA
1930 >                        if (display_type == DISPLAY_DGA) {
1931 >                                current_dga_cmap ^= 1;
1932 >                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1933 >                        }
1934 > #endif
1935                  }
1936          }
1937 + #ifdef HAVE_PTHREADS
1938 +        pthread_mutex_unlock(&palette_lock);
1939 + #endif
1940 + }
1941 +
1942 + static void video_refresh_dga(void)
1943 + {
1944 +        // Quit DGA mode if requested
1945 +        possibly_quit_dga_mode();
1946 +        
1947 +        // Handle X events
1948 +        handle_events();
1949 +        
1950 +        // Handle palette changes
1951 +        handle_palette_changes(depth, DISPLAY_DGA);
1952 + }
1953 +
1954 + #ifdef ENABLE_VOSF
1955 + #if REAL_ADDRESSING || DIRECT_ADDRESSING
1956 + static void video_refresh_dga_vosf(void)
1957 + {
1958 +        // Quit DGA mode if requested
1959 +        possibly_quit_dga_mode();
1960 +        
1961 +        // Handle X events
1962 +        handle_events();
1963 +        
1964 +        // Handle palette changes
1965 +        handle_palette_changes(depth, DISPLAY_DGA);
1966 +        
1967 +        // Update display (VOSF variant)
1968 +        static int tick_counter = 0;
1969 +        if (++tick_counter >= frame_skip) {
1970 +                tick_counter = 0;
1971 + #ifdef HAVE_PTHREADS
1972 +                pthread_mutex_lock(&Screen_draw_lock);
1973 + #endif
1974 +                update_display_dga_vosf();
1975 + #ifdef HAVE_PTHREADS
1976 +                pthread_mutex_unlock(&Screen_draw_lock);
1977 + #endif
1978 +        }
1979 + }
1980 + #endif
1981 +
1982 + static void video_refresh_window_vosf(void)
1983 + {
1984 +        // Quit DGA mode if requested
1985 +        possibly_quit_dga_mode();
1986 +        
1987 +        // Handle X events
1988 +        handle_events();
1989 +        
1990 +        // Handle palette changes
1991 +        handle_palette_changes(depth, DISPLAY_WINDOW);
1992 +        
1993 +        // Update display (VOSF variant)
1994 +        static int tick_counter = 0;
1995 +        if (++tick_counter >= frame_skip) {
1996 +                tick_counter = 0;
1997 + #ifdef HAVE_PTHREADS
1998 +                pthread_mutex_lock(&Screen_draw_lock);
1999 + #endif
2000 +                update_display_window_vosf();
2001 + #ifdef HAVE_PTHREADS
2002 +                pthread_mutex_unlock(&Screen_draw_lock);
2003 + #endif
2004 +        }
2005 + }
2006 + #endif // def ENABLE_VOSF
2007 +
2008 + static void video_refresh_window_static(void)
2009 + {
2010 +        // Handle X events
2011 +        handle_events();
2012 +        
2013 +        // Handle_palette changes
2014 +        handle_palette_changes(depth, DISPLAY_WINDOW);
2015 +        
2016 +        // Update display (static variant)
2017 +        static int tick_counter = 0;
2018 +        if (++tick_counter >= frame_skip) {
2019 +                tick_counter = 0;
2020 +                update_display_static();
2021 +        }
2022 + }
2023 +
2024 + static void video_refresh_window_dynamic(void)
2025 + {
2026 +        // Handle X events
2027 +        handle_events();
2028 +        
2029 +        // Handle_palette changes
2030 +        handle_palette_changes(depth, DISPLAY_WINDOW);
2031 +        
2032 +        // Update display (dynamic variant)
2033 +        static int tick_counter = 0;
2034 +        tick_counter++;
2035 +        update_display_dynamic(tick_counter);
2036   }
2037  
2038  
# Line 1150 | Line 2040 | static void update_display(void)
2040   *  Thread for screen refresh, input handling etc.
2041   */
2042  
2043 < static void *redraw_func(void *arg)
2043 > void VideoRefreshInit(void)
2044   {
2045 <        int tick_counter = 0;
2045 >        // TODO: set up specialised 8bpp VideoRefresh handlers ?
2046 >        if (display_type == DISPLAY_DGA) {
2047 > #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2048 >                if (use_vosf)
2049 >                        video_refresh = video_refresh_dga_vosf;
2050 >                else
2051 > #endif
2052 >                        video_refresh = video_refresh_dga;
2053 >        }
2054 >        else {
2055 > #ifdef ENABLE_VOSF
2056 >                if (use_vosf)
2057 >                        video_refresh = video_refresh_window_vosf;
2058 >                else
2059 > #endif
2060 >                if (frame_skip == 0)
2061 >                        video_refresh = video_refresh_window_dynamic;
2062 >                else
2063 >                        video_refresh = video_refresh_window_static;
2064 >        }
2065 > }
2066  
2067 <        while (!redraw_thread_cancel) {
2067 > void VideoRefresh(void)
2068 > {
2069 >        // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2070 >        video_refresh();
2071 > }
2072  
2073 <                // Wait
2074 < #ifdef HAVE_NANOSLEEP
2075 <                struct timespec req = {0, 16666667};
2076 <                nanosleep(&req, NULL);
2077 < #else
2078 <                usleep(16667);
2073 > #if 0
2074 > void VideoRefresh(void)
2075 > {
2076 > #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2077 >        // Quit DGA mode if requested
2078 >        if (quit_full_screen) {
2079 >                quit_full_screen = false;
2080 >                if (display_type == DISPLAY_DGA) {
2081 > #ifdef ENABLE_XF86_DGA
2082 >                        XF86DGADirectVideo(x_display, screen, 0);
2083   #endif
2084 <
2085 < #if ENABLE_DGA
2086 <                // Quit DGA mode if requested
2087 <                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 <                        }
2084 >                        XUngrabPointer(x_display, CurrentTime);
2085 >                        XUngrabKeyboard(x_display, CurrentTime);
2086 >                        XUnmapWindow(x_display, the_win);
2087 >                        XSync(x_display, false);
2088                  }
2089 +        }
2090   #endif
2091  
2092 <                // Handle X events
2093 <                handle_events();
2092 >        // Handle X events
2093 >        handle_events();
2094  
2095 <                // Handle palette changes
2096 <                pthread_mutex_lock(&palette_lock);
2097 <                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 <                                }
2095 >        // Handle palette changes
2096 > #ifdef HAVE_PTHREADS
2097 >        pthread_mutex_lock(&palette_lock);
2098   #endif
2099 +        if (palette_changed) {
2100 +                palette_changed = false;
2101 +                if (depth == 8) {
2102 +                        XStoreColors(x_display, cmap[0], palette, 256);
2103 +                        XStoreColors(x_display, cmap[1], palette, 256);
2104 +                                
2105 + #ifdef ENABLE_XF86_DGA
2106 +                        if (display_type == DISPLAY_DGA) {
2107 +                                current_dga_cmap ^= 1;
2108 +                                XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2109                          }
2110 + #endif
2111                  }
2112 <                pthread_mutex_unlock(&palette_lock);
2112 >        }
2113 > #ifdef HAVE_PTHREADS
2114 >        pthread_mutex_unlock(&palette_lock);
2115 > #endif
2116  
2117 <                // In window mode, update display and mouse pointer
2118 <                if (display_type == DISPLAY_WINDOW) {
2119 <                        tick_counter++;
2120 <                        if (tick_counter >= frame_skip) {
2121 <                                tick_counter = 0;
2122 <                                update_display();
2123 <                        }
2117 >        // In window mode, update display
2118 >        static int tick_counter = 0;
2119 >        if (display_type == DISPLAY_WINDOW) {
2120 >                tick_counter++;
2121 >                if (frame_skip == 0)
2122 >                        update_display_dynamic(tick_counter);
2123 >                else if (tick_counter >= frame_skip) {
2124 >                        tick_counter = 0;
2125 >                        update_display_static();
2126                  }
2127          }
2128 + }
2129 + #endif
2130 +
2131 + #ifdef HAVE_PTHREADS
2132 + static void *redraw_func(void *arg)
2133 + {
2134 +        uint64 start = GetTicks_usec();
2135 +        int64 ticks = 0;
2136 +        uint64 next = GetTicks_usec();
2137 +        while (!redraw_thread_cancel) {
2138 + //              VideoRefresh();
2139 +                video_refresh();
2140 +                next += 16667;
2141 +                int64 delay = next - GetTicks_usec();
2142 +                if (delay > 0)
2143 +                        Delay_usec(delay);
2144 +                else if (delay < -16667)
2145 +                        next = GetTicks_usec();
2146 +                ticks++;
2147 +        }
2148 +        uint64 end = GetTicks_usec();
2149 +        printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2150          return NULL;
2151   }
2152 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines