File: powerstatus.cpp

package info (click to toggle)
recoll 1.43.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,512 kB
  • sloc: cpp: 104,170; python: 9,500; xml: 7,248; ansic: 6,447; sh: 1,212; perl: 130; makefile: 72
file content (122 lines) | stat: -rw-r--r-- 3,157 bytes parent folder | download
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
/* Copyright (C) 2025 J.F.Dockes
 *
 * License: GPL 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#include "powerstatus.h"

#include <mutex>

PowerStatus *theInstance;

std::mutex theMutex;

static PowerStatus::powerstatus theStatus{PowerStatus::ONAC};

PowerStatus::PowerStatus()
{
}

PowerStatus *PowerStatus::instance()
{
    std::lock_guard<std::mutex> lock(theMutex);
    if (nullptr == theInstance) {
        theInstance = new PowerStatus;
    }
    return theInstance;
}

PowerStatus::powerstatus PowerStatus::set(PowerStatus::powerstatus n)
{
    std::lock_guard<std::mutex> lock(theMutex);
    auto o = theStatus;
    theStatus = n;
    return o;
}

static void systemgetpowerstatus();

PowerStatus::powerstatus PowerStatus::get()
{
    systemgetpowerstatus();
    return theStatus;
}

#if defined(_WIN32)
static void systemgetpowerstatus()
{
    // On Windows the status is obtained by a hidden window receiving a system event and calling
    // set(), nothing to do. See recollinit.cpp
}
#elif defined(__APPLE__)

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/ps/IOPSKeys.h>

static bool isRunningOnBattery()
{
    // Get power source information
    CFTypeRef snapshot = IOPSCopyPowerSourcesInfo();
    if (!snapshot) {
        return false; // Assume AC power if unable to retrieve info
    }

    // Check power source state
    CFStringRef powerSourceState = IOPSGetProvidingPowerSourceType(snapshot);
    bool onBattery =
        CFStringCompare(powerSourceState, CFSTR(kIOPSBatteryPowerValue), 0) == kCFCompareEqualTo;

    // Clean up
    CFRelease(snapshot);

    return onBattery;
}

static void systemgetpowerstatus()
{
    theStatus = PowerStatus::ONAC;
    if (isRunningOnBattery()) {
        theStatus = PowerStatus::ONBATTERY;
    }
}

#else // Not Windows neither MacOS

#include <fcntl.h>
#include <unistd.h>
// The following is for Linux, will need updates for the BSDs
const static char *statusfile = "/sys/class/power_supply/AC/online";
static void systemgetpowerstatus()
{
    theStatus = PowerStatus::ONAC;
    int fd = -1;
    if (access(statusfile, R_OK) == 0) {
        fd = open(statusfile, 0);
        if (fd >= 0) {
            char buf[1];
            if (read(fd, buf, 1) == 1) {
                if (buf[0] == '0') {
                    theStatus = PowerStatus::ONBATTERY;
                }
            }
        }
    }
    if (fd >= 0)
        close(fd);
}
#endif