1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use errors::*;
use gdk;
use gdk_sys;
use gdk_x11::*;
use gtk;
use x11;
use libc::c_int;
use libc::c_uint;
lazy_static! {
static ref KEY_MASKS: Vec<c_uint> = vec![
gdk_sys::GdkModifierType::empty().bits(),
gdk_sys::GDK_MOD2_MASK.bits(),
gdk_sys::GDK_LOCK_MASK.bits(),
gdk_sys::GDK_MOD2_MASK.bits() | gdk_sys::GDK_LOCK_MASK.bits(),
];
}
#[derive(Debug)]
pub struct Hotkey {
pub code: gdk::key,
pub mod_bits: u32,
pub sym: u64,
pub gtk_accel: String,
}
impl Hotkey {
pub fn new(code: gdk::key, mods: gdk::ModifierType) -> Result<Hotkey> {
let display = gdk_x11_get_default_xdisplay();
let mod_bits = mods.bits();
let sym =
unsafe { x11::xlib::XkbKeycodeToKeysym(display, code as u8, 0, 0) };
let gtk_accel = gtk::accelerator_name(sym as u32, mods)
.ok_or("Could net get accelerator name")?;
let hotkey = Hotkey {
code,
mod_bits,
sym,
gtk_accel,
};
hotkey.grab()?;
return Ok(hotkey);
}
pub fn new_from_accel(accel: &str) -> Result<Hotkey> {
let (code, mods) = hotkey_accel_to_code(accel);
return Hotkey::new(code, mods);
}
pub fn grab(&self) -> Result<()> {
let display = gdk_x11_get_default_xdisplay();
let old_hdlr = unsafe {
GRAB_ERROR = 0;
x11::xlib::XSetErrorHandler(Some(grab_error_handler))
};
for key in KEY_MASKS.iter() {
unsafe {
x11::xlib::XGrabKey(display,
self.code,
self.mod_bits | key,
gdk_x11_get_default_root_xwindow(),
1,
x11::xlib::GrabModeAsync,
x11::xlib::GrabModeAsync);
}
}
unsafe {
x11::xlib::XFlush(display);
x11::xlib::XSync(display, false as c_int);
}
unsafe {
x11::xlib::XSetErrorHandler(old_hdlr);
}
unsafe {
ensure!(GRAB_ERROR == 0, "Error grabbing");
}
return Ok(());
}
pub fn ungrab(&self) {
let display = gdk_x11_get_default_xdisplay();
for key in KEY_MASKS.iter() {
unsafe {
x11::xlib::XUngrabKey(display,
self.code,
self.mod_bits | key,
gdk_x11_get_default_root_xwindow());
}
}
}
pub fn matches(&self, code: gdk::key, mods: gdk::ModifierType) -> bool {
if code != self.code {
return false;
}
for key in KEY_MASKS.iter() {
if (self.mod_bits | key) == mods.bits() {
return true;
}
}
return false;
}
}
impl Drop for Hotkey {
fn drop(&mut self) {
debug!("Ungrabbing hotkey");
self.ungrab();
}
}
pub fn hotkey_accel_to_code(accel: &str) -> (gdk::key, gdk::ModifierType) {
let display = gdk_x11_get_default_xdisplay();
let (sym, mods) = gtk::accelerator_parse(accel);
unsafe {
if sym != 0 {
return (x11::xlib::XKeysymToKeycode(display, sym as u64) as i32,
mods);
} else {
return (-1, mods);
}
}
}
static mut GRAB_ERROR: u8 = 0;
extern "C" fn grab_error_handler(_: *mut x11::xlib::Display,
_: *mut x11::xlib::XErrorEvent)
-> c_int {
warn!("Error while grabbing hotkey");
unsafe {
GRAB_ERROR = 1;
}
return 0;
}