ソースを参照

Try creating/destroying window

Thomas Dy 8 年 前
コミット
c2c159d477
2 ファイル変更60 行追加9 行削除
  1. 4 1
      Cargo.toml
  2. 56 8
      src/main.rs

+ 4 - 1
Cargo.toml

@@ -4,6 +4,9 @@ version = "0.1.0"
 authors = ["Thomas Dy <thatsmydoing@gmail.com>"]
 
 [dependencies]
-xcb = "0.7.5"
 chan = "0.1.18"
 chan-signal = "0.1.6"
+
+[dependencies.xcb]
+version = "0.7.5"
+features = [ "thread" ]

+ 56 - 8
src/main.rs

@@ -1,19 +1,67 @@
 #[macro_use]
 extern crate chan;
 extern crate chan_signal;
+extern crate xcb;
+
+use std::thread;
+use std::sync::Arc;
 
 fn main() {
     let signal = chan_signal::notify(&[chan_signal::Signal::INT, chan_signal::Signal::TERM]);
 
-    println!("Hello, world!");
+    if let Ok((conn, preferred)) = xcb::Connection::connect(None) {
+        let conn = Arc::new(conn);
+
+        let setup = conn.get_setup();
+        let screen = setup.roots().nth(preferred as usize).unwrap();
 
-    loop {
-        chan_select!(
-            signal.recv() => {
-                break;
-            }
+        let window = conn.generate_id();
+        xcb::create_window(
+            &conn,
+            xcb::COPY_FROM_PARENT as u8,
+            window,
+            screen.root(),
+            0, 0,
+            20, 20,
+            0,
+            xcb::WINDOW_CLASS_INPUT_OUTPUT as u16,
+            screen.root_visual(),
+            &[
+                (xcb::CW_BACK_PIXEL, screen.black_pixel()),
+                (xcb::CW_EVENT_MASK, xcb::EVENT_MASK_EXPOSURE)
+            ]
         );
-    }
+        xcb::map_window(&conn, window);
+        conn.flush();
+
+        {
+            let conn = conn.clone();
+            thread::spawn(move || {
+                loop {
+                    match conn.wait_for_event() {
+                        Some(event) => match event.response_type() {
+                            xcb::EXPOSE => { println!("expose") },
+                            _ => {}
+                        },
+                        None => { break; }
+                    }
+                }
+            });
+        }
 
-    println!("Cleaning up...");
+        loop {
+            chan_select!(
+                signal.recv() => {
+                    break;
+                }
+            );
+        }
+
+        // cleanup code
+        xcb::destroy_window(&conn, window);
+        conn.flush();
+    }
+    else {
+        println!("Could not connect to X server!");
+    }
 }