Skip to main content

jay_config/
exec.rs

1//! Tools for spawning programs.
2
3use std::{cell::RefCell, collections::HashMap, os::fd::OwnedFd};
4
5/// Sets an environment variable.
6///
7/// This does not affect the compositor itself but only programs spawned by the compositor.
8pub fn set_env(key: &str, val: &str) {
9    get!().set_env(key, val);
10}
11
12/// Unsets an environment variable.
13///
14/// This does not affect the compositor itself but only programs spawned by the compositor.
15pub fn unset_env(key: &str) {
16    get!().unset_env(key);
17}
18
19/// A command to be spawned.
20pub struct Command {
21    pub(crate) prog: String,
22    pub(crate) args: Vec<String>,
23    pub(crate) env: HashMap<String, String>,
24    pub(crate) fds: RefCell<HashMap<i32, OwnedFd>>,
25    pub(crate) tag: Option<String>,
26}
27
28impl Command {
29    /// Creates a new command to be spawned.
30    ///
31    /// `prog` should be the path to the program being spawned. If `prog` does not contain
32    /// a `/`, then it will be searched in `PATH` similar to how a shell would do it.
33    ///
34    /// The first argument passed to `prog`, `argv[0]`, is `prog` itself.
35    pub fn new(prog: &str) -> Self {
36        Self {
37            prog: prog.to_string(),
38            args: vec![],
39            env: Default::default(),
40            fds: Default::default(),
41            tag: Default::default(),
42        }
43    }
44
45    /// Adds an argument to be passed to the command.
46    pub fn arg(&mut self, arg: &str) -> &mut Self {
47        self.args.push(arg.to_string());
48        self
49    }
50
51    /// Sets an environment variable for this command only.
52    pub fn env(&mut self, key: &str, val: &str) -> &mut Self {
53        self.env.insert(key.to_string(), val.to_string());
54        self
55    }
56
57    /// Sets a file descriptor of the process.
58    ///
59    /// By default, the process starts with exactly stdin, stdout, and stderr open and all
60    /// pointing to `/dev/null`.
61    pub fn fd<F: Into<OwnedFd>>(&mut self, idx: i32, fd: F) -> &mut Self {
62        self.fds.borrow_mut().insert(idx, fd.into());
63        self
64    }
65
66    /// Sets the stdin of the process.
67    ///
68    /// This is equivalent to `fd(0, fd)`.
69    pub fn stdin<F: Into<OwnedFd>>(&mut self, fd: F) -> &mut Self {
70        self.fd(0, fd)
71    }
72
73    /// Sets the stdout of the process.
74    ///
75    /// This is equivalent to `fd(1, fd)`.
76    pub fn stdout<F: Into<OwnedFd>>(&mut self, fd: F) -> &mut Self {
77        self.fd(1, fd)
78    }
79
80    /// Sets the stderr of the process.
81    ///
82    /// This is equivalent to `fd(2, fd)`.
83    pub fn stderr<F: Into<OwnedFd>>(&mut self, fd: F) -> &mut Self {
84        self.fd(2, fd)
85    }
86
87    /// Runs the application with access to privileged wayland protocols.
88    ///
89    /// The default is `false`.
90    pub fn privileged(&mut self) -> &mut Self {
91        match get!(self).get_socket_path() {
92            Some(path) => {
93                self.env("WAYLAND_DISPLAY", &format!("{path}.jay"));
94            }
95            _ => {
96                log::error!("Compositor did not send the socket path");
97            }
98        }
99        self
100    }
101
102    /// Adds a tag to Wayland connections created by the spawned command.
103    pub fn tag(&mut self, tag: &str) -> &mut Self {
104        self.tag = Some(tag.to_owned());
105        self
106    }
107
108    /// Executes the command.
109    ///
110    /// This consumes all attached file descriptors.
111    pub fn spawn(&self) {
112        get!().spawn(self);
113    }
114}