home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 25 / AACD 25.iso / AACD / Utilities / BasiliskII / src / BeOS / video_beos.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-26  |  24.7 KB  |  975 lines

  1. /*
  2.  *  video_beos.cpp - Video/graphics emulation, BeOS specific stuff
  3.  *
  4.  *  Basilisk II (C) 1997-2001 Christian Bauer
  5.  *  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 "about_window.h"
  38. #include "video.h"
  39.  
  40. #include "m68k.h"
  41. #include "memory.h"
  42. #include "readcpu.h"
  43. #include "newcpu.h"
  44.  
  45. #define DEBUG 0
  46. #include "debug.h"
  47.  
  48. #define DEBUGGER_AVAILABLE 0
  49.  
  50.  
  51. // Messages
  52. const uint32 MSG_REDRAW = 'draw';
  53. const uint32 MSG_ABOUT_REQUESTED = B_ABOUT_REQUESTED;
  54. const uint32 MSG_REF_5HZ = ' 5Hz';
  55. const uint32 MSG_REF_7_5HZ = ' 7Hz';
  56. const uint32 MSG_REF_10HZ = '10Hz';
  57. const uint32 MSG_REF_15HZ = '15Hz';
  58. const uint32 MSG_REF_30HZ = '30Hz';
  59. const uint32 MSG_REF_60HZ = '60Hz';
  60. const uint32 MSG_MOUNT = 'moun';
  61. const uint32 MSG_DEBUGGER = 'dbug';
  62.  
  63. // Display types
  64. enum {
  65.     DISPLAY_WINDOW,
  66.     DISPLAY_SCREEN
  67. };
  68.  
  69. // From sys_beos.cpp
  70. extern void SysCreateVolumeMenu(BMenu *menu, uint32 msg);
  71. extern void SysMountVolume(const char *name);
  72.  
  73.  
  74. /*
  75.  *  A simple view class for blitting a bitmap on the screen
  76.  */
  77.  
  78. class BitmapView : public BView {
  79. public:
  80.     BitmapView(BRect frame, BBitmap *bitmap) : BView(frame, "bitmap", B_FOLLOW_NONE, B_WILL_DRAW)
  81.     {
  82.         the_bitmap = bitmap;
  83.     }
  84.     virtual void Draw(BRect update)
  85.     {
  86.         DrawBitmap(the_bitmap, update, update);
  87.     }
  88.     virtual void MouseMoved(BPoint point, uint32 transit, const BMessage *message);
  89.  
  90. private:
  91.     BBitmap *the_bitmap;
  92. };
  93.  
  94.  
  95. /*
  96.  *  Window class
  97.  */
  98.  
  99. class MacWindow : public BDirectWindow {
  100. public:
  101.     MacWindow(BRect frame);
  102.     virtual ~MacWindow();
  103.     virtual void MessageReceived(BMessage *msg);
  104.     virtual void DirectConnected(direct_buffer_info *info);
  105.     virtual void WindowActivated(bool active);
  106.  
  107.     int32 frame_skip;
  108.     bool mouse_in_view;            // Flag: Mouse pointer within bitmap view
  109.     uint8 remap_mac_be[256];    // For remapping of Mac colors to Be colors
  110.  
  111. private:
  112.     static status_t tick_func(void *arg);
  113.  
  114.     thread_id tick_thread;
  115.     bool tick_thread_active;    // Flag for quitting the tick thread
  116.  
  117.     BitmapView *main_view;        // Main view for bitmap drawing
  118.     BBitmap *the_bitmap;        // Mac screen bitmap
  119.     uint8 *the_buffer;            // Mac frame buffer
  120.  
  121.     uint32 old_scroll_lock_state;
  122.  
  123.     bool supports_direct_mode;    // Flag: Direct frame buffer access supported
  124.     sem_id drawing_sem;
  125.  
  126.     void *bits;
  127.     int32 bytes_per_row;
  128.     color_space pixel_format;
  129.     bool unclipped;
  130. };
  131.  
  132.  
  133. /*
  134.  *  Screen class
  135.  */
  136.  
  137. class MacScreen : public BWindowScreen {
  138. public:
  139.     MacScreen(const char *name, int mode_bit, status_t *error);
  140.     virtual ~MacScreen();
  141.     virtual void Quit(void);
  142.     virtual    void ScreenConnected(bool active);
  143.  
  144.     rgb_color palette[256];        // Color palette, 256 entries
  145.     bool palette_changed;
  146.  
  147. private:
  148.     static status_t tick_func(void *arg);
  149.  
  150.     thread_id tick_thread;
  151.     bool tick_thread_active;    // Flag for quitting the tick thread
  152.  
  153.     BView *main_view;            // Main view for GetMouse()
  154.     uint8 *frame_backup;        // Frame buffer backup when switching from/to different workspace
  155.     bool quitting;                // Flag for ScreenConnected: We are quitting, don't pause emulator thread
  156.     bool screen_active;
  157. };
  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.             ShowAboutWindow();
  623.             break;
  624.         }
  625.  
  626.         case MSG_REF_5HZ:
  627.             PrefsReplaceInt32("frameskip", frame_skip = 12);
  628.             break;
  629.  
  630.         case MSG_REF_7_5HZ:
  631.             PrefsReplaceInt32("frameskip", frame_skip = 8);
  632.             break;
  633.  
  634.         case MSG_REF_10HZ:
  635.             PrefsReplaceInt32("frameskip", frame_skip = 6);
  636.             break;
  637.  
  638.         case MSG_REF_15HZ:
  639.             PrefsReplaceInt32("frameskip", frame_skip = 4);
  640.             break;
  641.  
  642.         case MSG_REF_30HZ:
  643.             PrefsReplaceInt32("frameskip", frame_skip = 2);
  644.             break;
  645.  
  646.         case MSG_REF_60HZ:
  647.             PrefsReplaceInt32("frameskip", frame_skip = 1);
  648.             break;
  649.  
  650.         case MSG_MOUNT: {
  651.             BMenuItem *source = NULL;
  652.             msg->FindPointer("source", (void **)&source);
  653.             if (source)
  654.                 SysMountVolume(source->Label());
  655.             break;
  656.         }
  657.  
  658. #if DEBUGGER_AVAILABLE
  659.         case MSG_DEBUGGER:
  660.             extern int debugging;
  661.             debugging = 1;
  662.             regs.spcflags |= SPCFLAG_BRK;
  663.             break;
  664. #endif
  665.  
  666.         default:
  667.             BDirectWindow::MessageReceived(msg);
  668.     }
  669. }
  670.  
  671.  
  672. /*
  673.  *  Window activated/deactivated
  674.  */
  675.  
  676. void MacWindow::WindowActivated(bool active)
  677. {
  678.     if (active) {
  679.         frame_skip = PrefsFindInt32("frameskip");
  680.         if (frame_skip == 0)
  681.             frame_skip = 1;
  682.     } else
  683.         frame_skip = 12;    // 5Hz in background
  684. }
  685.  
  686.  
  687. /*
  688.  *  60Hz interrupt routine
  689.  */
  690.  
  691. status_t MacWindow::tick_func(void *arg)
  692. {
  693.     MacWindow *obj = (MacWindow *)arg;
  694.     static int tick_counter = 0;
  695.     while (obj->tick_thread_active) {
  696.  
  697.         tick_counter++;
  698.         if (tick_counter >= obj->frame_skip) {
  699.             tick_counter = 0;
  700.  
  701.             // Window title is determined by Scroll Lock state
  702.             uint32 scroll_lock_state = modifiers() & B_SCROLL_LOCK;
  703.             if (scroll_lock_state != obj->old_scroll_lock_state) {
  704.                 if (scroll_lock_state)
  705.                     obj->SetTitle(GetString(STR_WINDOW_TITLE_FROZEN));
  706.                 else
  707.                     obj->SetTitle(GetString(STR_WINDOW_TITLE));
  708.                 obj->old_scroll_lock_state = scroll_lock_state;
  709.             }
  710.  
  711.             // Has the Mac started?
  712.             if (HasMacStarted()) {
  713.  
  714.                 // Yes, set new cursor image if it was changed
  715.                 if (memcmp(MacCursor+4, Mac2HostAddr(0x844), 64)) {
  716.                     Mac2Host_memcpy(MacCursor+4, 0x844, 64);    // Cursor image
  717.                     MacCursor[2] = ReadMacInt8(0x885);            // Hotspot
  718.                     MacCursor[3] = ReadMacInt8(0x887);
  719.                     be_app->SetCursor(MacCursor);
  720.                 }
  721.             }
  722.  
  723.             // Refresh screen unless Scroll Lock is down
  724.             if (!scroll_lock_state) {
  725.  
  726.                 // If direct frame buffer access is supported and the content area is completely visible,
  727.                 // convert the Mac screen buffer directly. Otherwise, send a message to the window to do
  728.                 // it into a bitmap
  729.                 if (obj->supports_direct_mode) {
  730.                     if (acquire_sem(obj->drawing_sem) != B_NO_ERROR)
  731.                         return 0;
  732.                     if (obj->unclipped && obj->pixel_format == B_CMAP8) {
  733.                         uint8 *source = obj->the_buffer - 1;
  734.                         uint8 *dest = (uint8 *)obj->bits;
  735.                         uint32 bytes_per_row = obj->bytes_per_row;
  736.                         int xsize = VideoMonitor.x;
  737.                         int ysize = VideoMonitor.y;
  738.                         for (int y=0; y<ysize; y++) {
  739.                             uint32 *p = (uint32 *)dest - 1;
  740.                             for (int x=0; x<xsize/4; x++) {
  741. #if B_HOST_IS_BENDIAN
  742.                                 uint32 c = obj->remap_mac_be[*++source] << 24;
  743.                                 c |= obj->remap_mac_be[*++source] << 16;
  744.                                 c |= obj->remap_mac_be[*++source] << 8;
  745.                                 c |= obj->remap_mac_be[*++source];
  746. #else
  747.                                 uint32 c = obj->remap_mac_be[*++source];
  748.                                 c |= obj->remap_mac_be[*++source] << 8;
  749.                                 c |= obj->remap_mac_be[*++source] << 16;
  750.                                 c |= obj->remap_mac_be[*++source] << 24;
  751. #endif
  752.                                 *++p = c;
  753.                             }
  754.                             dest += bytes_per_row;
  755.                         }
  756.                     } else
  757.                         obj->PostMessage(MSG_REDRAW);
  758.                     release_sem(obj->drawing_sem);
  759.                 } else
  760.                     obj->PostMessage(MSG_REDRAW);
  761.             }
  762.         }
  763.         snooze(16666);
  764.     }
  765.     return 0;
  766. }
  767.  
  768.  
  769. /*
  770.  *  Mouse moved in window
  771.  */
  772.  
  773. void BitmapView::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
  774. {
  775.     switch (transit) {
  776.         case B_ENTERED_VIEW:
  777.             ((MacWindow *)Window())->mouse_in_view = true;
  778.             be_app->SetCursor(MacCursor);
  779.             break;
  780.         case B_EXITED_VIEW:
  781.             ((MacWindow *)Window())->mouse_in_view = false;
  782.             be_app->SetCursor(B_HAND_CURSOR);
  783.             break;
  784.     }
  785. }
  786.  
  787.  
  788. /*
  789.  *  Screen constructor
  790.  */
  791.  
  792. MacScreen::MacScreen(const char *name, int mode_bit, status_t *error) : BWindowScreen(name, 1 << mode_bit, error), tick_thread(-1)
  793. {
  794.     // Set all variables
  795.     frame_backup = NULL;
  796.     palette_changed = false;
  797.     screen_active = false;
  798.     quitting = false;
  799.  
  800.     // Set relative mouse mode
  801.     ADBSetRelMouseMode(true);
  802.  
  803.     // Create view to get mouse events
  804.     main_view = new BView(Frame(), NULL, B_FOLLOW_NONE, 0);
  805.     AddChild(main_view);
  806.  
  807.     // Start 60Hz interrupt
  808.     tick_thread_active = true;
  809.     tick_thread = spawn_thread(tick_func, "Polling sucks...", B_DISPLAY_PRIORITY, this);
  810.     resume_thread(tick_thread);
  811.  
  812.     // Add filter for keyboard and mouse events
  813.     BMessageFilter *filter = new BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, filter_func);
  814.     AddCommonFilter(filter);
  815. }
  816.  
  817.  
  818. /*
  819.  *  Screen destructor
  820.  */
  821.  
  822. MacScreen::~MacScreen()
  823. {
  824.     // Stop 60Hz interrupt
  825.     if (tick_thread > 0) {
  826.         status_t l;
  827.         tick_thread_active = false;
  828.         wait_for_thread(tick_thread, &l);
  829.     }
  830.  
  831.     // Tell emulator that we're done
  832.     the_screen = NULL;
  833. }
  834.  
  835.  
  836. /*
  837.  *  Screen closed
  838.  */
  839.  
  840. void MacScreen::Quit(void)
  841. {
  842.     // Tell ScreenConnected() that we are quitting
  843.     quitting = true;
  844.     BWindowScreen::Quit();
  845. }
  846.  
  847.  
  848. /*
  849.  *  Screen connected/disconnected
  850.  */
  851.  
  852. void MacScreen::ScreenConnected(bool active)
  853. {
  854.     graphics_card_info *info = CardInfo();
  855.     screen_active = active;
  856.  
  857.     if (active == true) {
  858.  
  859.         // Set VideoMonitor
  860. #if REAL_ADDRESSING
  861.         VideoMonitor.mac_frame_base = (uint32)info->frame_buffer;
  862. #else
  863.         VideoMonitor.mac_frame_base = MacFrameBaseMac;
  864. #endif
  865.         VideoMonitor.bytes_per_row = info->bytes_per_row;
  866.         VideoMonitor.x = info->width;
  867.         VideoMonitor.y = info->height;
  868.         switch (info->bits_per_pixel) {
  869.             case 8:
  870.                 VideoMonitor.mode = VMODE_8BIT;
  871.                 break;
  872.             case 15:
  873.             case 16:
  874.                 VideoMonitor.mode = VMODE_16BIT;
  875.                 break;
  876.             case 32:
  877.                 VideoMonitor.mode = VMODE_32BIT;
  878.                 break;
  879.             default:
  880.                 VideoMonitor.mode = VMODE_8BIT;
  881.                 break;
  882.         }
  883.  
  884. #if !REAL_ADDRESSING
  885.         // Set variables for UAE memory mapping
  886.         MacFrameBaseHost = (uint8 *)info->frame_buffer;
  887.         MacFrameSize = VideoMonitor.bytes_per_row * VideoMonitor.y;
  888.         switch (info->bits_per_pixel) {
  889.             case 15:
  890.                 MacFrameLayout = FLAYOUT_HOST_555;
  891.                 break;
  892.             case 16:
  893.                 MacFrameLayout = FLAYOUT_HOST_565;
  894.                 break;
  895.             case 32:
  896.                 MacFrameLayout = FLAYOUT_HOST_888;
  897.                 break;
  898.             default:
  899.                 MacFrameLayout = FLAYOUT_DIRECT;
  900.                 break;
  901.         }
  902. #endif
  903.  
  904.         // Copy from backup store to frame buffer
  905.         if (frame_backup != NULL) {
  906.             memcpy(info->frame_buffer, frame_backup, VideoMonitor.bytes_per_row * VideoMonitor.y);
  907.             delete[] frame_backup;            
  908.             frame_backup = NULL;
  909.         }
  910.  
  911.         // Restore palette
  912.         if (VideoMonitor.mode == VMODE_8BIT)
  913.             SetColorList(palette);
  914.  
  915.         // Restart/signal emulator thread
  916.         release_sem(mac_os_lock);
  917.  
  918.     } else {
  919.  
  920.         if (!quitting) {
  921.  
  922.             // Stop emulator thread
  923.             acquire_sem(mac_os_lock);
  924.  
  925.             // Create backup store and save frame buffer
  926.             frame_backup = new uint8[VideoMonitor.bytes_per_row * VideoMonitor.y];
  927.             memcpy(frame_backup, info->frame_buffer, VideoMonitor.bytes_per_row * VideoMonitor.y);
  928.         }
  929.     }
  930. }
  931.  
  932.  
  933. /*
  934.  *  Screen 60Hz interrupt routine
  935.  */
  936.  
  937. status_t MacScreen::tick_func(void *arg)
  938. {
  939.     MacScreen *obj = (MacScreen *)arg;
  940.     while (obj->tick_thread_active) {
  941.  
  942.         // Wait
  943.         snooze(16667);
  944.  
  945.         // Workspace activated? Then poll the mouse and set the palette if needed
  946.         if (!obj->quitting && obj->LockWithTimeout(200000) == B_OK) {
  947.             if (obj->screen_active) {
  948.                 BPoint pt;
  949.                 uint32 button = 0;
  950.                 if (obj->palette_changed) {
  951.                     obj->palette_changed = false;
  952.                     obj->SetColorList(obj->palette);
  953.                 }
  954.                 obj->main_view->GetMouse(&pt, &button);
  955.                 set_mouse_position(320, 240);
  956.                 ADBMouseMoved(int(pt.x) - 320, int(pt.y) - 240);
  957.                 if (button & B_PRIMARY_MOUSE_BUTTON)
  958.                     ADBMouseDown(0);
  959.                 if (!(button & B_PRIMARY_MOUSE_BUTTON))
  960.                     ADBMouseUp(0);
  961.                 if (button & B_SECONDARY_MOUSE_BUTTON)
  962.                     ADBMouseDown(1);
  963.                 if (!(button & B_SECONDARY_MOUSE_BUTTON))
  964.                     ADBMouseUp(1);
  965.                 if (button & B_TERTIARY_MOUSE_BUTTON)
  966.                     ADBMouseDown(2);
  967.                 if (!(button & B_TERTIARY_MOUSE_BUTTON))
  968.                     ADBMouseUp(2);
  969.             }
  970.             obj->Unlock();
  971.         }
  972.     }
  973.     return 0;
  974. }
  975.