1 |
/* |
2 |
* video_x.cpp - Video/graphics emulation, X11 specific stuff |
3 |
* |
4 |
* Basilisk II (C) 1997-1999 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 <pthread.h> |
38 |
#include <errno.h> |
39 |
|
40 |
#include "cpu_emulation.h" |
41 |
#include "main.h" |
42 |
#include "adb.h" |
43 |
#include "macos_util.h" |
44 |
#include "prefs.h" |
45 |
#include "user_strings.h" |
46 |
#include "video.h" |
47 |
|
48 |
#define DEBUG 1 |
49 |
#include "debug.h" |
50 |
|
51 |
#if ENABLE_DGA |
52 |
#include <X11/extensions/xf86dga.h> |
53 |
#endif |
54 |
|
55 |
|
56 |
// Display types |
57 |
enum { |
58 |
DISPLAY_WINDOW, // X11 window, using MIT SHM extensions if possible |
59 |
DISPLAY_DGA // DGA fullscreen display |
60 |
}; |
61 |
|
62 |
|
63 |
// Constants |
64 |
const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes"; |
65 |
|
66 |
|
67 |
// Global variables |
68 |
static int32 frame_skip; |
69 |
static int display_type = DISPLAY_WINDOW; // See enum above |
70 |
static uint8 *the_buffer; // Mac frame buffer |
71 |
static bool redraw_thread_active = false; // Flag: Redraw thread installed |
72 |
static volatile bool redraw_thread_cancel = false; // Flag: Cancel Redraw thread |
73 |
static pthread_t redraw_thread; // Redraw thread |
74 |
|
75 |
static bool has_dga = false; // Flag: Video DGA capable |
76 |
|
77 |
static bool ctrl_down = false; // Flag: Ctrl key pressed |
78 |
static bool caps_on = false; // Flag: Caps Lock on |
79 |
static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread |
80 |
static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread |
81 |
static bool emul_suspended = false; // Flag: Emulator suspended |
82 |
|
83 |
static bool classic_mode = false; // Flag: Classic Mac video mode |
84 |
|
85 |
static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms |
86 |
static int keycode_table[256]; // X keycode -> Mac keycode translation table |
87 |
|
88 |
// X11 variables |
89 |
static int screen; // Screen number |
90 |
static int xdepth; // Depth of X screen |
91 |
static int depth; // Depth of Mac frame buffer |
92 |
static Window rootwin, the_win; // Root window and our window |
93 |
static XVisualInfo visualInfo; |
94 |
static Visual *vis; |
95 |
static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode |
96 |
static XColor black, white; |
97 |
static unsigned long black_pixel, white_pixel; |
98 |
static int eventmask; |
99 |
static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask; |
100 |
static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask; |
101 |
|
102 |
static pthread_mutex_t palette_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect palette |
103 |
static XColor palette[256]; // Color palette for 8-bit mode |
104 |
static bool palette_changed = false; // Flag: Palette changed, redraw thread must set new colors |
105 |
|
106 |
// Variables for window mode |
107 |
static GC the_gc; |
108 |
static XImage *img = NULL; |
109 |
static XShmSegmentInfo shminfo; |
110 |
static XImage *cursor_image, *cursor_mask_image; |
111 |
static Pixmap cursor_map, cursor_mask_map; |
112 |
static Cursor mac_cursor; |
113 |
static GC cursor_gc, cursor_mask_gc; |
114 |
static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer |
115 |
static uint8 the_cursor[64]; // Cursor image data |
116 |
static bool have_shm = false; // Flag: SHM extensions available |
117 |
|
118 |
// Variables for DGA mode |
119 |
static int current_dga_cmap; // Number (0 or 1) of currently installed DGA colormap |
120 |
static Window suspend_win; // "Suspend" window |
121 |
static void *fb_save = NULL; // Saved frame buffer for suspend |
122 |
|
123 |
static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect frame buffer |
124 |
|
125 |
|
126 |
// Prototypes |
127 |
static void *redraw_func(void *arg); |
128 |
static int event2keycode(XKeyEvent *ev); |
129 |
|
130 |
|
131 |
// From main_unix.cpp |
132 |
extern Display *x_display; |
133 |
|
134 |
// From sys_unix.cpp |
135 |
extern void SysMountFirstFloppy(void); |
136 |
|
137 |
|
138 |
/* |
139 |
* Initialization |
140 |
*/ |
141 |
|
142 |
// Set VideoMonitor according to video mode |
143 |
void set_video_monitor(int width, int height, int bytes_per_row, bool native_byte_order) |
144 |
{ |
145 |
int layout = FLAYOUT_DIRECT; |
146 |
switch (depth) { |
147 |
case 1: |
148 |
layout = FLAYOUT_DIRECT; |
149 |
VideoMonitor.mode = VMODE_1BIT; |
150 |
break; |
151 |
case 8: |
152 |
layout = FLAYOUT_DIRECT; |
153 |
VideoMonitor.mode = VMODE_8BIT; |
154 |
break; |
155 |
case 15: |
156 |
layout = FLAYOUT_HOST_555; |
157 |
VideoMonitor.mode = VMODE_16BIT; |
158 |
break; |
159 |
case 16: |
160 |
layout = FLAYOUT_HOST_565; |
161 |
VideoMonitor.mode = VMODE_16BIT; |
162 |
break; |
163 |
case 24: |
164 |
case 32: |
165 |
layout = FLAYOUT_HOST_888; |
166 |
VideoMonitor.mode = VMODE_32BIT; |
167 |
break; |
168 |
} |
169 |
VideoMonitor.x = width; |
170 |
VideoMonitor.y = height; |
171 |
VideoMonitor.bytes_per_row = bytes_per_row; |
172 |
if (native_byte_order) |
173 |
MacFrameLayout = layout; |
174 |
else |
175 |
MacFrameLayout = FLAYOUT_DIRECT; |
176 |
} |
177 |
|
178 |
// Trap SHM errors |
179 |
static bool shm_error = false; |
180 |
static int (*old_error_handler)(Display *, XErrorEvent *); |
181 |
|
182 |
static int error_handler(Display *d, XErrorEvent *e) |
183 |
{ |
184 |
if (e->error_code == BadAccess) { |
185 |
shm_error = true; |
186 |
return 0; |
187 |
} else |
188 |
return old_error_handler(d, e); |
189 |
} |
190 |
|
191 |
// Init window mode |
192 |
static bool init_window(int width, int height) |
193 |
{ |
194 |
// Set absolute mouse mode |
195 |
ADBSetRelMouseMode(false); |
196 |
|
197 |
// Read frame skip prefs |
198 |
frame_skip = PrefsFindInt32("frameskip"); |
199 |
if (frame_skip == 0) |
200 |
frame_skip = 1; |
201 |
|
202 |
// Create window |
203 |
XSetWindowAttributes wattr; |
204 |
wattr.event_mask = eventmask = win_eventmask; |
205 |
wattr.background_pixel = black_pixel; |
206 |
wattr.border_pixel = black_pixel; |
207 |
wattr.backing_store = NotUseful; |
208 |
|
209 |
XSync(x_display, false); |
210 |
the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
211 |
InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore, &wattr); |
212 |
XSync(x_display, false); |
213 |
XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); |
214 |
XMapRaised(x_display, the_win); |
215 |
XSync(x_display, false); |
216 |
|
217 |
// Set colormap |
218 |
if (depth == 8) { |
219 |
XSetWindowColormap(x_display, the_win, cmap[0]); |
220 |
XSetWMColormapWindows(x_display, the_win, &the_win, 1); |
221 |
} |
222 |
|
223 |
// Make window unresizable |
224 |
XSizeHints *hints; |
225 |
if ((hints = XAllocSizeHints()) != NULL) { |
226 |
hints->min_width = width; |
227 |
hints->max_width = width; |
228 |
hints->min_height = height; |
229 |
hints->max_height = height; |
230 |
hints->flags = PMinSize | PMaxSize; |
231 |
XSetWMNormalHints(x_display, the_win, hints); |
232 |
XFree((char *)hints); |
233 |
} |
234 |
|
235 |
// Try to create and attach SHM image |
236 |
have_shm = false; |
237 |
if (depth != 1 && XShmQueryExtension(x_display)) { |
238 |
|
239 |
// Create SHM image ("height + 2" for safety) |
240 |
img = XShmCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height); |
241 |
shminfo.shmid = shmget(IPC_PRIVATE, (height + 2) * img->bytes_per_line, IPC_CREAT | 0777); |
242 |
the_buffer = (uint8 *)shmat(shminfo.shmid, 0, 0); |
243 |
shminfo.shmaddr = img->data = (char *)the_buffer; |
244 |
shminfo.readOnly = False; |
245 |
|
246 |
// Try to attach SHM image, catching errors |
247 |
shm_error = false; |
248 |
old_error_handler = XSetErrorHandler(error_handler); |
249 |
XShmAttach(x_display, &shminfo); |
250 |
XSync(x_display, false); |
251 |
XSetErrorHandler(old_error_handler); |
252 |
if (shm_error) { |
253 |
shmdt(shminfo.shmaddr); |
254 |
XDestroyImage(img); |
255 |
shminfo.shmid = -1; |
256 |
} else { |
257 |
have_shm = true; |
258 |
shmctl(shminfo.shmid, IPC_RMID, 0); |
259 |
} |
260 |
} |
261 |
|
262 |
// Create normal X image if SHM doesn't work ("height + 2" for safety) |
263 |
if (!have_shm) { |
264 |
int bytes_per_row = width; |
265 |
switch (depth) { |
266 |
case 1: |
267 |
bytes_per_row /= 8; |
268 |
break; |
269 |
case 15: |
270 |
case 16: |
271 |
bytes_per_row *= 2; |
272 |
break; |
273 |
case 24: |
274 |
case 32: |
275 |
bytes_per_row *= 4; |
276 |
break; |
277 |
} |
278 |
the_buffer = (uint8 *)malloc((height + 2) * bytes_per_row); |
279 |
img = XCreateImage(x_display, vis, depth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer, width, height, 32, bytes_per_row); |
280 |
} |
281 |
|
282 |
// 1-Bit mode is big-endian |
283 |
if (depth == 1) { |
284 |
img->byte_order = MSBFirst; |
285 |
img->bitmap_bit_order = MSBFirst; |
286 |
} |
287 |
|
288 |
// Allocate memory for frame buffer copy |
289 |
the_buffer_copy = (uint8 *)malloc((height + 2) * img->bytes_per_line); |
290 |
|
291 |
// Create GC |
292 |
the_gc = XCreateGC(x_display, the_win, 0, 0); |
293 |
XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes); |
294 |
|
295 |
// Create cursor |
296 |
cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor, 16, 16, 16, 2); |
297 |
cursor_image->byte_order = MSBFirst; |
298 |
cursor_image->bitmap_bit_order = MSBFirst; |
299 |
cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)the_cursor+32, 16, 16, 16, 2); |
300 |
cursor_mask_image->byte_order = MSBFirst; |
301 |
cursor_mask_image->bitmap_bit_order = MSBFirst; |
302 |
cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1); |
303 |
cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1); |
304 |
cursor_gc = XCreateGC(x_display, cursor_map, 0, 0); |
305 |
cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0); |
306 |
mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0); |
307 |
|
308 |
// Set VideoMonitor |
309 |
#ifdef WORDS_BIGENDIAN |
310 |
set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == MSBFirst); |
311 |
#else |
312 |
set_video_monitor(width, height, img->bytes_per_line, img->bitmap_bit_order == LSBFirst); |
313 |
#endif |
314 |
#if REAL_ADDRESSING |
315 |
VideoMonitor.mac_frame_base = (uint32)the_buffer; |
316 |
MacFrameLayout = FLAYOUT_DIRECT; |
317 |
#else |
318 |
VideoMonitor.mac_frame_base = MacFrameBaseMac; |
319 |
#endif |
320 |
return true; |
321 |
} |
322 |
|
323 |
// Init DGA display |
324 |
static bool init_dga(int width, int height) |
325 |
{ |
326 |
#if ENABLE_DGA |
327 |
// Set relative mouse mode |
328 |
ADBSetRelMouseMode(true); |
329 |
|
330 |
// Create window |
331 |
XSetWindowAttributes wattr; |
332 |
wattr.event_mask = eventmask = dga_eventmask; |
333 |
wattr.border_pixel = black_pixel; |
334 |
wattr.override_redirect = True; |
335 |
|
336 |
XSync(x_display, false); |
337 |
the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
338 |
InputOutput, vis, CWEventMask | CWBorderPixel | CWOverrideRedirect, &wattr); |
339 |
XSync(x_display, false); |
340 |
XStoreName(x_display, the_win, GetString(STR_WINDOW_TITLE)); |
341 |
XMapRaised(x_display, the_win); |
342 |
XSync(x_display, false); |
343 |
|
344 |
// Establish direct screen connection |
345 |
XMoveResizeWindow(x_display, the_win, 0, 0, width, height); |
346 |
XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); |
347 |
XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime); |
348 |
XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); |
349 |
|
350 |
int v_width, v_bank, v_size; |
351 |
XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size); |
352 |
XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse); |
353 |
XF86DGASetViewPort(x_display, screen, 0, 0); |
354 |
XF86DGASetVidPage(x_display, screen, 0); |
355 |
|
356 |
// Set colormap |
357 |
if (depth == 8) { |
358 |
XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]); |
359 |
XSetWMColormapWindows(x_display, the_win, &the_win, 1); |
360 |
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
361 |
} |
362 |
|
363 |
// Set VideoMonitor |
364 |
int bytes_per_row = (v_width + 7) & ~7; |
365 |
switch (depth) { |
366 |
case 1: |
367 |
bytes_per_row /= 8; |
368 |
break; |
369 |
case 15: |
370 |
case 16: |
371 |
bytes_per_row *= 2; |
372 |
break; |
373 |
case 24: |
374 |
case 32: |
375 |
bytes_per_row *= 4; |
376 |
break; |
377 |
} |
378 |
set_video_monitor(width, height, bytes_per_row, true); |
379 |
#if REAL_ADDRESSING |
380 |
VideoMonitor.mac_frame_base = (uint32)the_buffer; |
381 |
MacFrameLayout = FLAYOUT_DIRECT; |
382 |
#else |
383 |
VideoMonitor.mac_frame_base = MacFrameBaseMac; |
384 |
#endif |
385 |
return true; |
386 |
#else |
387 |
ErrorAlert("Basilisk II has been compiled with DGA support disabled."); |
388 |
return false; |
389 |
#endif |
390 |
} |
391 |
|
392 |
// Init keycode translation table |
393 |
static void keycode_init(void) |
394 |
{ |
395 |
bool use_kc = PrefsFindBool("keycodes"); |
396 |
if (use_kc) { |
397 |
|
398 |
// Get keycode file path from preferences |
399 |
const char *kc_path = PrefsFindString("keycodefile"); |
400 |
|
401 |
// Open keycode table |
402 |
FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r"); |
403 |
if (f == NULL) { |
404 |
char str[256]; |
405 |
sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno)); |
406 |
WarningAlert(str); |
407 |
return; |
408 |
} |
409 |
|
410 |
// Default translation table |
411 |
for (int i=0; i<256; i++) |
412 |
keycode_table[i] = -1; |
413 |
|
414 |
// Search for server vendor string, then read keycodes |
415 |
const char *vendor = ServerVendor(x_display); |
416 |
bool vendor_found = false; |
417 |
char line[256]; |
418 |
while (fgets(line, 255, f)) { |
419 |
// Read line |
420 |
int len = strlen(line); |
421 |
if (len == 0) |
422 |
continue; |
423 |
line[len-1] = 0; |
424 |
|
425 |
// Comments begin with "#" or ";" |
426 |
if (line[0] == '#' || line[0] == ';' || line[0] == 0) |
427 |
continue; |
428 |
|
429 |
if (vendor_found) { |
430 |
// Read keycode |
431 |
int x_code, mac_code; |
432 |
if (sscanf(line, "%d %d", &x_code, &mac_code) == 2) |
433 |
keycode_table[x_code & 0xff] = mac_code; |
434 |
else |
435 |
break; |
436 |
} else { |
437 |
// Search for vendor string |
438 |
if (strstr(vendor, line) == vendor) |
439 |
vendor_found = true; |
440 |
} |
441 |
} |
442 |
|
443 |
// Keycode file completely read |
444 |
fclose(f); |
445 |
use_keycodes = vendor_found; |
446 |
|
447 |
// Vendor not found? Then display warning |
448 |
if (!vendor_found) { |
449 |
char str[256]; |
450 |
sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME); |
451 |
WarningAlert(str); |
452 |
return; |
453 |
} |
454 |
} |
455 |
} |
456 |
|
457 |
bool VideoInit(bool classic) |
458 |
{ |
459 |
// Init keycode translation |
460 |
keycode_init(); |
461 |
|
462 |
// Find screen and root window |
463 |
screen = XDefaultScreen(x_display); |
464 |
rootwin = XRootWindow(x_display, screen); |
465 |
|
466 |
// Get screen depth |
467 |
xdepth = DefaultDepth(x_display, screen); |
468 |
|
469 |
#if ENABLE_DGA |
470 |
// DGA available? |
471 |
int event_base, error_base; |
472 |
if (XF86DGAQueryExtension(x_display, &event_base, &error_base)) { |
473 |
int dga_flags = 0; |
474 |
XF86DGAQueryDirectVideo(x_display, screen, &dga_flags); |
475 |
has_dga = dga_flags & XF86DGADirectPresent; |
476 |
} else |
477 |
has_dga = false; |
478 |
#endif |
479 |
|
480 |
// Find black and white colors |
481 |
XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black); |
482 |
XAllocColor(x_display, DefaultColormap(x_display, screen), &black); |
483 |
XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white); |
484 |
XAllocColor(x_display, DefaultColormap(x_display, screen), &white); |
485 |
black_pixel = BlackPixel(x_display, screen); |
486 |
white_pixel = WhitePixel(x_display, screen); |
487 |
|
488 |
// Get appropriate visual |
489 |
int color_class; |
490 |
switch (xdepth) { |
491 |
case 1: |
492 |
color_class = StaticGray; |
493 |
break; |
494 |
case 8: |
495 |
color_class = PseudoColor; |
496 |
break; |
497 |
case 15: |
498 |
case 16: |
499 |
case 24: |
500 |
case 32: |
501 |
color_class = TrueColor; |
502 |
break; |
503 |
default: |
504 |
ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR)); |
505 |
return false; |
506 |
} |
507 |
if (!XMatchVisualInfo(x_display, screen, xdepth, color_class, &visualInfo)) { |
508 |
ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); |
509 |
return false; |
510 |
} |
511 |
if (visualInfo.depth != xdepth) { |
512 |
ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); |
513 |
return false; |
514 |
} |
515 |
vis = visualInfo.visual; |
516 |
|
517 |
// Mac screen depth is always 1 bit in Classic mode, but follows X depth otherwise |
518 |
classic_mode = classic; |
519 |
if (classic) |
520 |
depth = 1; |
521 |
else |
522 |
depth = xdepth; |
523 |
|
524 |
// Create color maps for 8 bit mode |
525 |
if (depth == 8) { |
526 |
cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
527 |
cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
528 |
XInstallColormap(x_display, cmap[0]); |
529 |
XInstallColormap(x_display, cmap[1]); |
530 |
} |
531 |
|
532 |
// Get screen mode from preferences |
533 |
const char *mode_str; |
534 |
if (classic) |
535 |
mode_str = "win/512/342"; |
536 |
else |
537 |
mode_str = PrefsFindString("screen"); |
538 |
|
539 |
// Determine type and mode |
540 |
int width = 512, height = 384; |
541 |
display_type = DISPLAY_WINDOW; |
542 |
if (mode_str) { |
543 |
if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2) |
544 |
display_type = DISPLAY_WINDOW; |
545 |
else if (has_dga && sscanf(mode_str, "dga/%d/%d", &width, &height) == 2) { |
546 |
display_type = DISPLAY_DGA; |
547 |
if (width > DisplayWidth(x_display, screen)) |
548 |
width = DisplayWidth(x_display, screen); |
549 |
if (height > DisplayHeight(x_display, screen)) |
550 |
height = DisplayHeight(x_display, screen); |
551 |
} |
552 |
if (width <= 0) |
553 |
width = DisplayWidth(x_display, screen); |
554 |
if (height <= 0) |
555 |
height = DisplayHeight(x_display, screen); |
556 |
} |
557 |
|
558 |
// Initialize according to display type |
559 |
switch (display_type) { |
560 |
case DISPLAY_WINDOW: |
561 |
if (!init_window(width, height)) |
562 |
return false; |
563 |
break; |
564 |
case DISPLAY_DGA: |
565 |
if (!init_dga(width, height)) |
566 |
return false; |
567 |
break; |
568 |
} |
569 |
|
570 |
// Lock down frame buffer |
571 |
pthread_mutex_lock(&frame_buffer_lock); |
572 |
|
573 |
#if !REAL_ADDRESSING |
574 |
// Set variables for UAE memory mapping |
575 |
MacFrameBaseHost = the_buffer; |
576 |
MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y; |
577 |
|
578 |
// No special frame buffer in Classic mode (frame buffer is in Mac RAM) |
579 |
if (classic) |
580 |
MacFrameLayout = FLAYOUT_NONE; |
581 |
#endif |
582 |
|
583 |
// Start redraw/input thread |
584 |
XSync(x_display, false); |
585 |
redraw_thread_active = (pthread_create(&redraw_thread, NULL, redraw_func, NULL) == 0); |
586 |
if (!redraw_thread_active) |
587 |
printf("FATAL: cannot create redraw thread\n"); |
588 |
return redraw_thread_active; |
589 |
} |
590 |
|
591 |
|
592 |
/* |
593 |
* Deinitialization |
594 |
*/ |
595 |
|
596 |
void VideoExit(void) |
597 |
{ |
598 |
// Stop redraw thread |
599 |
if (redraw_thread_active) { |
600 |
redraw_thread_cancel = true; |
601 |
#ifdef HAVE_PTHREAD_CANCEL |
602 |
pthread_cancel(redraw_thread); |
603 |
#endif |
604 |
pthread_join(redraw_thread, NULL); |
605 |
redraw_thread_active = false; |
606 |
} |
607 |
|
608 |
// Unlock frame buffer |
609 |
pthread_mutex_unlock(&frame_buffer_lock); |
610 |
|
611 |
// Close window and server connection |
612 |
if (x_display != NULL) { |
613 |
XSync(x_display, false); |
614 |
|
615 |
#if ENABLE_DGA |
616 |
if (display_type == DISPLAY_DGA) { |
617 |
XF86DGADirectVideo(x_display, screen, 0); |
618 |
XUngrabPointer(x_display, CurrentTime); |
619 |
XUngrabKeyboard(x_display, CurrentTime); |
620 |
} |
621 |
#endif |
622 |
|
623 |
if (the_buffer_copy) { |
624 |
free(the_buffer_copy); |
625 |
the_buffer_copy = NULL; |
626 |
} |
627 |
|
628 |
XFlush(x_display); |
629 |
XSync(x_display, false); |
630 |
if (depth == 8) { |
631 |
XFreeColormap(x_display, cmap[0]); |
632 |
XFreeColormap(x_display, cmap[1]); |
633 |
} |
634 |
} |
635 |
} |
636 |
|
637 |
|
638 |
/* |
639 |
* Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode) |
640 |
*/ |
641 |
|
642 |
void VideoQuitFullScreen(void) |
643 |
{ |
644 |
D(bug("VideoQuitFullScreen()\n")); |
645 |
if (display_type == DISPLAY_DGA) |
646 |
quit_full_screen = true; |
647 |
} |
648 |
|
649 |
|
650 |
/* |
651 |
* Mac VBL interrupt |
652 |
*/ |
653 |
|
654 |
void VideoInterrupt(void) |
655 |
{ |
656 |
// Emergency quit requested? Then quit |
657 |
if (emerg_quit) |
658 |
QuitEmulator(); |
659 |
|
660 |
// Temporarily give up frame buffer lock (this is the point where |
661 |
// we are suspended when the user presses Ctrl-Tab) |
662 |
pthread_mutex_unlock(&frame_buffer_lock); |
663 |
pthread_mutex_lock(&frame_buffer_lock); |
664 |
} |
665 |
|
666 |
|
667 |
/* |
668 |
* Set palette |
669 |
*/ |
670 |
|
671 |
void video_set_palette(uint8 *pal) |
672 |
{ |
673 |
pthread_mutex_lock(&palette_lock); |
674 |
|
675 |
// Convert colors to XColor array |
676 |
for (int i=0; i<256; i++) { |
677 |
palette[i].pixel = i; |
678 |
palette[i].red = pal[i*3] * 0x0101; |
679 |
palette[i].green = pal[i*3+1] * 0x0101; |
680 |
palette[i].blue = pal[i*3+2] * 0x0101; |
681 |
palette[i].flags = DoRed | DoGreen | DoBlue; |
682 |
} |
683 |
|
684 |
// Tell redraw thread to change palette |
685 |
palette_changed = true; |
686 |
|
687 |
pthread_mutex_unlock(&palette_lock); |
688 |
} |
689 |
|
690 |
|
691 |
/* |
692 |
* Suspend/resume emulator |
693 |
*/ |
694 |
|
695 |
#if ENABLE_DGA |
696 |
static void suspend_emul(void) |
697 |
{ |
698 |
if (display_type == DISPLAY_DGA) { |
699 |
// Release ctrl key |
700 |
ADBKeyUp(0x36); |
701 |
ctrl_down = false; |
702 |
|
703 |
// Lock frame buffer (this will stop the MacOS thread) |
704 |
pthread_mutex_lock(&frame_buffer_lock); |
705 |
|
706 |
// Save frame buffer |
707 |
fb_save = malloc(VideoMonitor.y * VideoMonitor.bytes_per_row); |
708 |
if (fb_save) |
709 |
memcpy(fb_save, the_buffer, VideoMonitor.y * VideoMonitor.bytes_per_row); |
710 |
|
711 |
// Close full screen display |
712 |
XF86DGADirectVideo(x_display, screen, 0); |
713 |
XUngrabPointer(x_display, CurrentTime); |
714 |
XUngrabKeyboard(x_display, CurrentTime); |
715 |
XUnmapWindow(x_display, the_win); |
716 |
XSync(x_display, false); |
717 |
|
718 |
// Open "suspend" window |
719 |
XSetWindowAttributes wattr; |
720 |
wattr.event_mask = KeyPressMask; |
721 |
wattr.background_pixel = black_pixel; |
722 |
wattr.border_pixel = black_pixel; |
723 |
wattr.backing_store = Always; |
724 |
wattr.backing_planes = xdepth; |
725 |
wattr.colormap = DefaultColormap(x_display, screen); |
726 |
XSync(x_display, false); |
727 |
suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth, |
728 |
InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | |
729 |
CWBackingStore | CWBackingPlanes | (xdepth == 8 ? CWColormap : 0), &wattr); |
730 |
XSync(x_display, false); |
731 |
XStoreName(x_display, suspend_win, GetString(STR_SUSPEND_WINDOW_TITLE)); |
732 |
XMapRaised(x_display, suspend_win); |
733 |
XSync(x_display, false); |
734 |
emul_suspended = true; |
735 |
} |
736 |
} |
737 |
|
738 |
static void resume_emul(void) |
739 |
{ |
740 |
// Close "suspend" window |
741 |
XDestroyWindow(x_display, suspend_win); |
742 |
XSync(x_display, false); |
743 |
|
744 |
// Reopen full screen display |
745 |
XMapRaised(x_display, the_win); |
746 |
XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); |
747 |
XSync(x_display, false); |
748 |
XGrabKeyboard(x_display, rootwin, 1, GrabModeAsync, GrabModeAsync, CurrentTime); |
749 |
XGrabPointer(x_display, rootwin, 1, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); |
750 |
XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse); |
751 |
XF86DGASetViewPort(x_display, screen, 0, 0); |
752 |
XSync(x_display, false); |
753 |
|
754 |
// Restore frame buffer |
755 |
if (fb_save) { |
756 |
memcpy(the_buffer, fb_save, VideoMonitor.y * VideoMonitor.bytes_per_row); |
757 |
free(fb_save); |
758 |
fb_save = NULL; |
759 |
} |
760 |
if (depth == 8) |
761 |
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
762 |
|
763 |
// Unlock frame buffer (and continue MacOS thread) |
764 |
pthread_mutex_unlock(&frame_buffer_lock); |
765 |
emul_suspended = false; |
766 |
} |
767 |
#endif |
768 |
|
769 |
|
770 |
/* |
771 |
* Translate key event to Mac keycode |
772 |
*/ |
773 |
|
774 |
static int kc_decode(KeySym ks) |
775 |
{ |
776 |
switch (ks) { |
777 |
case XK_A: case XK_a: return 0x00; |
778 |
case XK_B: case XK_b: return 0x0b; |
779 |
case XK_C: case XK_c: return 0x08; |
780 |
case XK_D: case XK_d: return 0x02; |
781 |
case XK_E: case XK_e: return 0x0e; |
782 |
case XK_F: case XK_f: return 0x03; |
783 |
case XK_G: case XK_g: return 0x05; |
784 |
case XK_H: case XK_h: return 0x04; |
785 |
case XK_I: case XK_i: return 0x22; |
786 |
case XK_J: case XK_j: return 0x26; |
787 |
case XK_K: case XK_k: return 0x28; |
788 |
case XK_L: case XK_l: return 0x25; |
789 |
case XK_M: case XK_m: return 0x2e; |
790 |
case XK_N: case XK_n: return 0x2d; |
791 |
case XK_O: case XK_o: return 0x1f; |
792 |
case XK_P: case XK_p: return 0x23; |
793 |
case XK_Q: case XK_q: return 0x0c; |
794 |
case XK_R: case XK_r: return 0x0f; |
795 |
case XK_S: case XK_s: return 0x01; |
796 |
case XK_T: case XK_t: return 0x11; |
797 |
case XK_U: case XK_u: return 0x20; |
798 |
case XK_V: case XK_v: return 0x09; |
799 |
case XK_W: case XK_w: return 0x0d; |
800 |
case XK_X: case XK_x: return 0x07; |
801 |
case XK_Y: case XK_y: return 0x10; |
802 |
case XK_Z: case XK_z: return 0x06; |
803 |
|
804 |
case XK_1: case XK_exclam: return 0x12; |
805 |
case XK_2: case XK_at: return 0x13; |
806 |
case XK_3: case XK_numbersign: return 0x14; |
807 |
case XK_4: case XK_dollar: return 0x15; |
808 |
case XK_5: case XK_percent: return 0x17; |
809 |
case XK_6: return 0x16; |
810 |
case XK_7: return 0x1a; |
811 |
case XK_8: return 0x1c; |
812 |
case XK_9: return 0x19; |
813 |
case XK_0: return 0x1d; |
814 |
|
815 |
case XK_grave: case XK_asciitilde: return 0x0a; |
816 |
case XK_minus: case XK_underscore: return 0x1b; |
817 |
case XK_equal: case XK_plus: return 0x18; |
818 |
case XK_bracketleft: case XK_braceleft: return 0x21; |
819 |
case XK_bracketright: case XK_braceright: return 0x1e; |
820 |
case XK_backslash: case XK_bar: return 0x2a; |
821 |
case XK_semicolon: case XK_colon: return 0x29; |
822 |
case XK_apostrophe: case XK_quotedbl: return 0x27; |
823 |
case XK_comma: case XK_less: return 0x2b; |
824 |
case XK_period: case XK_greater: return 0x2f; |
825 |
case XK_slash: case XK_question: return 0x2c; |
826 |
|
827 |
#if ENABLE_DGA |
828 |
case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30; |
829 |
#else |
830 |
case XK_Tab: return 0x30; |
831 |
#endif |
832 |
case XK_Return: return 0x24; |
833 |
case XK_space: return 0x31; |
834 |
case XK_BackSpace: return 0x33; |
835 |
|
836 |
case XK_Delete: return 0x75; |
837 |
case XK_Insert: return 0x72; |
838 |
case XK_Home: case XK_Help: return 0x73; |
839 |
case XK_End: return 0x77; |
840 |
#ifdef __hpux |
841 |
case XK_Prior: return 0x74; |
842 |
case XK_Next: return 0x79; |
843 |
#else |
844 |
case XK_Page_Up: return 0x74; |
845 |
case XK_Page_Down: return 0x79; |
846 |
#endif |
847 |
|
848 |
case XK_Control_L: return 0x36; |
849 |
case XK_Control_R: return 0x36; |
850 |
case XK_Shift_L: return 0x38; |
851 |
case XK_Shift_R: return 0x38; |
852 |
case XK_Alt_L: return 0x37; |
853 |
case XK_Alt_R: return 0x37; |
854 |
case XK_Meta_L: return 0x3a; |
855 |
case XK_Meta_R: return 0x3a; |
856 |
case XK_Menu: return 0x32; |
857 |
case XK_Caps_Lock: return 0x39; |
858 |
case XK_Num_Lock: return 0x47; |
859 |
|
860 |
case XK_Up: return 0x3e; |
861 |
case XK_Down: return 0x3d; |
862 |
case XK_Left: return 0x3b; |
863 |
case XK_Right: return 0x3c; |
864 |
|
865 |
case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35; |
866 |
|
867 |
case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a; |
868 |
case XK_F2: return 0x78; |
869 |
case XK_F3: return 0x63; |
870 |
case XK_F4: return 0x76; |
871 |
case XK_F5: return 0x60; |
872 |
case XK_F6: return 0x61; |
873 |
case XK_F7: return 0x62; |
874 |
case XK_F8: return 0x64; |
875 |
case XK_F9: return 0x65; |
876 |
case XK_F10: return 0x6d; |
877 |
case XK_F11: return 0x67; |
878 |
case XK_F12: return 0x6f; |
879 |
|
880 |
case XK_Print: return 0x69; |
881 |
case XK_Scroll_Lock: return 0x6b; |
882 |
case XK_Pause: return 0x71; |
883 |
|
884 |
#if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End) |
885 |
case XK_KP_0: case XK_KP_Insert: return 0x52; |
886 |
case XK_KP_1: case XK_KP_End: return 0x53; |
887 |
case XK_KP_2: case XK_KP_Down: return 0x54; |
888 |
case XK_KP_3: case XK_KP_Next: return 0x55; |
889 |
case XK_KP_4: case XK_KP_Left: return 0x56; |
890 |
case XK_KP_5: case XK_KP_Begin: return 0x57; |
891 |
case XK_KP_6: case XK_KP_Right: return 0x58; |
892 |
case XK_KP_7: case XK_KP_Home: return 0x59; |
893 |
case XK_KP_8: case XK_KP_Up: return 0x5b; |
894 |
case XK_KP_9: case XK_KP_Prior: return 0x5c; |
895 |
case XK_KP_Decimal: case XK_KP_Delete: return 0x41; |
896 |
#else |
897 |
case XK_KP_0: return 0x52; |
898 |
case XK_KP_1: return 0x53; |
899 |
case XK_KP_2: return 0x54; |
900 |
case XK_KP_3: return 0x55; |
901 |
case XK_KP_4: return 0x56; |
902 |
case XK_KP_5: return 0x57; |
903 |
case XK_KP_6: return 0x58; |
904 |
case XK_KP_7: return 0x59; |
905 |
case XK_KP_8: return 0x5b; |
906 |
case XK_KP_9: return 0x5c; |
907 |
case XK_KP_Decimal: return 0x41; |
908 |
#endif |
909 |
case XK_KP_Add: return 0x45; |
910 |
case XK_KP_Subtract: return 0x4e; |
911 |
case XK_KP_Multiply: return 0x43; |
912 |
case XK_KP_Divide: return 0x4b; |
913 |
case XK_KP_Enter: return 0x4c; |
914 |
case XK_KP_Equal: return 0x51; |
915 |
} |
916 |
return -1; |
917 |
} |
918 |
|
919 |
static int event2keycode(XKeyEvent *ev) |
920 |
{ |
921 |
KeySym ks; |
922 |
int as; |
923 |
int i = 0; |
924 |
|
925 |
do { |
926 |
ks = XLookupKeysym(ev, i++); |
927 |
as = kc_decode(ks); |
928 |
if (as != -1) |
929 |
return as; |
930 |
} while (ks != NoSymbol); |
931 |
|
932 |
return -1; |
933 |
} |
934 |
|
935 |
|
936 |
/* |
937 |
* X event handling |
938 |
*/ |
939 |
|
940 |
static void handle_events(void) |
941 |
{ |
942 |
XEvent event; |
943 |
for (;;) { |
944 |
if (!XCheckMaskEvent(x_display, eventmask, &event)) |
945 |
break; |
946 |
|
947 |
switch (event.type) { |
948 |
// Mouse button |
949 |
case ButtonPress: { |
950 |
unsigned int button = ((XButtonEvent *)&event)->button; |
951 |
if (button < 4) |
952 |
ADBMouseDown(button - 1); |
953 |
break; |
954 |
} |
955 |
case ButtonRelease: { |
956 |
unsigned int button = ((XButtonEvent *)&event)->button; |
957 |
if (button < 4) |
958 |
ADBMouseUp(button - 1); |
959 |
break; |
960 |
} |
961 |
|
962 |
// Mouse moved |
963 |
case EnterNotify: |
964 |
ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); |
965 |
break; |
966 |
case MotionNotify: |
967 |
ADBMouseMoved(((XMotionEvent *)&event)->x, ((XMotionEvent *)&event)->y); |
968 |
break; |
969 |
|
970 |
// Keyboard |
971 |
case KeyPress: { |
972 |
int code; |
973 |
if (use_keycodes) { |
974 |
event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys |
975 |
code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff]; |
976 |
} else |
977 |
code = event2keycode((XKeyEvent *)&event); |
978 |
if (code != -1) { |
979 |
if (!emul_suspended) { |
980 |
if (code == 0x39) { // Caps Lock pressed |
981 |
if (caps_on) { |
982 |
ADBKeyUp(code); |
983 |
caps_on = false; |
984 |
} else { |
985 |
ADBKeyDown(code); |
986 |
caps_on = true; |
987 |
} |
988 |
} else |
989 |
ADBKeyDown(code); |
990 |
if (code == 0x36) |
991 |
ctrl_down = true; |
992 |
} else { |
993 |
#if ENABLE_DGA |
994 |
if (code == 0x31) |
995 |
resume_emul(); // Space wakes us up |
996 |
#endif |
997 |
} |
998 |
} |
999 |
break; |
1000 |
} |
1001 |
case KeyRelease: { |
1002 |
int code; |
1003 |
if (use_keycodes) { |
1004 |
event2keycode((XKeyEvent *)&event); // This is called to process the hotkeys |
1005 |
code = keycode_table[((XKeyEvent *)&event)->keycode & 0xff]; |
1006 |
} else |
1007 |
code = event2keycode((XKeyEvent *)&event); |
1008 |
if (code != -1 && code != 0x39) { // Don't propagate Caps Lock releases |
1009 |
ADBKeyUp(code); |
1010 |
if (code == 0x36) |
1011 |
ctrl_down = false; |
1012 |
} |
1013 |
break; |
1014 |
} |
1015 |
|
1016 |
// Hidden parts exposed, force complete refresh of window |
1017 |
case Expose: |
1018 |
if (display_type == DISPLAY_WINDOW) |
1019 |
memset(the_buffer_copy, 0, VideoMonitor.bytes_per_row * VideoMonitor.y); |
1020 |
break; |
1021 |
} |
1022 |
} |
1023 |
} |
1024 |
|
1025 |
|
1026 |
/* |
1027 |
* Window display update |
1028 |
*/ |
1029 |
|
1030 |
static void update_display(void) |
1031 |
{ |
1032 |
// In classic mode, copy the frame buffer from Mac RAM |
1033 |
if (classic_mode) |
1034 |
memcpy(the_buffer, Mac2HostAddr(0x3fa700), VideoMonitor.bytes_per_row * VideoMonitor.y); |
1035 |
|
1036 |
// Incremental update code |
1037 |
int wide = 0, high = 0, x1, x2, y1, y2, i, j; |
1038 |
int bytes_per_row = VideoMonitor.bytes_per_row; |
1039 |
int bytes_per_pixel = VideoMonitor.bytes_per_row / VideoMonitor.x; |
1040 |
uint8 *p, *p2; |
1041 |
|
1042 |
// Check for first line from top and first line from bottom that have changed |
1043 |
y1 = 0; |
1044 |
for (j=0; j<VideoMonitor.y; j++) { |
1045 |
if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) { |
1046 |
y1 = j; |
1047 |
break; |
1048 |
} |
1049 |
} |
1050 |
y2 = y1 - 1; |
1051 |
for (j=VideoMonitor.y-1; j>=y1; j--) { |
1052 |
if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) { |
1053 |
y2 = j; |
1054 |
break; |
1055 |
} |
1056 |
} |
1057 |
high = y2 - y1 + 1; |
1058 |
|
1059 |
// Check for first column from left and first column from right that have changed |
1060 |
if (high) { |
1061 |
if (depth == 1) { |
1062 |
x1 = VideoMonitor.x; |
1063 |
for (j=y1; j<=y2; j++) { |
1064 |
p = &the_buffer[j * bytes_per_row]; |
1065 |
p2 = &the_buffer_copy[j * bytes_per_row]; |
1066 |
for (i=0; i<(x1>>3); i++) { |
1067 |
if (*p != *p2) { |
1068 |
x1 = i << 3; |
1069 |
break; |
1070 |
} |
1071 |
p++; |
1072 |
p2++; |
1073 |
} |
1074 |
} |
1075 |
x2 = x1; |
1076 |
for (j=y1; j<=y2; j++) { |
1077 |
p = &the_buffer[j * bytes_per_row]; |
1078 |
p2 = &the_buffer_copy[j * bytes_per_row]; |
1079 |
p += bytes_per_row; |
1080 |
p2 += bytes_per_row; |
1081 |
for (i=(VideoMonitor.x>>3); i>(x2>>3); i--) { |
1082 |
p--; |
1083 |
p2--; |
1084 |
if (*p != *p2) { |
1085 |
x2 = i << 3; |
1086 |
break; |
1087 |
} |
1088 |
} |
1089 |
} |
1090 |
wide = x2 - x1; |
1091 |
|
1092 |
// Update copy of the_buffer |
1093 |
if (high && wide) { |
1094 |
for (j=y1; j<=y2; j++) { |
1095 |
i = j * bytes_per_row + (x1 >> 3); |
1096 |
memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3); |
1097 |
} |
1098 |
} |
1099 |
|
1100 |
} else { |
1101 |
x1 = VideoMonitor.x; |
1102 |
for (j=y1; j<=y2; j++) { |
1103 |
p = &the_buffer[j * bytes_per_row]; |
1104 |
p2 = &the_buffer_copy[j * bytes_per_row]; |
1105 |
for (i=0; i<x1; i++) { |
1106 |
if (memcmp(p, p2, bytes_per_pixel)) { |
1107 |
x1 = i; |
1108 |
break; |
1109 |
} |
1110 |
p += bytes_per_pixel; |
1111 |
p2 += bytes_per_pixel; |
1112 |
} |
1113 |
} |
1114 |
x2 = x1; |
1115 |
for (j=y1; j<=y2; j++) { |
1116 |
p = &the_buffer[j * bytes_per_row]; |
1117 |
p2 = &the_buffer_copy[j * bytes_per_row]; |
1118 |
p += bytes_per_row; |
1119 |
p2 += bytes_per_row; |
1120 |
for (i=VideoMonitor.x; i>x2; i--) { |
1121 |
p -= bytes_per_pixel; |
1122 |
p2 -= bytes_per_pixel; |
1123 |
if (memcmp(p, p2, bytes_per_pixel)) { |
1124 |
x2 = i; |
1125 |
break; |
1126 |
} |
1127 |
} |
1128 |
} |
1129 |
wide = x2 - x1; |
1130 |
|
1131 |
// Update copy of the_buffer |
1132 |
if (high && wide) { |
1133 |
for (j=y1; j<=y2; j++) { |
1134 |
i = j * bytes_per_row + x1 * bytes_per_pixel; |
1135 |
memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide); |
1136 |
} |
1137 |
} |
1138 |
} |
1139 |
} |
1140 |
|
1141 |
// Refresh display |
1142 |
if (high && wide) { |
1143 |
if (have_shm) |
1144 |
XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0); |
1145 |
else |
1146 |
XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high); |
1147 |
} |
1148 |
|
1149 |
// Has the Mac started? (cursor data is not valid otherwise) |
1150 |
if (HasMacStarted()) { |
1151 |
|
1152 |
// Set new cursor image if it was changed |
1153 |
if (memcmp(the_cursor, Mac2HostAddr(0x844), 64)) { |
1154 |
memcpy(the_cursor, Mac2HostAddr(0x844), 64); |
1155 |
memcpy(cursor_image->data, the_cursor, 32); |
1156 |
memcpy(cursor_mask_image->data, the_cursor+32, 32); |
1157 |
XFreeCursor(x_display, mac_cursor); |
1158 |
XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16); |
1159 |
XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16); |
1160 |
mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, ReadMacInt8(0x885), ReadMacInt8(0x887)); |
1161 |
XDefineCursor(x_display, the_win, mac_cursor); |
1162 |
} |
1163 |
} |
1164 |
} |
1165 |
|
1166 |
|
1167 |
/* |
1168 |
* Thread for screen refresh, input handling etc. |
1169 |
*/ |
1170 |
|
1171 |
static void *redraw_func(void *arg) |
1172 |
{ |
1173 |
int tick_counter = 0; |
1174 |
|
1175 |
while (!redraw_thread_cancel) { |
1176 |
|
1177 |
// Wait |
1178 |
#ifdef HAVE_NANOSLEEP |
1179 |
struct timespec req = {0, 16666667}; |
1180 |
nanosleep(&req, NULL); |
1181 |
#else |
1182 |
usleep(16667); |
1183 |
#endif |
1184 |
|
1185 |
#if ENABLE_DGA |
1186 |
// Quit DGA mode if requested |
1187 |
if (quit_full_screen) { |
1188 |
quit_full_screen = false; |
1189 |
if (display_type == DISPLAY_DGA) { |
1190 |
XF86DGADirectVideo(x_display, screen, 0); |
1191 |
XUngrabPointer(x_display, CurrentTime); |
1192 |
XUngrabKeyboard(x_display, CurrentTime); |
1193 |
XUnmapWindow(x_display, the_win); |
1194 |
XSync(x_display, false); |
1195 |
} |
1196 |
} |
1197 |
#endif |
1198 |
|
1199 |
// Handle X events |
1200 |
handle_events(); |
1201 |
|
1202 |
// Handle palette changes |
1203 |
pthread_mutex_lock(&palette_lock); |
1204 |
if (palette_changed) { |
1205 |
palette_changed = false; |
1206 |
if (depth == 8) { |
1207 |
XStoreColors(x_display, cmap[0], palette, 256); |
1208 |
XStoreColors(x_display, cmap[1], palette, 256); |
1209 |
#if ENABLE_DGA |
1210 |
if (display_type == DISPLAY_DGA) { |
1211 |
current_dga_cmap ^= 1; |
1212 |
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
1213 |
} |
1214 |
#endif |
1215 |
} |
1216 |
} |
1217 |
pthread_mutex_unlock(&palette_lock); |
1218 |
|
1219 |
// In window mode, update display and mouse pointer |
1220 |
if (display_type == DISPLAY_WINDOW) { |
1221 |
tick_counter++; |
1222 |
if (tick_counter >= frame_skip) { |
1223 |
tick_counter = 0; |
1224 |
update_display(); |
1225 |
} |
1226 |
} |
1227 |
} |
1228 |
return NULL; |
1229 |
} |