ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/BeOS/video_beos.cpp
Revision: 1.10
Committed: 2001-06-30T17:21:53Z (23 years, 4 months ago) by cebix
Branch: MAIN
Changes since 1.9: +1 -1 lines
Log Message:
- experimental gamma table support
- restructured video_x.cpp: uses classes for display types

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