Skip to content

Commit

Permalink
split sysroot and destdir; bundle and set locale
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Jan 1, 2025
1 parent 6c604d5 commit cc38118
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 21 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ jobs:
-DCMAKE_BUILD_TYPE=Release
EMCC_FORCE_STDLIBS=libc++ FCITX_DISTRIBUTION=fcitx5-js cmake --build build
npm pack
cd build/sysroot/usr && tar cjvf ../../../fcitx5-js-dev.tar.bz2 include lib
mv fcitx5-js-*.tgz fcitx5-js.tgz
cd build/destdir/usr && tar cjvf ../../../fcitx5-js-dev.tar.bz2 include lib
- name: Setup tmate session
if: ${{ failure() }}
Expand All @@ -63,5 +64,5 @@ jobs:
prerelease: true
title: "Nightly Build"
files: |
fcitx5-js-*.tgz
fcitx5-js.tgz
fcitx5-js-dev.tar.bz2
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[Fcitx5](https://github.com/fcitx/fcitx5) input method framework ported to JavaScript with WebAssembly.

The project provides an npm package `fcitx5-js-*.tgz`, which powers derivative apps for end user.
The project provides an npm package `fcitx5-js.tgz`, which powers derivative apps for end user.

Derivative | Note
-|-
Expand Down
2 changes: 1 addition & 1 deletion fcitx5
Submodule fcitx5 updated 137 files
2 changes: 1 addition & 1 deletion fcitx5-webview
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
},
"license": "AGPL-3.0-or-later",
"devDependencies": {
"@antfu/eslint-config": "^3.12.0",
"@antfu/eslint-config": "^3.12.1",
"@types/textarea-caret": "^3.0.3",
"@types/uzip": "^0.20201231.2",
"error-stack-parser": "^2.1.4",
"esbuild": "^0.24.0",
"esbuild": "^0.24.2",
"eslint": "^9.17.0",
"serve": "^14.2.4",
"textarea-caret": "^3.1.0",
Expand Down
3 changes: 2 additions & 1 deletion page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getAddons, getConfig, setConfig } from './config'
import { blur, clickPanel, focus } from './focus'
import { currentInputMethod, getAllInputMethods, getInputMethods, setCurrentInputMethod, setInputMethods } from './input-method'
import { jsKeyToFcitxString, keyEvent } from './keycode'
import { getLocale } from './locale'
import Module from './module'
import { getInstalledPlugins, installPlugin, unzip } from './plugin'

