-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cc
More file actions
365 lines (318 loc) · 9.6 KB
/
main.cc
File metadata and controls
365 lines (318 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#define UNICODE
#include <Windows.h>
#include <UIAutomation.h>
#include <iostream>
#include <string>
#include <comip.h>
#include <comdef.h>
#include <vector>
#include <regex>
#include <WinUser.h>
#include <cctype>
#include <algorithm>
#include <locale>
using namespace std;
typedef _com_ptr_t<_com_IIID<IUIAutomation, &__uuidof(IUIAutomation)>> IUIAutomationPtr;
typedef _com_ptr_t<_com_IIID<IUIAutomationElement, &__uuidof(IUIAutomationElement)>> IUIAutomationElementPtr;
typedef _com_ptr_t<_com_IIID<IUIAutomationCondition, &__uuidof(IUIAutomationCondition)>> IUIAutomationConditionPtr;
typedef _com_ptr_t<_com_IIID<IUIAutomationElementArray, &__uuidof(IUIAutomationElementArray)>> IUIAutomationElementArrayPtr;
typedef _com_ptr_t<_com_IIID<IUIAutomationInvokePattern, &__uuidof(IUIAutomationInvokePattern)>> IUIAutomationInvokePatternPtr;
// command line options
struct CliOptions
{
// no prefix
wstring mode;
// -k=
wstring switch_keys;
// -t=
wstring taskbar_name;
// -i=
wstring ime_capture_re;
// --toolbar=
wstring toolbar_name;
// --toolbar_i=
wstring toolbar_ime_capture_re;
// -v
bool verbose;
wregex ime_capture;
wregex toolbar_ime_capture;
};
// ime button in taskbar
struct ImeButton
{
wstring current_mode;
IUIAutomationElementPtr pElement;
};
wstring get_element_name(IUIAutomationElementPtr pElement)
{
_bstr_t name;
pElement->get_CurrentName(name.GetAddress());
if (name.length() > 0)
{
return wstring((const wchar_t*)name);
}
else
{
return L"";
}
}
vector<wstring> split_string(const wstring & str, const wstring & delim)
{
vector<string> result;
wregex re(delim);
wsregex_token_iterator first{ str.begin(), str.end(), re, -1 }, last;
return { first, last };
}
SHORT vk_from_text(const wstring & text) {
if (text == L"shift") {
return VK_SHIFT;
}
if (text == L"ctrl") {
return VK_CONTROL;
}
if (text == L"alt") {
return VK_MENU;
}
if (text == L"win") {
return VK_LWIN;
}
if (text == L"space") {
return VK_SPACE;
}
// Check for hexadecimal format (0xhh)
// such that 0x1F means VK_MODECHANGE
if (text.length() == 4 && text.substr(0, 2) == L"0x") {
try {
return static_cast<SHORT>(std::stoi(text.substr(2), nullptr, 16));
}
catch (...) {
}
}
return 0;
}
vector<INPUT> get_input_from_string(wstring str)
{
vector<INPUT> result;
transform(str.begin(), str.end(), str.begin(), ::tolower);
auto keys = split_string(str, L"\\+");
transform(keys.begin(), keys.end(), back_insert_iterator(result), [](const wstring & key) {
INPUT input = { 0 };
input.type = INPUT_KEYBOARD;
input.ki.wVk = vk_from_text(key);
return input;
});
transform(keys.rbegin(), keys.rend(), back_insert_iterator(result), [](const wstring & key) {
INPUT input = { 0 };
input.type = INPUT_KEYBOARD;
input.ki.wVk = vk_from_text(key);
input.ki.dwFlags = KEYEVENTF_KEYUP;
return input;
});
return result;
}
ImeButton get_ime_button(const CliOptions & options) {
IUIAutomationPtr pAutomation;
IUIAutomationElementPtr pDesktop;
IUIAutomationElementPtr pTaskBar;
IUIAutomationConditionPtr pCondition;
auto hr = pAutomation.CreateInstance(CLSID_CUIAutomation);
pAutomation->GetRootElement(&pDesktop);
pAutomation->CreatePropertyCondition(UIA_NamePropertyId, _variant_t(options.taskbar_name.c_str()), &pCondition);
hr = pDesktop->FindFirst(TreeScope_Children, pCondition, &pTaskBar);
pAutomation->CreatePropertyCondition(UIA_ControlTypePropertyId, _variant_t(UIA_ButtonControlTypeId), &pCondition);
if (options.verbose) wcout << L"found taskbar: " << options.taskbar_name << endl;
IUIAutomationElementArrayPtr arrButtons;
pTaskBar->FindAll(TreeScope_Descendants, pCondition, &arrButtons);
int length = 0;
arrButtons->get_Length(&length);
for (int i = 0; i < length; i++)
{
IUIAutomationElementPtr pButton;
arrButtons->GetElement(i, &pButton);
auto name = get_element_name(pButton);
if (options.verbose) wcout << L"Is '" << name << L"' ime button? ";
wsmatch match;
if (regex_search(name, match, options.ime_capture)) {
if (options.verbose) wcout << L"YES" << endl;
return { match[1], pButton };
}
if (options.verbose) wcout << L"NO" << endl;
}
return { L"", nullptr };
}
/**
* Look for the mode switch button in the IME toolbar
* Suitable as a fallback solution when the tray input indicator is set to auto-hide along with the taskbar
* Requires the IME toolbar to be enabled first, and ensure the "中/英文" button is displayed in the toolbar
*/
ImeButton get_ime_button_from_toolbar(const CliOptions &options) {
IUIAutomationPtr pAutomation;
IUIAutomationElementPtr pDesktop;
IUIAutomationElementPtr pInputPanel;
IUIAutomationConditionPtr pCondition;
IUIAutomationElementArrayPtr arrButtons;
// UI Layout Structure:
// Element: Windows 输入体验
// Element: 简体中文工具栏菜单列表
// Element: 中/英文, 英语模式
// Element: 设置
pAutomation.CreateInstance(CLSID_CUIAutomation);
pAutomation->GetRootElement(&pDesktop);
pAutomation->CreatePropertyCondition(UIA_NamePropertyId, _variant_t(options.toolbar_name.c_str()), &pCondition);
pDesktop->FindFirst(TreeScope_Descendants, pCondition, &pInputPanel);
if (pInputPanel == nullptr)
{
return { L"", nullptr };
}
if (options.verbose) wcout << L"found toolbar: " << options.toolbar_name << endl;
pAutomation->CreatePropertyCondition(UIA_ControlTypePropertyId, _variant_t(UIA_ListItemControlTypeId), &pCondition);
pInputPanel->FindAll(TreeScope_Descendants, pCondition, &arrButtons);
int length = 0;
arrButtons->get_Length(&length);
for (int i = 0; i < length; i++)
{
IUIAutomationElementPtr pButton;
arrButtons->GetElement(i, &pButton);
auto name = get_element_name(pButton);
if (options.verbose) wcout << L"Is '" << name << L"' ime button?";
if (wsmatch match; regex_search(name, match, options.toolbar_ime_capture))
{
if (options.verbose) wcout << L"YES" << endl;
return {match[1], pButton};
}
if (options.verbose) wcout << L"NO" << endl;
}
return {L"", nullptr};
}
// default chinese options
CliOptions chinese_options()
{
CliOptions options;
options.taskbar_name = L"任务栏";
options.ime_capture_re = L"托盘输入指示器\\s+(\\w+)"; //\\s+(\\S+)\\s*.+";
options.switch_keys = L"shift";
options.toolbar_name = L"Windows 输入体验";
options.toolbar_ime_capture_re = L"中/英文, (\\w+)";
options.verbose = false;
return options;
}
// parse command line options
CliOptions parse_options(int argc, wchar_t * argv[])
{
CliOptions options = chinese_options();
for (int i = 1; i < argc; i++)
{
auto arg = argv[i];
if (arg[0] == L'-')
{
auto pos = wcschr(arg, L'=');
if (pos)
{
auto key = wstring(arg + 1, pos);
auto value = wstring(pos + 1);
if (key == L"k")
{
options.switch_keys = value;
}
else if (key == L"t")
{
options.taskbar_name = value;
}
else if (key == L"i")
{
options.ime_capture_re = value;
}
else if (key == L"-toolbar")
{
options.toolbar_name = value;
}
else if (key == L"-toolbar-i")
{
options.toolbar_ime_capture_re = value;
}
}
if (wcscmp(arg, L"-v") == 0)
{
options.verbose = true;
}
}
else
{
options.mode = arg;
}
}
if (options.ime_capture_re.length() > 0)
{
options.ime_capture = wregex(options.ime_capture_re);
}
if (options.toolbar_ime_capture_re.length() > 0)
{
options.toolbar_ime_capture = wregex(options.toolbar_ime_capture_re);
}
return options;
}
void print_options(const CliOptions & options)
{
wcout << L"taskbar name(-t): " << options.taskbar_name << endl;
wcout << L"ime capture(-i): " << options.ime_capture_re << endl;
wcout << L"switch keys(-k): " << options.switch_keys << endl;
wcout << L"toolbar name(--toolbar): " << options.toolbar_name << endl;
wcout << L"toolbar ime capture(--toolbar-i): " << options.toolbar_ime_capture_re << endl;
wcout << L"mode: " << options.mode << endl;
}
string w2utf8(const wstring& str)
{
string buf;
buf.resize(str.size() * 4);
auto n = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, buf.data(), (int)buf.length(), NULL, NULL);
if (n == 0) {
return "";
}
buf.resize(n-1);
return buf;
}
int wmain(int argc, wchar_t * argv[])
{
std::ios::sync_with_stdio(false);
std::locale::global( std::locale("") );
auto options = parse_options(argc, argv);
//print_options(options);
CoInitialize(NULL);
ImeButton ime_button;
try {
ime_button = get_ime_button(options);
}
catch (_com_error& e)
{
wcout << L"get ime button failed: " << e.ErrorMessage() << endl;
wcout << L"maybe the taskbar name (-t) is not correct" << endl;
print_options(options);
return 1;
}
if (!ime_button.pElement) {
// Perhaps the taskbar is hidden. Try finding the toggle button in the toolbar.
ime_button = get_ime_button_from_toolbar(options);
}
if (!ime_button.pElement)
{
wcout << L"ime button not found, maybe the ime_capture (-i) can't match the ime button name " << endl;
print_options(options);
return 1;
}
if (options.mode.empty())
{
// get current mode
cout << w2utf8(ime_button.current_mode) << endl;
}
else
{
// do switch
if (options.mode != ime_button.current_mode)
{
auto input = get_input_from_string(options.switch_keys);
SendInput((UINT)input.size(), input.data(), sizeof(input[0]));
}
}
CoUninitialize();
return 0;
}