jtroo/kanata

Programmatically changing layers

LGUG2Z opened this issue ยท 9 comments

I have a vague idea in my head of being able to programmatically change layers when the focused application changes.

As I mentioned on Reddit, komorebi has an event stream that can be subscribed to and acted upon; the idea I have looks like writing a daemon to respond to FocusChange events from komorebi by triggering LayerChange events in kanata.

The concrete use case I have for this right now is automatically changing to my ff layer for Vim-like navigation in Firefox whenever it takes focus and switching back to my base layer whenever any other application takes focus. I'm sure as I add more and more application-specific layers this sort of functionality would become exponentially more useful and ergonomic for long sessions at the computer.

This feature seems like it could naturally fit into the work that is being done in #44, which the server responding to client messages to trigger layer changes (and maybe other actions in the future?).

Let me know what you think!

I should have checked the open issues before posting this ๐Ÿ˜…

It seems like this issue is also related to what I'm daydreaming about: #40

As you can probably tell I'm quite a big fan of exposing interfaces to facilitate inter-process communication. ๐Ÿš€

jtroo commented

Yea this could definitely build on #44. I've merged it since it was in a good enough state, though one unresolved issue which I think would be good to include in this type of work is:

May need to think about TCP timeout for the clients, e.g. have a heartbeat event sent every 30s (the processing loop can keep track of the timer for this one).

Also handling (and ignoring) the RX on the TCP socket so that the kernel buffers don't fill up.

Can you point me in the right direction for where/how I should change the active layer? I rather naively tried to do this by setting self.prev_layer but it didn't work out ๐Ÿ˜….

20:53:49 [INFO] event received: {"LayerChange": {"new": "ff"}}
20:53:49 [INFO] Entered layer:
(deflayer ff
  @esr _    _    _    _    _    _    _    _    _    _    _    _          _    _    _
  _    _    _    _    _    _    _    _    _    _    _    _    _    _     _    _    _
  _    _    @cw  _    @cr  @tq  _    @cst @iq  @tq  S-f3 @cpu @cpd _     _    _    _
  @cap _    _    _    @fnd @gg  left down up   rght _    _    _
  _    _    _    _    _    _    _    f3    _    _   _    _    @sfq            _
  _    _    _              _                    _   @qwr      _          _    _    _
)
20:53:49 [INFO] Entered layer:
(deflayer qwerty
  @esr _    _    _    _    _    _    _    _    _    _    _    _          _    _    _
  _    _    _    _    _    _    _    _    _    _    _    _    _    _     _    _    _
  _    _    _    _    _    _    _    _    _    _    _    _    _    _     _    _    _
  @cap _    _    _    _    _    _    _    _    _    @scf @'n  _
  _    _    _    _    _    _    _    _    _    _    _    _    @sff            _
  _    _    _              _                    _   @ff       _          _    _    _
)

I can see that self.layout.current_layer() on the Kanata struct returns the index of the currently enabled layer, but I can't figure out how to set that value.

jtroo commented

I believe this function may be what you're looking for.

With the changes in the draft PR, I have a working MVP of a little daemon that changes the kanata layer based on notifications from komorebi. ๐Ÿš€

Definitely very very cool!

#![warn(clippy::all, clippy::nursery, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]

// [dependencies]
// color-eyre = "0.6"
// json_dotpath = "1"
// miow = "0.4"
// parking_lot = "0.12"
// serde = "1"
// serde_json = "1"

use color_eyre::Report;
use color_eyre::Result;
use json_dotpath::DotPaths;
use miow::pipe::NamedPipe;
use parking_lot::Mutex;
use serde_json::json;
use std::io::Read;
use std::io::Write;
use std::net::TcpStream;
use std::process::Command;
use std::sync::Arc;
use std::thread;
use std::thread::sleep;
use std::time::Duration;

fn main() -> Result<()> {
    let mut komokana = Komokana::init()?;
    komokana.listen()?;

    loop {
        sleep(Duration::from_secs(60));
    }
}

pub struct Komokana {
    pub komorebi: Arc<Mutex<NamedPipe>>,
    pub kanata: Arc<Mutex<TcpStream>>,
}

const PIPE: &str = r#"\\.\pipe\"#;

