2 * PROJECT: ReactOS Win32k subsystem
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: win32ss/user/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 UINT gSystemCPCharSet
= 0;
22 typedef PVOID (*PFN_KBDLAYERDESCRIPTOR
)(VOID
);
25 /* PRIVATE FUNCTIONS ******************************************************/
30 * Loads keyboard layout DLL and gets address to KbdTables
33 UserLoadKbdDll(WCHAR
*pwszLayoutPath
,
35 PKBDTABLES
*pKbdTables
)
37 PFN_KBDLAYERDESCRIPTOR pfnKbdLayerDescriptor
;
39 /* Load keyboard layout DLL */
40 TRACE("Loading Keyboard DLL %ws\n", pwszLayoutPath
);
41 *phModule
= EngLoadImage(pwszLayoutPath
);
44 ERR("Failed to load dll %ws\n", pwszLayoutPath
);
48 /* Find KbdLayerDescriptor function and get layout tables */
49 TRACE("Loaded %ws\n", pwszLayoutPath
);
50 pfnKbdLayerDescriptor
= EngFindImageProcAddress(*phModule
, "KbdLayerDescriptor");
52 /* FIXME: Windows reads file instead of executing!
53 It's not safe to kbdlayout DLL in kernel mode! */
55 if (pfnKbdLayerDescriptor
)
56 *pKbdTables
= pfnKbdLayerDescriptor();
58 ERR("Error: %ws has no KbdLayerDescriptor()\n", pwszLayoutPath
);
60 if (!pfnKbdLayerDescriptor
|| !*pKbdTables
)
62 ERR("Failed to load the keyboard layout.\n");
63 EngUnloadImage(*phModule
);
67 #if 0 /* Dump keyboard layout */
70 PVK_TO_BIT pVkToBit
= (*pKbdTables
)->pCharModifiers
->pVkToBit
;
71 PVK_TO_WCHAR_TABLE pVkToWchTbl
= (*pKbdTables
)->pVkToWcharTable
;
72 PVSC_VK pVscVk
= (*pKbdTables
)->pVSCtoVK_E0
;
73 DbgPrint("Kbd layout: fLocaleFlags %x bMaxVSCtoVK %x\n", (*pKbdTables
)->fLocaleFlags
, (*pKbdTables
)->bMaxVSCtoVK
);
74 DbgPrint("wMaxModBits %x\n", (*pKbdTables
)->pCharModifiers
->wMaxModBits
);
77 DbgPrint("VkToBit %x -> %x\n", pVkToBit
->Vk
, pVkToBit
->ModBits
);
80 for (i
= 0; i
<= (*pKbdTables
)->pCharModifiers
->wMaxModBits
; ++i
)
81 DbgPrint("ModNumber %x -> %x\n", i
, (*pKbdTables
)->pCharModifiers
->ModNumber
[i
]);
82 while (pVkToWchTbl
->pVkToWchars
)
84 PVK_TO_WCHARS1 pVkToWch
= pVkToWchTbl
->pVkToWchars
;
85 DbgPrint("pVkToWchTbl nModifications %x cbSize %x\n", pVkToWchTbl
->nModifications
, pVkToWchTbl
->cbSize
);
86 while (pVkToWch
->VirtualKey
)
88 DbgPrint("pVkToWch VirtualKey %x Attributes %x wc { ", pVkToWch
->VirtualKey
, pVkToWch
->Attributes
);
89 for (i
= 0; i
< pVkToWchTbl
->nModifications
; ++i
)
90 DbgPrint("%x ", pVkToWch
->wch
[i
]);
92 pVkToWch
= (PVK_TO_WCHARS1
)(((PBYTE
)pVkToWch
) + pVkToWchTbl
->cbSize
);
96 DbgPrint("pusVSCtoVK: { ");
97 for (i
= 0; i
< (*pKbdTables
)->bMaxVSCtoVK
; ++i
)
98 DbgPrint("%x -> %x, ", i
, (*pKbdTables
)->pusVSCtoVK
[i
]);
100 DbgPrint("pVSCtoVK_E0: { ");
103 DbgPrint("%x -> %x, ", pVscVk
->Vsc
, pVscVk
->Vk
);
107 pVscVk
= (*pKbdTables
)->pVSCtoVK_E1
;
108 DbgPrint("pVSCtoVK_E1: { ");
111 DbgPrint("%x -> %x, ", pVscVk
->Vsc
, pVscVk
->Vk
);
125 * Loads keyboard layout DLL and creates KBDFILE object
128 UserLoadKbdFile(PUNICODE_STRING pwszKLID
)
130 PKBDFILE pkf
, pRet
= NULL
;
134 WCHAR wszLayoutPath
[MAX_PATH
] = L
"\\SystemRoot\\System32\\";
135 WCHAR wszLayoutRegKey
[256] = L
"\\REGISTRY\\Machine\\SYSTEM\\CurrentControlSet\\"
136 L
"Control\\Keyboard Layouts\\";
138 /* Create keyboard layout file object */
139 pkf
= UserCreateObject(gHandleTable
, NULL
, NULL
, NULL
, TYPE_KBDFILE
, sizeof(KBDFILE
));
142 ERR("Failed to create object!\n");
146 /* Set keyboard layout name */
147 swprintf(pkf
->awchKF
, L
"%wZ", pwszKLID
);
149 /* Open layout registry key */
150 RtlStringCbCatW(wszLayoutRegKey
, sizeof(wszLayoutRegKey
), pkf
->awchKF
);
151 Status
= RegOpenKey(wszLayoutRegKey
, &hKey
);
152 if (!NT_SUCCESS(Status
))
154 ERR("Failed to open keyboard layouts registry key %ws (%lx)\n", wszLayoutRegKey
, Status
);
158 /* Read filename of layout DLL */
159 cbSize
= sizeof(wszLayoutPath
) - wcslen(wszLayoutPath
)*sizeof(WCHAR
);
160 Status
= RegQueryValue(hKey
,
163 wszLayoutPath
+ wcslen(wszLayoutPath
),
166 if (!NT_SUCCESS(Status
))
168 ERR("Can't get layout filename for %wZ (%lx)\n", pwszKLID
, Status
);
172 /* Load keyboard file now */
173 if (!UserLoadKbdDll(wszLayoutPath
, &pkf
->hBase
, &pkf
->pKbdTbl
))
175 ERR("Failed to load %ws dll!\n", wszLayoutPath
);
179 /* Update next field */
180 pkf
->pkfNext
= gpkfList
;
183 /* Return keyboard file */
190 UserDereferenceObject(pkf
); // we dont need ptr anymore
193 /* We have failed - destroy created object */
195 UserDeleteObject(pkf
->head
.h
, TYPE_KBDFILE
);
204 * Loads keyboard layout and creates KL object
207 UserLoadKbdLayout(PUNICODE_STRING pustrKLID
, HKL hKL
)
213 /* Create keyboard layout object */
214 pKl
= UserCreateObject(gHandleTable
, NULL
, NULL
, NULL
, TYPE_KBDLAYOUT
, sizeof(KL
));
217 ERR("Failed to create object!\n");
222 pKl
->spkf
= UserLoadKbdFile(pustrKLID
);
224 /* Dereference keyboard layout */
225 UserDereferenceObject(pKl
);
227 /* If we failed, remove KL object */
230 ERR("UserLoadKbdFile(%wZ) failed!\n", pustrKLID
);
231 UserDeleteObject(pKl
->head
.h
, TYPE_KBDLAYOUT
);
235 // Up to Language Identifiers..
236 if (!NT_SUCCESS(RtlUnicodeStringToInteger(pustrKLID
, 16, (PULONG
)&lCid
)))
238 ERR("RtlUnicodeStringToInteger failed for '%wZ'\n", pustrKLID
);
239 UserDeleteObject(pKl
->head
.h
, TYPE_KBDLAYOUT
);
243 TRACE("Language Identifiers %wZ LCID 0x%x\n", pustrKLID
, lCid
);
244 if (co_IntGetCharsetInfo(lCid
, &cs
))
246 pKl
->iBaseCharset
= cs
.ciCharset
;
247 pKl
->dwFontSigs
= cs
.fs
.fsCsb
[0];
248 pKl
->CodePage
= (USHORT
)cs
.ciACP
;
249 TRACE("Charset %u Font Sig %lu CodePage %u\n",
250 pKl
->iBaseCharset
, pKl
->dwFontSigs
, pKl
->CodePage
);
254 pKl
->iBaseCharset
= ANSI_CHARSET
;
255 pKl
->dwFontSigs
= FS_LATIN1
;
256 pKl
->CodePage
= CP_ACP
;
259 // Set initial system character set and font signature.
262 gSystemCPCharSet
= pKl
->iBaseCharset
;
263 gSystemFS
= pKl
->dwFontSigs
;
272 * Destroys specified Keyboard File object
276 UnloadKbdFile(_In_ PKBDFILE pkf
)
278 PKBDFILE
*ppkfLink
= &gpkfList
;
279 NT_ASSERT(pkf
!= NULL
);
281 /* Find previous object */
284 if (*ppkfLink
== pkf
)
287 ppkfLink
= &(*ppkfLink
)->pkfNext
;
290 if (*ppkfLink
== pkf
)
291 *ppkfLink
= pkf
->pkfNext
;
293 EngUnloadImage(pkf
->hBase
);
294 UserDeleteObject(pkf
->head
.h
, TYPE_KBDFILE
);
300 * Unloads specified Keyboard Layout if possible
303 UserUnloadKbl(PKL pKl
)
305 /* According to msdn, UnloadKeyboardLayout can fail
306 if the keyboard layout identifier was preloaded. */
307 if (pKl
== gspklBaseLayout
)
309 if (pKl
->pklNext
== pKl
->pklPrev
)
311 /* There is only one layout */
315 /* Set next layout as default */
316 gspklBaseLayout
= pKl
->pklNext
;
319 if (pKl
->head
.cLockObj
> 1)
321 /* Layout is used by other threads */
322 pKl
->dwKL_Flags
|= KLF_UNLOAD
;
326 /* Unload the layout */
327 pKl
->pklPrev
->pklNext
= pKl
->pklNext
;
328 pKl
->pklNext
->pklPrev
= pKl
->pklPrev
;
329 UnloadKbdFile(pKl
->spkf
);
330 UserDeleteObject(pKl
->head
.h
, TYPE_KBDLAYOUT
);
335 * W32kGetDefaultKeyLayout
337 * Returns default layout for new threads
340 W32kGetDefaultKeyLayout(VOID
)
342 PKL pKl
= gspklBaseLayout
;
347 /* Return not unloaded layout */
350 if (!(pKl
->dwKL_Flags
& KLF_UNLOAD
))
353 pKl
= pKl
->pklPrev
; /* Confirmed on Win2k */
354 } while(pKl
!= gspklBaseLayout
);
356 /* We have not found proper KL */
363 * Gets KL object from hkl value
367 UserHklToKbl(HKL hKl
)
369 PKL pKl
= gspklBaseLayout
;
371 if (!gspklBaseLayout
)
380 } while (pKl
!= gspklBaseLayout
);
386 * UserSetDefaultInputLang
388 * Sets default kyboard layout for system. Called from UserSystemParametersInfo.
392 UserSetDefaultInputLang(HKL hKl
)
396 pKl
= UserHklToKbl(hKl
);
400 gspklBaseLayout
= pKl
;
407 * Activates given layout in specified thread
410 co_UserActivateKbl(PTHREADINFO pti
, PKL pKl
, UINT Flags
)
415 pklPrev
= pti
->KeyboardLayout
;
417 UserDereferenceObject(pklPrev
);
419 pti
->KeyboardLayout
= pKl
;
420 pti
->pClientInfo
->hKL
= pKl
->hkl
;
421 UserReferenceObject(pKl
);
423 if (Flags
& KLF_SETFORPROCESS
)
428 if (!(pWnd
= pti
->MessageQueue
->spwndFocus
))
430 pWnd
= pti
->MessageQueue
->spwndActive
;
433 // Send WM_INPUTLANGCHANGE to thread's focus window
434 co_IntSendMessage( pWnd
? UserHMGetHandle(pWnd
) : 0,
436 (WPARAM
)pKl
->iBaseCharset
, // FIXME: How to set it?
437 (LPARAM
)pKl
->hkl
); // hkl
442 /* EXPORTS *******************************************************************/
445 * UserGetKeyboardLayout
447 * Returns hkl of given thread keyboard layout
450 UserGetKeyboardLayout(
454 PLIST_ENTRY ListEntry
;
457 pti
= PsGetCurrentThreadWin32Thread();
461 pKl
= pti
->KeyboardLayout
;
462 return pKl
? pKl
->hkl
: NULL
;
465 ListEntry
= pti
->rpdesk
->PtiList
.Flink
;
468 // Search the Desktop Thread list for related Desktop active Threads.
470 while(ListEntry
!= &pti
->rpdesk
->PtiList
)
472 pti
= CONTAINING_RECORD(ListEntry
, THREADINFO
, PtiLink
);
474 if (PsGetThreadId(pti
->pEThread
) == UlongToHandle(dwThreadId
))
476 pKl
= pti
->KeyboardLayout
;
477 return pKl
? pKl
->hkl
: NULL
;
480 ListEntry
= ListEntry
->Flink
;
487 * NtUserGetKeyboardLayoutList
489 * Returns list of loaded keyboard layouts in system
493 NtUserGetKeyboardLayoutList(
505 if (!gspklBaseLayout
)
510 pKl
= gspklBaseLayout
;
518 } while (pKl
!= gspklBaseLayout
);
524 ProbeForWrite(pHklBuff
, nBuff
*sizeof(HKL
), 4);
528 pHklBuff
[uRet
] = pKl
->hkl
;
531 if (pKl
== gspklBaseLayout
)
535 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
537 SetLastNtError(_SEH2_GetExceptionCode());
548 * NtUserGetKeyboardLayoutName
550 * Returns KLID of current thread keyboard layout
554 NtUserGetKeyboardLayoutName(
563 pti
= PsGetCurrentThreadWin32Thread();
564 pKl
= pti
->KeyboardLayout
;
571 ProbeForWrite(pwszName
, KL_NAMELENGTH
*sizeof(WCHAR
), 1);
572 wcscpy(pwszName
, pKl
->spkf
->awchKF
);
575 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
577 SetLastNtError(_SEH2_GetExceptionCode());
587 * NtUserLoadKeyboardLayoutEx
589 * Loads keyboard layout with given locale id
593 NtUserLoadKeyboardLayoutEx(
594 IN HANDLE Handle
, // hFile (See downloads.securityfocus.com/vulnerabilities/exploits/43774.c)
595 IN DWORD offTable
, // Offset to KbdTables
596 IN PUNICODE_STRING puszKeyboardName
, // Not used?
598 IN PUNICODE_STRING pustrKLID
,
603 PKL pKl
= NULL
, pklLast
;
605 UNICODE_STRING ustrSafeKLID
;
607 if (Flags
& ~(KLF_ACTIVATE
|KLF_NOTELLSHELL
|KLF_REORDER
|KLF_REPLACELANG
|
608 KLF_SUBSTITUTE_OK
|KLF_SETFORPROCESS
|KLF_UNLOADPREVIOUS
|
609 KLF_RESET
|KLF_SHIFTLOCK
))
611 ERR("Invalid flags: %x\n", Flags
);
612 EngSetLastError(ERROR_INVALID_FLAGS
);
616 /* FIXME: It seems KLF_RESET is only supported for WINLOGON */
618 RtlInitEmptyUnicodeString(&ustrSafeKLID
, Buffer
, sizeof(Buffer
));
621 ProbeForRead(pustrKLID
, sizeof(*pustrKLID
), 1);
622 ProbeForRead(pustrKLID
->Buffer
, sizeof(pustrKLID
->Length
), 1);
623 RtlCopyUnicodeString(&ustrSafeKLID
, pustrKLID
);
625 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
627 SetLastNtError(_SEH2_GetExceptionCode());
628 _SEH2_YIELD(return NULL
);
632 UserEnterExclusive();
634 /* If hklUnload is specified, unload it and load new layput as default */
635 if (hklUnload
&& hklUnload
!= (HKL
)hkl
)
637 pKl
= UserHklToKbl(hklUnload
);
642 /* Let's see if layout was already loaded. */
643 pKl
= UserHklToKbl((HKL
)hkl
);
646 /* It wasn't, so load it. */
647 pKl
= UserLoadKbdLayout(&ustrSafeKLID
, (HKL
)hkl
);
653 /* Find last not unloaded layout */
654 pklLast
= gspklBaseLayout
->pklPrev
;
655 while (pklLast
!= gspklBaseLayout
&& pklLast
->dwKL_Flags
& KLF_UNLOAD
)
656 pklLast
= pklLast
->pklPrev
;
658 /* Add new layout to the list */
659 pKl
->pklNext
= pklLast
->pklNext
;
660 pKl
->pklPrev
= pklLast
;
661 pKl
->pklNext
->pklPrev
= pKl
;
662 pKl
->pklPrev
->pklNext
= pKl
;
666 /* This is the first layout */
669 gspklBaseLayout
= pKl
;
673 /* If this layout was prepared to unload, undo it */
674 pKl
->dwKL_Flags
&= ~KLF_UNLOAD
;
676 /* Activate this layout in current thread */
677 if (Flags
& KLF_ACTIVATE
)
678 co_UserActivateKbl(PsGetCurrentThreadWin32Thread(), pKl
, Flags
);
680 /* Send shell message */
681 if (!(Flags
& KLF_NOTELLSHELL
))
682 co_IntShellHookNotify(HSHELL_LANGUAGE
, 0, (LPARAM
)hkl
);
684 /* Return hkl on success */
687 /* FIXME: KLF_REPLACELANG
696 * NtUserActivateKeyboardLayout
698 * Activates specified layout for thread or process
702 NtUserActivateKeyboardLayout(
710 UserEnterExclusive();
712 pti
= PsGetCurrentThreadWin32Thread();
714 /* hKl can have special value HKL_NEXT or HKL_PREV */
715 if (hKl
== (HKL
)HKL_NEXT
)
717 /* Get next keyboard layout starting with current */
718 if (pti
->KeyboardLayout
)
719 pKl
= pti
->KeyboardLayout
->pklNext
;
721 else if (hKl
== (HKL
)HKL_PREV
)
723 /* Get previous keyboard layout starting with current */
724 if (pti
->KeyboardLayout
)
725 pKl
= pti
->KeyboardLayout
->pklNext
;
728 pKl
= UserHklToKbl(hKl
);
732 ERR("Invalid HKL %p!\n", hKl
);
741 if (Flags
& KLF_REORDER
)
742 gspklBaseLayout
= pKl
;
744 if (pKl
!= pti
->KeyboardLayout
)
746 /* Activate layout for current thread */
747 pKl
= co_UserActivateKbl(pti
, pKl
, Flags
);
749 /* Send shell message */
750 if (!(Flags
& KLF_NOTELLSHELL
))
751 co_IntShellHookNotify(HSHELL_LANGUAGE
, 0, (LPARAM
)hkl
);
760 * NtUserUnloadKeyboardLayout
762 * Unloads keyboard layout with specified hkl value
766 NtUserUnloadKeyboardLayout(
772 UserEnterExclusive();
774 pKl
= UserHklToKbl(hKl
);
776 bRet
= UserUnloadKbl(pKl
);
778 ERR("Invalid HKL %p!\n", hKl
);