main.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #[macro_use]
  2. extern crate failure;
  3. mod config;
  4. mod widgets;
  5. mod window;
  6. use std::os::unix::io::AsRawFd;
  7. use pango::LayoutExt;
  8. use widgets::Size;
  9. use window::{Display,Event,Window};
  10. fn main() -> Result<(), failure::Error> {
  11. // set up the display and the window
  12. let config = config::Config::find_config()?;
  13. let mut d = Display::create()?;
  14. let mut ws = Vec::new();
  15. for (x_off, wd) in d.get_widths()? {
  16. let size = Size { wd, ht: 36, xo: x_off, yo: 0 };
  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", &[x_off as i64, 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. ws.push(w);
  37. }
  38. // we do some grossness with file descriptors later, so we need
  39. // the file descriptors we care about here
  40. let window_fds: Vec<i32> = ws.iter_mut().map({ |w| w.get_fd() }).collect();
  41. let stdin_fd = std::io::stdin().as_raw_fd();
  42. let mut fds = unsafe { std::mem::uninitialized() };
  43. // To begin with, our left-hand side---which normally is whatever
  44. // was last passed in on stdin---will start as a generic
  45. // message...
  46. let mut input = format!("Loading...");
  47. // And let's get a buffered stdin handle now
  48. let mut stdin = std::io::BufReader::new(std::io::stdin());
  49. // In the absence of other events, let's refresh every five
  50. // seconds. Or whatever.
  51. let mut timer = libc::timeval {
  52. tv_sec: 5,
  53. tv_usec: 0,
  54. };
  55. let mut ctxs = Vec::new();
  56. for w in ws.iter_mut() {
  57. // let's grab the cairo context here
  58. let surf = w.get_cairo_surface();
  59. let ctx = cairo::Context::new(&surf);
  60. let layout = pangocairo::functions::create_layout(&ctx)
  61. .ok_or(format_err!("unable to create layout"))?;
  62. // allow for the whole width of the bar, minus a small fixed amount
  63. layout.set_width((w.width - 20) * pango::SCALE);
  64. // this should also be configurable, but Fira Mono is a good font
  65. let mut font = pango::FontDescription::from_string("Fira Mono 18");
  66. font.set_weight(pango::Weight::Bold);
  67. layout.set_font_description(&font);
  68. // do an initial pass at drawing the bar!
  69. config.draw(&ctx, &layout, &input, w.size())?;
  70. ctxs.push((ctx, layout, w.size()));
  71. }
  72. let max_fd = window_fds.iter().max().unwrap_or(&0) + 1;
  73. // we're gonna keep looping until we don't
  74. loop {
  75. unsafe {
  76. // set up the FD set to be the X11 fd and the state of stdin
  77. libc::FD_ZERO(&mut fds);
  78. for fd in window_fds.iter() {
  79. libc::FD_SET(*fd, &mut fds);
  80. }
  81. libc::FD_SET(stdin_fd, &mut fds);
  82. timer.tv_sec = 5;
  83. // this will block until there's input on either of the
  84. // above FDs or until five seconds have passed, whichever comes first
  85. libc::select(
  86. max_fd,
  87. &mut fds,
  88. std::ptr::null_mut(),
  89. std::ptr::null_mut(),
  90. &mut timer,
  91. );
  92. }
  93. // if we _did_ have input on stdin, then read it in: that'll
  94. // be our new left-hand text
  95. if unsafe { libc::FD_ISSET(stdin_fd, &mut fds) } {
  96. use std::io::BufRead;
  97. input = String::new();
  98. stdin.read_line(&mut input)?;
  99. if input.len() == 0 {
  100. break;
  101. }
  102. for (ctx, layout, sz) in ctxs.iter() {
  103. config.draw(&ctx, &layout, &input, *sz)?;
  104. }
  105. }
  106. // if we have X11 events, handle them. If any one was a quit
  107. // event, then just... quit.
  108. for w in ws.iter_mut() {
  109. while w.has_events() {
  110. match w.handle() {
  111. Some(Event::QuitEvent) => break,
  112. _e => (),
  113. }
  114. }
  115. }
  116. for (ctx, layout, sz) in ctxs.iter() {
  117. // otherwise, draw the thing!
  118. config.draw(&ctx, &layout, &input, *sz)?;
  119. }
  120. }
  121. Ok(())
  122. }