4 releases

new 0.1.4 Dec 14, 2024
0.1.3 Dec 14, 2024
0.1.2 Dec 14, 2024
0.1.0 Dec 8, 2024

#635 in Rust patterns

Download history 97/week @ 2024-12-02 314/week @ 2024-12-09

411 downloads per month

MIT license

22KB
401 lines

fcitx5-dbus

Introduce

This crate provides high-level fcitx5 dbus bindings for rust code.
You could use this crate to develop somthing, like IME plugins in editors(vim/emacs).

(The document is coming soon...)

  • architecture
src
├── controller.rs
├── input_context.rs
├── input_method.rs
├── lib.rs
├── utils
│   ├── capability_flags.rs
│   ├── key_event.rs
│   ├── macros.rs
│   ├── mod.rs
│   └── rect.rs
└── virtual_keyboard.rs

Usage

// Cargo.toml
//
// [dependencies]
// fcitx5-dbus = "0.1.2"

use fcitx5_dbus::utils::CapabilityFlag;
use fcitx5_dbus::zbus::{blocking::Connection, Result};
use fcitx5_dbus::{
    controller::ControllerProxyBlocking, input_context::InputContextProxyBlocking,
    input_method::InputMethodProxyBlocking,
};
use std::thread;
use std::time::Duration;

type Ctx<'a> = InputContextProxyBlocking<'a>;

fn main() -> Result<()> {
    let ctx = prepare()?;

    thread::scope(|s| {
        s.spawn(|| input(&ctx));
        s.spawn(|| receive(&ctx));
    });

    Ok(())
}

fn prepare() -> Result<Ctx<'static>> {
    let conn = Connection::session()?;
    let controller = ControllerProxyBlocking::new(&conn)?;
    let input_method = InputMethodProxyBlocking::new(&conn)?;

    let (p, _) = input_method.create_input_context(&[("program", "zellij-my")])?;
    println!("Path is : {}\n", p);

    let ctx = InputContextProxyBlocking::builder(&conn).path(p)?.build()?;
    ctx.set_capability(CapabilityFlag::ClientSideInputPanel)?;
    ctx.focus_in()?;
    controller.toggle()?;

    Ok(ctx)
}

fn input(ctx: &Ctx) -> Result<()> {
    loop {
        wrapper::send_key(ctx, 'a')?;
        thread::sleep(Duration::from_secs(1));
    }
}

fn receive(ctx: &Ctx) -> Result<()> {
    let update_infos = ctx.receive_update_client_side_ui()?;
    for info in update_infos {
        let info = info.args()?;
        println!("{:?}\n", info);
    }
    Ok(())
}

pub(crate) mod wrapper {
    use crate::Ctx;
    use fcitx5_dbus::{utils::key_event, zbus::Result};

    pub(crate) fn send_key(ctx: &Ctx, chr: char) -> Result<()> {
        // let code = fcitx5_dbus::utils::key_event::KeyVal::CHAR_A;
        let code = key_event::KeyVal::from_char(chr);
        let state = key_event::KeyState::NoState;
        ctx.process_key_event(code, 0, state, false, 0)?;
        Ok(())
    }
}

Dependencies

~8–17MB
~265K SLoC