main.rs 5.0 KB

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