Converts OVInputSourceHelper to Swift.

This commit is contained in:
zonble 2022-01-10 17:20:18 +08:00
parent 84849bdb3d
commit ba6889fa63
6 changed files with 165 additions and 194 deletions

View File

@ -12,7 +12,6 @@
6A0D4ED015FC0D6400ABF4B3 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EC415FC0D6400ABF4B3 /* AppDelegate.m */; }; 6A0D4ED015FC0D6400ABF4B3 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EC415FC0D6400ABF4B3 /* AppDelegate.m */; };
6A0D4ED215FC0D6400ABF4B3 /* InputMethodController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EC715FC0D6400ABF4B3 /* InputMethodController.mm */; }; 6A0D4ED215FC0D6400ABF4B3 /* InputMethodController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EC715FC0D6400ABF4B3 /* InputMethodController.mm */; };
6A0D4ED315FC0D6400ABF4B3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EC815FC0D6400ABF4B3 /* main.m */; }; 6A0D4ED315FC0D6400ABF4B3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EC815FC0D6400ABF4B3 /* main.m */; };
6A0D4ED415FC0D6400ABF4B3 /* OVInputSourceHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4ECA15FC0D6400ABF4B3 /* OVInputSourceHelper.m */; };
6A0D4EFE15FC0DA600ABF4B3 /* VTCandidateController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EDA15FC0DA600ABF4B3 /* VTCandidateController.m */; }; 6A0D4EFE15FC0DA600ABF4B3 /* VTCandidateController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EDA15FC0DA600ABF4B3 /* VTCandidateController.m */; };
6A0D4EFF15FC0DA600ABF4B3 /* VTHorizontalCandidateController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EDC15FC0DA600ABF4B3 /* VTHorizontalCandidateController.m */; }; 6A0D4EFF15FC0DA600ABF4B3 /* VTHorizontalCandidateController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EDC15FC0DA600ABF4B3 /* VTHorizontalCandidateController.m */; };
6A0D4F0015FC0DA600ABF4B3 /* VTHorizontalCandidateView.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EDE15FC0DA600ABF4B3 /* VTHorizontalCandidateView.m */; }; 6A0D4F0015FC0DA600ABF4B3 /* VTHorizontalCandidateView.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EDE15FC0DA600ABF4B3 /* VTHorizontalCandidateView.m */; };
@ -32,7 +31,6 @@
6A2E40F9253A6AA000D1AE1D /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6A2E40F5253A69DA00D1AE1D /* Images.xcassets */; }; 6A2E40F9253A6AA000D1AE1D /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6A2E40F5253A69DA00D1AE1D /* Images.xcassets */; };
6A38BC1515FC117A00A8A51F /* data.txt in Resources */ = {isa = PBXBuildFile; fileRef = 6A38BBF615FC117A00A8A51F /* data.txt */; }; 6A38BC1515FC117A00A8A51F /* data.txt in Resources */ = {isa = PBXBuildFile; fileRef = 6A38BBF615FC117A00A8A51F /* data.txt */; };
6A38BC2815FC158A00A8A51F /* InputMethodKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A38BC2715FC158A00A8A51F /* InputMethodKit.framework */; }; 6A38BC2815FC158A00A8A51F /* InputMethodKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A38BC2715FC158A00A8A51F /* InputMethodKit.framework */; };
6AB3620D274CA50700AC7547 /* OVInputSourceHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4ECA15FC0D6400ABF4B3 /* OVInputSourceHelper.m */; };
6ACA41CD15FC1D7500935EF6 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A0D4EA615FC0D2D00ABF4B3 /* Cocoa.framework */; }; 6ACA41CD15FC1D7500935EF6 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A0D4EA615FC0D2D00ABF4B3 /* Cocoa.framework */; };
6ACA41F915FC1D9000935EF6 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6ACA41E915FC1D9000935EF6 /* AppDelegate.m */; }; 6ACA41F915FC1D9000935EF6 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 6ACA41E915FC1D9000935EF6 /* AppDelegate.m */; };
6ACA41FA15FC1D9000935EF6 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6ACA41EA15FC1D9000935EF6 /* InfoPlist.strings */; }; 6ACA41FA15FC1D9000935EF6 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6ACA41EA15FC1D9000935EF6 /* InfoPlist.strings */; };
@ -49,6 +47,8 @@
D47F7DCE278BFB57002F9DD7 /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DCD278BFB57002F9DD7 /* PreferencesWindowController.swift */; }; D47F7DCE278BFB57002F9DD7 /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DCD278BFB57002F9DD7 /* PreferencesWindowController.swift */; };
D47F7DD0278C0897002F9DD7 /* NonModalAlertWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DCF278C0897002F9DD7 /* NonModalAlertWindowController.swift */; }; D47F7DD0278C0897002F9DD7 /* NonModalAlertWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DCF278C0897002F9DD7 /* NonModalAlertWindowController.swift */; };
D47F7DD3278C1263002F9DD7 /* UserOverrideModel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DD2278C1263002F9DD7 /* UserOverrideModel.cpp */; }; D47F7DD3278C1263002F9DD7 /* UserOverrideModel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DD2278C1263002F9DD7 /* UserOverrideModel.cpp */; };
D47F7DD5278C25A0002F9DD7 /* InputSourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DD4278C25A0002F9DD7 /* InputSourceHelper.swift */; };
D47F7DD6278C3075002F9DD7 /* InputSourceHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DD4278C25A0002F9DD7 /* InputSourceHelper.swift */; };
D48550A325EBE689006A204C /* OpenCC in Frameworks */ = {isa = PBXBuildFile; productRef = D48550A225EBE689006A204C /* OpenCC */; }; D48550A325EBE689006A204C /* OpenCC in Frameworks */ = {isa = PBXBuildFile; productRef = D48550A225EBE689006A204C /* OpenCC */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
@ -81,8 +81,6 @@
6A0D4EC615FC0D6400ABF4B3 /* InputMethodController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InputMethodController.h; sourceTree = "<group>"; }; 6A0D4EC615FC0D6400ABF4B3 /* InputMethodController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InputMethodController.h; sourceTree = "<group>"; };
6A0D4EC715FC0D6400ABF4B3 /* InputMethodController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = InputMethodController.mm; sourceTree = "<group>"; }; 6A0D4EC715FC0D6400ABF4B3 /* InputMethodController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = InputMethodController.mm; sourceTree = "<group>"; };
6A0D4EC815FC0D6400ABF4B3 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; }; 6A0D4EC815FC0D6400ABF4B3 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
6A0D4EC915FC0D6400ABF4B3 /* OVInputSourceHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OVInputSourceHelper.h; sourceTree = "<group>"; };
6A0D4ECA15FC0D6400ABF4B3 /* OVInputSourceHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OVInputSourceHelper.m; sourceTree = "<group>"; };
6A0D4ED915FC0DA600ABF4B3 /* VTCandidateController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VTCandidateController.h; sourceTree = "<group>"; }; 6A0D4ED915FC0DA600ABF4B3 /* VTCandidateController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VTCandidateController.h; sourceTree = "<group>"; };
6A0D4EDA15FC0DA600ABF4B3 /* VTCandidateController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VTCandidateController.m; sourceTree = "<group>"; }; 6A0D4EDA15FC0DA600ABF4B3 /* VTCandidateController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VTCandidateController.m; sourceTree = "<group>"; };
6A0D4EDB15FC0DA600ABF4B3 /* VTHorizontalCandidateController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VTHorizontalCandidateController.h; sourceTree = "<group>"; }; 6A0D4EDB15FC0DA600ABF4B3 /* VTHorizontalCandidateController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VTHorizontalCandidateController.h; sourceTree = "<group>"; };
@ -182,6 +180,7 @@
D47F7DCF278C0897002F9DD7 /* NonModalAlertWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonModalAlertWindowController.swift; sourceTree = "<group>"; }; D47F7DCF278C0897002F9DD7 /* NonModalAlertWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonModalAlertWindowController.swift; sourceTree = "<group>"; };
D47F7DD1278C1263002F9DD7 /* UserOverrideModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UserOverrideModel.h; sourceTree = "<group>"; }; D47F7DD1278C1263002F9DD7 /* UserOverrideModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UserOverrideModel.h; sourceTree = "<group>"; };
D47F7DD2278C1263002F9DD7 /* UserOverrideModel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UserOverrideModel.cpp; sourceTree = "<group>"; }; D47F7DD2278C1263002F9DD7 /* UserOverrideModel.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UserOverrideModel.cpp; sourceTree = "<group>"; };
D47F7DD4278C25A0002F9DD7 /* InputSourceHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputSourceHelper.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -249,8 +248,7 @@
6A0D4EC615FC0D6400ABF4B3 /* InputMethodController.h */, 6A0D4EC615FC0D6400ABF4B3 /* InputMethodController.h */,
6A0D4EC715FC0D6400ABF4B3 /* InputMethodController.mm */, 6A0D4EC715FC0D6400ABF4B3 /* InputMethodController.mm */,
6A0D4EC815FC0D6400ABF4B3 /* main.m */, 6A0D4EC815FC0D6400ABF4B3 /* main.m */,
6A0D4EC915FC0D6400ABF4B3 /* OVInputSourceHelper.h */, D47F7DD4278C25A0002F9DD7 /* InputSourceHelper.swift */,
6A0D4ECA15FC0D6400ABF4B3 /* OVInputSourceHelper.m */,
D47F7DCF278C0897002F9DD7 /* NonModalAlertWindowController.swift */, D47F7DCF278C0897002F9DD7 /* NonModalAlertWindowController.swift */,
D47F7DCD278BFB57002F9DD7 /* PreferencesWindowController.swift */, D47F7DCD278BFB57002F9DD7 /* PreferencesWindowController.swift */,
D427A9C025ED28CC005D43E0 /* OpenCCBridge.swift */, D427A9C025ED28CC005D43E0 /* OpenCCBridge.swift */,
@ -573,7 +571,7 @@
6A0D4ED015FC0D6400ABF4B3 /* AppDelegate.m in Sources */, 6A0D4ED015FC0D6400ABF4B3 /* AppDelegate.m in Sources */,
6A0D4ED215FC0D6400ABF4B3 /* InputMethodController.mm in Sources */, 6A0D4ED215FC0D6400ABF4B3 /* InputMethodController.mm in Sources */,
6A0D4ED315FC0D6400ABF4B3 /* main.m in Sources */, 6A0D4ED315FC0D6400ABF4B3 /* main.m in Sources */,
6A0D4ED415FC0D6400ABF4B3 /* OVInputSourceHelper.m in Sources */, D47F7DD5278C25A0002F9DD7 /* InputSourceHelper.swift in Sources */,
6A0D4EFE15FC0DA600ABF4B3 /* VTCandidateController.m in Sources */, 6A0D4EFE15FC0DA600ABF4B3 /* VTCandidateController.m in Sources */,
6A0D4EFF15FC0DA600ABF4B3 /* VTHorizontalCandidateController.m in Sources */, 6A0D4EFF15FC0DA600ABF4B3 /* VTHorizontalCandidateController.m in Sources */,
D47F7DD3278C1263002F9DD7 /* UserOverrideModel.cpp in Sources */, D47F7DD3278C1263002F9DD7 /* UserOverrideModel.cpp in Sources */,
@ -595,8 +593,8 @@
files = ( files = (
6ACA41F915FC1D9000935EF6 /* AppDelegate.m in Sources */, 6ACA41F915FC1D9000935EF6 /* AppDelegate.m in Sources */,
6A225A232367A1D700F685C6 /* ArchiveUtil.m in Sources */, 6A225A232367A1D700F685C6 /* ArchiveUtil.m in Sources */,
6AB3620D274CA50700AC7547 /* OVInputSourceHelper.m in Sources */,
6ACA41FF15FC1D9000935EF6 /* main.m in Sources */, 6ACA41FF15FC1D9000935EF6 /* main.m in Sources */,
D47F7DD6278C3075002F9DD7 /* InputSourceHelper.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@ -981,6 +979,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.openvanilla.McBopomofo.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_BUNDLE_IDENTIFIER = "org.openvanilla.McBopomofo.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx; SDKROOT = macosx;
SWIFT_VERSION = 5.0;
WRAPPER_EXTENSION = app; WRAPPER_EXTENSION = app;
}; };
name = Debug; name = Debug;
@ -1013,6 +1012,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "org.openvanilla.McBopomofo.${PRODUCT_NAME:rfc1034identifier}"; PRODUCT_BUNDLE_IDENTIFIER = "org.openvanilla.McBopomofo.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx; SDKROOT = macosx;
SWIFT_VERSION = 5.0;
WRAPPER_EXTENSION = app; WRAPPER_EXTENSION = app;
}; };
name = Release; name = Release;

View File

@ -0,0 +1,142 @@
//
// OVInputSourceHelper.swift
//
// Copyright (c) 2011 The McBopomofo Project.
//
// Contributors:
// Mengjuei Hsieh (@mjhsieh)
// Weizhong Yang (@zonble)
//
// Based on the Syrup Project and the Formosana Library
// by Lukhnos Liu (@lukhnos).
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
import Cocoa
import Carbon
public class InputSourceHelper: NSObject {
@available(*, unavailable)
public override init() {
super.init()
}
public static func allInstalledInputSources() -> [TISInputSource] {
TISCreateInputSourceList(nil, true).takeRetainedValue() as! [TISInputSource]
}
@objc(inputSourceForProperty:stringValue:)
public static func inputSource(for propertyKey: CFString, stringValue: String) -> TISInputSource? {
let stringID = CFStringGetTypeID()
for source in allInstalledInputSources() {
if let proprtyPtr = TISGetInputSourceProperty(source, propertyKey) {
let property = Unmanaged<CFTypeRef>.fromOpaque(proprtyPtr).takeUnretainedValue()
let typeID = CFGetTypeID(property)
if typeID != stringID {
continue
}
if stringValue == property as? String {
return source
}
}
}
return nil
}
@objc(inputSourceForInputSourceID:)
public static func inputSource(for sourceID: String) -> TISInputSource? {
inputSource(for: kTISPropertyInputSourceID, stringValue: sourceID)
}
@objc(inputSourceEnabled:)
public static func inputSourceEnabled(for source: TISInputSource) -> Bool {
if let valuePts = TISGetInputSourceProperty(source, kTISPropertyInputSourceIsEnabled) {
let value = Unmanaged<CFBoolean>.fromOpaque(valuePts).takeUnretainedValue()
return value == kCFBooleanTrue
}
return false
}
@objc(enableInputSource:)
public static func enable(inputSource: TISInputSource) -> Bool {
let status = TISEnableInputSource(inputSource)
return status == noErr
}
@objc(enableAllInputModesForInputSourceBundleID:)
public static func enableAllInputMode(for inputSourceBundleD: String) -> Bool {
var enabled = false
for source in allInstalledInputSources() {
guard let bundleIDPtr = TISGetInputSourceProperty(source, kTISPropertyBundleID),
let _ = TISGetInputSourceProperty(source, kTISPropertyInputModeID) else {
continue
}
let bundleID = Unmanaged<CFString>.fromOpaque(bundleIDPtr).takeUnretainedValue()
if String(bundleID) == inputSourceBundleD {
let modeEnabled = self.enable(inputSource: source)
if !modeEnabled {
return false
}
enabled = true
}
}
return enabled
}
@objc(enableInputMode:forInputSourceBundleID:)
public static func enable(inputMode modeID: String, for bundleID: String) -> Bool {
for source in allInstalledInputSources() {
guard let bundleIDPtr = TISGetInputSourceProperty(source, kTISPropertyBundleID),
let modePtr = TISGetInputSourceProperty(source, kTISPropertyInputModeID) else {
continue
}
let inputsSourceBundleID = Unmanaged<CFString>.fromOpaque(bundleIDPtr).takeUnretainedValue()
let inputsSourceModeID = Unmanaged<CFString>.fromOpaque(modePtr).takeUnretainedValue()
if modeID == String(inputsSourceModeID) && bundleID == String(inputsSourceBundleID) {
let enabled = enable(inputSource: source)
print("Attempt to enable input source of mode: \(modeID), bundle ID: \(bundleID), result: \(enabled)")
return enabled
}
}
print("Failed to find any matching input source of mode: \(modeID), bundle ID: \(bundleID)")
return false
}
@objc(disableInputSource:)
public static func disable(inputSource: TISInputSource) -> Bool {
let status = TISDisableInputSource(inputSource)
return status == noErr
}
@objc(registerInputSource:)
public static func registerTnputSource(at url: URL) -> Bool {
let status = TISRegisterInputSource(url as CFURL)
return status == noErr
}
}

View File

@ -27,7 +27,7 @@
#import "AppDelegate.h" #import "AppDelegate.h"
#import <sys/mount.h> #import <sys/mount.h>
#import "OVInputSourceHelper.h" #import "McBopomofoInstaller-Swift.h"
static NSString *const kTargetBin = @"McBopomofo"; static NSString *const kTargetBin = @"McBopomofo";
static NSString *const kTargetType = @"app"; static NSString *const kTargetType = @"app";
@ -182,13 +182,13 @@ void RunAlertPanel(NSString *title, NSString *message, NSString *buttonTitle) {
NSURL *imeBundleURL = imeBundle.bundleURL; NSURL *imeBundleURL = imeBundle.bundleURL;
NSString *imeIdentifier = imeBundle.bundleIdentifier; NSString *imeIdentifier = imeBundle.bundleIdentifier;
TISInputSourceRef inputSource = [OVInputSourceHelper inputSourceForInputSourceID:imeIdentifier]; TISInputSourceRef inputSource = [InputSourceHelper inputSourceForInputSourceID:imeIdentifier];
// if this IME name is not found in the list of available IMEs // if this IME name is not found in the list of available IMEs
if (!inputSource) { if (!inputSource) {
NSLog(@"Registering input source %@ at %@.", imeIdentifier, imeBundleURL.absoluteString); NSLog(@"Registering input source %@ at %@.", imeIdentifier, imeBundleURL.absoluteString);
// then register // then register
BOOL status = [OVInputSourceHelper registerInputSource:imeBundleURL]; BOOL status = [InputSourceHelper registerInputSource:imeBundleURL];
if (!status) { if (!status) {
NSString *message = [NSString stringWithFormat:NSLocalizedString(@"Cannot register input source %@ at %@.", nil), imeIdentifier, imeBundleURL.absoluteString]; NSString *message = [NSString stringWithFormat:NSLocalizedString(@"Cannot register input source %@ at %@.", nil), imeIdentifier, imeBundleURL.absoluteString];
@ -197,7 +197,7 @@ void RunAlertPanel(NSString *title, NSString *message, NSString *buttonTitle) {
return; return;
} }
inputSource = [OVInputSourceHelper inputSourceForInputSourceID:imeIdentifier]; inputSource = [InputSourceHelper inputSourceForInputSourceID:imeIdentifier];
// if it still doesn't register successfully, bail. // if it still doesn't register successfully, bail.
if (!inputSource) { if (!inputSource) {
NSString *message = [NSString stringWithFormat:NSLocalizedString(@"Cannot find input source %@ after registration.", nil), imeIdentifier]; NSString *message = [NSString stringWithFormat:NSLocalizedString(@"Cannot find input source %@ after registration.", nil), imeIdentifier];
@ -219,10 +219,10 @@ void RunAlertPanel(NSString *title, NSString *message, NSString *buttonTitle) {
// as the kTISPropertyInputSourceIsEnabled can still be true even if the IME is *not* // as the kTISPropertyInputSourceIsEnabled can still be true even if the IME is *not*
// enabled in the user's current set of IMEs (which means the IME does not show up in // enabled in the user's current set of IMEs (which means the IME does not show up in
// the user's input menu). // the user's input menu).
BOOL mainInputSourceEnabled = [OVInputSourceHelper inputSourceEnabled:inputSource]; BOOL mainInputSourceEnabled = [InputSourceHelper inputSourceEnabled:inputSource];
if (!mainInputSourceEnabled || isMacOS12OrAbove) { if (!mainInputSourceEnabled || isMacOS12OrAbove) {
mainInputSourceEnabled = [OVInputSourceHelper enableInputSource:inputSource]; mainInputSourceEnabled = [InputSourceHelper enableInputSource:inputSource];
if (mainInputSourceEnabled) { if (mainInputSourceEnabled) {
NSLog(@"Input method enabled: %@", imeIdentifier); NSLog(@"Input method enabled: %@", imeIdentifier);
} else { } else {

View File

@ -1,50 +0,0 @@
//
// OVInputSourceHelper.h
//
// Copyright (c) 2010-2011 Lukhnos D. Liu (lukhnos at lukhnos dot org)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import <InputMethodKit/InputMethodKit.h>
@interface OVInputSourceHelper : NSObject
// list all installed input sources
+ (NSArray *)allInstalledInputSources;
// search for a certain input source
+ (TISInputSourceRef)inputSourceForProperty:(CFStringRef)inPropertyKey stringValue:(NSString *)inValue;
// shorthand for -inputSourceForProerty:kTISPropertyInputSourceID stringValue:<value>
+ (TISInputSourceRef)inputSourceForInputSourceID:(NSString *)inID;
// enable/disable an input source (along with all its input modes)
+ (BOOL)inputSourceEnabled:(TISInputSourceRef)inInputSource;
+ (BOOL)enableInputSource:(TISInputSourceRef)inInputSource;
+ (BOOL)enableAllInputModesForInputSourceBundleID:(NSString *)inID;
+ (BOOL)enableInputMode:(NSString *)modeID forInputSourceBundleID:(NSString *)bundleID;
+ (BOOL)disableInputSource:(TISInputSourceRef)inInputSource;
// register (i.e. make available to Input Source tab in Language & Text Preferences)
// an input source installed in (~)/Library/Input Methods or (~)/Library/Keyboard Layouts/
+ (BOOL)registerInputSource:(NSURL *)inBundleURL;
@end

View File

@ -1,122 +0,0 @@
//
// OVInputSourceHelper.m
//
// Copyright (c) 2010-2011 Lukhnos D. Liu (lukhnos at lukhnos dot org)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#import "OVInputSourceHelper.h"
@implementation OVInputSourceHelper
+ (NSArray *)allInstalledInputSources
{
CFArrayRef list = TISCreateInputSourceList(NULL, true);
return (__bridge NSArray *)list;
// return [NSMakeCollectable(list) autorelease];
}
+ (TISInputSourceRef)inputSourceForProperty:(CFStringRef)inPropertyKey stringValue:(NSString *)inValue
{
CFTypeID stringID = CFStringGetTypeID();
for (id source in [self allInstalledInputSources]) {
CFTypeRef property = TISGetInputSourceProperty((__bridge TISInputSourceRef)source, inPropertyKey);
if (!property || CFGetTypeID(property) != stringID) {
continue;
}
if (inValue && [inValue compare:(__bridge NSString *)property] == NSOrderedSame) {
return (__bridge TISInputSourceRef)source;
}
}
return NULL;
}
+ (TISInputSourceRef)inputSourceForInputSourceID:(NSString *)inID
{
return [self inputSourceForProperty:kTISPropertyInputSourceID stringValue:inID];
}
+ (BOOL)inputSourceEnabled:(TISInputSourceRef)inInputSource
{
CFBooleanRef value = TISGetInputSourceProperty(inInputSource, kTISPropertyInputSourceIsEnabled);
return value ? (BOOL)CFBooleanGetValue(value) : NO;
}
+ (BOOL)enableInputSource:(TISInputSourceRef)inInputSource
{
OSStatus status = TISEnableInputSource(inInputSource);
return status == noErr;
}
+ (BOOL)enableAllInputModesForInputSourceBundleID:(NSString *)inID
{
BOOL enabled = NO;
for (id source in [self allInstalledInputSources]) {
TISInputSourceRef inputSource = (__bridge TISInputSourceRef)source;
NSString *bundleID = (__bridge NSString *)TISGetInputSourceProperty(inputSource, kTISPropertyBundleID);
NSString *mode = (NSString *)CFBridgingRelease(TISGetInputSourceProperty(inputSource, kTISPropertyInputModeID));
if (mode && [bundleID isEqualToString:inID]) {
BOOL modeEnabled = [self enableInputSource:inputSource];
if (!modeEnabled) {
return NO;
}
enabled = YES;
}
}
return enabled;
}
+ (BOOL)enableInputMode:(NSString *)modeID forInputSourceBundleID:(NSString *)bundleID
{
for (id source in [self allInstalledInputSources]) {
TISInputSourceRef inputSource = (__bridge TISInputSourceRef)source;
NSString *inputSoureBundleID = (__bridge NSString *)TISGetInputSourceProperty(inputSource, kTISPropertyBundleID);
NSString *inputSourceModeID = (NSString *)CFBridgingRelease(TISGetInputSourceProperty(inputSource, kTISPropertyInputModeID));
if ([modeID isEqual:inputSourceModeID] && [bundleID isEqual:inputSoureBundleID]) {
BOOL enabled = [self enableInputSource:inputSource];
NSLog(@"Attempt to enable input source of mode: %@, bundle ID: %@, result: %d", modeID, bundleID, enabled);
return enabled;
}
}
NSLog(@"Failed to find any matching input source of mode: %@, bundle ID: %@", modeID, bundleID);
return NO;
}
+ (BOOL)disableInputSource:(TISInputSourceRef)inInputSource
{
OSStatus status = TISDisableInputSource(inInputSource);
return status == noErr;
}
+ (BOOL)registerInputSource:(NSURL *)inBundleURL
{
OSStatus status = TISRegisterInputSource((__bridge CFURLRef)inBundleURL);
return status == noErr;
}
@end

View File

@ -33,7 +33,8 @@
// //
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import "OVInputSourceHelper.h" #import <InputMethodKit/InputMethodKit.h>
#import "McBopomofo-Swift.h"
static NSString *const kConnectionName = @"McBopomofo_1_Connection"; static NSString *const kConnectionName = @"McBopomofo_1_Connection";
@ -54,20 +55,20 @@ int main(int argc, char *argv[])
bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
} }
TISInputSourceRef inputSource = [OVInputSourceHelper inputSourceForInputSourceID:bundleID]; TISInputSourceRef inputSource = [InputSourceHelper inputSourceForInputSourceID:bundleID];
// if this IME name is not found in the list of available IMEs // if this IME name is not found in the list of available IMEs
if (!inputSource) { if (!inputSource) {
NSLog(@"Registering input source %@ at %@.", bundleID, [bundleURL absoluteString]); NSLog(@"Registering input source %@ at %@.", bundleID, [bundleURL absoluteString]);
// then register // then register
BOOL status = [OVInputSourceHelper registerInputSource:bundleURL]; BOOL status = [InputSourceHelper registerInputSource:bundleURL];
if (!status) { if (!status) {
NSLog(@"Fatal error: Cannot register input source %@ at %@.", bundleID, [bundleURL absoluteString]); NSLog(@"Fatal error: Cannot register input source %@ at %@.", bundleID, [bundleURL absoluteString]);
return -1; return -1;
} }
inputSource = [OVInputSourceHelper inputSourceForInputSourceID:bundleID]; inputSource = [InputSourceHelper inputSourceForInputSourceID:bundleID];
// if it still doesn't register successfully, bail. // if it still doesn't register successfully, bail.
if (!inputSource) { if (!inputSource) {
NSLog(@"Fatal error: Cannot find input source %@ after registration.", bundleID); NSLog(@"Fatal error: Cannot find input source %@ after registration.", bundleID);
@ -76,22 +77,22 @@ int main(int argc, char *argv[])
} }
// if it's not enabled, just enabled it // if it's not enabled, just enabled it
if (inputSource && ![OVInputSourceHelper inputSourceEnabled:inputSource]) { if (inputSource && ![InputSourceHelper inputSourceEnabled:inputSource]) {
NSLog(@"Enabling input source %@ at %@.", bundleID, [bundleURL absoluteString]); NSLog(@"Enabling input source %@ at %@.", bundleID, [bundleURL absoluteString]);
BOOL status = [OVInputSourceHelper enableInputSource:inputSource]; BOOL status = [InputSourceHelper enableInputSource:inputSource];
if (!status) { if (!status) {
NSLog(@"Fatal error: Cannot enable input source %@.", bundleID); NSLog(@"Fatal error: Cannot enable input source %@.", bundleID);
return -1; return -1;
} }
if (![OVInputSourceHelper inputSourceEnabled:inputSource]){ if (![InputSourceHelper inputSourceEnabled:inputSource]){
NSLog(@"Fatal error: Cannot enable input source %@.", bundleID); NSLog(@"Fatal error: Cannot enable input source %@.", bundleID);
return -1; return -1;
} }
} }
if (argc > 2 && !strcmp(argv[2], "--all")) { if (argc > 2 && !strcmp(argv[2], "--all")) {
BOOL enabled = [OVInputSourceHelper enableAllInputModesForInputSourceBundleID:bundleID]; BOOL enabled = [InputSourceHelper enableAllInputModesForInputSourceBundleID:bundleID];
if (enabled) { if (enabled) {
NSLog(@"All input sources enabled for %@", bundleID); NSLog(@"All input sources enabled for %@", bundleID);
} }