diff --git a/Source/main.m b/Source/main.m deleted file mode 100644 index e50282aa..00000000 --- a/Source/main.m +++ /dev/null @@ -1,102 +0,0 @@ -/* - * main.m - * - * Copyright 2021-2022 vChewing Project (3-Clause BSD License). - * Derived from 2011-2022 OpenVanilla Project (MIT License). - * Some rights reserved. See "LICENSE.TXT" for details. - */ - -#import -#import -#import "vChewing-Swift.h" - -static NSString *const kConnectionName = @"vChewing_1_Connection"; - -int main(int argc, char *argv[]) -{ - @autoreleasepool { - - // register and enable the input source (along with all its input modes) - if (argc > 1 && !strcmp(argv[1], "install")) { - NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier]; - NSURL *bundleURL = nil; - if ([[NSBundle mainBundle] respondsToSelector:@selector(bundleURL)]) { - // For Mac OS X 10.6+ - bundleURL = [[NSBundle mainBundle] bundleURL]; - } - else { - // For Mac OS X 10.5 - bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; - } - - TISInputSourceRef inputSource = [InputSourceHelper inputSourceForInputSourceID:bundleID]; - - // if this IME name is not found in the list of available IMEs - if (!inputSource) { - NSLog(@"Registering input source %@ at %@.", bundleID, [bundleURL absoluteString]); - // then register - BOOL status = [InputSourceHelper registerInputSource:bundleURL]; - - if (!status) { - NSLog(@"Fatal error: Cannot register input source %@ at %@.", bundleID, [bundleURL absoluteString]); - return -1; - } - - inputSource = [InputSourceHelper inputSourceForInputSourceID:bundleID]; - // if it still doesn't register successfully, bail. - if (!inputSource) { - NSLog(@"Fatal error: Cannot find input source %@ after registration.", bundleID); - return -1; - } - } - - // if it's not enabled, just enabled it - if (inputSource && ![InputSourceHelper inputSourceEnabled:inputSource]) { - NSLog(@"Enabling input source %@ at %@.", bundleID, [bundleURL absoluteString]); - BOOL status = [InputSourceHelper enableInputSource:inputSource]; - - if (!status) { - NSLog(@"Fatal error: Cannot enable input source %@.", bundleID); - return -1; - } - if (![InputSourceHelper inputSourceEnabled:inputSource]){ - NSLog(@"Fatal error: Cannot enable input source %@.", bundleID); - return -1; - } - } - - if (argc > 2 && !strcmp(argv[2], "--all")) { - BOOL enabled = [InputSourceHelper enableAllInputModesForInputSourceBundleID:bundleID]; - if (enabled) { - NSLog(@"All input sources enabled for %@", bundleID); - } - else { - NSLog(@"Cannot enable all input sources for %@, but this is ignored", bundleID); - } - } - - return 0; - } - - NSString *mainNibName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"NSMainNibFile"]; - if (!mainNibName) { - NSLog(@"Fatal error: NSMainNibFile key not defined in Info.plist."); - return -1; - } - - BOOL loadResult = [[NSBundle mainBundle] loadNibNamed:mainNibName owner:[NSApplication sharedApplication] topLevelObjects:NULL]; - if (!loadResult) { - NSLog(@"Fatal error: Cannot load %@.", mainNibName); - return -1; - } - - IMKServer *server = [[IMKServer alloc] initWithName:kConnectionName bundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]]; - if (!server) { - NSLog(@"Fatal error: Cannot initialize input method server with connection %@.", kConnectionName); - return -1; - } - - [[NSApplication sharedApplication] run]; - } - return 0; -} diff --git a/Source/main.swift b/Source/main.swift new file mode 100644 index 00000000..98952a3f --- /dev/null +++ b/Source/main.swift @@ -0,0 +1,82 @@ +/* + * main.m + * + * Copyright 2021-2022 vChewing Project (3-Clause BSD License). + * Derived from 2011-2022 OpenVanilla Project (MIT License). + * Some rights reserved. See "LICENSE.TXT" for details. + */ + +import Cocoa +import InputMethodKit + +private func install() -> Int32 { + guard let bundleID = Bundle.main.bundleIdentifier else { + return -1 + } + let bundleUrl = Bundle.main.bundleURL + var inputSource = InputSourceHelper.inputSource(for: bundleID) + + if inputSource == nil { + NSLog("Registering input source \(bundleID) at \(bundleUrl.absoluteString)"); + // then register + let status = InputSourceHelper.registerTnputSource(at: bundleUrl) + + if !status { + NSLog("Fatal error: Cannot register input source \(bundleID) at \(bundleUrl.absoluteString).") + return -1 + } + + inputSource = InputSourceHelper.inputSource(for: bundleID) + } + + guard let inputSource = inputSource else { + NSLog("Fatal error: Cannot find input source \(bundleID) after registration.") + return -1 + } + + if !InputSourceHelper.inputSourceEnabled(for: inputSource) { + NSLog("Enabling input source \(bundleID) at \(bundleUrl.absoluteString).") + let status = InputSourceHelper.enable(inputSource: inputSource) + if !status { + NSLog("Fatal error: Cannot enable input source \(bundleID).") + return -1 + } + if !InputSourceHelper.inputSourceEnabled(for: inputSource) { + NSLog("Fatal error: Cannot enable input source \(bundleID).") + return -1 + } + } + + if CommandLine.arguments.count > 2 && CommandLine.arguments[2] == "--all" { + let enabled = InputSourceHelper.enableAllInputMode(for: bundleID) + NSLog(enabled ? "All input sources enabled for \(bundleID)" : "Cannot enable all input sources for \(bundleID), but this is ignored") + } + return 0 +} + +let kConnectionName = "vChewing_1_Connection" + +if CommandLine.arguments.count > 1 { + if CommandLine.arguments[1] == "install" { + let exitCode = install() + exit(exitCode) + } +} + +guard let mainNibName = Bundle.main.infoDictionary?["NSMainNibFile"] as? String else { + NSLog("Fatal error: NSMainNibFile key not defined in Info.plist."); + exit(-1) +} + +let loaded = Bundle.main.loadNibNamed(mainNibName, owner: NSApp, topLevelObjects: nil) +if !loaded { + NSLog("Fatal error: Cannot load \(mainNibName).") + exit(-1) +} + +guard let bundleID = Bundle.main.bundleIdentifier, let server = IMKServer(name: kConnectionName, bundleIdentifier: bundleID) else { + NSLog("Fatal error: Cannot initialize input method server with connection \(kConnectionName).") + exit(-1) +} + +NSApp.run() diff --git a/vChewing.xcodeproj/project.pbxproj b/vChewing.xcodeproj/project.pbxproj index c84a7bce..6f598c67 100644 --- a/vChewing.xcodeproj/project.pbxproj +++ b/vChewing.xcodeproj/project.pbxproj @@ -41,7 +41,7 @@ 6A0421A815FEF3F50061ED63 /* FastLM.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6A0421A615FEF3F50061ED63 /* FastLM.cpp */; }; 6A0D4EA715FC0D2D00ABF4B3 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A0D4EA615FC0D2D00ABF4B3 /* Cocoa.framework */; }; 6A0D4ED215FC0D6400ABF4B3 /* InputMethodController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EC715FC0D6400ABF4B3 /* InputMethodController.mm */; }; - 6A0D4ED315FC0D6400ABF4B3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EC815FC0D6400ABF4B3 /* main.m */; }; + 6A0D4ED315FC0D6400ABF4B3 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4EC815FC0D6400ABF4B3 /* main.swift */; }; 6A0D4F0815FC0DA600ABF4B3 /* Bopomofo.tiff in Resources */ = {isa = PBXBuildFile; fileRef = 6A0D4EEF15FC0DA600ABF4B3 /* Bopomofo.tiff */; }; 6A0D4F0915FC0DA600ABF4B3 /* Bopomofo@2x.tiff in Resources */ = {isa = PBXBuildFile; fileRef = 6A0D4EF015FC0DA600ABF4B3 /* Bopomofo@2x.tiff */; }; 6A0D4F4515FC0EB100ABF4B3 /* Mandarin.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6A0D4F2015FC0EB100ABF4B3 /* Mandarin.cpp */; }; @@ -152,7 +152,7 @@ 6A0D4EAB15FC0D2D00ABF4B3 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 6A0D4EC615FC0D6400ABF4B3 /* InputMethodController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InputMethodController.h; sourceTree = ""; }; 6A0D4EC715FC0D6400ABF4B3 /* InputMethodController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = InputMethodController.mm; sourceTree = ""; }; - 6A0D4EC815FC0D6400ABF4B3 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + 6A0D4EC815FC0D6400ABF4B3 /* main.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = ""; }; 6A0D4EEF15FC0DA600ABF4B3 /* Bopomofo.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = Bopomofo.tiff; sourceTree = ""; }; 6A0D4EF015FC0DA600ABF4B3 /* Bopomofo@2x.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = "Bopomofo@2x.tiff"; sourceTree = ""; }; 6A0D4EF515FC0DA600ABF4B3 /* vChewing-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "vChewing-Info.plist"; sourceTree = ""; }; @@ -367,7 +367,7 @@ 6A0D4EC715FC0D6400ABF4B3 /* InputMethodController.mm */, 5B5F4F91279294A300922DC2 /* LanguageModelManager.h */, 5B5F4F92279294A300922DC2 /* LanguageModelManager.mm */, - 6A0D4EC815FC0D6400ABF4B3 /* main.m */, + 6A0D4EC815FC0D6400ABF4B3 /* main.swift */, 6A0D4EF615FC0DA600ABF4B3 /* vChewing-Prefix.pch */, 5BDF2D052791DA6700838ADB /* AppDelegate.swift */, 5BDF2CFD2791BE4400838ADB /* InputSourceHelper.swift */, @@ -702,7 +702,7 @@ 5BE798A72793280C00337FF9 /* NotifierController.swift in Sources */, 5B5F4F93279294A300922DC2 /* LanguageModelManager.mm in Sources */, 6A0D4ED215FC0D6400ABF4B3 /* InputMethodController.mm in Sources */, - 6A0D4ED315FC0D6400ABF4B3 /* main.m in Sources */, + 6A0D4ED315FC0D6400ABF4B3 /* main.swift in Sources */, 5BF4A6FE27844738007DC6E7 /* frmAboutWindow.m in Sources */, 5B5F4F972792A4EA00922DC2 /* UserPhrasesLM.cpp in Sources */, 5BC2D2882793B434002C0BEC /* KeyValueBlobReader.cpp in Sources */,