Browse Source

Implement making panel window

Thomas Dy 8 years ago
parent
commit
13311001ef
5 changed files with 161 additions and 56 deletions
  1. 6 1
      Cargo.toml
  2. 56 48
      src/main.rs
  3. 42 0
      src/ui/mod.rs
  4. 44 0
      src/ui/panel.rs
  5. 13 7
      src/ui/x11.rs

+ 6 - 1
Cargo.toml

@@ -7,4 +7,9 @@ authors = ["Thomas Dy <thatsmydoing@gmail.com>"]
 time = "0.1"
 toml = "0.1"
 mpd = "0.0.11"
-xcb = "0.7.0"
+chan = "0.1"
+chan-signal = "0.1"
+
+[dependencies.xcb]
+version = "0.7.5"
+features = [ "thread" ]

+ 56 - 48
src/main.rs

@@ -1,58 +1,66 @@
 extern crate xcb;
+#[macro_use]
+extern crate chan;
+extern crate chan_signal;
 
-mod sensors;
-mod comm;
-mod config;
-mod bspwm;
-mod bar;
-mod tray;
-mod store;
-mod music;
-mod wm;
-mod x11;
+//mod sensors;
+//mod comm;
+//mod config;
+//mod bspwm;
+//mod bar;
+//mod tray;
+//mod store;
+//mod music;
+//mod wm;
+//mod x11;
+mod ui;
 
-use comm::{Channel, Message};
-use config::Config;
-use std::env;
-use std::sync::Arc;
-use std::sync::mpsc;
-use std::thread;
+//use comm::{Channel, Message};
+//use config::Config;
+//use std::env;
+//use std::sync::Arc;
+//use std::sync::mpsc;
+use std::process;
+//use std::thread;
 
 fn main() {
-    let config_path = env::args().nth(1).unwrap_or("./panel.toml".to_string());
-    let cfg = config::load(&config_path);
+    let signal = chan_signal::notify(&[chan_signal::Signal::INT, chan_signal::Signal::TERM]);
+//    let config_path = env::args().nth(1).unwrap_or("./panel.toml".to_string());
+//    let cfg = config::load(&config_path);
+//
+//    let mut topbar = bar::Bar::new(true, &cfg);
+//    let _tray = tray::Tray::new();
+//
+//    let cfg = Arc::new(cfg);
+//    let (tx, rx) = mpsc::channel::<Message>();
+//    make_thread(&tx, &cfg, bspwm::bspwm);
+//    make_thread(&tx, &cfg, sensors::sensors);
+//    make_thread(&tx, &cfg, music::music);
+//    make_thread(&tx, &cfg, wm::wm);
+//
+//    let mut data = store::Store::new();
+//
+//    loop {
+//        let msg = rx.recv().unwrap();
+//        data.save(msg);
+//
+//        topbar.send(&format!("{}| {}%{{r}}{}{}",
+//            data.get("desktops"),
+//            data.get("title"),
+//            data.get("sensors"),
+//            data.get("spacer")
+//        ));
+//    }
 
-    let mut topbar = bar::Bar::new(true, &cfg);
-    let _tray = tray::Tray::new();
-
-    let cfg = Arc::new(cfg);
-    let (tx, rx) = mpsc::channel::<Message>();
-    make_thread(&tx, &cfg, bspwm::bspwm);
-    make_thread(&tx, &cfg, sensors::sensors);
-    make_thread(&tx, &cfg, music::music);
-    make_thread(&tx, &cfg, wm::wm);
-
-    let mut data = store::Store::new();
-
-    loop {
-        let msg = rx.recv().unwrap();
-        data.save(msg);
-
-        topbar.send(&format!("{}| {}%{{r}}{}{}",
-            data.get("desktops"),
-            data.get("title"),
-            data.get("sensors"),
-            data.get("spacer")
-        ));
-    }
+    process::exit(ui::ui_main(signal));
 }
 
-fn make_thread(tx: &Channel, cfg: &Arc<Config>, func: fn(&Channel, &Config) -> ()) {
-    let thread_tx = tx.clone();
-    let thread_cfg = cfg.clone();
-    thread::spawn(move || {
-        func(&thread_tx, &thread_cfg);
-    });
-}
+//fn make_thread(tx: &Channel, cfg: &Arc<Config>, func: fn(&Channel, &Config) -> ()) {
+//    let thread_tx = tx.clone();
+//    let thread_cfg = cfg.clone();
+//    thread::spawn(move || {
+//        func(&thread_tx, &thread_cfg);
+//    });
+//}
 
 

+ 42 - 0
src/ui/mod.rs

