ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/BeOS/video_beos.cpp
Revision: 1.17
Committed: 2008-01-01T09:40:32Z (16 years, 11 months ago) by gbeauche
Branch: MAIN
CVS Tags: HEAD
Changes since 1.16: +1 -1 lines
Log Message:
Happy New Year!

File Contents

# Content
1 /*
2 * video_beos.cpp - Video/graphics emulation, BeOS specific stuff
3 *
4 * Basilisk II (C) 1997-2008 Christian Bauer
5 * Portions written by Marc Hellwig
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <AppKit.h>
23 #include <InterfaceKit.h>
24 #include <GameKit.h>
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "sysdeps.h"
30 #include "cpu_emulation.h"
31 #include "main.h"
32 #include "macos_util.h"
33 #include "prefs.h"
34 #include "adb.h"
35 #include "prefs.h"
36 #include "user_strings.h"
37 #include "about_window.h"
38 #include "video.h"
39
40 #include "m68k.h"
41 #include "memory.h"
42 #include "readcpu.h"
43 #include "newcpu.h"
44
45 #define DEBUG 0
46 #include "debug.h"
47
48 #define DEBUGGER_AVAILABLE 0
49
50
51 // Messages
52 const uint32 MSG_REDRAW = 'draw';
53 const uint32 MSG_ABOUT_REQUESTED = B_ABOUT_REQUESTED;
54 const uint32 MSG_REF_5HZ = ' 5Hz';
55 const uint32 MSG_REF_7_5HZ = ' 7Hz';
56 const uint32 MSG_REF_10HZ = '10Hz';
57 const uint32 MSG_REF_15HZ = '15Hz';
58 const uint32 MSG_REF_30HZ = '30Hz';
59 const uint32 MSG_REF_60HZ = '60Hz';
60 const uint32 MSG_MOUNT = 'moun';
61 const uint32 MSG_DEBUGGER = 'dbug';
62
63 // Display types
64 enum {
65 DISPLAY_WINDOW,
66 DISPLAY_SCREEN
67 };
68
69 // From sys_beos.cpp
70 extern void SysCreateVolumeMenu(BMenu *menu, uint32 msg);
71 extern void SysMountVolume(const char *name);
72
73
74 /*
75 * A simple view class for blitting a bitmap on the screen
76 */
77
78 class BitmapView : public BView {
79 public:
80 BitmapView(BRect frame, BBitmap *bitmap) : BView(frame, "bitmap", B_FOLLOW_NONE, B_WILL_DRAW)
81 {
82 the_bitmap = bitmap;
83 }
84 virtual void Draw(BRect update)
85 {
86 DrawBitmap(the_bitmap, update, update);
87 }
88 virtual void MouseMoved(BPoint point, uint32 transit, const BMessage *message);
89
90 private:
91 BBitmap *the_bitmap;
92 };
93
94
95 /*
96 * Window class
97 */
98
99 class MacWindow : public BDirectWindow {
100 public:
101 MacWindow(BRect frame);
102 virtual ~MacWindow();
103 virtual void MessageReceived(BMessage *msg);
104 virtual void DirectConnected(direct_buffer_info *info);
105 virtual void WindowActivated(bool active);
106
107 int32 frame_skip;
108 bool mouse_in_view; // Flag: Mouse pointer within bitmap view
109 uint8 remap_mac_be[256]; // For remapping of Mac colors to Be colors
110
111 private:
112 static status_t tick_func(void *arg);
113
114 thread_id tick_thread;
115 bool tick_thread_active; // Flag for quitting the tick thread
116
117 BitmapView *main_view; // Main view for bitmap drawing
118 BBitmap *the_bitmap; // Mac screen bitmap
119 uint8 *the_buffer; // Mac frame buffer
120
121 uint32 old_scroll_lock_state;
122
123 bool supports_direct_mode; // Flag: Direct frame buffer access supported
124 sem_id drawing_sem;
125
126 void *bits;
127 int32 bytes_per_row;
128 color_space pixel_format;
129 bool unclipped;
130 };
131
132
133 /*
134 * Screen class
135 */
136
137 class MacScreen : public BWindowScreen {
138 public:
139 MacScreen(const char *name, int mode_bit, status_t *error);
140 virtual ~MacScreen();
141 virtual void Quit(void);
142 virtual void ScreenConnected(bool active);
143
144 rgb_color palette[256]; // Color palette, 256 entries
145 bool palette_changed;
146
147 private:
148 static status_t tick_func(void *arg);
149
150 thread_id tick_thread;
151 bool tick_thread_active; // Flag for quitting the tick thread
152
153 BView *main_view; // Main view for GetMouse()
154 uint8 *frame_backup; // Frame buffer backup when switching from/to different workspace
155 bool quitting; // Flag for ScreenConnected: We are quitting, don't pause emulator thread
156 bool screen_active;
157 bool first_time;
158 };
159
160
161 // Global variables
162 static int display_type = DISPLAY_WINDOW; // See enum above
163 static MacWindow *the_window = NULL; // Pointer to the window
164 static MacScreen *the_screen = NULL; // Pointer to the screen
165 static sem_id mac_os_lock = -1; // This is used to stop the MacOS thread when the Basilisk workspace is switched out
166 static uint8 MacCursor[68] = {16, 1}; // Mac cursor image
167
168
169 /*
170 * Initialization
171 */
172
173 // Add resolution to list of supported modes and set VideoMonitor
174 static void set_video_monitor(uint32 width, uint32 height, uint32 bytes_per_row, int depth)
175 {
176 video_mode mode;
177
178 mode.x = width;
179 mode.y = height;
180 mode.resolution_id = 0x80;
181 mode.bytes_per_row = bytes_per_row;
182
183 switch (depth) {
184 case 1:
185 mode.depth = VDEPTH_1BIT;
186 break;
187 case 2:
188 mode.depth = VDEPTH_2BIT;
189 break;
190 case 4:
191 mode.depth = VDEPTH_4BIT;
192 break;
193 case 8:
194 mode.depth = VDEPTH_8BIT;
195 break;
196 case 15:
197 mode.depth = VDEPTH_16BIT;
198 break;
199 case 16:
200 mode.depth = VDEPTH_16BIT;
201 break;
202 case 24:
203 case 32:
204 mode.depth = VDEPTH_32BIT;
205 break;
206 }
207
208 VideoModes.push_back(mode);
209 video_init_depth_list();
210 VideoMonitor.mode = mode;
211 }
212
213
214 bool VideoInit(bool classic)
215 {
216 // Create semaphore
217 mac_os_lock = create_sem(0, "MacOS Frame Buffer Lock");
218
219 // Get screen mode from preferences
220 const char *mode_str = PrefsFindString("screen");
221
222 // Determine type and mode
223 display_type = DISPLAY_WINDOW;
224 int width = 512, height = 384;
225 int scr_mode_bit = 0;
226 if (mode_str) {
227 if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
228 display_type = DISPLAY_WINDOW;
229 else if (sscanf(mode_str, "scr/%d", &scr_mode_bit) == 1)
230 display_type = DISPLAY_SCREEN;
231 }
232
233 // Open display
234 switch (display_type) {
235 case DISPLAY_WINDOW:
236 the_window = new MacWindow(BRect(0, 0, width-1, height-1));
237 break;
238 case DISPLAY_SCREEN: {
239 status_t screen_error;
240 the_screen = new MacScreen(GetString(STR_WINDOW_TITLE), scr_mode_bit & 0x1f, &screen_error);
241 if (screen_error != B_NO_ERROR) {
242 the_screen->PostMessage(B_QUIT_REQUESTED);
243 while (the_screen)
244 snooze(200000);
245 ErrorAlert(STR_OPEN_SCREEN_ERR);
246 return false;
247 } else {
248 the_screen->Show();
249 acquire_sem(mac_os_lock);
250 }
251 break;
252 }
253 }
254 return true;
255 }
256
257
258 /*
259 * Deinitialization
260 */
261
262 void VideoExit(void)
263 {
264 // Close display
265 switch (display_type) {
266 case DISPLAY_WINDOW:
267 if (the_window != NULL) {
268 the_window->PostMessage(B_QUIT_REQUESTED);
269 while (the_window)
270 snooze(200000);
271 }
272 break;
273 case DISPLAY_SCREEN:
274 if (the_screen != NULL) {
275 the_screen->PostMessage(B_QUIT_REQUESTED);
276 while (the_screen)
277 snooze(200000);
278 }
279 break;
280 }
281
282 // Delete semaphore
283 delete_sem(mac_os_lock);
284 }
285
286
287 /*
288 * Set palette
289 */
290
291 void video_set_palette(uint8 *pal, int num)
292 {
293 switch (display_type) {
294 case DISPLAY_WINDOW: {
295 BScreen screen(the_window);
296 for (int i=0; i<256; i++)
297 the_window->remap_mac_be[i] = screen.IndexForColor(pal[i*3], pal[i*3+1], pal[i*3+2]);
298 break;
299 }
300 case DISPLAY_SCREEN:
301 for (int i=0; i<256; i++) {
302 the_screen->palette[i].red = pal[i*3];
303 the_screen->palette[i].green = pal[i*3+1];
304 the_screen->palette[i].blue = pal[i*3+2];
305 }
306 the_screen->palette_changed = true;
307 break;
308 }
309 }
310
311
312 /*
313 * Switch video mode
314 */
315
316 void video_switch_to_mode(const video_mode &mode)
317 {
318 }
319
320
321 /*
322 * Close down full-screen mode (if bringing up error alerts is unsafe while in full-screen mode)
323 */
324
325 void VideoQuitFullScreen(void)
326 {
327 D(bug("VideoQuitFullScreen()\n"));
328 if (display_type == DISPLAY_SCREEN) {
329 if (the_screen != NULL) {
330 the_screen->PostMessage(B_QUIT_REQUESTED);
331 while (the_screen)
332 snooze(200000);
333 }
334 }
335 }
336
337
338 /*
339 * Video event handling (not neccessary under BeOS, handled by filter function)
340 */
341
342 void VideoInterrupt(void)
343 {
344 release_sem(mac_os_lock);
345 while (acquire_sem(mac_os_lock) == B_INTERRUPTED) ;
346 }
347
348
349 /*
350 * Filter function for receiving mouse and keyboard events
351 */
352
353 #define MENU_IS_POWER 0
354
355 // Be -> Mac raw keycode translation table
356 static const uint8 keycode2mac[0x80] = {
357 0xff, 0x35, 0x7a, 0x78, 0x63, 0x76, 0x60, 0x61, // inv Esc F1 F2 F3 F4 F5 F6
358 0x62, 0x64, 0x65, 0x6d, 0x67, 0x6f, 0x69, 0x6b, // F7 F8 F9 F10 F11 F12 F13 F14
359 0x71, 0x0a, 0x12, 0x13, 0x14, 0x15, 0x17, 0x16, // F15 ` 1 2 3 4 5 6
360 0x1a, 0x1c, 0x19, 0x1d, 0x1b, 0x18, 0x33, 0x72, // 7 8 9 0 - = BSP INS
361 0x73, 0x74, 0x47, 0x4b, 0x43, 0x4e, 0x30, 0x0c, // HOM PUP NUM / * - TAB Q
362 0x0d, 0x0e, 0x0f, 0x11, 0x10, 0x20, 0x22, 0x1f, // W E R T Y U I O
363 0x23, 0x21, 0x1e, 0x2a, 0x75, 0x77, 0x79, 0x59, // P [ ] \ DEL END PDN 7
364 0x5b, 0x5c, 0x45, 0x39, 0x00, 0x01, 0x02, 0x03, // 8 9 + CAP A S D F
365 0x05, 0x04, 0x26, 0x28, 0x25, 0x29, 0x27, 0x24, // G H J K L ; ' RET
366 0x56, 0x57, 0x58, 0x38, 0x06, 0x07, 0x08, 0x09, // 4 5 6 SHL Z X C V
367 0x0b, 0x2d, 0x2e, 0x2b, 0x2f, 0x2c, 0x38, 0x3e, // B N M , . / SHR CUP
368 0x53, 0x54, 0x55, 0x4c, 0x36, 0x37, 0x31, 0x37, // 1 2 3 ENT CTL ALT SPC ALT
369 0x36, 0x3b, 0x3d, 0x3c, 0x52, 0x41, 0x3a, 0x3a, // CTR CLF CDN CRT 0 . CMD CMD
370 #if MENU_IS_POWER
371 0x7f, 0x32, 0x51, 0x7f, 0xff, 0xff, 0xff, 0xff, // MNU EUR = POW inv inv inv inv
372 #else
373 0x32, 0x32, 0x51, 0x7f, 0xff, 0xff, 0xff, 0xff, // MNU EUR = POW inv inv inv inv
374 #endif
375 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // inv inv inv inv inv inv inv inv
376 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff // inv inv inv inv inv inv inv inv
377 };
378
379 static const uint8 modifier2mac[0x20] = {
380 #if MENU_IS_POWER
381 0x38, 0x37, 0x36, 0x39, 0x6b, 0x47, 0x3a, 0x7f, // SHF CMD inv CAP F14 NUM OPT MNU
382 #else
383 0x38, 0x37, 0x36, 0x39, 0x6b, 0x47, 0x3a, 0x32, // SHF CMD CTR CAP F14 NUM OPT MNU
384 #endif
385 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // inv inv inv inv inv inv inv inv
386 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // inv inv inv inv inv inv inv inv
387 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff // inv inv inv inv inv inv inv inv
388 };
389
390 static filter_result filter_func(BMessage *msg, BHandler **target, BMessageFilter *filter)
391 {
392 switch (msg->what) {
393 case B_KEY_DOWN:
394 case B_KEY_UP: {
395 uint32 be_code = msg->FindInt32("key") & 0xff;
396 uint32 mac_code = keycode2mac[be_code];
397
398 // Intercept Ctrl-F1 (mount floppy disk shortcut)
399 uint32 mods = msg->FindInt32("modifiers");
400 if (be_code == 0x02 && (mods & B_CONTROL_KEY))
401 SysMountVolume("/dev/disk/floppy/raw");
402
403 if (mac_code == 0xff)
404 return B_DISPATCH_MESSAGE;
405 if (msg->what == B_KEY_DOWN)
406 ADBKeyDown(mac_code);
407 else
408 ADBKeyUp(mac_code);
409 return B_SKIP_MESSAGE;
410 }
411
412 case B_MODIFIERS_CHANGED: {
413 uint32 mods = msg->FindInt32("modifiers");
414 uint32 old_mods = msg->FindInt32("be:old_modifiers");
415 uint32 changed = mods ^ old_mods;
416 uint32 mask = 1;
417 for (int i=0; i<32; i++, mask<<=1)
418 if (changed & mask) {
419 uint32 mac_code = modifier2mac[i];
420 if (mac_code == 0xff)
421 continue;
422 if (mods & mask)
423 ADBKeyDown(mac_code);
424 else
425 ADBKeyUp(mac_code);
426 }
427 return B_SKIP_MESSAGE;
428 }
429
430 case B_MOUSE_MOVED: {
431 BPoint point;
432 msg->FindPoint("where", &point);
433 ADBMouseMoved(int(point.x), int(point.y));
434 return B_DISPATCH_MESSAGE; // Otherwise BitmapView::MouseMoved() wouldn't be called
435 }
436
437 case B_MOUSE_DOWN: {
438 uint32 buttons = msg->FindInt32("buttons");
439 if (buttons & B_PRIMARY_MOUSE_BUTTON)
440 ADBMouseDown(0);
441 if (buttons & B_SECONDARY_MOUSE_BUTTON)
442 ADBMouseDown(1);
443 if (buttons & B_TERTIARY_MOUSE_BUTTON)
444 ADBMouseDown(2);
445 return B_SKIP_MESSAGE;
446 }
447
448 case B_MOUSE_UP: // B_MOUSE_UP means "all buttons released"
449 ADBMouseUp(0);
450 ADBMouseUp(1);
451 ADBMouseUp(2);
452 return B_SKIP_MESSAGE;
453
454 default:
455 return B_DISPATCH_MESSAGE;
456 }
457 }
458
459
460 /*
461 * Window constructor
462 */
463
464 MacWindow::MacWindow(BRect frame) : BDirectWindow(frame, GetString(STR_WINDOW_TITLE), B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_CLOSABLE | B_NOT_ZOOMABLE)
465 {
466 supports_direct_mode = SupportsWindowMode();
467
468 // Move window to right position
469 Lock();
470 MoveTo(80, 60);
471
472 // Allocate bitmap and Mac frame buffer
473 uint32 x = frame.IntegerWidth() + 1;
474 uint32 y = frame.IntegerHeight() + 1;
475 the_bitmap = new BBitmap(frame, B_COLOR_8_BIT);
476 the_buffer = new uint8[x * (y + 2)]; // "y + 2" for safety
477
478 // Add resolution and set VideoMonitor
479 set_video_monitor(x, y, x, 8);
480 #if REAL_ADDRESSING
481 VideoMonitor.mac_frame_base = (uint32)the_buffer;
482 #else
483 VideoMonitor.mac_frame_base = MacFrameBaseMac;
484 #endif
485
486 #if !REAL_ADDRESSING
487 // Set variables for UAE memory mapping
488 MacFrameBaseHost = the_buffer;
489 MacFrameSize = x * y;
490 MacFrameLayout = FLAYOUT_DIRECT;
491 #endif
492
493 // Create bitmap view
494 main_view = new BitmapView(frame, the_bitmap);
495 AddChild(main_view);
496 main_view->MakeFocus();
497
498 // Read frame skip prefs
499 frame_skip = PrefsFindInt32("frameskip");
500 if (frame_skip == 0)
501 frame_skip = 1;
502
503 // Set up menus
504 BRect bounds = Bounds();
505 bounds.OffsetBy(0, bounds.IntegerHeight() + 1);
506 BMenuItem *item;
507 BMenuBar *bar = new BMenuBar(bounds, "menu");
508 BMenu *menu = new BMenu(GetString(STR_WINDOW_MENU));
509 menu->AddItem(new BMenuItem(GetString(STR_WINDOW_ITEM_ABOUT), new BMessage(MSG_ABOUT_REQUESTED)));
510 menu->AddItem(new BSeparatorItem);
511 BMenu *submenu = new BMenu(GetString(STR_WINDOW_ITEM_REFRESH));
512 submenu->AddItem(new BMenuItem(GetString(STR_REF_5HZ_LAB), new BMessage(MSG_REF_5HZ)));
513 submenu->AddItem(new BMenuItem(GetString(STR_REF_7_5HZ_LAB), new BMessage(MSG_REF_7_5HZ)));
514 submenu->AddItem(new BMenuItem(GetString(STR_REF_10HZ_LAB), new BMessage(MSG_REF_10HZ)));
515 submenu->AddItem(new BMenuItem(GetString(STR_REF_15HZ_LAB), new BMessage(MSG_REF_15HZ)));
516 submenu->AddItem(new BMenuItem(GetString(STR_REF_30HZ_LAB), new BMessage(MSG_REF_30HZ)));
517 submenu->AddItem(new BMenuItem(GetString(STR_REF_60HZ_LAB), new BMessage(MSG_REF_60HZ)));
518 submenu->SetRadioMode(true);
519 if (frame_skip == 12) {
520 if ((item = submenu->FindItem(GetString(STR_REF_5HZ_LAB))) != NULL)
521 item->SetMarked(true);
522 } else if (frame_skip == 8) {
523 if ((item = submenu->FindItem(GetString(STR_REF_7_5HZ_LAB))) != NULL)
524 item->SetMarked(true);
525 } else if (frame_skip == 6) {
526 if ((item = submenu->FindItem(GetString(STR_REF_10HZ_LAB))) != NULL)
527 item->SetMarked(true);
528 } else if (frame_skip == 4) {
529 if ((item = submenu->FindItem(GetString(STR_REF_15HZ_LAB))) != NULL)
530 item->SetMarked(true);
531 } else if (frame_skip == 2) {
532 if ((item = submenu->FindItem(GetString(STR_REF_30HZ_LAB))) != NULL)
533 item->SetMarked(true);
534 } else if (frame_skip == 1) {
535 if ((item = submenu->FindItem(GetString(STR_REF_60HZ_LAB))) != NULL)
536 item->SetMarked(true);
537 }
538 menu->AddItem(submenu);
539 submenu = new BMenu(GetString(STR_WINDOW_ITEM_MOUNT));
540 SysCreateVolumeMenu(submenu, MSG_MOUNT);
541 menu->AddItem(submenu);
542 #if DEBUGGER_AVAILABLE
543 menu->AddItem(new BMenuItem("Debugger", new BMessage(MSG_DEBUGGER)));
544 #endif
545 bar->AddItem(menu);
546 AddChild(bar);
547 SetKeyMenuBar(bar);
548 int mbar_height = bar->Frame().IntegerHeight() + 1;
549
550 // Resize window to fit menu bar
551 ResizeBy(0, mbar_height);
552
553 // Set absolute mouse mode and get scroll lock state
554 ADBSetRelMouseMode(false);
555 mouse_in_view = true;
556 old_scroll_lock_state = modifiers() & B_SCROLL_LOCK;
557 if (old_scroll_lock_state)
558 SetTitle(GetString(STR_WINDOW_TITLE_FROZEN));
559 else
560 SetTitle(GetString(STR_WINDOW_TITLE));
561
562 // Keep window aligned to 8-byte frame buffer boundaries for faster blitting
563 SetWindowAlignment(B_BYTE_ALIGNMENT, 8);
564
565 // Create drawing semaphore (for direct mode)
566 drawing_sem = create_sem(0, "direct frame buffer access");
567
568 // Start 60Hz interrupt
569 tick_thread_active = true;
570 tick_thread = spawn_thread(tick_func, "Window Redraw", B_DISPLAY_PRIORITY, this);
571 resume_thread(tick_thread);
572
573 // Add filter for keyboard and mouse events
574 BMessageFilter *filter = new BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, filter_func);
575 main_view->AddFilter(filter);
576
577 // Show window
578 Unlock();
579 Show();
580 Sync();
581 }
582
583
584 /*
585 * Window destructor
586 */
587
588 MacWindow::~MacWindow()
589 {
590 // Restore cursor
591 mouse_in_view = false;
592 be_app->SetCursor(B_HAND_CURSOR);
593
594 // Hide window
595 Hide();
596 Sync();
597
598 // Stop 60Hz interrupt
599 status_t l;
600 tick_thread_active = false;
601 delete_sem(drawing_sem);
602 wait_for_thread(tick_thread, &l);
603
604 // Free bitmap and frame buffer
605 delete the_bitmap;
606 delete[] the_buffer;
607
608 // Tell emulator that we're done
609 the_window = NULL;
610 }
611
612
613 /*
614 * Window connected/disconnected
615 */
616
617 void MacWindow::DirectConnected(direct_buffer_info *info)
618 {
619 switch (info->buffer_state & B_DIRECT_MODE_MASK) {
620 case B_DIRECT_STOP:
621 acquire_sem(drawing_sem);
622 break;
623 case B_DIRECT_MODIFY:
624 acquire_sem(drawing_sem);
625 case B_DIRECT_START:
626 bits = (void *)((uint8 *)info->bits + info->window_bounds.top * info->bytes_per_row + info->window_bounds.left * info->bits_per_pixel / 8);
627 bytes_per_row = info->bytes_per_row;
628 pixel_format = info->pixel_format;
629 unclipped = false;
630 if (info->clip_list_count == 1)
631 if (memcmp(&info->clip_bounds, &info->window_bounds, sizeof(clipping_rect)) == 0)
632 unclipped = true;
633 release_sem(drawing_sem);
634 break;
635 }
636 }
637
638
639 /*
640 * Handle redraw and menu messages
641 */
642
643 void MacWindow::MessageReceived(BMessage *msg)
644 {
645 BMessage *msg2;
646
647 switch (msg->what) {
648 case MSG_REDRAW: {
649
650 // Prevent backlog of messages
651 MessageQueue()->Lock();
652 while ((msg2 = MessageQueue()->FindMessage(MSG_REDRAW, 0)) != NULL) {
653 MessageQueue()->RemoveMessage(msg2);
654 delete msg2;
655 }
656 MessageQueue()->Unlock();
657
658 // Convert Mac screen buffer to BeOS palette and blit
659 uint8 *source = the_buffer - 1;
660 uint8 *dest = (uint8 *)the_bitmap->Bits() - 1;
661 uint32 length = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
662 for (int i=0; i<length; i++)
663 *++dest = remap_mac_be[*++source];
664 BRect update_rect = BRect(0, 0, VideoMonitor.mode.x-1, VideoMonitor.mode.y-1);
665 main_view->DrawBitmapAsync(the_bitmap, update_rect, update_rect);
666 break;
667 }
668
669 case MSG_ABOUT_REQUESTED: {
670 ShowAboutWindow();
671 break;
672 }
673
674 case MSG_REF_5HZ:
675 PrefsReplaceInt32("frameskip", frame_skip = 12);
676 break;
677
678 case MSG_REF_7_5HZ:
679 PrefsReplaceInt32("frameskip", frame_skip = 8);
680 break;
681
682 case MSG_REF_10HZ:
683 PrefsReplaceInt32("frameskip", frame_skip = 6);
684 break;
685
686 case MSG_REF_15HZ:
687 PrefsReplaceInt32("frameskip", frame_skip = 4);
688 break;
689
690 case MSG_REF_30HZ:
691 PrefsReplaceInt32("frameskip", frame_skip = 2);
692 break;
693
694 case MSG_REF_60HZ:
695 PrefsReplaceInt32("frameskip", frame_skip = 1);
696 break;
697
698 case MSG_MOUNT: {
699 BMenuItem *source = NULL;
700 msg->FindPointer("source", (void **)&source);
701 if (source)
702 SysMountVolume(source->Label());
703 break;
704 }
705
706 #if DEBUGGER_AVAILABLE
707 case MSG_DEBUGGER:
708 extern int debugging;
709 debugging = 1;
710 regs.spcflags |= SPCFLAG_BRK;
711 break;
712 #endif
713
714 default:
715 BDirectWindow::MessageReceived(msg);
716 }
717 }
718
719
720 /*
721 * Window activated/deactivated
722 */
723
724 void MacWindow::WindowActivated(bool active)
725 {
726 if (active) {
727 frame_skip = PrefsFindInt32("frameskip");
728 if (frame_skip == 0)
729 frame_skip = 1;
730 } else
731 frame_skip = 12; // 5Hz in background
732 }
733
734
735 /*
736 * 60Hz interrupt routine
737 */
738
739 status_t MacWindow::tick_func(void *arg)
740 {
741 MacWindow *obj = (MacWindow *)arg;
742 static int tick_counter = 0;
743 while (obj->tick_thread_active) {
744
745 tick_counter++;
746 if (tick_counter >= obj->frame_skip) {
747 tick_counter = 0;
748
749 // Window title is determined by Scroll Lock state
750 uint32 scroll_lock_state = modifiers() & B_SCROLL_LOCK;
751 if (scroll_lock_state != obj->old_scroll_lock_state) {
752 if (scroll_lock_state)
753 obj->SetTitle(GetString(STR_WINDOW_TITLE_FROZEN));
754 else
755 obj->SetTitle(GetString(STR_WINDOW_TITLE));
756 obj->old_scroll_lock_state = scroll_lock_state;
757 }
758
759 // Has the Mac started?
760 if (HasMacStarted()) {
761
762 // Yes, set new cursor image if it was changed
763 if (memcmp(MacCursor+4, Mac2HostAddr(0x844), 64)) {
764 Mac2Host_memcpy(MacCursor+4, 0x844, 64); // Cursor image
765 MacCursor[2] = ReadMacInt8(0x885); // Hotspot
766 MacCursor[3] = ReadMacInt8(0x887);
767 be_app->SetCursor(MacCursor);
768 }
769 }
770
771 // Refresh screen unless Scroll Lock is down
772 if (!scroll_lock_state) {
773
774 // If direct frame buffer access is supported and the content area is completely visible,
775 // convert the Mac screen buffer directly. Otherwise, send a message to the window to do
776 // it into a bitmap
777 if (obj->supports_direct_mode) {
778 if (acquire_sem(obj->drawing_sem) != B_NO_ERROR)
779 return 0;
780 if (obj->unclipped && obj->pixel_format == B_CMAP8) {
781 uint8 *source = obj->the_buffer - 1;
782 uint8 *dest = (uint8 *)obj->bits;
783 uint32 bytes_per_row = obj->bytes_per_row;
784 int xsize = VideoMonitor.mode.x;
785 int ysize = VideoMonitor.mode.y;
786 for (int y=0; y<ysize; y++) {
787 uint32 *p = (uint32 *)dest - 1;
788 for (int x=0; x<xsize/4; x++) {
789 #if B_HOST_IS_BENDIAN
790 uint32 c = obj->remap_mac_be[*++source] << 24;
791 c |= obj->remap_mac_be[*++source] << 16;
792 c |= obj->remap_mac_be[*++source] << 8;
793 c |= obj->remap_mac_be[*++source];
794 #else
795 uint32 c = obj->remap_mac_be[*++source];
796 c |= obj->remap_mac_be[*++source] << 8;
797 c |= obj->remap_mac_be[*++source] << 16;
798 c |= obj->remap_mac_be[*++source] << 24;
799 #endif
800 *++p = c;
801 }
802 dest += bytes_per_row;
803 }
804 } else
805 obj->PostMessage(MSG_REDRAW);
806 release_sem(obj->drawing_sem);
807 } else
808 obj->PostMessage(MSG_REDRAW);
809 }
810 }
811 snooze(16666);
812 }
813 return 0;
814 }
815
816
817 /*
818 * Mouse moved in window
819 */
820
821 void BitmapView::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
822 {
823 switch (transit) {
824 case B_ENTERED_VIEW:
825 ((MacWindow *)Window())->mouse_in_view = true;
826 be_app->SetCursor(MacCursor);
827 break;
828 case B_EXITED_VIEW:
829 ((MacWindow *)Window())->mouse_in_view = false;
830 be_app->SetCursor(B_HAND_CURSOR);
831 break;
832 }
833 }
834
835
836 /*
837 * Screen constructor
838 */
839
840 MacScreen::MacScreen(const char *name, int mode_bit, status_t *error) : BWindowScreen(name, 1 << mode_bit, error), tick_thread(-1)
841 {
842 // Set all variables
843 frame_backup = NULL;
844 palette_changed = false;
845 screen_active = false;
846 first_time = true;
847 quitting = false;
848
849 // Set relative mouse mode
850 ADBSetRelMouseMode(true);
851
852 // Create view to get mouse events
853 main_view = new BView(Frame(), NULL, B_FOLLOW_NONE, 0);
854 AddChild(main_view);
855
856 // Start 60Hz interrupt
857 tick_thread_active = true;
858 tick_thread = spawn_thread(tick_func, "Polling sucks...", B_DISPLAY_PRIORITY, this);
859 resume_thread(tick_thread);
860
861 // Add filter for keyboard and mouse events
862 BMessageFilter *filter = new BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, filter_func);
863 AddCommonFilter(filter);
864 }
865
866
867 /*
868 * Screen destructor
869 */
870
871 MacScreen::~MacScreen()
872 {
873 // Stop 60Hz interrupt
874 if (tick_thread > 0) {
875 status_t l;
876 tick_thread_active = false;
877 wait_for_thread(tick_thread, &l);
878 }
879
880 // Tell emulator that we're done
881 the_screen = NULL;
882 }
883
884
885 /*
886 * Screen closed
887 */
888
889 void MacScreen::Quit(void)
890 {
891 // Tell ScreenConnected() that we are quitting
892 quitting = true;
893 BWindowScreen::Quit();
894 }
895
896
897 /*
898 * Screen connected/disconnected
899 */
900
901 void MacScreen::ScreenConnected(bool active)
902 {
903 graphics_card_info *info = CardInfo();
904 screen_active = active;
905
906 if (active == true) {
907
908 // Add resolution and set VideoMonitor
909 if (first_time) {
910 set_video_monitor(info->width, info->height, info->bytes_per_row, info->bits_per_pixel);
911 first_time = false;
912 }
913
914 // Set VideoMonitor
915 #if REAL_ADDRESSING
916 VideoMonitor.mac_frame_base = (uint32)info->frame_buffer;
917 #else
918 VideoMonitor.mac_frame_base = MacFrameBaseMac;
919 #endif
920
921 #if !REAL_ADDRESSING
922 // Set variables for UAE memory mapping
923 MacFrameBaseHost = (uint8 *)info->frame_buffer;
924 MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
925 switch (info->bits_per_pixel) {
926 case 15:
927 MacFrameLayout = FLAYOUT_HOST_555;
928 break;
929 case 16:
930 MacFrameLayout = FLAYOUT_HOST_565;
931 break;
932 case 32:
933 MacFrameLayout = FLAYOUT_HOST_888;
934 break;
935 default:
936 MacFrameLayout = FLAYOUT_DIRECT;
937 break;
938 }
939 #endif
940
941 // Copy from backup store to frame buffer
942 if (frame_backup != NULL) {
943 memcpy(info->frame_buffer, frame_backup, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
944 delete[] frame_backup;
945 frame_backup = NULL;
946 }
947
948 // Restore palette
949 if (VideoMonitor.mode.depth == VDEPTH_8BIT)
950 SetColorList(palette);
951
952 // Restart/signal emulator thread
953 release_sem(mac_os_lock);
954
955 } else {
956
957 if (!quitting) {
958
959 // Stop emulator thread
960 acquire_sem(mac_os_lock);
961
962 // Create backup store and save frame buffer
963 frame_backup = new uint8[VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y];
964 memcpy(frame_backup, info->frame_buffer, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
965 }
966 }
967 }
968
969
970 /*
971 * Screen 60Hz interrupt routine
972 */
973
974 status_t MacScreen::tick_func(void *arg)
975 {
976 MacScreen *obj = (MacScreen *)arg;
977 while (obj->tick_thread_active) {
978
979 // Wait
980 snooze(16667);
981
982 // Workspace activated? Then poll the mouse and set the palette if needed
983 if (!obj->quitting && obj->LockWithTimeout(200000) == B_OK) {
984 if (obj->screen_active) {
985 BPoint pt;
986 uint32 button = 0;
987 if (obj->palette_changed) {
988 obj->palette_changed = false;
989 obj->SetColorList(obj->palette);
990 }
991 obj->main_view->GetMouse(&pt, &button);
992 set_mouse_position(320, 240);
993 ADBMouseMoved(int(pt.x) - 320, int(pt.y) - 240);
994 if (button & B_PRIMARY_MOUSE_BUTTON)
995 ADBMouseDown(0);
996 if (!(button & B_PRIMARY_MOUSE_BUTTON))
997 ADBMouseUp(0);
998 if (button & B_SECONDARY_MOUSE_BUTTON)
999 ADBMouseDown(1);
1000 if (!(button & B_SECONDARY_MOUSE_BUTTON))
1001 ADBMouseUp(1);
1002 if (button & B_TERTIARY_MOUSE_BUTTON)
1003 ADBMouseDown(2);
1004 if (!(button & B_TERTIARY_MOUSE_BUTTON))
1005 ADBMouseUp(2);
1006 }
1007 obj->Unlock();
1008 }
1009 }
1010 return 0;
1011 }