浏览代码

Merge event module into main

Thomas Dy 7 年之前
父节点
当前提交
e4e024fcf9
共有 2 个文件被更改,包括 36 次插入61 次删除
  1. 0 42
      src/event.rs
  2. 36 19
      src/main.rs

+ 0 - 42
src/event.rs

@@ -1,42 +0,0 @@
-use chan;
-use xcb;
-
-pub enum Event {
-    Ready(xcb::Timestamp),
-    ChildRequest(xcb::Window),
-    ChildDestroyed(xcb::Window),
-    ChildConfigured(xcb::Window, u16, u16)
-}
-
-const CLIENT_MESSAGE: u8 = xcb::CLIENT_MESSAGE | 0x80;
-
-pub fn event_loop(conn: &xcb::Connection, tx: chan::Sender<Event>) {
-    let mut ready = false;
-    loop {
-        match conn.wait_for_event() {
-            Some(event) => match event.response_type() {
-                xcb::PROPERTY_NOTIFY if !ready => {
-                    ready = true;
-                    let event: &xcb::PropertyNotifyEvent = xcb::cast_event(&event);
-                    tx.send(Event::Ready(event.time()));
-                },
-                CLIENT_MESSAGE => {
-                    let event: &xcb::ClientMessageEvent = xcb::cast_event(&event);
-                    let data = event.data().data32();
-                    let window = data[2];
-                    tx.send(Event::ChildRequest(window));
-                },
-                xcb::DESTROY_NOTIFY => {
-                    let event: &xcb::DestroyNotifyEvent = xcb::cast_event(&event);
-                    tx.send(Event::ChildDestroyed(event.window()));
-                },
-                xcb::CONFIGURE_NOTIFY => {
-                    let event: &xcb::ConfigureNotifyEvent = xcb::cast_event(&event);
-                    tx.send(Event::ChildConfigured(event.window(), event.width(), event.height()));
-                },
-                _ => {}
-            },
-            None => { break; }
-        }
-    }
-}

+ 36 - 19
src/main.rs

@@ -5,7 +5,6 @@ extern crate getopts;
 extern crate xcb;
 
 mod atom;
-mod event;
 mod tray;
 
 use std::env;
@@ -18,6 +17,8 @@ const EXIT_WRONG_ARGS: i32 = 1;
 const EXIT_FAILED_CONNECT: i32 = 10;
 const EXIT_FAILED_SELECT: i32 = 11;
 
+const CLIENT_MESSAGE: u8 = xcb::CLIENT_MESSAGE | 0x80;
+
 fn main() {
     process::exit(real_main());
 }
@@ -74,34 +75,50 @@ fn real_main() -> i32 {
             return EXIT_FAILED_SELECT
         }
 
-        let (tx, rx) = chan::sync::<event::Event>(0);
+        let (tx, rx) = chan::sync(0);
         {
             let conn = conn.clone();
             thread::spawn(move || {
-                event::event_loop(&conn, tx);
+                loop {
+                    match conn.wait_for_event() {
+                        Some(event) => { tx.send(event); },
+                        None => { break; }
+                    }
+                }
             });
         }
 
         tray.create();
 
+        let mut ready = false;
         loop {
-            use event::Event::*;
             chan_select!(
-                rx.recv() -> event => match event.unwrap() {
-                    Ready(timestamp) => {
-                        if !tray.take_selection(timestamp) {
-                            println!("Could not take ownership of tray selection. Maybe another tray is also running?");
-                            return EXIT_FAILED_SELECT
-                        }
-                    },
-                    ChildRequest(window) => {
-                        tray.adopt(window);
-                    },
-                    ChildDestroyed(window) => {
-                        tray.forget(window);
-                    },
-                    ChildConfigured(window, width, height) => {
-                        tray.force_size(window, Some((width, height)));
+                rx.recv() -> event => {
+                    let event = event.unwrap();
+                    match event.response_type() {
+                        xcb::PROPERTY_NOTIFY if !ready => {
+                            ready = true;
+                            let event: &xcb::PropertyNotifyEvent = xcb::cast_event(&event);
+                            if !tray.take_selection(event.time()) {
+                                println!("Could not take ownership of tray selection. Maybe another tray is also running?");
+                                return EXIT_FAILED_SELECT
+                            }
+                        },
+                        CLIENT_MESSAGE => {
+                            let event: &xcb::ClientMessageEvent = xcb::cast_event(&event);
+                            let data = event.data().data32();
+                            let window = data[2];
+                            tray.adopt(window);
+                        },
+                        xcb::DESTROY_NOTIFY => {
+                            let event: &xcb::DestroyNotifyEvent = xcb::cast_event(&event);
+                            tray.forget(event.window());
+                        },
+                        xcb::CONFIGURE_NOTIFY => {
+                            let event: &xcb::ConfigureNotifyEvent = xcb::cast_event(&event);
+                            tray.force_size(event.window(), Some((event.width(), event.height())));
+                        },
+                        _ => {}
                     }
                 },
                 signal.recv() => {