[NDK] Replace the SYSTEMTIME fields StandardDate and DaylightDate in RTL_TIME_ZONE_IN...
[reactos.git] / win32ss / user / ntuser / kbdlayout.c
index a6d20c5..f47ece5 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * PROJECT:         ReactOS Win32k subsystem
  * LICENSE:         GPL - See COPYING in the top level directory
- * FILE:            subsystems/win32/win32k/ntuser/kbdlayout.c
+ * FILE:            win32ss/user/ntuser/kbdlayout.c
  * PURPOSE:         Keyboard layout management
  * COPYRIGHT:       Copyright 2007 Saveliy Tretiakov
  *                  Copyright 2008 Colin Finck
@@ -9,10 +9,18 @@
  */
 
 #include <win32k.h>
+
+// Was included only because of CP_ACP and required  the
+// definition of SYSTEMTIME in ndk\rtltypes.h
+//#include <winnls.h>
+#define CP_ACP 0
+
 DBG_DEFAULT_CHANNEL(UserKbdLayout);
 
 PKL gspklBaseLayout = NULL;
 PKBDFILE gpkfList = NULL;
+DWORD gSystemFS = 0;
+UINT gSystemCPCharSet = 0;
 
 typedef PVOID (*PFN_KBDLAYERDESCRIPTOR)(VOID);
 
@@ -151,7 +159,7 @@ UserLoadKbdFile(PUNICODE_STRING pwszKLID)
     }
 
     /* Read filename of layout DLL */
-    cbSize = sizeof(wszLayoutPath) - wcslen(wszLayoutPath)*sizeof(WCHAR);
+    cbSize = (ULONG)(sizeof(wszLayoutPath) - wcslen(wszLayoutPath)*sizeof(WCHAR));
     Status = RegQueryValue(hKey,
                            L"Layout File",
                            REG_SZ,
@@ -199,7 +207,7 @@ cleanup:
  * Loads keyboard layout and creates KL object
  */
 static PKL
-UserLoadKbdLayout(PUNICODE_STRING pwszKLID, HKL hKL)
+UserLoadKbdLayout(PUNICODE_STRING pustrKLID, HKL hKL)
 {
     LCID lCid;
     CHARSETINFO cs;
@@ -214,7 +222,7 @@ UserLoadKbdLayout(PUNICODE_STRING pwszKLID, HKL hKL)
     }
 
     pKl->hkl = hKL;
-    pKl->spkf = UserLoadKbdFile(pwszKLID);
+    pKl->spkf = UserLoadKbdFile(pustrKLID);
 
     /* Dereference keyboard layout */
     UserDereferenceObject(pKl);
@@ -222,20 +230,27 @@ UserLoadKbdLayout(PUNICODE_STRING pwszKLID, HKL hKL)
     /* If we failed, remove KL object */
     if (!pKl->spkf)
     {
-        ERR("UserLoadKbdFile(%wZ) failed!\n", pwszKLID);
+        ERR("UserLoadKbdFile(%wZ) failed!\n", pustrKLID);
         UserDeleteObject(pKl->head.h, TYPE_KBDLAYOUT);
         return NULL;
     }
 
     // Up to Language Identifiers..
-    RtlUnicodeStringToInteger(pwszKLID, (ULONG)16, (PULONG)&lCid);
-    TRACE("Language Identifiers %wZ LCID 0x%x\n", pwszKLID, lCid);
+    if (!NT_SUCCESS(RtlUnicodeStringToInteger(pustrKLID, 16, (PULONG)&lCid)))
+    {
+        ERR("RtlUnicodeStringToInteger failed for '%wZ'\n", pustrKLID);
+        UserDeleteObject(pKl->head.h, TYPE_KBDLAYOUT);
+        return NULL;
+    }
+
+    TRACE("Language Identifiers %wZ LCID 0x%x\n", pustrKLID, lCid);
     if (co_IntGetCharsetInfo(lCid, &cs))
     {
        pKl->iBaseCharset = cs.ciCharset;
        pKl->dwFontSigs = cs.fs.fsCsb[0];
        pKl->CodePage = (USHORT)cs.ciACP;
-       TRACE("Charset %d Font Sig %d CodePage %d\n", pKl->iBaseCharset, pKl->dwFontSigs, pKl->CodePage);
+       TRACE("Charset %u Font Sig %lu CodePage %u\n",
+             pKl->iBaseCharset, pKl->dwFontSigs, pKl->CodePage);
     }
     else
     {
@@ -244,6 +259,13 @@ UserLoadKbdLayout(PUNICODE_STRING pwszKLID, HKL hKL)
        pKl->CodePage = CP_ACP;
     }
 
+    // Set initial system character set and font signature.
+    if (gSystemFS == 0)
+    {
+       gSystemCPCharSet = pKl->iBaseCharset;
+       gSystemFS = pKl->dwFontSigs;
+    }
+
     return pKl;
 }
 
@@ -391,6 +413,7 @@ static PKL
 co_UserActivateKbl(PTHREADINFO pti, PKL pKl, UINT Flags)
 {
     PKL pklPrev;
+    PWND pWnd;
 
     pklPrev = pti->KeyboardLayout;
     if (pklPrev)
@@ -405,8 +428,13 @@ co_UserActivateKbl(PTHREADINFO pti, PKL pKl, UINT Flags)
         // FIXME
     }
 
+    if (!(pWnd = pti->MessageQueue->spwndFocus))
+    {
+         pWnd = pti->MessageQueue->spwndActive;
+    }
+
     // Send WM_INPUTLANGCHANGE to thread's focus window
