2 * PROJECT: ReactOS Win32k subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: subsystems/win32/win32k/ntuser/kbdlayout.c
5 * PURPOSE: Keyboard layout management
6 * COPYRIGHT: Copyright 2007 Saveliy Tretiakov
7 * Copyright 2008 Colin Finck
8 * Copyright 2011 Rafal Harabien
12 DBG_DEFAULT_CHANNEL(UserKbdLayout
);
14 PKL gspklBaseLayout
= NULL
;
15 PKBDFILE gpkfList
= NULL
;
17 typedef PVOID (*PFN_KBDLAYERDESCRIPTOR
)(VOID
);
20 /* PRIVATE FUNCTIONS ******************************************************/
25 * Loads keyboard layout DLL and gets address to KbdTables
28 UserLoadKbdDll(WCHAR
*pwszLayoutPath
,
30 PKBDTABLES
*pKbdTables
)
32 PFN_KBDLAYERDESCRIPTOR pfnKbdLayerDescriptor
;
34 /* Load keyboard layout DLL */
35 TRACE("Loading Keyboard DLL %ws\n", pwszLayoutPath
);
36 *phModule
= EngLoadImage(pwszLayoutPath
);
39 ERR("Failed to load dll %ws\n", pwszLayoutPath
);
43 /* Find KbdLayerDescriptor function and get layout tables */
44 TRACE("Loaded %ws\n", pwszLayoutPath
);
45 pfnKbdLayerDescriptor
= EngFindImageProcAddress(*phModule
, "KbdLayerDescriptor");
47 /* FIXME: Windows reads file instead of executing!
48 It's not safe to kbdlayout DLL in kernel mode! */
50 if (pfnKbdLayerDescriptor
)
51 *pKbdTables
= pfnKbdLayerDescriptor();
53 ERR("Error: %ws has no KbdLayerDescriptor()\n", pwszLayoutPath
);
55 if (!pfnKbdLayerDescriptor
|| !*pKbdTables
)
57 ERR("Failed to load the keyboard layout.\n");
58 EngUnloadImage(*phModule
);
62 #if 0 /* Dump keyboard layout */
65 PVK_TO_BIT pVkToBit
= (*pKbdTables
)->pCharModifiers
->pVkToBit
;
66 PVK_TO_WCHAR_TABLE pVkToWchTbl
= (*pKbdTables
)->pVkToWcharTable
;
67 PVSC_VK pVscVk
= (*pKbdTables
)->pVSCtoVK_E0
;
68 DbgPrint("Kbd layout: fLocaleFlags %x bMaxVSCtoVK %x\n", (*pKbdTables
)->fLocaleFlags
, (*pKbdTables
)->bMaxVSCtoVK
);
69 DbgPrint("wMaxModBits %x\n", (*pKbdTables
)->pCharModifiers
->wMaxModBits
);
72 DbgPrint("VkToBit %x -> %x\n", pVkToBit
->Vk
, pVkToBit
->ModBits
);
75 for (i
= 0; i
<= (*pKbdTables
)->pCharModifiers
->wMaxModBits
; ++i
)
76 DbgPrint("ModNumber %x -> %x\n", i
, (*pKbdTables
)->pCharModifiers
->ModNumber
[i
]);
77 while (pVkToWchTbl
->pVkToWchars
)
79 PVK_TO_WCHARS1 pVkToWch
= pVkToWchTbl
->pVkToWchars
;
80 DbgPrint("pVkToWchTbl nModifications %x cbSize %x\n", pVkToWchTbl
->nModifications
, pVkToWchTbl
->cbSize
);
81 while (pVkToWch
->VirtualKey
)
83 DbgPrint("pVkToWch VirtualKey %x Attributes %x wc { ", pVkToWch
->VirtualKey
, pVkToWch
->Attributes
);
84 for (i
= 0; i
< pVkToWchTbl
->nModifications
; ++i
)
85 DbgPrint("%x ", pVkToWch
->wch
[i
]);
87 pVkToWch
= (PVK_TO_WCHARS1
)(((PBYTE
)pVkToWch
) + pVkToWchTbl
->cbSize
);
91 DbgPrint("pusVSCtoVK: { ");
92 for (i
= 0; i
< (*pKbdTables
)->bMaxVSCtoVK
; ++i
)
93 DbgPrint("%x -> %x, ", i
, (*pKbdTables
)->pusVSCtoVK
[i
]);
95 DbgPrint("pVSCtoVK_E0: { ");
98 DbgPrint("%x -> %x, ", pVscVk
->Vsc
, pVscVk
->Vk
);
102 pVscVk
= (*pKbdTables
)->pVSCtoVK_E1
;
103 DbgPrint("pVSCtoVK_E1: { ");
106 DbgPrint("%x -> %x, ", pVscVk
->Vsc
, pVscVk
->Vk
);
120 * Loads keyboard layout DLL and creates KBDFILE object
123 UserLoadKbdFile(PUNICODE_STRING pwszKLID
)
125 PKBDFILE pkf
, pRet
= NULL
;
129 WCHAR wszLayoutPath
[MAX_PATH
] = L
"\\SystemRoot\\System32\\";
130 WCHAR wszLayoutRegKey
[256] = L
"\\REGISTRY\\Machine\\SYSTEM\\CurrentControlSet\\"
131 L
"Control\\Keyboard Layouts\\";
133 /* Create keyboard layout file object */
134 pkf
= UserCreateObject(gHandleTable
, NULL
, NULL
, NULL
, TYPE_KBDFILE
, sizeof(KBDFILE
));
137 ERR("Failed to create object!\n");
141 /* Set keyboard layout name */
142 swprintf(pkf
->awchKF
, L
"%wZ", pwszKLID
);
144 /* Open layout registry key */
145 RtlStringCbCatW(wszLayoutRegKey
, sizeof(wszLayoutRegKey
), pkf
->awchKF
);
146 Status
= RegOpenKey(wszLayoutRegKey
, &hKey
);
147 if (!NT_SUCCESS(Status
))
149 ERR("Failed to open keyboard layouts registry key %ws (%lx)\n", wszLayoutRegKey
, Status
);
153 /* Read filename of layout DLL */
154 cbSize
= sizeof(wszLayoutPath
) - wcslen(wszLayoutPath
)*sizeof(WCHAR
);
155 Status
= RegQueryValue(hKey
,
158 wszLayoutPath
+ wcslen(wszLayoutPath
),
161 if (!NT_SUCCESS(Status
))
163 ERR("Can't get layout filename for %wZ (%lx)\n", pwszKLID
, Status
);
167 /* Load keyboard file now */
168 if (!UserLoadKbdDll(wszLayoutPath
, &pkf
->hBase
, &pkf
->pKbdTbl
))
170 ERR("Failed to load %ws dll!\n", wszLayoutPath
);
174 /* Update next field */
175 pkf
->pkfNext
= gpkfList
;
178 /* Return keyboard file */
185 UserDereferenceObject(pkf
); // we dont need ptr anymore
188 /* We have failed - destroy created object */
190 UserDeleteObject(pkf
->head
.h
, TYPE_KBDFILE
);
199 * Loads keyboard layout and creates KL object
202 UserLoadKbdLayout(PUNICODE_STRING pwszKLID
, HKL hKL
)
208 /* Create keyboard layout object */
209 pKl
= UserCreateObject(gHandleTable
, NULL
, NULL
, NULL
, TYPE_KBDLAYOUT
, sizeof(KL
));
212 ERR("Failed to create object!\n");
217 pKl
->spkf
= UserLoadKbdFile(pwszKLID
);
219 /* Dereference keyboard layout */
220 UserDereferenceObject(pKl
);
222 /* If we failed, remove KL object */
225 ERR("UserLoadKbdFile(%wZ) failed!\n", pwszKLID
);
226 UserDeleteObject(pKl
->head
.h
, TYPE_KBDLAYOUT
);
230 // Up to Language Identifiers..
231 RtlUnicodeStringToInteger(pwszKLID
, (ULONG
)16, (PULONG
)&lCid
);
232 TRACE("Language Identifiers %wZ LCID 0x%x\n", pwszKLID
, lCid
);
233 if (co_IntGetCharsetInfo(lCid
, &cs
))
235 pKl
->iBaseCharset
= cs
.ciCharset
;
236 pKl
->dwFontSigs
= cs
.fs
.fsCsb
[0];
237 pKl
->CodePage
= (USHORT
)cs
.ciACP
;
238 TRACE("Charset %u Font Sig %lu CodePage %u\n", pKl
->iBaseCharset
, pKl
->dwFontSigs
, pKl
->CodePage
);
242 pKl
->iBaseCharset
= ANSI_CHARSET
;
243 pKl
->dwFontSigs
= FS_LATIN1
;
244 pKl
->CodePage
= CP_ACP
;
253 * Destroys specified Keyboard File object
257 UnloadKbdFile(_In_ PKBDFILE pkf
)
259 PKBDFILE
*ppkfLink
= &gpkfList
;
260 NT_ASSERT(pkf
!= NULL
);
262 /* Find previous object */
265 if (*ppkfLink
== pkf
)
268 ppkfLink
= &(*ppkfLink
)->pkfNext
;
271 if (*ppkfLink
== pkf
)
272 *ppkfLink
= pkf
->pkfNext
;
274 EngUnloadImage(pkf
->hBase
);
275 UserDeleteObject(pkf
->head
.h
, TYPE_KBDFILE
);
281 * Unloads specified Keyboard Layout if possible
284 UserUnloadKbl(PKL pKl
)
286 /* According to msdn, UnloadKeyboardLayout can fail
287 if the keyboard layout identifier was preloaded. */
288 if (pKl
== gspklBaseLayout
)
290 if (pKl
->pklNext
== pKl
->pklPrev
)
292 /* There is only one layout */
296 /* Set next layout as default */
297 gspklBaseLayout
= pKl
->pklNext
;
300 if (pKl
->head
.cLockObj
> 1)
302 /* Layout is used by other threads */
303 pKl
->dwKL_Flags
|= KLF_UNLOAD
;
307 /* Unload the layout */
308 pKl
->pklPrev
->pklNext
= pKl
->pklNext
;
309 pKl
->pklNext
->pklPrev
= pKl
->pklPrev
;
310 UnloadKbdFile(pKl
->spkf
);
311 UserDeleteObject(pKl
->head
.h
, TYPE_KBDLAYOUT
);
316 * W32kGetDefaultKeyLayout
318 * Returns default layout for new threads
321 W32kGetDefaultKeyLayout(VOID
)
323 PKL pKl
= gspklBaseLayout
;
328 /* Return not unloaded layout */
331 if (!(pKl
->dwKL_Flags
& KLF_UNLOAD
))
334 pKl
= pKl
->pklPrev
; /* Confirmed on Win2k */
335 } while(pKl
!= gspklBaseLayout
);
337 /* We have not found proper KL */
344 * Gets KL object from hkl value
348 UserHklToKbl(HKL hKl
)
350 PKL pKl
= gspklBaseLayout
;
352 if (!gspklBaseLayout
)
361 } while (pKl
!= gspklBaseLayout
);
367 * UserSetDefaultInputLang
369 * Sets default kyboard layout for system. Called from UserSystemParametersInfo.
373 UserSetDefaultInputLang(HKL hKl
)
377 pKl
= UserHklToKbl(hKl
);
381 gspklBaseLayout
= pKl
;
388 * Activates given layout in specified thread
391 co_UserActivateKbl(PTHREADINFO pti
, PKL pKl
, UINT Flags
)
395 pklPrev
= pti
->KeyboardLayout
;
397 UserDereferenceObject(pklPrev
);
399 pti
->KeyboardLayout
= pKl
;
400 pti
->pClientInfo
->hKL
= pKl
->hkl
;
401 UserReferenceObject(pKl
);
403 if (Flags
& KLF_SETFORPROCESS
)
408 // Send WM_INPUTLANGCHANGE to thread's focus window
409 co_IntSendMessage(pti
->MessageQueue
->spwndFocus
? UserHMGetHandle(pti
->MessageQueue
->spwndFocus
) : 0,
411 (WPARAM
)pKl
->iBaseCharset
, // FIXME: How to set it?
412 (LPARAM
)pKl
->hkl
); // hkl
417 /* EXPORTS *******************************************************************/
420 * UserGetKeyboardLayout
422 * Returns hkl of given thread keyboard layout
425 UserGetKeyboardLayout(
436 pti
= PsGetCurrentThreadWin32Thread();
437 pKl
= pti
->KeyboardLayout
;
438 return pKl
? pKl
->hkl
: NULL
;
441 Status
= PsLookupThreadByThreadId((HANDLE
)(DWORD_PTR
)dwThreadId
, &pThread
);
442 if (!NT_SUCCESS(Status
))
444 EngSetLastError(ERROR_INVALID_PARAMETER
);
448 pti
= PsGetThreadWin32Thread(pThread
);
449 pKl
= pti
->KeyboardLayout
;
450 hKl
= pKl
? pKl
->hkl
: NULL
;;
451 ObDereferenceObject(pThread
);
456 * NtUserGetKeyboardLayoutList
458 * Returns list of loaded keyboard layouts in system
462 NtUserGetKeyboardLayoutList(
474 if (!gspklBaseLayout
)
476 pKl
= gspklBaseLayout
;
484 } while (pKl
!= gspklBaseLayout
);
490 ProbeForWrite(pHklBuff
, nBuff
*sizeof(HKL
), 4);
494 pHklBuff
[uRet
] = pKl
->hkl
;
497 if (pKl
== gspklBaseLayout
)
501 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
503 SetLastNtError(_SEH2_GetExceptionCode());
514 * NtUserGetKeyboardLayoutName
516 * Returns KLID of current thread keyboard layout
520 NtUserGetKeyboardLayoutName(
529 pti
= PsGetCurrentThreadWin32Thread();
530 pKl
= pti
->KeyboardLayout
;
537 ProbeForWrite(pwszName
, KL_NAMELENGTH
*sizeof(WCHAR
), 1);
538 wcscpy(pwszName
, pKl
->spkf
->awchKF
);
541 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
543 SetLastNtError(_SEH2_GetExceptionCode());
553 * NtUserLoadKeyboardLayoutEx
555 * Loads keyboard layout with given locale id
559 NtUserLoadKeyboardLayoutEx(
560 IN HANDLE Handle
, // hFile (See downloads.securityfocus.com/vulnerabilities/exploits/43774.c)
561 IN DWORD offTable
, // Offset to KbdTables
562 IN PUNICODE_STRING puszKeyboardName
, // Not used?
564 IN PUNICODE_STRING pustrKLID
,
569 PKL pKl
= NULL
, pklLast
;
571 UNICODE_STRING ustrSafeKLID
;
573 if (Flags
& ~(KLF_ACTIVATE
|KLF_NOTELLSHELL
|KLF_REORDER
|KLF_REPLACELANG
|
574 KLF_SUBSTITUTE_OK
|KLF_SETFORPROCESS
|KLF_UNLOADPREVIOUS
|
575 KLF_RESET
|KLF_SETFORPROCESS
|KLF_SHIFTLOCK
))
577 ERR("Invalid flags: %x\n", Flags
);
578 EngSetLastError(ERROR_INVALID_FLAGS
);
582 /* FIXME: It seems KLF_RESET is only supported for WINLOGON */
584 RtlInitEmptyUnicodeString(&ustrSafeKLID
, Buffer
, sizeof(Buffer
));
587 ProbeForRead(pustrKLID
, sizeof(*pustrKLID
), 1);
588 ProbeForRead(pustrKLID
->Buffer
, sizeof(pustrKLID
->Length
), 1);
589 RtlCopyUnicodeString(&ustrSafeKLID
, pustrKLID
);
591 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
593 SetLastNtError(_SEH2_GetExceptionCode());
594 _SEH2_YIELD(return NULL
);
598 UserEnterExclusive();
600 /* If hklUnload is specified, unload it and load new layput as default */
601 if (hklUnload
&& hklUnload
!= (HKL
)hkl
)
603 pKl
= UserHklToKbl(hklUnload
);
608 /* Let's see if layout was already loaded. */
609 pKl
= UserHklToKbl((HKL
)hkl
);
612 /* It wasn't, so load it. */
613 pKl
= UserLoadKbdLayout(&ustrSafeKLID
, (HKL
)hkl
);
619 /* Find last not unloaded layout */
620 pklLast
= gspklBaseLayout
->pklPrev
;
621 while (pklLast
!= gspklBaseLayout
&& pklLast
->dwKL_Flags
& KLF_UNLOAD
)
622 pklLast
= pklLast
->pklPrev
;
624 /* Add new layout to the list */
625 pKl
->pklNext
= pklLast
->pklNext
;
626 pKl
->pklPrev
= pklLast
;
627 pKl
->pklNext
->pklPrev
= pKl
;
628 pKl
->pklPrev
->pklNext
= pKl
;
632 /* This is the first layout */
635 gspklBaseLayout
= pKl
;
639 /* If this layout was prepared to unload, undo it */
640 pKl
->dwKL_Flags
&= ~KLF_UNLOAD
;
642 /* Activate this layout in current thread */
643 if (Flags
& KLF_ACTIVATE
)
644 co_UserActivateKbl(PsGetCurrentThreadWin32Thread(), pKl
, Flags
);
646 /* Send shell message */
647 if (!(Flags
& KLF_NOTELLSHELL
))
648 co_IntShellHookNotify(HSHELL_LANGUAGE
, 0, (LPARAM
)hkl
);
650 /* Return hkl on success */
653 /* FIXME: KLF_REPLACELANG
662 * NtUserActivateKeyboardLayout
664 * Activates specified layout for thread or process
668 NtUserActivateKeyboardLayout(
676 UserEnterExclusive();
678 pti
= PsGetCurrentThreadWin32Thread();
680 /* hKl can have special value HKL_NEXT or HKL_PREV */
681 if (hKl
== (HKL
)HKL_NEXT
)
683 /* Get next keyboard layout starting with current */
684 if (pti
->KeyboardLayout
)
685 pKl
= pti
->KeyboardLayout
->pklNext
;
687 else if (hKl
== (HKL
)HKL_PREV
)
689 /* Get previous keyboard layout starting with current */
690 if (pti
->KeyboardLayout
)
691 pKl
= pti
->KeyboardLayout
->pklNext
;
694 pKl
= UserHklToKbl(hKl
);
698 ERR("Invalid HKL %p!\n", hKl
);
707 if (Flags
& KLF_REORDER
)
708 gspklBaseLayout
= pKl
;
710 if (pKl
!= pti
->KeyboardLayout
)
712 /* Activate layout for current thread */
713 pKl
= co_UserActivateKbl(pti
, pKl
, Flags
);
715 /* Send shell message */
716 if (!(Flags
& KLF_NOTELLSHELL
))
717 co_IntShellHookNotify(HSHELL_LANGUAGE
, 0, (LPARAM
)hkl
);
726 * NtUserUnloadKeyboardLayout
728 * Unloads keyboard layout with specified hkl value
732 NtUserUnloadKeyboardLayout(
738 UserEnterExclusive();
740 pKl
= UserHklToKbl(hKl
);
742 bRet
= UserUnloadKbl(pKl
);
744 ERR("Invalid HKL %p!\n", hKl
);