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
pub mod client;

pub use client::Client;

use itertools::join;
use serde::Serialize;
use std::io::{self, Write};
use std::path::PathBuf;

// Naming convention: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/enum
#[derive(Serialize, Debug, num_derive::FromPrimitive)]
#[serde(rename_all = "snake_case")]
pub enum IpcEvent {
    Workspace = 0x80000000,
    Mode = 0x80000002,
    Window = 0x80000003,
    BarconfigUpdate = 0x80000004,
    Binding = 0x80000005,
    Shutdown = 0x80000006,
    Tick = 0x80000007,
    BarStatusUpdate = 0x80000014,
}

#[derive(Debug)]
enum IpcCommandCode {
    RunCommand = 0,
    GetWorkspaces = 1,
    Subscribe = 2,
    GetOutputs = 3,
    GetTree = 4,
    GetMarks = 5,
    GetBarConfig = 6,
    GetVersion = 7,
    GetBindingModes = 8,
    GetConfig = 9,
    SendTick = 10,
}

#[derive(Debug)]
pub enum IpcCommand {
    Run(String),
    GetBarConfig,
    GetBindingModes,
    GetConfig,
    GetMarks,
    GetOutputs,
    GetTree,
    GetVersion,
    GetWorkspaces,
    SendTick(Vec<u8>),
    Subscribe(Vec<IpcEvent>),
}

impl IpcCommand {
    fn write<W: Write>(&self, w: &mut W) -> io::Result<()> {
        w.write(b"i3-ipc")?;
        match self {
            IpcCommand::Run(command) => {
                let payload = command.as_bytes();
                w.write(&(payload.len() as u32).to_ne_bytes())?;
                w.write(&(self.code() as u32).to_ne_bytes())?;
                w.write(payload)?;
            }
            IpcCommand::SendTick(payload) => {
                w.write(&(payload.len() as u32).to_ne_bytes())?;
                w.write(&(self.code() as u32).to_ne_bytes())?;
                w.write(payload)?;
            }
            IpcCommand::Subscribe(events) => {
                let mut payload = Vec::new();
                serde_json::to_writer(&mut payload, &events)?;
                w.write(&(payload.len() as u32).to_ne_bytes())?;
                w.write(&(self.code() as u32).to_ne_bytes())?;
                w.write(&payload)?;
            }
            _ => {
                w.write(&0u32.to_ne_bytes())?;
                w.write(&(self.code() as u32).to_ne_bytes())?;
            }
        }
        Ok(())
    }

    fn code(&self) -> IpcCommandCode {
        use IpcCommandCode::*;
        match self {
            IpcCommand::GetBarConfig => GetBarConfig,
            IpcCommand::GetBindingModes => GetBindingModes,
            IpcCommand::GetConfig => GetConfig,
            IpcCommand::GetMarks => GetMarks,
            IpcCommand::GetOutputs => GetOutputs,
            IpcCommand::GetTree => GetTree,
            IpcCommand::GetVersion => GetVersion,
            IpcCommand::GetWorkspaces => GetWorkspaces,
            IpcCommand::Run(_) => RunCommand,
            IpcCommand::SendTick(_) => SendTick,
            IpcCommand::Subscribe(_) => Subscribe,
        }
    }
}

#[derive(derive_more::From, derive_more::Display, Debug)]
pub enum Error {
    /// Could not find or reliably guess a SWAYSOCK
    SockPathNotFound,
    /// Generic error for subscription problems. Currently includes send failure on the channel
    /// used to contain subscription events.
    SubscriptionError,
    /// Error thrown when you try to subscribe multiple times on a single connection, which is
    /// not supported.
    AlreadySubscribed,
    Io(io::Error),
}

pub type Result<T> = std::result::Result<T, Error>;

/// Try to guess the value of SWAYSOCK by first checking for the environment variable or using the
/// most recently created sock file at /run/user/$UID/sway-ipc.*.sock. This is useful for the
/// situation where a command is being run from systemd or outside of the GUI environment.
pub fn guess_sway_socket_path() -> Result<PathBuf> {
    match std::env::var("SWAYSOCK") {
        Ok(path) => Ok(PathBuf::from(path)),
        Err(_) => {
            let entry = globwalk::glob("/run/user/*/sway-ipc.*.sock")
                // Failed to get glob
                .map_err(|_| Error::SockPathNotFound)?
                .next()
                // No entries found
                .ok_or_else(|| Error::SockPathNotFound)?
                // Failed to unwrap entry. oof
                .map_err(|_| Error::SockPathNotFound)?;
            Ok(entry.into_path())
        }
    }
}

