niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·9d8f4f8·Ivan Molodetskikh·1 month ago·2997 commits
| 1 | use std::collections::HashMap; |
| 2 | use std::path::PathBuf; |
| 3 | |
| 4 | use niri_ipc::PickedColor; |
| 5 | use zbus::zvariant::OwnedValue; |
| 6 | use zbus::{fdo, interface, zvariant}; |
| 7 | |
| 8 | use super::{request_name, Start}; |
| 9 | |
| 10 | pub struct Screenshot { |
| 11 | to_niri: calloop::channel::Sender<ScreenshotToNiri>, |
| 12 | from_niri: async_channel::Receiver<NiriToScreenshot>, |
| 13 | } |
| 14 | |
| 15 | pub enum ScreenshotToNiri { |
| 16 | TakeScreenshot { include_cursor: bool }, |
| 17 | PickColor(async_channel::Sender<Option<PickedColor>>), |
| 18 | } |
| 19 | |
| 20 | pub enum NiriToScreenshot { |
| 21 | ScreenshotResult(Option<PathBuf>), |
| 22 | } |
| 23 | |
| 24 | #[interface(name = "org.gnome.Shell.Screenshot")] |
| 25 | impl Screenshot { |
| 26 | async fn screenshot( |
| 27 | &self, |
| 28 | include_cursor: bool, |
| 29 | _flash: bool, |
| 30 | _filename: PathBuf, |
| 31 | ) -> fdo::Result<(bool, PathBuf)> { |
| 32 | if let Err(err) = self |
| 33 | .to_niri |
| 34 | .send(ScreenshotToNiri::TakeScreenshot { include_cursor }) |
| 35 | { |
| 36 | warn!("error sending message to niri: {err:?}"); |
| 37 | return Err(fdo::Error::Failed("internal error".to_owned())); |
| 38 | } |
| 39 | |
| 40 | let filename = match self.from_niri.recv().await { |
| 41 | Ok(NiriToScreenshot::ScreenshotResult(Some(filename))) => filename, |
| 42 | Ok(NiriToScreenshot::ScreenshotResult(None)) => { |
| 43 | return Err(fdo::Error::Failed("internal error".to_owned())); |
| 44 | } |
| 45 | Err(err) => { |
| 46 | warn!("error receiving message from niri: {err:?}"); |
| 47 | return Err(fdo::Error::Failed("internal error".to_owned())); |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | Ok((true, filename)) |
| 52 | } |
| 53 | |
| 54 | async fn pick_color(&self) -> fdo::Result<HashMap<String, OwnedValue>> { |
| 55 | let (tx, rx) = async_channel::bounded(1); |
| 56 | if let Err(err) = self.to_niri.send(ScreenshotToNiri::PickColor(tx)) { |
| 57 | warn!("error sending pick color message to niri: {err:?}"); |
| 58 | return Err(fdo::Error::Failed("internal error".to_owned())); |
| 59 | } |
| 60 | |
| 61 | let color = match rx.recv().await { |
| 62 | Ok(Some(color)) => color, |
| 63 | Ok(None) => { |
| 64 | return Err(fdo::Error::Failed("no color picked".to_owned())); |
| 65 | } |
| 66 | Err(err) => { |
| 67 | warn!("error receiving message from niri: {err:?}"); |
| 68 | return Err(fdo::Error::Failed("internal error".to_owned())); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | let mut result = HashMap::new(); |
| 73 | let [r, g, b] = color.rgb; |
| 74 | result.insert( |
| 75 | "color".to_string(), |
| 76 | zvariant::OwnedValue::try_from(zvariant::Value::from((r, g, b))).unwrap(), |
| 77 | ); |
| 78 | |
| 79 | Ok(result) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl Screenshot { |
| 84 | pub fn new( |
| 85 | to_niri: calloop::channel::Sender<ScreenshotToNiri>, |
| 86 | from_niri: async_channel::Receiver<NiriToScreenshot>, |
| 87 | ) -> Self { |
| 88 | Self { to_niri, from_niri } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl Start for Screenshot { |
| 93 | fn start(self, monitor: bool) -> anyhow::Result<zbus::blocking::Connection> { |
| 94 | let conn = zbus::blocking::Connection::session()?; |
| 95 | conn.object_server() |
| 96 | .at("/org/gnome/Shell/Screenshot", self)?; |
| 97 | request_name(&conn, "org.gnome.Shell.Screenshot", monitor)?; |
| 98 | |
| 99 | Ok(conn) |
| 100 | } |
| 101 | } |