about summary refs log tree commit diff
path: root/modules
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2020-07-03 02:01:09 +0000
committerAlyssa Ross <hi@alyssa.is>2020-07-03 02:01:09 +0000
commitd44c3978506850942a43fe48b63993f1bea69d4b (patch)
treede711e60eb131d9631704c04ce0d1b79b60af386 /modules
parent8c8817818f5f411402f35159db456ba3dd297207 (diff)
downloadnixlib-d44c3978506850942a43fe48b63993f1bea69d4b.tar
nixlib-d44c3978506850942a43fe48b63993f1bea69d4b.tar.gz
nixlib-d44c3978506850942a43fe48b63993f1bea69d4b.tar.bz2
nixlib-d44c3978506850942a43fe48b63993f1bea69d4b.tar.lz
nixlib-d44c3978506850942a43fe48b63993f1bea69d4b.tar.xz
nixlib-d44c3978506850942a43fe48b63993f1bea69d4b.tar.zst
nixlib-d44c3978506850942a43fe48b63993f1bea69d4b.zip
modules/sway: show bat charge if no capacity data
I plugged a battery in and had no capacity data, which at the time
meant even the status (which was available) wouldn't be shown.  Now,
status and capacity will be shown independently of each other, and if
capacity information is unavailable, charge will be shown instead.
Diffstat (limited to 'modules')
-rw-r--r--modules/workstation/windowing/sway/status.cpp56
1 files changed, 38 insertions, 18 deletions
diff --git a/modules/workstation/windowing/sway/status.cpp b/modules/workstation/windowing/sway/status.cpp
index ff15995ed1d2..f39b13b079d1 100644
--- a/modules/workstation/windowing/sway/status.cpp
+++ b/modules/workstation/windowing/sway/status.cpp
@@ -51,6 +51,7 @@ public:
 	fs::path path();
 	BatteryStatus status();
 	int capacity();
+	int charge_now();
 
 private:
 	string m_name;
@@ -89,6 +90,11 @@ 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,
@@ -120,31 +126,45 @@ int main(void)
 				continue;
 
 			Battery battery (name);
-			BatteryStatus status;
-			int capacity;
+			auto batdisplay = false;
 
 			try {
-				status = battery.status();
-				capacity = battery.capacity();
+				switch (battery.status()) {
+				case Charging:
+					out << "↑";
+					break;
+				case Discharging:
+					out << "↓";
+					break;
+				default:
+					out << " ";
+				}
+				batdisplay = true;
 			} catch (const system_error& ex) {
-				if (ex.code().value() == ENOENT)
-					continue;
-
-				throw ex;
+				if (ex.code().value() != ENOENT)
+					throw ex;
 			}
 
-			switch (status) {
-			case Charging:
-				out << "↑";
-				break;
-			case Discharging:
-				out << "↓";
-				break;
-			default:
-				out << " ";
+			try {
+				int capacity = battery.capacity();
+				out << capacity << "%";
+				batdisplay = true;
+			} catch (const system_error& ex) {
+				if (ex.code().value() != ENOENT)
+					throw ex;
+
+				try {
+					int charge_now = battery.charge_now();
+					out << charge_now;
+					batdisplay = true;
+				} catch (const system_error& ex) {
+					if (ex.code().value() != ENOENT)
+						throw ex;
+				}
 			}
 
-			out << capacity << "%  ";
+			if (batdisplay)
+				out << "  ";
 		}
 
 		auto t = time(nullptr);