main.rs 2.9 KB

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