paneru 0.3.2

A sliding, tiling window manager for MacOS.
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
use core::ptr::NonNull;
use log::{debug, error, info};
use objc2::rc::Retained;
use std::ffi::c_void;
use stdext::function_name;

use super::workspace::WorkspaceObserver;
use crate::errors::{Error, Result};
use crate::events::{Event, EventSender};
use crate::platform::OSStatus;
use crate::util::Cleanuper;

/// Represents a process serial number (PSN), a unique identifier for a running process on macOS.
/// It is used by the Carbon APIs to identify applications.
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
#[repr(C)]
pub struct ProcessSerialNumber {
    /// The high-order 32 bits of the process serial number.
    pub high: u32,
    /// The low-order 32 bits of the process serial number.
    pub low: u32,
}

/// Type alias for the callback function signature used by `InstallEventHandler` for process events.
type ProcessCallbackFn = extern "C-unwind" fn(
    this: *mut c_void,
    event: *const ProcessEvent,
    context: *const c_void,
) -> OSStatus;

unsafe extern "C" {
    /// Retrieves the application event target.
    /// This function returns an `EventTargetRef` that represents the application's event queue.
    ///
    /// # Returns
    ///
    /// A raw pointer to an `EventTargetRef` for the application.
    ///
    /// # Original signature
    /// extern `EventTargetRef` GetApplicationEventTarget(void)
    fn GetApplicationEventTarget() -> *const ProcessEventTarget;

    /// Installs an event handler for a specific event target and event types.
    /// This function sets up a callback to be invoked when specified Carbon events occur.
    ///
    /// # Arguments
    ///
    /// * `target` - The `EventTargetRef` to install the handler on, typically obtained from `GetApplicationEventTarget`.
    /// * `handler` - The `ProcessCallbackFn` to be called when events match.
    /// * `event_len` - The number of event types in `events`.
    /// * `events` - A raw pointer to an array of `EventTypeSpec` defining the events to handle.
    /// * `user_data` - A raw pointer to user-defined data to pass to the handler's `context` parameter.
    /// * `handler_ref` - A mutable raw pointer to an `EventHandlerRef` where the installed handler
    ///   reference will be stored.
    ///
    /// # Returns
    ///
    /// An `OSStatus` indicating success or failure.
    ///
    /// # Original signature
    /// extern `OSStatus`
    /// `InstallEventHandler`(
    ///   `EventTargetRef`         inTarget,
    ///   `EventHandlerUPP`        inHandler,
    ///   `ItemCount`              inNumTypes,
    ///   const `EventTypeSpec` *  inList,
    ///   void *                 inUserData,
    ///   `EventHandlerRef` *      outRef)
    fn InstallEventHandler(
        target: *const ProcessEventTarget,
        handler: ProcessCallbackFn,
        event_len: u32,
        events: *const EventTypeSpec,
        user_data: *const c_void,
        handler_ref: *mut *const ProcessEventHandler,
    ) -> OSStatus;

    /// Removes a previously installed event handler.
    ///
    /// # Arguments
    ///
    /// * `handler_ref` - A raw pointer to the `EventHandlerRef` to remove.
    ///
    /// # Returns
    ///
    /// An `OSStatus` indicating success or failure.
    ///
    /// # Original signature
    /// extern `OSStatus` RemoveEventHandler(EventHandlerRef inHandlerRef)
    fn RemoveEventHandler(handler_ref: *const ProcessEventHandler) -> OSStatus;

    /// Gets a piece of data from the given event, if it exists.
    /// The Carbon Event Manager will automatically use `AppleEvent` coercion handlers to convert
    /// the data in the event into the desired type, if possible. You may also pass `typeWildCard`
    /// to request that the data be returned in its original format.
    ///
    /// # Mac OS X threading
    /// Not thread safe.
    ///
    /// # Arguments
    ///
    /// * `event` - The event to get the parameter from.
    /// * `param_name` - The symbolic name of the parameter (e.g., `kEventParamProcessID`).
    /// * `param_type` - The desired type of the parameter (e.g., `typeProcessSerialNumber`).
    /// * `actual_type` - A mutable pointer to `u32` to store the actual type of the parameter, or `NULL`.
    /// * `size` - The size of the output buffer specified by `data`. Pass zero and
    ///   `NULL` for `data` if data is not desired.
    /// * `actual_size` - A mutable pointer to `u32` to store the actual size of the
    ///   data, or `NULL`.
    /// * `data` - A mutable pointer to the buffer which will receive the parameter data, or `NULL`.
    ///
    /// # Returns
    ///
    /// An operating system result code (`OSStatus`).
    ///
    /// # Original signature
    /// extern `OSStatus`
    /// `GetEventParameter`(
    ///   `EventRef`          inEvent,
    ///   `EventParamName`    inName,
    ///   `EventParamType`    inDesiredType,
    ///   `EventParamType` *  outActualType,       /* can be NULL */
    ///   `ByteCount`         inBufferSize,
    ///   `ByteCount` *       outActualSize,       /* can be NULL */
    ///   void *            outData)             /* can be NULL */
    fn GetEventParameter(
        event: *const ProcessEvent,
        param_name: u32,
        param_type: u32,
        actual_type: *mut u32,
        size: u32,
        actual_size: *mut u32,
        data: *mut c_void,
    ) -> OSStatus;

    /// Returns the kind of the given event (e.g., mousedown).
    /// Event kinds overlap between event classes (e.g., `kEventMouseDown` and `kEventAppActivated`
    /// have the same value). The combination of class and kind determines an event signature.
    ///
    /// # Mac OS X threading
    /// Thread safe.
    ///
    /// # Arguments
    ///
    /// * `event` - The event in question.
    ///
    /// # Returns
    ///
    /// The kind of the event (`UInt32`).
    ///
    /// # Original signature
    /// extern `UInt32` GetEventKind(EventRef inEvent)
    fn GetEventKind(event: *const ProcessEvent) -> u32;

    /// Retrieves the next available process's serial number.
    /// This function iterates through all running processes and returns their PSN.
    ///
    /// # Arguments
    ///
    /// * `psn` - A mutable pointer to a `ProcessSerialNumber` structure. On the first call, pass a
    ///   PSN with `kNoProcess` for `highLongOfPSN` and `lowLongOfPSN`. On subsequent calls, pass
    ///   the PSN returned by the previous call.
    ///
    /// # Returns
    ///
    /// An `OSStatus` code. `noErr` (0) if a process was found, otherwise an error code.
    ///
    /// # Original signature
    /// GetNextProcess(ProcessSerialNumber * pPSN)
    fn GetNextProcess(psn: *mut ProcessSerialNumber) -> OSStatus;
}

