main.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. mod window;
  2. use std::os::unix::io::AsRawFd;
  3. use pango::LayoutExt;
  4. use window::{Display,Event,Window};
  5. fn main() {
  6. let mut d = Display::create();
  7. let width = d.get_width();
  8. let mut w = Window::create(d, width, 36);
  9. w.change_property(
  10. "_NET_WM_WINDOW_TYPE",
  11. &["_NET_WM_WINDOW_TYPE_DOCK"],
  12. );
  13. w.change_property(
  14. "_NET_WM_STRUT_PARTIAL",
  15. &[
  16. 0, 0, 36, 0,
  17. 0, 0, 0, 0,
  18. 0, width as i64, 0, 0,
  19. ],
  20. );
  21. w.change_property(
  22. "_NET_WM_STRUT",
  23. &[0i64, 0, 36, 0],
  24. );
  25. w.set_title("rbar");
  26. w.set_input_masks();
  27. w.set_protocols();
  28. w.map();
  29. let surf = w.get_cairo_surface();
  30. let ctx = cairo::Context::new(&surf);
  31. let window_fd = w.get_fd();
  32. let mut fds = unsafe { std::mem::uninitialized() };
  33. let mut input = format!("Loading...");
  34. let stdin_fd = std::io::stdin().as_raw_fd();
  35. let mut stdin = std::io::BufReader::new(std::io::stdin());
  36. let mut timer = libc::timeval {
  37. tv_sec: 5,
  38. tv_usec: 0,
  39. };
  40. draw(&ctx, "[1]", width);
  41. loop {
  42. use std::io::BufRead;
  43. unsafe {
  44. libc::FD_ZERO(&mut fds);
  45. libc::FD_SET(window_fd, &mut fds);
  46. libc::FD_SET(stdin_fd, &mut fds);
  47. libc::select(
  48. window_fd + 1,
  49. &mut fds,
  50. std::ptr::null_mut(),
  51. std::ptr::null_mut(),
  52. &mut timer,
  53. );
  54. }
  55. if unsafe { libc::FD_ISSET(stdin_fd, &mut fds) } {
  56. input = String::new();
  57. stdin.read_line(&mut input).unwrap();
  58. if input == "" {
  59. break;
  60. }
  61. draw(&ctx, &input, width);
  62. }
  63. while w.has_events() {
  64. draw(&ctx, &input, width);
  65. match w.handle() {
  66. Event::QuitEvent => break,
  67. Event::ShowEvent =>
  68. draw(&ctx, &input, width),
  69. _e => (),
  70. }
  71. }
  72. }
  73. }
  74. fn draw(ctx: &cairo::Context, left: &str, width: i32) {
  75. let now = time::now();
  76. ctx.set_source_rgb(0.1, 0.1, 0.1);
  77. ctx.paint();
  78. ctx.set_source_rgb(1.0, 1.0, 1.0);
  79. let layout = pangocairo::functions::create_layout(&ctx).unwrap();
  80. layout.set_alignment(pango::Alignment::Right);
  81. layout.set_width((width - 20) * pango::SCALE);
  82. let mut font = pango::FontDescription::from_string("Fira Mono 18");
  83. font.set_weight(pango::Weight::Bold);
  84. layout.set_font_description(&font);
  85. ctx.move_to(10.0, 4.0);
  86. layout.set_text(&time::strftime("%a %b %d %H:%M", &now).unwrap());
  87. pangocairo::functions::show_layout(&ctx, &layout);
  88. layout.set_alignment(pango::Alignment::Left);
  89. layout.set_text(left);
  90. pangocairo::functions::show_layout(&ctx, &layout);
  91. }