-
Notifications
You must be signed in to change notification settings - Fork 2
/
modules.cpp
341 lines (328 loc) · 10.1 KB
/
modules.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright (c) 2020 Anthony J. Greenberg
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/// C++ modules for the status bar (implementation)
/** \file
* \author Anthony J. Greenberg
* \copyright Copyright (c) 2020 Anthony J. Greenberg
* \version 0.9
*
* Implementation of classes that provide output useful for display in the status bar.
*
*/
#include <cstddef>
#include <cstdio>
#include <sys/statvfs.h>
#include <ios>
#include <string>
#include <sstream>
#include <fstream>
#include <ctime>
#include <iomanip>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include "modules.hpp"
using std::string;
using std::stof;
using std::stoi;
using std::to_string;
using std::stringstream;
using std::fstream;
using std::ios;
using std::setprecision;
using std::fixed;
using std::time;
using std::put_time;
using std::localtime;
using std::this_thread::sleep_for;
using std::mutex;
using std::unique_lock;
using std::chrono::seconds;
using namespace DWMBspace;
void Module::operator()() const {
if (refreshInterval_) { // if not zero, do a time-lapse loop
mutex mtx;
while (true) {
runModule_();
unique_lock<mutex> lk(mtx);
signalCondition_->wait_for(lk, seconds(refreshInterval_));
}
} else { // wait for a real-time signal
runModule_();
mutex mtx;
while (true) {
unique_lock<mutex> lk(mtx);
signalCondition_->wait(lk);
runModule_();
lk.unlock();
}
}
}
void ModuleDate::runModule_() const {
time_t t = time(nullptr);
stringstream outTime;
outTime << put_time( localtime(&t), dateFormat_.c_str() );
mutex mtx;
unique_lock<mutex> lk(mtx);
*outString_ = outTime.str();
outputCondition_->notify_one();
lk.unlock();
}
void ModuleBattery::runModule_() const {
string batStatus;
fstream statusStream;
statusStream.open("/sys/class/power_supply/BAT0/status", ios::in);
if ( statusStream.is_open() ) { // fail silently
getline(statusStream, batStatus);
}
statusStream.close();
string batCapacityStr;
fstream capacityStream;
capacityStream.open("/sys/class/power_supply/BAT0/capacity", ios::in);
if ( capacityStream.is_open() ) { // fail silently
getline(capacityStream, batCapacityStr);
}
capacityStream.close();
float batCapacity = 0.0;
if ( batCapacityStr.size() ) {
batCapacity = stof(batCapacityStr);
}
mutex mtx;
unique_lock<mutex> lk(mtx);
if (batStatus == "Charging") {
if (batCapacity < 5.0) {
*outString_ = batCapacityStr + "% \uf58d";
} else if (batCapacity < 20.0) {
*outString_ = batCapacityStr + "% \uf585";
} else if (batCapacity < 30.0) {
*outString_ = batCapacityStr + "% \uf586";
} else if (batCapacity < 40.0) {
*outString_ = batCapacityStr + "% \uf587";
} else if (batCapacity < 60.0) {
*outString_ = batCapacityStr + "% \uf588";
} else if (batCapacity < 80.0) {
*outString_ = batCapacityStr + "% \uf589";
} else if (batCapacity < 90.0) {
*outString_ = batCapacityStr + "% \uf58a";
} else if (batCapacity < 100.0){
*outString_ = batCapacityStr + "% \uf578";
}
} else {
if (batCapacity < 5.0) {
*outString_ = batCapacityStr + "% \uf58d";
} else if (batCapacity < 10.0) {
*outString_ = batCapacityStr + "% \uf579";
} else if (batCapacity < 20.0) {
*outString_ = batCapacityStr + "% \uf57a";
} else if (batCapacity < 30.0) {
*outString_ = batCapacityStr + "% \uf57b";
} else if (batCapacity < 40.0) {
*outString_ = batCapacityStr + "% \uf57c";
} else if (batCapacity < 50.0) {
*outString_ = batCapacityStr + "% \uf57d";
} else if (batCapacity < 60.0) {
*outString_ = batCapacityStr + "% \uf57e";
} else if (batCapacity < 70.0) {
*outString_ = batCapacityStr + "% \uf57f";
} else if (batCapacity < 80.0) {
*outString_ = batCapacityStr + "% \uf580";
} else if (batCapacity < 90.0) {
*outString_ = batCapacityStr + "% \uf581";
} else if (batCapacity < 100.0){
*outString_ = batCapacityStr + "% \uf578";
} else {
if (batStatus == "Discharging") {
*outString_ = batCapacityStr + "% \uf578";
} else {
*outString_ = batCapacityStr + "% \uf583";
}
}
}
outputCondition_->notify_one();
lk.unlock();
}
void ModuleCPU::runModule_() const{
string tempStatus;
fstream tempStream;
tempStream.open("/sys/class/thermal/thermal_zone0/temp", ios::in);
if ( tempStream.is_open() ) { // fail silently
getline(tempStream, tempStatus);
}
tempStream.close();
int32_t cpuTemp = 0;
if ( tempStatus.size() ) {
cpuTemp = stoi(tempStatus)/1000;
}
string loadLine;
fstream loadFileStream;
// the CPU usage data in this file are cumulative, so I must keep the values from the previous iteration (previous*_ private members)
// I then subtract these previous values to get the data for the measurement interval
loadFileStream.open("/proc/stat", ios::in);
if ( loadFileStream.is_open() ) { // fail silently
getline(loadFileStream, loadLine);
}
loadFileStream.close();
float curTotalLoad = 0.0;
float curIdleLoad = 0.0;
float percentLoad = 0.0;
if ( loadLine.size() ) {
string field;
stringstream lineStream(loadLine);
lineStream >> field; // first filed is the line name
uint16_t fInd = 1;
while (lineStream >> field) {
if ( (fInd == 4) || (fInd == 5) ) {
curIdleLoad += stod(field);
curTotalLoad += stod(field);
} else {
curTotalLoad += stod(field);
}
fInd++;
}
percentLoad = ( 1.0 - (curIdleLoad - previousIdleLoad_)/(curTotalLoad - previousTotalLoad_) )*100;
previousIdleLoad_ = curIdleLoad;
previousTotalLoad_ = curTotalLoad;
}
string thermGlyph;
if (cpuTemp < 35) {
thermGlyph = "\ue20c";
} else if (cpuTemp < 80) {
thermGlyph = "\ue20a";
} else {
thermGlyph = "\ue20b";
}
stringstream pctStr;
pctStr << fixed << setprecision(1) << percentLoad;
const string loadOut = "\ufb19 " + pctStr.str() + "% " + thermGlyph + " " + to_string(cpuTemp) + "°C";
mutex mtx;
unique_lock<mutex> lk(mtx);
*outString_ = loadOut;
outputCondition_->notify_one();
lk.unlock();
}
void ModuleRAM::runModule_() const {
string memLine;
fstream memInfoStream;
memInfoStream.open("/proc/meminfo", ios::in);
while ( getline(memInfoStream, memLine) ){
if (memLine.compare(0, 13, "MemAvailable:") == 0) {
break;
}
}
memInfoStream.close();
stringstream memLineStream(memLine);
string freeMemStr;
memLineStream >> freeMemStr;
memLineStream >> freeMemStr;
float memGi = stof(freeMemStr)/1048576.0; // the value in the file is in kb
stringstream outMemStr;
outMemStr << fixed << setprecision(1) << memGi;
mutex mtx;
unique_lock<mutex> lk(mtx);
*outString_ = "\uf85a " + outMemStr.str() + "Gi";
outputCondition_->notify_one();
lk.unlock();
}
void ModuleDisk::runModule_() const {
// start the output with the home icon for the home file system
// (assuming that it's in the first element of the file system vector)
string output;
uint16_t iconInd = 0;
for (auto &fs : fsNames_){
output += (iconInd == 0 ? "\uf015 " : " \uf0a0 ");
iconInd++;
struct statvfs buf;
int test = statvfs(fs.c_str(), &buf);
float diskSpace = 0.0;
if (test == 0) {
diskSpace = static_cast<float>(buf.f_bavail * buf.f_bsize)/1073741824.0;
}
stringstream dsStream;
dsStream << fixed << setprecision(0) << diskSpace;
output += dsStream.str() + "Gi";
}
// add RAID information if available
fstream raidStream;
raidStream.open("/proc/mdstat", ios::in);
string mdstatLine;
vector<string> mdstatDeviceLines;
if ( raidStream.good() ) {
while ( getline(raidStream, mdstatLine) ) {
if (mdstatLine.compare(0, 2, "md") == 0) { // found the array line
string curDevLine;
getline(raidStream, curDevLine);
mdstatDeviceLines.push_back(curDevLine); // in case there is more than one array on the system
}
}
}
if ( raidStream.is_open() ) {
raidStream.close();
}
vector<string> deviceStatus;
if ( mdstatDeviceLines.size() ) {
for (auto &mdl : mdstatDeviceLines){
string curStatus;
for (auto &mdc : mdl){
if (mdc == 'U') {
curStatus += "\uf431";
} else if (mdc == '_') {
curStatus += "\uf433";
}
}
deviceStatus.push_back(curStatus);
}
output += " \uf98a";
for (auto &ds : deviceStatus){
output += " " + ds;
}
}
mutex mtx;
unique_lock<mutex> lk(mtx);
if ( output.size() ) {
*outString_ = output;
}
outputCondition_->notify_one();
lk.unlock();
}
// static member
const size_t ModuleExtern::lengthLimit_ = 500;
void ModuleExtern::runModule_() const {
char buffer[100];
string output;
FILE *pipe = popen(extCommand_.c_str(), "r");
if (!pipe) { // fail silently
return;
}
while ( !feof(pipe) ) {
if (fgets(buffer, 100, pipe) != NULL) {
output += buffer;
}
if (output.size() > lengthLimit_) {
output.erase( output.begin()+lengthLimit_, output.end() );
break;
}
}
pclose(pipe);
mutex mtx;
unique_lock<mutex> lk(mtx);
*outString_ = output;
outputCondition_->notify_one();
lk.unlock();
}