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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Main entry point for wrtype - a Rust implementation of wtype (xdotool type for Wayland)
//
// This module handles command-line argument parsing, command sequencing, and orchestrates
// the interaction between the Wayland virtual keyboard protocol and the XKB keymap system.
use Parser;
use Duration;
use ;
/// Command-line arguments structure using clap for automatic parsing and help generation.
/// This structure mirrors the original wtype interface for full compatibility.
///
/// # Examples
///
/// Basic text typing:
/// ```bash
/// wrtype "Hello, World!"
/// wrtype -- "-special-text" # Use -- to avoid option parsing
/// ```
///
/// Keyboard shortcuts:
/// ```bash
/// wrtype -M ctrl c -m ctrl # Ctrl+C
/// wrtype -M shift -k Tab -m shift # Shift+Tab
/// wrtype -M ctrl -M shift t -m shift -m ctrl # Ctrl+Shift+T
/// ```
///
/// Key sequences with timing:
/// ```bash
/// wrtype -P space -s 1000 -p space # Hold space for 1 second
/// wrtype -k F5 -s 2000 "Page refreshed" # F5 then wait then type
/// ```
///
/// Text with delays:
/// ```bash
/// wrtype -d 100 "Slow typing" # 100ms between characters
/// wrtype -d 50 "Fast typing" # 50ms between characters
/// ```
///
/// Reading from stdin:
/// ```bash
/// echo "Hello from stdin" | wrtype --stdin
/// cat file.txt | wrtype --stdin -d 10
/// ```
/// Parse command-line arguments into a sequence of executable commands.
///
/// This function processes all the different argument types and converts them into
/// a linear sequence of commands that will be executed in order. The order of
/// execution follows the order of arguments on the command line.
///
/// # Arguments
/// * `args` - Parsed command-line arguments from clap
///
/// # Returns
/// * `Ok(Vec<Command>)` - Sequence of commands to execute
/// * `Err(anyhow::Error)` - If invalid modifier names are provided
///
/// # Command Processing Order
/// 1. Text arguments (including stdin placeholder "-")
/// 2. Modifier press commands (-M)
/// 3. Modifier release commands (-m)
/// 4. Key press commands (-P)
/// 5. Key release commands (-p)
/// 6. Type key commands (-k) - converted to press+release pairs
/// 7. Sleep commands (-s)
/// 8. Stdin flag (--stdin)
///
/// # Examples
///
/// Simple text typing:
/// ```bash
/// wrtype "hello"
/// # → [Text { text: "hello", delay: 0ms }]
/// ```
///
/// Keyboard shortcut:
/// ```bash
/// wrtype -M ctrl c -m ctrl
/// # → [ModPress(Ctrl), ModPress(c), ModRelease(Ctrl)]
/// ```
///
/// Complex sequence with timing:
/// ```bash
/// wrtype -k F5 -s 1000 -d 100 "Page loaded"
/// # → [KeyPress("F5"), KeyRelease("F5"), Sleep(1000ms), Text { text: "Page loaded", delay: 100ms }]
/// ```
///
/// Stdin integration:
/// ```bash
/// echo "dynamic" | wrtype "Static: " - " text"
/// # → [Text { text: "Static: ", delay: 0ms }, StdinText { delay: 0ms }, Text { text: " text", delay: 0ms }]
/// ```
/// Main entry point for the wrtype application.
///
/// This function orchestrates the entire process:
/// 1. Parse command-line arguments using clap
/// 2. Validate that at least one action was specified
/// 3. Convert arguments into a command sequence
/// 4. Establish Wayland connection and virtual keyboard
/// 5. Execute all commands in sequence
/// 6. Clean up resources (automatic via RAII)
///
/// # Returns
/// * `Ok(())` - All commands executed successfully
/// * `Err(anyhow::Error)` - Various failure modes:
/// - Invalid arguments
/// - Wayland connection failure
/// - Virtual keyboard protocol not supported
/// - Command execution failure
///
/// # Exit Behavior
/// - If no actions are specified, prints usage and exits with code 1
/// - On successful completion, exits with code 0
/// - On error, anyhow handles the error display and exits with code 1