/[cebix]/SheepShaver/src/Unix/video_x.cpp
ViewVC logotype

Diff of /SheepShaver/src/Unix/video_x.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by cebix, Mon Feb 4 16:58:13 2002 UTC revision 1.25 by gbeauche, Sat Jun 5 06:28:21 2004 UTC
# Line 1  Line 1 
1  /*  /*
2   *  video_x.cpp - Video/graphics emulation, X11 specific stuff   *  video_x.cpp - Video/graphics emulation, X11 specific stuff
3   *   *
4   *  SheepShaver (C) 1997-2002 Marc Hellwig and Christian Bauer   *  SheepShaver (C) 1997-2004 Marc Hellwig and Christian Bauer
5   *   *
6   *  This program is free software; you can redistribute it and/or modify   *  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   *  it under the terms of the GNU General Public License as published by
# Line 18  Line 18 
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */   */
20    
21    #include "sysdeps.h"
22    
23  #include <X11/Xlib.h>  #include <X11/Xlib.h>
24  #include <X11/Xutil.h>  #include <X11/Xutil.h>
25  #include <X11/keysym.h>  #include <X11/keysym.h>
26  #include <X11/extensions/XShm.h>  #include <X11/extensions/XShm.h>
27  #include <sys/ipc.h>  #include <sys/ipc.h>
28  #include <sys/shm.h>  #include <sys/shm.h>
29    #include <errno.h>
30  #include <pthread.h>  #include <pthread.h>
31    
32  #include "sysdeps.h"  #include <algorithm>
33    
34    #ifdef ENABLE_XF86_DGA
35    # include <X11/extensions/xf86dga.h>
36    #endif
37    
38    #ifdef ENABLE_XF86_VIDMODE
39    # include <X11/extensions/xf86vmode.h>
40    #endif
41    
42  #include "main.h"  #include "main.h"
43  #include "adb.h"  #include "adb.h"
44  #include "prefs.h"  #include "prefs.h"
# Line 38  Line 50 
50  #define DEBUG 0  #define DEBUG 0
51  #include "debug.h"  #include "debug.h"
52    
53  #ifdef ENABLE_XF86_DGA  #ifndef NO_STD_NAMESPACE
54  #include <X11/extensions/xf86dga.h>  using std::sort;
55  #endif  #endif
56    
 #ifdef ENABLE_XF86_VIDMODE  
 #include <X11/extensions/xf86vmode.h>  
 #endif  
57    
58    // Constants
59    const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
60    static const bool hw_mac_cursor_accl = true;    // Flag: Enable MacOS to X11 copy of cursor?
61    
62  // Global variables  // Global variables
63  static int32 frame_skip;  static int32 frame_skip;
64    static int16 mouse_wheel_mode;
65    static int16 mouse_wheel_lines;
66  static bool redraw_thread_active = false;       // Flag: Redraw thread installed  static bool redraw_thread_active = false;       // Flag: Redraw thread installed
67    static pthread_attr_t redraw_thread_attr;       // Redraw thread attributes
68  static pthread_t redraw_thread;                         // Redraw thread  static pthread_t redraw_thread;                         // Redraw thread
69    
70    static bool local_X11;                                          // Flag: X server running on local machine?
71  static volatile bool thread_stop_req = false;  static volatile bool thread_stop_req = false;
72  static volatile bool thread_stop_ack = false;   // Acknowledge for thread_stop_req  static volatile bool thread_stop_ack = false;   // Acknowledge for thread_stop_req
73    
74  static bool has_dga = false;                            // Flag: Video DGA capable  static bool has_dga = false;                            // Flag: Video DGA capable
75  static bool has_vidmode = false;                        // Flag: VidMode extension available  static bool has_vidmode = false;                        // Flag: VidMode extension available
76    
77    #ifdef ENABLE_VOSF
78    static bool use_vosf = true;                            // Flag: VOSF enabled
79    #else
80    static const bool use_vosf = false;                     // VOSF not possible
81    #endif
82    
83  static bool palette_changed = false;            // Flag: Palette changed, redraw thread must update palette  static bool palette_changed = false;            // Flag: Palette changed, redraw thread must update palette
84  static bool ctrl_down = false;                          // Flag: Ctrl key pressed  static bool ctrl_down = false;                          // Flag: Ctrl key pressed
85  static bool quit_full_screen = false;           // Flag: DGA close requested from redraw thread  static bool quit_full_screen = false;           // Flag: DGA close requested from redraw thread
# Line 67  static bool emerg_quit = false; // Fl Line 89  static bool emerg_quit = false; // Fl
89  static bool emul_suspended = false;                     // Flag: emulator suspended  static bool emul_suspended = false;                     // Flag: emulator suspended
90  static Window suspend_win;                                      // "Suspend" window  static Window suspend_win;                                      // "Suspend" window
91  static void *fb_save = NULL;                            // Saved frame buffer for suspend  static void *fb_save = NULL;                            // Saved frame buffer for suspend
92    static bool use_keycodes = false;                       // Flag: Use keycodes rather than keysyms
93    static int keycode_table[256];                          // X keycode -> Mac keycode translation table
94    
95  // X11 variables  // X11 variables
96  static int screen;                                                      // Screen number  static int screen;                                                      // Screen number
97  static int xdepth;                                                      // Depth of X screen  static int xdepth;                                                      // Depth of X screen
98  static int depth;                                                       // Depth of Mac frame buffer  static int depth;                                                       // Depth of Mac frame buffer
99  static Window rootwin, the_win;                         // Root window and our window  static Window rootwin, the_win;                         // Root window and our window
100    static int num_depths = 0;                                      // Number of available X depths
101    static int *avail_depths = NULL;                        // List of available X depths
102  static XVisualInfo visualInfo;  static XVisualInfo visualInfo;
103  static Visual *vis;  static Visual *vis;
104    static int color_class;
105    static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
106  static Colormap cmap[2];                                        // Two colormaps (DGA) for 8-bit mode  static Colormap cmap[2];                                        // Two colormaps (DGA) for 8-bit mode
107    static XColor x_palette[256];                           // Color palette to be used as CLUT and gamma table
108    
109  static XColor black, white;  static XColor black, white;
110  static unsigned long black_pixel, white_pixel;  static unsigned long black_pixel, white_pixel;
111  static int eventmask;  static int eventmask;
112  static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask;  static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
113  static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;  static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
114    
115  // Variables for window mode  // Variables for window mode
116  static GC the_gc;  static GC the_gc;
# Line 92  static Cursor mac_cursor; Line 122  static Cursor mac_cursor;
122  static GC cursor_gc, cursor_mask_gc;  static GC cursor_gc, cursor_mask_gc;
123  static bool cursor_changed = false;                     // Flag: Cursor changed, window_func must update cursor  static bool cursor_changed = false;                     // Flag: Cursor changed, window_func must update cursor
124  static bool have_shm = false;                           // Flag: SHM present and usable  static bool have_shm = false;                           // Flag: SHM present and usable
125  static uint8 *the_buffer;                                       // Pointer to Mac frame buffer  static uint8 *the_buffer = NULL;                        // Pointer to Mac frame buffer
126  static uint8 *the_buffer_copy = NULL;           // Copy of Mac frame buffer  static uint8 *the_buffer_copy = NULL;           // Copy of Mac frame buffer
127    static uint32 the_buffer_size;                          // Size of allocated the_buffer
128    
129  // Variables for DGA mode  // Variables for DGA mode
 static char *dga_screen_base;  
 static int dga_fb_width;  
