From 60cbbcfe22ce94ffb69c1867b902e9bb693debaf Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Sat, 25 Apr 2015 09:40:59 +0200
Subject: Add Haiku support.

* Haiku is mostly compatible with BeOS, but there are a few minor
changes.

diff --git a/BasiliskII/src/BeOS/Makefile b/BasiliskII/src/BeOS/Makefile
index 14ae78a..9079334 100644
--- a/BasiliskII/src/BeOS/Makefile
+++ b/BasiliskII/src/BeOS/Makefile
@@ -33,7 +33,7 @@ TYPE= APP
 MACHINE=$(shell uname -m)
 ifeq ($(MACHINE), BePC)
 	CPUSRCS = ../uae_cpu/basilisk_glue.cpp ../uae_cpu/memory.cpp ../uae_cpu/newcpu.cpp \
-	    ../uae_cpu/readcpu.cpp ../uae_cpu/fpu/fpu_x86.cpp cpustbl.cpp cpudefs.cpp cpufast.s
+	    ../uae_cpu/readcpu.cpp ../uae_cpu/fpu/fpu_uae.cpp cpustbl.cpp cpudefs.cpp cpufast.s
 else
 #	CPUSRCS = ../powerrom_cpu/powerrom_cpu.cpp
 	CPUSRCS = ../uae_cpu/basilisk_glue.cpp ../uae_cpu/newcpu.cpp \
@@ -123,7 +123,7 @@ LINKER_FLAGS =
 
 
 ## include the makefile-engine
-include /boot/develop/etc/makefile-engine
+include /boot/system/develop/etc/makefile-engine
 
 
 #	special handling of UAE CPU engine
diff --git a/BasiliskII/src/BeOS/ether_beos.cpp b/BasiliskII/src/BeOS/ether_beos.cpp
index 42db87f..ebc85da 100644
--- a/BasiliskII/src/BeOS/ether_beos.cpp
+++ b/BasiliskII/src/BeOS/ether_beos.cpp
@@ -31,6 +31,11 @@
 #include <string.h>
 #include <sys/socket.h>
 
+#ifdef __HAIKU__
+#include <sys/select.h>
+#include <netinet/in.h>
+#endif
+
 #include "cpu_emulation.h"
 #include "main.h"
 #include "prefs.h"
diff --git a/BasiliskII/src/BeOS/prefs_editor_beos.cpp b/BasiliskII/src/BeOS/prefs_editor_beos.cpp
index 53affcf..a40e474 100644
--- a/BasiliskII/src/BeOS/prefs_editor_beos.cpp
+++ b/BasiliskII/src/BeOS/prefs_editor_beos.cpp
@@ -625,17 +625,15 @@ void PrefsWindow::add_serial_names(BPopUpMenu *menu, uint32 msg)
 		port->GetDeviceName(i, name);
 		menu->AddItem(new BMenuItem(name, new BMessage(msg)));
 	}
