-
Notifications
You must be signed in to change notification settings - Fork 11
/
input_source_controller.cpp
158 lines (126 loc) · 4.56 KB
/
input_source_controller.cpp
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
#include <Carbon/Carbon.h>
#include <CoreFoundation/CoreFoundation.h>
#include <iostream>
#include <sstream>
#include "formatters.h"
#include "input_source_controller.h"
#include "utils.h"
InputSourceController::InputSourceController() {
}
void
InputSourceController::showSelected() const {
TISInputSourceRef is = TISCopyCurrentKeyboardInputSource();
std::cout << InputSourceFormatter(is) << std::endl;
CFRelease(is);
}
void
InputSourceController::listAvailable() const {
CFDictionaryWrap filter;
filter.set(kTISPropertyInputSourceIsEnabled, kCFBooleanTrue);
filter.set(kTISPropertyInputSourceIsSelectCapable, kCFBooleanTrue);
CFArrayRef sourceList = TISCreateInputSourceList(filter.cfDict(), false);
if (sourceList == NULL)
return;
CFIndex cnt = CFArrayGetCount(sourceList);
for (CFIndex i = 0; i < cnt; ++i) {
TISInputSourceRef inputSource = (TISInputSourceRef)CFArrayGetValueAtIndex(sourceList, i);
std::cout << InputSourceFormatter(inputSource) << std::endl;
}
CFRelease(sourceList);
}
void
InputSourceController::select(const std::string& isId) const {
TISInputSourceRef is = findInputSource(isId);
if (is != NULL) {
TISSelectInputSource(is);
CFRelease(is);
}
else {
throw program_error(std::string("Error: Cant find input source with id=") + isId);
}
}
TISInputSourceRef
InputSourceController::findInputSource(const std::string& name) const {
TISInputSourceRef is = findInputSourceById(name);
if (is == NULL) {
is = findInputSourceByLang(name);
}
return is;
}
TISInputSourceRef
InputSourceController::findInputSourceById(const std::string& isId) const {
CFStringWrap isIdStr(isId);
CFDictionaryWrap filter;
filter.set(kTISPropertyInputSourceID, isIdStr.cfString());
TISInputSourceRef is = NULL;
CFArrayRef sourceList = TISCreateInputSourceList(filter.cfDict(), false);
if (sourceList != NULL && CFArrayGetCount(sourceList) == 1) {
is = (TISInputSourceRef)CFArrayGetValueAtIndex(sourceList, 0);
CFRetain(is);
CFRelease(sourceList);
}
return is;
}
TISInputSourceRef
InputSourceController::findInputSourceByLang(const std::string& lang) const {
CFDictionaryWrap filter;
filter.set(kTISPropertyInputSourceIsEnabled, kCFBooleanTrue);
filter.set(kTISPropertyInputSourceIsSelectCapable, kCFBooleanTrue);
CFArrayRef sourceList = TISCreateInputSourceList(filter.cfDict(), false);
if (sourceList == NULL)
return NULL;
CFStringWrap requiredLangName(lang);
TISInputSourceRef is = NULL;
CFIndex cnt = CFArrayGetCount(sourceList);
for (CFIndex i = 0; i < cnt; ++i) {
TISInputSourceRef inputSource = (TISInputSourceRef)CFArrayGetValueAtIndex(sourceList, i);
CFArrayRef inputSourceLanguages = (CFArrayRef)TISGetInputSourceProperty(inputSource, kTISPropertyInputSourceLanguages);
CFIndex inputSourceLanguagesCount = CFArrayGetCount(inputSourceLanguages);
for (CFIndex langIdx = 0; langIdx < inputSourceLanguagesCount; ++langIdx) {
CFStringRef langName = (CFStringRef)CFArrayGetValueAtIndex(inputSourceLanguages, langIdx);
if (CFStringCompare(langName, requiredLangName.cfString(), 0) == kCFCompareEqualTo) {
is = inputSource;
goto found;
}
}
}
found:
if (is) CFRetain(is);
CFRelease(sourceList);
return is;
}
// vim-xkbswitch interface
extern "C" {
static char buffer[1024];
const char* Xkb_Switch_getXkbLayout(const char* /* unused */) {
// Hack to update current input source for programs that do not use standard runloop (MacVim in console mode for ex)
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false);
buffer[0] = '\0';
try {
InputSourceController ctrl;
TISInputSourceRef is = TISCopyCurrentKeyboardInputSource();
std::ostringstream os;
os << InputSourceFormatter(is);
CFRelease(is);
const std::string& name = os.str();
int len = name.length();
if (len < 1024 && len > 0) {
char* after_last = std::copy(name.begin(), name.end(), buffer);
*after_last = '\0';
}
}
catch( ... ) {
}
return buffer;
}
const char* Xkb_Switch_setXkbLayout(const char* newgrp) {
try {
InputSourceController ctrl;
std::string is_id(newgrp);
ctrl.select(is_id);
}
catch( ... ) {
}
return NULL;
}
}