impl Komokana {
    pub fn init() -> Result<Self> {
        let name = "komokana";
        let pipe = format!("{}\\{}", PIPE, name);

        let named_pipe = NamedPipe::new(pipe)?;

        let mut output = Command::new("cmd.exe")
            .args(["/C", "komorebic.exe", "subscribe", name])
            .output()?;

        while !output.status.success() {
            println!(
                "komorebic.exe failed with error code {:?}, retrying in 5 seconds...",
                output.status.code()
            );

            sleep(Duration::from_secs(5));

            output = Command::new("cmd.exe")
                .args(["/C", "komorebic.exe", "subscribe", name])
                .output()?;
        }

        named_pipe.connect()?;

        let stream = TcpStream::connect("localhost:9999")?;

        Ok(Self {
            komorebi: Arc::new(Mutex::new(named_pipe)),
            kanata: Arc::new(Mutex::new(stream)),
        })
    }

    pub fn listen(&mut self) -> Result<()> {
        let pipe = self.komorebi.clone();
        let stream = self.kanata.clone();
        thread::spawn(move || -> Result<()> {
            dbg!("listening now");
            let mut buf = vec![0; 4096];
            loop {
                let mut named_pipe = pipe.lock();
                match (*named_pipe).read(&mut buf) {
                    Ok(bytes_read) => {
                        let data = String::from_utf8(buf[0..bytes_read].to_vec())?;
                        if data == "\n" {
                            continue;
                        }

                        let notification: serde_json::Value = serde_json::from_str(&data)?;

                        if notification.dot_has("event.content.1.exe") {
                            if let Some(exe) =
                                notification.dot_get::<String>("event.content.1.exe")?
                            {
                                let mut stream = stream.lock();

                                #[allow(clippy::single_match_else)]
                                match exe.as_str() {
                                    "firefox.exe" => {
                                        stream.write_all(
                                            json!({
                                                "LayerChange": {
                                                    "new": "ff"
                                                }
                                            })
                                            .to_string()
                                            .as_bytes(),
                                        )?;

                                        println!("set layer to ff");
                                    }
                                    _ => {
                                        stream.write_all(
                                            json!({
                                                "LayerChange": {
                                                    "new": "qwerty"
                                                }
                                            })
                                            .to_string()
                                            .as_bytes(),
                                        )?;

                                        println!("set layer to qwerty");
                                    }
                                }
                            }
                        }
                    }
                    Err(error) => {
                        // Broken pipe
                        if error.raw_os_error().expect("could not get raw os error") == 109 {
                            named_pipe.disconnect()?;

                            let mut output = Command::new("cmd.exe")
                                .args(["/C", "komorebic.exe", "subscribe", "bar"])
                                .output()?;

                            while !output.status.success() {
                                println!(
                                    "komorebic.exe failed with error code {:?}, retrying in 5 seconds...",
                                    output.status.code()
                                );

                                sleep(Duration::from_secs(5));

                                output = Command::new("cmd.exe")
                                    .args(["/C", "komorebic.exe", "subscribe", "bar"])
                                    .output()?;
                            }

                            named_pipe.connect()?;
                        } else {
                            return Err(Report::from(error));
                        }
                    }
                }
            }
        });

        Ok(())
    }
}

I've spent a bit of time getting this little integration to a stable enough place where I can configure it with an external file instead of hardcoding everything. I've pushed what I have here if anyone else wants to try it, but be warned it's not something I'm really supporting for now.

It's a little hacky for now, but I have this running in a tiny command prompt outside of the work area on my screen to also see what the currently active layer is:
image

- exe: "firefox.exe" # when a window of this process is active
  target_layer: "firefox" # switch to this target layer
  title_overrides: # except if the window title matches one of these title rules
    - title: "Slack |"
      strategy: "starts_with"
      target_layer: "firefox-qwerty" # if it does, switch to this target layer
  virtual_key_overrides: # except if a modifier key is being held down at the time that the switch takes place
    - virtual_key_code: 18 # alt aka VK_MENU
      targer_layer: "firefox-alt" # then switch to this layer

In the next few days I'll look into ways of handling TCP timeouts and heartbeats and also make the changes to split the TCP server messages into Server and Client messages.

image

Again, it's not particularly pretty (yet!) , but I've managed to set up a simple widget with yasb that polls and reads the current layer from a file:

widgets:
  kanata:
    type: "yasb.custom.CustomWidget"
    options:
      label: "{data}"
      label_alt: "{data}"
      class_name: "kanata-widget"
      exec_options:
        run_cmd: "cat '%LOCALAPPDATA%\\Temp\\kanata_layer'"
        run_interval: 250
        return_format: "string"

If I get some time and energy I might write a real integration that changes the widget based on the tcp server notifications, but for now this is good enough and gets rid of that ugly command prompt I had open showing logs. ๐Ÿ˜…

jtroo commented

Nice! It's great seeing the cool things you're doing to integrate with kanata ๐Ÿ˜ƒ