-    co_IntSendMessage(pti->MessageQueue->spwndFocus ? UserHMGetHandle(pti->MessageQueue->spwndFocus) : 0,
+    co_IntSendMessage( pWnd ? UserHMGetHandle(pWnd) : 0,
                       WM_INPUTLANGCHANGE,
                       (WPARAM)pKl->iBaseCharset, // FIXME: How to set it?
                       (LPARAM)pKl->hkl); // hkl
@@ -425,31 +453,37 @@ HKL FASTCALL
 UserGetKeyboardLayout(
     DWORD dwThreadId)
 {
-    NTSTATUS Status;
-    PETHREAD pThread;
     PTHREADINFO pti;
+    PLIST_ENTRY ListEntry;
     PKL pKl;
-    HKL hKl;
+
+    pti = PsGetCurrentThreadWin32Thread();
 
     if (!dwThreadId)
     {
-        pti = PsGetCurrentThreadWin32Thread();
         pKl = pti->KeyboardLayout;
         return pKl ? pKl->hkl : NULL;
     }
 
-    Status = PsLookupThreadByThreadId((HANDLE)(DWORD_PTR)dwThreadId, &pThread);
-    if (!NT_SUCCESS(Status))
+    ListEntry = pti->rpdesk->PtiList.Flink;
+
+    //
+    // Search the Desktop Thread list for related Desktop active Threads.
+    //
+    while(ListEntry != &pti->rpdesk->PtiList)
     {
-        EngSetLastError(ERROR_INVALID_PARAMETER);
-        return NULL;
+        pti = CONTAINING_RECORD(ListEntry, THREADINFO, PtiLink);
+
+        if (PsGetThreadId(pti->pEThread) == UlongToHandle(dwThreadId))
+        {
+           pKl = pti->KeyboardLayout;
+           return pKl ? pKl->hkl : NULL;
+        }
+
+        ListEntry = ListEntry->Flink;
     }
 
-    pti = PsGetThreadWin32Thread(pThread);
-    pKl = pti->KeyboardLayout;
-    hKl = pKl ? pKl->hkl : NULL;;
-    ObDereferenceObject(pThread);
-    return hKl;
+    return NULL;
 }
 
 /*
@@ -472,7 +506,10 @@ NtUserGetKeyboardLayoutList(
     UserEnterShared();
 
     if (!gspklBaseLayout)
+    {
+        UserLeave();
         return 0;
+    }
     pKl = gspklBaseLayout;
 
     if (nBuff == 0)
@@ -572,7 +609,7 @@ NtUserLoadKeyboardLayoutEx(
 
     if (Flags & ~(KLF_ACTIVATE|KLF_NOTELLSHELL|KLF_REORDER|KLF_REPLACELANG|
                   KLF_SUBSTITUTE_OK|KLF_SETFORPROCESS|KLF_UNLOADPREVIOUS|
-                  KLF_RESET|KLF_SETFORPROCESS|KLF_SHIFTLOCK))
+                  KLF_RESET|KLF_SHIFTLOCK))
     {
         ERR("Invalid flags: %x\n", Flags);
         EngSetLastError(ERROR_INVALID_FLAGS);
@@ -598,7 +635,7 @@ NtUserLoadKeyboardLayoutEx(
     UserEnterExclusive();
 
     /* If hklUnload is specified, unload it and load new layput as default */
-    if (hklUnload && hklUnload != (HKL)hkl)
+    if (hklUnload && (hklUnload != UlongToHandle(hkl)))
     {
         pKl = UserHklToKbl(hklUnload);
         if (pKl)
@@ -606,11 +643,11 @@ NtUserLoadKeyboardLayoutEx(
     }
 
     /* Let's see if layout was already loaded. */
-    pKl = UserHklToKbl((HKL)hkl);
+    pKl = UserHklToKbl(UlongToHandle(hkl));
     if (!pKl)
     {
         /* It wasn't, so load it. */
-        pKl = UserLoadKbdLayout(&ustrSafeKLID, (HKL)hkl);
+        pKl = UserLoadKbdLayout(&ustrSafeKLID, UlongToHandle(hkl));
         if (!pKl)
             goto cleanup;
 
@@ -648,7 +685,7 @@ NtUserLoadKeyboardLayoutEx(
         co_IntShellHookNotify(HSHELL_LANGUAGE, 0, (LPARAM)hkl);
 
     /* Return hkl on success */
-    hklRet = (HKL)hkl;
+    hklRet = UlongToHandle(hkl);
 
     /* FIXME: KLF_REPLACELANG
               KLF_REORDER */
@@ -688,14 +725,14 @@ NtUserActivateKeyboardLayout(
     {
         /* Get previous keyboard layout starting with current */
         if (pti->KeyboardLayout)
-            pKl = pti->KeyboardLayout->pklNext;
+            pKl = pti->KeyboardLayout->pklPrev;
     }
     else
         pKl = UserHklToKbl(hKl);
 
     if (!pKl)
     {
-        ERR("Invalid HKL %x!\n", hKl);
+        ERR("Invalid HKL %p!\n", hKl);
         goto cleanup;
     }
 
@@ -741,7 +778,7 @@ NtUserUnloadKeyboardLayout(
     if (pKl)
         bRet = UserUnloadKbl(pKl);
     else
-        ERR("Invalid HKL %x!\n", hKl);
+        ERR("Invalid HKL %p!\n", hKl);
 
     UserLeave();
     return bRet;