~kennylevinsen/poweralertd

ref: fc612ab8fd5aa23ff70c7b837f1b2f871ff0d950 poweralertd/main.c -rw-r--r-- 7.0 KiB
fc612ab8 — Thomas Weißschuh feat: don't send warning when changing from unknown to no warning 5 months ago
                                                                                
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
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>

#include "dbus.h"
#include "notify.h"
#include "upower.h"
#include "list.h"

#define NOTIFICATION_MAX_LEN 128

static int send_remove(sd_bus *bus, struct upower_device *device) {
	enum urgency urgency = URGENCY_NORMAL;
	char title[NOTIFICATION_MAX_LEN];

	if (strlen(device->model) > 0) {
		snprintf(title, NOTIFICATION_MAX_LEN, "Power status: %s", device->model);
	} else {
		snprintf(title, NOTIFICATION_MAX_LEN, "Power status: %s (%s)", device->native_path, upower_device_type_string(device));
	}
	char *msg = "Device disconnected\n";

	return notify(bus, title, msg, 0, urgency);
}

static int send_online_update(sd_bus *bus, struct upower_device *device) {
	if (device->current.online == device->last.online) {
		return 0;
	}

	char title[NOTIFICATION_MAX_LEN];
	char *msg;

	if (strlen(device->model) > 0) {
		snprintf(title, NOTIFICATION_MAX_LEN, "Power status: %s", device->model);
	} else {
		snprintf(title, NOTIFICATION_MAX_LEN, "Power status: %s (%s)", device->native_path, upower_device_type_string(device));
	}
	if (device->current.online) {
		msg = "Power supply online";
	} else {
		msg = "Power supply offline";
	}

	return notify(bus, title, msg, &device->notifications[SLOT_ONLINE], URGENCY_NORMAL);
}

static int send_state_update(sd_bus *bus, struct upower_device *device) {
	if (device->current.state == device->last.state) {
		return 0;
	}

	enum urgency urgency;
	char title[NOTIFICATION_MAX_LEN];
	char msg[NOTIFICATION_MAX_LEN];

	switch (device->current.state) {
	case UPOWER_DEVICE_STATE_UNKNOWN:
		// Silence transitions to/from unknown
		device->current.state = device->last.state;
		return 0;
	case UPOWER_DEVICE_STATE_EMPTY:
	case UPOWER_DEVICE_STATE_PENDING_CHARGE:
		urgency = URGENCY_CRITICAL;
		break;
	default:
		urgency = URGENCY_NORMAL;
		break;
	}

	if (strlen(device->model) > 0) {
		snprintf(title, NOTIFICATION_MAX_LEN, "Power status: %s", device->model);
	} else {
		snprintf(title, NOTIFICATION_MAX_LEN, "Power status: %s (%s)", device->native_path, upower_device_type_string(device));
	}
	if (device->current.battery_level != UPOWER_DEVICE_LEVEL_NONE) {
		snprintf(msg, NOTIFICATION_MAX_LEN, "Battery %s\nCurrent level: %s\n", upower_device_state_string(device), upower_device_battery_level_string(device));
	} else {
		snprintf(msg, NOTIFICATION_MAX_LEN, "Battery %s\nCurrent level: %0.0lf%%\n", upower_device_state_string(device), device->current.percentage);
	}

	return notify(bus, title, msg, &device->notifications[SLOT_STATE], urgency);
}

static int send_warning_update(sd_bus *bus, struct upower_device *device) {
	if (device->current.warning_level == device->last.warning_level) {
		return 0;
	}

	if (device->current.warning_level == UPOWER_DEVICE_LEVEL_NONE && device->last.warning_level == UPOWER_DEVICE_LEVEL_UNKNOWN) {
		return 0;
	}

	enum urgency urgency = URGENCY_CRITICAL;
	char title[NOTIFICATION_MAX_LEN];
	char *msg;

	switch (device->current.warning_level) {
	case UPOWER_DEVICE_LEVEL_NONE:
		msg = "Warning cleared\n";
		urgency = URGENCY_NORMAL;
		break;
	case UPOWER_DEVICE_LEVEL_DISCHARGING:
		msg = "Warning: system discharging\n";
		break;
	case UPOWER_DEVICE_LEVEL_LOW:
		msg = "Warning: power level low\n";
		break;
	case UPOWER_DEVICE_LEVEL_CRITICAL:
		msg = "Warning: power level critical\n";
		urgency = URGENCY_CRITICAL;
		break;
	case UPOWER_DEVICE_LEVEL_ACTION:
		msg = "Warning: power level at action threshold\n";
		break;
	default:
		msg = "Warning: unknown warning level\n";
		break;
	}

	if (strlen(device->model) > 0) {
		snprintf(title, NOTIFICATION_MAX_LEN, "Power warning: %s", device->model);
	} else {
		snprintf(title, NOTIFICATION_MAX_LEN, "Power warning: %s (%s)", device->native_path, upower_device_type_string(device));
	}

	return notify(bus, title, msg, &device->notifications[SLOT_WARNING], urgency);
}

