aboutsummaryrefslogtreecommitdiff
path: root/quickshell/Procs.qml
blob: f24bec662cd52623ac615939fe855b4f056c1b35 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import Quickshell
import Quickshell.Io
import QtQuick

Scope {
  Timer {
    interval: 2000
    running: true
    repeat: true
    onTriggered: {
      cpuProc.running = true
      memProc.running = true
      brightnessProc.running = true
      volumeProc.running = true
      batteryProc.running = true
      networkProc.running = true
      bluetoothProc.running = true
      temperatureProc.running = true
    }
  }

  Process {
    id: cpuProc
    command: ["sh", "-c", "head -1 /proc/stat"]

    stdout: SplitParser {
      onRead: data => {
        var p = data.trim().split(/\s+/)
        var idle = parseInt(p[4]) + parseInt(p[5])
        var total = p.slice(1, 8).reduce((a, b) => a + parseInt(b), 0)
        if (lastCpuTotal > 0) {
          cpuUsage = Math.round(100 * (1 - (idle - lastCpuIdle) / (total - lastCpuTotal)))
        }
        lastCpuTotal = total
        lastCpuIdle = idle
      }
    }
    Component.onCompleted: running = true
  }

  Process {
    id: memProc
    command: ["sh", "-c", "free | grep Mem"]
    stdout: SplitParser {
      onRead: data => {
        var parts = data.trim().split(/\s+/)
        var total = parseInt(parts[1]) || 1
        var used = parseInt(parts[2]) || 0
        memUsage = Math.round(100 * used / total)
      }
    }
    Component.onCompleted: running = true
  }

  Process {
    id: temperatureProc
    command: ["sh", "-c", "~/.config/quickshell/scripts/cpu_temp.sh"]
    stdout: SplitParser {
      onRead: data => {
        cpu_temperature = parseInt(data.trim()) || 0
      }
    }
    Component.onCompleted: running = true
  }

  Process {
    id: brightnessProc
    command: ["sh", "-c", "~/.config/quickshell/scripts/brightness.sh"]
    stdout: SplitParser {
      onRead: data => {
        var parts = data.trim().split(/\s+/)
        brightness = parseInt(parts[0]) || 100
        temperature = parseInt(parts[1]) || 6000
      }
    }
    Component.onCompleted: running = true
  }

  Process {
    id: volumeProc
    command: ["sh", "-c", "wpctl get-volume @DEFAULT_AUDIO_SINK@"]
    stdout: SplitParser {
      onRead: data => {
        var parts = data.trim().split(/\s+/)
        if (parts.length == 3) {
          volume_muted = true;
        } else {
          volume_muted = false;
        }
        volume = (parseFloat(parts[1].trim()) || 0) * 100
      }
    }
    Component.onCompleted: running = true
  }

  Process {
    id: batteryProc
    command: ["sh", "-c", "~/.config/quickshell/scripts/battery.sh"]
    stdout: SplitParser {
      onRead: data => {
        var parts = data.trim().split(/\s+/)
        battery = parseInt(parts[0]) || 0
        batteryCharging = parseInt(parts[1]) || false
      }
    }
    Component.onCompleted: running = true
  }

  Process {
    id: networkProc
    command: ["sh", "-c", "~/.config/quickshell/scripts/network.sh"]
    stdout: SplitParser {
      onRead: data => {
        var parts = data.trim().split(/\s+/)
        wifiName = parts[0];
      }
    }
    Component.onCompleted: running = true
  }

  Process {
    id: bluetoothProc
    command: ["sh", "-c", "~/.config/quickshell/scripts/bluetooth.sh"]
    stdout: SplitParser {
      onRead: data => {
        data = data.trim()
        if (data == "Offline") {
          bluetoothName = "Offline"
          return
        }
        var parts = data.split(" ")
        bluetoothName = parts.slice(2).join(" ")
      }
    }
    Component.onCompleted: running = true
  }
}