Skip to content

Commit 745087f

Browse files
authored
build static spell (#5)
1 parent 8b7ffd6 commit 745087f

File tree

8 files changed

+147
-31
lines changed

8 files changed

+147
-31
lines changed

iosfrontend/CMakeLists.txt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ fcitx5_translate_desktop_file(${CMAKE_CURRENT_BINARY_DIR}/iosfrontend.conf.in io
77
add_custom_command(
88
TARGET iosfrontend
99
POST_BUILD COMMAND /bin/sh -c
10-
\"COMMAND_DONE=0 \;
11-
if ${CMAKE_COMMAND} -E copy
10+
\"
11+
${CMAKE_COMMAND} -E copy
1212
${CMAKE_CURRENT_BINARY_DIR}/iosfrontend.conf
1313
${PROJECT_BINARY_DIR}/keyboard/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/keyboard.appex/share/fcitx5/addon/iosfrontend.conf
14-
\&\>/dev/null \; then
15-
COMMAND_DONE=1 \;
16-
fi \;
17-
if [ \\$$COMMAND_DONE -eq 0 ] \; then
18-
echo Failed to copy the conf into the app bundle \;
19-
exit 1 \;
20-
fi\"
14+
\"
2115
)

keyboard/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ target_compile_options(keyboard PUBLIC
1616

1717
target_link_libraries(keyboard PRIVATE
1818
Fcitx5::Core
19+
spell
1920
iosfrontend
2021
)

keyboard/KeyboardViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class KeyboardViewController: UIInputViewController {
1818
super.viewDidLoad()
1919

2020
startFcitx(Bundle.main.bundlePath)
21+
focusIn()
2122

2223
// Perform custom UI setup here
2324
self.nextKeyboardButton = UIButton(type: .system)

keyboard/fcitx.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1+
#include "../fcitx5/src/modules/spell/spell.h"
12
#include "../iosfrontend/iosfrontend.h"
23
#include "nativestreambuf.h"
4+
#include <fcitx-utils/event.h>
5+
#include <fcitx-utils/eventdispatcher.h>
36
#include <fcitx/instance.h>
47
#include <filesystem>
58
#include <thread>
69

710
#include "fcitx.h"
11+
#include "util.h"
812

913
namespace fs = std::filesystem;
1014

15+
fcitx::SpellModuleFactory SpellModuleFactory;
1116
fcitx::IosFrontendFactory IosFrontendFactory;
17+
1218
fcitx::StaticAddonRegistry addons = {
19+
std::make_pair<std::string, fcitx::AddonFactory *>("spell",
20+
&SpellModuleFactory),
1321
std::make_pair<std::string, fcitx::AddonFactory *>("iosfrontend",
1422
&IosFrontendFactory),
1523
};
1624

1725
native_streambuf log_streambuf;
1826
std::unique_ptr<fcitx::Instance> instance;
27+
std::unique_ptr<fcitx::EventDispatcher> dispatcher;
28+
fcitx::IosFrontend *frontend;
29+
1930
std::ostream stream(&log_streambuf);
2031
std::thread fcitx_thread;
2132

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

2738
void setupEnv(const char *bundlePath) {
2839
fs::path bundle = bundlePath;
40+
std::string xdg_data_dirs = bundle / "share";
2941
std::string fcitx_data_home = bundle / "share/fcitx5";
42+
setenv("XDG_DATA_DIRS", xdg_data_dirs.c_str(), 1);
3043
setenv("FCITX_DATA_HOME", fcitx_data_home.c_str(), 1);
3144
}
3245

@@ -40,6 +53,15 @@ void startFcitx(const char *bundlePath) {
4053
instance = std::make_unique<fcitx::Instance>(0, nullptr);
4154
auto &addonMgr = instance->addonManager();
4255
addonMgr.registerDefaultLoader(&addons);
43-
fcitx_thread = std::thread([] { instance->exec(); });
56+
instance->initialize();
57+
frontend =
58+
dynamic_cast<fcitx::IosFrontend *>(addonMgr.addon("iosfrontend"));
59+
dispatcher = std::make_unique<fcitx::EventDispatcher>();
60+
dispatcher->attach(&instance->eventLoop());
61+
fcitx_thread = std::thread([] { instance->eventLoop().exec(); });
4462
return;
4563
}
64+
65+
void focusIn() {
66+
return with_fcitx([] { frontend->focusIn(); });
67+
}

keyboard/fcitx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
void startFcitx(const char *bundlePath);
2+
void focusIn();

keyboard/util.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#include <fcitx-utils/eventdispatcher.h>
3+
#include <future>
4+
5+
extern std::unique_ptr<fcitx::EventDispatcher> dispatcher;
6+
7+
template <class F, class T = std::invoke_result_t<F>>
8+
inline T with_fcitx(F func) {
9+
std::promise<T> prom;
10+
std::future<T> fut = prom.get_future();
11+
dispatcher->schedule([&prom, func = std::move(func)]() {
12+
try {
13+
if constexpr (std::is_void_v<T>) {
14+
func();
15+
prom.set_value();
16+
} else {
17+
T result = func();
18+
prom.set_value(std::move(result));
19+
}
20+
} catch (...) {
21+
prom.set_exception(std::current_exception());
22+
}
23+
});
24+
fut.wait();
25+
return fut.get();
26+
}

patches/fcitx5.patch

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ index df15dd57..3c1d3ff2 100644
133133
EXPORT_NAME Core
134134
)
135135
target_include_directories(Fcitx5Core PUBLIC
136+
diff --git a/src/lib/fcitx/addoninfo.cpp b/src/lib/fcitx/addoninfo.cpp
137+
index 49d8c6e4..f6acad08 100644
138+
--- a/src/lib/fcitx/addoninfo.cpp
139+
+++ b/src/lib/fcitx/addoninfo.cpp
140+
@@ -109,7 +109,8 @@ const I18NString &AddonInfo::comment() const {
141+
142+
const std::string &AddonInfo::type() const {
143+
FCITX_D();
144+
- return d->addon->type.value();
145+
+ static const std::string t = "StaticLibrary";
146+
+ return t;
147+
}
148+
149+
AddonCategory AddonInfo::category() const {
136150
diff --git a/src/lib/fcitx/instance.cpp b/src/lib/fcitx/instance.cpp
137151
index d2e9aa23..72f82213 100644
138152
--- a/src/lib/fcitx/instance.cpp
@@ -146,19 +160,88 @@ index d2e9aa23..72f82213 100644
146160

147161
void Instance::configureAddon(const std::string &) {}
148162
diff --git a/src/modules/spell/CMakeLists.txt b/src/modules/spell/CMakeLists.txt
149-
index 094e1a58..ab1ce4df 100644
163+
index 094e1a58..106a010c 100644
150164
--- a/src/modules/spell/CMakeLists.txt
151165
+++ b/src/modules/spell/CMakeLists.txt
152-
@@ -17,6 +17,7 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/spell.conf" DESTINATION "${FCITX_INST
166+
@@ -5,7 +5,7 @@ if (TARGET PkgConfig::Enchant)
167+
set(SPELL_SOURCES ${SPELL_SOURCES} spell-enchant.cpp)
168+
endif()
169+
170+
-add_library(spell MODULE ${SPELL_SOURCES})
171+
+add_library(spell STATIC ${SPELL_SOURCES})
172+
target_link_libraries(spell Fcitx5::Core)
173+
if (TARGET PkgConfig::Enchant)
174+
target_link_libraries(spell PkgConfig::Enchant)
175+
@@ -17,6 +17,17 @@ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/spell.conf" DESTINATION "${FCITX_INST
153176
COMPONENT config)
154177
fcitx5_export_module(Spell TARGET spell BUILD_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}" HEADERS spell_public.h INSTALL)
155178

179+
+add_custom_command(
180+
+ TARGET spell
181+
+ POST_BUILD COMMAND /bin/sh -c
182+
+ \"
183+
+ ${CMAKE_COMMAND} -E copy
184+
+ ${CMAKE_CURRENT_BINARY_DIR}/spell.conf
185+
+ ${CMAKE_BINARY_DIR}/keyboard/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/keyboard.appex/share/fcitx5/addon/spell.conf
186+
+ \"
187+
+)
188+
+
156189
+if (BUILD_SPELL_DICT)
157190
set(DICT_COMP_SRC
158191
comp_spell_dict.cpp
159192
)
160-
@@ -46,3 +47,4 @@ add_custom_command(
193+
@@ -46,3 +57,4 @@ add_custom_command(
161194
"${SPELL_EN_DICT_SRC}" "${SPELL_EN_DICT}")
162195
add_custom_target(spell_en_dict ALL DEPENDS "${SPELL_EN_DICT}")
163196
install(FILES "${SPELL_EN_DICT}" DESTINATION "${FCITX_INSTALL_PKGDATADIR}/spell")
164197
+endif()
198+
diff --git a/src/modules/spell/spell.cpp b/src/modules/spell/spell.cpp
199+
index cc3d951f..a4229234 100644
200+
--- a/src/modules/spell/spell.cpp
201+
+++ b/src/modules/spell/spell.cpp
202+
@@ -7,7 +7,6 @@
203+
204+
#include "spell.h"
205+
#include "fcitx-config/iniparser.h"
206+
-#include "fcitx/addonmanager.h"
207+
#include "config.h"
208+
#include "spell-custom.h"
209+
#ifdef ENABLE_ENCHANT
210+
@@ -110,12 +109,6 @@ Spell::hintForDisplay(const std::string &language, SpellProvider provider,
211+
212+
return iter->second->hint(language, word, limit);
213+
}
214+
-
215+
-class SpellModuleFactory : public AddonFactory {
216+
- AddonInstance *create(AddonManager *manager) override {
217+
- return new Spell(manager->instance());
218+
- }
219+
-};
220+
} // namespace fcitx
221+
222+
FCITX_ADDON_FACTORY(fcitx::SpellModuleFactory)
223+
diff --git a/src/modules/spell/spell.h b/src/modules/spell/spell.h
224+
index 60778962..3cfa2db5 100644
225+
--- a/src/modules/spell/spell.h
226+
+++ b/src/modules/spell/spell.h
227+
@@ -13,6 +13,7 @@
228+
#include "fcitx-utils/i18n.h"
229+
#include "fcitx/addonfactory.h"
230+
#include "fcitx/addoninstance.h"
231+
+#include "fcitx/addonmanager.h"
232+
#include "fcitx/instance.h"
233+
#include "spell_public.h"
234+
235+
@@ -104,6 +105,12 @@ public:
236+
private:
237+
Spell *parent_;
238+
};
239+
+
240+
+class SpellModuleFactory : public AddonFactory {
241+
+ AddonInstance *create(AddonManager *manager) override {
242+
+ return new Spell(manager->instance());
243+
+ }
244+
+};
245+
} // namespace fcitx
246+
247+
#endif // _FCITX_MODULES_SPELL_SPELL_H_

src/CMakeLists.txt

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,11 @@ add_executable(
88
add_custom_command(
99
TARGET ${BUNDLE_NAME}
1010
POST_BUILD COMMAND /bin/sh -c
11-
\"COMMAND_DONE=0 \;
12-
if ${CMAKE_COMMAND} -E copy
11+
\"
12+
${CMAKE_COMMAND} -E copy
1313
${PROJECT_SOURCE_DIR}/assets/${ICON_FILE}
1414
${PROJECT_BINARY_DIR}/src/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/${BUNDLE_NAME}.app
15-
\&\>/dev/null \; then
16-
COMMAND_DONE=1 \;
17-
fi \;
18-
if [ \\$$COMMAND_DONE -eq 0 ] \; then
19-
echo Failed to copy the icon into the app bundle \;
20-
exit 1 \;
21-
fi\"
15+
\"
2216
)
2317
2418
add_dependencies(${BUNDLE_NAME} keyboard)
@@ -30,15 +24,9 @@ set_target_properties(${BUNDLE_NAME} PROPERTIES
3024
add_custom_command(
3125
TARGET ${BUNDLE_NAME}
3226
POST_BUILD COMMAND /bin/sh -c
33-
\"COMMAND_DONE=0 \;
34-
if ${CMAKE_COMMAND} -E copy_directory
27+
\"
28+
${CMAKE_COMMAND} -E copy_directory
3529
${PROJECT_BINARY_DIR}/keyboard/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/keyboard.appex
3630
${PROJECT_BINARY_DIR}/src/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/${BUNDLE_NAME}.app/PlugIns/keyboard.appex
37-
\&\>/dev/null \; then
38-
COMMAND_DONE=1 \;
39-
fi \;
40-
if [ \\$$COMMAND_DONE -eq 0 ] \; then
41-
echo Failed to copy the extension into the app bundle \;
42-
exit 1 \;
43-
fi\"
31+
\"
4432
)

0 commit comments

Comments
 (0)