atom.rs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use std::collections::HashMap;
  2. use std::cell::RefCell;
  3. use xcb;
  4. macro_rules! atoms {
  5. ( $( $x:ident ),* ) => {
  6. #[allow(non_snake_case)]
  7. $(pub const $x: &'static str = stringify!($x);)*
  8. }
  9. }
  10. atoms!(
  11. _NET_SYSTEM_TRAY_S0,
  12. _NET_SYSTEM_TRAY_ORIENTATION,
  13. _NET_SYSTEM_TRAY_OPCODE,
  14. _NET_WM_WINDOW_TYPE,
  15. _NET_WM_WINDOW_TYPE_DOCK,
  16. MANAGER
  17. );
  18. pub struct Atoms<'a> {
  19. conn: &'a xcb::Connection,
  20. cache: RefCell<HashMap<String, xcb::Atom>>
  21. }
  22. impl<'a> Atoms<'a> {
  23. pub fn new(conn: &xcb::Connection) -> Atoms {
  24. Atoms {
  25. conn: conn,
  26. cache: RefCell::new(HashMap::new())
  27. }
  28. }
  29. pub fn get(&self, name: &str) -> xcb::Atom {
  30. let mut cache = self.cache.borrow_mut();
  31. if cache.contains_key(name) {
  32. *cache.get(name).unwrap()
  33. }
  34. else {
  35. let atom = xcb::intern_atom(self.conn, false, name).get_reply().unwrap().atom();
  36. cache.insert(name.to_string(), atom);
  37. atom
  38. }
  39. }
  40. }