From b99ffa7be18c29fe0107a3e9d562bc830f40d703 Mon Sep 17 00:00:00 2001 From: RaymondLim Date: Thu, 23 Oct 2014 11:25:52 -0700 Subject: [PATCH] Add two new APIs for currently active keyboard layout --- appshell/appshell_extensions.js | 22 ++++++++++++++++++++ appshell/client_app.cpp | 4 ++++ appshell/client_app.h | 2 ++ appshell/client_app_gtk.cpp | 13 ++++++++++++ appshell/client_app_mac.mm | 13 ++++++++++++ appshell/client_app_win.cpp | 36 ++++++++++++++++++++++++++++----- 6 files changed, 85 insertions(+), 5 deletions(-) diff --git a/appshell/appshell_extensions.js b/appshell/appshell_extensions.js index efc6aa6ae..74d162cc8 100644 --- a/appshell/appshell_extensions.js +++ b/appshell/appshell_extensions.js @@ -747,6 +747,28 @@ if (!appshell.app) { configurable : false }); + /** + * Return the user's keyboard layout language. + */ + native function GetCurrentKeyboardLayout(); + Object.defineProperty(appshell.app, "keyboard", { + writeable: false, + get : function() { return GetCurrentKeyboardLayout(); }, + enumerable : true, + configurable : false + }); + + /** + * Return the user's keyboard layout device type. + */ + native function GetKeyboardType(); + Object.defineProperty(appshell.app, "keyboardType", { + writeable: false, + get : function() { return GetKeyboardType(); }, + enumerable : true, + configurable : false + }); + /** * Returns the full path of the application support directory. * On the Mac, it's /Users//Library/Application Support[/GROUP_NAME]/APP_NAME diff --git a/appshell/client_app.cpp b/appshell/client_app.cpp index d79c4c05f..eb88e00e1 100644 --- a/appshell/client_app.cpp +++ b/appshell/client_app.cpp @@ -153,6 +153,10 @@ class AppShellExtensionHandler : public CefV8Handler { retval = CefV8Value::CreateDouble(client_app_->GetElapsedMilliseconds()); } else if (name == "GetCurrentLanguage") { retval = CefV8Value::CreateString(client_app_->GetCurrentLanguage()); + } else if (name == "GetCurrentKeyboardLayout") { + retval = CefV8Value::CreateString(client_app_->GetCurrentKeyboardLayout()); + } else if (name == "GetKeyboardType") { + retval = CefV8Value::CreateString(client_app_->GetKeyboardType()); } else if (name == "GetApplicationSupportDirectory") { retval = CefV8Value::CreateString(ClientApp::AppGetSupportDirectory()); } else if (name == "GetUserDocumentsDirectory") { diff --git a/appshell/client_app.h b/appshell/client_app.h index 3388211fc..6013de84e 100644 --- a/appshell/client_app.h +++ b/appshell/client_app.h @@ -90,6 +90,8 @@ class ClientApp : public CefApp, // Platform-specific methods implemented in client_app_mac/client_app_win double GetElapsedMilliseconds(); CefString GetCurrentLanguage(); + CefString GetCurrentKeyboardLayout(); + CefString GetKeyboardType(); std::string GetExtensionJSSource(); static CefString AppGetSupportDirectory(); static CefString AppGetDocumentsDirectory(); diff --git a/appshell/client_app_gtk.cpp b/appshell/client_app_gtk.cpp index a4173b903..aeabf1268 100644 --- a/appshell/client_app_gtk.cpp +++ b/appshell/client_app_gtk.cpp @@ -49,6 +49,19 @@ CefString ClientApp::GetCurrentLanguage() return CefString(loc); } +CefString ClientApp::GetCurrentKeyboardLayout() +{ + // TODO: Get the user's active keyboard layout language + // Returning the UI language for now + return GetCurrentLanguage(); +} + +CefString ClientApp::GetKeyboardType() +{ + // This API may not be needed for Linux. So just return an empty string for now. + return CefString(""); +} + std::string ClientApp::GetExtensionJSSource() { //# We objcopy the appshell/appshell_extensions.js file, and link it directly into the binary. diff --git a/appshell/client_app_mac.mm b/appshell/client_app_mac.mm index 51f20d290..c35ec619c 100644 --- a/appshell/client_app_mac.mm +++ b/appshell/client_app_mac.mm @@ -55,6 +55,19 @@ return result; } +CefString ClientApp::GetCurrentKeyboardLayout() +{ + // TODO: Get the user's active keyboard layout language + // Returning the UI language for now + return GetCurrentLanguage(); +} + +CefString ClientApp::GetKeyboardType() +{ + // This API is used for Windows only. So just return an empty string on Mac. + return CefString(@""); +} + std::string ClientApp::GetExtensionJSSource() { std::string result; diff --git a/appshell/client_app_win.cpp b/appshell/client_app_win.cpp index f55a1fa33..5d7756e26 100644 --- a/appshell/client_app_win.cpp +++ b/appshell/client_app_win.cpp @@ -30,15 +30,12 @@ #include #include #include +#include extern DWORD g_appStartupTime; -CefString ClientApp::GetCurrentLanguage() +CefString GetLanguageFromId(LANGID langID) { - // Get the user's selected language - // Defaults to the system installed language if not using MUI. - LANGID langID = GetUserDefaultUILanguage(); - // Convert LANGID to a RFC 4646 language tag (per navigator.language) int langSize = GetLocaleInfo(langID, LOCALE_SISO639LANGNAME, NULL, 0); int countrySize = GetLocaleInfo(langID, LOCALE_SISO3166CTRYNAME, NULL, 0); @@ -59,6 +56,35 @@ CefString ClientApp::GetCurrentLanguage() return CefString(locale); } +CefString ClientApp::GetCurrentLanguage() +{ + // Get the user's selected language + // Defaults to the system installed language if not using MUI. + LANGID langID = GetUserDefaultUILanguage(); + return GetLanguageFromId(langID); +} + + +CefString ClientApp::GetCurrentKeyboardLayout() +{ + // Get the user's active keyboard layout language + int kbd = (int)GetKeyboardLayout(0); + LANGID langID = MAKELANGID(kbd & 0xFFFF, kbd & 0xFFFF0000); + return GetLanguageFromId(langID); +} + +CefString ClientApp::GetKeyboardType() +{ + // Get the user's active keyboard device type + int keyboardLayout = ((int)GetKeyboardLayout(0) & 0xFFFF0000) >> 16; + wchar_t *type = new wchar_t[10]; + int len = swprintf_s(type, 10, L"%d", keyboardLayout); + std::wstring keyboardType(type); + + delete [] type; + return CefString(keyboardType); +} + std::string ClientApp::GetExtensionJSSource() { extern HINSTANCE hInst;