130  static int current_dga_cmap;  static int current_dga_cmap;
131    
132  #ifdef ENABLE_XF86_VIDMODE  #ifdef ENABLE_XF86_VIDMODE
# Line 106  static XF86VidModeModeInfo **x_video_mod Line 135  static XF86VidModeModeInfo **x_video_mod
135  static int num_x_video_modes;  static int num_x_video_modes;
136  #endif  #endif
137    
138    // Mutex to protect palette
139    #ifdef HAVE_SPINLOCKS
140    static spinlock_t x_palette_lock = SPIN_LOCK_UNLOCKED;
141    #define LOCK_PALETTE spin_lock(&x_palette_lock)
142    #define UNLOCK_PALETTE spin_unlock(&x_palette_lock)
143    #elif defined(HAVE_PTHREADS)
144    static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER;
145    #define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock)
146    #define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock)
147    #else
148    #define LOCK_PALETTE
149    #define UNLOCK_PALETTE
150    #endif
151    
152    
153  // Prototypes  // Prototypes
154  static void *redraw_func(void *arg);  static void *redraw_func(void *arg);
155    
156    
157  // From main_linux.cpp  // From main_unix.cpp
158    extern char *x_display_name;
159  extern Display *x_display;  extern Display *x_display;
160    
161  // From sys_unix.cpp  // From sys_unix.cpp
162  extern void SysMountFirstFloppy(void);  extern void SysMountFirstFloppy(void);
163    
164    // From clip_unix.cpp
165    extern void ClipboardSelectionClear(XSelectionClearEvent *);
166    extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
167    
168    
169    // Video acceleration through SIGSEGV
170    #ifdef ENABLE_VOSF
171    # include "video_vosf.h"
172    #endif
173    
174    
175    /*
176     *  Utility functions
177     */
178    
179    // Get current video mode
180    static inline int get_current_mode(void)
181    {
182            return VModes[cur_mode].viAppleMode;
183    }
184    
185    // Find palette size for given color depth
186    static int palette_size(int mode)
187    {
188            switch (mode) {
189            case APPLE_1_BIT: return 2;
190            case APPLE_2_BIT: return 4;
191            case APPLE_4_BIT: return 16;
192            case APPLE_8_BIT: return 256;
193            case APPLE_16_BIT: return 32;
194            case APPLE_32_BIT: return 256;
195            default: return 0;
196            }
197    }
198    
199    // Return bits per pixel for requested depth
200    static inline int bytes_per_pixel(int depth)
201    {
202            int bpp;
203            switch (depth) {
204            case 8:
205                    bpp = 1;
206                    break;
207            case 15: case 16:
208                    bpp = 2;
209                    break;
210            case 24: case 32:
211                    bpp = 4;
212                    break;
213            default:
214                    abort();
215            }
216            return bpp;
217    }
218    
219    // Map video_mode depth ID to numerical depth value
220    static inline int depth_of_video_mode(int mode)
221    {
222            int depth;
223            switch (mode) {
224            case APPLE_1_BIT:
225                    depth = 1;
226                    break;
227            case APPLE_2_BIT:
228                    depth = 2;
229                    break;
230            case APPLE_4_BIT:
231                    depth = 4;
232                    break;
233            case APPLE_8_BIT:
234                    depth = 8;
235                    break;
236            case APPLE_16_BIT:
237                    depth = 16;
238                    break;
239            case APPLE_32_BIT:
240                    depth = 32;
241                    break;
242            default:
243                    abort();
244            }
245            return depth;
246    }
247    
248    // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
249    static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
250    {
251            return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
252    }
253    
254    
255    // Do we have a visual for handling the specified Mac depth? If so, set the
256    // global variables "xdepth", "visualInfo", "vis" and "color_class".
257    static bool find_visual_for_depth(int depth)
258    {
259            D(bug("have_visual_for_depth(%d)\n", depth_of_video_mode(depth)));
260    
261            // 1-bit works always and uses default visual
262            if (depth == APPLE_1_BIT) {
263                    vis = DefaultVisual(x_display, screen);
264                    visualInfo.visualid = XVisualIDFromVisual(vis);
265                    int num = 0;
266                    XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
267                    visualInfo = vi[0];
268                    XFree(vi);
269                    xdepth = visualInfo.depth;
270                    color_class = visualInfo.c_class;
271                    D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
272                    return true;
273            }
274    
275            // Calculate minimum and maximum supported X depth
276            int min_depth = 1, max_depth = 32;
277            switch (depth) {
278    #ifdef ENABLE_VOSF
279                    case APPLE_2_BIT:
280                    case APPLE_4_BIT:       // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
281                    case APPLE_8_BIT:
282                            min_depth = 8;
283                            max_depth = 32;
284                            break;
285    #else
286                    case APPLE_2_BIT:
287                    case APPLE_4_BIT:       // 2/4-bit requires VOSF blitters
288                            return false;
289                    case APPLE_8_BIT:       // 8-bit without VOSF requires an 8-bit visual
290                            min_depth = 8;
291                            max_depth = 8;
292                            break;
293    #endif
294                    case APPLE_16_BIT:      // 16-bit requires a 15/16-bit visual
295                            min_depth = 15;
296                            max_depth = 16;
297                            break;
298                    case APPLE_32_BIT:      // 32-bit requires a 24/32-bit visual
299                            min_depth = 24;
300                            max_depth = 32;
301                            break;
302            }
303            D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth));
304    
305            // Try to find a visual for one of the color depths
306            bool visual_found = false;
307            for (int i=0; i<num_depths && !visual_found; i++) {
308    
309                    xdepth = avail_depths[i];
310                    D(bug(" trying to find visual for depth %d\n", xdepth));
311                    if (xdepth < min_depth || xdepth > max_depth)
312                            continue;
313    
314                    // Determine best color class for this depth
315                    switch (xdepth) {
316                            case 1: // Try StaticGray or StaticColor
317                                    if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo)
318                                     || XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo))
319                                            visual_found = true;
320                                    break;
321                            case 8: // Need PseudoColor
322                                    if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo))
323                                            visual_found = true;
324                                    break;
325                            case 15:
326                            case 16:
327                            case 24:
328                            case 32: // Try DirectColor first, as this will allow gamma correction
329                                    if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo)
330                                     || XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo))
331                                            visual_found = true;
332                                    break;
333                            default:
334                                    D(bug("  not a supported depth\n"));
335                                    break;
336                    }
337            }
338            if (!visual_found)
339                    return false;
340    
341            // Visual was found
342            vis = visualInfo.visual;
343            color_class = visualInfo.c_class;
344            D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth));
345    #if DEBUG
346            switch (color_class) {
347                    case StaticGray: D(bug("StaticGray\n")); break;
348                    case GrayScale: D(bug("GrayScale\n")); break;
349                    case StaticColor: D(bug("StaticColor\n")); break;
350                    case PseudoColor: D(bug("PseudoColor\n")); break;
351                    case TrueColor: D(bug("TrueColor\n")); break;
352                    case DirectColor: D(bug("DirectColor\n")); break;
353            }
354    #endif
355            return true;
356    }
357    
358    
359  /*  /*
360   *  Open display (window or fullscreen)   *  Open display (window or fullscreen)
361   */   */
362    
363    // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
364    static Atom WM_DELETE_WINDOW = (Atom)0;
365    static void set_window_delete_protocol(Window w)
366    {
367            WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
368            XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
369    }
370    
371    // Wait until window is mapped/unmapped
372    static void wait_mapped(Window w)
373    {
374            XEvent e;
375            do {
376                    XMaskEvent(x_display, StructureNotifyMask, &e);
377            } while ((e.type != MapNotify) || (e.xmap.event != w));
378    }
379    
380    static void wait_unmapped(Window w)
381    {
382            XEvent e;
383            do {
384                    XMaskEvent(x_display, StructureNotifyMask, &e);
385            } while ((e.type != UnmapNotify) || (e.xmap.event != w));
386    }
387    
388  // Trap SHM errors  // Trap SHM errors
389  static bool shm_error = false;  static bool shm_error = false;
390  static int (*old_error_handler)(Display *, XErrorEvent *);  static int (*old_error_handler)(Display *, XErrorEvent *);
# Line 138  static int error_handler(Display *d, XEr Line 401  static int error_handler(Display *d, XEr
401  // Open window  // Open window
402  static bool open_window(int width, int height)  static bool open_window(int width, int height)
403  {  {
404            int aligned_width = (width + 15) & ~15;
405            int aligned_height = (height + 15) & ~15;
406    
407          // Set absolute mouse mode          // Set absolute mouse mode
408          ADBSetRelMouseMode(false);          ADBSetRelMouseMode(false);
409    
         // Read frame skip prefs  
         frame_skip = PrefsFindInt32("frameskip");  
         if (frame_skip == 0)  
                 frame_skip = 1;  
   
410          // Create window          // Create window
411          XSetWindowAttributes wattr;          XSetWindowAttributes wattr;
412          wattr.event_mask = eventmask = win_eventmask;          wattr.event_mask = eventmask = win_eventmask;
413          wattr.background_pixel = black_pixel;          wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
414          wattr.border_pixel = black_pixel;          wattr.border_pixel = 0;
415          wattr.backing_store = NotUseful;          wattr.backing_store = NotUseful;
416            wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
         XSync(x_display, false);  
417          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,          the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
418                  InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore, &wattr);                  InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr);
419          XSync(x_display, false);  
420            // Set window name
421          XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));          XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
         XMapRaised(x_display, the_win);  
         XSync(x_display, false);  
422    
423          // Set colormap          // Set delete protocol property
424          if (depth == 8) {          set_window_delete_protocol(the_win);
                 XSetWindowColormap(x_display, the_win, cmap[0]);  
                 XSetWMColormapWindows(x_display, the_win, &the_win, 1);  
         }  
425    
426          // Make window unresizable          // Make window unresizable
427          XSizeHints *hints;          XSizeHints *hints;
# Line 179  static bool open_window(int width, int h Line 435  static bool open_window(int width, int h
435                  XFree((char *)hints);                  XFree((char *)hints);
436          }          }
437    
438            // Show window
439            XMapWindow(x_display, the_win);
440            wait_mapped(the_win);
441    
442            // 1-bit mode is big-endian; if the X server is little-endian, we can't
443            // use SHM because that doesn't allow changing the image byte order
444            bool need_msb_image = (depth == 1 && XImageByteOrder(x_display) == LSBFirst);
445    
446          // Try to create and attach SHM image          // Try to create and attach SHM image
447          have_shm = false;          have_shm = false;
448          if (depth != 1 && XShmQueryExtension(x_display)) {          if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) {
449    
450                  // Create SHM image ("height + 2" for safety)                  // Create SHM image ("height + 2" for safety)
451                  img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);                  img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
452                  shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);                  shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
453                  screen_base = (uint32)shmat(shminfo.shmid, 0, 0);                  D(bug(" shm image created\n"));
454                  the_buffer = (uint8 *)screen_base;                  the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
455                  shminfo.shmaddr = img->data = (char *)screen_base;                  shminfo.shmaddr = img->data = (char *)the_buffer_copy;
456                  shminfo.readOnly = False;                  shminfo.readOnly = False;
457    
458                  // Try to attach SHM image, catching errors                  // Try to attach SHM image, catching errors
# Line 205  static bool open_window(int width, int h Line 469  static bool open_window(int width, int h
469                          have_shm = true;                          have_shm = true;
470                          shmctl(shminfo.shmid, IPC_RMID, 0);                          shmctl(shminfo.shmid, IPC_RMID, 0);
471                  }                  }
472                    D(bug(" shm image attached\n"));
473          }          }
474    
475          // Create normal X image if SHM doesn't work ("height + 2" for safety)          // Create normal X image if SHM doesn't work ("height + 2" for safety)
476          if (!have_shm) {          if (!have_shm) {
477                  int bytes_per_row = width;                  int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth));
478                  switch (depth) {                  the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
479                          case 1:                  img = XCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
480                                  bytes_per_row /= 8;                  D(bug(" X image created\n"));
                                 break;  
                         case 15:  
                         case 16:  
                                 bytes_per_row *= 2;  
                                 break;  
                         case 24:  
                         case 32:  
                                 bytes_per_row *= 4;  
                                 break;  
                 }  
                 screen_base = (uint32)malloc((height + 2) * bytes_per_row);  
                 the_buffer = (uint8 *)screen_base;  
                 img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)screen_base, width, height, 32, bytes_per_row);  
