Skip to content

Commit d45b9a6

Browse files
committed
fix: install input source
usable, but with issues switching imes before log out.
1 parent 277ebfa commit d45b9a6

File tree

3 files changed

+89
-21
lines changed

3 files changed

+89
-21
lines changed

input_source.m

+61-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99
#define HANS_INPUT_MODE (1 << 0)
1010
#define HANT_INPUT_MODE (1 << 1)
1111

12+
#define DEFAULT_INPUT_MODE HANS_INPUT_MODE
13+
14+
int GetEnabledInputModes(void);
15+
1216
void RegisterInputSource(void) {
17+
int enabled_input_modes = GetEnabledInputModes();
18+
if (enabled_input_modes) {
19+
// Already registered.
20+
return;
21+
}
1322
CFURLRef installedLocationURL = CFURLCreateFromFileSystemRepresentation(
1423
NULL, (UInt8*)kInstallLocation, (CFIndex)strlen(kInstallLocation), false);
1524
if (installedLocationURL) {
@@ -19,7 +28,14 @@ void RegisterInputSource(void) {
1928
}
2029
}
2130

22-
void ActivateInputSource(int enabled_modes) {
31+
void EnableInputSource(void) {
32+
int enabled_input_modes = GetEnabledInputModes();
33+
if (enabled_input_modes) {
34+
// keep user's manually enabled input modes.
35+
return;
36+
}
37+
// neither is enabled, enable the default input mode.
38+
int input_modes_to_enable = DEFAULT_INPUT_MODE;
2339
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
2440
for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) {
2541
TISInputSourceRef inputSource =
@@ -28,23 +44,61 @@ void ActivateInputSource(int enabled_modes) {
2844
inputSource, kTISPropertyInputSourceID);
2945
// NSLog(@"Examining input source: %@", sourceID);
3046
if ((!CFStringCompare(sourceID, kHansInputModeID, 0) &&
31-
((enabled_modes & HANS_INPUT_MODE) != 0)) ||
47+
((input_modes_to_enable & HANS_INPUT_MODE) != 0)) ||
3248
(!CFStringCompare(sourceID, kHantInputModeID, 0) &&
33-
((enabled_modes & HANT_INPUT_MODE) != 0))) {
34-
TISEnableInputSource(inputSource);
35-
NSLog(@"Enabled input source: %@", sourceID);
49+
((input_modes_to_enable & HANT_INPUT_MODE) != 0))) {
50+
CFBooleanRef isEnabled = (CFBooleanRef)TISGetInputSourceProperty(
51+
inputSource, kTISPropertyInputSourceIsEnabled);
52+
if (!CFBooleanGetValue(isEnabled)) {
53+
TISEnableInputSource(inputSource);
54+
NSLog(@"Enabled input source: %@", sourceID);
55+
}
56+
}
57+
}
58+
CFRelease(sourceList);
59+
}
60+
61+
void SelectInputSource(void) {
62+
int enabled_input_modes = GetEnabledInputModes();
63+
int input_modes_to_select =
64+
((enabled_input_modes & DEFAULT_INPUT_MODE) != 0)
65+
? DEFAULT_INPUT_MODE : enabled_input_modes;
66+
if (!input_modes_to_select) {
67+
NSLog(@"No enabled input sources.");
68+
return;
69+
}
70+
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
71+
for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) {
72+
TISInputSourceRef inputSource =
73+
(TISInputSourceRef)CFArrayGetValueAtIndex(sourceList, i);
74+
CFStringRef sourceID = (CFStringRef)TISGetInputSourceProperty(
75+
inputSource, kTISPropertyInputSourceID);
76+
// NSLog(@"Examining input source: %@", sourceID);
77+
if ((!CFStringCompare(sourceID, kHansInputModeID, 0) &&
78+
((input_modes_to_select & HANS_INPUT_MODE) != 0)) ||
79+
(!CFStringCompare(sourceID, kHantInputModeID, 0) &&
80+
((input_modes_to_select & HANT_INPUT_MODE) != 0))) {
81+
// select the first enabled input mode in Squirrel.
82+
CFBooleanRef isEnabled = (CFBooleanRef)TISGetInputSourceProperty(
83+
inputSource, kTISPropertyInputSourceIsEnabled);
84+
if (!CFBooleanGetValue(isEnabled)) {
85+
continue;
86+
}
3687
CFBooleanRef isSelectable = (CFBooleanRef)TISGetInputSourceProperty(
3788
inputSource, kTISPropertyInputSourceIsSelectCapable);
38-
if (CFBooleanGetValue(isSelectable)) {
89+
CFBooleanRef isSelected = (CFBooleanRef)TISGetInputSourceProperty(
90+
inputSource, kTISPropertyInputSourceIsSelected);
91+
if (!CFBooleanGetValue(isSelected) && CFBooleanGetValue(isSelectable)) {
3992
TISSelectInputSource(inputSource);
4093
NSLog(@"Selected input source: %@", sourceID);
4194
}
95+
break;
4296
}
4397
}
4498
CFRelease(sourceList);
4599
}
46100

