f10c1108a67e42f1f40efe354155c57d44f9172a
[reactos.git] / reactos / subsystems / win32 / win32k / ntuser / keyboard.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Keyboard functions
5 * FILE: subsys/win32k/ntuser/keyboard.c
6 * PROGRAMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Rafal Harabien (rafalh@reactos.org)
8 */
9
10 #include <win32k.h>
11 DBG_DEFAULT_CHANNEL(UserKbd);
12
13 BYTE gafAsyncKeyState[256 * 2 / 8]; // 2 bits per key
14 static BYTE gafAsyncKeyStateRecentDown[256 / 8]; // 1 bit per key
15 static PKEYBOARD_INDICATOR_TRANSLATION gpKeyboardIndicatorTrans = NULL;
16 static KEYBOARD_INDICATOR_PARAMETERS gIndicators = {0, 0};
17
18 /* FUNCTIONS *****************************************************************/
19
20 /*
21 * InitKeyboardImpl
22 *
23 * Initialization -- Right now, just zero the key state
24 */
25 INIT_FUNCTION
26 NTSTATUS
27 NTAPI
28 InitKeyboardImpl(VOID)
29 {
30 RtlZeroMemory(&gafAsyncKeyState, sizeof(gafAsyncKeyState));
31 RtlZeroMemory(&gafAsyncKeyStateRecentDown, sizeof(gafAsyncKeyStateRecentDown));
32 return STATUS_SUCCESS;
33 }
34
35 /*
36 * IntKeyboardGetIndicatorTrans
37 *
38 * Asks the keyboard driver to send a small table that shows which
39 * lights should connect with which scancodes
40 */
41 static
42 NTSTATUS APIENTRY
43 IntKeyboardGetIndicatorTrans(HANDLE hKeyboardDevice,
44 PKEYBOARD_INDICATOR_TRANSLATION *ppIndicatorTrans)
45 {
46 NTSTATUS Status;
47 DWORD dwSize = 0;
48 IO_STATUS_BLOCK Block;
49 PKEYBOARD_INDICATOR_TRANSLATION pRet;
50
51 dwSize = sizeof(KEYBOARD_INDICATOR_TRANSLATION);
52
53 pRet = ExAllocatePoolWithTag(PagedPool,
54 dwSize,
55 USERTAG_KBDTABLE);
56
57 while (pRet)
58 {
59 Status = NtDeviceIoControlFile(hKeyboardDevice,
60 NULL,
61 NULL,
62 NULL,
63 &Block,
64 IOCTL_KEYBOARD_QUERY_INDICATOR_TRANSLATION,
65 NULL, 0,
66 pRet, dwSize);
67
68 if (Status != STATUS_BUFFER_TOO_SMALL)
69 break;
70
71 ExFreePoolWithTag(pRet, USERTAG_KBDTABLE);
72
73 dwSize += sizeof(KEYBOARD_INDICATOR_TRANSLATION);
74
75 pRet = ExAllocatePoolWithTag(PagedPool,
76 dwSize,
77 USERTAG_KBDTABLE);
78 }
79
80 if (!pRet)
81 return STATUS_INSUFFICIENT_RESOURCES;
82
83 if (!NT_SUCCESS(Status))
84 {
85 ExFreePoolWithTag(pRet, USERTAG_KBDTABLE);
86 return Status;
87 }
88
89 *ppIndicatorTrans = pRet;
90 return Status;
91 }
92
93 /*
94 * IntKeyboardUpdateLeds
95 *
96 * Sends the keyboard commands to turn on/off the lights
97 */
98 static
99 NTSTATUS APIENTRY
100 IntKeyboardUpdateLeds(HANDLE hKeyboardDevice,
101 WORD wVk,
102 WORD wScanCode,
103 BOOL bEnabled)
104 {
105 NTSTATUS Status;
106 UINT i;
107 USHORT LedFlag = 0;
108 IO_STATUS_BLOCK Block;
109
110 if (!gpKeyboardIndicatorTrans)
111 return STATUS_NOT_SUPPORTED;
112
113 switch (wVk)
114 {
115 case VK_CAPITAL: LedFlag = KEYBOARD_CAPS_LOCK_ON; break;
116 case VK_NUMLOCK: LedFlag = KEYBOARD_NUM_LOCK_ON; break;
117 case VK_SCROLL: LedFlag = KEYBOARD_SCROLL_LOCK_ON; break;
118 default:
119 for (i = 0; i < gpKeyboardIndicatorTrans->NumberOfIndicatorKeys; i++)
120 {
121 if (gpKeyboardIndicatorTrans->IndicatorList[i].MakeCode == wScanCode)
122 {
123 LedFlag = gpKeyboardIndicatorTrans->IndicatorList[i].IndicatorFlags;
124 break;
125 }
126 }
127 }
128
129 if (LedFlag)
130 {
131 if (bEnabled)
132 gIndicators.LedFlags |= LedFlag;
133 else
134 gIndicators.LedFlags = ~LedFlag;
135
136 /* Update the lights on the hardware */
137 Status = NtDeviceIoControlFile(hKeyboardDevice,
138 NULL,
139 NULL,
140 NULL,
141 &Block,
142 IOCTL_KEYBOARD_SET_INDICATORS,
143 &gIndicators, sizeof(gIndicators),
144 NULL, 0);
145
146 return Status;
147 }
148
149 return STATUS_SUCCESS;
150 }
151
152 /*
153 * UserInitKeyboard
154 *
155 * Initializes keyboard indicators translation and their state
156 */
157 VOID NTAPI
158 UserInitKeyboard(HANDLE hKeyboardDevice)
159 {
160 NTSTATUS Status;
161 IO_STATUS_BLOCK Block;
162
163 IntKeyboardGetIndicatorTrans(hKeyboardDevice, &gpKeyboardIndicatorTrans);
164
165 Status = NtDeviceIoControlFile(hKeyboardDevice,
166 NULL,
167 NULL,
168 NULL,
169 &Block,
170 IOCTL_KEYBOARD_QUERY_INDICATORS,
171 NULL, 0,
172 &gIndicators, sizeof(gIndicators));
173
174 if (!NT_SUCCESS(Status))
175 {
176 WARN("NtDeviceIoControlFile() failed, ignored\n");
177 }
178
179 SET_KEY_LOCKED(gafAsyncKeyState, VK_CAPITAL,
180 gIndicators.LedFlags & KEYBOARD_CAPS_LOCK_ON);
181 SET_KEY_LOCKED(gafAsyncKeyState, VK_NUMLOCK,
182 gIndicators.LedFlags & KEYBOARD_NUM_LOCK_ON);
183 SET_KEY_LOCKED(gafAsyncKeyState, VK_SCROLL,
184 gIndicators.LedFlags & KEYBOARD_SCROLL_LOCK_ON);
185 }
186
187 /*
188 * IntSimplifyVk
189 *
190 * Changes virtual keys which distinguish between left and right hand, to keys which don't distinguish
191 */
192 static
193 WORD
194 IntSimplifyVk(WORD wVk)
195 {
196 switch (wVk)
197 {
198 case VK_LSHIFT:
199 case VK_RSHIFT:
200 return VK_SHIFT;
201
202 case VK_LCONTROL:
203 case VK_RCONTROL:
204 return VK_CONTROL;
205
206 case VK_LMENU:
207 case VK_RMENU:
208 return VK_MENU;
209
210 default:
211 return wVk;
212 }
213 }
214
215 /*
216 * IntFixVk
217 *
218 * Changes virtual keys which don't not distinguish between left and right hand to proper keys
219 */
220 static
221 WORD
222 IntFixVk(WORD wVk, BOOL bExt)
223 {
224 switch (wVk)
225 {
226 case VK_SHIFT:
227 return bExt ? VK_RSHIFT : VK_LSHIFT;
228
229 case VK_CONTROL:
230 return bExt ? VK_RCONTROL : VK_LCONTROL;
231
232 case VK_MENU:
233 return bExt ? VK_RMENU : VK_LMENU;
234
235 default:
236 return wVk;
237 }
238 }
239
240 /*
241 * IntTranslateNumpadKey
242 *
243 * Translates numpad keys when numlock is enabled
244 */
245 static
246 WORD
247 IntTranslateNumpadKey(WORD wVk)
248 {
249 switch (wVk)
250 {
251 case VK_INSERT: return VK_NUMPAD0;
252 case VK_END: return VK_NUMPAD1;
253 case VK_DOWN: return VK_NUMPAD2;
254 case VK_NEXT: return VK_NUMPAD3;
255 case VK_LEFT: return VK_NUMPAD4;
256 case VK_CLEAR: return VK_NUMPAD5;
257 case VK_RIGHT: return VK_NUMPAD6;
258 case VK_HOME: return VK_NUMPAD7;
259 case VK_UP: return VK_NUMPAD8;
260 case VK_PRIOR: return VK_NUMPAD9;
261 case VK_DELETE: return VK_DECIMAL;
262 default: return wVk;
263 }
264 }
265
266 /*
267 * IntGetModBits
268 *
269 * Gets layout specific modification bits, for example KBDSHIFT, KBDCTRL, KBDALT
270 */
271 static
272 DWORD
273 IntGetModBits(PKBDTABLES pKbdTbl, PBYTE pKeyState)
274 {
275 DWORD i, dwModBits = 0;
276
277 /* DumpKeyState( KeyState ); */
278
279 for (i = 0; pKbdTbl->pCharModifiers->pVkToBit[i].Vk; i++)
280 if (IS_KEY_DOWN(pKeyState, pKbdTbl->pCharModifiers->pVkToBit[i].Vk))
281 dwModBits |= pKbdTbl->pCharModifiers->pVkToBit[i].ModBits;
282
283 TRACE("Current Mod Bits: %lx\n", dwModBits);
284
285 return dwModBits;
286 }
287
288 /*
289 * IntTranslateChar
290 *
291 * Translates virtual key to character
292 */
293 static
294 BOOL
295 IntTranslateChar(WORD wVirtKey,
296 PBYTE pKeyState,
297 PBOOL pbDead,
298 PBOOL pbLigature,
299 PWCHAR pwcTranslatedChar,
300 PKBDTABLES pKbdTbl)
301 {
302 PVK_TO_WCHAR_TABLE pVkToVchTbl;
303 PVK_TO_WCHARS10 pVkToVch;
304 DWORD i, dwModBits, dwVkModBits, dwModNumber = 0;
305 WCHAR wch;
306 BOOL bAltGr;
307 WORD wCaplokAttr;
308
309 dwModBits = pKeyState ? IntGetModBits(pKbdTbl, pKeyState) : 0;
310 bAltGr = pKeyState && (pKbdTbl->fLocaleFlags & KLLF_ALTGR) && IS_KEY_DOWN(pKeyState, VK_RMENU);
311 wCaplokAttr = bAltGr ? CAPLOKALTGR : CAPLOK;
312
313 TRACE("TryToTranslate: %04x %x\n", wVirtKey, dwModBits);
314
315 /* If ALT without CTRL has ben used, remove ALT flag */
316 if ((dwModBits & (KBDALT|KBDCTRL)) == KBDALT)
317 dwModBits &= ~KBDALT;
318
319 if (dwModBits > pKbdTbl->pCharModifiers->wMaxModBits)
320 {
321 TRACE("dwModBits %x > wMaxModBits %x\n", dwModBits, pKbdTbl->pCharModifiers->wMaxModBits);
322 return FALSE;
323 }
324
325 for (i = 0; pKbdTbl->pVkToWcharTable[i].pVkToWchars; i++)
326 {
327 pVkToVchTbl = &pKbdTbl->pVkToWcharTable[i];
328 pVkToVch = (PVK_TO_WCHARS10)(pVkToVchTbl->pVkToWchars);
329 while (pVkToVch->VirtualKey)
330 {
331 if (wVirtKey == (pVkToVch->VirtualKey & 0xFF))
332 {
333 dwVkModBits = dwModBits;
334
335 /* If CapsLock is enabled for this key and locked, add SHIFT bit */
336 if ((pVkToVch->Attributes & wCaplokAttr) &&
337 pKeyState &&
338 IS_KEY_LOCKED(pKeyState, VK_CAPITAL))
339 {
340 /* Note: we use special value here instead of getting VK_SHIFT mod bit - it's verified */
341 dwVkModBits ^= KBDSHIFT;
342 }
343
344 if (dwVkModBits > pKbdTbl->pCharModifiers->wMaxModBits)
345 break;
346
347 /* Get modification number */
348 dwModNumber = pKbdTbl->pCharModifiers->ModNumber[dwVkModBits];
349 if (dwModNumber >= pVkToVchTbl->nModifications)
350 {
351 TRACE("dwModNumber %u >= nModifications %u\n", dwModNumber, pVkToVchTbl->nModifications);
352 break;
353 }
354
355 /* Read character */
356 wch = pVkToVch->wch[dwModNumber];
357 if (wch == WCH_NONE)
358 break;
359
360 *pbDead = (wch == WCH_DEAD);
361 *pbLigature = (wch == WCH_LGTR);
362 *pwcTranslatedChar = wch;
363
364 TRACE("%d %04x: dwModNumber %08x Char %04x\n",
365 i, wVirtKey, dwModNumber, wch);
366
367 if (*pbDead)
368 {
369 /* After WCH_DEAD, real character is located */
370 pVkToVch = (PVK_TO_WCHARS10)(((BYTE *)pVkToVch) + pVkToVchTbl->cbSize);
371 if (pVkToVch->VirtualKey != 0xFF)
372 {
373 WARN("Found dead key with no trailer in the table.\n");
374 WARN("VK: %04x, ADDR: %p\n", wVirtKey, pVkToVch);
375 break;
376 }
377 *pwcTranslatedChar = pVkToVch->wch[dwModNumber];
378 }
379 return TRUE;
380 }
381 pVkToVch = (PVK_TO_WCHARS10)(((BYTE *)pVkToVch) + pVkToVchTbl->cbSize);
382 }
383 }
384
385 /* If nothing has been found in layout, check if this is ASCII control character.
386 Note: we could add it to layout table, but windows does not have it there */
387 if (wVirtKey >= 'A' && wVirtKey <= 'Z' &&
388 IS_KEY_DOWN(pKeyState, VK_CONTROL) &&
389 !IS_KEY_DOWN(pKeyState, VK_MENU))
390 {
391 *pwcTranslatedChar = (wVirtKey - 'A') + 1; /* ASCII control character */
392 *pbDead = FALSE;
393 *pbLigature = FALSE;
394 return TRUE;
395 }
396
397 return FALSE;
398 }
399
400 /*
401 * IntToUnicodeEx
402 *
403 * Translates virtual key to characters
404 */
405 static
406 int APIENTRY
407 IntToUnicodeEx(UINT wVirtKey,
408 UINT wScanCode,
409 PBYTE pKeyState,
410 LPWSTR pwszBuff,
411 int cchBuff,
412 UINT wFlags,
413 PKBDTABLES pKbdTbl)
414 {
415 WCHAR wchTranslatedChar;
416 BOOL bDead, bLigature;
417 static WCHAR wchDead = 0;
418 int iRet = 0;
419
420 ASSERT(pKbdTbl);
421
422 if (!IntTranslateChar(wVirtKey,
423 pKeyState,
424 &bDead,
425 &bLigature,
426 &wchTranslatedChar,
427 pKbdTbl))
428 {
429 return 0;
430 }
431
432 if (bLigature)
433 {
434 WARN("Not handling ligature (yet)\n" );
435 return 0;
436 }
437
438 /* If we got dead char in previous call check dead keys in keyboard layout */
439 if (wchDead)
440 {
441 UINT i;
442 WCHAR wchFirst, wchSecond;
443 TRACE("Previous dead char: %lc (%x)\n", wchDead, wchDead);
444
445 for (i = 0; pKbdTbl->pDeadKey[i].dwBoth; i++)
446 {
447 wchFirst = pKbdTbl->pDeadKey[i].dwBoth >> 16;
448 wchSecond = pKbdTbl->pDeadKey[i].dwBoth & 0xFFFF;
449 if (wchFirst == wchDead && wchSecond == wchTranslatedChar)
450 {
451 wchTranslatedChar = pKbdTbl->pDeadKey[i].wchComposed;
452 wchDead = 0;
453 bDead = FALSE;
454 break;
455 }
456 }
457
458 TRACE("Final char: %lc (%x)\n", wchTranslatedChar, wchTranslatedChar);
459 }
460
461 /* dead char has not been not found */
462 if (wchDead)
463 {
464 /* Treat both characters normally */
465 if (cchBuff > iRet)
466 pwszBuff[iRet++] = wchDead;
467 bDead = FALSE;
468 }
469
470 /* add character to the buffer */
471 if (cchBuff > iRet)
472 pwszBuff[iRet++] = wchTranslatedChar;
473
474 /* Save dead character */
475 wchDead = bDead ? wchTranslatedChar : 0;
476
477 return bDead ? -iRet : iRet;
478 }
479
480 /*
481 * IntVkToVsc
482 *
483 * Translates virtual key to scan code
484 */
485 static
486 WORD FASTCALL
487 IntVkToVsc(WORD wVk, PKBDTABLES pKbdTbl)
488 {
489 unsigned i;
490
491 ASSERT(pKbdTbl);
492
493 /* Check standard keys first */
494 for (i = 0; i < pKbdTbl->bMaxVSCtoVK; i++)
495 {
496 if ((pKbdTbl->pusVSCtoVK[i] & 0xFF) == wVk)
497 return i;
498 }
499
500 /* Check extended keys now */
501 for (i = 0; pKbdTbl->pVSCtoVK_E0[i].Vsc; i++)
502 {
503 if ((pKbdTbl->pVSCtoVK_E0[i].Vk & 0xFF) == wVk)
504 return 0xE000 | pKbdTbl->pVSCtoVK_E0[i].Vsc;
505 }
506
507 for (i = 0; pKbdTbl->pVSCtoVK_E1[i].Vsc; i++)
508 {
509 if ((pKbdTbl->pVSCtoVK_E1[i].Vk & 0xFF) == wVk)
510 return 0xE100 | pKbdTbl->pVSCtoVK_E1[i].Vsc;
511 }
512
513 /* Virtual key has not been found */
514 return 0;
515 }
516
517 /*
518 * IntVscToVk
519 *
520 * Translates prefixed scancode to virtual key
521 */
522 static
523 WORD FASTCALL
524 IntVscToVk(WORD wScanCode, PKBDTABLES pKbdTbl)
525 {
526 unsigned i;
527 WORD wVk = 0;
528
529 ASSERT(pKbdTbl);
530
531 if ((wScanCode & 0xFF00) == 0xE000)
532 {
533 for (i = 0; pKbdTbl->pVSCtoVK_E0[i].Vsc; i++)
534 {
535 if (pKbdTbl->pVSCtoVK_E0[i].Vsc == (wScanCode & 0xFF))
536 {
537 wVk = pKbdTbl->pVSCtoVK_E0[i].Vk;
538 }
539 }
540 }
541 else if ((wScanCode & 0xFF00) == 0xE100)
542 {
543 for (i = 0; pKbdTbl->pVSCtoVK_E1[i].Vsc; i++)
544 {
545 if (pKbdTbl->pVSCtoVK_E1[i].Vsc == (wScanCode & 0xFF))
546 {
547 wVk = pKbdTbl->pVSCtoVK_E1[i].Vk;
548 }
549 }
550 }
551 else if (wScanCode < pKbdTbl->bMaxVSCtoVK)
552 {
553 wVk = pKbdTbl->pusVSCtoVK[wScanCode];
554 }
555
556 /* 0xFF nad 0x00 are invalid VKs */
557 return wVk != 0xFF ? wVk : 0;
558 }
559
560 /*
561 * IntVkToChar
562 *
563 * Translates virtual key to character, ignoring shift state
564 */
565 static
566 WCHAR FASTCALL
567 IntVkToChar(WORD wVk, PKBDTABLES pKbdTbl)
568 {
569 WCHAR wch;
570 BOOL bDead, bLigature;
571
572 ASSERT(pKbdTbl);
573
574 if (IntTranslateChar(wVk,
575 NULL,
576 &bDead,
577 &bLigature,
578 &wch,
579 pKbdTbl))
580 {
581 return wch;
582 }
583
584 return 0;
585 }
586
587 /*
588 * NtUserGetAsyncKeyState
589 *
590 * Gets key state from global bitmap
591 */
592 SHORT
593 APIENTRY
594 NtUserGetAsyncKeyState(INT Key)
595 {
596 WORD wRet = 0;
597
598 TRACE("Enter NtUserGetAsyncKeyState\n");
599
600 if (Key >= 0x100)
601 {
602 EngSetLastError(ERROR_INVALID_PARAMETER);
603 ERR("Invalid parameter Key\n");
604 return 0;
605 }
606
607 UserEnterExclusive();
608
609 if (IS_KEY_DOWN(gafAsyncKeyState, Key))
610 wRet |= 0x8000; // If down, windows returns 0x8000.
611 if (gafAsyncKeyStateRecentDown[Key / 8] & (1 << (Key % 8)))
612 wRet |= 0x1;
613 gafAsyncKeyStateRecentDown[Key / 8] &= ~(1 << (Key % 8));
614
615 UserLeave();
616
617 TRACE("Leave NtUserGetAsyncKeyState, ret=%i\n", wRet);
618 return wRet;
619 }
620
621 /*
622 * UpdateAsyncKeyState
623 *
624 * Updates gafAsyncKeyState array
625 */
626 static
627 VOID NTAPI
628 UpdateAsyncKeyState(WORD wVk, BOOL bIsDown)
629 {
630 if (bIsDown)
631 {
632 /* If it's first key down event, xor lock bit */
633 if (!IS_KEY_DOWN(gafAsyncKeyState, wVk))
634 SET_KEY_LOCKED(gafAsyncKeyState, wVk, !IS_KEY_LOCKED(gafAsyncKeyState, wVk));
635
636 SET_KEY_DOWN(gafAsyncKeyState, wVk, TRUE);
637 gafAsyncKeyStateRecentDown[wVk / 8] |= (1 << (wVk % 8));
638 }
639 else
640 SET_KEY_DOWN(gafAsyncKeyState, wVk, FALSE);
641 }
642
643 /*
644 * co_CallLowLevelKeyboardHook
645 *
646 * Calls WH_KEYBOARD_LL hook
647 */
648 static LRESULT
649 co_CallLowLevelKeyboardHook(WORD wVk, WORD wScanCode, DWORD dwFlags, BOOL bInjected, DWORD dwTime, DWORD dwExtraInfo)
650 {
651 KBDLLHOOKSTRUCT KbdHookData;
652 UINT uMsg;
653
654 KbdHookData.vkCode = wVk;
655 KbdHookData.scanCode = wScanCode;
656 KbdHookData.flags = 0;
657 if (dwFlags & KEYEVENTF_EXTENDEDKEY)
658 KbdHookData.flags |= LLKHF_EXTENDED;
659 if (IS_KEY_DOWN(gafAsyncKeyState, VK_MENU))
660 KbdHookData.flags |= LLKHF_ALTDOWN;
661 if (dwFlags & KEYEVENTF_KEYUP)
662 KbdHookData.flags |= LLKHF_UP;
663 if (bInjected)
664 KbdHookData.flags |= LLKHF_INJECTED;
665 KbdHookData.time = dwTime;
666 KbdHookData.dwExtraInfo = dwExtraInfo;
667
668 /* Note: it doesnt support WM_SYSKEYUP */
669 if (dwFlags & KEYEVENTF_KEYUP)
670 uMsg = WM_KEYUP;
671 else if (IS_KEY_DOWN(gafAsyncKeyState, VK_MENU) && !IS_KEY_DOWN(gafAsyncKeyState, VK_CONTROL))
672 uMsg = WM_SYSKEYDOWN;
673 else
674 uMsg = WM_KEYDOWN;
675
676 return co_HOOK_CallHooks(WH_KEYBOARD_LL, HC_ACTION, uMsg, (LPARAM)&KbdHookData);
677 }
678
679 /*
680 * SnapWindow
681 *
682 * Saves snapshot of specified window or whole screen in the clipboard
683 */
684 static VOID
685 SnapWindow(HWND hWnd)
686 {
687 HBITMAP hbm = NULL, hbmOld;
688 HDC hdc = NULL, hdcMem;
689 SETCLIPBDATA scd;
690 INT cx, cy;
691 PWND pWnd = NULL;
692
693 TRACE("SnapWindow(%p)\n", hWnd);
694
695 /* If no windows is given, make snapshot of desktop window */
696 if (!hWnd)
697 hWnd = IntGetDesktopWindow();
698
699 pWnd = UserGetWindowObject(hWnd);
700 if (!pWnd)
701 {
702 ERR("Invalid window\n");
703 goto cleanup;
704 }
705
706 hdc = UserGetDCEx(pWnd, NULL, DCX_USESTYLE | DCX_WINDOW);
707 if (!hdc)
708 {
709 ERR("UserGetDCEx failed!\n");
710 goto cleanup;
711 }
712
713 cx = pWnd->rcWindow.right - pWnd->rcWindow.left;
714 cy = pWnd->rcWindow.bottom - pWnd->rcWindow.top;
715
716 hbm = NtGdiCreateCompatibleBitmap(hdc, cx, cy);
717 if (!hbm)
718 {
719 ERR("NtGdiCreateCompatibleBitmap failed!\n");
720 goto cleanup;
721 }
722
723 hdcMem = NtGdiCreateCompatibleDC(hdc);
724 if (!hdcMem)
725 {
726 ERR("NtGdiCreateCompatibleDC failed!\n");
727 goto cleanup;
728 }
729
730 hbmOld = NtGdiSelectBitmap(hdcMem, hbm);
731 NtGdiBitBlt(hdcMem, 0, 0, cx, cy, hdc, 0, 0, SRCCOPY, 0, 0);
732 NtGdiSelectBitmap(hdcMem, hbmOld);
733 IntGdiDeleteDC(hdcMem, FALSE);
734
735 /* Save snapshot in clipboard */
736 if (UserOpenClipboard(NULL))
737 {
738 UserEmptyClipboard();
739 scd.fIncSerialNumber = TRUE;
740 scd.fGlobalHandle = FALSE;
741 if (UserSetClipboardData(CF_BITMAP, hbm, &scd))
742 {
743 /* Bitmap is managed by system now */
744 hbm = NULL;
745 }
746 UserCloseClipboard();
747 }
748
749 cleanup:
750 if (hbm)
751 GreDeleteObject(hbm);
752 if (hdc)
753 UserReleaseDC(hWnd, hdc, FALSE);
754 }
755
756 /*
757 * UserSendKeyboardInput
758 *
759 * Process keyboard input from input devices and SendInput API
760 */
761 BOOL NTAPI
762 ProcessKeyEvent(WORD wVk, WORD wScanCode, DWORD dwFlags, BOOL bInjected, DWORD dwTime, DWORD dwExtraInfo)
763 {
764 WORD wSimpleVk = 0, wFixedVk, wVk2;
765 PUSER_MESSAGE_QUEUE pFocusQueue;
766 BOOL bExt = (dwFlags & KEYEVENTF_EXTENDEDKEY) ? TRUE : FALSE;
767 BOOL bIsDown = (dwFlags & KEYEVENTF_KEYUP) ? FALSE : TRUE;
768 BOOL bPacket = (dwFlags & KEYEVENTF_UNICODE) ? TRUE : FALSE;
769 BOOL bWasSimpleDown = FALSE, bPostMsg = TRUE, bIsSimpleDown;
770 MSG Msg;
771 static BOOL bMenuDownRecently = FALSE;
772
773 /* Get virtual key without shifts (VK_(L|R)* -> VK_*) */
774 wSimpleVk = IntSimplifyVk(wVk);
775 bWasSimpleDown = IS_KEY_DOWN(gafAsyncKeyState, wSimpleVk);
776
777 /* Update key without shifts */
778 wVk2 = IntFixVk(wSimpleVk, !bExt);
779 bIsSimpleDown = bIsDown || IS_KEY_DOWN(gafAsyncKeyState, wVk2);
780 UpdateAsyncKeyState(wSimpleVk, bIsSimpleDown);
781
782 if (bIsDown)
783 {
784 /* Update keyboard LEDs */
785 IntKeyboardUpdateLeds(ghKeyboardDevice,
786 wSimpleVk,
787 wScanCode,
788 IS_KEY_LOCKED(gafAsyncKeyState, wSimpleVk));
789 }
790
791 /* Call WH_KEYBOARD_LL hook */
792 if (co_CallLowLevelKeyboardHook(wVk, wScanCode, dwFlags, bInjected, dwTime, dwExtraInfo))
793 {
794 ERR("Kbd msg %d wParam %d lParam 0x%08x dropped by WH_KEYBOARD_LL hook\n",
795 Msg.message, Msg.wParam, Msg.lParam);
796 bPostMsg = FALSE;
797 }
798
799 /* Check if this is a hotkey */
800 if (co_UserProcessHotKeys(wSimpleVk, bIsDown))
801 bPostMsg = FALSE;
802
803 wFixedVk = IntFixVk(wSimpleVk, bExt); /* LSHIFT + EXT = RSHIFT */
804 if (wSimpleVk == VK_SHIFT) /* shift can't be extended */
805 bExt = FALSE;
806
807 /* If it is F10 or ALT is down and CTRL is up, it's a system key */
808 if (wVk == VK_F10 ||
809 (wSimpleVk == VK_MENU && bMenuDownRecently) ||
810 (IS_KEY_DOWN(gafAsyncKeyState, VK_MENU) &&
811 !IS_KEY_DOWN(gafAsyncKeyState, VK_CONTROL)))
812 {
813 bMenuDownRecently = FALSE; // reset
814 if (bIsDown)
815 {
816 Msg.message = WM_SYSKEYDOWN;
817 if (wSimpleVk == VK_MENU)
818 {
819 // Note: If only LALT is pressed WM_SYSKEYUP is generated instead of WM_KEYUP
820 bMenuDownRecently = TRUE;
821 }
822 }
823 else
824 Msg.message = WM_SYSKEYUP;
825 }
826 else
827 {
828 if (bIsDown)
829 Msg.message = WM_KEYDOWN;
830 else
831 Msg.message = WM_KEYUP;
832 }
833
834 /* Update async state of not simplified vk here.
835 See user32_apitest:GetKeyState */
836 UpdateAsyncKeyState(wFixedVk, bIsDown);
837
838 /* Alt-Tab/Esc Check. Use FocusQueue or RIT Queue */
839 if (bIsSimpleDown && !bWasSimpleDown &&
840 IS_KEY_DOWN(gafAsyncKeyState, VK_MENU) &&
841 !IS_KEY_DOWN(gafAsyncKeyState, VK_CONTROL) &&
842 (wVk == VK_ESCAPE || wVk == VK_TAB))
843 {
844 TRACE("Alt-Tab/Esc Pressed wParam %x\n",wVk);
845 }
846
847 /* If we have a focus queue, post a keyboard message */
848 pFocusQueue = IntGetFocusMessageQueue();
849 if (bIsDown && wVk == VK_SNAPSHOT)
850 {
851 if (pFocusQueue &&
852 IS_KEY_DOWN(gafAsyncKeyState, VK_MENU) &&
853 !IS_KEY_DOWN(gafAsyncKeyState, VK_CONTROL))
854 {
855 SnapWindow(pFocusQueue->FocusWindow);
856 }
857 else
858 SnapWindow(NULL);
859 }
860 else if (pFocusQueue && bPostMsg)
861 {
862 /* Init message */
863 Msg.hwnd = pFocusQueue->FocusWindow;
864 Msg.wParam = wFixedVk & 0xFF; /* Note: it's simplified by msg queue */
865 Msg.lParam = MAKELPARAM(1, wScanCode);
866 Msg.time = dwTime;
867 Msg.pt = gpsi->ptCursor;
868
869 /* If it is VK_PACKET, high word of wParam is used for wchar */
870 if (!bPacket)
871 {
872 if (bExt)
873 Msg.lParam |= KF_EXTENDED << 16;
874 if (IS_KEY_DOWN(gafAsyncKeyState, VK_MENU))
875 Msg.lParam |= KF_ALTDOWN << 16;
876 if (bWasSimpleDown)
877 Msg.lParam |= KF_REPEAT << 16;
878 if (!bIsDown)
879 Msg.lParam |= KF_UP << 16;
880 /* FIXME: set KF_DLGMODE and KF_MENUMODE when needed */
881 if (pFocusQueue->QF_flags & QF_DIALOGACTIVE)
882 Msg.lParam |= KF_DLGMODE << 16;
883 if (pFocusQueue->MenuOwner)//pFocusQueue->MenuState) // MenuState needs a start flag...
884 Msg.lParam |= KF_MENUMODE << 16;
885 }
886
887 /* Post a keyboard message */
888 TRACE("Posting keyboard msg %u wParam 0x%x lParam 0x%x\n", Msg.message, Msg.wParam, Msg.lParam);
889 MsqPostMessage(pFocusQueue, &Msg, TRUE, QS_KEY);
890 }
891
892 return TRUE;
893 }
894
895 BOOL NTAPI
896 UserSendKeyboardInput(KEYBDINPUT *pKbdInput, BOOL bInjected)
897 {
898 WORD wScanCode, wVk;
899 PKL pKl = NULL;
900 PKBDTABLES pKbdTbl;
901 PUSER_MESSAGE_QUEUE pFocusQueue;
902 struct _ETHREAD *pFocusThread;
903 LARGE_INTEGER LargeTickCount;
904 DWORD dwTime;
905 BOOL bExt = (pKbdInput->dwFlags & KEYEVENTF_EXTENDEDKEY) ? TRUE : FALSE;
906
907 /* Find the target thread whose locale is in effect */
908 pFocusQueue = IntGetFocusMessageQueue();
909
910 if (pFocusQueue)
911 {
912 pFocusThread = pFocusQueue->Thread;
913 if (pFocusThread && pFocusThread->Tcb.Win32Thread)
914 pKl = ((PTHREADINFO)pFocusThread->Tcb.Win32Thread)->KeyboardLayout;
915 }
916
917 if (!pKl)
918 pKl = W32kGetDefaultKeyLayout();
919 if (!pKl)
920 {
921 ERR("No keyboard layout!\n");
922 return FALSE;
923 }
924
925 pKbdTbl = pKl->spkf->pKbdTbl;
926
927 /* Note: wScan field is always used */
928 wScanCode = pKbdInput->wScan;
929
930 if (pKbdInput->dwFlags & KEYEVENTF_UNICODE)
931 {
932 /* Generate WM_KEYDOWN msg with wParam == VK_PACKET and
933 high order word of lParam == pKbdInput->wScan */
934 wVk = VK_PACKET;
935 }
936 else
937 {
938 wScanCode &= 0x7F;
939 if (pKbdInput->dwFlags & KEYEVENTF_SCANCODE)
940 {
941 /* Don't ignore invalid scan codes */
942 wVk = IntVscToVk(wScanCode | (bExt ? 0xE000 : 0), pKbdTbl);
943 if (!wVk) /* use 0xFF if vsc is invalid */
944 wVk = 0xFF;
945 }
946 else
947 {
948 wVk = pKbdInput->wVk & 0xFF;
949 }
950 }
951
952 /* If time is given, use it */
953 if (pKbdInput->time)
954 dwTime = pKbdInput->time;
955 else
956 {
957 KeQueryTickCount(&LargeTickCount);
958 dwTime = MsqCalculateMessageTime(&LargeTickCount);
959 }
960
961 if (wVk == VK_RMENU && (pKbdTbl->fLocaleFlags & KLLF_ALTGR))
962 {
963 /* For AltGr keyboards RALT generates CTRL events */
964 ProcessKeyEvent(VK_LCONTROL, 0, pKbdInput->dwFlags & KEYEVENTF_KEYUP, bInjected, dwTime, 0);
965 }
966
967 /* Finally process this key */
968 return ProcessKeyEvent(wVk, wScanCode, pKbdInput->dwFlags, bInjected, dwTime, pKbdInput->dwExtraInfo);
969 }
970
971 /*
972 * UserProcessKeyboardInput
973 *
974 * Process raw keyboard input data
975 */
976 VOID NTAPI
977 UserProcessKeyboardInput(
978 PKEYBOARD_INPUT_DATA pKbdInputData)
979 {
980 WORD wScanCode, wVk;
981 PKL pKl = NULL;
982 PKBDTABLES pKbdTbl;
983 PUSER_MESSAGE_QUEUE pFocusQueue;
984 struct _ETHREAD *pFocusThread;
985
986 /* Calculate scan code with prefix */
987 wScanCode = pKbdInputData->MakeCode & 0x7F;
988 if (pKbdInputData->Flags & KEY_E0)
989 wScanCode |= 0xE000;
990 if (pKbdInputData->Flags & KEY_E1)
991 wScanCode |= 0xE100;
992
993 /* Find the target thread whose locale is in effect */
994 pFocusQueue = IntGetFocusMessageQueue();
995
996 if (pFocusQueue)
997 {
998 pFocusThread = pFocusQueue->Thread;
999 if (pFocusThread && pFocusThread->Tcb.Win32Thread)
1000 pKl = ((PTHREADINFO)pFocusThread->Tcb.Win32Thread)->KeyboardLayout;
1001 }
1002
1003 if (!pKl)
1004 pKl = W32kGetDefaultKeyLayout();
1005 if (!pKl)
1006 return;
1007
1008 pKbdTbl = pKl->spkf->pKbdTbl;
1009
1010 /* Convert scan code to virtual key.
1011 Note: we could call UserSendKeyboardInput using scan code,
1012 but it wouldn't interpret E1 key(s) properly */
1013 wVk = IntVscToVk(wScanCode, pKbdTbl);
1014 TRACE("UserProcessKeyboardInput: %x (break: %u) -> %x\n",
1015 wScanCode, (pKbdInputData->Flags & KEY_BREAK) ? 1 : 0, wVk);
1016
1017 if (wVk)
1018 {
1019 KEYBDINPUT KbdInput;
1020
1021 /* Support numlock */
1022 if ((wVk & KBDNUMPAD) && IS_KEY_LOCKED(gafAsyncKeyState, VK_NUMLOCK))
1023 {
1024 wVk = IntTranslateNumpadKey(wVk & 0xFF);
1025 }
1026
1027 /* Send keyboard input */
1028 KbdInput.wVk = wVk & 0xFF;
1029 KbdInput.wScan = wScanCode & 0x7F;
1030 KbdInput.dwFlags = 0;
1031 if (pKbdInputData->Flags & KEY_BREAK)
1032 KbdInput.dwFlags |= KEYEVENTF_KEYUP;
1033 if (wVk & KBDEXT)
1034 KbdInput.dwFlags |= KEYEVENTF_EXTENDEDKEY;
1035 KbdInput.time = 0;
1036 KbdInput.dwExtraInfo = pKbdInputData->ExtraInformation;
1037 UserSendKeyboardInput(&KbdInput, FALSE);
1038
1039 /* E1 keys don't have break code */
1040 if (pKbdInputData->Flags & KEY_E1)
1041 {
1042 /* Send key up event */
1043 KbdInput.dwFlags |= KEYEVENTF_KEYUP;
1044 UserSendKeyboardInput(&KbdInput, FALSE);
1045 }
1046 }
1047 }
1048
1049 /*
1050 * IntTranslateKbdMessage
1051 *
1052 * Addes WM_(SYS)CHAR messages to message queue if message
1053 * describes key which produce character.
1054 */
1055 BOOL FASTCALL
1056 IntTranslateKbdMessage(LPMSG lpMsg,
1057 UINT flags)
1058 {
1059 PTHREADINFO pti;
1060 INT cch = 0, i;
1061 WCHAR wch[3] = { 0 };
1062 MSG NewMsg = { 0 };
1063 PKBDTABLES pKbdTbl;
1064 PWND pWnd;
1065 LARGE_INTEGER LargeTickCount;
1066 BOOL bResult = FALSE;
1067
1068 switch(lpMsg->message)
1069 {
1070 case WM_KEYDOWN:
1071 case WM_KEYUP:
1072 case WM_SYSKEYDOWN:
1073 case WM_SYSKEYUP:
1074 break;
1075 default:
1076 return FALSE;
1077 }
1078
1079 pWnd = UserGetWindowObject(lpMsg->hwnd);
1080 if (!pWnd) // Must have a window!
1081 {
1082 ERR("No Window for Translate.\n");
1083 return FALSE;
1084 }
1085
1086 pti = pWnd->head.pti;
1087
1088 if (!pti->KeyboardLayout)
1089 {
1090 pti->KeyboardLayout = W32kGetDefaultKeyLayout();
1091 pKbdTbl = pti->KeyboardLayout->spkf->pKbdTbl;
1092 }
1093 else
1094 pKbdTbl = pti->KeyboardLayout->spkf->pKbdTbl;
1095 if (!pKbdTbl)
1096 return FALSE;
1097
1098 if (lpMsg->message != WM_KEYDOWN && lpMsg->message != WM_SYSKEYDOWN)
1099 return FALSE;
1100
1101 /* Init pt, hwnd and time msg fields */
1102 NewMsg.pt = gpsi->ptCursor;
1103 NewMsg.hwnd = lpMsg->hwnd;
1104 KeQueryTickCount(&LargeTickCount);
1105 NewMsg.time = MsqCalculateMessageTime(&LargeTickCount);
1106
1107 TRACE("Enter IntTranslateKbdMessage msg %s, vk %x\n",
1108 lpMsg->message == WM_SYSKEYDOWN ? "WM_SYSKEYDOWN" : "WM_KEYDOWN", lpMsg->wParam);
1109
1110 if (lpMsg->wParam == VK_PACKET)
1111 {
1112 NewMsg.message = (lpMsg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
1113 NewMsg.wParam = HIWORD(lpMsg->lParam);
1114 NewMsg.lParam = LOWORD(lpMsg->lParam);
1115 MsqPostMessage(pti->MessageQueue, &NewMsg, FALSE, QS_KEY);
1116 return TRUE;
1117 }
1118
1119 cch = IntToUnicodeEx(lpMsg->wParam,
1120 HIWORD(lpMsg->lParam) & 0xFF,
1121 pti->MessageQueue->afKeyState,
1122 wch,
1123 sizeof(wch) / sizeof(wch[0]),
1124 0,
1125 pKbdTbl);
1126
1127 if (cch)
1128 {
1129 if (cch > 0) /* Normal characters */
1130 NewMsg.message = (lpMsg->message == WM_KEYDOWN) ? WM_CHAR : WM_SYSCHAR;
1131 else /* Dead character */
1132 {
1133 cch = -cch;
1134 NewMsg.message =
1135 (lpMsg->message == WM_KEYDOWN) ? WM_DEADCHAR : WM_SYSDEADCHAR;
1136 }
1137 NewMsg.lParam = lpMsg->lParam;
1138
1139 /* Send all characters */
1140 for (i = 0; i < cch; ++i)
1141 {
1142 TRACE("Msg: %x '%lc' (%04x) %08x\n", NewMsg.message, wch[i], wch[i], NewMsg.lParam);
1143 NewMsg.wParam = wch[i];
1144 MsqPostMessage(pti->MessageQueue, &NewMsg, FALSE, QS_KEY);
1145 }
1146 bResult = TRUE;
1147 }
1148
1149 TRACE("Leave IntTranslateKbdMessage ret %u, cch %d, msg %x, wch %x\n",
1150 bResult, cch, NewMsg.message, NewMsg.wParam);
1151 return bResult;
1152 }
1153
1154 /*
1155 * Map a virtual key code, or virtual scan code, to a scan code, key code,
1156 * or unshifted unicode character.
1157 *
1158 * Code: See Below
1159 * Type:
1160 * 0 -- Code is a virtual key code that is converted into a virtual scan code
1161 * that does not distinguish between left and right shift keys.
1162 * 1 -- Code is a virtual scan code that is converted into a virtual key code
1163 * that does not distinguish between left and right shift keys.
1164 * 2 -- Code is a virtual key code that is converted into an unshifted unicode
1165 * character.
1166 * 3 -- Code is a virtual scan code that is converted into a virtual key code
1167 * that distinguishes left and right shift keys.
1168 * KeyLayout: Keyboard layout handle
1169 *
1170 * @implemented
1171 */
1172 static UINT
1173 IntMapVirtualKeyEx(UINT uCode, UINT Type, PKBDTABLES pKbdTbl)
1174 {
1175 UINT uRet = 0;
1176
1177 switch (Type)
1178 {
1179 case MAPVK_VK_TO_VSC:
1180 uCode = IntFixVk(uCode, FALSE);
1181 uRet = IntVkToVsc(uCode, pKbdTbl);
1182 if (uRet > 0xFF) // fail for scancodes with prefix (e0, e1)
1183 uRet = 0;
1184 break;
1185
1186 case MAPVK_VSC_TO_VK:
1187 uRet = IntVscToVk(uCode, pKbdTbl) & 0xFF;
1188 uRet = IntSimplifyVk(uRet);
1189 break;
1190
1191 case MAPVK_VK_TO_CHAR:
1192 uRet = (UINT)IntVkToChar(uCode, pKbdTbl);
1193 break;
1194
1195 case MAPVK_VSC_TO_VK_EX:
1196 uRet = IntVscToVk(uCode, pKbdTbl) & 0xFF;
1197 break;
1198
1199 case MAPVK_VK_TO_VSC_EX:
1200 uRet = IntVkToVsc(uCode, pKbdTbl);
1201 break;
1202
1203 default:
1204 EngSetLastError(ERROR_INVALID_PARAMETER);
1205 ERR("Wrong type value: %u\n", Type);
1206 }
1207
1208 return uRet;
1209 }
1210
1211 /*
1212 * NtUserMapVirtualKeyEx
1213 *
1214 * Map a virtual key code, or virtual scan code, to a scan code, key code,
1215 * or unshifted unicode character. See IntMapVirtualKeyEx.
1216 */
1217 UINT
1218 APIENTRY
1219 NtUserMapVirtualKeyEx(UINT uCode, UINT uType, DWORD keyboardId, HKL dwhkl)
1220 {
1221 PKBDTABLES pKbdTbl = NULL;
1222 UINT ret = 0;
1223
1224 TRACE("Enter NtUserMapVirtualKeyEx\n");
1225 UserEnterShared();
1226
1227 if (!dwhkl)
1228 {
1229 PTHREADINFO pti;
1230
1231 pti = PsGetCurrentThreadWin32Thread();
1232 if (pti && pti->KeyboardLayout)
1233 pKbdTbl = pti->KeyboardLayout->spkf->pKbdTbl;
1234 }
1235 else
1236 {
1237 PKL pKl;
1238
1239 pKl = UserHklToKbl(dwhkl);
1240 if (pKl)
1241 pKbdTbl = pKl->spkf->pKbdTbl;
1242 }
1243
1244 if (pKbdTbl)
1245 ret = IntMapVirtualKeyEx(uCode, uType, pKbdTbl);
1246
1247 UserLeave();
1248 TRACE("Leave NtUserMapVirtualKeyEx, ret=%i\n", ret);
1249 return ret;
1250 }
1251
1252 /*
1253 * NtUserToUnicodeEx
1254 *
1255 * Translates virtual key to characters
1256 */
1257 int
1258 APIENTRY
1259 NtUserToUnicodeEx(
1260 UINT wVirtKey,
1261 UINT wScanCode,
1262 PBYTE pKeyStateUnsafe,
1263 LPWSTR pwszBuffUnsafe,
1264 INT cchBuff,
1265 UINT wFlags,
1266 HKL dwhkl)
1267 {
1268 PTHREADINFO pti;
1269 BYTE afKeyState[256 * 2 / 8] = {0};
1270 PWCHAR pwszBuff = NULL;
1271 INT i, iRet = 0;
1272 PKL pKl = NULL;
1273
1274 TRACE("Enter NtUserSetKeyboardState\n");
1275
1276 /* Return 0 if SC_KEY_UP bit is set */
1277 if (wScanCode & SC_KEY_UP || wVirtKey >= 0x100)
1278 {
1279 ERR("Invalid parameter\n");
1280 return 0;
1281 }
1282
1283 _SEH2_TRY
1284 {
1285 /* Probe and copy key state to smaller bitmap */
1286 ProbeForRead(pKeyStateUnsafe, 256 * sizeof(BYTE), 1);
1287 for (i = 0; i < 256; ++i)
1288 {
1289 if (pKeyStateUnsafe[i] & KS_DOWN_BIT)
1290 SET_KEY_DOWN(afKeyState, i, TRUE);
1291 if (pKeyStateUnsafe[i] & KS_LOCK_BIT)
1292 SET_KEY_LOCKED(afKeyState, i, TRUE);
1293 }
1294 }
1295 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1296 {
1297 ERR("Cannot copy key state\n");
1298 SetLastNtError(_SEH2_GetExceptionCode());
1299 _SEH2_YIELD(return 0);
1300 }
1301 _SEH2_END;
1302
1303 pwszBuff = ExAllocatePoolWithTag(NonPagedPool, sizeof(WCHAR) * cchBuff, TAG_STRING);
1304 if (!pwszBuff)
1305 {
1306 ERR("ExAllocatePoolWithTag(%d) failed\n", sizeof(WCHAR) * cchBuff);
1307 return 0;
1308 }
1309 RtlZeroMemory(pwszBuff, sizeof(WCHAR) * cchBuff);
1310
1311 UserEnterExclusive(); // Note: we modify wchDead static variable
1312
1313 if (dwhkl)
1314 pKl = UserHklToKbl(dwhkl);
1315
1316 if (!pKl)
1317 {
1318 pti = PsGetCurrentThreadWin32Thread();
1319 pKl = pti->KeyboardLayout;
1320 }
1321
1322 iRet = IntToUnicodeEx(wVirtKey,
1323 wScanCode,
1324 afKeyState,
1325 pwszBuff,
1326 cchBuff,
1327 wFlags,
1328 pKl ? pKl->spkf->pKbdTbl : NULL);
1329
1330 MmCopyToCaller(pwszBuffUnsafe, pwszBuff, cchBuff * sizeof(WCHAR));
1331 ExFreePoolWithTag(pwszBuff, TAG_STRING);
1332
1333 UserLeave();
1334 TRACE("Leave NtUserSetKeyboardState, ret=%i\n", iRet);
1335 return iRet;
1336 }
1337
1338 /*
1339 * NtUserGetKeyNameText
1340 *
1341 * Gets key name from keyboard layout
1342 */
1343 DWORD
1344 APIENTRY
1345 NtUserGetKeyNameText(LONG lParam, LPWSTR lpString, int cchSize)
1346 {
1347 PTHREADINFO pti;
1348 DWORD i, cchKeyName, dwRet = 0;
1349 WORD wScanCode = (lParam >> 16) & 0xFF;
1350 BOOL bExtKey = (HIWORD(lParam) & KF_EXTENDED) ? TRUE : FALSE;
1351 PKBDTABLES pKbdTbl;
1352 VSC_LPWSTR *pKeyNames = NULL;
1353 CONST WCHAR *pKeyName = NULL;
1354 WCHAR KeyNameBuf[2];
1355
1356 TRACE("Enter NtUserGetKeyNameText\n");
1357
1358 UserEnterShared();
1359
1360 /* Get current keyboard layout */
1361 pti = PsGetCurrentThreadWin32Thread();
1362 pKbdTbl = pti ? pti->KeyboardLayout->spkf->pKbdTbl : 0;
1363
1364 if (!pKbdTbl || cchSize < 1)
1365 {
1366 ERR("Invalid parameter\n");
1367 goto cleanup;
1368 }
1369
1370 /* "Do not care" flag */
1371 if(lParam & LP_DO_NOT_CARE_BIT)
1372 {
1373 /* Note: we could do vsc -> vk -> vsc conversion, instead of using
1374 hardcoded scan codes, but it's not what Windows does */
1375 if (wScanCode == SCANCODE_RSHIFT && !bExtKey)
1376 wScanCode = SCANCODE_LSHIFT;
1377 else if (wScanCode == SCANCODE_CTRL || wScanCode == SCANCODE_ALT)
1378 bExtKey = FALSE;
1379 }
1380
1381 if (bExtKey)
1382 pKeyNames = pKbdTbl->pKeyNamesExt;
1383 else
1384 pKeyNames = pKbdTbl->pKeyNames;
1385
1386 for (i = 0; pKeyNames[i].pwsz; i++)
1387 {
1388 if (pKeyNames[i].vsc == wScanCode)
1389 {
1390 pKeyName = pKeyNames[i].pwsz;
1391 break;
1392 }
1393 }
1394
1395 if (!pKeyName)
1396 {
1397 WORD wVk = IntVscToVk(wScanCode, pKbdTbl);
1398
1399 if (wVk)
1400 {
1401 KeyNameBuf[0] = IntVkToChar(wVk, pKbdTbl);
1402 KeyNameBuf[1] = 0;
1403 if (KeyNameBuf[0])
1404 pKeyName = KeyNameBuf;
1405 }
1406 }
1407
1408 if (pKeyName)
1409 {
1410 cchKeyName = wcslen(pKeyName);
1411 if (cchKeyName > cchSize - 1)
1412 cchKeyName = cchSize - 1; // don't count '\0'
1413
1414 _SEH2_TRY
1415 {
1416 ProbeForWrite(lpString, (cchKeyName + 1) * sizeof(WCHAR), 1);
1417 RtlCopyMemory(lpString, pKeyName, cchKeyName * sizeof(WCHAR));
1418 lpString[cchKeyName] = UNICODE_NULL;
1419 dwRet = cchKeyName;
1420 }
1421 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1422 {
1423 SetLastNtError(_SEH2_GetExceptionCode());
1424 }
1425 _SEH2_END;
1426 }
1427 else
1428 {
1429 EngSetLastError(ERROR_INVALID_PARAMETER);
1430 }
1431
1432 cleanup:
1433 UserLeave();
1434 TRACE("Leave NtUserGetKeyNameText, ret=%i\n", dwRet);
1435 return dwRet;
1436 }
1437
1438 /*
1439 * UserGetKeyboardType
1440 *
1441 * Returns some keyboard specific information
1442 */
1443 DWORD FASTCALL
1444 UserGetKeyboardType(
1445 DWORD dwTypeFlag)
1446 {
1447 switch (dwTypeFlag)
1448 {
1449 case 0: /* Keyboard type */
1450 return 4; /* AT-101 */
1451 case 1: /* Keyboard Subtype */
1452 return 0; /* There are no defined subtypes */
1453 case 2: /* Number of F-keys */
1454 return 12; /* We're doing an 101 for now, so return 12 F-keys */
1455 default:
1456 ERR("Unknown type!\n");
1457 return 0; /* Note: we don't have to set last error here */
1458 }
1459 }
1460
1461 /*
1462 * NtUserVkKeyScanEx
1463 *
1464 * Based on IntTranslateChar, instead of processing VirtualKey match,
1465 * look for wChar match.
1466 */
1467 DWORD
1468 APIENTRY
1469 NtUserVkKeyScanEx(
1470 WCHAR wch,
1471 HKL dwhkl,
1472 BOOL bUsehKL)
1473 {
1474 PKBDTABLES pKbdTbl;
1475 PVK_TO_WCHAR_TABLE pVkToWchTbl;
1476 PVK_TO_WCHARS10 pVkToWch;
1477 PKL pKl = NULL;
1478 DWORD i, dwModBits = 0, dwModNumber = 0, Ret = (DWORD)-1;
1479
1480 TRACE("NtUserVkKeyScanEx() wch %d, KbdLayout 0x%p\n", wch, dwhkl);
1481 UserEnterShared();
1482
1483 if (bUsehKL)
1484 {
1485 // Use given keyboard layout
1486 if (dwhkl)
1487 pKl = UserHklToKbl(dwhkl);
1488 }
1489 else
1490 {
1491 // Use thread keyboard layout
1492 pKl = ((PTHREADINFO)PsGetCurrentThreadWin32Thread())->KeyboardLayout;
1493 }
1494
1495 if (!pKl)
1496 goto Exit;
1497
1498 pKbdTbl = pKl->spkf->pKbdTbl;
1499
1500 // Interate through all VkToWchar tables while pVkToWchars is not NULL
1501 for (i = 0; pKbdTbl->pVkToWcharTable[i].pVkToWchars; i++)
1502 {
1503 pVkToWchTbl = &pKbdTbl->pVkToWcharTable[i];
1504 pVkToWch = (PVK_TO_WCHARS10)(pVkToWchTbl->pVkToWchars);
1505
1506 // Interate through all virtual keys
1507 while (pVkToWch->VirtualKey)
1508 {
1509 for (dwModNumber = 0; dwModNumber < pVkToWchTbl->nModifications; dwModNumber++)
1510 {
1511 if (pVkToWch->wch[dwModNumber] == wch)
1512 {
1513 dwModBits = pKbdTbl->pCharModifiers->ModNumber[dwModNumber];
1514 TRACE("i %d wC %04x: dwModBits %08x dwModNumber %08x MaxModBits %08x\n",
1515 i, wch, dwModBits, dwModNumber, pKbdTbl->pCharModifiers->wMaxModBits);
1516 Ret = (dwModBits << 8) | (pVkToWch->VirtualKey & 0xFF);
1517 goto Exit;
1518 }
1519 }
1520 pVkToWch = (PVK_TO_WCHARS10)(((BYTE *)pVkToWch) + pVkToWchTbl->cbSize);
1521 }
1522 }
1523 Exit:
1524 UserLeave();
1525 return Ret;
1526 }
1527
1528 /* EOF */