📄 tx_core.cpp
字号:
#include <windows.h>
#include <stdio.h>
#include <fcntl.h>
#include "txcpi.h"
#include "tx_core.h"
#include "conio.h"
struct _handles
{
short (*tx_handlers)(TX_HANDLE fd, short events);
TX_HANDLE tx_handles;
} tx_core_handles[MAXIMUM_WAIT_OBJECTS];
CPI_WAIT_TYPE tx_core_wait_objs[MAXIMUM_WAIT_OBJECTS];
DWORD numhndls = 0;
CPI_WAIT_TYPE stdInput, stdError, stdOutput;
BOOL tx_Core_newinit=FALSE;
/*
*
* tx_core_gets - This routine helps input a line from the keyboard
*
*/
S8 *tx_core_gets(S8 *input, S16 len)
{
char *fgets_result;
if (tx_Core_newinit == FALSE)
{
/* turn on SOME 'special' handling they (NT) will do!! */
SetConsoleMode(stdInput, ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT);
tx_Core_newinit = TRUE;
}
fgets_result = fgets(input, (int)len, stdin);
while ( (strlen(input) > 0) && (input[strlen(input) - 1] < ' ') )
input[strlen(input) - 1] = '\0'; /* remove any trailing non-printable characters */
return(fgets_result);
}
/*
*
* tx_core_kbhit - This routine helps input a line from the keyboard
* It takes the place of the kbhit for NT, simply by calling it.
*
*/
BOOL tx_core_kbhit()
{
return(kbhit());
}
/*
* SetPoll - This routine describes which events to watch for on a
* particular wait handle.
*
*/
S16 tx_core_set_poll(CPI_WAIT_TYPE fd, S16 events)
{
return(TX_CORE_SUCCESS); /* NT does not support this functionality*/
}
/*
* process_event - DUMMY routine for Unix code, NT does not need
*
*
*/
BOOL tx_core_process_events(S16 event, S16 events)
{
return(TRUE); /* NT does not support this functionality*/
}
/*
* InstallHandler - This routine sets up a routine to be called when
* an event is received for a particular wait object
*
* Parameters -
* fd - This is the object this OS will "wait on"
* handle - This is the OS specific CPI handle, This MAY
* be the same as fd, but for OS/2 and NT it will not
* If they are the same, set this to CPI_INVALID_HANDLE
* handler - A handler routine for when the wait wakes up
*
*/
S16 tx_core_install_handler(CPI_WAIT_TYPE fd, TX_HANDLE handle, S16 (*handler)(TX_HANDLE fd, S16 events))
{
short i;
/* check to see if this handle is already set up */
for (i = 0; i < MAXIMUM_WAIT_OBJECTS; i++)
{
if (tx_core_wait_objs[i] == fd)
return(TX_CORE_EEXIST);
}
/* if it does not already have an entry, find an empty slot */
for (i = 0; i < MAXIMUM_WAIT_OBJECTS; i++)
{
if (tx_core_wait_objs[i] == INVALID_HANDLE_VALUE)
{
tx_core_wait_objs[i] = fd;
tx_core_handles[i].tx_handlers = handler;
tx_core_handles[i].tx_handles = handle; /* set it even if CPI_INVALID_HANDLE */
++numhndls;
return(TX_CORE_SUCCESS);
}
}
/* if we get here, there were no empty handles, so tell the user that */
return(TX_CORE_ENOMEM);
}
/*
* CoreInit - This routine is called to set up the use of this "core"
*
* It should set up any other OS specific stuff, including those that
* are concerned with screen management
*
*/
S16 tx_core_init()
{
short i;
stdInput = GetStdHandle(STD_INPUT_HANDLE);
FlushConsoleInputBuffer(stdInput);
/* turn off the 'special' handling they will do!! */
SetConsoleMode(stdInput, 0);
stdError = GetStdHandle(STD_ERROR_HANDLE);
stdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
for (i = 0; i < MAXIMUM_WAIT_OBJECTS; i++)
{
tx_core_wait_objs[i] = INVALID_HANDLE_VALUE;
tx_core_handles[i].tx_handlers = NULL;
tx_core_handles[i].tx_handles = CPI_INVALID_HANDLE;
}
numhndls = 0;
return(TX_CORE_SUCCESS);
}
/*
* PollLoop - This routine actually does that wait then dispatches
* the proper routine to service the event
*
*/
S32 tx_core_poll_loop(S32 wait_time)
{
short retcode;
unsigned long i;
BOOL skip_this = FALSE;
while (1)
{
/* do a wait for all the objects */
if ( (i = WaitForMultipleObjects(numhndls, tx_core_wait_objs, FALSE, wait_time)) ==
WAIT_FAILED)
{
printf("TX_CORE - WaitForMultipleObjects failed %d\n", GetLastError() );
exit(GetLastError());
}
/* did ANYTHING happen, ie, did we timeout */
if (i == WAIT_TIMEOUT)
{
return(TX_CORE_TIMEOUT);
}
/* make sure we have a handler for this index */
if ( (tx_core_handles[i].tx_handlers != NULL) && (!skip_this) )
{
retcode = (*tx_core_handles[i].tx_handlers)(tx_core_handles[i].tx_handles, 0);
/* check the return **/
if (retcode == TX_CORE_ERROR)
{
printf("TX_CORE - Handler returned failure: retcode = %d, "
"handle = %d\n", retcode, tx_core_handles[i].tx_handles);
return((S32)GetLastError());
}
/* the return may stop this loop from happening */
else if (retcode == TX_CORE_EXIT_POLL_LOOP)
{
return(TX_CORE_SUCCESS);
}
}
/* this section MAY be an NT only kind of issue - DR */
/* for NT, we want to make sure the mouse hasn't goofed us up */
if (stdInput == tx_core_wait_objs[i])
FlushConsoleInputBuffer(stdInput);
/* make sure we re-set this next time */
skip_this = FALSE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -