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