Modules/Mouse - Fixing bugs, removing debug
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / input.c
index 4598502..9fe73ff 100644 (file)
@@ -74,17 +74,20 @@ void Input_HandleSelect(fd_set *set)
                if( read(giTerminalFD, &codepoint, sizeof(codepoint)) != sizeof(codepoint) )
                {
                        // oops, error
+                       _SysDebug("Terminal read failed?");
                }
        
+//             _SysDebug("Keypress 0x%x", codepoint);
+       
                switch(codepoint & 0xC0000000)
                {
                case 0x00000000:        // Key pressed
                        WM_Input_KeyDown(codepoint & KEY_CODEPOINT_MASK, scancode);
-               case 0x40000000:        // Key refire
+               case 0x80000000:        // Key release
                        WM_Input_KeyFire(codepoint & KEY_CODEPOINT_MASK, scancode);
                        scancode = 0;
                        break;
-               case 0x80000000:        // Key release
+               case 0x40000000:        // Key refire
                        WM_Input_KeyUp(codepoint & KEY_CODEPOINT_MASK, scancode);
                        scancode = 0;
                        break;
@@ -92,47 +95,72 @@ void Input_HandleSelect(fd_set *set)
                        scancode = codepoint & KEY_CODEPOINT_MASK;
                        break;
                }
-       
-               // TODO: pass on to message handler
-               _SysDebug("Keypress 0x%x", codepoint);
        }
 
        if(FD_ISSET(giMouseFD, set))
        {
+               const int c_n_axies = 4;
+               const int c_n_buttons = 5;
                 int    i;
-               struct sMouseInfo {
+               struct sMouseAxis {
+                        int16_t        MinValue;
+                        int16_t        MaxValue;
+                        int16_t        CurValue;
+                       uint16_t        CursorPos;
+               }       *axies;
+               uint8_t *buttons;
+               struct sMouseHdr {
                        uint16_t        NAxies;
                        uint16_t        NButtons;
-                       struct sMouseAxis {
-                                int16_t        MinValue;
-                                int16_t        MaxValue;
-                                int16_t        CurValue;
-                               uint16_t        CursorPos;
-                       }       Axies[2];
-                       uint8_t Buttons[3];
-               }       mouseinfo;
-       
+               }       *mouseinfo;
+               char    data[sizeof(*mouseinfo) + sizeof(*axies)*c_n_axies + c_n_buttons];
+
+               mouseinfo = (void*)data;
+
                seek(giMouseFD, 0, SEEK_SET);
-               if( read(giMouseFD, &mouseinfo, sizeof(mouseinfo)) != sizeof(mouseinfo) )
-               {
-                       // Not a 3 button mouse, oops
+               i = read(giMouseFD, data, sizeof(data));
+               i -= sizeof(*mouseinfo);
+               if( i < 0 ) {
+                       _SysDebug("Mouse data undersized (no header)");
+                       return ;
+               }
+               if( mouseinfo->NAxies > c_n_axies || mouseinfo->NButtons > c_n_buttons ) {
+                       _SysDebug(
+                               "%i axies, %i buttons above prealloc counts (%i, %i)",
+                               mouseinfo->NAxies, mouseinfo->NButtons, c_n_axies, c_n_buttons
+                               );
+                       return ;
+               }
+               if( i < sizeof(*axies)*mouseinfo->NAxies + mouseinfo->NButtons ) {
+                       _SysDebug("Mouse data undersized (body doesn't fit %i < %i)",
+                               i, sizeof(*axies)*mouseinfo->NAxies + mouseinfo->NButtons
+                               );
                        return ;
                }
 
+               // What? No X/Y?
+               if( mouseinfo->NAxies < 2 )
+                       return ;
+       
+               axies = (void*)( mouseinfo + 1 );
+               buttons = (void*)( axies + mouseinfo->NAxies );
+
                // Handle movement
-               Video_SetCursorPos( mouseinfo.Axies[0].CursorPos, mouseinfo.Axies[1].CursorPos );
+               Video_SetCursorPos( axies[0].CursorPos, axies[1].CursorPos );
+
+//             _SysDebug("Mouse to %i,%i", axies[0].CursorPos, axies[1].CursorPos);
 
                WM_Input_MouseMoved(
                        giInput_MouseX, giInput_MouseY,
-                       mouseinfo.Axies[0].CursorPos, mouseinfo.Axies[1].CursorPos
+                       axies[0].CursorPos, axies[1].CursorPos
                        );
-               giInput_MouseX = mouseinfo.Axies[0].CursorPos;
-               giInput_MouseY = mouseinfo.Axies[1].CursorPos;
+               giInput_MouseX = axies[0].CursorPos;
+               giInput_MouseY = axies[1].CursorPos;
 
-               for( i = 0; i < mouseinfo.NButtons; i ++ )
+               for( i = 0; i < mouseinfo->NButtons; i ++ )
                {
-                       int bit = 1 << i;
-                       int cur = mouseinfo.Buttons[i] > 128;
+                        int    bit = 1 << i;
+                        int    cur = buttons[i] > 128;
 
                        if( !!(giInput_MouseButtonState & bit) != cur )
                        {

UCC git Repository :: git.ucc.asn.au