2018-09-03 09:08:49 +02:00
|
|
|
#define _POSIX_C_SOURCE 200809
|
2018-03-29 06:07:35 +02:00
|
|
|
#include <limits.h>
|
2018-03-29 05:56:02 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <json-c/json.h>
|
|
|
|
#include "swaybar/config.h"
|
|
|
|
#include "swaybar/ipc.h"
|
2018-12-09 16:10:41 +01:00
|
|
|
#include "config.h"
|
2018-03-29 05:56:02 +02:00
|
|
|
#include "ipc-client.h"
|
2018-10-08 17:40:13 +02:00
|
|
|
#include "list.h"
|
2019-01-20 19:51:12 +01:00
|
|
|
#include "log.h"
|
2018-03-29 05:56:02 +02:00
|
|
|
|
2018-03-31 04:02:55 +02:00
|
|
|
void ipc_send_workspace_command(struct swaybar *bar, const char *ws) {
|
|
|
|
const char *fmt = "workspace \"%s\"";
|
|
|
|
uint32_t size = snprintf(NULL, 0, fmt, ws);
|
2019-01-16 11:18:46 +01:00
|
|
|
char *command = malloc(sizeof(char) * (size + 1));
|
2018-03-31 04:02:55 +02:00
|
|
|
snprintf(command, size, fmt, ws);
|
|
|
|
ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, command, &size);
|
2019-01-16 02:57:53 +01:00
|
|
|
free(command);
|
2018-03-31 04:02:55 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 05:56:02 +02:00
|
|
|
char *parse_font(const char *font) {
|
|
|
|
char *new_font = NULL;
|
|
|
|
if (strncmp("pango:", font, 6) == 0) {
|
|
|
|
font += 6;
|
|
|
|
}
|
|
|
|
new_font = strdup(font);
|
|
|
|
return new_font;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ipc_parse_colors(
|
|
|
|
struct swaybar_config *config, json_object *colors) {
|
|
|
|
json_object *background, *statusline, *separator;
|
|
|
|
json_object *focused_background, *focused_statusline, *focused_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_background", &focused_background);
|
|
|
|
json_object_object_get_ex(colors, "focused_statusline", &focused_statusline);
|
|
|
|
json_object_object_get_ex(colors, "focused_separator", &focused_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) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.background = parse_color(
|
|
|
|
json_object_get_string(background));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (statusline) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.statusline = parse_color(
|
|
|
|
json_object_get_string(statusline));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (separator) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.separator = parse_color(
|
|
|
|
json_object_get_string(separator));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (focused_background) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.focused_background = parse_color(
|
|
|
|
json_object_get_string(focused_background));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (focused_statusline) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.focused_statusline = parse_color(
|
|
|
|
json_object_get_string(focused_statusline));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (focused_separator) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.focused_separator = parse_color(
|
|
|
|
json_object_get_string(focused_separator));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (focused_workspace_border) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.focused_workspace.border = parse_color(
|
|
|
|
json_object_get_string(focused_workspace_border));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (focused_workspace_bg) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.focused_workspace.background = parse_color(
|
|
|
|
json_object_get_string(focused_workspace_bg));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (focused_workspace_text) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.focused_workspace.text = parse_color(
|
|
|
|
json_object_get_string(focused_workspace_text));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (active_workspace_border) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.active_workspace.border = parse_color(
|
|
|
|
json_object_get_string(active_workspace_border));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (active_workspace_bg) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.active_workspace.background = parse_color(
|
|
|
|
json_object_get_string(active_workspace_bg));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (active_workspace_text) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.active_workspace.text = parse_color(
|
|
|
|
json_object_get_string(active_workspace_text));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (inactive_workspace_border) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.inactive_workspace.border = parse_color(
|
|
|
|
json_object_get_string(inactive_workspace_border));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (inactive_workspace_bg) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.inactive_workspace.background = parse_color(
|
|
|
|
json_object_get_string(inactive_workspace_bg));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (inactive_workspace_text) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.inactive_workspace.text = parse_color(
|
|
|
|
json_object_get_string(inactive_workspace_text));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
2018-07-17 00:29:43 +02:00
|
|
|
if (urgent_workspace_border) {
|
|
|
|
config->colors.urgent_workspace.border = parse_color(
|
|
|
|
json_object_get_string(urgent_workspace_border));
|
|
|
|
}
|
|
|
|
if (urgent_workspace_bg) {
|
|
|
|
config->colors.urgent_workspace.background = parse_color(
|
|
|
|
json_object_get_string(urgent_workspace_bg));
|
|
|
|
}
|
|
|
|
if (urgent_workspace_text) {
|
|
|
|
config->colors.urgent_workspace.text = parse_color(
|
|
|
|
json_object_get_string(urgent_workspace_text));
|
|
|
|
}
|
2018-03-29 05:56:02 +02:00
|
|
|
if (binding_mode_border) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.binding_mode.border = parse_color(
|
|
|
|
json_object_get_string(binding_mode_border));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (binding_mode_bg) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.binding_mode.background = parse_color(
|
|
|
|
json_object_get_string(binding_mode_bg));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
if (binding_mode_text) {
|
2018-03-29 17:58:54 +02:00
|
|
|
config->colors.binding_mode.text = parse_color(
|
|
|
|
json_object_get_string(binding_mode_text));
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 19:42:25 +02:00
|
|
|
static bool ipc_parse_config(
|
2018-03-29 05:56:02 +02:00
|
|
|
struct swaybar_config *config, const char *payload) {
|
|
|
|
json_object *bar_config = json_tokener_parse(payload);
|
2018-09-24 19:42:25 +02:00
|
|
|
json_object *success;
|
|
|
|
if (json_object_object_get_ex(bar_config, "success", &success)
|
|
|
|
&& !json_object_get_boolean(success)) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "No bar with that ID. Use 'swaymsg -t get_bar_config to get the available bar configs.");
|
2018-09-24 19:42:25 +02:00
|
|
|
json_object_put(bar_config);
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-12 21:32:48 +02:00
|
|
|
json_object *markup, *mode, *hidden_state, *position, *status_command;
|
2018-11-28 17:23:48 +01:00
|
|
|
json_object *font, *gaps, *bar_height, *wrap_scroll, *workspace_buttons;
|
2018-11-17 17:11:28 +01:00
|
|
|
json_object *strip_workspace_numbers, *strip_workspace_name;
|
|
|
|
json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol;
|
2019-01-11 06:12:24 +01:00
|
|
|
json_object *outputs, *bindings, *status_padding, *status_edge_padding;
|
2018-03-29 05:56:02 +02:00
|
|
|
json_object_object_get_ex(bar_config, "mode", &mode);
|
2018-10-12 21:32:48 +02:00
|
|
|
json_object_object_get_ex(bar_config, "hidden_state", &hidden_state);
|
2018-03-29 05:56:02 +02:00
|
|
|
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);
|
2018-11-28 17:23:48 +01:00
|
|
|
json_object_object_get_ex(bar_config, "gaps", &gaps);
|
2018-03-29 05:56:02 +02:00
|
|
|
json_object_object_get_ex(bar_config, "bar_height", &bar_height);
|
|
|
|
json_object_object_get_ex(bar_config, "wrap_scroll", &wrap_scroll);
|
|
|
|
json_object_object_get_ex(bar_config, "workspace_buttons", &workspace_buttons);
|
|
|
|
json_object_object_get_ex(bar_config, "strip_workspace_numbers", &strip_workspace_numbers);
|
2018-11-17 17:11:28 +01:00
|
|
|
json_object_object_get_ex(bar_config, "strip_workspace_name", &strip_workspace_name);
|
2018-03-29 05:56:02 +02:00
|
|
|
json_object_object_get_ex(bar_config, "binding_mode_indicator", &binding_mode_indicator);
|
|
|
|
json_object_object_get_ex(bar_config, "verbose", &verbose);
|
|
|
|
json_object_object_get_ex(bar_config, "separator_symbol", &sep_symbol);
|
|
|
|
json_object_object_get_ex(bar_config, "colors", &colors);
|
|
|
|
json_object_object_get_ex(bar_config, "outputs", &outputs);
|
|
|
|
json_object_object_get_ex(bar_config, "pango_markup", &markup);
|
2018-10-08 17:40:13 +02:00
|
|
|
json_object_object_get_ex(bar_config, "bindings", &bindings);
|
2019-01-11 05:43:45 +01:00
|
|
|
json_object_object_get_ex(bar_config, "status_padding", &status_padding);
|
2019-01-11 06:12:24 +01:00
|
|
|
json_object_object_get_ex(bar_config, "status_edge_padding",
|
|
|
|
&status_edge_padding);
|
2018-03-29 05:56:02 +02:00
|
|
|
if (status_command) {
|
|
|
|
free(config->status_command);
|
|
|
|
config->status_command = strdup(json_object_get_string(status_command));
|
|
|
|
}
|
|
|
|
if (position) {
|
|
|
|
config->position = parse_position(json_object_get_string(position));
|
|
|
|
}
|
|
|
|
if (font) {
|
|
|
|
free(config->font);
|
|
|
|
config->font = parse_font(json_object_get_string(font));
|
|
|
|
}
|
|
|
|
if (sep_symbol) {
|
|
|
|
free(config->sep_symbol);
|
|
|
|
config->sep_symbol = strdup(json_object_get_string(sep_symbol));
|
|
|
|
}
|
|
|
|
if (strip_workspace_numbers) {
|
|
|
|
config->strip_workspace_numbers = json_object_get_boolean(strip_workspace_numbers);
|
|
|
|
}
|
2018-11-17 17:11:28 +01:00
|
|
|
if (strip_workspace_name) {
|
|
|
|
config->strip_workspace_name = json_object_get_boolean(strip_workspace_name);
|
|
|
|
}
|
2018-03-29 05:56:02 +02:00
|
|
|
if (binding_mode_indicator) {
|
|
|
|
config->binding_mode_indicator = json_object_get_boolean(binding_mode_indicator);
|
|
|
|
}
|
|
|
|
if (wrap_scroll) {
|
|
|
|
config->wrap_scroll = json_object_get_boolean(wrap_scroll);
|
|
|
|
}
|
|
|
|
if (workspace_buttons) {
|
|
|
|
config->workspace_buttons = json_object_get_boolean(workspace_buttons);
|
|
|
|
}
|
|
|
|
if (bar_height) {
|
|
|
|
config->height = json_object_get_int(bar_height);
|
|
|
|
}
|
2019-01-11 05:43:45 +01:00
|
|
|
if (status_padding) {
|
|
|
|
config->status_padding = json_object_get_int(status_padding);
|
|
|
|
}
|
2019-01-11 06:12:24 +01:00
|
|
|
if (status_edge_padding) {
|
|
|
|
config->status_edge_padding = json_object_get_int(status_edge_padding);
|
|
|
|
}
|
2018-11-28 17:23:48 +01:00
|
|
|
if (gaps) {
|
|
|
|
json_object *top = json_object_object_get(gaps, "top");
|
|
|
|
if (top) {
|
|
|
|
config->gaps.top = json_object_get_int(top);
|
|
|
|
}
|
|
|
|
json_object *right = json_object_object_get(gaps, "right");
|
|
|
|
if (right) {
|
|
|
|
config->gaps.right = json_object_get_int(right);
|
|
|
|
}
|
|
|
|
json_object *bottom = json_object_object_get(gaps, "bottom");
|
|
|
|
if (bottom) {
|
|
|
|
config->gaps.bottom = json_object_get_int(bottom);
|
|
|
|
}
|
|
|
|
json_object *left = json_object_object_get(gaps, "left");
|
|
|
|
if (left) {
|
|
|
|
config->gaps.left = json_object_get_int(left);
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 05:56:02 +02:00
|
|
|
if (markup) {
|
|
|
|
config->pango_markup = json_object_get_boolean(markup);
|
|
|
|
}
|
2018-10-08 17:40:13 +02:00
|
|
|
if (bindings) {
|
|
|
|
int length = json_object_array_length(bindings);
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
json_object *bindobj = json_object_array_get_idx(bindings, i);
|
|
|
|
struct swaybar_binding *binding =
|
|
|
|
calloc(1, sizeof(struct swaybar_binding));
|
|
|
|
binding->button = json_object_get_int(
|
2019-01-10 18:43:10 +01:00
|
|
|
json_object_object_get(bindobj, "event_code"));
|
2018-10-08 17:40:13 +02:00
|
|
|
binding->command = strdup(json_object_get_string(
|
|
|
|
json_object_object_get(bindobj, "command")));
|
|
|
|
binding->release = json_object_get_boolean(
|
|
|
|
json_object_object_get(bindobj, "release"));
|
|
|
|
list_add(config->bindings, binding);
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 21:32:48 +02:00
|
|
|
if (hidden_state) {
|
|
|
|
free(config->hidden_state);
|
|
|
|
config->hidden_state = strdup(json_object_get_string(hidden_state));
|
|
|
|
}
|
|
|
|
if (mode) {
|
|
|
|
free(config->mode);
|
|
|
|
config->mode = strdup(json_object_get_string(mode));
|
|
|
|
}
|
2018-03-29 05:56:02 +02:00
|
|
|
|
|
|
|
struct config_output *output, *tmp;
|
|
|
|
wl_list_for_each_safe(output, tmp, &config->outputs, link) {
|
|
|
|
wl_list_remove(&output->link);
|
|
|
|
free(output->name);
|
|
|
|
free(output);
|
|
|
|
}
|
|
|
|
if (outputs) {
|
|
|
|
int length = json_object_array_length(outputs);
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
json_object *output = json_object_array_get_idx(outputs, i);
|
|
|
|
const char *name = json_object_get_string(output);
|
|
|
|
if (strcmp("*", name) == 0) {
|
2018-03-30 06:30:38 +02:00
|
|
|
config->all_outputs = true;
|
2018-03-29 05:56:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
struct config_output *coutput = calloc(
|
|
|
|
1, sizeof(struct config_output));
|
|
|
|
coutput->name = strdup(name);
|
2018-03-29 06:07:35 +02:00
|
|
|
coutput->index = SIZE_MAX;
|
2018-03-29 05:56:02 +02:00
|
|
|
wl_list_insert(&config->outputs, &coutput->link);
|
|
|
|
}
|
2018-03-30 06:30:38 +02:00
|
|
|
} else {
|
|
|
|
config->all_outputs = true;
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (colors) {
|
|
|
|
ipc_parse_colors(config, colors);
|
|
|
|
}
|
|
|
|
|
2018-12-09 16:10:41 +01:00
|
|
|
#if HAVE_TRAY
|
|
|
|
json_object *tray_outputs, *tray_padding, *tray_bindings, *icon_theme;
|
|
|
|
|
|
|
|
if ((json_object_object_get_ex(bar_config, "tray_outputs", &tray_outputs))) {
|
|
|
|
config->tray_outputs = create_list();
|
|
|
|
int length = json_object_array_length(tray_outputs);
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
json_object *o = json_object_array_get_idx(tray_outputs, i);
|
|
|
|
list_add(config->tray_outputs, strdup(json_object_get_string(o)));
|
|
|
|
}
|
2018-12-11 17:27:39 +01:00
|
|
|
config->tray_hidden = strcmp(config->tray_outputs->items[0], "none") == 0;
|
2018-12-09 16:10:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((json_object_object_get_ex(bar_config, "tray_padding", &tray_padding))) {
|
|
|
|
config->tray_padding = json_object_get_int(tray_padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((json_object_object_get_ex(bar_config, "tray_bindings", &tray_bindings))) {
|
|
|
|
int length = json_object_array_length(tray_bindings);
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
json_object *bind = json_object_array_get_idx(tray_bindings, i);
|
2019-01-16 03:25:28 +01:00
|
|
|
struct tray_binding *binding =
|
|
|
|
calloc(1, sizeof(struct tray_binding));
|
|
|
|
binding->button = json_object_get_int(
|
|
|
|
json_object_object_get(bind, "event_code"));
|
|
|
|
binding->command = strdup(json_object_get_string(
|
|
|
|
json_object_object_get(bind, "command")));
|
|
|
|
wl_list_insert(&config->tray_bindings, &binding->link);
|
2018-12-09 16:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((json_object_object_get_ex(bar_config, "icon_theme", &icon_theme))) {
|
|
|
|
config->icon_theme = strdup(json_object_get_string(icon_theme));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-29 05:56:02 +02:00
|
|
|
json_object_put(bar_config);
|
2018-09-24 19:42:25 +02:00
|
|
|
return true;
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 22:05:43 +02:00
|
|
|
bool ipc_get_workspaces(struct swaybar *bar) {
|
2018-03-29 06:21:05 +02:00
|
|
|
struct swaybar_output *output;
|
|
|
|
wl_list_for_each(output, &bar->outputs, link) {
|
|
|
|
free_workspaces(&output->workspaces);
|
2018-03-29 22:02:59 +02:00
|
|
|
output->focused = false;
|
2018-03-29 06:21:05 +02:00
|
|
|
}
|
|
|
|
uint32_t len = 0;
|
|
|
|
char *res = ipc_single_command(bar->ipc_socketfd,
|
|
|
|
IPC_GET_WORKSPACES, NULL, &len);
|
|
|
|
json_object *results = json_tokener_parse(res);
|
|
|
|
if (!results) {
|
|
|
|
free(res);
|
2018-10-12 22:05:43 +02:00
|
|
|
return false;
|
2018-03-29 06:21:05 +02:00
|
|
|
}
|
2018-10-12 22:05:43 +02:00
|
|
|
|
|
|
|
bar->visible_by_urgency = false;
|
2018-03-29 06:21:05 +02:00
|
|
|
size_t length = json_object_array_length(results);
|
|
|
|
json_object *ws_json;
|
|
|
|
json_object *num, *name, *visible, *focused, *out, *urgent;
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
ws_json = json_object_array_get_idx(results, i);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
wl_list_for_each(output, &bar->outputs, link) {
|
|
|
|
const char *ws_output = json_object_get_string(out);
|
|
|
|
if (strcmp(ws_output, output->name) == 0) {
|
|
|
|
struct swaybar_workspace *ws =
|
|
|
|
calloc(1, sizeof(struct swaybar_workspace));
|
|
|
|
ws->num = json_object_get_int(num);
|
|
|
|
ws->name = strdup(json_object_get_string(name));
|
2018-11-17 17:11:28 +01:00
|
|
|
ws->label = strdup(ws->name);
|
|
|
|
// ws->num will be -1 if workspace name doesn't begin with int.
|
|
|
|
if (ws->num != -1) {
|
|
|
|
size_t len_offset = numlen(ws->num);
|
|
|
|
if (bar->config->strip_workspace_name) {
|
|
|
|
free(ws->label);
|
|
|
|
ws->label = malloc(len_offset + 1 * sizeof(char));
|
|
|
|
ws->label[len_offset] = '\0';
|
|
|
|
strncpy(ws->label, ws->name, len_offset);
|
|
|
|
} else if (bar->config->strip_workspace_numbers) {
|
|
|
|
len_offset += ws->label[len_offset] == ':';
|
|
|
|
if (strlen(ws->name) > len_offset) {
|
|
|
|
free(ws->label);
|
|
|
|
// Strip number prefix [1-?:] using len_offset.
|
|
|
|
ws->label = strdup(ws->name + len_offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 06:21:05 +02:00
|
|
|
ws->visible = json_object_get_boolean(visible);
|
|
|
|
ws->focused = json_object_get_boolean(focused);
|
|
|
|
if (ws->focused) {
|
|
|
|
output->focused = true;
|
|
|
|
}
|
|
|
|
ws->urgent = json_object_get_boolean(urgent);
|
2018-10-12 22:05:43 +02:00
|
|
|
if (ws->urgent) {
|
|
|
|
bar->visible_by_urgency = true;
|
|
|
|
}
|
2018-10-25 13:23:48 +02:00
|
|
|
wl_list_insert(output->workspaces.prev, &ws->link);
|
2018-03-29 06:21:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 22:02:59 +02:00
|
|
|
json_object_put(results);
|
2018-03-29 06:21:05 +02:00
|
|
|
free(res);
|
2018-10-12 22:05:43 +02:00
|
|
|
return determine_bar_visibility(bar, false);
|
2018-03-29 06:21:05 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 06:07:35 +02:00
|
|
|
static void ipc_get_outputs(struct swaybar *bar) {
|
|
|
|
uint32_t len = 0;
|
|
|
|
char *res = ipc_single_command(bar->ipc_socketfd,
|
|
|
|
IPC_GET_OUTPUTS, NULL, &len);
|
|
|
|
json_object *outputs = json_tokener_parse(res);
|
|
|
|
for (size_t i = 0; i < json_object_array_length(outputs); ++i) {
|
|
|
|
json_object *output = json_object_array_get_idx(outputs, i);
|
|
|
|
json_object *output_name, *output_active;
|
|
|
|
json_object_object_get_ex(output, "name", &output_name);
|
|
|
|
json_object_object_get_ex(output, "active", &output_active);
|
|
|
|
const char *name = json_object_get_string(output_name);
|
|
|
|
bool active = json_object_get_boolean(output_active);
|
|
|
|
if (!active) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-30 06:30:38 +02:00
|
|
|
if (bar->config->all_outputs) {
|
2018-04-24 23:04:19 +02:00
|
|
|
struct config_output *coutput =
|
|
|
|
calloc(1, sizeof(struct config_output));
|
2018-03-29 06:07:35 +02:00
|
|
|
coutput->name = strdup(name);
|
|
|
|
coutput->index = i;
|
|
|
|
wl_list_insert(&bar->config->outputs, &coutput->link);
|
|
|
|
} else {
|
|
|
|
struct config_output *coutput;
|
|
|
|
wl_list_for_each(coutput, &bar->config->outputs, link) {
|
|
|
|
if (strcmp(name, coutput->name) == 0) {
|
|
|
|
coutput->index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 06:21:05 +02:00
|
|
|
json_object_put(outputs);
|
2018-03-29 06:07:35 +02:00
|
|
|
free(res);
|
|
|
|
}
|
|
|
|
|
2018-10-08 17:40:13 +02:00
|
|
|
void ipc_execute_binding(struct swaybar *bar, struct swaybar_binding *bind) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Executing binding for button %u (release=%d): `%s`",
|
2018-10-09 14:12:02 +02:00
|
|
|
bind->button, bind->release, bind->command);
|
2018-10-08 17:40:13 +02:00
|
|
|
uint32_t len = strlen(bind->command);
|
|
|
|
free(ipc_single_command(bar->ipc_socketfd,
|
|
|
|
IPC_COMMAND, bind->command, &len));
|
|
|
|
}
|
|
|
|
|
2018-09-30 16:09:09 +02:00
|
|
|
bool ipc_initialize(struct swaybar *bar) {
|
|
|
|
uint32_t len = strlen(bar->id);
|
2018-03-29 05:56:02 +02:00
|
|
|
char *res = ipc_single_command(bar->ipc_socketfd,
|
2018-09-30 16:09:09 +02:00
|
|
|
IPC_GET_BAR_CONFIG, bar->id, &len);
|
2018-09-24 19:42:25 +02:00
|
|
|
if (!ipc_parse_config(bar->config, res)) {
|
|
|
|
free(res);
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-29 05:56:02 +02:00
|
|
|
free(res);
|
2018-03-29 06:07:35 +02:00
|
|
|
ipc_get_outputs(bar);
|
2018-03-29 16:38:17 +02:00
|
|
|
|
2018-09-30 16:09:55 +02:00
|
|
|
struct swaybar_config *config = bar->config;
|
|
|
|
char subscribe[128]; // suitably large buffer
|
|
|
|
len = snprintf(subscribe, 128,
|
2018-10-12 22:14:52 +02:00
|
|
|
"[ \"barconfig_update\" , \"bar_state_update\" %s %s ]",
|
2018-09-30 16:09:55 +02:00
|
|
|
config->binding_mode_indicator ? ", \"mode\"" : "",
|
|
|
|
config->workspace_buttons ? ", \"workspace\"" : "");
|
2018-03-29 16:38:17 +02:00
|
|
|
free(ipc_single_command(bar->ipc_event_socketfd,
|
|
|
|
IPC_SUBSCRIBE, subscribe, &len));
|
2018-09-24 19:42:25 +02:00
|
|
|
return true;
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 22:14:52 +02:00
|
|
|
static bool handle_bar_state_update(struct swaybar *bar, json_object *event) {
|
|
|
|
json_object *json_id;
|
|
|
|
json_object_object_get_ex(event, "id", &json_id);
|
|
|
|
const char *id = json_object_get_string(json_id);
|
|
|
|
if (strcmp(id, bar->id) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object *visible_by_modifier;
|
|
|
|
json_object_object_get_ex(event, "visible_by_modifier", &visible_by_modifier);
|
|
|
|
bar->visible_by_modifier = json_object_get_boolean(visible_by_modifier);
|
|
|
|
return determine_bar_visibility(bar, false);
|
|
|
|
}
|
|
|
|
|
2018-10-12 21:32:48 +02:00
|
|
|
static bool handle_barconfig_update(struct swaybar *bar,
|
|
|
|
json_object *json_config) {
|
|
|
|
json_object *json_id;
|
|
|
|
json_object_object_get_ex(json_config, "id", &json_id);
|
|
|
|
const char *id = json_object_get_string(json_id);
|
|
|
|
if (strcmp(id, bar->id) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct swaybar_config *config = bar->config;
|
|
|
|
|
|
|
|
json_object *json_state;
|
|
|
|
json_object_object_get_ex(json_config, "hidden_state", &json_state);
|
|
|
|
const char *new_state = json_object_get_string(json_state);
|
|
|
|
char *old_state = config->hidden_state;
|
|
|
|
if (strcmp(new_state, old_state) != 0) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Changing bar hidden state to %s", new_state);
|
2018-10-12 21:32:48 +02:00
|
|
|
free(old_state);
|
|
|
|
config->hidden_state = strdup(new_state);
|
|
|
|
return determine_bar_visibility(bar, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(config->mode);
|
|
|
|
json_object *json_mode;
|
|
|
|
json_object_object_get_ex(json_config, "mode", &json_mode);
|
|
|
|
config->mode = strdup(json_object_get_string(json_mode));
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_DEBUG, "Changing bar mode to %s", config->mode);
|
2018-10-12 21:32:48 +02:00
|
|
|
|
2018-11-28 17:23:48 +01:00
|
|
|
json_object *gaps;
|
|
|
|
json_object_object_get_ex(json_config, "gaps", &gaps);
|
|
|
|
if (gaps) {
|
|
|
|
json_object *top = json_object_object_get(gaps, "top");
|
|
|
|
if (top) {
|
|
|
|
config->gaps.top = json_object_get_int(top);
|
|
|
|
}
|
|
|
|
json_object *right = json_object_object_get(gaps, "right");
|
|
|
|
if (right) {
|
|
|
|
config->gaps.right = json_object_get_int(right);
|
|
|
|
}
|
|
|
|
json_object *bottom = json_object_object_get(gaps, "bottom");
|
|
|
|
if (bottom) {
|
|
|
|
config->gaps.bottom = json_object_get_int(bottom);
|
|
|
|
}
|
|
|
|
json_object *left = json_object_object_get(gaps, "left");
|
|
|
|
if (left) {
|
|
|
|
config->gaps.left = json_object_get_int(left);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 21:32:48 +02:00
|
|
|
return determine_bar_visibility(bar, true);
|
|
|
|
}
|
|
|
|
|
2018-03-31 20:39:18 +02:00
|
|
|
bool handle_ipc_readable(struct swaybar *bar) {
|
2018-03-29 05:56:02 +02:00
|
|
|
struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd);
|
2018-03-29 16:38:17 +02:00
|
|
|
if (!resp) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-12 21:28:04 +02:00
|
|
|
|
|
|
|
json_object *result = json_tokener_parse(resp->payload);
|
|
|
|
if (!result) {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "failed to parse payload as json");
|
2018-10-12 21:28:04 +02:00
|
|
|
free_ipc_response(resp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bar_is_dirty = true;
|
2018-03-29 16:38:17 +02:00
|
|
|
switch (resp->type) {
|
|
|
|
case IPC_EVENT_WORKSPACE:
|
2018-10-12 22:05:43 +02:00
|
|
|
bar_is_dirty = ipc_get_workspaces(bar);
|
2018-04-10 22:01:22 +02:00
|
|
|
break;
|
2018-03-29 16:38:17 +02:00
|
|
|
case IPC_EVENT_MODE: {
|
2018-03-29 17:58:54 +02:00
|
|
|
json_object *json_change, *json_pango_markup;
|
2018-03-29 16:38:17 +02:00
|
|
|
if (json_object_object_get_ex(result, "change", &json_change)) {
|
|
|
|
const char *change = json_object_get_string(json_change);
|
2018-10-12 21:23:01 +02:00
|
|
|
free(bar->mode);
|
|
|
|
bar->mode = strcmp(change, "default") != 0 ? strdup(change) : NULL;
|
2018-03-29 16:38:17 +02:00
|
|
|
} else {
|
2019-01-20 19:51:12 +01:00
|
|
|
sway_log(SWAY_ERROR, "failed to parse response");
|
2018-10-12 21:28:04 +02:00
|
|
|
bar_is_dirty = false;
|
|
|
|
break;
|
2018-03-29 16:38:17 +02:00
|
|
|
}
|
2018-03-29 17:58:54 +02:00
|
|
|
if (json_object_object_get_ex(result,
|
|
|
|
"pango_markup", &json_pango_markup)) {
|
2018-10-12 21:23:01 +02:00
|
|
|
bar->mode_pango_markup = json_object_get_boolean(json_pango_markup);
|
2018-03-29 17:58:54 +02:00
|
|
|
}
|
2018-03-29 16:38:17 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-10-12 21:32:48 +02:00
|
|
|
case IPC_EVENT_BARCONFIG_UPDATE:
|
|
|
|
bar_is_dirty = handle_barconfig_update(bar, result);
|
|
|
|
break;
|
2018-10-12 22:14:52 +02:00
|
|
|
case IPC_EVENT_BAR_STATE_UPDATE:
|
|
|
|
bar_is_dirty = handle_bar_state_update(bar, result);
|
|
|
|
break;
|
2018-03-29 16:38:17 +02:00
|
|
|
default:
|
2018-10-12 21:28:04 +02:00
|
|
|
bar_is_dirty = false;
|
|
|
|
break;
|
2018-03-29 16:38:17 +02:00
|
|
|
}
|
2018-10-12 21:28:04 +02:00
|
|
|
json_object_put(result);
|
2018-03-29 05:56:02 +02:00
|
|
|
free_ipc_response(resp);
|
2018-10-12 21:28:04 +02:00
|
|
|
return bar_is_dirty;
|
2018-03-29 05:56:02 +02:00
|
|
|
}
|