481          }          }
482    
483          // 1-Bit mode is big-endian          // 1-Bit mode is big-endian
484      if (depth == 1) {      if (need_msb_image) {
485          img->byte_order = MSBFirst;          img->byte_order = MSBFirst;
486          img->bitmap_bit_order = MSBFirst;          img->bitmap_bit_order = MSBFirst;
487      }      }
488    
489          // Allocate memory for frame buffer copy  #ifdef ENABLE_VOSF
490          the_buffer_copy = (uint8 *)malloc((height + 2) * img->bytes_per_line);          use_vosf = true;
491            // Allocate memory for frame buffer (SIZE is extended to page-boundary)
492            the_host_buffer = the_buffer_copy;
493            the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
494            the_buffer = (uint8 *)vm_acquire(the_buffer_size);
495            the_buffer_copy = (uint8 *)malloc(the_buffer_size);
496            D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
497    #else
498            // Allocate memory for frame buffer
499            the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
500            D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
501    #endif
502            screen_base = (uint32)the_buffer;
503    
504          // Create GC          // Create GC
505          the_gc = XCreateGC(x_display, the_win, 0, 0);          the_gc = XCreateGC(x_display, the_win, 0, 0);
506          XSetForeground(x_display, the_gc, black_pixel);          XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
507    
508          // Create cursor          // Create cursor
509            if (hw_mac_cursor_accl) {
510          cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);          cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
511          cursor_image->byte_order = MSBFirst;          cursor_image->byte_order = MSBFirst;
512          cursor_image->bitmap_bit_order = MSBFirst;          cursor_image->bitmap_bit_order = MSBFirst;
# Line 254  static bool open_window(int width, int h Line 519  static bool open_window(int width, int h
519          cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);          cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
520          mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);          mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
521          cursor_changed = false;          cursor_changed = false;
522            }
523    
524            // Create no_cursor
525            else {
526                    mac_cursor = XCreatePixmapCursor(x_display,
527                            XCreatePixmap(x_display, the_win, 1, 1, 1),
528                            XCreatePixmap(x_display, the_win, 1, 1, 1),
529                            &black, &white, 0, 0);
530                    XDefineCursor(x_display, the_win, mac_cursor);
531            }
532    
533            // Init blitting routines
534            bool native_byte_order;
535    #ifdef WORDS_BIGENDIAN
536            native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
537    #else
538            native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
539    #endif
540    #ifdef ENABLE_VOSF
541            Screen_blitter_init(&visualInfo, native_byte_order, depth);
542    #endif
543    
544          // Set bytes per row          // Set bytes per row
         VModes[cur_mode].viRowBytes = img->bytes_per_line;  
545          XSync(x_display, false);          XSync(x_display, false);
546          return true;          return true;
547  }  }
# Line 268  static bool open_dga(int width, int heig Line 553  static bool open_dga(int width, int heig
553          // Set relative mouse mode          // Set relative mouse mode
554          ADBSetRelMouseMode(true);          ADBSetRelMouseMode(true);
555    
556            // Create window
557            XSetWindowAttributes wattr;
558            wattr.event_mask = eventmask = dga_eventmask;
559            wattr.override_redirect = True;
560            wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
561            the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
562                    InputOutput, vis, CWEventMask | CWOverrideRedirect |
563                    (color_class == DirectColor ? CWColormap : 0), &wattr);
564    
565            // Show window
566            XMapRaised(x_display, the_win);
567            wait_mapped(the_win);
568    
569  #ifdef ENABLE_XF86_VIDMODE  #ifdef ENABLE_XF86_VIDMODE
570          // Switch to best mode          // Switch to best mode
571          if (has_vidmode) {          if (has_vidmode) {
# Line 284  static bool open_dga(int width, int heig Line 582  static bool open_dga(int width, int heig
582  #endif  #endif
583    
584          // Establish direct screen connection          // Establish direct screen connection
585            XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
586            XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
587          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);          XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
588          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);          XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
589    
590            int v_width, v_bank, v_size;
591            XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
592          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
593          XF86DGASetViewPort(x_display, screen, 0, 0);          XF86DGASetViewPort(x_display, screen, 0, 0);
594          XF86DGASetVidPage(x_display, screen, 0);          XF86DGASetVidPage(x_display, screen, 0);
         screen_base = (uint32)dga_screen_base;  
595    
596          // Set colormap          // Set colormap
597          if (depth == 8)          if (!IsDirectMode(get_current_mode())) {
598                    XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
599                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);                  XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
600            }
601            XSync(x_display, false);
602    
603          // Set bytes per row          // Init blitting routines
604          int bytes_per_row = (dga_fb_width + 7) & ~7;          int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, DepthModeForPixelDepth(depth));
605          switch (depth) {  #if ENABLE_VOSF
606                  case 15:          bool native_byte_order;
607                  case 16:  #ifdef WORDS_BIGENDIAN
608                          bytes_per_row *= 2;          native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
609                          break;  #else
610                  case 24:          native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
611                  case 32:  #endif
612                          bytes_per_row *= 4;  #if REAL_ADDRESSING || DIRECT_ADDRESSING
613                          break;          // Screen_blitter_init() returns TRUE if VOSF is mandatory
614            // i.e. the framebuffer update function is not Blit_Copy_Raw
615            use_vosf = Screen_blitter_init(&visualInfo, native_byte_order, depth);
616    
617            if (use_vosf) {
618              // Allocate memory for frame buffer (SIZE is extended to page-boundary)
619              the_host_buffer = the_buffer;
620              the_buffer_size = page_extend((height + 2) * bytes_per_row);
621              the_buffer_copy = (uint8 *)malloc(the_buffer_size);
622              the_buffer = (uint8 *)vm_acquire(the_buffer_size);
623              D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
624          }          }
625    #else
626            use_vosf = false;
627    #endif
628    #endif
629    
630            // Set frame buffer base
631            D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
632            screen_base = (uint32)the_buffer;
633          VModes[cur_mode].viRowBytes = bytes_per_row;          VModes[cur_mode].viRowBytes = bytes_per_row;
         XSync(x_display, false);  
