home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Online / Twiny / Windows / Source / main.cpp < prev    next >
C/C++ Source or Header  |  2000-08-14  |  8KB  |  303 lines

  1. #include <winsock2.h>
  2.  
  3. #include "inputevent.h"
  4. #include "keyboard.h"
  5. #include "config.h"
  6. //---------------------------------------------------------------------------
  7.  
  8. static long FAR PASCAL WindowProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  9. static int ResolveName( char *hostname );
  10. static BOOL Setup( void );
  11. static void Cleanup( void );
  12. static void SetMouseXY( HWND hWnd );
  13. static void GoActive( HWND hWnd );
  14. static void GoInactive( void );
  15.  
  16. void SendEvent( char Class, short Code, short Qual, short MX, short MY );
  17.  
  18. BOOL        Active, MouseRecurse;
  19. int            Socket;
  20. static int    lastmx, lastmy, mousex, mousey, mousexy;
  21. static UINT    OldSSR;
  22.  
  23. //---------------------------------------------------------------------------
  24. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  25. {
  26.     WNDCLASS    wc;
  27.     HWND        hWnd;
  28.     MSG            msg;
  29.  
  30.     memset( &wc, 0, sizeof( wc ));
  31.  
  32.     wc.lpfnWndProc     = WindowProc;
  33.     wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  34.     wc.hbrBackground = (HBRUSH)GetStockObject( LTGRAY_BRUSH );
  35.     wc.lpszClassName = "TwinyClass";
  36.  
  37.     if( !RegisterClass( &wc ))
  38.         return( FALSE );
  39.  
  40.     LoadConfig( &Config );
  41.  
  42.     // Create the main window. 
  43.     hWnd = CreateWindow( "TwinyClass", "Twiny", 
  44.                          WS_VISIBLE | WS_POPUP | WS_DLGFRAME | WS_SYSMENU, 
  45.                          Config.WinX, Config.WinY, 
  46.                          Config.WinWidth, Config.WinHeight, 
  47.                          (HWND) NULL, (HMENU) NULL, hInstance, (LPVOID) NULL );
  48.  
  49.     // If the main window cannot be created, terminate
  50.     // the application.  
  51.     if( !hWnd ) 
  52.         return( FALSE );
  53.  
  54.     // Start the message loop.
  55.     while( GetMessage( &msg, (HWND) NULL, 0, 0 ) == TRUE ) {
  56.         TranslateMessage( &msg );
  57.         DispatchMessage( &msg );
  58.     }
  59.  
  60.     // Return the exit code to Windows.      
  61.     return( msg.wParam );
  62. }
  63. //---------------------------------------------------------------------------
  64. static void SetMouseXY( HWND hWnd )
  65. {
  66.     RECT    win_rect;
  67.  
  68.     GetWindowRect( hWnd, &win_rect );
  69.  
  70.     mousex = win_rect.left + (( win_rect.right - win_rect.left ) / 2 );
  71.     mousey = win_rect.top + (( win_rect.bottom - win_rect.top ) / 2 );
  72.  
  73.     MouseRecurse = TRUE;
  74.  
  75.     SetCursorPos( mousex, mousey );
  76. }
  77. //---------------------------------------------------------------------------
  78. static void GoActive( HWND hWnd )
  79. {
  80.     if( !Active ) {
  81.  
  82.         ShowCursor( FALSE );
  83.         SetCapture( hWnd );
  84.         SetMouseXY( hWnd );
  85.         InitKeyboard();
  86.  
  87.         SystemParametersInfo( SPI_SETSCREENSAVERRUNNING, 1, &OldSSR, 0 );
  88.  
  89.         SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS );
  90.  
  91.         Active = TRUE;
  92.     }
  93. }
  94. //---------------------------------------------------------------------------
  95. static void GoInactive( void ) 
  96. {
  97.     if( Active ) {
  98.  
  99.         ReleaseCapture();
  100.         ShowCursor( TRUE );
  101.  
  102.         SystemParametersInfo( SPI_SETSCREENSAVERRUNNING, 0, &OldSSR, 0 );
  103.  
  104.         SetPriorityClass( GetCurrentProcess(), IDLE_PRIORITY_CLASS );
  105.     
  106.         Active = FALSE;
  107.     }
  108. }
  109. //---------------------------------------------------------------------------
  110. static long FAR PASCAL WindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  111. {
  112.     switch( message ) {
  113.  
  114.         case WM_ACTIVATEAPP:
  115.             if( !wParam )
  116.                 GoInactive();
  117.             break;
  118.  
  119.         case WM_KEYUP:
  120.         case WM_SYSKEYUP:
  121.             if( Active && my_kbd_handler( wParam, ( lParam >> 16 ) & 0x1ff, FALSE, lParam & 0xFFFF ))
  122.                 GoInactive();
  123.             break;
  124.  
  125.         case WM_KEYDOWN:
  126.         case WM_SYSKEYDOWN:
  127.             if( Active && my_kbd_handler( wParam, ( lParam >> 16 ) & 0x1ff, TRUE, lParam & 0xFFFF ))
  128.                 GoInactive();
  129.             break;
  130.  
  131.         case WM_LBUTTONDOWN:
  132.             if( !Active ) {
  133.                 RECT    rect;
  134.                 int        mx, my;
  135.                 
  136.                 GetClientRect( hWnd, &rect );
  137.  
  138.                 mx = LOWORD( lParam );
  139.                 my = HIWORD( lParam );
  140.  
  141.                 if(( rect.left <= mx ) && ( rect.right >= mx ) &&
  142.                    ( rect.top <= my ) && ( rect.bottom >= my ))
  143.                     GoActive( hWnd );
  144.  
  145.             } else
  146.                 SendEvent( IECLASS_RAWMOUSE, IECODE_LBUTTON, IEQUALIFIER_LEFTBUTTON, 0, 0 );
  147.             break;
  148.  
  149.         case WM_LBUTTONUP:
  150.             SendEvent( IECLASS_RAWMOUSE, IECODE_LBUTTON | IECODE_UP_PREFIX, 0, 0, 0 );
  151.             break;
  152.  
  153.         case WM_MBUTTONUP:
  154.             SendEvent( IECLASS_RAWMOUSE, IECODE_MBUTTON | IECODE_UP_PREFIX, 0, 0, 0 );
  155.             break;
  156.  
  157.         case WM_RBUTTONUP:
  158.             SendEvent( IECLASS_RAWMOUSE, IECODE_RBUTTON | IECODE_UP_PREFIX, 0, 0, 0 );
  159.             break;
  160.  
  161.         case WM_MBUTTONDOWN:
  162.             SendEvent( IECLASS_RAWMOUSE, IECODE_MBUTTON, IEQUALIFIERB_MIDBUTTON, 0, 0 );
  163.             break;
  164.  
  165.         case WM_RBUTTONDOWN:
  166.             SendEvent( IECLASS_RAWMOUSE, IECODE_RBUTTON, IEQUALIFIER_RBUTTON, 0, 0 );
  167.             break;
  168.  
  169.         case WM_MOUSEMOVE: 
  170.             /*if( lParam != mousexy )*/ {
  171.                 int    mx = LOWORD( lParam ), my = HIWORD( lParam );
  172.  
  173.                 mousexy = lParam;
  174.  
  175.                 if( Active && !MouseRecurse ) {
  176.                     unsigned short    qual = 0;
  177.  
  178.                     if( wParam & MK_LBUTTON )
  179.                         qual |= IEQUALIFIER_LEFTBUTTON;
  180.  
  181.                     if( wParam & MK_MBUTTON )
  182.                         qual |= IEQUALIFIER_MIDBUTTON;
  183.  
  184.                     if( wParam & MK_RBUTTON )
  185.                         qual |= IEQUALIFIER_RBUTTON;
  186.  
  187.                     SendEvent( IECLASS_RAWMOUSE, IECODE_NOBUTTON, qual,
  188.                                mx - lastmx, my - lastmy );
  189.  
  190.                     MouseRecurse = TRUE;
  191.  
  192.                     SetCursorPos( mousex, mousey );
  193.  
  194.                 } else
  195.                     MouseRecurse = FALSE;
  196.  
  197.                 lastmx = mx;
  198.                 lastmy = my;
  199.             }
  200.             break;
  201.  
  202.         case WM_WINDOWPOSCHANGED:
  203.             SetMouseXY( hWnd );
  204.             break;
  205.  
  206.         case WM_CREATE:
  207.             if( !Setup() )
  208.                 PostQuitMessage( 0 );
  209.             break;
  210.  
  211.         case WM_DESTROY:
  212.             Cleanup();
  213.             break;
  214.  
  215.         case WM_CLOSE:
  216.             PostQuitMessage( 0 );
  217.             break;
  218.  
  219.         case WM_GETDLGCODE:
  220.             return( DLGC_WANTALLKEYS );
  221.  
  222.         default:
  223.             return( DefWindowProc( hWnd, message, wParam, lParam ));
  224.     }
  225.  
  226.     return( 0 );
  227. }
  228. //---------------------------------------------------------------------------
  229. static int ResolveName( char *hostname )
  230. {
  231.     struct in_addr  addr;
  232.  
  233.     addr.s_addr = inet_addr( hostname );
  234.  
  235.     if( addr.s_addr == INADDR_NONE ) {
  236.         struct hostent *host;
  237.  
  238.         if( host = gethostbyname( hostname ))
  239.             memcpy(( char * )&addr.s_addr, host->h_addr, host->h_length );
  240.     }
  241.  
  242.     return( addr.s_addr );
  243. }
  244. //---------------------------------------------------------------------------
  245. static BOOL Setup( void )
  246. {
  247.     BOOL    ok = FALSE;
  248.     WORD    VersionRequested = MAKEWORD( 2, 0 );
  249.     WSADATA    WsaData;
  250.  
  251.     if( !WSAStartup( VersionRequested, &WsaData )) {
  252.  
  253.         Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  254.  
  255.         if( Socket != INVALID_SOCKET ) {
  256.             struct sockaddr_in  addr;
  257.  
  258.             memset( &addr, 0, sizeof( addr ));
  259.  
  260.             addr.sin_family      = AF_INET;
  261.             addr.sin_port        = htons( Config.Port );
  262.             addr.sin_addr.s_addr = ResolveName( Config.Host );
  263.  
  264.             if( addr.sin_addr.s_addr )
  265.                 ok = connect( Socket, (struct sockaddr *)&addr, sizeof( addr )) != SOCKET_ERROR;
  266.         }
  267.     }
  268.  
  269.     return( ok );
  270. }
  271. //---------------------------------------------------------------------------
  272. static void Cleanup( void )
  273. {
  274.     if( Socket != INVALID_SOCKET )
  275.         closesocket( Socket );
  276.  
  277.     WSACleanup();
  278. }
  279. //---------------------------------------------------------------------------
  280. void SendEvent( char Class, short Code, short Qual, short MX, short MY )
  281. {
  282.     if( Active ) {
  283.         struct {
  284.             char    Class;
  285.             char    Subclass;
  286.             short    Code;
  287.             short    Qual;
  288.             short    MX;
  289.             short    MY;
  290.         } msg;
  291.  
  292.         msg.Class    = Class;
  293.         msg.Subclass = 0;
  294.         msg.Code     = htons( Code );
  295.         msg.Qual     = htons( Qual | GetQualifiers() | IEQUALIFIER_RELATIVEMOUSE );
  296.         msg.MX       = htons( MX );
  297.         msg.MY       = htons( MY );
  298.  
  299.         send( Socket, (char *)&msg, sizeof( msg ), 0 );
  300.     }
  301. }
  302. //---------------------------------------------------------------------------
  303.