main.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #[macro_use]
  2. extern crate failure;
  3. mod widgets;
  4. mod window;
  5. use std::os::unix::io::AsRawFd;
  6. use pango::LayoutExt;
  7. use widgets::Widget;
  8. use window::{Display,Event,Size,Window};
  9. fn main() -> Result<(), failure::Error> {
  10. // set up the display and the window
  11. let mut d = Display::create()?;
  12. let size = Size {
  13. wd: d.get_width(),
  14. // TODO: this should be a function of font size
  15. ht: 36,
  16. };
  17. let mut w = Window::create(d, size)?;
  18. // set some window-manager properties: this is a dock
  19. w.change_property("_NET_WM_WINDOW_TYPE", &["_NET_WM_WINDOW_TYPE_DOCK"])?;
  20. // ...and should push other windows out of the way
  21. w.change_property("_NET_WM_STRUT", &[0i64, 0, size.ht as i64, 0])?;
  22. w.change_property(
  23. "_NET_WM_STRUT_PARTIAL",
  24. &[ 0, 0, size.ht as i64, 0,
  25. 0, 0, 0, 0,
  26. 0, size.wd as i64, 0, 0,
  27. ],
  28. )?;
  29. // we won't ever see this, but for good measure.
  30. w.set_title("rbar")?;
  31. // we care about some input events!
  32. w.set_input_masks()?;
  33. w.set_protocols()?;
  34. // and now show it!
  35. w.map();
  36. // let's grab the cairo context here
  37. let surf = w.get_cairo_surface();
  38. let ctx = cairo::Context::new(&surf);
  39. // we do some grossness with file descriptors later, so we need
  40. // the file descriptors we care about here
  41. let window_fd = w.get_fd();
  42. let stdin_fd = std::io::stdin().as_raw_fd();
  43. let mut fds = unsafe { std::mem::uninitialized() };
  44. // To begin with, our left-hand side---which normally is whatever
  45. // was last passed in on stdin---will start as a generic
  46. // message...
  47. let mut input = format!("Loading...");
  48. // And let's get a buffered stdin handle now
  49. let mut stdin = std::io::BufReader::new(std::io::stdin());
  50. // In the absence of other events, let's refresh every five
  51. // seconds. Or whatever.
  52. let mut timer = libc::timeval {
  53. tv_sec: 5,
  54. tv_usec: 0,
  55. };
  56. let layout = pangocairo::functions::create_layout(&ctx)
  57. .ok_or(format_err!("foo"))?;
  58. // allow for the whole width of the bar, minus a small fixed amount
  59. layout.set_width((size.wd - 20) * pango::SCALE);
  60. // this should also be configurable, but Fira Mono is a good font
  61. let mut font = pango::FontDescription::from_string("Fira Mono 18");
  62. font.set_weight(pango::Weight::Bold);
  63. layout.set_font_description(&font);
  64. // do an initial pass at drawing the bar!
  65. draw(&ctx, &layout, &input, size)?;
  66. // we're gonna keep looping until we don't
  67. loop {
  68. unsafe {
  69. // set up the FD set to be the X11 fd and the state of stdin
  70. libc::FD_ZERO(&mut fds);
  71. libc::FD_SET(window_fd, &mut fds);
  72. libc::FD_SET(stdin_fd, &mut fds);
  73. timer.tv_sec = 5;
  74. // this will block until there's input on either of the
  75. // above FDs or until five seconds have passed, whichever comes first
  76. libc::select(
  77. window_fd + 1,
  78. &mut fds,
  79. std::ptr::null_mut(),
  80. std::ptr::null_mut(),
  81. &mut timer,
  82. );
  83. }
  84. // if we _did_ have input on stdin, then read it in: that'll
  85. // be our new left-hand text
  86. if unsafe { libc::FD_ISSET(stdin_fd, &mut fds) } {
  87. use std::io::BufRead;
  88. input = String::new();
  89. stdin.read_line(&mut input)?;
  90. if input.len() == 0 {
  91. break;
  92. }
  93. draw(&ctx, &layout, &input, size)?;
  94. }
  95. // if we have X11 events, handle them. If any one was a quit
  96. // event, then just... quit.
  97. while w.has_events() {
  98. match w.handle() {
  99. Some(Event::QuitEvent) => break,
  100. _e => (),
  101. }
  102. }
  103. // otherwise, draw the thing!
  104. draw(&ctx, &layout, &input, size)?;
  105. }
  106. Ok(())
  107. }
  108. /// Do our Cairo drawing. This needs to be refactored to allow for
  109. /// more configurability in terms of what gets written!
  110. fn draw(
  111. ctx: &cairo::Context,
  112. layout: &pango::Layout,
  113. left: &str,
  114. size: Size)
  115. -> Result<(), failure::Error>
  116. {
  117. // the background is... gray-ish? this'll be configurable eventually
  118. ctx.set_source_rgb(0.1, 0.1, 0.1);
  119. ctx.paint();
  120. // and the text is white
  121. ctx.set_source_rgb(1.0, 1.0, 1.0);
  122. // set up a struct with everything that widgets need to draw
  123. let drawing = widgets::Drawing {
  124. ctx: ctx,
  125. lyt: &layout,
  126. size,
  127. };
  128. // set up our widgets
  129. let text = widgets::Text::new(left);
  130. let time = widgets::Time::new();
  131. let bat = widgets::Battery::new().unwrap();
  132. // and create a 'config' which tells us which widgets to draw from
  133. // the left, and which from the right
  134. let config = widgets::Config {
  135. left: vec![
  136. &text as &Widget,
  137. ],
  138. right: vec![
  139. &bat as &Widget,
  140. &time as &Widget,
  141. ],
  142. };
  143. // and draw them!
  144. config.draw(&drawing);
  145. Ok(())
  146. }