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
15 DBG_DEFAULT_CHANNEL(UserKbdLayout
);
17 PKL gspklBaseLayout
= NULL
;
18 PKBDFILE gpkfList
= NULL
;
20 typedef PVOID (*PFN_KBDLAYERDESCRIPTOR
)(VOID
);
23 /* PRIVATE FUNCTIONS ******************************************************/
28 * Loads keyboard layout DLL and gets address to KbdTables
31 UserLoadKbdDll(WCHAR
*pwszLayoutPath
,
33 PKBDTABLES
*pKbdTables
)
35 PFN_KBDLAYERDESCRIPTOR pfnKbdLayerDescriptor
;
37 /* Load keyboard layout DLL */
38 TRACE("Loading Keyboard DLL %ws\n", pwszLayoutPath
);
39 *phModule
= EngLoadImage(pwszLayoutPath
);
42 ERR("Failed to load dll %ws\n", pwszLayoutPath
);
46 /* Find KbdLayerDescriptor function and get layout tables */
47 TRACE("Loaded %ws\n", pwszLayoutPath
);
48 pfnKbdLayerDescriptor
= EngFindImageProcAddress(*phModule
, "KbdLayerDescriptor");
50 /* FIXME: Windows reads file instead of executing!
51 It's not safe to kbdlayout DLL in kernel mode! */
53 if (pfnKbdLayerDescriptor
)
54 *pKbdTables
= pfnKbdLayerDescriptor();
56 ERR("Error: %ws has no KbdLayerDescriptor()\n", pwszLayoutPath
);
58 if (!pfnKbdLayerDescriptor
|| !*pKbdTables
)
60 ERR("Failed to load the keyboard layout.\n");
61 EngUnloadImage(*phModule
);
65 #if 0 /* Dump keyboard layout */
68 PVK_TO_BIT pVkToBit
= (*pKbdTables
)->pCharModifiers
->pVkToBit
;
69 PVK_TO_WCHAR_TABLE pVkToWchTbl
= (*pKbdTables
)->pVkToWcharTable
;
70 PVSC_VK pVscVk
= (*pKbdTables
)->pVSCtoVK_E0
;
71 DbgPrint("Kbd layout: fLocaleFlags %x bMaxVSCtoVK %x\n", (*pKbdTables
)->fLocaleFlags
, (*pKbdTables
)->bMaxVSCtoVK
);
72 DbgPrint("wMaxModBits %x\n", (*pKbdTables
)->pCharModifiers
->wMaxModBits
);
75 DbgPrint("VkToBit %x -> %x\n", pVkToBit
->Vk
, pVkToBit
->ModBits
);
78 for (i
= 0; i
<= (*pKbdTables
)->pCharModifiers
->wMaxModBits
; ++i
)
79 DbgPrint("ModNumber %x -> %x\n", i
, (*pKbdTables
)->pCharModifiers
->ModNumber
[i
]);
80 while (pVkToWchTbl
->pVkToWchars
)
82 PVK_TO_WCHARS1 pVkToWch
= pVkToWchTbl
->pVkToWchars
;
83 DbgPrint("pVkToWchTbl nModifications %x cbSize %x\n", pVkToWchTbl
->nModifications
, pVkToWchTbl
->cbSize
);
84 while (pVkToWch
->VirtualKey
)
86 DbgPrint("pVkToWch VirtualKey %x Attributes %x wc { ", pVkToWch
->VirtualKey
, pVkToWch
->Attributes
);
87 for (i
= 0; i
< pVkToWchTbl
->nModifications
; ++i
)
88 DbgPrint("%x ", pVkToWch
->wch
[i
]);
90 pVkToWch
= (PVK_TO_WCHARS1
)(((PBYTE
)pVkToWch
) + pVkToWchTbl
->cbSize
);
94 DbgPrint("pusVSCtoVK: { ");
95 for (i
= 0; i
< (*pKbdTables
)->bMaxVSCtoVK
; ++i
)
96 DbgPrint("%x -> %x, ", i
, (*pKbdTables
)->pusVSCtoVK
[i
]);
98 DbgPrint("pVSCtoVK_E0: { ");
101 DbgPrint("%x -> %x, ", pVscVk
->Vsc
, pVscVk
->Vk
);
105 pVscVk
= (*pKbdTables
)->pVSCtoVK_E1
;
106 DbgPrint("pVSCtoVK_E1: { ");
109 DbgPrint("%x -> %x, ", pVscVk
->Vsc
, pVscVk
->Vk
);
123 * Loads keyboard layout DLL and creates KBDFILE object
126 UserLoadKbdFile(PUNICODE_STRING pwszKLID
)
128 PKBDFILE pkf
, pRet
= NULL
;
132 WCHAR wszLayoutPath
[MAX_PATH
] = L
"\\SystemRoot\\System32\\";
133 WCHAR wszLayoutRegKey
[256] = L
"\\REGISTRY\\Machine\\SYSTEM\\CurrentControlSet\\"
134 L
"Control\\Keyboard Layouts\\";
136 /* Create keyboard layout file object */
137 pkf
= UserCreateObject(gHandleTable
, NULL
, NULL
, NULL
, TYPE_KBDFILE
, sizeof(KBDFILE
));
140 ERR("Failed to create object!\n");
144 /* Set keyboard layout name */
145 swprintf(pkf
->awchKF
, L
"%wZ", pwszKLID
);
147 /* Open layout registry key */
148 RtlStringCbCatW(wszLayoutRegKey
, sizeof(wszLayoutRegKey
), pkf
->awchKF
);
149 Status
= RegOpenKey(wszLayoutRegKey
, &hKey
);
150 if (!NT_SUCCESS(Status
))
152 ERR("Failed to open keyboard layouts registry key %ws (%lx)\n", wszLayoutRegKey
, Status
);
156 /* Read filename of layout DLL */
157 cbSize
= sizeof(wszLayoutPath
) - wcslen(wszLayoutPath
)*sizeof(WCHAR
);
158 Status
= RegQueryValue(hKey
,
161 wszLayoutPath
+ wcslen(wszLayoutPath
),
164 if (!NT_SUCCESS(Status
))
166 ERR("Can't get layout filename for %wZ (%lx)\n", pwszKLID
, Status
);
170 /* Load keyboard file now */
171 if (!UserLoadKbdDll(wszLayoutPath
, &pkf
->hBase
, &pkf
->pKbdTbl
))
173 ERR("Failed to load %ws dll!\n", wszLayoutPath
);
177 /* Update next field */
178 pkf
->pkfNext
= gpkfList
;
181 /* Return keyboard file */
188 UserDereferenceObject(pkf
); // we dont need ptr anymore
191 /* We have failed - destroy created object */
193 UserDeleteObject(pkf
->head
.h
, TYPE_KBDFILE
);
202 * Loads keyboard layout and creates KL object
205 UserLoadKbdLayout(PUNICODE_STRING pwszKLID
, HKL hKL
)
211 /* Create keyboard layout object */
212 pKl
= UserCreateObject(gHandleTable
, NULL
, NULL
, NULL
, TYPE_KBDLAYOUT
, sizeof(KL
));
215 ERR("Failed to create object!\n");
220 pKl
->spkf
= UserLoadKbdFile(pwszKLID
);
222 /* Dereference keyboard layout */
223 UserDereferenceObject(pKl
);
225 /* If we failed, remove KL object */
228 ERR("UserLoadKbdFile(%wZ) failed!\n", pwszKLID
);
229 UserDeleteObject(pKl
->head
.h
, TYPE_KBDLAYOUT
);
233 // Up to Language Identifiers..
234 RtlUnicodeStringToInteger(pwszKLID
, (ULONG
)16, (PULONG
)&lCid
);
235 TRACE("Language Identifiers %wZ LCID 0x%x\n", pwszKLID
, lCid
);
236 if (co_IntGetCharsetInfo(lCid
, &cs
))
238 pKl
->iBaseCharset
= cs
.ciCharset
;
239 pKl
->dwFontSigs
= cs
.fs
.fsCsb
[0];
240 pKl
->CodePage
= (USHORT
)cs
.ciACP
;
241 TRACE("Charset %u Font Sig %lu CodePage %u\n", pKl
->iBaseCharset
, pKl
->dwFontSigs
, pKl
->CodePage
);
245 pKl
->iBaseCharset
= ANSI_CHARSET
;
246 pKl
->dwFontSigs
= FS_LATIN1
;
247 pKl
->CodePage
= CP_ACP
;
256 * Destroys specified Keyboard File object
260 UnloadKbdFile(_In_ PKBDFILE pkf
)
262 PKBDFILE
*ppkfLink
= &gpkfList
;
263 NT_ASSERT(pkf
!= NULL
);
265 /* Find previous object */
268 if (*ppkfLink
== pkf
)
271 ppkfLink
= &(*ppkfLink
)->pkfNext
;
274 if (*ppkfLink
== pkf
)
275 *ppkfLink
= pkf
->pkfNext
;
277 EngUnloadImage(pkf
->hBase
);
278 UserDeleteObject(pkf
->head
.h
, TYPE_KBDFILE
);
284 * Unloads specified Keyboard Layout if possible
287 UserUnloadKbl(PKL pKl
)
289 /* According to msdn, UnloadKeyboardLayout can fail
290 if the keyboard layout identifier was preloaded. */
291 if (pKl
== gspklBaseLayout
)
293 if (pKl
->pklNext
== pKl
->pklPrev
)
295 /* There is only one layout */
299 /* Set next layout as default */
300 gspklBaseLayout
= pKl
->pklNext
;
303 if (pKl
->head
.cLockObj
> 1)
305 /* Layout is used by other threads */
306 pKl
->dwKL_Flags
|= KLF_UNLOAD
;
310 /* Unload the layout */
311 pKl
->pklPrev
->pklNext
= pKl
->pklNext
;
312 pKl
->pklNext
->pklPrev
= pKl
->pklPrev
;
313 UnloadKbdFile(pKl
->spkf
);
314 UserDeleteObject(pKl
->head
.h
, TYPE_KBDLAYOUT
);
319 * W32kGetDefaultKeyLayout
321 * Returns default layout for new threads
324 W32kGetDefaultKeyLayout(VOID
)
326 PKL pKl
= gspklBaseLayout
;
331 /* Return not unloaded layout */
334 if (!(pKl
->dwKL_Flags
& KLF_UNLOAD
))
337 pKl
= pKl
->pklPrev
; /* Confirmed on Win2k */
338 } while(pKl
!= gspklBaseLayout
);
340 /* We have not found proper KL */
347 * Gets KL object from hkl value
351 UserHklToKbl(HKL hKl
)
353 PKL pKl
= gspklBaseLayout
;
355 if (!gspklBaseLayout
)
364 } while (pKl
!= gspklBaseLayout
);
370 * UserSetDefaultInputLang
372 * Sets default kyboard layout for system. Called from UserSystemParametersInfo.
376 UserSetDefaultInputLang(HKL hKl
)
380 pKl
= UserHklToKbl(hKl
);
384 gspklBaseLayout
= pKl
;
391 * Activates given layout in specified thread
394 co_UserActivateKbl(PTHREADINFO pti
, PKL pKl
, UINT Flags
)
398 pklPrev
= pti
->KeyboardLayout
;
400 UserDereferenceObject(pklPrev
);
402 pti
->KeyboardLayout
= pKl
;
403 pti
->pClientInfo
->hKL
= pKl
->hkl
;
404 UserReferenceObject(pKl
);
406 if (Flags
& KLF_SETFORPROCESS
)
411 // Send WM_INPUTLANGCHANGE to thread's focus window
412 co_IntSendMessage(pti
->MessageQueue
->spwndFocus
? UserHMGetHandle(pti
->MessageQueue
->spwndFocus
) : 0,
414 (WPARAM
)pKl
->iBaseCharset
, // FIXME: How to set it?
415 (LPARAM
)pKl
->hkl
); // hkl
420 /* EXPORTS *******************************************************************/
423 * UserGetKeyboardLayout
425 * Returns hkl of given thread keyboard layout
428 UserGetKeyboardLayout(
439 pti
= PsGetCurrentThreadWin32Thread();
440 pKl
= pti
->KeyboardLayout
;
441 return pKl
? pKl
->hkl
: NULL
;
444 Status
= PsLookupThreadByThreadId((HANDLE
)(DWORD_PTR
)dwThreadId
, &pThread
);
445 if (!NT_SUCCESS(Status
))
447 EngSetLastError(ERROR_INVALID_PARAMETER
);
451 pti
= PsGetThreadWin32Thread(pThread
);
452 pKl
= pti
->KeyboardLayout
;
453 hKl
= pKl
? pKl
->hkl
: NULL
;;
454 ObDereferenceObject(pThread
);
459 * NtUserGetKeyboardLayoutList
461 * Returns list of loaded keyboard layouts in system
465 NtUserGetKeyboardLayoutList(
477 if (!gspklBaseLayout
)
482 pKl
= gspklBaseLayout
;
490 } while (pKl
!= gspklBaseLayout
);
496 ProbeForWrite(pHklBuff
, nBuff
*sizeof(HKL
), 4);
500 pHklBuff
[uRet
] = pKl
->hkl
;
503 if (pKl
== gspklBaseLayout
)
507 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
509 SetLastNtError(_SEH2_GetExceptionCode());
520 * NtUserGetKeyboardLayoutName
522 * Returns KLID of current thread keyboard layout
526 NtUserGetKeyboardLayoutName(
535 pti
= PsGetCurrentThreadWin32Thread();
536 pKl
= pti
->KeyboardLayout
;
543 ProbeForWrite(pwszName
, KL_NAMELENGTH
*sizeof(WCHAR
), 1);
544 wcscpy(pwszName
, pKl
->spkf
->awchKF
);
547 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
549 SetLastNtError(_SEH2_GetExceptionCode());
559 * NtUserLoadKeyboardLayoutEx
561 * Loads keyboard layout with given locale id
565 NtUserLoadKeyboardLayoutEx(
566 IN HANDLE Handle
, // hFile (See downloads.securityfocus.com/vulnerabilities/exploits/43774.c)
567 IN DWORD offTable
, // Offset to KbdTables
568 IN PUNICODE_STRING puszKeyboardName
, // Not used?
570 IN PUNICODE_STRING pustrKLID
,
575 PKL pKl
= NULL
, pklLast
;
577 UNICODE_STRING ustrSafeKLID
;
579 if (Flags
& ~(KLF_ACTIVATE
|KLF_NOTELLSHELL
|KLF_REORDER
|KLF_REPLACELANG
|
580 KLF_SUBSTITUTE_OK
|KLF_SETFORPROCESS
|KLF_UNLOADPREVIOUS
|
581 KLF_RESET
|KLF_SETFORPROCESS
|KLF_SHIFTLOCK
))
583 ERR("Invalid flags: %x\n", Flags
);
584 EngSetLastError(ERROR_INVALID_FLAGS
);
588 /* FIXME: It seems KLF_RESET is only supported for WINLOGON */
590 RtlInitEmptyUnicodeString(&ustrSafeKLID
, Buffer
, sizeof(Buffer
));
593 ProbeForRead(pustrKLID
, sizeof(*pustrKLID
), 1);
594 ProbeForRead(pustrKLID
->Buffer
, sizeof(pustrKLID
->Length
), 1);
595 RtlCopyUnicodeString(&ustrSafeKLID
, pustrKLID
);
597 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
599 SetLastNtError(_SEH2_GetExceptionCode());
600 _SEH2_YIELD(return NULL
);
604 UserEnterExclusive();
606 /* If hklUnload is specified, unload it and load new layput as default */
607 if (hklUnload
&& hklUnload
!= (HKL
)hkl
)
609 pKl
= UserHklToKbl(hklUnload
);
614 /* Let's see if layout was already loaded. */
615 pKl
= UserHklToKbl((HKL
)hkl
);
618 /* It wasn't, so load it. */
619 pKl
= UserLoadKbdLayout(&ustrSafeKLID
, (HKL
)hkl
);
625 /* Find last not unloaded layout */
626 pklLast
= gspklBaseLayout
->pklPrev
;
627 while (pklLast
!= gspklBaseLayout
&& pklLast
->dwKL_Flags
& KLF_UNLOAD
)
628 pklLast
= pklLast
->pklPrev
;
630 /* Add new layout to the list */
631 pKl
->pklNext
= pklLast
->pklNext
;
632 pKl
->pklPrev
= pklLast
;
633 pKl
->pklNext
->pklPrev
= pKl
;
634 pKl
->pklPrev
->pklNext
= pKl
;
638 /* This is the first layout */
641 gspklBaseLayout
= pKl
;
645 /* If this layout was prepared to unload, undo it */
646 pKl
->dwKL_Flags
&= ~KLF_UNLOAD
;
648 /* Activate this layout in current thread */
649 if (Flags
& KLF_ACTIVATE
)
650 co_UserActivateKbl(PsGetCurrentThreadWin32Thread(), pKl
, Flags
);
652 /* Send shell message */
653 if (!(Flags
& KLF_NOTELLSHELL
))
654 co_IntShellHookNotify(HSHELL_LANGUAGE
, 0, (LPARAM
)hkl
);
656 /* Return hkl on success */
659 /* FIXME: KLF_REPLACELANG
668 * NtUserActivateKeyboardLayout
670 * Activates specified layout for thread or process
674 NtUserActivateKeyboardLayout(
682 UserEnterExclusive();
684 pti
= PsGetCurrentThreadWin32Thread();
686 /* hKl can have special value HKL_NEXT or HKL_PREV */
687 if (hKl
== (HKL
)HKL_NEXT
)
689 /* Get next keyboard layout starting with current */
690 if (pti
->KeyboardLayout
)
691 pKl
= pti
->KeyboardLayout
->pklNext
;
693 else if (hKl
== (HKL
)HKL_PREV
)
695 /* Get previous keyboard layout starting with current */
696 if (pti
->KeyboardLayout
)
697 pKl
= pti
->KeyboardLayout
->pklNext
;
700 pKl
= UserHklToKbl(hKl
);
704 ERR("Invalid HKL %p!\n", hKl
);
713 if (Flags
& KLF_REORDER
)
714 gspklBaseLayout
= pKl
;
716 if (pKl
!= pti
->KeyboardLayout
)
718 /* Activate layout for current thread */
719 pKl
= co_UserActivateKbl(pti
, pKl
, Flags
);
721 /* Send shell message */
722 if (!(Flags
& KLF_NOTELLSHELL
))
723 co_IntShellHookNotify(HSHELL_LANGUAGE
, 0, (LPARAM
)hkl
);
732 * NtUserUnloadKeyboardLayout
734 * Unloads keyboard layout with specified hkl value
738 NtUserUnloadKeyboardLayout(
744 UserEnterExclusive();
746 pKl
= UserHklToKbl(hKl
);
748 bRet
= UserUnloadKbl(pKl
);
750 ERR("Invalid HKL %p!\n", hKl
);