Expand Down Expand Up @@ -54,7 +55,7 @@ window.fcitx = {
getInstalledPlugins,
unzip,
enable() {
Module.ccall('init', 'void', [], [])
Module.ccall('init', 'void', ['string'], [getLocale()])
document.addEventListener('focus', focus, true)
document.addEventListener('blur', blur, true)
document.addEventListener('keydown', keyEvent)
Expand Down
19 changes: 19 additions & 0 deletions page/locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { lsDir } from './fs'

export function getLocale() {
const supported = lsDir('/usr/share/locale')
for (const language of navigator.languages) {
if (language === 'zh-HK' || language === 'zh-TW') {
return 'zh_TW'
}
if (language === 'zh-CN' || language === 'zh-SG') {
return 'zh_CN'
}
const lang = language.split('-')[0]
// en is not in locale dir.
if (lang === 'en' || supported.includes(lang)) {
return lang
}
}
return 'en'
}
5 changes: 3 additions & 2 deletions page/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import UZIP from 'uzip'
import { lsDir, mkdirP } from './fs'
import { getLocale } from './locale'
import Module from './module'

function reload() {
Module.ccall('reload', 'void', [], [])
Module.ccall('reload', 'void', ['string'], [getLocale()])
}

const textDecoder = new TextDecoder()
Expand Down Expand Up @@ -45,7 +46,7 @@ export function installPlugin(buffer: ArrayBuffer) {
if (names.length !== 1) {
throw new Error('Invalid plugin')
}
const name = names[0]
const name = names[0] as string
const byteArray = manifest[`plugin/${name}.json`]
if (!byteArray) {
throw new Error('Invalid plugin')
Expand Down
2 changes: 1 addition & 1 deletion preview/libFcitx5Config.so
2 changes: 1 addition & 1 deletion preview/libFcitx5Core.so
2 changes: 1 addition & 1 deletion preview/libFcitx5Utils.so
2 changes: 1 addition & 1 deletion scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export DESTDIR=build/sysroot
export DESTDIR=build/destdir
cmake --install build/fcitx5
cmake --install build/wasmfrontend
cmake --install build/webpanel
Expand Down
12 changes: 8 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,30 @@ set(EXE_LINK_OPTIONS
"-sENVIRONMENT=web" # disable require("ws") in Fcitx5.js which is rejected by project user's bundler
"--extern-pre-js ${PROJECT_BINARY_DIR}/pre.js"
"--extern-post-js ${PROJECT_BINARY_DIR}/index.js"
"-L${PROJECT_BINARY_DIR}/sysroot/usr/lib"
"-L${PROJECT_BINARY_DIR}/destdir/usr/lib"
Fcitx5::Core
)

set(INSTALL_LIB_DIR "${PROJECT_BINARY_DIR}/destdir/usr/lib")
set(INSTALL_SHARE_DIR "${PROJECT_BINARY_DIR}/destdir/usr/share")

set(STATIC_ADDONS webpanel wasmfrontend)

if (RIME_ONLY)
foreach(ADDON ${STATIC_ADDONS})
# Very small so use embed.
list(APPEND EXE_LINK_OPTIONS
"--embed-file ${PREBUILDER_SHARE_DIR}/fcitx5/addon/${ADDON}.conf@/usr/share/fcitx5/addon/${ADDON}.conf"
"--embed-file ${INSTALL_SHARE_DIR}/fcitx5/addon/${ADDON}.conf@/usr/share/fcitx5/addon/${ADDON}.conf"
)
endforeach()
else()
list(APPEND EXE_LINK_OPTIONS
"--preload-file ${PREBUILDER_SHARE_DIR}/X11@/usr/share/X11"
"--preload-file ${PROJECT_BINARY_DIR}/sysroot${ISOCODES_ISO3166_JSON}@${ISOCODES_ISO3166_JSON}"
"--preload-file ${PROJECT_BINARY_DIR}/sysroot${ISOCODES_ISO639_JSON}@${ISOCODES_ISO639_JSON}"
"--preload-file ${PREBUILDER_SHARE_DIR}/fcitx5@/usr/share/fcitx5"
"--preload-file ${PREBUILDER_LIB_DIR}/fcitx5@/usr/lib/fcitx5"
"--preload-file ${INSTALL_SHARE_DIR}/locale@/usr/share/locale"
"--preload-file ${INSTALL_SHARE_DIR}/fcitx5@/usr/share/fcitx5"
"--preload-file ${INSTALL_LIB_DIR}/fcitx5@/usr/lib/fcitx5"
)
list(APPEND STATIC_ADDONS keyboard)
endif()
Expand Down
9 changes: 6 additions & 3 deletions src/fcitx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ EMSCRIPTEN_KEEPALIVE bool process_key(const char *key, const char *code,
isRelease);
}

EMSCRIPTEN_KEEPALIVE void init() {
EMSCRIPTEN_KEEPALIVE void init(const char *locale) {
if (instance) {
return;
}
Expand All @@ -40,6 +40,9 @@ EMSCRIPTEN_KEEPALIVE void init() {
#else
Log::setLogRule("*=5,notimedate");
#endif

setlocale(LC_ALL, locale); // emscripten musl specific.

EventLoop::setEventLoopFactory(
[] { return std::make_unique<JSEventLoop>(); });
instance = std::make_unique<Instance>(0, nullptr);
Expand All @@ -49,9 +52,9 @@ EMSCRIPTEN_KEEPALIVE void init() {
frontend = dynamic_cast<WasmFrontend *>(addonMgr.addon("wasmfrontend"));
}

EMSCRIPTEN_KEEPALIVE void reload() {
EMSCRIPTEN_KEEPALIVE void reload(const char *locale) {
if (!instance) { // Pre-install plugins.
return init();
return init(locale);
}
instance->reloadConfig();
instance->refresh();
Expand Down

0 comments on commit cc38118

Please sign in to comment.