ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
Revision: 1.6
Committed: 2003-11-21T17:01:33Z (20 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.5: +97 -15 lines
Log Message:
Merge in "keycodes" support from Basilisk II. e.g. make French keyboard
layout work correctly for me.

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * video_x.cpp - Video/graphics emulation, X11 specific stuff
3     *
4     * SheepShaver (C) 1997-2002 Marc Hellwig and Christian Bauer
5     *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
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    
31     #ifdef HAVE_PTHREADS
32     # include <pthread.h>
33     #endif
34    
35     #ifdef ENABLE_XF86_DGA
36     #include <X11/extensions/xf86dga.h>
37     #endif
38    
39     #ifdef ENABLE_XF86_VIDMODE
40     # include <X11/extensions/xf86vmode.h>
41     #endif
42 cebix 1.1
43     #include "main.h"
44     #include "adb.h"
45     #include "prefs.h"
46     #include "user_strings.h"
47     #include "about_window.h"
48     #include "video.h"
49     #include "video_defs.h"
50    
51     #define DEBUG 0
52     #include "debug.h"
53    
54    
55 gbeauche 1.6 // Constants
56     const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
57 cebix 1.1
58     // Global variables
59     static int32 frame_skip;
60     static bool redraw_thread_active = false; // Flag: Redraw thread installed
61     static pthread_t redraw_thread; // Redraw thread
62    
63 gbeauche 1.4 static bool local_X11; // Flag: X server running on local machine?
64 cebix 1.1 static volatile bool thread_stop_req = false;
65     static volatile bool thread_stop_ack = false; // Acknowledge for thread_stop_req
66    
67     static bool has_dga = false; // Flag: Video DGA capable
68     static bool has_vidmode = false; // Flag: VidMode extension available
69    
70 gbeauche 1.3 #ifdef ENABLE_VOSF
71     static bool use_vosf = true; // Flag: VOSF enabled
72     #else
73     static const bool use_vosf = false; // VOSF not possible
74     #endif
75    
76 cebix 1.1 static bool palette_changed = false; // Flag: Palette changed, redraw thread must update palette
77     static bool ctrl_down = false; // Flag: Ctrl key pressed
78     static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
79     static volatile bool quit_full_screen_ack = false; // Acknowledge for quit_full_screen
80     static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
81    
82     static bool emul_suspended = false; // Flag: emulator suspended
83     static Window suspend_win; // "Suspend" window
84     static void *fb_save = NULL; // Saved frame buffer for suspend
85 gbeauche 1.6 static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
86     static int keycode_table[256]; // X keycode -> Mac keycode translation table
87 cebix 1.1
88     // X11 variables
89     static int screen; // Screen number
90     static int xdepth; // Depth of X screen
91     static int depth; // Depth of Mac frame buffer
92     static Window rootwin, the_win; // Root window and our window
93     static XVisualInfo visualInfo;
94     static Visual *vis;
95     static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode
96     static XColor black, white;
97     static unsigned long black_pixel, white_pixel;
98     static int eventmask;
99     static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask;
100     static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
101    
102     // Variables for window mode
103     static GC the_gc;
104     static XImage *img = NULL;
105     static XShmSegmentInfo shminfo;
106     static XImage *cursor_image, *cursor_mask_image;
107     static Pixmap cursor_map, cursor_mask_map;
108     static Cursor mac_cursor;
109     static GC cursor_gc, cursor_mask_gc;
110     static bool cursor_changed = false; // Flag: Cursor changed, window_func must update cursor
111     static bool have_shm = false; // Flag: SHM present and usable
112 gbeauche 1.3 static uint8 *the_buffer = NULL; // Pointer to Mac frame buffer
113 cebix 1.1 static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer
114 gbeauche 1.3 static uint32 the_buffer_size; // Size of allocated the_buffer
115 cebix 1.1
116     // Variables for DGA mode
117     static char *dga_screen_base;
118     static int dga_fb_width;
119     static int current_dga_cmap;
120    
121     #ifdef ENABLE_XF86_VIDMODE
122     // Variables for XF86 VidMode support
123     static XF86VidModeModeInfo **x_video_modes; // Array of all available modes
124     static int num_x_video_modes;
125     #endif
126    
127    
128     // Prototypes
129     static void *redraw_func(void *arg);
130    
131    
132 gbeauche 1.4 // From main_unix.cpp
133     extern char *x_display_name;
134 cebix 1.1 extern Display *x_display;
135    
136     // From sys_unix.cpp
137     extern void SysMountFirstFloppy(void);
138    
139    
140 gbeauche 1.3 // Video acceleration through SIGSEGV
141     #ifdef ENABLE_VOSF
142     # include "video_vosf.h"
143     #endif
144    
145    
146 cebix 1.1 /*
147     * Open display (window or fullscreen)
148     */
149    
150     // Trap SHM errors
151     static bool shm_error = false;
152     static int (*old_error_handler)(Display *, XErrorEvent *);
153    
154     static int error_handler(Display *d, XErrorEvent *e)
155     {
156     if (e->error_code == BadAccess) {
157     shm_error = true;
158     return 0;
159     } else
160     return old_error_handler(d, e);
161     }
162    
163     // Open window
164     static bool open_window(int width, int height)
165     {
166 gbeauche 1.3 int aligned_width = (width + 15) & ~15;
167     int aligned_height = (height + 15) & ~15;
168    
169 cebix 1.1 // Set absolute mouse mode
170     ADBSetRelMouseMode(false);
171    
172     // Read frame skip prefs
173     frame_skip = PrefsFindInt32("frameskip");
174     if (frame_skip == 0)
175     frame_skip = 1;
176    
177     // Create window
178     XSetWindowAttributes wattr;
179     wattr.event_mask = eventmask = win_eventmask;
180     wattr.background_pixel = black_pixel;
181     wattr.border_pixel = black_pixel;
182     wattr.backing_store = NotUseful;
183    
184     XSync(x_display, false);
185     the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
186     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore, &wattr);
187     XSync(x_display, false);
188     XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
189     XMapRaised(x_display, the_win);
190     XSync(x_display, false);
191    
192     // Set colormap
193     if (depth == 8) {
194     XSetWindowColormap(x_display, the_win, cmap[0]);
195     XSetWMColormapWindows(x_display, the_win, &the_win, 1);
196     }
197    
198     // Make window unresizable
199     XSizeHints *hints;
200     if ((hints = XAllocSizeHints()) != NULL) {
201     hints->min_width = width;
202     hints->max_width = width;
203     hints->min_height = height;
204     hints->max_height = height;
205     hints->flags = PMinSize | PMaxSize;
206     XSetWMNormalHints(x_display, the_win, hints);
207     XFree((char *)hints);
208     }
209    
210 gbeauche 1.5 // 1-bit mode is big-endian; if the X server is little-endian, we can't
211     // use SHM because that doesn't allow changing the image byte order
212     bool need_msb_image = (depth == 1 && XImageByteOrder(x_display) == LSBFirst);
213    
214 cebix 1.1 // Try to create and attach SHM image
215     have_shm = false;
216 gbeauche 1.5 if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) {
217 cebix 1.1
218     // Create SHM image ("height + 2" for safety)
219 gbeauche 1.5 img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
220 cebix 1.1 shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
221 gbeauche 1.3 the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
222     shminfo.shmaddr = img->data = (char *)the_buffer_copy;
223 cebix 1.1 shminfo.readOnly = False;
224    
225     // Try to attach SHM image, catching errors
226     shm_error = false;
227     old_error_handler = XSetErrorHandler(error_handler);
228     XShmAttach(x_display, &shminfo);
229     XSync(x_display, false);
230     XSetErrorHandler(old_error_handler);
231     if (shm_error) {
232     shmdt(shminfo.shmaddr);
233     XDestroyImage(img);
234     shminfo.shmid = -1;
235     } else {
236     have_shm = true;
237     shmctl(shminfo.shmid, IPC_RMID, 0);
238     }
239     }
240    
241     // Create normal X image if SHM doesn't work ("height + 2" for safety)
242     if (!have_shm) {
243 gbeauche 1.3 int bytes_per_row = aligned_width;
244 cebix 1.1 switch (depth) {
245     case 1:
246     bytes_per_row /= 8;
247     break;
248     case 15:
249     case 16:
250     bytes_per_row *= 2;
251     break;
252     case 24:
253     case 32:
254     bytes_per_row *= 4;
255     break;
256     }
257 gbeauche 1.3 the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
258     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);
259 cebix 1.1 }
260    
261     // 1-Bit mode is big-endian
262     if (depth == 1) {
263     img->byte_order = MSBFirst;
264     img->bitmap_bit_order = MSBFirst;
265     }
266    
267 gbeauche 1.3 #ifdef ENABLE_VOSF
268     use_vosf = true;
269     // Allocate memory for frame buffer (SIZE is extended to page-boundary)
270     the_host_buffer = the_buffer_copy;
271     the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line);
272     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
273     the_buffer_copy = (uint8 *)malloc(the_buffer_size);
274     D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer));
275     #else
276     // Allocate memory for frame buffer
277     the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
278     D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy));
279     #endif
280     screen_base = (uint32)the_buffer;
281 cebix 1.1
282     // Create GC
283     the_gc = XCreateGC(x_display, the_win, 0, 0);
284     XSetForeground(x_display, the_gc, black_pixel);
285    
286     // Create cursor
287     cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2);
288     cursor_image->byte_order = MSBFirst;
289     cursor_image->bitmap_bit_order = MSBFirst;
290     cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 36, 16, 16, 16, 2);
291     cursor_mask_image->byte_order = MSBFirst;
292     cursor_mask_image->bitmap_bit_order = MSBFirst;
293     cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
294     cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1);
295     cursor_gc = XCreateGC(x_display, cursor_map, 0, 0);
296     cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0);
297     mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0);
298     cursor_changed = false;
299    
300 gbeauche 1.3 // Init blitting routines
301     bool native_byte_order;
302     #ifdef WORDS_BIGENDIAN
303     native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
304     #else
305     native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
306     #endif
307     #ifdef ENABLE_VOSF
308     Screen_blitter_init(&visualInfo, native_byte_order, depth);
309     #endif
310    
311 cebix 1.1 // Set bytes per row
312     VModes[cur_mode].viRowBytes = img->bytes_per_line;
313     XSync(x_display, false);
314     return true;
315     }
316    
317     // Open DGA display (!! should use X11 VidMode extensions to set mode)
318     static bool open_dga(int width, int height)
319     {
320     #ifdef ENABLE_XF86_DGA
321     // Set relative mouse mode
322     ADBSetRelMouseMode(true);
323    
324     #ifdef ENABLE_XF86_VIDMODE
325     // Switch to best mode
326     if (has_vidmode) {
327     int best = 0;
328     for (int i=1; i<num_x_video_modes; i++) {
329     if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
330     x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
331     best = i;
332     }
333     }
334     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
335     XF86VidModeSetViewPort(x_display, screen, 0, 0);
336     }
337     #endif
338    
339     // Establish direct screen connection
340     XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
341     XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
342     XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
343     XF86DGASetViewPort(x_display, screen, 0, 0);
344     XF86DGASetVidPage(x_display, screen, 0);
345    
346     // Set colormap
347     if (depth == 8)
348     XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
349    
350     // Set bytes per row
351     int bytes_per_row = (dga_fb_width + 7) & ~7;
352     switch (depth) {
353     case 15:
354     case 16:
355     bytes_per_row *= 2;
356     break;
357     case 24:
358     case 32:
359     bytes_per_row *= 4;
360     break;
361     }
362 gbeauche 1.3
363     #if ENABLE_VOSF
364     bool native_byte_order;
365     #ifdef WORDS_BIGENDIAN
366     native_byte_order = (XImageByteOrder(x_display) == MSBFirst);
367     #else
368     native_byte_order = (XImageByteOrder(x_display) == LSBFirst);
369     #endif
370     #if REAL_ADDRESSING || DIRECT_ADDRESSING
371     // Screen_blitter_init() returns TRUE if VOSF is mandatory
372     // i.e. the framebuffer update function is not Blit_Copy_Raw
373     use_vosf = Screen_blitter_init(&visualInfo, native_byte_order, depth);
374    
375     if (use_vosf) {
376     // Allocate memory for frame buffer (SIZE is extended to page-boundary)
377     the_host_buffer = the_buffer;
378     the_buffer_size = page_extend((height + 2) * bytes_per_row);
379     the_buffer_copy = (uint8 *)malloc(the_buffer_size);
380     the_buffer = (uint8 *)vm_acquire(the_buffer_size);
381     }
382     #else
383     use_vosf = false;
384     the_buffer = dga_screen_base;
385     #endif
386     #endif
387     screen_base = (uint32)the_buffer;
388    
389 cebix 1.1 VModes[cur_mode].viRowBytes = bytes_per_row;
390     XSync(x_display, false);
391     return true;
392     #else
393     ErrorAlert("SheepShaver has been compiled with DGA support disabled.");
394     return false;
395     #endif
396     }
397    
398     static bool open_display(void)
399     {
400     display_type = VModes[cur_mode].viType;
401     switch (VModes[cur_mode].viAppleMode) {
402     case APPLE_1_BIT:
403     depth = 1;
404     break;
405     case APPLE_2_BIT:
406     depth = 2;
407     break;
408     case APPLE_4_BIT:
409     depth = 4;
410     break;
411     case APPLE_8_BIT:
412     depth = 8;
413     break;
414     case APPLE_16_BIT:
415 gbeauche 1.2 depth = xdepth == 15 ? 15 : 16;
416 cebix 1.1 break;
417     case APPLE_32_BIT:
418     depth = 32;
419     break;
420     }
421 gbeauche 1.3
422     bool display_open = false;
423 cebix 1.1 if (display_type == DIS_SCREEN)
424 gbeauche 1.3 display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
425 cebix 1.1 else if (display_type == DIS_WINDOW)
426 gbeauche 1.3 display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize);
427    
428     #ifdef ENABLE_VOSF
429     if (use_vosf) {
430     // Initialize the VOSF system
431     if (!video_vosf_init()) {
432     ErrorAlert(GetString(STR_VOSF_INIT_ERR));
433     return false;
434     }
435     }
436     #endif
437    
438     return display_open;
439 cebix 1.1 }
440    
441    
442     /*
443     * Close display
444     */
445    
446     // Close window
447     static void close_window(void)
448     {
449 gbeauche 1.3 if (have_shm) {
450     XShmDetach(x_display, &shminfo);
451     #ifdef ENABLE_VOSF
452     the_host_buffer = NULL; // don't free() in driver_base dtor
453     #else
454     the_buffer_copy = NULL; // don't free() in driver_base dtor
455     #endif
456     }
457     if (img) {
458     if (!have_shm)
459     img->data = NULL;
460     XDestroyImage(img);
461     }
462     if (have_shm) {
463     shmdt(shminfo.shmaddr);
464     shmctl(shminfo.shmid, IPC_RMID, 0);
465     }
466     if (the_gc)
467     XFreeGC(x_display, the_gc);
468    
469 cebix 1.1 // Close window
470     XDestroyWindow(x_display, the_win);
471     }
472    
473     // Close DGA mode
474     static void close_dga(void)
475     {
476     #ifdef ENABLE_XF86_DGA
477     XF86DGADirectVideo(x_display, screen, 0);
478     XUngrabPointer(x_display, CurrentTime);
479     XUngrabKeyboard(x_display, CurrentTime);
480     #endif
481    
482     #ifdef ENABLE_XF86_VIDMODE
483     if (has_vidmode)
484     XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
485     #endif
486 gbeauche 1.3
487     if (!use_vosf) {
488     // don't free() the screen buffer in driver_base dtor
489     the_buffer = NULL;
490     }
491     #ifdef ENABLE_VOSF
492     else {
493     // don't free() the screen buffer in driver_base dtor
494     the_host_buffer = NULL;
495     }
496     #endif
497 cebix 1.1 }
498    
499     static void close_display(void)
500     {
501     if (display_type == DIS_SCREEN)
502     close_dga();
503     else if (display_type == DIS_WINDOW)
504     close_window();
505 gbeauche 1.3
506     #ifdef ENABLE_VOSF
507     if (use_vosf) {
508     // Deinitialize VOSF
509     video_vosf_exit();
510     }
511     #endif
512    
513     // Free frame buffer(s)
514     if (!use_vosf) {
515     if (the_buffer_copy) {
516     free(the_buffer_copy);
517     the_buffer_copy = NULL;
518     }
519     }
520     #ifdef ENABLE_VOSF
521     else {
522     // the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will
523     if (the_buffer != VM_MAP_FAILED) {
524     D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size));
525     vm_release(the_buffer, the_buffer_size);
526     the_buffer = NULL;
527     }
528     if (the_host_buffer) {
529     D(bug(" freeing the_host_buffer at %p\n", the_host_buffer));
530     free(the_host_buffer);
531     the_host_buffer = NULL;
532     }
533     if (the_buffer_copy) {
534     D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy));
535     free(the_buffer_copy);
536     the_buffer_copy = NULL;
537     }
538     }
539     #endif
540 cebix 1.1 }
541    
542    
543     /*
544     * Initialization
545     */
546    
547 gbeauche 1.6 // Init keycode translation table
548     static void keycode_init(void)
549     {
550     bool use_kc = PrefsFindBool("keycodes");
551     if (use_kc) {
552    
553     // Get keycode file path from preferences
554     const char *kc_path = PrefsFindString("keycodefile");
555    
556     // Open keycode table
557     FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
558     if (f == NULL) {
559     char str[256];
560     sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
561     WarningAlert(str);
562     return;
563     }
564    
565     // Default translation table
566     for (int i=0; i<256; i++)
567     keycode_table[i] = -1;
568    
569     // Search for server vendor string, then read keycodes
570     const char *vendor = ServerVendor(x_display);
571     bool vendor_found = false;
572     char line[256];
573     while (fgets(line, 255, f)) {
574     // Read line
575     int len = strlen(line);
576     if (len == 0)
577     continue;
578     line[len-1] = 0;
579    
580     // Comments begin with "#" or ";"
581     if (line[0] == '#' || line[0] == ';' || line[0] == 0)
582     continue;
583    
584     if (vendor_found) {
585     // Read keycode
586     int x_code, mac_code;
587     if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
588     keycode_table[x_code & 0xff] = mac_code;
589     else
590     break;
591     } else {
592     // Search for vendor string
593     if (strstr(vendor, line) == vendor)
594     vendor_found = true;
595     }
596     }
597    
598     // Keycode file completely read
599     fclose(f);
600     use_keycodes = vendor_found;
601    
602     // Vendor not found? Then display warning
603     if (!vendor_found) {
604     char str[256];
605     sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
606     WarningAlert(str);
607     return;
608     }
609     }
610     }
611    
612 cebix 1.1 static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, long apple_mode, long apple_id, int type)
613     {
614     if (allow & test) {
615     p->viType = type;
616     switch (apple_id) {
617     case APPLE_W_640x480:
618     case APPLE_640x480:
619     p->viXsize = 640;
620     p->viYsize = 480;
621     break;
622     case APPLE_W_800x600:
623     case APPLE_800x600:
624     p->viXsize = 800;
625     p->viYsize = 600;
626     break;
627     case APPLE_1024x768:
628     p->viXsize = 1024;
629     p->viYsize = 768;
630     break;
631     case APPLE_1152x900:
632     p->viXsize = 1152;
633     p->viYsize = 900;
634     break;
635     case APPLE_1280x1024:
636     p->viXsize = 1280;
637     p->viYsize = 1024;
638     break;
639     case APPLE_1600x1200:
640     p->viXsize = 1600;
641     p->viYsize = 1200;
642     break;
643     }
644     switch (apple_mode) {
645     case APPLE_8_BIT:
646     p->viRowBytes = p->viXsize;
647     break;
648     case APPLE_16_BIT:
649     p->viRowBytes = p->viXsize * 2;
650     break;
651     case APPLE_32_BIT:
652     p->viRowBytes = p->viXsize * 4;
653     break;
654     }
655     p->viAppleMode = apple_mode;
656     p->viAppleID = apple_id;
657     p++;
658     }
659     }
660    
661     static bool has_mode(int x, int y)
662     {
663     #ifdef ENABLE_XF86_VIDMODE
664     for (int i=0; i<num_x_video_modes; i++)
665     if (x_video_modes[i]->hdisplay >= x && x_video_modes[i]->vdisplay >= y)
666     return true;
667     return false;
668     #else
669     return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y;
670     #endif
671     }
672    
673     bool VideoInit(void)
674     {
675 gbeauche 1.3 #ifdef ENABLE_VOSF
676     // Zero the mainBuffer structure
677     mainBuffer.dirtyPages = NULL;
678     mainBuffer.pageInfo = NULL;
679     #endif
680    
681 gbeauche 1.4 // Check if X server runs on local machine
682     local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
683     || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
684    
685 gbeauche 1.6 // Init keycode translation
686     keycode_init();
687    
688 cebix 1.1 // Init variables
689     private_data = NULL;
690     cur_mode = 0; // Window 640x480
691     video_activated = true;
692    
693     // Find screen and root window
694     screen = XDefaultScreen(x_display);
695     rootwin = XRootWindow(x_display, screen);
696    
697     // Get screen depth
698     xdepth = DefaultDepth(x_display, screen);
699    
700     #ifdef ENABLE_XF86_DGA
701     // DGA available?
702     int event_base, error_base;
703 gbeauche 1.4 if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) {
704 cebix 1.1 int dga_flags = 0;
705     XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
706     has_dga = dga_flags & XF86DGADirectPresent;
707     } else
708     has_dga = false;
709     #endif
710    
711     #ifdef ENABLE_XF86_VIDMODE
712     // VidMode available?
713     int vm_event_base, vm_error_base;
714     has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
715     if (has_vidmode)
716     XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
717     #endif
718    
719     // Find black and white colors
720     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
721     XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
722     XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
723     XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
724     black_pixel = BlackPixel(x_display, screen);
725     white_pixel = WhitePixel(x_display, screen);
726    
727     // Get appropriate visual
728     int color_class;
729     switch (xdepth) {
730     #if 0
731     case 1:
732     color_class = StaticGray;
733     break;
734     #endif
735     case 8:
736     color_class = PseudoColor;
737     break;
738     case 15:
739     case 16:
740     case 24:
741     case 32:
742     color_class = TrueColor;
743     break;
744     default:
745     ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
746     return false;
747     }
748     if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
749     ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
750     return false;
751     }
752     if (visualInfo.depth != xdepth) {
753     ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
754     return false;
755     }
756     vis = visualInfo.visual;
757    
758     // Mac screen depth follows X depth (for now)
759     depth = xdepth;
760    
761     // Create color maps for 8 bit mode
762     if (depth == 8) {
763     cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
764     cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
765     XInstallColormap(x_display, cmap[0]);
766     XInstallColormap(x_display, cmap[1]);
767     }
768    
769     // Construct video mode table
770     int mode = APPLE_8_BIT;
771     int bpr_mult = 8;
772     switch (depth) {
773     case 1:
774     mode = APPLE_1_BIT;
775     bpr_mult = 1;
776     break;
777     case 8:
778     mode = APPLE_8_BIT;
779     bpr_mult = 8;
780     break;
781     case 15:
782     case 16:
783     mode = APPLE_16_BIT;
784     bpr_mult = 16;
785     break;
786     case 24:
787     case 32:
788     mode = APPLE_32_BIT;
789     bpr_mult = 32;
790     break;
791     }
792    
793     uint32 window_modes = PrefsFindInt32("windowmodes");
794     uint32 screen_modes = PrefsFindInt32("screenmodes");
795     if (!has_dga)
796     screen_modes = 0;
797     if (window_modes == 0 && screen_modes == 0)
798     window_modes |= 3; // Allow at least 640x480 and 800x600 window modes
799    
800     VideoInfo *p = VModes;
801     add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW);
802     add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW);
803     if (has_vidmode) {
804     if (has_mode(640, 480))
805     add_mode(p, screen_modes, 1, mode, APPLE_640x480, DIS_SCREEN);
806     if (has_mode(800, 600))
807     add_mode(p, screen_modes, 2, mode, APPLE_800x600, DIS_SCREEN);
808     if (has_mode(1024, 768))
809     add_mode(p, screen_modes, 4, mode, APPLE_1024x768, DIS_SCREEN);
810     if (has_mode(1152, 900))
811     add_mode(p, screen_modes, 8, mode, APPLE_1152x900, DIS_SCREEN);
812     if (has_mode(1280, 1024))
813     add_mode(p, screen_modes, 16, mode, APPLE_1280x1024, DIS_SCREEN);
814     if (has_mode(1600, 1200))
815     add_mode(p, screen_modes, 32, mode, APPLE_1600x1200, DIS_SCREEN);
816     } else if (screen_modes) {
817     int xsize = DisplayWidth(x_display, screen);
818     int ysize = DisplayHeight(x_display, screen);
819     int apple_id;
820     if (xsize < 800)
821     apple_id = APPLE_640x480;
822     else if (xsize < 1024)
823     apple_id = APPLE_800x600;
824     else if (xsize < 1152)
825     apple_id = APPLE_1024x768;
826     else if (xsize < 1280)
827     apple_id = APPLE_1152x900;
828     else if (xsize < 1600)
829     apple_id = APPLE_1280x1024;
830     else
831     apple_id = APPLE_1600x1200;
832     p->viType = DIS_SCREEN;
833     p->viRowBytes = 0;
834     p->viXsize = xsize;
835     p->viYsize = ysize;
836     p->viAppleMode = mode;
837     p->viAppleID = apple_id;
838     p++;
839     }
840     p->viType = DIS_INVALID; // End marker
841     p->viRowBytes = 0;
842     p->viXsize = p->viYsize = 0;
843     p->viAppleMode = 0;
844     p->viAppleID = 0;
845    
846     #ifdef ENABLE_XF86_DGA
847     if (has_dga && screen_modes) {
848     int v_bank, v_size;
849     XF86DGAGetVideo(x_display, screen, &dga_screen_base, &dga_fb_width, &v_bank, &v_size);
850     D(bug("DGA screen_base %p, v_width %d\n", dga_screen_base, dga_fb_width));
851     }
852     #endif
853    
854     // Open window/screen
855     if (!open_display())
856     return false;
857    
858     #if 0
859     // Ignore errors from now on
860     XSetErrorHandler(ignore_errors);
861     #endif
862    
863     // Start periodic thread
864     XSync(x_display, false);
865     redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
866     D(bug("Redraw thread installed (%ld)\n", redraw_thread));
867     return true;
868     }
869    
870    
871     /*
872     * Deinitialization
873     */
874    
875     void VideoExit(void)
876     {
877     // Stop redraw thread
878     if (redraw_thread_active) {
879     pthread_cancel(redraw_thread);
880     pthread_join(redraw_thread, NULL);
881     redraw_thread_active = false;
882     }
883    
884 gbeauche 1.3 #ifdef ENABLE_VOSF
885     if (use_vosf) {
886     // Deinitialize VOSF
887     video_vosf_exit();
888     }
889     #endif
890    
891 cebix 1.1 // Close window and server connection
892     if (x_display != NULL) {
893     XSync(x_display, false);
894     close_display();
895     XFlush(x_display);
896     XSync(x_display, false);
897     if (depth == 8) {
898     XFreeColormap(x_display, cmap[0]);
899     XFreeColormap(x_display, cmap[1]);
900     }
901     }
902     }
903    
904    
905     /*
906     * Suspend/resume emulator
907     */
908    
909     extern void PauseEmulator(void);
910     extern void ResumeEmulator(void);
911    
912     static void suspend_emul(void)
913     {
914     if (display_type == DIS_SCREEN) {
915     // Release ctrl key
916     ADBKeyUp(0x36);
917     ctrl_down = false;
918    
919     // Pause MacOS thread
920     PauseEmulator();
921     emul_suspended = true;
922    
923     // Save frame buffer
924     fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
925     if (fb_save)
926     memcpy(fb_save, (void *)screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
927    
928     // Close full screen display
929     #ifdef ENABLE_XF86_DGA
930     XF86DGADirectVideo(x_display, screen, 0);
931     XUngrabPointer(x_display, CurrentTime);
932     XUngrabKeyboard(x_display, CurrentTime);
933     #endif
934     XSync(x_display, false);
935    
936     // Open "suspend" window
937     XSetWindowAttributes wattr;
938     wattr.event_mask = KeyPressMask;
939     wattr.background_pixel = black_pixel;
940     wattr.border_pixel = black_pixel;
941     wattr.backing_store = Always;
942     wattr.backing_planes = xdepth;
943     wattr.colormap = DefaultColormap(x_display, screen);
944     XSync(x_display, false);
945     suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
946     InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
947     CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
948     XSync(x_display, false);
949     XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
950     XMapRaised(x_display, suspend_win);
951     XSync(x_display, false);
952     }
953     }
954    
955     static void resume_emul(void)
956     {
957     // Close "suspend" window
958     XDestroyWindow(x_display, suspend_win);
959     XSync(x_display, false);
960    
961     // Reopen full screen display
962     XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
963     XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
964     XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
965     XF86DGASetViewPort(x_display, screen, 0, 0);
966     XSync(x_display, false);
967    
968 gbeauche 1.3 // the_buffer already contains the data to restore. i.e. since a temporary
969     // frame buffer is used when VOSF is actually used, fb_save is therefore
970     // not necessary.
971     #ifdef ENABLE_VOSF
972     if (use_vosf) {
973     LOCK_VOSF;
974     PFLAG_SET_ALL;
975     UNLOCK_VOSF;
976     memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
977     }
978     #endif
979    
980 cebix 1.1 // Restore frame buffer
981     if (fb_save) {
982 gbeauche 1.3 #ifdef ENABLE_VOSF
983     // Don't copy fb_save to the temporary frame buffer in VOSF mode
984     if (!use_vosf)
985     #endif
986 cebix 1.1 memcpy((void *)screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes);
987     free(fb_save);
988     fb_save = NULL;
989     }
990     if (depth == 8)
991     palette_changed = true;
992    
993     // Resume MacOS thread
994     emul_suspended = false;
995     ResumeEmulator();
996     }
997    
998    
999     /*
1000     * Close screen in full-screen mode
1001     */
1002    
1003     void VideoQuitFullScreen(void)
1004     {
1005     D(bug("VideoQuitFullScreen()\n"));
1006     if (display_type == DIS_SCREEN) {
1007     quit_full_screen = true;
1008     while (!quit_full_screen_ack) ;
1009     }
1010     }
1011    
1012    
1013     /*
1014     * X11 event handling
1015     */
1016    
1017     // Translate key event to Mac keycode
1018     static int kc_decode(KeySym ks)
1019     {
1020     switch (ks) {
1021     case XK_A: case XK_a: return 0x00;
1022     case XK_B: case XK_b: return 0x0b;
1023     case XK_C: case XK_c: return 0x08;
1024     case XK_D: case XK_d: return 0x02;
1025     case XK_E: case XK_e: return 0x0e;
1026     case XK_F: case XK_f: return 0x03;
1027     case XK_G: case XK_g: return 0x05;
1028     case XK_H: case XK_h: return 0x04;
1029     case XK_I: case XK_i: return 0x22;
1030     case XK_J: case XK_j: return 0x26;
1031     case XK_K: case XK_k: return 0x28;
1032     case XK_L: case XK_l: return 0x25;
1033     case XK_M: case XK_m: return 0x2e;
1034     case XK_N: case XK_n: return 0x2d;
1035     case XK_O: case XK_o: return 0x1f;
1036     case XK_P: case XK_p: return 0x23;
1037     case XK_Q: case XK_q: return 0x0c;
1038     case XK_R: case XK_r: return 0x0f;
1039     case XK_S: case XK_s: return 0x01;
1040     case XK_T: case XK_t: return 0x11;
1041     case XK_U: case XK_u: return 0x20;
1042     case XK_V: case XK_v: return 0x09;
1043     case XK_W: case XK_w: return 0x0d;
1044     case XK_X: case XK_x: return 0x07;
1045     case XK_Y: case XK_y: return 0x10;
1046     case XK_Z: case XK_z: return 0x06;
1047    
1048     case XK_1: case XK_exclam: return 0x12;
1049     case XK_2: case XK_at: return 0x13;
1050     case XK_3: case XK_numbersign: return 0x14;
1051     case XK_4: case XK_dollar: return 0x15;
1052     case XK_5: case XK_percent: return 0x17;
1053     case XK_6: return 0x16;
1054     case XK_7: return 0x1a;
1055     case XK_8: return 0x1c;
1056     case XK_9: return 0x19;
1057     case XK_0: return 0x1d;
1058    
1059     case XK_grave: case XK_asciitilde: return 0x0a;
1060     case XK_minus: case XK_underscore: return 0x1b;
1061     case XK_equal: case XK_plus: return 0x18;
1062     case XK_bracketleft: case XK_braceleft: return 0x21;
1063     case XK_bracketright: case XK_braceright: return 0x1e;
1064     case XK_backslash: case XK_bar: return 0x2a;
1065     case XK_semicolon: case XK_colon: return 0x29;
1066     case XK_apostrophe: case XK_quotedbl: return 0x27;
1067     case XK_comma: case XK_less: return 0x2b;
1068     case XK_period: case XK_greater: return 0x2f;
1069     case XK_slash: case XK_question: return 0x2c;
1070    
1071     case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1072     case XK_Return: return 0x24;
1073     case XK_space: return 0x31;
1074     case XK_BackSpace: return 0x33;
1075    
1076     case XK_Delete: return 0x75;
1077     case XK_Insert: return 0x72;
1078     case XK_Home: case XK_Help: return 0x73;
1079     case XK_End: return 0x77;
1080     #ifdef __hpux
1081     case XK_Prior: return 0x74;
1082     case XK_Next: return 0x79;
1083     #else
1084     case XK_Page_Up: return 0x74;
1085     case XK_Page_Down: return 0x79;
1086     #endif
1087    
1088     case XK_Control_L: return 0x36;
1089     case XK_Control_R: return 0x36;
1090     case XK_Shift_L: return 0x38;
1091     case XK_Shift_R: return 0x38;
1092     case XK_Alt_L: return 0x37;
1093     case XK_Alt_R: return 0x37;
1094     case XK_Meta_L: return 0x3a;
1095     case XK_Meta_R: return 0x3a;
1096     case XK_Menu: return 0x32;
1097     case XK_Caps_Lock: return 0x39;
1098     case XK_Num_Lock: return 0x47;
1099    
1100     case XK_Up: return 0x3e;
1101     case XK_Down: return 0x3d;
1102     case XK_Left: return 0x3b;
1103     case XK_Right: return 0x3c;
1104    
1105     case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1106    
1107     case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1108     case XK_F2: return 0x78;
1109     case XK_F3: return 0x63;
1110     case XK_F4: return 0x76;
1111     case XK_F5: return 0x60;
1112     case XK_F6: return 0x61;
1113     case XK_F7: return 0x62;
1114     case XK_F8: return 0x64;
1115     case XK_F9: return 0x65;
1116     case XK_F10: return 0x6d;
1117     case XK_F11: return 0x67;
1118     case XK_F12: return 0x6f;
1119    
1120     case XK_Print: return 0x69;
1121     case XK_Scroll_Lock: return 0x6b;
1122     case XK_Pause: return 0x71;
1123    
1124     #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1125     case XK_KP_0: case XK_KP_Insert: return 0x52;
1126     case XK_KP_1: case XK_KP_End: return 0x53;
1127     case XK_KP_2: case XK_KP_Down: return 0x54;
1128     case XK_KP_3: case XK_KP_Next: return 0x55;
1129     case XK_KP_4: case XK_KP_Left: return 0x56;
1130     case XK_KP_5: case XK_KP_Begin: return 0x57;
1131     case XK_KP_6: case XK_KP_Right: return 0x58;
1132     case XK_KP_7: case XK_KP_Home: return 0x59;
1133     case XK_KP_8: case XK_KP_Up: return 0x5b;
1134     case XK_KP_9: case XK_KP_Prior: return 0x5c;
1135     case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1136     #else
1137     case XK_KP_0: return 0x52;
1138     case XK_KP_1: return 0x53;
1139     case XK_KP_2: return 0x54;
1140     case XK_KP_3: return 0x55;
1141     case XK_KP_4: return 0x56;
1142     case XK_KP_5: return 0x57;
1143     case XK_KP_6: return 0x58;
1144     case XK_KP_7: return 0x59;
1145     case XK_KP_8: return 0x5b;
1146     case XK_KP_9: return 0x5c;
1147     case XK_KP_Decimal: return 0x41;
1148     #endif
1149     case XK_KP_Add: return 0x45;
1150     case XK_KP_Subtract: return 0x4e;
1151     case XK_KP_Multiply: return 0x43;
1152     case XK_KP_Divide: return 0x4b;
1153     case XK_KP_Enter: return 0x4c;
1154     case XK_KP_Equal: return 0x51;
1155     }
1156     return -1;
1157     }
1158    
1159 gbeauche 1.6 static int event2keycode(XKeyEvent &ev)
1160 cebix 1.1 {
1161     KeySym ks;
1162     int as;
1163     int i = 0;
1164    
1165     do {
1166 gbeauche 1.6 ks = XLookupKeysym(&ev, i++);
1167 cebix 1.1 as = kc_decode(ks);
1168     if (as != -1)
1169     return as;
1170     } while (ks != NoSymbol);
1171    
1172     return -1;
1173     }
1174    
1175     static void handle_events(void)
1176     {
1177     // Handle events
1178     for (;;) {
1179     XEvent event;
1180    
1181     if (!XCheckMaskEvent(x_display, eventmask, &event))
1182     break;
1183    
1184     switch (event.type) {
1185     // Mouse button
1186     case ButtonPress: {
1187     unsigned int button = ((XButtonEvent *)&event)->button;
1188     if (button < 4)
1189     ADBMouseDown(button - 1);
1190     break;
1191     }
1192     case ButtonRelease: {
1193     unsigned int button = ((XButtonEvent *)&event)->button;
1194     if (button < 4)
1195     ADBMouseUp(button - 1);
1196     break;
1197     }
1198    
1199     // Mouse moved
1200     case EnterNotify:
1201     ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1202     break;
1203     case MotionNotify:
1204     ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1205     break;
1206    
1207     // Keyboard
1208     case KeyPress: {
1209 gbeauche 1.6 int code = event2keycode(event.xkey);
1210     if (use_keycodes && code != -1)
1211     code = keycode_table[event.xkey.keycode & 0xff];
1212     if (code != -1) {
1213 cebix 1.1 if (!emul_suspended) {
1214     ADBKeyDown(code);
1215     if (code == 0x36)
1216     ctrl_down = true;
1217     } else {
1218     if (code == 0x31)
1219     resume_emul(); // Space wakes us up
1220     }
1221     }
1222     break;
1223     }
1224     case KeyRelease: {
1225 gbeauche 1.6 int code = event2keycode(event.xkey);
1226     if (use_keycodes && code != 1)
1227     code = keycode_table[event.xkey.keycode & 0xff];
1228     if (code != -1) {
1229 cebix 1.1 ADBKeyUp(code);
1230     if (code == 0x36)
1231     ctrl_down = false;
1232     }
1233     break;
1234     }
1235    
1236     // Hidden parts exposed, force complete refresh
1237     case Expose:
1238 gbeauche 1.3 #ifdef ENABLE_VOSF
1239     if (use_vosf) { // VOSF refresh
1240     LOCK_VOSF;
1241     PFLAG_SET_ALL;
1242     UNLOCK_VOSF;
1243     }
1244     #endif
1245 cebix 1.1 memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize);
1246     break;
1247     }
1248     }
1249     }
1250    
1251    
1252     /*
1253     * Execute video VBL routine
1254     */
1255    
1256     void VideoVBL(void)
1257     {
1258     if (emerg_quit)
1259     QuitEmulator();
1260    
1261     // Execute video VBL
1262     if (private_data != NULL && private_data->interruptsEnabled)
1263     VSLDoInterruptService(private_data->vslServiceID);
1264     }
1265    
1266    
1267     /*
1268     * Install graphics acceleration
1269     */
1270    
1271     #if 0
1272     // Rectangle blitting
1273     static void accl_bitblt(accl_params *p)
1274     {
1275     D(bug("accl_bitblt\n"));
1276    
1277     // Get blitting parameters
1278     int16 src_X = p->src_rect[1] - p->src_bounds[1];
1279     int16 src_Y = p->src_rect[0] - p->src_bounds[0];
1280     int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1281     int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1282     int16 width = p->dest_rect[3] - p->dest_rect[1] - 1;
1283     int16 height = p->dest_rect[2] - p->dest_rect[0] - 1;
1284     D(bug(" src X %d, src Y %d, dest X %d, dest Y %d\n", src_X, src_Y, dest_X, dest_Y));
1285     D(bug(" width %d, height %d\n", width, height));
1286    
1287     // And perform the blit
1288     bitblt_hook(src_X, src_Y, dest_X, dest_Y, width, height);
1289     }
1290    
1291     static bool accl_bitblt_hook(accl_params *p)
1292     {
1293     D(bug("accl_draw_hook %p\n", p));
1294    
1295     // Check if we can accelerate this bitblt
1296     if (p->src_base_addr == screen_base && p->dest_base_addr == screen_base &&
1297     display_type == DIS_SCREEN && bitblt_hook != NULL &&
1298     ((uint32 *)p)[0x18 >> 2] + ((uint32 *)p)[0x128 >> 2] == 0 &&
1299     ((uint32 *)p)[0x130 >> 2] == 0 &&
1300     p->transfer_mode == 0 &&
1301     p->src_row_bytes > 0 && ((uint32 *)p)[0x15c >> 2] > 0) {
1302    
1303     // Yes, set function pointer
1304     p->draw_proc = accl_bitblt;
1305     return true;
1306     }
1307     return false;
1308     }
1309    
1310     // Rectangle filling/inversion
1311     static void accl_fillrect8(accl_params *p)
1312     {
1313     D(bug("accl_fillrect8\n"));
1314    
1315     // Get filling parameters
1316     int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1317     int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1318     int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;
1319     int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;
1320     uint8 color = p->pen_mode == 8 ? p->fore_pen : p->back_pen;
1321     D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1322     D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));
1323    
1324     // And perform the fill
1325     fillrect8_hook(dest_X, dest_Y, dest_X_max, dest_Y_max, color);
1326     }
1327    
1328     static void accl_fillrect32(accl_params *p)
1329     {
1330     D(bug("accl_fillrect32\n"));
1331    
1332     // Get filling parameters
1333     int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1334     int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1335     int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;
1336     int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;
1337     uint32 color = p->pen_mode == 8 ? p->fore_pen : p->back_pen;
1338     D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1339     D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));
1340    
1341     // And perform the fill
1342     fillrect32_hook(dest_X, dest_Y, dest_X_max, dest_Y_max, color);
1343     }
1344    
1345     static void accl_invrect(accl_params *p)
1346     {
1347     D(bug("accl_invrect\n"));
1348    
1349     // Get inversion parameters
1350     int16 dest_X = p->dest_rect[1] - p->dest_bounds[1];
1351     int16 dest_Y = p->dest_rect[0] - p->dest_bounds[0];
1352     int16 dest_X_max = p->dest_rect[3] - p->dest_bounds[1] - 1;
1353     int16 dest_Y_max = p->dest_rect[2] - p->dest_bounds[0] - 1;
1354     D(bug(" dest X %d, dest Y %d\n", dest_X, dest_Y));
1355     D(bug(" dest X max %d, dest Y max %d\n", dest_X_max, dest_Y_max));
1356    
1357     //!!?? pen_mode == 14
1358    
1359     // And perform the inversion
1360     invrect_hook(dest_X, dest_Y, dest_X_max, dest_Y_max);
1361     }
1362    
1363     static bool accl_fillrect_hook(accl_params *p)
1364     {
1365     D(bug("accl_fillrect_hook %p\n", p));
1366    
1367     // Check if we can accelerate this fillrect
1368     if (p->dest_base_addr == screen_base && ((uint32 *)p)[0x284 >> 2] != 0 && display_type == DIS_SCREEN) {
1369     if (p->transfer_mode == 8) {
1370     // Fill
1371     if (p->dest_pixel_size == 8 && fillrect8_hook != NULL) {
1372     p->draw_proc = accl_fillrect8;
1373     return true;
1374     } else if (p->dest_pixel_size == 32 && fillrect32_hook != NULL) {
1375     p->draw_proc = accl_fillrect32;
1376     return true;
1377     }
1378     } else if (p->transfer_mode == 10 && invrect_hook != NULL) {
1379     // Invert
1380     p->draw_proc = accl_invrect;
1381     return true;
1382     }
1383     }
1384     return false;
1385     }
1386    
1387     // Wait for graphics operation to finish
1388     static bool accl_sync_hook(void *arg)
1389     {
1390     D(bug("accl_sync_hook %p\n", arg));
1391     if (sync_hook != NULL)
1392     sync_hook();
1393     return true;
1394     }
1395    
1396     static struct accl_hook_info bitblt_hook_info = {accl_bitblt_hook, accl_sync_hook, ACCL_BITBLT};
1397     static struct accl_hook_info fillrect_hook_info = {accl_fillrect_hook, accl_sync_hook, ACCL_FILLRECT};
1398     #endif
1399    
1400     void VideoInstallAccel(void)
1401     {
1402     // Install acceleration hooks
1403     if (PrefsFindBool("gfxaccel")) {
1404     D(bug("Video: Installing acceleration hooks\n"));
1405     //!! NQDMisc(6, &bitblt_hook_info);
1406     // NQDMisc(6, &fillrect_hook_info);
1407     }
1408     }
1409    
1410    
1411     /*
1412     * Change video mode
1413     */
1414    
1415     int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr)
1416     {
1417     /* return if no mode change */
1418     if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) &&
1419     (csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr;
1420    
1421     /* first find video mode in table */
1422     for (int i=0; VModes[i].viType != DIS_INVALID; i++) {
1423     if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) &&
1424     (ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) {
1425     csSave->saveMode = ReadMacInt16(ParamPtr + csMode);
1426     csSave->saveData = ReadMacInt32(ParamPtr + csData);
1427     csSave->savePage = ReadMacInt16(ParamPtr + csPage);
1428    
1429     // Disable interrupts and pause redraw thread
1430     DisableInterrupt();
1431     thread_stop_ack = false;
1432     thread_stop_req = true;
1433     while (!thread_stop_ack) ;
1434    
1435     /* close old display */
1436     close_display();
1437    
1438     /* open new display */
1439     cur_mode = i;
1440     bool ok = open_display();
1441    
1442     /* opening the screen failed? Then bail out */
1443     if (!ok) {
1444     ErrorAlert(GetString(STR_FULL_SCREEN_ERR));
1445     QuitEmulator();
1446     }
1447    
1448     WriteMacInt32(ParamPtr + csBaseAddr, screen_base);
1449     csSave->saveBaseAddr=screen_base;
1450     csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */
1451     csSave->saveMode=VModes[cur_mode].viAppleMode;
1452    
1453     // Enable interrupts and resume redraw thread
1454     thread_stop_req = false;
1455     EnableInterrupt();
1456     return noErr;
1457     }
1458     }
1459     return paramErr;
1460     }
1461    
1462    
1463     /*
1464     * Set color palette
1465     */
1466    
1467     void video_set_palette(void)
1468     {
1469     palette_changed = true;
1470     }
1471    
1472    
1473     /*
1474     * Set cursor image for window
1475     */
1476    
1477     void video_set_cursor(void)
1478     {
1479     cursor_changed = true;
1480     }
1481    
1482    
1483     /*
1484     * Thread for window refresh, event handling and other periodic actions
1485     */
1486    
1487     static void update_display(void)
1488     {
1489     // Incremental update code
1490     int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1491     int bytes_per_row = VModes[cur_mode].viRowBytes;
1492     int bytes_per_pixel = VModes[cur_mode].viRowBytes / VModes[cur_mode].viXsize;
1493     uint8 *p, *p2;
1494    
1495     // Check for first line from top and first line from bottom that have changed
1496     y1 = 0;
1497     for (j=0; j<VModes[cur_mode].viYsize; j++) {
1498     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1499     y1 = j;
1500     break;
1501     }
1502     }
1503     y2 = y1 - 1;
1504     for (j=VModes[cur_mode].viYsize-1; j>=y1; j--) {
1505     if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1506     y2 = j;
1507     break;
1508     }
1509     }
1510     high = y2 - y1 + 1;
1511    
1512     // Check for first column from left and first column from right that have changed
1513     if (high) {
1514     if (depth == 1) {
1515     x1 = VModes[cur_mode].viXsize;
1516     for (j=y1; j<=y2; j++) {
1517     p = &the_buffer[j * bytes_per_row];
1518     p2 = &the_buffer_copy[j * bytes_per_row];
1519     for (i=0; i<(x1>>3); i++) {
1520     if (*p != *p2) {
1521     x1 = i << 3;
1522     break;
1523     }
1524     p++;
1525     p2++;
1526     }
1527     }
1528     x2 = x1;
1529     for (j=y1; j<=y2; j++) {
1530     p = &the_buffer[j * bytes_per_row];
1531     p2 = &the_buffer_copy[j * bytes_per_row];
1532     p += bytes_per_row;
1533     p2 += bytes_per_row;
1534     for (i=(VModes[cur_mode].viXsize>>3); i>(x2>>3); i--) {
1535     p--;
1536     p2--;
1537     if (*p != *p2) {
1538     x2 = i << 3;
1539     break;
1540     }
1541     }
1542     }
1543     wide = x2 - x1;
1544    
1545     // Update copy of the_buffer
1546     if (high && wide) {
1547     for (j=y1; j<=y2; j++) {
1548     i = j * bytes_per_row + (x1 >> 3);
1549     memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3);
1550     }
1551     }
1552    
1553     } else {
1554     x1 = VModes[cur_mode].viXsize;
1555     for (j=y1; j<=y2; j++) {
1556     p = &the_buffer[j * bytes_per_row];
1557     p2 = &the_buffer_copy[j * bytes_per_row];
1558     for (i=0; i<x1; i++) {
1559     if (memcmp(p, p2, bytes_per_pixel)) {
1560     x1 = i;
1561     break;
1562     }
1563     p += bytes_per_pixel;
1564     p2 += bytes_per_pixel;
1565     }
1566     }
1567     x2 = x1;
1568     for (j=y1; j<=y2; j++) {
1569     p = &the_buffer[j * bytes_per_row];
1570     p2 = &the_buffer_copy[j * bytes_per_row];
1571     p += bytes_per_row;
1572     p2 += bytes_per_row;
1573     for (i=VModes[cur_mode].viXsize; i>x2; i--) {
1574     p -= bytes_per_pixel;
1575     p2 -= bytes_per_pixel;
1576     if (memcmp(p, p2, bytes_per_pixel)) {
1577     x2 = i;
1578     break;
1579     }
1580     }
1581     }
1582     wide = x2 - x1;
1583    
1584     // Update copy of the_buffer
1585     if (high && wide) {
1586     for (j=y1; j<=y2; j++) {
1587     i = j * bytes_per_row + x1 * bytes_per_pixel;
1588     memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide);
1589     }
1590     }
1591     }
1592     }
1593    
1594     // Refresh display
1595     if (high && wide) {
1596     if (have_shm)
1597     XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1598     else
1599     XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1600     }
1601     }
1602    
1603     static void *redraw_func(void *arg)
1604     {
1605     int tick_counter = 0;
1606     struct timespec req = {0, 16666667};
1607    
1608     for (;;) {
1609    
1610     // Wait
1611     nanosleep(&req, NULL);
1612    
1613     // Pause if requested (during video mode switches)
1614     while (thread_stop_req)
1615     thread_stop_ack = true;
1616    
1617     // Handle X11 events
1618     handle_events();
1619    
1620     // Quit DGA mode if requested
1621     if (quit_full_screen) {
1622     quit_full_screen = false;
1623     if (display_type == DIS_SCREEN) {
1624     #ifdef ENABLE_XF86_DGA
1625     XF86DGADirectVideo(x_display, screen, 0);
1626     XUngrabPointer(x_display, CurrentTime);
1627     XUngrabKeyboard(x_display, CurrentTime);
1628     #endif
1629     XSync(x_display, false);
1630     quit_full_screen_ack = true;
1631     return NULL;
1632     }
1633     }
1634    
1635     // Refresh display and set cursor image in window mode
1636     if (display_type == DIS_WINDOW) {
1637     tick_counter++;
1638     if (tick_counter >= frame_skip) {
1639     tick_counter = 0;
1640    
1641     // Update display
1642 gbeauche 1.3 #ifdef ENABLE_VOSF
1643     if (use_vosf) {
1644     if (mainBuffer.dirty) {
1645     LOCK_VOSF;
1646     update_display_window_vosf();
1647     UNLOCK_VOSF;
1648     XSync(x_display, false); // Let the server catch up
1649     }
1650     }
1651     else
1652     #endif
1653     update_display();
1654 cebix 1.1
1655     // Set new cursor image if it was changed
1656     if (cursor_changed) {
1657     cursor_changed = false;
1658     memcpy(cursor_image->data, MacCursor + 4, 32);
1659     memcpy(cursor_mask_image->data, MacCursor + 36, 32);
1660     XFreeCursor(x_display, mac_cursor);
1661     XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16);
1662     XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16);
1663     mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]);
1664     XDefineCursor(x_display, the_win, mac_cursor);
1665     }
1666     }
1667     }
1668 gbeauche 1.3 #ifdef ENABLE_VOSF
1669     else if (use_vosf) {
1670     // Update display (VOSF variant)
1671     static int tick_counter = 0;
1672     if (++tick_counter >= frame_skip) {
1673     tick_counter = 0;
1674     if (mainBuffer.dirty) {
1675     LOCK_VOSF;
1676     update_display_dga_vosf();
1677     UNLOCK_VOSF;
1678     }
1679     }
1680     }
1681     #endif
1682 cebix 1.1
1683     // Set new palette if it was changed
1684     if (palette_changed && !emul_suspended) {
1685     palette_changed = false;
1686     XColor c[256];
1687     for (int i=0; i<256; i++) {
1688     c[i].pixel = i;
1689     c[i].red = mac_pal[i].red * 0x0101;
1690     c[i].green = mac_pal[i].green * 0x0101;
1691     c[i].blue = mac_pal[i].blue * 0x0101;
1692     c[i].flags = DoRed | DoGreen | DoBlue;
1693     }
1694     if (depth == 8) {
1695     XStoreColors(x_display, cmap[0], c, 256);
1696     XStoreColors(x_display, cmap[1], c, 256);
1697     #ifdef ENABLE_XF86_DGA
1698     if (display_type == DIS_SCREEN) {
1699     current_dga_cmap ^= 1;
1700     XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1701     }
1702     #endif
1703     }
1704     }
1705     }
1706     return NULL;
1707     }