ninjadev64 / OpenDeck

OpenDeck is a cross-platform desktop application that provides functionality for stream controller devices.
https://discord.gg/26Nf8rHvaj
GNU General Public License v3.0
144 stars 6 forks source link

Changing profiles based on the active window title/class #27

Open teras opened 1 week ago

teras commented 1 week ago

Recapping some info based on previous discussions.

When the focus moved to another application's window then (probably) a new profile should be selected. For example the focus is on FIrefox, now it's on Nemo, now it's on the Desktop (i.e. none).

This is a feature of the app itself, and this is what other applications are doing, and maybe the paradigm should be similar.

StreamController screenshot image

StreamDeck screenshot image

Demo code in rust

Cargo.toml

```toml [package] name = "focus_listener" version = "0.1.0" edition = "2021" [dependencies] x11rb = "0.13.1" ```

test.rs

```rust use std::error::Error; use x11rb::atom_manager; use x11rb::connection::Connection; use x11rb::protocol::xproto::{AtomEnum, ConnectionExt, EventMask, PropertyNotifyEvent, Window}; use x11rb::rust_connection::RustConnection; atom_manager! { pub Atoms: AtomsCookie { _NET_ACTIVE_WINDOW, WM_CLASS, _NET_WM_NAME, WM_NAME, UTF8_STRING, } } struct X11FocusListener { conn: RustConnection, root: Window, atoms: Atoms, last_active_window: Option, } impl X11FocusListener { fn new() -> Result> { let (conn, screen_num) = RustConnection::connect(None)?; let screen = &conn.setup().roots[screen_num]; let root = screen.root; let atoms = Atoms::new(&conn)?.reply()?; conn.change_window_attributes(root, &x11rb::protocol::xproto::ChangeWindowAttributesAux::new() .event_mask(EventMask::PROPERTY_CHANGE | EventMask::SUBSTRUCTURE_NOTIFY))?; for child in conn.query_tree(root)?.reply()?.children { conn.change_window_attributes(child, &x11rb::protocol::xproto::ChangeWindowAttributesAux::new() .event_mask(EventMask::PROPERTY_CHANGE))?; } Ok(Self { conn, root, atoms, last_active_window: None, }) } fn get_next_focus_change(&mut self) -> Result<(String, String), Box> { loop { let event = self.conn.wait_for_event()?; if let x11rb::protocol::Event::PropertyNotify(PropertyNotifyEvent { atom, .. }) = event { if atom == self.atoms._NET_ACTIVE_WINDOW { if let Ok(reply) = self.conn.get_property(false, self.root, self.atoms._NET_ACTIVE_WINDOW, AtomEnum::WINDOW, 0, 1)?.reply() { if let Some(window_id) = reply.value32().and_then(|mut v| v.next()) { if self.last_active_window != Some(window_id) { self.last_active_window = Some(window_id); let wm_class = self.get_window_class(window_id)?; let wm_title = self.get_window_title(window_id)?; return Ok((wm_title.unwrap_or_default(), wm_class.unwrap_or_default())); } } } } } } } fn get_window_class(&self, window: Window) -> Result, Box> { if let Ok(reply) = self.conn.get_property(false, window, self.atoms.WM_CLASS, AtomEnum::STRING, 0, 1024)?.reply() { if let Some(value) = reply.value8() { return Ok(Some(String::from_utf8_lossy(&value.collect::>()).split(' ').filter(|s| !s.is_empty()).collect::>().join("."))); } } Ok(None) } fn get_window_title(&self, window: Window) -> Result, Box> { if let Ok(reply) = self.conn.get_property(false, window, self.atoms._NET_WM_NAME, self.atoms.UTF8_STRING, 0, 1024)?.reply() { if let Some(value) = reply.value8() { return Ok(Some(String::from_utf8_lossy(&value.collect::>()).to_string())); } } if let Ok(reply) = self.conn.get_property(false, window, self.atoms.WM_NAME, AtomEnum::STRING, 0, 1024)?.reply() { if let Some(value) = reply.value8() { return Ok(Some(String::from_utf8_lossy(&value.collect::>()).to_string())); } } Ok(None) } } fn main() -> Result<(), Box> { let mut listener = X11FocusListener::new()?; loop { let (title, class) = listener.get_next_focus_change()?; println!("Class: {}", class); println!("Title: {}", title); } } ```

Some key notes need discussion

ninjadev64 commented 1 week ago

All good :)

Looking forward to it!

teras commented 1 week ago

Actually I am not sure I can do this , at least alone. I know that I requested it, but it's not something I feel I can manage there.

ninjadev64 commented 1 week ago

Well, you've done 75% of the work already! If you have Discord or Matrix, feel free to join the OpenDeck chats and I can show you which bits of the OpenDeck internals you need to interface with ;)

teras commented 1 week ago

I might have an old Discord account. We''l find some time that both of us are online.

ninjadev64 commented 1 week ago

Awesome