The Road to Fcitx 5: 6. Addon Loader

Due to a frequently request about adding scripting support to Fcitx, I tried to make Fcitx 5 able to be extended in a different way.

During Fcitx 4 times, the addon must be written with Shared library and those assumptions are hard-coded. In Fcitx 5, I added an another abstraction called Addon Loader, which is responsible for the actual addon loading.

The builtin addon loader enables addon to be statically linked into Fcitx, which avoids the inconsistency of library and addon version, and it makes some important addon always available. And the shared library addon loader just loads the addon like the old days.

And starting from today, there can be a new addon type Lua to be used to extend Fcitx. In old Fcitx 4 days, lua addon is yet another shared library addon, instead of an “lua” typed addon. With the new addon loader mechanism, lua addon loader will do all the C++ coding work, and people can simply write pure lua addon and enable/disable them just like any other addons.

In order to add support of the Google Pinyin’s API back, fcitx5-lua also comes with a imeapi addon, which is purely lua (Though, I did wrote some code specifically to it to support all APIs, because the full Google Pinyin API contains some weird helper function like utf8 to utf16 conversion.).

So either, you can put a old days lua file to share/fcitx5/lua/imeapi/extensions , so imeapi will load it for you. Or you can write your own standalone lua Addon, by having a regular addon config file.

Right now, due to missing the real use case for lua addon, the API exposed by lua addon loader is quite limited to only covers imeapi’s use case. Documentation of existing API can be found at https://fcitx.github.io/fcitx5-lua/index.html. In the future, based on the use case we want to support more API will be added to it.

Right now, imeapi is designed to bridge the api to quickphrase and pinyin in chinese addons (register_trigger + candidate string will only works for pinyin).

Also, this lua addon loader enables to invoke any lua functions from C++ side. The arguments and return values are passed with RawConfig in Fcitx to avoid any ABI issues. In this case, Pinyin just simply invokes a function in imeapi to send over the candidates string. In old Fcitx 4 days, such thing are usually done by addon actively scan the candidate list, these kinds of style is not encouraged anymore in Fcitx 5. Instead, if certain functionality can be extended, it should be exposed by the addon itself (E.g. quickphrase now supports to be extended by other addons, and that’s how it works with luad addon lodaer),

As for extending addon loader, while there is no plan to add other addon type, code contribution are welcomed for things like Python, etc. Or even a “Type=DBus” addon that simply bridges all the call by dbus to another process.

This entry was posted in fcitx development and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.