Skip to content

Commit

Permalink
fix: 修复安装后无法启用输入法的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
marchyang committed Sep 16, 2023
1 parent b4f3598 commit a18d07d
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 121 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ DerivedData/
!default.mode2v3
*.perspectivev3
!default.perspectivev3
/Fire.xcodeproj/xcuserdata
# xcode构建有bug,需要把runner.xcuserdatad目录提交上来才,github action才能正常构建
/Fire.xcodeproj/project.xcworkspace/xcuserdata/*
!runner.xcuserdatad
Expand Down
12 changes: 7 additions & 5 deletions Fire.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,10 @@
675341A02AB54A3A00757F76 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Mac Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
"DEVELOPMENT_TEAM[sdk=macosx*]" = T68XK6867P;
ENABLE_HARDENED_RUNTIME = YES;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -544,7 +546,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 13.3;
MACOSX_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -598,7 +600,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 13.3;
MACOSX_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = macosx;
Expand All @@ -613,7 +615,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = Fire/Fire.entitlements;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
Expand Down Expand Up @@ -643,7 +645,7 @@
"-DSQLCIPHER_CRYPTO_CC",
"-DNDEBUG",
);
PRODUCT_BUNDLE_IDENTIFIER = cn.qwertyyb.inputmethod.Fire;
PRODUCT_BUNDLE_IDENTIFIER = com.qwertyyb.inputmethod.Fire;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = NO;
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down Expand Up @@ -686,7 +688,7 @@
"-DSQLCIPHER_CRYPTO_CC",
"-DNDEBUG",
);
PRODUCT_BUNDLE_IDENTIFIER = cn.qwertyyb.inputmethod.Fire;
PRODUCT_BUNDLE_IDENTIFIER = com.qwertyyb.inputmethod.Fire;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SKIP_INSTALL = NO;
Expand Down
92 changes: 92 additions & 0 deletions Fire.xcodeproj/xcshareddata/xcschemes/install.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "67C99FE72AB53DB2000B5281"
BuildableName = "Fire.app"
BlueprintName = "Fire"
ReferencedContainer = "container:Fire.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<PreActions>
<ExecutionAction
ActionType = "Xcode.IDEStandardExecutionActionsCore.ExecutionActionType.ShellScriptAction">
<ActionContent
title = "Run Script"
scriptText = "# Type a script or drag a script file from your workspace to insert its path.&#10;pkill -9 Fire&#10;">
</ActionContent>
</ExecutionAction>
</PreActions>
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "67C99FE72AB53DB2000B5281"
BuildableName = "Fire.app"
BlueprintName = "Fire"
ReferencedContainer = "container:Fire.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "--install"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "67C99FE72AB53DB2000B5281"
BuildableName = "Fire.app"
BlueprintName = "Fire"
ReferencedContainer = "container:Fire.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
76 changes: 33 additions & 43 deletions Fire/InputSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ enum InputSourceUsage {

class InputSource {
let installLocation = "/Library/Input Methods/Fire.app"
let kSourceID = "com.qwertyyb.inputmethod.Fire"
let kInputModeID = "com.qwertyyb.inputmethod.Fire"
let kSourceID = Bundle.main.bundleIdentifier!

func registerInputSource() {
if !isEnabled() {
Expand All @@ -28,39 +27,30 @@ class InputSource {
}
}

private func transformTargetSource(_ inputSource: TISInputSource)
-> (inputSource: TISInputSource, sourceID: NSString)? {
let ptr = TISGetInputSourceProperty(inputSource, kTISPropertyInputSourceID)
let sourceID = Unmanaged<CFString>.fromOpaque(ptr!).takeUnretainedValue() as NSString
if (sourceID.isEqual(to: kSourceID) ) || sourceID.isEqual(to: kInputModeID) {
return (inputSource, sourceID)
}
return nil
}

private func findInputSource(forUsage: InputSourceUsage = .enable)
-> (inputSource: TISInputSource, sourceID: NSString)? {
let sourceList = TISCreateInputSourceList(nil, true).takeRetainedValue() as NSArray
-> TISInputSource? {
let conditions = NSMutableDictionary()
conditions.setValue(kSourceID, forKey: kTISPropertyInputSourceID as String)
guard let sourceList = TISCreateInputSourceList(conditions, true)?.takeRetainedValue() as? [TISInputSource] else {
return nil
}

for index in 0..<sourceList.count {
let inputSource = Unmanaged<TISInputSource>.fromOpaque(CFArrayGetValueAtIndex(
sourceList, index)).takeUnretainedValue()
if let result = transformTargetSource(inputSource) {
let selectable = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(result.inputSource, kTISPropertyInputSourceIsSelectCapable)
).takeUnretainedValue())
let enableable = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(result.inputSource, kTISPropertyInputSourceIsEnableCapable)
).takeUnretainedValue())
if forUsage == .enable && enableable {
return result
}
if forUsage == .selected && selectable {
return result
}
if selectable {
return result
}
let inputSource = sourceList[index]
let selectable = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(inputSource, kTISPropertyInputSourceIsSelectCapable)
).takeUnretainedValue())
let enableable = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(inputSource, kTISPropertyInputSourceIsEnableCapable)
).takeUnretainedValue())
if forUsage == .enable && enableable {
return inputSource
}
if forUsage == .selected && selectable {
return inputSource
}
if selectable {
return inputSource
}
}
return nil
Expand All @@ -79,11 +69,11 @@ class InputSource {
guard let result = self.findInputSource(forUsage: .selected) else {
return
}
TISSelectInputSource(result.inputSource)
TISSelectInputSource(result)
let isSelected = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(result.inputSource, kTISPropertyInputSourceIsSelected)
TISGetInputSourceProperty(result, kTISPropertyInputSourceIsSelected)
).takeUnretainedValue())
NSLog("Selected input source: %@, selected: \(isSelected)", result.sourceID)
NSLog("Selected input source")
if isSelected {
timer.invalidate()
callback(true)
Expand All @@ -96,21 +86,21 @@ class InputSource {
return
}
let enabled = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(
TISGetInputSourceProperty(result.inputSource, kTISPropertyInputSourceIsEnabled)
TISGetInputSourceProperty(result, kTISPropertyInputSourceIsEnabled)
).takeUnretainedValue())
if !enabled {
TISEnableInputSource(result.inputSource)
NSLog("Enabled input source: %@", result.sourceID)
TISEnableInputSource(result)
NSLog("Enabled input source")
}
}

func deactivateInputSource() {
guard let source = findInputSource() else {
return
}
TISDeselectInputSource(source.inputSource)
TISDisableInputSource(source.inputSource)
NSLog("Disable input source: %@", source.sourceID)
TISDeselectInputSource(source)
TISDisableInputSource(source)
NSLog("Disable input source")
}

func onSelectChanged(callback: @escaping (Bool) -> Void) -> NSObjectProtocol {
Expand All @@ -131,7 +121,7 @@ class InputSource {
return false
}
let unsafeIsSelected = TISGetInputSourceProperty(
result.inputSource,
result,
kTISPropertyInputSourceIsSelected
).assumingMemoryBound(to: CFBoolean.self)
let isSelected = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(unsafeIsSelected).takeUnretainedValue())
Expand All @@ -144,7 +134,7 @@ class InputSource {
return false
}
let unsafeIsEnabled = TISGetInputSourceProperty(
result.inputSource,
result,
kTISPropertyInputSourceIsEnabled
).assumingMemoryBound(to: CFBoolean.self)
let isEnabled = CFBooleanGetValue(Unmanaged<CFBoolean>.fromOpaque(unsafeIsEnabled).takeUnretainedValue())
Expand Down
29 changes: 0 additions & 29 deletions Podfile

This file was deleted.

44 changes: 0 additions & 44 deletions Podfile.lock

This file was deleted.

0 comments on commit a18d07d

Please sign in to comment.