#include <stdio.h>
#include "sme_cross_platform.h"
#include "sme_ext_event.h"
#include "sme.h"
#include "sme_debug.h"
#include "Player.h"
#include "SingleState.h"
#include "EventId.h"
#ifdef WIN32
unsigned __stdcall ConsoleProc(void *Param);
unsigned __stdcall AppThread2Proc(void *Param);
#else
void* ConsoleProc(void *Param);
void* AppThread2Proc(void *Param);
#endif
BOOL g_bQuit=FALSE;
extern CPlayer Player1;
extern CPlayer Player2;
SME_THREAD_CONTEXT_T g_AppThreadContext1;
SME_THREAD_CONTEXT_T g_AppThreadContext2;
int main(int argc, char* argv[])
{
XTHREADHANDLE ConsoleThreadHandle = 0;
XTHREADHANDLE AppThread2Handle = 0;
int ret;
printf("Cross-Platform Orthogonal State Sample: \n");
// Install thread local storage data functions.
XTlsAlloc();
SmeSetTlsProc(XSetThreadContext, XGetThreadContext);
////////////////////////////////////////////////////////////////
// Engine initialization.
SmeInitEngine(&g_AppThreadContext1); // Initialize at the thread-1
// Save the thread context pointer to the TLS.
// XSetThreadContext(&g_AppThreadContext1);
XInitMsgBuf();
// Install event handler functions.
SmeSetExtEventOprProc(XGetExtEvent, XDelExtEvent, XPostThreadExtIntEvent, XPostThreadExtPtrEvent, XInitMsgBuf, XFreeMsgBuf);
// Install memory operation functions.
// SmeSetMemOprProc(SrvMAlloc, SrvMFree);???
////////////////////////////////////////////////////////////////
//SME_TURN_OFF_ALL_LOG_FIELDS(); // Test case
////////////////////////////////////////////////////////////////
// Create a thread to trigger external events.
ret = XCreateThread(ConsoleProc, NULL, &ConsoleThreadHandle);
// Create the Player2 application thread
ret = XCreateThread(AppThread2Proc, NULL, &AppThread2Handle);
SmeActivateObj(&Player1,NULL);
SmeRun();
printf("Exit from SmeRun() at thread-1 \n");
XFreeMsgBuf();
XFreeThreadContext(&g_AppThreadContext1); /* At last, free thread local storage resource. */
XWaitForThread(ConsoleThreadHandle);
XCLOSE_HANDLE(ConsoleThreadHandle);
XWaitForThread(AppThread2Handle);
XCLOSE_HANDLE(AppThread2Handle);
/*
do {
SME_EVENT_T * ret = XGetExtEvent();
if (ret == NULL)
printf("Timeout! \n");
else
printf("Message Received (id=%d,p1=%d,p2=%d)\n", ret->nEventID, 0,0);
} while (XIsThreadRunning(ThreadHandle));
*/
return 0;
}
#ifdef WIN32
unsigned __stdcall AppThread2Proc(void *Param)
#else
void* AppThread2Proc(void *Param)
#endif
{
SmeInitEngine(&g_AppThreadContext2); // Initialize at the thread-2
// Save the thread context pointer to the TLS.
// XSetThreadContext(&g_AppThreadContext2);
XInitMsgBuf();
// Install event handler functions.
SmeSetExtEventOprProc(XGetExtEvent, XDelExtEvent, XPostThreadExtIntEvent, XPostThreadExtPtrEvent, XInitMsgBuf, XFreeMsgBuf);
SmeActivateObj(&Player2,NULL);
SmeRun();
printf("Exit from SmeRun() at thread-2 \n");
XFreeMsgBuf();
XFreeThreadContext(&g_AppThreadContext2); /* At last, free thread local storage resource. */
return 0;
}
void ConsoleProcUsage()
{
printf("Console command usage:\n");
printf("==============================================================\n");
printf("? - Help\n");
printf("1 - Press Power button on Player-1 panel \n");
printf("2 - Press Pause/Resume button on Player-1 panel \n");
printf("a - Press Power button on Player-2 panel \n");
printf("b - Press Pause/Resume button on Player-2 panel \n");
printf("x/X- Quit\n");
printf("Others- Post a message.\n");
printf("==============================================================\n");
}
#ifdef WIN32
unsigned __stdcall ConsoleProc(void *Param)
#else
void* ConsoleProc(void *Param)
#endif
{
// On Linux platform, call the XInitTimer function at the time-out event trigger thread.
// On time-out, the external event trigger thread posts SME_EVENT_TIMER to the state machine application thread,
// and then invokes the callback function installed by the XSetTimer function.
XInitTimer();
printf("Enter the console procedure thread.\n");
ConsoleProcUsage();
while(TRUE)
{
int nParam1 =0;
if(g_bQuit) break;
//OSRelated::Sleep(ISVW_LOOP_INTERVAL);
nParam1 = fgetc(stdin);
switch(nParam1)
{
case EOF: return 0;//Cancel ConsoleProc in Daemon
case 'x':
case 'X'://Quit
XPostThreadExtIntEvent(&g_AppThreadContext1, SME_EVENT_EXIT_LOOP, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
printf("Exiting thread-1 ... Please wait. \n");
XPostThreadExtIntEvent(&g_AppThreadContext2, SME_EVENT_EXIT_LOOP, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
printf("Exiting thread-2 ... Please wait. \n");
return 0;
break;
case '?'://Help
ConsoleProcUsage();
break;
case '1':
XPostThreadExtIntEvent(&g_AppThreadContext1, EXT_EVENT_ID_POWER, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
printf("Post a message: EXT_EVENT_ID_POWER to the Thread-1!\n");
break;
case '2':
XPostThreadExtIntEvent(&g_AppThreadContext1, EXT_EVENT_ID_PAUSE_RESUME, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
printf("Post a message: EXT_EVENT_ID_PAUSE_RESUME to the Thread-1!\n");
break;
case 'a':
XPostThreadExtIntEvent(&g_AppThreadContext2, EXT_EVENT_ID_POWER, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
printf("Post a message: EXT_EVENT_ID_POWER to the Thread-2!\n");
break;
case 'b':
XPostThreadExtIntEvent(&g_AppThreadContext2, EXT_EVENT_ID_PAUSE_RESUME, 0, 0, NULL,0,SME_EVENT_CAT_OTHER);
printf("Post a message: EXT_EVENT_ID_PAUSE_RESUME to the Thread-2!\n");
break;
case '\n':
case '\r':
break;
default:
break;
}
}
return 0;
}