about summary refs log tree commit diff
path: root/modules/workstation/windowing/sway/status.cpp
blob: 1f73458d86b2b20301faa7bdabac02733f9cf1b6 (plain) (blame)
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
// SPDX-License-Identifier: GPL-3.0-or-later

// Copyright 2020 Alyssa Ross
//
// 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 3 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#include <chrono>
#include <fcntl.h>
#include <filesystem>
#include <iostream>
#include <thread>
#include <unistd.h>

namespace fs = std::filesystem;

using fs::directory_iterator;
using std::chrono::seconds;
using std::generic_category;
using std::put_time;
using std::stoi;
using std::string;
using std::stringstream;
using std::system_error;
using std::this_thread::sleep_for;
using std::time;

enum BatteryStatus {
	Unknown,
	Charging,
	Discharging,
	NotCharging,
	Full,
};

class Battery {
public:
	Battery(string name);

	string name() { return m_name; }
	fs::path path();
	BatteryStatus status();
	int capacity();
	int charge_now();

private:
	string m_name;
	string attr(string name);
};

Battery::Battery(string name)
{
	m_name = name;
}

fs::path Battery::path()
{
	static fs::path base = "/sys/class/power_supply";
	return base / name();
}

BatteryStatus Battery::status()
{
	auto status = attr("status");

	if (status == "Charging")
		return Charging;
	if (status == "Discharging")
		return Discharging;
	if (status == "Not charging")
		return NotCharging;
	if (status == "Full")
		return Full;

	return Unknown;
}

int Battery::capacity()
{
	return stoi(attr("capacity"));
}

int Battery::charge_now()
{
	return stoi(attr("charge_now"));
}

string Battery::attr(string name)
{
	// Use read() to make sure this is done in a single read syscall,
	// because sysfs doesn't like multiple reads.
	int fd = open((path() / name).c_str(), O_RDONLY);
	if (fd == -1)
		throw system_error(errno, generic_category());
	char buf[13];
	int len = read(fd, &buf, 13);
	if (len == -1)
		throw system_error(errno, generic_category());
	close(fd);
	string value (buf, len);
	if (value.back() == '\n')
		value.pop_back();
	return value;
}

int main(void)
{
	while (true) {
		// Buffer output so it can be done all at once in a single write(),
		// because of <https://github.com/swaywm/sway/issues/3857>.
		stringstream out;

		for (const auto& entry : directory_iterator("/sys/class/power_supply")) {
			auto name = entry.path().filename().string();

			Battery battery (name);
			auto batdisplay = false;

			try {
				switch (battery.status()) {
				case Charging:
					out << "↑";
					break;
				case Discharging:
					out << "↓";
					break;
				default:
					out << " ";
				}
				batdisplay = true;
			} catch (const system_error& ex) {
				switch (ex.code().value()) {
				case ENOENT:
					break;
				case ENODEV:
					out << "? ";
					batdisplay = true;
					break;
				default:
					throw ex;
				}
			}

			try {
				int capacity = battery.capacity();
				out << capacity << "%";
				batdisplay = true;
			} catch (const system_error& ex) {
				switch (ex.code().value()) {
				case ENOENT:
					break;
				case ENODEV:
					out << "??%";
					batdisplay = true;
					break;
				default:
					throw ex;
				}

				try {
					int charge_now = battery.charge_now();
					out << charge_now;
					batdisplay = true;
				} catch (const system_error& ex) {
					switch (ex.code().value()) {
					case ENOENT:
						break;
					case ENODEV:
						out << "??????";
						batdisplay = true;
						break;
					default:
						throw ex;
					}
				}
			}

			if (batdisplay)
				out << "  ";
		}

		auto t = time(nullptr);
		out << put_time(localtime(&t), "%F %T") << "\n";

		auto buf = out.str();
		if (write(STDOUT_FILENO, buf.c_str(), buf.length()) == -1)
			perror("write");

		sleep_for(seconds(1));
	}
}