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