Comments (9)
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.
I believe this function may be what you're looking for.
from kanata.
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.
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.
https://github.com/LGUG2Z/komokana It's alive!
from kanata.
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.
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.
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.
from kanata.
Nice! It's great seeing the cool things you're doing to integrate with kanata 😃
from kanata.
Related Issues (20)
- Bug: Release 1.6.1 macos_x86_64 executable isn't what it says. HOT 6
- Feature request: Show keystroke or can let other app know the real key is stroked. HOT 2
- Feature request: Can we have a more flexible unmod? HOT 1
- See if windows shift workaround can be compiled out for winiov2 HOT 1
- Bug: kanata does not work properly with listary HOT 3
- `release-key` releases both sides of a mod and not just one, e.g. `lctl` and `rctl` or `lmet` and `rmet` HOT 7
- Feature request: Compile-time conditional mappings HOT 1
- chordsv2 activation does not trigger early interruption of `tap-hold-press|release` HOT 9
- make macro-release-cancel also cancel virtual keys HOT 1
- Bug: switch's layer logic not recognized HOT 2
- Feature request: remove dependency on AutohotKey for EnableUIAccess
- Bluetooth keyboard cannot use without Chicony Keyboard attached on Thinkpad X1 Tablet Gen 3 HOT 9
- unable to set linux-use-trackpoint-propety HOT 3
- one-shot shift interferes with defoverrides HOT 5
- Feature request: chord toggle action HOT 1
- Feature request: homebrew
- Bug: special buttons on function row not working on macos HOT 2
- Bug: chord v2 overlapping chords HOT 2
- Bug: tray icons change randomly when layer changes by Meta key.
- Bug: S-home does not work as expected HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kanata.