Skip to content

Commit

Permalink
build static spell (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj authored Oct 4, 2024
1 parent 8b7ffd6 commit 745087f
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 31 deletions.
12 changes: 3 additions & 9 deletions iosfrontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@ fcitx5_translate_desktop_file(${CMAKE_CURRENT_BINARY_DIR}/iosfrontend.conf.in io
add_custom_command(
TARGET iosfrontend
POST_BUILD COMMAND /bin/sh -c
\"COMMAND_DONE=0 \;
if ${CMAKE_COMMAND} -E copy
\"
${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_BINARY_DIR}/iosfrontend.conf
${PROJECT_BINARY_DIR}/keyboard/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/keyboard.appex/share/fcitx5/addon/iosfrontend.conf
\&\>/dev/null \; then
COMMAND_DONE=1 \;
fi \;
if [ \\$$COMMAND_DONE -eq 0 ] \; then
echo Failed to copy the conf into the app bundle \;
exit 1 \;
fi\"
\"
)
1 change: 1 addition & 0 deletions keyboard/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ target_compile_options(keyboard PUBLIC

target_link_libraries(keyboard PRIVATE
Fcitx5::Core
spell
iosfrontend
)
1 change: 1 addition & 0 deletions keyboard/KeyboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class KeyboardViewController: UIInputViewController {
super.viewDidLoad()

startFcitx(Bundle.main.bundlePath)
focusIn()

// Perform custom UI setup here
self.nextKeyboardButton = UIButton(type: .system)
Expand Down
24 changes: 23 additions & 1 deletion keyboard/fcitx.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
#include "../fcitx5/src/modules/spell/spell.h"
#include "../iosfrontend/iosfrontend.h"
#include "nativestreambuf.h"
#include <fcitx-utils/event.h>
#include <fcitx-utils/eventdispatcher.h>
#include <fcitx/instance.h>
#include <filesystem>
#include <thread>

#include "fcitx.h"
#include "util.h"

namespace fs = std::filesystem;

fcitx::SpellModuleFactory SpellModuleFactory;
fcitx::IosFrontendFactory IosFrontendFactory;

fcitx::StaticAddonRegistry addons = {
std::make_pair<std::string, fcitx::AddonFactory *>("spell",
&SpellModuleFactory),
std::make_pair<std::string, fcitx::AddonFactory *>("iosfrontend",
&IosFrontendFactory),
};

native_streambuf log_streambuf;
std::unique_ptr<fcitx::Instance> instance;
std::unique_ptr<fcitx::EventDispatcher> dispatcher;
fcitx::IosFrontend *frontend;

std::ostream stream(&log_streambuf);
std::thread fcitx_thread;

Expand All @@ -26,7 +37,9 @@ void setupLog() {

void setupEnv(const char *bundlePath) {
fs::path bundle = bundlePath;
std::string xdg_data_dirs = bundle / "share";
std::string fcitx_data_home = bundle / "share/fcitx5";
setenv("XDG_DATA_DIRS", xdg_data_dirs.c_str(), 1);
setenv("FCITX_DATA_HOME", fcitx_data_home.c_str(), 1);
}

Expand All @@ -40,6 +53,15 @@ void startFcitx(const char *bundlePath) {
instance = std::make_unique<fcitx::Instance>(0, nullptr);
auto &addonMgr = instance->addonManager();
addonMgr.registerDefaultLoader(&addons);
fcitx_thread = std::thread([] { instance->exec(); });
instance->initialize();
frontend =
dynamic_cast<fcitx::IosFrontend *>(addonMgr.addon("iosfrontend"));
dispatcher = std::make_unique<fcitx::EventDispatcher>();
dispatcher->attach(&instance->eventLoop());
fcitx_thread = std::thread([] { instance->eventLoop().exec(); });
return;
}

void focusIn() {
return with_fcitx([] { frontend->focusIn(); });
}
1 change: 1 addition & 0 deletions keyboard/fcitx.h
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
void startFcitx(const char *bundlePath);
void focusIn();
26 changes: 26 additions & 0 deletions keyboard/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

#include <fcitx-utils/eventdispatcher.h>
#include <future>

extern std::unique_ptr<fcitx::EventDispatcher> dispatcher;

template <class F, class T = std::invoke_result_t<F>>
inline T with_fcitx(F func) {
std::promise<T> prom;
std::future<T> fut = prom.get_future();
dispatcher->schedule([&prom, func = std::move(func)]() {
try {
if constexpr (std::is_void_v<T>) {
func();
prom.set_value();
} else {
T result = func();
prom.set_value(std::move(result));
}
} catch (...) {
prom.set_exception(std::current_exception());
}
});
fut.wait();
return fut.get();
}
89 changes: 86 additions & 3 deletions patches/fcitx5.patch
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ index df15dd57..3c1d3ff2 100644
EXPORT_NAME Core
)
target_include_directories(Fcitx5Core PUBLIC
diff --git a/src/lib/fcitx/addoninfo.cpp b/src/lib/fcitx/addoninfo.cpp
index 49d8c6e4..f6acad08 100644
--- a/src/lib/fcitx/addoninfo.cpp
+++ b/src/lib/fcitx/addoninfo.cpp
@@ -109,7 +109,8 @@ const I18NString &AddonInfo::comment() const {

const std::string &AddonInfo::type() const {
FCITX_D();
- return d->addon->type.value();
+ static const std::string t = "StaticLibrary";
+ return t;
}

AddonCategory AddonInfo::category() const {
diff --git a/src/lib/fcitx/instance.cpp b/src/lib/fcitx/instance.cpp
index d2e9aa23..72f82213 100644
--- a/src/lib/fcitx/instance.cpp
Expand All @@ -146,19 +160,88 @@ index d2e9aa23..72f82213 100644

void Instance::configureAddon(const std::string &) {}
diff --git a/src/modules/spell/CMakeLists.txt b/src/modules/spell/CMakeLists.txt
index 094e1a58..ab1ce4df 100644
index 094e1a58..106a010c 100644
--- a/src/modules/spell/CMakeLists.txt
+++ b/src/modules/spell/CMakeLists.txt
@@ -17,6 +17,7 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/spell.conf" DESTINATION "${FCITX_INST
@@ -5,7 +5,7 @@ if (TARGET PkgConfig::Enchant)
set(SPELL_SOURCES ${SPELL_SOURCES} spell-enchant.cpp)
endif()

-add_library(spell MODULE ${SPELL_SOURCES})
+add_library(spell STATIC ${SPELL_SOURCES})
target_link_libraries(spell Fcitx5::Core)
if (TARGET PkgConfig::Enchant)
target_link_libraries(spell PkgConfig::Enchant)
@@ -17,6 +17,17 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/spell.conf" DESTINATION "${FCITX_INST
COMPONENT config)
fcitx5_export_module(Spell TARGET spell BUILD_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}" HEADERS spell_public.h INSTALL)

+add_custom_command(
+ TARGET spell
+ POST_BUILD COMMAND /bin/sh -c
+ \"
+ ${CMAKE_COMMAND} -E copy
+ ${CMAKE_CURRENT_BINARY_DIR}/spell.conf
+ ${CMAKE_BINARY_DIR}/keyboard/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/keyboard.appex/share/fcitx5/addon/spell.conf
+ \"
+)
+
+if (BUILD_SPELL_DICT)
set(DICT_COMP_SRC
comp_spell_dict.cpp
)
@@ -46,3 +47,4 @@ add_custom_command(
@@ -46,3 +57,4 @@ add_custom_command(
"${SPELL_EN_DICT_SRC}" "${SPELL_EN_DICT}")
add_custom_target(spell_en_dict ALL DEPENDS "${SPELL_EN_DICT}")
install(FILES "${SPELL_EN_DICT}" DESTINATION "${FCITX_INSTALL_PKGDATADIR}/spell")
+endif()
diff --git a/src/modules/spell/spell.cpp b/src/modules/spell/spell.cpp
index cc3d951f..a4229234 100644
--- a/src/modules/spell/spell.cpp
+++ b/src/modules/spell/spell.cpp
@@ -7,7 +7,6 @@

#include "spell.h"
#include "fcitx-config/iniparser.h"
-#include "fcitx/addonmanager.h"
#include "config.h"
#include "spell-custom.h"
#ifdef ENABLE_ENCHANT
@@ -110,12 +109,6 @@ Spell::hintForDisplay(const std::string &language, SpellProvider provider,

return iter->second->hint(language, word, limit);
}
-
-class SpellModuleFactory : public AddonFactory {
- AddonInstance *create(AddonManager *manager) override {
- return new Spell(manager->instance());
- }
-};
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::SpellModuleFactory)
diff --git a/src/modules/spell/spell.h b/src/modules/spell/spell.h
index 60778962..3cfa2db5 100644
--- a/src/modules/spell/spell.h
+++ b/src/modules/spell/spell.h
@@ -13,6 +13,7 @@
#include "fcitx-utils/i18n.h"
#include "fcitx/addonfactory.h"
#include "fcitx/addoninstance.h"
+#include "fcitx/addonmanager.h"
#include "fcitx/instance.h"
#include "spell_public.h"

@@ -104,6 +105,12 @@ public:
private:
Spell *parent_;
};
+
+class SpellModuleFactory : public AddonFactory {
+ AddonInstance *create(AddonManager *manager) override {
+ return new Spell(manager->instance());
+ }
+};
} // namespace fcitx

#endif // _FCITX_MODULES_SPELL_SPELL_H_
24 changes: 6 additions & 18 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ add_executable(
add_custom_command(
TARGET ${BUNDLE_NAME}
POST_BUILD COMMAND /bin/sh -c
\"COMMAND_DONE=0 \;
if ${CMAKE_COMMAND} -E copy
\"
${CMAKE_COMMAND} -E copy
${PROJECT_SOURCE_DIR}/assets/${ICON_FILE}
${PROJECT_BINARY_DIR}/src/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/${BUNDLE_NAME}.app
\&\>/dev/null \; then
COMMAND_DONE=1 \;
fi \;
if [ \\$$COMMAND_DONE -eq 0 ] \; then
echo Failed to copy the icon into the app bundle \;
exit 1 \;
fi\"
\"
)

add_dependencies(${BUNDLE_NAME} keyboard)
Expand All @@ -30,15 +24,9 @@ set_target_properties(${BUNDLE_NAME} PROPERTIES
add_custom_command(
TARGET ${BUNDLE_NAME}
POST_BUILD COMMAND /bin/sh -c
\"COMMAND_DONE=0 \;
if ${CMAKE_COMMAND} -E copy_directory
\"
${CMAKE_COMMAND} -E copy_directory
${PROJECT_BINARY_DIR}/keyboard/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/keyboard.appex
${PROJECT_BINARY_DIR}/src/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/${BUNDLE_NAME}.app/PlugIns/keyboard.appex
\&\>/dev/null \; then
COMMAND_DONE=1 \;
fi \;
if [ \\$$COMMAND_DONE -eq 0 ] \; then
echo Failed to copy the extension into the app bundle \;
exit 1 \;
fi\"
\"
)

0 comments on commit 745087f

Please sign in to comment.