swaybar: move ipc stuff to ipc.{h,c}

master
Mikkel Oscar Lyderik 9 years ago
parent a6349a2444
commit fcc47cb3bd

@ -12,6 +12,7 @@ add_executable(swaybar
render.c
state.c
status_line.c
ipc.c
)
target_link_libraries(swaybar

@ -45,19 +45,15 @@ struct swaybar_config *init_config() {
struct swaybar_config *config = calloc(1, sizeof(struct swaybar_config));
config->status_command = NULL;
config->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM;
config->font = NULL;
config->font = strdup("monospace 10");
config->mode = NULL;
config->sep_symbol = NULL;
config->strip_workspace_numbers = false;
config->binding_mode_indicator = true;
config->workspace_buttons = true;
/* layout */
config->margin = 3;
config->ws_horizontal_padding = 5;
config->ws_vertical_padding = 1.5;
config->ws_spacing = 1;
config->text_height = 30;
/* height */
config->height = 0;
/* colors */
config->colors.background = 0x000000FF;

@ -26,11 +26,7 @@ struct swaybar_config {
bool binding_mode_indicator;
bool workspace_buttons;
int margin;
int ws_horizontal_padding;
double ws_vertical_padding;
int ws_spacing;
int text_height;
int height;
struct {
uint32_t background;

@ -0,0 +1,259 @@
#include <string.h>
#include <json-c/json.h>
#include "ipc-client.h"
#include "list.h"
#include "log.h"
#include "config.h"
#include "ipc.h"
static void ipc_parse_config(struct swaybar_config *config, const char *payload) {
json_object *bar_config = json_tokener_parse(payload);
json_object *tray_output, *mode, *hidden_state, *position, *status_command;
json_object *font, *bar_height, *workspace_buttons, *strip_workspace_numbers;
json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol;
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);
json_object_object_get_ex(bar_config, "separator_symbol", &sep_symbol);
json_object_object_get_ex(bar_config, "colors", &colors);
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);
}
if (binding_mode_indicator) {
config->binding_mode_indicator = json_object_get_boolean(binding_mode_indicator);
}
if (workspace_buttons) {
config->workspace_buttons = json_object_get_boolean(workspace_buttons);
}
if (bar_height) {
config->height = json_object_get_int(bar_height);
}
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) {
config->colors.background = parse_color(json_object_get_string(background));
}
if (statusline) {
config->colors.statusline = parse_color(json_object_get_string(statusline));
}
if (separator) {
config->colors.separator = parse_color(json_object_get_string(separator));
}
if (focused_workspace_border) {
config->colors.focused_workspace.border = parse_color(json_object_get_string(focused_workspace_border));
}
if (focused_workspace_bg) {
config->colors.focused_workspace.background = parse_color(json_object_get_string(focused_workspace_bg));
}
if (focused_workspace_text) {
config->colors.focused_workspace.text = parse_color(json_object_get_string(focused_workspace_text));
}
if (active_workspace_border) {
config->colors.active_workspace.border = parse_color(json_object_get_string(active_workspace_border));
}
if (active_workspace_bg) {
config->colors.active_workspace.background = parse_color(json_object_get_string(active_workspace_bg));
}
if (active_workspace_text) {
config->colors.active_workspace.text = parse_color(json_object_get_string(active_workspace_text));
}
if (inactive_workspace_border) {
config->colors.inactive_workspace.border = parse_color(json_object_get_string(inactive_workspace_border));
}
if (inactive_workspace_bg) {
config->colors.inactive_workspace.background = parse_color(json_object_get_string(inactive_workspace_bg));
}
if (inactive_workspace_text) {
config->colors.inactive_workspace.text = parse_color(json_object_get_string(inactive_workspace_text));
}
if (binding_mode_border) {
config->colors.binding_mode.border = parse_color(json_object_get_string(binding_mode_border));
}
if (binding_mode_bg) {
config->colors.binding_mode.background = parse_color(json_object_get_string(binding_mode_bg));
}
if (binding_mode_text) {
config->colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text));
}
}
json_object_put(bar_config);
}
static void ipc_update_workspaces(struct swaybar_state *state) {
if (state->output->workspaces) {
list_foreach(state->output->workspaces, free_workspace);
list_free(state->output->workspaces);
}
state->output->workspaces = create_list();
uint32_t len = 0;
char *res = ipc_single_command(state->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len);
json_object *results = json_tokener_parse(res);
if (!results) {
free(res);
return;
}
int i;
int length = json_object_array_length(results);
json_object *ws_json;
json_object *num, *name, *visible, *focused, *out, *urgent;
for (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);
if (strcmp(json_object_get_string(out), state->output->name) == 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(state->output->workspaces, ws);
}
}
json_object_put(results);
free(res);
}
void ipc_bar_init(struct swaybar_state *state, int outputi, const char *bar_id) {
uint32_t len = 0;
char *res = ipc_single_command(state->ipc_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);
state->output->name = strdup(json_object_get_string(name));
free(res);
json_object_put(outputs);
len = strlen(bar_id);
res = ipc_single_command(state->ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len);
ipc_parse_config(state->config, res);
free(res);
const char *subscribe_json = "[ \"workspace\", \"mode\" ]";
len = strlen(subscribe_json);
res = ipc_single_command(state->ipc_event_socketfd, IPC_SUBSCRIBE, subscribe_json, &len);
free(res);
ipc_update_workspaces(state);
}
bool handle_ipc_event(struct swaybar_state *state) {
struct ipc_response *resp = ipc_recv_response(state->ipc_event_socketfd);
switch (resp->type) {
case IPC_EVENT_WORKSPACE:
ipc_update_workspaces(state);
break;
case IPC_EVENT_MODE: {
json_object *result = json_tokener_parse(resp->payload);
if (!result) {
free_ipc_response(resp);
sway_log(L_ERROR, "failed to parse payload as json");
return false;
}
json_object *json_change;
if (json_object_object_get_ex(result, "change", &json_change)) {
const char *change = json_object_get_string(json_change);
free(state->config->mode);
if (strcmp(change, "default") == 0) {
state->config->mode = NULL;
} else {
state->config->mode = strdup(change);
}
} else {
sway_log(L_ERROR, "failed to parse response");
}
json_object_put(result);
break;
}
default:
free_ipc_response(resp);
return false;
}
free_ipc_response(resp);
return true;
}

