File: unity_focused_window.rs

package info (click to toggle)
rust-dbus 0.9.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: makefile: 2
file content (28 lines) | stat: -rw-r--r-- 956 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
extern crate dbus;

// Tracks currently focused window under the Unity desktop by listening to the
// FocusedWindowChanged signal. The signal contains "window_id", "app_id" and "stage",
// we print only "app_id".

use dbus::{ffidisp::Connection, Message, MessageType};

fn focus_msg(msg: &Message) -> Option<&str> {
    if msg.msg_type() != MessageType::Signal { return None };
    if &*msg.interface().unwrap() != "com.canonical.Unity.WindowStack" { return None };
    if &*msg.member().unwrap() != "FocusedWindowChanged" { return None };
    let (_, app) = msg.get2::<u32, &str>();
    app
}

fn main() {
    let c = Connection::new_session().unwrap();
    c.add_match("interface='com.canonical.Unity.WindowStack',member='FocusedWindowChanged'").unwrap();

    loop {
        if let Some(msg) = c.incoming(1000).next() {
            if let Some(app) = focus_msg(&msg) {
                println!("{} has now focus.", app);
            }
        }
    }
}