@@ -0,0 +1,42 @@
+mod panel;
+mod x11;
+
+use chan;
+use chan_signal;
+
+use std::thread;
+
+pub fn ui_main(signal: chan::Receiver<chan_signal::Signal>) -> i32 {
+    if let Some(conn) = x11::Connection::new() {
+
+        let panel = panel::Panel::new(&conn);
+        panel.create();
+
+        let (tx, rx) = chan::sync(0);
+        {
+            let conn = conn.clone_connection();
+            thread::spawn(move || {
+                loop {
+                    match conn.wait_for_event() {
+                        Some(event) => { tx.send(event); },
+                        None => { break; }
+                    }
+                }
+            });
+        }
+
+        loop {
+            chan_select!(
+                rx.recv() -> event => {
+                    if panel.handle_event(event.unwrap()) {
+                        break;
+                    }
+                },
+                signal.recv() => {
+                    panel.finish();
+                }
+            );
+        }
+    }
+    0
+}

+ 44 - 0
src/ui/panel.rs

@@ -0,0 +1,44 @@
+use xcb;
+use ui::x11;
+
+pub struct Panel<'a> {
+    conn: &'a x11::Connection,
+    window: xcb::Window
+}
+
+impl<'a> Panel<'a> {
+    pub fn new(conn: &x11::Connection) -> Panel {
+        Panel {
+            conn: conn,
+            window: conn.generate_id()
+        }
+    }
+
+    pub fn create(&self) {
+        let screen = self.conn.default_screen();
+
+        xcb::create_window(
+            self.conn,
+            xcb::COPY_FROM_PARENT as u8,
+            self.window,
+            screen.root,
+            0, 0,
+            screen.width, 20,
+            0,
+            xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
+            xcb::COPY_FROM_PARENT,
+            &[
+                (xcb::CW_BACK_PIXEL, 0xFF000000),
+                (xcb::CW_EVENT_MASK, xcb::EVENT_MASK_PROPERTY_CHANGE)
+            ]
+        );
+        xcb::map_window(self.conn, self.window);
+        self.conn.flush();
+    }
+
+    pub fn handle_event(&self, _event: xcb::GenericEvent) -> bool {
+        false
+    }
+
+    pub fn finish(&self) {}
+}

+ 13 - 7
src/x11.rs → src/ui/x11.rs

@@ -1,8 +1,11 @@
+#![allow(dead_code)]
+
 use xcb;
 use std::cell::RefCell;
 use std::collections::HashMap;
 use std::mem;
 use std::ops::Deref;
+use std::sync::Arc;
 
 macro_rules! atoms {
     ( $( $x:ident ),* ) => {
@@ -23,7 +26,7 @@ pub struct WinClass {
 }
 
 pub struct Connection {
-    conn: xcb::Connection,
+    conn: Arc<xcb::Connection>,
     default_screen: i32,
     cache: RefCell<HashMap<String, xcb::Atom>>
 }
@@ -32,8 +35,8 @@ impl Connection {
     pub fn new() -> Option<Connection> {
         if let Ok((conn, default_screen)) = xcb::Connection::connect(None) {
             Some(Connection {
+                conn: Arc::new(conn),
                 default_screen: default_screen,
-                conn: conn,
                 cache: RefCell::new(HashMap::new())
             })
         }
@@ -42,6 +45,10 @@ impl Connection {
         }
     }
 
+    pub fn clone_connection(&self) -> Arc<xcb::Connection> {
+        self.conn.clone()
+    }
+
     pub fn atom(&self, name: &str) -> xcb::Atom {
         let mut cache = self.cache.borrow_mut();
         if cache.contains_key(name) {
@@ -83,14 +90,14 @@ impl Connection {
         let cookie = xcb::get_property(&self.conn, false, win, self.atom(_NET_WM_NAME), self.atom(UTF8_STRING), 0, 100);
         let reply = cookie.get_reply();
         let value: &str = match reply {
-            Ok(reply) => unsafe { mem::transmute(reply.value()) },
+            Ok(reply) => unsafe { mem::transmute(reply.value::<u8>()) },
             _ => ""
         };
         if value == "" {
             let cookie = xcb::get_property(&self.conn, false, win, xcb::ATOM_WM_NAME, xcb::ATOM_STRING, 0, 100);
             let reply = cookie.get_reply();
             let value: &str = match reply {
-                Ok(reply) => unsafe { mem::transmute(reply.value()) },
+                Ok(reply) => unsafe { mem::transmute(reply.value::<u8>()) },
                 _ => ""
             };
             value.to_string()
@@ -104,7 +111,7 @@ impl Connection {
         let cookie = xcb::get_property(&self.conn, false, win, xcb::ATOM_WM_CLASS, xcb::ATOM_STRING, 0, 100);
         let reply = cookie.get_reply();
         let value: &str = match reply {
-            Ok(reply) => unsafe { mem::transmute(reply.value()) },
+            Ok(reply) => unsafe { mem::transmute(reply.value::<u8>()) },
             _ => ""
         };
         let parts: Vec<&str> = value.split_terminator('\0').collect();
@@ -157,8 +164,7 @@ impl<'a> Screen<'a> {
             0
         }
         else {
-            let value: &xcb::Window = unsafe { mem::transmute(&(reply.value()[0])) };
-            *value
+            reply.value::<u32>()[0]
         }
     }