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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Hook for adding additional functionality around standard WindowManager actions
//!
//! # Overview
//!
//! Hooks are the primary way of injecting custom functionality into penrose when you want to go
//! beyond simply binding actions to key presses. There are multiple points in normal
//! [WindowManager] execution that will trigger the running of user defined hooks, during which you
//! will have complete control over the window manager state and (importantly) block the event loop
//! until your hook exits. For details of what hook points are available, see each of the trait
//! methods outlined below. Note that a single [Hook] can register itself to be called at multiple
//! hook points (all, if desired!) and that hooks are allways called in the order that they are
//! registered with the [WindowManager] on init (i.e. the order of the `Vec` itself).
//!
//! # Implementing Hook
//!
//! As an example of how to write a hook and register it, lets implement a simple hook that logs
//! each new client that is added to a particular workspace, noting if we've seen it before or not.
//! Completely pointless, but it will serve as a nice starting point to show what is happening.
//!
//! ```no_run
//! use penrose::{
//! core::{
//! hooks::Hook,
//! xconnection::{XConn, Xid},
//! },
//! xcb::XcbConnection,
//! Config, Result, WindowManager, logging_error_handler
//! };
//!
//! use std::collections::{HashMap, HashSet};
//!
//! use tracing::info;
//!
//! // Start with the struct itself which will contain any internal state we need to track
//! pub struct LogAddedClients {
//! seen: HashMap<usize, HashSet<Xid>>,
//! }
//!
//! // It is idiomatic for Hooks to provide a `new` method that returns a pre-boxed struct
//! // so that you can add it straight into your hooks Vector in your main.rs
//! impl LogAddedClients {
//! pub fn new() -> Box<Self> {
//! Box::new(Self { seen: HashMap::new() })
//! }
//! }
//!
//! // As we only care about one of the hook points, that is the only method we need to
//! // implement: all other Hook methods for this struct will be no-ops
//! impl<X: XConn> Hook<X> for LogAddedClients {
//! fn client_added_to_workspace(
//! &mut self,
//! wm: &mut WindowManager<X>,
//! id: Xid,
//! wix: usize
//! ) -> Result<()> {
//! let clients = self.seen.entry(wix).or_insert(HashSet::new());
//! if clients.contains(&id) {
//! info!("'{}' has been on '{}' before!", id, wix)
//! } else {
//! clients.insert(id);
//! info!("'{}' was added to '{}' for the first time", id, wix)
//! };
//!
//! Ok(())
//! }
//! }
//!
//! // Now we simply pass our hook to the WindowManager when we create it
//! fn main() -> penrose::Result<()> {
//! let mut manager = WindowManager::new(
//! Config::default(),
//! XcbConnection::new()?,
//! vec![LogAddedClients::new()],
//! logging_error_handler()
//! );
//!
//! manager.init()?;
//!
//! // rest of your startup logic here
//!
//! Ok(())
//! }
//! ```
//!
//! Now, whenever a [Client][4] is added to a [Workspace][1] (either because it has been newly
//! created, or because it has been moved from one workspace to another) our hook will be called,
//! and our log message will be included in the penrose log stream. More complicated hooks can be
//! built that listen to multiple triggers, but most of the time you will likely only need to
//! implement a single method. For an example of a more complex set up, see the [Scratchpad][2]
//! extension which uses multiple hooks to spawn and manage a client program outside of normal
//! `WindowManager` operation.
//!
//! # When hooks are called
//!
//! Each Hook trigger will be called as part of normal execution of `WindowManager` methods at a
//! point that should be relatively intuitive based on the name of the method. Each method provides
//! a more detailed explanation of exactly what conditions it will be called under. If you would
//! like to see exactly which user level actions lead to specific triggers, try turning on `DEBUG`
//! logging in your logging config as part of your **main.rs** and lookk for the "Running <method>
//! hooks" message that each trigger logs out.
//!
//! *Please see the documentation on each of the individual methods for more details.*
//!
//! # WindowManager execution with user defined Hooks
//!
//! As mentioned above, each time a hook trigger point is reached the `WindowManager` stops normal
//! execution (including responding to [XEvents][3]) and each of the registered hooks is called in
//! turn. If the hook implements the method associated with the trigger that has been hit, then
//! your logic will be run and you will have a mutable reference to the current [WindowManager]
//! state, giving you complete control over what happens next. Note that method calls on the
//! `WindowManager` itself will (of course) resolve immediately, but that any actions which
//! generate [XEvents][3] will only be processed once all hooks have run and control has returned to
//! the manager itself.
//!
//! [1]: crate::core::workspace::Workspace
//! [2]: crate::contrib::extensions::scratchpad::Scratchpad
//! [3]: crate::core::xconnection::XEvent
//! [4]: crate::core::client::Client
use crate::;
/// Names of each of the individual hooks that are triggerable in Penrose.
///
/// This enum is used to indicate to the [WindowManager] that a particular hook should now be
/// triggered as the result of some other action that has taken place during execution.
/// Utility type for defining hooks in your penrose configuration.
pub type Hooks<X> = ;
/// User defined functionality triggered by [WindowManager] actions.
///
/// impls of [Hook] can be registered to receive events during [WindowManager] operation. Each hook
/// point is documented as individual methods detailing when and how they will be called. All
/// registered hooks will be called for each trigger so the required methods all provide a no-op
/// default implementation that must be overriden to provide functionality. Hooks may subscribe to
/// multiple triggers to implement more complex behaviours and may store additional state.
///
/// *Care should be taken when writing [Hook] impls to ensure that infinite loops are not created by
/// nested triggers and that, where possible, support for other hooks running from the same triggers
/// is possible.*
///
///
/// # Implementing Hook
///
/// For an example of how to write Hooks, please see the [module level][1] documentation.
///
/// Note that you only need to implement the methods for triggers you intended to respond to: all
/// hook methods have a default empty implementation that is ignored by the `WindowManager`.
///
/// [1]: crate::core::hooks