/*
 *  EventTypeSpec
 *
 *  Discussion:
 *    This structure is used to specify an event. Typically, a static
 *    array of EventTypeSpecs are passed into functions such as
 *    InstallEventHandler, as well as routines such as
 *    FlushEventsMatchingListFromQueue.
 */
// struct EventTypeSpec {
//   OSType              eventClass;
//   UInt32              eventKind;
// };
/// Specifies a Carbon event by its class and kind.
/// Used for registering and matching events with event handlers.
#[repr(C)]
struct EventTypeSpec {
    /// The event class (e.g., `kEventClassApplication`).
    event_class: u32,
    /// The event kind within its class (e.g., `kEventAppLaunched`).
    event_kind: u32,
}

/// An opaque type representing a Carbon event handler reference.
#[repr(C)]
struct ProcessEventHandler {
    _opaque: [u8; 0],
}

/// An opaque type representing a Carbon event target reference.
#[repr(C)]
struct ProcessEventTarget {
    _opaque: [u8; 0],
}

/// An opaque type representing a Carbon event.
#[repr(C)]
struct ProcessEvent {
    _opaque: [u8; 0],
}

/// `ProcessHandler` is a Rust-side representation of the handler for Carbon process events.
/// It receives raw Carbon events and dispatches them as higher-level `Event`s through an `EventSender`.
#[repr(C)]
pub(super) struct ProcessHandler {
    /// The sender for dispatching processed events.
    events: EventSender,
    /// An optional `Cleanuper` to manage the unregistration of the Carbon event handler.
    cleanup: Option<Cleanuper>,
    /// A retained reference to the `WorkspaceObserver`, used for `ApplicationLaunched` events.
    observer: Retained<WorkspaceObserver>,
}

