forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBf51dd67Ivan Molodetskikh2 years ago981 commits

mod.rs178 lines, 5.5 KiBHistoryRawDownload
1use std::ffi::{CString, OsStr};
2use std::io::Write;
3use std::os::unix::prelude::OsStrExt;
4use std::path::{Path, PathBuf};
5use std::ptr::null_mut;
6use std::sync::atomic::AtomicBool;
7use std::time::Duration;
8
9use anyhow::{ensure, Context};
10use directories::UserDirs;
11use git_version::git_version;
12use niri_config::Config;
13use smithay::output::Output;
14use smithay::reexports::rustix::time::{clock_gettime, ClockId};
15use smithay::utils::{Logical, Point, Rectangle, Size, Transform};
16
17pub mod id;
18pub mod spawning;
19pub mod watcher;
20
21pub static IS_SYSTEMD_SERVICE: AtomicBool = AtomicBool::new(false);
22
23pub fn version() -> String {
24 format!(
25 "{} ({})",
26 env!("CARGO_PKG_VERSION"),
27 git_version!(fallback = "unknown commit"),
28 )
29}
30
31pub fn get_monotonic_time() -> Duration {
32 let ts = clock_gettime(ClockId::Monotonic);
33 Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32)
34}
35
36pub fn center(rect: Rectangle<i32, Logical>) -> Point<i32, Logical> {
37 rect.loc + rect.size.downscale(2).to_point()
38}
39
40pub fn center_f64(rect: Rectangle<f64, Logical>) -> Point<f64, Logical> {
41 rect.loc + rect.size.downscale(2.0).to_point()
42}
43
44pub fn output_size(output: &Output) -> Size<i32, Logical> {
45 let output_scale = output.current_scale().integer_scale();
46 let output_transform = output.current_transform();
47 let output_mode = output.current_mode().unwrap();
48
49 output_transform
50 .transform_size(output_mode.size)
51 .to_logical(output_scale)
52}
53
54pub fn logical_output(output: &Output) -> niri_ipc::LogicalOutput {
55 let loc = output.current_location();
56 let size = output_size(output);
57 let transform = match output.current_transform() {
58 Transform::Normal => niri_ipc::Transform::Normal,
59 Transform::_90 => niri_ipc::Transform::_90,
60 Transform::_180 => niri_ipc::Transform::_180,
61 Transform::_270 => niri_ipc::Transform::_270,
62 Transform::Flipped => niri_ipc::Transform::Flipped,
63 Transform::Flipped90 => niri_ipc::Transform::Flipped90,
64 Transform::Flipped180 => niri_ipc::Transform::Flipped180,
65 Transform::Flipped270 => niri_ipc::Transform::Flipped270,
66 };
67 niri_ipc::LogicalOutput {
68 x: loc.x,
69 y: loc.y,
70 width: size.w as u32,
71 height: size.h as u32,
72 scale: output.current_scale().fractional_scale(),
73 transform,
74 }
75}
76
77pub fn ipc_transform_to_smithay(transform: niri_ipc::Transform) -> Transform {
78 match transform {
79 niri_ipc::Transform::Normal => Transform::Normal,
80 niri_ipc::Transform::_90 => Transform::_90,
81 niri_ipc::Transform::_180 => Transform::_180,
82 niri_ipc::Transform::_270 => Transform::_270,
83 niri_ipc::Transform::Flipped => Transform::Flipped,
84 niri_ipc::Transform::Flipped90 => Transform::Flipped90,
85 niri_ipc::Transform::Flipped180 => Transform::Flipped180,
86 niri_ipc::Transform::Flipped270 => Transform::Flipped270,
87 }
88}
89
90pub fn expand_home(path: &Path) -> anyhow::Result<Option<PathBuf>> {
91 if let Ok(rest) = path.strip_prefix("~") {
92 let dirs = UserDirs::new().context("error retrieving home directory")?;
93 Ok(Some([dirs.home_dir(), rest].iter().collect()))
94 } else {
95 Ok(None)
96 }
97}
98
99pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> {
100 let Some(path) = &config.screenshot_path else {
101 return Ok(None);
102 };
103
104 let format = CString::new(path.clone()).context("path must not contain nul bytes")?;
105
106 let mut buf = [0u8; 2048];
107 let mut path;
108 unsafe {
109 let time = libc::time(null_mut());
110 ensure!(time != -1, "error in time()");
111
112 let tm = libc::localtime(&time);
113 ensure!(!tm.is_null(), "error in localtime()");
114
115 let rv = libc::strftime(buf.as_mut_ptr().cast(), buf.len(), format.as_ptr(), tm);
116 ensure!(rv != 0, "error formatting time");
117
118 path = PathBuf::from(OsStr::from_bytes(&buf[..rv]));
119 }
120
121 if let Some(expanded) = expand_home(&path).context("error expanding ~")? {
122 path = expanded;
123 }
124
125 Ok(Some(path))
126}
127
128pub fn write_png_rgba8(
129 w: impl Write,
130 width: u32,
131 height: u32,
132 pixels: &[u8],
133) -> Result<(), png::EncodingError> {
134 let mut encoder = png::Encoder::new(w, width, height);
135 encoder.set_color(png::ColorType::Rgba);
136 encoder.set_depth(png::BitDepth::Eight);
137
138 let mut writer = encoder.write_header()?;
139 writer.write_image_data(pixels)
140}
141
142#[cfg(feature = "dbus")]
143pub fn show_screenshot_notification(image_path: Option<PathBuf>) {
144 let mut notification = notify_rust::Notification::new();
145 notification
146 .summary("Screenshot captured")
147 .body("You can paste the image from the clipboard.")
148 .urgency(notify_rust::Urgency::Normal)
149 .hint(notify_rust::Hint::Transient(true));
150
151 // Try to add the screenshot as an image if possible.
152 if let Some(path) = image_path {
153 match path.canonicalize() {
154 Ok(path) => match url::Url::from_file_path(path) {
155 Ok(url) => {
156 notification.image_path(url.as_str());
157 }
158 Err(err) => {
159 warn!("error converting screenshot path to file url: {err:?}");
160 }
161 },
162 Err(err) => {
163 warn!("error canonicalizing screenshot path: {err:?}");
164 }
165 }
166 }
167
168 if let Err(err) = notification.show() {
169 warn!("error showing screenshot notification: {err:?}");
170 }
171}
172
173#[inline(never)]
174pub fn cause_panic() {
175 let a = Duration::from_secs(1);
176 let b = Duration::from_secs(2);
177 let _ = a - b;
178}