@ -0,0 +1,17 @@
#ifndef _SWAYBAR_IPC_H
#define _SWAYBAR_IPC_H
#include "state.h"
/**
* Initialize ipc connection to sway and get sway state, outputs, bar_config.
*/
void ipc_bar_init(struct swaybar_state *state, int outputi, const char *bar_id);
/**
* Handle ipc event from sway.
*/
bool handle_ipc_event(struct swaybar_state *state);
#endif /* _SWAYBAR_IPC_H */

@ -23,8 +23,8 @@
#include "config.h"
#include "render.h"
#include "status_line.h"
#include "ipc.h"
char *output;
struct swaybar_state *state;
void swaybar_teardown() {
@ -71,247 +71,6 @@ void sig_handler(int signal) {
exit(0);
}
void ipc_update_workspaces() {
if (state->output->workspaces) {
list_foreach(state->output->workspaces, free_workspace);
list_free(state->output->workspaces);
}
state->output->workspaces = create_list();
uint32_t len = 0;
char *res = ipc_single_command(state->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len);
json_object *results = json_tokener_parse(res);
if (!results) {
free(res);
return;
}
int i;
int length = json_object_array_length(results);
json_object *ws_json;
json_object *num, *name, *visible, *focused, *out, *urgent;
for (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);
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(state->output->workspaces, ws);
}
}
json_object_put(results);
free(res);
}
void bar_ipc_init(int outputi, const char *bar_id) {
uint32_t len = 0;
char *res = ipc_single_command(state->ipc_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);
len = strlen(bar_id);
res = ipc_single_command(state->ipc_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;
json_object *_binding_mode_indicator, *verbose, *_colors, *sep_symbol;
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);
json_object_object_get_ex(bar_config, "separator_symbol", &sep_symbol);
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?)
if (_status_command) {
free(state->config->status_command);
state->config->status_command = strdup(json_object_get_string(_status_command));
}
if (position) {
state->config->position = parse_position(json_object_get_string(position));
desktop_shell_set_panel_position(state->output->registry->desktop_shell, state->config->position);
}
if (font) {
state->output->window->font = parse_font(json_object_get_string(font));
}
if (sep_symbol) {
free(state->config->sep_symbol);
state->config->sep_symbol = strdup(json_object_get_string(sep_symbol));
}
if (_strip_workspace_numbers) {
state->config->strip_workspace_numbers = json_object_get_boolean(_strip_workspace_numbers);
}
if (_binding_mode_indicator) {
state->config->binding_mode_indicator = json_object_get_boolean(_binding_mode_indicator);
}
if (_workspace_buttons) {
state->config->workspace_buttons = json_object_get_boolean(_workspace_buttons);
}
if (bar_height) {
int width, height;
get_text_size(state->output->window, &width, &height, "Test string for measuring purposes");
int bar_height_value = json_object_get_int(bar_height);
if (bar_height_value > 0) {
state->config->margin = (bar_height_value - height) / 2;
state->config->ws_vertical_padding = state->config->margin - 1.5;
}
state->output->window->height = height + state->config->margin * 2;
}
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) {
state->config->colors.background = parse_color(json_object_get_string(background));
}
if (statusline) {
state->config->colors.statusline = parse_color(json_object_get_string(statusline));
}
if (separator) {
state->config->colors.separator = parse_color(json_object_get_string(separator));
}
if (focused_workspace_border) {
state->config->colors.focused_workspace.border = parse_color(json_object_get_string(focused_workspace_border));
}
if (focused_workspace_bg) {
state->config->colors.focused_workspace.background = parse_color(json_object_get_string(focused_workspace_bg));
}
if (focused_workspace_text) {
state->config->colors.focused_workspace.text = parse_color(json_object_get_string(focused_workspace_text));
}
if (active_workspace_border) {
state->config->colors.active_workspace.border = parse_color(json_object_get_string(active_workspace_border));
}
if (active_workspace_bg) {
state->config->colors.active_workspace.background = parse_color(json_object_get_string(active_workspace_bg));
}
if (active_workspace_text) {
state->config->colors.active_workspace.text = parse_color(json_object_get_string(active_workspace_text));
}
if (inactive_workspace_border) {
state->config->colors.inactive_workspace.border = parse_color(json_object_get_string(inactive_workspace_border));
}
if (inactive_workspace_bg) {
state->config->colors.inactive_workspace.background = parse_color(json_object_get_string(inactive_workspace_bg));
}
if (inactive_workspace_text) {
state->config->colors.inactive_workspace.text = parse_color(json_object_get_string(inactive_workspace_text));
}
if (binding_mode_border) {
state->config->colors.binding_mode.border = parse_color(json_object_get_string(binding_mode_border));
}
if (binding_mode_bg) {
state->config->colors.binding_mode.background = parse_color(json_object_get_string(binding_mode_bg));
}
if (binding_mode_text) {
state->config->colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text));
}
}
json_object_put(bar_config);
free(res);
const char *subscribe_json = "[ \"workspace\", \"mode\" ]";
len = strlen(subscribe_json);
res = ipc_single_command(state->ipc_event_socketfd, IPC_SUBSCRIBE, subscribe_json, &len);
free(res);
ipc_update_workspaces();
}
bool handle_ipc_event() {
struct ipc_response *resp = ipc_recv_response(state->ipc_event_socketfd);
switch (resp->type) {
case IPC_EVENT_WORKSPACE:
ipc_update_workspaces();
break;
case IPC_EVENT_MODE: {
json_object *result = json_tokener_parse(resp->payload);
if (!result) {
free_ipc_response(resp);
sway_log(L_ERROR, "failed to parse payload as json");
return false;
}
json_object *json_change;
if (json_object_object_get_ex(result, "change", &json_change)) {
const char *change = json_object_get_string(json_change);
free(state->config->mode);
if (strcmp(change, "default") == 0) {
state->config->mode = NULL;
} else {
state->config->mode = strdup(change);
}
} else {
sway_log(L_ERROR, "failed to parse response");
}
json_object_put(result);
break;
}
default:
free_ipc_response(resp);
return false;
}
free_ipc_response(resp);
return true;
}
void poll_for_update() {
fd_set readfds;
int activity;
@ -341,7 +100,7 @@ void poll_for_update() {
if (FD_ISSET(state->ipc_event_socketfd, &readfds)) {
sway_log(L_DEBUG, "Got IPC event.");
dirty = handle_ipc_event();
dirty = handle_ipc_event(state);
}
if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) {
@ -435,12 +194,13 @@ int main(int argc, char **argv) {
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);
@ -448,8 +208,13 @@ int main(int argc, char **argv) {
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;
bar_ipc_init(desired_output, bar_id);
/* set window height */
set_window_height(state->output->window, state->config->height);
if (state->config->status_command) {
int pipefd[2];

@ -8,6 +8,13 @@
#include "status_line.h"
#include "render.h"
/* internal spacing */
static int margin = 3;
static int ws_horizontal_padding = 5;
static double ws_vertical_padding = 1.5;
static int ws_spacing = 1;
static void cairo_set_source_u32(cairo_t *cairo, uint32_t color) {
cairo_set_source_rgba(cairo,
((color & 0xFF000000) >> 24) / 256.0,
@ -62,13 +69,13 @@ static void render_block(struct window *window, struct swaybar_config *config, s
*x -= width;
if (block->border != 0 && block->border_left > 0) {
*x -= (block->border_left + config->margin);
block_width += block->border_left + config->margin;
*x -= (block->border_left + margin);
block_width += block->border_left + margin;
}
if (block->border != 0 && block->border_right > 0) {
*x -= (block->border_right + config->margin);
block_width += block->border_right + config->margin;
*x -= (block->border_right + margin);
block_width += block->border_right + margin;
}
// Add separator
@ -76,13 +83,13 @@ static void render_block(struct window *window, struct swaybar_config *config, s
if (config->sep_symbol) {
get_text_size(window, &sep_width, &height, "%s", config->sep_symbol);
if (sep_width > block->separator_block_width) {
block->separator_block_width = sep_width + config->margin * 2;
block->separator_block_width = sep_width + margin * 2;
}
}
*x -= block->separator_block_width;
} else {
*x -= config->margin;
*x -= margin;
}
double pos = *x;
@ -120,7 +127,7 @@ static void render_block(struct window *window, struct swaybar_config *config, s
block->border_left,
window->height - 2);
pos += block->border_left + config->margin;
pos += block->border_left + margin;
}
// render text
@ -134,7 +141,7 @@ static void render_block(struct window *window, struct swaybar_config *config, s
offset = pos + (width - textwidth) / 2;
}
cairo_move_to(window->cairo, offset, config->margin);
cairo_move_to(window->cairo, offset, margin);
cairo_set_source_u32(window->cairo, block->color);
pango_printf(window, "%s", block->full_text);
@ -142,7 +149,7 @@ static void render_block(struct window *window, struct swaybar_config *config, s
// render right border
if (block->border != 0 && block->border_right > 0) {
pos += config->margin;
pos += margin;
render_sharp_line(window->cairo, block->border,
pos - 0.5,
@ -158,14 +165,14 @@ static void render_block(struct window *window, struct swaybar_config *config, s
cairo_set_source_u32(window->cairo, config->colors.separator);
if (config->sep_symbol) {
offset = pos + (block->separator_block_width - sep_width) / 2;
cairo_move_to(window->cairo, offset, config->margin);
cairo_move_to(window->cairo, offset, margin);
pango_printf(window, "%s", config->sep_symbol);
} else {
cairo_set_line_width(window->cairo, 1);
cairo_move_to(window->cairo, pos + block->separator_block_width/2,
config->margin);
margin);
cairo_line_to(window->cairo, pos + block->separator_block_width/2,
window->height - config->margin);
window->height - margin);
cairo_stroke(window->cairo);
}
}
@ -215,22 +222,22 @@ static void render_workspace_button(struct window *window, struct swaybar_config
// background
cairo_set_source_u32(window->cairo, box_colors.background);
cairo_rectangle(window->cairo, *x, 1.5, width + config->ws_horizontal_padding * 2 - 1,
height + config->ws_vertical_padding * 2);
cairo_rectangle(window->cairo, *x, 1.5, width + ws_horizontal_padding * 2 - 1,
height + ws_vertical_padding * 2);
cairo_fill(window->cairo);
// border
cairo_set_source_u32(window->cairo, box_colors.border);
cairo_rectangle(window->cairo, *x, 1.5, width + config->ws_horizontal_padding * 2 - 1,
height + config->ws_vertical_padding * 2);
cairo_rectangle(window->cairo, *x, 1.5, width + ws_horizontal_padding * 2 - 1,
height + ws_vertical_padding * 2);
cairo_stroke(window->cairo);
// text
cairo_set_source_u32(window->cairo, box_colors.text);
cairo_move_to(window->cairo, (int)*x + config->ws_horizontal_padding, config->margin);
cairo_move_to(window->cairo, (int)*x + ws_horizontal_padding, margin);
pango_printf(window, "%s", name);
*x += width + config->ws_horizontal_padding * 2 + config->ws_spacing;
*x += width + ws_horizontal_padding * 2 + ws_spacing;
free(name);
}
@ -241,19 +248,19 @@ static void render_binding_mode_indicator(struct window *window, struct swaybar_
// background
cairo_set_source_u32(window->cairo, config->colors.binding_mode.background);
cairo_rectangle(window->cairo, pos, 1.5, width + config->ws_horizontal_padding * 2 - 1,
height + config->ws_vertical_padding * 2);
cairo_rectangle(window->cairo, pos, 1.5, width + ws_horizontal_padding * 2 - 1,
height + ws_vertical_padding * 2);
cairo_fill(window->cairo);
// border
cairo_set_source_u32(window->cairo, config->colors.binding_mode.border);
cairo_rectangle(window->cairo, pos, 1.5, width + config->ws_horizontal_padding * 2 - 1,
height + config->ws_vertical_padding * 2);
cairo_rectangle(window->cairo, pos, 1.5, width + ws_horizontal_padding * 2 - 1,
height + ws_vertical_padding * 2);
cairo_stroke(window->cairo);
// text
cairo_set_source_u32(window->cairo, config->colors.binding_mode.text);
cairo_move_to(window->cairo, (int)pos + config->ws_horizontal_padding, config->margin);
cairo_move_to(window->cairo, (int)pos + ws_horizontal_padding, margin);
pango_printf(window, "%s", config->mode);
}
@ -279,7 +286,7 @@ void render(struct output *output, struct swaybar_config *config, struct status_
if (line->protocol == TEXT) {
get_text_size(window, &width, &height, "%s", line->text_line);
cairo_move_to(cairo, window->width - config->margin - width, config->margin);
cairo_move_to(cairo, window->width - margin - width, margin);
pango_printf(window, "%s", line);
} else if (line->protocol == I3BAR && line->block_line) {
double pos = window->width - 0.5;
@ -309,3 +316,13 @@ void render(struct output *output, struct swaybar_config *config, struct status_
render_binding_mode_indicator(window, config, x);
}
}
void set_window_height(struct window *window, int height) {
int text_width, text_height;
get_text_size(window, &text_width, &text_height, "Test string for measuring purposes");
if (height > 0) {
margin = (height - text_height) / 2;
ws_vertical_padding = margin - 1.5;
}
window->height = text_height + margin * 2;
}

@ -9,4 +9,9 @@
*/
void render(struct output *output, struct swaybar_config *config, struct status_line *line);
/**
* Set window height and modify internal spacing accordingly.
*/
void set_window_height(struct window *window, int height);
#endif /* _SWAYBAR_RENDER_H */

@ -13,6 +13,7 @@ struct swaybar_state *init_state() {
state->output->window = NULL;
state->output->registry = NULL;
state->output->workspaces = create_list();
state->output->name = NULL;
return state;
}

@ -20,6 +20,7 @@ struct output {
struct window *window;
struct registry *registry;
list_t *workspaces;
char *name;
};
struct workspace {

@ -48,7 +48,7 @@ static void free_status_block(void *item) {
free(sb);
}
static void parse_json(struct swaybar_state *st, const char *text) {
static void parse_json(struct swaybar_state *state, const char *text) {
json_object *results = json_tokener_parse(text);
if (!results) {
sway_log(L_DEBUG, "Failed to parse json");
@ -59,12 +59,12 @@ static void parse_json(struct swaybar_state *st, const char *text) {
return;
}
if (st->status->block_line) {
list_foreach(st->status->block_line, free_status_block);
list_free(st->status->block_line);
if (state->status->block_line) {
list_foreach(state->status->block_line, free_status_block);
list_free(state->status->block_line);
}
st->status->block_line = create_list();
state->status->block_line = create_list();
int i;
for (i = 0; i < json_object_array_length(results); ++i) {
@ -108,7 +108,7 @@ static void parse_json(struct swaybar_state *st, const char *text) {
if (color) {
new->color = parse_color(json_object_get_string(color));
} else {
new->color = st->config->colors.statusline;
new->color = state->config->colors.statusline;
}
if (min_width) {
@ -188,7 +188,7 @@ static void parse_json(struct swaybar_state *st, const char *text) {
new->border_right = 1;
}
list_add(st->status->block_line, new);
list_add(state->status->block_line, new);
}
json_object_put(results);
@ -369,41 +369,41 @@ static int i3json_handle_data(struct swaybar_state *st, char *data) {
}
// read data from fd and parse it.
static int i3json_handle_fd(struct swaybar_state *st) {
static int i3json_handle_fd(struct swaybar_state *state) {
i3json_ensure_free(10240);
// get fresh data at the end of the buffer
int readlen = read(st->status_read_fd, i3json_state.parserpos, 10239);
int readlen = read(state->status_read_fd, i3json_state.parserpos, 10239);
if (readlen < 0) {
return readlen;
}
i3json_state.parserpos[readlen] = '\0';
return i3json_parse(st);
return i3json_parse(state);
}
bool handle_status_line(struct swaybar_state *st) {
bool handle_status_line(struct swaybar_state *state) {
bool dirty = false;
switch (st->status->protocol) {
switch (state->status->protocol) {
case I3BAR:
sway_log(L_DEBUG, "Got i3bar protocol.");
if (i3json_handle_fd(st) > 0) {
if (i3json_handle_fd(state) > 0) {
dirty = true;
}
break;
case TEXT:
sway_log(L_DEBUG, "Got text protocol.");
read_line_tail(st->status_read_fd, line, sizeof(line), line_rest);
read_line_tail(state->status_read_fd, line, sizeof(line), line_rest);
dirty = true;
st->status->text_line = line;
state->status->text_line = line;
break;
case UNDEF:
sway_log(L_DEBUG, "Detecting protocol...");
if (read_line_tail(st->status_read_fd, line, sizeof(line), line_rest) < 0) {
if (read_line_tail(state->status_read_fd, line, sizeof(line), line_rest) < 0) {
break;
}
dirty = true;
st->status->text_line = line;
st->status->protocol = TEXT;
state->status->text_line = line;
state->status->protocol = TEXT;
if (line[0] == '{') {
// detect i3bar json protocol
json_object *proto = json_tokener_parse(line);
@ -413,8 +413,8 @@ bool handle_status_line(struct swaybar_state *st) {
&& json_object_get_int(version) == 1
) {
sway_log(L_DEBUG, "Switched to i3bar protocol.");
st->status->protocol = I3BAR;
i3json_handle_data(st, line_rest);
state->status->protocol = I3BAR;
i3json_handle_data(state, line_rest);
}
json_object_put(proto);
}

Loading…
Cancel
Save