| 1 |
/* |
| 2 |
* Copyright (c) 2021 Philip Zander |
| 3 |
* Copyright (c) 2018 Microsoft |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
* of this software and associated documentation files (the "Software"), to deal |
| 7 |
* in the Software without restriction, including without limitation the rights |
| 8 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the Software is |
| 10 |
* furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in all |
| 13 |
* copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <glib.h> |
| 25 |
#include <windows.h> |
| 26 |
|
| 27 |
/* For lookup table VK -> chars */ |
| 28 |
typedef struct |
| 29 |
{ |
| 30 |
int table; |
| 31 |
int index; |
| 32 |
} GdkWin32KeymapTableAndIndex; |
| 33 |
|
| 34 |
/* For reverse lookup char -> VKs */ |
| 35 |
typedef struct |
| 36 |
{ |
| 37 |
BYTE mod_bits; |
| 38 |
BYTE vk; |
| 39 |
|
| 40 |
/* Index of next KeyEntry. -1 if there is no next entry. */ |
| 41 |
int next; |
| 42 |
} GdkWin32KeymapKeyEntry; |
| 43 |
|
| 44 |
typedef struct |
| 45 |
{ |
| 46 |
HKL handle; |
| 47 |
|
| 48 |
/* Keyboard layout identifier */ |
| 49 |
char name[KL_NAMELENGTH]; |
| 50 |
|
| 51 |
/* Path of the layout DLL */ |
| 52 |
char *file; |
| 53 |
|
| 54 |
/* Handle of the layout DLL */ |
| 55 |
HINSTANCE lib; |
| 56 |
|
| 57 |
/* The actual conversion tables provided by the layout DLL. |
| 58 |
* |
| 59 |
* This is a pointer to a KBDTABLES structure. The exact definition |
| 60 |
* of this structure depends on the kernel on which the executable |
| 61 |
* run and can in general only be determined at runtime. That's why |
| 62 |
* we have to use a generic gpointer instead of the actual type here. |
| 63 |
* |
| 64 |
* See comment on GdkWin32KeymapImpl below for more information. */ |
| 65 |
gpointer tables; |
| 66 |
|
| 67 |
/* VK -> chars lookup table so we don't have to do a linear scan |
| 68 |
* every time we look up a key. */ |
| 69 |
GdkWin32KeymapTableAndIndex vk_lookup_table[256]; |
| 70 |
|
| 71 |
/* List of entries for reverse (char ->VKs) lookup. */ |
| 72 |
GArray *key_entries; |
| 73 |
|
| 74 |
/* Reverse lookup table (char -> VKs). Key: Unichar. Value: int. |
| 75 |
* The value is used to index into the key_entries array. The key_entries |
| 76 |
* array can contain multiple consecutive entries for a given char. |
| 77 |
* The end of the list for the char is marked by a key entry that has |
| 78 |
* mod_bits and vk set to 0xFF. */ |
| 79 |
GHashTable *reverse_lookup_table; |
| 80 |
|
| 81 |
/* Map level to modbits */ |
| 82 |
BYTE level_to_modbits[256]; |
| 83 |
|
| 84 |
/* Max Number of levels */ |
| 85 |
BYTE max_level; |
| 86 |
|
| 87 |
/* Maximum possible value of a modbits bitset. */ |
| 88 |
BYTE max_modbit_value; |
| 89 |
|
| 90 |
} GdkWin32KeymapLayoutInfo; |
| 91 |
|
| 92 |
/* Some keyboard driver constants |
| 93 |
* See https://github.com/microsoft/windows-rs/blob/0.28.0/crates/deps/sys/src/Windows/Win32/UI/Input/KeyboardAndMouse/mod.rs |
| 94 |
*/ |
| 95 |
|
| 96 |
/* Modifier bits */ |
| 97 |
#define KBDBASE 0x00 |
| 98 |
#define KBDSHIFT 0x01 |
| 99 |
#define KBDCTRL 0x02 |
| 100 |
#define KBDALT 0x04 |
| 101 |
#define KBDKANA 0x08 |
| 102 |
#define KBDROYA 0x10 |
| 103 |
#define KBDLOYA 0x20 |
| 104 |
#define KBDGRPSELTAP 0x80 |
| 105 |
|
| 106 |
#define KBDALTGR (KBDCTRL| KBDALT) |
| 107 |
|
| 108 |
/* */ |
| 109 |
#define SHFT_INVALID 0x0F |
| 110 |
|
| 111 |
/* Char table constants */ |
| 112 |
#define WCH_NONE 0xF000 |
| 113 |
#define WCH_DEAD 0xF001 |
| 114 |
#define WCH_LGTR 0xF002 |
| 115 |
|
| 116 |
/* Char table flags */ |
| 117 |
#define CAPLOK 0x01 |
| 118 |
#define SGCAPS 0x02 |
| 119 |
#define CAPLOKALTGR 0x04 |
| 120 |
#define KANALOK 0x08 |
| 121 |
#define GRPSELTAP 0x80 |
| 122 |
|
| 123 |
/* IMPORTANT: |
| 124 |
* |
| 125 |
* Keyboard layout DLLs are dependent on the host architecture. |
| 126 |
* |
| 127 |
* - 32 bit systems have just one 32 bit DLL in System32. |
| 128 |
* - 64 bit systems contain two versions of each layout DLL: One in System32 |
| 129 |
* for 64-bit applications, and one in SysWOW64 for 32-bit applications. |
| 130 |
* |
| 131 |
* Here comes the tricky part: |
| 132 |
* |
| 133 |
* The 32-bit DLL in SysWOW64 is *not* identical to the DLL you would find |
| 134 |
* on a true 32 bit system, because all the pointers there are declared with |
| 135 |
* the attribute `__ptr64` (which means they are 64 bits wide, but only the |
| 136 |
* lower 32 bits are used). |
| 137 |
* |
| 138 |
* This leads to the following problems: |
| 139 |
* |
| 140 |
* (1) GCC does not support `__ptr64` |
| 141 |
* (2) When compiling the 32-bit library, we need two versions of the same code |
| 142 |
* and decide at run-time which one to execute, because we can't know at |
| 143 |
* compile time whether we will be running on a true 32-bit system, or on |
| 144 |
* WOW64. |
| 145 |
* |
| 146 |
* To solve this problem, we generate code for both cases (see |
| 147 |
* gdkkeys-win32-impl.c + gdkkeys-win32-impl-wow64.c) and encapsulate |
| 148 |
* the resulting functions in a struct of type GdkWin32KeymapImpl, |
| 149 |
* allowing us to select the correct implementation at runtime. |
| 150 |
* |
| 151 |
*/ |
| 152 |
|
| 153 |
typedef struct |
| 154 |
{ |
| 155 |
gboolean (*load_layout_dll) (const char *dll, |
| 156 |
GdkWin32KeymapLayoutInfo *info); |
| 157 |
void (*init_vk_lookup_table) (GdkWin32KeymapLayoutInfo *info); |
| 158 |
BYTE (*keystate_to_modbits) (GdkWin32KeymapLayoutInfo *info, |
| 159 |
const BYTE keystate[256]); |
| 160 |
BYTE (*modbits_to_level) (GdkWin32KeymapLayoutInfo *info, |
| 161 |
BYTE modbits); |
| 162 |
WCHAR (*vk_to_char_fuzzy) (GdkWin32KeymapLayoutInfo *info, |
| 163 |
BYTE mod_bits, |
| 164 |
BYTE lock_bits, |
| 165 |
BYTE *consumed_mod_bits, |
| 166 |
gboolean *is_dead, |
| 167 |
BYTE vk); |
| 168 |
} GdkWin32KeymapImpl; |