ReSet_Lib/
utils.rs

1use std::time::Duration;
2
3use dbus::{
4    arg::{Append, AppendAll, Arg, Get, ReadAll},
5    blocking::Connection,
6    Path,
7};
8
9pub fn call_system_dbus_method<I: AppendAll + 'static, O: ReadAll + 'static>(
10    name: &str,
11    object: Path<'static>,
12    function: &str,
13    proxy_name: &str,
14    params: I,
15    time: u64,
16) -> Result<O, dbus::Error> {
17    let conn = Connection::new_system().unwrap();
18    let proxy = conn.with_proxy(name, object, Duration::from_millis(time));
19    let result: Result<O, dbus::Error> = proxy.method_call(proxy_name, function, params);
20    result
21}
22
23pub fn get_system_dbus_property<I: AppendAll, O: for<'a> Get<'a> + 'static>(
24    name: &str,
25    object: Path<'static>,
26    interface: &str,
27    property: &str,
28) -> Result<O, dbus::Error> {
29    let conn = Connection::new_system().unwrap();
30    let proxy = conn.with_proxy(name, object, Duration::from_millis(1000));
31    use dbus::blocking::stdintf::org_freedesktop_dbus::Properties;
32
33    let result: Result<O, dbus::Error> = proxy.get(interface, property);
34    result
35}
36
37pub fn set_system_dbus_property<I: Arg + Append>(
38    name: &str,
39    object: Path<'static>,
40    interface: &str,
41    property: &str,
42    value: I,
43) -> Result<(), dbus::Error> {
44    let conn = Connection::new_system().unwrap();
45    let proxy = conn.with_proxy(name, object, Duration::from_millis(1000));
46    use dbus::blocking::stdintf::org_freedesktop_dbus::Properties;
47
48    let result: Result<(), dbus::Error> = proxy.set(interface, property, value);
49    result
50}
51
52pub fn call_session_dbus_method<
53    I: AppendAll + Sync + Send + 'static,
54    O: ReadAll + Sync + Send + 'static,
55>(
56    name: &str,
57    object: Path<'static>,
58    function: &str,
59    proxy_name: &str,
60    params: I,
61) -> Result<O, dbus::Error> {
62    let conn = Connection::new_session().unwrap();
63    let proxy = conn.with_proxy(name, object, Duration::from_millis(1000));
64    let result: Result<O, dbus::Error> = proxy.method_call(proxy_name, function, params);
65    result
66}
67
68pub fn call_reset_dbus_method<
69    I: AppendAll + Sync + Send + 'static,
70    O: ReadAll + Sync + Send + 'static,
71>(
72    interface: &str,
73    function: &str,
74    params: I,
75) -> Result<O, dbus::Error> {
76    let conn = Connection::new_session().unwrap();
77    let proxy = conn.with_proxy(
78        "org.Xetibo.ReSetDaemon",
79        "/org/Xetibo/ResetDaemon",
80        Duration::from_millis(1000),
81    );
82    let result: Result<O, dbus::Error> = proxy.method_call(
83        "org.Xetibo.ReSet".to_string() + "." + interface,
84        function,
85        params,
86    );
87    result
88}