-	if (sys_info.platform_type == B_BEBOX_PLATFORM) {
-		BDirectory dir;
-		BEntry entry;
-		dir.SetTo("/dev/parallel");
-		if (dir.InitCheck() == B_NO_ERROR) {
-			dir.Rewind();
-			while (dir.GetNextEntry(&entry) >= 0) {
-				if (!entry.IsDirectory()) {
-					entry.GetName(name);
-					menu->AddItem(new BMenuItem(name, new BMessage(msg)));
-				}
+	BDirectory dir;
+	BEntry entry;
+	dir.SetTo("/dev/parallel");
+	if (dir.InitCheck() == B_NO_ERROR) {
+		dir.Rewind();
+		while (dir.GetNextEntry(&entry) >= 0) {
+			if (!entry.IsDirectory()) {
+				entry.GetName(name);
+				menu->AddItem(new BMenuItem(name, new BMessage(msg)));
 			}
 		}
 	}
diff --git a/BasiliskII/src/BeOS/scsi_beos.cpp b/BasiliskII/src/BeOS/scsi_beos.cpp
index 42f4498..75d1e29 100644
--- a/BasiliskII/src/BeOS/scsi_beos.cpp
+++ b/BasiliskII/src/BeOS/scsi_beos.cpp
@@ -23,7 +23,11 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <device/scsi.h>
+#ifdef __HAIKU__
+#include <CAM.h>
+#else
 #include <drivers/CAM.h>
+#endif
 
 #include "sysdeps.h"
 #include "main.h"
diff --git a/BasiliskII/src/BeOS/sys_beos.cpp b/BasiliskII/src/BeOS/sys_beos.cpp
index 7ad5e64..ff6fcb8 100644
--- a/BasiliskII/src/BeOS/sys_beos.cpp
+++ b/BasiliskII/src/BeOS/sys_beos.cpp
@@ -40,6 +40,11 @@
 #define DEBUG 0
 #include "debug.h"
 
+#ifdef __HAIKU__
+#include <fs_volume.h>
+#define unmount(x) fs_unmount_volume(x, 0)
+#endif
+
 
 // File handles are pointers to these structures
 struct file_handle {
@@ -284,6 +289,10 @@ void SysAddCDROMPrefs(void)
 
 void SysAddSerialPrefs(void)
 {
+#ifdef __HAIKU__
+	PrefsAddString("seriala", "serial1");
+	PrefsAddString("serialb", "serial2");
+#else
 	system_info info;
 	get_system_info(&info);
 	switch (info.platform_type) {
@@ -301,6 +310,7 @@ void SysAddSerialPrefs(void)
 			PrefsAddString("serialb", "none");
 			break;
 	}
+#endif
 }
 
 
diff --git a/BasiliskII/src/BeOS/sysdeps.h b/BasiliskII/src/BeOS/sysdeps.h
index 8280336..abeadbd 100644
--- a/BasiliskII/src/BeOS/sysdeps.h
+++ b/BasiliskII/src/BeOS/sysdeps.h
@@ -63,7 +63,9 @@ typedef off_t loff_t;
 
 // Networking types
 #define PF_INET AF_INET
+#ifndef __HAIKU__
 typedef int socklen_t;
+#endif
 
 // UAE CPU data types
 #define uae_s8 int8
-- 
2.2.2


From fe559c739aee468246e2efdc564d55985ba87e2d Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Sat, 25 Apr 2015 10:13:36 +0200
Subject: Haiku: Set -fomit-frame-pointer and -fno-PIC

* This frees some registers and make the x86 assembler code work.
* Restore use of fpu_x86 because of this.

diff --git a/BasiliskII/src/BeOS/Makefile b/BasiliskII/src/BeOS/Makefile
index 9079334..16cae87 100644
--- a/BasiliskII/src/BeOS/Makefile
+++ b/BasiliskII/src/BeOS/Makefile
@@ -33,7 +33,7 @@ TYPE= APP
 MACHINE=$(shell uname -m)
 ifeq ($(MACHINE), BePC)
 	CPUSRCS = ../uae_cpu/basilisk_glue.cpp ../uae_cpu/memory.cpp ../uae_cpu/newcpu.cpp \
-	    ../uae_cpu/readcpu.cpp ../uae_cpu/fpu/fpu_uae.cpp cpustbl.cpp cpudefs.cpp cpufast.s
+	    ../uae_cpu/readcpu.cpp ../uae_cpu/fpu/fpu_x86.cpp cpustbl.cpp cpudefs.cpp cpufast.s
 else
 #	CPUSRCS = ../powerrom_cpu/powerrom_cpu.cpp
 	CPUSRCS = ../uae_cpu/basilisk_glue.cpp ../uae_cpu/newcpu.cpp \
@@ -116,7 +116,7 @@ SYMBOLS =
 DEBUGGER = 
 
 #	specify additional compiler flags for all files
-COMPILER_FLAGS =
+COMPILER_FLAGS = -fomit-frame-pointer -fno-PIC
 
 #	specify additional linker flags
 LINKER_FLAGS =
-- 
2.2.2


From b1b4bf90050ccf5d6be8cc80e8399e68722a07cf Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Sun, 26 Apr 2015 10:37:51 +0200
Subject: BeOS: WIP update to video mode switching support code.


diff --git a/BasiliskII/src/BeOS/user_strings_beos.cpp b/BasiliskII/src/BeOS/user_strings_beos.cpp
index 138b0d7..c369457 100644
--- a/BasiliskII/src/BeOS/user_strings_beos.cpp
+++ b/BasiliskII/src/BeOS/user_strings_beos.cpp
@@ -38,6 +38,7 @@ user_string_def platform_strings[] = {
 	{STR_NET_CONFIG_MODIFY_WARN, "To enable Ethernet networking for Basilisk II, your network configuration has to be modified and the network restarted. Do you want this to be done now (selecting \"Cancel\" will disable Ethernet under Basilisk II)?."},
 	{STR_NET_ADDON_INIT_FAILED, "SheepShaver net server add-on found\nbut there seems to be no network hardware.\nPlease check your network preferences."},
 	{STR_NET_ADDON_CLONE_FAILED, "Cloning of the network transfer area failed."},
+	{STR_VIDEO_FAILED, "Failed to set video mode."},
 
 	{-1, NULL}	// End marker
 };
diff --git a/BasiliskII/src/BeOS/user_strings_beos.h b/BasiliskII/src/BeOS/user_strings_beos.h
index 87902de..8de695e 100644
--- a/BasiliskII/src/BeOS/user_strings_beos.h
+++ b/BasiliskII/src/BeOS/user_strings_beos.h
@@ -28,7 +28,8 @@ enum {
 	STR_NO_NET_ADDON_WARN,
 	STR_NET_CONFIG_MODIFY_WARN,
 	STR_NET_ADDON_INIT_FAILED,
-	STR_NET_ADDON_CLONE_FAILED
+	STR_NET_ADDON_CLONE_FAILED,
+	STR_VIDEO_FAILED
 };
 
 #endif
diff --git a/BasiliskII/src/BeOS/video_beos.cpp b/BasiliskII/src/BeOS/video_beos.cpp
index 1bb1d10..407630a 100644
--- a/BasiliskII/src/BeOS/video_beos.cpp
+++ b/BasiliskII/src/BeOS/video_beos.cpp
@@ -42,12 +42,16 @@
 #include "readcpu.h"
 #include "newcpu.h"
 
-#define DEBUG 0
+#define DEBUG 1
 #include "debug.h"
 
 #define DEBUGGER_AVAILABLE 0
 
 
+// Supported video modes
+static vector<video_mode> VideoModes;
+
+
 // Messages
 const uint32 MSG_REDRAW = 'draw';
 const uint32 MSG_ABOUT_REQUESTED = B_ABOUT_REQUESTED;
@@ -70,6 +74,26 @@ enum {
 extern void SysCreateVolumeMenu(BMenu *menu, uint32 msg);
 extern void SysMountVolume(const char *name);
 
+// Global variables
+static bool classic_mode = false;					// Flag: Classic Mac video mode
+static int scr_mode_bit = 0;
+
+ /*
+ *  monitor_desc subclass for BeOS display
+ */
+
+class BeOS_monitor_desc : public monitor_desc {
+public:
+       BeOS_monitor_desc(const vector<video_mode> &available_modes, video_depth default_depth, uint32 default_id) : monitor_desc(available_modes, default_depth, default_id) {}
+       ~BeOS_monitor_desc() {}
+
+       virtual void switch_to_current_mode(void);
+       virtual void set_palette(uint8 *pal, int num);
+
+       bool video_open(void);
+       void video_close(void);
+};
+
 
 /*
  *  A simple view class for blitting a bitmap on the screen
@@ -98,7 +122,7 @@ private:
 
 class MacWindow : public BDirectWindow {
 public:
-	MacWindow(BRect frame);
+	MacWindow(BRect frame, BeOS_monitor_desc& monitor);
 	virtual ~MacWindow();
 	virtual void MessageReceived(BMessage *msg);
 	virtual void DirectConnected(direct_buffer_info *info);
@@ -127,6 +151,8 @@ private:
 	int32 bytes_per_row;
 	color_space pixel_format;
 	bool unclipped;
+
+	BeOS_monitor_desc monitor;
 };
 
 
@@ -136,7 +162,7 @@ private:
 
 class MacScreen : public BWindowScreen {
 public:
-	MacScreen(const char *name, int mode_bit, status_t *error);
+	MacScreen(BeOS_monitor_desc& monitor, const char *name, int mode_bit, status_t *error);
 	virtual ~MacScreen();
 	virtual void Quit(void);
 	virtual	void ScreenConnected(bool active);
@@ -155,6 +181,8 @@ private:
 	bool quitting;				// Flag for ScreenConnected: We are quitting, don't pause emulator thread
 	bool screen_active;
 	bool first_time;
+
+	BeOS_monitor_desc monitor;
 };
 
 
@@ -170,74 +198,144 @@ static uint8 MacCursor[68] = {16, 1};		// Mac cursor image
  *  Initialization
  */
 
-// Add resolution to list of supported modes and set VideoMonitor
-static void set_video_monitor(uint32 width, uint32 height, uint32 bytes_per_row, int depth)
+// Add mode to list of supported modes
+static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 bytes_per_row, video_depth depth)
 {
 	video_mode mode;
-
 	mode.x = width;
 	mode.y = height;
-	mode.resolution_id = 0x80;
+	mode.resolution_id = resolution_id;
 	mode.bytes_per_row = bytes_per_row;
-
-	switch (depth) {
-		case 1:
-			mode.depth = VDEPTH_1BIT;
-			break;
-		case 2:
-			mode.depth = VDEPTH_2BIT;
-			break;
-		case 4:
-			mode.depth = VDEPTH_4BIT;
-			break;
-		case 8:
-			mode.depth = VDEPTH_8BIT;
-			break;
-		case 15:
-			mode.depth = VDEPTH_16BIT;
-			break;
-		case 16:
-			mode.depth = VDEPTH_16BIT;
-			break;
-		case 24:
-		case 32:
-			mode.depth = VDEPTH_32BIT;
-			break;
-	}
-
+	mode.depth = depth;
 	VideoModes.push_back(mode);
-	video_init_depth_list();
-	VideoMonitor.mode = mode;
 }
 
+// Add standard list of windowed modes for given color depth
+static void add_window_modes(video_depth depth)
+{
+	add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
+	add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
+	add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
+	add_mode(1024, 768, 0x83, TrivialBytesPerRow(1024, depth), depth);
+	add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth);
+	add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth);
+	add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth);
+}
+
+
 
 bool VideoInit(bool classic)
 {
-	// Create semaphore
-	mac_os_lock = create_sem(0, "MacOS Frame Buffer Lock");
+	classic_mode = classic;
 
 	// Get screen mode from preferences
-	const char *mode_str = PrefsFindString("screen");
+	const char *mode_str;
+	if (classic_mode)
+		mode_str = "win/512/342";
+	else
+		mode_str = PrefsFindString("screen");
 
 	// Determine type and mode
+	int default_width = 512, default_height = 384;
 	display_type = DISPLAY_WINDOW;
-	int width = 512, height = 384;
-	int scr_mode_bit = 0;
 	if (mode_str) {
-		if (sscanf(mode_str, "win/%d/%d", &width, &height) == 2)
+		if (sscanf(mode_str, "win/%d/%d", &default_width, &default_height) == 2)
 			display_type = DISPLAY_WINDOW;
 		else if (sscanf(mode_str, "scr/%d", &scr_mode_bit) == 1)
 			display_type = DISPLAY_SCREEN;
 	}
+#if 0
+	if (default_width <= 0)
+		default_width = DisplayWidth(x_display, screen);
+	else if (default_width > DisplayWidth(x_display, screen))
+		default_width = DisplayWidth(x_display, screen);
+	if (default_height <= 0)
+		default_height = DisplayHeight(x_display, screen);
+	else if (default_height > DisplayHeight(x_display, screen))
+		default_height = DisplayHeight(x_display, screen);
+#endif
+
+	// Mac screen depth follows X depth
+	video_depth default_depth = VDEPTH_1BIT;
+#if 0
+	switch (DefaultDepth(x_display, screen)) {
+		case 8:
+			default_depth = VDEPTH_8BIT;
+			break;
+		case 15: case 16:
+			default_depth = VDEPTH_16BIT;
+			break;
+		case 24: case 32:
+			default_depth = VDEPTH_32BIT;
+			break;
+	}
+#endif
+
+	// Construct list of supported modes
+	if (display_type == DISPLAY_WINDOW) {
+		if (classic)
+			add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
+		else {
+			for (unsigned d=VDEPTH_1BIT; d<=VDEPTH_32BIT; d++) {
+				//if (find_visual_for_depth(video_depth(d)))
+					add_window_modes(video_depth(d));
+			}
+		}
+	} else
+		add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
+	if (VideoModes.empty()) {
+		ErrorAlert(STR_VIDEO_FAILED);
+		return false;
+	}
+
+	// Find requested default mode with specified dimensions
+	uint32 default_id;
+	std::vector<video_mode>::const_iterator i, end = VideoModes.end();
+	for (i = VideoModes.begin(); i != end; ++i) {
+		if (i->x == default_width && i->y == default_height && i->depth == default_depth) {
+			default_id = i->resolution_id;
+			break;
+		}
+	}
+	if (i == end) { // not found, use first available mode
+		default_depth = VideoModes[0].depth;
+		default_id = VideoModes[0].resolution_id;
+	}
+
+#if DEBUG
+	D(bug("Available video modes:\n"));
+	for (i = VideoModes.begin(); i != end; ++i) {
+		int bits = 1 << i->depth;
+		if (bits == 16)
+			bits = 15;
+		else if (bits == 32)
+			bits = 24;
+		D(bug(" %dx%d (ID %02x), %d colors\n", i->x, i->y, i->resolution_id, 1 << bits));
+	}
+#endif
+
+    // Create X11_monitor_desc for this (the only) display
+    BeOS_monitor_desc *monitor = new BeOS_monitor_desc(VideoModes, default_depth, default_id);
+    VideoMonitors.push_back(monitor);
+
+    // Open display
+    return monitor->video_open();
+}
+
+bool BeOS_monitor_desc::video_open() {
+	// Create semaphore
+	mac_os_lock = create_sem(0, "MacOS Frame Buffer Lock");
+
+	const video_mode &mode = get_current_mode();
 
 	// Open display
 	switch (display_type) {
 		case DISPLAY_WINDOW:
-			the_window = new MacWindow(BRect(0, 0, width-1, height-1));
+			the_window = new MacWindow(BRect(0, 0, mode.x-1, mode.y-1), *this);
 			break;
 		case DISPLAY_SCREEN: {
 			status_t screen_error;
-			the_screen = new MacScreen(GetString(STR_WINDOW_TITLE), scr_mode_bit & 0x1f, &screen_error);
+			the_screen = new MacScreen(*this, GetString(STR_WINDOW_TITLE), scr_mode_bit & 0x1f, &screen_error);
 			if (screen_error != B_NO_ERROR) {
 				the_screen->PostMessage(B_QUIT_REQUESTED);
 				while (the_screen)
@@ -288,7 +386,7 @@ void VideoExit(void)
  *  Set palette
  */
 
-void video_set_palette(uint8 *pal, int num)
+void BeOS_monitor_desc::set_palette(uint8 *pal, int num)
 {
 	switch (display_type) {
 		case DISPLAY_WINDOW: {
@@ -313,8 +411,11 @@ void video_set_palette(uint8 *pal, int num)
  *  Switch video mode
  */
 
-void video_switch_to_mode(const video_mode &mode)
+void BeOS_monitor_desc::switch_to_current_mode()
 {
+	const video_mode &mode = get_current_mode();
+	D(bug("Switching modes!\n"));
+	fprintf(stderr, "width: %d", 1 << mode.depth);
 }
 
 
@@ -461,7 +562,9 @@ static filter_result filter_func(BMessage *msg, BHandler **target, BMessageFilte
  *  Window constructor
  */
 
-MacWindow::MacWindow(BRect frame) : BDirectWindow(frame, GetString(STR_WINDOW_TITLE), B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_CLOSABLE | B_NOT_ZOOMABLE)
+MacWindow::MacWindow(BRect frame, BeOS_monitor_desc& monitor)
+	: BDirectWindow(frame, GetString(STR_WINDOW_TITLE), B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_CLOSABLE | B_NOT_ZOOMABLE)
+	, monitor(monitor)
 {
 	supports_direct_mode = SupportsWindowMode();
 
@@ -472,15 +575,24 @@ MacWindow::MacWindow(BRect frame) : BDirectWindow(frame, GetString(STR_WINDOW_TI
 	// Allocate bitmap and Mac frame buffer
 	uint32 x = frame.IntegerWidth() + 1;
 	uint32 y = frame.IntegerHeight() + 1;
-	the_bitmap = new BBitmap(frame, B_COLOR_8_BIT);
-	the_buffer = new uint8[x * (y + 2)];	// "y + 2" for safety
+	const video_mode &mode = monitor.get_current_mode();
+	switch (mode.depth) {
+		case VDEPTH_1BIT:
+			the_bitmap = new BBitmap(frame, B_RGB16);
+			break;
+		case VDEPTH_8BIT:
+			the_bitmap = new BBitmap(frame, B_CMAP8);
+			break;
+		default:
+			fprintf(stderr, "width: %d", 1 << mode.depth);
+			debugger("OOPS");
+	}
+	the_buffer = new uint8[x * (y + 2) * 2];	// "y + 2" for safety
 
-	// Add resolution and set VideoMonitor
-	set_video_monitor(x, y, x, 8);
 #if REAL_ADDRESSING
-	VideoMonitor.mac_frame_base = (uint32)the_buffer;
+	monitor.set_mac_frame_base((uint32)the_buffer);
 #else
-	VideoMonitor.mac_frame_base = MacFrameBaseMac;
+	monitor.set_mac_frame_base(MacFrameBaseMac);
 #endif
 
 #if !REAL_ADDRESSING
@@ -656,12 +768,13 @@ void MacWindow::MessageReceived(BMessage *msg)
 			MessageQueue()->Unlock();
 			
 			// Convert Mac screen buffer to BeOS palette and blit
+			const video_mode &mode = monitor.get_current_mode();
 			uint8 *source = the_buffer - 1;
 			uint8 *dest = (uint8 *)the_bitmap->Bits() - 1;
-			uint32 length = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
+			uint32 length = mode.bytes_per_row * mode.y;
 			for (int i=0; i<length; i++)
 				*++dest = remap_mac_be[*++source];
-			BRect update_rect = BRect(0, 0, VideoMonitor.mode.x-1, VideoMonitor.mode.y-1);
+			BRect update_rect = BRect(0, 0, mode.x-1, mode.y-1);
 			main_view->DrawBitmapAsync(the_bitmap, update_rect, update_rect);
 			break;
 		}
@@ -781,8 +894,9 @@ status_t MacWindow::tick_func(void *arg)
 						uint8 *source = obj->the_buffer - 1;
 						uint8 *dest = (uint8 *)obj->bits;
 						uint32 bytes_per_row = obj->bytes_per_row;
-						int xsize = VideoMonitor.mode.x;
-						int ysize = VideoMonitor.mode.y;
+						const video_mode &mode = obj->monitor.get_current_mode();
+						int xsize = mode.x;
+						int ysize = mode.y;
 						for (int y=0; y<ysize; y++) {
 							uint32 *p = (uint32 *)dest - 1;
 							for (int x=0; x<xsize/4; x++) {
@@ -837,7 +951,9 @@ void BitmapView::MouseMoved(BPoint point, uint32 transit, const BMessage *messag
  *  Screen constructor
  */
 
-MacScreen::MacScreen(const char *name, int mode_bit, status_t *error) : BWindowScreen(name, 1 << mode_bit, error), tick_thread(-1)
+MacScreen::MacScreen(BeOS_monitor_desc& monitor, const char *name, int mode_bit, status_t *error)
+	: BWindowScreen(name, 1 << mode_bit, error), tick_thread(-1)
+	, monitor(monitor)
 {
 	// Set all variables
 	frame_backup = NULL;
@@ -902,26 +1018,21 @@ void MacScreen::ScreenConnected(bool active)
 {
 	graphics_card_info *info = CardInfo();
 	screen_active = active;
+	const video_mode &mode = monitor.get_current_mode();
 
 	if (active == true) {
 
-		// Add resolution and set VideoMonitor
-		if (first_time) {
-			set_video_monitor(info->width, info->height, info->bytes_per_row, info->bits_per_pixel);
-			first_time = false;
-		}
-
 		// Set VideoMonitor
 #if REAL_ADDRESSING
-		VideoMonitor.mac_frame_base = (uint32)info->frame_buffer;
+		monitor.set_mac_frame_base((uint32)info->frame_buffer);
 #else
-		VideoMonitor.mac_frame_base = MacFrameBaseMac;
+		monitor.set_mac_frame_base(MacFrameBaseMac);
 #endif
 
 #if !REAL_ADDRESSING
 		// Set variables for UAE memory mapping
 		MacFrameBaseHost = (uint8 *)info->frame_buffer;
-		MacFrameSize = VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y;
+		MacFrameSize = mode.bytes_per_row * mode.y;
 		switch (info->bits_per_pixel) {
 			case 15:
 				MacFrameLayout = FLAYOUT_HOST_555;
@@ -940,13 +1051,13 @@ void MacScreen::ScreenConnected(bool active)
 
 		// Copy from backup store to frame buffer
 		if (frame_backup != NULL) {
-			memcpy(info->frame_buffer, frame_backup, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
+			memcpy(info->frame_buffer, frame_backup, mode.bytes_per_row * mode.y);
 			delete[] frame_backup;			
 			frame_backup = NULL;
 		}
 
 		// Restore palette
-		if (VideoMonitor.mode.depth == VDEPTH_8BIT)
+		if (mode.depth == VDEPTH_8BIT)
 			SetColorList(palette);
 
 		// Restart/signal emulator thread
@@ -960,8 +1071,8 @@ void MacScreen::ScreenConnected(bool active)
 			acquire_sem(mac_os_lock);
 
 			// Create backup store and save frame buffer
-			frame_backup = new uint8[VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y];
-			memcpy(frame_backup, info->frame_buffer, VideoMonitor.mode.bytes_per_row * VideoMonitor.mode.y);
+			frame_backup = new uint8[mode.bytes_per_row * mode.y];
+			memcpy(frame_backup, info->frame_buffer, mode.bytes_per_row * mode.y);
 		}
 	}
 }
-- 
2.2.2


From f89bfc3201fc7f6f21dcdaa01f2129940b01964a Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Sun, 26 Apr 2015 15:26:11 +0200
Subject: BeOS: set some required defines for FPU configuration.


diff --git a/BasiliskII/src/BeOS/Makefile b/BasiliskII/src/BeOS/Makefile
index 16cae87..e7feb63 100644
--- a/BasiliskII/src/BeOS/Makefile
+++ b/BasiliskII/src/BeOS/Makefile
@@ -97,7 +97,7 @@ OPTIMIZE= FULL
 #	to use.  For example, setting DEFINES to "DEBUG=1" will cause the
 #	compiler option "-DDEBUG=1" to be used.  Setting DEFINES to "DEBUG"
 #	would pass "-DDEBUG" on the compiler's command line.
-DEFINES= 
+DEFINES= FPU_X86 SIZEOF_FLOAT=4 SIZEOF_DOUBLE=8 SIZEOF_LONG_DOUBLE=10
 
 #	specify special warning levels
 #	if unspecified default warnings will be used
-- 
2.2.2


From 10906fdb215ad7a089b80e4db5e88e329db80a5d Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Sun, 26 Apr 2015 15:31:30 +0200
Subject: Missing include for memset.


diff --git a/BasiliskII/src/uae_cpu/fpu/fpu_x86.cpp b/BasiliskII/src/uae_cpu/fpu/fpu_x86.cpp
index 91b88d3..70e5908 100644
--- a/BasiliskII/src/uae_cpu/fpu/fpu_x86.cpp
+++ b/BasiliskII/src/uae_cpu/fpu/fpu_x86.cpp
@@ -136,6 +136,7 @@
 
 #include <math.h>
 #include <stdio.h>
+#include <string.h>
 #include <signal.h>
 
 #include "sysdeps.h"
-- 
2.2.2


From a3718712b333e5da5bc1b9233a651f9a7d9841c2 Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Sun, 26 Apr 2015 16:09:31 +0200
Subject: BeOS: stray backslash in about_window.cpp


diff --git a/BasiliskII/src/BeOS/about_window.cpp b/BasiliskII/src/BeOS/about_window.cpp
index 4866c6e..94f9944 100644
--- a/BasiliskII/src/BeOS/about_window.cpp
+++ b/BasiliskII/src/BeOS/about_window.cpp
@@ -1,4 +1,4 @@
-\/*
+/*
  *  about_window.cpp - "About" window
  *
  *  Basilisk II (C) 1997-2008 Christian Bauer
-- 
2.2.2


From 336fdb9fdfd4632095e7670ecf2b56f58ee32987 Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Sun, 26 Apr 2015 16:56:44 +0200
Subject: BeOS: implement charset conversion.


diff --git a/BasiliskII/src/BeOS/extfs_beos.cpp b/BasiliskII/src/BeOS/extfs_beos.cpp
index 7ee4253..4c6fd7e 100644
--- a/BasiliskII/src/BeOS/extfs_beos.cpp
+++ b/BasiliskII/src/BeOS/extfs_beos.cpp
@@ -32,6 +32,7 @@
 
 #include <fs_attr.h>
 #include <support/TypeConstants.h>
+#include <support/UTF8.h>
 #include <storage/Mime.h>
 
 #include "extfs.h"
@@ -458,3 +459,29 @@ bool extfs_rename(const char *old_path, const char *new_path)
 {
 	return rename(old_path, new_path) == 0;
 }
+
+/*
+ *  Strings (filenames) conversion
+ */
+
+// Convert from the host OS filename encoding to MacRoman
+const char *host_encoding_to_macroman(const char *filename)
+{
+	int32 state = 0;
+	static char buffer[512];
+	int32 size = 512;
+	int32 insize = strlen(filename);
+	convert_from_utf8(B_MAC_ROMAN_CONVERSION, filename, &insize, buffer, &size, &state);
+	return buffer;
+}
+
+// Convert from MacRoman to host OS filename encoding
+const char *macroman_to_host_encoding(const char *filename)
+{
+	int32 state = 0;
+	static char buffer[512];
+	int32 insize = strlen(filename);
+	int32 size = 512;
+	convert_to_utf8(B_MAC_ROMAN_CONVERSION, filename, &insize, buffer, &size, &state);
+	return buffer;
+}
-- 
2.2.2


From 625b9151c7acbd075e0aa3efcb66e72952ad018e Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Sun, 26 Apr 2015 17:27:36 +0200
Subject: BeOS: support for vmdir preferences.


diff --git a/BasiliskII/src/BeOS/main_beos.cpp b/BasiliskII/src/BeOS/main_beos.cpp
index c8e1928..507e3bd 100644
--- a/BasiliskII/src/BeOS/main_beos.cpp
+++ b/BasiliskII/src/BeOS/main_beos.cpp
@@ -118,6 +118,8 @@ private:
 	class file_open_error {};
 	class file_read_error {};
 	class rom_size_error {};
+
+	char* vmdir;
 };
 
 static BasiliskII *the_app;
@@ -178,7 +180,7 @@ void BasiliskII::ReadyToRun(void)
 	// Read preferences
 	int argc = 0;
 	char **argv = NULL;
-	PrefsInit(argc, argv);
+	PrefsInit(vmdir, argc, argv);
 
 	// Init system routines
 	SysInit();
diff --git a/BasiliskII/src/BeOS/prefs_beos.cpp b/BasiliskII/src/BeOS/prefs_beos.cpp
index 556ed28..9a74d64 100644
--- a/BasiliskII/src/BeOS/prefs_beos.cpp
+++ b/BasiliskII/src/BeOS/prefs_beos.cpp
@@ -44,8 +44,23 @@ static BPath prefs_path;
  *  Load preferences from settings file
  */
 
-void LoadPrefs(void)
+void LoadPrefs(const char* vmdir)
 {
+#if 0
+	if (vmdir) {
+		prefs_path.SetTo(vmdir);
+		prefs_path.Append("prefs");
+		FILE *prefs = fopen(prefs_path.Path(), "r");
+		if (!prefs) {
+			printf("No file at %s found.\n", prefs_path.Path());
+			exit(1);
+		}
+		LoadPrefsFromStream(prefs);
+		fclose(prefs);
+		return;
+	}
+#endif
+
 	// Construct prefs path
 	find_directory(B_USER_SETTINGS_DIRECTORY, &prefs_path, true);
 	prefs_path.Append(PREFS_FILE_NAME);
-- 
2.2.2


From 59584722688e3e4b221d9fc4c9a04ead11493603 Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Sun, 26 Apr 2015 18:39:22 +0200
Subject: BeOS: get video to work again.

* The code was hardwired for 8-bit mode, unplug that and set it up for
32bit mode (much simpler).
* You need a ROM that supports 32bit mode for now
* Mode switching not supported yet.

diff --git a/BasiliskII/src/BeOS/video_beos.cpp b/BasiliskII/src/BeOS/video_beos.cpp
index 407630a..ae37d98 100644
--- a/BasiliskII/src/BeOS/video_beos.cpp
+++ b/BasiliskII/src/BeOS/video_beos.cpp
@@ -140,7 +140,6 @@ private:
 
 	BitmapView *main_view;		// Main view for bitmap drawing
 	BBitmap *the_bitmap;		// Mac screen bitmap
-	uint8 *the_buffer;			// Mac frame buffer
 
 	uint32 old_scroll_lock_state;
 
@@ -213,6 +212,7 @@ static void add_mode(uint32 width, uint32 height, uint32 resolution_id, uint32 b
 // Add standard list of windowed modes for given color depth
 static void add_window_modes(video_depth depth)
 {
+#if 0
 	add_mode(512, 384, 0x80, TrivialBytesPerRow(512, depth), depth);
 	add_mode(640, 480, 0x81, TrivialBytesPerRow(640, depth), depth);
 	add_mode(800, 600, 0x82, TrivialBytesPerRow(800, depth), depth);
@@ -220,6 +220,7 @@ static void add_window_modes(video_depth depth)
 	add_mode(1152, 870, 0x84, TrivialBytesPerRow(1152, depth), depth);
 	add_mode(1280, 1024, 0x85, TrivialBytesPerRow(1280, depth), depth);
 	add_mode(1600, 1200, 0x86, TrivialBytesPerRow(1600, depth), depth);
+#endif
 }
 
 
@@ -255,31 +256,34 @@ bool VideoInit(bool classic)
 		default_height = DisplayHeight(x_display, screen);
 #endif
 
-	// Mac screen depth follows X depth
+	// Mac screen depth follows BeOS depth
 	video_depth default_depth = VDEPTH_1BIT;
-#if 0
-	switch (DefaultDepth(x_display, screen)) {
-		case 8:
+	switch (BScreen().ColorSpace()) {
+		case B_CMAP8:
 			default_depth = VDEPTH_8BIT;
 			break;
-		case 15: case 16:
+		case B_RGB15:
 			default_depth = VDEPTH_16BIT;
 			break;
-		case 24: case 32:
+		case B_RGB32:
 			default_depth = VDEPTH_32BIT;
 			break;
+		default:
+			fprintf(stderr, "Unknown color space!");
 	}
-#endif
 
 	// Construct list of supported modes
 	if (display_type == DISPLAY_WINDOW) {
 		if (classic)
 			add_mode(512, 342, 0x80, 64, VDEPTH_1BIT);
 		else {
+			add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
+#if 0
 			for (unsigned d=VDEPTH_1BIT; d<=VDEPTH_32BIT; d++) {
-				//if (find_visual_for_depth(video_depth(d)))
+				if (find_visual_for_depth(video_depth(d)))
 					add_window_modes(video_depth(d));
 			}
+#endif
 		}
 	} else
 		add_mode(default_width, default_height, 0x80, TrivialBytesPerRow(default_width, default_depth), default_depth);
@@ -575,30 +579,38 @@ MacWindow::MacWindow(BRect frame, BeOS_monitor_desc& monitor)
 	// Allocate bitmap and Mac frame buffer
 	uint32 x = frame.IntegerWidth() + 1;
 	uint32 y = frame.IntegerHeight() + 1;
+	int fbsize = x * y;
 	const video_mode &mode = monitor.get_current_mode();
 	switch (mode.depth) {
 		case VDEPTH_1BIT:
-			the_bitmap = new BBitmap(frame, B_RGB16);
+			fprintf(stderr, "1BIT SCREEN CREATED");
+			the_bitmap = new BBitmap(frame, B_GRAY1);
+			fbsize /= 8;
 			break;
 		case VDEPTH_8BIT:
+			fprintf(stderr, "8BIT SCREEN CREATED");
 			the_bitmap = new BBitmap(frame, B_CMAP8);
 			break;
+		case VDEPTH_32BIT:
+			fprintf(stderr, "32BIT SCREEN CREATED");
+			the_bitmap = new BBitmap(frame, B_RGB32_BIG);
+			fbsize *= 4;
+			break;
 		default:
 			fprintf(stderr, "width: %d", 1 << mode.depth);
 			debugger("OOPS");
 	}
-	the_buffer = new uint8[x * (y + 2) * 2];	// "y + 2" for safety
 
 #if REAL_ADDRESSING
-	monitor.set_mac_frame_base((uint32)the_buffer);
+	monitor.set_mac_frame_base((uint32)the_bitmap->Bits());
 #else
 	monitor.set_mac_frame_base(MacFrameBaseMac);
 #endif
 
 #if !REAL_ADDRESSING
 	// Set variables for UAE memory mapping
-	MacFrameBaseHost = the_buffer;
-	MacFrameSize = x * y;
+	MacFrameBaseHost = (uint8*)the_bitmap->Bits();
+	MacFrameSize = fbsize;
 	MacFrameLayout = FLAYOUT_DIRECT;
 #endif
 
@@ -715,7 +727,6 @@ MacWindow::~MacWindow()
 
 	// Free bitmap and frame buffer
 	delete the_bitmap;
-	delete[] the_buffer;
 
 	// Tell emulator that we're done
 	the_window = NULL;
@@ -769,11 +780,6 @@ void MacWindow::MessageReceived(BMessage *msg)
 			
 			// Convert Mac screen buffer to BeOS palette and blit
 			const video_mode &mode = monitor.get_current_mode();
-			uint8 *source = the_buffer - 1;
-			uint8 *dest = (uint8 *)the_bitmap->Bits() - 1;
-			uint32 length = mode.bytes_per_row * mode.y;
-			for (int i=0; i<length; i++)
-				*++dest = remap_mac_be[*++source];
 			BRect update_rect = BRect(0, 0, mode.x-1, mode.y-1);
 			main_view->DrawBitmapAsync(the_bitmap, update_rect, update_rect);
 			break;
@@ -883,43 +889,7 @@ status_t MacWindow::tick_func(void *arg)
 
 			// Refresh screen unless Scroll Lock is down
 			if (!scroll_lock_state) {
-
-				// If direct frame buffer access is supported and the content area is completely visible,
-				// convert the Mac screen buffer directly. Otherwise, send a message to the window to do
-				// it into a bitmap
-				if (obj->supports_direct_mode) {
-					if (acquire_sem(obj->drawing_sem) != B_NO_ERROR)
-						return 0;
-					if (obj->unclipped && obj->pixel_format == B_CMAP8) {
-						uint8 *source = obj->the_buffer - 1;
-						uint8 *dest = (uint8 *)obj->bits;
-						uint32 bytes_per_row = obj->bytes_per_row;
-						const video_mode &mode = obj->monitor.get_current_mode();
-						int xsize = mode.x;
-						int ysize = mode.y;
-						for (int y=0; y<ysize; y++) {
-							uint32 *p = (uint32 *)dest - 1;
-							for (int x=0; x<xsize/4; x++) {
-#if B_HOST_IS_BENDIAN
-								uint32 c = obj->remap_mac_be[*++source] << 24;
-								c |= obj->remap_mac_be[*++source] << 16;
-								c |= obj->remap_mac_be[*++source] << 8;
-								c |= obj->remap_mac_be[*++source];
-#else
-								uint32 c = obj->remap_mac_be[*++source];
-								c |= obj->remap_mac_be[*++source] << 8;
-								c |= obj->remap_mac_be[*++source] << 16;
-								c |= obj->remap_mac_be[*++source] << 24;
-#endif
-								*++p = c;
-							}
-							dest += bytes_per_row;
-						}
-					} else
-						obj->PostMessage(MSG_REDRAW);
-					release_sem(obj->drawing_sem);
-				} else
-					obj->PostMessage(MSG_REDRAW);
+				obj->PostMessage(MSG_REDRAW);
 			}
 		}
 		snooze(16666);
-- 
2.2.2


From 4b5175d8d847788906e2849dfc37be06f3c0188a Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@gmail.com>
Date: Sun, 26 Apr 2015 18:42:06 +0200
Subject: BeOS: null-terminate strings when converting file names.


diff --git a/BasiliskII/src/BeOS/extfs_beos.cpp b/BasiliskII/src/BeOS/extfs_beos.cpp
index 4c6fd7e..bbaa8a8 100644
--- a/BasiliskII/src/BeOS/extfs_beos.cpp
+++ b/BasiliskII/src/BeOS/extfs_beos.cpp
@@ -472,6 +472,7 @@ const char *host_encoding_to_macroman(const char *filename)
 	int32 size = 512;
 	int32 insize = strlen(filename);
 	convert_from_utf8(B_MAC_ROMAN_CONVERSION, filename, &insize, buffer, &size, &state);
+	buffer[size] = 0;
 	return buffer;
 }
 
@@ -483,5 +484,6 @@ const char *macroman_to_host_encoding(const char *filename)
 	int32 insize = strlen(filename);
 	int32 size = 512;
 	convert_to_utf8(B_MAC_ROMAN_CONVERSION, filename, &insize, buffer, &size, &state);
+	buffer[size] = 0;
 	return buffer;
 }
-- 
2.2.2

