/* 
 * UAE - The Un*x Amiga Emulator
 *
 * BeBox port specific stuff
 * 
 * (c) 1996 Christian Bauer
 * (c) 1996 Patrick Hanevold
 */

extern "C" {
#include "sysconfig.h"
#include "sysdeps.h"
#include "config.h"
#include "options.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "disk.h"
#include "debug.h"
#include "xwin.h"
#include "os.h"
#include "filesys.h"
#include "keybuf.h"
#include "keyboard.h"
#include "gui.h"
extern void usage(void);
extern void parse_cmdline(int,char**);
extern unsigned long frametime,timeframes;
}

class UAEWindow;
class BitmapView;
class Emulator;


/*
 *  The BeOS application object
 */

class UAE : public BApplication {
public:
	UAE();
	virtual void ArgvReceived(int argc, char **argv);
	virtual void ReadyToRun(void);
	virtual bool QuitRequested(void);
	virtual void AboutRequested(void);
//	virtual void MessageReceived(BMessage*);
	virtual void RefsReceived(BMessage*);
	virtual void FilePanelClosed(BMessage*);
	void RequestFile(char*,int);
private:
	BBitmap *the_bitmap;
	UAEWindow *main_window;
	Emulator *the_emulator;
};


/*
 *  The window in which the Amiga graphics are displayed, handles I/O
 */

class UAEWindow : public BWindow {
public:
	UAEWindow(BRect frame, BBitmap *bitmap);
	virtual bool QuitRequested(void);
	virtual void WindowActivated(bool active);
	virtual void MessageReceived(BMessage*);
private:
	BitmapView *main_view;
};

/*
 *  A simple view class for blitting a bitmap on the screen
 */

class BitmapView : public BView {
public:
	BitmapView(BRect frame, BBitmap *bitmap);
	virtual void Draw(BRect update);
	virtual void KeyDown(ulong);
	void Draw(BRect from, BRect to);
private:
	BBitmap *the_bitmap;
};

/*
 *	LED
 */

struct Color{
	int	R;
	int	G;
	int	B;
};

class LEDView : public BView {
public:
	LEDView(BRect,int,int,int,int,int,int);
	virtual void AttachedToWindow(void);
	virtual void Draw(BRect);
	void	SetState(bool);
private:
	void	Refresh(void);
	BRect	bounds;
	Color	ActiveColor;
	Color	IdleColor;
	bool	State;
};

/*
 *	Meter
 */

class MeterView : public BView {
public:
	MeterView(BRect);
	virtual void AttachedToWindow(void);
	virtual void Draw(BRect);
	virtual void Pulse(void);
private:
	void	Refresh(void);
	BRect	bounds;
};

/*
 *  For running the emulation in its own thread
 */

class Emulator {
public:
	Emulator() {thread_running = FALSE;}
	void Run(void);
	void Quit(void);

private:
	static long thread_invoc(void*); //Emulator *obj);
	void thread_func(void);

	thread_id the_thread;
	bool thread_running;
};


// Keyboard and mouse
int buttonstate[3];
int newmousecounters;
int lastmx, lastmy;

key_info old_key_info;
bool inwindow;
bool window_open;

struct Event{
	Event	*Next;
	int		Age;
	char	Key;
};

Event *CurentEvent,*LastEvent;

// Color map and bitmap
xcolnr xcolors[4096];
struct vidbuf_description gfxvidinfo;

int vsize, hsize, hpixels;

BitmapView *bitmap_view;
UAEWindow *bitmap_window;


// Array for converting Be keycodes to Amiga keycodes
int keycode2amiga[128] = {
	-1, AK_ESC, AK_F1, AK_F2, AK_F3, AK_F4, AK_F5, AK_F6,
	AK_F7, AK_F8, AK_F9, AK_F10, AK_LALT, AK_mousestuff, AK_RALT, -1,

	-1, AK_BACKQUOTE, AK_1, AK_2, AK_3, AK_4, AK_5, AK_6,
	AK_7, AK_8, AK_9, AK_0, AK_MINUS, AK_EQUAL, AK_BS, AK_HELP,

	AK_NPLPAREN, AK_RAMI, AK_NPLPAREN, AK_NPDIV, AK_NPMUL, AK_NPSUB, AK_TAB, AK_Q,
	AK_W, AK_E, AK_R, AK_T, AK_Y, AK_U, AK_I, AK_O,

	AK_P, AK_LBRACKET, AK_RBRACKET, AK_BACKSLASH, AK_DEL, AK_NPRPAREN, AK_LAMI, AK_NP7,
	AK_NP8, AK_NP9, AK_NPADD, AK_CAPSLOCK, AK_A, AK_S, AK_D, AK_F,

	AK_G, AK_H, AK_J, AK_K, AK_L, AK_SEMICOLON, AK_QUOTE, AK_RET,
	AK_NP4, AK_NP5, AK_NP6, AK_LSH, AK_Z, AK_X, AK_C, AK_V,

	AK_B, AK_N, AK_M, AK_COMMA, AK_PERIOD, AK_SLASH, AK_RSH, AK_UP,
	AK_NP1, AK_NP2, AK_NP3, AK_ENT, AK_CTRL, AK_LAMI, AK_SPC, AK_RAMI,

	AK_RALT, AK_LF, AK_DN, AK_RT, AK_NP0, AK_NPDEL, AK_LALT, AK_RALT,
	AK_LTGT, -1, -1, -1, -1, -1, -1, -1,

	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1
};
