2015-11-30 02:26:50 +11:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-12-14 00:35:39 +11:00
|
|
|
#include <string.h>
|
2015-12-14 07:40:19 +11:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2015-12-18 13:02:35 +11:00
|
|
|
#include <unistd.h>
|
2015-12-20 02:24:51 +11:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <errno.h>
|
2015-12-14 07:40:19 +11:00
|
|
|
#include <json-c/json.h>
|
2015-12-14 08:04:54 +11:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
2015-12-16 14:08:09 +11:00
|
|
|
#include <getopt.h>
|
2015-12-14 07:40:19 +11:00
|
|
|
#include "ipc-client.h"
|
2015-11-30 02:26:50 +11:00
|
|
|
#include "client/registry.h"
|
|
|
|
#include "client/window.h"
|
2015-11-30 07:27:28 +11:00
|
|
|
#include "client/pango.h"
|
2015-12-14 07:40:19 +11:00
|
|
|
#include "stringop.h"
|
2015-11-30 02:26:50 +11:00
|
|
|
#include "log.h"
|
|
|
|
|
2015-11-30 02:58:18 +11:00
|
|
|
struct box_colors {
|
|
|
|
uint32_t border;
|
|
|
|
uint32_t background;
|
|
|
|
uint32_t text;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct colors {
|
|
|
|
uint32_t background;
|
|
|
|
uint32_t statusline;
|
2015-12-17 00:14:27 +11:00
|
|
|
uint32_t separator;
|
2015-11-30 02:58:18 +11:00
|
|
|
|
|
|
|
struct box_colors focused_workspace;
|
|
|
|
struct box_colors active_workspace;
|
|
|
|
struct box_colors inactive_workspace;
|
|
|
|
struct box_colors urgent_workspace;
|
|
|
|
struct box_colors binding_mode;
|
|
|
|
};
|
|
|
|
|
2015-12-14 07:40:19 +11:00
|
|
|
struct workspace {
|
|
|
|
int num;
|
|
|
|
char *name;
|
|
|
|
bool focused;
|
|
|
|
bool visible;
|
|
|
|
bool urgent;
|
|
|
|
};
|
|
|
|
|
|
|
|
list_t *workspaces = NULL;
|
|
|
|
int socketfd;
|
2015-12-18 13:02:35 +11:00
|
|
|
pid_t pid;
|
|
|
|
int pipefd[2];
|
2015-12-14 00:35:39 +11:00
|
|
|
FILE *command;
|
2015-12-20 02:24:51 +11:00
|
|
|
char line[1024];
|
|
|
|
char *output, *status_command;
|
2015-11-30 02:26:50 +11:00
|
|
|
struct registry *registry;
|
|
|
|
struct window *window;
|
2015-12-17 12:09:23 +11:00
|
|
|
bool dirty = true;
|
2015-12-14 07:40:19 +11:00
|
|
|
|
2015-11-30 02:58:18 +11:00
|
|
|
struct colors colors = {
|
2015-11-30 04:03:13 +11:00
|
|
|
.background = 0x000000FF,
|
|
|
|
.statusline = 0xFFFFFFFF,
|
2015-12-17 00:14:27 +11:00
|
|
|
.separator = 0x666666FF,
|
2015-11-30 02:58:18 +11:00
|
|
|
|
|
|
|
.focused_workspace = {
|
2015-11-30 04:03:13 +11:00
|
|
|
.border = 0x4C7899FF,
|
|
|
|
.background = 0x285577FF,
|
|
|
|
.text = 0xFFFFFFFF
|
2015-11-30 02:58:18 +11:00
|
|
|
},
|
|
|
|
.active_workspace = {
|
2015-11-30 04:03:13 +11:00
|
|
|
.border = 0x333333FF,
|
|
|
|
.background = 0x5F676AFF,
|
|
|
|
.text = 0xFFFFFFFF
|
2015-11-30 02:58:18 +11:00
|
|
|
},
|
|
|
|
.inactive_workspace = {
|
2015-11-30 04:03:13 +11:00
|
|
|
.border = 0x333333FF,
|
|
|
|
.background = 0x222222FF,
|
|
|
|
.text = 0x888888FF
|
2015-11-30 02:58:18 +11:00
|
|
|
},
|
|
|
|
.urgent_workspace = {
|
2015-11-30 04:03:13 +11:00
|
|
|
.border = 0x2F343AFF,
|
|
|
|
.background = 0x900000FF,
|
|
|
|
.text = 0xFFFFFFFF
|
2015-11-30 02:58:18 +11:00
|
|
|
},
|
|
|
|
.binding_mode = {
|
2015-11-30 04:03:13 +11:00
|
|
|
.border = 0x2F343AFF,
|
|
|
|
.background = 0x900000FF,
|
|
|
|
.text = 0xFFFFFFFF
|
2015-11-30 02:58:18 +11:00
|
|
|
},
|
|
|
|
};
|
2015-11-30 02:26:50 +11:00
|
|
|
|
|
|
|
void sway_terminate(void) {
|
|
|
|
window_teardown(window);
|
2015-12-17 12:45:14 +11:00
|
|
|
if (registry) {
|
|
|
|
registry_teardown(registry);
|
|
|
|
}
|
2015-12-18 13:02:35 +11:00
|
|
|
|
|
|
|
if (command) {
|
|
|
|
fclose(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid) {
|
|
|
|
// terminate status_command process
|
|
|
|
kill(pid, SIGTERM);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pipefd[0]) {
|
|
|
|
close(pipefd[0]);
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:26:50 +11:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:58:18 +11:00
|
|
|
void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
|
2015-12-17 00:14:53 +11:00
|
|
|
cairo_set_source_rgba(cairo,
|
2015-11-30 02:58:18 +11:00
|
|
|
((color & 0xFF000000) >> 24) / 256.0,
|
|
|
|
((color & 0xFF0000) >> 16) / 256.0,
|
|
|
|
((color & 0xFF00) >> 8) / 256.0,
|
|
|
|
(color & 0xFF) / 256.0);
|
|
|
|
}
|
|
|
|
|
2015-12-14 08:04:54 +11:00
|
|
|
void ipc_update_workspaces() {
|
|
|
|
if (workspaces) {
|
|
|
|
free_flat_list(workspaces);
|
|
|
|
}
|
|
|
|
workspaces = create_list();
|
|
|
|
|
|
|
|
uint32_t len = 0;
|
|
|
|
char *res = ipc_single_command(socketfd, IPC_GET_WORKSPACES, NULL, &len);
|
|
|
|
json_object *results = json_tokener_parse(res);
|
2015-12-18 00:30:20 +11:00
|
|
|
if (!results) {
|
|
|
|
free(res);
|
|
|
|
return;
|
|
|
|
}
|
2015-12-14 08:04:54 +11:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < json_object_array_length(results); ++i) {
|
|
|
|
json_object *ws_json = json_object_array_get_idx(results, i);
|
|
|
|
json_object *num, *name, *visible, *focused, *out, *urgent;
|
2015-12-19 11:38:36 +11:00
|
|
|
if (!ws_json) {
|
2015-12-19 11:45:16 +11:00
|
|
|
return;
|
2015-12-19 11:35:27 +11:00
|
|
|
}
|
2015-12-14 08:04:54 +11:00
|
|
|
json_object_object_get_ex(ws_json, "num", &num);
|
|
|
|
json_object_object_get_ex(ws_json, "name", &name);
|
|
|
|
json_object_object_get_ex(ws_json, "visible", &visible);
|
|
|
|
json_object_object_get_ex(ws_json, "focused", &focused);
|
|
|
|
json_object_object_get_ex(ws_json, "output", &out);
|
|
|
|
json_object_object_get_ex(ws_json, "urgent", &urgent);
|
|
|
|
|
|
|
|
if (strcmp(json_object_get_string(out), output) == 0) {
|
|
|
|
struct workspace *ws = malloc(sizeof(struct workspace));
|
|
|
|
ws->num = json_object_get_int(num);
|
|
|
|
ws->name = strdup(json_object_get_string(name));
|
|
|
|
ws->visible = json_object_get_boolean(visible);
|
|
|
|
ws->focused = json_object_get_boolean(focused);
|
|
|
|
ws->urgent = json_object_get_boolean(urgent);
|
|
|
|
list_add(workspaces, ws);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_put(results);
|
|
|
|
free(res);
|
|
|
|
}
|
|
|
|
|
2015-12-16 14:31:35 +11:00
|
|
|
uint32_t parse_color(const char *color) {
|
|
|
|
if (color[0] != '#') {
|
|
|
|
sway_abort("Invalid color %s", color);
|
|
|
|
}
|
|
|
|
char *end;
|
|
|
|
uint32_t res = (uint32_t)strtol(color + 1, &end, 16);
|
|
|
|
if (strlen(color) == 7) {
|
|
|
|
res = (res << 8) | 0xFF;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-12-19 12:15:12 +11:00
|
|
|
uint32_t parse_position(const char *position) {
|
|
|
|
if (strcmp("top", position) == 0) {
|
|
|
|
return DESKTOP_SHELL_PANEL_POSITION_TOP;
|
|
|
|
} else if (strcmp("bottom", position) == 0) {
|
|
|
|
return DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
|
|
|
|
} else if (strcmp("left", position) == 0) {
|
|
|
|
return DESKTOP_SHELL_PANEL_POSITION_LEFT;
|
|
|
|
} else if (strcmp("right", position) == 0) {
|
|
|
|
return DESKTOP_SHELL_PANEL_POSITION_RIGHT;
|
|
|
|
} else {
|
|
|
|
return DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 03:02:35 +11:00
|
|
|
static int margin = 3;
|
|
|
|
static const int ws_hor_padding = 5;
|
|
|
|
static double ws_ver_padding = 1.5;
|
|
|
|
static const int ws_spacing = 1;
|
|
|
|
|
2015-12-16 14:22:22 +11:00
|
|
|
void bar_ipc_init(int outputi, const char *bar_id) {
|
2015-12-14 08:04:54 +11:00
|
|
|
uint32_t len = 0;
|
|
|
|
char *res = ipc_single_command(socketfd, IPC_GET_OUTPUTS, NULL, &len);
|
|
|
|
json_object *outputs = json_tokener_parse(res);
|
|
|
|
json_object *info = json_object_array_get_idx(outputs, outputi);
|
|
|
|
json_object *name;
|
|
|
|
json_object_object_get_ex(info, "name", &name);
|
|
|
|
output = strdup(json_object_get_string(name));
|
|
|
|
free(res);
|
|
|
|
json_object_put(outputs);
|
|
|
|
|
2015-12-16 14:22:22 +11:00
|
|
|
len = strlen(bar_id);
|
|
|
|
res = ipc_single_command(socketfd, IPC_GET_BAR_CONFIG, bar_id, &len);
|
|
|
|
|
|
|
|
json_object *bar_config = json_tokener_parse(res);
|
|
|
|
json_object *tray_output, *mode, *hidden_state, *position, *_status_command;
|
|
|
|
json_object *font, *bar_height, *workspace_buttons, *strip_workspace_numbers;
|
2015-12-16 14:31:35 +11:00
|
|
|
json_object *binding_mode_indicator, *verbose, *_colors;
|
2015-12-16 14:22:22 +11:00
|
|
|
json_object_object_get_ex(bar_config, "tray_output", &tray_output);
|
|
|
|
json_object_object_get_ex(bar_config, "mode", &mode);
|
|
|
|
json_object_object_get_ex(bar_config, "hidden_state", &hidden_state);
|
|
|
|
json_object_object_get_ex(bar_config, "position", &position);
|
|
|
|
json_object_object_get_ex(bar_config, "status_command", &_status_command);
|
|
|
|
json_object_object_get_ex(bar_config, "font", &font);
|
|
|
|
json_object_object_get_ex(bar_config, "bar_height", &bar_height);
|
|
|
|
json_object_object_get_ex(bar_config, "workspace_buttons", &workspace_buttons);
|
|
|
|
json_object_object_get_ex(bar_config, "strip_workspace_numbers", &strip_workspace_numbers);
|
|
|
|
json_object_object_get_ex(bar_config, "binding_mode_indicator", &binding_mode_indicator);
|
|
|
|
json_object_object_get_ex(bar_config, "verbose", &verbose);
|
2015-12-16 14:31:35 +11:00
|
|
|
json_object_object_get_ex(bar_config, "colors", &_colors);
|
|
|
|
|
|
|
|
// TODO: More of these options
|
|
|
|
// TODO: Refactor swaybar into several files, create a bar config struct (shared with compositor?)
|
2015-12-16 21:25:55 +11:00
|
|
|
if (_status_command) {
|
|
|
|
status_command = strdup(json_object_get_string(_status_command));
|
|
|
|
}
|
2015-12-16 14:31:35 +11:00
|
|
|
|
2015-12-19 12:15:12 +11:00
|
|
|
if (position) {
|
|
|
|
desktop_shell_set_panel_position(registry->desktop_shell, parse_position(json_object_get_string(position)));
|
|
|
|
}
|
|
|
|
|
2015-12-20 15:00:09 +11:00
|
|
|
if (bar_height) {
|
2015-12-21 03:02:35 +11:00
|
|
|
int width, height;
|
|
|
|
get_text_size(window, &width, &height, "Test string for measuring purposes");
|
|
|
|
int bar_height_value = json_object_get_int(bar_height);
|
|
|
|
if (bar_height_value > 0) {
|
|
|
|
margin = (bar_height_value - height) / 2;
|
|
|
|
ws_ver_padding = margin - 1.5;
|
2015-12-20 15:00:09 +11:00
|
|
|
}
|
2015-12-21 03:02:35 +11:00
|
|
|
window->height = height + margin * 2;
|
2015-12-20 15:00:09 +11:00
|
|
|
}
|
|
|
|
|
2015-12-16 14:31:35 +11:00
|
|
|
if (_colors) {
|
|
|
|
json_object *background, *statusline, *separator;
|
|
|
|
json_object *focused_workspace_border, *focused_workspace_bg, *focused_workspace_text;
|
|
|
|
json_object *inactive_workspace_border, *inactive_workspace_bg, *inactive_workspace_text;
|
|
|
|
json_object *active_workspace_border, *active_workspace_bg, *active_workspace_text;
|
|
|
|
json_object *urgent_workspace_border, *urgent_workspace_bg, *urgent_workspace_text;
|
|
|
|
json_object *binding_mode_border, *binding_mode_bg, *binding_mode_text;
|
|
|
|
json_object_object_get_ex(_colors, "background", &background);
|
|
|
|
json_object_object_get_ex(_colors, "statusline", &statusline);
|
|
|
|
json_object_object_get_ex(_colors, "separator", &separator);
|
|
|
|
json_object_object_get_ex(_colors, "focused_workspace_border", &focused_workspace_border);
|
|
|
|
json_object_object_get_ex(_colors, "focused_workspace_bg", &focused_workspace_bg);
|
|
|
|
json_object_object_get_ex(_colors, "focused_workspace_text", &focused_workspace_text);
|
|
|
|
json_object_object_get_ex(_colors, "active_workspace_border", &active_workspace_border);
|
|
|
|
json_object_object_get_ex(_colors, "active_workspace_bg", &active_workspace_bg);
|
|
|
|
json_object_object_get_ex(_colors, "active_workspace_text", &active_workspace_text);
|
|
|
|
json_object_object_get_ex(_colors, "inactive_workspace_border", &inactive_workspace_border);
|
|
|
|
json_object_object_get_ex(_colors, "inactive_workspace_bg", &inactive_workspace_bg);
|
|
|
|
json_object_object_get_ex(_colors, "inactive_workspace_text", &inactive_workspace_text);
|
|
|
|
json_object_object_get_ex(_colors, "urgent_workspace_border", &urgent_workspace_border);
|
|
|
|
json_object_object_get_ex(_colors, "urgent_workspace_bg", &urgent_workspace_bg);
|
|
|
|
json_object_object_get_ex(_colors, "urgent_workspace_text", &urgent_workspace_text);
|
|
|
|
json_object_object_get_ex(_colors, "binding_mode_border", &binding_mode_border);
|
|
|
|
json_object_object_get_ex(_colors, "binding_mode_bg", &binding_mode_bg);
|
|
|
|
json_object_object_get_ex(_colors, "binding_mode_text", &binding_mode_text);
|
|
|
|
if (background) colors.background = parse_color(json_object_get_string(background));
|
|
|
|
if (statusline) colors.statusline = parse_color(json_object_get_string(statusline));
|
2015-12-17 00:14:27 +11:00
|
|
|
if (separator) colors.separator = parse_color(json_object_get_string(separator));
|
2015-12-16 21:25:55 +11:00
|
|
|
if (focused_workspace_border) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.focused_workspace.border = parse_color(json_object_get_string(focused_workspace_border));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (focused_workspace_bg) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.focused_workspace.background = parse_color(json_object_get_string(focused_workspace_bg));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (focused_workspace_text) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.focused_workspace.text = parse_color(json_object_get_string(focused_workspace_text));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (active_workspace_border) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.active_workspace.border = parse_color(json_object_get_string(active_workspace_border));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (active_workspace_bg) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.active_workspace.background = parse_color(json_object_get_string(active_workspace_bg));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (active_workspace_text) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.active_workspace.text = parse_color(json_object_get_string(active_workspace_text));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (inactive_workspace_border) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.inactive_workspace.border = parse_color(json_object_get_string(inactive_workspace_border));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (inactive_workspace_bg) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.inactive_workspace.background = parse_color(json_object_get_string(inactive_workspace_bg));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (inactive_workspace_text) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.inactive_workspace.text = parse_color(json_object_get_string(inactive_workspace_text));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (binding_mode_border) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.binding_mode.border = parse_color(json_object_get_string(binding_mode_border));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (binding_mode_bg) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.binding_mode.background = parse_color(json_object_get_string(binding_mode_bg));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
|
|
|
if (binding_mode_text) {
|
2015-12-16 14:31:35 +11:00
|
|
|
colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text));
|
2015-12-16 21:25:55 +11:00
|
|
|
}
|
2015-12-16 14:31:35 +11:00
|
|
|
}
|
|
|
|
|
2015-12-16 14:22:22 +11:00
|
|
|
json_object_put(bar_config);
|
|
|
|
free(res);
|
|
|
|
|
2015-12-14 08:04:54 +11:00
|
|
|
const char *subscribe_json = "[ \"workspace\" ]";
|
|
|
|
len = strlen(subscribe_json);
|
|
|
|
res = ipc_single_command(socketfd, IPC_SUBSCRIBE, subscribe_json, &len);
|
|
|
|
|
|
|
|
ipc_update_workspaces();
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:58:18 +11:00
|
|
|
void render() {
|
2015-12-14 07:40:19 +11:00
|
|
|
// Clear
|
2015-11-30 02:58:18 +11:00
|
|
|
cairo_save(window->cairo);
|
|
|
|
cairo_set_operator(window->cairo, CAIRO_OPERATOR_CLEAR);
|
|
|
|
cairo_paint(window->cairo);
|
|
|
|
cairo_restore(window->cairo);
|
|
|
|
|
2015-12-14 07:40:19 +11:00
|
|
|
// Background
|
2015-11-30 02:58:18 +11:00
|
|
|
cairo_set_source_u32(window->cairo, colors.background);
|
|
|
|
cairo_paint(window->cairo);
|
2015-11-30 07:27:28 +11:00
|
|
|
|
2015-12-14 07:40:19 +11:00
|
|
|
// Command output
|
2015-11-30 07:27:28 +11:00
|
|
|
cairo_set_source_u32(window->cairo, colors.statusline);
|
2015-12-14 00:35:39 +11:00
|
|
|
int width, height;
|
|
|
|
get_text_size(window, &width, &height, "%s", line);
|
|
|
|
|
2015-12-21 03:02:35 +11:00
|
|
|
cairo_move_to(window->cairo, window->width - margin - width, margin);
|
2015-12-14 00:35:39 +11:00
|
|
|
pango_printf(window, "%s", line);
|
2015-12-14 07:40:19 +11:00
|
|
|
|
|
|
|
// Workspaces
|
2015-12-21 03:02:35 +11:00
|
|
|
cairo_set_line_width(window->cairo, 1.0);
|
|
|
|
double x = 0.5;
|
2015-12-14 07:40:19 +11:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < workspaces->length; ++i) {
|
|
|
|
struct workspace *ws = workspaces->items[i];
|
|
|
|
get_text_size(window, &width, &height, "%s", ws->name);
|
|
|
|
struct box_colors box_colors;
|
|
|
|
if (ws->urgent) {
|
|
|
|
box_colors = colors.urgent_workspace;
|
|
|
|
} else if (ws->focused) {
|
|
|
|
box_colors = colors.focused_workspace;
|
|
|
|
} else if (ws->visible) {
|
|
|
|
box_colors = colors.active_workspace;
|
|
|
|
} else {
|
|
|
|
box_colors = colors.inactive_workspace;
|
|
|
|
}
|
2015-12-21 03:02:35 +11:00
|
|
|
|
2015-12-14 07:40:19 +11:00
|
|
|
cairo_set_source_u32(window->cairo, box_colors.background);
|
2015-12-21 03:02:35 +11:00
|
|
|
cairo_rectangle(window->cairo, x, 1.5, width + ws_hor_padding * 2 - 1, height + ws_ver_padding * 2);
|
2015-12-14 07:40:19 +11:00
|
|
|
cairo_fill(window->cairo);
|
|
|
|
|
|
|
|
cairo_set_source_u32(window->cairo, box_colors.border);
|
2015-12-21 03:02:35 +11:00
|
|
|
cairo_rectangle(window->cairo, x, 1.5, width + ws_hor_padding * 2 - 1, height + ws_ver_padding * 2);
|
2015-12-14 07:40:19 +11:00
|
|
|
cairo_stroke(window->cairo);
|
|
|
|
|
|
|
|
cairo_set_source_u32(window->cairo, box_colors.text);
|
2015-12-21 03:02:35 +11:00
|
|
|
cairo_move_to(window->cairo, (int)x + ws_hor_padding, margin);
|
2015-12-14 07:40:19 +11:00
|
|
|
pango_printf(window, "%s", ws->name);
|
|
|
|
|
2015-12-21 03:02:35 +11:00
|
|
|
x += width + ws_hor_padding * 2 + ws_spacing;
|
2015-12-14 07:40:19 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-20 02:24:51 +11:00
|
|
|
void poll_for_update() {
|
|
|
|
fd_set readfds;
|
|
|
|
int activity;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (dirty && window_prerender(window) && window->cairo) {
|
|
|
|
render();
|
|
|
|
window_render(window);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wl_display_dispatch(registry->display) == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirty = false;
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(socketfd, &readfds);
|
|
|
|
FD_SET(pipefd[0], &readfds);
|
|
|
|
|
|
|
|
activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
|
|
|
|
if (activity < 0) {
|
|
|
|
sway_log(L_ERROR, "polling failed: %d", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(socketfd, &readfds)) {
|
|
|
|
sway_log(L_DEBUG, "Got workspace update.");
|
|
|
|
uint32_t len;
|
|
|
|
char *buf = ipc_recv_response(socketfd, &len);
|
|
|
|
free(buf);
|
|
|
|
ipc_update_workspaces();
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status_command && FD_ISSET(pipefd[0], &readfds)) {
|
|
|
|
sway_log(L_DEBUG, "Got update from status command.");
|
|
|
|
fgets(line, sizeof(line), command);
|
|
|
|
int l = strlen(line) - 1;
|
|
|
|
if (line[l] == '\n') {
|
|
|
|
line[l] = '\0';
|
|
|
|
}
|
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:26:50 +11:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
init_log(L_INFO);
|
2015-12-16 14:08:09 +11:00
|
|
|
|
|
|
|
char *socket_path = NULL;
|
|
|
|
char *bar_id = NULL;
|
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{"socket", required_argument, NULL, 's'},
|
|
|
|
{"bar_id", required_argument, NULL, 'b'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0;
|
|
|
|
c = getopt_long(argc, argv, "vs:b:", 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;
|
|
|
|
default:
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 21:13:12 +11:00
|
|
|
if (!bar_id) {
|
|
|
|
sway_abort("No bar_id passed. Provide --bar_id or let sway start swaybar");
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:58:18 +11:00
|
|
|
registry = registry_poll();
|
|
|
|
|
|
|
|
if (!registry->desktop_shell) {
|
|
|
|
sway_abort("swaybar requires the compositor to support the desktop-shell extension.");
|
|
|
|
}
|
|
|
|
|
2015-12-16 14:22:22 +11:00
|
|
|
if (!socket_path) {
|
|
|
|
socket_path = get_socketpath();
|
|
|
|
if (!socket_path) {
|
|
|
|
sway_abort("Unable to retrieve socket path");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
socketfd = ipc_open_socket(socket_path);
|
2015-11-30 02:58:18 +11:00
|
|
|
|
2015-12-16 21:13:12 +11:00
|
|
|
if (argc == optind) {
|
|
|
|
sway_abort("No output index provided");
|
|
|
|
}
|
|
|
|
|
2015-12-16 14:22:22 +11:00
|
|
|
int desired_output = atoi(argv[optind]);
|
2015-11-30 02:58:18 +11:00
|
|
|
struct output_state *output = registry->outputs->items[desired_output];
|
2015-12-21 03:02:35 +11:00
|
|
|
|
2015-12-20 15:00:09 +11:00
|
|
|
window = window_setup(registry, output->width, 30, false);
|
|
|
|
if (!window) {
|
|
|
|
sway_abort("Failed to create window.");
|
|
|
|
}
|
|
|
|
desktop_shell_set_panel(registry->desktop_shell, output->output, window->surface);
|
2015-12-16 14:22:22 +11:00
|
|
|
|
|
|
|
bar_ipc_init(desired_output, bar_id);
|
|
|
|
|
2015-12-16 21:25:55 +11:00
|
|
|
if (status_command) {
|
2015-12-18 13:02:35 +11:00
|
|
|
pipe(pipefd);
|
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
close(pipefd[0]);
|
|
|
|
dup2(pipefd[1], STDOUT_FILENO);
|
|
|
|
close(pipefd[1]);
|
|
|
|
char *const cmd[] = {
|
|
|
|
"sh",
|
|
|
|
"-c",
|
|
|
|
status_command,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
execvp(cmd[0], cmd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(pipefd[1]);
|
|
|
|
command = fdopen(pipefd[0], "r");
|
2015-12-16 21:25:55 +11:00
|
|
|
line[0] = '\0';
|
|
|
|
}
|
2015-12-16 14:22:22 +11:00
|
|
|
|
2015-12-20 02:24:51 +11:00
|
|
|
poll_for_update();
|
2015-11-30 02:58:18 +11:00
|
|
|
|
|
|
|
window_teardown(window);
|
|
|
|
registry_teardown(registry);
|
2015-12-18 13:02:35 +11:00
|
|
|
fclose(command);
|
|
|
|
// terminate status_command process
|
|
|
|
kill(pid, SIGTERM);
|
|
|
|
close(pipefd[0]);
|
2015-11-30 02:58:18 +11:00
|
|
|
|
2015-11-30 02:26:50 +11:00
|
|
|
return 0;
|
|
|
|
}
|