ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/BeOS/video_beos.cpp
Revision: 1.6
Committed: 2001-03-31T14:31:59Z (23 years, 6 months ago) by cebix
Branch: MAIN
Changes since 1.5: +0 -1 lines
Log Message:
- fixed bugs in the BeOS extfs file types handling

File Contents

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