Suspend the window locks before DefKeybEventProc and
[wine] / windows / keyboard.c
1 /*
2  * KEYBOARD driver
3  *
4  * Copyright 1993 Bob Amstadt
5  * Copyright 1996 Albrecht Kleine 
6  * Copyright 1997 David Faure
7  * Copyright 1998 Morten Welinder
8  * Copyright 1998 Ulrich Weigand
9  *
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15
16 #include "winuser.h"
17 #include "wine/keyboard16.h"
18 #include "win.h"
19 #include "heap.h"
20 #include "keyboard.h"
21 #include "message.h"
22 #include "debug.h"
23 #include "debugtools.h"
24 #include "struct32.h"
25 #include "winerror.h"
26
27 /**********************************************************************/
28
29 KEYBOARD_DRIVER *KEYBOARD_Driver = NULL;
30
31 static LPKEYBD_EVENT_PROC DefKeybEventProc = NULL;
32 LPBYTE pKeyStateTable = NULL;
33
34 /***********************************************************************
35  *           KEYBOARD_Inquire                   (KEYBOARD.1)
36  */
37 WORD WINAPI KEYBOARD_Inquire(LPKBINFO kbInfo) 
38 {
39   kbInfo->Begin_First_Range = 0;
40   kbInfo->End_First_Range = 0;
41   kbInfo->Begin_Second_Range = 0;
42   kbInfo->End_Second_Range = 0;
43   kbInfo->StateSize = 16; 
44   
45   return sizeof(KBINFO);
46 }
47
48 /***********************************************************************
49  *           KEYBOARD_Enable                    (KEYBOARD.2)
50  */
51 VOID WINAPI KEYBOARD_Enable( LPKEYBD_EVENT_PROC lpKeybEventProc, 
52                              LPBYTE lpKeyState )
53 {
54   static BOOL initDone = FALSE;
55   
56   DefKeybEventProc = lpKeybEventProc;
57   pKeyStateTable = lpKeyState;
58   
59   /* all states to false */
60   memset( lpKeyState, 0, sizeof(lpKeyState) );
61   
62   if (!initDone) KEYBOARD_Driver->pInit();
63   initDone = TRUE;
64 }
65
66 /***********************************************************************
67  *           KEYBOARD_Disable                   (KEYBOARD.3)
68  */
69 VOID WINAPI KEYBOARD_Disable(VOID)
70 {
71   DefKeybEventProc = NULL;
72   pKeyStateTable = NULL;
73 }
74
75 /***********************************************************************
76  *           KEYBOARD_SendEvent
77  */
78 void KEYBOARD_SendEvent( BYTE bVk, BYTE bScan, DWORD dwFlags,
79                          DWORD posX, DWORD posY, DWORD time )
80 {
81   WINE_KEYBDEVENT wke;
82   int iWndsLocks;
83   
84   if ( !DefKeybEventProc ) return;
85   
86   TRACE( event, "(%d,%d,%04lX)\n", bVk, bScan, dwFlags );
87   
88   wke.magic = WINE_KEYBDEVENT_MAGIC;
89   wke.posX  = posX;
90   wke.posY  = posY;
91   wke.time  = time;
92   
93   /* To avoid deadlocks, we have to suspend all locks on windows structures
94      before the program control is passed to the keyboard driver */
95   iWndsLocks = WIN_SuspendWndsLock();
96   DefKeybEventProc( bVk, bScan, dwFlags, (DWORD)&wke );
97   WIN_RestoreWndsLock(iWndsLocks);
98 }
99
100 /**********************************************************************
101  *           ScreenSwitchEnable      (KEYBOARD.100)
102  */
103 VOID WINAPI ScreenSwitchEnable16(WORD unused)
104 {
105   FIXME(keyboard,"(%04x): stub\n",unused);
106 }
107
108 /**********************************************************************
109  *           OemKeyScan      (KEYBOARD.128)(USER32.401)
110  */
111 DWORD WINAPI OemKeyScan(WORD wOemChar)
112 {
113   TRACE(keyboard,"*OemKeyScan (%d)\n",wOemChar);
114
115   return wOemChar;
116 }
117
118 /**********************************************************************
119  *      VkKeyScan                       [KEYBOARD.129]
120  */
121 /* VkKeyScan translates an ANSI character to a virtual-key and shift code
122  * for the current keyboard.
123  * high-order byte yields :
124  *      0       Unshifted
125  *      1       Shift
126  *      2       Ctrl
127  *      3-5     Shift-key combinations that are not used for characters
128  *      6       Ctrl-Alt
129  *      7       Ctrl-Alt-Shift
130  *      I.e. :  Shift = 1, Ctrl = 2, Alt = 4.
131  * FIXME : works ok except for dead chars :
132  * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
133  * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
134  */
135
136 WORD WINAPI VkKeyScan16(CHAR cChar)
137 {
138   return KEYBOARD_Driver->pVkKeyScan(cChar);
139 }
140
141 /******************************************************************************
142  *      GetKeyboardType16      (KEYBOARD.130)
143  */
144 INT16 WINAPI GetKeyboardType16(INT16 nTypeFlag)
145 {
146   TRACE(keyboard,"(%d)\n",nTypeFlag);
147   switch(nTypeFlag)
148     {
149     case 0:      /* Keyboard type */
150       return 4;    /* AT-101 */
151       break;
152     case 1:      /* Keyboard Subtype */
153       return 0;    /* There are no defined subtypes */
154       break;
155     case 2:      /* Number of F-keys */
156       return 12;   /* We're doing an 101 for now, so return 12 F-keys */
157       break;
158     default:     
159       WARN(keyboard, "Unknown type\n");
160       return 0;    /* The book says 0 here, so 0 */
161     }
162 }
163
164 /******************************************************************************
165  *      MapVirtualKey16      (KEYBOARD.131)
166  *
167  * MapVirtualKey translates keycodes from one format to another
168  */
169 UINT16 WINAPI MapVirtualKey16(UINT16 wCode, UINT16 wMapType)
170 {
171   return KEYBOARD_Driver->pMapVirtualKey(wCode,wMapType);
172 }
173
174 /****************************************************************************
175  *      GetKBCodePage16   (KEYBOARD.132)
176  */
177 INT16 WINAPI GetKBCodePage16(void)
178 {
179   TRACE(keyboard,"(void)\n");
180   return 850;
181 }
182
183 /****************************************************************************
184  *      GetKeyNameText16   (KEYBOARD.133)
185  */
186 INT16 WINAPI GetKeyNameText16(LONG lParam, LPSTR lpBuffer, INT16 nSize)
187 {
188   return KEYBOARD_Driver->pGetKeyNameText(lParam, lpBuffer, nSize);
189 }
190
191 /****************************************************************************
192  *      ToAscii   (KEYBOARD.4)
193  *
194  * The ToAscii function translates the specified virtual-key code and keyboard
195  * state to the corresponding Windows character or characters.
196  *
197  * If the specified key is a dead key, the return value is negative. Otherwise,
198  * it is one of the following values:
199  * Value        Meaning
200  * 0    The specified virtual key has no translation for the current state of the keyboard.
201  * 1    One Windows character was copied to the buffer.
202  * 2    Two characters were copied to the buffer. This usually happens when a
203  *      dead-key character (accent or diacritic) stored in the keyboard layout cannot
204  *      be composed with the specified virtual key to form a single character.
205  *
206  * FIXME : should do the above (return 2 for non matching deadchar+char combinations)
207  *
208  */
209 INT16 WINAPI ToAscii16(UINT16 virtKey,UINT16 scanCode, LPBYTE lpKeyState, 
210                        LPVOID lpChar, UINT16 flags) 
211 {
212     return KEYBOARD_Driver->pToAscii(
213         virtKey, scanCode, lpKeyState, lpChar, flags
214     );
215 }
216
217 /***********************************************************************
218  *              KEYBOARD_GetBeepActive
219  */
220 BOOL KEYBOARD_GetBeepActive()
221 {
222   return KEYBOARD_Driver->pGetBeepActive();
223 }
224
225 /***********************************************************************
226  *              KEYBOARD_SetBeepActive
227  */
228 void KEYBOARD_SetBeepActive(BOOL bActivate)
229 {
230   KEYBOARD_Driver->pSetBeepActive(bActivate);
231 }
232
233 /***********************************************************************
234  *              KEYBOARD_Beep
235  */
236 void KEYBOARD_Beep(void)
237 {
238   KEYBOARD_Driver->pBeep();
239 }
240