main.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 height = config.get_height();
  14. let mut d = Display::create()?;
  15. let mut ws = Vec::new();
  16. for (x_off, wd) in d.get_widths()? {
  17. let size = Size { wd, ht: height, xo: x_off, yo: 0 };
  18. let mut w = Window::create(&d, size)?;
  19. // set some window-manager properties: this is a dock
  20. w.change_property("_NET_WM_WINDOW_TYPE", &["_NET_WM_WINDOW_TYPE_DOCK"])?;
  21. // ...and should push other windows out of the way
  22. w.change_property("_NET_WM_STRUT", &[x_off as i64, 0, size.ht as i64, 0])?;
  23. w.change_property(
  24. "_NET_WM_STRUT_PARTIAL",
  25. &[ 0, 0, size.ht as i64, 0,
  26. 0, 0, 0, 0,
  27. 0, size.wd as i64, 0, 0,
  28. ],
  29. )?;
  30. // we won't ever see this, but for good measure.
  31. w.set_title("rbar")?;
  32. // we care about some input events!
  33. w.set_input_masks()?;
  34. w.set_protocols()?;
  35. // and now show it!
  36. w.map();
  37. ws.push(w);
  38. }
  39. // we do some grossness with file descriptors later, so we need
  40. // the file descriptors we care about here
  41. let window_fds: Vec<i32> = ws.iter_mut().map({ |w| w.get_fd() }).collect();
  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 mut ctxs = Vec::new();
  57. for w in ws.iter_mut() {
  58. // let's grab the cairo context here
  59. let surf = w.get_cairo_surface();
  60. let ctx = cairo::Context::new(&surf);
  61. let layout = pangocairo::functions::create_layout(&ctx)
  62. .ok_or(format_err!("unable to create layout"))?;
  63. // allow for the whole width of the bar, minus a small fixed amount
  64. layout.set_width((w.width - 20) * pango::SCALE);
  65. // this should also be configurable, but Fira Mono is a good font
  66. let mut font = pango::FontDescription::from_string(config.font());
  67. font.set_weight(pango::Weight::Bold);
  68. layout.set_font_description(&font);
  69. // do an initial pass at drawing the bar!
  70. config.draw(&ctx, &layout, &input, w.size())?;
  71. ctxs.push((ctx, layout, w.size()));
  72. }
  73. let max_fd = window_fds.iter().max().unwrap_or(&0) + 1;
  74. // we're gonna keep looping until we don't
  75. loop {
  76. unsafe {
  77. // set up the FD set to be the X11 fd and the state of stdin
  78. libc::FD_ZERO(&mut fds);
  79. for fd in window_fds.iter() {
  80. libc::FD_SET(*fd, &mut fds);
  81. }
  82. libc::FD_SET(stdin_fd, &mut fds);
  83. timer.tv_sec = 5;
  84. // this will block until there's input on either of the
  85. // above FDs or until five seconds have passed, whichever comes first
  86. libc::select(
  87. max_fd,
  88. &mut fds,
  89. std::ptr::null_mut(),
  90. std::ptr::null_mut(),
  91. &mut timer,
  92. );
  93. }
  94. // if we _did_ have input on stdin, then read it in: that'll
  95. // be our new left-hand text
  96. if unsafe { libc::FD_ISSET(stdin_fd, &mut fds) } {
  97. use std::io::BufRead;
  98. input = String::new();
  99. stdin.read_line(&mut input)?;
  100. if input.len() == 0 {
  101. break;
  102. }
  103. for (ctx, layout, sz) in ctxs.iter() {
  104. config.draw(&ctx, &layout, &input, *sz)?;
  105. }
  106. }
  107. // if we have X11 events, handle them. If any one was a quit
  108. // event, then just... quit.
  109. for w in ws.iter_mut() {
  110. while w.has_events() {
  111. match w.handle() {
  112. Some(Event::QuitEvent) => break,
  113. _e => (),
  114. }
  115. }
  116. }
  117. for (ctx, layout, sz) in ctxs.iter() {
  118. // otherwise, draw the thing!
  119. config.draw(&ctx, &layout, &input, *sz)?;
  120. }
  121. }
  122. Ok(())
  123. }