Browse Source

Parameterize the whole thing by the actual screen width

Getty Ritter 5 years ago
parent
commit
f040d6bc6e
2 changed files with 100 additions and 73 deletions
  1. 73 70
      src/main.rs
  2. 27 3
      src/window.rs

+ 73 - 70
src/main.rs

@@ -1,92 +1,95 @@
 mod window;
 
-use std::ptr;
-use std::io::Write;
 use std::os::unix::io::AsRawFd;
-
 use pango::LayoutExt;
 
-use window::{Event,Window};
+use window::{Display,Event,Window};
 
 fn main() {
-    unsafe {
-        let mut w = Window::create();
-        w.change_property(
-            "_NET_WM_WINDOW_TYPE",
-            &["_NET_WM_WINDOW_TYPE_DOCK"],
-        );
-
-        w.change_property(
-            "_NET_WM_STRUT_PARTIAL",
-            &[
-                0,    0, 36,  0,
-                0,    0,  0,  0,
-                0, 3840,  0,  0i64,
-            ],
-        );
-        w.change_property(
-            "_NET_WM_STRUT",
-            &[0i64, 0, 36, 0],
-        );
-
-        w.set_title("rbar");
-
-        w.set_input_masks();
-
-        w.set_protocols();
-        w.map();
-
-        let surf = w.get_cairo_surface();
-        let ctx = cairo::Context::new(&surf);
-
-        let window_fd = w.get_fd();
-
-        let mut fds = std::mem::uninitialized();
-        let mut input = format!("Loading...");
-        let stdin_fd = std::io::stdin().as_raw_fd();
-        let mut stdin = std::io::BufReader::new(std::io::stdin());
-        let mut timer = libc::timeval {
-            tv_sec: 5,
-            tv_usec: 0,
-        };
-        let mut log = std::fs::File::create("/home/gdritter/log.txt").unwrap();
-        draw(&ctx, "[1]");
-
-        loop {
-            use std::io::BufRead;
-
+    let mut d = Display::create();
+    let width = d.get_width();
+    let mut w = Window::create(d, width, 36);
+    w.change_property(
+        "_NET_WM_WINDOW_TYPE",
+        &["_NET_WM_WINDOW_TYPE_DOCK"],
+    );
+
+    w.change_property(
+        "_NET_WM_STRUT_PARTIAL",
+        &[
+            0,            0, 36, 0,
+            0,            0,  0, 0,
+            0, width as i64,  0, 0,
+        ],
+    );
+    w.change_property(
+        "_NET_WM_STRUT",
+        &[0i64, 0, 36, 0],
+    );
+
+    w.set_title("rbar");
+
+    w.set_input_masks();
+
+    w.set_protocols();
+    w.map();
+
+    let surf = w.get_cairo_surface();
+    let ctx = cairo::Context::new(&surf);
+
+    let window_fd = w.get_fd();
+
+    let mut fds = unsafe { std::mem::uninitialized() };
+    let mut input = format!("Loading...");
+    let stdin_fd = std::io::stdin().as_raw_fd();
+    let mut stdin = std::io::BufReader::new(std::io::stdin());
+    let mut timer = libc::timeval {
+        tv_sec: 5,
+        tv_usec: 0,
+    };
+    draw(&ctx, "[1]", width);
+
+    loop {
+        use std::io::BufRead;
+
+        unsafe {
             libc::FD_ZERO(&mut fds);
             libc::FD_SET(window_fd, &mut fds);
             libc::FD_SET(stdin_fd, &mut fds);
 
-            libc::select(window_fd + 1, &mut fds, ptr::null_mut(), ptr::null_mut(), &mut timer);
+            libc::select(
+                window_fd + 1,
+                &mut fds,
+                std::ptr::null_mut(),
+                std::ptr::null_mut(),
+                &mut timer,
+            );
+        }
 
-            if libc::FD_ISSET(stdin_fd, &mut fds) {
-                input = String::new();
-                stdin.read_line(&mut input).unwrap();
-                log.write_fmt(format_args!("got {}", input)).unwrap();
-                if input == "" {
-                    break;
-                }
-                draw(&ctx, &input);
+        if unsafe { libc::FD_ISSET(stdin_fd, &mut fds) } {
+            input = String::new();
+            stdin.read_line(&mut input).unwrap();
+            if input == "" {
+                break;
             }
+            draw(&ctx, &input, width);
+        }
 
-            while w.has_events() {
-                draw(&ctx, &input);
-                match w.handle() {
-                    Event::QuitEvent => break,
-                    Event::ShowEvent =>
-                        draw(&ctx, &input),
-                    _e => (),
-                }
+        while w.has_events() {
+            draw(&ctx, &input, width);
+            match w.handle() {
+                Event::QuitEvent => break,
+                Event::ShowEvent =>
+                    draw(&ctx, &input, width),
+                _e => (),
             }
-
         }
+
     }
 }
 
 
-fn draw(ctx: &cairo::Context, left: &str) {
+fn draw(ctx: &cairo::Context, left: &str, width: i32) {
     let now = time::now();
 
     ctx.set_source_rgb(0.1, 0.1, 0.1);
@@ -95,7 +98,7 @@ fn draw(ctx: &cairo::Context, left: &str) {
 
     let layout = pangocairo::functions::create_layout(&ctx).unwrap();
     layout.set_alignment(pango::Alignment::Right);
-    layout.set_width((3840 - 20) * pango::SCALE);
+    layout.set_width((width - 20) * pango::SCALE);
     let mut font = pango::FontDescription::from_string("Fira Mono 18");
     font.set_weight(pango::Weight::Bold);
     layout.set_font_description(&font);

+ 27 - 3
src/window.rs

@@ -4,19 +4,41 @@ use std::ffi::CString;
 use std::{mem,ptr};
 use std::os::raw::{c_int,c_uchar};
 
+pub struct Display {
+    pub display: *mut xlib::_XDisplay,
+    pub screen: i32,
+}
+
+impl Display {
+    pub fn create() -> Display {
+        let display = unsafe { xlib::XOpenDisplay(ptr::null()) };
+        let screen = unsafe { xlib::XDefaultScreen(display) };
+        Display { display, screen }
+    }
+
+    pub fn get_width(&mut self) -> i32 {
+        unsafe {
+            let s = xlib::XScreenOfDisplay(self.display, self.screen);
+            xlib::XWidthOfScreen(s)
+        }
+    }
+}
+
 pub struct Window {
     pub display: *mut xlib::_XDisplay,
     pub screen: i32,
     pub window: u64,
     pub wm_protocols: u64,
     pub wm_delete_window: u64,
+    pub width: i32,
+    pub height: i32,
 }
 
 impl Window {
-    pub fn create() -> Window {
+    pub fn create(d: Display, width: i32, height: i32) -> Window {
         unsafe {
-            let display = xlib::XOpenDisplay(ptr::null());
-            let screen = xlib::XDefaultScreen(display);
+            let display = d.display;
+            let screen = d.screen;
             let window = xlib::XCreateSimpleWindow(
                 display,
                 xlib::XRootWindow(display, screen),
@@ -42,6 +64,8 @@ impl Window {
                 window,
                 wm_protocols,
                 wm_delete_window,
+                width,
+                height,
             }
         }
     }