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
|
// See LICENSE file for copyright and license details.
//! Parse the socket info.
//!
//! For now this module will have 2 methods. One for inmediate execution and
//! other for add a bindind.
use x11::xlib;
use x11::keysym;
use command::*;
use ::socket::ParsedCmd;
use ::socket::FnType;
macro_rules! simple_try {
($expr:expr) => (match $expr {
Ok(val) => val,
Err(err) => {
return Err(err)
}
})
}
fn modifier_from<'a>(s: &'a str) -> Result<u32, &'static str> {
match s {
"Mod1" => Ok(xlib::Mod1Mask),
"Mod2" => Ok(xlib::Mod2Mask),
"Mod3" => Ok(xlib::Mod3Mask),
"Mod4" => Ok(xlib::Mod4Mask),
"Control" => Ok(xlib::ControlMask),
"Shift" => Ok(xlib::ShiftMask),
_ => Err("unknown modifier"),
}
}
fn modifiers<'a>(s: &'a str) -> Result<Vec<u32>, &'static str> {
let mut result = vec![];
for smod in s.split("-") {
let modifier = simple_try!(modifier_from(smod));
result.push(modifier);
}
Ok(result)
}
fn key<'a>(s: &'a str) -> Result<u32, &'static str> {
match s {
"a" => Ok(keysym::XK_a),
"b" => Ok(keysym::XK_b),
"c" => Ok(keysym::XK_c),
"d" => Ok(keysym::XK_d),
"e" => Ok(keysym::XK_e),
"f" => Ok(keysym::XK_f),
"g" => Ok(keysym::XK_g),
"h" => Ok(keysym::XK_h),
"i" => Ok(keysym::XK_i),
"j" => Ok(keysym::XK_j),
"k" => Ok(keysym::XK_k),
"l" => Ok(keysym::XK_l),
"m" => Ok(keysym::XK_m),
"n" => Ok(keysym::XK_n),
"o" => Ok(keysym::XK_o),
"p" => Ok(keysym::XK_p),
"q" => Ok(keysym::XK_q),
"r" => Ok(keysym::XK_r),
"s" => Ok(keysym::XK_s),
"t" => Ok(keysym::XK_t),
"u" => Ok(keysym::XK_u),
"v" => Ok(keysym::XK_v),
"w" => Ok(keysym::XK_w),
"x" => Ok(keysym::XK_x),
"y" => Ok(keysym::XK_y),
"z" => Ok(keysym::XK_z),
"A" => Ok(keysym::XK_A),
"B" => Ok(keysym::XK_B),
"C" => Ok(keysym::XK_C),
"D" => Ok(keysym::XK_D),
"E" => Ok(keysym::XK_E),
"F" => Ok(keysym::XK_F),
"G" => Ok(keysym::XK_G),
"H" => Ok(keysym::XK_H),
"I" => Ok(keysym::XK_I),
"J" => Ok(keysym::XK_J),
"K" => Ok(keysym::XK_K),
"L" => Ok(keysym::XK_L),
"M" => Ok(keysym::XK_M),
"N" => Ok(keysym::XK_N),
"O" => Ok(keysym::XK_O),
"P" => Ok(keysym::XK_P),
"Q" => Ok(keysym::XK_Q),
"R" => Ok(keysym::XK_R),
"S" => Ok(keysym::XK_S),
"T" => Ok(keysym::XK_T),
"U" => Ok(keysym::XK_U),
"V" => Ok(keysym::XK_V),
"W" => Ok(keysym::XK_W),
"X" => Ok(keysym::XK_X),
"Y" => Ok(keysym::XK_Y),
"Z" => Ok(keysym::XK_Z),
"1" => Ok(keysym::XK_1),
"2" => Ok(keysym::XK_2),
"Tab" => Ok(keysym::XK_Tab),
"Return" => Ok(keysym::XK_Return),
_ => Err("unknown key"),
}
}
fn func<'a>(s: &'a str) -> Result<ExecFn, &'static str> {
match s {
"exec" => Ok(exec),
"move-win" => Ok(move_win),
"move-win-to" => Ok(move_win_to),
"move-win-sticky" => Ok(move_win_sticky),
"resize-win" => Ok(resize_win),
"resize-win-sticky" => Ok(resize_win_sticky),
"focus-next" => Ok(focus_next),
"close-win" => Ok(close_win),
"fullscreen" => Ok(fullscreen),
"change-desktop" => Ok(change_desktop),
"quit" => Ok(quit_dotwm),
_ => Err("unknown function"),
}
}
pub fn parse<'a>(input: &'a str) -> Result<ParsedCmd<'a>, &'static str> {
let args: Vec<&'a str> = input.split_whitespace().collect();
match args.first() {
Some(cmd) => {
match cmd {
&"bind" => {
if args.len() > 2 {
let modifiers = simple_try!(modifiers(args[1]));
let key = simple_try!(key(args[2]));
let func = simple_try!(func(args[3]));
let arguments: &[&'a str]= &args[4..];
Ok(ParsedCmd {
f: FnType::Bind,
modifiers: modifiers,
key: key,
args: arguments.to_vec(),
func: func,
})
} else {
Err("missing arguments")
}
},
&"exec" => {
Ok(ParsedCmd {
f: FnType::Exec,
modifiers: vec![],
key: 0,
args: vec![],
func: exec
})
},
_ => {
Err("unknown command")
},
}
},
None => Err("no input"),
}
}
#[test]
fn parse_unknown() {
let res = parse("unknown");
if let Err(e) = res {
assert_eq!(e, "unknown command");
} else {
assert!(false);
}
}
#[test]
fn parse_bind_modifier() {
let res = parse("bind modifier1 k exec ");
if let Err(e) = res {
assert_eq!(e, "unknown modifier");
} else {
assert!(false);
}
let res2 = parse("bind Mod4 k exec ");
if let Ok(modifier) = res2 {
assert_eq!(modifier.modifiers[0], xlib::Mod4Mask);
} else {
assert!(false);
}
}
#[test]
fn parse_bind_key() {
let res = parse("bind Mod4 ' exec ");
if let Err(e) = res {
assert_eq!(e, "unknown key");
} else {
assert!(false);
}
let res2 = parse("bind Mod4 k exec ");
if let Ok(modifier) = res2 {
assert_eq!(modifier.key, keysym::XK_k);
} else {
assert!(false);
}
}
#[test]
fn parse_exec() {
let res = parse("exec Mod4 ' exec ");
if let Ok(pcmd) = res {
assert_eq!(pcmd.f, FnType::Exec);
} else {
assert!(false);
}
}
|