/*****************************************************************************/

/*
 *      remote.h -- REALmagic Celeste Application
 *
 *      Copyright (C) 1999-2000 Sigma Designs
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*****************************************************************************/

#ifndef _remote_h
#define _remote_h

#include "osremote.h"

//#include "pch.h"
#include "fmp.h"
#include "qerror.h"
#include "iobject.h"
#include "rmadef.h"
#include "irmaosd.h"
#include "cregset.h"
#include "rmabout.h"
#include "sduuids.h"
#include "hwiface.h"

typedef QGUID QIID;
typedef QGUID QCLSID;
#define REFQCLSID QIID * 
#define REFQIID QIID *
#include "osdlib.h"

//Defines for the domains 
#define STOP_STATE	0x01
#define VMGM_DOM	0x03
#define VTSM_DOM	0x04

#define HDTV_PAR_SIZE 9

#ifndef DPRINT
#ifdef DEBUG
#define DPRINT(_x_) printf _x_
#else
#define DPRINT(_x_)
#endif
#endif

// information message
typedef struct tagINFO {
	int		symbol;
	BYTE	test;
	BYTE	maddie;
	int	playing;
	BYTE	bDiscType;
	MESSAGE_QUEUE_OBJECT msgQueue;
	MESSAGE_QUEUE_OBJECT osdMsgQ;
	BYTE bABRepeat;
	BYTE bRepeat;
/*
	WORD	wFSpeed;
	WORD	wBSpeed;
*/
	BYTE	bAudioStream;
	BYTE	bAngleNumber;
	BYTE	bSPStream;	
	BYTE	bFullScreen;
	// Position
	DWORD	dwX;
	DWORD	dwY;
	DWORD	dwcX;
	DWORD dwcY;
	BYTE	bStep;
	BYTE	bPosAction;	// PosAction
	IRma  *pIRma;
	IOsd  *pIOsd;
	DWORD totalTime;
	DWORD dwTime;
//	CRITICAL_SECTION cS;
	IHdtv *pIHdtv;
	int   IHdtv_c;
//	BOOL  IHdtv_AR;
	BOOL  osdFlag;
	BOOL  blk;
	BOOL  bMode;
	DWORD dwSearch;
	BOOL  bSearch;
	BOOL  bFast;
//variables for the timer
//	DWORD stopTime;
//	DWORD currTime;
//
#ifdef PRINCETON
	BOOL  bAns;
#endif
} INFO;

// information message
typedef struct tagThINFO {
	PORTHANDLE portID;
	BYTE (*func)(PORTHANDLE hPort);
	MESSAGE_QUEUE_OBJECT msg_q_id;
#ifdef PRINCETON
	DWORD info;
#endif
} ThINFO;

#define CLOSED			-1
#define STOPPED			0
#define STOP_FOR_RESUME	1
#define PAUSED			2
#define PLAYING			3
#define TRICK_MODE		4
#define UNDETERMINED 5
#define TRICK_MODE_PAUSED 6
#define VCD_WAITING 7

#ifndef DRIVE_NUMBER
#ifdef PRINCETON
#define DRIVE_NUMBER 0
#else
#define DRIVE_NUMBER 2
#endif
#endif

#define POS_NO_ACTION	0
#define POS_RESIZE		1
#define POS_MOVE		2
#define POS_MAX			3

#define VGA_TV   118 //'v'
#define L_R      120 //'x'
#define MUTE     104 //'h'
#define VOL_MORE  43 //'+'
#define VOL_LESS  45 //'-'
#define ENT       10 //ENTER

#define COMPUTER 113 //'q'
#define PLAYER   105 //'i'

#define UP    28 //Up arrow
#define DOWN  29 //Down arrow
#define RIGHT 30 //Right arrow
#define LEFT  31 //Left arrow
#define SEL   32 //SPACE	

#define MENU     109 //'m'
#define TITLE    116 //'t'
#define OSD      111 //'o'
#define LANGUAGE 108 //'l'
#define ANGLE    97  //'a'
#define SUBTITLE 122 //'z'

