aboutsummaryrefslogtreecommitdiff
path: root/swaybar/main.c
blob: a521fa793c5a10cc43e6bcee7f21feb46fa326b2 (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
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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <errno.h>
#include <json-c/json.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include "ipc-client.h"
#include "client/registry.h"
#include "client/window.h"
#include "client/pango.h"
#include "stringop.h"
#include "log.h"
#include "state.h"
#include "config.h"
#include "render.h"
#include "status_line.h"
#include "ipc.h"

struct swaybar_state *state;

void swaybar_teardown() {
	window_teardown(state->output->window);
	if (state->output->registry) {
		registry_teardown(state->output->registry);
	}

	if (state->status_read_fd) {
		close(state->status_read_fd);
	}

	if (state->status_command_pid) {
		// terminate status_command process
		int ret = kill(state->status_command_pid, SIGTERM);
		if (ret != 0) {
			sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", state->status_command_pid);
		} else {
			int status;
			waitpid(state->status_command_pid, &status, 0);
		}
	}

	if (state->status_read_fd) {
		close(state->status_read_fd);
	}

	if (state->ipc_socketfd) {
		close(state->ipc_socketfd);
	}

	if (state->ipc_event_socketfd) {
		close(state->ipc_event_socketfd);
	}
}

void sway_terminate(void) {
	swaybar_teardown();
	exit(EXIT_FAILURE);
}

void sig_handler(int signal) {
	swaybar_teardown();
	exit(0);
}

void poll_for_update() {
	fd_set readfds;
	int activity;
	bool dirty = true;

	while (1) {
		if (dirty) {
			struct output *output = state->output;
			if (window_prerender(output->window) && output->window->cairo) {
				render(output, state->config, state->status);
				window_render(output->window);
				if (wl_display_dispatch(output->registry->display) == -1) {
					break;
				}
			}
		}

		dirty = false;
		FD_ZERO(&readfds);
		FD_SET(state->ipc_event_socketfd, &readfds);
		FD_SET(state->status_read_fd, &readfds);

		activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
		if (activity < 0) {
			sway_log(L_ERROR, "polling failed: %d", errno);
		}

		if (FD_ISSET(state->ipc_event_socketfd, &readfds)) {
			sway_log(L_DEBUG, "Got IPC event.");
			dirty = handle_ipc_event(state);
		}

		if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) {
			sway_log(L_DEBUG, "Got update from status command.");
			dirty = handle_status_line(state);
		}
	}
}

int main(int argc, char **argv) {
	char *socket_path = NULL;
	char *bar_id = NULL;
	bool debug = false;
	state = init_state();

	static struct option long_options[] = {
		{"help", no_argument, NULL, 'h'},
		{"version", no_argument, NULL, 'v'},
		{"socket", required_argument, NULL, 's'},
		{"bar_id", required_argument, NULL, 'b'},
		{"debug", no_argument, NULL, 'd'},
		{0, 0, 0, 0}
	};

	const char *usage =
		"Usage: swaybar [options...] <output>\n"
		"\n"
		"  -h, --help             Show help message and quit.\n"
		"  -v, --version          Show the version number and quit.\n"
		"  -s, --socket <socket>  Connect to sway via socket.\n"
		"  -b, --bar_id <id>      Bar ID for which to get the configuration.\n"
		"  -d, --debug            Enable debugging.\n"
		"\n"
		" PLEASE NOTE that swaybar will be automatically started by sway as\n"
		" soon as there is a 'bar' configuration block in your config file.\n"
		" You should never need to start it manually.\n";

	int c;
	while (1) {
		int option_index = 0;
		c = getopt_long(argc, argv, "hvs:b:d", long_options, &option_index);
		if (c == -1) {
			break;
		}
		switch (c) {
		case 's': // Socket
			socket_path = strdup(optarg);
			break;
		case 'b': // Type
			bar_id = strdup(optarg);
			break;
		case 'v':
#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
			fprintf(stdout, "sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH);
#else
			fprintf(stdout, "version not detected\n");
#endif
			exit(EXIT_SUCCESS);
			break;
		case 'd': // Debug
			debug = true;
			break;
		default:
			fprintf(stderr, "%s", usage);
			exit(EXIT_FAILURE);
		}
	}

	if (!bar_id) {
		sway_abort("No bar_id passed. Provide --bar_id or let sway start swaybar");
	}

	if (debug) {
		init_log(L_DEBUG);
	} else {
		init_log(L_ERROR);
	}

	state->output->registry = registry_poll();

	if (!state->output->registry->desktop_shell) {
		sway_abort("swaybar requires the compositor to support the desktop-shell extension.");
	}

	if (!socket_path) {
		socket_path = get_socketpath();
		if (!socket_path) {
			sway_abort("Unable to retrieve socket path");
		}
	}
	state->ipc_socketfd = ipc_open_socket(socket_path);
	state->ipc_event_socketfd = ipc_open_socket(socket_path);

	if (argc == optind) {
		sway_abort("No output index provided");
	}

	int desired_output = atoi(argv[optind]);
	ipc_bar_init(state, desired_output, bar_id);

	struct output_state *output = state->output->registry->outputs->items[desired_output];

	state->output->window = window_setup(state->output->registry, output->width, 30, false);
	if (!state->output->window) {
		sway_abort("Failed to create window.");
	}
	desktop_shell_set_panel(state->output->registry->desktop_shell, output->output, state->output->window->surface);
	desktop_shell_set_panel_position(state->output->registry->desktop_shell, state->config->position);

	/* set font */
	state->output->window->font = state->config->font;

	/* set window height */
	set_window_height(state->output->window, state->config->height);

	if (state->config->status_command) {
		int pipefd[2];
		pipe(pipefd);
		state->status_command_pid = fork();
		if (state->status_command_pid == 0) {
			close(pipefd[0]);
			dup2(pipefd[1], STDOUT_FILENO);
			close(pipefd[1]);
			char *const cmd[] = {
				"sh",
				"-c",
				state->config->status_command,
				NULL,
			};
			execvp(cmd[0], cmd);
			return 0;
		}

		close(pipefd[1]);
		state->status_read_fd = pipefd[0];
		fcntl(state->status_read_fd, F_SETFL, O_NONBLOCK);
	}

	signal(SIGTERM, sig_handler);

	poll_for_update();

	// gracefully shutdown swaybar and status_command
	swaybar_teardown();

	return 0;
}