ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/video_x.cpp
Revision: 1.28
Committed: 2000-11-03T12:23:49Z (24 years, 1 month ago) by cebix
Branch: MAIN
Changes since 1.27: +9 -24 lines
Log Message:
removed unnecessary window attributes

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