ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
Revision: 1.23
Committed: 2004-05-16T15:48:25Z (20 years, 6 months ago) by gbeauche
Branch: MAIN
Changes since 1.22: +2 -2 lines
Log Message:
Use memmove() in NQD_bitblt() in order to avoid possible overlaps

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * video_x.cpp - Video/graphics emulation, X11 specific stuff
3     *
4 cebix 1.11 * SheepShaver (C) 1997-2004 Marc Hellwig and Christian Bauer
5 cebix 1.1 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21 gbeauche 1.6 #include "sysdeps.h"
22    
23 cebix 1.1 #include <X11/Xlib.h>
24     #include <X11/Xutil.h>
25     #include <X11/keysym.h>
26     #include <X11/extensions/XShm.h>
27     #include <sys/ipc.h>
28     #include <sys/shm.h>
29 gbeauche 1.6 #include <errno.h>
30 gbeauche 1.7 #include <pthread.h>
31 gbeauche 1.6
32 gbeauche 1.13 #include <algorithm>
33    
34 gbeauche 1.6 #ifdef ENABLE_XF86_DGA
35 gbeauche 1.15 # include <X11/extensions/xf86dga.h>
36 gbeauche 1.6 #endif
37    
38     #ifdef ENABLE_XF86_VIDMODE
39     # include <X11/extensions/xf86vmode.h>
40     #endif
41 cebix 1.1
42     #include "main.h"
43     #include "adb.h"
44     #include "prefs.h"
45     #include "user_strings.h"
46     #include "about_window.h"
47     #include "video.h"
48     #include "video_defs.h"
49    
50     #define DEBUG 0
51     #include "debug.h"
52    
53 gbeauche 1.13 #ifndef NO_STD_NAMESPACE
54     using std::sort;
55     #endif
56    
57 cebix 1.1
58 gbeauche 1.6 // Constants
59     const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
60 gbeauche 1.22 static const bool hw_mac_cursor_accl = true; // Flag: Enable MacOS to X11 copy of cursor?
61 cebix 1.1
62     // Global variables
63     static int32 frame_skip;
64 gbeauche 1.8 static int16 mouse_wheel_mode;
65     static int16 mouse_wheel_lines;
66 cebix 1.1 static bool redraw_thread_active = false; // Flag: Redraw thread installed
67 gbeauche 1.15 static pthread_attr_t redraw_thread_attr; // Redraw thread attributes
68 cebix 1.1 static pthread_t redraw_thread; // Redraw thread
69    
70 gbeauche 1.4 static bool local_X11; // Flag: X server running on local machine?
71 cebix 1.1 static volatile bool thread_stop_req = false;
72     static volatile bool thread_stop_ack = false; // Acknowledge for thread_stop_req
73    
74     static bool has_dga = false; // Flag: Video DGA capable
75     static bool has_vidmode = false; // Flag: VidMode extension available
76    
77 gbeauche 1.3 #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 cebix 1.1 static bool palette_changed = false; // Flag: Palette changed, redraw thread must update palette
84     static bool ctrl_down = false; // Flag: Ctrl key pressed
85     static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
86     static volatile bool quit_full_screen_ack = false; // Acknowledge for quit_full_screen
87     static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
88    
89     static bool emul_suspended = false; // Flag: emulator suspended
90     static Window suspend_win; // "Suspend" window
91     static void *fb_save = NULL; // Saved frame buffer for suspend
92 gbeauche 1.6 static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
93     static int keycode_table[256]; // X keycode -> Mac keycode translation table
94 cebix 1.1
95     // X11 variables
96     static int screen; // Screen number
97     static int xdepth; // Depth of X screen
98     static int depth; // Depth of Mac frame buffer
99     static Window rootwin, the_win; // Root window and our window
100 gbeauche 1.13 static int num_depths = 0; // Number of available X depths
101     static int *avail_depths = NULL; // List of available X depths
102 cebix 1.1 static XVisualInfo visualInfo;
103     static Visual *vis;
104 gbeauche 1.13 static int color_class;
105     static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
106 cebix 1.1 static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode
107 gbeauche 1.13 static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table
108    
109 cebix 1.1 static XColor black, white;
110     static unsigned long black_pixel, white_pixel;
111     static int eventmask;
112 gbeauche 1.13 static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
113     static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
114 cebix 1.1
115     // Variables for window mode
116     static GC the_gc;
117     static XImage *img = NULL;
118     static XShmSegmentInfo shminfo;
119     static XImage *cursor_image, *cursor_mask_image;
120     static Pixmap cursor_map, cursor_mask_map;
121     static Cursor mac_cursor;
122     static GC cursor_gc, cursor_mask_gc;
123     static bool cursor_changed = false; // Flag: Cursor changed, window_func must update cursor
124     static bool have_shm = false; // Flag: SHM present and usable
125 gbeauche 1.3 static uint8 *the_buffer = NULL; // Pointer to Mac frame buffer
126 cebix 1.1 static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer
127 gbeauche 1.3 static uint32 the_buffer_size; // Size of allocated the_buffer
128 cebix 1.1
129     // Variables for DGA mode
130     static int current_dga_cmap;
131    
132     #ifdef ENABLE_XF86_VIDMODE
133     // Variables for XF86 VidMode support
134     static XF86VidModeModeInfo **x_video_modes; // Array of all available modes
135     static int num_x_video_modes;
136     #endif
137    
138 gbeauche 1.13 // 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 cebix 1.1
153     // Prototypes
154     static void *redraw_func(void *arg);
155    
156    
157 gbeauche 1.4 // From main_unix.cpp
158     extern char *x_display_name;
159 cebix 1.1 extern Display *x_display;
160    
161     // From sys_unix.cpp
162     extern void SysMountFirstFloppy(void);
163    
164 gbeauche 1.9 // From clip_unix.cpp
165     extern void ClipboardSelectionClear(XSelectionClearEvent *);
166     extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
167    
168 cebix 1.1
169 gbeauche 1.3 // Video acceleration through SIGSEGV
170     #ifdef ENABLE_VOSF
171     # include "video_vosf.h"
172     #endif
173    
174    
175 cebix 1.1 /*
176 gbeauche 1.13 * 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 gbeauche 1.16 // 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 gbeauche 1.13 // Map video_mode depth ID to numerical depth value
220     static inline int depth_of_video_mode(int mode)
221     {
222 gbeauche 1.16 int depth;
223 gbeauche 1.13 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 cebix 1.1 * Open display (window or fullscreen)
361     */
362    
363 gbeauche 1.14 // 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 gbeauche 1.13 // Wait until window is mapped/unmapped
372 gbeauche 1.15 static void wait_mapped(Window w)
373 gbeauche 1.13 {
374     XEvent e;
375     do {
376     XMaskEvent(x_display, StructureNotifyMask, &e);
377     } while ((e.type != MapNotify) || (e.xmap.event != w));
378     }
379    
380 gbeauche 1.15 static void wait_unmapped(Window w)
381 gbeauche 1.13 {
382     XEvent e;
383     do {
384     XMaskEvent(x_display, StructureNotifyMask, &e);
385     } while ((e.type != UnmapNotify) || (e.xmap.event != w));
386     }
387    
388 cebix 1.1 // Trap SHM errors
389     static bool shm_error = false;
390     static int (*old_error_handler)(Display *, XErrorEvent *);
391    
392     static int error_handler(Display *d, XErrorEvent *e)
393     {
394     if (e->error_code == BadAccess) {
395     shm_error = true;
396     return 0;
397     } else
398     return old_error_handler(d, e);
399     }
400    
401     // Open window
402     static bool open_window(int width, int height)
403     {
404 gbeauche 1.3 int aligned_width = (width + 15) & ~15;
405     int aligned_height = (height + 15) & ~15;
406    
407 cebix 1.1 // Set absolute mouse mode
408     ADBSetRelMouseMode(false);
409    
410     // Create window
411     XSetWindowAttributes wattr;
412     wattr.event_mask = eventmask = win_eventmask;
413 gbeauche 1.13 wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
414     wattr.border_pixel = 0;
415 cebix 1.1 wattr.backing_store = NotUseful;
416 gbeauche 1.13 wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
417     the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
418     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr);
419 cebix 1.1
420 gbeauche 1.13 // Set window name
421 cebix 1.1 XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
422    
423 gbeauche 1.14 // Set delete protocol property
424     set_window_delete_protocol(the_win);
425    
426 cebix 1.1 // Make window unresizable
427     XSizeHints *hints;
428     if ((hints = XAllocSizeHints()) != NULL) {
429     hints->min_width = width;
430     hints->max_width = width;
431     hints->min_height = height;
432     hints->max_height = height;
433     hints->flags = PMinSize | PMaxSize;
434     XSetWMNormalHints(x_display, the_win, hints);
435     XFree((char *)hints);
436     }
437    
438 gbeauche 1.13 // Show window
439     XMapWindow(x_display, the_win);
440     wait_mapped(the_win);
441    
442 gbeauche 1.5 // 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 cebix 1.1 // Try to create and attach SHM image
447     have_shm = false;
448 gbeauche 1.5 if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) {
449 cebix 1.1
450     // Create SHM image ("height + 2" for safety)
451 gbeauche 1.5 img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
452 gbeauche 1.13 shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
453     D(bug(" shm image created\n"));
454 gbeauche 1.3 the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
455     shminfo.shmaddr = img->data = (char *)the_buffer_copy;
456 cebix 1.1 shminfo.readOnly = False;
457    
458     // Try to attach SHM image, catching errors
459     shm_error = false;
460     old_error_handler = XSetErrorHandler(error_handler);
461     XShmAttach(x_display, &shminfo);
462     XSync(x_display, false);
463     XSetErrorHandler(old_error_handler);
464     if (shm_error) {
465     shmdt(shminfo.shmaddr);
466     XDestroyImage(img);
467     shminfo.shmid = -1;
468     } else {
469     have_shm = true;
470     shmctl(shminfo.shmid, IPC_RMID, 0);
471     }
472 gbeauche 1.13 D(bug(" shm image attached\n"));
473 cebix 1.1 }
474    
475     // Create normal X image if SHM doesn't work ("height + 2" for safety)
476     if (!have_shm) {
477 gbeauche 1.13 int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth));
478 gbeauche 1.3 the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
479     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 gbeauche 1.13 D(bug(" X image created\n"));
481 cebix 1.1 }
482    
483     // 1-Bit mode is big-endian
484 gbeauche 1.13 if (need_msb_image) {
485 cebix 1.1 img->byte_order = MSBFirst;
486     img->bitmap_bit_order = MSBFirst;
487     }
488    
489 gbeauche 1.3 #ifdef ENABLE_VOSF
490     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 cebix 1.1
504     // Create GC
505     the_gc = XCreateGC(x_display, the_win, 0, 0);
506 gbeauche 1.13 XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
507 cebix 1.1
508     // Create cursor
509 gbeauche 1.22 if (hw_mac_cursor_accl) {
510 gbeauche 1.21 cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
511     cursor_image->byte_order = MSBFirst;
512     cursor_image->bitmap_bit_order = MSBFirst;
513     cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 36, 16, 16, 16, 2);
514     cursor_mask_image->byte_order = MSBFirst;
515     cursor_mask_image->bitmap_bit_order = MSBFirst;
516     cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
517     cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
518     cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
519     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);
521     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 cebix 1.1
533 gbeauche 1.3 // 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 cebix 1.1 // Set bytes per row
545     XSync(x_display, false);
546     return true;
547     }
548    
549     // Open DGA display (!! should use X11 VidMode extensions to set mode)
550     static bool open_dga(int width, int height)
551     {
552     #ifdef ENABLE_XF86_DGA
553     // Set relative mouse mode
554     ADBSetRelMouseMode(true);
555    
556 gbeauche 1.15 // 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 cebix 1.1 #ifdef ENABLE_XF86_VIDMODE
570     // Switch to best mode
571     if (has_vidmode) {
572     int best = 0;
573     for (int i=1; i<num_x_video_modes; i++) {
574     if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
575     x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
576     best = i;
577     }
578     }
579     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
580     XF86VidModeSetViewPort(x_display, screen, 0, 0);
581     }
582     #endif
583    
584     // Establish direct screen connection
585 gbeauche 1.15 XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
586     XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
587 cebix 1.1 XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
588     XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
589 gbeauche 1.15
590     int v_width, v_bank, v_size;
591     XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
592 cebix 1.1 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
593     XF86DGASetViewPort(x_display, screen, 0, 0);
594     XF86DGASetVidPage(x_display, screen, 0);
595    
596     // Set colormap
597 gbeauche 1.15 if (!IsDirectMode(get_current_mode())) {
598     XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
599 cebix 1.1 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
600 gbeauche 1.15 }
601     XSync(x_display, false);
602 cebix 1.1
603 gbeauche 1.15 // Init blitting routines
604     int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, DepthModeForPixelDepth(depth));
605 gbeauche 1.3 #if ENABLE_VOSF
606     bool native_byte_order;
607     #ifdef WORDS_BIGENDIAN
608     native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
609     #else
610     native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
611     #endif
612     #if REAL_ADDRESSING || DIRECT_ADDRESSING
613     // 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 gbeauche 1.15 D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
624 gbeauche 1.3 }
625     #else
626     use_vosf = false;
627     #endif
628     #endif
629 gbeauche 1.15
630     // Set frame buffer base
631     D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
632 gbeauche 1.3 screen_base = (uint32)the_buffer;
633 cebix 1.1 VModes[cur_mode].viRowBytes = bytes_per_row;
634     return true;
635     #else
636     ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
637     return false;
638     #endif
639     }
640    
641     static bool open_display(void)
642     {
643 gbeauche 1.13 D(bug("open_display()\n"));
644     const VideoInfo &mode = VModes[cur_mode];
645    
646     // Find best available X visual
647     if (!find_visual_for_depth(mode.viAppleMode)) {
648     ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
649     return false;
650     }
651    
652     // Create color maps
653     if (color_class == PseudoColor || color_class == DirectColor) {
654     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
655     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
656     } else {
657     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
658     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
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 cebix 1.1 }
707 gbeauche 1.3
708 gbeauche 1.13 #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 gbeauche 1.3 bool display_open = false;
720 cebix 1.1 if (display_type == DIS_SCREEN)
721 gbeauche 1.3 display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
722 cebix 1.1 else if (display_type == DIS_WINDOW)
723 gbeauche 1.3 display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
724    
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;
731     }
732     }
733     #endif
734    
735     return display_open;
736 cebix 1.1 }
737    
738    
739     /*
740     * Close display
741     */
742    
743     // Close window
744     static void close_window(void)
745     {
746 gbeauche 1.3 if (have_shm) {
747     XShmDetach(x_display, &shminfo);
748     #ifdef ENABLE_VOSF
749     the_host_buffer = NULL; // don't free() in driver_base dtor
750     #else
751     the_buffer_copy = NULL; // don't free() in driver_base dtor
752     #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 gbeauche 1.13 XFlush(x_display);
767     XSync(x_display, false);
768 cebix 1.1 }
769    
770     // Close DGA mode
771     static void close_dga(void)
772     {
773     #ifdef ENABLE_XF86_DGA
774     XF86DGADirectVideo(x_display, screen, 0);
775     XUngrabPointer(x_display, CurrentTime);
776     XUngrabKeyboard(x_display, CurrentTime);
777     #endif
778    
779     #ifdef ENABLE_XF86_VIDMODE
780     if (has_vidmode)
781     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
782     #endif
783 gbeauche 1.3
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 cebix 1.1 }
795    
796     static void close_display(void)
797     {
798     if (display_type == DIS_SCREEN)
799     close_dga();
800     else if (display_type == DIS_WINDOW)
801     close_window();
802 gbeauche 1.3
803 gbeauche 1.15 // 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 gbeauche 1.13 // 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 gbeauche 1.3 #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 cebix 1.1 }
855    
856    
857     /*
858     * Initialization
859     */
860    
861 gbeauche 1.6 // 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 gbeauche 1.15 // 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 gbeauche 1.13 // 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 cebix 1.1 {
962     if (allow & test) {
963     p->viType = type;
964     switch (apple_id) {
965     case APPLE_W_640x480:
966     case APPLE_640x480:
967     p->viXsize = 640;
968     p->viYsize = 480;
969     break;
970     case APPLE_W_800x600:
971     case APPLE_800x600:
972     p->viXsize = 800;
973     p->viYsize = 600;
974     break;
975     case APPLE_1024x768:
976     p->viXsize = 1024;
977     p->viYsize = 768;
978     break;
979 gbeauche 1.15 case APPLE_1152x768:
980     p->viXsize = 1152;
981     p->viYsize = 768;
982     break;
983 cebix 1.1 case APPLE_1152x900:
984     p->viXsize = 1152;
985     p->viYsize = 900;
986     break;
987     case APPLE_1280x1024:
988     p->viXsize = 1280;
989     p->viYsize = 1024;
990     break;
991     case APPLE_1600x1200:
992     p->viXsize = 1600;
993     p->viYsize = 1200;
994     break;
995     }
996 gbeauche 1.13 p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
997 cebix 1.1 p->viAppleMode = apple_mode;
998     p->viAppleID = apple_id;
999     p++;
1000     }
1001     }
1002    
1003 gbeauche 1.13 // 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 cebix 1.1 static bool has_mode(int x, int y)
1011     {
1012     #ifdef ENABLE_XF86_VIDMODE
1013     for (int i=0; i<num_x_video_modes; i++)
1014     if (x_video_modes[i]->hdisplay >= x && x_video_modes[i]->vdisplay >= y)
1015     return true;
1016     return false;
1017     #else
1018     return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y;
1019     #endif
1020     }
1021    
1022     bool VideoInit(void)
1023     {
1024 gbeauche 1.3 #ifdef ENABLE_VOSF
1025     // Zero the mainBuffer structure
1026     mainBuffer.dirtyPages = NULL;
1027     mainBuffer.pageInfo = NULL;
1028     #endif
1029    
1030 gbeauche 1.4 // 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 gbeauche 1.6 // Init keycode translation
1035     keycode_init();
1036    
1037 gbeauche 1.8 // 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 cebix 1.1 // Init variables
1047     private_data = NULL;
1048     video_activated = true;
1049    
1050     // Find screen and root window
1051     screen = XDefaultScreen(x_display);
1052     rootwin = XRootWindow(x_display, screen);
1053    
1054 gbeauche 1.13 // 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 cebix 1.1 // Get screen depth
1063     xdepth = DefaultDepth(x_display, screen);
1064    
1065     #ifdef ENABLE_XF86_DGA
1066     // DGA available?
1067     int event_base, error_base;
1068 gbeauche 1.4 if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1069 cebix 1.1 int dga_flags = 0;
1070     XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1071     has_dga = dga_flags & XF86DGADirectPresent;
1072     } else
1073     has_dga = false;
1074     #endif
1075    
1076     #ifdef ENABLE_XF86_VIDMODE
1077     // VidMode available?
1078     int vm_event_base, vm_error_base;
1079     has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
1080     if (has_vidmode)
1081     XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1082     #endif
1083    
1084     // Find black and white colors
1085     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1086     XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
1087     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
1088     XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
1089     black_pixel = BlackPixel(x_display, screen);
1090     white_pixel = WhitePixel(x_display, screen);
1091    
1092     // Mac screen depth follows X depth (for now)
1093 gbeauche 1.13 int default_mode = APPLE_8_BIT;
1094     switch (DefaultDepth(x_display, screen)) {
1095     case 1:
1096     default_mode = APPLE_1_BIT;
1097     break;
1098     case 8:
1099     default_mode = APPLE_8_BIT;
1100     break;
1101     case 15: case 16:
1102     default_mode = APPLE_16_BIT;
1103     break;
1104     case 24: case 32:
1105     default_mode = APPLE_32_BIT;
1106     break;
1107 cebix 1.1 }
1108    
1109     // Construct video mode table
1110     uint32 window_modes = PrefsFindInt32("windowmodes");
1111     uint32 screen_modes = PrefsFindInt32("screenmodes");
1112     if (!has_dga)
1113     screen_modes = 0;
1114     if (window_modes == 0 && screen_modes == 0)
1115     window_modes |= 3; // Allow at least 640x480 and 800x600 window modes
1116    
1117     VideoInfo *p = VModes;
1118 gbeauche 1.13 for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1119     if (find_visual_for_depth(d))
1120     add_window_modes(p, window_modes, d);
1121    
1122 cebix 1.1 if (has_vidmode) {
1123     if (has_mode(640, 480))
1124 gbeauche 1.13 add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1125 cebix 1.1 if (has_mode(800, 600))
1126 gbeauche 1.13 add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN);
1127 cebix 1.1 if (has_mode(1024, 768))
1128 gbeauche 1.13 add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN);
1129 gbeauche 1.15 if (has_mode(1152, 768))
1130     add_mode(p, screen_modes, 64, default_mode, APPLE_1152x768, DIS_SCREEN);
1131 cebix 1.1 if (has_mode(1152, 900))
1132 gbeauche 1.13 add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN);
1133 cebix 1.1 if (has_mode(1280, 1024))
1134 gbeauche 1.13 add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN);
1135 cebix 1.1 if (has_mode(1600, 1200))
1136 gbeauche 1.13 add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN);
1137 cebix 1.1 } else if (screen_modes) {
1138     int xsize = DisplayWidth(x_display, screen);
1139     int ysize = DisplayHeight(x_display, screen);
1140 gbeauche 1.15 int apple_id = find_apple_resolution(xsize, ysize);
1141 cebix 1.1 p->viType = DIS_SCREEN;
1142     p->viRowBytes = 0;
1143     p->viXsize = xsize;
1144     p->viYsize = ysize;
1145 gbeauche 1.13 p->viAppleMode = default_mode;
1146 cebix 1.1 p->viAppleID = apple_id;
1147     p++;
1148     }
1149     p->viType = DIS_INVALID; // End marker
1150     p->viRowBytes = 0;
1151     p->viXsize = p->viYsize = 0;
1152     p->viAppleMode = 0;
1153     p->viAppleID = 0;
1154    
1155 gbeauche 1.13 // Find default mode (window 640x480)
1156     cur_mode = -1;
1157 gbeauche 1.15 if (has_dga && screen_modes) {
1158     int screen_width = DisplayWidth(x_display, screen);
1159     int screen_height = DisplayHeight(x_display, screen);
1160     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 gbeauche 1.13 }
1164 gbeauche 1.15 if (cur_mode == -1)
1165     cur_mode = find_mode(default_mode, APPLE_W_640x480, DIS_WINDOW);
1166 gbeauche 1.13 assert(cur_mode != -1);
1167    
1168     #if DEBUG
1169     D(bug("Available video modes:\n"));
1170     for (p = VModes; p->viType != DIS_INVALID; p++) {
1171     int bits = depth_of_video_mode(p->viAppleMode);
1172     D(bug(" %dx%d (ID %02x), %d colors\n", p->viXsize, p->viYsize, p->viAppleID, 1 << bits));
1173     }
1174     #endif
1175    
1176 cebix 1.1 // Open window/screen
1177     if (!open_display())
1178     return false;
1179    
1180     #if 0
1181     // Ignore errors from now on
1182     XSetErrorHandler(ignore_errors);
1183     #endif
1184    
1185     // Start periodic thread
1186     XSync(x_display, false);
1187 gbeauche 1.15 Set_pthread_attr(&redraw_thread_attr, 0);
1188     redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1189 cebix 1.1 D(bug("Redraw thread installed (%ld)\n", redraw_thread));
1190     return true;
1191     }
1192    
1193    
1194     /*
1195     * Deinitialization
1196     */
1197    
1198     void VideoExit(void)
1199     {
1200     // Stop redraw thread
1201     if (redraw_thread_active) {
1202     pthread_cancel(redraw_thread);
1203     pthread_join(redraw_thread, NULL);
1204     redraw_thread_active = false;
1205     }
1206    
1207 gbeauche 1.3 #ifdef ENABLE_VOSF
1208     if (use_vosf) {
1209     // Deinitialize VOSF
1210     video_vosf_exit();
1211     }
1212     #endif
1213    
1214 cebix 1.1 // Close window and server connection
1215     if (x_display != NULL) {
1216     XSync(x_display, false);
1217     close_display();
1218     XFlush(x_display);
1219     XSync(x_display, false);
1220     }
1221     }
1222    
1223    
1224     /*
1225     * Suspend/resume emulator
1226     */
1227    
1228     extern void PauseEmulator(void);
1229     extern void ResumeEmulator(void);
1230    
1231     static void suspend_emul(void)
1232     {
1233     if (display_type == DIS_SCREEN) {
1234     // Release ctrl key
1235     ADBKeyUp(0x36);
1236     ctrl_down = false;
1237    
1238     // Pause MacOS thread
1239     PauseEmulator();
1240     emul_suspended = true;
1241    
1242     // Save frame buffer
1243     fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1244     if (fb_save)
1245     memcpy(fb_save, (void *)screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1246    
1247     // Close full screen display
1248     #ifdef ENABLE_XF86_DGA
1249     XF86DGADirectVideo(x_display, screen, 0);
1250     XUngrabPointer(x_display, CurrentTime);
1251     XUngrabKeyboard(x_display, CurrentTime);
1252     #endif
1253     XSync(x_display, false);
1254    
1255     // Open "suspend" window
1256     XSetWindowAttributes wattr;
1257     wattr.event_mask = KeyPressMask;
1258     wattr.background_pixel = black_pixel;
1259     wattr.border_pixel = black_pixel;
1260     wattr.backing_store = Always;
1261     wattr.backing_planes = xdepth;
1262     wattr.colormap = DefaultColormap(x_display, screen);
1263     XSync(x_display, false);
1264     suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1265     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1266     CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1267     XSync(x_display, false);
1268     XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1269     XMapRaised(x_display, suspend_win);
1270     XSync(x_display, false);
1271     }
1272     }
1273    
1274     static void resume_emul(void)
1275     {
1276     // Close "suspend" window
1277     XDestroyWindow(x_display, suspend_win);
1278     XSync(x_display, false);
1279    
1280     // Reopen full screen display
1281     XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1282     XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1283 gbeauche 1.12 #ifdef ENABLE_XF86_DGA
1284 cebix 1.1 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1285     XF86DGASetViewPort(x_display, screen, 0, 0);
1286 gbeauche 1.12 #endif
1287 cebix 1.1 XSync(x_display, false);
1288    
1289 gbeauche 1.3 // the_buffer already contains the data to restore. i.e. since a temporary
1290     // frame buffer is used when VOSF is actually used, fb_save is therefore
1291     // not necessary.
1292     #ifdef ENABLE_VOSF
1293     if (use_vosf) {
1294     LOCK_VOSF;
1295     PFLAG_SET_ALL;
1296     UNLOCK_VOSF;
1297     memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1298     }
1299     #endif
1300    
1301 cebix 1.1 // Restore frame buffer
1302     if (fb_save) {
1303 gbeauche 1.3 #ifdef ENABLE_VOSF
1304     // Don't copy fb_save to the temporary frame buffer in VOSF mode
1305     if (!use_vosf)
1306     #endif
1307 cebix 1.1 memcpy((void *)screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1308     free(fb_save);
1309     fb_save = NULL;
1310     }
1311     if (depth == 8)
1312     palette_changed = true;
1313    
1314     // Resume MacOS thread
1315     emul_suspended = false;
1316     ResumeEmulator();
1317     }
1318    
1319    
1320     /*
1321     * Close screen in full-screen mode
1322     */
1323    
1324     void VideoQuitFullScreen(void)
1325     {
1326     D(bug("VideoQuitFullScreen()\n"));
1327     if (display_type == DIS_SCREEN) {
1328     quit_full_screen = true;
1329     while (!quit_full_screen_ack) ;
1330     }
1331     }
1332    
1333    
1334     /*
1335     * X11 event handling
1336     */
1337    
1338     // Translate key event to Mac keycode
1339     static int kc_decode(KeySym ks)
1340     {
1341     switch (ks) {
1342     case XK_A: case XK_a: return 0x00;
1343     case XK_B: case XK_b: return 0x0b;
1344     case XK_C: case XK_c: return 0x08;
1345     case XK_D: case XK_d: return 0x02;
1346     case XK_E: case XK_e: return 0x0e;
1347     case XK_F: case XK_f: return 0x03;
1348     case XK_G: case XK_g: return 0x05;
1349     case XK_H: case XK_h: return 0x04;
1350     case XK_I: case XK_i: return 0x22;
1351     case XK_J: case XK_j: return 0x26;
1352     case XK_K: case XK_k: return 0x28;
1353     case XK_L: case XK_l: return 0x25;
1354     case XK_M: case XK_m: return 0x2e;
1355     case XK_N: case XK_n: return 0x2d;
1356     case XK_O: case XK_o: return 0x1f;
1357     case XK_P: case XK_p: return 0x23;
1358     case XK_Q: case XK_q: return 0x0c;
1359     case XK_R: case XK_r: return 0x0f;
1360     case XK_S: case XK_s: return 0x01;
1361     case XK_T: case XK_t: return 0x11;
1362     case XK_U: case XK_u: return 0x20;
1363     case XK_V: case XK_v: return 0x09;
1364     case XK_W: case XK_w: return 0x0d;
1365     case XK_X: case XK_x: return 0x07;
1366     case XK_Y: case XK_y: return 0x10;
1367     case XK_Z: case XK_z: return 0x06;
1368    
1369     case XK_1: case XK_exclam: return 0x12;
1370     case XK_2: case XK_at: return 0x13;
1371     case XK_3: case XK_numbersign: return 0x14;
1372     case XK_4: case XK_dollar: return 0x15;
1373     case XK_5: case XK_percent: return 0x17;
1374     case XK_6: return 0x16;
1375     case XK_7: return 0x1a;
1376     case XK_8: return 0x1c;
1377     case XK_9: return 0x19;
1378     case XK_0: return 0x1d;
1379    
1380     case XK_grave: case XK_asciitilde: return 0x0a;
1381     case XK_minus: case XK_underscore: return 0x1b;
1382     case XK_equal: case XK_plus: return 0x18;
1383     case XK_bracketleft: case XK_braceleft: return 0x21;
1384     case XK_bracketright: case XK_braceright: return 0x1e;
1385     case XK_backslash: case XK_bar: return 0x2a;
1386     case XK_semicolon: case XK_colon: return 0x29;
1387     case XK_apostrophe: case XK_quotedbl: return 0x27;
1388     case XK_comma: case XK_less: return 0x2b;
1389     case XK_period: case XK_greater: return 0x2f;
1390     case XK_slash: case XK_question: return 0x2c;
1391    
1392     case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1393     case XK_Return: return 0x24;
1394     case XK_space: return 0x31;
1395     case XK_BackSpace: return 0x33;
1396    
1397     case XK_Delete: return 0x75;
1398     case XK_Insert: return 0x72;
1399     case XK_Home: case XK_Help: return 0x73;
1400     case XK_End: return 0x77;
1401     #ifdef __hpux
1402     case XK_Prior: return 0x74;
1403     case XK_Next: return 0x79;
1404     #else
1405     case XK_Page_Up: return 0x74;
1406     case XK_Page_Down: return 0x79;
1407     #endif
1408    
1409     case XK_Control_L: return 0x36;
1410     case XK_Control_R: return 0x36;
1411     case XK_Shift_L: return 0x38;
1412     case XK_Shift_R: return 0x38;
1413     case XK_Alt_L: return 0x37;
1414     case XK_Alt_R: return 0x37;
1415     case XK_Meta_L: return 0x3a;
1416     case XK_Meta_R: return 0x3a;
1417     case XK_Menu: return 0x32;
1418     case XK_Caps_Lock: return 0x39;
1419     case XK_Num_Lock: return 0x47;
1420    
1421     case XK_Up: return 0x3e;
1422     case XK_Down: return 0x3d;
1423     case XK_Left: return 0x3b;
1424     case XK_Right: return 0x3c;
1425    
1426     case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1427    
1428     case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1429     case XK_F2: return 0x78;
1430     case XK_F3: return 0x63;
1431     case XK_F4: return 0x76;
1432     case XK_F5: return 0x60;
1433     case XK_F6: return 0x61;
1434     case XK_F7: return 0x62;
1435     case XK_F8: return 0x64;
1436     case XK_F9: return 0x65;
1437     case XK_F10: return 0x6d;
1438     case XK_F11: return 0x67;
1439     case XK_F12: return 0x6f;
1440    
1441     case XK_Print: return 0x69;
1442     case XK_Scroll_Lock: return 0x6b;
1443     case XK_Pause: return 0x71;
1444    
1445     #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1446     case XK_KP_0: case XK_KP_Insert: return 0x52;
1447     case XK_KP_1: case XK_KP_End: return 0x53;
1448     case XK_KP_2: case XK_KP_Down: return 0x54;
1449     case XK_KP_3: case XK_KP_Next: return 0x55;
1450     case XK_KP_4: case XK_KP_Left: return 0x56;
1451     case XK_KP_5: case XK_KP_Begin: return 0x57;
1452     case XK_KP_6: case XK_KP_Right: return 0x58;
1453     case XK_KP_7: case XK_KP_Home: return 0x59;
1454     case XK_KP_8: case XK_KP_Up: return 0x5b;
1455     case XK_KP_9: case XK_KP_Prior: return 0x5c;
1456     case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1457     #else
1458     case XK_KP_0: return 0x52;
1459     case XK_KP_1: return 0x53;
1460     case XK_KP_2: return 0x54;
1461     case XK_KP_3: return 0x55;
1462     case XK_KP_4: return 0x56;
1463     case XK_KP_5: return 0x57;
1464     case XK_KP_6: return 0x58;
1465     case XK_KP_7: return 0x59;
1466     case XK_KP_8: return 0x5b;
1467     case XK_KP_9: return 0x5c;
1468     case XK_KP_Decimal: return 0x41;
1469     #endif
1470     case XK_KP_Add: return 0x45;
1471     case XK_KP_Subtract: return 0x4e;
1472     case XK_KP_Multiply: return 0x43;
1473     case XK_KP_Divide: return 0x4b;
1474     case XK_KP_Enter: return 0x4c;
1475     case XK_KP_Equal: return 0x51;
1476     }
1477     return -1;
1478     }
1479    
1480 gbeauche 1.6 static int event2keycode(XKeyEvent &ev)
1481 cebix 1.1 {
1482     KeySym ks;
1483     int as;
1484     int i = 0;
1485    
1486     do {
1487 gbeauche 1.6 ks = XLookupKeysym(&ev, i++);
1488 cebix 1.1 as = kc_decode(ks);
1489     if (as != -1)
1490     return as;
1491     } while (ks != NoSymbol);
1492    
1493     return -1;
1494     }
1495    
1496     static void handle_events(void)
1497     {
1498     // Handle events
1499     for (;;) {
1500     XEvent event;
1501    
1502 gbeauche 1.10 XDisplayLock();
1503 gbeauche 1.9 if (!XCheckMaskEvent(x_display, eventmask, &event)) {
1504     // Handle clipboard events
1505     if (XCheckTypedEvent(x_display, SelectionRequest, &event))
1506     ClipboardSelectionRequest(&event.xselectionrequest);
1507     else if (XCheckTypedEvent(x_display, SelectionClear, &event))
1508     ClipboardSelectionClear(&event.xselectionclear);
1509 gbeauche 1.14
1510     // Window "close" widget clicked
1511     else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
1512     if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1513     ADBKeyDown(0x7f); // Power key
1514     ADBKeyUp(0x7f);
1515     }
1516     }
1517 gbeauche 1.10
1518     XDisplayUnlock();
1519 cebix 1.1 break;
1520 gbeauche 1.9 }
1521 gbeauche 1.10 XDisplayUnlock();
1522 cebix 1.1
1523     switch (event.type) {
1524     // Mouse button
1525     case ButtonPress: {
1526     unsigned int button = ((XButtonEvent *)&event)->button;
1527     if (button < 4)
1528     ADBMouseDown(button - 1);
1529 gbeauche 1.8 else if (button < 6) { // Wheel mouse
1530     if (mouse_wheel_mode == 0) {
1531     int key = (button == 5) ? 0x79 : 0x74; // Page up/down
1532     ADBKeyDown(key);
1533     ADBKeyUp(key);
1534     } else {
1535     int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down
1536     for(int i=0; i<mouse_wheel_lines; i++) {
1537     ADBKeyDown(key);
1538     ADBKeyUp(key);
1539     }
1540     }
1541     }
1542 cebix 1.1 break;
1543     }
1544     case ButtonRelease: {
1545     unsigned int button = ((XButtonEvent *)&event)->button;
1546     if (button < 4)
1547     ADBMouseUp(button - 1);
1548     break;
1549     }
1550    
1551 gbeauche 1.15 // Mouse entered window
1552 cebix 1.1 case EnterNotify:
1553 gbeauche 1.15 if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
1554     ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1555 cebix 1.1 break;
1556 gbeauche 1.15
1557     // Mouse moved
1558 cebix 1.1 case MotionNotify:
1559 gbeauche 1.15 ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1560 cebix 1.1 break;
1561    
1562     // Keyboard
1563     case KeyPress: {
1564 gbeauche 1.6 int code = event2keycode(event.xkey);
1565     if (use_keycodes && code != -1)
1566     code = keycode_table[event.xkey.keycode & 0xff];
1567     if (code != -1) {
1568 cebix 1.1 if (!emul_suspended) {
1569     ADBKeyDown(code);
1570     if (code == 0x36)
1571     ctrl_down = true;
1572     } else {
1573     if (code == 0x31)
1574     resume_emul(); // Space wakes us up
1575     }
1576     }
1577     break;
1578     }
1579     case KeyRelease: {
1580 gbeauche 1.6 int code = event2keycode(event.xkey);
1581     if (use_keycodes && code != 1)
1582     code = keycode_table[event.xkey.keycode & 0xff];
1583     if (code != -1) {
1584 cebix 1.1 ADBKeyUp(code);
1585     if (code == 0x36)
1586     ctrl_down = false;
1587     }
1588     break;
1589     }
1590    
1591     // Hidden parts exposed, force complete refresh
1592     case Expose:
1593 gbeauche 1.3 #ifdef ENABLE_VOSF
1594     if (use_vosf) { // VOSF refresh
1595     LOCK_VOSF;
1596     PFLAG_SET_ALL;
1597     UNLOCK_VOSF;
1598     }
1599     #endif
1600 cebix 1.1 memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1601     break;
1602     }
1603     }
1604     }
1605    
1606    
1607     /*
1608     * Execute video VBL routine
1609     */
1610    
1611     void VideoVBL(void)
1612     {
1613     if (emerg_quit)
1614     QuitEmulator();
1615    
1616     // Execute video VBL
1617     if (private_data != NULL && private_data->interruptsEnabled)
1618     VSLDoInterruptService(private_data->vslServiceID);
1619     }
1620    
1621    
1622     /*
1623     * Install graphics acceleration
1624     */
1625    
1626 gbeauche 1.18 // Rectangle inversion
1627     template< int bpp >
1628     static inline void do_invrect(uint8 *dest, uint32 length)
1629 cebix 1.1 {
1630 gbeauche 1.18 #define INVERT_1(PTR, OFS) ((uint8 *)(PTR))[OFS] = ~((uint8 *)(PTR))[OFS]
1631     #define INVERT_2(PTR, OFS) ((uint16 *)(PTR))[OFS] = ~((uint16 *)(PTR))[OFS]
1632     #define INVERT_4(PTR, OFS) ((uint32 *)(PTR))[OFS] = ~((uint32 *)(PTR))[OFS]
1633     #define INVERT_8(PTR, OFS) ((uint64 *)(PTR))[OFS] = ~((uint64 *)(PTR))[OFS]
1634    
1635     #ifndef UNALIGNED_PROFITABLE
1636     // Align on 16-bit boundaries
1637     if (bpp < 16 && (((uintptr)dest) & 1)) {
1638     INVERT_1(dest, 0);
1639     dest += 1; length -= 1;
1640     }
1641    
1642     // Align on 32-bit boundaries
1643     if (bpp < 32 && (((uintptr)dest) & 2)) {
1644     INVERT_2(dest, 0);
1645     dest += 2; length -= 2;
1646     }
1647     #endif
1648    
1649     // Invert 8-byte words
1650     if (length >= 8) {
1651     const int r = (length / 8) % 8;
1652     dest += r * 8;
1653    
1654     int n = ((length / 8) + 7) / 8;
1655     switch (r) {
1656     case 0: do {
1657     dest += 64;
1658     INVERT_8(dest, -8);
1659     case 7: INVERT_8(dest, -7);
1660     case 6: INVERT_8(dest, -6);
1661     case 5: INVERT_8(dest, -5);
1662     case 4: INVERT_8(dest, -4);
1663     case 3: INVERT_8(dest, -3);
1664     case 2: INVERT_8(dest, -2);
1665     case 1: INVERT_8(dest, -1);
1666     } while (--n > 0);
1667     }
1668     }
1669 cebix 1.1
1670 gbeauche 1.18 // 32-bit cell to invert?
1671     if (length & 4) {
1672     INVERT_4(dest, 0);
1673     if (bpp <= 16)
1674     dest += 4;
1675     }
1676    
1677     // 16-bit cell to invert?
1678     if (bpp <= 16 && (length & 2)) {
1679     INVERT_2(dest, 0);
1680     if (bpp <= 8)
1681     dest += 2;
1682     }
1683    
1684     // 8-bit cell to invert?
1685     if (bpp <= 8 && (length & 1))
1686     INVERT_1(dest, 0);
1687    
1688     #undef INVERT_1
1689     #undef INVERT_2
1690     #undef INVERT_4
1691     #undef INVERT_8
1692     }
1693    
1694 gbeauche 1.19 void NQD_invrect(uint32 p)
1695 gbeauche 1.18 {
1696 gbeauche 1.19 D(bug("accl_invrect %08x\n", p));
1697 gbeauche 1.18
1698     // Get inversion parameters
1699 gbeauche 1.19 int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1700     int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1701     int16 width = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1702     int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1703 cebix 1.1 D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1704 gbeauche 1.19 D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1705 cebix 1.1
1706 gbeauche 1.18 //!!?? pen_mode == 14
1707    
1708     // And perform the inversion
1709 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1710     const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1711 gbeauche 1.20 uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1712 gbeauche 1.18 width *= bpp;
1713     switch (bpp) {
1714     case 1:
1715     for (int i = 0; i < height; i++) {
1716     do_invrect<8>(dest, width);
1717     dest += dest_row_bytes;
1718     }
1719     break;
1720     case 2:
1721     for (int i = 0; i < height; i++) {
1722     do_invrect<16>(dest, width);
1723     dest += dest_row_bytes;
1724     }
1725     break;
1726     case 4:
1727     for (int i = 0; i < height; i++) {
1728     do_invrect<32>(dest, width);
1729     dest += dest_row_bytes;
1730     }
1731     break;
1732     }
1733 cebix 1.1 }
1734    
1735 gbeauche 1.18 // Rectangle filling
1736     template< int bpp >
1737     static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1738 cebix 1.1 {
1739 gbeauche 1.18 #define FILL_1(PTR, OFS, VAL) ((uint8 *)(PTR))[OFS] = (VAL)
1740     #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1741     #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1742     #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1743    
1744     #ifndef UNALIGNED_PROFITABLE
1745     // Align on 16-bit boundaries
1746     if (bpp < 16 && (((uintptr)dest) & 1)) {
1747     FILL_1(dest, 0, color);
1748     dest += 1; length -= 1;
1749     }
1750    
1751     // Align on 32-bit boundaries
1752     if (bpp < 32 && (((uintptr)dest) & 2)) {
1753     FILL_2(dest, 0, color);
1754     dest += 2; length -= 2;
1755     }
1756     #endif
1757    
1758     // Fill 8-byte words
1759     if (length >= 8) {
1760     const uint64 c = (((uint64)color) << 32) | color;
1761     const int r = (length / 8) % 8;
1762     dest += r * 8;
1763    
1764     int n = ((length / 8) + 7) / 8;
1765     switch (r) {
1766     case 0: do {
1767     dest += 64;
1768     FILL_8(dest, -8, c);
1769     case 7: FILL_8(dest, -7, c);
1770     case 6: FILL_8(dest, -6, c);
1771     case 5: FILL_8(dest, -5, c);
1772     case 4: FILL_8(dest, -4, c);
1773     case 3: FILL_8(dest, -3, c);
1774     case 2: FILL_8(dest, -2, c);
1775     case 1: FILL_8(dest, -1, c);
1776     } while (--n > 0);
1777     }
1778     }
1779 cebix 1.1
1780 gbeauche 1.18 // 32-bit cell to fill?
1781     if (length & 4) {
1782     FILL_4(dest, 0, color);
1783     if (bpp <= 16)
1784     dest += 4;
1785     }
1786    
1787     // 16-bit cell to fill?
1788     if (bpp <= 16 && (length & 2)) {
1789     FILL_2(dest, 0, color);
1790     if (bpp <= 8)
1791     dest += 2;
1792     }
1793 cebix 1.1
1794 gbeauche 1.18 // 8-bit cell to fill?
1795     if (bpp <= 8 && (length & 1))
1796     FILL_1(dest, 0, color);
1797    
1798     #undef FILL_1
1799     #undef FILL_2
1800     #undef FILL_4
1801     #undef FILL_8
1802 cebix 1.1 }
1803    
1804 gbeauche 1.19 void NQD_fillrect(uint32 p)
1805 cebix 1.1 {
1806 gbeauche 1.19 D(bug("accl_fillrect %08x\n", p));
1807 cebix 1.1
1808 gbeauche 1.18 // Get filling parameters
1809 gbeauche 1.19 int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1810     int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1811     int16 width = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1812     int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1813     uint32 color = ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen);
1814 cebix 1.1 D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1815 gbeauche 1.18 D(bug(" width %d, height %d\n", width, height));
1816 gbeauche 1.19 D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1817 cebix 1.1
1818 gbeauche 1.18 // And perform the fill
1819 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1820     const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1821 gbeauche 1.20 uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1822 gbeauche 1.18 width *= bpp;
1823     switch (bpp) {
1824     case 1:
1825     for (int i = 0; i < height; i++) {
1826     memset(dest, color, width);
1827     dest += dest_row_bytes;
1828     }
1829     break;
1830     case 2:
1831     for (int i = 0; i < height; i++) {
1832     do_fillrect<16>(dest, color, width);
1833     dest += dest_row_bytes;
1834     }
1835     break;
1836     case 4:
1837     for (int i = 0; i < height; i++) {
1838     do_fillrect<32>(dest, color, width);
1839     dest += dest_row_bytes;
1840     }
1841     break;
1842     }
1843 cebix 1.1 }
1844    
1845 gbeauche 1.19 bool NQD_fillrect_hook(uint32 p)
1846 cebix 1.1 {
1847 gbeauche 1.19 D(bug("accl_fillrect_hook %08x\n", p));
1848 cebix 1.1
1849     // Check if we can accelerate this fillrect
1850 gbeauche 1.19 if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1851     const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1852     if (transfer_mode == 8) {
1853 cebix 1.1 // Fill
1854 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1855 gbeauche 1.18 return true;
1856     }
1857 gbeauche 1.19 else if (transfer_mode == 10) {
1858 cebix 1.1 // Invert
1859 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1860 cebix 1.1 return true;
1861     }
1862     }
1863     return false;
1864     }
1865    
1866 gbeauche 1.16 // Rectangle blitting
1867     // TODO: optimize for VOSF and target pixmap == screen
1868 gbeauche 1.19 void NQD_bitblt(uint32 p)
1869 gbeauche 1.16 {
1870 gbeauche 1.19 D(bug("accl_bitblt %08x\n", p));
1871 gbeauche 1.16
1872     // Get blitting parameters
1873 gbeauche 1.19 int16 src_X = (int16)ReadMacInt16(p + acclSrcRect + 2) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 2);
1874     int16 src_Y = (int16)ReadMacInt16(p + acclSrcRect + 0) - (int16)ReadMacInt16(p + acclSrcBoundsRect + 0);
1875     int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1876     int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1877     int16 width = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1878     int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1879     D(bug(" src addr %08x, dest addr %08x\n", ReadMacInt32(p + acclSrcBaseAddr), ReadMacInt32(p + acclDestBaseAddr)));
1880 gbeauche 1.16 D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1881     D(bug(" width %d, height %d\n", width, height));
1882    
1883     // And perform the blit
1884 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1885 gbeauche 1.16 width *= bpp;
1886 gbeauche 1.19 if ((int32)ReadMacInt32(p + acclSrcRowBytes) > 0) {
1887     const int src_row_bytes = (int32)ReadMacInt32(p + acclSrcRowBytes);
1888     const int dst_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1889 gbeauche 1.20 uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + (src_Y * src_row_bytes) + (src_X * bpp));
1890     uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dst_row_bytes) + (dest_X * bpp));
1891 gbeauche 1.16 for (int i = 0; i < height; i++) {
1892 gbeauche 1.23 memmove(dst, src, width);
1893 gbeauche 1.16 src += src_row_bytes;
1894     dst += dst_row_bytes;
1895     }
1896     }
1897     else {
1898 gbeauche 1.19 const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1899     const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1900 gbeauche 1.20 uint8 *src = Mac2HostAddr(ReadMacInt32(p + acclSrcBaseAddr) + ((src_Y + height - 1) * src_row_bytes) + (src_X * bpp));
1901     uint8 *dst = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + ((dest_Y + height - 1) * dst_row_bytes) + (dest_X * bpp));
1902 gbeauche 1.16 for (int i = height - 1; i >= 0; i--) {
1903 gbeauche 1.23 memmove(dst, src, width);
1904 gbeauche 1.16 src -= src_row_bytes;
1905     dst -= dst_row_bytes;
1906     }
1907     }
1908     }
1909    
1910 gbeauche 1.18 /*
1911     BitBlt transfer modes:
1912     0 : srcCopy
1913     1 : srcOr
1914     2 : srcXor
1915     3 : srcBic
1916     4 : notSrcCopy
1917     5 : notSrcOr
1918     6 : notSrcXor
1919     7 : notSrcBic
1920     32 : blend
1921     33 : addPin
1922     34 : addOver
1923     35 : subPin
1924     36 : transparent
1925     37 : adMax
1926     38 : subOver
1927     39 : adMin
1928     50 : hilite
1929     */
1930    
1931 gbeauche 1.19 bool NQD_bitblt_hook(uint32 p)
1932 gbeauche 1.16 {
1933 gbeauche 1.19 D(bug("accl_draw_hook %08x\n", p));
1934 gbeauche 1.16
1935     // Check if we can accelerate this bitblt
1936 gbeauche 1.19 if (ReadMacInt32(p + 0x018) + ReadMacInt32(p + 0x128) == 0 &&
1937     ReadMacInt32(p + 0x130) == 0 &&
1938     ReadMacInt32(p + acclSrcPixelSize) >= 8 &&
1939     ReadMacInt32(p + acclSrcPixelSize) == ReadMacInt32(p + acclDestPixelSize) &&
1940     (ReadMacInt32(p + acclSrcRowBytes) ^ ReadMacInt32(p + acclDestRowBytes)) >= 0 && // same sign?
1941     ReadMacInt32(p + acclTransferMode) == 0 && // srcCopy?
1942     ReadMacInt32(p + 0x15c) > 0) {
1943 gbeauche 1.16
1944     // Yes, set function pointer
1945 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1946 gbeauche 1.16 return true;
1947     }
1948     return false;
1949     }
1950    
1951 cebix 1.1 // Wait for graphics operation to finish
1952 gbeauche 1.16 bool NQD_sync_hook(uint32 arg)
1953 cebix 1.1 {
1954 gbeauche 1.16 D(bug("accl_sync_hook %08x\n", arg));
1955 cebix 1.1 return true;
1956     }
1957    
1958     void VideoInstallAccel(void)
1959     {
1960 gbeauche 1.18 // Temporary hack until it's fixed for e.g. little-endian & 64-bit platforms
1961     #ifndef __powerpc__
1962     return;
1963     #endif
1964    
1965 cebix 1.1 // Install acceleration hooks
1966     if (PrefsFindBool("gfxaccel")) {
1967     D(bug("Video: Installing acceleration hooks\n"));
1968 gbeauche 1.16 uint32 base;
1969    
1970     SheepVar bitblt_hook_info(sizeof(accl_hook_info));
1971     base = bitblt_hook_info.addr();
1972     WriteMacInt32(base + 0, NativeTVECT(NATIVE_BITBLT_HOOK));
1973     WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1974     WriteMacInt32(base + 8, ACCL_BITBLT);
1975     NQDMisc(6, bitblt_hook_info.ptr());
1976    
1977 gbeauche 1.18 SheepVar fillrect_hook_info(sizeof(accl_hook_info));
1978     base = fillrect_hook_info.addr();
1979     WriteMacInt32(base + 0, NativeTVECT(NATIVE_FILLRECT_HOOK));
1980     WriteMacInt32(base + 4, NativeTVECT(NATIVE_SYNC_HOOK));
1981     WriteMacInt32(base + 8, ACCL_FILLRECT);
1982     NQDMisc(6, fillrect_hook_info.ptr());
1983 cebix 1.1 }
1984     }
1985    
1986    
1987     /*
1988     * Change video mode
1989     */
1990    
1991     int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1992     {
1993     /* return if no mode change */
1994     if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1995     (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1996    
1997     /* first find video mode in table */
1998     for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
1999     if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
2000     (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
2001     csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
2002     csSave->saveData = ReadMacInt32(ParamPtr + csData);
2003     csSave->savePage = ReadMacInt16(ParamPtr + csPage);
2004    
2005     // Disable interrupts and pause redraw thread
2006     DisableInterrupt();
2007     thread_stop_ack = false;
2008     thread_stop_req = true;
2009     while (!thread_stop_ack) ;
2010    
2011     /* close old display */
2012     close_display();
2013    
2014     /* open new display */
2015     cur_mode = i;
2016     bool ok = open_display();
2017    
2018     /* opening the screen failed? Then bail out */
2019     if (!ok) {
2020     ErrorAlert(GetString(STR_FULL_SCREEN_ERR));
2021     QuitEmulator();
2022     }
2023    
2024     WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
2025     csSave->saveBaseAddr=screen_base;
2026     csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
2027     csSave->saveMode=VModes[cur_mode].viAppleMode;
2028    
2029     // Enable interrupts and resume redraw thread
2030     thread_stop_req = false;
2031     EnableInterrupt();
2032     return noErr;
2033     }
2034     }
2035     return paramErr;
2036     }
2037    
2038    
2039     /*
2040     * Set color palette
2041     */
2042    
2043     void video_set_palette(void)
2044     {
2045 gbeauche 1.13 LOCK_PALETTE;
2046    
2047     // Convert colors to XColor array
2048     int mode = get_current_mode();
2049     int num_in = palette_size(mode);
2050     int num_out = 256;
2051     bool stretch = false;
2052     if (IsDirectMode(mode)) {
2053     // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
2054     num_out = vis->map_entries;
2055     stretch = true;
2056     }
2057     XColor *p = x_palette;
2058     for (int i=0; i<num_out; i++) {
2059     int c = (stretch ? (i * num_in) / num_out : i);
2060     p->red = mac_pal[c].red * 0x0101;
2061     p->green = mac_pal[c].green * 0x0101;
2062     p->blue = mac_pal[c].blue * 0x0101;
2063     p++;
2064     }
2065    
2066     #ifdef ENABLE_VOSF
2067     // Recalculate pixel color expansion map
2068     if (!IsDirectMode(mode) && xdepth > 8) {
2069     for (int i=0; i<256; i++) {
2070     int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
2071     ExpandMap[i] = map_rgb(mac_pal[c].red, mac_pal[c].green, mac_pal[c].blue);
2072     }
2073    
2074     // We have to redraw everything because the interpretation of pixel values changed
2075     LOCK_VOSF;
2076     PFLAG_SET_ALL;
2077     UNLOCK_VOSF;
2078     memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
2079     }
2080     #endif
2081    
2082     // Tell redraw thread to change palette
2083 cebix 1.1 palette_changed = true;
2084 gbeauche 1.13
2085     UNLOCK_PALETTE;
2086 cebix 1.1 }
2087    
2088    
2089     /*
2090 gbeauche 1.21 * Can we set the MacOS cursor image into the window?
2091     */
2092    
2093     bool video_can_change_cursor(void)
2094     {
2095 gbeauche 1.22 return hw_mac_cursor_accl && (display_type != DIS_SCREEN);
2096 gbeauche 1.21 }
2097    
2098    
2099     /*
2100 cebix 1.1 * Set cursor image for window
2101     */
2102    
2103     void video_set_cursor(void)
2104     {
2105     cursor_changed = true;
2106     }
2107    
2108    
2109     /*
2110     * Thread for window refresh, event handling and other periodic actions
2111     */
2112    
2113     static void update_display(void)
2114     {
2115     // Incremental update code
2116     int wide = 0, high = 0, x1, x2, y1, y2, i, j;
2117     int bytes_per_row = VModes[cur_mode].viRowBytes;
2118     int bytes_per_pixel = VModes[cur_mode].viRowBytes / VModes[cur_mode].viXsize;
2119     uint8 *p, *p2;
2120    
2121     // Check for first line from top and first line from bottom that have changed
2122     y1 = 0;
2123     for (j=0; j<VModes[cur_mode].viYsize; j++) {
2124     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2125     y1 = j;
2126     break;
2127     }
2128     }
2129     y2 = y1 - 1;
2130     for (j=VModes[cur_mode].viYsize-1; j>=y1; j--) {
2131     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2132     y2 = j;
2133     break;
2134     }
2135     }
2136     high = y2 - y1 + 1;
2137    
2138     // Check for first column from left and first column from right that have changed
2139     if (high) {
2140     if (depth == 1) {
2141     x1 = VModes[cur_mode].viXsize;
2142     for (j=y1; j<=y2; j++) {
2143     p = &the_buffer[j * bytes_per_row];
2144     p2 = &the_buffer_copy[j * bytes_per_row];
2145     for (i=0; i<(x1>>3); i++) {
2146     if (*p != *p2) {
2147     x1 = i << 3;
2148     break;
2149     }
2150     p++;
2151     p2++;
2152     }
2153     }
2154     x2 = x1;
2155     for (j=y1; j<=y2; j++) {
2156     p = &the_buffer[j * bytes_per_row];
2157     p2 = &the_buffer_copy[j * bytes_per_row];
2158     p += bytes_per_row;
2159     p2 += bytes_per_row;
2160     for (i=(VModes[cur_mode].viXsize>>3); i>(x2>>3); i--) {
2161     p--;
2162     p2--;
2163     if (*p != *p2) {
2164     x2 = i << 3;
2165     break;
2166     }
2167     }
2168     }
2169     wide = x2 - x1;
2170    
2171     // Update copy of the_buffer
2172     if (high && wide) {
2173     for (j=y1; j<=y2; j++) {
2174     i = j * bytes_per_row + (x1 >> 3);
2175     memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
2176     }
2177     }
2178    
2179     } else {
2180     x1 = VModes[cur_mode].viXsize;
2181     for (j=y1; j<=y2; j++) {
2182     p = &the_buffer[j * bytes_per_row];
2183     p2 = &the_buffer_copy[j * bytes_per_row];
2184     for (i=0; i<x1; i++) {
2185     if (memcmp(p, p2, bytes_per_pixel)) {
2186     x1 = i;
2187     break;
2188     }
2189     p += bytes_per_pixel;
2190     p2 += bytes_per_pixel;
2191     }
2192     }
2193     x2 = x1;
2194     for (j=y1; j<=y2; j++) {
2195     p = &the_buffer[j * bytes_per_row];
2196     p2 = &the_buffer_copy[j * bytes_per_row];
2197     p += bytes_per_row;
2198     p2 += bytes_per_row;
2199     for (i=VModes[cur_mode].viXsize; i>x2; i--) {
2200     p -= bytes_per_pixel;
2201     p2 -= bytes_per_pixel;
2202     if (memcmp(p, p2, bytes_per_pixel)) {
2203     x2 = i;
2204     break;
2205     }
2206     }
2207     }
2208     wide = x2 - x1;
2209    
2210     // Update copy of the_buffer
2211     if (high && wide) {
2212     for (j=y1; j<=y2; j++) {
2213     i = j * bytes_per_row + x1 * bytes_per_pixel;
2214     memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
2215     }
2216     }
2217     }
2218     }
2219    
2220     // Refresh display
2221     if (high && wide) {
2222 gbeauche 1.10 XDisplayLock();
2223 cebix 1.1 if (have_shm)
2224     XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
2225     else
2226     XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
2227 gbeauche 1.10 XDisplayUnlock();
2228 cebix 1.1 }
2229     }
2230    
2231 gbeauche 1.10 const int VIDEO_REFRESH_HZ = 60;
2232     const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2233    
2234 gbeauche 1.13 static void handle_palette_changes(void)
2235     {
2236     LOCK_PALETTE;
2237    
2238     if (palette_changed && !emul_suspended) {
2239     palette_changed = false;
2240    
2241     int mode = get_current_mode();
2242     if (color_class == PseudoColor || color_class == DirectColor) {
2243     int num = vis->map_entries;
2244     bool set_clut = true;
2245     if (!IsDirectMode(mode) && color_class == DirectColor) {
2246     if (display_type == DIS_WINDOW)
2247     set_clut = false; // Indexed mode on true color screen, don't set CLUT
2248     }
2249    
2250     if (set_clut) {
2251     XDisplayLock();
2252     XStoreColors(x_display, cmap[0], x_palette, num);
2253     XStoreColors(x_display, cmap[1], x_palette, num);
2254     XSync(x_display, false);
2255     XDisplayUnlock();
2256     }
2257     }
2258    
2259     #ifdef ENABLE_XF86_DGA
2260     if (display_type == DIS_SCREEN) {
2261     current_dga_cmap ^= 1;
2262     if (!IsDirectMode(mode) && cmap[current_dga_cmap])
2263     XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2264     }
2265     #endif
2266     }
2267    
2268     UNLOCK_PALETTE;
2269     }
2270    
2271 cebix 1.1 static void *redraw_func(void *arg)
2272     {
2273 gbeauche 1.10 int fd = ConnectionNumber(x_display);
2274    
2275     uint64 start = GetTicks_usec();
2276     int64 ticks = 0;
2277     uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2278 cebix 1.1
2279     for (;;) {
2280    
2281     // Pause if requested (during video mode switches)
2282     while (thread_stop_req)
2283     thread_stop_ack = true;
2284    
2285 gbeauche 1.10 int64 delay = next - GetTicks_usec();
2286     if (delay < -VIDEO_REFRESH_DELAY) {
2287    
2288     // We are lagging far behind, so we reset the delay mechanism
2289     next = GetTicks_usec();
2290 cebix 1.1
2291 gbeauche 1.10 } else if (delay <= 0) {
2292    
2293     // Delay expired, refresh display
2294     next += VIDEO_REFRESH_DELAY;
2295     ticks++;
2296    
2297     // Handle X11 events
2298     handle_events();
2299    
2300     // Quit DGA mode if requested
2301     if (quit_full_screen) {
2302     quit_full_screen = false;
2303     if (display_type == DIS_SCREEN) {
2304     XDisplayLock();
2305 cebix 1.1 #ifdef ENABLE_XF86_DGA
2306 gbeauche 1.10 XF86DGADirectVideo(x_display, screen, 0);
2307     XUngrabPointer(x_display, CurrentTime);
2308     XUngrabKeyboard(x_display, CurrentTime);
2309 gbeauche 1.15 XUnmapWindow(x_display, the_win);
2310     wait_unmapped(the_win);
2311     XDestroyWindow(x_display, the_win);
2312 gbeauche 1.10 #endif
2313     XSync(x_display, false);
2314     XDisplayUnlock();
2315     quit_full_screen_ack = true;
2316     return NULL;
2317     }
2318 cebix 1.1 }
2319    
2320 gbeauche 1.10 // Refresh display and set cursor image in window mode
2321     static int tick_counter = 0;
2322     if (display_type == DIS_WINDOW) {
2323     tick_counter++;
2324     if (tick_counter >= frame_skip) {
2325     tick_counter = 0;
2326 cebix 1.1
2327 gbeauche 1.10 // Update display
2328 gbeauche 1.3 #ifdef ENABLE_VOSF
2329 gbeauche 1.10 if (use_vosf) {
2330     XDisplayLock();
2331     if (mainBuffer.dirty) {
2332     LOCK_VOSF;
2333     update_display_window_vosf();
2334     UNLOCK_VOSF;
2335     XSync(x_display, false); // Let the server catch up
2336     }
2337     XDisplayUnlock();
2338 gbeauche 1.3 }
2339 gbeauche 1.10 else
2340 gbeauche 1.3 #endif
2341 gbeauche 1.10 update_display();
2342 cebix 1.1
2343 gbeauche 1.10 // Set new cursor image if it was changed
2344 gbeauche 1.22 if (hw_mac_cursor_accl && cursor_changed) {
2345 gbeauche 1.10 cursor_changed = false;
2346     memcpy(cursor_image->data, MacCursor + 4, 32);
2347     memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2348     XDisplayLock();
2349     XFreeCursor(x_display, mac_cursor);
2350     XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2351     XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2352     mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2353     XDefineCursor(x_display, the_win, mac_cursor);
2354     XDisplayUnlock();
2355     }
2356 cebix 1.1 }
2357     }
2358 gbeauche 1.3 #ifdef ENABLE_VOSF
2359 gbeauche 1.10 else if (use_vosf) {
2360     // Update display (VOSF variant)
2361     if (++tick_counter >= frame_skip) {
2362     tick_counter = 0;
2363     if (mainBuffer.dirty) {
2364     LOCK_VOSF;
2365     update_display_dga_vosf();
2366     UNLOCK_VOSF;
2367     }
2368 gbeauche 1.3 }
2369     }
2370     #endif
2371 cebix 1.1
2372 gbeauche 1.10 // Set new palette if it was changed
2373 gbeauche 1.13 handle_palette_changes();
2374 gbeauche 1.10
2375     } else {
2376    
2377     // No display refresh pending, check for X events
2378     fd_set readfds;
2379     FD_ZERO(&readfds);
2380     FD_SET(fd, &readfds);
2381     struct timeval timeout;
2382     timeout.tv_sec = 0;
2383     timeout.tv_usec = delay;
2384     if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2385     handle_events();
2386 cebix 1.1 }
2387     }
2388     return NULL;
2389     }