fcitx / fcitx5-rime

218 stars 22 forks source link

请教如何从命令行控制 fcitx-rime 的算法服务? #42

Closed yanhuacuo closed 2 years ago

yanhuacuo commented 2 years ago

我们在开发一款可视化的 rime 助手 : 中州韵98五笔助手

现在有一个需求是这样的,我们期望在 rime 助手可视化管理词库时, librime 的算法服务可以使用 QProcess(Qt类库) 控制退出或唤醒,因为 librime 在运行时,它实际上在占用着「上屏历史文件」,只有算服务服退出了,它才会真正解除对这个文件的占用。

我查到 fcitx5 有一些内置命令,如下,似乎 fcitx5 --disable rime 应该是可用的,但实际上它并不能结束掉 fcitx5-rime 的相关进程。

--replace 会重新加载,-s + seconds 的功能没看明白,没有找到合适的方法。 大佬,fcitx5-rime 是否有 Squirrel --reloadSquirrel --exit 类似的 API ?

i7@debian:~$ fcitx5 -h
Usage: fcitx5 [Option]
  --disable <addon names>       A comma separated list of addons to be disabled.
                                "all" can be used to disable all addons.
  --enable <addon names>        A comma separated list of addons to be enabled.
                                "all" can be used to enable all addons.
                                This value will override the value in the flag --disable.
  --verbose <logging rule>      Set the logging rule for displaying message.
                                E.g. category1=level1,category2=level2
                                "*" may be used to represent all logging category.
  -u, --ui <addon name>         Set the UI addon to be used.
  -d                            Run as a daemon.
  -D                            Do not run as a daemon (default).
  -s <seconds>                  Number of seconds to wait before start.
  -k, --keep                    Keep running even the main display is disconnected.
  -r, --replace                 Replace the existing instance.
  -v, --version                 Show version and quit.
  -h, --help                    Show this help message and quit.
wengxt commented 2 years ago

用 QDBus: 调用 org.fcitx.Fcitx5 上 /controller 对象的 org.fcitx.Fcitx.Controller1.SetConfig 方法

参数 fcitx://config/addon/rime/deploy ''

yanhuacuo commented 2 years ago

用 QDBus: 调用 org.fcitx.Fcitx5 上 /controller 对象的 org.fcitx.Fcitx.Controller1.SetConfig 方法

参数 fcitx://config/addon/rime/deploy ''

感谢回复,

下面的指令,让 fcitx5-rime 重新部署是有效的:

qdbus org.fcitx.Fcitx5 /controller org.fcitx.Fcitx.Controller1.SetConfig "fcitx://config/addon/rime/deploy" ""

注意到 fcitx5/blob/master/src/modules/dbus/dbusmodule.cpp 中有定义一些公有成员,比如 exit()restart() ,但是尝试仿写无效。

qdbus org.fcitx.Fcitx5 /controller org.fcitx.Fcitx.Controller1.SetConfig "fcitx://config/addon/rime/exit" ""

qdbus org.fcitx.Fcitx5 /controller org.fcitx.Fcitx.Controller1.SetConfig "fcitx://config/addon/rime" "exit"`

qdbus org.fcitx.Fcitx5 /controller org.fcitx.Fcitx.Controller1.SetConfig "fcitx://config/addon/rime" "exit()"

又或

qdbus org.fcitx.Fcitx5 /controller org.fcitx.Fcitx.Controller1.SetConfig "fcitx://config/addon/rime/exit()" ""

都不能让 fcitx5-rime 的 librime 引擎退出。

yanhuacuo commented 2 years ago

求指点正确的用法

yanhuacuo commented 2 years ago

目前使用较为丑陋的 bash 管道命令

void MainWindow::on_btnFcitxOFF_clicked()
{
    QProcess process;
    process.startDetached("/bin/bash", {"-c" , "kill `ps -A | grep fcitx5 | awk '{print $1}'`"});

}

void MainWindow::on_btnFcitxON_clicked()
{
    QProcess process;
    QString b = QString("\"%1\" ").arg("fcitx://config/addon/rime/deploy");
    QString a = "qdbus org.fcitx.Fcitx5 /controller org.fcitx.Fcitx.Controller1.SetConfig ";
    QString c =  "\"\"";
    QString d = a+b+c;

    process.startDetached("/bin/bash", {"-c" , "fcitx5 -d"});
    process.startDetached("/bin/bash", {"-c" , d});

}
wengxt commented 2 years ago

@yanhuacuo 为什么要退出,我给你的方法和squirrel —reload是等价的

yanhuacuo commented 2 years ago

@yanhuacuo 为什么要退出,我给你的方法和squirrel —reload是等价的,但我期望有与 Squirrel --exit 等价的实现。

因为只有 librime 的进程退出后,它才会真正释放上屏记录文件( xxx.txt )。仅仅是 reload 的话,每一次都只是在本地生成一份 xxx.txt 。这而份 xxx.txt 里的条目,是能被打出来的。

譬如有一条词条 风花雪月,一旦用户「打过」该词,它就会被收入到上屏记录中,当用户想删掉它时,无论是在 dict.yaml 中删,还是去 build 中清空 bin,都不能实现目的,因为 reload 时,它会重新生成一份 xxx.txt。

所以解决办法就是让 librime 的进程暂时退出,清掉 build 中的 bin 文件,这时对 xxx.txt 的操控,会成为 librime 再次启动时加载上屏记录的唯一依据。

yanhuacuo commented 2 years ago

weasel 和 squirrel ,都有 exit 指令,可以暂退 librime,在 fcitx 上,我只实现了用 shell 这种比较有入侵性的方式达成上述目的。

yanhuacuo commented 2 years ago

已经解决了这个问题,不需要杀进程了。 :)

nkta3m commented 1 year ago

请问是怎么解决的?

sci-42ver commented 6 months ago

@nkta3m 看到您在好几个 issue 里问类似的问题。

我感觉 OP 的方法应该是 qdbus org.fcitx.Fcitx5 /controller org.fcitx.Fcitx.Controller1.SetConfig fcitx://config/addon/rime/sync ''

所以解决办法就是让 librime 的进程暂时退出,清掉 build 中的 bin 文件,这时对 xxx.txt 的操控,会成为 librime 再次启动时加载上屏记录的唯一依据。

find ~/.local/share/fcitx5/rime -name "rime_ice*.userdb*" | xargs -I{} rm -rf {} 然后 sync,之前的 rime_ice 记录会都清空。


不过注意,Fcitx5 应该是不会特定处理诸如 LOCK 的问题 hchunhui/librime-lua#330,需要用户自己避免 LOCK 问题。所以这个 comment 我一开始想问yanhuacuo怎么处理他的问题的


P.s. kde6 里如果一定要重启 Fcitx5 的话(一般情况不需要,上述 sync 基本可以重新导入 rime yaml 配置),关掉 Fcitx5 后,暂时可以在 systemsettings kcm_virtualkeyboard 里面先 apply 'Fcitx 5 Wayland Launcher',然后再 apply 'Fcitx 5',可以用 (y)dotool 自动化。