-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: launch desktop entries via dbus
- Loading branch information
Showing
6 changed files
with
171 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use freedesktop_desktop_entry::DesktopEntry; | ||
use std::path::PathBuf; | ||
use std::{env, fs}; | ||
|
||
fn main() { | ||
let args: Vec<String> = env::args().collect(); | ||
let path = &args.get(1).expect("Not enough arguments"); | ||
let path = PathBuf::from(path); | ||
let input = fs::read_to_string(&path).expect("Failed to read file"); | ||
let de = DesktopEntry::decode(path.as_path(), &input).expect("Error decoding desktop entry"); | ||
de.launch(&[], false) | ||
.expect("Failed to run desktop entry"); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use crate::exec::error::ExecError; | ||
use crate::exec::graphics::Gpus; | ||
use crate::DesktopEntry; | ||
use std::collections::HashMap; | ||
use zbus::blocking::Connection; | ||
use zbus::dbus_proxy; | ||
use zbus::names::OwnedBusName; | ||
use zbus::zvariant::{OwnedValue, Str}; | ||
|
||
#[dbus_proxy(interface = "org.freedesktop.Application")] | ||
trait Application { | ||
fn activate(&self, platform_data: HashMap<String, OwnedValue>) -> zbus::Result<()>; | ||
fn activate_action( | ||
&self, | ||
action_name: &str, | ||
parameters: &[OwnedValue], | ||
platform_data: HashMap<String, OwnedValue>, | ||
) -> zbus::Result<()>; | ||
fn open(&self, uris: &[&str], platform_data: HashMap<String, OwnedValue>) -> zbus::Result<()>; | ||
} | ||
|
||
impl DesktopEntry<'_> { | ||
pub(crate) fn dbus_launch( | ||
&self, | ||
conn: &Connection, | ||
uris: &[&str], | ||
prefer_non_default_gpu: bool, | ||
) -> Result<(), ExecError> { | ||
let dbus_path = self.appid.replace('.', "/"); | ||
let dbus_path = format!("/{dbus_path}"); | ||
let app_proxy = ApplicationProxyBlocking::builder(conn) | ||
.destination(self.appid)? | ||
.path(dbus_path.as_str())? | ||
.build()?; | ||
|
||
let mut platform_data = HashMap::new(); | ||
if prefer_non_default_gpu { | ||
let gpus = Gpus::load(); | ||
if let Some(gpu) = gpus.non_default() { | ||
for (opt, value) in gpu.launch_options() { | ||
platform_data.insert(opt, OwnedValue::from(Str::from(value.as_str()))); | ||
} | ||
} | ||
} | ||
|
||
if !uris.is_empty() { | ||
app_proxy.open(uris, platform_data)?; | ||
} else { | ||
app_proxy.activate(platform_data)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) fn is_bus_actionable(&self, conn: &Connection) -> bool { | ||
let dbus_proxy = zbus::blocking::fdo::DBusProxy::new(conn); | ||
|
||
if dbus_proxy.is_err() { | ||
return false; | ||
} | ||
|
||
let dbus_proxy = dbus_proxy.unwrap(); | ||
let dbus_names = dbus_proxy.list_activatable_names().unwrap(); | ||
|
||
dbus_names | ||
.into_iter() | ||
.map(OwnedBusName::into_inner) | ||
.any(|name| name.as_str() == self.appid) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters