aboutsummaryrefslogtreecommitdiff
path: root/swaybar/tray/dbus.c
blob: 333d398ed4d021d9690b2dd4ee54e59979d788f0 (plain)
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
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <poll.h>
#include <signal.h>
#include <time.h>
#include <dbus/dbus.h>
#include "swaybar/tray/dbus.h"
#include "swaybar/event_loop.h"
#include "log.h"

DBusConnection *conn = NULL;

static void dispatch_watch(int fd, short mask, void *data) {
	sway_log(L_DEBUG, "Dispatching watch");
	DBusWatch *watch = data;

	if (!dbus_watch_get_enabled(watch)) {
		return;
	}

	uint32_t flags = 0;

	if (mask & POLLIN) {
		flags |= DBUS_WATCH_READABLE;
	} if (mask & POLLOUT) {
		flags |= DBUS_WATCH_WRITABLE;
	} if (mask & POLLHUP) {
		flags |= DBUS_WATCH_HANGUP;
	} if (mask & POLLERR) {
		flags |= DBUS_WATCH_ERROR;
	}

	dbus_watch_handle(watch, flags);
}

static dbus_bool_t add_watch(DBusWatch *watch, void *_data) {
	if (!dbus_watch_get_enabled(watch)) {
		// Watch should not be polled
		return TRUE;
	}

	short mask = 0;
	uint32_t flags = dbus_watch_get_flags(watch);

	if (flags & DBUS_WATCH_READABLE) {
		mask |= POLLIN;
	} if (flags & DBUS_WATCH_WRITABLE) {
		mask |= POLLOUT;
	}

	int fd = dbus_watch_get_unix_fd(watch);

	sway_log(L_DEBUG, "Adding DBus watch fd: %d", fd);
	add_event(fd, mask, dispatch_watch, watch);

	return TRUE;
}

static void remove_watch(DBusWatch *watch, void *_data) {
	int fd = dbus_watch_get_unix_fd(watch);

	remove_event(fd);
}

static void dispatch_timeout(timer_t timer, void *data) {
	sway_log(L_DEBUG, "Dispatching DBus timeout");
	DBusTimeout *timeout = data;

	if (dbus_timeout_get_enabled(timeout)) {
		dbus_timeout_handle(timeout);
	}
}

static dbus_bool_t add_timeout(DBusTimeout *timeout, void *_data) {
	if (!dbus_timeout_get_enabled(timeout)) {
		return TRUE;
	}

	timer_t *timer = malloc(sizeof(timer_t));
	if (!timer) {
		sway_log(L_ERROR, "Cannot allocate memory");
		return FALSE;
	}
	struct sigevent ev = {
		.sigev_notify = SIGEV_NONE,
	};

	if (timer_create(CLOCK_MONOTONIC, &ev, timer)) {
		sway_log(L_ERROR, "Could not create DBus timer");
		return FALSE;
	}

	int interval = dbus_timeout_get_interval(timeout);
	int interval_sec = interval / 1000;
	int interval_msec = (interval_sec * 1000) - interval;

	struct timespec period = {
		(time_t) interval_sec,
		((long) interval_msec) * 1000 * 1000,
	};
	struct itimerspec time = {
		period,
		period,
	};

	timer_settime(*timer, 0, &time, NULL);

	dbus_timeout_set_data(timeout, timer, free);

	sway_log(L_DEBUG, "Adding DBus timeout. Interval: %ds %dms", interval_sec, interval_msec);
	add_timer(*timer, dispatch_timeout, timeout);

	return TRUE;
}
static void remove_timeout(DBusTimeout *timeout, void *_data) {
	timer_t *timer = (timer_t *) dbus_timeout_get_data(timeout);
	sway_log(L_DEBUG, "Removing DBus timeout.");

	if (timer) {
		remove_timer(*timer);
	}
}

static bool should_dispatch = true;

static void dispatch_status(DBusConnection *connection, DBusDispatchStatus new_status,
		void *_data) {
	if (new_status == DBUS_DISPATCH_DATA_REMAINS) {
		should_dispatch = true;
	}
}

/* Public functions below */

void dispatch_dbus() {
	if (!should_dispatch) {
		return;
	}

	DBusDispatchStatus status;

	do {
		status = dbus_connection_dispatch(conn);
	} while (status == DBUS_DISPATCH_DATA_REMAINS);

	if (status != DBUS_DISPATCH_COMPLETE) {
		sway_log(L_ERROR, "Cannot dispatch dbus events: %d", status);
	}

	should_dispatch = false;
}

int dbus_init() {
	DBusError error;
	dbus_error_init(&error);

	conn = dbus_bus_get(DBUS_BUS_SESSION, &error);
	dbus_connection_set_exit_on_disconnect(conn, FALSE);
	if (dbus_error_is_set(&error)) {
		sway_log(L_ERROR, "Cannot get bus connection: %s\n", error.message);
		conn = NULL;
		return -1;
	}

	sway_log(L_INFO, "Unique name: %s\n", dbus_bus_get_unique_name(conn));

	// Will be called if dispatch status changes
	dbus_connection_set_dispatch_status_function(conn, dispatch_status, NULL, NULL);

	if (!dbus_connection_set_watch_functions(conn, add_watch, remove_watch,
			NULL, NULL, NULL)) {
		dbus_connection_set_watch_functions(conn, NULL, NULL, NULL, NULL, NULL);
		sway_log(L_ERROR, "Failed to activate DBUS watch functions");
		return -1;
	}

	if (!dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout,
			NULL, NULL, NULL)) {
		dbus_connection_set_watch_functions(conn, NULL, NULL, NULL, NULL, NULL);
		dbus_connection_set_timeout_functions(conn, NULL, NULL, NULL, NULL, NULL);
		sway_log(L_ERROR, "Failed to activate DBUS timeout functions");
		return -1;
	}

	return 0;
}