1 |
/* |
2 |
* video_x.cpp - Video/graphics emulation, X11 specific stuff |
3 |
* |
4 |
* SheepShaver (C) 1997-2005 Marc Hellwig and 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 |
* Ctrl-F5 = grab mouse (in windowed mode) |
28 |
*/ |
29 |
|
30 |
#include "sysdeps.h" |
31 |
|
32 |
#include <X11/Xlib.h> |
33 |
#include <X11/Xutil.h> |
34 |
#include <X11/keysym.h> |
35 |
#include <X11/extensions/XShm.h> |
36 |
#include <sys/ipc.h> |
37 |
#include <sys/shm.h> |
38 |
#include <errno.h> |
39 |
#include <pthread.h> |
40 |
|
41 |
#include <algorithm> |
42 |
|
43 |
#ifdef ENABLE_FBDEV_DGA |
44 |
# include <linux/fb.h> |
45 |
# include <sys/ioctl.h> |
46 |
#endif |
47 |
|
48 |
#ifdef ENABLE_XF86_DGA |
49 |
# include <X11/extensions/xf86dga.h> |
50 |
#endif |
51 |
|
52 |
#ifdef ENABLE_XF86_VIDMODE |
53 |
# include <X11/extensions/xf86vmode.h> |
54 |
#endif |
55 |
|
56 |
#ifdef ENABLE_FBDEV_DGA |
57 |
# include <sys/mman.h> |
58 |
#endif |
59 |
|
60 |
#include "main.h" |
61 |
#include "adb.h" |
62 |
#include "prefs.h" |
63 |
#include "user_strings.h" |
64 |
#include "about_window.h" |
65 |
#include "video.h" |
66 |
#include "video_defs.h" |
67 |
#include "video_blit.h" |
68 |
|
69 |
#define DEBUG 0 |
70 |
#include "debug.h" |
71 |
|
72 |
#ifndef NO_STD_NAMESPACE |
73 |
using std::sort; |
74 |
#endif |
75 |
|
76 |
|
77 |
// Constants |
78 |
const char KEYCODE_FILE_NAME[] = DATADIR "/keycodes"; |
79 |
static const bool hw_mac_cursor_accl = true; // Flag: Enable MacOS to X11 copy of cursor? |
80 |
|
81 |
// Global variables |
82 |
static int32 frame_skip; |
83 |
static int16 mouse_wheel_mode; |
84 |
static int16 mouse_wheel_lines; |
85 |
static bool redraw_thread_active = false; // Flag: Redraw thread installed |
86 |
static pthread_attr_t redraw_thread_attr; // Redraw thread attributes |
87 |
static volatile bool redraw_thread_cancel; // Flag: Cancel Redraw thread |
88 |
static pthread_t redraw_thread; // Redraw thread |
89 |
|
90 |
static bool local_X11; // Flag: X server running on local machine? |
91 |
static volatile bool thread_stop_req = false; |
92 |
static volatile bool thread_stop_ack = false; // Acknowledge for thread_stop_req |
93 |
|
94 |
static bool has_dga = false; // Flag: Video DGA capable |
95 |
static bool has_vidmode = false; // Flag: VidMode extension available |
96 |
|
97 |
#ifdef ENABLE_VOSF |
98 |
static bool use_vosf = true; // Flag: VOSF enabled |
99 |
#else |
100 |
static const bool use_vosf = false; // VOSF not possible |
101 |
#endif |
102 |
|
103 |
static bool palette_changed = false; // Flag: Palette changed, redraw thread must update palette |
104 |
static bool ctrl_down = false; // Flag: Ctrl key pressed |
105 |
static bool caps_on = false; // Flag: Caps Lock on |
106 |
static bool quit_full_screen = false; // Flag: DGA close requested from redraw thread |
107 |
static volatile bool quit_full_screen_ack = false; // Acknowledge for quit_full_screen |
108 |
static bool emerg_quit = false; // Flag: Ctrl-Esc pressed, emergency quit requested from MacOS thread |
109 |
|
110 |
static bool emul_suspended = false; // Flag: emulator suspended |
111 |
static Window suspend_win; // "Suspend" window |
112 |
static void *fb_save = NULL; // Saved frame buffer for suspend |
113 |
static bool use_keycodes = false; // Flag: Use keycodes rather than keysyms |
114 |
static int keycode_table[256]; // X keycode -> Mac keycode translation table |
115 |
|
116 |
// X11 variables |
117 |
static int screen; // Screen number |
118 |
static int xdepth; // Depth of X screen |
119 |
static int depth; // Depth of Mac frame buffer |
120 |
static Window rootwin, the_win; // Root window and our window |
121 |
static int num_depths = 0; // Number of available X depths |
122 |
static int *avail_depths = NULL; // List of available X depths |
123 |
static VisualFormat visualFormat; |
124 |
static XVisualInfo visualInfo; |
125 |
static Visual *vis; |
126 |
static int color_class; |
127 |
static int rshift, rloss, gshift, gloss, bshift, bloss; // Pixel format of DirectColor/TrueColor modes |
128 |
static Colormap cmap[2]; // Two colormaps (DGA) for 8-bit mode |
129 |
static XColor x_palette[256]; // Color palette to be used as CLUT and gamma table |
130 |
static int orig_accel_numer, orig_accel_denom, orig_threshold; // Original mouse acceleration |
131 |
|
132 |
static XColor black, white; |
133 |
static unsigned long black_pixel, white_pixel; |
134 |
static int eventmask; |
135 |
static const int win_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | EnterWindowMask | ExposureMask | StructureNotifyMask; |
136 |
static const int dga_eventmask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask; |
137 |
|
138 |
// Variables for window mode |
139 |
static GC the_gc; |
140 |
static XImage *img = NULL; |
141 |
static XShmSegmentInfo shminfo; |
142 |
static XImage *cursor_image, *cursor_mask_image; |
143 |
static Pixmap cursor_map, cursor_mask_map; |
144 |
static Cursor mac_cursor; |
145 |
static GC cursor_gc, cursor_mask_gc; |
146 |
static bool cursor_changed = false; // Flag: Cursor changed, window_func must update cursor |
147 |
static bool have_shm = false; // Flag: SHM present and usable |
148 |
static uint8 *the_buffer = NULL; // Pointer to Mac frame buffer |
149 |
static uint8 *the_buffer_copy = NULL; // Copy of Mac frame buffer |
150 |
static uint32 the_buffer_size; // Size of allocated the_buffer |
151 |
|
152 |
// Variables for DGA mode |
153 |
static bool is_fbdev_dga_mode = false; // Flag: Use FBDev DGA mode? |
154 |
static int current_dga_cmap; |
155 |
|
156 |
#ifdef ENABLE_FBDEV_DGA |
157 |
static int fb_dev_fd = -1; // Handle to fb device name |
158 |
static struct fb_fix_screeninfo fb_finfo; // Fixed info |
159 |
static struct fb_var_screeninfo fb_vinfo; // Variable info |
160 |
static struct fb_var_screeninfo fb_orig_vinfo; // Variable info to restore later |
161 |
static struct fb_cmap fb_oldcmap; // Colormap to restore later |
162 |
#endif |
163 |
|
164 |
#ifdef ENABLE_XF86_VIDMODE |
165 |
// Variables for XF86 VidMode support |
166 |
static XF86VidModeModeInfo **x_video_modes; // Array of all available modes |
167 |
static int num_x_video_modes; |
168 |
#endif |
169 |
|
170 |
// Mutex to protect palette |
171 |
#ifdef HAVE_SPINLOCKS |
172 |
static spinlock_t x_palette_lock = SPIN_LOCK_UNLOCKED; |
173 |
#define LOCK_PALETTE spin_lock(&x_palette_lock) |
174 |
#define UNLOCK_PALETTE spin_unlock(&x_palette_lock) |
175 |
#elif defined(HAVE_PTHREADS) |
176 |
static pthread_mutex_t x_palette_lock = PTHREAD_MUTEX_INITIALIZER; |
177 |
#define LOCK_PALETTE pthread_mutex_lock(&x_palette_lock) |
178 |
#define UNLOCK_PALETTE pthread_mutex_unlock(&x_palette_lock) |
179 |
#else |
180 |
#define LOCK_PALETTE |
181 |
#define UNLOCK_PALETTE |
182 |
#endif |
183 |
|
184 |
// Mutex to protect frame buffer |
185 |
#ifdef HAVE_SPINLOCKS |
186 |
static spinlock_t frame_buffer_lock = SPIN_LOCK_UNLOCKED; |
187 |
#define LOCK_FRAME_BUFFER spin_lock(&frame_buffer_lock) |
188 |
#define UNLOCK_FRAME_BUFFER spin_unlock(&frame_buffer_lock) |
189 |
#elif defined(HAVE_PTHREADS) |
190 |
static pthread_mutex_t frame_buffer_lock = PTHREAD_MUTEX_INITIALIZER; |
191 |
#define LOCK_FRAME_BUFFER pthread_mutex_lock(&frame_buffer_lock); |
192 |
#define UNLOCK_FRAME_BUFFER pthread_mutex_unlock(&frame_buffer_lock); |
193 |
#else |
194 |
#define LOCK_FRAME_BUFFER |
195 |
#define UNLOCK_FRAME_BUFFER |
196 |
#endif |
197 |
|
198 |
|
199 |
// Prototypes |
200 |
static void *redraw_func(void *arg); |
201 |
|
202 |
|
203 |
// From main_unix.cpp |
204 |
extern char *x_display_name; |
205 |
extern Display *x_display; |
206 |
|
207 |
// From sys_unix.cpp |
208 |
extern void SysMountFirstFloppy(void); |
209 |
|
210 |
// From clip_unix.cpp |
211 |
extern void ClipboardSelectionClear(XSelectionClearEvent *); |
212 |
extern void ClipboardSelectionRequest(XSelectionRequestEvent *); |
213 |
|
214 |
|
215 |
// Video acceleration through SIGSEGV |
216 |
#ifdef ENABLE_VOSF |
217 |
# include "video_vosf.h" |
218 |
#endif |
219 |
|
220 |
|
221 |
/* |
222 |
* Utility functions |
223 |
*/ |
224 |
|
225 |
// Get current video mode |
226 |
static inline int get_current_mode(void) |
227 |
{ |
228 |
return VModes[cur_mode].viAppleMode; |
229 |
} |
230 |
|
231 |
// Find palette size for given color depth |
232 |
static int palette_size(int mode) |
233 |
{ |
234 |
switch (mode) { |
235 |
case APPLE_1_BIT: return 2; |
236 |
case APPLE_2_BIT: return 4; |
237 |
case APPLE_4_BIT: return 16; |
238 |
case APPLE_8_BIT: return 256; |
239 |
case APPLE_16_BIT: return 32; |
240 |
case APPLE_32_BIT: return 256; |
241 |
default: return 0; |
242 |
} |
243 |
} |
244 |
|
245 |
// Return bits per pixel for requested depth |
246 |
static inline int bytes_per_pixel(int depth) |
247 |
{ |
248 |
int bpp; |
249 |
switch (depth) { |
250 |
case 8: |
251 |
bpp = 1; |
252 |
break; |
253 |
case 15: case 16: |
254 |
bpp = 2; |
255 |
break; |
256 |
case 24: case 32: |
257 |
bpp = 4; |
258 |
break; |
259 |
default: |
260 |
abort(); |
261 |
} |
262 |
return bpp; |
263 |
} |
264 |
|
265 |
// Map video_mode depth ID to numerical depth value |
266 |
static inline int depth_of_video_mode(int mode) |
267 |
{ |
268 |
int depth; |
269 |
switch (mode) { |
270 |
case APPLE_1_BIT: |
271 |
depth = 1; |
272 |
break; |
273 |
case APPLE_2_BIT: |
274 |
depth = 2; |
275 |
break; |
276 |
case APPLE_4_BIT: |
277 |
depth = 4; |
278 |
break; |
279 |
case APPLE_8_BIT: |
280 |
depth = 8; |
281 |
break; |
282 |
case APPLE_16_BIT: |
283 |
depth = 16; |
284 |
break; |
285 |
case APPLE_32_BIT: |
286 |
depth = 32; |
287 |
break; |
288 |
default: |
289 |
abort(); |
290 |
} |
291 |
return depth; |
292 |
} |
293 |
|
294 |
// Map RGB color to pixel value (this only works in TrueColor/DirectColor visuals) |
295 |
static inline uint32 map_rgb(uint8 red, uint8 green, uint8 blue) |
296 |
{ |
297 |
return ((red >> rloss) << rshift) | ((green >> gloss) << gshift) | ((blue >> bloss) << bshift); |
298 |
} |
299 |
|
300 |
|
301 |
// Do we have a visual for handling the specified Mac depth? If so, set the |
302 |
// global variables "xdepth", "visualInfo", "vis" and "color_class". |
303 |
static bool find_visual_for_depth(int depth) |
304 |
{ |
305 |
D(bug("have_visual_for_depth(%d)\n", depth_of_video_mode(depth))); |
306 |
|
307 |
// 1-bit works always and uses default visual |
308 |
if (depth == APPLE_1_BIT) { |
309 |
vis = DefaultVisual(x_display, screen); |
310 |
visualInfo.visualid = XVisualIDFromVisual(vis); |
311 |
int num = 0; |
312 |
XVisualInfo *vi = XGetVisualInfo(x_display, VisualIDMask, &visualInfo, &num); |
313 |
visualInfo = vi[0]; |
314 |
XFree(vi); |
315 |
xdepth = visualInfo.depth; |
316 |
color_class = visualInfo.c_class; |
317 |
D(bug(" found visual ID 0x%02x, depth %d\n", visualInfo.visualid, xdepth)); |
318 |
return true; |
319 |
} |
320 |
|
321 |
// Calculate minimum and maximum supported X depth |
322 |
int min_depth = 1, max_depth = 32; |
323 |
switch (depth) { |
324 |
#ifdef ENABLE_VOSF |
325 |
case APPLE_2_BIT: |
326 |
case APPLE_4_BIT: // VOSF blitters can convert 2/4/8-bit -> 8/16/32-bit |
327 |
case APPLE_8_BIT: |
328 |
min_depth = 8; |
329 |
max_depth = 32; |
330 |
break; |
331 |
#else |
332 |
case APPLE_2_BIT: |
333 |
case APPLE_4_BIT: // 2/4-bit requires VOSF blitters |
334 |
return false; |
335 |
case APPLE_8_BIT: // 8-bit without VOSF requires an 8-bit visual |
336 |
min_depth = 8; |
337 |
max_depth = 8; |
338 |
break; |
339 |
#endif |
340 |
case APPLE_16_BIT: // 16-bit requires a 15/16-bit visual |
341 |
min_depth = 15; |
342 |
max_depth = 16; |
343 |
break; |
344 |
case APPLE_32_BIT: // 32-bit requires a 24/32-bit visual |
345 |
min_depth = 24; |
346 |
max_depth = 32; |
347 |
break; |
348 |
} |
349 |
D(bug(" minimum required X depth is %d, maximum supported X depth is %d\n", min_depth, max_depth)); |
350 |
|
351 |
// Try to find a visual for one of the color depths |
352 |
bool visual_found = false; |
353 |
for (int i=0; i<num_depths && !visual_found; i++) { |
354 |
|
355 |
xdepth = avail_depths[i]; |
356 |
D(bug(" trying to find visual for depth %d\n", xdepth)); |
357 |
if (xdepth < min_depth || xdepth > max_depth) |
358 |
continue; |
359 |
|
360 |
// Determine best color class for this depth |
361 |
switch (xdepth) { |
362 |
case 1: // Try StaticGray or StaticColor |
363 |
if (XMatchVisualInfo(x_display, screen, xdepth, StaticGray, &visualInfo) |
364 |
|| XMatchVisualInfo(x_display, screen, xdepth, StaticColor, &visualInfo)) |
365 |
visual_found = true; |
366 |
break; |
367 |
case 8: // Need PseudoColor |
368 |
if (XMatchVisualInfo(x_display, screen, xdepth, PseudoColor, &visualInfo)) |
369 |
visual_found = true; |
370 |
break; |
371 |
case 15: |
372 |
case 16: |
373 |
case 24: |
374 |
case 32: // Try DirectColor first, as this will allow gamma correction |
375 |
if (XMatchVisualInfo(x_display, screen, xdepth, DirectColor, &visualInfo) |
376 |
|| XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo)) |
377 |
visual_found = true; |
378 |
break; |
379 |
default: |
380 |
D(bug(" not a supported depth\n")); |
381 |
break; |
382 |
} |
383 |
} |
384 |
if (!visual_found) |
385 |
return false; |
386 |
|
387 |
// Visual was found |
388 |
vis = visualInfo.visual; |
389 |
color_class = visualInfo.c_class; |
390 |
D(bug(" found visual ID 0x%02x, depth %d, class ", visualInfo.visualid, xdepth)); |
391 |
#if DEBUG |
392 |
switch (color_class) { |
393 |
case StaticGray: D(bug("StaticGray\n")); break; |
394 |
case GrayScale: D(bug("GrayScale\n")); break; |
395 |
case StaticColor: D(bug("StaticColor\n")); break; |
396 |
case PseudoColor: D(bug("PseudoColor\n")); break; |
397 |
case TrueColor: D(bug("TrueColor\n")); break; |
398 |
case DirectColor: D(bug("DirectColor\n")); break; |
399 |
} |
400 |
#endif |
401 |
return true; |
402 |
} |
403 |
|
404 |
|
405 |
/* |
406 |
* Open display (window or fullscreen) |
407 |
*/ |
408 |
|
409 |
// Set window name and class |
410 |
static void set_window_name(Window w, int name) |
411 |
{ |
412 |
const char *str = GetString(name); |
413 |
XStoreName(x_display, w, str); |
414 |
XSetIconName(x_display, w, str); |
415 |
|
416 |
XClassHint *hints; |
417 |
hints = XAllocClassHint(); |
418 |
if (hints) { |
419 |
hints->res_name = "SheepShaver"; |
420 |
hints->res_class = "SheepShaver"; |
421 |
XSetClassHint(x_display, w, hints); |
422 |
XFree(hints); |
423 |
} |
424 |
} |
425 |
|
426 |
// Set window input focus flag |
427 |
static void set_window_focus(Window w) |
428 |
{ |
429 |
XWMHints *hints = XAllocWMHints(); |
430 |
if (hints) { |
431 |
hints->input = True; |
432 |
hints->initial_state = NormalState; |
433 |
hints->flags = InputHint | StateHint; |
434 |
XSetWMHints(x_display, w, hints); |
435 |
XFree(hints); |
436 |
} |
437 |
} |
438 |
|
439 |
// Set WM_DELETE_WINDOW protocol on window (preventing it from being destroyed by the WM when clicking on the "close" widget) |
440 |
static Atom WM_DELETE_WINDOW = (Atom)0; |
441 |
static void set_window_delete_protocol(Window w) |
442 |
{ |
443 |
WM_DELETE_WINDOW = XInternAtom(x_display, "WM_DELETE_WINDOW", false); |
444 |
XSetWMProtocols(x_display, w, &WM_DELETE_WINDOW, 1); |
445 |
} |
446 |
|
447 |
// Wait until window is mapped/unmapped |
448 |
static void wait_mapped(Window w) |
449 |
{ |
450 |
XEvent e; |
451 |
do { |
452 |
XMaskEvent(x_display, StructureNotifyMask, &e); |
453 |
} while ((e.type != MapNotify) || (e.xmap.event != w)); |
454 |
} |
455 |
|
456 |
static void wait_unmapped(Window w) |
457 |
{ |
458 |
XEvent e; |
459 |
do { |
460 |
XMaskEvent(x_display, StructureNotifyMask, &e); |
461 |
} while ((e.type != UnmapNotify) || (e.xmap.event != w)); |
462 |
} |
463 |
|
464 |
// Disable mouse acceleration |
465 |
static void disable_mouse_accel(void) |
466 |
{ |
467 |
XChangePointerControl(x_display, True, False, 1, 1, 0); |
468 |
} |
469 |
|
470 |
// Restore mouse acceleration to original value |
471 |
static void restore_mouse_accel(void) |
472 |
{ |
473 |
XChangePointerControl(x_display, True, True, orig_accel_numer, orig_accel_denom, orig_threshold); |
474 |
} |
475 |
|
476 |
// Trap SHM errors |
477 |
static bool shm_error = false; |
478 |
static int (*old_error_handler)(Display *, XErrorEvent *); |
479 |
|
480 |
static int error_handler(Display *d, XErrorEvent *e) |
481 |
{ |
482 |
if (e->error_code == BadAccess) { |
483 |
shm_error = true; |
484 |
return 0; |
485 |
} else |
486 |
return old_error_handler(d, e); |
487 |
} |
488 |
|
489 |
// Open window |
490 |
static bool open_window(int width, int height) |
491 |
{ |
492 |
int aligned_width = (width + 15) & ~15; |
493 |
int aligned_height = (height + 15) & ~15; |
494 |
|
495 |
// Set absolute mouse mode |
496 |
ADBSetRelMouseMode(false); |
497 |
|
498 |
// Create window |
499 |
XSetWindowAttributes wattr; |
500 |
wattr.event_mask = eventmask = win_eventmask; |
501 |
wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0); |
502 |
wattr.border_pixel = 0; |
503 |
wattr.backing_store = NotUseful; |
504 |
wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]); |
505 |
the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
506 |
InputOutput, vis, CWEventMask | CWBackPixel | CWBorderPixel | CWBackingStore | CWColormap, &wattr); |
507 |
|
508 |
// Set window name/class |
509 |
set_window_name(the_win, STR_WINDOW_TITLE); |
510 |
|
511 |
// Indicate that we want keyboard input |
512 |
set_window_focus(the_win); |
513 |
|
514 |
// Set delete protocol property |
515 |
set_window_delete_protocol(the_win); |
516 |
|
517 |
// Make window unresizable |
518 |
XSizeHints *hints; |
519 |
if ((hints = XAllocSizeHints()) != NULL) { |
520 |
hints->min_width = width; |
521 |
hints->max_width = width; |
522 |
hints->min_height = height; |
523 |
hints->max_height = height; |
524 |
hints->flags = PMinSize | PMaxSize; |
525 |
XSetWMNormalHints(x_display, the_win, hints); |
526 |
XFree((char *)hints); |
527 |
} |
528 |
|
529 |
// Show window |
530 |
XMapWindow(x_display, the_win); |
531 |
wait_mapped(the_win); |
532 |
|
533 |
// 1-bit mode is big-endian; if the X server is little-endian, we can't |
534 |
// use SHM because that doesn't allow changing the image byte order |
535 |
bool need_msb_image = (depth == 1 && XImageByteOrder(x_display) == LSBFirst); |
536 |
|
537 |
// Try to create and attach SHM image |
538 |
have_shm = false; |
539 |
if (local_X11 && !need_msb_image && XShmQueryExtension(x_display)) { |
540 |
|
541 |
// Create SHM image ("height + 2" for safety) |
542 |
img = XShmCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, &shminfo, width, height); |
543 |
shminfo.shmid = shmget(IPC_PRIVATE, (aligned_height + 2) * img->bytes_per_line, IPC_CREAT | 0777); |
544 |
D(bug(" shm image created\n")); |
545 |
the_buffer_copy = (uint8 *)shmat(shminfo.shmid, 0, 0); |
546 |
shminfo.shmaddr = img->data = (char *)the_buffer_copy; |
547 |
shminfo.readOnly = False; |
548 |
|
549 |
// Try to attach SHM image, catching errors |
550 |
shm_error = false; |
551 |
old_error_handler = XSetErrorHandler(error_handler); |
552 |
XShmAttach(x_display, &shminfo); |
553 |
XSync(x_display, false); |
554 |
XSetErrorHandler(old_error_handler); |
555 |
if (shm_error) { |
556 |
shmdt(shminfo.shmaddr); |
557 |
XDestroyImage(img); |
558 |
shminfo.shmid = -1; |
559 |
} else { |
560 |
have_shm = true; |
561 |
shmctl(shminfo.shmid, IPC_RMID, 0); |
562 |
} |
563 |
D(bug(" shm image attached\n")); |
564 |
} |
565 |
|
566 |
// Create normal X image if SHM doesn't work ("height + 2" for safety) |
567 |
if (!have_shm) { |
568 |
int bytes_per_row = depth == 1 ? aligned_width/8 : TrivialBytesPerRow(aligned_width, DepthModeForPixelDepth(xdepth)); |
569 |
the_buffer_copy = (uint8 *)malloc((aligned_height + 2) * bytes_per_row); |
570 |
img = XCreateImage(x_display, vis, depth == 1 ? 1 : xdepth, depth == 1 ? XYBitmap : ZPixmap, 0, (char *)the_buffer_copy, aligned_width, aligned_height, 32, bytes_per_row); |
571 |
D(bug(" X image created\n")); |
572 |
} |
573 |
|
574 |
// 1-Bit mode is big-endian |
575 |
if (need_msb_image) { |
576 |
img->byte_order = MSBFirst; |
577 |
img->bitmap_bit_order = MSBFirst; |
578 |
} |
579 |
|
580 |
#ifdef ENABLE_VOSF |
581 |
use_vosf = true; |
582 |
// Allocate memory for frame buffer (SIZE is extended to page-boundary) |
583 |
the_host_buffer = the_buffer_copy; |
584 |
the_buffer_size = page_extend((aligned_height + 2) * img->bytes_per_line); |
585 |
the_buffer = (uint8 *)vm_acquire(the_buffer_size); |
586 |
the_buffer_copy = (uint8 *)malloc(the_buffer_size); |
587 |
D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer)); |
588 |
#else |
589 |
// Allocate memory for frame buffer |
590 |
the_buffer = (uint8 *)malloc((aligned_height + 2) * img->bytes_per_line); |
591 |
D(bug("the_buffer = %p, the_buffer_copy = %p\n", the_buffer, the_buffer_copy)); |
592 |
#endif |
593 |
screen_base = Host2MacAddr(the_buffer); |
594 |
|
595 |
// Create GC |
596 |
the_gc = XCreateGC(x_display, the_win, 0, 0); |
597 |
XSetState(x_display, the_gc, black_pixel, white_pixel, GXcopy, AllPlanes); |
598 |
|
599 |
// Create cursor |
600 |
if (hw_mac_cursor_accl) { |
601 |
cursor_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 4, 16, 16, 16, 2); |
602 |
cursor_image->byte_order = MSBFirst; |
603 |
cursor_image->bitmap_bit_order = MSBFirst; |
604 |
cursor_mask_image = XCreateImage(x_display, vis, 1, XYPixmap, 0, (char *)MacCursor + 36, 16, 16, 16, 2); |
605 |
cursor_mask_image->byte_order = MSBFirst; |
606 |
cursor_mask_image->bitmap_bit_order = MSBFirst; |
607 |
cursor_map = XCreatePixmap(x_display, the_win, 16, 16, 1); |
608 |
cursor_mask_map = XCreatePixmap(x_display, the_win, 16, 16, 1); |
609 |
cursor_gc = XCreateGC(x_display, cursor_map, 0, 0); |
610 |
cursor_mask_gc = XCreateGC(x_display, cursor_mask_map, 0, 0); |
611 |
mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, 0, 0); |
612 |
cursor_changed = false; |
613 |
} |
614 |
|
615 |
// Create no_cursor |
616 |
else { |
617 |
mac_cursor = XCreatePixmapCursor(x_display, |
618 |
XCreatePixmap(x_display, the_win, 1, 1, 1), |
619 |
XCreatePixmap(x_display, the_win, 1, 1, 1), |
620 |
&black, &white, 0, 0); |
621 |
XDefineCursor(x_display, the_win, mac_cursor); |
622 |
} |
623 |
|
624 |
// Init blitting routines |
625 |
bool native_byte_order; |
626 |
#ifdef WORDS_BIGENDIAN |
627 |
native_byte_order = (XImageByteOrder(x_display) == MSBFirst); |
628 |
#else |
629 |
native_byte_order = (XImageByteOrder(x_display) == LSBFirst); |
630 |
#endif |
631 |
#ifdef ENABLE_VOSF |
632 |
Screen_blitter_init(visualFormat, native_byte_order, depth); |
633 |
#endif |
634 |
|
635 |
// Set bytes per row |
636 |
XSync(x_display, false); |
637 |
return true; |
638 |
} |
639 |
|
640 |
// Open FBDev DGA display |
641 |
static bool open_fbdev_dga(int width, int height) |
642 |
{ |
643 |
#ifdef ENABLE_FBDEV_DGA |
644 |
#ifdef ENABLE_XF86_VIDMODE |
645 |
// Switch to best mode |
646 |
if (has_vidmode) { |
647 |
int best = -1; |
648 |
for (int i = 0; i < num_x_video_modes; i++) { |
649 |
if (x_video_modes[i]->hdisplay == width && x_video_modes[i]->vdisplay == height) { |
650 |
best = i; |
651 |
break; |
652 |
} |
653 |
}; |
654 |
assert(best != -1); |
655 |
XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]); |
656 |
XF86VidModeSetViewPort(x_display, screen, 0, 0); |
657 |
D(bug("[fbdev] VideoMode %d: %d x %d @ %d\n", best, |
658 |
x_video_modes[best]->hdisplay, x_video_modes[best]->vdisplay, |
659 |
1000 * x_video_modes[best]->dotclock / (x_video_modes[best]->htotal * x_video_modes[best]->vtotal))); |
660 |
} |
661 |
#endif |
662 |
|
663 |
if (ioctl(fb_dev_fd, FBIOGET_FSCREENINFO, &fb_finfo) != 0) { |
664 |
D(bug("[fbdev] Can't get FSCREENINFO: %s\n", strerror(errno))); |
665 |
return false; |
666 |
} |
667 |
D(bug("[fbdev] Device ID: %s\n", fb_finfo.id)); |
668 |
D(bug("[fbdev] smem_start: %p [%d bytes]\n", fb_finfo.smem_start, fb_finfo.smem_len)); |
669 |
|
670 |
int fb_type = fb_finfo.type; |
671 |
const char *fb_type_str = NULL; |
672 |
switch (fb_type) { |
673 |
case FB_TYPE_PACKED_PIXELS: fb_type_str = "Packed Pixels"; break; |
674 |
case FB_TYPE_PLANES: fb_type_str = "Non interleaved planes"; break; |
675 |
case FB_TYPE_INTERLEAVED_PLANES: fb_type_str = "Interleaved planes"; break; |
676 |
case FB_TYPE_TEXT: fb_type_str = "Text/attributes"; break; |
677 |
case FB_TYPE_VGA_PLANES: fb_type_str = "EGA/VGA planes"; break; |
678 |
default: fb_type_str = "<unknown>"; break; |
679 |
} |
680 |
D(bug("[fbdev] type: %s\n", fb_type_str)); |
681 |
|
682 |
if (fb_type != FB_TYPE_PACKED_PIXELS) { |
683 |
D(bug("[fbdev] type '%s' not supported\n", fb_type_str)); |
684 |
return false; |
685 |
} |
686 |
|
687 |
int fb_visual = fb_finfo.visual; |
688 |
const char *fb_visual_str; |
689 |
switch (fb_visual) { |
690 |
case FB_VISUAL_MONO01: fb_visual_str = "Monochrome 1=Black 0=White"; break; |
691 |
case FB_VISUAL_MONO10: fb_visual_str = "Monochrome 1=While 0=Black"; break; |
692 |
case FB_VISUAL_TRUECOLOR: fb_visual_str = "True color"; break; |
693 |
case FB_VISUAL_PSEUDOCOLOR: fb_visual_str = "Pseudo color (like atari)"; break; |
694 |
case FB_VISUAL_DIRECTCOLOR: fb_visual_str = "Direct color"; break; |
695 |
case FB_VISUAL_STATIC_PSEUDOCOLOR: fb_visual_str = "Pseudo color readonly"; break; |
696 |
default: fb_visual_str = "<unknown>"; break; |
697 |
} |
698 |
D(bug("[fbdev] visual: %s\n", fb_visual_str)); |
699 |
|
700 |
if (fb_visual != FB_VISUAL_TRUECOLOR) { |
701 |
D(bug("[fbdev] visual '%s' not supported\n", fb_visual_str)); |
702 |
return false; |
703 |
} |
704 |
|
705 |
// Map frame buffer |
706 |
the_buffer = (uint8 *)mmap(NULL, fb_finfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev_fd, 0); |
707 |
if (the_buffer == MAP_FAILED) { |
708 |
D(bug("[fbdev] Can't mmap /dev/fb0: %s\n", strerror(errno))); |
709 |
return false; |
710 |
} |
711 |
|
712 |
// Set absolute mouse mode |
713 |
ADBSetRelMouseMode(false); |
714 |
|
715 |
// Create window |
716 |
XSetWindowAttributes wattr; |
717 |
wattr.event_mask = eventmask = dga_eventmask; |
718 |
wattr.override_redirect = True; |
719 |
the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
720 |
InputOutput, DefaultVisual(x_display, screen), |
721 |
CWEventMask | CWOverrideRedirect, &wattr); |
722 |
|
723 |
// Show window |
724 |
XMapRaised(x_display, the_win); |
725 |
wait_mapped(the_win); |
726 |
|
727 |
// Grab mouse and keyboard |
728 |
XGrabKeyboard(x_display, the_win, True, |
729 |
GrabModeAsync, GrabModeAsync, CurrentTime); |
730 |
XGrabPointer(x_display, the_win, True, |
731 |
PointerMotionMask | ButtonPressMask | ButtonReleaseMask, |
732 |
GrabModeAsync, GrabModeAsync, the_win, None, CurrentTime); |
733 |
disable_mouse_accel(); |
734 |
|
735 |
// Create no_cursor |
736 |
mac_cursor = XCreatePixmapCursor(x_display, |
737 |
XCreatePixmap(x_display, the_win, 1, 1, 1), |
738 |
XCreatePixmap(x_display, the_win, 1, 1, 1), |
739 |
&black, &white, 0, 0); |
740 |
XDefineCursor(x_display, the_win, mac_cursor); |
741 |
|
742 |
// Init blitting routines |
743 |
int bytes_per_row = TrivialBytesPerRow((width + 7) & ~7, DepthModeForPixelDepth(depth)); |
744 |
#if ENABLE_VOSF |
745 |
// Extract current screen color masks (we are in True Color mode) |
746 |
VisualFormat visualFormat; |
747 |
visualFormat.depth = xdepth = DefaultDepth(x_display, screen); |
748 |
XMatchVisualInfo(x_display, screen, xdepth, TrueColor, &visualInfo); |
749 |
assert(visualFormat.depth == visualInfo.depth); |
750 |
visualFormat.Rmask = visualInfo.red_mask; |
751 |
visualFormat.Gmask = visualInfo.green_mask; |
752 |
visualFormat.Bmask = visualInfo.blue_mask; |
753 |
D(bug("[fbdev] %d bpp, (%08x,%08x,%08x)\n", |
754 |
visualFormat.depth, |
755 |
visualFormat.Rmask, visualFormat.Gmask, visualFormat.Bmask)); |
756 |
D(bug("[fbdev] Mac depth %d bpp\n", depth)); |
757 |
|
758 |
// Screen_blitter_init() returns TRUE if VOSF is mandatory |
759 |
// i.e. the framebuffer update function is not Blit_Copy_Raw |
760 |
#ifdef WORDS_BIGENDIAN |
761 |
const bool native_byte_order = (XImageByteOrder(x_display) == MSBFirst); |
762 |
#else |
763 |
const bool native_byte_order = (XImageByteOrder(x_display) == LSBFirst); |
764 |
#endif |
765 |
Screen_blitter_init(visualFormat, native_byte_order, depth); |
766 |
|
767 |
// Allocate memory for frame buffer (SIZE is extended to page-boundary) |
768 |
use_vosf = true; |
769 |
the_host_buffer = the_buffer; |
770 |
the_buffer_size = page_extend((height + 2) * bytes_per_row); |
771 |
the_buffer_copy = (uint8 *)malloc(the_buffer_size); |
772 |
the_buffer = (uint8 *)vm_acquire(the_buffer_size); |
773 |
D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer)); |
774 |
#endif |
775 |
|
776 |
// Set frame buffer base |
777 |
D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf)); |
778 |
screen_base = Host2MacAddr(the_buffer); |
779 |
VModes[cur_mode].viRowBytes = bytes_per_row; |
780 |
return true; |
781 |
#else |
782 |
ErrorAlert("SheepShaver has been compiled with DGA support disabled."); |
783 |
return false; |
784 |
#endif |
785 |
} |
786 |
|
787 |
// Open XF86 DGA display (!! should use X11 VidMode extensions to set mode) |
788 |
static bool open_xf86_dga(int width, int height) |
789 |
{ |
790 |
if (is_fbdev_dga_mode) |
791 |
return false; |
792 |
|
793 |
#ifdef ENABLE_XF86_DGA |
794 |
// Set relative mouse mode |
795 |
ADBSetRelMouseMode(true); |
796 |
|
797 |
// Create window |
798 |
XSetWindowAttributes wattr; |
799 |
wattr.event_mask = eventmask = dga_eventmask; |
800 |
wattr.override_redirect = True; |
801 |
wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]); |
802 |
the_win = XCreateWindow(x_display, rootwin, 0, 0, width, height, 0, xdepth, |
803 |
InputOutput, vis, CWEventMask | CWOverrideRedirect | |
804 |
(color_class == DirectColor ? CWColormap : 0), &wattr); |
805 |
|
806 |
// Show window |
807 |
XMapRaised(x_display, the_win); |
808 |
wait_mapped(the_win); |
809 |
|
810 |
#ifdef ENABLE_XF86_VIDMODE |
811 |
// Switch to best mode |
812 |
if (has_vidmode) { |
813 |
int best = 0; |
814 |
for (int i=1; i<num_x_video_modes; i++) { |
815 |
if (x_video_modes[i]->hdisplay >= width && x_video_modes[i]->vdisplay >= height && |
816 |
x_video_modes[i]->hdisplay <= x_video_modes[best]->hdisplay && x_video_modes[i]->vdisplay <= x_video_modes[best]->vdisplay) { |
817 |
best = i; |
818 |
} |
819 |
} |
820 |
XF86VidModeSwitchToMode(x_display, screen, x_video_modes[best]); |
821 |
XF86VidModeSetViewPort(x_display, screen, 0, 0); |
822 |
} |
823 |
#endif |
824 |
|
825 |
// Establish direct screen connection |
826 |
XMoveResizeWindow(x_display, the_win, 0, 0, width, height); |
827 |
XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); |
828 |
XGrabKeyboard(x_display, rootwin, True, GrabModeAsync, GrabModeAsync, CurrentTime); |
829 |
XGrabPointer(x_display, rootwin, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None, CurrentTime); |
830 |
|
831 |
int v_width, v_bank, v_size; |
832 |
XF86DGAGetVideo(x_display, screen, (char **)&the_buffer, &v_width, &v_bank, &v_size); |
833 |
XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse); |
834 |
XF86DGASetViewPort(x_display, screen, 0, 0); |
835 |
XF86DGASetVidPage(x_display, screen, 0); |
836 |
|
837 |
// Set colormap |
838 |
if (!IsDirectMode(get_current_mode())) { |
839 |
XSetWindowColormap(x_display, the_win, cmap[current_dga_cmap = 0]); |
840 |
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
841 |
} |
842 |
XSync(x_display, false); |
843 |
|
844 |
// Init blitting routines |
845 |
int bytes_per_row = TrivialBytesPerRow((v_width + 7) & ~7, DepthModeForPixelDepth(depth)); |
846 |
#if ENABLE_VOSF |
847 |
bool native_byte_order; |
848 |
#ifdef WORDS_BIGENDIAN |
849 |
native_byte_order = (XImageByteOrder(x_display) == MSBFirst); |
850 |
#else |
851 |
native_byte_order = (XImageByteOrder(x_display) == LSBFirst); |
852 |
#endif |
853 |
#if REAL_ADDRESSING || DIRECT_ADDRESSING |
854 |
// Screen_blitter_init() returns TRUE if VOSF is mandatory |
855 |
// i.e. the framebuffer update function is not Blit_Copy_Raw |
856 |
use_vosf = Screen_blitter_init(visualFormat, native_byte_order, depth); |
857 |
|
858 |
if (use_vosf) { |
859 |
// Allocate memory for frame buffer (SIZE is extended to page-boundary) |
860 |
the_host_buffer = the_buffer; |
861 |
the_buffer_size = page_extend((height + 2) * bytes_per_row); |
862 |
the_buffer_copy = (uint8 *)malloc(the_buffer_size); |
863 |
the_buffer = (uint8 *)vm_acquire(the_buffer_size); |
864 |
D(bug("the_buffer = %p, the_buffer_copy = %p, the_host_buffer = %p\n", the_buffer, the_buffer_copy, the_host_buffer)); |
865 |
} |
866 |
#else |
867 |
use_vosf = false; |
868 |
#endif |
869 |
#endif |
870 |
|
871 |
// Set frame buffer base |
872 |
D(bug("the_buffer = %p, use_vosf = %d\n", the_buffer, use_vosf)); |
873 |
screen_base = Host2MacAddr(the_buffer); |
874 |
VModes[cur_mode].viRowBytes = bytes_per_row; |
875 |
return true; |
876 |
#else |
877 |
ErrorAlert("SheepShaver has been compiled with DGA support disabled."); |
878 |
return false; |
879 |
#endif |
880 |
} |
881 |
|
882 |
// Open DGA display |
883 |
static bool open_dga(int width, int height) |
884 |
{ |
885 |
bool display_open; |
886 |
|
887 |
display_open = open_xf86_dga(width, height); |
888 |
#ifdef ENABLE_FBDEV_DGA |
889 |
// Try to fallback to FBDev DGA mode |
890 |
if (!display_open) { |
891 |
is_fbdev_dga_mode = true; |
892 |
display_open = open_fbdev_dga(width, height); |
893 |
} |
894 |
#endif |
895 |
|
896 |
// Common DGA display initialization |
897 |
if (display_open) { |
898 |
|
899 |
// Fake image to get display bounds in the refresh function |
900 |
if ((img = (XImage *)malloc(sizeof(*img))) == NULL) |
901 |
return false; |
902 |
img->width = DisplayWidth(x_display, screen); |
903 |
img->height = DisplayHeight(x_display, screen); |
904 |
img->depth = is_fbdev_dga_mode ? xdepth : depth; |
905 |
img->bytes_per_line = TrivialBytesPerRow(img->width, DepthModeForPixelDepth(img->depth)); |
906 |
} |
907 |
|
908 |
return display_open; |
909 |
} |
910 |
|
911 |
static bool open_display(void) |
912 |
{ |
913 |
D(bug("open_display()\n")); |
914 |
const VideoInfo &mode = VModes[cur_mode]; |
915 |
|
916 |
// Get original mouse acceleration |
917 |
XGetPointerControl(x_display, &orig_accel_numer, &orig_accel_denom, &orig_threshold); |
918 |
|
919 |
// Find best available X visual |
920 |
if (!find_visual_for_depth(mode.viAppleMode)) { |
921 |
ErrorAlert(GetString(STR_NO_XVISUAL_ERR)); |
922 |
return false; |
923 |
} |
924 |
|
925 |
// Build up visualFormat structure |
926 |
visualFormat.fullscreen = (display_type == DIS_SCREEN); |
927 |
visualFormat.depth = visualInfo.depth; |
928 |
visualFormat.Rmask = visualInfo.red_mask; |
929 |
visualFormat.Gmask = visualInfo.green_mask; |
930 |
visualFormat.Bmask = visualInfo.blue_mask; |
931 |
|
932 |
// Create color maps |
933 |
if (color_class == PseudoColor || color_class == DirectColor) { |
934 |
cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
935 |
cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocAll); |
936 |
} else { |
937 |
cmap[0] = XCreateColormap(x_display, rootwin, vis, AllocNone); |
938 |
cmap[1] = XCreateColormap(x_display, rootwin, vis, AllocNone); |
939 |
} |
940 |
|
941 |
// Find pixel format of direct modes |
942 |
if (color_class == DirectColor || color_class == TrueColor) { |
943 |
rshift = gshift = bshift = 0; |
944 |
rloss = gloss = bloss = 8; |
945 |
uint32 mask; |
946 |
for (mask=vis->red_mask; !(mask&1); mask>>=1) |
947 |
++rshift; |
948 |
for (; mask&1; mask>>=1) |
949 |
--rloss; |
950 |
for (mask=vis->green_mask; !(mask&1); mask>>=1) |
951 |
++gshift; |
952 |
for (; mask&1; mask>>=1) |
953 |
--gloss; |
954 |
for (mask=vis->blue_mask; !(mask&1); mask>>=1) |
955 |
++bshift; |
956 |
for (; mask&1; mask>>=1) |
957 |
--bloss; |
958 |
} |
959 |
|
960 |
// Preset palette pixel values for CLUT or gamma table |
961 |
if (color_class == DirectColor) { |
962 |
int num = vis->map_entries; |
963 |
for (int i=0; i<num; i++) { |
964 |
int c = (i * 256) / num; |
965 |
x_palette[i].pixel = map_rgb(c, c, c); |
966 |
x_palette[i].flags = DoRed | DoGreen | DoBlue; |
967 |
} |
968 |
} else if (color_class == PseudoColor) { |
969 |
for (int i=0; i<256; i++) { |
970 |
x_palette[i].pixel = i; |
971 |
x_palette[i].flags = DoRed | DoGreen | DoBlue; |
972 |
} |
973 |
} |
974 |
|
975 |
// Load gray ramp to color map |
976 |
int num = (color_class == DirectColor ? vis->map_entries : 256); |
977 |
for (int i=0; i<num; i++) { |
978 |
int c = (i * 256) / num; |
979 |
x_palette[i].red = c * 0x0101; |
980 |
x_palette[i].green = c * 0x0101; |
981 |
x_palette[i].blue = c * 0x0101; |
982 |
} |
983 |
if (color_class == PseudoColor || color_class == DirectColor) { |
984 |
XStoreColors(x_display, cmap[0], x_palette, num); |
985 |
XStoreColors(x_display, cmap[1], x_palette, num); |
986 |
} |
987 |
|
988 |
#ifdef ENABLE_VOSF |
989 |
// Load gray ramp to 8->16/32 expand map |
990 |
if (!IsDirectMode(get_current_mode()) && xdepth > 8) |
991 |
for (int i=0; i<256; i++) |
992 |
ExpandMap[i] = map_rgb(i, i, i); |
993 |
#endif |
994 |
|
995 |
// Create display of requested type |
996 |
display_type = mode.viType; |
997 |
depth = depth_of_video_mode(mode.viAppleMode); |
998 |
|
999 |
bool display_open; |
1000 |
switch (display_type) { |
1001 |
case DIS_SCREEN: |
1002 |
display_open = open_dga(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize); |
1003 |
break; |
1004 |
case DIS_WINDOW: |
1005 |
display_open = open_window(VModes[cur_mode].viXsize, VModes[cur_mode].viYsize); |
1006 |
break; |
1007 |
default: |
1008 |
display_open = false; |
1009 |
break; |
1010 |
} |
1011 |
|
1012 |
#ifdef ENABLE_VOSF |
1013 |
if (use_vosf) { |
1014 |
// Initialize the VOSF system |
1015 |
LOCK_VOSF; |
1016 |
if (!video_vosf_init()) { |
1017 |
ErrorAlert(GetString(STR_VOSF_INIT_ERR)); |
1018 |
UNLOCK_VOSF; |
1019 |
return false; |
1020 |
} |
1021 |
UNLOCK_VOSF; |
1022 |
} |
1023 |
#endif |
1024 |
|
1025 |
// Zero screen buffers, viRowBytes is initialized at this stage |
1026 |
memset(the_buffer, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); |
1027 |
memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); |
1028 |
return display_open; |
1029 |
} |
1030 |
|
1031 |
|
1032 |
/* |
1033 |
* Close display |
1034 |
*/ |
1035 |
|
1036 |
// Close window |
1037 |
static void close_window(void) |
1038 |
{ |
1039 |
if (have_shm) { |
1040 |
XShmDetach(x_display, &shminfo); |
1041 |
#ifdef ENABLE_VOSF |
1042 |
the_host_buffer = NULL; // don't free() in driver_base dtor |
1043 |
#else |
1044 |
the_buffer_copy = NULL; // don't free() in driver_base dtor |
1045 |
#endif |
1046 |
} |
1047 |
if (img) { |
1048 |
if (!have_shm) |
1049 |
img->data = NULL; |
1050 |
XDestroyImage(img); |
1051 |
} |
1052 |
if (have_shm) { |
1053 |
shmdt(shminfo.shmaddr); |
1054 |
shmctl(shminfo.shmid, IPC_RMID, 0); |
1055 |
} |
1056 |
if (the_gc) |
1057 |
XFreeGC(x_display, the_gc); |
1058 |
|
1059 |
XFlush(x_display); |
1060 |
XSync(x_display, false); |
1061 |
} |
1062 |
|
1063 |
// Close FBDev mode |
1064 |
static void close_fbdev_dga(void) |
1065 |
{ |
1066 |
#ifdef ENABLE_FBDEV_DGA |
1067 |
uint8 *fb_base; |
1068 |
if (!use_vosf) |
1069 |
fb_base = the_buffer; |
1070 |
#ifdef ENABLE_VOSF |
1071 |
else |
1072 |
fb_base = the_host_buffer; |
1073 |
#endif |
1074 |
munmap(fb_base, fb_finfo.smem_len); |
1075 |
#endif |
1076 |
} |
1077 |
|
1078 |
// Close XF86 DGA mode |
1079 |
static void close_xf86_dga(void) |
1080 |
{ |
1081 |
#ifdef ENABLE_XF86_DGA |
1082 |
XF86DGADirectVideo(x_display, screen, 0); |
1083 |
#endif |
1084 |
} |
1085 |
|
1086 |
// Close DGA mode |
1087 |
static void close_dga(void) |
1088 |
{ |
1089 |
if (is_fbdev_dga_mode) |
1090 |
close_fbdev_dga(); |
1091 |
else |
1092 |
close_xf86_dga(); |
1093 |
|
1094 |
XUngrabPointer(x_display, CurrentTime); |
1095 |
XUngrabKeyboard(x_display, CurrentTime); |
1096 |
|
1097 |
#ifdef ENABLE_XF86_VIDMODE |
1098 |
if (has_vidmode) |
1099 |
XF86VidModeSwitchToMode(x_display, screen, x_video_modes[0]); |
1100 |
#endif |
1101 |
|
1102 |
// Release fake image (it's not a normal XImage!) |
1103 |
free(img); |
1104 |
img = NULL; |
1105 |
|
1106 |
if (!use_vosf) { |
1107 |
// don't free() the screen buffer in driver_base dtor |
1108 |
the_buffer = NULL; |
1109 |
} |
1110 |
#ifdef ENABLE_VOSF |
1111 |
else { |
1112 |
// don't free() the screen buffer in driver_base dtor |
1113 |
the_host_buffer = NULL; |
1114 |
} |
1115 |
#endif |
1116 |
} |
1117 |
|
1118 |
static void close_display(void) |
1119 |
{ |
1120 |
if (display_type == DIS_SCREEN) |
1121 |
close_dga(); |
1122 |
else if (display_type == DIS_WINDOW) |
1123 |
close_window(); |
1124 |
|
1125 |
// Close window |
1126 |
if (the_win) { |
1127 |
XUnmapWindow(x_display, the_win); |
1128 |
wait_unmapped(the_win); |
1129 |
XDestroyWindow(x_display, the_win); |
1130 |
} |
1131 |
|
1132 |
// Free colormaps |
1133 |
if (cmap[0]) { |
1134 |
XFreeColormap(x_display, cmap[0]); |
1135 |
cmap[0] = 0; |
1136 |
} |
1137 |
if (cmap[1]) { |
1138 |
XFreeColormap(x_display, cmap[1]); |
1139 |
cmap[1] = 0; |
1140 |
} |
1141 |
|
1142 |
#ifdef ENABLE_VOSF |
1143 |
if (use_vosf) { |
1144 |
// Deinitialize VOSF |
1145 |
video_vosf_exit(); |
1146 |
} |
1147 |
#endif |
1148 |
|
1149 |
// Free frame buffer(s) |
1150 |
if (!use_vosf) { |
1151 |
if (the_buffer_copy) { |
1152 |
free(the_buffer_copy); |
1153 |
the_buffer_copy = NULL; |
1154 |
} |
1155 |
} |
1156 |
#ifdef ENABLE_VOSF |
1157 |
else { |
1158 |
// the_buffer shall always be mapped through vm_acquire() so that we can vm_protect() it at will |
1159 |
if (the_buffer != VM_MAP_FAILED) { |
1160 |
D(bug(" releasing the_buffer at %p (%d bytes)\n", the_buffer, the_buffer_size)); |
1161 |
vm_release(the_buffer, the_buffer_size); |
1162 |
the_buffer = NULL; |
1163 |
} |
1164 |
if (the_host_buffer) { |
1165 |
D(bug(" freeing the_host_buffer at %p\n", the_host_buffer)); |
1166 |
free(the_host_buffer); |
1167 |
the_host_buffer = NULL; |
1168 |
} |
1169 |
if (the_buffer_copy) { |
1170 |
D(bug(" freeing the_buffer_copy at %p\n", the_buffer_copy)); |
1171 |
free(the_buffer_copy); |
1172 |
the_buffer_copy = NULL; |
1173 |
} |
1174 |
} |
1175 |
#endif |
1176 |
|
1177 |
// Restore mouse acceleration |
1178 |
restore_mouse_accel(); |
1179 |
} |
1180 |
|
1181 |
|
1182 |
/* |
1183 |
* Initialization |
1184 |
*/ |
1185 |
|
1186 |
// Init keycode translation table |
1187 |
static void keycode_init(void) |
1188 |
{ |
1189 |
bool use_kc = PrefsFindBool("keycodes"); |
1190 |
if (use_kc) { |
1191 |
|
1192 |
// Get keycode file path from preferences |
1193 |
const char *kc_path = PrefsFindString("keycodefile"); |
1194 |
|
1195 |
// Open keycode table |
1196 |
FILE *f = fopen(kc_path ? kc_path : KEYCODE_FILE_NAME, "r"); |
1197 |
if (f == NULL) { |
1198 |
char str[256]; |
1199 |
sprintf(str, GetString(STR_KEYCODE_FILE_WARN), kc_path ? kc_path : KEYCODE_FILE_NAME, strerror(errno)); |
1200 |
WarningAlert(str); |
1201 |
return; |
1202 |
} |
1203 |
|
1204 |
// Default translation table |
1205 |
for (int i=0; i<256; i++) |
1206 |
keycode_table[i] = -1; |
1207 |
|
1208 |
// Search for server vendor string, then read keycodes |
1209 |
const char *vendor = ServerVendor(x_display); |
1210 |
// Force use of MacX mappings on MacOS X with Apple's X server |
1211 |
int dummy; |
1212 |
if (XQueryExtension(x_display, "Apple-DRI", &dummy, &dummy, &dummy)) |
1213 |
vendor = "MacX"; |
1214 |
bool vendor_found = false; |
1215 |
char line[256]; |
1216 |
while (fgets(line, 255, f)) { |
1217 |
// Read line |
1218 |
int len = strlen(line); |
1219 |
if (len == 0) |
1220 |
continue; |
1221 |
line[len-1] = 0; |
1222 |
|
1223 |
// Comments begin with "#" or ";" |
1224 |
if (line[0] == '#' || line[0] == ';' || line[0] == 0) |
1225 |
continue; |
1226 |
|
1227 |
if (vendor_found) { |
1228 |
// Read keycode |
1229 |
int x_code, mac_code; |
1230 |
if (sscanf(line, "%d %d", &x_code, &mac_code) == 2) |
1231 |
keycode_table[x_code & 0xff] = mac_code; |
1232 |
else |
1233 |
break; |
1234 |
} else { |
1235 |
// Search for vendor string |
1236 |
if (strstr(vendor, line) == vendor) |
1237 |
vendor_found = true; |
1238 |
} |
1239 |
} |
1240 |
|
1241 |
// Keycode file completely read |
1242 |
fclose(f); |
1243 |
use_keycodes = vendor_found; |
1244 |
|
1245 |
// Vendor not found? Then display warning |
1246 |
if (!vendor_found) { |
1247 |
char str[256]; |
1248 |
sprintf(str, GetString(STR_KEYCODE_VENDOR_WARN), vendor, kc_path ? kc_path : KEYCODE_FILE_NAME); |
1249 |
WarningAlert(str); |
1250 |
return; |
1251 |
} |
1252 |
} |
1253 |
} |
1254 |
|
1255 |
// Find Apple mode matching best specified dimensions |
1256 |
static int find_apple_resolution(int xsize, int ysize) |
1257 |
{ |
1258 |
int apple_id; |
1259 |
if (xsize < 800) |
1260 |
apple_id = APPLE_640x480; |
1261 |
else if (xsize < 1024) |
1262 |
apple_id = APPLE_800x600; |
1263 |
else if (xsize < 1152) |
1264 |
apple_id = APPLE_1024x768; |
1265 |
else if (xsize < 1280) { |
1266 |
if (ysize < 900) |
1267 |
apple_id = APPLE_1152x768; |
1268 |
else |
1269 |
apple_id = APPLE_1152x900; |
1270 |
} |
1271 |
else if (xsize < 1600) |
1272 |
apple_id = APPLE_1280x1024; |
1273 |
else |
1274 |
apple_id = APPLE_1600x1200; |
1275 |
return apple_id; |
1276 |
} |
1277 |
|
1278 |
// Find mode in list of supported modes |
1279 |
static int find_mode(int apple_mode, int apple_id, int type) |
1280 |
{ |
1281 |
for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) { |
1282 |
if (p->viType == type && p->viAppleID == apple_id && p->viAppleMode == apple_mode) |
1283 |
return p - VModes; |
1284 |
} |
1285 |
return -1; |
1286 |
} |
1287 |
|
1288 |
// Add custom video mode |
1289 |
static void add_custom_mode(VideoInfo *&p, int type, uint32 x, uint32 y, int apple_mode, int apple_id) |
1290 |
{ |
1291 |
p->viType = type; |
1292 |
p->viXsize = x; |
1293 |
p->viYsize = y; |
1294 |
p->viRowBytes = TrivialBytesPerRow(p->viXsize, apple_mode); |
1295 |
p->viAppleMode = apple_mode; |
1296 |
p->viAppleID = apple_id; |
1297 |
p++; |
1298 |
} |
1299 |
|
1300 |
// Add mode to list of supported modes |
1301 |
static void add_mode(VideoInfo *&p, uint32 allow, uint32 test, int apple_mode, int apple_id, int type) |
1302 |
{ |
1303 |
if (allow & test) { |
1304 |
uint32 x = 0, y = 0; |
1305 |
switch (apple_id) { |
1306 |
case APPLE_W_640x480: |
1307 |
case APPLE_640x480: |
1308 |
x = 640; |
1309 |
y = 480; |
1310 |
break; |
1311 |
case APPLE_W_800x600: |
1312 |
case APPLE_800x600: |
1313 |
x = 800; |
1314 |
y = 600; |
1315 |
break; |
1316 |
case APPLE_1024x768: |
1317 |
x = 1024; |
1318 |
y = 768; |
1319 |
break; |
1320 |
case APPLE_1152x768: |
1321 |
x = 1152; |
1322 |
y = 768; |
1323 |
break; |
1324 |
case APPLE_1152x900: |
1325 |
x = 1152; |
1326 |
y = 900; |
1327 |
break; |
1328 |
case APPLE_1280x1024: |
1329 |
x = 1280; |
1330 |
y = 1024; |
1331 |
break; |
1332 |
case APPLE_1600x1200: |
1333 |
x = 1600; |
1334 |
y = 1200; |
1335 |
break; |
1336 |
} |
1337 |
add_custom_mode(p, type, x, y, apple_mode, apple_id); |
1338 |
} |
1339 |
} |
1340 |
|
1341 |
// Add standard list of windowed modes for given color depth |
1342 |
static void add_window_modes(VideoInfo *&p, int window_modes, int mode) |
1343 |
{ |
1344 |
add_mode(p, window_modes, 1, mode, APPLE_W_640x480, DIS_WINDOW); |
1345 |
add_mode(p, window_modes, 2, mode, APPLE_W_800x600, DIS_WINDOW); |
1346 |
} |
1347 |
|
1348 |
static bool has_mode(int x, int y) |
1349 |
{ |
1350 |
#ifdef ENABLE_XF86_VIDMODE |
1351 |
if (has_vidmode) { |
1352 |
for (int i=0; i<num_x_video_modes; i++) |
1353 |
if (x_video_modes[i]->hdisplay == x && x_video_modes[i]->vdisplay == y) |
1354 |
return true; |
1355 |
return false; |
1356 |
} |
1357 |
#endif |
1358 |
return DisplayWidth(x_display, screen) >= x && DisplayHeight(x_display, screen) >= y; |
1359 |
} |
1360 |
|
1361 |
bool VideoInit(void) |
1362 |
{ |
1363 |
#ifdef ENABLE_VOSF |
1364 |
// Zero the mainBuffer structure |
1365 |
mainBuffer.dirtyPages = NULL; |
1366 |
mainBuffer.pageInfo = NULL; |
1367 |
#endif |
1368 |
|
1369 |
// Check if X server runs on local machine |
1370 |
local_X11 = (strncmp(XDisplayName(x_display_name), ":", 1) == 0) |
1371 |
|| (strncmp(XDisplayName(x_display_name), "unix:", 5) == 0); |
1372 |
|
1373 |
// Init keycode translation |
1374 |
keycode_init(); |
1375 |
|
1376 |
// Read frame skip prefs |
1377 |
frame_skip = PrefsFindInt32("frameskip"); |
1378 |
if (frame_skip == 0) |
1379 |
frame_skip = 1; |
1380 |
|
1381 |
// Read mouse wheel prefs |
1382 |
mouse_wheel_mode = PrefsFindInt32("mousewheelmode"); |
1383 |
mouse_wheel_lines = PrefsFindInt32("mousewheellines"); |
1384 |
|
1385 |
// Init variables |
1386 |
private_data = NULL; |
1387 |
video_activated = true; |
1388 |
|
1389 |
// Find screen and root window |
1390 |
screen = XDefaultScreen(x_display); |
1391 |
rootwin = XRootWindow(x_display, screen); |
1392 |
|
1393 |
// Get sorted list of available depths |
1394 |
avail_depths = XListDepths(x_display, screen, &num_depths); |
1395 |
if (avail_depths == NULL) { |
1396 |
ErrorAlert(GetString(STR_UNSUPP_DEPTH_ERR)); |
1397 |
return false; |
1398 |
} |
1399 |
sort(avail_depths, avail_depths + num_depths); |
1400 |
|
1401 |
// Get screen depth |
1402 |
xdepth = DefaultDepth(x_display, screen); |
1403 |
|
1404 |
#ifdef ENABLE_XF86_DGA |
1405 |
// DGA available? |
1406 |
int event_base, error_base; |
1407 |
is_fbdev_dga_mode = false; |
1408 |
if (local_X11 && XF86DGAQueryExtension(x_display, &event_base, &error_base)) { |
1409 |
int dga_flags = 0; |
1410 |
XF86DGAQueryDirectVideo(x_display, screen, &dga_flags); |
1411 |
has_dga = dga_flags & XF86DGADirectPresent; |
1412 |
#if defined(__linux__) |
1413 |
// Check r/w permission on /dev/mem for DGA mode to work |
1414 |
if (has_dga && access("/dev/mem", R_OK | W_OK) != 0) |
1415 |
has_dga = false; |
1416 |
#endif |
1417 |
} else |
1418 |
has_dga = false; |
1419 |
#endif |
1420 |
|
1421 |
#ifdef ENABLE_XF86_VIDMODE |
1422 |
// VidMode available? |
1423 |
int vm_event_base, vm_error_base; |
1424 |
has_vidmode = XF86VidModeQueryExtension(x_display, &vm_event_base, &vm_error_base); |
1425 |
if (has_vidmode) { |
1426 |
int vm_major_version, vm_minor_version; |
1427 |
XF86VidModeQueryVersion(x_display, &vm_major_version, &vm_minor_version); |
1428 |
D(bug("VidMode extension %d.%d available\n", vm_major_version, vm_minor_version)); |
1429 |
XF86VidModeGetAllModeLines(x_display, screen, &num_x_video_modes, &x_video_modes); |
1430 |
} |
1431 |
#endif |
1432 |
|
1433 |
#ifdef ENABLE_FBDEV_DGA |
1434 |
// FBDev available? |
1435 |
bool has_fbdev_dga = false; |
1436 |
if (local_X11) { |
1437 |
if ((fb_dev_fd = open("/dev/fb0", O_RDWR)) > 0) { |
1438 |
if (ioctl(fb_dev_fd, FBIOGET_VSCREENINFO, &fb_vinfo) != 0) |
1439 |
close(fb_dev_fd); |
1440 |
else { |
1441 |
has_fbdev_dga = true; |
1442 |
if (!has_dga) { |
1443 |
// Fallback to FBDev DGA mode if XF86 DGA is not possible |
1444 |
has_dga = true; |
1445 |
is_fbdev_dga_mode = true; |
1446 |
} |
1447 |
fb_orig_vinfo = fb_vinfo; |
1448 |
D(bug("Frame buffer device initial resolution: %dx%dx%d\n", fb_vinfo.xres, fb_vinfo.yres, fb_vinfo.bits_per_pixel)); |
1449 |
} |
1450 |
} |
1451 |
} |
1452 |
#endif |
1453 |
|
1454 |
// Find black and white colors |
1455 |
XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:00/00/00", &black); |
1456 |
XAllocColor(x_display, DefaultColormap(x_display, screen), &black); |
1457 |
XParseColor(x_display, DefaultColormap(x_display, screen), "rgb:ff/ff/ff", &white); |
1458 |
XAllocColor(x_display, DefaultColormap(x_display, screen), &white); |
1459 |
black_pixel = BlackPixel(x_display, screen); |
1460 |
white_pixel = WhitePixel(x_display, screen); |
1461 |
|
1462 |
// Mac screen depth follows X depth (for now) |
1463 |
int default_mode = APPLE_8_BIT; |
1464 |
switch (DefaultDepth(x_display, screen)) { |
1465 |
case 1: |
1466 |
default_mode = APPLE_1_BIT; |
1467 |
break; |
1468 |
case 8: |
1469 |
default_mode = APPLE_8_BIT; |
1470 |
break; |
1471 |
case 15: case 16: |
1472 |
default_mode = APPLE_16_BIT; |
1473 |
break; |
1474 |
case 24: case 32: |
1475 |
default_mode = APPLE_32_BIT; |
1476 |
break; |
1477 |
} |
1478 |
|
1479 |
// Get screen mode from preferences |
1480 |
const char *mode_str = PrefsFindString("screen"); |
1481 |
int default_width = 640, default_height = 480; |
1482 |
if (mode_str) { |
1483 |
display_type = DIS_INVALID; |
1484 |
if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2) |
1485 |
display_type = DIS_WINDOW; |
1486 |
#ifdef ENABLE_XF86_DGA |
1487 |
else if (has_dga && sscanf(mode_str, "dga/%d/%d", &default_width, &default_height) == 2) |
1488 |
display_type = DIS_SCREEN; |
1489 |
#endif |
1490 |
#ifdef ENABLE_FBDEV_DGA |
1491 |
else if (has_fbdev_dga && sscanf(mode_str, "fbdev/%d/%d", &default_width, &default_height) == 2) { |
1492 |
is_fbdev_dga_mode = true; |
1493 |
display_type = DIS_SCREEN; |
1494 |
} |
1495 |
#endif |
1496 |
if (display_type == DIS_INVALID) { |
1497 |
D(bug("Invalid screen mode specified, defaulting to old modes selection\n")); |
1498 |
mode_str = NULL; |
1499 |
} |
1500 |
else { |
1501 |
if (default_width <= 0) |
1502 |
default_width = DisplayWidth(x_display, screen); |
1503 |
else if (default_width > DisplayWidth(x_display, screen)) |
1504 |
default_width = DisplayWidth(x_display, screen); |
1505 |
if (default_height <= 0) |
1506 |
default_height = DisplayHeight(x_display, screen); |
1507 |
else if (default_height > DisplayHeight(x_display, screen)) |
1508 |
default_height = DisplayHeight(x_display, screen); |
1509 |
} |
1510 |
} |
1511 |
|
1512 |
// Construct video mode table |
1513 |
uint32 window_modes = PrefsFindInt32("windowmodes"); |
1514 |
uint32 screen_modes = PrefsFindInt32("screenmodes"); |
1515 |
if (!has_dga) |
1516 |
screen_modes = 0; |
1517 |
if (mode_str) |
1518 |
window_modes = screen_modes = 0; |
1519 |
else if (window_modes == 0 && screen_modes == 0) |
1520 |
window_modes |= 3; // Allow at least 640x480 and 800x600 window modes |
1521 |
|
1522 |
VideoInfo *p = VModes; |
1523 |
if (mode_str) { |
1524 |
if (display_type == DIS_WINDOW) { |
1525 |
for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) { |
1526 |
if (find_visual_for_depth(d)) { |
1527 |
if (default_width > 640 && default_height > 480) |
1528 |
add_mode(p, 3, 1, d, APPLE_W_640x480, DIS_WINDOW); |
1529 |
if (default_width > 800 && default_height > 600) |
1530 |
add_mode(p, 3, 2, d, APPLE_W_800x600, DIS_WINDOW); |
1531 |
add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM); |
1532 |
} |
1533 |
} |
1534 |
#ifdef ENABLE_VOSF |
1535 |
} else if (display_type == DIS_SCREEN && is_fbdev_dga_mode) { |
1536 |
for (unsigned int d = APPLE_1_BIT; d <= default_mode; d++) |
1537 |
if (find_visual_for_depth(d)) |
1538 |
add_custom_mode(p, display_type, default_width, default_height, d, APPLE_CUSTOM); |
1539 |
#endif |
1540 |
} else |
1541 |
add_custom_mode(p, display_type, default_width, default_height, default_mode, APPLE_CUSTOM); |
1542 |
|
1543 |
// Add extra VidMode capable modes |
1544 |
if (display_type == DIS_SCREEN) { |
1545 |
struct { |
1546 |
int w; |
1547 |
int h; |
1548 |
int apple_id; |
1549 |
} |
1550 |
video_modes[] = { |
1551 |
{ 640, 480, APPLE_640x480 }, |
1552 |
{ 800, 600, APPLE_800x600 }, |
1553 |
{ 1024, 768, APPLE_1024x768 }, |
1554 |
{ 1152, 768, APPLE_1152x768 }, |
1555 |
{ 1152, 900, APPLE_1152x900 }, |
1556 |
{ 1280, 1024, APPLE_1280x1024 }, |
1557 |
{ 1600, 1200, APPLE_1600x1200 }, |
1558 |
{ 0, } |
1559 |
}; |
1560 |
|
1561 |
for (int i = 0; video_modes[i].w != 0; i++) { |
1562 |
const int w = video_modes[i].w; |
1563 |
const int h = video_modes[i].h; |
1564 |
if (w >= default_width || h >= default_height) |
1565 |
continue; |
1566 |
if (has_mode(w, h)) { |
1567 |
#ifdef ENABLE_VOSF |
1568 |
if (is_fbdev_dga_mode) { |
1569 |
for (unsigned int d = APPLE_1_BIT; d <= default_mode; d++) |
1570 |
if (find_visual_for_depth(d)) |
1571 |
add_custom_mode(p, display_type, w, h, d, video_modes[i].apple_id); |
1572 |
} else |
1573 |
#endif |
1574 |
add_custom_mode(p, display_type, w, h, default_mode, video_modes[i].apple_id); |
1575 |
} |
1576 |
} |
1577 |
} |
1578 |
} else if (window_modes) { |
1579 |
for (unsigned int d = APPLE_1_BIT; d <= APPLE_32_BIT; d++) |
1580 |
if (find_visual_for_depth(d)) |
1581 |
add_window_modes(p, window_modes, d); |
1582 |
} else if (has_vidmode) { |
1583 |
if (has_mode(640, 480)) |
1584 |
add_mode(p, screen_modes, 1, default_mode, APPLE_640x480, DIS_SCREEN); |
1585 |
if (has_mode(800, 600)) |
1586 |
add_mode(p, screen_modes, 2, default_mode, APPLE_800x600, DIS_SCREEN); |
1587 |
if (has_mode(1024, 768)) |
1588 |
add_mode(p, screen_modes, 4, default_mode, APPLE_1024x768, DIS_SCREEN); |
1589 |
if (has_mode(1152, 768)) |
1590 |
add_mode(p, screen_modes, 64, default_mode, APPLE_1152x768, DIS_SCREEN); |
1591 |
if (has_mode(1152, 900)) |
1592 |
add_mode(p, screen_modes, 8, default_mode, APPLE_1152x900, DIS_SCREEN); |
1593 |
if (has_mode(1280, 1024)) |
1594 |
add_mode(p, screen_modes, 16, default_mode, APPLE_1280x1024, DIS_SCREEN); |
1595 |
if (has_mode(1600, 1200)) |
1596 |
add_mode(p, screen_modes, 32, default_mode, APPLE_1600x1200, DIS_SCREEN); |
1597 |
} else if (screen_modes) { |
1598 |
int xsize = DisplayWidth(x_display, screen); |
1599 |
int ysize = DisplayHeight(x_display, screen); |
1600 |
int apple_id = find_apple_resolution(xsize, ysize); |
1601 |
p->viType = DIS_SCREEN; |
1602 |
p->viRowBytes = 0; |
1603 |
p->viXsize = xsize; |
1604 |
p->viYsize = ysize; |
1605 |
p->viAppleMode = default_mode; |
1606 |
p->viAppleID = apple_id; |
1607 |
p++; |
1608 |
} |
1609 |
p->viType = DIS_INVALID; // End marker |
1610 |
p->viRowBytes = 0; |
1611 |
p->viXsize = p->viYsize = 0; |
1612 |
p->viAppleMode = 0; |
1613 |
p->viAppleID = 0; |
1614 |
|
1615 |
// Find default mode (window 640x480) |
1616 |
cur_mode = -1; |
1617 |
if (has_dga && screen_modes) { |
1618 |
int screen_width = DisplayWidth(x_display, screen); |
1619 |
int screen_height = DisplayHeight(x_display, screen); |
1620 |
int apple_id = find_apple_resolution(screen_width, screen_height); |
1621 |
if (apple_id != -1) |
1622 |
cur_mode = find_mode(default_mode, apple_id, DIS_SCREEN); |
1623 |
} else if (has_dga && mode_str) |
1624 |
cur_mode = find_mode(default_mode, APPLE_CUSTOM, DIS_SCREEN); |
1625 |
|
1626 |
if (cur_mode == -1) { |
1627 |
// pick up first windowed mode available |
1628 |
for (VideoInfo *p = VModes; p->viType != DIS_INVALID; p++) { |
1629 |
if (p->viType == DIS_WINDOW && p->viAppleMode == default_mode) { |
1630 |
cur_mode = p - VModes; |
1631 |
break; |
1632 |
} |
1633 |
} |
1634 |
} |
1635 |
assert(cur_mode != -1); |
1636 |
|
1637 |
#if DEBUG |
1638 |
D(bug("Available video modes:\n")); |
1639 |
for (p = VModes; p->viType != DIS_INVALID; p++) { |
1640 |
int bits = depth_of_video_mode(p->viAppleMode); |
1641 |
D(bug(" %dx%d (ID %02x), %d colors\n", p->viXsize, p->viYsize, p->viAppleID, 1 << bits)); |
1642 |
} |
1643 |
#endif |
1644 |
|
1645 |
// Open window/screen |
1646 |
if (!open_display()) |
1647 |
return false; |
1648 |
|
1649 |
#if 0 |
1650 |
// Ignore errors from now on |
1651 |
XSetErrorHandler(ignore_errors); |
1652 |
#endif |
1653 |
|
1654 |
// Lock down frame buffer |
1655 |
XSync(x_display, false); |
1656 |
LOCK_FRAME_BUFFER; |
1657 |
|
1658 |
// Start periodic thread |
1659 |
XSync(x_display, false); |
1660 |
Set_pthread_attr(&redraw_thread_attr, 0); |
1661 |
redraw_thread_cancel = false; |
1662 |
redraw_thread_active = (pthread_create(&redraw_thread, &redraw_thread_attr, redraw_func, NULL) == 0); |
1663 |
D(bug("Redraw thread installed (%ld)\n", redraw_thread)); |
1664 |
return true; |
1665 |
} |
1666 |
|
1667 |
|
1668 |
/* |
1669 |
* Deinitialization |
1670 |
*/ |
1671 |
|
1672 |
void VideoExit(void) |
1673 |
{ |
1674 |
// Stop redraw thread |
1675 |
if (redraw_thread_active) { |
1676 |
redraw_thread_cancel = true; |
1677 |
pthread_cancel(redraw_thread); |
1678 |
pthread_join(redraw_thread, NULL); |
1679 |
redraw_thread_active = false; |
1680 |
} |
1681 |
|
1682 |
// Unlock frame buffer |
1683 |
UNLOCK_FRAME_BUFFER; |
1684 |
XSync(x_display, false); |
1685 |
D(bug(" frame buffer unlocked\n")); |
1686 |
|
1687 |
#ifdef ENABLE_VOSF |
1688 |
if (use_vosf) { |
1689 |
// Deinitialize VOSF |
1690 |
video_vosf_exit(); |
1691 |
} |
1692 |
#endif |
1693 |
|
1694 |
// Close window and server connection |
1695 |
if (x_display != NULL) { |
1696 |
XSync(x_display, false); |
1697 |
close_display(); |
1698 |
XFlush(x_display); |
1699 |
XSync(x_display, false); |
1700 |
} |
1701 |
|
1702 |
#ifdef ENABLE_FBDEV_DGA |
1703 |
// Close framebuffer device |
1704 |
if (fb_dev_fd >= 0) { |
1705 |
close(fb_dev_fd); |
1706 |
fb_dev_fd = -1; |
1707 |
} |
1708 |
#endif |
1709 |
} |
1710 |
|
1711 |
|
1712 |
/* |
1713 |
* Suspend/resume emulator |
1714 |
*/ |
1715 |
|
1716 |
static void suspend_emul(void) |
1717 |
{ |
1718 |
if (display_type == DIS_SCREEN) { |
1719 |
// Release ctrl key |
1720 |
ADBKeyUp(0x36); |
1721 |
ctrl_down = false; |
1722 |
|
1723 |
// Lock frame buffer (this will stop the MacOS thread) |
1724 |
LOCK_FRAME_BUFFER; |
1725 |
|
1726 |
// Save frame buffer |
1727 |
fb_save = malloc(VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes); |
1728 |
if (fb_save) |
1729 |
Mac2Host_memcpy(fb_save, screen_base, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes); |
1730 |
|
1731 |
// Close full screen display |
1732 |
#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) |
1733 |
#ifdef ENABLE_XF86_DGA |
1734 |
if (!is_fbdev_dga_mode) |
1735 |
XF86DGADirectVideo(x_display, screen, 0); |
1736 |
#endif |
1737 |
XUngrabPointer(x_display, CurrentTime); |
1738 |
XUngrabKeyboard(x_display, CurrentTime); |
1739 |
#endif |
1740 |
restore_mouse_accel(); |
1741 |
XUnmapWindow(x_display, the_win); |
1742 |
wait_unmapped(the_win); |
1743 |
XSync(x_display, false); |
1744 |
|
1745 |
// Open "suspend" window |
1746 |
XSetWindowAttributes wattr; |
1747 |
wattr.event_mask = KeyPressMask; |
1748 |
wattr.background_pixel = (vis == DefaultVisual(x_display, screen) ? black_pixel : 0); |
1749 |
wattr.backing_store = Always; |
1750 |
wattr.colormap = (depth == 1 ? DefaultColormap(x_display, screen) : cmap[0]); |
1751 |
|
1752 |
suspend_win = XCreateWindow(x_display, rootwin, 0, 0, 512, 1, 0, xdepth, |
1753 |
InputOutput, vis, CWEventMask | CWBackPixel | CWBackingStore | CWColormap, &wattr); |
1754 |
set_window_name(suspend_win, STR_SUSPEND_WINDOW_TITLE); |
1755 |
set_window_focus(suspend_win); |
1756 |
XMapWindow(x_display, suspend_win); |
1757 |
emul_suspended = true; |
1758 |
} |
1759 |
} |
1760 |
|
1761 |
static void resume_emul(void) |
1762 |
{ |
1763 |
// Close "suspend" window |
1764 |
XDestroyWindow(x_display, suspend_win); |
1765 |
XSync(x_display, false); |
1766 |
|
1767 |
// Reopen full screen display |
1768 |
XMapRaised(x_display, the_win); |
1769 |
wait_mapped(the_win); |
1770 |
XWarpPointer(x_display, None, rootwin, 0, 0, 0, 0, 0, 0); |
1771 |
Window w = is_fbdev_dga_mode ? the_win : rootwin; |
1772 |
XGrabKeyboard(x_display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime); |
1773 |
XGrabPointer(x_display, w, True, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, GrabModeAsync, GrabModeAsync, is_fbdev_dga_mode ? w : None, None, CurrentTime); |
1774 |
disable_mouse_accel(); |
1775 |
#ifdef ENABLE_XF86_DGA |
1776 |
if (!is_fbdev_dga_mode) { |
1777 |
XF86DGADirectVideo(x_display, screen, XF86DGADirectGraphics | XF86DGADirectKeyb | XF86DGADirectMouse); |
1778 |
XF86DGASetViewPort(x_display, screen, 0, 0); |
1779 |
} |
1780 |
#endif |
1781 |
XSync(x_display, false); |
1782 |
|
1783 |
// the_buffer already contains the data to restore. i.e. since a temporary |
1784 |
// frame buffer is used when VOSF is actually used, fb_save is therefore |
1785 |
// not necessary. |
1786 |
#ifdef ENABLE_VOSF |
1787 |
if (use_vosf) { |
1788 |
LOCK_VOSF; |
1789 |
PFLAG_SET_ALL; |
1790 |
memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); |
1791 |
UNLOCK_VOSF; |
1792 |
} |
1793 |
#endif |
1794 |
|
1795 |
// Restore frame buffer |
1796 |
if (fb_save) { |
1797 |
#ifdef ENABLE_VOSF |
1798 |
// Don't copy fb_save to the temporary frame buffer in VOSF mode |
1799 |
if (!use_vosf) |
1800 |
#endif |
1801 |
Host2Mac_memcpy(screen_base, fb_save, VModes[cur_mode].viYsize * VModes[cur_mode].viRowBytes); |
1802 |
free(fb_save); |
1803 |
fb_save = NULL; |
1804 |
} |
1805 |
|
1806 |
// Unlock frame buffer (and continue MacOS thread) |
1807 |
UNLOCK_FRAME_BUFFER; |
1808 |
emul_suspended = false; |
1809 |
} |
1810 |
|
1811 |
|
1812 |
/* |
1813 |
* Close screen in full-screen mode |
1814 |
*/ |
1815 |
|
1816 |
void VideoQuitFullScreen(void) |
1817 |
{ |
1818 |
D(bug("VideoQuitFullScreen()\n")); |
1819 |
if (display_type == DIS_SCREEN) { |
1820 |
quit_full_screen = true; |
1821 |
while (!quit_full_screen_ack) ; |
1822 |
} |
1823 |
} |
1824 |
|
1825 |
|
1826 |
/* |
1827 |
* X11 event handling |
1828 |
*/ |
1829 |
|
1830 |
// Translate key event to Mac keycode |
1831 |
static int kc_decode(KeySym ks) |
1832 |
{ |
1833 |
switch (ks) { |
1834 |
case XK_A: case XK_a: return 0x00; |
1835 |
case XK_B: case XK_b: return 0x0b; |
1836 |
case XK_C: case XK_c: return 0x08; |
1837 |
case XK_D: case XK_d: return 0x02; |
1838 |
case XK_E: case XK_e: return 0x0e; |
1839 |
case XK_F: case XK_f: return 0x03; |
1840 |
case XK_G: case XK_g: return 0x05; |
1841 |
case XK_H: case XK_h: return 0x04; |
1842 |
case XK_I: case XK_i: return 0x22; |
1843 |
case XK_J: case XK_j: return 0x26; |
1844 |
case XK_K: case XK_k: return 0x28; |
1845 |
case XK_L: case XK_l: return 0x25; |
1846 |
case XK_M: case XK_m: return 0x2e; |
1847 |
case XK_N: case XK_n: return 0x2d; |
1848 |
case XK_O: case XK_o: return 0x1f; |
1849 |
case XK_P: case XK_p: return 0x23; |
1850 |
case XK_Q: case XK_q: return 0x0c; |
1851 |
case XK_R: case XK_r: return 0x0f; |
1852 |
case XK_S: case XK_s: return 0x01; |
1853 |
case XK_T: case XK_t: return 0x11; |
1854 |
case XK_U: case XK_u: return 0x20; |
1855 |
case XK_V: case XK_v: return 0x09; |
1856 |
case XK_W: case XK_w: return 0x0d; |
1857 |
case XK_X: case XK_x: return 0x07; |
1858 |
case XK_Y: case XK_y: return 0x10; |
1859 |
case XK_Z: case XK_z: return 0x06; |
1860 |
|
1861 |
case XK_1: case XK_exclam: return 0x12; |
1862 |
case XK_2: case XK_at: return 0x13; |
1863 |
case XK_3: case XK_numbersign: return 0x14; |
1864 |
case XK_4: case XK_dollar: return 0x15; |
1865 |
case XK_5: case XK_percent: return 0x17; |
1866 |
case XK_6: return 0x16; |
1867 |
case XK_7: return 0x1a; |
1868 |
case XK_8: return 0x1c; |
1869 |
case XK_9: return 0x19; |
1870 |
case XK_0: return 0x1d; |
1871 |
|
1872 |
case XK_grave: case XK_asciitilde: return 0x0a; |
1873 |
case XK_minus: case XK_underscore: return 0x1b; |
1874 |
case XK_equal: case XK_plus: return 0x18; |
1875 |
case XK_bracketleft: case XK_braceleft: return 0x21; |
1876 |
case XK_bracketright: case XK_braceright: return 0x1e; |
1877 |
case XK_backslash: case XK_bar: return 0x2a; |
1878 |
case XK_semicolon: case XK_colon: return 0x29; |
1879 |
case XK_apostrophe: case XK_quotedbl: return 0x27; |
1880 |
case XK_comma: case XK_less: return 0x2b; |
1881 |
case XK_period: case XK_greater: return 0x2f; |
1882 |
case XK_slash: case XK_question: return 0x2c; |
1883 |
|
1884 |
case XK_Tab: if (ctrl_down) {suspend_emul(); return -1;} else return 0x30; |
1885 |
case XK_Return: return 0x24; |
1886 |
case XK_space: return 0x31; |
1887 |
case XK_BackSpace: return 0x33; |
1888 |
|
1889 |
case XK_Delete: return 0x75; |
1890 |
case XK_Insert: return 0x72; |
1891 |
case XK_Home: case XK_Help: return 0x73; |
1892 |
case XK_End: return 0x77; |
1893 |
#ifdef __hpux |
1894 |
case XK_Prior: return 0x74; |
1895 |
case XK_Next: return 0x79; |
1896 |
#else |
1897 |
case XK_Page_Up: return 0x74; |
1898 |
case XK_Page_Down: return 0x79; |
1899 |
#endif |
1900 |
|
1901 |
case XK_Control_L: return 0x36; |
1902 |
case XK_Control_R: return 0x36; |
1903 |
case XK_Shift_L: return 0x38; |
1904 |
case XK_Shift_R: return 0x38; |
1905 |
case XK_Alt_L: return 0x37; |
1906 |
case XK_Alt_R: return 0x37; |
1907 |
case XK_Meta_L: return 0x3a; |
1908 |
case XK_Meta_R: return 0x3a; |
1909 |
case XK_Menu: return 0x32; |
1910 |
case XK_Caps_Lock: return 0x39; |
1911 |
case XK_Num_Lock: return 0x47; |
1912 |
|
1913 |
case XK_Up: return 0x3e; |
1914 |
case XK_Down: return 0x3d; |
1915 |
case XK_Left: return 0x3b; |
1916 |
case XK_Right: return 0x3c; |
1917 |
|
1918 |
case XK_Escape: if (ctrl_down) {quit_full_screen = true; emerg_quit = true; return -1;} else return 0x35; |
1919 |
|
1920 |
case XK_F1: if (ctrl_down) {SysMountFirstFloppy(); return -1;} else return 0x7a; |
1921 |
case XK_F2: return 0x78; |
1922 |
case XK_F3: return 0x63; |
1923 |
case XK_F4: return 0x76; |
1924 |
case XK_F5: return 0x60; |
1925 |
case XK_F6: return 0x61; |
1926 |
case XK_F7: return 0x62; |
1927 |
case XK_F8: return 0x64; |
1928 |
case XK_F9: return 0x65; |
1929 |
case XK_F10: return 0x6d; |
1930 |
case XK_F11: return 0x67; |
1931 |
case XK_F12: return 0x6f; |
1932 |
|
1933 |
case XK_Print: return 0x69; |
1934 |
case XK_Scroll_Lock: return 0x6b; |
1935 |
case XK_Pause: return 0x71; |
1936 |
|
1937 |
#if defined(XK_KP_Prior) && defined(XK_KP_Left) && defined(XK_KP_Insert) && defined (XK_KP_End) |
1938 |
case XK_KP_0: case XK_KP_Insert: return 0x52; |
1939 |
case XK_KP_1: case XK_KP_End: return 0x53; |
1940 |
case XK_KP_2: case XK_KP_Down: return 0x54; |
1941 |
case XK_KP_3: case XK_KP_Next: return 0x55; |
1942 |
case XK_KP_4: case XK_KP_Left: return 0x56; |
1943 |
case XK_KP_5: case XK_KP_Begin: return 0x57; |
1944 |
case XK_KP_6: case XK_KP_Right: return 0x58; |
1945 |
case XK_KP_7: case XK_KP_Home: return 0x59; |
1946 |
case XK_KP_8: case XK_KP_Up: return 0x5b; |
1947 |
case XK_KP_9: case XK_KP_Prior: return 0x5c; |
1948 |
case XK_KP_Decimal: case XK_KP_Delete: return 0x41; |
1949 |
#else |
1950 |
case XK_KP_0: return 0x52; |
1951 |
case XK_KP_1: return 0x53; |
1952 |
case XK_KP_2: return 0x54; |
1953 |
case XK_KP_3: return 0x55; |
1954 |
case XK_KP_4: return 0x56; |
1955 |
case XK_KP_5: return 0x57; |
1956 |
case XK_KP_6: return 0x58; |
1957 |
case XK_KP_7: return 0x59; |
1958 |
case XK_KP_8: return 0x5b; |
1959 |
case XK_KP_9: return 0x5c; |
1960 |
case XK_KP_Decimal: return 0x41; |
1961 |
#endif |
1962 |
case XK_KP_Add: return 0x45; |
1963 |
case XK_KP_Subtract: return 0x4e; |
1964 |
case XK_KP_Multiply: return 0x43; |
1965 |
case XK_KP_Divide: return 0x4b; |
1966 |
case XK_KP_Enter: return 0x4c; |
1967 |
case XK_KP_Equal: return 0x51; |
1968 |
} |
1969 |
return -1; |
1970 |
} |
1971 |
|
1972 |
static int event2keycode(XKeyEvent &ev, bool key_down) |
1973 |
{ |
1974 |
KeySym ks; |
1975 |
int i = 0; |
1976 |
|
1977 |
do { |
1978 |
ks = XLookupKeysym(&ev, i++); |
1979 |
int as = kc_decode(ks); |
1980 |
if (as >= 0) |
1981 |
return as; |
1982 |
if (as == -2) |
1983 |
return as; |
1984 |
} while (ks != NoSymbol); |
1985 |
|
1986 |
return -1; |
1987 |
} |
1988 |
|
1989 |
static void handle_events(void) |
1990 |
{ |
1991 |
// Handle events |
1992 |
for (;;) { |
1993 |
XEvent event; |
1994 |
|
1995 |
XDisplayLock(); |
1996 |
if (!XCheckMaskEvent(x_display, eventmask, &event)) { |
1997 |
// Handle clipboard events |
1998 |
if (XCheckTypedEvent(x_display, SelectionRequest, &event)) |
1999 |
ClipboardSelectionRequest(&event.xselectionrequest); |
2000 |
else if (XCheckTypedEvent(x_display, SelectionClear, &event)) |
2001 |
ClipboardSelectionClear(&event.xselectionclear); |
2002 |
|
2003 |
// Window "close" widget clicked |
2004 |
else if (XCheckTypedEvent(x_display, ClientMessage, &event)) { |
2005 |
if (event.xclient.format == 32 && event.xclient.data.l[0] == WM_DELETE_WINDOW) { |
2006 |
ADBKeyDown(0x7f); // Power key |
2007 |
ADBKeyUp(0x7f); |
2008 |
} |
2009 |
} |
2010 |
|
2011 |
XDisplayUnlock(); |
2012 |
break; |
2013 |
} |
2014 |
XDisplayUnlock(); |
2015 |
|
2016 |
switch (event.type) { |
2017 |
// Mouse button |
2018 |
case ButtonPress: { |
2019 |
unsigned int button = ((XButtonEvent *)&event)->button; |
2020 |
if (button < 4) |
2021 |
ADBMouseDown(button - 1); |
2022 |
else if (button < 6) { // Wheel mouse |
2023 |
if (mouse_wheel_mode == 0) { |
2024 |
int key = (button == 5) ? 0x79 : 0x74; // Page up/down |
2025 |
ADBKeyDown(key); |
2026 |
ADBKeyUp(key); |
2027 |
} else { |
2028 |
int key = (button == 5) ? 0x3d : 0x3e; // Cursor up/down |
2029 |
for(int i=0; i<mouse_wheel_lines; i++) { |
2030 |
ADBKeyDown(key); |
2031 |
ADBKeyUp(key); |
2032 |
} |
2033 |
} |
2034 |
} |
2035 |
break; |
2036 |
} |
2037 |
case ButtonRelease: { |
2038 |
unsigned int button = ((XButtonEvent *)&event)->button; |
2039 |
if (button < 4) |
2040 |
ADBMouseUp(button - 1); |
2041 |
break; |
2042 |
} |
2043 |
|
2044 |
// Mouse entered window |
2045 |
case EnterNotify: |
2046 |
if (event.xcrossing.mode != NotifyGrab && event.xcrossing.mode != NotifyUngrab) |
2047 |
ADBMouseMoved(event.xmotion.x, event.xmotion.y); |
2048 |
break; |
2049 |
|
2050 |
// Mouse moved |
2051 |
case MotionNotify: |
2052 |
ADBMouseMoved(event.xmotion.x, event.xmotion.y); |
2053 |
break; |
2054 |
|
2055 |
// Keyboard |
2056 |
case KeyPress: { |
2057 |
int code = -1; |
2058 |
if (use_keycodes) { |
2059 |
if (event2keycode(event.xkey, true) != -2) // This is called to process the hotkeys |
2060 |
code = keycode_table[event.xkey.keycode & 0xff]; |
2061 |
} else |
2062 |
code = event2keycode(event.xkey, true); |
2063 |
if (code >= 0) { |
2064 |
if (!emul_suspended) { |
2065 |
if (code == 0x39) { // Caps Lock pressed |
2066 |
if (caps_on) { |
2067 |
ADBKeyUp(code); |
2068 |
caps_on = false; |
2069 |
} else { |
2070 |
ADBKeyDown(code); |
2071 |
caps_on = true; |
2072 |
} |
2073 |
} else |
2074 |
ADBKeyDown(code); |
2075 |
if (code == 0x36) |
2076 |
ctrl_down = true; |
2077 |
} else { |
2078 |
if (code == 0x31) |
2079 |
resume_emul(); // Space wakes us up |
2080 |
} |
2081 |
} |
2082 |
break; |
2083 |
} |
2084 |
case KeyRelease: { |
2085 |
int code = -1; |
2086 |
if (use_keycodes) { |
2087 |
if (event2keycode(event.xkey, false) != -2) // This is called to process the hotkeys |
2088 |
code = keycode_table[event.xkey.keycode & 0xff]; |
2089 |
} else |
2090 |
code = event2keycode(event.xkey, false); |
2091 |
if (code >= 0 && code != 0x39) { // Don't propagate Caps Lock releases |
2092 |
ADBKeyUp(code); |
2093 |
if (code == 0x36) |
2094 |
ctrl_down = false; |
2095 |
} |
2096 |
break; |
2097 |
} |
2098 |
|
2099 |
// Hidden parts exposed, force complete refresh |
2100 |
case Expose: |
2101 |
#ifdef ENABLE_VOSF |
2102 |
if (use_vosf) { // VOSF refresh |
2103 |
LOCK_VOSF; |
2104 |
PFLAG_SET_ALL; |
2105 |
memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); |
2106 |
UNLOCK_VOSF; |
2107 |
} |
2108 |
else |
2109 |
#endif |
2110 |
memset(the_buffer_copy, 0, VModes[cur_mode].viRowBytes * VModes[cur_mode].viYsize); |
2111 |
break; |
2112 |
} |
2113 |
} |
2114 |
} |
2115 |
|
2116 |
|
2117 |
/* |
2118 |
* Execute video VBL routine |
2119 |
*/ |
2120 |
|
2121 |
void VideoVBL(void) |
2122 |
{ |
2123 |
if (emerg_quit) |
2124 |
QuitEmulator(); |
2125 |
|
2126 |
// Temporarily give up frame buffer lock (this is the point where |
2127 |
// we are suspended when the user presses Ctrl-Tab) |
2128 |
UNLOCK_FRAME_BUFFER; |
2129 |
LOCK_FRAME_BUFFER; |
2130 |
|
2131 |
// Execute video VBL |
2132 |
if (private_data != NULL && private_data->interruptsEnabled) |
2133 |
VSLDoInterruptService(private_data->vslServiceID); |
2134 |
} |
2135 |
|
2136 |
|
2137 |
/* |
2138 |
* Change video mode |
2139 |
*/ |
2140 |
|
2141 |
int16 video_mode_change(VidLocals *csSave, uint32 ParamPtr) |
2142 |
{ |
2143 |
/* return if no mode change */ |
2144 |
if ((csSave->saveData == ReadMacInt32(ParamPtr + csData)) && |
2145 |
(csSave->saveMode == ReadMacInt16(ParamPtr + csMode))) return noErr; |
2146 |
|
2147 |
/* first find video mode in table */ |
2148 |
for (int i=0; VModes[i].viType != DIS_INVALID; i++) { |
2149 |
if ((ReadMacInt16(ParamPtr + csMode) == VModes[i].viAppleMode) && |
2150 |
(ReadMacInt32(ParamPtr + csData) == VModes[i].viAppleID)) { |
2151 |
csSave->saveMode = ReadMacInt16(ParamPtr + csMode); |
2152 |
csSave->saveData = ReadMacInt32(ParamPtr + csData); |
2153 |
csSave->savePage = ReadMacInt16(ParamPtr + csPage); |
2154 |
|
2155 |
// Disable interrupts and pause redraw thread |
2156 |
DisableInterrupt(); |
2157 |
thread_stop_ack = false; |
2158 |
thread_stop_req = true; |
2159 |
while (!thread_stop_ack) ; |
2160 |
|
2161 |
/* close old display */ |
2162 |
close_display(); |
2163 |
|
2164 |
/* open new display */ |
2165 |
cur_mode = i; |
2166 |
bool ok = open_display(); |
2167 |
|
2168 |
/* opening the screen failed? Then bail out */ |
2169 |
if (!ok) { |
2170 |
ErrorAlert(GetString(STR_FULL_SCREEN_ERR)); |
2171 |
QuitEmulator(); |
2172 |
} |
2173 |
|
2174 |
WriteMacInt32(ParamPtr + csBaseAddr, screen_base); |
2175 |
csSave->saveBaseAddr=screen_base; |
2176 |
csSave->saveData=VModes[cur_mode].viAppleID;/* First mode ... */ |
2177 |
csSave->saveMode=VModes[cur_mode].viAppleMode; |
2178 |
|
2179 |
// Enable interrupts and resume redraw thread |
2180 |
thread_stop_req = false; |
2181 |
EnableInterrupt(); |
2182 |
return noErr; |
2183 |
} |
2184 |
} |
2185 |
return paramErr; |
2186 |
} |
2187 |
|
2188 |
|
2189 |
/* |
2190 |
* Set color palette |
2191 |
*/ |
2192 |
|
2193 |
void video_set_palette(void) |
2194 |
{ |
2195 |
LOCK_PALETTE; |
2196 |
|
2197 |
// Convert colors to XColor array |
2198 |
int mode = get_current_mode(); |
2199 |
int num_in = palette_size(mode); |
2200 |
int num_out = 256; |
2201 |
bool stretch = false; |
2202 |
if (IsDirectMode(mode)) { |
2203 |
// If X is in 565 mode we have to stretch the gamma table from 32 to 64 entries |
2204 |
num_out = vis->map_entries; |
2205 |
stretch = true; |
2206 |
} |
2207 |
XColor *p = x_palette; |
2208 |
for (int i=0; i<num_out; i++) { |
2209 |
int c = (stretch ? (i * num_in) / num_out : i); |
2210 |
p->red = mac_pal[c].red * 0x0101; |
2211 |
p->green = mac_pal[c].green * 0x0101; |
2212 |
p->blue = mac_pal[c].blue * 0x0101; |
2213 |
p++; |
2214 |
} |
2215 |
|
2216 |
#ifdef ENABLE_VOSF |
2217 |
// Recalculate pixel color expansion map |
2218 |
if (!IsDirectMode(mode) && xdepth > 8) { |
2219 |
for (int i=0; i<256; i++) { |
2220 |
int c = i & (num_in-1); // If there are less than 256 colors, we repeat the first entries (this makes color expansion easier) |
2221 |
ExpandMap[i] = map_rgb(mac_pal[c].red, mac_pal[c].green, mac_pal[c].blue); |
2222 |
} |
2223 |
|
2224 |
// We have to redraw everything because the interpretation of pixel values changed |
2225 |
LOCK_VOSF; |
2226 |
PFLAG_SET_ALL; |
2227 |
if (display_type == DIS_SCREEN) |
2228 |
PFLAG_SET_VERY_DIRTY; |
2229 |
UNLOCK_VOSF; |
2230 |
} |
2231 |
#endif |
2232 |
|
2233 |
// Tell redraw thread to change palette |
2234 |
palette_changed = true; |
2235 |
|
2236 |
UNLOCK_PALETTE; |
2237 |
} |
2238 |
|
2239 |
|
2240 |
/* |
2241 |
* Can we set the MacOS cursor image into the window? |
2242 |
*/ |
2243 |
|
2244 |
bool video_can_change_cursor(void) |
2245 |
{ |
2246 |
return hw_mac_cursor_accl && (display_type != DIS_SCREEN); |
2247 |
} |
2248 |
|
2249 |
|
2250 |
/* |
2251 |
* Set cursor image for window |
2252 |
*/ |
2253 |
|
2254 |
void video_set_cursor(void) |
2255 |
{ |
2256 |
cursor_changed = true; |
2257 |
} |
2258 |
|
2259 |
|
2260 |
/* |
2261 |
* Thread for window refresh, event handling and other periodic actions |
2262 |
*/ |
2263 |
|
2264 |
static void update_display(void) |
2265 |
{ |
2266 |
// Incremental update code |
2267 |
int wide = 0, high = 0, x1, x2, y1, y2, i, j; |
2268 |
int bytes_per_row = VModes[cur_mode].viRowBytes; |
2269 |
int bytes_per_pixel = VModes[cur_mode].viRowBytes / VModes[cur_mode].viXsize; |
2270 |
uint8 *p, *p2; |
2271 |
|
2272 |
// Check for first line from top and first line from bottom that have changed |
2273 |
y1 = 0; |
2274 |
for (j=0; j<VModes[cur_mode].viYsize; j++) { |
2275 |
if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) { |
2276 |
y1 = j; |
2277 |
break; |
2278 |
} |
2279 |
} |
2280 |
y2 = y1 - 1; |
2281 |
for (j=VModes[cur_mode].viYsize-1; j>=y1; j--) { |
2282 |
if (memcmp(&the_buffer[j * bytes_per_row], &the_buffer_copy[j * bytes_per_row], bytes_per_row)) { |
2283 |
y2 = j; |
2284 |
break; |
2285 |
} |
2286 |
} |
2287 |
high = y2 - y1 + 1; |
2288 |
|
2289 |
// Check for first column from left and first column from right that have changed |
2290 |
if (high) { |
2291 |
if (depth == 1) { |
2292 |
x1 = VModes[cur_mode].viXsize; |
2293 |
for (j=y1; j<=y2; j++) { |
2294 |
p = &the_buffer[j * bytes_per_row]; |
2295 |
p2 = &the_buffer_copy[j * bytes_per_row]; |
2296 |
for (i=0; i<(x1>>3); i++) { |
2297 |
if (*p != *p2) { |
2298 |
x1 = i << 3; |
2299 |
break; |
2300 |
} |
2301 |
p++; |
2302 |
p2++; |
2303 |
} |
2304 |
} |
2305 |
x2 = x1; |
2306 |
for (j=y1; j<=y2; j++) { |
2307 |
p = &the_buffer[j * bytes_per_row]; |
2308 |
p2 = &the_buffer_copy[j * bytes_per_row]; |
2309 |
p += bytes_per_row; |
2310 |
p2 += bytes_per_row; |
2311 |
for (i=(VModes[cur_mode].viXsize>>3); i>(x2>>3); i--) { |
2312 |
p--; |
2313 |
p2--; |
2314 |
if (*p != *p2) { |
2315 |
x2 = i << 3; |
2316 |
break; |
2317 |
} |
2318 |
} |
2319 |
} |
2320 |
wide = x2 - x1; |
2321 |
|
2322 |
// Update copy of the_buffer |
2323 |
if (high && wide) { |
2324 |
for (j=y1; j<=y2; j++) { |
2325 |
i = j * bytes_per_row + (x1 >> 3); |
2326 |
memcpy(&the_buffer_copy[i], &the_buffer[i], wide >> 3); |
2327 |
} |
2328 |
} |
2329 |
|
2330 |
} else { |
2331 |
x1 = VModes[cur_mode].viXsize; |
2332 |
for (j=y1; j<=y2; j++) { |
2333 |
p = &the_buffer[j * bytes_per_row]; |
2334 |
p2 = &the_buffer_copy[j * bytes_per_row]; |
2335 |
for (i=0; i<x1; i++) { |
2336 |
if (memcmp(p, p2, bytes_per_pixel)) { |
2337 |
x1 = i; |
2338 |
break; |
2339 |
} |
2340 |
p += bytes_per_pixel; |
2341 |
p2 += bytes_per_pixel; |
2342 |
} |
2343 |
} |
2344 |
x2 = x1; |
2345 |
for (j=y1; j<=y2; j++) { |
2346 |
p = &the_buffer[j * bytes_per_row]; |
2347 |
p2 = &the_buffer_copy[j * bytes_per_row]; |
2348 |
p += bytes_per_row; |
2349 |
p2 += bytes_per_row; |
2350 |
for (i=VModes[cur_mode].viXsize; i>x2; i--) { |
2351 |
p -= bytes_per_pixel; |
2352 |
p2 -= bytes_per_pixel; |
2353 |
if (memcmp(p, p2, bytes_per_pixel)) { |
2354 |
x2 = i; |
2355 |
break; |
2356 |
} |
2357 |
} |
2358 |
} |
2359 |
wide = x2 - x1; |
2360 |
|
2361 |
// Update copy of the_buffer |
2362 |
if (high && wide) { |
2363 |
for (j=y1; j<=y2; j++) { |
2364 |
i = j * bytes_per_row + x1 * bytes_per_pixel; |
2365 |
memcpy(&the_buffer_copy[i], &the_buffer[i], bytes_per_pixel * wide); |
2366 |
} |
2367 |
} |
2368 |
} |
2369 |
} |
2370 |
|
2371 |
// Refresh display |
2372 |
if (high && wide) { |
2373 |
XDisplayLock(); |
2374 |
if (have_shm) |
2375 |
XShmPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high, 0); |
2376 |
else |
2377 |
XPutImage(x_display, the_win, the_gc, img, x1, y1, x1, y1, wide, high); |
2378 |
XDisplayUnlock(); |
2379 |
} |
2380 |
} |
2381 |
|
2382 |
const int VIDEO_REFRESH_HZ = 60; |
2383 |
const int VIDEO_REFRESH_DELAY = 1000000 / VIDEO_REFRESH_HZ; |
2384 |
|
2385 |
static void handle_palette_changes(void) |
2386 |
{ |
2387 |
LOCK_PALETTE; |
2388 |
|
2389 |
if (palette_changed && !emul_suspended) { |
2390 |
palette_changed = false; |
2391 |
|
2392 |
int mode = get_current_mode(); |
2393 |
if (color_class == PseudoColor || color_class == DirectColor) { |
2394 |
int num = vis->map_entries; |
2395 |
bool set_clut = true; |
2396 |
if (!IsDirectMode(mode) && color_class == DirectColor) { |
2397 |
if (display_type == DIS_WINDOW) |
2398 |
set_clut = false; // Indexed mode on true color screen, don't set CLUT |
2399 |
} |
2400 |
|
2401 |
if (set_clut) { |
2402 |
XDisplayLock(); |
2403 |
XStoreColors(x_display, cmap[0], x_palette, num); |
2404 |
XStoreColors(x_display, cmap[1], x_palette, num); |
2405 |
XSync(x_display, false); |
2406 |
XDisplayUnlock(); |
2407 |
} |
2408 |
} |
2409 |
|
2410 |
#ifdef ENABLE_XF86_DGA |
2411 |
if (display_type == DIS_SCREEN && !is_fbdev_dga_mode) { |
2412 |
current_dga_cmap ^= 1; |
2413 |
if (!IsDirectMode(mode) && cmap[current_dga_cmap]) |
2414 |
XF86DGAInstallColormap(x_display, screen, cmap[current_dga_cmap]); |
2415 |
} |
2416 |
#endif |
2417 |
} |
2418 |
|
2419 |
UNLOCK_PALETTE; |
2420 |
} |
2421 |
|
2422 |
static void *redraw_func(void *arg) |
2423 |
{ |
2424 |
int fd = ConnectionNumber(x_display); |
2425 |
|
2426 |
uint64 start = GetTicks_usec(); |
2427 |
int64 ticks = 0; |
2428 |
uint64 next = GetTicks_usec() + VIDEO_REFRESH_DELAY; |
2429 |
|
2430 |
while (!redraw_thread_cancel) { |
2431 |
|
2432 |
// Pause if requested (during video mode switches) |
2433 |
while (thread_stop_req) |
2434 |
thread_stop_ack = true; |
2435 |
|
2436 |
int64 delay = next - GetTicks_usec(); |
2437 |
if (delay < -VIDEO_REFRESH_DELAY) { |
2438 |
|
2439 |
// We are lagging far behind, so we reset the delay mechanism |
2440 |
next = GetTicks_usec(); |
2441 |
|
2442 |
} else if (delay <= 0) { |
2443 |
|
2444 |
// Delay expired, refresh display |
2445 |
next += VIDEO_REFRESH_DELAY; |
2446 |
ticks++; |
2447 |
|
2448 |
// Handle X11 events |
2449 |
handle_events(); |
2450 |
|
2451 |
// Quit DGA mode if requested |
2452 |
if (quit_full_screen) { |
2453 |
quit_full_screen = false; |
2454 |
if (display_type == DIS_SCREEN) { |
2455 |
XDisplayLock(); |
2456 |
#if defined(ENABLE_XF86_DGA) || defined(ENABLE_FBDEV_DGA) |
2457 |
#ifdef ENABLE_XF86_DGA |
2458 |
if (!is_fbdev_dga_mode) |
2459 |
XF86DGADirectVideo(x_display, screen, 0); |
2460 |
#endif |
2461 |
XUngrabPointer(x_display, CurrentTime); |
2462 |
XUngrabKeyboard(x_display, CurrentTime); |
2463 |
XUnmapWindow(x_display, the_win); |
2464 |
wait_unmapped(the_win); |
2465 |
XDestroyWindow(x_display, the_win); |
2466 |
#endif |
2467 |
XSync(x_display, false); |
2468 |
XDisplayUnlock(); |
2469 |
quit_full_screen_ack = true; |
2470 |
return NULL; |
2471 |
} |
2472 |
} |
2473 |
|
2474 |
// Refresh display and set cursor image in window mode |
2475 |
static int tick_counter = 0; |
2476 |
if (display_type == DIS_WINDOW) { |
2477 |
tick_counter++; |
2478 |
if (tick_counter >= frame_skip) { |
2479 |
tick_counter = 0; |
2480 |
|
2481 |
// Update display |
2482 |
#ifdef ENABLE_VOSF |
2483 |
if (use_vosf) { |
2484 |
XDisplayLock(); |
2485 |
if (mainBuffer.dirty) { |
2486 |
LOCK_VOSF; |
2487 |
update_display_window_vosf(); |
2488 |
UNLOCK_VOSF; |
2489 |
XSync(x_display, false); // Let the server catch up |
2490 |
} |
2491 |
XDisplayUnlock(); |
2492 |
} |
2493 |
else |
2494 |
#endif |
2495 |
update_display(); |
2496 |
|
2497 |
// Set new cursor image if it was changed |
2498 |
if (hw_mac_cursor_accl && cursor_changed) { |
2499 |
cursor_changed = false; |
2500 |
uint8 *x_data = (uint8 *)cursor_image->data; |
2501 |
uint8 *x_mask = (uint8 *)cursor_mask_image->data; |
2502 |
for (int i = 0; i < 32; i++) { |
2503 |
x_mask[i] = MacCursor[4 + i] | MacCursor[36 + i]; |
2504 |
x_data[i] = MacCursor[4 + i]; |
2505 |
} |
2506 |
XDisplayLock(); |
2507 |
XFreeCursor(x_display, mac_cursor); |
2508 |
XPutImage(x_display, cursor_map, cursor_gc, cursor_image, 0, 0, 0, 0, 16, 16); |
2509 |
XPutImage(x_display, cursor_mask_map, cursor_mask_gc, cursor_mask_image, 0, 0, 0, 0, 16, 16); |
2510 |
mac_cursor = XCreatePixmapCursor(x_display, cursor_map, cursor_mask_map, &black, &white, MacCursor[2], MacCursor[3]); |
2511 |
XDefineCursor(x_display, the_win, mac_cursor); |
2512 |
XDisplayUnlock(); |
2513 |
} |
2514 |
} |
2515 |
} |
2516 |
#ifdef ENABLE_VOSF |
2517 |
else if (use_vosf) { |
2518 |
// Update display (VOSF variant) |
2519 |
if (++tick_counter >= frame_skip) { |
2520 |
tick_counter = 0; |
2521 |
if (mainBuffer.dirty) { |
2522 |
LOCK_VOSF; |
2523 |
update_display_dga_vosf(); |
2524 |
UNLOCK_VOSF; |
2525 |
} |
2526 |
} |
2527 |
} |
2528 |
#endif |
2529 |
|
2530 |
// Set new palette if it was changed |
2531 |
handle_palette_changes(); |
2532 |
|
2533 |
} else { |
2534 |
|
2535 |
// No display refresh pending, check for X events |
2536 |
fd_set readfds; |
2537 |
FD_ZERO(&readfds); |
2538 |
FD_SET(fd, &readfds); |
2539 |
struct timeval timeout; |
2540 |
timeout.tv_sec = 0; |
2541 |
timeout.tv_usec = delay; |
2542 |
if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0) |
2543 |
handle_events(); |
2544 |
} |
2545 |
} |
2546 |
return NULL; |
2547 |
} |