From bb986cb33637147663c115c8b8f8bdff170f23a8 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sat, 23 Jan 2016 02:47:44 +0100 Subject: [PATCH 1/7] swaybar: Separate config --- swaybar/CMakeLists.txt | 1 + swaybar/config.c | 96 +++++++++++++++ swaybar/config.h | 73 ++++++++++++ swaybar/main.c | 258 ++++++++++++++--------------------------- 4 files changed, 257 insertions(+), 171 deletions(-) create mode 100644 swaybar/config.c create mode 100644 swaybar/config.h diff --git a/swaybar/CMakeLists.txt b/swaybar/CMakeLists.txt index a03ffcf4c..c09cf020a 100644 --- a/swaybar/CMakeLists.txt +++ b/swaybar/CMakeLists.txt @@ -8,6 +8,7 @@ include_directories( add_executable(swaybar main.c + config.c ) target_link_libraries(swaybar diff --git a/swaybar/config.c b/swaybar/config.c new file mode 100644 index 000000000..388daa556 --- /dev/null +++ b/swaybar/config.c @@ -0,0 +1,96 @@ +#include +#include + +#include "wayland-desktop-shell-client-protocol.h" +#include "log.h" +#include "config.h" + +uint32_t parse_color(const char *color) { + if (color[0] != '#') { + sway_log(L_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color); + return 0xFFFFFFFF; + } + char *end; + uint32_t res = (uint32_t)strtol(color + 1, &end, 16); + if (strlen(color) == 7) { + res = (res << 8) | 0xFF; + } + return res; +} + +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; + } +} + +char *parse_font(const char *font) { + char *new_font = NULL; + if (strncmp("pango:", font, 6) == 0) { + new_font = strdup(font + 6); + } + + return new_font; +} + +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->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; + + /* colors */ + config->colors.background = 0x000000FF; + config->colors.statusline = 0xFFFFFFFF; + config->colors.separator = 0x666666FF; + + config->colors.focused_workspace.border = 0x4C7899FF; + config->colors.focused_workspace.background = 0x285577FF; + config->colors.focused_workspace.text = 0xFFFFFFFF; + + config->colors.active_workspace.border = 0x333333FF; + config->colors.active_workspace.background = 0x5F676AFF; + config->colors.active_workspace.text = 0xFFFFFFFF; + + config->colors.inactive_workspace.border = 0x333333FF; + config->colors.inactive_workspace.background = 0x222222FF; + config->colors.inactive_workspace.text = 0x888888FF; + + config->colors.urgent_workspace.border = 0x2F343AFF; + config->colors.urgent_workspace.background = 0x900000FF; + config->colors.urgent_workspace.text = 0xFFFFFFFF; + + config->colors.binding_mode.border = 0x2F343AFF; + config->colors.binding_mode.background = 0x900000FF; + config->colors.binding_mode.text = 0xFFFFFFFF; + + return config; +} + +void free_config(struct swaybar_config *config) { + free(config->status_command); + free(config->font); + free(config->mode); + free(config->sep_symbol); + free(config); +} diff --git a/swaybar/config.h b/swaybar/config.h new file mode 100644 index 000000000..5cda97e6b --- /dev/null +++ b/swaybar/config.h @@ -0,0 +1,73 @@ +#ifndef _SWAYBAR_CONFIG_H +#define _SWAYBAR_CONFIG_H + +#include +#include + +/** + * Colors for a box with background, border and text colors. + */ +struct box_colors { + uint32_t border; + uint32_t background; + uint32_t text; +}; + +/** + * Swaybar config. + */ +struct swaybar_config { + char *status_command; + uint32_t position; + char *font; + char *sep_symbol; + char *mode; + bool strip_workspace_numbers; + bool binding_mode_indicator; + bool workspace_buttons; + + int margin; + int ws_horizontal_padding; + double ws_vertical_padding; + int ws_spacing; + int text_height; + + struct { + uint32_t background; + uint32_t statusline; + uint32_t separator; + + 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; + } colors; +}; + +/** + * Parse colors defined as hex string to uint32_t. + */ +uint32_t parse_color(const char *color); + +/** + * Parse position top|bottom|left|right. + */ +uint32_t parse_position(const char *position); + +/** + * Parse font. + */ +char *parse_font(const char *font); + +/** + * Initialize default sway config. + */ +struct swaybar_config *init_config(); + +/** + * Free config struct. + */ +void free_config(struct swaybar_config *config); + +#endif /* _SWAYBAR_CONFIG_H */ diff --git a/swaybar/main.c b/swaybar/main.c index a3a3b56a7..41170bf7c 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -19,24 +19,7 @@ #include "client/pango.h" #include "stringop.h" #include "log.h" - -struct box_colors { - uint32_t border; - uint32_t background; - uint32_t text; -}; - -struct colors { - uint32_t background; - uint32_t statusline; - uint32_t separator; - - 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; -}; +#include "config.h" struct workspace { int num; @@ -71,50 +54,14 @@ pid_t pid; int status_read_fd; char line[1024]; char line_rest[1024]; -char *output, *status_command; +char *output; struct registry *registry; struct window *window; bool dirty = true; -char *separator_symbol = NULL; -char *mode = NULL; -bool binding_mode_indicator = true; -bool strip_workspace_numbers = false; -bool workspace_buttons = true; +struct swaybar_config *config; typedef enum {UNDEF, TEXT, I3BAR} command_protocol; command_protocol protocol = UNDEF; -struct colors colors = { - .background = 0x000000FF, - .statusline = 0xFFFFFFFF, - .separator = 0x666666FF, - - .focused_workspace = { - .border = 0x4C7899FF, - .background = 0x285577FF, - .text = 0xFFFFFFFF - }, - .active_workspace = { - .border = 0x333333FF, - .background = 0x5F676AFF, - .text = 0xFFFFFFFF - }, - .inactive_workspace = { - .border = 0x333333FF, - .background = 0x222222FF, - .text = 0x888888FF - }, - .urgent_workspace = { - .border = 0x2F343AFF, - .background = 0x900000FF, - .text = 0xFFFFFFFF - }, - .binding_mode = { - .border = 0x2F343AFF, - .background = 0x900000FF, - .text = 0xFFFFFFFF - }, -}; - #define I3JSON_MAXDEPTH 4 #define I3JSON_UNKNOWN 0 #define I3JSON_ARRAY 1 @@ -237,46 +184,6 @@ void ipc_update_workspaces() { free(res); } -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; -} - -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; - } -} - -char *parse_font(const char *font) { - char *new_font = NULL; - if (strncmp("pango:", font, 6) == 0) { - new_font = strdup(font + 6); - } - - return new_font; -} - -static int margin = 3; -static const int ws_hor_padding = 5; -static double ws_ver_padding = 1.5; -static const int ws_spacing = 1; - void bar_ipc_init(int outputi, const char *bar_id) { uint32_t len = 0; char *res = ipc_single_command(ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len); @@ -312,11 +219,13 @@ void bar_ipc_init(int outputi, const char *bar_id) { // TODO: More of these options // TODO: Refactor swaybar into several files, create a bar config struct (shared with compositor?) if (_status_command) { - status_command = strdup(json_object_get_string(_status_command)); + free(config->status_command); + config->status_command = strdup(json_object_get_string(_status_command)); } if (position) { - desktop_shell_set_panel_position(registry->desktop_shell, parse_position(json_object_get_string(position))); + config->position = parse_position(json_object_get_string(position)); + desktop_shell_set_panel_position(registry->desktop_shell, config->position); } if (font) { @@ -324,19 +233,20 @@ void bar_ipc_init(int outputi, const char *bar_id) { } if (sep_symbol) { - separator_symbol = strdup(json_object_get_string(sep_symbol)); + free(config->sep_symbol); + config->sep_symbol = strdup(json_object_get_string(sep_symbol)); } if (_strip_workspace_numbers) { - strip_workspace_numbers = json_object_get_boolean(_strip_workspace_numbers); + config->strip_workspace_numbers = json_object_get_boolean(_strip_workspace_numbers); } if (_binding_mode_indicator) { - binding_mode_indicator = json_object_get_boolean(_binding_mode_indicator); + config->binding_mode_indicator = json_object_get_boolean(_binding_mode_indicator); } if (_workspace_buttons) { - workspace_buttons = json_object_get_boolean(_workspace_buttons); + config->workspace_buttons = json_object_get_boolean(_workspace_buttons); } if (bar_height) { @@ -344,10 +254,10 @@ void bar_ipc_init(int outputi, const char *bar_id) { 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; + config->margin = (bar_height_value - height) / 2; + config->ws_vertical_padding = config->margin - 1.5; } - window->height = height + margin * 2; + window->height = height + config->margin * 2; } if (_colors) { @@ -375,44 +285,50 @@ void bar_ipc_init(int outputi, const char *bar_id) { 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)); - if (separator) colors.separator = parse_color(json_object_get_string(separator)); + 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) { - colors.focused_workspace.border = parse_color(json_object_get_string(focused_workspace_border)); + config->colors.focused_workspace.border = parse_color(json_object_get_string(focused_workspace_border)); } if (focused_workspace_bg) { - colors.focused_workspace.background = parse_color(json_object_get_string(focused_workspace_bg)); + config->colors.focused_workspace.background = parse_color(json_object_get_string(focused_workspace_bg)); } if (focused_workspace_text) { - colors.focused_workspace.text = parse_color(json_object_get_string(focused_workspace_text)); + config->colors.focused_workspace.text = parse_color(json_object_get_string(focused_workspace_text)); } if (active_workspace_border) { - colors.active_workspace.border = parse_color(json_object_get_string(active_workspace_border)); + config->colors.active_workspace.border = parse_color(json_object_get_string(active_workspace_border)); } if (active_workspace_bg) { - colors.active_workspace.background = parse_color(json_object_get_string(active_workspace_bg)); + config->colors.active_workspace.background = parse_color(json_object_get_string(active_workspace_bg)); } if (active_workspace_text) { - colors.active_workspace.text = parse_color(json_object_get_string(active_workspace_text)); + config->colors.active_workspace.text = parse_color(json_object_get_string(active_workspace_text)); } if (inactive_workspace_border) { - colors.inactive_workspace.border = parse_color(json_object_get_string(inactive_workspace_border)); + config->colors.inactive_workspace.border = parse_color(json_object_get_string(inactive_workspace_border)); } if (inactive_workspace_bg) { - colors.inactive_workspace.background = parse_color(json_object_get_string(inactive_workspace_bg)); + config->colors.inactive_workspace.background = parse_color(json_object_get_string(inactive_workspace_bg)); } if (inactive_workspace_text) { - colors.inactive_workspace.text = parse_color(json_object_get_string(inactive_workspace_text)); + config->colors.inactive_workspace.text = parse_color(json_object_get_string(inactive_workspace_text)); } if (binding_mode_border) { - colors.binding_mode.border = parse_color(json_object_get_string(binding_mode_border)); + config->colors.binding_mode.border = parse_color(json_object_get_string(binding_mode_border)); } if (binding_mode_bg) { - colors.binding_mode.background = parse_color(json_object_get_string(binding_mode_bg)); + config->colors.binding_mode.background = parse_color(json_object_get_string(binding_mode_bg)); } if (binding_mode_text) { - colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text)); + config->colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text)); } } @@ -473,27 +389,27 @@ void render_block(struct status_block *block, double *x, bool edge) { *x -= width; if (block->border != 0 && block->border_left > 0) { - *x -= (block->border_left + margin); - block_width += block->border_left + margin; + *x -= (block->border_left + config->margin); + block_width += block->border_left + config->margin; } if (block->border != 0 && block->border_right > 0) { - *x -= (block->border_right + margin); - block_width += block->border_right + margin; + *x -= (block->border_right + config->margin); + block_width += block->border_right + config->margin; } // Add separator if (!edge) { - if (separator_symbol) { - get_text_size(window, &sep_width, &height, "%s", separator_symbol); + 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 + margin * 2; + block->separator_block_width = sep_width + config->margin * 2; } } *x -= block->separator_block_width; } else { - *x -= margin; + *x -= config->margin; } double pos = *x; @@ -531,7 +447,7 @@ void render_block(struct status_block *block, double *x, bool edge) { block->border_left, window->height - 2); - pos += block->border_left + margin; + pos += block->border_left + config->margin; } // render text @@ -545,7 +461,7 @@ void render_block(struct status_block *block, double *x, bool edge) { offset = pos + (width - textwidth) / 2; } - cairo_move_to(window->cairo, offset, margin); + cairo_move_to(window->cairo, offset, config->margin); cairo_set_source_u32(window->cairo, block->color); pango_printf(window, "%s", block->full_text); @@ -553,7 +469,7 @@ void render_block(struct status_block *block, double *x, bool edge) { // render right border if (block->border != 0 && block->border_right > 0) { - pos += margin; + pos += config->margin; render_sharp_line(window->cairo, block->border, pos - 0.5, @@ -566,17 +482,17 @@ void render_block(struct status_block *block, double *x, bool edge) { // render separator if (!edge && block->separator) { - cairo_set_source_u32(window->cairo, colors.separator); - if (separator_symbol) { + 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, margin); - pango_printf(window, "%s", separator_symbol); + cairo_move_to(window->cairo, offset, config->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, - margin); + config->margin); cairo_line_to(window->cairo, pos + block->separator_block_width/2, - window->height - margin); + window->height - config->margin); cairo_stroke(window->cairo); } } @@ -609,63 +525,63 @@ char *handle_workspace_number(bool strip_num, const char *ws_name) { void render_workspace_button(struct workspace *ws, double *x) { // strip workspace numbers if required - char *name = handle_workspace_number(strip_workspace_numbers, ws->name); + char *name = handle_workspace_number(config->strip_workspace_numbers, ws->name); int width, height; get_text_size(window, &width, &height, "%s", name); struct box_colors box_colors; if (ws->urgent) { - box_colors = colors.urgent_workspace; + box_colors = config->colors.urgent_workspace; } else if (ws->focused) { - box_colors = colors.focused_workspace; + box_colors = config->colors.focused_workspace; } else if (ws->visible) { - box_colors = colors.active_workspace; + box_colors = config->colors.active_workspace; } else { - box_colors = colors.inactive_workspace; + box_colors = config->colors.inactive_workspace; } // background cairo_set_source_u32(window->cairo, box_colors.background); - cairo_rectangle(window->cairo, *x, 1.5, width + ws_hor_padding * 2 - 1, - height + ws_ver_padding * 2); + cairo_rectangle(window->cairo, *x, 1.5, width + config->ws_horizontal_padding * 2 - 1, + height + config->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 + ws_hor_padding * 2 - 1, - height + ws_ver_padding * 2); + cairo_rectangle(window->cairo, *x, 1.5, width + config->ws_horizontal_padding * 2 - 1, + height + config->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 + ws_hor_padding, margin); + cairo_move_to(window->cairo, (int)*x + config->ws_horizontal_padding, config->margin); pango_printf(window, "%s", name); - *x += width + ws_hor_padding * 2 + ws_spacing; + *x += width + config->ws_horizontal_padding * 2 + config->ws_spacing; free(name); } void render_binding_mode_indicator(double pos) { int width, height; - get_text_size(window, &width, &height, "%s", mode); + get_text_size(window, &width, &height, "%s", config->mode); // background - cairo_set_source_u32(window->cairo, colors.binding_mode.background); - cairo_rectangle(window->cairo, pos, 1.5, width + ws_hor_padding * 2 - 1, - height + ws_ver_padding * 2); + 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_fill(window->cairo); // border - cairo_set_source_u32(window->cairo, colors.binding_mode.border); - cairo_rectangle(window->cairo, pos, 1.5, width + ws_hor_padding * 2 - 1, - height + ws_ver_padding * 2); + 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_stroke(window->cairo); // text - cairo_set_source_u32(window->cairo, colors.binding_mode.text); - cairo_move_to(window->cairo, (int)pos + ws_hor_padding, margin); - pango_printf(window, "%s", mode); + cairo_set_source_u32(window->cairo, config->colors.binding_mode.text); + cairo_move_to(window->cairo, (int)pos + config->ws_horizontal_padding, config->margin); + pango_printf(window, "%s", config->mode); } void render() { @@ -678,16 +594,16 @@ void render() { cairo_restore(window->cairo); // Background - cairo_set_source_u32(window->cairo, colors.background); + cairo_set_source_u32(window->cairo, config->colors.background); cairo_paint(window->cairo); // Command output - cairo_set_source_u32(window->cairo, colors.statusline); + cairo_set_source_u32(window->cairo, config->colors.statusline); int width, height; if (protocol == TEXT) { get_text_size(window, &width, &height, "%s", line); - cairo_move_to(window->cairo, window->width - margin - width, margin); + cairo_move_to(window->cairo, window->width - config->margin - width, config->margin); pango_printf(window, "%s", line); } else if (protocol == I3BAR && status_line) { double pos = window->width - 0.5; @@ -705,7 +621,7 @@ void render() { double x = 0.5; // Workspaces - if (workspace_buttons) { + if (config->workspace_buttons) { for (i = 0; i < workspaces->length; ++i) { struct workspace *ws = workspaces->items[i]; render_workspace_button(ws, &x); @@ -713,7 +629,7 @@ void render() { } // binding mode indicator - if (mode && binding_mode_indicator) { + if (config->mode && config->binding_mode_indicator) { render_binding_mode_indicator(x); } } @@ -802,7 +718,7 @@ void parse_json(const char *text) { if (color) { new->color = parse_color(json_object_get_string(color)); } else { - new->color = colors.statusline; + new->color = config->colors.statusline; } if (min_width) { @@ -1092,11 +1008,11 @@ bool handle_ipc_event() { if (json_object_object_get_ex(result, "change", &json_change)) { const char *change = json_object_get_string(json_change); - free(mode); + free(config->mode); if (strcmp(change, "default") == 0) { - mode = NULL; + config->mode = NULL; } else { - mode = strdup(change); + config->mode = strdup(change); } } else { sway_log(L_ERROR, "failed to parse response"); @@ -1142,7 +1058,7 @@ void poll_for_update() { dirty = handle_ipc_event(); } - if (status_command && FD_ISSET(status_read_fd, &readfds)) { + if (config->status_command && FD_ISSET(status_read_fd, &readfds)) { sway_log(L_DEBUG, "Got update from status command."); switch (protocol) { case I3BAR: @@ -1185,10 +1101,10 @@ void poll_for_update() { } int main(int argc, char **argv) { - char *socket_path = NULL; char *bar_id = NULL; bool debug = false; + config = init_config(); static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, @@ -1284,7 +1200,7 @@ int main(int argc, char **argv) { bar_ipc_init(desired_output, bar_id); - if (status_command) { + if (config->status_command) { int pipefd[2]; pipe(pipefd); pid = fork(); @@ -1295,7 +1211,7 @@ int main(int argc, char **argv) { char *const cmd[] = { "sh", "-c", - status_command, + config->status_command, NULL, }; execvp(cmd[0], cmd); From a6349a2444571624c792ca222dff57c7f1711c71 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sat, 23 Jan 2016 20:55:01 +0100 Subject: [PATCH 2/7] swaybar: feactor render, statusline --- swaybar/CMakeLists.txt | 3 + swaybar/main.c | 930 ++++------------------------------------- swaybar/render.c | 311 ++++++++++++++ swaybar/render.h | 12 + swaybar/state.c | 29 ++ swaybar/state.h | 43 ++ swaybar/status_line.c | 435 +++++++++++++++++++ swaybar/status_line.h | 45 ++ 8 files changed, 968 insertions(+), 840 deletions(-) create mode 100644 swaybar/render.c create mode 100644 swaybar/render.h create mode 100644 swaybar/state.c create mode 100644 swaybar/state.h create mode 100644 swaybar/status_line.c create mode 100644 swaybar/status_line.h diff --git a/swaybar/CMakeLists.txt b/swaybar/CMakeLists.txt index c09cf020a..ebb3819b8 100644 --- a/swaybar/CMakeLists.txt +++ b/swaybar/CMakeLists.txt @@ -9,6 +9,9 @@ include_directories( add_executable(swaybar main.c config.c + render.c + state.c + status_line.c ) target_link_libraries(swaybar diff --git a/swaybar/main.c b/swaybar/main.c index 41170bf7c..6ba7c825b 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -19,95 +19,45 @@ #include "client/pango.h" #include "stringop.h" #include "log.h" +#include "state.h" #include "config.h" +#include "render.h" +#include "status_line.h" -struct workspace { - int num; - char *name; - bool focused; - bool visible; - bool urgent; -}; - -struct status_block { - char *full_text, *short_text, *align; - bool urgent; - uint32_t color; - int min_width; - char *name, *instance; - bool separator; - int separator_block_width; - // Airblader features - uint32_t background; - uint32_t border; - int border_top; - int border_bottom; - int border_left; - int border_right; -}; - -list_t *status_line = NULL; - -list_t *workspaces = NULL; -int ipc_socketfd, ipc_event_socketfd; -pid_t pid; -int status_read_fd; -char line[1024]; -char line_rest[1024]; char *output; -struct registry *registry; -struct window *window; -bool dirty = true; -struct swaybar_config *config; -typedef enum {UNDEF, TEXT, I3BAR} command_protocol; -command_protocol protocol = UNDEF; - -#define I3JSON_MAXDEPTH 4 -#define I3JSON_UNKNOWN 0 -#define I3JSON_ARRAY 1 -#define I3JSON_STRING 2 -struct { - int bufsize; - char *buffer; - char *line_start; - char *parserpos; - bool escape; - int depth; - int state[I3JSON_MAXDEPTH+1]; -} i3json_state = { 0, NULL, NULL, NULL, false, 0, { I3JSON_UNKNOWN } }; - +struct swaybar_state *state; void swaybar_teardown() { - window_teardown(window); - if (registry) { - registry_teardown(registry); + window_teardown(state->output->window); + if (state->output->registry) { + registry_teardown(state->output->registry); } - if (status_read_fd) { - close(status_read_fd); + if (state->status_read_fd) { + close(state->status_read_fd); } - if (pid) { + if (state->status_command_pid) { // terminate status_command process - int ret = kill(pid, SIGTERM); + int ret = kill(state->status_command_pid, SIGTERM); if (ret != 0) { - sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid); + sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", state->status_command_pid); } else { int status; - waitpid(pid, &status, 0); + waitpid(state->status_command_pid, &status, 0); } } - if (status_read_fd) { - close(status_read_fd); + if (state->status_read_fd) { + close(state->status_read_fd); } - if (ipc_socketfd) { - close(ipc_socketfd); + if (state->ipc_socketfd) { + close(state->ipc_socketfd); } - if (ipc_event_socketfd) { - close(ipc_event_socketfd); + if (state->ipc_event_socketfd) { + close(state->ipc_event_socketfd); } } @@ -121,34 +71,15 @@ void sig_handler(int signal) { exit(0); } -void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { - cairo_set_source_rgba(cairo, - ((color & 0xFF000000) >> 24) / 256.0, - ((color & 0xFF0000) >> 16) / 256.0, - ((color & 0xFF00) >> 8) / 256.0, - (color & 0xFF) / 256.0); -} - -void free_workspace(void *item) { - if (!item) { - return; - } - struct workspace *ws = (struct workspace *)item; - if (ws->name) { - free(ws->name); - } - free(ws); -} - void ipc_update_workspaces() { - if (workspaces) { - list_foreach(workspaces, free_workspace); - list_free(workspaces); + if (state->output->workspaces) { + list_foreach(state->output->workspaces, free_workspace); + list_free(state->output->workspaces); } - workspaces = create_list(); + state->output->workspaces = create_list(); uint32_t len = 0; - char *res = ipc_single_command(ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len); + char *res = ipc_single_command(state->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len); json_object *results = json_tokener_parse(res); if (!results) { free(res); @@ -176,7 +107,7 @@ void ipc_update_workspaces() { 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); + list_add(state->output->workspaces, ws); } } @@ -186,7 +117,7 @@ void ipc_update_workspaces() { void bar_ipc_init(int outputi, const char *bar_id) { uint32_t len = 0; - char *res = ipc_single_command(ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len); + 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; @@ -196,7 +127,7 @@ void bar_ipc_init(int outputi, const char *bar_id) { json_object_put(outputs); len = strlen(bar_id); - res = ipc_single_command(ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len); + 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; @@ -219,45 +150,45 @@ void bar_ipc_init(int outputi, const char *bar_id) { // TODO: More of these options // TODO: Refactor swaybar into several files, create a bar config struct (shared with compositor?) if (_status_command) { - free(config->status_command); - config->status_command = strdup(json_object_get_string(_status_command)); + free(state->config->status_command); + state->config->status_command = strdup(json_object_get_string(_status_command)); } if (position) { - config->position = parse_position(json_object_get_string(position)); - desktop_shell_set_panel_position(registry->desktop_shell, config->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) { - window->font = parse_font(json_object_get_string(font)); + state->output->window->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)); + free(state->config->sep_symbol); + state->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); + state->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); + state->config->binding_mode_indicator = json_object_get_boolean(_binding_mode_indicator); } if (_workspace_buttons) { - config->workspace_buttons = json_object_get_boolean(_workspace_buttons); + state->config->workspace_buttons = json_object_get_boolean(_workspace_buttons); } if (bar_height) { int width, height; - get_text_size(window, &width, &height, "Test string for measuring purposes"); + 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) { - config->margin = (bar_height_value - height) / 2; - config->ws_vertical_padding = config->margin - 1.5; + state->config->margin = (bar_height_value - height) / 2; + state->config->ws_vertical_padding = state->config->margin - 1.5; } - window->height = height + config->margin * 2; + state->output->window->height = height + state->config->margin * 2; } if (_colors) { @@ -286,49 +217,49 @@ void bar_ipc_init(int outputi, const char *bar_id) { 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)); + state->config->colors.background = parse_color(json_object_get_string(background)); } if (statusline) { - config->colors.statusline = parse_color(json_object_get_string(statusline)); + state->config->colors.statusline = parse_color(json_object_get_string(statusline)); } if (separator) { - config->colors.separator = parse_color(json_object_get_string(separator)); + state->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)); + state->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)); + state->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)); + state->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)); + state->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)); + state->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)); + state->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)); + state->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)); + state->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)); + state->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)); + state->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)); + state->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)); + state->config->colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text)); } } @@ -337,662 +268,13 @@ void bar_ipc_init(int outputi, const char *bar_id) { const char *subscribe_json = "[ \"workspace\", \"mode\" ]"; len = strlen(subscribe_json); - res = ipc_single_command(ipc_event_socketfd, IPC_SUBSCRIBE, subscribe_json, &len); + res = ipc_single_command(state->ipc_event_socketfd, IPC_SUBSCRIBE, subscribe_json, &len); free(res); ipc_update_workspaces(); } - -/** - * Renders a sharp line of any width and height. - * - * The line is drawn from (x,y) to (x+width,y+height) where width/height is 0 - * if the line has a width/height of one pixel, respectively. - */ -void render_sharp_line(cairo_t *cairo, uint32_t color, double x, double y, double width, double height) { - cairo_set_source_u32(cairo, color); - - if (width > 1 && height > 1) { - cairo_rectangle(cairo, x, y, width, height); - cairo_fill(cairo); - } else { - if (width == 1) { - x += 0.5; - height += y; - width = x; - } - - if (height == 1) { - y += 0.5; - width += x; - height = y; - } - - cairo_move_to(cairo, x, y); - cairo_set_line_width(cairo, 1.0); - cairo_line_to(cairo, width, height); - cairo_stroke(cairo); - } -} - -void render_block(struct status_block *block, double *x, bool edge) { - int width, height, sep_width; - get_text_size(window, &width, &height, "%s", block->full_text); - - int textwidth = width; - double block_width = width; - - if (width < block->min_width) { - width = block->min_width; - } - - *x -= width; - - if (block->border != 0 && block->border_left > 0) { - *x -= (block->border_left + config->margin); - block_width += block->border_left + config->margin; - } - - if (block->border != 0 && block->border_right > 0) { - *x -= (block->border_right + config->margin); - block_width += block->border_right + config->margin; - } - - // Add separator - if (!edge) { - 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; - } - } - - *x -= block->separator_block_width; - } else { - *x -= config->margin; - } - - double pos = *x; - - // render background - if (block->background != 0x0) { - cairo_set_source_u32(window->cairo, block->background); - cairo_rectangle(window->cairo, pos - 0.5, 1, block_width, window->height - 2); - cairo_fill(window->cairo); - } - - // render top border - if (block->border != 0 && block->border_top > 0) { - render_sharp_line(window->cairo, block->border, - pos - 0.5, - 1, - block_width, - block->border_top); - } - - // render bottom border - if (block->border != 0 && block->border_bottom > 0) { - render_sharp_line(window->cairo, block->border, - pos - 0.5, - window->height - 1 - block->border_bottom, - block_width, - block->border_bottom); - } - - // render left border - if (block->border != 0 && block->border_left > 0) { - render_sharp_line(window->cairo, block->border, - pos - 0.5, - 1, - block->border_left, - window->height - 2); - - pos += block->border_left + config->margin; - } - - // render text - double offset = 0; - - if (strncmp(block->align, "left", 5) == 0) { - offset = pos; - } else if (strncmp(block->align, "right", 5) == 0) { - offset = pos + width - textwidth; - } else if (strncmp(block->align, "center", 6) == 0) { - offset = pos + (width - textwidth) / 2; - } - - cairo_move_to(window->cairo, offset, config->margin); - cairo_set_source_u32(window->cairo, block->color); - pango_printf(window, "%s", block->full_text); - - pos += width; - - // render right border - if (block->border != 0 && block->border_right > 0) { - pos += config->margin; - - render_sharp_line(window->cairo, block->border, - pos - 0.5, - 1, - block->border_right, - window->height - 2); - - pos += block->border_right; - } - - // render separator - if (!edge && block->separator) { - 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); - 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); - cairo_line_to(window->cairo, pos + block->separator_block_width/2, - window->height - config->margin); - cairo_stroke(window->cairo); - } - } - -} - -char *handle_workspace_number(bool strip_num, const char *ws_name) { - bool strip = false; - int i; - - if (strip_num) { - int len = strlen(ws_name); - for (i = 0; i < len; ++i) { - if (!('0' <= ws_name[i] && ws_name[i] <= '9')) { - if (':' == ws_name[i] && i < len-1 && i > 0) { - strip = true; - ++i; - } - break; - } - } - } - - if (strip) { - return strdup(ws_name + i); - } - - return strdup(ws_name); -} - -void render_workspace_button(struct workspace *ws, double *x) { - // strip workspace numbers if required - char *name = handle_workspace_number(config->strip_workspace_numbers, ws->name); - - int width, height; - get_text_size(window, &width, &height, "%s", name); - struct box_colors box_colors; - if (ws->urgent) { - box_colors = config->colors.urgent_workspace; - } else if (ws->focused) { - box_colors = config->colors.focused_workspace; - } else if (ws->visible) { - box_colors = config->colors.active_workspace; - } else { - box_colors = config->colors.inactive_workspace; - } - - // 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_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_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); - pango_printf(window, "%s", name); - - *x += width + config->ws_horizontal_padding * 2 + config->ws_spacing; - - free(name); -} - -void render_binding_mode_indicator(double pos) { - int width, height; - get_text_size(window, &width, &height, "%s", config->mode); - - // 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_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_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); - pango_printf(window, "%s", config->mode); -} - -void render() { - int i; - - // Clear - cairo_save(window->cairo); - cairo_set_operator(window->cairo, CAIRO_OPERATOR_CLEAR); - cairo_paint(window->cairo); - cairo_restore(window->cairo); - - // Background - cairo_set_source_u32(window->cairo, config->colors.background); - cairo_paint(window->cairo); - - // Command output - cairo_set_source_u32(window->cairo, config->colors.statusline); - int width, height; - - if (protocol == TEXT) { - get_text_size(window, &width, &height, "%s", line); - cairo_move_to(window->cairo, window->width - config->margin - width, config->margin); - pango_printf(window, "%s", line); - } else if (protocol == I3BAR && status_line) { - double pos = window->width - 0.5; - bool edge = true; - for (i = status_line->length - 1; i >= 0; --i) { - struct status_block *block = status_line->items[i]; - if (block->full_text && block->full_text[0]) { - render_block(block, &pos, edge); - edge = false; - } - } - } - - cairo_set_line_width(window->cairo, 1.0); - double x = 0.5; - - // Workspaces - if (config->workspace_buttons) { - for (i = 0; i < workspaces->length; ++i) { - struct workspace *ws = workspaces->items[i]; - render_workspace_button(ws, &x); - } - } - - // binding mode indicator - if (config->mode && config->binding_mode_indicator) { - render_binding_mode_indicator(x); - } -} - -void free_status_block(void *item) { - if (!item) { - return; - } - struct status_block *sb = (struct status_block*)item; - if (sb->full_text) { - free(sb->full_text); - } - if (sb->short_text) { - free(sb->short_text); - } - if (sb->align) { - free(sb->align); - } - if (sb->name) { - free(sb->name); - } - if (sb->instance) { - free(sb->instance); - } - free(sb); -} - -void parse_json(const char *text) { - json_object *results = json_tokener_parse(text); - if (!results) { - sway_log(L_DEBUG, "Failed to parse json"); - return; - } - - if (json_object_array_length(results) < 1) { - return; - } - - if (status_line) { - list_foreach(status_line, free_status_block); - list_free(status_line); - } - - status_line = create_list(); - - int i; - for (i = 0; i < json_object_array_length(results); ++i) { - json_object *full_text, *short_text, *color, *min_width, *align, *urgent; - json_object *name, *instance, *separator, *separator_block_width; - json_object *background, *border, *border_top, *border_bottom; - json_object *border_left, *border_right; - - json_object *json = json_object_array_get_idx(results, i); - if (!json) { - continue; - } - - json_object_object_get_ex(json, "full_text", &full_text); - json_object_object_get_ex(json, "short_text", &short_text); - json_object_object_get_ex(json, "color", &color); - json_object_object_get_ex(json, "min_width", &min_width); - json_object_object_get_ex(json, "align", &align); - json_object_object_get_ex(json, "urgent", &urgent); - json_object_object_get_ex(json, "name", &name); - json_object_object_get_ex(json, "instance", &instance); - json_object_object_get_ex(json, "separator", &separator); - json_object_object_get_ex(json, "separator_block_width", &separator_block_width); - json_object_object_get_ex(json, "background", &background); - json_object_object_get_ex(json, "border", &border); - json_object_object_get_ex(json, "border_top", &border_top); - json_object_object_get_ex(json, "border_bottom", &border_bottom); - json_object_object_get_ex(json, "border_left", &border_left); - json_object_object_get_ex(json, "border_right", &border_right); - - struct status_block *new = malloc(sizeof(struct status_block)); - memset(new, 0, sizeof(struct status_block)); - - if (full_text) { - new->full_text = strdup(json_object_get_string(full_text)); - } - - if (short_text) { - new->short_text = strdup(json_object_get_string(short_text)); - } - - if (color) { - new->color = parse_color(json_object_get_string(color)); - } else { - new->color = config->colors.statusline; - } - - if (min_width) { - json_type type = json_object_get_type(min_width); - if (type == json_type_int) { - new->min_width = json_object_get_int(min_width); - } else if (type == json_type_string) { - int width, height; - get_text_size(window, &width, &height, "%s", json_object_get_string(min_width)); - new->min_width = width; - } - } - - if (align) { - new->align = strdup(json_object_get_string(align)); - } else { - new->align = strdup("left"); - } - - if (urgent) { - new->urgent = json_object_get_int(urgent); - } - - if (name) { - new->name = strdup(json_object_get_string(name)); - } - - if (instance) { - new->instance = strdup(json_object_get_string(instance)); - } - - if (separator) { - new->separator = json_object_get_int(separator); - } else { - new->separator = true; // i3bar spec - } - - if (separator_block_width) { - new->separator_block_width = json_object_get_int(separator_block_width); - } else { - new->separator_block_width = 9; // i3bar spec - } - - // Airblader features - if (background) { - new->background = parse_color(json_object_get_string(background)); - } else { - new->background = 0x0; // transparent - } - - if (border) { - new->border = parse_color(json_object_get_string(border)); - } else { - new->border = 0x0; // transparent - } - - if (border_top) { - new->border_top = json_object_get_int(border_top); - } else { - new->border_top = 1; - } - - if (border_bottom) { - new->border_bottom = json_object_get_int(border_bottom); - } else { - new->border_bottom = 1; - } - - if (border_left) { - new->border_left = json_object_get_int(border_left); - } else { - new->border_left = 1; - } - - if (border_right) { - new->border_right = json_object_get_int(border_right); - } else { - new->border_right = 1; - } - - list_add(status_line, new); - } - - json_object_put(results); -} - -// Read line from file descriptor, only show the line tail if it is too long. -// In non-blocking mode treat "no more data" as a linebreak. -// If data after a line break has been read, return it in rest. -// If rest is non-empty, then use that as the start of the next line. -int read_line_tail(int fd, char *buf, int nbyte, char *rest) { - if (fd < 0 || !buf || !nbyte) { - return -1; - } - int l; - char *buffer = malloc(nbyte*2+1); - char *readpos = buffer; - char *lf; - // prepend old data to new line if necessary - if (rest) { - l = strlen(rest); - if (l > nbyte) { - strcpy(buffer, rest + l - nbyte); - readpos += nbyte; - } else if (l) { - strcpy(buffer, rest); - readpos += l; - } - } - // read until a linefeed is found or no more data is available - while ((l = read(fd, readpos, nbyte)) > 0) { - readpos[l] = '\0'; - lf = strchr(readpos, '\n'); - if (lf) { - // linefeed found, replace with \0 - *lf = '\0'; - // give data from the end of the line, try to fill the buffer - if (lf-buffer > nbyte) { - strcpy(buf, lf - nbyte + 1); - } else { - strcpy(buf, buffer); - } - // we may have read data from the next line, save it to rest - if (rest) { - rest[0] = '\0'; - strcpy(rest, lf + 1); - } - free(buffer); - return strlen(buf); - } else { - // no linefeed found, slide data back. - int overflow = readpos - buffer + l - nbyte; - if (overflow > 0) { - memmove(buffer, buffer + overflow , nbyte + 1); - } - } - } - if (l < 0) { - free(buffer); - return l; - } - readpos[l]='\0'; - if (rest) { - rest[0] = '\0'; - } - if (nbyte < readpos - buffer + l - 1) { - memcpy(buf, readpos - nbyte + l + 1, nbyte); - } else { - strncpy(buf, buffer, nbyte); - } - buf[nbyte-1] = '\0'; - free(buffer); - return strlen(buf); -} - -// make sure that enough buffer space is available starting from parserpos -void i3json_ensure_free(int min_free) { - int _step = 10240; - int r = min_free % _step; - if (r) { - min_free += _step - r; - } - if (!i3json_state.buffer) { - i3json_state.buffer = malloc(min_free); - i3json_state.bufsize = min_free; - i3json_state.parserpos = i3json_state.buffer; - } else { - int len = 0; - int pos = 0; - if (i3json_state.line_start) { - len = strlen(i3json_state.line_start); - pos = i3json_state.parserpos - i3json_state.line_start; - if (i3json_state.line_start != i3json_state.buffer) { - memmove(i3json_state.buffer, i3json_state.line_start, len+1); - } - } else { - len = strlen(i3json_state.buffer); - } - if (i3json_state.bufsize < len+min_free) { - i3json_state.bufsize += min_free; - if (i3json_state.bufsize > 1024000) { - sway_abort("Status line json too long or malformed."); - } - i3json_state.buffer = realloc(i3json_state.buffer, i3json_state.bufsize); - if (!i3json_state.buffer) { - sway_abort("Could not allocate json buffer"); - } - } - if (i3json_state.line_start) { - i3json_state.line_start = i3json_state.buffer; - i3json_state.parserpos = i3json_state.buffer + pos; - } else { - i3json_state.parserpos = i3json_state.buffer; - } - } - if (!i3json_state.buffer) { - sway_abort("Could not allocate buffer."); - } -} - -// continue parsing from last parserpos -int i3json_parse() { - char *c = i3json_state.parserpos; - int handled = 0; - while (*c) { - if (i3json_state.state[i3json_state.depth] == I3JSON_STRING) { - if (!i3json_state.escape && *c == '"') { - --i3json_state.depth; - } - i3json_state.escape = !i3json_state.escape && *c == '\\'; - } else { - switch (*c) { - case '[': - ++i3json_state.depth; - if (i3json_state.depth > I3JSON_MAXDEPTH) { - sway_abort("JSON too deep"); - } - i3json_state.state[i3json_state.depth] = I3JSON_ARRAY; - if (i3json_state.depth == 2) { - i3json_state.line_start = c; - } - break; - case ']': - if (i3json_state.state[i3json_state.depth] != I3JSON_ARRAY) { - sway_abort("JSON malformed"); - } - --i3json_state.depth; - if (i3json_state.depth == 1) { - // c[1] is valid since c[0] != '\0' - char p = c[1]; - c[1] = '\0'; - parse_json(i3json_state.line_start); - c[1] = p; - ++handled; - i3json_state.line_start = c+1; - } - break; - case '"': - ++i3json_state.depth; - if (i3json_state.depth > I3JSON_MAXDEPTH) { - sway_abort("JSON too deep"); - } - i3json_state.state[i3json_state.depth] = I3JSON_STRING; - break; - } - } - ++c; - } - i3json_state.parserpos = c; - return handled; -} - -// append data and parse it. -int i3json_handle_data(char *data) { - int len = strlen(data); - i3json_ensure_free(len); - strcpy(i3json_state.parserpos, data); - return i3json_parse(); -} - -// read data from fd and parse it. -int i3json_handle_fd(int fd) { - i3json_ensure_free(10240); - // get fresh data at the end of the buffer - int readlen = read(fd, i3json_state.parserpos, 10239); - if (readlen < 0) { - return readlen; - } - i3json_state.parserpos[readlen] = '\0'; - return i3json_parse(); -} - bool handle_ipc_event() { - struct ipc_response *resp = ipc_recv_response(ipc_event_socketfd); + struct ipc_response *resp = ipc_recv_response(state->ipc_event_socketfd); switch (resp->type) { case IPC_EVENT_WORKSPACE: ipc_update_workspaces(); @@ -1008,11 +290,11 @@ bool handle_ipc_event() { if (json_object_object_get_ex(result, "change", &json_change)) { const char *change = json_object_get_string(json_change); - free(config->mode); + free(state->config->mode); if (strcmp(change, "default") == 0) { - config->mode = NULL; + state->config->mode = NULL; } else { - config->mode = strdup(change); + state->config->mode = strdup(change); } } else { sway_log(L_ERROR, "failed to parse response"); @@ -1033,69 +315,38 @@ bool handle_ipc_event() { void poll_for_update() { fd_set readfds; int activity; + bool dirty = true; while (1) { - if (dirty && window_prerender(window) && window->cairo) { - render(); - window_render(window); - if (wl_display_dispatch(registry->display) == -1) { - break; + if (dirty) { + struct output *output = state->output; + if (window_prerender(output->window) && output->window->cairo) { + render(output, state->config, state->status); + window_render(output->window); + if (wl_display_dispatch(output->registry->display) == -1) { + break; + } } } dirty = false; FD_ZERO(&readfds); - FD_SET(ipc_event_socketfd, &readfds); - FD_SET(status_read_fd, &readfds); + FD_SET(state->ipc_event_socketfd, &readfds); + FD_SET(state->status_read_fd, &readfds); activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); if (activity < 0) { sway_log(L_ERROR, "polling failed: %d", errno); } - if (FD_ISSET(ipc_event_socketfd, &readfds)) { + if (FD_ISSET(state->ipc_event_socketfd, &readfds)) { sway_log(L_DEBUG, "Got IPC event."); dirty = handle_ipc_event(); } - if (config->status_command && FD_ISSET(status_read_fd, &readfds)) { + if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) { sway_log(L_DEBUG, "Got update from status command."); - switch (protocol) { - case I3BAR: - sway_log(L_DEBUG, "Got i3bar protocol."); - if (i3json_handle_fd(status_read_fd) > 0) { - dirty = true; - } - break; - case TEXT: - sway_log(L_DEBUG, "Got text protocol."); - read_line_tail(status_read_fd, line, sizeof(line), line_rest); - dirty = true; - break; - case UNDEF: - sway_log(L_DEBUG, "Detecting protocol..."); - if (read_line_tail(status_read_fd, line, sizeof(line), line_rest) < 0) { - break; - } - dirty = true; - protocol = TEXT; - if (line[0] == '{') { - // detect i3bar json protocol - json_object *proto = json_tokener_parse(line); - json_object *version; - if (proto) { - if (json_object_object_get_ex(proto, "version", &version) - && json_object_get_int(version) == 1 - ) { - sway_log(L_DEBUG, "Switched to i3bar protocol."); - protocol = I3BAR; - i3json_handle_data(line_rest); - } - json_object_put(proto); - } - } - break; - } + dirty = handle_status_line(state); } } } @@ -1104,7 +355,7 @@ int main(int argc, char **argv) { char *socket_path = NULL; char *bar_id = NULL; bool debug = false; - config = init_config(); + state = init_state(); static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, @@ -1169,9 +420,9 @@ int main(int argc, char **argv) { init_log(L_ERROR); } - registry = registry_poll(); + state->output->registry = registry_poll(); - if (!registry->desktop_shell) { + if (!state->output->registry->desktop_shell) { sway_abort("swaybar requires the compositor to support the desktop-shell extension."); } @@ -1181,8 +432,8 @@ int main(int argc, char **argv) { sway_abort("Unable to retrieve socket path"); } } - ipc_socketfd = ipc_open_socket(socket_path); - ipc_event_socketfd = ipc_open_socket(socket_path); + state->ipc_socketfd = ipc_open_socket(socket_path); + state->ipc_event_socketfd = ipc_open_socket(socket_path); if (argc == optind) { @@ -1190,28 +441,28 @@ int main(int argc, char **argv) { } int desired_output = atoi(argv[optind]); - struct output_state *output = registry->outputs->items[desired_output]; + struct output_state *output = state->output->registry->outputs->items[desired_output]; - window = window_setup(registry, output->width, 30, false); - if (!window) { + state->output->window = window_setup(state->output->registry, output->width, 30, false); + if (!state->output->window) { sway_abort("Failed to create window."); } - desktop_shell_set_panel(registry->desktop_shell, output->output, window->surface); + desktop_shell_set_panel(state->output->registry->desktop_shell, output->output, state->output->window->surface); bar_ipc_init(desired_output, bar_id); - if (config->status_command) { + if (state->config->status_command) { int pipefd[2]; pipe(pipefd); - pid = fork(); - if (pid == 0) { + state->status_command_pid = fork(); + if (state->status_command_pid == 0) { close(pipefd[0]); dup2(pipefd[1], STDOUT_FILENO); close(pipefd[1]); char *const cmd[] = { "sh", "-c", - config->status_command, + state->config->status_command, NULL, }; execvp(cmd[0], cmd); @@ -1219,9 +470,8 @@ int main(int argc, char **argv) { } close(pipefd[1]); - status_read_fd = pipefd[0]; - fcntl(status_read_fd, F_SETFL, O_NONBLOCK); - line[0] = '\0'; + state->status_read_fd = pipefd[0]; + fcntl(state->status_read_fd, F_SETFL, O_NONBLOCK); } signal(SIGTERM, sig_handler); diff --git a/swaybar/render.c b/swaybar/render.c new file mode 100644 index 000000000..1e1554c5b --- /dev/null +++ b/swaybar/render.c @@ -0,0 +1,311 @@ +#include +#include +#include + +#include "client/pango.h" +#include "client/window.h" +#include "config.h" +#include "status_line.h" +#include "render.h" + +static void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { + cairo_set_source_rgba(cairo, + ((color & 0xFF000000) >> 24) / 256.0, + ((color & 0xFF0000) >> 16) / 256.0, + ((color & 0xFF00) >> 8) / 256.0, + (color & 0xFF) / 256.0); +} + +/** + * Renders a sharp line of any width and height. + * + * The line is drawn from (x,y) to (x+width,y+height) where width/height is 0 + * if the line has a width/height of one pixel, respectively. + */ +static void render_sharp_line(cairo_t *cairo, uint32_t color, double x, double y, double width, double height) { + cairo_set_source_u32(cairo, color); + + if (width > 1 && height > 1) { + cairo_rectangle(cairo, x, y, width, height); + cairo_fill(cairo); + } else { + if (width == 1) { + x += 0.5; + height += y; + width = x; + } + + if (height == 1) { + y += 0.5; + width += x; + height = y; + } + + cairo_move_to(cairo, x, y); + cairo_set_line_width(cairo, 1.0); + cairo_line_to(cairo, width, height); + cairo_stroke(cairo); + } +} + +static void render_block(struct window *window, struct swaybar_config *config, struct status_block *block, double *x, bool edge) { + int width, height, sep_width; + get_text_size(window, &width, &height, "%s", block->full_text); + + int textwidth = width; + double block_width = width; + + if (width < block->min_width) { + width = block->min_width; + } + + *x -= width; + + if (block->border != 0 && block->border_left > 0) { + *x -= (block->border_left + config->margin); + block_width += block->border_left + config->margin; + } + + if (block->border != 0 && block->border_right > 0) { + *x -= (block->border_right + config->margin); + block_width += block->border_right + config->margin; + } + + // Add separator + if (!edge) { + 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; + } + } + + *x -= block->separator_block_width; + } else { + *x -= config->margin; + } + + double pos = *x; + + // render background + if (block->background != 0x0) { + cairo_set_source_u32(window->cairo, block->background); + cairo_rectangle(window->cairo, pos - 0.5, 1, block_width, window->height - 2); + cairo_fill(window->cairo); + } + + // render top border + if (block->border != 0 && block->border_top > 0) { + render_sharp_line(window->cairo, block->border, + pos - 0.5, + 1, + block_width, + block->border_top); + } + + // render bottom border + if (block->border != 0 && block->border_bottom > 0) { + render_sharp_line(window->cairo, block->border, + pos - 0.5, + window->height - 1 - block->border_bottom, + block_width, + block->border_bottom); + } + + // render left border + if (block->border != 0 && block->border_left > 0) { + render_sharp_line(window->cairo, block->border, + pos - 0.5, + 1, + block->border_left, + window->height - 2); + + pos += block->border_left + config->margin; + } + + // render text + double offset = 0; + + if (strncmp(block->align, "left", 5) == 0) { + offset = pos; + } else if (strncmp(block->align, "right", 5) == 0) { + offset = pos + width - textwidth; + } else if (strncmp(block->align, "center", 6) == 0) { + offset = pos + (width - textwidth) / 2; + } + + cairo_move_to(window->cairo, offset, config->margin); + cairo_set_source_u32(window->cairo, block->color); + pango_printf(window, "%s", block->full_text); + + pos += width; + + // render right border + if (block->border != 0 && block->border_right > 0) { + pos += config->margin; + + render_sharp_line(window->cairo, block->border, + pos - 0.5, + 1, + block->border_right, + window->height - 2); + + pos += block->border_right; + } + + // render separator + if (!edge && block->separator) { + 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); + 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); + cairo_line_to(window->cairo, pos + block->separator_block_width/2, + window->height - config->margin); + cairo_stroke(window->cairo); + } + } + +} + +static char *handle_workspace_number(bool strip_num, const char *ws_name) { + bool strip = false; + int i; + + if (strip_num) { + int len = strlen(ws_name); + for (i = 0; i < len; ++i) { + if (!('0' <= ws_name[i] && ws_name[i] <= '9')) { + if (':' == ws_name[i] && i < len-1 && i > 0) { + strip = true; + ++i; + } + break; + } + } + } + + if (strip) { + return strdup(ws_name + i); + } + + return strdup(ws_name); +} + +static void render_workspace_button(struct window *window, struct swaybar_config *config, struct workspace *ws, double *x) { + // strip workspace numbers if required + char *name = handle_workspace_number(config->strip_workspace_numbers, ws->name); + + int width, height; + get_text_size(window, &width, &height, "%s", name); + struct box_colors box_colors; + if (ws->urgent) { + box_colors = config->colors.urgent_workspace; + } else if (ws->focused) { + box_colors = config->colors.focused_workspace; + } else if (ws->visible) { + box_colors = config->colors.active_workspace; + } else { + box_colors = config->colors.inactive_workspace; + } + + // 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_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_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); + pango_printf(window, "%s", name); + + *x += width + config->ws_horizontal_padding * 2 + config->ws_spacing; + + free(name); +} + +static void render_binding_mode_indicator(struct window *window, struct swaybar_config *config, double pos) { + int width, height; + get_text_size(window, &width, &height, "%s", config->mode); + + // 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_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_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); + pango_printf(window, "%s", config->mode); +} + +void render(struct output *output, struct swaybar_config *config, struct status_line *line) { + int i; + + struct window *window = output->window; + cairo_t *cairo = window->cairo; + + // Clear + cairo_save(cairo); + cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); + cairo_paint(cairo); + cairo_restore(cairo); + + // Background + cairo_set_source_u32(cairo, config->colors.background); + cairo_paint(cairo); + + // Command output + cairo_set_source_u32(cairo, config->colors.statusline); + int width, height; + + 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); + pango_printf(window, "%s", line); + } else if (line->protocol == I3BAR && line->block_line) { + double pos = window->width - 0.5; + bool edge = true; + for (i = line->block_line->length - 1; i >= 0; --i) { + struct status_block *block = line->block_line->items[i]; + if (block->full_text && block->full_text[0]) { + render_block(window, config, block, &pos, edge); + edge = false; + } + } + } + + cairo_set_line_width(cairo, 1.0); + double x = 0.5; + + // Workspaces + if (config->workspace_buttons) { + for (i = 0; i < output->workspaces->length; ++i) { + struct workspace *ws = output->workspaces->items[i]; + render_workspace_button(window, config, ws, &x); + } + } + + // binding mode indicator + if (config->mode && config->binding_mode_indicator) { + render_binding_mode_indicator(window, config, x); + } +} diff --git a/swaybar/render.h b/swaybar/render.h new file mode 100644 index 000000000..2815edfce --- /dev/null +++ b/swaybar/render.h @@ -0,0 +1,12 @@ +#ifndef _SWAYBAR_RENDER_H +#define _SWAYBAR_RENDER_H + +#include "config.h" +#include "state.h" + +/** + * Render swaybar. + */ +void render(struct output *output, struct swaybar_config *config, struct status_line *line); + +#endif /* _SWAYBAR_RENDER_H */ diff --git a/swaybar/state.c b/swaybar/state.c new file mode 100644 index 000000000..900842e00 --- /dev/null +++ b/swaybar/state.c @@ -0,0 +1,29 @@ +#include + +#include "list.h" +#include "config.h" +#include "status_line.h" +#include "state.h" + +struct swaybar_state *init_state() { + struct swaybar_state *state = calloc(1, sizeof(struct swaybar_state)); + state->config = init_config(); + state->status = init_status_line(); + state->output = malloc(sizeof(struct output)); + state->output->window = NULL; + state->output->registry = NULL; + state->output->workspaces = create_list(); + + return state; +} + +void free_workspace(void *item) { + if (!item) { + return; + } + struct workspace *ws = (struct workspace *)item; + if (ws->name) { + free(ws->name); + } + free(ws); +} diff --git a/swaybar/state.h b/swaybar/state.h new file mode 100644 index 000000000..5949548e1 --- /dev/null +++ b/swaybar/state.h @@ -0,0 +1,43 @@ +#ifndef _SWAYBAR_STATE_H +#define _SWAYBAR_STATE_H + +#include "client/registry.h" +#include "client/window.h" + +struct swaybar_state { + struct swaybar_config *config; + struct status_line *status; + struct output *output; + /* list_t *outputs; */ + + int ipc_event_socketfd; + int ipc_socketfd; + int status_read_fd; + pid_t status_command_pid; +}; + +struct output { + struct window *window; + struct registry *registry; + list_t *workspaces; +}; + +struct workspace { + int num; + char *name; + bool focused; + bool visible; + bool urgent; +}; + +/** + * Initialize state. + */ +struct swaybar_state *init_state(); + +/** + * free workspace struct. + */ +void free_workspace(void *item); + +#endif /* _SWAYBAR_STATE_H */ diff --git a/swaybar/status_line.c b/swaybar/status_line.c new file mode 100644 index 000000000..a9ed8d8cb --- /dev/null +++ b/swaybar/status_line.c @@ -0,0 +1,435 @@ +#include +#include +#include +#include + +#include "log.h" +#include "config.h" +#include "status_line.h" + +#define I3JSON_MAXDEPTH 4 +#define I3JSON_UNKNOWN 0 +#define I3JSON_ARRAY 1 +#define I3JSON_STRING 2 + +struct { + int bufsize; + char *buffer; + char *line_start; + char *parserpos; + bool escape; + int depth; + int state[I3JSON_MAXDEPTH+1]; +} i3json_state = { 0, NULL, NULL, NULL, false, 0, { I3JSON_UNKNOWN } }; + +static char line[1024]; +static char line_rest[1024]; + +static void free_status_block(void *item) { + if (!item) { + return; + } + struct status_block *sb = (struct status_block*)item; + if (sb->full_text) { + free(sb->full_text); + } + if (sb->short_text) { + free(sb->short_text); + } + if (sb->align) { + free(sb->align); + } + if (sb->name) { + free(sb->name); + } + if (sb->instance) { + free(sb->instance); + } + free(sb); +} + +static void parse_json(struct swaybar_state *st, const char *text) { + json_object *results = json_tokener_parse(text); + if (!results) { + sway_log(L_DEBUG, "Failed to parse json"); + return; + } + + if (json_object_array_length(results) < 1) { + return; + } + + if (st->status->block_line) { + list_foreach(st->status->block_line, free_status_block); + list_free(st->status->block_line); + } + + st->status->block_line = create_list(); + + int i; + for (i = 0; i < json_object_array_length(results); ++i) { + json_object *full_text, *short_text, *color, *min_width, *align, *urgent; + json_object *name, *instance, *separator, *separator_block_width; + json_object *background, *border, *border_top, *border_bottom; + json_object *border_left, *border_right; + + json_object *json = json_object_array_get_idx(results, i); + if (!json) { + continue; + } + + json_object_object_get_ex(json, "full_text", &full_text); + json_object_object_get_ex(json, "short_text", &short_text); + json_object_object_get_ex(json, "color", &color); + json_object_object_get_ex(json, "min_width", &min_width); + json_object_object_get_ex(json, "align", &align); + json_object_object_get_ex(json, "urgent", &urgent); + json_object_object_get_ex(json, "name", &name); + json_object_object_get_ex(json, "instance", &instance); + json_object_object_get_ex(json, "separator", &separator); + json_object_object_get_ex(json, "separator_block_width", &separator_block_width); + json_object_object_get_ex(json, "background", &background); + json_object_object_get_ex(json, "border", &border); + json_object_object_get_ex(json, "border_top", &border_top); + json_object_object_get_ex(json, "border_bottom", &border_bottom); + json_object_object_get_ex(json, "border_left", &border_left); + json_object_object_get_ex(json, "border_right", &border_right); + + struct status_block *new = calloc(1, sizeof(struct status_block)); + + if (full_text) { + new->full_text = strdup(json_object_get_string(full_text)); + } + + if (short_text) { + new->short_text = strdup(json_object_get_string(short_text)); + } + + if (color) { + new->color = parse_color(json_object_get_string(color)); + } else { + new->color = st->config->colors.statusline; + } + + if (min_width) { + json_type type = json_object_get_type(min_width); + if (type == json_type_int) { + new->min_width = json_object_get_int(min_width); + } else if (type == json_type_string) { + /* the width will be calculated when rendering */ + new->min_width = 0; + } + } + + if (align) { + new->align = strdup(json_object_get_string(align)); + } else { + new->align = strdup("left"); + } + + if (urgent) { + new->urgent = json_object_get_int(urgent); + } + + if (name) { + new->name = strdup(json_object_get_string(name)); + } + + if (instance) { + new->instance = strdup(json_object_get_string(instance)); + } + + if (separator) { + new->separator = json_object_get_int(separator); + } else { + new->separator = true; // i3bar spec + } + + if (separator_block_width) { + new->separator_block_width = json_object_get_int(separator_block_width); + } else { + new->separator_block_width = 9; // i3bar spec + } + + // Airblader features + if (background) { + new->background = parse_color(json_object_get_string(background)); + } else { + new->background = 0x0; // transparent + } + + if (border) { + new->border = parse_color(json_object_get_string(border)); + } else { + new->border = 0x0; // transparent + } + + if (border_top) { + new->border_top = json_object_get_int(border_top); + } else { + new->border_top = 1; + } + + if (border_bottom) { + new->border_bottom = json_object_get_int(border_bottom); + } else { + new->border_bottom = 1; + } + + if (border_left) { + new->border_left = json_object_get_int(border_left); + } else { + new->border_left = 1; + } + + if (border_right) { + new->border_right = json_object_get_int(border_right); + } else { + new->border_right = 1; + } + + list_add(st->status->block_line, new); + } + + json_object_put(results); +} + +// continue parsing from last parserpos +static int i3json_parse(struct swaybar_state *st) { + char *c = i3json_state.parserpos; + int handled = 0; + while (*c) { + if (i3json_state.state[i3json_state.depth] == I3JSON_STRING) { + if (!i3json_state.escape && *c == '"') { + --i3json_state.depth; + } + i3json_state.escape = !i3json_state.escape && *c == '\\'; + } else { + switch (*c) { + case '[': + ++i3json_state.depth; + if (i3json_state.depth > I3JSON_MAXDEPTH) { + sway_abort("JSON too deep"); + } + i3json_state.state[i3json_state.depth] = I3JSON_ARRAY; + if (i3json_state.depth == 2) { + i3json_state.line_start = c; + } + break; + case ']': + if (i3json_state.state[i3json_state.depth] != I3JSON_ARRAY) { + sway_abort("JSON malformed"); + } + --i3json_state.depth; + if (i3json_state.depth == 1) { + // c[1] is valid since c[0] != '\0' + char p = c[1]; + c[1] = '\0'; + parse_json(st, i3json_state.line_start); + c[1] = p; + ++handled; + i3json_state.line_start = c+1; + } + break; + case '"': + ++i3json_state.depth; + if (i3json_state.depth > I3JSON_MAXDEPTH) { + sway_abort("JSON too deep"); + } + i3json_state.state[i3json_state.depth] = I3JSON_STRING; + break; + } + } + ++c; + } + i3json_state.parserpos = c; + return handled; +} + +// Read line from file descriptor, only show the line tail if it is too long. +// In non-blocking mode treat "no more data" as a linebreak. +// If data after a line break has been read, return it in rest. +// If rest is non-empty, then use that as the start of the next line. +static int read_line_tail(int fd, char *buf, int nbyte, char *rest) { + if (fd < 0 || !buf || !nbyte) { + return -1; + } + int l; + char *buffer = malloc(nbyte*2+1); + char *readpos = buffer; + char *lf; + // prepend old data to new line if necessary + if (rest) { + l = strlen(rest); + if (l > nbyte) { + strcpy(buffer, rest + l - nbyte); + readpos += nbyte; + } else if (l) { + strcpy(buffer, rest); + readpos += l; + } + } + // read until a linefeed is found or no more data is available + while ((l = read(fd, readpos, nbyte)) > 0) { + readpos[l] = '\0'; + lf = strchr(readpos, '\n'); + if (lf) { + // linefeed found, replace with \0 + *lf = '\0'; + // give data from the end of the line, try to fill the buffer + if (lf-buffer > nbyte) { + strcpy(buf, lf - nbyte + 1); + } else { + strcpy(buf, buffer); + } + // we may have read data from the next line, save it to rest + if (rest) { + rest[0] = '\0'; + strcpy(rest, lf + 1); + } + free(buffer); + return strlen(buf); + } else { + // no linefeed found, slide data back. + int overflow = readpos - buffer + l - nbyte; + if (overflow > 0) { + memmove(buffer, buffer + overflow , nbyte + 1); + } + } + } + if (l < 0) { + free(buffer); + return l; + } + readpos[l]='\0'; + if (rest) { + rest[0] = '\0'; + } + if (nbyte < readpos - buffer + l - 1) { + memcpy(buf, readpos - nbyte + l + 1, nbyte); + } else { + strncpy(buf, buffer, nbyte); + } + buf[nbyte-1] = '\0'; + free(buffer); + return strlen(buf); +} + +// make sure that enough buffer space is available starting from parserpos +static void i3json_ensure_free(int min_free) { + int _step = 10240; + int r = min_free % _step; + if (r) { + min_free += _step - r; + } + if (!i3json_state.buffer) { + i3json_state.buffer = malloc(min_free); + i3json_state.bufsize = min_free; + i3json_state.parserpos = i3json_state.buffer; + } else { + int len = 0; + int pos = 0; + if (i3json_state.line_start) { + len = strlen(i3json_state.line_start); + pos = i3json_state.parserpos - i3json_state.line_start; + if (i3json_state.line_start != i3json_state.buffer) { + memmove(i3json_state.buffer, i3json_state.line_start, len+1); + } + } else { + len = strlen(i3json_state.buffer); + } + if (i3json_state.bufsize < len+min_free) { + i3json_state.bufsize += min_free; + if (i3json_state.bufsize > 1024000) { + sway_abort("Status line json too long or malformed."); + } + i3json_state.buffer = realloc(i3json_state.buffer, i3json_state.bufsize); + if (!i3json_state.buffer) { + sway_abort("Could not allocate json buffer"); + } + } + if (i3json_state.line_start) { + i3json_state.line_start = i3json_state.buffer; + i3json_state.parserpos = i3json_state.buffer + pos; + } else { + i3json_state.parserpos = i3json_state.buffer; + } + } + if (!i3json_state.buffer) { + sway_abort("Could not allocate buffer."); + } +} + +// append data and parse it. +static int i3json_handle_data(struct swaybar_state *st, char *data) { + int len = strlen(data); + i3json_ensure_free(len); + strcpy(i3json_state.parserpos, data); + return i3json_parse(st); +} + +// read data from fd and parse it. +static int i3json_handle_fd(struct swaybar_state *st) { + i3json_ensure_free(10240); + // get fresh data at the end of the buffer + int readlen = read(st->status_read_fd, i3json_state.parserpos, 10239); + if (readlen < 0) { + return readlen; + } + i3json_state.parserpos[readlen] = '\0'; + return i3json_parse(st); +} + +bool handle_status_line(struct swaybar_state *st) { + bool dirty = false; + + switch (st->status->protocol) { + case I3BAR: + sway_log(L_DEBUG, "Got i3bar protocol."); + if (i3json_handle_fd(st) > 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); + dirty = true; + st->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) { + break; + } + dirty = true; + st->status->text_line = line; + st->status->protocol = TEXT; + if (line[0] == '{') { + // detect i3bar json protocol + json_object *proto = json_tokener_parse(line); + json_object *version; + if (proto) { + if (json_object_object_get_ex(proto, "version", &version) + && json_object_get_int(version) == 1 + ) { + sway_log(L_DEBUG, "Switched to i3bar protocol."); + st->status->protocol = I3BAR; + i3json_handle_data(st, line_rest); + } + json_object_put(proto); + } + } + break; + } + + return dirty; +} + +struct status_line *init_status_line() { + struct status_line *line = malloc(sizeof(struct status_line)); + line->block_line = create_list(); + line->text_line = NULL; + line->protocol = UNDEF; + + return line; +} diff --git a/swaybar/status_line.h b/swaybar/status_line.h new file mode 100644 index 000000000..36020aeb9 --- /dev/null +++ b/swaybar/status_line.h @@ -0,0 +1,45 @@ +#ifndef _SWAYBAR_STATUS_LINE_H +#define _SWAYBAR_STATUS_LINE_H + +#include +#include + +#include "list.h" +#include "state.h" + +typedef enum {UNDEF, TEXT, I3BAR} command_protocol; + +struct status_line { + list_t *block_line; + char *text_line; + command_protocol protocol; +}; + +struct status_block { + char *full_text, *short_text, *align; + bool urgent; + uint32_t color; + int min_width; + char *name, *instance; + bool separator; + int separator_block_width; + // Airblader features + uint32_t background; + uint32_t border; + int border_top; + int border_bottom; + int border_left; + int border_right; +}; + +/** + * Initialize status line struct. + */ +struct status_line *init_status_line(); + +/** + * handle status line activity. + */ +bool handle_status_line(struct swaybar_state *st); + +#endif /* _SWAYBAR_STATUS_LINE_H */ From fcc47cb3bddd20a2fd068a4e486415112e4d4d20 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sun, 24 Jan 2016 00:23:09 +0100 Subject: [PATCH 3/7] swaybar: move ipc stuff to ipc.{h,c} --- swaybar/CMakeLists.txt | 1 + swaybar/config.c | 10 +- swaybar/config.h | 6 +- swaybar/ipc.c | 259 +++++++++++++++++++++++++++++++++++++++++ swaybar/ipc.h | 17 +++ swaybar/main.c | 255 ++-------------------------------------- swaybar/render.c | 65 +++++++---- swaybar/render.h | 5 + swaybar/state.c | 1 + swaybar/state.h | 1 + swaybar/status_line.c | 40 +++---- 11 files changed, 359 insertions(+), 301 deletions(-) create mode 100644 swaybar/ipc.c create mode 100644 swaybar/ipc.h diff --git a/swaybar/CMakeLists.txt b/swaybar/CMakeLists.txt index ebb3819b8..474d8474f 100644 --- a/swaybar/CMakeLists.txt +++ b/swaybar/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable(swaybar render.c state.c status_line.c + ipc.c ) target_link_libraries(swaybar diff --git a/swaybar/config.c b/swaybar/config.c index 388daa556..7c03198ad 100644 --- a/swaybar/config.c +++ b/swaybar/config.c @@ -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; diff --git a/swaybar/config.h b/swaybar/config.h index 5cda97e6b..ee06668ee 100644 --- a/swaybar/config.h +++ b/swaybar/config.h @@ -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; diff --git a/swaybar/ipc.c b/swaybar/ipc.c new file mode 100644 index 000000000..cb0b81aa5 --- /dev/null +++ b/swaybar/ipc.c @@ -0,0 +1,259 @@ +#include +#include + +#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; +} diff --git a/swaybar/ipc.h b/swaybar/ipc.h new file mode 100644 index 000000000..06d300761 --- /dev/null +++ b/swaybar/ipc.h @@ -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 */ + diff --git a/swaybar/main.c b/swaybar/main.c index 6ba7c825b..a521fa793 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -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); - bar_ipc_init(desired_output, bar_id); + /* set font */ + state->output->window->font = state->config->font; + + /* set window height */ + set_window_height(state->output->window, state->config->height); if (state->config->status_command) { int pipefd[2]; diff --git a/swaybar/render.c b/swaybar/render.c index 1e1554c5b..9ac0c766d 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -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; +} diff --git a/swaybar/render.h b/swaybar/render.h index 2815edfce..527cf76a0 100644 --- a/swaybar/render.h +++ b/swaybar/render.h @@ -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 */ diff --git a/swaybar/state.c b/swaybar/state.c index 900842e00..774275557 100644 --- a/swaybar/state.c +++ b/swaybar/state.c @@ -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; } diff --git a/swaybar/state.h b/swaybar/state.h index 5949548e1..f95e03bc7 100644 --- a/swaybar/state.h +++ b/swaybar/state.h @@ -20,6 +20,7 @@ struct output { struct window *window; struct registry *registry; list_t *workspaces; + char *name; }; struct workspace { diff --git a/swaybar/status_line.c b/swaybar/status_line.c index a9ed8d8cb..ee740c6b0 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c @@ -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); } From 6140f9c42c4f09142d647c96236cc030689e6f34 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sun, 24 Jan 2016 01:03:08 +0100 Subject: [PATCH 4/7] swaybar: Move swaybar_teardown to free_state --- swaybar/ipc.c | 3 +- swaybar/main.c | 40 ++----------------------- swaybar/state.c | 69 ++++++++++++++++++++++++++++++++++++++----- swaybar/state.h | 10 +++++-- swaybar/status_line.c | 7 +++++ swaybar/status_line.h | 7 ++++- 6 files changed, 87 insertions(+), 49 deletions(-) diff --git a/swaybar/ipc.c b/swaybar/ipc.c index cb0b81aa5..884d02ff2 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -152,8 +152,7 @@ static void ipc_parse_config(struct swaybar_config *config, const char *payload) 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); + free_workspaces(state->output->workspaces); } state->output->workspaces = create_list(); diff --git a/swaybar/main.c b/swaybar/main.c index a521fa793..976fcea09 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -27,47 +27,13 @@ struct swaybar_state *state; -void swaybar_teardown() { - window_teardown(state->output->window); - if (state->output->registry) { - registry_teardown(state->output->registry); - } - - if (state->status_read_fd) { - close(state->status_read_fd); - } - - if (state->status_command_pid) { - // terminate status_command process - int ret = kill(state->status_command_pid, SIGTERM); - if (ret != 0) { - sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", state->status_command_pid); - } else { - int status; - waitpid(state->status_command_pid, &status, 0); - } - } - - if (state->status_read_fd) { - close(state->status_read_fd); - } - - if (state->ipc_socketfd) { - close(state->ipc_socketfd); - } - - if (state->ipc_event_socketfd) { - close(state->ipc_event_socketfd); - } -} - void sway_terminate(void) { - swaybar_teardown(); + free_state(state); exit(EXIT_FAILURE); } void sig_handler(int signal) { - swaybar_teardown(); + free_state(state); exit(0); } @@ -244,7 +210,7 @@ int main(int argc, char **argv) { poll_for_update(); // gracefully shutdown swaybar and status_command - swaybar_teardown(); + free_state(state); return 0; } diff --git a/swaybar/state.c b/swaybar/state.c index 774275557..26cdcafee 100644 --- a/swaybar/state.c +++ b/swaybar/state.c @@ -1,6 +1,10 @@ #include +#include +#include +#include #include "list.h" +#include "log.h" #include "config.h" #include "status_line.h" #include "state.h" @@ -18,13 +22,64 @@ struct swaybar_state *init_state() { return state; } -void free_workspace(void *item) { - if (!item) { - return; - } - struct workspace *ws = (struct workspace *)item; - if (ws->name) { +void free_workspaces(list_t *workspaces) { + int i; + for (i = 0; i < workspaces->length; ++i) { + struct workspace *ws = workspaces->items[i]; free(ws->name); + free(ws); } - free(ws); + list_free(workspaces); +} + +static void free_output(struct output *output) { + window_teardown(output->window); + if (output->registry) { + registry_teardown(output->registry); + } + + free(output->name); + + if (output->workspaces) { + free_workspaces(output->workspaces); + } + + free(output); +} + +static void terminate_status_command(pid_t pid) { + if (pid) { + // terminate status_command process + int ret = kill(pid, SIGTERM); + if (ret != 0) { + sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid); + } else { + int status; + waitpid(pid, &status, 0); + } + } +} + +void free_state(struct swaybar_state *state) { + free_config(state->config); + free_output(state->output); + free_status_line(state->status); + + /* close sockets/pipes */ + if (state->status_read_fd) { + close(state->status_read_fd); + } + + if (state->ipc_socketfd) { + close(state->ipc_socketfd); + } + + if (state->ipc_event_socketfd) { + close(state->ipc_event_socketfd); + } + + /* terminate status command process */ + terminate_status_command(state->status_command_pid); + + free(state); } diff --git a/swaybar/state.h b/swaybar/state.h index f95e03bc7..e09807d0c 100644 --- a/swaybar/state.h +++ b/swaybar/state.h @@ -3,6 +3,7 @@ #include "client/registry.h" #include "client/window.h" +#include "list.h" struct swaybar_state { struct swaybar_config *config; @@ -37,8 +38,13 @@ struct workspace { struct swaybar_state *init_state(); /** - * free workspace struct. + * free workspace list. */ -void free_workspace(void *item); +void free_workspaces(list_t *workspaces); + +/** + * Free state struct. + */ +void free_state(struct swaybar_state *state); #endif /* _SWAYBAR_STATE_H */ diff --git a/swaybar/status_line.c b/swaybar/status_line.c index ee740c6b0..a072673b7 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c @@ -433,3 +433,10 @@ struct status_line *init_status_line() { return line; } + +void free_status_line(struct status_line *line) { + if (line->block_line) { + list_foreach(line->block_line, free_status_block); + list_free(line->block_line); + } +} diff --git a/swaybar/status_line.h b/swaybar/status_line.h index 36020aeb9..1d73dd574 100644 --- a/swaybar/status_line.h +++ b/swaybar/status_line.h @@ -11,7 +11,7 @@ typedef enum {UNDEF, TEXT, I3BAR} command_protocol; struct status_line { list_t *block_line; - char *text_line; + const char *text_line; command_protocol protocol; }; @@ -42,4 +42,9 @@ struct status_line *init_status_line(); */ bool handle_status_line(struct swaybar_state *st); +/** + * Free status line struct. + */ +void free_status_line(struct status_line *line); + #endif /* _SWAYBAR_STATUS_LINE_H */ From c6fc0033e1bf8aa1deb2c44284049041766361f8 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sun, 24 Jan 2016 02:19:08 +0100 Subject: [PATCH 5/7] swaybar: move core functionality to state.c --- include/ipc-client.h | 2 + swaybar/main.c | 120 ++++--------------------------------------- swaybar/state.c | 112 +++++++++++++++++++++++++++++++++++++--- swaybar/state.h | 13 +++-- 4 files changed, 127 insertions(+), 120 deletions(-) diff --git a/include/ipc-client.h b/include/ipc-client.h index 030c80b67..c9f5b3441 100644 --- a/include/ipc-client.h +++ b/include/ipc-client.h @@ -1,6 +1,8 @@ #ifndef _SWAY_IPC_CLIENT_H #define _SWAY_IPC_CLIENT_H +#include + #include "ipc.h" /** diff --git a/swaybar/main.c b/swaybar/main.c index 976fcea09..51846bca9 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -1,86 +1,28 @@ -#include #include #include #include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include #include #include "ipc-client.h" -#include "client/registry.h" -#include "client/window.h" -#include "client/pango.h" -#include "stringop.h" #include "log.h" #include "state.h" -#include "config.h" -#include "render.h" -#include "status_line.h" -#include "ipc.h" -struct swaybar_state *state; +struct swaybar_state state; void sway_terminate(void) { - free_state(state); + state_teardown(&state); exit(EXIT_FAILURE); } void sig_handler(int signal) { - free_state(state); + state_teardown(&state); exit(0); } -void poll_for_update() { - fd_set readfds; - int activity; - bool dirty = true; - - while (1) { - if (dirty) { - struct output *output = state->output; - if (window_prerender(output->window) && output->window->cairo) { - render(output, state->config, state->status); - window_render(output->window); - if (wl_display_dispatch(output->registry->display) == -1) { - break; - } - } - } - - dirty = false; - FD_ZERO(&readfds); - FD_SET(state->ipc_event_socketfd, &readfds); - FD_SET(state->status_read_fd, &readfds); - - activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); - if (activity < 0) { - sway_log(L_ERROR, "polling failed: %d", errno); - } - - if (FD_ISSET(state->ipc_event_socketfd, &readfds)) { - sway_log(L_DEBUG, "Got IPC event."); - dirty = handle_ipc_event(state); - } - - if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) { - sway_log(L_DEBUG, "Got update from status command."); - dirty = handle_status_line(state); - } - } -} - int main(int argc, char **argv) { char *socket_path = NULL; char *bar_id = NULL; bool debug = false; - state = init_state(); static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, @@ -145,72 +87,30 @@ int main(int argc, char **argv) { init_log(L_ERROR); } - state->output->registry = registry_poll(); - - if (!state->output->registry->desktop_shell) { - sway_abort("swaybar requires the compositor to support the desktop-shell extension."); - } - if (!socket_path) { socket_path = get_socketpath(); if (!socket_path) { sway_abort("Unable to retrieve socket path"); } } - 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); - if (!state->output->window) { - 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; - - /* set window height */ - set_window_height(state->output->window, state->config->height); - - if (state->config->status_command) { - int pipefd[2]; - pipe(pipefd); - state->status_command_pid = fork(); - if (state->status_command_pid == 0) { - close(pipefd[0]); - dup2(pipefd[1], STDOUT_FILENO); - close(pipefd[1]); - char *const cmd[] = { - "sh", - "-c", - state->config->status_command, - NULL, - }; - execvp(cmd[0], cmd); - return 0; - } - - close(pipefd[1]); - state->status_read_fd = pipefd[0]; - fcntl(state->status_read_fd, F_SETFL, O_NONBLOCK); - } signal(SIGTERM, sig_handler); - poll_for_update(); + state_setup(&state, socket_path, bar_id, desired_output); + + free(socket_path); + free(bar_id); + + state_run(&state); // gracefully shutdown swaybar and status_command - free_state(state); + state_teardown(&state); return 0; } diff --git a/swaybar/state.c b/swaybar/state.c index 26cdcafee..535efbc47 100644 --- a/swaybar/state.c +++ b/swaybar/state.c @@ -1,16 +1,20 @@ #include #include +#include +#include #include #include +#include "ipc-client.h" #include "list.h" #include "log.h" +#include "ipc.h" +#include "render.h" #include "config.h" #include "status_line.h" #include "state.h" -struct swaybar_state *init_state() { - struct swaybar_state *state = calloc(1, sizeof(struct swaybar_state)); +static void state_init(struct swaybar_state *state) { state->config = init_config(); state->status = init_status_line(); state->output = malloc(sizeof(struct output)); @@ -18,8 +22,106 @@ struct swaybar_state *init_state() { state->output->registry = NULL; state->output->workspaces = create_list(); state->output->name = NULL; +} - return state; +static void spawn_status_cmd_proc(struct swaybar_state *state) { + if (state->config->status_command) { + int pipefd[2]; + pipe(pipefd); + state->status_command_pid = fork(); + if (state->status_command_pid == 0) { + close(pipefd[0]); + dup2(pipefd[1], STDOUT_FILENO); + close(pipefd[1]); + char *const cmd[] = { + "sh", + "-c", + state->config->status_command, + NULL, + }; + execvp(cmd[0], cmd); + return; + } + + close(pipefd[1]); + state->status_read_fd = pipefd[0]; + fcntl(state->status_read_fd, F_SETFL, O_NONBLOCK); + } +} + + +void state_setup(struct swaybar_state *state, const char *socket_path, const char *bar_id, int desired_output) { + /* initialize state with default values */ + state_init(state); + + state->output->registry = registry_poll(); + + if (!state->output->registry->desktop_shell) { + sway_abort("swaybar requires the compositor to support the desktop-shell extension."); + } + + /* connect to sway ipc */ + state->ipc_socketfd = ipc_open_socket(socket_path); + state->ipc_event_socketfd = ipc_open_socket(socket_path); + + 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); + if (!state->output->window) { + 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; + + /* set window height */ + set_window_height(state->output->window, state->config->height); + + /* spawn status command */ + spawn_status_cmd_proc(state); +} + +void state_run(struct swaybar_state *state) { + fd_set readfds; + int activity; + bool dirty = true; + + while (1) { + if (dirty) { + struct output *output = state->output; + if (window_prerender(output->window) && output->window->cairo) { + render(output, state->config, state->status); + window_render(output->window); + if (wl_display_dispatch(output->registry->display) == -1) { + break; + } + } + } + + dirty = false; + FD_ZERO(&readfds); + FD_SET(state->ipc_event_socketfd, &readfds); + FD_SET(state->status_read_fd, &readfds); + + activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); + if (activity < 0) { + sway_log(L_ERROR, "polling failed: %d", errno); + } + + if (FD_ISSET(state->ipc_event_socketfd, &readfds)) { + sway_log(L_DEBUG, "Got IPC event."); + dirty = handle_ipc_event(state); + } + + if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) { + sway_log(L_DEBUG, "Got update from status command."); + dirty = handle_status_line(state); + } + } } void free_workspaces(list_t *workspaces) { @@ -60,7 +162,7 @@ static void terminate_status_command(pid_t pid) { } } -void free_state(struct swaybar_state *state) { +void state_teardown(struct swaybar_state *state) { free_config(state->config); free_output(state->output); free_status_line(state->status); @@ -80,6 +182,4 @@ void free_state(struct swaybar_state *state) { /* terminate status command process */ terminate_status_command(state->status_command_pid); - - free(state); } diff --git a/swaybar/state.h b/swaybar/state.h index e09807d0c..985002f84 100644 --- a/swaybar/state.h +++ b/swaybar/state.h @@ -33,9 +33,14 @@ struct workspace { }; /** - * Initialize state. + * Setup state. */ -struct swaybar_state *init_state(); +void state_setup(struct swaybar_state *state, const char *socket_path, const char *bar_id, int desired_output); + +/** + * State mainloop. + */ +void state_run(struct swaybar_state *state); /** * free workspace list. @@ -43,8 +48,8 @@ struct swaybar_state *init_state(); void free_workspaces(list_t *workspaces); /** - * Free state struct. + * Teardown state. */ -void free_state(struct swaybar_state *state); +void state_teardown(struct swaybar_state *state); #endif /* _SWAYBAR_STATE_H */ From aa6ad09183d623647bc8fd5315721bd8b196cafa Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sun, 24 Jan 2016 02:34:20 +0100 Subject: [PATCH 6/7] swaybar: rename state to bar --- swaybar/CMakeLists.txt | 2 +- swaybar/bar.c | 185 +++++++++++++++++++++++++++++++++++++ swaybar/{state.h => bar.h} | 22 ++--- swaybar/config.c | 6 +- swaybar/config.h | 6 +- swaybar/ipc.c | 46 ++++----- swaybar/ipc.h | 6 +- swaybar/main.c | 15 +-- swaybar/render.c | 8 +- swaybar/render.h | 4 +- swaybar/state.c | 185 ------------------------------------- swaybar/status_line.c | 58 ++++++------ swaybar/status_line.h | 4 +- 13 files changed, 274 insertions(+), 273 deletions(-) create mode 100644 swaybar/bar.c rename swaybar/{state.h => bar.h} (58%) delete mode 100644 swaybar/state.c diff --git a/swaybar/CMakeLists.txt b/swaybar/CMakeLists.txt index 474d8474f..7f3cb3834 100644 --- a/swaybar/CMakeLists.txt +++ b/swaybar/CMakeLists.txt @@ -10,7 +10,7 @@ add_executable(swaybar main.c config.c render.c - state.c + bar.c status_line.c ipc.c ) diff --git a/swaybar/bar.c b/swaybar/bar.c new file mode 100644 index 000000000..fc01863b8 --- /dev/null +++ b/swaybar/bar.c @@ -0,0 +1,185 @@ +#include +#include +#include +#include +#include +#include + +#include "ipc-client.h" +#include "list.h" +#include "log.h" +#include "ipc.h" +#include "render.h" +#include "config.h" +#include "status_line.h" +#include "bar.h" + +static void bar_init(struct bar *bar) { + bar->config = init_config(); + bar->status = init_status_line(); + bar->output = malloc(sizeof(struct output)); + bar->output->window = NULL; + bar->output->registry = NULL; + bar->output->workspaces = create_list(); + bar->output->name = NULL; +} + +static void spawn_status_cmd_proc(struct bar *bar) { + if (bar->config->status_command) { + int pipefd[2]; + pipe(pipefd); + bar->status_command_pid = fork(); + if (bar->status_command_pid == 0) { + close(pipefd[0]); + dup2(pipefd[1], STDOUT_FILENO); + close(pipefd[1]); + char *const cmd[] = { + "sh", + "-c", + bar->config->status_command, + NULL, + }; + execvp(cmd[0], cmd); + return; + } + + close(pipefd[1]); + bar->status_read_fd = pipefd[0]; + fcntl(bar->status_read_fd, F_SETFL, O_NONBLOCK); + } +} + + +void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id, int desired_output) { + /* initialize bar with default values */ + bar_init(bar); + + bar->output->registry = registry_poll(); + + if (!bar->output->registry->desktop_shell) { + sway_abort("swaybar requires the compositor to support the desktop-shell extension."); + } + + /* connect to sway ipc */ + bar->ipc_socketfd = ipc_open_socket(socket_path); + bar->ipc_event_socketfd = ipc_open_socket(socket_path); + + ipc_bar_init(bar, desired_output, bar_id); + + struct output_state *output = bar->output->registry->outputs->items[desired_output]; + + bar->output->window = window_setup(bar->output->registry, output->width, 30, false); + if (!bar->output->window) { + sway_abort("Failed to create window."); + } + desktop_shell_set_panel(bar->output->registry->desktop_shell, output->output, bar->output->window->surface); + desktop_shell_set_panel_position(bar->output->registry->desktop_shell, bar->config->position); + + /* set font */ + bar->output->window->font = bar->config->font; + + /* set window height */ + set_window_height(bar->output->window, bar->config->height); + + /* spawn status command */ + spawn_status_cmd_proc(bar); +} + +void bar_run(struct bar *bar) { + fd_set readfds; + int activity; + bool dirty = true; + + while (1) { + if (dirty) { + struct output *output = bar->output; + if (window_prerender(output->window) && output->window->cairo) { + render(output, bar->config, bar->status); + window_render(output->window); + if (wl_display_dispatch(output->registry->display) == -1) { + break; + } + } + } + + dirty = false; + FD_ZERO(&readfds); + FD_SET(bar->ipc_event_socketfd, &readfds); + FD_SET(bar->status_read_fd, &readfds); + + activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); + if (activity < 0) { + sway_log(L_ERROR, "polling failed: %d", errno); + } + + if (FD_ISSET(bar->ipc_event_socketfd, &readfds)) { + sway_log(L_DEBUG, "Got IPC event."); + dirty = handle_ipc_event(bar); + } + + if (bar->config->status_command && FD_ISSET(bar->status_read_fd, &readfds)) { + sway_log(L_DEBUG, "Got update from status command."); + dirty = handle_status_line(bar); + } + } +} + +void free_workspaces(list_t *workspaces) { + int i; + for (i = 0; i < workspaces->length; ++i) { + struct workspace *ws = workspaces->items[i]; + free(ws->name); + free(ws); + } + list_free(workspaces); +} + +static void free_output(struct output *output) { + window_teardown(output->window); + if (output->registry) { + registry_teardown(output->registry); + } + + free(output->name); + + if (output->workspaces) { + free_workspaces(output->workspaces); + } + + free(output); +} + +static void terminate_status_command(pid_t pid) { + if (pid) { + // terminate status_command process + int ret = kill(pid, SIGTERM); + if (ret != 0) { + sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid); + } else { + int status; + waitpid(pid, &status, 0); + } + } +} + +void bar_teardown(struct bar *bar) { + free_config(bar->config); + free_output(bar->output); + free_status_line(bar->status); + + /* close sockets/pipes */ + if (bar->status_read_fd) { + close(bar->status_read_fd); + } + + if (bar->ipc_socketfd) { + close(bar->ipc_socketfd); + } + + if (bar->ipc_event_socketfd) { + close(bar->ipc_event_socketfd); + } + + /* terminate status command process */ + terminate_status_command(bar->status_command_pid); +} diff --git a/swaybar/state.h b/swaybar/bar.h similarity index 58% rename from swaybar/state.h rename to swaybar/bar.h index 985002f84..89496da67 100644 --- a/swaybar/state.h +++ b/swaybar/bar.h @@ -1,12 +1,12 @@ -#ifndef _SWAYBAR_STATE_H -#define _SWAYBAR_STATE_H +#ifndef _SWAYBAR_BAR_H +#define _SWAYBAR_BAR_H #include "client/registry.h" #include "client/window.h" #include "list.h" -struct swaybar_state { - struct swaybar_config *config; +struct bar { + struct config *config; struct status_line *status; struct output *output; /* list_t *outputs; */ @@ -33,14 +33,14 @@ struct workspace { }; /** - * Setup state. + * Setup bar. */ -void state_setup(struct swaybar_state *state, const char *socket_path, const char *bar_id, int desired_output); +void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id, int desired_output); /** - * State mainloop. + * Bar mainloop. */ -void state_run(struct swaybar_state *state); +void bar_run(struct bar *bar); /** * free workspace list. @@ -48,8 +48,8 @@ void state_run(struct swaybar_state *state); void free_workspaces(list_t *workspaces); /** - * Teardown state. + * Teardown bar. */ -void state_teardown(struct swaybar_state *state); +void bar_teardown(struct bar *bar); -#endif /* _SWAYBAR_STATE_H */ +#endif /* _SWAYBAR_BAR_H */ diff --git a/swaybar/config.c b/swaybar/config.c index 7c03198ad..28b609e6b 100644 --- a/swaybar/config.c +++ b/swaybar/config.c @@ -41,8 +41,8 @@ char *parse_font(const char *font) { return new_font; } -struct swaybar_config *init_config() { - struct swaybar_config *config = calloc(1, sizeof(struct swaybar_config)); +struct config *init_config() { + struct config *config = calloc(1, sizeof(struct config)); config->status_command = NULL; config->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM; config->font = strdup("monospace 10"); @@ -83,7 +83,7 @@ struct swaybar_config *init_config() { return config; } -void free_config(struct swaybar_config *config) { +void free_config(struct config *config) { free(config->status_command); free(config->font); free(config->mode); diff --git a/swaybar/config.h b/swaybar/config.h index ee06668ee..508b9c425 100644 --- a/swaybar/config.h +++ b/swaybar/config.h @@ -16,7 +16,7 @@ struct box_colors { /** * Swaybar config. */ -struct swaybar_config { +struct config { char *status_command; uint32_t position; char *font; @@ -59,11 +59,11 @@ char *parse_font(const char *font); /** * Initialize default sway config. */ -struct swaybar_config *init_config(); +struct config *init_config(); /** * Free config struct. */ -void free_config(struct swaybar_config *config); +void free_config(struct config *config); #endif /* _SWAYBAR_CONFIG_H */ diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 884d02ff2..547041cec 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -7,14 +7,14 @@ #include "config.h" #include "ipc.h" -static void ipc_parse_config(struct swaybar_config *config, const char *payload) { +static void ipc_parse_config(struct 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 *tray_output, *mode, *hidden_bar, *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, "hidden_bar", &hidden_bar); 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); @@ -150,14 +150,14 @@ static void ipc_parse_config(struct swaybar_config *config, const char *payload) json_object_put(bar_config); } -static void ipc_update_workspaces(struct swaybar_state *state) { - if (state->output->workspaces) { - free_workspaces(state->output->workspaces); +static void ipc_update_workspaces(struct bar *bar) { + if (bar->output->workspaces) { + free_workspaces(bar->output->workspaces); } - state->output->workspaces = create_list(); + bar->output->workspaces = create_list(); uint32_t len = 0; - char *res = ipc_single_command(state->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len); + char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len); json_object *results = json_tokener_parse(res); if (!results) { free(res); @@ -178,14 +178,14 @@ static void ipc_update_workspaces(struct swaybar_state *state) { 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) { + if (strcmp(json_object_get_string(out), bar->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); + list_add(bar->output->workspaces, ws); } } @@ -193,36 +193,36 @@ static void ipc_update_workspaces(struct swaybar_state *state) { free(res); } -void ipc_bar_init(struct swaybar_state *state, int outputi, const char *bar_id) { +void ipc_bar_init(struct bar *bar, int outputi, const char *bar_id) { uint32_t len = 0; - char *res = ipc_single_command(state->ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len); + char *res = ipc_single_command(bar->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)); + bar->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); + res = ipc_single_command(bar->ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len); - ipc_parse_config(state->config, res); + ipc_parse_config(bar->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); + res = ipc_single_command(bar->ipc_event_socketfd, IPC_SUBSCRIBE, subscribe_json, &len); free(res); - ipc_update_workspaces(state); + ipc_update_workspaces(bar); } -bool handle_ipc_event(struct swaybar_state *state) { - struct ipc_response *resp = ipc_recv_response(state->ipc_event_socketfd); +bool handle_ipc_event(struct bar *bar) { + struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd); switch (resp->type) { case IPC_EVENT_WORKSPACE: - ipc_update_workspaces(state); + ipc_update_workspaces(bar); break; case IPC_EVENT_MODE: { json_object *result = json_tokener_parse(resp->payload); @@ -235,11 +235,11 @@ bool handle_ipc_event(struct swaybar_state *state) { if (json_object_object_get_ex(result, "change", &json_change)) { const char *change = json_object_get_string(json_change); - free(state->config->mode); + free(bar->config->mode); if (strcmp(change, "default") == 0) { - state->config->mode = NULL; + bar->config->mode = NULL; } else { - state->config->mode = strdup(change); + bar->config->mode = strdup(change); } } else { sway_log(L_ERROR, "failed to parse response"); diff --git a/swaybar/ipc.h b/swaybar/ipc.h index 06d300761..c3f661f86 100644 --- a/swaybar/ipc.h +++ b/swaybar/ipc.h @@ -1,17 +1,17 @@ #ifndef _SWAYBAR_IPC_H #define _SWAYBAR_IPC_H -#include "state.h" +#include "bar.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); +void ipc_bar_init(struct bar *bar, int outputi, const char *bar_id); /** * Handle ipc event from sway. */ -bool handle_ipc_event(struct swaybar_state *state); +bool handle_ipc_event(struct bar *bar); #endif /* _SWAYBAR_IPC_H */ diff --git a/swaybar/main.c b/swaybar/main.c index 51846bca9..fc5acdaee 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -5,17 +5,18 @@ #include #include "ipc-client.h" #include "log.h" -#include "state.h" +#include "bar.h" -struct swaybar_state state; +/* global bar state */ +struct bar swaybar; void sway_terminate(void) { - state_teardown(&state); + bar_teardown(&swaybar); exit(EXIT_FAILURE); } void sig_handler(int signal) { - state_teardown(&state); + bar_teardown(&swaybar); exit(0); } @@ -102,15 +103,15 @@ int main(int argc, char **argv) { signal(SIGTERM, sig_handler); - state_setup(&state, socket_path, bar_id, desired_output); + bar_setup(&swaybar, socket_path, bar_id, desired_output); free(socket_path); free(bar_id); - state_run(&state); + bar_run(&swaybar); // gracefully shutdown swaybar and status_command - state_teardown(&state); + bar_teardown(&swaybar); return 0; } diff --git a/swaybar/render.c b/swaybar/render.c index 9ac0c766d..f3ce60101 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -55,7 +55,7 @@ static void render_sharp_line(cairo_t *cairo, uint32_t color, double x, double y } } -static void render_block(struct window *window, struct swaybar_config *config, struct status_block *block, double *x, bool edge) { +static void render_block(struct window *window, struct config *config, struct status_block *block, double *x, bool edge) { int width, height, sep_width; get_text_size(window, &width, &height, "%s", block->full_text); @@ -203,7 +203,7 @@ static char *handle_workspace_number(bool strip_num, const char *ws_name) { return strdup(ws_name); } -static void render_workspace_button(struct window *window, struct swaybar_config *config, struct workspace *ws, double *x) { +static void render_workspace_button(struct window *window, struct config *config, struct workspace *ws, double *x) { // strip workspace numbers if required char *name = handle_workspace_number(config->strip_workspace_numbers, ws->name); @@ -242,7 +242,7 @@ static void render_workspace_button(struct window *window, struct swaybar_config free(name); } -static void render_binding_mode_indicator(struct window *window, struct swaybar_config *config, double pos) { +static void render_binding_mode_indicator(struct window *window, struct config *config, double pos) { int width, height; get_text_size(window, &width, &height, "%s", config->mode); @@ -264,7 +264,7 @@ static void render_binding_mode_indicator(struct window *window, struct swaybar_ pango_printf(window, "%s", config->mode); } -void render(struct output *output, struct swaybar_config *config, struct status_line *line) { +void render(struct output *output, struct config *config, struct status_line *line) { int i; struct window *window = output->window; diff --git a/swaybar/render.h b/swaybar/render.h index 527cf76a0..931a1cdd6 100644 --- a/swaybar/render.h +++ b/swaybar/render.h @@ -2,12 +2,12 @@ #define _SWAYBAR_RENDER_H #include "config.h" -#include "state.h" +#include "bar.h" /** * Render swaybar. */ -void render(struct output *output, struct swaybar_config *config, struct status_line *line); +void render(struct output *output, struct config *config, struct status_line *line); /** * Set window height and modify internal spacing accordingly. diff --git a/swaybar/state.c b/swaybar/state.c deleted file mode 100644 index 535efbc47..000000000 --- a/swaybar/state.c +++ /dev/null @@ -1,185 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "ipc-client.h" -#include "list.h" -#include "log.h" -#include "ipc.h" -#include "render.h" -#include "config.h" -#include "status_line.h" -#include "state.h" - -static void state_init(struct swaybar_state *state) { - state->config = init_config(); - state->status = init_status_line(); - state->output = malloc(sizeof(struct output)); - state->output->window = NULL; - state->output->registry = NULL; - state->output->workspaces = create_list(); - state->output->name = NULL; -} - -static void spawn_status_cmd_proc(struct swaybar_state *state) { - if (state->config->status_command) { - int pipefd[2]; - pipe(pipefd); - state->status_command_pid = fork(); - if (state->status_command_pid == 0) { - close(pipefd[0]); - dup2(pipefd[1], STDOUT_FILENO); - close(pipefd[1]); - char *const cmd[] = { - "sh", - "-c", - state->config->status_command, - NULL, - }; - execvp(cmd[0], cmd); - return; - } - - close(pipefd[1]); - state->status_read_fd = pipefd[0]; - fcntl(state->status_read_fd, F_SETFL, O_NONBLOCK); - } -} - - -void state_setup(struct swaybar_state *state, const char *socket_path, const char *bar_id, int desired_output) { - /* initialize state with default values */ - state_init(state); - - state->output->registry = registry_poll(); - - if (!state->output->registry->desktop_shell) { - sway_abort("swaybar requires the compositor to support the desktop-shell extension."); - } - - /* connect to sway ipc */ - state->ipc_socketfd = ipc_open_socket(socket_path); - state->ipc_event_socketfd = ipc_open_socket(socket_path); - - 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); - if (!state->output->window) { - 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; - - /* set window height */ - set_window_height(state->output->window, state->config->height); - - /* spawn status command */ - spawn_status_cmd_proc(state); -} - -void state_run(struct swaybar_state *state) { - fd_set readfds; - int activity; - bool dirty = true; - - while (1) { - if (dirty) { - struct output *output = state->output; - if (window_prerender(output->window) && output->window->cairo) { - render(output, state->config, state->status); - window_render(output->window); - if (wl_display_dispatch(output->registry->display) == -1) { - break; - } - } - } - - dirty = false; - FD_ZERO(&readfds); - FD_SET(state->ipc_event_socketfd, &readfds); - FD_SET(state->status_read_fd, &readfds); - - activity = select(FD_SETSIZE, &readfds, NULL, NULL, NULL); - if (activity < 0) { - sway_log(L_ERROR, "polling failed: %d", errno); - } - - if (FD_ISSET(state->ipc_event_socketfd, &readfds)) { - sway_log(L_DEBUG, "Got IPC event."); - dirty = handle_ipc_event(state); - } - - if (state->config->status_command && FD_ISSET(state->status_read_fd, &readfds)) { - sway_log(L_DEBUG, "Got update from status command."); - dirty = handle_status_line(state); - } - } -} - -void free_workspaces(list_t *workspaces) { - int i; - for (i = 0; i < workspaces->length; ++i) { - struct workspace *ws = workspaces->items[i]; - free(ws->name); - free(ws); - } - list_free(workspaces); -} - -static void free_output(struct output *output) { - window_teardown(output->window); - if (output->registry) { - registry_teardown(output->registry); - } - - free(output->name); - - if (output->workspaces) { - free_workspaces(output->workspaces); - } - - free(output); -} - -static void terminate_status_command(pid_t pid) { - if (pid) { - // terminate status_command process - int ret = kill(pid, SIGTERM); - if (ret != 0) { - sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid); - } else { - int status; - waitpid(pid, &status, 0); - } - } -} - -void state_teardown(struct swaybar_state *state) { - free_config(state->config); - free_output(state->output); - free_status_line(state->status); - - /* close sockets/pipes */ - if (state->status_read_fd) { - close(state->status_read_fd); - } - - if (state->ipc_socketfd) { - close(state->ipc_socketfd); - } - - if (state->ipc_event_socketfd) { - close(state->ipc_event_socketfd); - } - - /* terminate status command process */ - terminate_status_command(state->status_command_pid); -} diff --git a/swaybar/status_line.c b/swaybar/status_line.c index a072673b7..6b630c490 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c @@ -19,7 +19,7 @@ struct { char *parserpos; bool escape; int depth; - int state[I3JSON_MAXDEPTH+1]; + int bar[I3JSON_MAXDEPTH+1]; } i3json_state = { 0, NULL, NULL, NULL, false, 0, { I3JSON_UNKNOWN } }; static char line[1024]; @@ -48,7 +48,7 @@ static void free_status_block(void *item) { free(sb); } -static void parse_json(struct swaybar_state *state, const char *text) { +static void parse_json(struct bar *bar, 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 *state, const char *text) { return; } - if (state->status->block_line) { - list_foreach(state->status->block_line, free_status_block); - list_free(state->status->block_line); + if (bar->status->block_line) { + list_foreach(bar->status->block_line, free_status_block); + list_free(bar->status->block_line); } - state->status->block_line = create_list(); + bar->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 *state, const char *text) { if (color) { new->color = parse_color(json_object_get_string(color)); } else { - new->color = state->config->colors.statusline; + new->color = bar->config->colors.statusline; } if (min_width) { @@ -188,18 +188,18 @@ static void parse_json(struct swaybar_state *state, const char *text) { new->border_right = 1; } - list_add(state->status->block_line, new); + list_add(bar->status->block_line, new); } json_object_put(results); } // continue parsing from last parserpos -static int i3json_parse(struct swaybar_state *st) { +static int i3json_parse(struct bar *bar) { char *c = i3json_state.parserpos; int handled = 0; while (*c) { - if (i3json_state.state[i3json_state.depth] == I3JSON_STRING) { + if (i3json_state.bar[i3json_state.depth] == I3JSON_STRING) { if (!i3json_state.escape && *c == '"') { --i3json_state.depth; } @@ -211,13 +211,13 @@ static int i3json_parse(struct swaybar_state *st) { if (i3json_state.depth > I3JSON_MAXDEPTH) { sway_abort("JSON too deep"); } - i3json_state.state[i3json_state.depth] = I3JSON_ARRAY; + i3json_state.bar[i3json_state.depth] = I3JSON_ARRAY; if (i3json_state.depth == 2) { i3json_state.line_start = c; } break; case ']': - if (i3json_state.state[i3json_state.depth] != I3JSON_ARRAY) { + if (i3json_state.bar[i3json_state.depth] != I3JSON_ARRAY) { sway_abort("JSON malformed"); } --i3json_state.depth; @@ -225,7 +225,7 @@ static int i3json_parse(struct swaybar_state *st) { // c[1] is valid since c[0] != '\0' char p = c[1]; c[1] = '\0'; - parse_json(st, i3json_state.line_start); + parse_json(bar, i3json_state.line_start); c[1] = p; ++handled; i3json_state.line_start = c+1; @@ -236,7 +236,7 @@ static int i3json_parse(struct swaybar_state *st) { if (i3json_state.depth > I3JSON_MAXDEPTH) { sway_abort("JSON too deep"); } - i3json_state.state[i3json_state.depth] = I3JSON_STRING; + i3json_state.bar[i3json_state.depth] = I3JSON_STRING; break; } } @@ -361,49 +361,49 @@ static void i3json_ensure_free(int min_free) { } // append data and parse it. -static int i3json_handle_data(struct swaybar_state *st, char *data) { +static int i3json_handle_data(struct bar *bar, char *data) { int len = strlen(data); i3json_ensure_free(len); strcpy(i3json_state.parserpos, data); - return i3json_parse(st); + return i3json_parse(bar); } // read data from fd and parse it. -static int i3json_handle_fd(struct swaybar_state *state) { +static int i3json_handle_fd(struct bar *bar) { i3json_ensure_free(10240); // get fresh data at the end of the buffer - int readlen = read(state->status_read_fd, i3json_state.parserpos, 10239); + int readlen = read(bar->status_read_fd, i3json_state.parserpos, 10239); if (readlen < 0) { return readlen; } i3json_state.parserpos[readlen] = '\0'; - return i3json_parse(state); + return i3json_parse(bar); } -bool handle_status_line(struct swaybar_state *state) { +bool handle_status_line(struct bar *bar) { bool dirty = false; - switch (state->status->protocol) { + switch (bar->status->protocol) { case I3BAR: sway_log(L_DEBUG, "Got i3bar protocol."); - if (i3json_handle_fd(state) > 0) { + if (i3json_handle_fd(bar) > 0) { dirty = true; } break; case TEXT: sway_log(L_DEBUG, "Got text protocol."); - read_line_tail(state->status_read_fd, line, sizeof(line), line_rest); + read_line_tail(bar->status_read_fd, line, sizeof(line), line_rest); dirty = true; - state->status->text_line = line; + bar->status->text_line = line; break; case UNDEF: sway_log(L_DEBUG, "Detecting protocol..."); - if (read_line_tail(state->status_read_fd, line, sizeof(line), line_rest) < 0) { + if (read_line_tail(bar->status_read_fd, line, sizeof(line), line_rest) < 0) { break; } dirty = true; - state->status->text_line = line; - state->status->protocol = TEXT; + bar->status->text_line = line; + bar->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 *state) { && json_object_get_int(version) == 1 ) { sway_log(L_DEBUG, "Switched to i3bar protocol."); - state->status->protocol = I3BAR; - i3json_handle_data(state, line_rest); + bar->status->protocol = I3BAR; + i3json_handle_data(bar, line_rest); } json_object_put(proto); } diff --git a/swaybar/status_line.h b/swaybar/status_line.h index 1d73dd574..273542dc8 100644 --- a/swaybar/status_line.h +++ b/swaybar/status_line.h @@ -5,7 +5,7 @@ #include #include "list.h" -#include "state.h" +#include "bar.h" typedef enum {UNDEF, TEXT, I3BAR} command_protocol; @@ -40,7 +40,7 @@ struct status_line *init_status_line(); /** * handle status line activity. */ -bool handle_status_line(struct swaybar_state *st); +bool handle_status_line(struct bar *bar); /** * Free status line struct. From a61a27c485d1b20fc7c5cbfc42b5d90148d82355 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Sun, 24 Jan 2016 03:06:18 +0100 Subject: [PATCH 7/7] swaybar: cleanup CmakeLists.txt --- swaybar/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/swaybar/CMakeLists.txt b/swaybar/CMakeLists.txt index 7f3cb3834..60975f404 100644 --- a/swaybar/CMakeLists.txt +++ b/swaybar/CMakeLists.txt @@ -23,7 +23,6 @@ target_link_libraries(swaybar ${CAIRO_LIBRARIES} ${PANGO_LIBRARIES} ${JSONC_LIBRARIES} - m ) install(