ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
Revision: 1.37
Committed: 2005-03-27T14:53:04Z (19 years, 2 months ago) by gbeauche
Branch: MAIN
Changes since 1.36: +51 -7 lines
Log Message:
Implement screen win/WIDTH/HEIGHT prefs item that overrides any other value
for windowmodes and screenmodes. Necessary for arbitrary full screen mode
sizes.

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * video_x.cpp - Video/graphics emulation, X11 specific stuff
3     *
4 gbeauche 1.35 * SheepShaver (C) 1997-2005 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 gbeauche 1.30 #include "video_blit.h"
50 cebix 1.1
51     #define DEBUG 0
52     #include "debug.h"
53    
54 gbeauche 1.13 #ifndef NO_STD_NAMESPACE
55     using std::sort;
56     #endif
57    
58 cebix 1.1
59 gbeauche 1.6 // Constants
60     const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
61 gbeauche 1.22 static const bool hw_mac_cursor_accl = true; // Flag: Enable MacOS to X11 copy of cursor?
62 cebix 1.1
63     // Global variables
64     static int32 frame_skip;
65 gbeauche 1.8 static int16 mouse_wheel_mode;
66     static int16 mouse_wheel_lines;
67 cebix 1.1 static bool redraw_thread_active = false; // Flag: Redraw thread installed
68 gbeauche 1.15 static pthread_attr_t redraw_thread_attr; // Redraw thread attributes
69 gbeauche 1.28 static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread
70 cebix 1.1 static pthread_t redraw_thread; // Redraw thread
71    
72 gbeauche 1.4 static bool local_X11; // Flag: X server running on local machine?
73 cebix 1.1 static volatile bool thread_stop_req = false;
74     static volatile bool thread_stop_ack = false; // Acknowledge for thread_stop_req
75    
76     static bool has_dga = false; // Flag: Video DGA capable
77     static bool has_vidmode = false; // Flag: VidMode extension available
78    
79 gbeauche 1.3 #ifdef ENABLE_VOSF
80     static bool use_vosf = true; // Flag: VOSF enabled
81     #else
82     static const bool use_vosf = false; // VOSF not possible
83     #endif
84    
85 cebix 1.1 static bool palette_changed = false; // Flag: Palette changed, redraw thread must update palette
86     static bool ctrl_down = false; // Flag: Ctrl key pressed
87 gbeauche 1.27 static bool caps_on = false; // Flag: Caps Lock on
88 cebix 1.1 static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
89     static volatile bool quit_full_screen_ack = false; // Acknowledge for quit_full_screen
90     static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
91    
92     static bool emul_suspended = false; // Flag: emulator suspended
93     static Window suspend_win; // "Suspend" window
94     static void *fb_save = NULL; // Saved frame buffer for suspend
95 gbeauche 1.6 static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
96     static int keycode_table[256]; // X keycode -> Mac keycode translation table
97 cebix 1.1
98     // X11 variables
99     static int screen; // Screen number
100     static int xdepth; // Depth of X screen
101     static int depth; // Depth of Mac frame buffer
102     static Window rootwin, the_win; // Root window and our window
103 gbeauche 1.13 static int num_depths = 0; // Number of available X depths
104     static int *avail_depths = NULL; // List of available X depths
105 gbeauche 1.30 static VisualFormat visualFormat;
106 cebix 1.1 static XVisualInfo visualInfo;
107     static Visual *vis;
108 gbeauche 1.13 static int color_class;
109     static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes
110 cebix 1.1 static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode
111 gbeauche 1.13 static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table
112    
113 cebix 1.1 static XColor black, white;
114     static unsigned long black_pixel, white_pixel;
115     static int eventmask;
116 gbeauche 1.13 static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask;
117     static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask;
118 cebix 1.1
119     // Variables for window mode
120     static GC the_gc;
121     static XImage *img = NULL;
122     static XShmSegmentInfo shminfo;
123     static XImage *cursor_image, *cursor_mask_image;
124     static Pixmap cursor_map, cursor_mask_map;
125     static Cursor mac_cursor;
126     static GC cursor_gc, cursor_mask_gc;
127     static bool cursor_changed = false; // Flag: Cursor changed, window_func must update cursor
128     static bool have_shm = false; // Flag: SHM present and usable
129 gbeauche 1.3 static uint8 *the_buffer = NULL; // Pointer to Mac frame buffer
130 cebix 1.1 static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer
131 gbeauche 1.3 static uint32 the_buffer_size; // Size of allocated the_buffer
132 cebix 1.1
133     // Variables for DGA mode
134     static int current_dga_cmap;
135    
136     #ifdef ENABLE_XF86_VIDMODE
137     // Variables for XF86 VidMode support
138     static XF86VidModeModeInfo **x_video_modes; // Array of all available modes
139     static int num_x_video_modes;
140     #endif
141    
142 gbeauche 1.13 // Mutex to protect palette
143     #ifdef HAVE_SPINLOCKS
144     static spinlock_t x_palette_lock = SPIN_LOCK_UNLOCKED;
145     #define LOCK_PALETTE spin_lock(&x_palette_lock)
146     #define UNLOCK_PALETTE spin_unlock(&x_palette_lock)
147     #elif defined(HAVE_PTHREADS)
148     static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER;
149     #define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock)
150     #define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock)
151     #else
152     #define LOCK_PALETTE
153     #define UNLOCK_PALETTE
154     #endif
155    
156 cebix 1.1
157     // Prototypes
158     static void *redraw_func(void *arg);
159    
160    
161 gbeauche 1.4 // From main_unix.cpp
162     extern char *x_display_name;
163 cebix 1.1 extern Display *x_display;
164    
165     // From sys_unix.cpp
166     extern void SysMountFirstFloppy(void);
167    
168 gbeauche 1.9 // From clip_unix.cpp
169     extern void ClipboardSelectionClear(XSelectionClearEvent *);
170     extern void ClipboardSelectionRequest(XSelectionRequestEvent *);
171    
172 cebix 1.1
173 gbeauche 1.3 // Video acceleration through SIGSEGV
174     #ifdef ENABLE_VOSF
175     # include "video_vosf.h"
176     #endif
177    
178    
179 cebix 1.1 /*
180 gbeauche 1.13 * Utility functions
181     */
182    
183     // Get current video mode
184     static inline int get_current_mode(void)
185     {
186     return VModes[cur_mode].viAppleMode;
187     }
188    
189     // Find palette size for given color depth
190     static int palette_size(int mode)
191     {
192     switch (mode) {
193     case APPLE_1_BIT: return 2;
194     case APPLE_2_BIT: return 4;
195     case APPLE_4_BIT: return 16;
196     case APPLE_8_BIT: return 256;
197     case APPLE_16_BIT: return 32;
198     case APPLE_32_BIT: return 256;
199     default: return 0;
200     }
201     }
202    
203 gbeauche 1.16 // Return bits per pixel for requested depth
204     static inline int bytes_per_pixel(int depth)
205     {
206     int bpp;
207     switch (depth) {
208     case 8:
209     bpp = 1;
210     break;
211     case 15: case 16:
212     bpp = 2;
213     break;
214     case 24: case 32:
215     bpp = 4;
216     break;
217     default:
218     abort();
219     }
220     return bpp;
221     }
222    
223 gbeauche 1.13 // Map video_mode depth ID to numerical depth value
224     static inline int depth_of_video_mode(int mode)
225     {
226 gbeauche 1.16 int depth;
227 gbeauche 1.13 switch (mode) {
228     case APPLE_1_BIT:
229     depth = 1;
230     break;
231     case APPLE_2_BIT:
232     depth = 2;
233     break;
234     case APPLE_4_BIT:
235     depth = 4;
236     break;
237     case APPLE_8_BIT:
238     depth = 8;
239     break;
240     case APPLE_16_BIT:
241     depth = 16;
242     break;
243     case APPLE_32_BIT:
244     depth = 32;
245     break;
246     default:
247     abort();
248     }
249     return depth;
250     }
251    
252     // Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals)
253     static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue)
254     {
255     return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift);
256     }
257    
258    
259     // Do we have a visual for handling the specified Mac depth? If so, set the
260     // global variables "xdepth", "visualInfo", "vis" and "color_class".
261     static bool find_visual_for_depth(int depth)
262     {
263     D(bug("have_visual_for_depth(%d)\n", depth_of_video_mode(depth)));
264    
265     // 1-bit works always and uses default visual
266     if (depth == APPLE_1_BIT) {
267     vis = DefaultVisual(x_display, screen);
268     visualInfo.visualid = XVisualIDFromVisual(vis);
269     int num = 0;
270     XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num);
271     visualInfo = vi[0];
272     XFree(vi);
273     xdepth = visualInfo.depth;
274     color_class = visualInfo.c_class;
275     D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth));
276     return true;
277     }
278    
279     // Calculate minimum and maximum supported X depth
280     int min_depth = 1, max_depth = 32;
281     switch (depth) {
282     #ifdef ENABLE_VOSF
283     case APPLE_2_BIT:
284     case APPLE_4_BIT: // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit
285     case APPLE_8_BIT:
286     min_depth = 8;
287     max_depth = 32;
288     break;
289     #else
290     case APPLE_2_BIT:
291     case APPLE_4_BIT: // 2/4-bit requires VOSF blitters
292     return false;
293     case APPLE_8_BIT: // 8-bit without VOSF requires an 8-bit visual
294     min_depth = 8;
295     max_depth = 8;
296     break;
297     #endif
298     case APPLE_16_BIT: // 16-bit requires a 15/16-bit visual
299     min_depth = 15;
300     max_depth = 16;
301     break;
302     case APPLE_32_BIT: // 32-bit requires a 24/32-bit visual
303     min_depth = 24;
304     max_depth = 32;
305     break;
306     }
307     D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth));
308    
309     // Try to find a visual for one of the color depths
310     bool visual_found = false;
311     for (int i=0; i<num_depths && !visual_found; i++) {
312    
313     xdepth = avail_depths[i];
314     D(bug(" trying to find visual for depth %d\n", xdepth));
315     if (xdepth < min_depth || xdepth > max_depth)
316     continue;
317    
318     // Determine best color class for this depth
319     switch (xdepth) {
320     case 1: // Try StaticGray or StaticColor
321     if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo)
322     || XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo))
323     visual_found = true;
324     break;
325     case 8: // Need PseudoColor
326     if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo))
327     visual_found = true;
328     break;
329     case 15:
330     case 16:
331     case 24:
332     case 32: // Try DirectColor first, as this will allow gamma correction
333     if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo)
334     || XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo))
335     visual_found = true;
336     break;
337     default:
338     D(bug(" not a supported depth\n"));
339     break;
340     }
341     }
342     if (!visual_found)
343     return false;
344    
345     // Visual was found
346     vis = visualInfo.visual;
347     color_class = visualInfo.c_class;
348     D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth));
349     #if DEBUG
350     switch (color_class) {
351     case StaticGray: D(bug("StaticGray\n")); break;
352     case GrayScale: D(bug("GrayScale\n")); break;
353     case StaticColor: D(bug("StaticColor\n")); break;
354     case PseudoColor: D(bug("PseudoColor\n")); break;
355     case TrueColor: D(bug("TrueColor\n")); break;
356     case DirectColor: D(bug("DirectColor\n")); break;
357     }
358     #endif
359     return true;
360     }
361    
362    
363     /*
364 cebix 1.1 * Open display (window or fullscreen)
365     */
366    
367 gbeauche 1.14 // Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget)
368     static Atom WM_DELETE_WINDOW = (Atom)0;
369     static void set_window_delete_protocol(Window w)
370     {
371     WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false);
372     XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1);
373     }
374    
375 gbeauche 1.13 // Wait until window is mapped/unmapped
376 gbeauche 1.15 static void wait_mapped(Window w)
377 gbeauche 1.13 {
378     XEvent e;
379     do {
380     XMaskEvent(x_display, StructureNotifyMask, &e);
381     } while ((e.type != MapNotify) || (e.xmap.event != w));
382     }
383    
384 gbeauche 1.15 static void wait_unmapped(Window w)
385 gbeauche 1.13 {
386     XEvent e;
387     do {
388     XMaskEvent(x_display, StructureNotifyMask, &e);
389     } while ((e.type != UnmapNotify) || (e.xmap.event != w));
390     }
391    
392 cebix 1.1 // Trap SHM errors
393     static bool shm_error = false;
394     static int (*old_error_handler)(Display *, XErrorEvent *);
395    
396     static int error_handler(Display *d, XErrorEvent *e)
397     {
398     if (e->error_code == BadAccess) {
399     shm_error = true;
400     return 0;
401     } else
402     return old_error_handler(d, e);
403     }
404    
405     // Open window
406     static bool open_window(int width, int height)
407     {
408 gbeauche 1.3 int aligned_width = (width + 15) & ~15;
409     int aligned_height = (height + 15) & ~15;
410    
411 cebix 1.1 // Set absolute mouse mode
412     ADBSetRelMouseMode(false);
413    
414     // Create window
415     XSetWindowAttributes wattr;
416     wattr.event_mask = eventmask = win_eventmask;
417 gbeauche 1.13 wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0);
418     wattr.border_pixel = 0;
419 cebix 1.1 wattr.backing_store = NotUseful;
420 gbeauche 1.13 wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
421     the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
422     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr);
423 cebix 1.1
424 gbeauche 1.13 // Set window name
425 cebix 1.1 XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
426    
427 gbeauche 1.14 // Set delete protocol property
428     set_window_delete_protocol(the_win);
429    
430 cebix 1.1 // Make window unresizable
431     XSizeHints *hints;
432     if ((hints = XAllocSizeHints()) != NULL) {
433     hints->min_width = width;
434     hints->max_width = width;
435     hints->min_height = height;
436     hints->max_height = height;
437     hints->flags = PMinSize | PMaxSize;
438     XSetWMNormalHints(x_display, the_win, hints);
439     XFree((char *)hints);
440     }
441    
442 gbeauche 1.13 // Show window
443     XMapWindow(x_display, the_win);
444     wait_mapped(the_win);
445    
446 gbeauche 1.5 // 1-bit mode is big-endian; if the X server is little-endian, we can't
447     // use SHM because that doesn't allow changing the image byte order
448     bool need_msb_image = (depth == 1 && XImageByteOrder(x_display) == LSBFirst);
449    
450 cebix 1.1 // Try to create and attach SHM image
451     have_shm = false;
452 gbeauche 1.5 if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) {
453 cebix 1.1
454     // Create SHM image ("height + 2" for safety)
455 gbeauche 1.5 img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
456 gbeauche 1.13 shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
457     D(bug(" shm image created\n"));
458 gbeauche 1.3 the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
459     shminfo.shmaddr = img->data = (char *)the_buffer_copy;
460 cebix 1.1 shminfo.readOnly = False;
461    
462     // Try to attach SHM image, catching errors
463     shm_error = false;
464     old_error_handler = XSetErrorHandler(error_handler);
465     XShmAttach(x_display, &shminfo);
466     XSync(x_display, false);
467     XSetErrorHandler(old_error_handler);
468     if (shm_error) {
469     shmdt(shminfo.shmaddr);
470     XDestroyImage(img);
471     shminfo.shmid = -1;
472     } else {
473     have_shm = true;
474     shmctl(shminfo.shmid, IPC_RMID, 0);
475     }
476 gbeauche 1.13 D(bug(" shm image attached\n"));
477 cebix 1.1 }
478    
479     // Create normal X image if SHM doesn't work ("height + 2" for safety)
480     if (!have_shm) {
481 gbeauche 1.13 int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth));
482 gbeauche 1.3 the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
483     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);
484 gbeauche 1.13 D(bug(" X image created\n"));
485 cebix 1.1 }
486    
487     // 1-Bit mode is big-endian
488 gbeauche 1.13 if (need_msb_image) {
489 cebix 1.1 img->byte_order = MSBFirst;
490     img->bitmap_bit_order = MSBFirst;
491     }
492    
493 gbeauche 1.3 #ifdef ENABLE_VOSF
494     use_vosf = true;
495     // Allocate memory for frame buffer (SIZE is extended to page-boundary)
496     the_host_buffer = the_buffer_copy;
497     the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
498     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
499     the_buffer_copy = (uint8 *)malloc(the_buffer_size);
500     D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
501     #else
502     // Allocate memory for frame buffer
503     the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
504     D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
505     #endif
506 gbeauche 1.33 screen_base = Host2MacAddr(the_buffer);
507 cebix 1.1
508     // Create GC
509     the_gc = XCreateGC(x_display, the_win, 0, 0);
510 gbeauche 1.13 XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
511 cebix 1.1
512     // Create cursor
513 gbeauche 1.22 if (hw_mac_cursor_accl) {
514 gbeauche 1.21 cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
515     cursor_image->byte_order = MSBFirst;
516     cursor_image->bitmap_bit_order = MSBFirst;
517     cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 36, 16, 16, 16, 2);
518     cursor_mask_image->byte_order = MSBFirst;
519     cursor_mask_image->bitmap_bit_order = MSBFirst;
520     cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
521     cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
522     cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
523     cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
524     mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
525     cursor_changed = false;
526     }
527    
528     // Create no_cursor
529     else {
530     mac_cursor = XCreatePixmapCursor(x_display,
531     XCreatePixmap(x_display, the_win, 1, 1, 1),
532     XCreatePixmap(x_display, the_win, 1, 1, 1),
533     &black, &white, 0, 0);
534     XDefineCursor(x_display, the_win, mac_cursor);
535     }
536 cebix 1.1
537 gbeauche 1.3 // Init blitting routines
538     bool native_byte_order;
539     #ifdef WORDS_BIGENDIAN
540     native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
541     #else
542     native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
543     #endif
544     #ifdef ENABLE_VOSF
545 gbeauche 1.30 Screen_blitter_init(visualFormat, native_byte_order, depth);
546 gbeauche 1.3 #endif
547    
548 cebix 1.1 // Set bytes per row
549     XSync(x_display, false);
550     return true;
551     }
552    
553     // Open DGA display (!! should use X11 VidMode extensions to set mode)
554     static bool open_dga(int width, int height)
555     {
556     #ifdef ENABLE_XF86_DGA
557     // Set relative mouse mode
558     ADBSetRelMouseMode(true);
559    
560 gbeauche 1.15 // Create window
561     XSetWindowAttributes wattr;
562     wattr.event_mask = eventmask = dga_eventmask;
563     wattr.override_redirect = True;
564     wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]);
565     the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
566     InputOutput, vis, CWEventMask | CWOverrideRedirect |
567     (color_class == DirectColor ? CWColormap : 0), &wattr);
568    
569     // Show window
570     XMapRaised(x_display, the_win);
571     wait_mapped(the_win);
572    
573 cebix 1.1 #ifdef ENABLE_XF86_VIDMODE
574     // Switch to best mode
575     if (has_vidmode) {
576     int best = 0;
577     for (int i=1; i<num_x_video_modes; i++) {
578     if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
579     x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
580     best = i;
581     }
582     }
583     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
584     XF86VidModeSetViewPort(x_display, screen, 0, 0);
585     }
586     #endif
587    
588     // Establish direct screen connection
589 gbeauche 1.15 XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
590     XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
591 cebix 1.1 XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
592     XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
593 gbeauche 1.15
594     int v_width, v_bank, v_size;
595     XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
596 cebix 1.1 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
597     XF86DGASetViewPort(x_display, screen, 0, 0);
598     XF86DGASetVidPage(x_display, screen, 0);
599    
600     // Set colormap
601 gbeauche 1.15 if (!IsDirectMode(get_current_mode())) {
602     XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
603 cebix 1.1 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
604 gbeauche 1.15 }
605     XSync(x_display, false);
606 cebix 1.1
607 gbeauche 1.15 // Init blitting routines
608     int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, DepthModeForPixelDepth(depth));
609 gbeauche 1.3 #if ENABLE_VOSF
610     bool native_byte_order;
611     #ifdef WORDS_BIGENDIAN
612     native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
613     #else
614     native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
615     #endif
616     #if REAL_ADDRESSING || DIRECT_ADDRESSING
617     // Screen_blitter_init() returns TRUE if VOSF is mandatory
618     // i.e. the framebuffer update function is not Blit_Copy_Raw
619 gbeauche 1.30 use_vosf = Screen_blitter_init(visualFormat, native_byte_order, depth);
620 gbeauche 1.3
621     if (use_vosf) {
622     // Allocate memory for frame buffer (SIZE is extended to page-boundary)
623     the_host_buffer = the_buffer;
624     the_buffer_size = page_extend((height + 2) * bytes_per_row);
625     the_buffer_copy = (uint8 *)malloc(the_buffer_size);
626     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
627 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));
628 gbeauche 1.3 }
629     #else
630     use_vosf = false;
631     #endif
632     #endif
633 gbeauche 1.15
634     // Set frame buffer base
635     D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf));
636 gbeauche 1.33 screen_base = Host2MacAddr(the_buffer);
637 cebix 1.1 VModes[cur_mode].viRowBytes = bytes_per_row;
638     return true;
639     #else
640     ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
641     return false;
642     #endif
643     }
644    
645     static bool open_display(void)
646     {
647 gbeauche 1.13 D(bug("open_display()\n"));
648     const VideoInfo &mode = VModes[cur_mode];
649    
650     // Find best available X visual
651     if (!find_visual_for_depth(mode.viAppleMode)) {
652     ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
653     return false;
654     }
655    
656 gbeauche 1.30 // Build up visualFormat structure
657     visualFormat.depth = visualInfo.depth;
658     visualFormat.Rmask = visualInfo.red_mask;
659     visualFormat.Gmask = visualInfo.green_mask;
660     visualFormat.Bmask = visualInfo.blue_mask;
661    
662 gbeauche 1.13 // Create color maps
663     if (color_class == PseudoColor || color_class == DirectColor) {
664     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
665     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
666     } else {
667     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone);
668     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone);
669     }
670    
671     // Find pixel format of direct modes
672     if (color_class == DirectColor || color_class == TrueColor) {
673     rshift = gshift = bshift = 0;
674     rloss = gloss = bloss = 8;
675     uint32 mask;
676     for (mask=vis->red_mask; !(mask&1); mask>>=1)
677     ++rshift;
678     for (; mask&1; mask>>=1)
679     --rloss;
680     for (mask=vis->green_mask; !(mask&1); mask>>=1)
681     ++gshift;
682     for (; mask&1; mask>>=1)
683     --gloss;
684     for (mask=vis->blue_mask; !(mask&1); mask>>=1)
685     ++bshift;
686     for (; mask&1; mask>>=1)
687     --bloss;
688     }
689    
690     // Preset palette pixel values for CLUT or gamma table
691     if (color_class == DirectColor) {
692     int num = vis->map_entries;
693     for (int i=0; i<num; i++) {
694     int c = (i * 256) / num;
695     x_palette[i].pixel = map_rgb(c, c, c);
696     x_palette[i].flags = DoRed | DoGreen | DoBlue;
697     }
698     } else if (color_class == PseudoColor) {
699     for (int i=0; i<256; i++) {
700     x_palette[i].pixel = i;
701     x_palette[i].flags = DoRed | DoGreen | DoBlue;
702     }
703     }
704    
705     // Load gray ramp to color map
706     int num = (color_class == DirectColor ? vis->map_entries : 256);
707     for (int i=0; i<num; i++) {
708     int c = (i * 256) / num;
709     x_palette[i].red = c * 0x0101;
710     x_palette[i].green = c * 0x0101;
711     x_palette[i].blue = c * 0x0101;
712     }
713     if (color_class == PseudoColor || color_class == DirectColor) {
714     XStoreColors(x_display, cmap[0], x_palette, num);
715     XStoreColors(x_display, cmap[1], x_palette, num);
716 cebix 1.1 }
717 gbeauche 1.3
718 gbeauche 1.13 #ifdef ENABLE_VOSF
719     // Load gray ramp to 8->16/32 expand map
720     if (!IsDirectMode(get_current_mode()) && xdepth > 8)
721     for (int i=0; i<256; i++)
722     ExpandMap[i] = map_rgb(i, i, i);
723     #endif
724    
725     // Create display of requested type
726     display_type = mode.viType;
727     depth = depth_of_video_mode(mode.viAppleMode);
728    
729 gbeauche 1.3 bool display_open = false;
730 cebix 1.1 if (display_type == DIS_SCREEN)
731 gbeauche 1.3 display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
732 cebix 1.1 else if (display_type == DIS_WINDOW)
733 gbeauche 1.3 display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
734    
735     #ifdef ENABLE_VOSF
736     if (use_vosf) {
737     // Initialize the VOSF system
738     if (!video_vosf_init()) {
739     ErrorAlert(GetString(STR_VOSF_INIT_ERR));
740     return false;
741     }
742     }
743     #endif
744    
745     return display_open;
746 cebix 1.1 }
747    
748    
749     /*
750     * Close display
751     */
752    
753     // Close window
754     static void close_window(void)
755     {
756 gbeauche 1.3 if (have_shm) {
757     XShmDetach(x_display, &shminfo);
758     #ifdef ENABLE_VOSF
759     the_host_buffer = NULL; // don't free() in driver_base dtor
760     #else
761     the_buffer_copy = NULL; // don't free() in driver_base dtor
762     #endif
763     }
764     if (img) {
765     if (!have_shm)
766     img->data = NULL;
767     XDestroyImage(img);
768     }
769     if (have_shm) {
770     shmdt(shminfo.shmaddr);
771     shmctl(shminfo.shmid, IPC_RMID, 0);
772     }
773     if (the_gc)
774     XFreeGC(x_display, the_gc);
775    
776 gbeauche 1.13 XFlush(x_display);
777     XSync(x_display, false);
778 cebix 1.1 }
779    
780     // Close DGA mode
781     static void close_dga(void)
782     {
783     #ifdef ENABLE_XF86_DGA
784     XF86DGADirectVideo(x_display, screen, 0);
785     XUngrabPointer(x_display, CurrentTime);
786     XUngrabKeyboard(x_display, CurrentTime);
787     #endif
788    
789     #ifdef ENABLE_XF86_VIDMODE
790     if (has_vidmode)
791     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
792     #endif
793 gbeauche 1.3
794     if (!use_vosf) {
795     // don't free() the screen buffer in driver_base dtor
796     the_buffer = NULL;
797     }
798     #ifdef ENABLE_VOSF
799     else {
800     // don't free() the screen buffer in driver_base dtor
801     the_host_buffer = NULL;
802     }
803     #endif
804 cebix 1.1 }
805    
806     static void close_display(void)
807     {
808     if (display_type == DIS_SCREEN)
809     close_dga();
810     else if (display_type == DIS_WINDOW)
811     close_window();
812 gbeauche 1.3
813 gbeauche 1.15 // Close window
814     if (the_win) {
815     XUnmapWindow(x_display, the_win);
816     wait_unmapped(the_win);
817     XDestroyWindow(x_display, the_win);
818     }
819    
820 gbeauche 1.13 // Free colormaps
821     if (cmap[0]) {
822     XFreeColormap(x_display, cmap[0]);
823     cmap[0] = 0;
824     }
825     if (cmap[1]) {
826     XFreeColormap(x_display, cmap[1]);
827     cmap[1] = 0;
828     }
829    
830 gbeauche 1.3 #ifdef ENABLE_VOSF
831     if (use_vosf) {
832     // Deinitialize VOSF
833     video_vosf_exit();
834     }
835     #endif
836    
837     // Free frame buffer(s)
838     if (!use_vosf) {
839     if (the_buffer_copy) {
840     free(the_buffer_copy);
841     the_buffer_copy = NULL;
842     }
843     }
844     #ifdef ENABLE_VOSF
845     else {
846     // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
847     if (the_buffer != VM_MAP_FAILED) {
848     D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
849     vm_release(the_buffer, the_buffer_size);
850     the_buffer = NULL;
851     }
852     if (the_host_buffer) {
853     D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
854     free(the_host_buffer);
855     the_host_buffer = NULL;
856     }
857     if (the_buffer_copy) {
858     D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
859     free(the_buffer_copy);
860     the_buffer_copy = NULL;
861     }
862     }
863     #endif
864 cebix 1.1 }
865    
866    
867     /*
868     * Initialization
869     */
870    
871 gbeauche 1.6 // Init keycode translation table
872     static void keycode_init(void)
873     {
874     bool use_kc = PrefsFindBool("keycodes");
875     if (use_kc) {
876    
877     // Get keycode file path from preferences
878     const char *kc_path = PrefsFindString("keycodefile");
879    
880     // Open keycode table
881     FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
882     if (f == NULL) {
883     char str[256];
884     sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
885     WarningAlert(str);
886     return;
887     }
888    
889     // Default translation table
890     for (int i=0; i<256; i++)
891     keycode_table[i] = -1;
892    
893     // Search for server vendor string, then read keycodes
894     const char *vendor = ServerVendor(x_display);
895 gbeauche 1.29 // Force use of MacX mappings on MacOS X with Apple's X server
896     int dummy;
897     if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy))
898     vendor = "MacX";
899 gbeauche 1.6 bool vendor_found = false;
900     char line[256];
901     while (fgets(line, 255, f)) {
902     // Read line
903     int len = strlen(line);
904     if (len == 0)
905     continue;
906     line[len-1] = 0;
907    
908     // Comments begin with "#" or ";"
909     if (line[0] == '#' || line[0] == ';' || line[0] == 0)
910     continue;
911    
912     if (vendor_found) {
913     // Read keycode
914     int x_code, mac_code;
915     if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
916     keycode_table[x_code & 0xff] = mac_code;
917     else
918     break;
919     } else {
920     // Search for vendor string
921     if (strstr(vendor, line) == vendor)
922     vendor_found = true;
923     }
924     }
925    
926     // Keycode file completely read
927     fclose(f);
928     use_keycodes = vendor_found;
929    
930     // Vendor not found? Then display warning
931     if (!vendor_found) {
932     char str[256];
933     sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
934     WarningAlert(str);
935     return;
936     }
937     }
938     }
939    
940 gbeauche 1.15 // Find Apple mode matching best specified dimensions
941     static int find_apple_resolution(int xsize, int ysize)
942     {
943     int apple_id;
944     if (xsize < 800)
945     apple_id = APPLE_640x480;
946     else if (xsize < 1024)
947     apple_id = APPLE_800x600;
948     else if (xsize < 1152)
949     apple_id = APPLE_1024x768;
950     else if (xsize < 1280) {
951     if (ysize < 900)
952     apple_id = APPLE_1152x768;
953     else
954     apple_id = APPLE_1152x900;
955     }
956     else if (xsize < 1600)
957     apple_id = APPLE_1280x1024;
958     else
959     apple_id = APPLE_1600x1200;
960     return apple_id;
961     }
962    
963     // Find mode in list of supported modes
964     static int find_mode(int apple_mode, int apple_id, int type)
965     {
966     for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
967     if (p->viType == type && p->viAppleID == apple_id && p->viAppleMode == apple_mode)
968     return p - VModes;
969     }
970     return -1;
971     }
972    
973 gbeauche 1.36 // Add custom video mode
974     static void add_custom_mode(VideoInfo *&p, int type, uint32 x, uint32 y, int apple_mode, int apple_id)
975     {
976     p->viType = type;
977     p->viXsize = x;
978     p->viYsize = y;
979     p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode);
980     p->viAppleMode = apple_mode;
981     p->viAppleID = apple_id;
982     p++;
983     }
984    
985 gbeauche 1.13 // Add mode to list of supported modes
986     static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type)
987 cebix 1.1 {
988     if (allow & test) {
989 gbeauche 1.36 uint32 x = 0, y = 0;
990 cebix 1.1 switch (apple_id) {
991     case APPLE_W_640x480:
992     case APPLE_640x480:
993 gbeauche 1.36 x = 640;
994     y = 480;
995 cebix 1.1 break;
996     case APPLE_W_800x600:
997     case APPLE_800x600:
998 gbeauche 1.36 x = 800;
999     y = 600;
1000 cebix 1.1 break;
1001     case APPLE_1024x768:
1002 gbeauche 1.36 x = 1024;
1003     y = 768;
1004 cebix 1.1 break;
1005 gbeauche 1.15 case APPLE_1152x768:
1006 gbeauche 1.36 x = 1152;
1007     y = 768;
1008 gbeauche 1.15 break;
1009 cebix 1.1 case APPLE_1152x900:
1010 gbeauche 1.36 x = 1152;
1011     y = 900;
1012 cebix 1.1 break;
1013     case APPLE_1280x1024:
1014 gbeauche 1.36 x = 1280;
1015     y = 1024;
1016 cebix 1.1 break;
1017     case APPLE_1600x1200:
1018 gbeauche 1.36 x = 1600;
1019     y = 1200;
1020 cebix 1.1 break;
1021     }
1022 gbeauche 1.36 add_custom_mode(p, type, x, y, apple_mode, apple_id);
1023 cebix 1.1 }
1024     }
1025    
1026 gbeauche 1.13 // Add standard list of windowed modes for given color depth
1027     static void add_window_modes(VideoInfo *&p, int window_modes, int mode)
1028     {
1029     add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
1030     add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
1031     }
1032    
1033 cebix 1.1 static bool has_mode(int x, int y)
1034     {
1035     #ifdef ENABLE_XF86_VIDMODE
1036     for (int i=0; i<num_x_video_modes; i++)
1037     if (x_video_modes[i]->hdisplay >= x && x_video_modes[i]->vdisplay >= y)
1038     return true;
1039     return false;
1040     #else
1041     return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y;
1042     #endif
1043     }
1044    
1045     bool VideoInit(void)
1046     {
1047 gbeauche 1.3 #ifdef ENABLE_VOSF
1048     // Zero the mainBuffer structure
1049     mainBuffer.dirtyPages = NULL;
1050     mainBuffer.pageInfo = NULL;
1051     #endif
1052    
1053 gbeauche 1.4 // Check if X server runs on local machine
1054     local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
1055     || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
1056    
1057 gbeauche 1.6 // Init keycode translation
1058     keycode_init();
1059    
1060 gbeauche 1.8 // Read frame skip prefs
1061     frame_skip = PrefsFindInt32("frameskip");
1062     if (frame_skip == 0)
1063     frame_skip = 1;
1064    
1065     // Read mouse wheel prefs
1066     mouse_wheel_mode = PrefsFindInt32("mousewheelmode");
1067     mouse_wheel_lines = PrefsFindInt32("mousewheellines");
1068    
1069 cebix 1.1 // Init variables
1070     private_data = NULL;
1071     video_activated = true;
1072    
1073     // Find screen and root window
1074     screen = XDefaultScreen(x_display);
1075     rootwin = XRootWindow(x_display, screen);
1076    
1077 gbeauche 1.13 // Get sorted list of available depths
1078     avail_depths = XListDepths(x_display, screen, &num_depths);
1079     if (avail_depths == NULL) {
1080     ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
1081     return false;
1082     }
1083     sort(avail_depths, avail_depths + num_depths);
1084    
1085 cebix 1.1 // Get screen depth
1086     xdepth = DefaultDepth(x_display, screen);
1087    
1088     #ifdef ENABLE_XF86_DGA
1089     // DGA available?
1090     int event_base, error_base;
1091 gbeauche 1.4 if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
1092 cebix 1.1 int dga_flags = 0;
1093     XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
1094     has_dga = dga_flags & XF86DGADirectPresent;
1095     } else
1096     has_dga = false;
1097     #endif
1098    
1099     #ifdef ENABLE_XF86_VIDMODE
1100     // VidMode available?
1101     int vm_event_base, vm_error_base;
1102     has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
1103     if (has_vidmode)
1104     XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
1105     #endif
1106    
1107     // Find black and white colors
1108     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
1109     XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
1110     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
1111     XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
1112     black_pixel = BlackPixel(x_display, screen);
1113     white_pixel = WhitePixel(x_display, screen);
1114    
1115     // Mac screen depth follows X depth (for now)
1116 gbeauche 1.13 int default_mode = APPLE_8_BIT;
1117     switch (DefaultDepth(x_display, screen)) {
1118     case 1:
1119     default_mode = APPLE_1_BIT;
1120     break;
1121     case 8:
1122     default_mode = APPLE_8_BIT;
1123     break;
1124     case 15: case 16:
1125     default_mode = APPLE_16_BIT;
1126     break;
1127     case 24: case 32:
1128     default_mode = APPLE_32_BIT;
1129     break;
1130 cebix 1.1 }
1131    
1132 gbeauche 1.37 // Get screen mode from preferences
1133     const char *mode_str = PrefsFindString("screen");
1134     int default_width = 640, default_height = 480;
1135     if (mode_str) {
1136     display_type = DIS_INVALID;
1137     if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
1138     display_type = DIS_WINDOW;
1139     #ifdef ENABLE_XF86_DGA
1140     else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2)
1141     display_type = DIS_SCREEN;
1142     #endif
1143     if (display_type == DIS_INVALID) {
1144     D(bug("Invalid screen mode specified, defaulting to old modes selection\n"));
1145     mode_str = NULL;
1146     }
1147     else {
1148     if (default_width <= 0)
1149     default_width = DisplayWidth(x_display, screen);
1150     else if (default_width > DisplayWidth(x_display, screen))
1151     default_width = DisplayWidth(x_display, screen);
1152     if (default_height <= 0)
1153     default_height = DisplayHeight(x_display, screen);
1154     else if (default_height > DisplayHeight(x_display, screen))
1155     default_height = DisplayHeight(x_display, screen);
1156     }
1157     }
1158    
1159 cebix 1.1 // Construct video mode table
1160     uint32 window_modes = PrefsFindInt32("windowmodes");
1161     uint32 screen_modes = PrefsFindInt32("screenmodes");
1162     if (!has_dga)
1163     screen_modes = 0;
1164 gbeauche 1.37 if (mode_str)
1165     window_modes = screen_modes = 0;
1166     else if (window_modes == 0 && screen_modes == 0)
1167 cebix 1.1 window_modes |= 3; // Allow at least 640x480 and 800x600 window modes
1168    
1169     VideoInfo *p = VModes;
1170 gbeauche 1.37 if (mode_str) {
1171     if (display_type == DIS_WINDOW) {
1172     for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) {
1173     if (find_visual_for_depth(d)) {
1174     if (default_width > 640 && default_height > 480)
1175     add_mode(p, 3, 1, d, APPLE_W_640x480, DIS_WINDOW);
1176     if (default_width > 800 && default_height > 600)
1177     add_mode(p, 3, 2, d, APPLE_W_800x600, DIS_WINDOW);
1178     add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM);
1179     }
1180     }
1181     } else
1182     add_custom_mode(p, display_type, default_width, default_height, default_mode, APPLE_CUSTOM);
1183     } else if (window_modes) {
1184     for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++)
1185     if (find_visual_for_depth(d))
1186     add_window_modes(p, window_modes, d);
1187     } else if (has_vidmode) {
1188 cebix 1.1 if (has_mode(640, 480))
1189 gbeauche 1.13 add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN);
1190 cebix 1.1 if (has_mode(800, 600))
1191 gbeauche 1.13 add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN);
1192 cebix 1.1 if (has_mode(1024, 768))
1193 gbeauche 1.13 add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN);
1194 gbeauche 1.15 if (has_mode(1152, 768))
1195     add_mode(p, screen_modes, 64, default_mode, APPLE_1152x768, DIS_SCREEN);
1196 cebix 1.1 if (has_mode(1152, 900))
1197 gbeauche 1.13 add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN);
1198 cebix 1.1 if (has_mode(1280, 1024))
1199 gbeauche 1.13 add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN);
1200 cebix 1.1 if (has_mode(1600, 1200))
1201 gbeauche 1.13 add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN);
1202 cebix 1.1 } else if (screen_modes) {
1203     int xsize = DisplayWidth(x_display, screen);
1204     int ysize = DisplayHeight(x_display, screen);
1205 gbeauche 1.15 int apple_id = find_apple_resolution(xsize, ysize);
1206 cebix 1.1 p->viType = DIS_SCREEN;
1207     p->viRowBytes = 0;
1208     p->viXsize = xsize;
1209     p->viYsize = ysize;
1210 gbeauche 1.13 p->viAppleMode = default_mode;
1211 cebix 1.1 p->viAppleID = apple_id;
1212     p++;
1213     }
1214     p->viType = DIS_INVALID; // End marker
1215     p->viRowBytes = 0;
1216     p->viXsize = p->viYsize = 0;
1217     p->viAppleMode = 0;
1218     p->viAppleID = 0;
1219    
1220 gbeauche 1.13 // Find default mode (window 640x480)
1221     cur_mode = -1;
1222 gbeauche 1.15 if (has_dga && screen_modes) {
1223     int screen_width = DisplayWidth(x_display, screen);
1224     int screen_height = DisplayHeight(x_display, screen);
1225     int apple_id = find_apple_resolution(screen_width, screen_height);
1226     if (apple_id != -1)
1227     cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN);
1228 gbeauche 1.37 } else if (has_dga && mode_str)
1229     cur_mode = find_mode(default_mode, APPLE_CUSTOM, DIS_SCREEN);
1230    
1231 gbeauche 1.25 if (cur_mode == -1) {
1232     // pick up first windowed mode available
1233     for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) {
1234     if (p->viType == DIS_WINDOW && p->viAppleMode == default_mode) {
1235     cur_mode = p - VModes;
1236     break;
1237     }
1238     }
1239     }
1240 gbeauche 1.13 assert(cur_mode != -1);
1241    
1242     #if DEBUG
1243     D(bug("Available video modes:\n"));
1244     for (p = VModes; p->viType != DIS_INVALID; p++) {
1245     int bits = depth_of_video_mode(p->viAppleMode);
1246     D(bug(" %dx%d (ID %02x), %d colors\n", p->viXsize, p->viYsize, p->viAppleID, 1 << bits));
1247     }
1248     #endif
1249    
1250 cebix 1.1 // Open window/screen
1251     if (!open_display())
1252     return false;
1253    
1254     #if 0
1255     // Ignore errors from now on
1256     XSetErrorHandler(ignore_errors);
1257     #endif
1258    
1259     // Start periodic thread
1260     XSync(x_display, false);
1261 gbeauche 1.15 Set_pthread_attr(&redraw_thread_attr, 0);
1262 gbeauche 1.28 redraw_thread_cancel = false;
1263 gbeauche 1.15 redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0);
1264 cebix 1.1 D(bug("Redraw thread installed (%ld)\n", redraw_thread));
1265     return true;
1266     }
1267    
1268    
1269     /*
1270     * Deinitialization
1271     */
1272    
1273     void VideoExit(void)
1274     {
1275     // Stop redraw thread
1276     if (redraw_thread_active) {
1277 gbeauche 1.28 redraw_thread_cancel = true;
1278 cebix 1.1 pthread_cancel(redraw_thread);
1279     pthread_join(redraw_thread, NULL);
1280     redraw_thread_active = false;
1281     }
1282    
1283 gbeauche 1.3 #ifdef ENABLE_VOSF
1284     if (use_vosf) {
1285     // Deinitialize VOSF
1286     video_vosf_exit();
1287     }
1288     #endif
1289    
1290 cebix 1.1 // Close window and server connection
1291     if (x_display != NULL) {
1292     XSync(x_display, false);
1293     close_display();
1294     XFlush(x_display);
1295     XSync(x_display, false);
1296     }
1297     }
1298    
1299    
1300     /*
1301     * Suspend/resume emulator
1302     */
1303    
1304     extern void PauseEmulator(void);
1305     extern void ResumeEmulator(void);
1306    
1307     static void suspend_emul(void)
1308     {
1309     if (display_type == DIS_SCREEN) {
1310     // Release ctrl key
1311     ADBKeyUp(0x36);
1312     ctrl_down = false;
1313    
1314     // Pause MacOS thread
1315     PauseEmulator();
1316     emul_suspended = true;
1317    
1318     // Save frame buffer
1319     fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1320     if (fb_save)
1321 gbeauche 1.33 Mac2Host_memcpy(fb_save, screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1322 cebix 1.1
1323     // Close full screen display
1324     #ifdef ENABLE_XF86_DGA
1325     XF86DGADirectVideo(x_display, screen, 0);
1326     XUngrabPointer(x_display, CurrentTime);
1327     XUngrabKeyboard(x_display, CurrentTime);
1328     #endif
1329     XSync(x_display, false);
1330    
1331     // Open "suspend" window
1332     XSetWindowAttributes wattr;
1333     wattr.event_mask = KeyPressMask;
1334     wattr.background_pixel = black_pixel;
1335     wattr.border_pixel = black_pixel;
1336     wattr.backing_store = Always;
1337     wattr.backing_planes = xdepth;
1338     wattr.colormap = DefaultColormap(x_display, screen);
1339     XSync(x_display, false);
1340     suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1341     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1342     CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1343     XSync(x_display, false);
1344     XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1345     XMapRaised(x_display, suspend_win);
1346     XSync(x_display, false);
1347     }
1348     }
1349    
1350     static void resume_emul(void)
1351     {
1352     // Close "suspend" window
1353     XDestroyWindow(x_display, suspend_win);
1354     XSync(x_display, false);
1355    
1356     // Reopen full screen display
1357     XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1358     XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1359 gbeauche 1.12 #ifdef ENABLE_XF86_DGA
1360 cebix 1.1 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1361     XF86DGASetViewPort(x_display, screen, 0, 0);
1362 gbeauche 1.12 #endif
1363 cebix 1.1 XSync(x_display, false);
1364    
1365 gbeauche 1.3 // the_buffer already contains the data to restore. i.e. since a temporary
1366     // frame buffer is used when VOSF is actually used, fb_save is therefore
1367     // not necessary.
1368     #ifdef ENABLE_VOSF
1369     if (use_vosf) {
1370     LOCK_VOSF;
1371     PFLAG_SET_ALL;
1372     UNLOCK_VOSF;
1373     memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1374     }
1375     #endif
1376    
1377 cebix 1.1 // Restore frame buffer
1378     if (fb_save) {
1379 gbeauche 1.3 #ifdef ENABLE_VOSF
1380     // Don't copy fb_save to the temporary frame buffer in VOSF mode
1381     if (!use_vosf)
1382     #endif
1383 gbeauche 1.33 Host2Mac_memcpy(screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
1384 cebix 1.1 free(fb_save);
1385     fb_save = NULL;
1386     }
1387     if (depth == 8)
1388     palette_changed = true;
1389    
1390     // Resume MacOS thread
1391     emul_suspended = false;
1392     ResumeEmulator();
1393     }
1394    
1395    
1396     /*
1397     * Close screen in full-screen mode
1398     */
1399    
1400     void VideoQuitFullScreen(void)
1401     {
1402     D(bug("VideoQuitFullScreen()\n"));
1403     if (display_type == DIS_SCREEN) {
1404     quit_full_screen = true;
1405     while (!quit_full_screen_ack) ;
1406     }
1407     }
1408    
1409    
1410     /*
1411     * X11 event handling
1412     */
1413    
1414     // Translate key event to Mac keycode
1415     static int kc_decode(KeySym ks)
1416     {
1417     switch (ks) {
1418     case XK_A: case XK_a: return 0x00;
1419     case XK_B: case XK_b: return 0x0b;
1420     case XK_C: case XK_c: return 0x08;
1421     case XK_D: case XK_d: return 0x02;
1422     case XK_E: case XK_e: return 0x0e;
1423     case XK_F: case XK_f: return 0x03;
1424     case XK_G: case XK_g: return 0x05;
1425     case XK_H: case XK_h: return 0x04;
1426     case XK_I: case XK_i: return 0x22;
1427     case XK_J: case XK_j: return 0x26;
1428     case XK_K: case XK_k: return 0x28;
1429     case XK_L: case XK_l: return 0x25;
1430     case XK_M: case XK_m: return 0x2e;
1431     case XK_N: case XK_n: return 0x2d;
1432     case XK_O: case XK_o: return 0x1f;
1433     case XK_P: case XK_p: return 0x23;
1434     case XK_Q: case XK_q: return 0x0c;
1435     case XK_R: case XK_r: return 0x0f;
1436     case XK_S: case XK_s: return 0x01;
1437     case XK_T: case XK_t: return 0x11;
1438     case XK_U: case XK_u: return 0x20;
1439     case XK_V: case XK_v: return 0x09;
1440     case XK_W: case XK_w: return 0x0d;
1441     case XK_X: case XK_x: return 0x07;
1442     case XK_Y: case XK_y: return 0x10;
1443     case XK_Z: case XK_z: return 0x06;
1444    
1445     case XK_1: case XK_exclam: return 0x12;
1446     case XK_2: case XK_at: return 0x13;
1447     case XK_3: case XK_numbersign: return 0x14;
1448     case XK_4: case XK_dollar: return 0x15;
1449     case XK_5: case XK_percent: return 0x17;
1450     case XK_6: return 0x16;
1451     case XK_7: return 0x1a;
1452     case XK_8: return 0x1c;
1453     case XK_9: return 0x19;
1454     case XK_0: return 0x1d;
1455    
1456     case XK_grave: case XK_asciitilde: return 0x0a;
1457     case XK_minus: case XK_underscore: return 0x1b;
1458     case XK_equal: case XK_plus: return 0x18;
1459     case XK_bracketleft: case XK_braceleft: return 0x21;
1460     case XK_bracketright: case XK_braceright: return 0x1e;
1461     case XK_backslash: case XK_bar: return 0x2a;
1462     case XK_semicolon: case XK_colon: return 0x29;
1463     case XK_apostrophe: case XK_quotedbl: return 0x27;
1464     case XK_comma: case XK_less: return 0x2b;
1465     case XK_period: case XK_greater: return 0x2f;
1466     case XK_slash: case XK_question: return 0x2c;
1467    
1468     case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1469     case XK_Return: return 0x24;
1470     case XK_space: return 0x31;
1471     case XK_BackSpace: return 0x33;
1472    
1473     case XK_Delete: return 0x75;
1474     case XK_Insert: return 0x72;
1475     case XK_Home: case XK_Help: return 0x73;
1476     case XK_End: return 0x77;
1477     #ifdef __hpux
1478     case XK_Prior: return 0x74;
1479     case XK_Next: return 0x79;
1480     #else
1481     case XK_Page_Up: return 0x74;
1482     case XK_Page_Down: return 0x79;
1483     #endif
1484    
1485     case XK_Control_L: return 0x36;
1486     case XK_Control_R: return 0x36;
1487     case XK_Shift_L: return 0x38;
1488     case XK_Shift_R: return 0x38;
1489     case XK_Alt_L: return 0x37;
1490     case XK_Alt_R: return 0x37;
1491     case XK_Meta_L: return 0x3a;
1492     case XK_Meta_R: return 0x3a;
1493     case XK_Menu: return 0x32;
1494     case XK_Caps_Lock: return 0x39;
1495     case XK_Num_Lock: return 0x47;
1496    
1497     case XK_Up: return 0x3e;
1498     case XK_Down: return 0x3d;
1499     case XK_Left: return 0x3b;
1500     case XK_Right: return 0x3c;
1501    
1502     case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1503    
1504     case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1505     case XK_F2: return 0x78;
1506     case XK_F3: return 0x63;
1507     case XK_F4: return 0x76;
1508     case XK_F5: return 0x60;
1509     case XK_F6: return 0x61;
1510     case XK_F7: return 0x62;
1511     case XK_F8: return 0x64;
1512     case XK_F9: return 0x65;
1513     case XK_F10: return 0x6d;
1514     case XK_F11: return 0x67;
1515     case XK_F12: return 0x6f;
1516    
1517     case XK_Print: return 0x69;
1518     case XK_Scroll_Lock: return 0x6b;
1519     case XK_Pause: return 0x71;
1520    
1521     #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1522     case XK_KP_0: case XK_KP_Insert: return 0x52;
1523     case XK_KP_1: case XK_KP_End: return 0x53;
1524     case XK_KP_2: case XK_KP_Down: return 0x54;
1525     case XK_KP_3: case XK_KP_Next: return 0x55;
1526     case XK_KP_4: case XK_KP_Left: return 0x56;
1527     case XK_KP_5: case XK_KP_Begin: return 0x57;
1528     case XK_KP_6: case XK_KP_Right: return 0x58;
1529     case XK_KP_7: case XK_KP_Home: return 0x59;
1530     case XK_KP_8: case XK_KP_Up: return 0x5b;
1531     case XK_KP_9: case XK_KP_Prior: return 0x5c;
1532     case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1533     #else
1534     case XK_KP_0: return 0x52;
1535     case XK_KP_1: return 0x53;
1536     case XK_KP_2: return 0x54;
1537     case XK_KP_3: return 0x55;
1538     case XK_KP_4: return 0x56;
1539     case XK_KP_5: return 0x57;
1540     case XK_KP_6: return 0x58;
1541     case XK_KP_7: return 0x59;
1542     case XK_KP_8: return 0x5b;
1543     case XK_KP_9: return 0x5c;
1544     case XK_KP_Decimal: return 0x41;
1545     #endif
1546     case XK_KP_Add: return 0x45;
1547     case XK_KP_Subtract: return 0x4e;
1548     case XK_KP_Multiply: return 0x43;
1549     case XK_KP_Divide: return 0x4b;
1550     case XK_KP_Enter: return 0x4c;
1551     case XK_KP_Equal: return 0x51;
1552     }
1553     return -1;
1554     }
1555    
1556 gbeauche 1.27 static int event2keycode(XKeyEvent &ev, bool key_down)
1557 cebix 1.1 {
1558     KeySym ks;
1559     int i = 0;
1560    
1561     do {
1562 gbeauche 1.6 ks = XLookupKeysym(&ev, i++);
1563 gbeauche 1.27 int as = kc_decode(ks);
1564     if (as >= 0)
1565     return as;
1566     if (as == -2)
1567 cebix 1.1 return as;
1568     } while (ks != NoSymbol);
1569    
1570     return -1;
1571     }
1572    
1573     static void handle_events(void)
1574     {
1575     // Handle events
1576     for (;;) {
1577     XEvent event;
1578    
1579 gbeauche 1.10 XDisplayLock();
1580 gbeauche 1.9 if (!XCheckMaskEvent(x_display, eventmask, &event)) {
1581     // Handle clipboard events
1582     if (XCheckTypedEvent(x_display, SelectionRequest, &event))
1583     ClipboardSelectionRequest(&event.xselectionrequest);
1584     else if (XCheckTypedEvent(x_display, SelectionClear, &event))
1585     ClipboardSelectionClear(&event.xselectionclear);
1586 gbeauche 1.14
1587     // Window "close" widget clicked
1588     else if (XCheckTypedEvent(x_display, ClientMessage, &event)) {
1589     if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) {
1590     ADBKeyDown(0x7f); // Power key
1591     ADBKeyUp(0x7f);
1592     }
1593     }
1594 gbeauche 1.10
1595     XDisplayUnlock();
1596 cebix 1.1 break;
1597 gbeauche 1.9 }
1598 gbeauche 1.10 XDisplayUnlock();
1599 cebix 1.1
1600     switch (event.type) {
1601     // Mouse button
1602     case ButtonPress: {
1603     unsigned int button = ((XButtonEvent *)&event)->button;
1604     if (button < 4)
1605     ADBMouseDown(button - 1);
1606 gbeauche 1.8 else if (button < 6) { // Wheel mouse
1607     if (mouse_wheel_mode == 0) {
1608     int key = (button == 5) ? 0x79 : 0x74; // Page up/down
1609     ADBKeyDown(key);
1610     ADBKeyUp(key);
1611     } else {
1612     int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down
1613     for(int i=0; i<mouse_wheel_lines; i++) {
1614     ADBKeyDown(key);
1615     ADBKeyUp(key);
1616     }
1617     }
1618     }
1619 cebix 1.1 break;
1620     }
1621     case ButtonRelease: {
1622     unsigned int button = ((XButtonEvent *)&event)->button;
1623     if (button < 4)
1624     ADBMouseUp(button - 1);
1625     break;
1626     }
1627    
1628 gbeauche 1.15 // Mouse entered window
1629 cebix 1.1 case EnterNotify:
1630 gbeauche 1.15 if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab)
1631     ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1632 cebix 1.1 break;
1633 gbeauche 1.15
1634     // Mouse moved
1635 cebix 1.1 case MotionNotify:
1636 gbeauche 1.15 ADBMouseMoved(event.xmotion.x, event.xmotion.y);
1637 cebix 1.1 break;
1638    
1639     // Keyboard
1640     case KeyPress: {
1641 gbeauche 1.27 int code = -1;
1642     if (use_keycodes) {
1643     if (event2keycode(event.xkey, true) != -2) // This is called to process the hotkeys
1644     code = keycode_table[event.xkey.keycode & 0xff];
1645     } else
1646     code = event2keycode(event.xkey, true);
1647     if (code >= 0) {
1648 cebix 1.1 if (!emul_suspended) {
1649 gbeauche 1.27 if (code == 0x39) { // Caps Lock pressed
1650     if (caps_on) {
1651     ADBKeyUp(code);
1652     caps_on = false;
1653     } else {
1654     ADBKeyDown(code);
1655     caps_on = true;
1656     }
1657     } else
1658     ADBKeyDown(code);
1659 cebix 1.1 if (code == 0x36)
1660     ctrl_down = true;
1661     } else {
1662     if (code == 0x31)
1663     resume_emul(); // Space wakes us up
1664     }
1665     }
1666     break;
1667     }
1668     case KeyRelease: {
1669 gbeauche 1.27 int code = -1;
1670     if (use_keycodes) {
1671     if (event2keycode(event.xkey, false) != -2) // This is called to process the hotkeys
1672     code = keycode_table[event.xkey.keycode & 0xff];
1673     } else
1674     code = event2keycode(event.xkey, false);
1675     if (code >= 0 && code != 0x39) { // Don't propagate Caps Lock releases
1676 cebix 1.1 ADBKeyUp(code);
1677     if (code == 0x36)
1678     ctrl_down = false;
1679     }
1680     break;
1681     }
1682    
1683     // Hidden parts exposed, force complete refresh
1684     case Expose:
1685 gbeauche 1.3 #ifdef ENABLE_VOSF
1686     if (use_vosf) { // VOSF refresh
1687     LOCK_VOSF;
1688     PFLAG_SET_ALL;
1689     UNLOCK_VOSF;
1690     }
1691     #endif
1692 cebix 1.1 memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1693     break;
1694     }
1695     }
1696     }
1697    
1698    
1699     /*
1700     * Execute video VBL routine
1701     */
1702    
1703     void VideoVBL(void)
1704     {
1705     if (emerg_quit)
1706     QuitEmulator();
1707    
1708     // Execute video VBL
1709     if (private_data != NULL && private_data->interruptsEnabled)
1710     VSLDoInterruptService(private_data->vslServiceID);
1711     }
1712    
1713    
1714     /*
1715     * Change video mode
1716     */
1717    
1718     int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1719     {
1720     /* return if no mode change */
1721     if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1722     (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1723    
1724     /* first find video mode in table */
1725     for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
1726     if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
1727     (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
1728     csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
1729     csSave->saveData = ReadMacInt32(ParamPtr + csData);
1730     csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1731    
1732     // Disable interrupts and pause redraw thread
1733     DisableInterrupt();
1734     thread_stop_ack = false;
1735     thread_stop_req = true;
1736     while (!thread_stop_ack) ;
1737    
1738     /* close old display */
1739     close_display();
1740    
1741     /* open new display */
1742     cur_mode = i;
1743     bool ok = open_display();
1744    
1745     /* opening the screen failed? Then bail out */
1746     if (!ok) {
1747     ErrorAlert(GetString(STR_FULL_SCREEN_ERR));
1748     QuitEmulator();
1749     }
1750    
1751     WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
1752     csSave->saveBaseAddr=screen_base;
1753     csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1754     csSave->saveMode=VModes[cur_mode].viAppleMode;
1755    
1756     // Enable interrupts and resume redraw thread
1757     thread_stop_req = false;
1758     EnableInterrupt();
1759     return noErr;
1760     }
1761     }
1762     return paramErr;
1763     }
1764    
1765    
1766     /*
1767     * Set color palette
1768     */
1769    
1770     void video_set_palette(void)
1771     {
1772 gbeauche 1.13 LOCK_PALETTE;
1773    
1774     // Convert colors to XColor array
1775     int mode = get_current_mode();
1776     int num_in = palette_size(mode);
1777     int num_out = 256;
1778     bool stretch = false;
1779     if (IsDirectMode(mode)) {
1780     // If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries
1781     num_out = vis->map_entries;
1782     stretch = true;
1783     }
1784     XColor *p = x_palette;
1785     for (int i=0; i<num_out; i++) {
1786     int c = (stretch ? (i * num_in) / num_out : i);
1787     p->red = mac_pal[c].red * 0x0101;
1788     p->green = mac_pal[c].green * 0x0101;
1789     p->blue = mac_pal[c].blue * 0x0101;
1790     p++;
1791     }
1792    
1793     #ifdef ENABLE_VOSF
1794     // Recalculate pixel color expansion map
1795     if (!IsDirectMode(mode) && xdepth > 8) {
1796     for (int i=0; i<256; i++) {
1797     int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier)
1798     ExpandMap[i] = map_rgb(mac_pal[c].red, mac_pal[c].green, mac_pal[c].blue);
1799     }
1800    
1801     // We have to redraw everything because the interpretation of pixel values changed
1802     LOCK_VOSF;
1803     PFLAG_SET_ALL;
1804     UNLOCK_VOSF;
1805     memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1806     }
1807     #endif
1808    
1809     // Tell redraw thread to change palette
1810 cebix 1.1 palette_changed = true;
1811 gbeauche 1.13
1812     UNLOCK_PALETTE;
1813 cebix 1.1 }
1814    
1815    
1816     /*
1817 gbeauche 1.21 * Can we set the MacOS cursor image into the window?
1818     */
1819    
1820     bool video_can_change_cursor(void)
1821     {
1822 gbeauche 1.22 return hw_mac_cursor_accl && (display_type != DIS_SCREEN);
1823 gbeauche 1.21 }
1824    
1825    
1826     /*
1827 cebix 1.1 * Set cursor image for window
1828     */
1829    
1830     void video_set_cursor(void)
1831     {
1832     cursor_changed = true;
1833     }
1834    
1835    
1836     /*
1837     * Thread for window refresh, event handling and other periodic actions
1838     */
1839    
1840     static void update_display(void)
1841     {
1842     // Incremental update code
1843     int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1844     int bytes_per_row = VModes[cur_mode].viRowBytes;
1845     int bytes_per_pixel = VModes[cur_mode].viRowBytes / VModes[cur_mode].viXsize;
1846     uint8 *p, *p2;
1847    
1848     // Check for first line from top and first line from bottom that have changed
1849     y1 = 0;
1850     for (j=0; j<VModes[cur_mode].viYsize; j++) {
1851     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1852     y1 = j;
1853     break;
1854     }
1855     }
1856     y2 = y1 - 1;
1857     for (j=VModes[cur_mode].viYsize-1; j>=y1; j--) {
1858     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1859     y2 = j;
1860     break;
1861     }
1862     }
1863     high = y2 - y1 + 1;
1864    
1865     // Check for first column from left and first column from right that have changed
1866     if (high) {
1867     if (depth == 1) {
1868     x1 = VModes[cur_mode].viXsize;
1869     for (j=y1; j<=y2; j++) {
1870     p = &the_buffer[j * bytes_per_row];
1871     p2 = &the_buffer_copy[j * bytes_per_row];
1872     for (i=0; i<(x1>>3); i++) {
1873     if (*p != *p2) {
1874     x1 = i << 3;
1875     break;
1876     }
1877     p++;
1878     p2++;
1879     }
1880     }
1881     x2 = x1;
1882     for (j=y1; j<=y2; j++) {
1883     p = &the_buffer[j * bytes_per_row];
1884     p2 = &the_buffer_copy[j * bytes_per_row];
1885     p += bytes_per_row;
1886     p2 += bytes_per_row;
1887     for (i=(VModes[cur_mode].viXsize>>3); i>(x2>>3); i--) {
1888     p--;
1889     p2--;
1890     if (*p != *p2) {
1891     x2 = i << 3;
1892     break;
1893     }
1894     }
1895     }
1896     wide = x2 - x1;
1897    
1898     // Update copy of the_buffer
1899     if (high && wide) {
1900     for (j=y1; j<=y2; j++) {
1901     i = j * bytes_per_row + (x1 >> 3);
1902     memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
1903     }
1904     }
1905    
1906     } else {
1907     x1 = VModes[cur_mode].viXsize;
1908     for (j=y1; j<=y2; j++) {
1909     p = &the_buffer[j * bytes_per_row];
1910     p2 = &the_buffer_copy[j * bytes_per_row];
1911     for (i=0; i<x1; i++) {
1912     if (memcmp(p, p2, bytes_per_pixel)) {
1913     x1 = i;
1914     break;
1915     }
1916     p += bytes_per_pixel;
1917     p2 += bytes_per_pixel;
1918     }
1919     }
1920     x2 = x1;
1921     for (j=y1; j<=y2; j++) {
1922     p = &the_buffer[j * bytes_per_row];
1923     p2 = &the_buffer_copy[j * bytes_per_row];
1924     p += bytes_per_row;
1925     p2 += bytes_per_row;
1926     for (i=VModes[cur_mode].viXsize; i>x2; i--) {
1927     p -= bytes_per_pixel;
1928     p2 -= bytes_per_pixel;
1929     if (memcmp(p, p2, bytes_per_pixel)) {
1930     x2 = i;
1931     break;
1932     }
1933     }
1934     }
1935     wide = x2 - x1;
1936    
1937     // Update copy of the_buffer
1938     if (high && wide) {
1939     for (j=y1; j<=y2; j++) {
1940     i = j * bytes_per_row + x1 * bytes_per_pixel;
1941     memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
1942     }
1943     }
1944     }
1945     }
1946    
1947     // Refresh display
1948     if (high && wide) {
1949 gbeauche 1.10 XDisplayLock();
1950 cebix 1.1 if (have_shm)
1951     XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1952     else
1953     XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1954 gbeauche 1.10 XDisplayUnlock();
1955 cebix 1.1 }
1956     }
1957    
1958 gbeauche 1.10 const int VIDEO_REFRESH_HZ = 60;
1959     const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ;
1960    
1961 gbeauche 1.13 static void handle_palette_changes(void)
1962     {
1963     LOCK_PALETTE;
1964    
1965     if (palette_changed && !emul_suspended) {
1966     palette_changed = false;
1967    
1968     int mode = get_current_mode();
1969     if (color_class == PseudoColor || color_class == DirectColor) {
1970     int num = vis->map_entries;
1971     bool set_clut = true;
1972     if (!IsDirectMode(mode) && color_class == DirectColor) {
1973     if (display_type == DIS_WINDOW)
1974     set_clut = false; // Indexed mode on true color screen, don't set CLUT
1975     }
1976    
1977     if (set_clut) {
1978     XDisplayLock();
1979     XStoreColors(x_display, cmap[0], x_palette, num);
1980     XStoreColors(x_display, cmap[1], x_palette, num);
1981     XSync(x_display, false);
1982     XDisplayUnlock();
1983     }
1984     }
1985    
1986     #ifdef ENABLE_XF86_DGA
1987     if (display_type == DIS_SCREEN) {
1988     current_dga_cmap ^= 1;
1989     if (!IsDirectMode(mode) && cmap[current_dga_cmap])
1990     XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1991     }
1992     #endif
1993     }
1994    
1995     UNLOCK_PALETTE;
1996     }
1997    
1998 cebix 1.1 static void *redraw_func(void *arg)
1999     {
2000 gbeauche 1.10 int fd = ConnectionNumber(x_display);
2001    
2002     uint64 start = GetTicks_usec();
2003     int64 ticks = 0;
2004     uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY;
2005 cebix 1.1
2006 gbeauche 1.28 while (!redraw_thread_cancel) {
2007 cebix 1.1
2008     // Pause if requested (during video mode switches)
2009     while (thread_stop_req)
2010     thread_stop_ack = true;
2011    
2012 gbeauche 1.10 int64 delay = next - GetTicks_usec();
2013     if (delay < -VIDEO_REFRESH_DELAY) {
2014    
2015     // We are lagging far behind, so we reset the delay mechanism
2016     next = GetTicks_usec();
2017 cebix 1.1
2018 gbeauche 1.10 } else if (delay <= 0) {
2019    
2020     // Delay expired, refresh display
2021     next += VIDEO_REFRESH_DELAY;
2022     ticks++;
2023    
2024     // Handle X11 events
2025     handle_events();
2026    
2027     // Quit DGA mode if requested
2028     if (quit_full_screen) {
2029     quit_full_screen = false;
2030     if (display_type == DIS_SCREEN) {
2031     XDisplayLock();
2032 cebix 1.1 #ifdef ENABLE_XF86_DGA
2033 gbeauche 1.10 XF86DGADirectVideo(x_display, screen, 0);
2034     XUngrabPointer(x_display, CurrentTime);
2035     XUngrabKeyboard(x_display, CurrentTime);
2036 gbeauche 1.15 XUnmapWindow(x_display, the_win);
2037     wait_unmapped(the_win);
2038     XDestroyWindow(x_display, the_win);
2039 gbeauche 1.10 #endif
2040     XSync(x_display, false);
2041     XDisplayUnlock();
2042     quit_full_screen_ack = true;
2043     return NULL;
2044     }
2045 cebix 1.1 }
2046    
2047 gbeauche 1.10 // Refresh display and set cursor image in window mode
2048     static int tick_counter = 0;
2049     if (display_type == DIS_WINDOW) {
2050     tick_counter++;
2051     if (tick_counter >= frame_skip) {
2052     tick_counter = 0;
2053 cebix 1.1
2054 gbeauche 1.10 // Update display
2055 gbeauche 1.3 #ifdef ENABLE_VOSF
2056 gbeauche 1.10 if (use_vosf) {
2057     XDisplayLock();
2058     if (mainBuffer.dirty) {
2059     LOCK_VOSF;
2060     update_display_window_vosf();
2061     UNLOCK_VOSF;
2062     XSync(x_display, false); // Let the server catch up
2063     }
2064     XDisplayUnlock();
2065 gbeauche 1.3 }
2066 gbeauche 1.10 else
2067 gbeauche 1.3 #endif
2068 gbeauche 1.10 update_display();
2069 cebix 1.1
2070 gbeauche 1.10 // Set new cursor image if it was changed
2071 gbeauche 1.22 if (hw_mac_cursor_accl && cursor_changed) {
2072 gbeauche 1.10 cursor_changed = false;
2073 gbeauche 1.31 uint8 *x_data = (uint8 *)cursor_image->data;
2074     uint8 *x_mask = (uint8 *)cursor_mask_image->data;
2075     for (int i = 0; i < 32; i++) {
2076     x_mask[i] = MacCursor[4 + i] | MacCursor[36 + i];
2077     x_data[i] = MacCursor[4 + i];
2078     }
2079 gbeauche 1.10 XDisplayLock();
2080     XFreeCursor(x_display, mac_cursor);
2081     XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
2082     XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
2083     mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
2084     XDefineCursor(x_display, the_win, mac_cursor);
2085     XDisplayUnlock();
2086     }
2087 cebix 1.1 }
2088     }
2089 gbeauche 1.3 #ifdef ENABLE_VOSF
2090 gbeauche 1.10 else if (use_vosf) {
2091     // Update display (VOSF variant)
2092     if (++tick_counter >= frame_skip) {
2093     tick_counter = 0;
2094     if (mainBuffer.dirty) {
2095     LOCK_VOSF;
2096     update_display_dga_vosf();
2097     UNLOCK_VOSF;
2098     }
2099 gbeauche 1.3 }
2100     }
2101     #endif
2102 cebix 1.1
2103 gbeauche 1.10 // Set new palette if it was changed
2104 gbeauche 1.13 handle_palette_changes();
2105 gbeauche 1.10
2106     } else {
2107    
2108     // No display refresh pending, check for X events
2109     fd_set readfds;
2110     FD_ZERO(&readfds);
2111     FD_SET(fd, &readfds);
2112     struct timeval timeout;
2113     timeout.tv_sec = 0;
2114     timeout.tv_usec = delay;
2115     if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0)
2116     handle_events();
2117 cebix 1.1 }
2118     }
2119     return NULL;
2120     }