main.rs 5.2 KB

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