main.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. mod window;
  2. use x11::xlib;
  3. use x11::xinput2;
  4. use std::ffi::CString;
  5. use std::os::raw::{c_int,c_uchar};
  6. use std::ptr;
  7. use pango::LayoutExt;
  8. use window::{Event,Window};
  9. fn main() {
  10. unsafe {
  11. let mut w = Window::create();
  12. w.change_property("_NET_WM_WINDOW_TYPE", "_NET_WM_WINDOW_TYPE_DOCK");
  13. {
  14. let prop = w.intern("_NET_WM_STRUT_PARTIAL");
  15. let val = [
  16. 0i64, 0, 36, 0,
  17. 0, 0, 0, 0,
  18. 0, 3840, 0, 0,
  19. ];
  20. xlib::XChangeProperty(
  21. w.display,
  22. w.window,
  23. prop,
  24. xlib::XA_CARDINAL,
  25. 32,
  26. xlib::PropModeReplace,
  27. std::mem::transmute(val.as_ptr()),
  28. val.len() as c_int,
  29. );
  30. }
  31. {
  32. let prop = w.intern("_NET_WM_STRUT");
  33. let val = &[
  34. 0i64, 0, 36, 0,
  35. ];
  36. xlib::XChangeProperty(
  37. w.display,
  38. w.window,
  39. prop,
  40. xlib::XA_CARDINAL,
  41. 32,
  42. xlib::PropModeReplace,
  43. std::mem::transmute(val.as_ptr()),
  44. val.len() as c_int,
  45. );
  46. }
  47. w.set_title("rbar");
  48. {
  49. let mut opcode = 0;
  50. let mut event = 0;
  51. let mut error = 0;
  52. let xinput_str = CString::new("XInputExtension").unwrap();
  53. let _xinput_available =
  54. xlib::XQueryExtension(w.display, xinput_str.as_ptr(), &mut opcode, &mut event, &mut error);
  55. let mut mask: [c_uchar;1] = [0];
  56. let mut input_event_mask = xinput2::XIEventMask {
  57. deviceid: xinput2::XIAllMasterDevices,
  58. mask_len: mask.len() as i32,
  59. mask: mask.as_mut_ptr(),
  60. };
  61. let events = &[
  62. xinput2::XI_ButtonPress,
  63. xinput2::XI_ButtonRelease,
  64. ];
  65. for &event in events {
  66. xinput2::XISetMask(&mut mask, event);
  67. }
  68. match xinput2::XISelectEvents(w.display, w.window, &mut input_event_mask, 1) {
  69. status if status as u8 == xlib::Success => (),
  70. err => panic!("Failed to select events {:?}", err)
  71. }
  72. }
  73. w.set_protocols();
  74. w.map();
  75. let surf = w.get_cairo_surface();
  76. let ctx = cairo::Context::new(&surf);
  77. let window_fd = w.get_fd();
  78. let mut fds = std::mem::uninitialized();
  79. let mut input = format!("Loading...");
  80. let mut stdin = std::io::BufReader::new(std::io::stdin());
  81. let mut timer = libc::timeval {
  82. tv_sec: 5,
  83. tv_usec: 0,
  84. };
  85. draw(&ctx, "[1]");
  86. loop {
  87. use std::io::BufRead;
  88. libc::FD_ZERO(&mut fds);
  89. libc::FD_SET(window_fd, &mut fds);
  90. libc::FD_SET(1, &mut fds);
  91. libc::select(window_fd + 1, &mut fds, ptr::null_mut(), ptr::null_mut(), &mut timer);
  92. if libc::FD_ISSET(1, &mut fds) {
  93. input = String::new();
  94. stdin.read_line(&mut input).unwrap();
  95. if input == "" {
  96. break;
  97. }
  98. draw(&ctx, &input);
  99. }
  100. while w.has_events() {
  101. draw(&ctx, &input);
  102. match w.handle() {
  103. Event::QuitEvent => break,
  104. e => (),
  105. }
  106. }
  107. }
  108. }
  109. }
  110. fn draw(ctx: &cairo::Context, left: &str) {
  111. let now = time::now();
  112. ctx.set_source_rgb(0.1, 0.1, 0.1);
  113. ctx.paint();
  114. ctx.set_source_rgb(1.0, 1.0, 1.0);
  115. let layout = pangocairo::functions::create_layout(&ctx).unwrap();
  116. layout.set_alignment(pango::Alignment::Right);
  117. layout.set_width((3840 - 20) * pango::SCALE);
  118. let mut font = pango::FontDescription::from_string("Fira Mono 18");
  119. font.set_weight(pango::Weight::Bold);
  120. layout.set_font_description(&font);
  121. ctx.move_to(10.0, 4.0);
  122. layout.set_text(&time::strftime("%a %b %d %H:%M", &now).unwrap());
  123. pangocairo::functions::show_layout(&ctx, &layout);
  124. layout.set_alignment(pango::Alignment::Left);
  125. layout.set_text(left);
  126. pangocairo::functions::show_layout(&ctx, &layout);
  127. }