/// An enum representing different types of Carbon application-related events.
/// These correspond to specific event kinds within the `kEventClassApplication` event class.
#[repr(C)]
#[allow(dead_code)]
enum ProcessEventApp {
    /// Application activated event.
    Activated = 1,
    /// Application deactivated event.
    Deactivated = 2,
    /// Application quit event.
    Quit = 3,
    /// Application launch notification.
    LaunchNotification = 4,
    /// Application launched event.
    Launched = 5,
    /// Application terminated event.
    Terminated = 6,
    /// Frontmost application switched event.
    FrontSwitched = 7,

    /// Focus menu bar event.
    FocusMenuBar = 8,
    /// Focus next document window event.
    FocusNextDocumentWindow = 9,
    /// Focus next floating window event.
    FocusNextFloatingWindow = 10,
    /// Focus toolbar event.
    FocusToolbar = 11,
    /// Focus drawer event.
    FocusDrawer = 12,

    /// Get Dock tile menu event.
    GetDockTileMenu = 20,
    /// Update Dock tile event.
    UpdateDockTile = 21,

    /// Is event in Instant Mouser event.
    IsEventInInstantMouser = 104,

    /// Application hidden event.
    Hidden = 107,
    /// Application shown event.
    Shown = 108,
    /// System UI mode changed event.
    SystemUIModeChanged = 109,
    /// Available window bounds changed event.
    AvailableWindowBoundsChanged = 110,
    /// Active window changed event.
    ActiveWindowChanged = 111,
}

impl ProcessHandler {
    /// Creates a new `ProcessHandler` instance.
    ///
    /// # Arguments
    ///
    /// * `events` - An `EventSender` to send process-related events.
    /// * `observer` - A `Retained<WorkspaceObserver>` to pass to `ApplicationLaunched` events.
    ///
    /// # Returns
    ///
    /// A new `ProcessHandler`.
    pub(super) fn new(events: EventSender, observer: Retained<WorkspaceObserver>) -> Self {
        ProcessHandler {
            events,
            cleanup: None,
            observer,
        }
    }

    /// Starts the process handler by registering a C-callback with the underlying private API.
    /// It also sends initial `ApplicationLaunched` events for already running processes.
    ///
    /// # Side Effects
    ///
    /// - Registers a Carbon event handler, which will be unregistered when `cleanup` is dropped.
    /// - Iterates through existing processes and dispatches `ApplicationLaunched` events for them.
    pub(super) fn start(&mut self) -> Result<()> {
        const APPL_CLASS: &str = "appl";
        const PROCESS_EVENT_LAUNCHED: u32 = 5;
        const PROCESS_EVENT_TERMINATED: u32 = 6;
        const PROCESS_EVENT_FRONTSWITCHED: u32 = 7;

        info!("{}: Registering process_handler", function_name!());

        // Fake launch the existing processes.
        let mut psn = ProcessSerialNumber::default();
        while 0 == unsafe { GetNextProcess(&raw mut psn) } {
            self.process_handler(psn, ProcessEventApp::Launched);
        }

        let target = unsafe { GetApplicationEventTarget() };
        let event_class = u32::from_be_bytes(APPL_CLASS.as_bytes().try_into()?);
        let events = [
            EventTypeSpec {
                event_class,
                event_kind: PROCESS_EVENT_LAUNCHED,
            },
            EventTypeSpec {
                event_class,
                event_kind: PROCESS_EVENT_TERMINATED,
            },
            EventTypeSpec {
                event_class,
                event_kind: PROCESS_EVENT_FRONTSWITCHED,
            },
        ];
        let mut handler: *const ProcessEventHandler = std::ptr::null();
        let result = unsafe {
            InstallEventHandler(
                target,
                Self::callback,
                events.len().try_into()?,
                events.as_ptr(),
                NonNull::new_unchecked(self).as_ptr().cast(),
                &raw mut handler,
            )
        };
        if result != 0 || handler.is_null() {
            return Err(Error::PermissionDenied(format!(
                "{}: Error registering process event handler.",
                function_name!()
            )));
        }
        debug!(
            "{}: Registered process_handler (result = {result}): {handler:x?}",
            function_name!()
        );

        self.cleanup = Some(Cleanuper::new(Box::new(move || unsafe {
            info!(
                "{}: Unregistering process_handler: {handler:?}",
                function_name!()
            );
            RemoveEventHandler(handler);
        })));
        Ok(())
    }

