ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/BeOS/video_beos.cpp
Revision: 1.8
Committed: 2001-06-27T19:03:36Z (23 years, 5 months ago) by cebix
Branch: MAIN
Changes since 1.7: +60 -33 lines
Log Message:
added infrastructure for resolution/depth switching (currently, all video
drivers only support one mode, the one selected by the user)

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