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