Skip to content

Commit

Permalink
fix: install input source
Browse files Browse the repository at this point in the history
usable, but with issues switching imes before log out.
  • Loading branch information
lotem committed Mar 28, 2024
1 parent 277ebfa commit d45b9a6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 21 deletions.
68 changes: 61 additions & 7 deletions input_source.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@
#define HANS_INPUT_MODE (1 << 0)
#define HANT_INPUT_MODE (1 << 1)

#define DEFAULT_INPUT_MODE HANS_INPUT_MODE

int GetEnabledInputModes(void);

void RegisterInputSource(void) {
int enabled_input_modes = GetEnabledInputModes();
if (enabled_input_modes) {
// Already registered.
return;
}
CFURLRef installedLocationURL = CFURLCreateFromFileSystemRepresentation(
NULL, (UInt8*)kInstallLocation, (CFIndex)strlen(kInstallLocation), false);
if (installedLocationURL) {
Expand All @@ -19,7 +28,14 @@ void RegisterInputSource(void) {
}
}

void ActivateInputSource(int enabled_modes) {
void EnableInputSource(void) {
int enabled_input_modes = GetEnabledInputModes();
if (enabled_input_modes) {
// keep user's manually enabled input modes.
return;
}
// neither is enabled, enable the default input mode.
int input_modes_to_enable = DEFAULT_INPUT_MODE;
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) {
TISInputSourceRef inputSource =
Expand All @@ -28,23 +44,61 @@ void ActivateInputSource(int enabled_modes) {
inputSource, kTISPropertyInputSourceID);
// NSLog(@"Examining input source: %@", sourceID);
if ((!CFStringCompare(sourceID, kHansInputModeID, 0) &&
((enabled_modes & HANS_INPUT_MODE) != 0)) ||
((input_modes_to_enable & HANS_INPUT_MODE) != 0)) ||
(!CFStringCompare(sourceID, kHantInputModeID, 0) &&
((enabled_modes & HANT_INPUT_MODE) != 0))) {
TISEnableInputSource(inputSource);
NSLog(@"Enabled input source: %@", sourceID);
((input_modes_to_enable & HANT_INPUT_MODE) != 0))) {
CFBooleanRef isEnabled = (CFBooleanRef)TISGetInputSourceProperty(
inputSource, kTISPropertyInputSourceIsEnabled);
if (!CFBooleanGetValue(isEnabled)) {
TISEnableInputSource(inputSource);
NSLog(@"Enabled input source: %@", sourceID);
}
}
}
CFRelease(sourceList);
}

void SelectInputSource(void) {
int enabled_input_modes = GetEnabledInputModes();
int input_modes_to_select =
((enabled_input_modes & DEFAULT_INPUT_MODE) != 0)
? DEFAULT_INPUT_MODE : enabled_input_modes;
if (!input_modes_to_select) {
NSLog(@"No enabled input sources.");
return;
}
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
for (CFIndex i = 0; i < CFArrayGetCount(sourceList); ++i) {
TISInputSourceRef inputSource =
(TISInputSourceRef)CFArrayGetValueAtIndex(sourceList, i);
CFStringRef sourceID = (CFStringRef)TISGetInputSourceProperty(
inputSource, kTISPropertyInputSourceID);
// NSLog(@"Examining input source: %@", sourceID);
if ((!CFStringCompare(sourceID, kHansInputModeID, 0) &&
((input_modes_to_select & HANS_INPUT_MODE) != 0)) ||
(!CFStringCompare(sourceID, kHantInputModeID, 0) &&
((input_modes_to_select & HANT_INPUT_MODE) != 0))) {
// select the first enabled input mode in Squirrel.
CFBooleanRef isEnabled = (CFBooleanRef)TISGetInputSourceProperty(
inputSource, kTISPropertyInputSourceIsEnabled);
if (!CFBooleanGetValue(isEnabled)) {
continue;
}
CFBooleanRef isSelectable = (CFBooleanRef)TISGetInputSourceProperty(
inputSource, kTISPropertyInputSourceIsSelectCapable);
if (CFBooleanGetValue(isSelectable)) {
CFBooleanRef isSelected = (CFBooleanRef)TISGetInputSourceProperty(
inputSource, kTISPropertyInputSourceIsSelected);
if (!CFBooleanGetValue(isSelected) && CFBooleanGetValue(isSelectable)) {
TISSelectInputSource(inputSource);
NSLog(@"Selected input source: %@", sourceID);
}
break;
}
}
CFRelease(sourceList);
}

void DeactivateInputSource(void) {
void DisableInputSource(void) {
CFArrayRef sourceList = TISCreateInputSourceList(NULL, true);
for (CFIndex i = CFArrayGetCount(sourceList); i > 0; --i) {
TISInputSourceRef inputSource =
Expand Down
30 changes: 20 additions & 10 deletions main.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
#import <string.h>

void RegisterInputSource(void);
int GetEnabledInputModes(void);
void DeactivateInputSource(void);
void ActivateInputSource(int input_modes);

#define DEFAULT_INPUT_MODE 1
void DisableInputSource(void);
void EnableInputSource(void);
void SelectInputSource(void);

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

if (argc > 1 && !strcmp("--install", argv[1])) {
// register and enable Squirrel
if (argc > 1 && (!strcmp("--register-input-source", argv[1]) ||
!strcmp("--install", argv[1]))) {
RegisterInputSource();
int input_modes = GetEnabledInputModes();
DeactivateInputSource();
ActivateInputSource(input_modes ?: DEFAULT_INPUT_MODE);
return 0;
}

if (argc > 1 && !strcmp("--enable-input-source", argv[1])) {
EnableInputSource();
return 0;
}

if (argc > 1 && !strcmp("--disable-input-source", argv[1])) {
DisableInputSource();
return 0;
}

if (argc > 1 && !strcmp("--select-input-source", argv[1])) {
SelectInputSource();
return 0;
}

Expand Down
12 changes: 8 additions & 4 deletions scripts/postinstall
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#!/bin/bash
set -e

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

/usr/bin/sudo -u "${login_user}" /usr/bin/killall Squirrel > /dev/null
/usr/bin/sudo -u "${login_user}" /usr/bin/killall Squirrel > /dev/null || true

"${squirrel_executable}" --register-input-source

if [ -z "${RIME_NO_PREBUILD}" ]; then
pushd "${rime_shared_data_path}" > /dev/null
"${squirrel_executable}" --build
popd > /dev/null
fi

/usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --install
fi && (
/usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --enable-input-source
/usr/bin/sudo -u "${login_user}" "${squirrel_executable}" --select-input-source
)

0 comments on commit d45b9a6

Please sign in to comment.