pub mod criteria {
    use std::fmt::Display;

    #[derive(derive_more::Display)]
    pub enum Criteria {
        /// Compare value against the app id. Can be a regular expression. If value is __focused__, then the app id must be the same as that of the
        /// currently focused window. app_id are specific to Wayland applications.
        #[display(fmt = "app_id=\"{}\"", "_0")]
        AppId(OrFocused<String>),

        /// Compare value against the window class. Can be a regular expression. If value is __focused__, then the window class must be the same as
        /// that of the currently focused window. class are specific to X11 applications.
        #[display(fmt = "class=\"{}\"", "_0")]
        Class(OrFocused<String>),

        /// Compare against the internal container ID, which you can find via IPC. If value is __focused__, then the id must be the same as that of the
        /// currently focused window.
        #[display(fmt = "con_id=\"{}\"", "_0")]
        ConId(OrFocused<u32>),

        /// Compare against the window marks. Can be a regular expression.
        #[display(fmt = "con_mark=\"{}\"", "_0")]
        ConMark(String),

        /// Matches floating windows.
        #[display(fmt = "floating")]
        Floating,

        /// Compare value against the X11 window ID. Must be numeric.
        #[display(fmt = "id=\"{}\"", "_0")]
        Id(u32),

        /// Compare value against the window instance. Can be a regular expression. If value is __focused__, then the window instance must be the same
        /// as that of the currently focused window.
        #[display(fmt = "instance=\"{}\"", "_0")]
        Instance(OrFocused<String>),

        /// Compare value against the window shell, such as "xdg_shell" or "xwayland".  Can be a regular expression. If value is __focused__, then the
        /// shell must be the same as that of the currently focused window.
        #[display(fmt = "shell=\"{}\"", "_0")]
        Shell(OrFocused<String>),

        /// Matches tiling windows.
        #[display(fmt = "tiling")]
        Tiling,

        /// Compare against the window title. Can be a regular expression. If value is __focused__, then the window title must be the same as that of
        /// the currently focused window.
        #[display(fmt = "title=\"{}\"", "_0")]
        Title(OrFocused<String>),

        /// Compares the urgent state of the window. Can be "first", "last", "latest", "newest", "oldest" or "recent".
        // TODO make enum
        #[display(fmt = "urgent=\"{}\"", "_0")]
        Urgent(String),

        /// Compare against the window role (WM_WINDOW_ROLE). Can be a regular expression. If value is __focused__, then the window role must be the
        /// same as that of the currently focused window.
        #[display(fmt = "window_role=\"{}\"", "_0")]
        WindowRole(OrFocused<String>),

        /// Compare against the window type (_NET_WM_WINDOW_TYPE). Possible values are normal, dialog, utility, toolbar, splash, menu, dropdown_menu,
        /// popup_menu, tooltip and notification.
        // TODO make enum
        #[display(fmt = "window_type=\"{}\"", "_0")]
        WindowType(String),

        /// Compare against the workspace name for this view. Can be a regular expression. If the value is __focused__, then all the views on the cur‐
        /// rently focused workspace matches.
        #[display(fmt = "workspace=\"{}\"", "_0")]
        Workspace(OrFocused<String>),
    }

    #[derive(derive_more::Display)]
    pub enum OrFocused<T> {
        #[display(fmt = "__focused__")]
        Focused,
        #[display(fmt = "{}", "_0")]
        Value(T),
    }

    impl<T> From<T> for OrFocused<T> {
        fn from(t: T) -> Self {
            OrFocused::Value(t)
        }
    }

    impl<T> From<Option<T>> for OrFocused<T> {
        fn from(t: Option<T>) -> Self {
            match t {
                Some(t) => OrFocused::Value(t),
                None => OrFocused::Focused,
            }
        }
    }

    impl<T> OrFocused<T> {
        fn map<U, F: FnOnce(T) -> U>(self, f: F) -> OrFocused<U> {
            match self {
                OrFocused::Focused => OrFocused::Focused,
                OrFocused::Value(t) => OrFocused::Value(f(t)),
            }
        }
    }

