"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/Hotkey.cpp" (10 Oct 2018, 6976 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Hotkey.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #include "System.h"
   14 #include "Application.h"
   15 #include "LanguageStrings.h"
   16 #include "GraphicUserInterface.h"
   17 #include "Hotkey.h"
   18 #include "Xml.h"
   19 
   20 namespace VeraCrypt
   21 {
   22     HotkeyList Hotkey::GetAvailableHotkeys ()
   23     {
   24         HotkeyList hotkeys;
   25 #ifdef TC_WINDOWS
   26 
   27 #define TC_HOTKEY(ID,LANG) hotkeys.push_back (shared_ptr <Hotkey> (new Hotkey (Id::##ID, L###ID, LangString[LANG])))
   28 
   29         TC_HOTKEY (CloseAllSecurityTokenSessions, "IDM_CLOSE_ALL_TOKEN_SESSIONS");
   30         TC_HOTKEY (DismountAll, "HK_DISMOUNT_ALL");
   31         TC_HOTKEY (DismountAllWipeCache, "HK_DISMOUNT_ALL_AND_WIPE");
   32         TC_HOTKEY (ForceDismountAllWipeCache, "HK_FORCE_DISMOUNT_ALL_AND_WIPE");
   33         TC_HOTKEY (ForceDismountAllWipeCacheExit, "HK_FORCE_DISMOUNT_ALL_AND_WIPE_AND_EXIT");
   34         TC_HOTKEY (MountAllDevices, "HK_AUTOMOUNT_DEVICES");
   35         TC_HOTKEY (MountAllFavorites, "HK_MOUNT_FAVORITE_VOLUMES");
   36         TC_HOTKEY (ShowHideApplication, "HK_SHOW_HIDE_MAIN_WINDOW");
   37         TC_HOTKEY (WipeCache, "HK_WIPE_CACHE");
   38 
   39 #endif
   40         return hotkeys;
   41     }
   42 
   43     wxString Hotkey::GetShortcutString () const
   44     {
   45         wxString keyStr = Hotkey::GetVirtualKeyCodeString (VirtualKeyCode);
   46         if (keyStr.empty())
   47             return L"";
   48 
   49         wxString str;
   50 
   51         if (VirtualKeyModifiers & wxMOD_SHIFT)
   52             str += LangString["VK_SHIFT"] + L"+";
   53 
   54         if (VirtualKeyModifiers & wxMOD_CONTROL)
   55             str += LangString["VK_CONTROL"] + L"+";
   56 
   57         if (VirtualKeyModifiers & wxMOD_ALT)
   58             str += LangString["VK_ALT"] + L"+";
   59 
   60         if (VirtualKeyModifiers & wxMOD_WIN )
   61             str += LangString["VK_WIN"] + L"+";
   62 
   63         return str + keyStr;
   64     }
   65 
   66     wxString Hotkey::GetVirtualKeyCodeString (int virtualKeyCode)
   67     {
   68 #ifdef TC_WINDOWS
   69         // ASCII characters
   70         if (virtualKeyCode >= 0x30 && virtualKeyCode <= 0x5a)
   71             return StringFormatter (L"{0}", char (virtualKeyCode));
   72 
   73         // OEM-specific
   74         if (virtualKeyCode >= 0xE9 && virtualKeyCode <= 0xF5)
   75             return StringFormatter (L"OEM-{0}", virtualKeyCode);
   76 
   77         // F1-F24
   78         if (virtualKeyCode >= VK_F1 && virtualKeyCode <= VK_F24)
   79             return StringFormatter (L"F{0}", virtualKeyCode - VK_F1 + 1);
   80 
   81         // Numpad numbers
   82         if (virtualKeyCode >= VK_NUMPAD0 && virtualKeyCode <= VK_NUMPAD9)
   83             return StringFormatter (L"{0} {1}", LangString["VK_NUMPAD"], virtualKeyCode - VK_NUMPAD0);
   84 
   85         switch (virtualKeyCode)
   86         {
   87         case VK_MULTIPLY:   return LangString["VK_NUMPAD"] + L" *";
   88         case VK_ADD:        return LangString["VK_NUMPAD"] + L" +";
   89         case VK_SEPARATOR:  return LangString["VK_NUMPAD"] + L" Separator";
   90         case VK_SUBTRACT:   return LangString["VK_NUMPAD"] + L" -";
   91         case VK_DECIMAL:    return LangString["VK_NUMPAD"] + L" .";
   92         case VK_DIVIDE:     return LangString["VK_NUMPAD"] + L" /";
   93         case VK_OEM_1:      return L"OEM 1 (';')";
   94         case VK_OEM_PLUS:   return L"+";
   95         case VK_OEM_COMMA:  return L",";
   96         case VK_OEM_MINUS:  return L"-";
   97         case VK_OEM_PERIOD: return L".";
   98         case VK_OEM_2:      return L"OEM 2 ('/')";
   99         case VK_OEM_3:      return L"OEM 3 (`)";
  100         case VK_OEM_4:      return L"OEM 4 ('[')";
  101         case VK_OEM_5:      return L"OEM 5 ('\\')";
  102         case VK_OEM_6:      return L"OEM 6 (']')";
  103         case VK_OEM_7:      return L"OEM 7 (')";
  104         case VK_OEM_8:      return L"OEM 8";
  105         case VK_OEM_AX:     return L"OEM AX";
  106         case VK_OEM_102:    return L"OEM 102";
  107         case VK_ICO_HELP:   return L"ICO_HELP";
  108         case VK_ICO_00:     return L"ICO_00";
  109         case VK_ICO_CLEAR:  return L"ICO_CLEAR";
  110         case VK_ATTN:       return L"Attn";
  111         case VK_CRSEL:      return L"CrSel";
  112         case VK_EXSEL:      return L"ExSel";
  113         case VK_EREOF:      return L"Erase EOF";
  114         case VK_PA1:        return L"PA1";
  115         case VK_OEM_CLEAR:  return L"OEM Clear";
  116 
  117         case 0:
  118         case 1:
  119         case 0xFF:
  120             break;
  121 
  122         default:
  123             {
  124                 string langStrId = StringConverter::ToSingle (wstring (wxString::Format (L"VKEY_%02X", virtualKeyCode)));
  125                 if (LangString.Exists (langStrId))
  126                     return LangString[langStrId];
  127             }
  128         }
  129 #endif // TC_WINDOWS
  130         return L"";
  131     }
  132 
  133     HotkeyList Hotkey::LoadList ()
  134     {
  135         HotkeyList hotkeys = GetAvailableHotkeys();
  136 
  137         FilePath path = Application::GetConfigFilePath (GetFileName());
  138         if (path.IsFile())
  139         {
  140             foreach (XmlNode node, XmlParser (path).GetNodes (L"hotkey"))
  141             {
  142                 wstring keyName (node.Attributes[L"name"]);
  143 
  144                 foreach (shared_ptr <Hotkey> hotkey, hotkeys)
  145                 {
  146                     if (hotkey->Name == keyName)
  147                     {
  148                         hotkey->VirtualKeyCode = StringConverter::ToUInt32 (wstring (node.Attributes[L"vkeycode"]));
  149                         hotkey->VirtualKeyModifiers = 0;
  150 
  151                         if (node.Attributes[L"modshift"] == L"1")
  152                             hotkey->VirtualKeyModifiers |= wxMOD_SHIFT;
  153 
  154                         if (node.Attributes[L"modcontrol"] == L"1")
  155                             hotkey->VirtualKeyModifiers |= wxMOD_CONTROL;
  156 
  157                         if (node.Attributes[L"modalt"] == L"1")
  158                             hotkey->VirtualKeyModifiers |= wxMOD_ALT;
  159 
  160                         if (node.Attributes[L"modwin"] == L"1")
  161                             hotkey->VirtualKeyModifiers |= wxMOD_WIN;
  162 
  163                         break;
  164                     }
  165                 }
  166             }
  167         }
  168 
  169         return hotkeys;
  170     }
  171 
  172     void Hotkey::RegisterList (wxWindow *handler, const HotkeyList &hotkeys)
  173     {
  174 #ifdef TC_WINDOWS
  175         bool res = true;
  176         foreach (shared_ptr <Hotkey> hotkey, hotkeys)
  177         {
  178             if (hotkey->VirtualKeyCode != 0)
  179             {
  180                 if (!handler->RegisterHotKey (hotkey->Id, hotkey->VirtualKeyModifiers, hotkey->VirtualKeyCode))
  181                     res = false;
  182             }
  183         }
  184 
  185         if (!res)
  186             Gui->ShowWarning ("HOTKEY_REGISTRATION_ERROR");
  187 #endif
  188     }
  189 
  190     void Hotkey::SaveList (const HotkeyList &hotkeys)
  191     {
  192         FilePath hotkeysCfgPath = Application::GetConfigFilePath (GetFileName(), true);
  193 
  194         bool noHotkey = true;
  195         XmlNode hotkeysXml (L"hotkeys");
  196         foreach_ref (const Hotkey &hotkey, hotkeys)
  197         {
  198             if (hotkey.VirtualKeyCode == 0)
  199                 continue;
  200 
  201             noHotkey = false;
  202             XmlNode node (L"hotkey");
  203             node.Attributes[L"name"] = wstring (hotkey.Name);
  204 
  205             node.Attributes[L"vkeycode"] = StringConverter::FromNumber (hotkey.VirtualKeyCode);
  206 
  207             if (hotkey.VirtualKeyModifiers & wxMOD_SHIFT)
  208                 node.Attributes[L"modshift"] = L"1";
  209 
  210             if (hotkey.VirtualKeyModifiers & wxMOD_CONTROL)
  211                 node.Attributes[L"modcontrol"] = L"1";
  212 
  213             if (hotkey.VirtualKeyModifiers & wxMOD_ALT)
  214                 node.Attributes[L"modalt"] = L"1";
  215 
  216             if (hotkey.VirtualKeyModifiers & wxMOD_WIN )
  217                 node.Attributes[L"modwin"] = L"1";
  218 
  219             hotkeysXml.InnerNodes.push_back (node);
  220         }
  221 
  222         if (noHotkey)
  223         {
  224             if (hotkeysCfgPath.IsFile())
  225                 hotkeysCfgPath.Delete();
  226         }
  227         else
  228         {
  229             XmlWriter hotkeysWriter (hotkeysCfgPath);
  230             hotkeysWriter.WriteNode (hotkeysXml);
  231             hotkeysWriter.Close();
  232         }
  233     }
  234 
  235     void Hotkey::UnregisterList (wxWindow *handler, const HotkeyList &hotkeys)
  236     {
  237 #ifdef TC_WINDOWS
  238         foreach (shared_ptr <Hotkey> hotkey, hotkeys)
  239         {
  240             if (hotkey->VirtualKeyCode != 0)
  241                 handler->UnregisterHotKey (hotkey->Id);
  242         }
  243 #endif
  244     }
  245 }