forked from rime/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_source.m
79 lines (74 loc) · 3.05 KB
/
input_source.m
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
#import <Carbon/Carbon.h>
static const unsigned char kInstalledLocation[] =
"/Library/Input Methods/Squirrel.app";
static NSString *const kSourceID =
@"com.googlecode.rimeime.inputmethod.Squirrel";
static NSString *const kInputModeID =
@"com.googlecode.rimeime.inputmethod.Squirrel.Rime";
void RegisterInputSource() {
NSLog(@"RegisterInputSource.");
CFURLRef installedLocationURL = CFURLCreateFromFileSystemRepresentation(
NULL, kInstalledLocation, strlen((const char *)kInstalledLocation), NO);
if (installedLocationURL) {
TISRegisterInputSource(installedLocationURL);
}
}
void ActivateInputSource() {
NSLog(@"ActivateInputSource.");
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
for (int i = 0; i < CFArrayGetCount(sourceList); ++i) {
TISInputSourceRef inputSource = (TISInputSourceRef)(CFArrayGetValueAtIndex(
sourceList, i));
NSString *sourceID = (NSString *)(TISGetInputSourceProperty(
inputSource, kTISPropertyInputSourceID));
//NSLog(@"examining input source '%@", sourceID);
if ([sourceID isEqualToString:kSourceID] ||
[sourceID isEqualToString:kInputModeID]) {
TISEnableInputSource(inputSource);
CFBooleanRef isSelectable = (CFBooleanRef)TISGetInputSourceProperty(
inputSource, kTISPropertyInputSourceIsSelectCapable);
if (CFBooleanGetValue(isSelectable)) {
NSLog(@"selecting input source '%@'.", sourceID);
TISSelectInputSource(inputSource);
}
NSLog(@"'%@' should have been activated.", sourceID);
}
}
}
void DeactivateInputSource() {
NSLog(@"DeactivateInputSource.");
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
for (int i = CFArrayGetCount(sourceList); i > 0; --i) {
TISInputSourceRef inputSource = (TISInputSourceRef)(CFArrayGetValueAtIndex(
sourceList, i - 1));
NSString *sourceID = (NSString *)(TISGetInputSourceProperty(
inputSource, kTISPropertyInputSourceID));
//NSLog(@"examining input source '%@", sourceID);
if ([sourceID isEqualToString:kSourceID] ||
[sourceID isEqualToString:kInputModeID]) {
TISDisableInputSource(inputSource);
NSLog(@"'%@' should have been deactivated.", sourceID);
}
}
}
BOOL IsInputSourceActive() {
int active = 0;
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
for (int i = 0; i < CFArrayGetCount(sourceList); ++i) {
TISInputSourceRef inputSource = (TISInputSourceRef)(CFArrayGetValueAtIndex(
sourceList, i));
NSString *sourceID = (NSString *)(TISGetInputSourceProperty(
inputSource, kTISPropertyInputSourceID));
NSLog(@"examining input source '%@'", sourceID);
if ([sourceID isEqualToString:kSourceID] ||
[sourceID isEqualToString:kInputModeID]) {
CFBooleanRef isEnabled = (CFBooleanRef)(TISGetInputSourceProperty(
inputSource, kTISPropertyInputSourceIsEnabled));
if (CFBooleanGetValue(isEnabled)) {
++active;
}
}
}
NSLog(@"IsInputSourceActive: %d / 2", active);
return active == 2; // 1 active input method + 1 active input mode
}