634          return true;          return true;
635  #else  #else
636          ErrorAlert("SheepShaver has been compiled with DGA support disabled.");          ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
# Line 318  static bool open_dga(int width, int heig Line 640  static bool open_dga(int width, int heig
640    
641  static bool open_display(void)  static bool open_display(void)
642  {  {
643          display_type = VModes[cur_mode].viType;          D(bug("open_display()\n"));
644          switch (VModes[cur_mode].viAppleMode) {          const VideoInfo &mode = VModes[cur_mode];
645                  case APPLE_1_BIT:  
646                          depth = 1;          // Find best available X visual
647                          break;          if (!find_visual_for_depth(mode.viAppleMode)) {
648                  case APPLE_2_BIT:                  ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
649                          depth = 2;                  return false;
650                          break;          }
651                  case APPLE_4_BIT:  
652                          depth = 4;          // Create color maps
653                          break;          if (color_class == PseudoColor || color_class == DirectColor) {
654                  case APPLE_8_BIT:                  cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
655                          depth = 8;                  cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
656                          break;          } else {
657                  case APPLE_16_BIT:                  cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
658                          depth = 16;                  cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
                         break;  
                 case APPLE_32_BIT:  
                         depth = 32;  
                         break;  
659          }          }
660    
661            // Find pixel format of direct modes
662            if (color_class == DirectColor || color_class == TrueColor) {
663                    rshift = gshift = bshift = 0;
664                    rloss = gloss = bloss = 8;
665                    uint32 mask;
666                    for (mask=vis->red_mask; !(mask&1); mask>>=1)
667                            ++rshift;
668                    for (; mask&1; mask>>=1)
669                            --rloss;
670                    for (mask=vis->green_mask; !(mask&1); mask>>=1)
671                            ++gshift;
672                    for (; mask&1; mask>>=1)
673                            --gloss;
674                    for (mask=vis->blue_mask; !(mask&1); mask>>=1)
675                            ++bshift;
676                    for (; mask&1; mask>>=1)
677                            --bloss;
678            }
679    
680            // Preset palette pixel values for CLUT or gamma table
681            if (color_class == DirectColor) {
682                    int num = vis->map_entries;
683                    for (int i=0; i<num; i++) {
684                            int c = (i * 256) / num;
685                            x_palette[i].pixel = map_rgb(c, c, c);
686                            x_palette[i].flags = DoRed | DoGreen | DoBlue;
687                    }
688            } else if (color_class == PseudoColor) {
689                    for (int i=0; i<256; i++) {
690                            x_palette[i].pixel = i;
691                            x_palette[i].flags = DoRed | DoGreen | DoBlue;
692                    }
693            }
694    
695            // Load gray ramp to color map
696            int num = (color_class == DirectColor ? vis->map_entries : 256);
697            for (int i=0; i<num; i++) {
698                    int c = (i * 256) / num;
699                    x_palette[i].red = c * 0x0101;
700                    x_palette[i].green = c * 0x0101;
701                    x_palette[i].blue = c * 0x0101;
702            }
703            if (color_class == PseudoColor || color_class == DirectColor) {
704                    XStoreColors(x_display, cmap[0], x_palette, num);
705                    XStoreColors(x_display, cmap[1], x_palette, num);
706            }
707    
708    #ifdef ENABLE_VOSF
709            // Load gray ramp to 8->16/32 expand map
710            if (!IsDirectMode(get_current_mode()) && xdepth > 8)
711                    for (int i=0; i<256; i++)
712                            ExpandMap[i] = map_rgb(i, i, i);
713    #endif
714    
715            // Create display of requested type
716            display_type = mode.viType;
717            depth = depth_of_video_mode(mode.viAppleMode);
718    
719            bool display_open = false;
720          if (display_type == DIS_SCREEN)          if (display_type == DIS_SCREEN)
721                  return open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);                  display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
722          else if (display_type == DIS_WINDOW)          else if (display_type == DIS_WINDOW)
723                  return open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);                  display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
724          else  
725    #ifdef ENABLE_VOSF
726            if (use_vosf) {
727                    // Initialize the VOSF system
728                    if (!video_vosf_init()) {
729                            ErrorAlert(GetString(STR_VOSF_INIT_ERR));
730                  return false;                  return false;
731  }  }
732            }
733    #endif
734    
735            return display_open;
736    }
737    
738    
739  /*  /*
# Line 355  static bool open_display(void) Line 743  static bool open_display(void)
743  // Close window  // Close window
744  static void close_window(void)  static void close_window(void)
745  {  {
746          // Close window          if (have_shm) {
747          XDestroyWindow(x_display, the_win);                  XShmDetach(x_display, &shminfo);
748    #ifdef ENABLE_VOSF
749          // Close frame buffer copy                  the_host_buffer = NULL; // don't free() in driver_base dtor
750          if (the_buffer_copy) {  #else
751                  free(the_buffer_copy);                  the_buffer_copy = NULL; // don't free() in driver_base dtor
752                  the_buffer_copy = NULL;  #endif
753            }
754            if (img) {
755                    if (!have_shm)
756                            img->data = NULL;
757                    XDestroyImage(img);
758          }          }
759            if (have_shm) {
760                    shmdt(shminfo.shmaddr);
761                    shmctl(shminfo.shmid, IPC_RMID, 0);
762            }
763            if (the_gc)
764                    XFreeGC(x_display, the_gc);
765    
766            XFlush(x_display);
767            XSync(x_display, false);
768  }  }
769    
770  // Close DGA mode  // Close DGA mode
# Line 378  static void close_dga(void) Line 780  static void close_dga(void)
780          if (has_vidmode)          if (has_vidmode)
781                  XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);                  XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
782  #endif  #endif
783    
784            if (!use_vosf) {
785                    // don't free() the screen buffer in driver_base dtor
786                    the_buffer = NULL;
787            }
788    #ifdef ENABLE_VOSF
789            else {
790                    // don't free() the screen buffer in driver_base dtor
791                    the_host_buffer = NULL;
792            }
793    #endif
794  }  }
795    
796  static void close_display(void)  static void close_display(void)
# Line 386  static void close_display(void) Line 799  static void close_display(void)
799                  close_dga();                  close_dga();
800          else if (display_type == DIS_WINDOW)          else if (display_type == DIS_WINDOW)
801                  close_window();                  close_window();
802    
803            // Close window
804            if (the_win) {
805                    XUnmapWindow(x_display, the_win);
806                    wait_unmapped(the_win);
807                    XDestroyWindow(x_display, the_win);
808            }
809    
810            // Free colormaps
811            if (cmap[0]) {
812                    XFreeColormap(x_display, cmap[0]);
813                    cmap[0] = 0;
814            }
815            if (cmap[1]) {
816                    XFreeColormap(x_display, cmap[1]);
817                    cmap[1] = 0;
818            }
819    
820    #ifdef ENABLE_VOSF
821            if (use_vosf) {
822                    // Deinitialize VOSF
823                    video_vosf_exit();
824            }
825    #endif
826    
827            // Free frame buffer(s)
828            if (!use_vosf) {
829                    if (the_buffer_copy) {
830                            free(the_buffer_copy);
831                            the_buffer_copy = NULL;
832                    }
833            }
834    #ifdef ENABLE_VOSF
835            else {
836                    // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
837                    if (the_buffer != VM_MAP_FAILED) {
838                            D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
839                            vm_release(the_buffer, the_buffer_size);
840                            the_buffer = NULL;
841                    }
842                    if (the_host_buffer) {
843                            D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
844                            free(the_host_buffer);
845                            the_host_buffer = NULL;
846                    }
847                    if (the_buffer_copy) {
848                            D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
849                            free(the_buffer_copy);
850                            the_buffer_copy = NULL;
851                    }
852            }
853    #endif
854  }  }
855    
856    
# Line 393  static void close_display(void) Line 858  static void close_display(void)
858   *  Initialization   *  Initialization
859   */   */
860    
861  static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, long apple_mode, long apple_id, int type)  // Init keycode translation table
862    static void keycode_init(void)
863    {
864            bool use_kc = PrefsFindBool("keycodes");
865            if (use_kc) {
866    
867                    // Get keycode file path from preferences
868                    const char *kc_path = PrefsFindString("keycodefile");
869    
870                    // Open keycode table
871                    FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
872                    if (f == NULL) {
873                            char str[256];
874                            sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
875                            WarningAlert(str);
876                            return;
877                    }
878    
879                    // Default translation table
880                    for (int i=0; i<256; i++)
881                            keycode_table[i] = -1;
882    
883                    // Search for server vendor string, then read keycodes
884                    const char *vendor = ServerVendor(x_display);
885                    bool vendor_found = false;
886                    char line[256];
887                    while (fgets(line, 255, f)) {
888                            // Read line
889                            int len = strlen(line);
890                            if (len == 0)
891                                    continue;
892                            line[len-1] = 0;
893    
894                            // Comments begin with "#" or ";"
895                            if (line[0] == '#' || line[0] == ';' || line[0] == 0)
896                                    continue;
897    
898                            if (vendor_found) {
899                                    // Read keycode
900                                    int x_code, mac_code;
901                                    if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
902                                            keycode_table[x_code & 0xff] = mac_code;
903                                    else
904                                            break;
905                            } else {
906                                    // Search for vendor string
907                                    if (strstr(vendor, line) == vendor)
908                                            vendor_found = true;
909                            }
910                    }
911    
912                    // Keycode file completely read
913                    fclose(f);
914                    use_keycodes = vendor_found;
915    
916                    // Vendor not found? Then display warning
917                    if (!vendor_found) {
918                            char str[256];
919                            sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
920                            WarningAlert(str);
921                            return;
922                    }
923            }
924    }
925    
926    // Find Apple mode matching best specified dimensions
927    static int find_apple_resolution(int xsize, int ysize)
928    {
929            int apple_id;
930            if (xsize < 800)
931                    apple_id = APPLE_640x480;
932            else if (xsize < 1024)
933                    apple_id = APPLE_800x600;
934            else if (xsize < 1152)
935                    apple_id = APPLE_1024x768;
936            else if (xsize < 1280) {
937                    if (ysize < 900)
938                            apple_id = APPLE_1152x768;
939                    else
940                            apple_id = APPLE_1152x900;
941            }
942            else if (xsize < 1600)
943                    apple_id = APPLE_1280x1024;
944            else
945                    apple_id = APPLE_1600x1200;
946            return apple_id;
947    }
948    
949    // Find mode in list of supported modes
950    static int find_mode(int apple_mode, int apple_id, int type)
951    {
952            for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
953                    if (p->viType == type && p->viAppleID == apple_id && p->viAppleMode == apple_mode)
954                            return p - VModes;
955            }
956            return -1;
957    }
958    
959    // Add mode to list of supported modes
960    static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
961  {  {
962          if (allow & test) {          if (allow & test) {
963                  p->viType = type;                  p->viType = type;
# Line 412  static void add_mode(VideoInfo *&p, uint Line 976  static void add_mode(VideoInfo *&p, uint
976                                  p->viXsize = 1024;                                  p->viXsize = 1024;
977                                  p->viYsize = 768;                                  p->viYsize = 768;
978                                  break;                                  break;
979                            case APPLE_1152x768:
980                                    p->viXsize = 1152;
981                                    p->viYsize = 768;
982                                    break;
983                          case APPLE_1152x900:                          case APPLE_1152x900:
984                                  p->viXsize = 1152;                                  p->viXsize = 1152;
985                                  p->viYsize = 900;                                  p->viYsize = 900;
# Line 421  static void add_mode(VideoInfo *&p, uint Line 989  static void add_mode(VideoInfo *&p, uint
989                                  p->viYsize = 1024;                                  p->viYsize = 1024;
990                                  break;                                  break;
991                          case APPLE_1600x1200:                          case APPLE_1600x1200:
992                                  p->viXsize = 1600;                                  p->viXsize = 1600;
993                                  p->viYsize = 1200;                                  p->viYsize = 1200;
                                 break;  
                 }  
                 switch (apple_mode) {  
                         case APPLE_8_BIT:  
                                 p->viRowBytes = p->viXsize;  
                                 break;  
                         case APPLE_16_BIT:  
                                 p->viRowBytes = p->viXsize * 2;  
                                 break;  
                         case APPLE_32_BIT:  
                                 p->viRowBytes = p->viXsize * 4;  
994                                  break;                                  break;
995                  }                  }
996                    p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
997                  p->viAppleMode = apple_mode;                  p->viAppleMode = apple_mode;
998                  p->viAppleID = apple_id;                  p->viAppleID = apple_id;
999                  p++;                  p++;
1000          }          }
1001  }  }
1002    
1003    // Add standard list of windowed modes for given color depth
1004    static void add_window_modes(VideoInfo *&p, int window_modes, int mode)
1005    {
1006            add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
1007            add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
1008    }
1009    
1010  static bool has_mode(int x, int y)  static bool has_mode(int x, int y)
1011  {  {
1012  #ifdef ENABLE_XF86_VIDMODE  #ifdef ENABLE_XF86_VIDMODE
# Line 456  static bool has_mode(int x, int y) Line 1021  static bool has_mode(int x, int y)
1021    
1022  bool VideoInit(void)  bool VideoInit(void)
1023  {  {
1024    #ifdef ENABLE_VOSF
1025            // Zero the mainBuffer structure
1026            mainBuffer.dirtyPages = NULL;
1027            mainBuffer.pageInfo = NULL;
1028    #endif
1029    
1030            // Check if X server runs on local machine
1031            local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
1032                     || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
1033    
1034            // Init keycode translation
1035            keycode_init();
1036    
1037            // Read frame skip prefs
1038            frame_skip = PrefsFindInt32("frameskip");
1039            if (frame_skip == 0)
1040                    frame_skip = 1;
1041    
1042            // Read mouse wheel prefs
1043            mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1044            mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1045    
1046          // Init variables          // Init variables
1047          private_data = NULL;          private_data = NULL;
         cur_mode = 0;   // Window 640x480  
1048          video_activated = true;          video_activated = true;
1049    
1050          // Find screen and root window          // Find screen and root window
1051          screen = XDefaultScreen(x_display);          screen = XDefaultScreen(x_display);
1052          rootwin = XRootWindow(x_display, screen);          rootwin = XRootWindow(x_display, screen);
1053    
1054            // Get sorted list of available depths
1055            avail_depths = XListDepths(x_display, screen, &num_depths);
1056            if (avail_depths == NULL) {
1057                    ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
1058                    return false;
1059            }
1060            sort(avail_depths, avail_depths + num_depths);
1061    
1062          // Get screen depth          // Get screen depth
1063          xdepth = DefaultDepth(x_display, screen);          xdepth = DefaultDepth(x_display, screen);
1064    
1065  #ifdef ENABLE_XF86_DGA  #ifdef ENABLE_XF86_DGA
1066          // DGA available?          // DGA available?
1067      int event_base, error_base;      int event_base, error_base;
1068      if (XF86DGAQueryExtension(x_display, &event_base, &error_base)) {      if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1069                  int dga_flags = 0;                  int dga_flags = 0;
1070                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);                  XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1071                  has_dga = dga_flags & XF86DGADirectPresent;                  has_dga = dga_flags & XF86DGADirectPresent;
# Line 495  bool VideoInit(void) Line 1089  bool VideoInit(void)
1089          black_pixel = BlackPixel(x_display, screen);          black_pixel = BlackPixel(x_display, screen);
1090          white_pixel = WhitePixel(x_display, screen);          white_pixel = WhitePixel(x_display, screen);
1091    
         // Get appropriate visual  
         int color_class;  
         switch (xdepth) {  
 #if 0  
                 case 1:  
                         color_class = StaticGray;  
                         break;  
 #endif  
                 case 8:  
                         color_class = PseudoColor;  
                         break;  
                 case 15:  
                 case 16:  
                 case 24:  
                 case 32:  
                         color_class = TrueColor;  
                         break;  
                 default:  
                         ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));  
                         return false;  
         }  
         if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {  
                 ErrorAlert(GetString(STR_NO_XVISUAL_ERR));  
                 return false;  
         }  
         if (visualInfo.depth != xdepth) {  
                 ErrorAlert(GetString(STR_NO_XVISUAL_ERR));  
                 return false;  
         }  
         vis = visualInfo.visual;  
   
1092          // Mac screen depth follows X depth (for now)          // Mac screen depth follows X depth (for now)
1093          depth = xdepth;          int default_mode = APPLE_8_BIT;
1094            switch (DefaultDepth(x_display, screen)) {
         // Create color maps for 8 bit mode  
         if (depth == 8) {  
                 cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);  
                 cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);  
                 XInstallColormap(x_display, cmap[0]);  
                 XInstallColormap(x_display, cmap[1]);  
         }  
   
         // Construct video mode table  
         int mode = APPLE_8_BIT;  
         int bpr_mult = 8;  
         switch (depth) {  
1095                  case 1:                  case 1:
1096                          mode = APPLE_1_BIT;                  default_mode = APPLE_1_BIT;
                         bpr_mult = 1;  
1097                          break;                          break;
1098                  case 8:                  case 8:
1099                          mode = APPLE_8_BIT;                  default_mode = APPLE_8_BIT;
                         bpr_mult = 8;  
1100                          break;                          break;
1101                  case 15:          case 15: case 16:
1102                  case 16:                  default_mode = APPLE_16_BIT;
                         mode = APPLE_16_BIT;  
                         bpr_mult = 16;  
1103                          break;                          break;
1104                  case 24:          case 24: case 32:
1105                  case 32:                  default_mode = APPLE_32_BIT;
                         mode = APPLE_32_BIT;  
                         bpr_mult = 32;  
1106                          break;                          break;
1107          }          }
1108    
1109            // Construct video mode table
1110          uint32 window_modes = PrefsFindInt32("windowmodes");          uint32 window_modes = PrefsFindInt32("windowmodes");
1111          uint32 screen_modes = PrefsFindInt32("screenmodes");          uint32 screen_modes = PrefsFindInt32("screenmodes");
1112          if (!has_dga)          if (!has_dga)
# Line 569  bool VideoInit(void) Line 1115  bool VideoInit(void)
1115                  window_modes |= 3;      // Allow at least 640x480 and 800x600 window modes                  window_modes |= 3;      // Allow at least 640x480 and 800x600 window modes
1116    
1117          VideoInfo *p = VModes;          VideoInfo *p = VModes;
1118          add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);          for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1119          add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);                  if (find_visual_for_depth(d))
1120                            add_window_modes(p, window_modes, d);
1121    
1122          if (has_vidmode) {          if (has_vidmode) {
1123                  if (has_mode(640, 480))                  if (has_mode(640, 480))
1124                          add_mode(p, screen_modes, 1, mode, APPLE_640x480, DIS_SCREEN);                          add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1125                  if (has_mode(800, 600))                  if (has_mode(800, 600))
1126                          add_mode(p, screen_modes, 2, mode, APPLE_800x600, DIS_SCREEN);                          add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN);
1127                  if (has_mode(1024, 768))                  if (has_mode(1024, 768))
1128                          add_mode(p, screen_modes, 4, mode, APPLE_1024x768, DIS_SCREEN);                          add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN);
1129                    if (has_mode(1152, 768))
1130                            add_mode(p, screen_modes, 64, default_mode, APPLE_1152x768, DIS_SCREEN);
1131                  if (has_mode(1152, 900))                  if (has_mode(1152, 900))
1132                          add_mode(p, screen_modes, 8, mode, APPLE_1152x900, DIS_SCREEN);                          add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN);
1133                  if (has_mode(1280, 1024))                  if (has_mode(1280, 1024))
1134                          add_mode(p, screen_modes, 16, mode, APPLE_1280x1024, DIS_SCREEN);                          add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN);
1135                  if (has_mode(1600, 1200))                  if (has_mode(1600, 1200))
1136                          add_mode(p, screen_modes, 32, mode, APPLE_1600x1200, DIS_SCREEN);                          add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN);
1137          } else if (screen_modes) {          } else if (screen_modes) {
1138                  int xsize = DisplayWidth(x_display, screen);                  int xsize = DisplayWidth(x_display, screen);
1139                  int ysize = DisplayHeight(x_display, screen);                  int ysize = DisplayHeight(x_display, screen);
1140                  int apple_id;                  int apple_id = find_apple_resolution(xsize, ysize);
                 if (xsize < 800)  
                         apple_id = APPLE_640x480;  
                 else if (xsize < 1024)  
                         apple_id = APPLE_800x600;  
                 else if (xsize < 1152)  
                         apple_id = APPLE_1024x768;  
                 else if (xsize < 1280)  
                         apple_id = APPLE_1152x900;  
                 else if (xsize < 1600)  
                         apple_id = APPLE_1280x1024;  
                 else  
                         apple_id = APPLE_1600x1200;  
1141                  p->viType = DIS_SCREEN;                  p->viType = DIS_SCREEN;
1142                  p->viRowBytes = 0;                  p->viRowBytes = 0;
1143                  p->viXsize = xsize;                  p->viXsize = xsize;
1144                  p->viYsize = ysize;                  p->viYsize = ysize;
1145                  p->viAppleMode = mode;                  p->viAppleMode = default_mode;
1146                  p->viAppleID = apple_id;                  p->viAppleID = apple_id;
1147                  p++;                  p++;
1148          }          }
# Line 614  bool VideoInit(void) Line 1152  bool VideoInit(void)
1152          p->viAppleMode = 0;          p->viAppleMode = 0;
1153          p->viAppleID = 0;          p->viAppleID = 0;
1154    
1155  #ifdef ENABLE_XF86_DGA          // Find default mode (window 640x480)
1156            cur_mode = -1;
1157          if (has_dga && screen_modes) {          if (has_dga && screen_modes) {
1158                  int v_bank, v_size;                  int screen_width = DisplayWidth(x_display, screen);
1159                  XF86DGAGetVideo(x_display, screen, &dga_screen_base, &dga_fb_width, &v_bank, &v_size);                  int screen_height = DisplayHeight(x_display, screen);
1160                  D(bug("DGA screen_base %p, v_width %d\n", dga_screen_base, dga_fb_width));                  int apple_id = find_apple_resolution(screen_width, screen_height);
1161                    if (apple_id != -1)
1162                            cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN);
1163            }
1164            if (cur_mode == -1) {
1165                    // pick up first windowed mode available
1166                    for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
1167                            if (p->viType == DIS_WINDOW && p->viAppleMode == default_mode) {
1168                                    cur_mode = p - VModes;
1169                                    break;
1170                            }
1171                    }
1172            }
1173            if (cur_mode == -1)
1174                    cur_mode = find_mode(default_mode, APPLE_W_640x480, DIS_WINDOW);
1175            assert(cur_mode != -1);
1176    
1177    #if DEBUG
1178            D(bug("Available video modes:\n"));
1179            for (p = VModes; p->viType != DIS_INVALID; p++) {
1180                    int bits = depth_of_video_mode(p->viAppleMode);
1181                    D(bug(" %dx%d (ID %02x), %d colors\n", p->viXsize, p->viYsize, p->viAppleID, 1 << bits));
1182          }          }
1183  #endif  #endif
1184    
# Line 633  bool VideoInit(void) Line 1193  bool VideoInit(void)
1193    
1194          // Start periodic thread          // Start periodic thread
1195          XSync(x_display, false);          XSync(x_display, false);
1196          redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);          Set_pthread_attr(&redraw_thread_attr, 0);
1197            redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1198          D(bug("Redraw thread installed (%ld)\n", redraw_thread));          D(bug("Redraw thread installed (%ld)\n", redraw_thread));
1199          return true;          return true;
1200  }  }
# Line 652  void VideoExit(void) Line 1213  void VideoExit(void)
1213                  redraw_thread_active = false;                  redraw_thread_active = false;
1214          }          }
1215    
1216    #ifdef ENABLE_VOSF
1217            if (use_vosf) {
1218                    // Deinitialize VOSF
1219                    video_vosf_exit();
1220            }
1221    #endif
1222    
1223          // Close window and server connection          // Close window and server connection
1224          if (x_display != NULL) {          if (x_display != NULL) {
1225                  XSync(x_display, false);                  XSync(x_display, false);
1226                  close_display();                  close_display();
1227                  XFlush(x_display);                  XFlush(x_display);
1228                  XSync(x_display, false);                  XSync(x_display, false);
                 if (depth == 8) {  
                         XFreeColormap(x_display, cmap[0]);  
                         XFreeColormap(x_display, cmap[1]);  
                 }  
1229          }          }
1230  }  }
1231    
# Line 725  static void resume_emul(void) Line 1289  static void resume_emul(void)
1289          // Reopen full screen display          // Reopen full screen display
1290          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);          XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1291          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);          XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1292    #ifdef ENABLE_XF86_DGA
1293          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);          XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1294          XF86DGASetViewPort(x_display, screen, 0, 0);          XF86DGASetViewPort(x_display, screen, 0, 0);
1295    #endif
1296          XSync(x_display, false);          XSync(x_display, false);
1297    
1298            // the_buffer already contains the data to restore. i.e. since a temporary
1299            // frame buffer is used when VOSF is actually used, fb_save is therefore
1300            // not necessary.
1301    #ifdef ENABLE_VOSF
1302            if (use_vosf) {
1303                    LOCK_VOSF;
1304                    PFLAG_SET_ALL;
1305                    UNLOCK_VOSF;
1306                    memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1307            }
1308    #endif
1309    
1310          // Restore frame buffer          // Restore frame buffer
1311          if (fb_save) {          if (fb_save) {
1312    #ifdef ENABLE_VOSF
1313                    // Don't copy fb_save to the temporary frame buffer in VOSF mode
1314                    if (!use_vosf)
1315    #endif
1316                  memcpy((void *)screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);                  memcpy((void *)screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1317                  free(fb_save);                  free(fb_save);
1318                  fb_save = NULL;                  fb_save = NULL;
# Line 904  static int kc_decode(KeySym ks) Line 1486  static int kc_decode(KeySym ks)
1486          return -1;          return -1;
1487  }  }
1488    
1489  static int event2keycode(XKeyEvent *ev)  static int event2keycode(XKeyEvent &ev)
1490  {  {
1491          KeySym ks;          KeySym ks;
1492          int as;          int as;
1493          int i = 0;          int i = 0;
1494    
1495          do {          do {
1496                  ks = XLookupKeysym(ev, i++);                  ks = XLookupKeysym(&ev, i++);
1497                  as = kc_decode(ks);                  as = kc_decode(ks);
1498                  if (as != -1)                  if (as != -1)
1499                          return as;                          return as;
# Line 926  static void handle_events(void) Line 1508  static void handle_events(void)
1508          for (;;) {          for (;;) {
1509                  XEvent event;                  XEvent event;
1510    
1511                  if (!XCheckMaskEvent(x_display, eventmask, &event))                  XDisplayLock();
1512                    if (!XCheckMaskEvent(x_display, eventmask, &event)) {
1513                            // Handle clipboard events
1514                            if (XCheckTypedEvent(x_display, SelectionRequest, &event))
1515                                    ClipboardSelectionRequest(&event.xselectionrequest);
1516                            else if (XCheckTypedEvent(x_display, SelectionClear, &event))
1517                                    ClipboardSelectionClear(&event.xselectionclear);
1518    
1519                            // Window "close" widget clicked
1520                            else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
1521                                    if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1522                                            ADBKeyDown(0x7f);       // Power key
1523                                            ADBKeyUp(0x7f);
1524                                    }
1525                            }
1526    
1527                            XDisplayUnlock();
1528                          break;                          break;
1529                    }
1530                    XDisplayUnlock();
1531    
1532                  switch (event.type) {                  switch (event.type) {
1533                          // Mouse button                          // Mouse button
# Line 935  static void handle_events(void) Line 1535  static void handle_events(void)
1535                                  unsigned int button = ((XButtonEvent *)&event)->button;                                  unsigned int button = ((XButtonEvent *)&event)->button;
1536                                  if (button < 4)                                  if (button < 4)
1537                                          ADBMouseDown(button - 1);                                          ADBMouseDown(button - 1);
1538                                    else if (button < 6) {  // Wheel mouse
1539                                            if (mouse_wheel_mode == 0) {
1540                                                    int key = (button == 5) ? 0x79 : 0x74;  // Page up/down
1541                                                    ADBKeyDown(key);
1542                                                    ADBKeyUp(key);
1543                                            } else {
1544                                                    int key = (button == 5) ? 0x3d : 0x3e;  // Cursor up/down
1545                                                    for(int i=0; i<mouse_wheel_lines; i++) {
1546                                                            ADBKeyDown(key);
1547                                                            ADBKeyUp(key);
1548                                                    }
1549                                            }
1550                                    }
1551                                  break;                                  break;
1552                          }                          }
1553                          case ButtonRelease: {                          case ButtonRelease: {
# Line 944  static void handle_events(void) Line 1557  static void handle_events(void)
1557                                  break;                                  break;
1558                          }                          }
1559    
1560                          // Mouse moved                          // Mouse entered window
1561                          case EnterNotify:                          case EnterNotify:
1562                                  ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);                                  if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
1563                                            ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1564                                  break;                                  break;
1565    
1566                            // Mouse moved
1567                          case MotionNotify:                          case MotionNotify:
1568                                  ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);                                  ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1569                                  break;                                  break;
1570    
1571                          // Keyboard                          // Keyboard
1572                          case KeyPress: {                          case KeyPress: {
1573                                  int code;                                  int code = event2keycode(event.xkey);
1574                                  if ((code = event2keycode((XKeyEvent *)&event)) != -1) {                                  if (use_keycodes && code != -1)
1575                                            code = keycode_table[event.xkey.keycode & 0xff];
1576                                    if (code != -1) {
1577                                          if (!emul_suspended) {                                          if (!emul_suspended) {
1578                                                  ADBKeyDown(code);                                                  ADBKeyDown(code);
1579                                                  if (code == 0x36)                                                  if (code == 0x36)
# Line 968  static void handle_events(void) Line 1586  static void handle_events(void)
1586                                  break;                                  break;
1587                          }                          }
1588                          case KeyRelease: {                          case KeyRelease: {
1589                                  int code;                                  int code = event2keycode(event.xkey);
1590                                  if ((code = event2keycode((XKeyEvent *)&event)) != -1) {                                  if (use_keycodes && code != 1)
1591                                            code = keycode_table[event.xkey.keycode & 0xff];
1592                                    if (code != -1) {
1593                                          ADBKeyUp(code);                                          ADBKeyUp(code);
1594                                          if (code == 0x36)                                          if (code == 0x36)
1595                                                  ctrl_down = false;                                                  ctrl_down = false;
# Line 979  static void handle_events(void) Line 1599  static void handle_events(void)
1599    
1600                          // Hidden parts exposed, force complete refresh                          // Hidden parts exposed, force complete refresh
1601                          case Expose:                          case Expose:
1602    #ifdef ENABLE_VOSF
1603                                    if (use_vosf) {                 // VOSF refresh
1604                                            LOCK_VOSF;
1605                                            PFLAG_SET_ALL;
1606                                            UNLOCK_VOSF;
1607                                    }
1608    #endif
1609                                  memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);                                  memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1610                                  break;                                  break;
1611                  }                  }
# Line 1005  void VideoVBL(void) Line 1632  void VideoVBL(void)
1632   *  Install graphics acceleration   *  Install graphics acceleration
1633   */   */
1634    
1635  #if 0  // Rectangle inversion
1636  // Rectangle blitting  template< int bpp >
1637  static void accl_bitblt(accl_params *p)  static inline void do_invrect(uint8 *dest, uint32 length)
1638  {  {
1639          D(bug("accl_bitblt\n"));  #define INVERT_1(PTR, OFS) ((uint8  *)(PTR))[OFS] = ~((uint8  *)(PTR))[OFS]
1640    #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1641    #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1642    #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1643    
1644    #ifndef UNALIGNED_PROFITABLE
1645            // Align on 16-bit boundaries
1646            if (bpp < 16 && (((uintptr)dest) & 1)) {
1647                    INVERT_1(dest, 0);
1648                    dest += 1; length -= 1;
1649            }
1650    
1651            // Align on 32-bit boundaries
1652            if (bpp < 32 && (((uintptr)dest) & 2)) {
1653                    INVERT_2(dest, 0);
1654                    dest += 2; length -= 2;
1655            }
1656    #endif
1657    
1658          // Get blitting parameters          // Invert 8-byte words
1659          int16 src_X = p->src_rect[1] - p->src_bounds[1];          if (length >= 8) {
1660          int16 src_Y = p->src_rect[0] - p->src_bounds[0];                  const int r = (length / 8) % 8;
1661          int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];                  dest += r * 8;
1662          int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];  
1663          int16 width = p->dest_rect[3] - p->dest_rect[1] - 1;                  int n = ((length / 8) + 7) / 8;
1664          int16 height = p->dest_rect[2] - p->dest_rect[0] - 1;                  switch (r) {
1665          D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));                  case 0: do {
1666          D(bug(" width %d, height %d\n", width, height));                                  dest += 64;
1667                                    INVERT_8(dest, -8);
1668                    case 7: INVERT_8(dest, -7);
1669                    case 6: INVERT_8(dest, -6);
1670                    case 5: INVERT_8(dest, -5);
1671                    case 4: INVERT_8(dest, -4);
1672                    case 3: INVERT_8(dest, -3);
1673                    case 2: INVERT_8(dest, -2);
1674                    case 1: INVERT_8(dest, -1);
1675                                    } while (--n > 0);
1676                    }
1677            }
1678    
1679          // And perform the blit          // 32-bit cell to invert?
1680          bitblt_hook(src_X, src_Y, dest_X, dest_Y, width, height);          if (length & 4) {
1681                    INVERT_4(dest, 0);
1682                    if (bpp <= 16)
1683                            dest += 4;
1684            }
1685    
1686            // 16-bit cell to invert?
1687            if (bpp <= 16 && (length & 2)) {
1688                    INVERT_2(dest, 0);
1689                    if (bpp <= 8)
1690                            dest += 2;
1691            }
1692    
1693            // 8-bit cell to invert?
1694            if (bpp <= 8 && (length & 1))
1695                    INVERT_1(dest, 0);
1696    
1697    #undef INVERT_1
1698    #undef INVERT_2
1699    #undef INVERT_4
1700    #undef INVERT_8
1701  }  }
1702    
1703  static bool accl_bitblt_hook(accl_params *p)  void NQD_invrect(uint32 p)
1704  {  {
1705          D(bug("accl_draw_hook %p\n", p));          D(bug("accl_invrect %08x\n", p));
1706    
1707          // Check if we can accelerate this bitblt          // Get inversion parameters
1708          if (p->src_base_addr == screen_base && p->dest_base_addr == screen_base &&          int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1709                  display_type == DIS_SCREEN && bitblt_hook != NULL &&          int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1710                  ((uint32 *)p)[0x18 >> 2] + ((uint32 *)p)[0x128 >> 2] == 0 &&          int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1711                  ((uint32 *)p)[0x130 >> 2] == 0 &&          int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1712                  p->transfer_mode == 0 &&          D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1713                  p->src_row_bytes > 0 && ((uint32 *)p)[0x15c >> 2] > 0) {          D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1714    
1715                  // Yes, set function pointer          //!!?? pen_mode == 14
1716                  p->draw_proc = accl_bitblt;  
1717                  return true;          // And perform the inversion
1718            const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1719            const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1720            uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1721            width *= bpp;
1722            switch (bpp) {
1723            case 1:
1724                    for (int i = 0; i < height; i++) {
1725                            do_invrect<8>(dest, width);
1726                            dest += dest_row_bytes;
1727                    }
1728                    break;
1729            case 2:
1730                    for (int i = 0; i < height; i++) {
1731                            do_invrect<16>(dest, width);
1732                            dest += dest_row_bytes;
1733                    }
1734                    break;
1735            case 4:
1736                    for (int i = 0; i < height; i++) {
1737                            do_invrect<32>(dest, width);
1738                            dest += dest_row_bytes;
1739                    }
1740                    break;
1741          }          }
         return false;  
1742  }  }
1743    
1744  // Rectangle filling/inversion  // Rectangle filling
1745  static void accl_fillrect8(accl_params *p)  template< int bpp >
1746    static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1747  {  {
1748          D(bug("accl_fillrect8\n"));  #define FILL_1(PTR, OFS, VAL) ((uint8  *)(PTR))[OFS] = (VAL)
1749    #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1750          // Get filling parameters  #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1751          int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];  #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
         int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];  
         int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;  
         int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;  
         uint8 color = p->pen_mode == 8 ? p->fore_pen : p->back_pen;  
         D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));  
         D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));  
1752    
1753          // And perform the fill  #ifndef UNALIGNED_PROFITABLE
1754          fillrect8_hook(dest_X, dest_Y, dest_X_max, dest_Y_max, color);          // Align on 16-bit boundaries
1755            if (bpp < 16 && (((uintptr)dest) & 1)) {
1756                    FILL_1(dest, 0, color);
1757                    dest += 1; length -= 1;
1758  }  }
1759    
1760  static void accl_fillrect32(accl_params *p)          // Align on 32-bit boundaries
1761  {          if (bpp < 32 && (((uintptr)dest) & 2)) {
1762          D(bug("accl_fillrect32\n"));                  FILL_2(dest, 0, color);
1763                    dest += 2; length -= 2;
1764            }
1765    #endif
1766    
1767          // Get filling parameters          // Fill 8-byte words
1768          int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];          if (length >= 8) {
1769          int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];                  const uint64 c = (((uint64)color) << 32) | color;
1770          int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;                  const int r = (length / 8) % 8;
1771          int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;                  dest += r * 8;
1772          uint32 color = p->pen_mode == 8 ? p->fore_pen : p->back_pen;  
1773          D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));                  int n = ((length / 8) + 7) / 8;
1774          D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));                  switch (r) {
1775                    case 0: do {
1776                                    dest += 64;
1777                                    FILL_8(dest, -8, c);
1778                    case 7: FILL_8(dest, -7, c);
1779                    case 6: FILL_8(dest, -6, c);
1780                    case 5: FILL_8(dest, -5, c);
1781                    case 4: FILL_8(dest, -4, c);
1782                    case 3: FILL_8(dest, -3, c);
1783                    case 2: FILL_8(dest, -2, c);
1784                    case 1: FILL_8(dest, -1, c);
1785                                    } while (--n > 0);
1786                    }
1787            }
1788    
1789          // And perform the fill          // 32-bit cell to fill?
1790          fillrect32_hook(dest_X, dest_Y, dest_X_max, dest_Y_max, color);          if (length & 4) {
1791                    FILL_4(dest, 0, color);
1792                    if (bpp <= 16)
1793                            dest += 4;
1794            }
1795    
1796            // 16-bit cell to fill?
1797            if (bpp <= 16 && (length & 2)) {
1798                    FILL_2(dest, 0, color);
1799                    if (bpp <= 8)
1800                            dest += 2;
1801            }
1802    
1803            // 8-bit cell to fill?
1804            if (bpp <= 8 && (length & 1))
1805                    FILL_1(dest, 0, color);
1806    
1807    #undef FILL_1
1808    #undef FILL_2
1809    #undef FILL_4
1810    #undef FILL_8
1811  }  }
1812    
1813  static void accl_invrect(accl_params *p)  void NQD_fillrect(uint32 p)
1814  {  {
1815          D(bug("accl_invrect\n"));          D(bug("accl_fillrect %08x\n", p));
1816    
1817          // Get inversion parameters          // Get filling parameters
1818          int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];          int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1819          int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];          int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1820          int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;          int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1821          int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;          int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1822            uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1823          D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));          D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1824          D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));          D(bug(" width %d, height %d\n", width, height));
1825            D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
         //!!?? pen_mode == 14  
