Some applications call GlobalMemoryStatus() very often. Cache the
[wine/dcerpc.git] / windows / keyboard.c
blob2c9ce44c48cb567b84f4cb3dd87da19fb2dd6177
1 /*
2 * KEYBOARD driver
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
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
16 #include "windef.h"
17 #include "wingdi.h"
18 #include "winuser.h"
19 #include "wine/keyboard16.h"
20 #include "win.h"
21 #include "heap.h"
22 #include "keyboard.h"
23 #include "message.h"
24 #include "callback.h"
25 #include "builtin16.h"
26 #include "debugtools.h"
27 #include "struct32.h"
28 #include "winerror.h"
30 DEFAULT_DEBUG_CHANNEL(keyboard)
31 DECLARE_DEBUG_CHANNEL(event)
33 /**********************************************************************/
35 KEYBOARD_DRIVER *KEYBOARD_Driver = NULL;
37 static LPKEYBD_EVENT_PROC DefKeybEventProc = NULL;
38 LPBYTE pKeyStateTable = NULL;
40 /***********************************************************************
41 * KEYBOARD_Inquire (KEYBOARD.1)
43 WORD WINAPI KEYBOARD_Inquire(LPKBINFO kbInfo)
45 kbInfo->Begin_First_Range = 0;
46 kbInfo->End_First_Range = 0;
47 kbInfo->Begin_Second_Range = 0;
48 kbInfo->End_Second_Range = 0;
49 kbInfo->StateSize = 16;
51 return sizeof(KBINFO);
54 /***********************************************************************
55 * KEYBOARD_Enable (KEYBOARD.2)
57 VOID WINAPI KEYBOARD_Enable( LPKEYBD_EVENT_PROC lpKeybEventProc,
58 LPBYTE lpKeyState )
60 static BOOL initDone = FALSE;
62 THUNK_Free( (FARPROC)DefKeybEventProc );
64 DefKeybEventProc = lpKeybEventProc;
65 pKeyStateTable = lpKeyState;
67 /* all states to false */
68 memset( lpKeyState, 0, 256 );
70 if (!initDone) KEYBOARD_Driver->pInit();
71 initDone = TRUE;
74 static VOID WINAPI KEYBOARD_CallKeybdEventProc( FARPROC16 proc,
75 BYTE bVk, BYTE bScan,
76 DWORD dwFlags, DWORD dwExtraInfo )
78 CONTEXT86 context;
80 memset( &context, 0, sizeof(context) );
81 CS_reg(&context) = SELECTOROF( proc );
82 EIP_reg(&context) = OFFSETOF( proc );
83 AH_reg(&context) = (dwFlags & KEYEVENTF_KEYUP)? 0x80 : 0;
84 AL_reg(&context) = bVk;
85 BH_reg(&context) = (dwFlags & KEYEVENTF_EXTENDEDKEY)? 1 : 0;
86 BL_reg(&context) = bScan;
87 SI_reg(&context) = LOWORD( dwExtraInfo );
88 DI_reg(&context) = HIWORD( dwExtraInfo );
90 CallTo16RegisterShort( &context, 0 );
93 VOID WINAPI WIN16_KEYBOARD_Enable( FARPROC16 proc, LPBYTE lpKeyState )
95 LPKEYBD_EVENT_PROC thunk =
96 (LPKEYBD_EVENT_PROC)THUNK_Alloc( proc, (RELAY)KEYBOARD_CallKeybdEventProc );
98 KEYBOARD_Enable( thunk, lpKeyState );
101 /***********************************************************************
102 * KEYBOARD_Disable (KEYBOARD.3)
104 VOID WINAPI KEYBOARD_Disable(VOID)
106 THUNK_Free( (FARPROC)DefKeybEventProc );
108 DefKeybEventProc = NULL;
109 pKeyStateTable = NULL;
112 /***********************************************************************
113 * KEYBOARD_SendEvent
115 void KEYBOARD_SendEvent( BYTE bVk, BYTE bScan, DWORD dwFlags,
116 DWORD posX, DWORD posY, DWORD time )
118 WINE_KEYBDEVENT wke;
119 int iWndsLocks;
121 if ( !DefKeybEventProc ) return;
123 TRACE_(event)("(%d,%d,%04lX)\n", bVk, bScan, dwFlags );
125 wke.magic = WINE_KEYBDEVENT_MAGIC;
126 wke.posX = posX;
127 wke.posY = posY;
128 wke.time = time;
130 /* To avoid deadlocks, we have to suspend all locks on windows structures
131 before the program control is passed to the keyboard driver */
132 iWndsLocks = WIN_SuspendWndsLock();
133 DefKeybEventProc( bVk, bScan, dwFlags, (DWORD)&wke );
134 WIN_RestoreWndsLock(iWndsLocks);
137 /**********************************************************************
138 * SetSpeed16 (KEYBOARD.7)
140 WORD WINAPI SetSpeed16(WORD unused)
142 FIXME("(%04x): stub\n", unused);
143 return 0xffff;
146 /**********************************************************************
147 * ScreenSwitchEnable (KEYBOARD.100)
149 VOID WINAPI ScreenSwitchEnable16(WORD unused)
151 FIXME("(%04x): stub\n", unused);
154 /**********************************************************************
155 * OemKeyScan (KEYBOARD.128)(USER32.401)
157 DWORD WINAPI OemKeyScan(WORD wOemChar)
159 TRACE("*OemKeyScan (%d)\n", wOemChar);
161 return wOemChar;
164 /**********************************************************************
165 * VkKeyScan [KEYBOARD.129]
167 /* VkKeyScan translates an ANSI character to a virtual-key and shift code
168 * for the current keyboard.
169 * high-order byte yields :
170 * 0 Unshifted
171 * 1 Shift
172 * 2 Ctrl
173 * 3-5 Shift-key combinations that are not used for characters
174 * 6 Ctrl-Alt
175 * 7 Ctrl-Alt-Shift
176 * I.e. : Shift = 1, Ctrl = 2, Alt = 4.
177 * FIXME : works ok except for dead chars :
178 * VkKeyScan '^'(0x5e, 94) ... got keycode 00 ... returning 00
179 * VkKeyScan '`'(0x60, 96) ... got keycode 00 ... returning 00
182 WORD WINAPI VkKeyScan16(CHAR cChar)
184 return KEYBOARD_Driver->pVkKeyScan(cChar);
187 /******************************************************************************
188 * GetKeyboardType16 (KEYBOARD.130)
190 INT16 WINAPI GetKeyboardType16(INT16 nTypeFlag)
192 TRACE("(%d)\n", nTypeFlag);
193 switch(nTypeFlag)
195 case 0: /* Keyboard type */
196 return 4; /* AT-101 */
197 break;
198 case 1: /* Keyboard Subtype */
199 return 0; /* There are no defined subtypes */
200 break;
201 case 2: /* Number of F-keys */
202 return 12; /* We're doing an 101 for now, so return 12 F-keys */
203 break;
204 default:
205 WARN("Unknown type\n");
206 return 0; /* The book says 0 here, so 0 */
210 /******************************************************************************
211 * MapVirtualKey16 (KEYBOARD.131)
213 * MapVirtualKey translates keycodes from one format to another
215 UINT16 WINAPI MapVirtualKey16(UINT16 wCode, UINT16 wMapType)
217 return KEYBOARD_Driver->pMapVirtualKey(wCode,wMapType);
220 /****************************************************************************
221 * GetKBCodePage16 (KEYBOARD.132)
223 INT16 WINAPI GetKBCodePage16(void)
225 TRACE("(void)\n");
226 return 850;
229 /****************************************************************************
230 * GetKeyNameText16 (KEYBOARD.133)
232 INT16 WINAPI GetKeyNameText16(LONG lParam, LPSTR lpBuffer, INT16 nSize)
234 return KEYBOARD_Driver->pGetKeyNameText(lParam, lpBuffer, nSize);
237 /****************************************************************************
238 * ToAscii (KEYBOARD.4)
240 * The ToAscii function translates the specified virtual-key code and keyboard
241 * state to the corresponding Windows character or characters.
243 * If the specified key is a dead key, the return value is negative. Otherwise,
244 * it is one of the following values:
245 * Value Meaning
246 * 0 The specified virtual key has no translation for the current state of the keyboard.
247 * 1 One Windows character was copied to the buffer.
248 * 2 Two characters were copied to the buffer. This usually happens when a
249 * dead-key character (accent or diacritic) stored in the keyboard layout cannot
250 * be composed with the specified virtual key to form a single character.
252 * FIXME : should do the above (return 2 for non matching deadchar+char combinations)
255 INT16 WINAPI ToAscii16(UINT16 virtKey,UINT16 scanCode, LPBYTE lpKeyState,
256 LPVOID lpChar, UINT16 flags)
258 return KEYBOARD_Driver->pToAscii(
259 virtKey, scanCode, lpKeyState, lpChar, flags
263 /***********************************************************************
264 * KEYBOARD_GetBeepActive
266 BOOL KEYBOARD_GetBeepActive()
268 return KEYBOARD_Driver->pGetBeepActive();
271 /***********************************************************************
272 * KEYBOARD_SetBeepActive
274 void KEYBOARD_SetBeepActive(BOOL bActivate)
276 KEYBOARD_Driver->pSetBeepActive(bActivate);
279 /***********************************************************************
280 * KEYBOARD_Beep
282 void KEYBOARD_Beep(void)
284 KEYBOARD_Driver->pBeep();