static const char usage[] = "usage: %s [options]\n"
"  -h				show this help message\n"
"  -s				ignore the events at startup\n"
"  -i <device_type>		ignore this device type, can be use several times\n";


int main(int argc, char *argv[]) {
	int opt = 0;
	int device_type = 0;
	int ignore_types_mask = 0;
	bool ignore_initial = false;
	bool initialized = false;

	while ((opt = getopt(argc, argv, "hsi:")) != -1) {
		switch (opt) {
		case 'i':
			device_type = upower_device_type_int(optarg);
			if (device_type > -1) {
				ignore_types_mask |= 1 << device_type;
			}
			else {
				printf("Unrecognized device type: %s\n", optarg);
			}
			break;
		case 's':
			ignore_initial = true;
			break;
		case 'h':
		default:
			fprintf(stderr, usage, argv[0]);
			return opt == 'h' ? EXIT_SUCCESS : EXIT_FAILURE;
		}
	}

	struct upower state = { 0 };
	sd_bus *user_bus = NULL;
	sd_bus *system_bus = NULL;
	int ret;

	ret = sd_bus_open_user(&user_bus);
	if (ret < 0) {
		fprintf(stderr, "could not connect to session bus: %s\n", strerror(-ret));
		goto finish;
	}

	ret = sd_bus_open_system(&system_bus);
	if (ret < 0) {
		fprintf(stderr, "could not connect to system bus: %s\n", strerror(-ret));
		goto finish;
	}

	state.bus = system_bus;

	ret = init_upower(system_bus, &state);
	if (ret < 0) {
		fprintf(stderr, "could not init upower: %s\n", strerror(-ret));
		goto finish;
	}

	while (1) {
		for (int idx = 0; idx < state.devices->length; idx++) {
			struct upower_device *device = state.devices->items[idx];

			if ((ignore_types_mask & (1 << device->type))) {
				goto next_device;
			}

			if (!initialized && ignore_initial) {
				goto next_device;
			}


			if (upower_device_has_battery(device)) {
				ret = send_state_update(user_bus, device);
				if (ret < 0) {
					fprintf(stderr, "could not send state update notification: %s\n", strerror(-ret));
					goto finish;
				}
				ret = send_warning_update(user_bus, device);
				if (ret < 0) {
					fprintf(stderr, "could not send warning update notification: %s\n", strerror(-ret));
					goto finish;
				}
			} else {
				ret = send_online_update(user_bus, device);
				if (ret < 0) {
					fprintf(stderr, "could not send online update notification: %s\n", strerror(-ret));
					goto finish;
				}
			}
next_device:
			device->last = device->current;
		}

		for (int idx = 0; idx < state.removed_devices->length; idx++) {
			struct upower_device *device = state.removed_devices->items[idx];

			ret = send_remove(user_bus, device);
			if (ret < 0) {
				fprintf(stderr, "could not send device removal notification: %s\n", strerror(-ret));
				goto finish;
			}
			upower_device_destroy(device);
			list_del(state.removed_devices, idx);
		}

		ret = sd_bus_process(system_bus, NULL);
		if (ret < 0) {
			fprintf(stderr, "could not process system bus messages: %s\n", strerror(-ret));
			goto finish;
		} else if (ret > 0) {
			continue;
		}


		ret = sd_bus_wait(system_bus, UINT64_MAX);
		if (ret < 0) {
			fprintf(stderr, "could not wait for system bus messages: %s\n", strerror(-ret));
			goto finish;
		}

		initialized = true;

	}

finish:
	destroy_upower(system_bus, &state);
	sd_bus_unref(user_bus);
	sd_bus_unref(system_bus);

	return ret < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}