1 use xcb;
2 use ffi::keysyms::*;
3 use libc::{free, c_void};
4 
5 pub struct KeySymbols<'a> {
6 	ptr:  *mut xcb_key_symbols_t,
7 	conn: &'a xcb::Connection,
8 }
9 
10 impl<'a> KeySymbols<'a> {
new(c: &xcb::Connection) -> KeySymbols11 	pub fn new(c: &xcb::Connection) -> KeySymbols {
12 		unsafe {
13 			KeySymbols {
14 				ptr:  xcb_key_symbols_alloc(c.get_raw_conn()),
15 				conn: c,
16 			}
17 		}
18 	}
19 
get_keysym(&self, keycode: xcb::Keycode, col: i32) -> xcb::Keysym20 	pub fn get_keysym(&self, keycode: xcb::Keycode, col: i32) -> xcb::Keysym {
21 		unsafe {
22 			xcb_key_symbols_get_keysym(self.ptr, keycode, col)
23 		}
24 	}
25 
get_keycode(&self, keysym: xcb::Keysym) -> KeycodeIter26 	pub fn get_keycode(&self, keysym: xcb::Keysym) -> KeycodeIter {
27 		unsafe {
28 			KeycodeIter {
29 				ptr:   xcb_key_symbols_get_keycode(self.ptr, keysym),
30 				index: 0,
31 			}
32 		}
33 	}
34 
press_lookup_keysym(&self, event: &xcb::KeyPressEvent, col: i32) -> xcb::Keysym35 	pub fn press_lookup_keysym(&self, event: &xcb::KeyPressEvent, col: i32) -> xcb::Keysym {
36 		unsafe {
37 			xcb_key_press_lookup_keysym(self.ptr, event.ptr, col)
38 		}
39 	}
40 
release_lookup_keysym(&self, event: &xcb::KeyReleaseEvent, col: i32) -> xcb::Keysym41 	pub fn release_lookup_keysym(&self, event: &xcb::KeyReleaseEvent, col: i32) -> xcb::Keysym {
42 		unsafe {
43 			xcb_key_release_lookup_keysym(self.ptr, event.ptr, col)
44 		}
45 	}
46 
refresh_keyboard_mapping(&self, event: &xcb::MappingNotifyEvent) -> i3247 	pub fn refresh_keyboard_mapping(&self, event: &xcb::MappingNotifyEvent) -> i32 {
48 		unsafe {
49 			xcb_refresh_keyboard_mapping(self.ptr, event.ptr)
50 		}
51 	}
52 }
53 
54 impl<'a> Drop for KeySymbols<'a> {
drop(&mut self)55 	fn drop(&mut self) {
56 		unsafe {
57 			xcb_key_symbols_free(self.ptr);
58 		}
59 	}
60 }
61 
62 pub struct KeycodeIter {
63 	ptr: *mut xcb::Keycode,
64 	index: isize,
65 }
66 
67 impl Drop for KeycodeIter {
drop(&mut self)68 	fn drop(&mut self) {
69 		unsafe {
70 			free(self.ptr as *mut c_void);
71 		}
72 	}
73 }
74 
75 impl Iterator for KeycodeIter {
76 	type Item = xcb::Keycode;
77 
next(&mut self) -> Option<xcb::Keycode>78 	fn next(&mut self) -> Option<xcb::Keycode> {
79 		unsafe {
80 			if self.ptr.is_null() {
81 				return None;
82 			}
83 
84 			match *self.ptr.offset(self.index) {
85 				0 =>
86 					None,
87 
88 				keycode => {
89 					self.index += 1;
90 					Some(keycode)
91 				}
92 			}
93 		}
94 	}
95 }
96 
is_keypad_key(keysym: xcb::Keysym) -> bool97 pub fn is_keypad_key(keysym: xcb::Keysym) -> bool {
98 	unsafe {
99 		xcb_is_keypad_key(keysym) != 0
100 	}
101 }
102 
is_private_keypad_key(keysym: xcb::Keysym) -> bool103 pub fn is_private_keypad_key(keysym: xcb::Keysym) -> bool {
104 	unsafe {
105 		xcb_is_private_keypad_key(keysym) != 0
106 	}
107 }
108 
is_cursor_key(keysym: xcb::Keysym) -> bool109 pub fn is_cursor_key(keysym: xcb::Keysym) -> bool {
110 	unsafe {
111 		xcb_is_cursor_key(keysym) != 0
112 	}
113 }
114 
is_pf_key(keysym: xcb::Keysym) -> bool115 pub fn is_pf_key(keysym: xcb::Keysym) -> bool {
116 	unsafe {
117 		xcb_is_pf_key(keysym) != 0
118 	}
119 }
120 
is_function_key(keysym: xcb::Keysym) -> bool121 pub fn is_function_key(keysym: xcb::Keysym) -> bool {
122 	unsafe {
123 		xcb_is_function_key(keysym) != 0
124 	}
125 }
126 
is_misc_function_key(keysym: xcb::Keysym) -> bool127 pub fn is_misc_function_key(keysym: xcb::Keysym) -> bool {
128 	unsafe {
129 		xcb_is_misc_function_key(keysym) != 0
130 	}
131 }
132 
is_modifier_key(keysym: xcb::Keysym) -> bool133 pub fn is_modifier_key(keysym: xcb::Keysym) -> bool {
134 	unsafe {
135 		xcb_is_modifier_key(keysym) != 0
136 	}
137 }
138