sway/swaybar/main.c

217 lines
5.3 KiB
C
Raw Normal View History

#include <fcntl.h>
2015-11-29 16:26:50 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2015-12-13 21:40:19 +01:00
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <errno.h>
2015-12-13 21:40:19 +01:00
#include <json-c/json.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
2015-12-16 04:08:09 +01:00
#include <getopt.h>
2015-12-13 21:40:19 +01:00
#include "ipc-client.h"
2015-11-29 16:26:50 +01:00
#include "client/registry.h"
#include "client/window.h"
#include "client/pango.h"
2015-12-13 21:40:19 +01:00
#include "stringop.h"
2015-11-29 16:26:50 +01:00
#include "log.h"
2016-01-23 20:55:01 +01:00
#include "state.h"
2016-01-23 02:47:44 +01:00
#include "config.h"
2016-01-23 20:55:01 +01:00
#include "render.h"
#include "status_line.h"
2016-01-24 00:23:09 +01:00
#include "ipc.h"
2016-01-23 20:55:01 +01:00
struct swaybar_state *state;
void sway_terminate(void) {
free_state(state);
2015-11-29 16:26:50 +01:00
exit(EXIT_FAILURE);
}
void sig_handler(int signal) {
free_state(state);
exit(0);
}
void poll_for_update() {
fd_set readfds;
int activity;
2016-01-23 20:55:01 +01:00
bool dirty = true;
while (1) {
2016-01-23 20:55:01 +01:00
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);
2016-01-23 20:55:01 +01:00
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);
}
2016-01-23 20:55:01 +01:00
if (FD_ISSET(state->ipc_event_socketfd, &readfds)) {
sway_log(L_DEBUG, "Got IPC event.");
2016-01-24 00:23:09 +01:00
dirty = handle_ipc_event(state);
}
2016-01-23 20:55:01 +01:00
if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) {
sway_log(L_DEBUG, "Got update from status command.");
2016-01-23 20:55:01 +01:00
dirty = handle_status_line(state);
}
}
}
2015-11-29 16:26:50 +01:00
int main(int argc, char **argv) {
2015-12-16 04:08:09 +01:00
char *socket_path = NULL;
char *bar_id = NULL;
bool debug = false;
2016-01-23 20:55:01 +01:00
state = init_state();
2015-12-16 04:08:09 +01:00
static struct option long_options[] = {
2016-01-23 20:22:27 +01:00
{"help", no_argument, NULL, 'h'},
2015-12-16 04:08:09 +01:00
{"version", no_argument, NULL, 'v'},
{"socket", required_argument, NULL, 's'},
{"bar_id", required_argument, NULL, 'b'},
{"debug", no_argument, NULL, 'd'},
2015-12-16 04:08:09 +01:00
{0, 0, 0, 0}
};
2016-01-23 20:22:27 +01:00
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";
2015-12-16 04:08:09 +01:00
int c;
while (1) {
int option_index = 0;
2016-01-23 20:22:27 +01:00
c = getopt_long(argc, argv, "hvs:b:d", long_options, &option_index);
2015-12-16 04:08:09 +01:00
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;
2015-12-16 04:08:09 +01:00
default:
2016-01-23 20:22:27 +01:00
fprintf(stderr, "%s", usage);
2015-12-16 04:08:09 +01:00
exit(EXIT_FAILURE);
}
}
2015-12-16 11:13:12 +01:00
if (!bar_id) {
sway_abort("No bar_id passed. Provide --bar_id or let sway start swaybar");
}
2016-01-05 23:42:40 +01:00
if (debug) {
init_log(L_DEBUG);
} else {
init_log(L_ERROR);
}
2015-12-16 11:13:12 +01:00
2016-01-23 20:55:01 +01:00
state->output->registry = registry_poll();
2016-01-23 20:55:01 +01:00
if (!state->output->registry->desktop_shell) {
sway_abort("swaybar requires the compositor to support the desktop-shell extension.");
}
2015-12-16 04:22:22 +01:00
if (!socket_path) {
socket_path = get_socketpath();
if (!socket_path) {
sway_abort("Unable to retrieve socket path");
}
}
2016-01-23 20:55:01 +01:00
state->ipc_socketfd = ipc_open_socket(socket_path);
state->ipc_event_socketfd = ipc_open_socket(socket_path);
2015-12-16 11:13:12 +01:00
if (argc == optind) {
sway_abort("No output index provided");
}
2015-12-16 04:22:22 +01:00
int desired_output = atoi(argv[optind]);
2016-01-24 00:23:09 +01:00
ipc_bar_init(state, desired_output, bar_id);
2016-01-23 20:55:01 +01:00
struct output_state *output = state->output->registry->outputs->items[desired_output];
2016-01-23 20:55:01 +01:00
state->output->window = window_setup(state->output->registry, output->width, 30, false);
if (!state->output->window) {
2015-12-20 05:00:09 +01:00
sway_abort("Failed to create window.");
}
2016-01-23 20:55:01 +01:00
desktop_shell_set_panel(state->output->registry->desktop_shell, output->output, state->output->window->surface);
2016-01-24 00:23:09 +01:00
desktop_shell_set_panel_position(state->output->registry->desktop_shell, state->config->position);
/* set font */
state->output->window->font = state->config->font;
2015-12-16 04:22:22 +01:00
2016-01-24 00:23:09 +01:00
/* set window height */
set_window_height(state->output->window, state->config->height);
2015-12-16 04:22:22 +01:00
2016-01-23 20:55:01 +01:00
if (state->config->status_command) {
int pipefd[2];
pipe(pipefd);
2016-01-23 20:55:01 +01:00
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",
2016-01-23 20:55:01 +01:00
state->config->status_command,
NULL,
};
execvp(cmd[0], cmd);
return 0;
}
close(pipefd[1]);
2016-01-23 20:55:01 +01:00
state->status_read_fd = pipefd[0];
fcntl(state->status_read_fd, F_SETFL, O_NONBLOCK);
}
2015-12-16 04:22:22 +01:00
signal(SIGTERM, sig_handler);
poll_for_update();
// gracefully shutdown swaybar and status_command
free_state(state);
2015-11-29 16:26:50 +01:00
return 0;
}