home *** CD-ROM | disk | FTP | other *** search
- #include <winsock2.h>
-
- #include "inputevent.h"
- #include "keyboard.h"
- #include "config.h"
- //---------------------------------------------------------------------------
-
- static long FAR PASCAL WindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
- static int ResolveName( char *hostname );
- static BOOL Setup( void );
- static void Cleanup( void );
- static void SetMouseXY( HWND hWnd );
- static void GoActive( HWND hWnd );
- static void GoInactive( void );
-
- void SendEvent( char Class, short Code, short Qual, short MX, short MY );
-
- BOOL Active, MouseRecurse;
- int Socket;
- static int lastmx, lastmy, mousex, mousey, mousexy;
- static UINT OldSSR;
-
- //---------------------------------------------------------------------------
- int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
- {
- WNDCLASS wc;
- HWND hWnd;
- MSG msg;
-
- memset( &wc, 0, sizeof( wc ));
-
- wc.lpfnWndProc = WindowProc;
- wc.hCursor = LoadCursor( NULL, IDC_ARROW );
- wc.hbrBackground = (HBRUSH)GetStockObject( LTGRAY_BRUSH );
- wc.lpszClassName = "TwinyClass";
-
- if( !RegisterClass( &wc ))
- return( FALSE );
-
- LoadConfig( &Config );
-
- // Create the main window.
- hWnd = CreateWindow( "TwinyClass", "Twiny",
- WS_VISIBLE | WS_POPUP | WS_DLGFRAME | WS_SYSMENU,
- Config.WinX, Config.WinY,
- Config.WinWidth, Config.WinHeight,
- (HWND) NULL, (HMENU) NULL, hInstance, (LPVOID) NULL );
-
- // If the main window cannot be created, terminate
- // the application.
- if( !hWnd )
- return( FALSE );
-
- // Start the message loop.
- while( GetMessage( &msg, (HWND) NULL, 0, 0 ) == TRUE ) {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
-
- // Return the exit code to Windows.
- return( msg.wParam );
- }
- //---------------------------------------------------------------------------
- static void SetMouseXY( HWND hWnd )
- {
- RECT win_rect;
-
- GetWindowRect( hWnd, &win_rect );
-
- mousex = win_rect.left + (( win_rect.right - win_rect.left ) / 2 );
- mousey = win_rect.top + (( win_rect.bottom - win_rect.top ) / 2 );
-
- MouseRecurse = TRUE;
-
- SetCursorPos( mousex, mousey );
- }
- //---------------------------------------------------------------------------
- static void GoActive( HWND hWnd )
- {
- if( !Active ) {
-
- ShowCursor( FALSE );
- SetCapture( hWnd );
- SetMouseXY( hWnd );
- InitKeyboard();
-
- SystemParametersInfo( SPI_SETSCREENSAVERRUNNING, 1, &OldSSR, 0 );
-
- SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS );
-
- Active = TRUE;
- }
- }
- //---------------------------------------------------------------------------
- static void GoInactive( void )
- {
- if( Active ) {
-
- ReleaseCapture();
- ShowCursor( TRUE );
-
- SystemParametersInfo( SPI_SETSCREENSAVERRUNNING, 0, &OldSSR, 0 );
-
- SetPriorityClass( GetCurrentProcess(), IDLE_PRIORITY_CLASS );
-
- Active = FALSE;
- }
- }
- //---------------------------------------------------------------------------
- static long FAR PASCAL WindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
- {
- switch( message ) {
-
- case WM_ACTIVATEAPP:
- if( !wParam )
- GoInactive();
- break;
-
- case WM_KEYUP:
- case WM_SYSKEYUP:
- if( Active && my_kbd_handler( wParam, ( lParam >> 16 ) & 0x1ff, FALSE, lParam & 0xFFFF ))
- GoInactive();
- break;
-
- case WM_KEYDOWN:
- case WM_SYSKEYDOWN:
- if( Active && my_kbd_handler( wParam, ( lParam >> 16 ) & 0x1ff, TRUE, lParam & 0xFFFF ))
- GoInactive();
- break;
-
- case WM_LBUTTONDOWN:
- if( !Active ) {
- RECT rect;
- int mx, my;
-
- GetClientRect( hWnd, &rect );
-
- mx = LOWORD( lParam );
- my = HIWORD( lParam );
-
- if(( rect.left <= mx ) && ( rect.right >= mx ) &&
- ( rect.top <= my ) && ( rect.bottom >= my ))
- GoActive( hWnd );
-
- } else
- SendEvent( IECLASS_RAWMOUSE, IECODE_LBUTTON, IEQUALIFIER_LEFTBUTTON, 0, 0 );
- break;
-
- case WM_LBUTTONUP:
- SendEvent( IECLASS_RAWMOUSE, IECODE_LBUTTON | IECODE_UP_PREFIX, 0, 0, 0 );
- break;
-
- case WM_MBUTTONUP:
- SendEvent( IECLASS_RAWMOUSE, IECODE_MBUTTON | IECODE_UP_PREFIX, 0, 0, 0 );
- break;
-
- case WM_RBUTTONUP:
- SendEvent( IECLASS_RAWMOUSE, IECODE_RBUTTON | IECODE_UP_PREFIX, 0, 0, 0 );
- break;
-
- case WM_MBUTTONDOWN:
- SendEvent( IECLASS_RAWMOUSE, IECODE_MBUTTON, IEQUALIFIERB_MIDBUTTON, 0, 0 );
- break;
-
- case WM_RBUTTONDOWN:
- SendEvent( IECLASS_RAWMOUSE, IECODE_RBUTTON, IEQUALIFIER_RBUTTON, 0, 0 );
- break;
-
- case WM_MOUSEMOVE:
- /*if( lParam != mousexy )*/ {
- int mx = LOWORD( lParam ), my = HIWORD( lParam );
-
- mousexy = lParam;
-
- if( Active && !MouseRecurse ) {
- unsigned short qual = 0;
-
- if( wParam & MK_LBUTTON )
- qual |= IEQUALIFIER_LEFTBUTTON;
-
- if( wParam & MK_MBUTTON )
- qual |= IEQUALIFIER_MIDBUTTON;
-
- if( wParam & MK_RBUTTON )
- qual |= IEQUALIFIER_RBUTTON;
-
- SendEvent( IECLASS_RAWMOUSE, IECODE_NOBUTTON, qual,
- mx - lastmx, my - lastmy );
-
- MouseRecurse = TRUE;
-
- SetCursorPos( mousex, mousey );
-
- } else
- MouseRecurse = FALSE;
-
- lastmx = mx;
- lastmy = my;
- }
- break;
-
- case WM_WINDOWPOSCHANGED:
- SetMouseXY( hWnd );
- break;
-
- case WM_CREATE:
- if( !Setup() )
- PostQuitMessage( 0 );
- break;
-
- case WM_DESTROY:
- Cleanup();
- break;
-
- case WM_CLOSE:
- PostQuitMessage( 0 );
- break;
-
- case WM_GETDLGCODE:
- return( DLGC_WANTALLKEYS );
-
- default:
- return( DefWindowProc( hWnd, message, wParam, lParam ));
- }
-
- return( 0 );
- }
- //---------------------------------------------------------------------------
- static int ResolveName( char *hostname )
- {
- struct in_addr addr;
-
- addr.s_addr = inet_addr( hostname );
-
- if( addr.s_addr == INADDR_NONE ) {
- struct hostent *host;
-
- if( host = gethostbyname( hostname ))
- memcpy(( char * )&addr.s_addr, host->h_addr, host->h_length );
- }
-
- return( addr.s_addr );
- }
- //---------------------------------------------------------------------------
- static BOOL Setup( void )
- {
- BOOL ok = FALSE;
- WORD VersionRequested = MAKEWORD( 2, 0 );
- WSADATA WsaData;
-
- if( !WSAStartup( VersionRequested, &WsaData )) {
-
- Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
-
- if( Socket != INVALID_SOCKET ) {
- struct sockaddr_in addr;
-
- memset( &addr, 0, sizeof( addr ));
-
- addr.sin_family = AF_INET;
- addr.sin_port = htons( Config.Port );
- addr.sin_addr.s_addr = ResolveName( Config.Host );
-
- if( addr.sin_addr.s_addr )
- ok = connect( Socket, (struct sockaddr *)&addr, sizeof( addr )) != SOCKET_ERROR;
- }
- }
-
- return( ok );
- }
- //---------------------------------------------------------------------------
- static void Cleanup( void )
- {
- if( Socket != INVALID_SOCKET )
- closesocket( Socket );
-
- WSACleanup();
- }
- //---------------------------------------------------------------------------
- void SendEvent( char Class, short Code, short Qual, short MX, short MY )
- {
- if( Active ) {
- struct {
- char Class;
- char Subclass;
- short Code;
- short Qual;
- short MX;
- short MY;
- } msg;
-
- msg.Class = Class;
- msg.Subclass = 0;
- msg.Code = htons( Code );
- msg.Qual = htons( Qual | GetQualifiers() | IEQUALIFIER_RELATIVEMOUSE );
- msg.MX = htons( MX );
- msg.MY = htons( MY );
-
- send( Socket, (char *)&msg, sizeof( msg ), 0 );
- }
- }
- //---------------------------------------------------------------------------
-