-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCtrlACCallback.h
70 lines (63 loc) · 1.83 KB
/
CtrlACCallback.h
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
#ifndef CTRLACCALLBACK_H
#define CTRLACCALLBACK_H
#include <QObject>
#include <QTimer>
#include <QDebug>
enum ACState {
Battery = 0,
ACPower,
ACNone
};
#ifdef WIN32
#include <Windows.h>
#define currentAc_refresh_time 333
#else // WIN32
#include <QFile>
#define currentAc_refresh_time 1332
#endif // WIN32
class CtrlACCallback : public QObject
{
Q_OBJECT
public:
CtrlACCallback() {
currentAc_refresh_timer = new QTimer;
connect(currentAc_refresh_timer, &QTimer::timeout,
this, &CtrlACCallback::checkCurrentACState);
currentAc_refresh_timer->start(currentAc_refresh_time);
}
~CtrlACCallback() {
disconnect(currentAc_refresh_timer, &QTimer::timeout,
this, &CtrlACCallback::checkCurrentACState);
currentAc_refresh_timer->stop();
}
signals:
void currentACStateChanged(ACState state);
private:
ACState currentACState = ACNone;
void checkCurrentACState() {
#ifdef WIN32
SYSTEM_POWER_STATUS sps;
if (GetSystemPowerStatus(&sps))
if(currentACState != ACState(sps.ACLineStatus)) {
currentACState = ACState(sps.ACLineStatus);
emit currentACStateChanged(currentACState);
}
#else // WIN32
QFile acOnline("/sys/class/power_supply/AC0/online");
if(acOnline.open(QIODevice::ReadOnly) && acOnline.size() > 0) {
int state = acOnline.readAll().toInt();
if(currentACState != ACState(state)) {
currentACState = ACState(state);
emit currentACStateChanged(currentACState);
}
}
#endif // WIN32
}
QTimer *currentAc_refresh_timer;
public slots:
void emitCurrentACState(){
if(currentACState != ACNone)
emit currentACStateChanged(currentACState);
}
};
#endif // CTRLACCALLBACK_H