    /// The C-callback function invoked by the private process handling API. It dispatches to the `process_handler` method.
    /// This function is declared as `extern "C-unwind"`.
    ///
    /// # Arguments
    ///
    /// * `_` - Unused callback info parameter.
    /// * `event` - A raw pointer to the `ProcessEvent`.
    /// * `this` - A raw pointer to the `ProcessHandler` instance.
    ///
    /// # Returns
    ///
    /// An `OSStatus`.
    extern "C-unwind" fn callback(
        _: *mut c_void,
        event: *const ProcessEvent,
        this: *const c_void,
    ) -> OSStatus {
        match NonNull::new(this.cast_mut())
            .map(|this| unsafe { this.cast::<ProcessHandler>().as_mut() })
        {
            Some(this) => {
                const PARAM: &str = "psn "; // kEventParamProcessID and typeProcessSerialNumber
                let param_name = u32::from_be_bytes(PARAM.as_bytes().try_into().unwrap());
                let param_type = param_name; // Uses the same FourCharCode as param_name

                let mut psn = ProcessSerialNumber::default();

                let res = unsafe {
                    GetEventParameter(
                        event,
                        param_name,
                        param_type,
                        std::ptr::null_mut(),
                        std::mem::size_of::<ProcessSerialNumber>()
                            .try_into()
                            .unwrap(),
                        std::ptr::null_mut(),
                        NonNull::from(&mut psn).as_ptr().cast(),
                    )
                };
                if res == 0 {
                    let decoded: ProcessEventApp =
                        unsafe { std::mem::transmute(GetEventKind(event)) };
                    this.process_handler(psn, decoded);
                }
            }
            _ => error!("Zero passed to Process Handler."),
        }
        0
    }

    /// Handles various process events received from the C callback. It sends corresponding `Event`s via `events`.
    ///
    /// # Arguments
    ///
    /// * `psn` - The `ProcessSerialNumber` of the process involved in the event.
    /// * `event` - The `ProcessEventApp` indicating the type of event (e.g., `Launched`, `Terminated`).
    fn process_handler(&mut self, psn: ProcessSerialNumber, event: ProcessEventApp) {
        let _ = match event {
            ProcessEventApp::Launched => self.events.send(Event::ApplicationLaunched {
                psn,
                observer: self.observer.clone(),
            }),
            ProcessEventApp::Terminated => self.events.send(Event::ApplicationTerminated { psn }),
            ProcessEventApp::FrontSwitched => {
                self.events.send(Event::ApplicationFrontSwitched { psn })
            }
            _ => {
                error!(
                    "{}: Unknown process event: {}",
                    function_name!(),
                    event as u32
                );
                Ok(())
            }
        }
        .inspect_err(|err| error!("{}: error sending event: {err}", function_name!()));
    }
}