Fcitx 5 Documentation

repository·master·Indexed 25 days ago

https://github.com/fcitx/fcitx5

A next-generation, generic input method framework for Linux and BSD. It provides core infrastructure for keyboard layouts and serves as a base for language-specific input engines. The framework supports X11 and Wayland, and includes developer utilities for managing hierarchical configurations via RawConfig, DBus connectivity through the Bus class, and keyboard event handling via the fcitx::Key class.

Tokens
7.5K
Snippets
2
Records
46
Agent score
81%

What's inside Fcitx 5

  1. Understand Fcitx 5 Engines

    master

    The core fcitx5 repository contains only the keyboard layout engine. To support specific languages such as Chinese, Japanese, or Korean, you must install corresponding input method engines separately.

    A complete list of available input method engines can be found on the Fcitx Wiki.

  2. Use RawConfig to manage hierarchical configuration

    master

    The RawConfig class provides a hierarchical tree structure for managing key-value pairs, suitable for formats like INI. Each node in the tree can contain a value, a comment, and sub-items (child nodes). Paths are represented using / as a separator.

    Key capabilities include:

    • Path-based access: Retrieve or create nodes using string paths (e.g., "group/subgroup/key").
    • Implicit values: Mark nodes as implicit so they act as default values and are not written to the physical INI file when saved.
    • Tree traversal: Visit sub-items recursively or via specific paths using callbacks.
    • Value assignment: Use the [] operator or setValueByPath to modify the configuration tree.
  3. Implement an Addon by subclassing AddonInstance

    master

    To create a new plugin or addon in fcitx5, you must create a subclass of fcitx::AddonInstance.

    Key lifecycle methods you can override:

    • reloadConfig(): Called to reload configuration from disk.
    • save(): Called to save relevant data (typically when fcitx exits).
    • getConfig(): Returns a pointer to the addon's Configuration.
    • setConfig(const RawConfig &): Sets configuration from a RawConfig object.

    To make your addon discoverable as a SharedLibrary addon, you must also use the FCITX_ADDON_FACTORY(ClassName) macro to export the factory.

  4. Handle InputContext events

    master

    Fcitx5 uses an event-driven system for interacting with input contexts. Most events related to a specific client (like focus changes, key presses, or capability updates) are subclasses of InputContextEvent. You can identify if an event is an input context event using isInputContextEvent().

    Common input context events include:

    • FocusInEvent: Triggered when a client gains focus.
    • FocusOutEvent: Triggered when a client loses focus. Note that by default, Fcitx may commit preedit text during this event depending on the Output/DontCommitPreeditWhenUnfocus configuration.
    • KeyEvent: Represents a key press or release.
    • CommitStringEvent: Contains text to be committed to the client.
    • InputContextSwitchInputMethodEvent: Triggered when the user switches input methods manually.
  5. Understand the InputContext abstraction

    master

    An InputContext represents a client of Fcitx, such as a window or a specific text field within an application. It serves as the primary interface for communication between the Fcitx core (and input methods) and the application frontend.

    Key responsibilities include:

    • Managing focus: Tracking whether the client has input focus via focusIn() and focusOut().
    • Handling text: Committing strings to the client via commitString() or commitStringWithCursor(), and managing surrounding text via surroundingText().
    • Event forwarding: Sending key events to the client using forwardKey().
    • UI Coordination: Notifying the client of preedit changes via updatePreedit() and coordinating with the InputPanel and StatusArea.

    Developers implementing a new frontend should inherit from InputContext (or InputContextV2 if cursor-based commits are required) and implement the protected virtual methods like commitStringImpl, deleteSurroundingTextImpl, and forwardKeyImpl to bridge Fcitx commands to the application's actual text handling logic.

  6. Export functions from an Addon for use by other addons

    master

    Addons can export specific functions that other addons can invoke. This requires a three-step process involving CMake, a header declaration, and the implementation macro.

    1. CMake Configuration: Use fcitx5_export_module in your CMakeLists.txt to export the module, including its public headers.

    2. Header Declaration: Use FCITX_ADDON_DECLARE_FUNCTION in a public header to define the function signature and create a meta-type for it.

    3. Implementation: Use FCITX_ADDON_EXPORT_FUNCTION inside your addon class to register the function. This macro performs a compile-time check to ensure the implementation matches the declared signature.

    Example Implementation:

    // In dummyaddon_public.h
    FCITX_ADDON_DECLARE_FUNCTION(DummyAddon, addOne, int(int));
    
    // In dummyaddon.cpp
    class DummyAddon : public fcitx::AddonInstance {
    public:
        int addOne(int a) { return a + 1; }
    
        FCITX_ADDON_EXPORT_FUNCTION(DummyAddon, addOne);
    };
    FCITX_ADDON_DECLARE_FUNCTION(DummyAddon, addOne, int(int));
    
    class DummyAddon : public fcitx::AddonInstance {
    public:
        int addOne(int a) { return a + 1; }
    
        FCITX_ADDON_EXPORT_FUNCTION(DummyAddon, addOne);
    };
  7. Invoke exported functions from another Addon

    master

    To call a function exported by another addon, first obtain a pointer to that addon via the AddonManager. Then, use the .call<...>() template method, passing the appropriate meta-type generated by the FCITX_ADDON_DECLARE_FUNCTION macro.

    // Assuming DummyAddon is already obtained via AddonManager
    int result = addon->call<fcitx::IDummyAddon::addOne>(7);
    addon->call<fcitx::IDummyAddon::addOne>(7);
  8. Use FCITX_ADDON_DEPENDENCY_LOADER to simplify addon access

    master

    The FCITX_ADDON_DEPENDENCY_LOADER macro provides a convenient way to lazily load and access a dependency addon within your own addon code without manually managing the AddonManager calls every time.

    // Inside your addon class or implementation file
    FCITX_ADDON_DEPENDENCY_LOADER(TargetAddonName, MyAddonManager)
    
    void someFunction() {
        // TargetAddonName() returns the pointer to the addon instance
        if (TargetAddonName()) {
            // use the addon
        }
    }