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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#[macro_use]
mod macros;
pub mod state;
mod err;
use std::fmt;
use std::mem;
use std::ptr;
use std::os::unix::ffi::OsStrExt;
use std::cmp::{Eq, Ordering};
use std::path::PathBuf;
use std::ffi::CString;
use std::ops::Deref;
pub use self::state::LibraryState;
pub use self::err::{LibraryError, Result};
use ::libc;
use ::pty;
pub struct Library {
install: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void)>,
uninstall: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void)>,
start: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void)>,
end: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void)>,
idle: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void)>,
process: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, name: *const libc::c_uchar, pid: libc::c_int)>,
command: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, line: *const libc::c_uchar)>,
key_unicode_down: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, code: libc::c_ulonglong)>,
key_string_down: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, text: *const libc::c_uchar)>,
key_repeat_down: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, repeat: libc::c_ulong)>,
key_interval_down: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, interval: libc::c_longlong)>,
mouse_pressed: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, code: libc::c_uint, xy: [libc::c_ushort; 2])>,
mouse_released: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, code: libc::c_uint, xy: [libc::c_ushort; 2])>,
input: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, text: *const libc::c_uchar)>,
output: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, text: *const libc::c_uchar)>,
resized: Option<extern fn(state: *const LibraryState, save: &*const *const libc::c_void, text: *const pty::Winszed)>,
save: *const *const libc::c_void,
handle: *mut libc::c_void,
index: i64,
path: PathBuf,
unmounted: bool,
}
impl Library {
pub fn new(path: PathBuf, index: i64, state: &LibraryState) -> Result<Self> {
unsafe {
let mut libname: Vec<libc::c_uchar> = Vec::with_capacity(4096);
libname.extend_from_slice(&path.as_os_str().as_bytes()[..]);
libname.push(b'\0');
let handle: *mut libc::c_void = libc::dlopen(
libname.as_ptr() as *const libc::c_char,
libc::RTLD_LAZY
);
if handle.eq(&ptr::null_mut()) {
Err(LibraryError::BadDyLib(CString::from_raw(libc::dlerror()).into_string().unwrap_or_default()))
} else {
let lib: Library = Library {
install: symbol!(handle, b"install\0".as_ptr() as *const libc::c_char),
uninstall: symbol!(handle, b"uninstall\0".as_ptr() as *const libc::c_char),
start: symbol!(handle, b"start\0".as_ptr() as *const libc::c_char),
end: symbol!(handle, b"end\0".as_ptr() as *const libc::c_char),
idle: symbol!(handle, b"idle\0".as_ptr() as *const libc::c_char),
process: symbol!(handle, b"process\0".as_ptr() as *const libc::c_char),
command: symbol!(handle, b"command\0".as_ptr() as *const libc::c_char),
key_unicode_down: symbol!(handle, b"key_unicode_down\0".as_ptr() as *const libc::c_char),
key_string_down: symbol!(handle, b"key_string_down\0".as_ptr() as *const libc::c_char),
key_repeat_down: symbol!(handle, b"key_repeat_down\0".as_ptr() as *const libc::c_char),
key_interval_down: symbol!(handle, b"key_interval_down\0".as_ptr() as *const libc::c_char),
mouse_pressed: symbol!(handle, b"mouse_pressed\0".as_ptr() as *const libc::c_char),
mouse_released: symbol!(handle, b"mouse_released\0".as_ptr() as *const libc::c_char),
input: symbol!(handle, b"input\0".as_ptr() as *const libc::c_char),
output: symbol!(handle, b"output\0".as_ptr() as *const libc::c_char),
resized: symbol!(handle, b"resized\0".as_ptr() as *const libc::c_char),
save: ptr::null_mut(),
handle: handle,
index: index,
path: path,
unmounted: false,
};
lib.start(state);
Ok(lib)
}
}
}
pub fn as_path_buf(&self) -> &PathBuf {
&self.path
}
pub fn get_priority(&self) -> i64 {
self.index
}
pub fn is_unmounted(&self) -> bool {
self.unmounted
}
pub fn install(&self, state: &LibraryState) {
if let Some(install) = self.install {
install(state, &self.save);
}
}
pub fn uninstall(&self, state: &LibraryState) {
if let Some(uninstall) = self.uninstall {
uninstall(state, &self.save);
}
}
pub fn start(&self, state: &LibraryState) {
if let Some(start) = self.start {
start(state, &self.save);
}
}
pub fn end(&self, state: &LibraryState) {
if let Some(end) = self.end {
end(state, &self.save);
}
}
pub fn idle(&self, state: &LibraryState) {
if let Some(idle) = self.idle {
idle(state, &self.save);
}
}
pub fn process(&self, state: &LibraryState, taskname: &[libc::c_uchar], pid: libc::c_int) {
if let Some(process) = self.process {
process(state, &self.save, taskname.as_ptr(), pid);
}
}
pub fn command(&self, state: &LibraryState, line: &[libc::c_uchar]) {
if let Some(command) = self.command {
command(state, &self.save, line.as_ptr());
}
}
pub fn key_unicode_down(&self, state: &LibraryState, code: libc::c_ulonglong) {
if let Some(key_unicode_down) = self.key_unicode_down {
key_unicode_down(state, &self.save, code);
}
}
pub fn key_string_down(&self, state: &LibraryState, text: &[libc::c_uchar]) {
if let Some(key_string_down) = self.key_string_down {
key_string_down(state, &self.save, text.as_ptr());
}
}
pub fn key_repeat_down(&self, state: &LibraryState, repeat: libc::c_ulong) {
if let Some(key_repeat_down) = self.key_repeat_down {
key_repeat_down(state, &self.save, repeat);
}
}
pub fn key_interval_down(&self, state: &LibraryState, interval: libc::c_longlong) {
if let Some(key_interval_down) = self.key_interval_down {
key_interval_down(state, &self.save, interval);
}
}
pub fn mouse_pressed(&self, state: &LibraryState, code: libc::c_uint, xy: [libc::c_ushort; 2]) {
if let Some(mouse_pressed) = self.mouse_pressed {
mouse_pressed(state, &self.save, code, xy);
}
}
pub fn mouse_released(&self, state: &LibraryState, code: libc::c_uint, xy: [libc::c_ushort; 2]) {
if let Some(mouse_released) = self.mouse_released {
mouse_released(state, &self.save, code, xy);
}
}
pub fn input(&self, state: &LibraryState, text: &[libc::c_uchar]) {
if let Some(input) = self.input {
input(state, &self.save, text.as_ptr());
}
}
pub fn output(&self, state: &LibraryState, text: &[libc::c_uchar]) {
if let Some(output) = self.output {
output(state, &self.save, text.as_ptr());
}
}
pub fn resized(&self, state: &LibraryState, size: &pty::Winszed) {
if let Some(resized) = self.resized {
resized(state, &self.save, size);
}
}
pub fn call(&self, state: &LibraryState, event: &pty::ShellState) {
if let Some(()) = event.is_idle() {
if let Some(idle) = self.idle {
idle(state, &self.save);
}
} else {
if let Some(pty::Mouse {code, pressed, x, y}) = event.is_input_mouse() {
if pressed {
self.mouse_pressed(state, code as u32, [x, y])
} else {
self.mouse_released(state, code as u32, [x, y])
}
} else if let Some(key) = event.is_input_keydown() {
match key {
pty::Key::Char(code) => self.key_unicode_down(state, code),
pty::Key::Str(text) => self.key_string_down(state, &text.deref()),
}
} else if let Some(repeat) = event.is_input_keyrepeat() {
self.key_repeat_down(state, repeat)
} else if let Some(interval) = event.is_input_keyinterval() {
self.key_interval_down(state, interval)
} else if let Some(slice) = event.is_input_slice() {
self.input(state, slice)
} else if let Some(slice) = event.is_output_last() {
self.output(state, slice)
}
if let Some(&(pid, name)) = event.is_task() {
self.process(state, &name[..], pid)
}
}
}
}
impl Eq for Library {}
impl PartialEq for Library {
fn eq(&self, rhs: &Library) -> bool {
self.index.eq(&rhs.index)
}
}
impl PartialOrd for Library {
fn partial_cmp(&self, rhs: &Library) -> Option<Ordering> {
self.index.partial_cmp(&rhs.index)
}
}
impl Ord for Library {
fn cmp(&self, rhs: &Library) -> Ordering {
self.index.cmp(&rhs.index)
}
}
impl fmt::Debug for Library {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "library({}): start:{} path:({:?})",
self.index,
self.start.is_some(),
self.path)
}
}
impl Drop for Library {
fn drop(&mut self) {
unsafe {
assert_ne!(libc::dlclose(self.handle), -1);
}
}
}