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