    pub fn focused<T>() -> OrFocused<T> {
        OrFocused::Focused
    }

    /// Compare value against the app id. Can be a regular expression. If value is __focused__, then the app id must be the same as that of the
    /// currently focused window. app_id are specific to Wayland applications.
    pub fn app_id<S: Display, T: Into<OrFocused<S>>>(t: T) -> Criteria {
        Criteria::AppId(t.into().map(|s| s.to_string()))
    }

    /// Compare value against the window class. Can be a regular expression. If value is __focused__, then the window class must be the same as
    /// that of the currently focused window. class are specific to X11 applications.
    pub fn class<S: Display, T: Into<OrFocused<S>>>(t: T) -> Criteria {
        Criteria::Class(t.into().map(|s| s.to_string()))
    }

    /// Compare against the internal container ID, which you can find via IPC. If value is __focused__, then the id must be the same as that of the
    /// currently focused window.
    pub fn con_id<T: Into<OrFocused<u32>>>(t: T) -> Criteria {
        Criteria::ConId(t.into())
    }

    /// Compare against the window marks. Can be a regular expression.
    pub fn con_mark(t: String) -> Criteria {
        Criteria::ConMark(t)
    }

    /// Matches floating windows.
    pub fn floating() -> Criteria {
        Criteria::Floating
    }

    /// Compare value against the X11 window ID. Must be numeric.
    pub fn id<T: Into<u32>>(t: T) -> Criteria {
        Criteria::Id(t.into())
    }

    /// Compare value against the window instance. Can be a regular expression. If value is __focused__, then the window instance must be the same
    /// as that of the currently focused window.
    pub fn instance<S: Display, T: Into<OrFocused<S>>>(t: T) -> Criteria {
        Criteria::Instance(t.into().map(|s| s.to_string()))
    }

    /// Compare value against the window shell, such as "xdg_shell" or "xwayland".  Can be a regular expression. If value is __focused__, then the
    /// shell must be the same as that of the currently focused window.
    pub fn shell<S: Display, T: Into<OrFocused<S>>>(t: T) -> Criteria {
        Criteria::Shell(t.into().map(|s| s.to_string()))
    }

    /// Matches tiling windows.
    pub fn tiling() -> Criteria {
        Criteria::Tiling
    }

    /// Compare against the window title. Can be a regular expression. If value is __focused__, then the window title must be the same as that of
    /// the currently focused window.
    pub fn title<S: Display, T: Into<OrFocused<S>>>(t: T) -> Criteria {
        Criteria::Title(t.into().map(|s| s.to_string()))
    }

    /// Compares the urgent state of the window. Can be "first", "last", "latest", "newest", "oldest" or "recent".
    // TODO make enum
    pub fn urgent<T: Display>(t: T) -> Criteria {
        Criteria::Urgent(t.to_string())
    }

    /// Compare against the window role (WM_WINDOW_ROLE). Can be a regular expression. If value is __focused__, then the window role must be the
    /// same as that of the currently focused window.
    pub fn window_role<S: Display, T: Into<OrFocused<S>>>(t: T) -> Criteria {
        Criteria::WindowRole(t.into().map(|s| s.to_string()))
    }

    /// Compare against the window type (_NET_WM_WINDOW_TYPE). Possible values are normal, dialog, utility, toolbar, splash, menu, dropdown_menu,
    /// popup_menu, tooltip and notification.
    // TODO make enum
    pub fn window_type<T: Display>(t: T) -> Criteria {
        Criteria::WindowType(t.to_string())
    }

    /// Compare against the workspace name for this view. Can be a regular expression. If the value is __focused__, then all the views on the cur‐
    /// rently focused workspace matches.
    pub fn workspace<T: Into<OrFocused<String>>>(t: T) -> Criteria {
        Criteria::Workspace(t.into())
    }
}

pub mod command {
    use super::Command;

    pub fn exec<T: Into<String>>(t: T) -> Command {
        Command::Exec(t.into())
    }

    pub fn raw<T: Into<String>>(t: T) -> Command {
        Command::Raw(t.into())
    }
}

pub mod ipc_command {
    use super::IpcCommand;
    use super::IpcEvent;