47-
void DeactivateInputSource(void) {
101+
void DisableInputSource(void) {
48102
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
49103
for (CFIndex i = CFArrayGetCount(sourceList); i > 0; --i) {
50104
TISInputSourceRef inputSource =

main.m

+20-10
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
#import <string.h>
77

88
void RegisterInputSource(void);
9-
int GetEnabledInputModes(void);
10-
void DeactivateInputSource(void);
11-
void ActivateInputSource(int input_modes);
12-
13-
#define DEFAULT_INPUT_MODE 1
9+
void DisableInputSource(void);
10+
void EnableInputSource(void);
11+
void SelectInputSource(void);
1412

1513
// Each input method needs a unique connection name.
1614
// Note that periods and spaces are not allowed in the connection name.
@@ -34,12 +32,24 @@ int main(int argc, char* argv[]) {
3432
return 0;
3533
}
3634

37-
if (argc > 1 && !strcmp("--install", argv[1])) {
38-
// register and enable Squirrel
35+
if (argc > 1 && (!strcmp("--register-input-source", argv[1]) ||
36+
!strcmp("--install", argv[1]))) {
3937
RegisterInputSource();
40-
int input_modes = GetEnabledInputModes();
41-
DeactivateInputSource();
42-
ActivateInputSource(input_modes ?: DEFAULT_INPUT_MODE);
38+
return 0;
39+
}
40+
41+
if (argc > 1 && !strcmp("--enable-input-source", argv[1])) {
42+
EnableInputSource();
43+
return 0;
44+
}
45+
46+
if (argc > 1 && !strcmp("--disable-input-source", argv[1])) {
47+
DisableInputSource();
48+
return 0;
49+
}
50+
51+
if (argc > 1 && !strcmp("--select-input-source", argv[1])) {
52+
SelectInputSource();
4353
return 0;
4454
}
4555

scripts/postinstall

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
#!/bin/bash
2+
set -e
23

34
login_user=`/usr/bin/stat -f%Su /dev/console`
45
squirrel_app_root="${DSTROOT}/Squirrel.app"
56
squirrel_executable="${squirrel_app_root}/Contents/MacOS/Squirrel"
67
rime_package_installer="${squirrel_app_root}/Contents/MacOS/rime-install"
78
rime_shared_data_path="${squirrel_app_root}/Contents/SharedSupport"
89

9-
/usr/bin/sudo -u "${login_user}" /usr/bin/killall Squirrel > /dev/null
10+
/usr/bin/sudo -u "${login_user}" /usr/bin/killall Squirrel > /dev/null || true
11+
12+
"${squirrel_executable}" --register-input-source
1013

1114
if [ -z "${RIME_NO_PREBUILD}" ]; then
1215
pushd "${rime_shared_data_path}" > /dev/null
1316
"${squirrel_executable}" --build
1417
popd > /dev/null
15-
fi
16-
17-
/usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --install
18+
fi && (
19+
/usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --enable-input-source
20+
/usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --select-input-source
21+
)

0 commit comments

Comments
 (0)