ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
Revision: 1.25
Committed: 2004-06-05T06:28:21Z (19 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.24: +9 -0 lines
Log Message:
When no fullscreen mode requested nor available, always try to pick the
first windowed mode matching current depth.

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.25 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 gbeauche 1.15 if (cur_mode == -1)
1174     cur_mode = find_mode(default_mode, APPLE_W_640x480, DIS_WINDOW);
1175 gbeauche 1.13 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
1184    
1185 cebix 1.1 // Open window/screen
1186     if (!open_display())
1187     return false;
1188    
1189     #if 0
1190     // Ignore errors from now on
1191     XSetErrorHandler(ignore_errors);
1192     #endif
1193    
1194     // Start periodic thread
1195     XSync(x_display, false);
1196 gbeauche 1.15 Set_pthread_attr(&redraw_thread_attr, 0);
1197     redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1198 cebix 1.1 D(bug("Redraw thread installed (%ld)\n", redraw_thread));
1199     return true;
1200     }
1201    
1202    
1203     /*
1204     * Deinitialization
1205     */
1206    
1207     void VideoExit(void)
1208     {
1209     // Stop redraw thread
1210     if (redraw_thread_active) {
1211     pthread_cancel(redraw_thread);
1212     pthread_join(redraw_thread, NULL);
1213     redraw_thread_active = false;
1214     }
1215    
1216 gbeauche 1.3 #ifdef ENABLE_VOSF
1217     if (use_vosf) {
1218     // Deinitialize VOSF
1219     video_vosf_exit();
1220     }
1221     #endif
1222    
1223 cebix 1.1 // Close window and server connection
1224     if (x_display != NULL) {
1225     XSync(x_display, false);
1226     close_display();
1227     XFlush(x_display);
1228     XSync(x_display, false);
1229     }
1230     }
1231    
1232    
1233     /*
1234     * Suspend/resume emulator
1235     */
1236    
1237     extern void PauseEmulator(void);
1238     extern void ResumeEmulator(void);
1239    
1240     static void suspend_emul(void)
1241     {
1242     if (display_type == DIS_SCREEN) {
1243     // Release ctrl key
1244     ADBKeyUp(0x36);
1245     ctrl_down = false;
1246    
1247     // Pause MacOS thread
1248     PauseEmulator();
1249     emul_suspended = true;
1250    
1251     // Save frame buffer
1252     fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1253     if (fb_save)
1254     memcpy(fb_save, (void *)screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1255    
1256     // Close full screen display
1257     #ifdef ENABLE_XF86_DGA
1258     XF86DGADirectVideo(x_display, screen, 0);
1259     XUngrabPointer(x_display, CurrentTime);
1260     XUngrabKeyboard(x_display, CurrentTime);
1261     #endif
1262     XSync(x_display, false);
1263    
1264     // Open "suspend" window
1265     XSetWindowAttributes wattr;
1266     wattr.event_mask = KeyPressMask;
1267     wattr.background_pixel = black_pixel;
1268     wattr.border_pixel = black_pixel;
1269     wattr.backing_store = Always;
1270     wattr.backing_planes = xdepth;
1271     wattr.colormap = DefaultColormap(x_display, screen);
1272     XSync(x_display, false);
1273     suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1274     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1275     CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1276     XSync(x_display, false);
1277     XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1278     XMapRaised(x_display, suspend_win);
1279     XSync(x_display, false);
1280     }
1281     }
1282    
1283     static void resume_emul(void)
1284     {
1285     // Close "suspend" window
1286     XDestroyWindow(x_display, suspend_win);
1287     XSync(x_display, false);
1288    
1289     // Reopen full screen display
1290     XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1291     XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1292 gbeauche 1.12 #ifdef ENABLE_XF86_DGA
1293 cebix 1.1 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1294     XF86DGASetViewPort(x_display, screen, 0, 0);
1295 gbeauche 1.12 #endif
1296 cebix 1.1 XSync(x_display, false);
1297    
1298 gbeauche 1.3 // 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 cebix 1.1 // Restore frame buffer
1311     if (fb_save) {
1312 gbeauche 1.3 #ifdef ENABLE_VOSF
1313     // Don't copy fb_save to the temporary frame buffer in VOSF mode
1314     if (!use_vosf)
1315     #endif
1316 cebix 1.1 memcpy((void *)screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1317     free(fb_save);
1318     fb_save = NULL;
1319     }
1320     if (depth == 8)
1321     palette_changed = true;
1322    
1323     // Resume MacOS thread
1324     emul_suspended = false;
1325     ResumeEmulator();
1326     }
1327    
1328    
1329     /*
1330     * Close screen in full-screen mode
1331     */
1332    
1333     void VideoQuitFullScreen(void)
1334     {
1335     D(bug("VideoQuitFullScreen()\n"));
1336     if (display_type == DIS_SCREEN) {
1337     quit_full_screen = true;
1338     while (!quit_full_screen_ack) ;
1339     }
1340     }
1341    
1342    
1343     /*
1344     * X11 event handling
1345     */
1346    
1347     // Translate key event to Mac keycode
1348     static int kc_decode(KeySym ks)
1349     {
1350     switch (ks) {
1351     case XK_A: case XK_a: return 0x00;
1352     case XK_B: case XK_b: return 0x0b;
1353     case XK_C: case XK_c: return 0x08;
1354     case XK_D: case XK_d: return 0x02;
1355     case XK_E: case XK_e: return 0x0e;
1356     case XK_F: case XK_f: return 0x03;
1357     case XK_G: case XK_g: return 0x05;
1358     case XK_H: case XK_h: return 0x04;
1359     case XK_I: case XK_i: return 0x22;
1360     case XK_J: case XK_j: return 0x26;
1361     case XK_K: case XK_k: return 0x28;
1362     case XK_L: case XK_l: return 0x25;
1363     case XK_M: case XK_m: return 0x2e;
1364     case XK_N: case XK_n: return 0x2d;
1365     case XK_O: case XK_o: return 0x1f;
1366     case XK_P: case XK_p: return 0x23;
1367     case XK_Q: case XK_q: return 0x0c;
1368     case XK_R: case XK_r: return 0x0f;
1369     case XK_S: case XK_s: return 0x01;
1370     case XK_T: case XK_t: return 0x11;
1371     case XK_U: case XK_u: return 0x20;
1372     case XK_V: case XK_v: return 0x09;
1373     case XK_W: case XK_w: return 0x0d;
1374     case XK_X: case XK_x: return 0x07;
1375     case XK_Y: case XK_y: return 0x10;
1376     case XK_Z: case XK_z: return 0x06;
1377    
1378     case XK_1: case XK_exclam: return 0x12;
1379     case XK_2: case XK_at: return 0x13;
1380     case XK_3: case XK_numbersign: return 0x14;
1381     case XK_4: case XK_dollar: return 0x15;
1382     case XK_5: case XK_percent: return 0x17;
1383     case XK_6: return 0x16;
1384     case XK_7: return 0x1a;
1385     case XK_8: return 0x1c;
1386     case XK_9: return 0x19;
1387     case XK_0: return 0x1d;
1388    
1389     case XK_grave: case XK_asciitilde: return 0x0a;
1390     case XK_minus: case XK_underscore: return 0x1b;
1391     case XK_equal: case XK_plus: return 0x18;
1392     case XK_bracketleft: case XK_braceleft: return 0x21;
1393     case XK_bracketright: case XK_braceright: return 0x1e;
1394     case XK_backslash: case XK_bar: return 0x2a;
1395     case XK_semicolon: case XK_colon: return 0x29;
1396     case XK_apostrophe: case XK_quotedbl: return 0x27;
1397     case XK_comma: case XK_less: return 0x2b;
1398     case XK_period: case XK_greater: return 0x2f;
1399     case XK_slash: case XK_question: return 0x2c;
1400    
1401     case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1402     case XK_Return: return 0x24;
1403     case XK_space: return 0x31;
1404     case XK_BackSpace: return 0x33;
1405    
1406     case XK_Delete: return 0x75;
1407     case XK_Insert: return 0x72;
1408     case XK_Home: case XK_Help: return 0x73;
1409     case XK_End: return 0x77;
1410     #ifdef __hpux
1411     case XK_Prior: return 0x74;
1412     case XK_Next: return 0x79;
1413     #else
1414     case XK_Page_Up: return 0x74;
1415     case XK_Page_Down: return 0x79;
1416     #endif
1417    
1418     case XK_Control_L: return 0x36;
1419     case XK_Control_R: return 0x36;
1420     case XK_Shift_L: return 0x38;
1421     case XK_Shift_R: return 0x38;
1422     case XK_Alt_L: return 0x37;
1423     case XK_Alt_R: return 0x37;
1424     case XK_Meta_L: return 0x3a;
1425     case XK_Meta_R: return 0x3a;
1426     case XK_Menu: return 0x32;
1427     case XK_Caps_Lock: return 0x39;
1428     case XK_Num_Lock: return 0x47;
1429    
1430     case XK_Up: return 0x3e;
1431     case XK_Down: return 0x3d;
1432     case XK_Left: return 0x3b;
1433     case XK_Right: return 0x3c;
1434    
1435     case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1436    
1437     case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1438     case XK_F2: return 0x78;
1439     case XK_F3: return 0x63;
1440     case XK_F4: return 0x76;
1441     case XK_F5: return 0x60;
1442     case XK_F6: return 0x61;
1443     case XK_F7: return 0x62;
1444     case XK_F8: return 0x64;
1445     case XK_F9: return 0x65;
1446     case XK_F10: return 0x6d;
1447     case XK_F11: return 0x67;
1448     case XK_F12: return 0x6f;
1449    
1450     case XK_Print: return 0x69;
1451     case XK_Scroll_Lock: return 0x6b;
1452     case XK_Pause: return 0x71;
1453    
1454     #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1455     case XK_KP_0: case XK_KP_Insert: return 0x52;
1456     case XK_KP_1: case XK_KP_End: return 0x53;
1457     case XK_KP_2: case XK_KP_Down: return 0x54;
1458     case XK_KP_3: case XK_KP_Next: return 0x55;
1459     case XK_KP_4: case XK_KP_Left: return 0x56;
1460     case XK_KP_5: case XK_KP_Begin: return 0x57;
1461     case XK_KP_6: case XK_KP_Right: return 0x58;
1462     case XK_KP_7: case XK_KP_Home: return 0x59;
1463     case XK_KP_8: case XK_KP_Up: return 0x5b;
1464     case XK_KP_9: case XK_KP_Prior: return 0x5c;
1465     case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1466     #else
1467     case XK_KP_0: return 0x52;
1468     case XK_KP_1: return 0x53;
1469     case XK_KP_2: return 0x54;
1470     case XK_KP_3: return 0x55;
1471     case XK_KP_4: return 0x56;
1472     case XK_KP_5: return 0x57;
1473     case XK_KP_6: return 0x58;
1474     case XK_KP_7: return 0x59;
1475     case XK_KP_8: return 0x5b;
1476     case XK_KP_9: return 0x5c;
1477     case XK_KP_Decimal: return 0x41;
1478     #endif
1479     case XK_KP_Add: return 0x45;
1480     case XK_KP_Subtract: return 0x4e;
1481     case XK_KP_Multiply: return 0x43;
1482     case XK_KP_Divide: return 0x4b;
1483     case XK_KP_Enter: return 0x4c;
1484     case XK_KP_Equal: return 0x51;
1485     }
1486     return -1;
1487     }
1488    
1489 gbeauche 1.6 static int event2keycode(XKeyEvent &ev)
1490 cebix 1.1 {
1491     KeySym ks;
1492     int as;
1493     int i = 0;
1494    
1495     do {
1496 gbeauche 1.6 ks = XLookupKeysym(&ev, i++);
1497 cebix 1.1 as = kc_decode(ks);
1498     if (as != -1)
1499     return as;
1500     } while (ks != NoSymbol);
1501    
1502     return -1;
1503     }
1504    
1505     static void handle_events(void)
1506     {
1507     // Handle events
1508     for (;;) {
1509     XEvent event;
1510    
1511 gbeauche 1.10 XDisplayLock();
1512 gbeauche 1.9 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 gbeauche 1.14
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 gbeauche 1.10
1527     XDisplayUnlock();
1528 cebix 1.1 break;
1529 gbeauche 1.9 }
1530 gbeauche 1.10 XDisplayUnlock();
1531 cebix 1.1
1532     switch (event.type) {
1533     // Mouse button
1534     case ButtonPress: {
1535     unsigned int button = ((XButtonEvent *)&event)->button;
1536     if (button < 4)
1537     ADBMouseDown(button - 1);
1538 gbeauche 1.8 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 cebix 1.1 break;
1552     }
1553     case ButtonRelease: {
1554     unsigned int button = ((XButtonEvent *)&event)->button;
1555     if (button < 4)
1556     ADBMouseUp(button - 1);
1557     break;
1558     }
1559    
1560 gbeauche 1.15 // Mouse entered window
1561 cebix 1.1 case EnterNotify:
1562 gbeauche 1.15 if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
1563     ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1564 cebix 1.1 break;
1565 gbeauche 1.15
1566     // Mouse moved
1567 cebix 1.1 case MotionNotify:
1568 gbeauche 1.15 ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1569 cebix 1.1 break;
1570    
1571     // Keyboard
1572     case KeyPress: {
1573 gbeauche 1.6 int code = event2keycode(event.xkey);
1574     if (use_keycodes && code != -1)
1575     code = keycode_table[event.xkey.keycode & 0xff];
1576     if (code != -1) {
1577 cebix 1.1 if (!emul_suspended) {
1578     ADBKeyDown(code);
1579     if (code == 0x36)
1580     ctrl_down = true;
1581     } else {
1582     if (code == 0x31)
1583     resume_emul(); // Space wakes us up
1584     }
1585     }
1586     break;
1587     }
1588     case KeyRelease: {
1589 gbeauche 1.6 int code = event2keycode(event.xkey);
1590     if (use_keycodes && code != 1)
1591     code = keycode_table[event.xkey.keycode & 0xff];
1592     if (code != -1) {
1593 cebix 1.1 ADBKeyUp(code);
1594     if (code == 0x36)
1595     ctrl_down = false;
1596     }
1597     break;
1598     }
1599    
1600     // Hidden parts exposed, force complete refresh
1601     case Expose:
1602 gbeauche 1.3 #ifdef ENABLE_VOSF
1603     if (use_vosf) { // VOSF refresh
1604     LOCK_VOSF;
1605     PFLAG_SET_ALL;
1606     UNLOCK_VOSF;
1607     }
1608     #endif
1609 cebix 1.1 memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1610     break;
1611     }
1612     }
1613     }
1614    
1615    
1616     /*
1617     * Execute video VBL routine
1618     */
1619    
1620     void VideoVBL(void)
1621     {
1622     if (emerg_quit)
1623     QuitEmulator();
1624    
1625     // Execute video VBL
1626     if (private_data != NULL && private_data->interruptsEnabled)
1627     VSLDoInterruptService(private_data->vslServiceID);
1628     }
1629    
1630    
1631     /*
1632     * Install graphics acceleration
1633     */
1634    
1635 gbeauche 1.18 // Rectangle inversion
1636     template< int bpp >
1637     static inline void do_invrect(uint8 *dest, uint32 length)
1638 cebix 1.1 {
1639 gbeauche 1.18 #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     // Invert 8-byte words
1659     if (length >= 8) {
1660     const int r = (length / 8) % 8;
1661     dest += r * 8;
1662    
1663     int n = ((length / 8) + 7) / 8;
1664     switch (r) {
1665     case 0: do {
1666     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 cebix 1.1
1679 gbeauche 1.18 // 32-bit cell to invert?
1680     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 gbeauche 1.19 void NQD_invrect(uint32 p)
1704 gbeauche 1.18 {
1705 gbeauche 1.19 D(bug("accl_invrect %08x\n", p));
1706 gbeauche 1.18
1707     // Get inversion parameters
1708 gbeauche 1.19 int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1709     int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1710     int16 width = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1711     int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1712 cebix 1.1 D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1713 gbeauche 1.19 D(bug(" width %d, height %d, bytes_per_row %d\n", width, height, (int32)ReadMacInt32(p + acclDestRowBytes)));
1714 cebix 1.1
1715 gbeauche 1.18 //!!?? pen_mode == 14
1716    
1717     // And perform the inversion
1718 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1719     const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1720 gbeauche 1.20 uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1721 gbeauche 1.18 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     }
1742 cebix 1.1 }
1743    
1744 gbeauche 1.18 // Rectangle filling
1745     template< int bpp >
1746     static inline void do_fillrect(uint8 *dest, uint32 color, uint32 length)
1747 cebix 1.1 {
1748 gbeauche 1.18 #define FILL_1(PTR, OFS, VAL) ((uint8 *)(PTR))[OFS] = (VAL)
1749     #define FILL_2(PTR, OFS, VAL) ((uint16 *)(PTR))[OFS] = (VAL)
1750     #define FILL_4(PTR, OFS, VAL) ((uint32 *)(PTR))[OFS] = (VAL)
1751     #define FILL_8(PTR, OFS, VAL) ((uint64 *)(PTR))[OFS] = (VAL)
1752    
1753     #ifndef UNALIGNED_PROFITABLE
1754     // 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     // Align on 32-bit boundaries
1761     if (bpp < 32 && (((uintptr)dest) & 2)) {
1762     FILL_2(dest, 0, color);
1763     dest += 2; length -= 2;
1764     }
1765     #endif
1766    
1767     // Fill 8-byte words
1768     if (length >= 8) {
1769     const uint64 c = (((uint64)color) << 32) | color;
1770     const int r = (length / 8) % 8;
1771     dest += r * 8;
1772    
1773     int n = ((length / 8) + 7) / 8;
1774     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 cebix 1.1
1789 gbeauche 1.18 // 32-bit cell to fill?
1790     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 cebix 1.1
1803 gbeauche 1.18 // 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 cebix 1.1 }
1812    
1813 gbeauche 1.19 void NQD_fillrect(uint32 p)
1814 cebix 1.1 {
1815 gbeauche 1.19 D(bug("accl_fillrect %08x\n", p));
1816 cebix 1.1
1817 gbeauche 1.18 // Get filling parameters
1818 gbeauche 1.19 int16 dest_X = (int16)ReadMacInt16(p + acclDestRect + 2) - (int16)ReadMacInt16(p + acclDestBoundsRect + 2);
1819     int16 dest_Y = (int16)ReadMacInt16(p + acclDestRect + 0) - (int16)ReadMacInt16(p + acclDestBoundsRect + 0);
1820     int16 width = (int16)ReadMacInt16(p + acclDestRect + 6) - (int16)ReadMacInt16(p + acclDestRect + 2);
1821     int16 height = (int16)ReadMacInt16(p + acclDestRect + 4) - (int16)ReadMacInt16(p + acclDestRect + 0);
1822 gbeauche 1.24 uint32 color = htonl(ReadMacInt32(p + acclPenMode) == 8 ? ReadMacInt32(p + acclForePen) : ReadMacInt32(p + acclBackPen));
1823 cebix 1.1 D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1824 gbeauche 1.18 D(bug(" width %d, height %d\n", width, height));
1825 gbeauche 1.19 D(bug(" bytes_per_row %d color %08x\n", (int32)ReadMacInt32(p + acclDestRowBytes), color));
1826 cebix 1.1
1827 gbeauche 1.18 // And perform the fill
1828 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclDestPixelSize));
1829     const int dest_row_bytes = (int32)ReadMacInt32(p + acclDestRowBytes);
1830 gbeauche 1.20 uint8 *dest = Mac2HostAddr(ReadMacInt32(p + acclDestBaseAddr) + (dest_Y * dest_row_bytes) + (dest_X * bpp));
1831 gbeauche 1.18 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 cebix 1.1 }
1853    
1854 gbeauche 1.19 bool NQD_fillrect_hook(uint32 p)
1855 cebix 1.1 {
1856 gbeauche 1.19 D(bug("accl_fillrect_hook %08x\n", p));
1857 cebix 1.1
1858     // Check if we can accelerate this fillrect
1859 gbeauche 1.19 if (ReadMacInt32(p + 0x284) != 0 && ReadMacInt32(p + acclDestPixelSize) >= 8) {
1860     const int transfer_mode = ReadMacInt32(p + acclTransferMode);
1861     if (transfer_mode == 8) {
1862 cebix 1.1 // Fill
1863 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_FILLRECT));
1864 gbeauche 1.18 return true;
1865     }
1866 gbeauche 1.19 else if (transfer_mode == 10) {
1867 cebix 1.1 // Invert
1868 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_INVRECT));
1869 cebix 1.1 return true;
1870     }
1871     }
1872     return false;
1873     }
1874    
1875 gbeauche 1.16 // Rectangle blitting
1876     // TODO: optimize for VOSF and target pixmap == screen
1877 gbeauche 1.19 void NQD_bitblt(uint32 p)
1878 gbeauche 1.16 {
1879 gbeauche 1.19 D(bug("accl_bitblt %08x\n", p));
1880 gbeauche 1.16
1881     // Get blitting parameters
1882 gbeauche 1.19 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 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));
1890     D(bug(" width %d, height %d\n", width, height));
1891    
1892     // And perform the blit
1893 gbeauche 1.19 const int bpp = bytes_per_pixel(ReadMacInt32(p + acclSrcPixelSize));
1894 gbeauche 1.16 width *= bpp;
1895 gbeauche 1.19 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 gbeauche 1.20 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 gbeauche 1.16 for (int i = 0; i < height; i++) {
1901 gbeauche 1.23 memmove(dst, src, width);
1902 gbeauche 1.16 src += src_row_bytes;
1903     dst += dst_row_bytes;
1904     }
1905     }
1906     else {
1907 gbeauche 1.19 const int src_row_bytes = -(int32)ReadMacInt32(p + acclSrcRowBytes);
1908     const int dst_row_bytes = -(int32)ReadMacInt32(p + acclDestRowBytes);
1909 gbeauche 1.20 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 gbeauche 1.16 for (int i = height - 1; i >= 0; i--) {
1912 gbeauche 1.23 memmove(dst, src, width);
1913 gbeauche 1.16 src -= src_row_bytes;
1914     dst -= dst_row_bytes;
1915     }
1916     }
1917     }
1918    
1919 gbeauche 1.18 /*
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 gbeauche 1.19 bool NQD_bitblt_hook(uint32 p)
1941 gbeauche 1.16 {
1942 gbeauche 1.19 D(bug("accl_draw_hook %08x\n", p));
1943 gbeauche 1.16
1944     // Check if we can accelerate this bitblt
1945 gbeauche 1.19 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 gbeauche 1.16
1953     // Yes, set function pointer
1954 gbeauche 1.19 WriteMacInt32(p + acclDrawProc, NativeTVECT(NATIVE_BITBLT));
1955 gbeauche 1.16 return true;
1956     }
1957     return false;
1958     }
1959    
1960 cebix 1.1 // Wait for graphics operation to finish
1961 gbeauche 1.16 bool NQD_sync_hook(uint32 arg)
1962 cebix 1.1 {
1963 gbeauche 1.16 D(bug("accl_sync_hook %08x\n", arg));
1964 cebix 1.1 return true;
1965     }
1966    
1967     void VideoInstallAccel(void)
1968     {
1969     // Install acceleration hooks
1970     if (PrefsFindBool("gfxaccel")) {
1971     D(bug("Video: Installing acceleration hooks\n"));
1972 gbeauche 1.16 uint32 base;
1973    
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 gbeauche 1.18 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 cebix 1.1 }
1988     }
1989    
1990    
1991     /*
1992     * Change video mode
1993     */
1994    
1995     int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1996     {
1997     /* return if no mode change */
1998     if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1999     (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
2000    
2001     /* first find video mode in table */
2002     for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
2003     if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
2004     (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
2005     csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
2006     csSave->saveData = ReadMacInt32(ParamPtr + csData);
2007     csSave->savePage = ReadMacInt16(ParamPtr + csPage);
2008    
2009     // Disable interrupts and pause redraw thread
2010     DisableInterrupt();
2011     thread_stop_ack = false;
2012     thread_stop_req = true;
2013     while (!thread_stop_ack) ;
2014    
2015     /* close old display */
2016     close_display();
2017    
2018     /* open new display */
2019     cur_mode = i;
2020     bool ok = open_display();
2021    
2022     /* opening the screen failed? Then bail out */
2023     if (!ok) {
2024     ErrorAlert(GetString(STR_FULL_SCREEN_ERR));
2025     QuitEmulator();
2026     }
2027    
2028     WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
2029     csSave->saveBaseAddr=screen_base;
2030     csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
2031     csSave->saveMode=VModes[cur_mode].viAppleMode;
2032    
2033     // Enable interrupts and resume redraw thread
2034     thread_stop_req = false;
2035     EnableInterrupt();
2036     return noErr;
2037     }
2038     }
2039     return paramErr;
2040     }
2041    
2042    
2043     /*
2044     * Set color palette
2045     */
2046    
2047     void video_set_palette(void)
2048     {
2049 gbeauche 1.13 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 cebix 1.1 palette_changed = true;
2088 gbeauche 1.13
2089     UNLOCK_PALETTE;
2090 cebix 1.1 }
2091    
2092    
2093     /*
2094 gbeauche 1.21 * Can we set the MacOS cursor image into the window?
2095     */
2096    
2097     bool video_can_change_cursor(void)
2098     {
2099 gbeauche 1.22 return hw_mac_cursor_accl && (display_type != DIS_SCREEN);
2100 gbeauche 1.21 }
2101    
2102    
2103     /*
2104 cebix 1.1 * Set cursor image for window
2105     */
2106    
2107     void video_set_cursor(void)
2108     {
2109     cursor_changed = true;
2110     }
2111    
2112    
2113     /*
2114     * Thread for window refresh, event handling and other periodic actions
2115     */
2116    
2117     static void update_display(void)
2118     {
2119     // Incremental update code
2120     int wide = 0, high = 0, x1, x2, y1, y2, i, j;
2121     int bytes_per_row = VModes[cur_mode].viRowBytes;
2122     int bytes_per_pixel = VModes[cur_mode].viRowBytes / VModes[cur_mode].viXsize;
2123     uint8 *p, *p2;
2124    
2125     // Check for first line from top and first line from bottom that have changed
2126     y1 = 0;
2127     for (j=0; j<VModes[cur_mode].viYsize; j++) {
2128     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2129     y1 = j;
2130     break;
2131     }
2132     }
2133     y2 = y1 - 1;
2134     for (j=VModes[cur_mode].viYsize-1; j>=y1; j--) {
2135     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
2136     y2 = j;
2137     break;
2138     }
2139     }
2140     high = y2 - y1 + 1;
2141    
2142     // Check for first column from left and first column from right that have changed
2143     if (high) {
2144     if (depth == 1) {
2145     x1 = VModes[cur_mode].viXsize;
2146     for (j=y1; j<=y2; j++) {
2147     p = &the_buffer[j * bytes_per_row];
2148     p2 = &the_buffer_copy[j * bytes_per_row];
2149     for (i=0; i<(x1>>3); i++) {
2150     if (*p != *p2) {
2151     x1 = i << 3;
2152     break;
2153     }
2154     p++;
2155     p2++;
2156     }
2157     }
2158     x2 = x1;
2159     for (j=y1; j<=y2; j++) {
2160     p = &the_buffer[j * bytes_per_row];
2161     p2 = &the_buffer_copy[j * bytes_per_row];
2162     p += bytes_per_row;
2163     p2 += bytes_per_row;
2164     for (i=(VModes[cur_mode].viXsize>>3); i>(x2>>3); i--) {
2165     p--;
2166     p2--;
2167     if (*p != *p2) {
2168     x2 = i << 3;
2169     break;
2170     }
2171     }
2172     }
2173     wide = x2 - x1;
2174    
2175     // Update copy of the_buffer
2176     if (high && wide) {
2177     for (j=y1; j<=y2; j++) {
2178     i = j * bytes_per_row + (x1 >> 3);
2179     memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
2180     }
2181     }
2182    
2183     } else {
2184     x1 = VModes[cur_mode].viXsize;
2185     for (j=y1; j<=y2; j++) {
2186     p = &the_buffer[j * bytes_per_row];
2187     p2 = &the_buffer_copy[j * bytes_per_row];
2188     for (i=0; i<x1; i++) {
2189     if (memcmp(p, p2, bytes_per_pixel)) {
2190     x1 = i;
2191     break;
2192     }
2193     p += bytes_per_pixel;
2194     p2 += bytes_per_pixel;
2195     }
2196     }
2197     x2 = x1;
2198     for (j=y1; j<=y2; j++) {
2199     p = &the_buffer[j * bytes_per_row];
2200     p2 = &the_buffer_copy[j * bytes_per_row];
2201     p += bytes_per_row;
2202     p2 += bytes_per_row;
2203     for (i=VModes[cur_mode].viXsize; i>x2; i--) {
2204     p -= bytes_per_pixel;
2205     p2 -= bytes_per_pixel;
2206     if (memcmp(p, p2, bytes_per_pixel)) {
2207     x2 = i;
2208     break;
2209     }
2210     }
2211     }
2212     wide = x2 - x1;
2213    
2214     // Update copy of the_buffer
2215     if (high && wide) {
2216     for (j=y1; j<=y2; j++) {
2217     i = j * bytes_per_row + x1 * bytes_per_pixel;
2218     memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
2219     }
2220     }
2221     }
2222     }
2223    
2224     // Refresh display
2225     if (high && wide) {
2226 gbeauche 1.10 XDisplayLock();
2227 cebix 1.1 if (have_shm)
2228     XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
2229     else
2230     XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
2231 gbeauche 1.10 XDisplayUnlock();
2232 cebix 1.1 }
2233     }
2234    
2235 gbeauche 1.10 const int VIDEO_REFRESH_HZ = 60;
2236     const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
2237    
2238 gbeauche 1.13 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 cebix 1.1 static void *redraw_func(void *arg)
2276     {
2277 gbeauche 1.10 int fd = ConnectionNumber(x_display);
2278    
2279     uint64 start = GetTicks_usec();
2280     int64 ticks = 0;
2281     uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2282 cebix 1.1
2283     for (;;) {
2284    
2285     // Pause if requested (during video mode switches)
2286     while (thread_stop_req)
2287     thread_stop_ack = true;
2288    
2289 gbeauche 1.10 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 cebix 1.1
2295 gbeauche 1.10 } else if (delay <= 0) {
2296    
2297     // Delay expired, refresh display
2298     next += VIDEO_REFRESH_DELAY;
2299     ticks++;
2300    
2301     // Handle X11 events
2302     handle_events();
2303    
2304     // Quit DGA mode if requested
2305     if (quit_full_screen) {
2306     quit_full_screen = false;
2307     if (display_type == DIS_SCREEN) {
2308     XDisplayLock();
2309 cebix 1.1 #ifdef ENABLE_XF86_DGA
2310 gbeauche 1.10 XF86DGADirectVideo(x_display, screen, 0);
2311     XUngrabPointer(x_display, CurrentTime);
2312     XUngrabKeyboard(x_display, CurrentTime);
2313 gbeauche 1.15 XUnmapWindow(x_display, the_win);
2314     wait_unmapped(the_win);
2315     XDestroyWindow(x_display, the_win);
2316 gbeauche 1.10 #endif
2317     XSync(x_display, false);
2318     XDisplayUnlock();
2319     quit_full_screen_ack = true;
2320     return NULL;
2321     }
2322 cebix 1.1 }
2323    
2324 gbeauche 1.10 // Refresh display and set cursor image in window mode
2325     static int tick_counter = 0;
2326     if (display_type == DIS_WINDOW) {
2327     tick_counter++;
2328     if (tick_counter >= frame_skip) {
2329     tick_counter = 0;
2330 cebix 1.1
2331 gbeauche 1.10 // Update display
2332 gbeauche 1.3 #ifdef ENABLE_VOSF
2333 gbeauche 1.10 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 gbeauche 1.3 }
2343 gbeauche 1.10 else
2344 gbeauche 1.3 #endif
2345 gbeauche 1.10 update_display();
2346 cebix 1.1
2347 gbeauche 1.10 // Set new cursor image if it was changed
2348 gbeauche 1.22 if (hw_mac_cursor_accl && cursor_changed) {
2349 gbeauche 1.10 cursor_changed = false;
2350     memcpy(cursor_image->data, MacCursor + 4, 32);
2351     memcpy(cursor_mask_image->data, MacCursor + 36, 32);
2352     XDisplayLock();
2353     XFreeCursor(x_display, mac_cursor);
2354     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);
2356     mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2357     XDefineCursor(x_display, the_win, mac_cursor);
2358     XDisplayUnlock();
2359     }
2360 cebix 1.1 }
2361     }
2362 gbeauche 1.3 #ifdef ENABLE_VOSF
2363 gbeauche 1.10 else if (use_vosf) {
2364     // Update display (VOSF variant)
2365     if (++tick_counter >= frame_skip) {
2366     tick_counter = 0;
2367     if (mainBuffer.dirty) {
2368     LOCK_VOSF;
2369     update_display_dga_vosf();
2370     UNLOCK_VOSF;
2371     }
2372 gbeauche 1.3 }
2373     }
2374     #endif
2375 cebix 1.1
2376 gbeauche 1.10 // Set new palette if it was changed
2377 gbeauche 1.13 handle_palette_changes();
2378 gbeauche 1.10
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 cebix 1.1 }
2391     }
2392     return NULL;
2393     }