#define PLAY_PAUSE 112 //'p'
#define STOP_1     115 //'s'
#define STEP_PLAY  121 //'y'
#define EJECT      101 //'e'
#define EJECT1     123 //"{"
#define REW        119 //'w'
#define FF         102 //'f'
#define LAST_TRACK  8  //Backspace
#define NEXT_TRACK  9  //Tab
#define SOFT_POWER 103 //'g'
#define RS232C     125 //'}'
#define DEF        46  //'.'
#define BREAK      98  //'b'
#define TIME_ADJUST 99 //'c'

#define ZERO  48
#define ONE   49
#define TWO   50
#define THREE 51
#define FOUR  52
#define FIVE  53
#define SIX   54
#define SEVEN 55
#define EIGHT 56
#define NINE  57

//constants for Osd
#define LEFT_TOP     0
#define LEFT_BOTTOM  1
#define RIGHT_TOP    2
#define RIGHT_BOTTOM 3

#define SHOW_HIDDEN    -1
#define SCR_OFF         0
#define SHOW_PLAY       1
#define SHOW_PAUSE      2
#define SHOW_STOP       3
#define SHOW_OPEN       4
#define SHOW_CLOSE      5
#define SHOW_REVERSE    6
#define SHOW_NEXT       7
#define SHOW_BACK       8
#define SHOW_SKIP       9
#define SHOW_TIME      10
#define SHOW_PREV      10
#define EV_NOTE        11
#define SHOW_MENU      12
#define SHOW_STOP2     13
#define SHOW_RES       14
#define SHOW_SUBTITLE  15
#define SHOW_LANGUAGE  16
#define SHOW_ANGLE     17
#define SHOW_AB_REPEAT 18
#define SHOW_REPEAT    19
#define SHOW_SEARCH    20
#define SHOW_ERROR     21
#define SHOW_STEPFF    22
#define SHOW_STEPRW    23
#define SHOW_SLOWFF    24
#define SHOW_SLOWRW    25
#define SHOW_SVCD_WAITING 26
#define SHOW_CHECK_PASSWORD 27
#define SHOW_LOADING 28

#define ROUND(x) ((DWORD)((x) + 0.5f))

#define SET_OSD_WINDOW(wnd,nx,ny,nw,nh,al) { \
	(wnd).x = (nx) - ((al) >> 1) * (nw);       \
	(wnd).y = (ny) - ((al) & 1) * (nh);        \
	(wnd).w = (nw); (wnd).h = (nh);            \
}

#define DISPLAY_OSD(pIOsd,p,x,y,al,sw,sh)                          \
if (pIOsd) {                                                       \
	OSD_WND Wnd;                                                     \
	DWORD width, height;                                             \
	int size;                                                        \
	PBYTE pict = bScale(p, sw, sh);                                  \
	if(pict) {                                                       \
		size = (((DWORD)(pict[1])) << 16) |                            \
					 (((DWORD)(pict[2])) << 8) |                             \
					 (DWORD)(pict[3]);                                       \
		width = ((((DWORD)(pict[4])) << 8) | pict[5]);                 \
		height = ((((DWORD)(pict[6])) << 8) | pict[7]) - 2;            \
		SET_OSD_WINDOW(Wnd, ROUND((x) * (sw)) & 0xfffffffe,            \
		               ROUND((y) * (sh)) & 0xfffffffe,                 \
		               width, height, al);                             \
		/*DPRINT(("COsd__Write %d\n", COsd__Write_c++));*/             \
		IOsd_Write(pIOsd, pict, size);                                 \
		IOsd_SetDest(pIOsd, &Wnd);                                     \
		/*OSSleep(10);*/                                               \
		IOsd_On(pIOsd);                                                \
		OSCelFree(pict);                                               \
	}                                                                \
}

#define SET_HIGHLIGHT(pIOsd,pwnd,sw,sh) \
if(pIOsd) {                             \
	OSD_WND Wnd;                          \
	Wnd.x = ROUND((sw) * (pwnd)->x);      \
	Wnd.y = ROUND((sh) * (pwnd)->y);      \
	Wnd.w = ROUND((sw) * (pwnd)->w);      \
	Wnd.h = ROUND((sh) * (pwnd)->h);      \
	IOsd_SetHli((pIOsd), &Wnd);           \
}

