Release 970112
[wine] / windows / keyboard.c
1 /*
2  * Keyboard related functions
3  *
4  * Copyright 1993 Bob Amstadt
5  * Copyright 1996 Albrecht Kleine 
6  */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include "win.h"
11 #include "windows.h"
12 #include "accel.h"
13 #include "stddebug.h"
14 #include "debug.h"
15
16 extern BOOL MouseButtonsStates[3];
17 extern BOOL AsyncMouseButtonsStates[3];
18 extern BYTE InputKeyStateTable[256];
19 BYTE AsyncKeyStateTable[256];
20
21 extern BYTE QueueKeyStateTable[256];
22
23 /**********************************************************************
24  *              GetKeyState                     [USER.106]
25  * An application calls the GetKeyState function in response to a
26  * keyboard-input message.  This function retrieves the state of the key
27  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 390)
28  */
29 INT GetKeyState(INT vkey)
30 {
31     INT retval;
32
33     switch (vkey)
34         {
35         case VK_LBUTTON : /* VK_LBUTTON is 1 */
36             retval = MouseButtonsStates[0];
37             break;
38         case VK_MBUTTON : /* VK_MBUTTON is 4 */
39             retval = MouseButtonsStates[1];
40             break;
41         case VK_RBUTTON : /* VK_RBUTTON is 2 */
42             retval = MouseButtonsStates[2];
43             break;
44         default :
45             if (vkey >= 'a' && vkey <= 'z')
46                 vkey += 'A' - 'a';
47             retval = ( (INT)(QueueKeyStateTable[vkey] & 0x80) << 8 ) |
48                        (INT)(QueueKeyStateTable[vkey] & 0x01);
49         }
50     dprintf_key(stddeb, "GetKeyState(0x%x) -> %x\n", vkey, retval);
51     return retval;
52 }
53
54 /**********************************************************************
55  *              GetKeyboardState                        [USER.222]
56  * An application calls the GetKeyboardState function in response to a
57  * keyboard-input message.  This function retrieves the state of the keyboard
58  * at the time the input message was generated.  (SDK 3.1 Vol 2. p 387)
59  */
60 void GetKeyboardState(BYTE *lpKeyState)
61 {
62     dprintf_key(stddeb, "GetKeyboardState()\n");
63     if (lpKeyState != NULL) {
64         QueueKeyStateTable[VK_LBUTTON] = MouseButtonsStates[0] >> 8;
65         QueueKeyStateTable[VK_MBUTTON] = MouseButtonsStates[1] >> 8;
66         QueueKeyStateTable[VK_RBUTTON] = MouseButtonsStates[2] >> 8;
67         memcpy(lpKeyState, QueueKeyStateTable, 256);
68     }
69 }
70
71 /**********************************************************************
72  *      SetKeyboardState            [USER.223]
73  */
74 void SetKeyboardState(BYTE *lpKeyState)
75 {
76     dprintf_key(stddeb, "SetKeyboardState()\n");
77     if (lpKeyState != NULL) {
78         memcpy(QueueKeyStateTable, lpKeyState, 256);
79         MouseButtonsStates[0] = QueueKeyStateTable[VK_LBUTTON]? 0x8000: 0;
80         MouseButtonsStates[1] = QueueKeyStateTable[VK_MBUTTON]? 0x8000: 0;
81         MouseButtonsStates[2] = QueueKeyStateTable[VK_RBUTTON]? 0x8000: 0;
82     }
83 }
84
85 /**********************************************************************
86  *            GetAsyncKeyState        (USER.249)
87  *
88  *      Determine if a key is or was pressed.  retval has high-order 
89  * bit set to 1 if currently pressed, low-order bit set to 1 if key has
90  * been pressed.
91  *
92  *      This uses the variable AsyncMouseButtonsStates and
93  * AsyncKeyStateTable (set in event.c) which have the mouse button
94  * number or key number (whichever is applicable) set to true if the
95  * mouse or key had been depressed since the last call to 
96  * GetAsyncKeyState.
97  */
98 int GetAsyncKeyState(int nKey)
99 {
100     short retval;       
101
102     switch (nKey) {
103      case VK_LBUTTON:
104         retval = AsyncMouseButtonsStates[0] | 
105         MouseButtonsStates[0]? 0x0001: 0;
106         break;
107      case VK_MBUTTON:
108         retval = AsyncMouseButtonsStates[1] |
109         MouseButtonsStates[1]? 0x0001: 0;
110         break;
111      case VK_RBUTTON:
112         retval = AsyncMouseButtonsStates[2] |
113         MouseButtonsStates[2]? 0x0001: 0;
114         break;
115      default:
116         retval = AsyncKeyStateTable[nKey] | 
117         (InputKeyStateTable[nKey] ? 0x8000 : 0);
118         break;
119     }
120
121     memset( AsyncMouseButtonsStates, 0, 3 );  /* all states to false */
122     memset( AsyncKeyStateTable, 0, 256 );
123
124     dprintf_key(stddeb, "GetAsyncKeyState(%x) -> %x\n", nKey, retval);
125     return retval;
126 }
127
128
129 /**********************************************************************
130  *                      TranslateAccelerator    [USER.178]
131  *
132  * FIXME: should send some WM_INITMENU or/and WM_INITMENUPOPUP  -messages
133  */
134 INT16 TranslateAccelerator(HWND hWnd, HACCEL16 hAccel, LPMSG16 msg)
135 {
136     ACCELHEADER *lpAccelTbl;
137     int         i;
138     BOOL sendmsg;
139     
140     if (hAccel == 0 || msg == NULL) return 0;
141     if (msg->message != WM_KEYDOWN &&
142         msg->message != WM_KEYUP &&
143         msg->message != WM_SYSKEYDOWN &&
144         msg->message != WM_SYSKEYUP &&
145         msg->message != WM_CHAR) return 0;
146
147     dprintf_accel(stddeb, "TranslateAccelerators hAccel=%04x, hWnd=%04x,\
148 msg->hwnd=%04x, msg->message=%04x\n", hAccel,hWnd,msg->hwnd,msg->message);
149
150     lpAccelTbl = (LPACCELHEADER)GlobalLock16(hAccel);
151     for (sendmsg= i = 0; i < lpAccelTbl->wCount; i++) 
152     {
153      if(msg->wParam == lpAccelTbl->tbl[i].wEvent) 
154      {
155       if (msg->message == WM_CHAR) 
156       {
157         if ( !(lpAccelTbl->tbl[i].type & ALT_ACCEL) && 
158              !(lpAccelTbl->tbl[i].type & VIRTKEY_ACCEL) )
159         {
160           dprintf_accel(stddeb,"found accel for WM_CHAR: ('%c')",msg->wParam&0xff);
161           sendmsg=TRUE;
162         }  
163       }
164       else
165       {
166        if(lpAccelTbl->tbl[i].type & VIRTKEY_ACCEL) 
167        {
168         INT mask = 0;
169         dprintf_accel(stddeb,"found accel for virt_key %04x (scan %04x)",
170                                msg->wParam,0xff & HIWORD(msg->lParam));                
171         if(GetKeyState(VK_SHIFT) & 0x8000) mask |= SHIFT_ACCEL;
172         if(GetKeyState(VK_CONTROL) & 0x8000) mask |= CONTROL_ACCEL;
173         if(GetKeyState(VK_MENU) & 0x8000) mask |= ALT_ACCEL;
174         if(mask == (lpAccelTbl->tbl[i].type &
175                             (SHIFT_ACCEL | CONTROL_ACCEL | ALT_ACCEL)))
176           sendmsg=TRUE;                     
177         else
178           dprintf_accel(stddeb,", but incorrect SHIFT/CTRL/ALT-state\n");
179        }
180        else
181        {
182          if (!(msg->lParam & 0x01000000))  /* no special_key */
183          {
184            if ((lpAccelTbl->tbl[i].type & ALT_ACCEL) && (msg->lParam & 0x20000000))
185            {                                                   /* ^^ ALT pressed */
186             dprintf_accel(stddeb,"found accel for Alt-%c", msg->wParam&0xff);
187             sendmsg=TRUE;           
188            } 
189          } 
190        }
191       } 
192
193       if (sendmsg)      /* found an accelerator, but send a message... ? */
194       {
195         INT16  iSysStat,iStat,mesg=0;
196         HMENU16 hSysMenu,hMenu;
197         
198         if (msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP)
199           mesg=1;
200         else 
201          if (GetCapture16())
202            mesg=2;
203          else
204           if (!IsWindowEnabled(hWnd))
205             mesg=3;
206           else
207           {
208             hMenu=GetMenu32(hWnd);
209             hSysMenu=GetSystemMenu32(hWnd,FALSE);
210             if (hSysMenu)
211               iSysStat=GetMenuState32(hSysMenu,lpAccelTbl->tbl[i].wIDval,MF_BYCOMMAND);
212             else
213               iSysStat=-1;
214             if (hMenu)
215               iStat=GetMenuState32(hMenu,lpAccelTbl->tbl[i].wIDval,MF_BYCOMMAND);
216             else
217               iStat=-1;
218             if (iSysStat!=-1)
219             {
220               if (iSysStat & (MF_DISABLED|MF_GRAYED))
221                 mesg=4;
222               else
223                 mesg=WM_SYSCOMMAND;
224             }
225             else
226             {
227               if (iStat!=-1)
228               {
229                 if (IsIconic(hWnd))
230                   mesg=5;
231                 else
232                 {
233                  if (iStat & (MF_DISABLED|MF_GRAYED))
234                    mesg=6;
235                  else
236                    mesg=WM_COMMAND;  
237                 }   
238               }
239               else
240                mesg=WM_COMMAND;  
241             }
242           }
243           if ( mesg==WM_COMMAND || mesg==WM_SYSCOMMAND )
244           {
245               dprintf_accel(stddeb,", sending %s, wParam=%0x\n",
246                   mesg==WM_COMMAND ? "WM_COMMAND" : "WM_SYSCOMMAND",
247                   lpAccelTbl->tbl[i].wIDval);
248               SendMessage16(hWnd, mesg, lpAccelTbl->tbl[i].wIDval,0x00010000L);
249           }
250           else
251           {
252            /*  some reasons for NOT sending the WM_{SYS}COMMAND message: 
253             *   #0: unknown (please report!)
254             *   #1: for WM_KEYUP,WM_SYSKEYUP
255             *   #2: mouse is captured
256             *   #3: window is disabled 
257             *   #4: it's a disabled system menu option
258             *   #5: it's a menu option, but window is iconic
259             *   #6: it's a menu option, but disabled
260             */
261             dprintf_accel(stddeb,", but won't send WM_{SYS}COMMAND, reason is #%d\n",mesg);
262           }          
263           GlobalUnlock16(hAccel);
264           return 1;         
265       }
266      }
267     }
268     GlobalUnlock16(hAccel);
269     return 0;
270 }