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