| 1 | use std::ffi::{CString, OsStr}; |
| 2 | use std::io::{self, Write}; |
| 3 | use std::os::unix::prelude::OsStrExt; |
| 4 | use std::os::unix::process::CommandExt; |
| 5 | use std::path::PathBuf; |
| 6 | use std::process::{Command, Stdio}; |
| 7 | use std::ptr::null_mut; |
| 8 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 9 | use std::thread; |
| 10 | use std::time::Duration; |
| 11 | |
| 12 | use anyhow::{ensure, Context}; |
| 13 | use directories::UserDirs; |
| 14 | use smithay::reexports::rustix::time::{clock_gettime, ClockId}; |
| 15 | use smithay::utils::{Logical, Point, Rectangle}; |
| 16 | |
| 17 | use crate::config::Config; |
| 18 | |
| 19 | pub fn get_monotonic_time() -> Duration { |
| 20 | let ts = clock_gettime(ClockId::Monotonic); |
| 21 | Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32) |
| 22 | } |
| 23 | |
| 24 | pub fn center(rect: Rectangle<i32, Logical>) -> Point<i32, Logical> { |
| 25 | rect.loc + rect.size.downscale(2).to_point() |
| 26 | } |
| 27 | |
| 28 | pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> { |
| 29 | let Some(path) = &config.screenshot_path else { |
| 30 | return Ok(None); |
| 31 | }; |
| 32 | |
| 33 | let format = CString::new(path.clone()).context("path must not contain nul bytes")?; |
| 34 | |
| 35 | let mut buf = [0u8; 2048]; |
| 36 | let mut path; |
| 37 | unsafe { |
| 38 | let time = libc::time(null_mut()); |
| 39 | ensure!(time != -1, "error in time()"); |
| 40 | |
| 41 | let tm = libc::localtime(&time); |
| 42 | ensure!(!tm.is_null(), "error in localtime()"); |
| 43 | |
| 44 | let rv = libc::strftime(buf.as_mut_ptr().cast(), buf.len(), format.as_ptr(), tm); |
| 45 | ensure!(rv != 0, "error formatting time"); |
| 46 | |
| 47 | path = PathBuf::from(OsStr::from_bytes(&buf[..rv])); |
| 48 | } |
| 49 | |
| 50 | if let Ok(rest) = path.strip_prefix("~") { |
| 51 | let dirs = UserDirs::new().context("error retrieving home directory")?; |
| 52 | path = [dirs.home_dir(), rest].iter().collect(); |
| 53 | } |
| 54 | |
| 55 | Ok(Some(path)) |
| 56 | } |
| 57 | |
| 58 | pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false); |
| 59 | pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false); |
| 60 | |
| 61 | |
| 62 | pub fn spawn<T: AsRef<OsStr> + Send + 'static>(command: Vec<T>) { |
| 63 | let _span = tracy_client::span!(); |
| 64 | |
| 65 | if command.is_empty() { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | let res = thread::Builder::new() |
| 71 | .name("Command Spawner".to_owned()) |
| 72 | .spawn(move || { |
| 73 | let (command, args) = command.split_first().unwrap(); |
| 74 | spawn_sync(command, args); |
| 75 | }); |
| 76 | |
| 77 | if let Err(err) = res { |
| 78 | warn!("error spawning a thread to spawn the command: {err:?}"); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) { |
| 83 | let _span = tracy_client::span!(); |
| 84 | |
| 85 | let command = command.as_ref(); |
| 86 | |
| 87 | let mut process = Command::new(command); |
| 88 | process |
| 89 | .args(args) |
| 90 | .stdin(Stdio::null()) |
| 91 | .stdout(Stdio::null()) |
| 92 | .stderr(Stdio::null()); |
| 93 | |
| 94 | |
| 95 | if REMOVE_ENV_RUST_BACKTRACE.load(Ordering::Relaxed) { |
| 96 | process.env_remove("RUST_BACKTRACE"); |
| 97 | } |
| 98 | if REMOVE_ENV_RUST_LIB_BACKTRACE.load(Ordering::Relaxed) { |
| 99 | process.env_remove("RUST_LIB_BACKTRACE"); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | unsafe { |
| 104 | process.pre_exec(|| { |
| 105 | match libc::fork() { |
| 106 | -1 => return Err(io::Error::last_os_error()), |
| 107 | 0 => (), |
| 108 | _ => libc::_exit(0), |
| 109 | } |
| 110 | |
| 111 | Ok(()) |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | let mut child = match process.spawn() { |
| 116 | Ok(child) => child, |
| 117 | Err(err) => { |
| 118 | warn!("error spawning {command:?}: {err:?}"); |
| 119 | return; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | match child.wait() { |
| 124 | Ok(status) => { |
| 125 | if !status.success() { |
| 126 | warn!("child did not exit successfully: {status:?}"); |
| 127 | } |
| 128 | } |
| 129 | Err(err) => { |
| 130 | warn!("error waiting for child: {err:?}"); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | pub fn write_png_rgba8( |
| 136 | w: impl Write, |
| 137 | width: u32, |
| 138 | height: u32, |
| 139 | pixels: &[u8], |
| 140 | ) -> Result<(), png::EncodingError> { |
| 141 | let mut encoder = png::Encoder::new(w, width, height); |
| 142 | encoder.set_color(png::ColorType::Rgba); |
| 143 | encoder.set_depth(png::BitDepth::Eight); |
| 144 | |
| 145 | let mut writer = encoder.write_header()?; |
| 146 | writer.write_image_data(pixels) |
| 147 | } |
| 148 | |
| 149 | #[cfg(feature = "dbus")] |
| 150 | pub fn show_screenshot_notification(image_path: Option<PathBuf>) { |
| 151 | let mut notification = notify_rust::Notification::new(); |
| 152 | notification |
| 153 | .summary("Screenshot captured") |
| 154 | .body("You can paste the image from the clipboard.") |
| 155 | .urgency(notify_rust::Urgency::Normal) |
| 156 | .hint(notify_rust::Hint::Transient(true)); |
| 157 | |
| 158 | |
| 159 | if let Some(path) = image_path { |
| 160 | match path.canonicalize() { |
| 161 | Ok(path) => match url::Url::from_file_path(path) { |
| 162 | Ok(url) => { |
| 163 | notification.image_path(url.as_str()); |
| 164 | } |
| 165 | Err(err) => { |
| 166 | warn!("error converting screenshot path to file url: {err:?}"); |
| 167 | } |
| 168 | }, |
| 169 | Err(err) => { |
| 170 | warn!("error canonicalizing screenshot path: {err:?}"); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if let Err(err) = notification.show() { |
| 176 | warn!("error showing screenshot notification: {err:?}"); |
| 177 | } |
| 178 | } |