Giter Club home page Giter Club logo

Comments (9)

LGUG2Z avatar LGUG2Z commented on July 21, 2024 1

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. 🚀

from kanata.

jtroo avatar jtroo commented on July 21, 2024 1

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

from kanata.

LGUG2Z avatar LGUG2Z commented on July 21, 2024 1

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(())
    }
}

from kanata.

LGUG2Z avatar LGUG2Z commented on July 21, 2024 1

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. 😅

from kanata.

LGUG2Z avatar LGUG2Z commented on July 21, 2024 1

https://github.com/LGUG2Z/komokana It's alive!

from kanata.

jtroo avatar jtroo commented on July 21, 2024

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.

from kanata.

LGUG2Z avatar LGUG2Z commented on July 21, 2024

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.

from kanata.

LGUG2Z avatar LGUG2Z commented on July 21, 2024

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.

from kanata.

jtroo avatar jtroo commented on July 21, 2024

Nice! It's great seeing the cool things you're doing to integrate with kanata 😃

from kanata.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.