-
Notifications
You must be signed in to change notification settings - Fork 0
/
power_options.c
91 lines (76 loc) · 3.09 KB
/
power_options.c
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
#include "power_options.h"
static void
spm_free(DBusError* error, DBusConnection* connection, DBusMessage* message,
const char* message_to_log)
{
uint16_t size = strlen(message_to_log) + 3;
if (error->message)
size += strlen(error->message);
char* msg;
if (strncmp(message_to_log, "Cleaning", 8) == 0)
msg = format(strlen(message_to_log) + 1, "%s\n", message_to_log);
else
msg = format(size + 1, "%s: %s\n", message_to_log, error->message);
logger("spm_free", msg, stderr);
free(msg);
if (error != NULL)
dbus_error_free(error);
if (connection != NULL)
dbus_connection_unref(connection);
if (message != NULL)
dbus_message_unref(message);
}
void*
lock_as_needed(void* data)
{
const char* method = (char*) data;
if (strncmp(method, SUSPEND, strlen(SUSPEND)) == 0
|| strncmp(method, HIBERNATE, strlen(HIBERNATE)) == 0)
{
run_locker(locker_cmd);
}
return data;
}
uint8_t
spm_power(const char* method)
{
DBusConnection* connection;
DBusMessage* message;
DBusError error;
DBusBusType bus_type = DBUS_BUS_SYSTEM;
int timeout = DBUS_TIMEOUT_USE_DEFAULT;
dbus_error_init(&error);
connection = dbus_bus_get(bus_type, &error);
if (connection == NULL) {
spm_free(&error, NULL, NULL, "There was an error while making the connection");
return 1;
}
message = dbus_message_new_method_call(DEST, PATH, NAME, method);
if (message == NULL) {
spm_free(&error, connection, NULL, "There was an error while making the message to the bus");
return 1;
}
dbus_message_set_auto_start(message, FALSE);
if (dbus_message_append_args(message, DBUS_TYPE_BOOLEAN, EXTRA, DBUS_TYPE_INVALID) != TRUE) {
spm_free(&error, connection, message, "There was an error while appending args to the message");
return 1;
}
/* If user wants to lock their screen before suspend or hibernate... */
pthread_t lock_id;
/* I know what I'm doing is horrible (casting a const pointer to a non-const one),
* but the lock_as_needed function although it receives it as a non-const value,
* it treats it as a const one.
* It's done in this way because of pthread_create requirements. */
pthread_create(&lock_id, NULL, lock_as_needed, (void *) method);
dbus_connection_send_with_reply_and_block(connection, message, timeout, &error);
if (dbus_error_is_set(&error)) {
spm_free(&error, connection, message, "There was an error");
return 1;
}
char* message_to_log = format(strlen(method) + 5, "%s-ing\n", method);
logger("spm_power", message_to_log, stdout);
free(message_to_log);
spm_free(&error, connection, message, "Cleaning");
pthread_join(lock_id, NULL);
return 0;
}