// Function defined in remote.c.
//void GetDigitEntry(int digit, MSG_Q_ID msgQueue);
int	 ReadDigit(int entry);
HRESULT ReadLibrary(INFO *Info);
int	 GetSymbol(MESSAGE_QUEUE_OBJECT msgQueue, INFO *pInfo);
void StartPlaying(INFO* Info);
void OSMain();
void portReadingThread(ThINFO *ms);
void timerThread(INFO *pInfo);
void autoPlayThread(INFO *pInfo);
#ifdef PRINCETON
void rsReadPort(ThINFO *ms);
#endif

// Function defined in osdFunc.c.
void osdDelay(INFO *ms);
void osdManager(INFO *pInfo);
ULONG textToBitmap(IRma *, unsigned char **, char *, RMAYUVENTRY *, PRMAFONT);
ULONG addPict(IRma *, unsigned char *, unsigned char *, unsigned char *);
void changeRes(INFO *pInfo);
#ifdef PRINCETON
BYTE rmaOsd2monoLCD(PBYTE bmp, PBYTE *pict0, PBYTE *pict1, BYTE color);
#endif

// Function defined in menus.c.
char onScreenMenus(INFO *pInfo);
PBYTE bScale(PBYTE bmp, float sw, float sh);

// Functions defined in VXWRemote.c.
void *OSCelMalloc(DWORD size);
void OSCelFree(void *p);
void OSCelMemCheck();
PORTHANDLE OSOpenPort(const char *name);
BOOL OSClosePort(PORTHANDLE fd);
int  OSReadPort(PORTHANDLE hPort);
void OSPurge(PORTHANDLE hPort);
int	 OSInitPolling(char *thread_name, FUNCPTR func_name, DWORD context);
int OSCelSetThreadPriority(int thID, int pri);
int OSCelGetThreadPriority(int thID, int *pri);
void OSThreadDelete(int thID);
//int OSGetThreadID(char *name);
void OSThreadExit();
void OSSleep_s(int i);
void OSSleep_t(int i);
DWORD OSGetTickCount_();
DWORD OSGetClockRate();
MESSAGE_QUEUE_OBJECT OSCreateMessageQueue (void);
unsigned long OSDestroyMessageQueue(MESSAGE_QUEUE_OBJECT MsgQ);
unsigned long OSGetMessage(MESSAGE_QUEUE_OBJECT MsgQ, char *Msg, unsigned long MsgLength);
unsigned long OSPostMessage(MESSAGE_QUEUE_OBJECT MsgQ, char *Msg, unsigned long MsgLength);
void OSPurgeMQ(MESSAGE_QUEUE_OBJECT msgQueue);

#ifdef _STPC_
// Functions defined in switch.c.
void ConfigSwitches ();
void ReadFrontPanel(INFO *pInfo);
#endif

#ifdef PRINCETON
void OSThreadSuspend(int threadID);
void OSThreadResume(int threadID);
PORTHANDLE OSOpenPortRW(const char *name);
void OSWriteToPort(PORTHANDLE fd, unsigned char value);

// Functions defined in rmlcd.c.
void DrawImage(BYTE *bitmap1, BYTE *bitmap2);
void LCDOn();
void LCDOff();

// Functions defined in rs232c.c.
PBYTE sendRSCommand(PBYTE command, INFO *pInfo);
BYTE checkSint(PBYTE str);

#define NACK 0x15
#define ACK 0x06
#define STX 0x02
#define ETX 0x03
#endif

#ifdef _INCLUDE_IGS_
int igs_start (void);
int igs_end (void);
int igs_overlay (int x, int y, int cx, int cy);
int igs_capture (int x, int y, int cx, int cy);
int igs_fullscreen_step1 (int cx, int cy);
int igs_fullscreen_step2 (int cx, int cy);
int igs_cursor (BYTE onoff);
int igs_overlay_enable (int e);
#endif

#endif // _remote_h