1826    
1827          // And perform the inversion          // And perform the fill
1828          invrect_hook(dest_X, dest_Y, dest_X_max, dest_Y_max);          const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1829            const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1830            uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1831            width *= bpp;
1832            switch (bpp) {
1833            case 1:
1834                    for (int i = 0; i < height; i++) {
1835                            memset(dest, color, width);
1836                            dest += dest_row_bytes;
1837                    }
1838                    break;
1839            case 2:
1840                    for (int i = 0; i < height; i++) {
1841                            do_fillrect<16>(dest, color, width);
1842                            dest += dest_row_bytes;
1843                    }
1844                    break;
1845            case 4:
1846                    for (int i = 0; i < height; i++) {
1847                            do_fillrect<32>(dest, color, width);
1848                            dest += dest_row_bytes;
1849                    }
1850                    break;
1851            }
1852  }  }
1853    
1854  static bool accl_fillrect_hook(accl_params *p)  bool NQD_fillrect_hook(uint32 p)
1855  {  {
1856          D(bug("accl_fillrect_hook %p\n", p));          D(bug("accl_fillrect_hook %08x\n", p));
1857    
1858          // Check if we can accelerate this fillrect          // Check if we can accelerate this fillrect
1859          if (p->dest_base_addr == screen_base && ((uint32 *)p)[0x284 >> 2] != 0 && display_type == DIS_SCREEN) {          if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1860                  if (p->transfer_mode == 8) {                  const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1861                    if (transfer_mode == 8) {
1862                          // Fill                          // Fill
1863                          if (p->dest_pixel_size == 8 && fillrect8_hook != NULL) {                          WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
                                 p->draw_proc = accl_fillrect8;  
                                 return true;  
                         } else if (p->dest_pixel_size == 32 && fillrect32_hook != NULL) {  
                                 p->draw_proc = accl_fillrect32;  
1864                                  return true;                                  return true;
1865                          }                          }
1866                  } else if (p->transfer_mode == 10 && invrect_hook != NULL) {                  else if (transfer_mode == 10) {
1867                          // Invert                          // Invert
1868                          p->draw_proc = accl_invrect;                          WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1869                          return true;                          return true;
1870                  }                  }
1871          }          }
1872          return false;          return false;
1873  }  }
1874    
1875  // Wait for graphics operation to finish  // Rectangle blitting
1876  static bool accl_sync_hook(void *arg)  // TODO: optimize for VOSF and target pixmap == screen
1877    void NQD_bitblt(uint32 p)
1878    {
1879            D(bug("accl_bitblt %08x\n", p));
1880    
1881            // Get blitting parameters
1882            int16 src_X  = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1883            int16 src_Y  = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1884            int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1885            int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1886            int16 width  = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1887            int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1888            D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1889            D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1890            D(bug(" width %d, height %d\n", width, height));
1891    
1892            // And perform the blit
1893            const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1894            width *= bpp;
1895            if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1896                    const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1897                    const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1898                    uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1899                    uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1900                    for (int i = 0; i < height; i++) {
1901                            memmove(dst, src, width);
1902                            src += src_row_bytes;
1903                            dst += dst_row_bytes;
1904                    }
1905            }
1906            else {
1907                    const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1908                    const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1909                    uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1910                    uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1911                    for (int i = height - 1; i >= 0; i--) {
1912                            memmove(dst, src, width);
1913                            src -= src_row_bytes;
1914                            dst -= dst_row_bytes;
1915                    }
1916            }
1917    }
1918    
1919    /*
1920      BitBlt transfer modes:
1921      0 : srcCopy
1922      1 : srcOr
1923      2 : srcXor
1924      3 : srcBic
1925      4 : notSrcCopy
1926      5 : notSrcOr
1927      6 : notSrcXor
1928      7 : notSrcBic
1929      32 : blend
1930      33 : addPin
1931      34 : addOver
1932      35 : subPin
1933      36 : transparent
1934      37 : adMax
1935      38 : subOver
1936      39 : adMin
1937      50 : hilite
1938    */
1939    
1940    bool NQD_bitblt_hook(uint32 p)
1941  {  {
1942          D(bug("accl_sync_hook %p\n", arg));          D(bug("accl_draw_hook %08x\n", p));
1943          if (sync_hook != NULL)  
1944                  sync_hook();          // Check if we can accelerate this bitblt
1945            if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1946                    ReadMacInt32(p + 0x130) == 0 &&
1947                    ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1948                    ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1949                    (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1950                    ReadMacInt32(p + acclTransferMode) == 0 &&                                                                               // srcCopy?
1951                    ReadMacInt32(p + 0x15c) > 0) {
1952    
1953                    // Yes, set function pointer
1954                    WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1955          return true;          return true;
1956  }  }
1957            return false;
1958    }
1959    
1960  static struct accl_hook_info bitblt_hook_info = {accl_bitblt_hook, accl_sync_hook, ACCL_BITBLT};  // Wait for graphics operation to finish
1961  static struct accl_hook_info fillrect_hook_info = {accl_fillrect_hook, accl_sync_hook, ACCL_FILLRECT};  bool NQD_sync_hook(uint32 arg)
1962  #endif  {
1963            D(bug("accl_sync_hook %08x\n", arg));
1964            return true;
1965    }
1966    
1967  void VideoInstallAccel(void)  void VideoInstallAccel(void)
1968  {  {
1969          // Install acceleration hooks          // Install acceleration hooks
1970          if (PrefsFindBool("gfxaccel")) {          if (PrefsFindBool("gfxaccel")) {
1971                  D(bug("Video: Installing acceleration hooks\n"));                  D(bug("Video: Installing acceleration hooks\n"));
1972  //!!    NQDMisc(6, &bitblt_hook_info);                  uint32 base;
1973  //              NQDMisc(6, &fillrect_hook_info);  
1974                    SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1975                    base = bitblt_hook_info.addr();
1976                    WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
1977                    WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1978                    WriteMacInt32(base + 8, ACCL_BITBLT);
1979                    NQDMisc(6, bitblt_hook_info.ptr());
1980    
1981                    SheepVar fillrect_hook_info(sizeof(accl_hook_info));
1982                    base = fillrect_hook_info.addr();
1983                    WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
1984                    WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1985                    WriteMacInt32(base + 8, ACCL_FILLRECT);
1986                    NQDMisc(6, fillrect_hook_info.ptr());
1987          }          }
1988  }  }
1989    
# Line 1203  int16 video_mode_change(VidLocals *csSav Line 2046  int16 video_mode_change(VidLocals *csSav
2046    
2047  void video_set_palette(void)  void video_set_palette(void)
2048  {  {
2049            LOCK_PALETTE;
2050    
2051            // Convert colors to XColor array
2052            int mode = get_current_mode();
2053            int num_in = palette_size(mode);
2054            int num_out = 256;
2055            bool stretch = false;
2056            if (IsDirectMode(mode)) {
2057                    // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
2058                    num_out = vis->map_entries;
2059                    stretch = true;
2060            }
2061            XColor *p = x_palette;
2062            for (int i=0; i<num_out; i++) {
2063                    int c = (stretch ? (i * num_in) / num_out : i);
2064                    p->red = mac_pal[c].red * 0x0101;
2065                    p->green = mac_pal[c].green * 0x0101;
2066                    p->blue = mac_pal[c].blue * 0x0101;
2067                    p++;
2068            }
2069    
2070    #ifdef ENABLE_VOSF
2071            // Recalculate pixel color expansion map
2072            if (!IsDirectMode(mode) && xdepth > 8) {
2073                    for (int i=0; i<256; i++) {
2074                            int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
2075                            ExpandMap[i] = map_rgb(mac_pal[c].red, mac_pal[c].green, mac_pal[c].blue);
2076                    }
2077    
2078                    // We have to redraw everything because the interpretation of pixel values changed
2079                    LOCK_VOSF;
2080                    PFLAG_SET_ALL;
2081                    UNLOCK_VOSF;
2082                    memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2083            }
2084    #endif
2085    
2086            // Tell redraw thread to change palette
2087          palette_changed = true;          palette_changed = true;
2088    
2089            UNLOCK_PALETTE;
2090    }
2091    
2092    
2093    /*
2094     *  Can we set the MacOS cursor image into the window?
2095     */
2096    
2097    bool video_can_change_cursor(void)
2098    {
2099            return hw_mac_cursor_accl && (display_type != DIS_SCREEN);
2100  }  }
2101    
2102    
# Line 1330  static void update_display(void) Line 2223  static void update_display(void)
2223    
2224          // Refresh display          // Refresh display
2225          if (high && wide) {          if (high && wide) {
2226                    XDisplayLock();
2227                  if (have_shm)                  if (have_shm)
2228                          XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);                          XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
2229                  else                  else
2230                          XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);                          XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
2231                    XDisplayUnlock();
2232            }
2233    }
2234    
2235    const int VIDEO_REFRESH_HZ = 60;
2236    const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2237    
2238    static void handle_palette_changes(void)
2239    {
2240            LOCK_PALETTE;
2241    
2242            if (palette_changed && !emul_suspended) {
2243                    palette_changed = false;
2244    
2245                    int mode = get_current_mode();
2246                    if (color_class == PseudoColor || color_class == DirectColor) {
2247                            int num = vis->map_entries;
2248                            bool set_clut = true;
2249                            if (!IsDirectMode(mode) && color_class == DirectColor) {
2250                                    if (display_type == DIS_WINDOW)
2251                                            set_clut = false; // Indexed mode on true color screen, don't set CLUT
2252                            }
2253    
2254                            if (set_clut) {
2255                                    XDisplayLock();
2256                                    XStoreColors(x_display, cmap[0], x_palette, num);
2257                                    XStoreColors(x_display, cmap[1], x_palette, num);
2258                                    XSync(x_display, false);
2259                                    XDisplayUnlock();
2260                            }
2261                    }
2262    
2263    #ifdef ENABLE_XF86_DGA
2264                    if (display_type == DIS_SCREEN) {
2265                            current_dga_cmap ^= 1;
2266                            if (!IsDirectMode(mode) && cmap[current_dga_cmap])
2267                                    XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2268                    }
2269    #endif
2270          }          }
2271    
2272            UNLOCK_PALETTE;
2273  }  }
2274    
2275  static void *redraw_func(void *arg)  static void *redraw_func(void *arg)
2276  {  {
2277          int tick_counter = 0;          int fd = ConnectionNumber(x_display);
         struct timespec req = {0, 16666667};  
2278    
2279          for (;;) {          uint64 start = GetTicks_usec();
2280            int64 ticks = 0;
2281            uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2282    
2283                  // Wait          for (;;) {
                 nanosleep(&req, NULL);  
2284    
2285                  // Pause if requested (during video mode switches)                  // Pause if requested (during video mode switches)
2286                  while (thread_stop_req)                  while (thread_stop_req)
2287                          thread_stop_ack = true;                          thread_stop_ack = true;
2288    
2289                    int64 delay = next - GetTicks_usec();
2290                    if (delay < -VIDEO_REFRESH_DELAY) {
2291    
2292                            // We are lagging far behind, so we reset the delay mechanism
2293                            next = GetTicks_usec();
2294    
2295                    } else if (delay <= 0) {
2296    
2297                            // Delay expired, refresh display
2298                            next += VIDEO_REFRESH_DELAY;
2299                            ticks++;
2300    
2301                  // Handle X11 events                  // Handle X11 events
2302                  handle_events();                  handle_events();
2303    
# Line 1358  static void *redraw_func(void *arg) Line 2305  static void *redraw_func(void *arg)
2305                  if (quit_full_screen) {                  if (quit_full_screen) {
2306                          quit_full_screen = false;                          quit_full_screen = false;
2307                          if (display_type == DIS_SCREEN) {                          if (display_type == DIS_SCREEN) {
2308                                            XDisplayLock();
2309  #ifdef ENABLE_XF86_DGA  #ifdef ENABLE_XF86_DGA
2310                                  XF86DGADirectVideo(x_display, screen, 0);                                  XF86DGADirectVideo(x_display, screen, 0);
2311                                  XUngrabPointer(x_display, CurrentTime);                                  XUngrabPointer(x_display, CurrentTime);
2312                                  XUngrabKeyboard(x_display, CurrentTime);                                  XUngrabKeyboard(x_display, CurrentTime);
2313                                            XUnmapWindow(x_display, the_win);
2314                                            wait_unmapped(the_win);
2315                                            XDestroyWindow(x_display, the_win);
2316  #endif  #endif
2317                                  XSync(x_display, false);                                  XSync(x_display, false);
2318                                            XDisplayUnlock();
2319                                  quit_full_screen_ack = true;                                  quit_full_screen_ack = true;
2320                                  return NULL;                                  return NULL;
2321                          }                          }
2322                  }                  }
2323    
2324                  // Refresh display and set cursor image in window mode                  // Refresh display and set cursor image in window mode
2325                            static int tick_counter = 0;
2326                  if (display_type == DIS_WINDOW) {                  if (display_type == DIS_WINDOW) {
2327                          tick_counter++;                          tick_counter++;
2328                          if (tick_counter >= frame_skip) {                          if (tick_counter >= frame_skip) {
2329                                  tick_counter = 0;                                  tick_counter = 0;
2330    
2331                                  // Update display                                  // Update display
2332    #ifdef ENABLE_VOSF
2333                                            if (use_vosf) {
2334                                                    XDisplayLock();
2335                                                    if (mainBuffer.dirty) {
2336                                                            LOCK_VOSF;
2337                                                            update_display_window_vosf();
2338                                                            UNLOCK_VOSF;
2339                                                            XSync(x_display, false); // Let the server catch up
2340                                                    }
2341                                                    XDisplayUnlock();
2342                                            }
2343                                            else
2344    #endif
2345                                  update_display();                                  update_display();
2346    
2347                                  // Set new cursor image if it was changed                                  // Set new cursor image if it was changed
2348                                  if (cursor_changed) {                                          if (hw_mac_cursor_accl && cursor_changed) {
2349                                          cursor_changed = false;                                          cursor_changed = false;
2350                                          memcpy(cursor_image->data, MacCursor + 4, 32);                                          memcpy(cursor_image->data, MacCursor + 4, 32);
2351                                          memcpy(cursor_mask_image->data, MacCursor + 36, 32);                                          memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2352                                                    XDisplayLock();
2353                                          XFreeCursor(x_display, mac_cursor);                                          XFreeCursor(x_display, mac_cursor);
2354                                          XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);                                          XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2355                                          XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);                                          XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2356                                          mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);                                          mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2357                                          XDefineCursor(x_display, the_win, mac_cursor);                                          XDefineCursor(x_display, the_win, mac_cursor);
2358                                                    XDisplayUnlock();
2359                                  }                                  }
2360                          }                          }
2361                  }                  }
2362    #ifdef ENABLE_VOSF
2363                  // Set new palette if it was changed                          else if (use_vosf) {
2364                  if (palette_changed && !emul_suspended) {                                  // Update display (VOSF variant)
2365                          palette_changed = false;                                  if (++tick_counter >= frame_skip) {
2366                          XColor c[256];                                          tick_counter = 0;
2367                          for (int i=0; i<256; i++) {                                          if (mainBuffer.dirty) {
2368                                  c[i].pixel = i;                                                  LOCK_VOSF;
2369                                  c[i].red = mac_pal[i].red * 0x0101;                                                  update_display_dga_vosf();
2370                                  c[i].green = mac_pal[i].green * 0x0101;                                                  UNLOCK_VOSF;
                                 c[i].blue = mac_pal[i].blue * 0x0101;  
                                 c[i].flags = DoRed | DoGreen | DoBlue;  
                         }  
                         if (depth == 8) {  
                                 XStoreColors(x_display, cmap[0], c, 256);  
                                 XStoreColors(x_display, cmap[1], c, 256);  
 #ifdef ENABLE_XF86_DGA  
                                 if (display_type == DIS_SCREEN) {  
                                         current_dga_cmap ^= 1;  
                                         XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);  
2371                                  }                                  }
 #endif  
2372                          }                          }
2373                  }                  }
2374    #endif
2375    
2376                            // Set new palette if it was changed
2377                            handle_palette_changes();
2378    
2379                    } else {
2380    
2381                            // No display refresh pending, check for X events
2382                            fd_set readfds;
2383                            FD_ZERO(&readfds);
2384                            FD_SET(fd, &readfds);
2385                            struct timeval timeout;
2386                            timeout.tv_sec = 0;
2387                            timeout.tv_usec = delay;
2388                            if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2389                                    handle_events();
2390                    }
2391          }          }
2392          return NULL;          return NULL;
2393  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.25

Christian Bauer">Christian Bauer
ViewVC Help
Powered by ViewVC 1.1.15