ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.27
Committed: 2000-11-02T14:45:16Z (24 years ago) by cebix
Branch: MAIN
Changes since 1.26: +1 -1 lines
Log Message:
- added audio support for IRIX [Brian J. Johnson]
- improved Delay_usec() under FreeBSD and IRIX
- fixed typo ("HAVE_PTHREDS") in video_x.cpp

File Contents

# Content
1 /*
2 * video_x.cpp - Video/graphics emulation, X11 specific stuff
3 *
4 * Basilisk II (C) 1997-2000 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 /*
22 * NOTES:
23 * The Ctrl key works like a qualifier for special actions:
24 * Ctrl-Tab = suspend DGA mode
25 * Ctrl-Esc = emergency quit
26 * Ctrl-F1 = mount floppy
27 */
28
29 #include "sysdeps.h"
30
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33 #include <X11/keysym.h>
34 #include <X11/extensions/XShm.h>
35 #include <sys/ipc.h>
36 #include <sys/shm.h>
37 #include <errno.h>
38
39 #ifdef HAVE_PTHREADS
40 # include <pthread.h>
41 #endif
42
43 #ifdef ENABLE_XF86_DGA
44 # include <X11/extensions/xf86dga.h>
45 #endif
46
47 #ifdef ENABLE_XF86_VIDMODE
48 # include <X11/extensions/xf86vmode.h>
49 #endif
50
51 #ifdef ENABLE_FBDEV_DGA
52 # include <sys/mman.h>
53 #endif
54
55 #ifdef ENABLE_VOSF
56 # include <unistd.h>
57 # include <signal.h>
58 # include <fcntl.h>
59 # include <sys/mman.h>
60 #endif
61
62 #include "cpu_emulation.h"
63 #include "main.h"
64 #include "adb.h"
65 #include "macos_util.h"
66 #include "prefs.h"
67 #include "user_strings.h"
68 #include "video.h"
69
70 #define DEBUG 0
71 #include "debug.h"
72
73
74 // Display types
75 enum {
76 DISPLAY_WINDOW, // X11 window, using MIT SHM extensions if possible
77 DISPLAY_DGA // DGA fullscreen display
78 };
79
80 // Constants
81 const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes";
82 const char FBDEVICES_FILE_NAME[] = DATADIR "/fbdevices";
83
84
85 // Global variables
86 static int32 frame_skip; // Prefs items
87 static int16 mouse_wheel_mode = 1;
88 static int16 mouse_wheel_lines = 3;
89
90 static int display_type = DISPLAY_WINDOW; // See enum above
91 static bool local_X11; // Flag: X server running on local machine?
92 static uint8 *the_buffer; // Mac frame buffer
93
94 #ifdef HAVE_PTHREADS
95 static bool redraw_thread_active = false; // Flag: Redraw thread installed
96 static volatile bool redraw_thread_cancel = false; // Flag: Cancel Redraw thread
97 static pthread_t redraw_thread; // Redraw thread
98 #endif
99
100 static bool has_dga = false; // Flag: Video DGA capable
101 static bool has_vidmode = false; // Flag: VidMode extension available
102
103 static bool ctrl_down = false; // Flag: Ctrl key pressed
104 static bool caps_on = false; // Flag: Caps Lock on
105 static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread
106 static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread
107 static bool emul_suspended = false; // Flag: Emulator suspended
108
109 static bool classic_mode = false; // Flag: Classic Mac video mode
110
111 static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms
112 static int keycode_table[256]; // X keycode -> Mac keycode translation table
113
114 // X11 variables
115 static int screen; // Screen number
116 static int xdepth; // Depth of X screen
117 static int depth; // Depth of Mac frame buffer
118 static Window rootwin, the_win; // Root window and our window
119 static XVisualInfo visualInfo;
120 static Visual *vis;
121 static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode
122 static XColor black, white;
123 static unsigned long black_pixel, white_pixel;
124 static int eventmask;
125 static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | FocusChangeMask | ExposureMask;
126 static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
127
128 static XColor palette[256]; // Color palette for 8-bit mode
129 static bool palette_changed = false; // Flag: Palette changed, redraw thread must set new colors
130 #ifdef HAVE_PTHREADS
131 static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect palette
132 #endif
133
134 // Variables for window mode
135 static GC the_gc;
136 static XImage *img = NULL;
137 static XShmSegmentInfo shminfo;
138 static Cursor mac_cursor;
139 static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer
140 static bool have_shm = false; // Flag: SHM extensions available
141 static bool updt_box[17][17]; // Flag for Update
142 static int nr_boxes;
143 static const int sm_uptd[] = {4,1,6,3,0,5,2,7};
144 static int sm_no_boxes[] = {1,8,32,64,128,300};
145
146 // Variables for XF86 DGA mode
147 static int current_dga_cmap; // Number (0 or 1) of currently installed DGA colormap
148 static Window suspend_win; // "Suspend" window
149 static void *fb_save = NULL; // Saved frame buffer for suspend
150 #ifdef HAVE_PTHREADS
151 static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer
152 #endif
153
154 // Variables for fbdev DGA mode
155 const char FBDEVICE_FILE_NAME[] = "/dev/fb";
156 static int fbdev_fd;
157
158 #ifdef ENABLE_XF86_VIDMODE
159 // Variables for XF86 VidMode support
160 static XF86VidModeModeInfo **x_video_modes; // Array of all available modes
161 static int num_x_video_modes;
162 #endif
163
164 #ifdef ENABLE_VOSF
165 static bool use_vosf = true; // Flag: VOSF enabled
166 #else
167 static const bool use_vosf = false; // Flag: VOSF enabled
168 #endif
169
170 #ifdef ENABLE_VOSF
171 static uint8 * the_host_buffer; // Host frame buffer in VOSF mode
172 static uint32 the_buffer_size; // Size of allocated the_buffer
173 #endif
174
175 #ifdef ENABLE_VOSF
176 // Variables for Video on SEGV support (taken from the Win32 port)
177 struct ScreenPageInfo {
178 int top, bottom; // Mapping between this virtual page and Mac scanlines
179 };
180
181 struct ScreenInfo {
182 uint32 memBase; // Real start address
183 uint32 memStart; // Start address aligned to page boundary
184 uint32 memEnd; // Address of one-past-the-end of the screen
185 uint32 memLength; // Length of the memory addressed by the screen pages
186
187 uint32 pageSize; // Size of a page
188 int pageBits; // Shift count to get the page number
189 uint32 pageCount; // Number of pages allocated to the screen
190
191 uint8 * dirtyPages; // Table of flags set if page was altered
192 ScreenPageInfo * pageInfo; // Table of mappings page -> Mac scanlines
193 };
194
195 static ScreenInfo mainBuffer;
196
197 #define PFLAG_SET(page) mainBuffer.dirtyPages[page] = 1
198 #define PFLAG_CLEAR(page) mainBuffer.dirtyPages[page] = 0
199 #define PFLAG_ISSET(page) mainBuffer.dirtyPages[page]
200 #define PFLAG_ISCLEAR(page) (mainBuffer.dirtyPages[page] == 0)
201 #ifdef UNALIGNED_PROFITABLE
202 # define PFLAG_ISCLEAR_4(page) (*((uint32 *)(mainBuffer.dirtyPages + page)) == 0)
203 #else
204 # define PFLAG_ISCLEAR_4(page) \
205 (mainBuffer.dirtyPages[page ] == 0) \
206 && (mainBuffer.dirtyPages[page+1] == 0) \
207 && (mainBuffer.dirtyPages[page+2] == 0) \
208 && (mainBuffer.dirtyPages[page+3] == 0)
209 #endif
210 #define PFLAG_CLEAR_ALL memset(mainBuffer.dirtyPages, 0, mainBuffer.pageCount)
211 #define PFLAG_SET_ALL memset(mainBuffer.dirtyPages, 1, mainBuffer.pageCount)
212
213 static int zero_fd = -1;
214 static bool Screen_fault_handler_init();
215 static struct sigaction vosf_sa;
216
217 #ifdef HAVE_PTHREADS
218 static pthread_mutex_t Screen_draw_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer (dirtyPages in fact)
219 #endif
220
221 static int log_base_2(uint32 x)
222 {
223 uint32 mask = 0x80000000;
224 int l = 31;
225 while (l >= 0 && (x & mask) == 0) {
226 mask >>= 1;
227 l--;
228 }
229 return l;
230 }
231
232 #endif
233
234 // VideoRefresh function
235 void VideoRefreshInit(void);
236 static void (*video_refresh)(void);
237
238 // Prototypes
239 static void *redraw_func(void *arg);
240 static int event2keycode(XKeyEvent *ev);
241
242
243 // From main_unix.cpp
244 extern char *x_display_name;
245 extern Display *x_display;
246
247 // From sys_unix.cpp
248 extern void SysMountFirstFloppy(void);
249
250 #ifdef ENABLE_VOSF
251 # include "video_vosf.h"
252 #endif
253
254
255 /*
256 * Initialization
257 */
258
259 // Set VideoMonitor according to video mode
260 void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order)
261 {
262 #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
263 int layout = FLAYOUT_DIRECT;
264 switch (depth) {
265 case 1:
266 layout = FLAYOUT_DIRECT;
267 break;
268 case 8:
269 layout = FLAYOUT_DIRECT;
270 break;
271 case 15:
272 layout = FLAYOUT_HOST_555;
273 break;
274 case 16:
275 layout = FLAYOUT_HOST_565;
276 break;
277 case 24:
278 case 32:
279 layout = FLAYOUT_HOST_888;
280 break;
281 }
282 if (native_byte_order)
283 MacFrameLayout = layout;
284 else
285 MacFrameLayout = FLAYOUT_DIRECT;
286 #endif
287 switch (depth) {
288 case 1:
289 VideoMonitor.mode = VMODE_1BIT;
290 break;
291 case 8:
292 VideoMonitor.mode = VMODE_8BIT;
293 break;
294 case 15:
295 VideoMonitor.mode = VMODE_16BIT;
296 break;
297 case 16:
298 VideoMonitor.mode = VMODE_16BIT;
299 break;
300 case 24:
301 case 32:
302 VideoMonitor.mode = VMODE_32BIT;
303 break;
304 }
305 VideoMonitor.x = width;
306 VideoMonitor.y = height;
307 VideoMonitor.bytes_per_row = bytes_per_row;
308 }
309
310 // Trap SHM errors
311 static bool shm_error = false;
312 static int (*old_error_handler)(Display *, XErrorEvent *);
313
314 static int error_handler(Display *d, XErrorEvent *e)
315 {
316 if (e->error_code == BadAccess) {
317 shm_error = true;
318 return 0;
319 } else
320 return old_error_handler(d, e);
321 }
322
323 // Init window mode
324 static bool init_window(int width, int height)
325 {
326 int aligned_width = (width + 15) & ~15;
327 int aligned_height = (height + 15) & ~15;
328
329 // Set absolute mouse mode
330 ADBSetRelMouseMode(false);
331
332 // Read frame skip prefs
333 frame_skip = PrefsFindInt32("frameskip");
334
335 // Create window
336 XSetWindowAttributes wattr;
337 wattr.event_mask = eventmask = win_eventmask;
338 wattr.background_pixel = black_pixel;
339 wattr.border_pixel = black_pixel;
340 wattr.backing_store = NotUseful;
341 wattr.save_under = false;
342 wattr.backing_planes = xdepth;
343
344 XSync(x_display, false);
345 the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
346 InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
347 CWBackingStore | CWBackingPlanes, &wattr);
348
349 // Indicate that we want keyboard input
350 {
351 XWMHints *hints = XAllocWMHints();
352 if (hints) {
353 hints->input = True;
354 hints->flags = InputHint;
355 XSetWMHints(x_display, the_win, hints);
356 XFree((char *)hints);
357 }
358 }
359
360 // Make window unresizable
361 {
362 XSizeHints *hints = XAllocSizeHints();
363 if (hints) {
364 hints->min_width = width;
365 hints->max_width = width;
366 hints->min_height = height;
367 hints->max_height = height;
368 hints->flags = PMinSize | PMaxSize;
369 XSetWMNormalHints(x_display, the_win, hints);
370 XFree((char *)hints);
371 }
372 }
373
374 // Set window title
375 {
376 XTextProperty title_prop;
377 const char *title = GetString(STR_WINDOW_TITLE);
378 XStringListToTextProperty((char **)&title, 1, &title_prop);
379 XSetWMName(x_display, the_win, &title_prop);
380 XFree(title_prop.value);
381 }
382
383 // Set window class
384 {
385 XClassHint *hints;
386 hints = XAllocClassHint();
387 if (hints) {
388 hints->res_name = "BasiliskII";
389 hints->res_class = "BasiliskII";
390 XSetClassHint(x_display, the_win, hints);
391 XFree((char *)hints);
392 }
393 }
394
395 // Show window
396 XSync(x_display, false);
397 XMapRaised(x_display, the_win);
398 XFlush(x_display);
399
400 // Set colormap
401 if (depth == 8)
402 XSetWindowColormap(x_display, the_win, cmap[0]);
403
404 // Try to create and attach SHM image
405 have_shm = false;
406 if (depth != 1 && local_X11 && XShmQueryExtension(x_display)) {
407
408 // Create SHM image ("height + 2" for safety)
409 img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height);
410 shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777);
411 the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0);
412 shminfo.shmaddr = img->data = (char *)the_buffer_copy;
413 shminfo.readOnly = False;
414
415 // Try to attach SHM image, catching errors
416 shm_error = false;
417 old_error_handler = XSetErrorHandler(error_handler);
418 XShmAttach(x_display, &shminfo);
419 XSync(x_display, false);
420 XSetErrorHandler(old_error_handler);
421 if (shm_error) {
422 shmdt(shminfo.shmaddr);
423 XDestroyImage(img);
424 shminfo.shmid = -1;
425 } else {
426 have_shm = true;
427 shmctl(shminfo.shmid, IPC_RMID, 0);
428 }
429 }
430
431 // Create normal X image if SHM doesn't work ("height + 2" for safety)
432 if (!have_shm) {
433 int bytes_per_row = aligned_width;
434 switch (depth) {
435 case 1:
436 bytes_per_row /= 8;
437 break;
438 case 15:
439 case 16:
440 bytes_per_row *= 2;
441 break;
442 case 24:
443 case 32:
444 bytes_per_row *= 4;
445 break;
446 }
447 the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row);
448 img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row);
449 }
450
451 // 1-Bit mode is big-endian
452 if (depth == 1) {
453 img->byte_order = MSBFirst;
454 img->bitmap_bit_order = MSBFirst;
455 }
456
457 #ifdef ENABLE_VOSF
458 // Allocate a page-aligned chunk of memory for frame buffer
459 the_buffer_size = align_on_page_boundary((aligned_height + 2) * img->bytes_per_line);
460 the_host_buffer = the_buffer_copy;
461
462 the_buffer_copy = (uint8 *)allocate_framebuffer(the_buffer_size);
463 memset(the_buffer_copy, 0, the_buffer_size);
464
465 the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
466 memset(the_buffer, 0, the_buffer_size);
467 #else
468 // Allocate memory for frame buffer
469 the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line);
470 #endif
471
472 // Create GC
473 the_gc = XCreateGC(x_display, the_win, 0, 0);
474 XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes);
475
476 // Create no_cursor
477 mac_cursor = XCreatePixmapCursor(x_display,
478 XCreatePixmap(x_display, the_win, 1, 1, 1),
479 XCreatePixmap(x_display, the_win, 1, 1, 1),
480 &black, &white, 0, 0);
481 XDefineCursor(x_display, the_win, mac_cursor);
482
483 // Set VideoMonitor
484 bool native_byte_order;
485 #ifdef WORDS_BIGENDIAN
486 native_byte_order = (img->bitmap_bit_order == MSBFirst);
487 #else
488 native_byte_order = (img->bitmap_bit_order == LSBFirst);
489 #endif
490 #ifdef ENABLE_VOSF
491 do_update_framebuffer = GET_FBCOPY_FUNC(depth, native_byte_order, DISPLAY_WINDOW);
492 #endif
493 set_video_monitor(width, height, img->bytes_per_line, native_byte_order);
494
495 #if REAL_ADDRESSING || DIRECT_ADDRESSING
496 VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
497 #else
498 VideoMonitor.mac_frame_base = MacFrameBaseMac;
499 #endif
500 return true;
501 }
502
503 // Init fbdev DGA display
504 static bool init_fbdev_dga(char *in_fb_name)
505 {
506 #ifdef ENABLE_FBDEV_DGA
507 // Find the maximum depth available
508 int ndepths, max_depth(0);
509 int *depths = XListDepths(x_display, screen, &ndepths);
510 if (depths == NULL) {
511 printf("FATAL: Could not determine the maximal depth available\n");
512 return false;
513 } else {
514 while (ndepths-- > 0) {
515 if (depths[ndepths] > max_depth)
516 max_depth = depths[ndepths];
517 }
518 }
519
520 // Get fbdevices file path from preferences
521 const char *fbd_path = PrefsFindString("fbdevicefile");
522
523 // Open fbdevices file
524 FILE *fp = fopen(fbd_path ? fbd_path : FBDEVICES_FILE_NAME, "r");
525 if (fp == NULL) {
526 char str[256];
527 sprintf(str, GetString(STR_NO_FBDEVICE_FILE_ERR), fbd_path ? fbd_path : FBDEVICES_FILE_NAME, strerror(errno));
528 ErrorAlert(str);
529 return false;
530 }
531
532 int fb_depth; // supported depth
533 uint32 fb_offset; // offset used for mmap(2)
534 char fb_name[20];
535 char line[256];
536 bool device_found = false;
537 while (fgets(line, 255, fp)) {
538 // Read line
539 int len = strlen(line);
540 if (len == 0)
541 continue;
542 line[len - 1] = '\0';
543
544 // Comments begin with "#" or ";"
545 if ((line[0] == '#') || (line[0] == ';') || (line[0] == '\0'))
546 continue;
547
548 if ((sscanf(line, "%19s %d %x", &fb_name, &fb_depth, &fb_offset) == 3)
549 && (strcmp(fb_name, in_fb_name) == 0) && (fb_depth == max_depth)) {
550 device_found = true;
551 break;
552 }
553 }
554
555 // fbdevices file completely read
556 fclose(fp);
557
558 // Frame buffer name not found ? Then, display warning
559 if (!device_found) {
560 char str[256];
561 sprintf(str, GetString(STR_FBDEV_NAME_ERR), in_fb_name, max_depth);
562 ErrorAlert(str);
563 return false;
564 }
565
566 int width = DisplayWidth(x_display, screen);
567 int height = DisplayHeight(x_display, screen);
568 depth = fb_depth; // max_depth
569
570 // Set relative mouse mode
571 ADBSetRelMouseMode(false);
572
573 // Create window
574 XSetWindowAttributes wattr;
575 wattr.override_redirect = True;
576 wattr.backing_store = NotUseful;
577 wattr.background_pixel = white_pixel;
578 wattr.border_pixel = black_pixel;
579 wattr.event_mask = eventmask = dga_eventmask;
580
581 XSync(x_display, false);
582 the_win = XCreateWindow(x_display, rootwin,
583 0, 0, width, height,
584 0, xdepth, InputOutput, vis,
585 CWEventMask|CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWBackingStore,
586 &wattr);
587 XSync(x_display, false);
588 XMapRaised(x_display, the_win);
589 XSync(x_display, false);
590
591 // Grab mouse and keyboard
592 XGrabKeyboard(x_display, the_win, True,
593 GrabModeAsync, GrabModeAsync, CurrentTime);
594 XGrabPointer(x_display, the_win, True,
595 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
596 GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime);
597
598 // Set colormap
599 if (depth == 8)
600 XSetWindowColormap(x_display, the_win, cmap[0]);
601
602 // Set VideoMonitor
603 int bytes_per_row = width;
604 switch (depth) {
605 case 1:
606 bytes_per_row = ((width | 7) & ~7) >> 3;
607 break;
608 case 15:
609 case 16:
610 bytes_per_row *= 2;
611 break;
612 case 24:
613 case 32:
614 bytes_per_row *= 4;
615 break;
616 }
617
618 if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_PRIVATE, fbdev_fd, fb_offset)) == MAP_FAILED) {
619 if ((the_buffer = (uint8 *) mmap(NULL, height * bytes_per_row, PROT_READ | PROT_WRITE, MAP_SHARED, fbdev_fd, fb_offset)) == MAP_FAILED) {
620 char str[256];
621 sprintf(str, GetString(STR_FBDEV_MMAP_ERR), strerror(errno));
622 ErrorAlert(str);
623 return false;
624 }
625 }
626
627 #if ENABLE_VOSF
628 #if REAL_ADDRESSING || DIRECT_ADDRESSING
629 // If the blit function is null, i.e. just a copy of the buffer,
630 // we first try to avoid the allocation of a temporary frame buffer
631 use_vosf = true;
632 do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
633 if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
634 use_vosf = false;
635
636 if (use_vosf) {
637 the_host_buffer = the_buffer;
638 the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
639 the_buffer_copy = (uint8 *)malloc(the_buffer_size);
640 memset(the_buffer_copy, 0, the_buffer_size);
641 the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
642 memset(the_buffer, 0, the_buffer_size);
643 }
644 #else
645 use_vosf = false;
646 #endif
647 #endif
648
649 set_video_monitor(width, height, bytes_per_row, true);
650 #if REAL_ADDRESSING || DIRECT_ADDRESSING
651 VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
652 #else
653 VideoMonitor.mac_frame_base = MacFrameBaseMac;
654 #endif
655 return true;
656 #else
657 ErrorAlert("Basilisk II has been compiled with fbdev DGA support disabled.");
658 return false;
659 #endif
660 }
661
662 // Init XF86 DGA display
663 static bool init_xf86_dga(int width, int height)
664 {
665 #ifdef ENABLE_XF86_DGA
666 // Set relative mouse mode
667 ADBSetRelMouseMode(true);
668
669 #ifdef ENABLE_XF86_VIDMODE
670 // Switch to best mode
671 if (has_vidmode) {
672 int best = 0;
673 for (int i=1; i<num_x_video_modes; i++) {
674 if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height &&
675 x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) {
676 best = i;
677 }
678 }
679 XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]);
680 XF86VidModeSetViewPort(x_display, screen, 0, 0);
681 }
682 #endif
683
684 // Create window
685 XSetWindowAttributes wattr;
686 wattr.event_mask = eventmask = dga_eventmask;
687 wattr.border_pixel = black_pixel;
688 wattr.override_redirect = True;
689
690 XSync(x_display, false);
691 the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth,
692 InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr);
693 XSync(x_display, false);
694 XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE));
695 XMapRaised(x_display, the_win);
696 XSync(x_display, false);
697
698 // Establish direct screen connection
699 XMoveResizeWindow(x_display, the_win, 0, 0, width, height);
700 XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
701 XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime);
702 XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
703
704 int v_width, v_bank, v_size;
705 XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size);
706 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
707 XF86DGASetViewPort(x_display, screen, 0, 0);
708 XF86DGASetVidPage(x_display, screen, 0);
709
710 // Set colormap
711 if (depth == 8) {
712 XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]);
713 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
714 }
715
716 // Set VideoMonitor
717 int bytes_per_row = (v_width + 7) & ~7;
718 switch (depth) {
719 case 1:
720 bytes_per_row /= 8;
721 break;
722 case 15:
723 case 16:
724 bytes_per_row *= 2;
725 break;
726 case 24:
727 case 32:
728 bytes_per_row *= 4;
729 break;
730 }
731
732 #if REAL_ADDRESSING || DIRECT_ADDRESSING
733 // If the blit function is null, i.e. just a copy of the buffer,
734 // we first try to avoid the allocation of a temporary frame buffer
735 use_vosf = true;
736 do_update_framebuffer = GET_FBCOPY_FUNC(depth, true, DISPLAY_DGA);
737 if (do_update_framebuffer == FBCOPY_FUNC(fbcopy_raw))
738 use_vosf = false;
739
740 if (use_vosf) {
741 the_host_buffer = the_buffer;
742 the_buffer_size = align_on_page_boundary((height + 2) * bytes_per_row);
743 the_buffer_copy = (uint8 *)malloc(the_buffer_size);
744 memset(the_buffer_copy, 0, the_buffer_size);
745 the_buffer = (uint8 *)allocate_framebuffer(the_buffer_size);
746 memset(the_buffer, 0, the_buffer_size);
747 }
748 #elif defined(ENABLE_VOSF)
749 // The UAE memory handlers will already handle color conversion, if needed.
750 use_vosf = false;
751 #endif
752
753 set_video_monitor(width, height, bytes_per_row, true);
754 #if REAL_ADDRESSING || DIRECT_ADDRESSING
755 VideoMonitor.mac_frame_base = Host2MacAddr(the_buffer);
756 // MacFrameLayout = FLAYOUT_DIRECT;
757 #else
758 VideoMonitor.mac_frame_base = MacFrameBaseMac;
759 #endif
760 return true;
761 #else
762 ErrorAlert("Basilisk II has been compiled with XF86 DGA support disabled.");
763 return false;
764 #endif
765 }
766
767 // Init keycode translation table
768 static void keycode_init(void)
769 {
770 bool use_kc = PrefsFindBool("keycodes");
771 if (use_kc) {
772
773 // Get keycode file path from preferences
774 const char *kc_path = PrefsFindString("keycodefile");
775
776 // Open keycode table
777 FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r");
778 if (f == NULL) {
779 char str[256];
780 sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno));
781 WarningAlert(str);
782 return;
783 }
784
785 // Default translation table
786 for (int i=0; i<256; i++)
787 keycode_table[i] = -1;
788
789 // Search for server vendor string, then read keycodes
790 const char *vendor = ServerVendor(x_display);
791 bool vendor_found = false;
792 char line[256];
793 while (fgets(line, 255, f)) {
794 // Read line
795 int len = strlen(line);
796 if (len == 0)
797 continue;
798 line[len-1] = 0;
799
800 // Comments begin with "#" or ";"
801 if (line[0] == '#' || line[0] == ';' || line[0] == 0)
802 continue;
803
804 if (vendor_found) {
805 // Read keycode
806 int x_code, mac_code;
807 if (sscanf(line, "%d %d", &x_code, &mac_code) == 2)
808 keycode_table[x_code & 0xff] = mac_code;
809 else
810 break;
811 } else {
812 // Search for vendor string
813 if (strstr(vendor, line) == vendor)
814 vendor_found = true;
815 }
816 }
817
818 // Keycode file completely read
819 fclose(f);
820 use_keycodes = vendor_found;
821
822 // Vendor not found? Then display warning
823 if (!vendor_found) {
824 char str[256];
825 sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME);
826 WarningAlert(str);
827 return;
828 }
829 }
830 }
831
832 bool VideoInitBuffer()
833 {
834 #ifdef ENABLE_VOSF
835 if (use_vosf) {
836 const uint32 page_size = getpagesize();
837 const uint32 page_mask = page_size - 1;
838
839 mainBuffer.memBase = (uint32) the_buffer;
840 // Align the frame buffer on page boundary
841 mainBuffer.memStart = (uint32)((((unsigned long) the_buffer) + page_mask) & ~page_mask);
842 mainBuffer.memLength = the_buffer_size;
843 mainBuffer.memEnd = mainBuffer.memStart + mainBuffer.memLength;
844
845 mainBuffer.pageSize = page_size;
846 mainBuffer.pageCount = (mainBuffer.memLength + page_mask)/mainBuffer.pageSize;
847 mainBuffer.pageBits = log_base_2(mainBuffer.pageSize);
848
849 if (mainBuffer.dirtyPages != 0)
850 free(mainBuffer.dirtyPages);
851
852 mainBuffer.dirtyPages = (uint8 *) malloc(mainBuffer.pageCount);
853
854 if (mainBuffer.pageInfo != 0)
855 free(mainBuffer.pageInfo);
856
857 mainBuffer.pageInfo = (ScreenPageInfo *) malloc(mainBuffer.pageCount * sizeof(ScreenPageInfo));
858
859 if ((mainBuffer.dirtyPages == 0) || (mainBuffer.pageInfo == 0))
860 return false;
861
862 PFLAG_CLEAR_ALL;
863
864 uint32 a = 0;
865 for (int i = 0; i < mainBuffer.pageCount; i++) {
866 int y1 = a / VideoMonitor.bytes_per_row;
867 if (y1 >= VideoMonitor.y)
868 y1 = VideoMonitor.y - 1;
869
870 int y2 = (a + mainBuffer.pageSize) / VideoMonitor.bytes_per_row;
871 if (y2 >= VideoMonitor.y)
872 y2 = VideoMonitor.y - 1;
873
874 mainBuffer.pageInfo[i].top = y1;
875 mainBuffer.pageInfo[i].bottom = y2;
876
877 a += mainBuffer.pageSize;
878 if (a > mainBuffer.memLength)
879 a = mainBuffer.memLength;
880 }
881
882 // We can now write-protect the frame buffer
883 if (mprotect((caddr_t)mainBuffer.memStart, mainBuffer.memLength, PROT_READ) != 0)
884 return false;
885 }
886 #endif
887 return true;
888 }
889
890 bool VideoInit(bool classic)
891 {
892 #ifdef ENABLE_VOSF
893 // Open /dev/zero
894 zero_fd = open("/dev/zero", O_RDWR);
895 if (zero_fd < 0) {
896 char str[256];
897 sprintf(str, GetString(STR_NO_DEV_ZERO_ERR), strerror(errno));
898 ErrorAlert(str);
899 return false;
900 }
901
902 // Zero the mainBuffer structure
903 mainBuffer.dirtyPages = 0;
904 mainBuffer.pageInfo = 0;
905 #endif
906
907 // Check if X server runs on local machine
908 local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0)
909 || (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0);
910
911 // Init keycode translation
912 keycode_init();
913
914 // Read prefs
915 mouse_wheel_mode = PrefsFindInt16("mousewheelmode");
916 mouse_wheel_lines = PrefsFindInt16("mousewheellines");
917
918 // Find screen and root window
919 screen = XDefaultScreen(x_display);
920 rootwin = XRootWindow(x_display, screen);
921
922 // Get screen depth
923 xdepth = DefaultDepth(x_display, screen);
924
925 #ifdef ENABLE_FBDEV_DGA
926 // Frame buffer name
927 char fb_name[20];
928
929 // Could do fbdev dga ?
930 if ((fbdev_fd = open(FBDEVICE_FILE_NAME, O_RDWR)) != -1)
931 has_dga = true;
932 else
933 has_dga = false;
934 #endif
935
936 #ifdef ENABLE_XF86_DGA
937 // DGA available?
938 int dga_event_base, dga_error_base;
939 if (local_X11 && XF86DGAQueryExtension(x_display, &dga_event_base, &dga_error_base)) {
940 int dga_flags = 0;
941 XF86DGAQueryDirectVideo(x_display, screen, &dga_flags);
942 has_dga = dga_flags & XF86DGADirectPresent;
943 } else
944 has_dga = false;
945 #endif
946
947 #ifdef ENABLE_XF86_VIDMODE
948 // VidMode available?
949 int vm_event_base, vm_error_base;
950 has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base);
951 if (has_vidmode)
952 XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes);
953 #endif
954
955 // Find black and white colors
956 XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black);
957 XAllocColor(x_display, DefaultColormap(x_display, screen), &black);
958 XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white);
959 XAllocColor(x_display, DefaultColormap(x_display, screen), &white);
960 black_pixel = BlackPixel(x_display, screen);
961 white_pixel = WhitePixel(x_display, screen);
962
963 // Get appropriate visual
964 int color_class;
965 switch (xdepth) {
966 case 1:
967 color_class = StaticGray;
968 break;
969 case 8:
970 color_class = PseudoColor;
971 break;
972 case 15:
973 case 16:
974 case 24:
975 case 32:
976 color_class = TrueColor;
977 break;
978 default:
979 ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR));
980 return false;
981 }
982 if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) {
983 ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
984 return false;
985 }
986 if (visualInfo.depth != xdepth) {
987 ErrorAlert(GetString(STR_NO_XVISUAL_ERR));
988 return false;
989 }
990 vis = visualInfo.visual;
991
992 // Mac screen depth is always 1 bit in Classic mode, but follows X depth otherwise
993 classic_mode = classic;
994 if (classic)
995 depth = 1;
996 else
997 depth = xdepth;
998
999 // Create color maps for 8 bit mode
1000 if (depth == 8) {
1001 cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1002 cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll);
1003 XInstallColormap(x_display, cmap[0]);
1004 XInstallColormap(x_display, cmap[1]);
1005 }
1006
1007 // Get screen mode from preferences
1008 const char *mode_str;
1009 if (classic)
1010 mode_str = "win/512/342";
1011 else
1012 mode_str = PrefsFindString("screen");
1013
1014 // Determine type and mode
1015 int width = 512, height = 384;
1016 display_type = DISPLAY_WINDOW;
1017 if (mode_str) {
1018 if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
1019 display_type = DISPLAY_WINDOW;
1020 #ifdef ENABLE_FBDEV_DGA
1021 else if (has_dga && sscanf(mode_str, "dga/%19s", fb_name) == 1) {
1022 #else
1023 else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) {
1024 #endif
1025 display_type = DISPLAY_DGA;
1026 if (width > DisplayWidth(x_display, screen))
1027 width = DisplayWidth(x_display, screen);
1028 if (height > DisplayHeight(x_display, screen))
1029 height = DisplayHeight(x_display, screen);
1030 }
1031 if (width <= 0)
1032 width = DisplayWidth(x_display, screen);
1033 if (height <= 0)
1034 height = DisplayHeight(x_display, screen);
1035 }
1036
1037 // Initialize according to display type
1038 switch (display_type) {
1039 case DISPLAY_WINDOW:
1040 if (!init_window(width, height))
1041 return false;
1042 break;
1043 case DISPLAY_DGA:
1044 #ifdef ENABLE_FBDEV_DGA
1045 if (!init_fbdev_dga(fb_name))
1046 #else
1047 if (!init_xf86_dga(width, height))
1048 #endif
1049 return false;
1050 break;
1051 }
1052
1053 #ifdef HAVE_PTHREADS
1054 // Lock down frame buffer
1055 pthread_mutex_lock(&frame_buffer_lock);
1056 #endif
1057
1058 #if !REAL_ADDRESSING && !DIRECT_ADDRESSING
1059 // Set variables for UAE memory mapping
1060 MacFrameBaseHost = the_buffer;
1061 MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
1062
1063 // No special frame buffer in Classic mode (frame buffer is in Mac RAM)
1064 if (classic)
1065 MacFrameLayout = FLAYOUT_NONE;
1066 #endif
1067
1068 #ifdef ENABLE_VOSF
1069 if (use_vosf) {
1070 // Initialize the mainBuffer structure
1071 if (!VideoInitBuffer()) {
1072 // TODO: STR_VOSF_INIT_ERR ?
1073 ErrorAlert("Could not initialize Video on SEGV signals");
1074 return false;
1075 }
1076
1077 // Initialize the handler for SIGSEGV
1078 if (!Screen_fault_handler_init()) {
1079 // TODO: STR_VOSF_INIT_ERR ?
1080 ErrorAlert("Could not initialize Video on SEGV signals");
1081 return false;
1082 }
1083 }
1084 #endif
1085
1086 // Initialize VideoRefresh function
1087 VideoRefreshInit();
1088
1089 XSync(x_display, false);
1090
1091 #ifdef HAVE_PTHREADS
1092 // Start redraw/input thread
1093 redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0);
1094 if (!redraw_thread_active) {
1095 printf("FATAL: cannot create redraw thread\n");
1096 return false;
1097 }
1098 #endif
1099 return true;
1100 }
1101
1102
1103 /*
1104 * Deinitialization
1105 */
1106
1107 void VideoExit(void)
1108 {
1109 #ifdef HAVE_PTHREADS
1110 // Stop redraw thread
1111 if (redraw_thread_active) {
1112 redraw_thread_cancel = true;
1113 #ifdef HAVE_PTHREAD_CANCEL
1114 pthread_cancel(redraw_thread);
1115 #endif
1116 pthread_join(redraw_thread, NULL);
1117 redraw_thread_active = false;
1118 }
1119 #endif
1120
1121 #ifdef HAVE_PTHREADS
1122 // Unlock frame buffer
1123 pthread_mutex_unlock(&frame_buffer_lock);
1124 #endif
1125
1126 // Close window and server connection
1127 if (x_display != NULL) {
1128 XSync(x_display, false);
1129
1130 #ifdef ENABLE_XF86_DGA
1131 if (display_type == DISPLAY_DGA) {
1132 XF86DGADirectVideo(x_display, screen, 0);
1133 XUngrabPointer(x_display, CurrentTime);
1134 XUngrabKeyboard(x_display, CurrentTime);
1135 }
1136 #endif
1137
1138 #ifdef ENABLE_XF86_VIDMODE
1139 if (has_vidmode && display_type == DISPLAY_DGA)
1140 XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]);
1141 #endif
1142
1143 #ifdef ENABLE_FBDEV_DGA
1144 if (display_type == DISPLAY_DGA) {
1145 XUngrabPointer(x_display, CurrentTime);
1146 XUngrabKeyboard(x_display, CurrentTime);
1147 close(fbdev_fd);
1148 }
1149 #endif
1150
1151 XFlush(x_display);
1152 XSync(x_display, false);
1153 if (depth == 8) {
1154 XFreeColormap(x_display, cmap[0]);
1155 XFreeColormap(x_display, cmap[1]);
1156 }
1157
1158 if (!use_vosf) {
1159 if (the_buffer) {
1160 free(the_buffer);
1161 the_buffer = NULL;
1162 }
1163
1164 if (!have_shm && the_buffer_copy) {
1165 free(the_buffer_copy);
1166 the_buffer_copy = NULL;
1167 }
1168 }
1169 #ifdef ENABLE_VOSF
1170 else {
1171 if (the_buffer != (uint8 *)MAP_FAILED) {
1172 munmap((caddr_t)the_buffer, the_buffer_size);
1173 the_buffer = 0;
1174 }
1175
1176 if (the_buffer_copy != (uint8 *)MAP_FAILED) {
1177 munmap((caddr_t)the_buffer_copy, the_buffer_size);
1178 the_buffer_copy = 0;
1179 }
1180 }
1181 #endif
1182 }
1183
1184 #ifdef ENABLE_VOSF
1185 if (use_vosf) {
1186 // Clear mainBuffer data
1187 if (mainBuffer.pageInfo) {
1188 free(mainBuffer.pageInfo);
1189 mainBuffer.pageInfo = 0;
1190 }
1191
1192 if (mainBuffer.dirtyPages) {
1193 free(mainBuffer.dirtyPages);
1194 mainBuffer.dirtyPages = 0;
1195 }
1196 }
1197
1198 // Close /dev/zero
1199 if (zero_fd > 0)
1200 close(zero_fd);
1201 #endif
1202 }
1203
1204
1205 /*
1206 * Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
1207 */
1208
1209 void VideoQuitFullScreen(void)
1210 {
1211 D(bug("VideoQuitFullScreen()\n"));
1212 if (display_type == DISPLAY_DGA)
1213 quit_full_screen = true;
1214 }
1215
1216
1217 /*
1218 * Mac VBL interrupt
1219 */
1220
1221 void VideoInterrupt(void)
1222 {
1223 // Emergency quit requested? Then quit
1224 if (emerg_quit)
1225 QuitEmulator();
1226
1227 #ifdef HAVE_PTHREADS
1228 // Temporarily give up frame buffer lock (this is the point where
1229 // we are suspended when the user presses Ctrl-Tab)
1230 pthread_mutex_unlock(&frame_buffer_lock);
1231 pthread_mutex_lock(&frame_buffer_lock);
1232 #endif
1233 }
1234
1235
1236 /*
1237 * Set palette
1238 */
1239
1240 void video_set_palette(uint8 *pal)
1241 {
1242 #ifdef HAVE_PTHREADS
1243 pthread_mutex_lock(&palette_lock);
1244 #endif
1245
1246 // Convert colors to XColor array
1247 for (int i=0; i<256; i++) {
1248 palette[i].pixel = i;
1249 palette[i].red = pal[i*3] * 0x0101;
1250 palette[i].green = pal[i*3+1] * 0x0101;
1251 palette[i].blue = pal[i*3+2] * 0x0101;
1252 palette[i].flags = DoRed | DoGreen | DoBlue;
1253 }
1254
1255 // Tell redraw thread to change palette
1256 palette_changed = true;
1257
1258 #ifdef HAVE_PTHREADS
1259 pthread_mutex_unlock(&palette_lock);
1260 #endif
1261 }
1262
1263
1264 /*
1265 * Suspend/resume emulator
1266 */
1267
1268 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1269 static void suspend_emul(void)
1270 {
1271 if (display_type == DISPLAY_DGA) {
1272 // Release ctrl key
1273 ADBKeyUp(0x36);
1274 ctrl_down = false;
1275
1276 #ifdef HAVE_PTHREADS
1277 // Lock frame buffer (this will stop the MacOS thread)
1278 pthread_mutex_lock(&frame_buffer_lock);
1279 #endif
1280
1281 // Save frame buffer
1282 fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row);
1283 if (fb_save)
1284 memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row);
1285
1286 // Close full screen display
1287 #ifdef ENABLE_XF86_DGA
1288 XF86DGADirectVideo(x_display, screen, 0);
1289 #endif
1290 XUngrabPointer(x_display, CurrentTime);
1291 XUngrabKeyboard(x_display, CurrentTime);
1292 XUnmapWindow(x_display, the_win);
1293 XSync(x_display, false);
1294
1295 // Open "suspend" window
1296 XSetWindowAttributes wattr;
1297 wattr.event_mask = KeyPressMask;
1298 wattr.background_pixel = black_pixel;
1299 wattr.border_pixel = black_pixel;
1300 wattr.backing_store = Always;
1301 wattr.backing_planes = xdepth;
1302 wattr.colormap = DefaultColormap(x_display, screen);
1303
1304 XSync(x_display, false);
1305 suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth,
1306 InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel |
1307 CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr);
1308 XSync(x_display, false);
1309 XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE));
1310 XMapRaised(x_display, suspend_win);
1311 XSync(x_display, false);
1312 emul_suspended = true;
1313 }
1314 }
1315
1316 static void resume_emul(void)
1317 {
1318 // Close "suspend" window
1319 XDestroyWindow(x_display, suspend_win);
1320 XSync(x_display, false);
1321
1322 // Reopen full screen display
1323 XMapRaised(x_display, the_win);
1324 XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0);
1325 XSync(x_display, false);
1326 XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime);
1327 XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
1328 #ifdef ENABLE_XF86_DGA
1329 XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse);
1330 XF86DGASetViewPort(x_display, screen, 0, 0);
1331 #endif
1332 XSync(x_display, false);
1333
1334 // the_buffer already contains the data to restore. i.e. since a temporary
1335 // frame buffer is used when VOSF is actually used, fb_save is therefore
1336 // not necessary.
1337 #ifdef ENABLE_VOSF
1338 if (use_vosf) {
1339 #ifdef HAVE_PTHREADS
1340 pthread_mutex_lock(&Screen_draw_lock);
1341 #endif
1342 PFLAG_SET_ALL;
1343 #ifdef HAVE_PTHREADS
1344 pthread_mutex_unlock(&Screen_draw_lock);
1345 #endif
1346 memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1347 }
1348 #endif
1349
1350 // Restore frame buffer
1351 if (fb_save) {
1352 #ifdef ENABLE_VOSF
1353 // Don't copy fb_save to the temporary frame buffer in VOSF mode
1354 if (!use_vosf)
1355 #endif
1356 memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row);
1357 free(fb_save);
1358 fb_save = NULL;
1359 }
1360
1361 #ifdef ENABLE_XF86_DGA
1362 if (depth == 8)
1363 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1364 #endif
1365
1366 #ifdef HAVE_PTHREADS
1367 // Unlock frame buffer (and continue MacOS thread)
1368 pthread_mutex_unlock(&frame_buffer_lock);
1369 emul_suspended = false;
1370 #endif
1371 }
1372 #endif
1373
1374
1375 /*
1376 * Translate key event to Mac keycode
1377 */
1378
1379 static int kc_decode(KeySym ks)
1380 {
1381 switch (ks) {
1382 case XK_A: case XK_a: return 0x00;
1383 case XK_B: case XK_b: return 0x0b;
1384 case XK_C: case XK_c: return 0x08;
1385 case XK_D: case XK_d: return 0x02;
1386 case XK_E: case XK_e: return 0x0e;
1387 case XK_F: case XK_f: return 0x03;
1388 case XK_G: case XK_g: return 0x05;
1389 case XK_H: case XK_h: return 0x04;
1390 case XK_I: case XK_i: return 0x22;
1391 case XK_J: case XK_j: return 0x26;
1392 case XK_K: case XK_k: return 0x28;
1393 case XK_L: case XK_l: return 0x25;
1394 case XK_M: case XK_m: return 0x2e;
1395 case XK_N: case XK_n: return 0x2d;
1396 case XK_O: case XK_o: return 0x1f;
1397 case XK_P: case XK_p: return 0x23;
1398 case XK_Q: case XK_q: return 0x0c;
1399 case XK_R: case XK_r: return 0x0f;
1400 case XK_S: case XK_s: return 0x01;
1401 case XK_T: case XK_t: return 0x11;
1402 case XK_U: case XK_u: return 0x20;
1403 case XK_V: case XK_v: return 0x09;
1404 case XK_W: case XK_w: return 0x0d;
1405 case XK_X: case XK_x: return 0x07;
1406 case XK_Y: case XK_y: return 0x10;
1407 case XK_Z: case XK_z: return 0x06;
1408
1409 case XK_1: case XK_exclam: return 0x12;
1410 case XK_2: case XK_at: return 0x13;
1411 case XK_3: case XK_numbersign: return 0x14;
1412 case XK_4: case XK_dollar: return 0x15;
1413 case XK_5: case XK_percent: return 0x17;
1414 case XK_6: return 0x16;
1415 case XK_7: return 0x1a;
1416 case XK_8: return 0x1c;
1417 case XK_9: return 0x19;
1418 case XK_0: return 0x1d;
1419
1420 case XK_grave: case XK_asciitilde: return 0x0a;
1421 case XK_minus: case XK_underscore: return 0x1b;
1422 case XK_equal: case XK_plus: return 0x18;
1423 case XK_bracketleft: case XK_braceleft: return 0x21;
1424 case XK_bracketright: case XK_braceright: return 0x1e;
1425 case XK_backslash: case XK_bar: return 0x2a;
1426 case XK_semicolon: case XK_colon: return 0x29;
1427 case XK_apostrophe: case XK_quotedbl: return 0x27;
1428 case XK_comma: case XK_less: return 0x2b;
1429 case XK_period: case XK_greater: return 0x2f;
1430 case XK_slash: case XK_question: return 0x2c;
1431
1432 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1433 case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30;
1434 #else
1435 case XK_Tab: return 0x30;
1436 #endif
1437 case XK_Return: return 0x24;
1438 case XK_space: return 0x31;
1439 case XK_BackSpace: return 0x33;
1440
1441 case XK_Delete: return 0x75;
1442 case XK_Insert: return 0x72;
1443 case XK_Home: case XK_Help: return 0x73;
1444 case XK_End: return 0x77;
1445 #ifdef __hpux
1446 case XK_Prior: return 0x74;
1447 case XK_Next: return 0x79;
1448 #else
1449 case XK_Page_Up: return 0x74;
1450 case XK_Page_Down: return 0x79;
1451 #endif
1452
1453 case XK_Control_L: return 0x36;
1454 case XK_Control_R: return 0x36;
1455 case XK_Shift_L: return 0x38;
1456 case XK_Shift_R: return 0x38;
1457 case XK_Alt_L: return 0x37;
1458 case XK_Alt_R: return 0x37;
1459 case XK_Meta_L: return 0x3a;
1460 case XK_Meta_R: return 0x3a;
1461 case XK_Menu: return 0x32;
1462 case XK_Caps_Lock: return 0x39;
1463 case XK_Num_Lock: return 0x47;
1464
1465 case XK_Up: return 0x3e;
1466 case XK_Down: return 0x3d;
1467 case XK_Left: return 0x3b;
1468 case XK_Right: return 0x3c;
1469
1470 case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35;
1471
1472 case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a;
1473 case XK_F2: return 0x78;
1474 case XK_F3: return 0x63;
1475 case XK_F4: return 0x76;
1476 case XK_F5: return 0x60;
1477 case XK_F6: return 0x61;
1478 case XK_F7: return 0x62;
1479 case XK_F8: return 0x64;
1480 case XK_F9: return 0x65;
1481 case XK_F10: return 0x6d;
1482 case XK_F11: return 0x67;
1483 case XK_F12: return 0x6f;
1484
1485 case XK_Print: return 0x69;
1486 case XK_Scroll_Lock: return 0x6b;
1487 case XK_Pause: return 0x71;
1488
1489 #if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End)
1490 case XK_KP_0: case XK_KP_Insert: return 0x52;
1491 case XK_KP_1: case XK_KP_End: return 0x53;
1492 case XK_KP_2: case XK_KP_Down: return 0x54;
1493 case XK_KP_3: case XK_KP_Next: return 0x55;
1494 case XK_KP_4: case XK_KP_Left: return 0x56;
1495 case XK_KP_5: case XK_KP_Begin: return 0x57;
1496 case XK_KP_6: case XK_KP_Right: return 0x58;
1497 case XK_KP_7: case XK_KP_Home: return 0x59;
1498 case XK_KP_8: case XK_KP_Up: return 0x5b;
1499 case XK_KP_9: case XK_KP_Prior: return 0x5c;
1500 case XK_KP_Decimal: case XK_KP_Delete: return 0x41;
1501 #else
1502 case XK_KP_0: return 0x52;
1503 case XK_KP_1: return 0x53;
1504 case XK_KP_2: return 0x54;
1505 case XK_KP_3: return 0x55;
1506 case XK_KP_4: return 0x56;
1507 case XK_KP_5: return 0x57;
1508 case XK_KP_6: return 0x58;
1509 case XK_KP_7: return 0x59;
1510 case XK_KP_8: return 0x5b;
1511 case XK_KP_9: return 0x5c;
1512 case XK_KP_Decimal: return 0x41;
1513 #endif
1514 case XK_KP_Add: return 0x45;
1515 case XK_KP_Subtract: return 0x4e;
1516 case XK_KP_Multiply: return 0x43;
1517 case XK_KP_Divide: return 0x4b;
1518 case XK_KP_Enter: return 0x4c;
1519 case XK_KP_Equal: return 0x51;
1520 }
1521 return -1;
1522 }
1523
1524 static int event2keycode(XKeyEvent *ev)
1525 {
1526 KeySym ks;
1527 int as;
1528 int i = 0;
1529
1530 do {
1531 ks = XLookupKeysym(ev, i++);
1532 as = kc_decode(ks);
1533 if (as != -1)
1534 return as;
1535 } while (ks != NoSymbol);
1536
1537 return -1;
1538 }
1539
1540
1541 /*
1542 * X event handling
1543 */
1544
1545 static void handle_events(void)
1546 {
1547 XEvent event;
1548 for (;;) {
1549 if (!XCheckMaskEvent(x_display, eventmask, &event))
1550 break;
1551
1552 switch (event.type) {
1553 // Mouse button
1554 case ButtonPress: {
1555 unsigned int button = ((XButtonEvent *)&event)->button;
1556 if (button < 4)
1557 ADBMouseDown(button - 1);
1558 else if (button < 6) { // Wheel mouse
1559 if (mouse_wheel_mode == 0) {
1560 int key = (button == 5) ? 0x79 : 0x74; // Page up/down
1561 ADBKeyDown(key);
1562 ADBKeyUp(key);
1563 } else {
1564 int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down
1565 for(int i=0; i<mouse_wheel_lines; i++) {
1566 ADBKeyDown(key);
1567 ADBKeyUp(key);
1568 }
1569 }
1570 }
1571 break;
1572 }
1573 case ButtonRelease: {
1574 unsigned int button = ((XButtonEvent *)&event)->button;
1575 if (button < 4)
1576 ADBMouseUp(button - 1);
1577 break;
1578 }
1579
1580 // Mouse moved
1581 case EnterNotify:
1582 ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1583 break;
1584 case MotionNotify:
1585 ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y);
1586 break;
1587
1588 // Keyboard
1589 case KeyPress: {
1590 int code;
1591 if (use_keycodes) {
1592 event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys
1593 code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1594 } else
1595 code = event2keycode((XKeyEvent *)&event);
1596 if (code != -1) {
1597 if (!emul_suspended) {
1598 if (code == 0x39) { // Caps Lock pressed
1599 if (caps_on) {
1600 ADBKeyUp(code);
1601 caps_on = false;
1602 } else {
1603 ADBKeyDown(code);
1604 caps_on = true;
1605 }
1606 } else
1607 ADBKeyDown(code);
1608 if (code == 0x36)
1609 ctrl_down = true;
1610 } else {
1611 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1612 if (code == 0x31)
1613 resume_emul(); // Space wakes us up
1614 #endif
1615 }
1616 }
1617 break;
1618 }
1619 case KeyRelease: {
1620 int code;
1621 if (use_keycodes) {
1622 event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys
1623 code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff];
1624 } else
1625 code = event2keycode((XKeyEvent *)&event);
1626 if (code != -1 && code != 0x39) { // Don't propagate Caps Lock releases
1627 ADBKeyUp(code);
1628 if (code == 0x36)
1629 ctrl_down = false;
1630 }
1631 break;
1632 }
1633
1634 // Hidden parts exposed, force complete refresh of window
1635 case Expose:
1636 if (display_type == DISPLAY_WINDOW) {
1637 #ifdef ENABLE_VOSF
1638 if (use_vosf) { // VOSF refresh
1639 #ifdef HAVE_PTHREADS
1640 pthread_mutex_lock(&Screen_draw_lock);
1641 #endif
1642 PFLAG_SET_ALL;
1643 #ifdef HAVE_PTHREADS
1644 pthread_mutex_unlock(&Screen_draw_lock);
1645 #endif
1646 memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1647 }
1648 else
1649 #endif
1650 if (frame_skip == 0) { // Dynamic refresh
1651 int x1, y1;
1652 for (y1=0; y1<16; y1++)
1653 for (x1=0; x1<16; x1++)
1654 updt_box[x1][y1] = true;
1655 nr_boxes = 16 * 16;
1656 } else // Static refresh
1657 memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y);
1658 }
1659 break;
1660
1661 case FocusIn:
1662 case FocusOut:
1663 break;
1664 }
1665 }
1666 }
1667
1668
1669 /*
1670 * Window display update
1671 */
1672
1673 // Dynamic display update (variable frame rate for each box)
1674 static void update_display_dynamic(int ticker)
1675 {
1676 int y1, y2, y2s, y2a, i, x1, xm, xmo, ymo, yo, yi, yil, xi;
1677 int xil = 0;
1678 int rxm = 0, rxmo = 0;
1679 int bytes_per_row = VideoMonitor.bytes_per_row;
1680 int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1681 int rx = VideoMonitor.bytes_per_row / 16;
1682 int ry = VideoMonitor.y / 16;
1683 int max_box;
1684
1685 y2s = sm_uptd[ticker % 8];
1686 y2a = 8;
1687 for (i = 0; i < 6; i++)
1688 if (ticker % (2 << i))
1689 break;
1690 max_box = sm_no_boxes[i];
1691
1692 if (y2a) {
1693 for (y1=0; y1<16; y1++) {
1694 for (y2=y2s; y2 < ry; y2 += y2a) {
1695 i = ((y1 * ry) + y2) * bytes_per_row;
1696 for (x1=0; x1<16; x1++, i += rx) {
1697 if (updt_box[x1][y1] == false) {
1698 if (memcmp(&the_buffer_copy[i], &the_buffer[i], rx)) {
1699 updt_box[x1][y1] = true;
1700 nr_boxes++;
1701 }
1702 }
1703 }
1704 }
1705 }
1706 }
1707
1708 if ((nr_boxes <= max_box) && (nr_boxes)) {
1709 for (y1=0; y1<16; y1++) {
1710 for (x1=0; x1<16; x1++) {
1711 if (updt_box[x1][y1] == true) {
1712 if (rxm == 0)
1713 xm = x1;
1714 rxm += rx;
1715 updt_box[x1][y1] = false;
1716 }
1717 if (((updt_box[x1+1][y1] == false) || (x1 == 15)) && (rxm)) {
1718 if ((rxmo != rxm) || (xmo != xm) || (yo != y1 - 1)) {
1719 if (rxmo) {
1720 xi = xmo * rx;
1721 yi = ymo * ry;
1722 xil = rxmo;
1723 yil = (yo - ymo +1) * ry;
1724 }
1725 rxmo = rxm;
1726 xmo = xm;
1727 ymo = y1;
1728 }
1729 rxm = 0;
1730 yo = y1;
1731 }
1732 if (xil) {
1733 i = (yi * bytes_per_row) + xi;
1734 for (y2=0; y2 < yil; y2++, i += bytes_per_row)
1735 memcpy(&the_buffer_copy[i], &the_buffer[i], xil);
1736 if (depth == 1) {
1737 if (have_shm)
1738 XShmPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil, 0);
1739 else
1740 XPutImage(x_display, the_win, the_gc, img, xi * 8, yi, xi * 8, yi, xil * 8, yil);
1741 } else {
1742 if (have_shm)
1743 XShmPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil, 0);
1744 else
1745 XPutImage(x_display, the_win, the_gc, img, xi / bytes_per_pixel, yi, xi / bytes_per_pixel, yi, xil / bytes_per_pixel, yil);
1746 }
1747 xil = 0;
1748 }
1749 if ((x1 == 15) && (y1 == 15) && (rxmo)) {
1750 x1--;
1751 xi = xmo * rx;
1752 yi = ymo * ry;
1753 xil = rxmo;
1754 yil = (yo - ymo +1) * ry;
1755 rxmo = 0;
1756 }
1757 }
1758 }
1759 nr_boxes = 0;
1760 }
1761 }
1762
1763 // Static display update (fixed frame rate, but incremental)
1764 static void update_display_static(void)
1765 {
1766 // Incremental update code
1767 int wide = 0, high = 0, x1, x2, y1, y2, i, j;
1768 int bytes_per_row = VideoMonitor.bytes_per_row;
1769 int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x;
1770 uint8 *p, *p2;
1771
1772 // Check for first line from top and first line from bottom that have changed
1773 y1 = 0;
1774 for (j=0; j<VideoMonitor.y; j++) {
1775 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1776 y1 = j;
1777 break;
1778 }
1779 }
1780 y2 = y1 - 1;
1781 for (j=VideoMonitor.y-1; j>=y1; j--) {
1782 if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) {
1783 y2 = j;
1784 break;
1785 }
1786 }
1787 high = y2 - y1 + 1;
1788
1789 // Check for first column from left and first column from right that have changed
1790 if (high) {
1791 if (depth == 1) {
1792 x1 = VideoMonitor.x - 1;
1793 for (j=y1; j<=y2; j++) {
1794 p = &the_buffer[j * bytes_per_row];
1795 p2 = &the_buffer_copy[j * bytes_per_row];
1796 for (i=0; i<(x1>>3); i++) {
1797 if (*p != *p2) {
1798 x1 = i << 3;
1799 break;
1800 }
1801 p++; p2++;
1802 }
1803 }
1804 x2 = x1;
1805 for (j=y1; j<=y2; j++) {
1806 p = &the_buffer[j * bytes_per_row];
1807 p2 = &the_buffer_copy[j * bytes_per_row];
1808 p += bytes_per_row;
1809 p2 += bytes_per_row;
1810 for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) {
1811 p--; p2--;
1812 if (*p != *p2) {
1813 x2 = (i << 3) + 7;
1814 break;
1815 }
1816 }
1817 }
1818 wide = x2 - x1 + 1;
1819
1820 // Update copy of the_buffer
1821 if (high && wide) {
1822 for (j=y1; j<=y2; j++) {
1823 i = j * bytes_per_row + (x1 >> 3);
1824 memcpy(the_buffer_copy + i, the_buffer + i, wide >> 3);
1825 }
1826 }
1827
1828 } else {
1829 x1 = VideoMonitor.x;
1830 for (j=y1; j<=y2; j++) {
1831 p = &the_buffer[j * bytes_per_row];
1832 p2 = &the_buffer_copy[j * bytes_per_row];
1833 for (i=0; i<x1*bytes_per_pixel; i++) {
1834 if (*p != *p2) {
1835 x1 = i / bytes_per_pixel;
1836 break;
1837 }
1838 p++; p2++;
1839 }
1840 }
1841 x2 = x1;
1842 for (j=y1; j<=y2; j++) {
1843 p = &the_buffer[j * bytes_per_row];
1844 p2 = &the_buffer_copy[j * bytes_per_row];
1845 p += bytes_per_row;
1846 p2 += bytes_per_row;
1847 for (i=VideoMonitor.x*bytes_per_pixel; i>x2*bytes_per_pixel; i--) {
1848 p--;
1849 p2--;
1850 if (*p != *p2) {
1851 x2 = i / bytes_per_pixel;
1852 break;
1853 }
1854 }
1855 }
1856 wide = x2 - x1;
1857
1858 // Update copy of the_buffer
1859 if (high && wide) {
1860 for (j=y1; j<=y2; j++) {
1861 i = j * bytes_per_row + x1 * bytes_per_pixel;
1862 memcpy(the_buffer_copy + i, the_buffer + i, bytes_per_pixel * wide);
1863 }
1864 }
1865 }
1866 }
1867
1868 // Refresh display
1869 if (high && wide) {
1870 if (have_shm)
1871 XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0);
1872 else
1873 XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high);
1874 }
1875 }
1876
1877
1878 /*
1879 * Screen refresh functions
1880 */
1881
1882 // The specialisations hereunder are meant to enable VOSF with DGA in direct
1883 // addressing mode in case the address spaces (RAM, ROM, FrameBuffer) could
1884 // not get mapped correctly with respect to the predetermined host frame
1885 // buffer base address.
1886 //
1887 // Hmm, in other words, when in direct addressing mode and DGA is requested,
1888 // we first try to "triple allocate" the address spaces according to the real
1889 // host frame buffer address. Then, if it fails, we will use a temporary
1890 // frame buffer thus making the real host frame buffer updated when pages
1891 // of the temp frame buffer are altered.
1892 //
1893 // As a side effect, a little speed gain in screen updates could be noticed
1894 // for other modes than DGA.
1895 //
1896 // The following two functions below are inline so that a clever compiler
1897 // could specialise the code according to the current screen depth and
1898 // display type. A more clever compiler would the job by itself though...
1899 // (update_display_vosf is inlined as well)
1900
1901 static inline void possibly_quit_dga_mode()
1902 {
1903 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
1904 // Quit DGA mode if requested
1905 if (quit_full_screen) {
1906 quit_full_screen = false;
1907 #ifdef ENABLE_XF86_DGA
1908 XF86DGADirectVideo(x_display, screen, 0);
1909 #endif
1910 XUngrabPointer(x_display, CurrentTime);
1911 XUngrabKeyboard(x_display, CurrentTime);
1912 XUnmapWindow(x_display, the_win);
1913 XSync(x_display, false);
1914 }
1915 #endif
1916 }
1917
1918 static inline void handle_palette_changes(int depth, int display_type)
1919 {
1920 #ifdef HAVE_PTHREADS
1921 pthread_mutex_lock(&palette_lock);
1922 #endif
1923 if (palette_changed) {
1924 palette_changed = false;
1925 if (depth == 8) {
1926 XStoreColors(x_display, cmap[0], palette, 256);
1927 XStoreColors(x_display, cmap[1], palette, 256);
1928
1929 #ifdef ENABLE_XF86_DGA
1930 if (display_type == DISPLAY_DGA) {
1931 current_dga_cmap ^= 1;
1932 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
1933 }
1934 #endif
1935 }
1936 }
1937 #ifdef HAVE_PTHREADS
1938 pthread_mutex_unlock(&palette_lock);
1939 #endif
1940 }
1941
1942 static void video_refresh_dga(void)
1943 {
1944 // Quit DGA mode if requested
1945 possibly_quit_dga_mode();
1946
1947 // Handle X events
1948 handle_events();
1949
1950 // Handle palette changes
1951 handle_palette_changes(depth, DISPLAY_DGA);
1952 }
1953
1954 #ifdef ENABLE_VOSF
1955 #if REAL_ADDRESSING || DIRECT_ADDRESSING
1956 static void video_refresh_dga_vosf(void)
1957 {
1958 // Quit DGA mode if requested
1959 possibly_quit_dga_mode();
1960
1961 // Handle X events
1962 handle_events();
1963
1964 // Handle palette changes
1965 handle_palette_changes(depth, DISPLAY_DGA);
1966
1967 // Update display (VOSF variant)
1968 static int tick_counter = 0;
1969 if (++tick_counter >= frame_skip) {
1970 tick_counter = 0;
1971 #ifdef HAVE_PTHREADS
1972 pthread_mutex_lock(&Screen_draw_lock);
1973 #endif
1974 update_display_dga_vosf();
1975 #ifdef HAVE_PTHREADS
1976 pthread_mutex_unlock(&Screen_draw_lock);
1977 #endif
1978 }
1979 }
1980 #endif
1981
1982 static void video_refresh_window_vosf(void)
1983 {
1984 // Quit DGA mode if requested
1985 possibly_quit_dga_mode();
1986
1987 // Handle X events
1988 handle_events();
1989
1990 // Handle palette changes
1991 handle_palette_changes(depth, DISPLAY_WINDOW);
1992
1993 // Update display (VOSF variant)
1994 static int tick_counter = 0;
1995 if (++tick_counter >= frame_skip) {
1996 tick_counter = 0;
1997 #ifdef HAVE_PTHREADS
1998 pthread_mutex_lock(&Screen_draw_lock);
1999 #endif
2000 update_display_window_vosf();
2001 #ifdef HAVE_PTHREADS
2002 pthread_mutex_unlock(&Screen_draw_lock);
2003 #endif
2004 }
2005 }
2006 #endif // def ENABLE_VOSF
2007
2008 static void video_refresh_window_static(void)
2009 {
2010 // Handle X events
2011 handle_events();
2012
2013 // Handle_palette changes
2014 handle_palette_changes(depth, DISPLAY_WINDOW);
2015
2016 // Update display (static variant)
2017 static int tick_counter = 0;
2018 if (++tick_counter >= frame_skip) {
2019 tick_counter = 0;
2020 update_display_static();
2021 }
2022 }
2023
2024 static void video_refresh_window_dynamic(void)
2025 {
2026 // Handle X events
2027 handle_events();
2028
2029 // Handle_palette changes
2030 handle_palette_changes(depth, DISPLAY_WINDOW);
2031
2032 // Update display (dynamic variant)
2033 static int tick_counter = 0;
2034 tick_counter++;
2035 update_display_dynamic(tick_counter);
2036 }
2037
2038
2039 /*
2040 * Thread for screen refresh, input handling etc.
2041 */
2042
2043 void VideoRefreshInit(void)
2044 {
2045 // TODO: set up specialised 8bpp VideoRefresh handlers ?
2046 if (display_type == DISPLAY_DGA) {
2047 #if ENABLE_VOSF && (REAL_ADDRESSING || DIRECT_ADDRESSING)
2048 if (use_vosf)
2049 video_refresh = video_refresh_dga_vosf;
2050 else
2051 #endif
2052 video_refresh = video_refresh_dga;
2053 }
2054 else {
2055 #ifdef ENABLE_VOSF
2056 if (use_vosf)
2057 video_refresh = video_refresh_window_vosf;
2058 else
2059 #endif
2060 if (frame_skip == 0)
2061 video_refresh = video_refresh_window_dynamic;
2062 else
2063 video_refresh = video_refresh_window_static;
2064 }
2065 }
2066
2067 void VideoRefresh(void)
2068 {
2069 // TODO: make main_unix/VideoRefresh call directly video_refresh() ?
2070 video_refresh();
2071 }
2072
2073 #if 0
2074 void VideoRefresh(void)
2075 {
2076 #if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA)
2077 // Quit DGA mode if requested
2078 if (quit_full_screen) {
2079 quit_full_screen = false;
2080 if (display_type == DISPLAY_DGA) {
2081 #ifdef ENABLE_XF86_DGA
2082 XF86DGADirectVideo(x_display, screen, 0);
2083 #endif
2084 XUngrabPointer(x_display, CurrentTime);
2085 XUngrabKeyboard(x_display, CurrentTime);
2086 XUnmapWindow(x_display, the_win);
2087 XSync(x_display, false);
2088 }
2089 }
2090 #endif
2091
2092 // Handle X events
2093 handle_events();
2094
2095 // Handle palette changes
2096 #ifdef HAVE_PTHREADS
2097 pthread_mutex_lock(&palette_lock);
2098 #endif
2099 if (palette_changed) {
2100 palette_changed = false;
2101 if (depth == 8) {
2102 XStoreColors(x_display, cmap[0], palette, 256);
2103 XStoreColors(x_display, cmap[1], palette, 256);
2104
2105 #ifdef ENABLE_XF86_DGA
2106 if (display_type == DISPLAY_DGA) {
2107 current_dga_cmap ^= 1;
2108 XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]);
2109 }
2110 #endif
2111 }
2112 }
2113 #ifdef HAVE_PTHREADS
2114 pthread_mutex_unlock(&palette_lock);
2115 #endif
2116
2117 // In window mode, update display
2118 static int tick_counter = 0;
2119 if (display_type == DISPLAY_WINDOW) {
2120 tick_counter++;
2121 if (frame_skip == 0)
2122 update_display_dynamic(tick_counter);
2123 else if (tick_counter >= frame_skip) {
2124 tick_counter = 0;
2125 update_display_static();
2126 }
2127 }
2128 }
2129 #endif
2130
2131 #ifdef HAVE_PTHREADS
2132 static void *redraw_func(void *arg)
2133 {
2134 uint64 start = GetTicks_usec();
2135 int64 ticks = 0;
2136 uint64 next = GetTicks_usec();
2137 while (!redraw_thread_cancel) {
2138 // VideoRefresh();
2139 video_refresh();
2140 next += 16667;
2141 int64 delay = next - GetTicks_usec();
2142 if (delay > 0)
2143 Delay_usec(delay);
2144 else if (delay < -16667)
2145 next = GetTicks_usec();
2146 ticks++;
2147 }
2148 uint64 end = GetTicks_usec();
2149 printf("%Ld ticks in %Ld usec = %Ld ticks/sec\n", ticks, end - start, (end - start) / ticks);
2150 return NULL;
2151 }
2152 #endif