2015-12-24 01:22:29 +01:00
|
|
|
#include <fcntl.h>
|
2015-11-29 16:26:50 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-12-13 14:35:39 +01:00
|
|
|
#include <string.h>
|
2015-12-13 21:40:19 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2015-12-18 03:02:35 +01:00
|
|
|
#include <unistd.h>
|
2015-12-19 16:24:51 +01:00
|
|
|
#include <sys/select.h>
|
2015-12-21 11:46:15 +01:00
|
|
|
#include <sys/wait.h>
|
2015-12-19 16:24:51 +01:00
|
|
|
#include <errno.h>
|
2015-12-13 21:40:19 +01:00
|
|
|
#include <json-c/json.h>
|
2015-12-13 22:04:54 +01:00
|
|
|
#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"
|
2015-11-29 21:27:28 +01:00
|
|
|
#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"
|
2015-11-29 16:58:18 +01:00
|
|
|
|
2016-01-23 20:55:01 +01:00
|
|
|
struct swaybar_state *state;
|
2015-12-22 17:34:37 +01:00
|
|
|
|
2015-12-21 11:46:15 +01:00
|
|
|
void sway_terminate(void) {
|
2016-01-24 01:03:08 +01:00
|
|
|
free_state(state);
|
2015-11-29 16:26:50 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-12-21 11:46:15 +01:00
|
|
|
void sig_handler(int signal) {
|
2016-01-24 01:03:08 +01:00
|
|
|
free_state(state);
|
2015-12-21 11:46:15 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2015-12-19 16:24:51 +01:00
|
|
|
void poll_for_update() {
|
|
|
|
fd_set readfds;
|
|
|
|
int activity;
|
2016-01-23 20:55:01 +01:00
|
|
|
bool dirty = true;
|
2015-12-19 16:24:51 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-12-23 00:49:21 +01:00
|
|
|
}
|
2015-12-19 16:24:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-12-19 16:24:51 +01:00
|
|
|
|
|
|
|
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)) {
|
2016-01-03 22:00:59 +01:00
|
|
|
sway_log(L_DEBUG, "Got IPC event.");
|
2016-01-24 00:23:09 +01:00
|
|
|
dirty = handle_ipc_event(state);
|
2015-12-19 16:24:51 +01:00
|
|
|
}
|
|
|
|
|
2016-01-23 20:55:01 +01:00
|
|
|
if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) {
|
2015-12-19 16:24:51 +01:00
|
|
|
sway_log(L_DEBUG, "Got update from status command.");
|
2016-01-23 20:55:01 +01:00
|
|
|
dirty = handle_status_line(state);
|
2015-12-19 16:24:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2016-01-04 01:22:20 +01:00
|
|
|
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'},
|
2016-01-23 20:21:34 +01:00
|
|
|
{"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;
|
2016-01-04 01:22:20 +01:00
|
|
|
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
|
|
|
|
2016-01-04 01:22:20 +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();
|
2015-11-29 16:58:18 +01:00
|
|
|
|
2016-01-23 20:55:01 +01:00
|
|
|
if (!state->output->registry->desktop_shell) {
|
2015-11-29 16:58:18 +01:00
|
|
|
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-25 15:29:36 +01:00
|
|
|
|
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];
|
2015-12-20 17:02:35 +01:00
|
|
|
|
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) {
|
2015-12-24 01:22:29 +01:00
|
|
|
int pipefd[2];
|
2015-12-18 03:02:35 +01:00
|
|
|
pipe(pipefd);
|
2016-01-23 20:55:01 +01:00
|
|
|
state->status_command_pid = fork();
|
|
|
|
if (state->status_command_pid == 0) {
|
2015-12-18 03:02:35 +01:00
|
|
|
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,
|
2015-12-18 03:02:35 +01:00
|
|
|
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 11:25:55 +01:00
|
|
|
}
|
2015-12-16 04:22:22 +01:00
|
|
|
|
2015-12-21 11:46:15 +01:00
|
|
|
signal(SIGTERM, sig_handler);
|
|
|
|
|
2015-12-19 16:24:51 +01:00
|
|
|
poll_for_update();
|
2015-11-29 16:58:18 +01:00
|
|
|
|
2015-12-21 11:46:15 +01:00
|
|
|
// gracefully shutdown swaybar and status_command
|
2016-01-24 01:03:08 +01:00
|
|
|
free_state(state);
|
2015-11-29 16:58:18 +01:00
|
|
|
|
2015-11-29 16:26:50 +01:00
|
|
|
return 0;
|
|
|
|
}
|