gdritter repos knurling / f040d6b
Parameterize the whole thing by the actual screen width Getty Ritter 5 years ago
2 changed file(s) with 92 addition(s) and 65 deletion(s). Collapse all Expand all
11 mod window;
22
3 use std::ptr;
4 use std::io::Write;
53 use std::os::unix::io::AsRawFd;
6
74 use pango::LayoutExt;
85
9 use window::{Event,Window};
6 use window::{Display,Event,Window};
107
118 fn main() {
12 unsafe {
13 let mut w = Window::create();
14 w.change_property(
15 "_NET_WM_WINDOW_TYPE",
16 &["_NET_WM_WINDOW_TYPE_DOCK"],
17 );
9 let mut d = Display::create();
10 let width = d.get_width();
11 let mut w = Window::create(d, width, 36);
12 w.change_property(
13 "_NET_WM_WINDOW_TYPE",
14 &["_NET_WM_WINDOW_TYPE_DOCK"],
15 );
1816
19 w.change_property(
20 "_NET_WM_STRUT_PARTIAL",
21 &[
22 0, 0, 36, 0,
23 0, 0, 0, 0,
24 0, 3840, 0, 0i64,
25 ],
26 );
27 w.change_property(
28 "_NET_WM_STRUT",
29 &[0i64, 0, 36, 0],
30 );
17 w.change_property(
18 "_NET_WM_STRUT_PARTIAL",
19 &[
20 0, 0, 36, 0,
21 0, 0, 0, 0,
22 0, width as i64, 0, 0,
23 ],
24 );
25 w.change_property(
26 "_NET_WM_STRUT",
27 &[0i64, 0, 36, 0],
28 );
3129
32 w.set_title("rbar");
30 w.set_title("rbar");
3331
34 w.set_input_masks();
32 w.set_input_masks();
3533
36 w.set_protocols();
37 w.map();
34 w.set_protocols();
35 w.map();
3836
39 let surf = w.get_cairo_surface();
40 let ctx = cairo::Context::new(&surf);
37 let surf = w.get_cairo_surface();
38 let ctx = cairo::Context::new(&surf);
4139
42 let window_fd = w.get_fd();
40 let window_fd = w.get_fd();
4341
44 let mut fds = std::mem::uninitialized();
45 let mut input = format!("Loading...");
46 let stdin_fd = std::io::stdin().as_raw_fd();
47 let mut stdin = std::io::BufReader::new(std::io::stdin());
48 let mut timer = libc::timeval {
49 tv_sec: 5,
50 tv_usec: 0,
51 };
52 let mut log = std::fs::File::create("/home/gdritter/log.txt").unwrap();
53 draw(&ctx, "[1]");
42 let mut fds = unsafe { std::mem::uninitialized() };
43 let mut input = format!("Loading...");
44 let stdin_fd = std::io::stdin().as_raw_fd();
45 let mut stdin = std::io::BufReader::new(std::io::stdin());
46 let mut timer = libc::timeval {
47 tv_sec: 5,
48 tv_usec: 0,
49 };
50 draw(&ctx, "[1]", width);
5451
55 loop {
56 use std::io::BufRead;
52 loop {
53 use std::io::BufRead;
5754
55 unsafe {
5856 libc::FD_ZERO(&mut fds);
5957 libc::FD_SET(window_fd, &mut fds);
6058 libc::FD_SET(stdin_fd, &mut fds);
6159
62 libc::select(window_fd + 1, &mut fds, ptr::null_mut(), ptr::null_mut(), &mut timer);
60 libc::select(
61 window_fd + 1,
62 &mut fds,
63 std::ptr::null_mut(),
64 std::ptr::null_mut(),
65 &mut timer,
66 );
67 }
6368
64 if libc::FD_ISSET(stdin_fd, &mut fds) {
65 input = String::new();
66 stdin.read_line(&mut input).unwrap();
67 log.write_fmt(format_args!("got {}", input)).unwrap();
68 if input == "" {
69 break;
70 }
71 draw(&ctx, &input);
69 if unsafe { libc::FD_ISSET(stdin_fd, &mut fds) } {
70 input = String::new();
71 stdin.read_line(&mut input).unwrap();
72 if input == "" {
73 break;
7274 }
75 draw(&ctx, &input, width);
76 }
7377
74 while w.has_events() {
75 draw(&ctx, &input);
76 match w.handle() {
77 Event::QuitEvent => break,
78 Event::ShowEvent =>
79 draw(&ctx, &input),
80 _e => (),
81 }
78 while w.has_events() {
79 draw(&ctx, &input, width);
80 match w.handle() {
81 Event::QuitEvent => break,
82 Event::ShowEvent =>
83 draw(&ctx, &input, width),
84 _e => (),
8285 }
86 }
8387
84 }
8588 }
8689 }
8790
8891
89 fn draw(ctx: &cairo::Context, left: &str) {
92 fn draw(ctx: &cairo::Context, left: &str, width: i32) {
9093 let now = time::now();
9194
9295 ctx.set_source_rgb(0.1, 0.1, 0.1);
9598
9699 let layout = pangocairo::functions::create_layout(&ctx).unwrap();
97100 layout.set_alignment(pango::Alignment::Right);
98 layout.set_width((3840 - 20) * pango::SCALE);
101 layout.set_width((width - 20) * pango::SCALE);
99102 let mut font = pango::FontDescription::from_string("Fira Mono 18");
100103 font.set_weight(pango::Weight::Bold);
101104 layout.set_font_description(&font);
33 use std::ffi::CString;
44 use std::{mem,ptr};
55 use std::os::raw::{c_int,c_uchar};
6
7 pub struct Display {
8 pub display: *mut xlib::_XDisplay,
9 pub screen: i32,
10 }
11
12 impl Display {
13 pub fn create() -> Display {
14 let display = unsafe { xlib::XOpenDisplay(ptr::null()) };
15 let screen = unsafe { xlib::XDefaultScreen(display) };
16 Display { display, screen }
17 }
18
19 pub fn get_width(&mut self) -> i32 {
20 unsafe {
21 let s = xlib::XScreenOfDisplay(self.display, self.screen);
22 xlib::XWidthOfScreen(s)
23 }
24 }
25 }
626
727 pub struct Window {
828 pub display: *mut xlib::_XDisplay,
1030 pub window: u64,
1131 pub wm_protocols: u64,
1232 pub wm_delete_window: u64,
33 pub width: i32,
34 pub height: i32,
1335 }
1436
1537 impl Window {
16 pub fn create() -> Window {
17 unsafe {
18 let display = xlib::XOpenDisplay(ptr::null());
19 let screen = xlib::XDefaultScreen(display);
38 pub fn create(d: Display, width: i32, height: i32) -> Window {
39 unsafe {
40 let display = d.display;
41 let screen = d.screen;
2042 let window = xlib::XCreateSimpleWindow(
2143 display,
2244 xlib::XRootWindow(display, screen),
4264 window,
4365 wm_protocols,
4466 wm_delete_window,
67 width,
68 height,
4569 }
4670 }
4771 }