summaryrefslogtreecommitdiff
path: root/services/Bluetooth.qml
blob: f77001e362595c120589f69aef2eab5f1b451b91 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
pragma Singleton

import Quickshell
import Quickshell.Io
import QtQuick

Singleton {
    id: root

    property bool powered
    property bool discovering
    readonly property list<Device> devices: []

    Process {
        running: true
        command: ["bluetoothctl"]
        stdout: SplitParser {
            onRead: {
                getInfo.running = true;
                getDevices.running = true;
            }
        }
    }

    Process {
        id: getInfo
        running: true
        command: ["sh", "-c", "bluetoothctl show | paste -s"]
        stdout: SplitParser {
            onRead: data => {
                root.powered = data.includes("Powered: yes");
                root.discovering = data.includes("Discovering: yes");
            }
        }
    }

    Process {
        id: getDevices
        running: true
        command: ["fish", "-c", `for a in (bluetoothctl devices | cut -d ' ' -f 2); bluetoothctl info $a | jq -R 'reduce (inputs / ":") as [$key, $value] ({}; .[$key | ltrimstr("\t")] = ($value | ltrimstr(" ")))' | jq -c --arg addr $a '.Address = $addr'; end | jq -sc`]
        stdout: SplitParser {
            onRead: data => {
                const devices = JSON.parse(data).filter(d => d.Name);
                const rDevices = root.devices;

                const len = rDevices.length;
                for (let i = 0; i < len; i++) {
                    const device = rDevices[i];
                    if (!devices.find(d => d.Address === device?.address))
                        rDevices.splice(i, 1);
                }

                for (const device of devices) {
                    const match = rDevices.find(d => d.address === device.Address);
                    if (match) {
                        match.lastIpcObject = device;
                    } else {
                        rDevices.push(deviceComp.createObject(root, {
                            lastIpcObject: device
                        }));
                    }
                }
            }
        }
    }

    component Device: QtObject {
        required property var lastIpcObject
        readonly property string name: lastIpcObject.Name
        readonly property string alias: lastIpcObject.Alias
        readonly property string address: lastIpcObject.Address
        readonly property string icon: lastIpcObject.Icon
        readonly property bool connected: lastIpcObject.Connected === "yes"
        readonly property bool paired: lastIpcObject.Paired === "yes"
        readonly property bool trusted: lastIpcObject.Trusted === "yes"
    }

    Component {
        id: deviceComp

        Device {}
    }
}