1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (C) 2022 Purism SPC
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
use crate::main;
use crate::state;

use std::thread;
use zbus::{Connection, ObjectServer, dbus_interface, fdo};

use super::Void;

use std::convert::TryInto;


/// Accepts commands controlling the debug mode
struct Manager {
    sender: main::EventLoop,
    enabled: bool,
}

#[dbus_interface(name = "sm.puri.SqueekDebug")]
impl Manager {
    #[dbus_interface(property, name = "Enabled")]
    fn get_enabled(&self) -> bool {
        self.enabled
    }
    #[dbus_interface(property, name = "Enabled")]
    fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
        self.sender
            .send(state::Event::Debug(
                if enabled { Event::Enable }
                else { Event::Disable }
            ))
            .unwrap();
    }
}

fn start(mgr: Manager) -> Result<Void, Box<dyn std::error::Error>> {
    let connection = Connection::new_session()?;
    fdo::DBusProxy::new(&connection)?.request_name(
        "sm.puri.SqueekDebug",
        fdo::RequestNameFlags::ReplaceExisting.into(),
    )?;

    let mut object_server = ObjectServer::new(&connection);
    object_server.at(&"/sm/puri/SqueekDebug".try_into()?, mgr)?;

    loop {
        if let Err(err) = object_server.try_handle_next() {
            eprintln!("{}", err);
        }
    }
}

pub fn init(sender: main::EventLoop) {
    let mgr = Manager {
        sender,
        enabled: false,
    };
    thread::spawn(move || {
        start(mgr).unwrap();
    });
}

#[derive(Debug, Clone, Copy)]
pub enum Event {
    Enable,
    Disable,
}