-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Programmatically changing layers #47
Comments
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. 🚀 |
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:
|
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
I can see that |
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 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: - 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. |
This commit sets a 30-second keep-alive on opened TcpStreams (using net2::TcpStreamExt as keep-alive functionality is not currently in the standard library) and splits the previous EventNotification enum into a ServerMessage and a ClientMessage enum respectively. I have changed the semantics between ServerMessage and ClientMessage slightly so that ClientMessage variants are imperative requests that align closer with the names of the handler functions, ie. ChangeLayer calls fn change_layer(). Whenever a client's TcpStream cannot be written to, either because it has notified the server of a disconnect or because it has failed the keep-alive, it will be removed from the connections HashMap on the TcpServer struct. re jtroo#47
This commit sets a 30-second keep-alive on opened TcpStreams (using net2::TcpStreamExt as keep-alive functionality is not currently in the standard library) and splits the previous EventNotification enum into a ServerMessage and a ClientMessage enum respectively. I have changed the semantics between ServerMessage and ClientMessage slightly so that ClientMessage variants are imperative requests that align closer with the names of the handler functions, ie. ChangeLayer calls fn change_layer(). Whenever a client's TcpStream cannot be written to, either because it has notified the server of a disconnect or because it has failed the keep-alive, it will be removed from the connections HashMap on the TcpServer struct. re jtroo#47
This commit sets a 30-second keep-alive on opened TcpStreams (using net2::TcpStreamExt as keep-alive functionality is not currently in the standard library) and splits the previous EventNotification enum into a ServerMessage and a ClientMessage enum respectively. I have changed the semantics between ServerMessage and ClientMessage slightly so that ClientMessage variants are imperative requests that align closer with the names of the handler functions, ie. ChangeLayer calls fn change_layer(). Whenever a client's TcpStream cannot be written to, either because it has notified the server of a disconnect or because it has failed the keep-alive, it will be removed from the connections HashMap on the TcpServer struct. re jtroo#47
Again, it's not particularly pretty (yet!) , but I've managed to set up a simple widget with 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. 😅 |
Nice! It's great seeing the cool things you're doing to integrate with kanata 😃 |
This commit sets a 30-second keep-alive on opened TcpStreams (using net2::TcpStreamExt as keep-alive functionality is not currently in the standard library) and splits the previous EventNotification enum into a ServerMessage and a ClientMessage enum respectively. I have changed the semantics between ServerMessage and ClientMessage slightly so that ClientMessage variants are imperative requests that align closer with the names of the handler functions, ie. ChangeLayer calls fn change_layer(). Whenever a client's TcpStream cannot be written to, either because it has notified the server of a disconnect or because it has failed the keep-alive, it will be removed from the connections HashMap on the TcpServer struct. re #47
https://github.com/LGUG2Z/komokana It's alive! |
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 toFocusChange
events fromkomorebi
by triggeringLayerChange
events inkanata
.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!
The text was updated successfully, but these errors were encountered: