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