Added DBUS support
Added Plasma handler
This commit is contained in:
1 parent
368ac08449
commit
ae708d4f87
11 files changed
+666
-28
No files matched your search
+1
-1
@@ -6,7 +6,7 @@ use untildify;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Monitors {
|
||||
monitors: Vec<MonitorType>,
|
||||
pub monitors: Vec<MonitorType>,
|
||||
}
|
||||
|
||||
impl Monitors {
|
||||
|
||||
+8
-25
@@ -2,8 +2,6 @@ use core::fmt;
|
||||
use glob::glob;
|
||||
use rand::seq::SliceRandom;
|
||||
use std::fmt::Write as _;
|
||||
use std::io::Write as _;
|
||||
use std::process::exit;
|
||||
|
||||
use crate::config::*;
|
||||
use crate::enums::*;
|
||||
@@ -44,31 +42,16 @@ impl Wallpapers {
|
||||
),
|
||||
}
|
||||
}
|
||||
// Todo implement a random selector for a given key, like `wallpapers.random_selection(MonitorType::Ultrawide)`
|
||||
pub fn random_selection(&self, monitor_type: MonitorType) {
|
||||
|
||||
pub fn random_selection(&self, monitor_type: &MonitorType) -> &Wallpaper {
|
||||
match monitor_type {
|
||||
MonitorType::Ultrawide => {
|
||||
println!(
|
||||
"{:?}",
|
||||
self.ultrawide.choose(&mut rand::thread_rng()).unwrap()
|
||||
);
|
||||
}
|
||||
MonitorType::Horizontal => {
|
||||
println!(
|
||||
"{:?}",
|
||||
self.horizontal.choose(&mut rand::thread_rng()).unwrap()
|
||||
);
|
||||
}
|
||||
MonitorType::Vertical => {
|
||||
println!(
|
||||
"{:?}",
|
||||
self.vertical.choose(&mut rand::thread_rng()).unwrap()
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
MonitorType::Ultrawide => self.ultrawide.choose(&mut rand::thread_rng()).unwrap(),
|
||||
MonitorType::Horizontal => self.horizontal.choose(&mut rand::thread_rng()).unwrap(),
|
||||
MonitorType::Vertical => self.vertical.choose(&mut rand::thread_rng()).unwrap(),
|
||||
_ => self.horizontal.choose(&mut rand::thread_rng()).unwrap(),
|
||||
}
|
||||
}
|
||||
// Todo implement a function to load all files of particular filetypes into the Vec<Wallpaper>
|
||||
|
||||
pub fn load_images(&mut self, monitor_type: MonitorType) {
|
||||
let path = self
|
||||
.config
|
||||
@@ -99,7 +82,7 @@ impl Wallpapers {
|
||||
MonitorType::Ultrawide => self.ultrawide.push(Wallpaper { path: filepath }),
|
||||
MonitorType::Horizontal => self.horizontal.push(Wallpaper { path: filepath }),
|
||||
MonitorType::Vertical => self.vertical.push(Wallpaper { path: filepath }),
|
||||
_ => {}
|
||||
MonitorType::Error => self.horizontal.push(Wallpaper { path: filepath }),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
use dbus::blocking::Connection;
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn test() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// First open up a connection to the session bus
|
||||
let conn = Connection::new_session()?;
|
||||
|
||||
// Second, create a wrapper struct around the connection that makes it easy
|
||||
// to send method calls to a specific destination and path.
|
||||
let proxy = conn.with_proxy(
|
||||
"org.kde.plasmashell",
|
||||
"/PlasmaShell",
|
||||
Duration::from_millis(5000),
|
||||
);
|
||||
|
||||
use dbus_s::OrgKdePlasmaShell;
|
||||
// Now make the method call. The ListNames method call takes zero input parameters and
|
||||
// one output parameter which is an array of strings
|
||||
// Therefore the input is a zero tuple "()", and the output is a single tuple "(names, )"
|
||||
|
||||
let test = "
|
||||
var allDesktops = desktops();
|
||||
allDesktops[0].wallpaperPlugin = 'org.kde.image';
|
||||
allDesktops[0].currentConfigGroup = Array('Wallpaper', 'org.kde.image', 'General');
|
||||
allDesktops[0].writeConfig('Image', 'file:///home/ben/Pictures/wallpaper-ultra/wallhaven-pkj7gm.png');w
|
||||
allDesktops[1].wallpaperPlugin = 'org.kde.image';
|
||||
allDesktops[1].currentConfigGroup = Array('Wallpaper', 'org.kde.image', 'General');
|
||||
allDesktops[1].writeConfig('Image', 'file:///home/ben/Pictures/wallpaper-hor/papes/1490136858978.jpg');
|
||||
";
|
||||
|
||||
proxy.evaluate_script(test);
|
||||
|
||||
// let (_, error): (String, dbus::Error) = proxy
|
||||
// .method_call("org.kde.PlasmaShell", "evaluateScript", (test,))
|
||||
// .unwrap();
|
||||
|
||||
// Let's print all the names to stdout
|
||||
// for name in names {
|
||||
// println!("{}", name);
|
||||
// }
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
// This code was autogenerated with `dbus-codegen-rust -g -m None -d org.kde.plasmashell -p /PlasmaShell`, see https://github.com/diwic/dbus-rs
|
||||
use dbus;
|
||||
#[allow(unused_imports)]
|
||||
use dbus::arg;
|
||||
use dbus::blocking;
|
||||
|
||||
pub trait OrgKdePlasmaShell {
|
||||
fn toggle_dashboard(&self) -> Result<(), dbus::Error>;
|
||||
fn toggle_activity_manager(&self) -> Result<(), dbus::Error>;
|
||||
fn toggle_widget_explorer(&self) -> Result<(), dbus::Error>;
|
||||
fn set_dashboard_shown(&self, show: bool) -> Result<(), dbus::Error>;
|
||||
fn evaluate_script(&self, script: &str) -> Result<String, dbus::Error>;
|
||||
fn color(&self) -> Result<u32, dbus::Error>;
|
||||
fn dump_current_layout_js(&self) -> Result<Vec<u8>, dbus::Error>;
|
||||
fn load_look_and_feel_default_layout(&self, layout: &str) -> Result<(), dbus::Error>;
|
||||
fn activate_launcher_menu(&self) -> Result<(), dbus::Error>;
|
||||
fn refresh_current_shell(&self) -> Result<(), dbus::Error>;
|
||||
fn wallpaper(&self, screen_num: u32) -> Result<arg::PropMap, dbus::Error>;
|
||||
fn set_wallpaper(
|
||||
&self,
|
||||
wallpaper_plugin: &str,
|
||||
parameters: &str,
|
||||
screen_num: u32,
|
||||
) -> Result<(), dbus::Error>;
|
||||
fn edit_mode(&self) -> Result<bool, dbus::Error>;
|
||||
fn setedit_mode(&self, value: bool) -> Result<(), dbus::Error>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OrgKdePlasmaShellWallpaperChanged {
|
||||
pub screen_num: u32,
|
||||
}
|
||||
|
||||
impl arg::AppendAll for OrgKdePlasmaShellWallpaperChanged {
|
||||
fn append(&self, i: &mut arg::IterAppend) {
|
||||
arg::RefArg::append(&self.screen_num, i);
|
||||
}
|
||||
}
|
||||
|
||||
impl arg::ReadAll for OrgKdePlasmaShellWallpaperChanged {
|
||||
fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> {
|
||||
Ok(OrgKdePlasmaShellWallpaperChanged {
|
||||
screen_num: i.read()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl dbus::message::SignalArgs for OrgKdePlasmaShellWallpaperChanged {
|
||||
const NAME: &'static str = "wallpaperChanged";
|
||||
const INTERFACE: &'static str = "org.kde.PlasmaShell";
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OrgKdePlasmaShellColorChanged {
|
||||
pub changed_color: String,
|
||||
}
|
||||
|
||||
impl arg::AppendAll for OrgKdePlasmaShellColorChanged {
|
||||
fn append(&self, i: &mut arg::IterAppend) {
|
||||
arg::RefArg::append(&self.changed_color, i);
|
||||
}
|
||||
}
|
||||
|
||||
impl arg::ReadAll for OrgKdePlasmaShellColorChanged {
|
||||
fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> {
|
||||
Ok(OrgKdePlasmaShellColorChanged {
|
||||
changed_color: i.read()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl dbus::message::SignalArgs for OrgKdePlasmaShellColorChanged {
|
||||
const NAME: &'static str = "colorChanged";
|
||||
const INTERFACE: &'static str = "org.kde.PlasmaShell";
|
||||
}
|
||||
|
||||
impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref<Target = T>> OrgKdePlasmaShell
|
||||
for blocking::Proxy<'a, C>
|
||||
{
|
||||
fn toggle_dashboard(&self) -> Result<(), dbus::Error> {
|
||||
self.method_call("org.kde.PlasmaShell", "toggleDashboard", ())
|
||||
}
|
||||
|
||||
fn toggle_activity_manager(&self) -> Result<(), dbus::Error> {
|
||||
self.method_call("org.kde.PlasmaShell", "toggleActivityManager", ())
|
||||
}
|
||||
|
||||
fn toggle_widget_explorer(&self) -> Result<(), dbus::Error> {
|
||||
self.method_call("org.kde.PlasmaShell", "toggleWidgetExplorer", ())
|
||||
}
|
||||
|
||||
fn set_dashboard_shown(&self, show: bool) -> Result<(), dbus::Error> {
|
||||
self.method_call("org.kde.PlasmaShell", "setDashboardShown", (show,))
|
||||
}
|
||||
|
||||
fn evaluate_script(&self, script: &str) -> Result<String, dbus::Error> {
|
||||
self.method_call("org.kde.PlasmaShell", "evaluateScript", (script,))
|
||||
.and_then(|r: (String,)| Ok(r.0))
|
||||
}
|
||||
|
||||
fn color(&self) -> Result<u32, dbus::Error> {
|
||||
self.method_call("org.kde.PlasmaShell", "color", ())
|
||||
.and_then(|r: (u32,)| Ok(r.0))
|
||||
}
|
||||
|
||||
fn dump_current_layout_js(&self) -> Result<Vec<u8>, dbus::Error> {
|
||||
self.method_call("org.kde.PlasmaShell", "dumpCurrentLayoutJS", ())
|
||||
.and_then(|r: (Vec<u8>,)| Ok(r.0))
|
||||
}
|
||||
|
||||
fn load_look_and_feel_default_layout(&self, layout: &str) -> Result<(), dbus::Error> {
|
||||
self.method_call(
|
||||
"org.kde.PlasmaShell",
|
||||
"loadLookAndFeelDefaultLayout",
|
||||
(layout,),
|
||||
)
|
||||
}
|
||||
|
||||
fn activate_launcher_menu(&self) -> Result<(), dbus::Error> {
|
||||
self.method_call("org.kde.PlasmaShell", "activateLauncherMenu", ())
|
||||
}
|
||||
|
||||
fn refresh_current_shell(&self) -> Result<(), dbus::Error> {
|
||||
self.method_call("org.kde.PlasmaShell", "refreshCurrentShell", ())
|
||||
}
|
||||
|
||||
fn wallpaper(&self, screen_num: u32) -> Result<arg::PropMap, dbus::Error> {
|
||||
self.method_call("org.kde.PlasmaShell", "wallpaper", (screen_num,))
|
||||
.and_then(|r: (arg::PropMap,)| Ok(r.0))
|
||||
}
|
||||
|
||||
fn set_wallpaper(
|
||||
&self,
|
||||
wallpaper_plugin: &str,
|
||||
parameters: &str,
|
||||
screen_num: u32,
|
||||
) -> Result<(), dbus::Error> {
|
||||
self.method_call(
|
||||
"org.kde.PlasmaShell",
|
||||
"setWallpaper",
|
||||
(wallpaper_plugin, parameters, screen_num),
|
||||
)
|
||||
}
|
||||
|
||||
fn edit_mode(&self) -> Result<bool, dbus::Error> {
|
||||
<Self as blocking::stdintf::org_freedesktop_dbus::Properties>::get(
|
||||
self,
|
||||
"org.kde.PlasmaShell",
|
||||
"editMode",
|
||||
)
|
||||
}
|
||||
|
||||
fn setedit_mode(&self, value: bool) -> Result<(), dbus::Error> {
|
||||
<Self as blocking::stdintf::org_freedesktop_dbus::Properties>::set(
|
||||
self,
|
||||
"org.kde.PlasmaShell",
|
||||
"editMode",
|
||||
value,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait OrgFreedesktopDBusProperties {
|
||||
fn get<R0: for<'b> arg::Get<'b> + 'static>(
|
||||
&self,
|
||||
interface_name: &str,
|
||||
property_name: &str,
|
||||
) -> Result<R0, dbus::Error>;
|
||||
fn set<I2: arg::Arg + arg::Append>(
|
||||
&self,
|
||||
interface_name: &str,
|
||||
property_name: &str,
|
||||
value: I2,
|
||||
) -> Result<(), dbus::Error>;
|
||||
fn get_all(&self, interface_name: &str) -> Result<arg::PropMap, dbus::Error>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OrgFreedesktopDBusPropertiesPropertiesChanged {
|
||||
pub interface_name: String,
|
||||
pub changed_properties: arg::PropMap,
|
||||
pub invalidated_properties: Vec<String>,
|
||||
}
|
||||
|
||||
impl arg::AppendAll for OrgFreedesktopDBusPropertiesPropertiesChanged {
|
||||
fn append(&self, i: &mut arg::IterAppend) {
|
||||
arg::RefArg::append(&self.interface_name, i);
|
||||
arg::RefArg::append(&self.changed_properties, i);
|
||||
arg::RefArg::append(&self.invalidated_properties, i);
|
||||
}
|
||||
}
|
||||
|
||||
impl arg::ReadAll for OrgFreedesktopDBusPropertiesPropertiesChanged {
|
||||
fn read(i: &mut arg::Iter) -> Result<Self, arg::TypeMismatchError> {
|
||||
Ok(OrgFreedesktopDBusPropertiesPropertiesChanged {
|
||||
interface_name: i.read()?,
|
||||
changed_properties: i.read()?,
|
||||
invalidated_properties: i.read()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl dbus::message::SignalArgs for OrgFreedesktopDBusPropertiesPropertiesChanged {
|
||||
const NAME: &'static str = "PropertiesChanged";
|
||||
const INTERFACE: &'static str = "org.freedesktop.DBus.Properties";
|
||||
}
|
||||
|
||||
impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref<Target = T>> OrgFreedesktopDBusProperties
|
||||
for blocking::Proxy<'a, C>
|
||||
{
|
||||
fn get<R0: for<'b> arg::Get<'b> + 'static>(
|
||||
&self,
|
||||
interface_name: &str,
|
||||
property_name: &str,
|
||||
) -> Result<R0, dbus::Error> {
|
||||
self.method_call(
|
||||
"org.freedesktop.DBus.Properties",
|
||||
"Get",
|
||||
(interface_name, property_name),
|
||||
)
|
||||
.and_then(|r: (arg::Variant<R0>,)| Ok((r.0).0))
|
||||
}
|
||||
|
||||
fn set<I2: arg::Arg + arg::Append>(
|
||||
&self,
|
||||
interface_name: &str,
|
||||
property_name: &str,
|
||||
value: I2,
|
||||
) -> Result<(), dbus::Error> {
|
||||
self.method_call(
|
||||
"org.freedesktop.DBus.Properties",
|
||||
"Set",
|
||||
(interface_name, property_name, arg::Variant(value)),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_all(&self, interface_name: &str) -> Result<arg::PropMap, dbus::Error> {
|
||||
self.method_call(
|
||||
"org.freedesktop.DBus.Properties",
|
||||
"GetAll",
|
||||
(interface_name,),
|
||||
)
|
||||
.and_then(|r: (arg::PropMap,)| Ok(r.0))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait OrgFreedesktopDBusIntrospectable {
|
||||
fn introspect(&self) -> Result<String, dbus::Error>;
|
||||
}
|
||||
|
||||
impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref<Target = T>>
|
||||
OrgFreedesktopDBusIntrospectable for blocking::Proxy<'a, C>
|
||||
{
|
||||
fn introspect(&self) -> Result<String, dbus::Error> {
|
||||
self.method_call("org.freedesktop.DBus.Introspectable", "Introspect", ())
|
||||
.and_then(|r: (String,)| Ok(r.0))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait OrgFreedesktopDBusPeer {
|
||||
fn ping(&self) -> Result<(), dbus::Error>;
|
||||
fn get_machine_id(&self) -> Result<String, dbus::Error>;
|
||||
}
|
||||
|
||||
impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref<Target = T>> OrgFreedesktopDBusPeer
|
||||
for blocking::Proxy<'a, C>
|
||||
{
|
||||
fn ping(&self) -> Result<(), dbus::Error> {
|
||||
self.method_call("org.freedesktop.DBus.Peer", "Ping", ())
|
||||
}
|
||||
|
||||
fn get_machine_id(&self) -> Result<String, dbus::Error> {
|
||||
self.method_call("org.freedesktop.DBus.Peer", "GetMachineId", ())
|
||||
.and_then(|r: (String,)| Ok(r.0))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
pub mod dbus_s;
|
||||
use dbus_s::*;
|
||||
pub mod plasma;
|
||||
use plasma::*;
|
||||
@@ -0,0 +1,57 @@
|
||||
use crate::config::Monitors;
|
||||
use crate::handlers::dbus_s;
|
||||
use crate::Wallpapers;
|
||||
|
||||
use dbus::blocking::Connection;
|
||||
use std::fmt::Write as _;
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn change_wallpapers(wallpapers: &Wallpapers) {
|
||||
// First open up a connection to the session bus
|
||||
let conn = Connection::new_session().unwrap();
|
||||
|
||||
// Second, create a wrapper struct around the connection that makes it easy
|
||||
// to send method calls to a specific destination and path.
|
||||
let proxy = conn.with_proxy(
|
||||
"org.kde.plasmashell",
|
||||
"/PlasmaShell",
|
||||
Duration::from_millis(5000),
|
||||
);
|
||||
|
||||
use dbus_s::OrgKdePlasmaShell;
|
||||
|
||||
let javascript = generate_js(wallpapers);
|
||||
let resp = proxy.evaluate_script(javascript.as_ref());
|
||||
match resp {
|
||||
Ok(str) => {}
|
||||
Err(e) => println!("{}", e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_js(wallpapers: &Wallpapers) -> String {
|
||||
let mut javascript = String::new();
|
||||
javascript.push_str("var allDesktops = desktops();\n");
|
||||
let mut count = 0;
|
||||
for monitor in wallpapers.config.monitors.monitors.iter() {
|
||||
let mut file_buff = String::new();
|
||||
let _ = write!(
|
||||
&mut file_buff,
|
||||
"file://{}",
|
||||
wallpapers.random_selection(monitor)
|
||||
);
|
||||
let mut boilerplate = String::new();
|
||||
// let boilerplate_formatter = ;
|
||||
let _ = write!(
|
||||
&mut boilerplate,
|
||||
"
|
||||
allDesktops[{0}].wallpaperPlugin = 'org.kde.image';
|
||||
allDesktops[{0}].currentConfigGroup = Array('Wallpaper', 'org.kde.image', 'General');
|
||||
allDesktops[{0}].writeConfig('Image', '{1}');
|
||||
",
|
||||
count, file_buff
|
||||
);
|
||||
javascript.push_str(&boilerplate);
|
||||
count += 1;
|
||||
}
|
||||
javascript
|
||||
}
|
||||
+6
-2
@@ -3,11 +3,15 @@ mod enums;
|
||||
use enums::*;
|
||||
mod files;
|
||||
use files::*;
|
||||
mod handlers;
|
||||
|
||||
fn main() {
|
||||
let mut wallpapers = Wallpapers::new();
|
||||
|
||||
wallpapers.load_all();
|
||||
wallpapers.random_selection(MonitorType::Ultrawide);
|
||||
println!("{}", "");
|
||||
wallpapers.random_selection(&MonitorType::Ultrawide);
|
||||
wallpapers.random_selection(&MonitorType::Horizontal);
|
||||
wallpapers.random_selection(&MonitorType::Vertical);
|
||||
let resp = handlers::plasma::change_wallpapers(&wallpapers);
|
||||
// println!("{}", resp);
|
||||
}
|
||||
Reference in new issue
Block a user