remove whitespace from end of lines
[reactos.git] / reactos / subsys / win32k / ntuser / keyboard.c
1 /*
2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * PURPOSE: Messages
23 * FILE: subsys/win32k/ntuser/keyboard.c
24 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
25 * REVISION HISTORY:
26 * 06-06-2001 CSH Created
27 */
28
29 /* INCLUDES ******************************************************************/
30
31 #include <w32k.h>
32
33 #define NDEBUG
34 #include <debug.h>
35
36 /* Directory to load key layouts from */
37 #define SYSTEMROOT_DIR L"\\SystemRoot\\System32\\"
38 /* Lock modifiers */
39 #define CAPITAL_BIT 0x80000000
40 #define NUMLOCK_BIT 0x40000000
41 #define MOD_BITS_MASK 0x3fffffff
42 #define MOD_KCTRL 0x02
43 /* Key States */
44 #define KS_DOWN_MASK 0xc0
45 #define KS_DOWN_BIT 0x80
46 #define KS_LOCK_BIT 0x01
47 /* lParam bits */
48 #define LP_EXT_BIT (1<<24)
49 /* From kbdxx.c -- Key changes with numlock */
50 #define KNUMP 0x400
51
52 /* Lock the keyboard state to prevent unusual concurrent access */
53 FAST_MUTEX QueueStateLock;
54
55 BYTE QueueKeyStateTable[256];
56
57 #define IntLockQueueState \
58 ExAcquireFastMutex(&QueueStateLock)
59
60 #define IntUnLockQueueState \
61 ExReleaseFastMutex(&QueueStateLock)
62
63 /* FUNCTIONS *****************************************************************/
64
65 /* Initialization -- Right now, just zero the key state and init the lock */
66 NTSTATUS FASTCALL InitKeyboardImpl(VOID) {
67 ExInitializeFastMutex(&QueueStateLock);
68 RtlZeroMemory(&QueueKeyStateTable,0x100);
69 return STATUS_SUCCESS;
70 }
71
72 /*** Statics used by TranslateMessage ***/
73
74 /*** Shift state code was out of hand, sorry. --- arty */
75
76 static UINT DontDistinguishShifts( UINT ret ) {
77 if( ret == VK_LSHIFT || ret == VK_RSHIFT ) ret = VK_LSHIFT;
78 if( ret == VK_LCONTROL || ret == VK_RCONTROL ) ret = VK_LCONTROL;
79 if( ret == VK_LMENU || ret == VK_RMENU ) ret = VK_LMENU;
80 return ret;
81 }
82
83 static VOID STDCALL SetKeyState(DWORD key, DWORD vk, DWORD ext, BOOL down) {
84 ASSERT(vk <= 0xff);
85
86 /* Special handling for toggles like numpad and caps lock */
87 if (vk == VK_CAPITAL || vk == VK_NUMLOCK) {
88 if (down) QueueKeyStateTable[vk] ^= KS_LOCK_BIT;
89 }
90
91 if (ext && vk == VK_LSHIFT)
92 vk = VK_RSHIFT;
93 if (ext && vk == VK_LCONTROL)
94 vk = VK_RCONTROL;
95 if (ext && vk == VK_LMENU)
96 vk = VK_RMENU;
97
98 if (down)
99 QueueKeyStateTable[vk] |= KS_DOWN_BIT;
100 else
101 QueueKeyStateTable[vk] &= ~KS_DOWN_MASK;
102
103 if (vk == VK_LSHIFT || vk == VK_RSHIFT) {
104 if ((QueueKeyStateTable[VK_LSHIFT] & KS_DOWN_BIT) ||
105 (QueueKeyStateTable[VK_RSHIFT] & KS_DOWN_BIT)) {
106 QueueKeyStateTable[VK_SHIFT] |= KS_DOWN_BIT;
107 } else {
108 QueueKeyStateTable[VK_SHIFT] &= ~KS_DOWN_MASK;
109 }
110 }
111
112 if (vk == VK_LCONTROL || vk == VK_RCONTROL) {
113 if ((QueueKeyStateTable[VK_LCONTROL] & KS_DOWN_BIT) ||
114 (QueueKeyStateTable[VK_RCONTROL] & KS_DOWN_BIT)) {
115 QueueKeyStateTable[VK_CONTROL] |= KS_DOWN_BIT;
116 } else {
117 QueueKeyStateTable[VK_CONTROL] &= ~KS_DOWN_MASK;
118 }
119 }
120
121 if (vk == VK_LMENU || vk == VK_RMENU) {
122 if ((QueueKeyStateTable[VK_LMENU] & KS_DOWN_BIT) ||
123 (QueueKeyStateTable[VK_RMENU] & KS_DOWN_BIT)) {
124 QueueKeyStateTable[VK_MENU] |= KS_DOWN_BIT;
125 } else {
126 QueueKeyStateTable[VK_MENU] &= ~KS_DOWN_MASK;
127 }
128 }
129 }
130
131 VOID DumpKeyState( PBYTE KeyState ) {
132 int i;
133
134 DbgPrint( "KeyState { " );
135 for( i = 0; i < 0x100; i++ ) {
136 if( KeyState[i] ) DbgPrint( "%02x(%02x) ", i, KeyState[i] );
137 }
138 DbgPrint( "};\n" );
139 }
140
141 static BYTE KeysSet( PKBDTABLES pkKT, PBYTE KeyState,
142 int FakeModLeft, int FakeModRight ) {
143 if( !KeyState || !pkKT ) return 0;
144
145 /* Search special codes first */
146 if( FakeModLeft && KeyState[FakeModLeft] )
147 return KeyState[FakeModLeft];
148 else if( FakeModRight && KeyState[FakeModRight] )
149 return KeyState[FakeModRight];
150
151 return 0;
152 }
153
154 /* Search the keyboard layout modifiers table for the shift bit. I don't
155 * want to count on the shift bit not moving, because it can be specified
156 * in the layout */
157
158 static DWORD FASTCALL GetShiftBit( PKBDTABLES pkKT, DWORD Vk ) {
159 int i;
160
161 for( i = 0; pkKT->pCharModifiers->pVkToBit[i].Vk; i++ )
162 if( pkKT->pCharModifiers->pVkToBit[i].Vk == Vk )
163 return pkKT->pCharModifiers->pVkToBit[i].ModBits;
164
165 return 0;
166 }
167
168 static DWORD ModBits( PKBDTABLES pkKT, PBYTE KeyState ) {
169 DWORD ModBits = 0;
170
171 if( !KeyState ) return 0;
172
173 /* DumpKeyState( KeyState ); */
174
175 if (KeysSet( pkKT, KeyState, VK_LSHIFT, VK_RSHIFT ) &
176 KS_DOWN_BIT)
177 ModBits |= GetShiftBit( pkKT, VK_SHIFT );
178
179 if (KeysSet( pkKT, KeyState, VK_LCONTROL, VK_RCONTROL ) &
180 KS_DOWN_BIT )
181 ModBits |= GetShiftBit( pkKT, VK_CONTROL );
182
183 if (KeysSet( pkKT, KeyState, VK_LMENU, VK_RMENU ) &
184 KS_DOWN_BIT )
185 ModBits |= GetShiftBit( pkKT, VK_MENU );
186
187 /* Handle Alt+Gr */
188 if (KeysSet( pkKT, KeyState, VK_RMENU, 0 ) &
189 KS_DOWN_BIT )
190 ModBits |= GetShiftBit( pkKT, VK_CONTROL );
191
192 /* Deal with VK_CAPITAL */
193 if (KeysSet( pkKT, KeyState, VK_CAPITAL, 0 ) & KS_LOCK_BIT)
194 {
195 ModBits |= CAPITAL_BIT;
196 }
197
198 /* Deal with VK_NUMLOCK */
199 if (KeysSet( pkKT, KeyState, VK_NUMLOCK, 0 ) & KS_LOCK_BIT)
200 {
201 ModBits |= NUMLOCK_BIT;
202 }
203
204 DPRINT( "Current Mod Bits: %x\n", ModBits );
205
206 return ModBits;
207 }
208
209 static BOOL TryToTranslateChar(WORD wVirtKey,
210 DWORD ModBits,
211 PBOOL pbDead,
212 PBOOL pbLigature,
213 PWCHAR pwcTranslatedChar,
214 PKBDTABLES keyLayout )
215 {
216 PVK_TO_WCHAR_TABLE vtwTbl;
217 PVK_TO_WCHARS10 vkPtr;
218 size_t size_this_entry;
219 int nMod;
220 DWORD CapsMod = 0, CapsState = 0;
221
222 CapsState = ModBits & ~MOD_BITS_MASK;
223 ModBits = ModBits & MOD_BITS_MASK;
224
225 DPRINT ( "TryToTranslate: %04x %x\n", wVirtKey, ModBits );
226
227 if (ModBits > keyLayout->pCharModifiers->wMaxModBits)
228 {
229 return FALSE;
230 }
231 for (nMod = 0; keyLayout->pVkToWcharTable[nMod].nModifications; nMod++)
232 {
233 vtwTbl = &keyLayout->pVkToWcharTable[nMod];
234 size_this_entry = vtwTbl->cbSize;
235 vkPtr = (PVK_TO_WCHARS10)((BYTE *)vtwTbl->pVkToWchars);
236 while(vkPtr->VirtualKey)
237 {
238 if( wVirtKey == (vkPtr->VirtualKey & 0xff) )
239 {
240 CapsMod = keyLayout->pCharModifiers->ModNumber
241 [ModBits ^
242 ((CapsState & CAPITAL_BIT) ? vkPtr->Attributes : 0)];
243
244 if( CapsMod > keyLayout->pVkToWcharTable[nMod].nModifications ) {
245 DWORD MaxBit = 1;
246 while( MaxBit <
247 keyLayout->pVkToWcharTable[nMod].nModifications )
248 MaxBit <<= 1;
249
250 CapsMod &= MaxBit - 1; /* Guarantee that CapsMod lies
251 in bounds. */
252 }
253
254 *pbDead = vkPtr->wch[CapsMod] == WCH_DEAD;
255 *pbLigature = vkPtr->wch[CapsMod] == WCH_LGTR;
256 *pwcTranslatedChar = vkPtr->wch[CapsMod];
257
258 DPRINT("%d %04x: CapsMod %08x CapsState %08x Char %04x\n",
259 nMod, wVirtKey,
260 CapsMod, CapsState, *pwcTranslatedChar);
261
262 if( *pbDead )
263 {
264 vkPtr = (PVK_TO_WCHARS10)(((BYTE *)vkPtr) + size_this_entry);
265 if( vkPtr->VirtualKey != 0xff )
266 {
267 DPRINT( "Found dead key with no trailer in the table.\n" );
268 DPRINT( "VK: %04x, ADDR: %08x\n", wVirtKey, (int)vkPtr );
269 return FALSE;
270 }
271 *pwcTranslatedChar = vkPtr->wch[CapsMod];
272 }
273 return TRUE;
274 }
275 vkPtr = (PVK_TO_WCHARS10)(((BYTE *)vkPtr) + size_this_entry);
276 }
277 }
278 return FALSE;
279 }
280
281 static
282 int STDCALL
283 ToUnicodeInner(UINT wVirtKey,
284 UINT wScanCode,
285 PBYTE lpKeyState,
286 LPWSTR pwszBuff,
287 int cchBuff,
288 UINT wFlags,
289 PKBDTABLES pkKT)
290 {
291 WCHAR wcTranslatedChar;
292 BOOL bDead;
293 BOOL bLigature;
294
295 if( !pkKT ) return 0;
296
297 if( TryToTranslateChar( wVirtKey,
298 ModBits( pkKT, lpKeyState ),
299 &bDead,
300 &bLigature,
301 &wcTranslatedChar,
302 pkKT ) )
303 {
304 if( bLigature )
305 {
306 DPRINT("Not handling ligature (yet)\n" );
307 return 0;
308 }
309
310 if( cchBuff > 0 ) pwszBuff[0] = wcTranslatedChar;
311
312 return bDead ? -1 : 1;
313 }
314
315 return 0;
316 }
317
318 DWORD
319 STDCALL
320 NtUserGetKeyState(
321 DWORD key)
322 {
323 DWORD ret = 0;
324
325 IntLockQueueState;
326 if( key < 0x100 ) {
327 ret = ((DWORD)(QueueKeyStateTable[key] & KS_DOWN_BIT) << 8 ) |
328 (QueueKeyStateTable[key] & KS_LOCK_BIT);
329 }
330 IntUnLockQueueState;
331 return ret;
332 }
333
334 int STDCALL ToUnicodeEx( UINT wVirtKey,
335 UINT wScanCode,
336 PBYTE lpKeyState,
337 LPWSTR pwszBuff,
338 int cchBuff,
339 UINT wFlags,
340 HKL dwhkl ) {
341 int ToUnicodeResult = 0;
342
343 if (0 == (lpKeyState[wVirtKey] & KS_DOWN_BIT))
344 {
345 ToUnicodeResult = 0;
346 }
347 else
348 {
349 IntLockQueueState;
350 ToUnicodeResult = ToUnicodeInner( wVirtKey,
351 wScanCode,
352 lpKeyState,
353 pwszBuff,
354 cchBuff,
355 wFlags,
356 PsGetWin32Thread() ?
357 PsGetWin32Thread()->KeyboardLayout : 0 );
358 IntUnLockQueueState;
359 }
360
361 return ToUnicodeResult;
362 }
363
364 int STDCALL ToUnicode( UINT wVirtKey,
365 UINT wScanCode,
366 PBYTE lpKeyState,
367 LPWSTR pwszBuff,
368 int cchBuff,
369 UINT wFlags ) {
370 return ToUnicodeEx( wVirtKey,
371 wScanCode,
372 QueueKeyStateTable,
373 pwszBuff,
374 cchBuff,
375 wFlags,
376 0 );
377 }
378
379 /*
380 * Utility to copy and append two unicode strings.
381 *
382 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
383 * IN PUNICODE_STRING Second -> Second string to append
384 * IN BOOL Deallocate -> TRUE: Deallocate First string before
385 * overwriting.
386 *
387 * Returns NTSTATUS.
388 */
389
390 NTSTATUS NTAPI AppendUnicodeString(PUNICODE_STRING ResultFirst,
391 PUNICODE_STRING Second,
392 BOOL Deallocate) {
393 NTSTATUS Status;
394 PWSTR new_string =
395 ExAllocatePoolWithTag(PagedPool,
396 (ResultFirst->Length + Second->Length + sizeof(WCHAR)),
397 TAG_STRING);
398 if( !new_string ) {
399 return STATUS_NO_MEMORY;
400 }
401 memcpy( new_string, ResultFirst->Buffer,
402 ResultFirst->Length );
403 memcpy( new_string + ResultFirst->Length / sizeof(WCHAR),
404 Second->Buffer,
405 Second->Length );
406 if( Deallocate ) RtlFreeUnicodeString(ResultFirst);
407 ResultFirst->Length += Second->Length;
408 ResultFirst->MaximumLength = ResultFirst->Length;
409 new_string[ResultFirst->Length / sizeof(WCHAR)] = 0;
410 Status = RtlCreateUnicodeString(ResultFirst,new_string) ?
411 STATUS_SUCCESS : STATUS_NO_MEMORY;
412 ExFreePool(new_string);
413 return Status;
414 }
415
416 /*
417 * Utility function to read a value from the registry more easily.
418 *
419 * IN PUNICODE_STRING KeyName -> Name of key to open
420 * IN PUNICODE_STRING ValueName -> Name of value to open
421 * OUT PUNICODE_STRING ReturnedValue -> String contained in registry
422 *
423 * Returns NTSTATUS
424 */
425
426 static NTSTATUS NTAPI ReadRegistryValue( PUNICODE_STRING KeyName,
427 PUNICODE_STRING ValueName,
428 PUNICODE_STRING ReturnedValue ) {
429 NTSTATUS Status;
430 HANDLE KeyHandle;
431 OBJECT_ATTRIBUTES KeyAttributes;
432 PKEY_VALUE_PARTIAL_INFORMATION KeyValuePartialInfo;
433 ULONG Length = 0;
434 ULONG ResLength = 0;
435 UNICODE_STRING Temp;
436
437 InitializeObjectAttributes(&KeyAttributes, KeyName, OBJ_CASE_INSENSITIVE,
438 NULL, NULL);
439 Status = ZwOpenKey(&KeyHandle, KEY_ALL_ACCESS, &KeyAttributes);
440 if( !NT_SUCCESS(Status) ) {
441 return Status;
442 }
443
444 Status = ZwQueryValueKey(KeyHandle, ValueName, KeyValuePartialInformation,
445 0,
446 0,
447 &ResLength);
448
449 if( Status != STATUS_BUFFER_TOO_SMALL ) {
450 NtClose(KeyHandle);
451 return Status;
452 }
453
454 ResLength += sizeof( *KeyValuePartialInfo );
455 KeyValuePartialInfo =
456 ExAllocatePoolWithTag(PagedPool, ResLength, TAG_STRING);
457 Length = ResLength;
458
459 if( !KeyValuePartialInfo ) {
460 NtClose(KeyHandle);
461 return STATUS_NO_MEMORY;
462 }
463
464 Status = ZwQueryValueKey(KeyHandle, ValueName, KeyValuePartialInformation,
465 (PVOID)KeyValuePartialInfo,
466 Length,
467 &ResLength);
468
469 if( !NT_SUCCESS(Status) ) {
470 NtClose(KeyHandle);
471 ExFreePool(KeyValuePartialInfo);
472 return Status;
473 }
474
475 Temp.Length = Temp.MaximumLength = KeyValuePartialInfo->DataLength;
476 Temp.Buffer = (PWCHAR)KeyValuePartialInfo->Data;
477
478 /* At this point, KeyValuePartialInfo->Data contains the key data */
479 RtlInitUnicodeString(ReturnedValue,L"");
480 AppendUnicodeString(ReturnedValue,&Temp,FALSE);
481
482 ExFreePool(KeyValuePartialInfo);
483 NtClose(KeyHandle);
484
485 return Status;
486 }
487
488 typedef PVOID (*KbdLayerDescriptor)(VOID);
489 NTSTATUS STDCALL LdrGetProcedureAddress(PVOID module,
490 PANSI_STRING import_name,
491 DWORD flags,
492 PVOID *func_addr);
493
494 void InitKbdLayout( PVOID *pkKeyboardLayout )
495 {
496 WCHAR LocaleBuffer[16];
497 UNICODE_STRING LayoutKeyName;
498 UNICODE_STRING LayoutValueName;
499 UNICODE_STRING DefaultLocale;
500 UNICODE_STRING LayoutFile;
501 UNICODE_STRING FullLayoutPath;
502 LCID LocaleId;
503 PWCHAR KeyboardLayoutWSTR;
504 HMODULE kbModule = 0;
505 NTSTATUS Status;
506 ANSI_STRING kbdProcedureName;
507 KbdLayerDescriptor layerDescGetFn;
508
509 #define XX_STATUS(x) if (!NT_SUCCESS(Status = (x))) continue;
510
511 do {
512 Status = ZwQueryDefaultLocale(FALSE, &LocaleId);
513 if (!NT_SUCCESS(Status))
514 {
515 DPRINT1("Could not get default locale (%08lx).\n", Status);
516 }
517 else
518 {
519 DPRINT("DefaultLocale = %lx\n", LocaleId);
520 swprintf(LocaleBuffer, L"%08lx", LocaleId);
521 DPRINT("DefaultLocale = %S\n", LocaleBuffer);
522 RtlInitUnicodeString(&DefaultLocale, LocaleBuffer);
523
524 RtlInitUnicodeString(&LayoutKeyName,
525 L"\\REGISTRY\\Machine\\SYSTEM\\CurrentControlSet"
526 L"\\Control\\KeyboardLayouts\\");
527
528 AppendUnicodeString(&LayoutKeyName,&DefaultLocale,FALSE);
529
530 RtlInitUnicodeString(&LayoutValueName,L"Layout File");
531
532 Status = ReadRegistryValue(&LayoutKeyName,&LayoutValueName,&LayoutFile);
533 RtlInitUnicodeString(&FullLayoutPath,SYSTEMROOT_DIR);
534
535 if( !NT_SUCCESS(Status) ) {
536 DPRINT1("Got default locale but not layout file. (%08lx)\n",
537 Status);
538 } else {
539 DPRINT("Read registry and got %wZ\n", &LayoutFile);
540
541 RtlFreeUnicodeString(&LayoutKeyName);
542
543 AppendUnicodeString(&FullLayoutPath,&LayoutFile,FALSE);
544
545 DPRINT("Loading Keyboard DLL %wZ\n", &FullLayoutPath);
546
547 RtlFreeUnicodeString(&LayoutFile);
548
549 KeyboardLayoutWSTR =
550 ExAllocatePoolWithTag(PagedPool,
551 FullLayoutPath.Length + sizeof(WCHAR),
552 TAG_STRING);
553
554 if( !KeyboardLayoutWSTR ) {
555 DPRINT1("Couldn't allocate a string for the keyboard layout name.\n");
556 RtlFreeUnicodeString(&FullLayoutPath);
557 return;
558 }
559 memcpy(KeyboardLayoutWSTR,FullLayoutPath.Buffer,
560 FullLayoutPath.Length + sizeof(WCHAR));
561 KeyboardLayoutWSTR[FullLayoutPath.Length / sizeof(WCHAR)] = 0;
562
563 kbModule = EngLoadImage(KeyboardLayoutWSTR);
564 DPRINT( "Load Keyboard Layout: %S\n", KeyboardLayoutWSTR );
565
566 if( !kbModule )
567 DPRINT1( "Load Keyboard Layout: No %wZ\n", &FullLayoutPath );
568
569 RtlFreeUnicodeString(&FullLayoutPath);
570 }
571 }
572
573 if( !kbModule )
574 {
575 DPRINT1("Trying to load US Keyboard Layout\n");
576 kbModule = EngLoadImage(L"\\SystemRoot\\system32\\kbdus.dll");
577
578 if (!kbModule)
579 {
580 DPRINT1("Failed to load any Keyboard Layout\n");
581 return;
582 }
583 }
584
585 RtlInitAnsiString( &kbdProcedureName, "KbdLayerDescriptor" );
586
587 LdrGetProcedureAddress((PVOID)kbModule,
588 &kbdProcedureName,
589 0,
590 (PVOID*)&layerDescGetFn);
591
592 if( layerDescGetFn ) {
593 *pkKeyboardLayout = layerDescGetFn();
594 }
595 } while (FALSE);
596
597 if( !*pkKeyboardLayout ) {
598 DPRINT1("Failed to load the keyboard layout.\n");
599 }
600
601 #undef XX_STATUS
602 }
603
604 PKBDTABLES W32kGetDefaultKeyLayout() {
605 PKBDTABLES pkKeyboardLayout = 0;
606 InitKbdLayout( (PVOID) &pkKeyboardLayout );
607 return pkKeyboardLayout;
608 }
609
610 BOOL FASTCALL
611 IntTranslateKbdMessage(LPMSG lpMsg,
612 HKL dwhkl)
613 {
614 static INT dead_char = 0;
615 LONG UState = 0;
616 WCHAR wp[2] = { 0 };
617 MSG NewMsg = { 0 };
618 PKBDTABLES keyLayout;
619 BOOL Result = FALSE;
620 DWORD ScanCode = 0;
621
622
623 keyLayout = PsGetWin32Thread()->KeyboardLayout;
624 if( !keyLayout )
625 return FALSE;
626
627 if (lpMsg->message != WM_KEYDOWN && lpMsg->message != WM_SYSKEYDOWN)
628 return FALSE;
629
630 ScanCode = (lpMsg->lParam >> 16) & 0xff;
631
632 IntLockQueueState;
633
634 /* All messages have to contain the cursor point. */
635 IntGetCursorLocation(PsGetWin32Thread()->Desktop->WindowStation,
636 &NewMsg.pt);
637
638 UState = ToUnicodeInner(lpMsg->wParam, HIWORD(lpMsg->lParam) & 0xff,
639 QueueKeyStateTable, wp, 2, 0,
640 keyLayout );
641
642 if (UState == 1)
643 {
644 NewMsg.message = (lpMsg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
645 if (dead_char)
646 {
647 ULONG i;
648 WCHAR first, second;
649 DPRINT("PREVIOUS DEAD CHAR: %c\n", dead_char);
650
651 for( i = 0; keyLayout->pDeadKey[i].dwBoth; i++ )
652 {
653 first = keyLayout->pDeadKey[i].dwBoth >> 16;
654 second = keyLayout->pDeadKey[i].dwBoth;
655 if (first == dead_char && second == wp[0])
656 {
657 wp[0] = keyLayout->pDeadKey[i].wchComposed;
658 dead_char = 0;
659 break;
660 }
661 }
662
663 DPRINT("FINAL CHAR: %c\n", wp[0]);
664 }
665
666 if (dead_char)
667 {
668 NewMsg.hwnd = lpMsg->hwnd;
669 NewMsg.wParam = dead_char;
670 NewMsg.lParam = lpMsg->lParam;
671 dead_char = 0;
672 MsqPostMessage(PsGetWin32Thread()->MessageQueue, &NewMsg, FALSE, QS_KEY);
673 }
674
675 NewMsg.hwnd = lpMsg->hwnd;
676 NewMsg.wParam = wp[0];
677 NewMsg.lParam = lpMsg->lParam;
678 DPRINT( "CHAR='%c' %04x %08x\n", wp[0], wp[0], lpMsg->lParam );
679 MsqPostMessage(PsGetWin32Thread()->MessageQueue, &NewMsg, FALSE, QS_KEY);
680 Result = TRUE;
681 }
682 else if (UState == -1)
683 {
684 NewMsg.message =
685 (lpMsg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
686 NewMsg.hwnd = lpMsg->hwnd;
687 NewMsg.wParam = wp[0];
688 NewMsg.lParam = lpMsg->lParam;
689 dead_char = wp[0];
690 MsqPostMessage(PsGetWin32Thread()->MessageQueue, &NewMsg, FALSE, QS_KEY);
691 Result = TRUE;
692 }
693
694 IntUnLockQueueState;
695 return Result;
696 }
697
698 DWORD
699 STDCALL
700 NtUserGetKeyboardState(
701 LPBYTE lpKeyState)
702 {
703 BOOL Result = TRUE;
704
705 IntLockQueueState;
706 if (lpKeyState) {
707 if(!NT_SUCCESS(MmCopyToCaller(lpKeyState, QueueKeyStateTable, 256)))
708 Result = FALSE;
709 }
710 IntUnLockQueueState;
711 return Result;
712 }
713
714 DWORD
715 STDCALL
716 NtUserSetKeyboardState(
717 LPBYTE lpKeyState)
718 {
719 BOOL Result = TRUE;
720
721 IntLockQueueState;
722 if (lpKeyState) {
723 if(! NT_SUCCESS(MmCopyFromCaller(QueueKeyStateTable, lpKeyState, 256)))
724 Result = FALSE;
725 }
726 IntUnLockQueueState;
727
728 return Result;
729 }
730
731 static UINT VkToScan( UINT Code, BOOL ExtCode, PKBDTABLES pkKT ) {
732 int i;
733
734 for( i = 0; i < pkKT->bMaxVSCtoVK; i++ ) {
735 if( pkKT->pusVSCtoVK[i] == Code ) { return i; }
736 }
737
738 return 0;
739 }
740
741 UINT ScanToVk( UINT Code, BOOL ExtKey, PKBDTABLES pkKT ) {
742 if( !pkKT ) {
743 DPRINT("ScanToVk: No layout\n");
744 return 0;
745 }
746
747 if( ExtKey ) {
748 int i;
749
750 for( i = 0; pkKT->pVSCtoVK_E0[i].Vsc; i++ ) {
751 if( pkKT->pVSCtoVK_E0[i].Vsc == Code )
752 return pkKT->pVSCtoVK_E0[i].Vk & 0xff;
753 }
754 for( i = 0; pkKT->pVSCtoVK_E1[i].Vsc; i++ ) {
755 if( pkKT->pVSCtoVK_E1[i].Vsc == Code )
756 return pkKT->pVSCtoVK_E1[i].Vk & 0xff;
757 }
758
759 return 0;
760 } else {
761 if( Code >= pkKT->bMaxVSCtoVK ) { return 0; }
762 return pkKT->pusVSCtoVK[Code] & 0xff;
763 }
764 }
765
766 /*
767 * Map a virtual key code, or virtual scan code, to a scan code, key code,
768 * or unshifted unicode character.
769 *
770 * Code: See Below
771 * Type:
772 * 0 -- Code is a virtual key code that is converted into a virtual scan code
773 * that does not distinguish between left and right shift keys.
774 * 1 -- Code is a virtual scan code that is converted into a virtual key code
775 * that does not distinguish between left and right shift keys.
776 * 2 -- Code is a virtual key code that is converted into an unshifted unicode
777 * character.
778 * 3 -- Code is a virtual scan code that is converted into a virtual key code
779 * that distinguishes left and right shift keys.
780 * KeyLayout: Keyboard layout handle (currently, unused)
781 *
782 * @implemented
783 */
784
785 static UINT IntMapVirtualKeyEx( UINT Code, UINT Type, PKBDTABLES keyLayout ) {
786 UINT ret = 0;
787
788 switch( Type ) {
789 case 0:
790 if( Code == VK_RSHIFT ) Code = VK_LSHIFT;
791 if( Code == VK_RMENU ) Code = VK_LMENU;
792 if( Code == VK_RCONTROL ) Code = VK_LCONTROL;
793 ret = VkToScan( Code, FALSE, keyLayout );
794 break;
795
796 case 1:
797 ret =
798 DontDistinguishShifts
799 (IntMapVirtualKeyEx( Code, 3, keyLayout ) );
800 break;
801
802 case 2: {
803 WCHAR wp[2];
804
805 ret = VkToScan( Code, FALSE, keyLayout );
806 ToUnicodeInner( Code, ret, 0, wp, 2, 0, keyLayout );
807 ret = wp[0];
808 } break;
809
810 case 3:
811
812 ret = ScanToVk( Code, FALSE, keyLayout );
813 break;
814 }
815
816 return ret;
817 }
818
819 UINT
820 STDCALL
821 NtUserMapVirtualKeyEx( UINT Code, UINT Type, DWORD keyboardId, HKL dwhkl ) {
822 PKBDTABLES keyLayout = PsGetWin32Thread() ?
823 PsGetWin32Thread()->KeyboardLayout : 0;
824
825 if( !keyLayout ) return 0;
826
827 return IntMapVirtualKeyEx( Code, Type, keyLayout );
828 }
829
830
831 int
832 STDCALL
833 NtUserToUnicodeEx(
834 UINT wVirtKey,
835 UINT wScanCode,
836 PBYTE lpKeyState,
837 LPWSTR pwszBuff,
838 int cchBuff,
839 UINT wFlags,
840 HKL dwhkl ) {
841 BYTE KeyStateBuf[0x100];
842 PWCHAR OutPwszBuff = 0;
843 int ret = 0;
844
845
846 if( !NT_SUCCESS(MmCopyFromCaller(KeyStateBuf,
847 lpKeyState,
848 sizeof(KeyStateBuf))) ) {
849 DPRINT1( "Couldn't copy key state from caller.\n" );
850 return 0;
851 }
852 OutPwszBuff = ExAllocatePoolWithTag(NonPagedPool,sizeof(WCHAR) * cchBuff, TAG_STRING);
853 if( !OutPwszBuff ) {
854 DPRINT1( "ExAllocatePool(%d) failed\n", sizeof(WCHAR) * cchBuff);
855 return 0;
856 }
857 RtlZeroMemory( OutPwszBuff, sizeof( WCHAR ) * cchBuff );
858
859 ret = ToUnicodeEx( wVirtKey,
860 wScanCode,
861 KeyStateBuf,
862 OutPwszBuff,
863 cchBuff,
864 wFlags,
865 dwhkl );
866
867 MmCopyToCaller(pwszBuff,OutPwszBuff,sizeof(WCHAR)*cchBuff);
868 ExFreePool(OutPwszBuff);
869
870 return ret;
871 }
872
873 static int W32kSimpleToupper( int ch ) {
874 if( ch >= 'a' && ch <= 'z' ) ch = ch - 'a' + 'A';
875 return ch;
876 }
877
878 DWORD
879 STDCALL
880 NtUserGetKeyNameText( LONG lParam, LPWSTR lpString, int nSize ) {
881 int i;
882 DWORD ret = 0;
883 UINT CareVk = 0;
884 UINT VkCode = 0;
885 UINT ScanCode = (lParam >> 16) & 0xff;
886 BOOL ExtKey = lParam & (1<<24) ? TRUE : FALSE;
887 PKBDTABLES keyLayout =
888 PsGetWin32Thread() ?
889 PsGetWin32Thread()->KeyboardLayout : 0;
890
891 if( !keyLayout || nSize < 1 ) return 0;
892
893 if( lParam & (1<<25) ) {
894 CareVk = VkCode = ScanToVk( ScanCode, ExtKey, keyLayout );
895 if( VkCode == VK_LSHIFT || VkCode == VK_RSHIFT )
896 VkCode = VK_LSHIFT;
897 if( VkCode == VK_LCONTROL || VkCode == VK_RCONTROL )
898 VkCode = VK_LCONTROL;
899 if( VkCode == VK_LMENU || VkCode == VK_RMENU )
900 VkCode = VK_LMENU;
901 } else {
902 VkCode = ScanToVk( ScanCode, ExtKey, keyLayout );
903 }
904
905 VSC_LPWSTR *KeyNames = 0;
906
907 if( CareVk != VkCode )
908 ScanCode = VkToScan( VkCode, ExtKey, keyLayout );
909
910 if( ExtKey )
911 KeyNames = keyLayout->pKeyNamesExt;
912 else
913 KeyNames = keyLayout->pKeyNames;
914
915 for( i = 0; KeyNames[i].pwsz; i++ ) {
916 if( KeyNames[i].vsc == ScanCode ) {
917 UINT StrLen = wcslen(KeyNames[i].pwsz);
918 UINT StrMax = StrLen > (nSize - 1) ? (nSize - 1) : StrLen;
919 WCHAR null_wc = 0;
920 if( NT_SUCCESS( MmCopyToCaller( lpString,
921 KeyNames[i].pwsz,
922 StrMax * sizeof(WCHAR) ) ) &&
923 NT_SUCCESS( MmCopyToCaller( lpString + StrMax,
924 &null_wc,
925 sizeof( WCHAR ) ) ) ) {
926 ret = StrMax;
927 break;
928 }
929 }
930 }
931
932 if( ret == 0 ) {
933 WCHAR UCName[2];
934
935 UCName[0] = W32kSimpleToupper(IntMapVirtualKeyEx( VkCode, 2, keyLayout ));
936 UCName[1] = 0;
937 ret = 1;
938
939 if( !NT_SUCCESS(MmCopyToCaller( lpString, UCName, 2 * sizeof(WCHAR) )) )
940 return 0;
941 }
942
943 return ret;
944 }
945
946 /*
947 * Filter this message according to the current key layout, setting wParam
948 * appropriately.
949 */
950
951 VOID FASTCALL W32kKeyProcessMessage(LPMSG Msg, PKBDTABLES KeyboardLayout) {
952 DWORD ScanCode = 0, ModifierBits = 0;
953 DWORD i = 0;
954 DWORD BaseMapping = 0;
955 DWORD RawVk = 0;
956 static WORD NumpadConversion[][2] =
957 { { VK_DELETE, VK_DECIMAL },
958 { VK_INSERT, VK_NUMPAD0 },
959 { VK_END, VK_NUMPAD1 },
960 { VK_DOWN, VK_NUMPAD2 },
961 { VK_NEXT, VK_NUMPAD3 },
962 { VK_LEFT, VK_NUMPAD4 },
963 { VK_CLEAR, VK_NUMPAD5 },
964 { VK_RIGHT, VK_NUMPAD6 },
965 { VK_HOME, VK_NUMPAD7 },
966 { VK_UP, VK_NUMPAD8 },
967 { VK_PRIOR, VK_NUMPAD9 },
968 { 0,0 } };
969
970 if( !KeyboardLayout || !Msg ||
971 (Msg->message != WM_KEYDOWN && Msg->message != WM_SYSKEYDOWN &&
972 Msg->message != WM_KEYUP && Msg->message != WM_SYSKEYUP) )
973 {
974 return;
975 }
976
977 IntLockQueueState;
978
979 /* arty -- handle numpad -- On real windows, the actual key produced
980 * by the messaging layer is different based on the state of numlock. */
981 ModifierBits = ModBits(KeyboardLayout,QueueKeyStateTable);
982
983 /* Get the raw scan code, so we can look up whether the key is a numpad
984 * key
985 *
986 * Shift and the LP_EXT_BIT cancel. */
987 ScanCode = (Msg->lParam >> 16) & 0xff;
988 BaseMapping = Msg->wParam =
989 IntMapVirtualKeyEx( ScanCode, 1, KeyboardLayout );
990 if( ScanCode >= KeyboardLayout->bMaxVSCtoVK )
991 RawVk = 0;
992 else
993 RawVk = KeyboardLayout->pusVSCtoVK[ScanCode];
994
995 if ((ModifierBits & NUMLOCK_BIT) &&
996 !(ModifierBits & GetShiftBit(KeyboardLayout, VK_SHIFT)) &&
997 (RawVk & KNUMP) &&
998 !(Msg->lParam & LP_EXT_BIT))
999 {
1000 /* The key in question is a numpad key. Search for a translation. */
1001 for (i = 0; NumpadConversion[i][0]; i++)
1002 {
1003 if ((BaseMapping & 0xff) == NumpadConversion[i][0]) /* RawVk? */
1004 {
1005 Msg->wParam = NumpadConversion[i][1];
1006 break;
1007 }
1008 }
1009 }
1010
1011 DPRINT("Key: [%04x -> %04x]\n", BaseMapping, Msg->wParam);
1012
1013 /* Now that we have the VK, we can set the keymap appropriately
1014 * This is a better place for this code, as it's guaranteed to be
1015 * run, unlike translate message. */
1016 if (Msg->message == WM_KEYDOWN || Msg->message == WM_SYSKEYDOWN)
1017 {
1018 SetKeyState( ScanCode, Msg->wParam, Msg->lParam & LP_EXT_BIT,
1019 TRUE ); /* Strike key */
1020 }
1021 else if (Msg->message == WM_KEYUP || Msg->message == WM_SYSKEYUP)
1022 {
1023 SetKeyState( ScanCode, Msg->wParam, Msg->lParam & LP_EXT_BIT,
1024 FALSE ); /* Release key */
1025 }
1026
1027 /* We need to unset SYSKEYDOWN if the ALT key is an ALT+Gr */
1028 if( QueueKeyStateTable[VK_RMENU] & KS_DOWN_BIT ) {
1029 if( Msg->message == WM_SYSKEYDOWN ) Msg->message = WM_KEYDOWN;
1030 else Msg->message = WM_KEYUP;
1031 }
1032
1033 IntUnLockQueueState;
1034 }
1035 /* EOF */