ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/video_x.cpp
Revision: 1.7
Committed: 2003-12-04T22:29:15Z (20 years, 11 months ago) by gbeauche
Branch: MAIN
Changes since 1.6: +1 -4 lines
Log Message:
We do need <pthread.h> in any case, especially native Linux/PPC

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