event.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use chan;
  2. use xcb;
  3. pub enum Event {
  4. Ready(xcb::Timestamp),
  5. ChildRequest(xcb::Window),
  6. ChildDestroyed(xcb::Window),
  7. ChildConfigured(xcb::Window, u16, u16)
  8. }
  9. const CLIENT_MESSAGE: u8 = xcb::CLIENT_MESSAGE | 0x80;
  10. pub fn event_loop(conn: &xcb::Connection, tx: chan::Sender<Event>) {
  11. let mut ready = false;
  12. loop {
  13. match conn.wait_for_event() {
  14. Some(event) => match event.response_type() {
  15. xcb::PROPERTY_NOTIFY if !ready => {
  16. ready = true;
  17. let event: &xcb::PropertyNotifyEvent = xcb::cast_event(&event);
  18. tx.send(Event::Ready(event.time()));
  19. },
  20. CLIENT_MESSAGE => {
  21. let event: &xcb::ClientMessageEvent = xcb::cast_event(&event);
  22. let data = event.data().data32();
  23. let window = data[2];
  24. tx.send(Event::ChildRequest(window));
  25. },
  26. xcb::DESTROY_NOTIFY => {
  27. let event: &xcb::DestroyNotifyEvent = xcb::cast_event(&event);
  28. tx.send(Event::ChildDestroyed(event.window()));
  29. },
  30. xcb::CONFIGURE_NOTIFY => {
  31. let event: &xcb::ConfigureNotifyEvent = xcb::cast_event(&event);
  32. tx.send(Event::ChildConfigured(event.window(), event.width(), event.height()));
  33. },
  34. _ => {}
  35. },
  36. None => { break; }
  37. }
  38. }
  39. }