    pub fn get_bar_config() -> IpcCommand {
        IpcCommand::GetBarConfig
    }
    pub fn get_binding_modes() -> IpcCommand {
        IpcCommand::GetBindingModes
    }
    pub fn get_config() -> IpcCommand {
        IpcCommand::GetConfig
    }
    pub fn get_marks() -> IpcCommand {
        IpcCommand::GetMarks
    }
    pub fn get_outputs() -> IpcCommand {
        IpcCommand::GetOutputs
    }
    pub fn get_tree() -> IpcCommand {
        IpcCommand::GetTree
    }
    pub fn get_version() -> IpcCommand {
        IpcCommand::GetVersion
    }
    pub fn get_workspaces() -> IpcCommand {
        IpcCommand::GetWorkspaces
    }

    pub fn run<T: Into<String>>(t: T) -> IpcCommand {
        IpcCommand::Run(t.into())
    }

    pub fn tick<T: Into<Vec<u8>>>(t: T) -> IpcCommand {
        IpcCommand::SendTick(t.into())
    }

    pub fn subscribe<T: Into<Vec<IpcEvent>>>(t: T) -> IpcCommand {
        IpcCommand::Subscribe(t.into())
    }
}

#[derive(derive_more::Display)]
pub enum Command {
    #[display(
        fmt = "[{}] {}",
        r#"join(criteria.iter().map(ToString::to_string), " ")"#,
        "command"
    )]
    WithCriteria {
        // TODO hashset?
        criteria: Vec<criteria::Criteria>,
        command: Box<Command>,
    },
    #[display(fmt = "exec {}", "_0")]
    Exec(String),
    #[display(fmt = "{}", "_0")]
    Raw(String),
}

impl Command {
    /// Prepend criteria to this command. A vec is used so that ordering can be deterministic,
    /// which can be useful.
    pub fn with_criteria(self, criteria: Vec<criteria::Criteria>) -> Self {
        Command::WithCriteria {
            criteria,
            command: Box::new(self),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn verify_buffer(buf: &[u8], code: IpcCommandCode, payload: &[u8]) {
        let prefix = b"i3-ipc";
        assert_eq!(&buf[0..prefix.len()], prefix);
        assert_eq!(
            buf[prefix.len()..prefix.len() + 4],
            dbg!(payload.len() as u32).to_ne_bytes()
        );
        assert_eq!(
            buf[prefix.len() + 4..prefix.len() + 8],
            dbg!(code as u32).to_ne_bytes()
        );
        assert_eq!(&buf[prefix.len() + 8..], payload);
    }

    #[test]
    fn ipc_command_write() {
        {
            let mut buffer = Vec::new();
            // dbg!(IpcCommand::Run("exec st".into()))
            dbg!(ipc_command::run("exec st"))
                .write(&mut buffer)
                .unwrap();
            verify_buffer(&buffer, IpcCommandCode::RunCommand, b"exec st");
        }
        {
            let mut buffer = Vec::new();
            // dbg!(IpcCommand::SendTick("HELLO WORLD".into()))
            dbg!(ipc_command::tick("HELLO WORLD"))
                .write(&mut buffer)
                .unwrap();
            verify_buffer(&buffer, IpcCommandCode::SendTick, b"HELLO WORLD");
        }
        {
            use IpcEvent::*;
            let mut buffer = Vec::new();
            // dbg!(IpcCommand::Subscribe(vec![Window, Tick]))
            dbg!(ipc_command::subscribe(vec![Window, Tick]))
                .write(&mut buffer)
                .unwrap();
            verify_buffer(&buffer, IpcCommandCode::Subscribe, b"[\"window\",\"tick\"]");
        }
    }

    #[test]
    fn criteria_command() {
        use command::*;
        use criteria::*;

        assert_eq!(&exec("st").to_string(), "exec st");
        assert_eq!(
            &exec("st").with_criteria(vec![con_id(123)]).to_string(),
            r#"[con_id="123"] exec st"#
        );
        assert_eq!(
            &raw("123123")
                .with_criteria(vec![
                    con_mark("123".into()),
                    con_id(123),
                    workspace(focused()),
                ])
                .to_string(),
            r#"[con_mark="123" con_id="123" workspace="__focused__"] 123123"#
        );
    }
}