diff options
-rw-r--r-- | README.ja.md | 2 | ||||
-rw-r--r-- | README.md | 8 | ||||
-rw-r--r-- | common/pango.c | 53 | ||||
-rw-r--r-- | common/readline.c | 25 | ||||
-rw-r--r-- | include/pango.h | 14 | ||||
-rw-r--r-- | include/sway/commands.h | 1 | ||||
-rw-r--r-- | include/sway/config.h | 4 | ||||
-rw-r--r-- | include/sway/tree/view.h | 2 | ||||
-rw-r--r-- | include/swaybar/event_loop.h | 4 | ||||
-rw-r--r-- | include/swaybar/status_line.h | 32 | ||||
-rw-r--r-- | sway/commands.c | 14 | ||||
-rw-r--r-- | sway/commands/create_output.c | 45 | ||||
-rw-r--r-- | sway/config/bar.c | 26 | ||||
-rw-r--r-- | sway/config/input.c | 7 | ||||
-rw-r--r-- | sway/desktop/render.c | 11 | ||||
-rw-r--r-- | sway/desktop/transaction.c | 11 | ||||
-rw-r--r-- | sway/ipc-json.c | 2 | ||||
-rw-r--r-- | sway/main.c | 1 | ||||
-rw-r--r-- | sway/meson.build | 1 | ||||
-rw-r--r-- | sway/tree/output.c | 2 | ||||
-rw-r--r-- | sway/tree/view.c | 10 | ||||
-rw-r--r-- | swaybar/bar.c | 112 | ||||
-rw-r--r-- | swaybar/event_loop.c | 4 | ||||
-rw-r--r-- | swaybar/i3bar.c | 216 | ||||
-rw-r--r-- | swaybar/render.c | 33 | ||||
-rw-r--r-- | swaybar/status_line.c | 148 |
26 files changed, 444 insertions, 344 deletions
diff --git a/README.ja.md b/README.ja.md index 75d29c73..b0488c53 100644 --- a/README.ja.md +++ b/README.ja.md @@ -5,7 +5,7 @@ i3互換な[Wayland](http://wayland.freedesktop.org/)コンポジタです。 [FAQ](https://github.com/swaywm/sway/wiki)も合わせてご覧ください。 [IRC チャンネル](http://webchat.freenode.net/?channels=sway&uio=d4) (#sway on irc.freenode.net)もあります。 -**注意**: Swayは現在*凍結中*であり、Swayとwlrootsの統合が完了するまで、新たな機能は追加されません。バグフィックスは行われます。詳しくは[この記事](https://drewdevault.com/2017/10/09/Future-of-sway.html)をご覧ください。wlrootsとの統合状況については、[このチケット](https://github.com/swaywm/sway/issues/1390)をご覧ください。 +**注意**: Swayは現在*凍結中*であり、wlcからwlrootsへの移植が完了するまで新たな機能は追加されません。2018年9月以降に発見されるバグは0.15では対応されません。詳しくは[この記事](https://drewdevault.com/2017/10/09/Future-of-sway.html)をご覧ください。wlrootsとの統合状況については、[このチケット](https://github.com/swaywm/sway/issues/1390)をご覧ください。 [![](https://sr.ht/ICd5.png)](https://sr.ht/ICd5.png) @@ -9,10 +9,10 @@ Read the [FAQ](https://github.com/swaywm/sway/wiki). Join the [IRC channel](http://webchat.freenode.net/?channels=sway&uio=d4) (#sway on irc.freenode.net). -**Notice**: You are viewing the **unstable** and **unsupported** master branch -of sway, where work is ongoing to port it to -[wlroots](https://github.com/swaywm/wlroots). The supported branch is the 0.15 -branch, and end users are encouraged to use the stable releases cut from it. +**Notice**: work is well underway to port sway to +[wlroots](https://github.com/swaywm/wlroots). This is **unstable** and +**unsupported** - we accept patches, but are not fond of bug reports. We are no +longer accepting bugs for 0.15. If you'd like to support sway development, please contribute to [SirCmpwn's Patreon page](https://patreon.com/sircmpwn). diff --git a/common/pango.c b/common/pango.c index ea71ac4a..5afd72d8 100644 --- a/common/pango.c +++ b/common/pango.c @@ -7,66 +7,45 @@ #include <stdlib.h> #include <string.h> #include "log.h" +#include "stringop.h" -int escape_markup_text(const char *src, char *dest, int dest_length) { - int length = 0; +size_t escape_markup_text(const char *src, char *dest) { + size_t length = 0; + if (dest) { + dest[0] = '\0'; + } while (src[0]) { switch (src[0]) { case '&': length += 5; - if (dest && dest_length - length >= 0) { - dest += sprintf(dest, "%s", "&"); - } else { - dest_length = -1; - } + lenient_strcat(dest, "&"); break; case '<': length += 4; - if (dest && dest_length - length >= 0) { - dest += sprintf(dest, "%s", "<"); - } else { - dest_length = -1; - } + lenient_strcat(dest, "<"); break; case '>': length += 4; - if (dest && dest_length - length >= 0) { - dest += sprintf(dest, "%s", ">"); - } else { - dest_length = -1; - } + lenient_strcat(dest, ">"); break; case '\'': length += 6; - if (dest && dest_length - length >= 0) { - dest += sprintf(dest, "%s", "'"); - } else { - dest_length = -1; - } + lenient_strcat(dest, "'"); break; case '"': length += 6; - if (dest && dest_length - length >= 0) { - dest += sprintf(dest, "%s", """); - } else { - dest_length = -1; - } + lenient_strcat(dest, """); break; default: - length += 1; - if (dest && dest_length - length >= 0) { - *(dest++) = *src; - } else { - dest_length = -1; + if (dest) { + dest[length] = *src; + dest[length + 1] = '\0'; } + length += 1; } src++; } - // if we could not fit the escaped string in dest, return -1 - if (dest && dest_length == -1) { - return -1; - } return length; } @@ -78,7 +57,7 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, char *buf; GError *error = NULL; if (pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, &error)) { - pango_layout_set_markup(layout, buf, -1); + pango_layout_set_text(layout, buf, -1); free(buf); } else { wlr_log(WLR_ERROR, "pango_parse_markup '%s' -> error %s", text, diff --git a/common/readline.c b/common/readline.c index a2c69018..58652429 100644 --- a/common/readline.c +++ b/common/readline.c @@ -70,28 +70,3 @@ char *peek_line(FILE *file, int line_offset, long *position) { fseek(file, pos, SEEK_SET); return line; } - -char *read_line_buffer(FILE *file, char *string, size_t string_len) { - size_t length = 0; - if (!string) { - return NULL; - } - while (1) { - int c = getc(file); - if (c == EOF || c == '\n' || c == '\0') { - break; - } - if (c == '\r') { - continue; - } - string[length++] = c; - if (string_len <= length) { - return NULL; - } - } - if (length + 1 == string_len) { - return NULL; - } - string[length] = '\0'; - return string; -} diff --git a/include/pango.h b/include/pango.h index 09a535a5..6ab83c16 100644 --- a/include/pango.h +++ b/include/pango.h @@ -6,17 +6,13 @@ #include <cairo/cairo.h> #include <pango/pangocairo.h> -/* Utility function which escape characters a & < > ' ". +/** + * Utility function which escape characters a & < > ' ". * - * If the dest parameter is NULL, then the function returns the length of - * of the escaped src string. The dest_length doesn't matter. - * - * If the dest parameter is not NULL then the fuction escapes the src string - * an puts the escaped string in dest and returns the lenght of the escaped string. - * The dest_length parameter is the size of dest array. If the size of dest is not - * enough, then the function returns -1. + * The function returns the length of the escaped string, optionally writing the + * escaped string to dest if provided. */ -int escape_markup_text(const char *src, char *dest, int dest_length); +size_t escape_markup_text(const char *src, char *dest); PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text, double scale, bool markup); void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, diff --git a/include/sway/commands.h b/include/sway/commands.h index e51b12fd..226cf932 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -103,6 +103,7 @@ sway_cmd cmd_client_urgent; sway_cmd cmd_client_placeholder; sway_cmd cmd_client_background; sway_cmd cmd_commands; +sway_cmd cmd_create_output; sway_cmd cmd_debuglog; sway_cmd cmd_default_border; sway_cmd cmd_default_floating_border; diff --git a/include/sway/config.h b/include/sway/config.h index b53c1f1f..36d78ec6 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -512,9 +512,7 @@ void free_sway_binding(struct sway_binding *sb); void seat_execute_command(struct sway_seat *seat, struct sway_binding *binding); -void load_swaybars(); - -void invoke_swaybar(struct bar_config *bar); +void load_swaybars(void); void terminate_swaybg(pid_t pid); diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h index 439dc1bf..d10251dd 100644 --- a/include/sway/tree/view.h +++ b/include/sway/tree/view.h @@ -61,6 +61,8 @@ struct sway_view { struct sway_container *container; // NULL if unmapped and transactions finished struct wlr_surface *surface; // NULL for unmapped views + pid_t pid; + // Geometry of the view itself (excludes borders) in layout coordinates double x, y; int width, height; diff --git a/include/swaybar/event_loop.h b/include/swaybar/event_loop.h index 99f6ed36..47be5b79 100644 --- a/include/swaybar/event_loop.h +++ b/include/swaybar/event_loop.h @@ -19,8 +19,8 @@ bool remove_event(int fd); bool remove_timer(timer_t timer); // Blocks and returns after sending callbacks -void event_loop_poll(); +void event_loop_poll(void); -void init_event_loop(); +void init_event_loop(void); #endif diff --git a/include/swaybar/status_line.h b/include/swaybar/status_line.h index 150267cd..d3eabdf6 100644 --- a/include/swaybar/status_line.h +++ b/include/swaybar/status_line.h @@ -1,5 +1,6 @@ #ifndef _SWAYBAR_STATUS_LINE_H #define _SWAYBAR_STATUS_LINE_H +#include <json-c/json.h> #include <stdint.h> #include <stdio.h> #include <stdbool.h> @@ -12,28 +13,6 @@ enum status_protocol { PROTOCOL_I3BAR, }; -struct text_protocol_state { - char *buffer; - size_t buffer_size; -}; - -enum json_node_type { - JSON_NODE_UNKNOWN, - JSON_NODE_ARRAY, - JSON_NODE_STRING, -}; - -struct i3bar_protocol_state { - bool click_events; - char *buffer; - size_t buffer_size; - size_t buffer_index; - const char *current_node; - bool escape; - size_t depth; - enum json_node_type nodes[16]; -}; - struct i3bar_block { struct wl_list link; int ref_count; @@ -63,8 +42,13 @@ struct status_line { const char *text; struct wl_list blocks; // i3bar_block::link - struct text_protocol_state text_state; - struct i3bar_protocol_state i3bar_state; + bool click_events; + char *buffer; + size_t buffer_size; + size_t buffer_index; + bool started; + bool expecting_comma; + json_tokener *tokener; }; struct status_line *status_line_init(char *cmd); diff --git a/sway/commands.c b/sway/commands.c index 41e1c653..07169f1e 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -143,6 +143,7 @@ static struct cmd_handler config_handlers[] = { /* Runtime-only commands. Keep alphabetized */ static struct cmd_handler command_handlers[] = { { "border", cmd_border }, + { "create_output", cmd_create_output }, { "exit", cmd_exit }, { "floating", cmd_floating }, { "fullscreen", cmd_fullscreen }, @@ -215,18 +216,23 @@ struct cmd_handler *find_handler(char *line, struct cmd_handler *cmd_handlers, static void set_config_node(struct sway_node *node) { config->handler_context.node = node; + config->handler_context.container = NULL; + config->handler_context.workspace = NULL; + + if (node == NULL) { + return; + } + switch (node->type) { case N_CONTAINER: config->handler_context.container = node->sway_container; config->handler_context.workspace = node->sway_container->workspace; break; case N_WORKSPACE: - config->handler_context.container = NULL; config->handler_context.workspace = node->sway_workspace; break; - default: - config->handler_context.container = NULL; - config->handler_context.workspace = NULL; + case N_ROOT: + case N_OUTPUT: break; } } diff --git a/sway/commands/create_output.c b/sway/commands/create_output.c new file mode 100644 index 00000000..1c2464ea --- /dev/null +++ b/sway/commands/create_output.c @@ -0,0 +1,45 @@ +#include <wlr/config.h> +#include <wlr/backend/multi.h> +#include <wlr/backend/wayland.h> +#ifdef WLR_HAS_X11_BACKEND +#include <wlr/backend/x11.h> +#endif +#include "sway/commands.h" +#include "sway/server.h" +#include "log.h" + +static void create_output(struct wlr_backend *backend, void *data) { + bool *done = data; + if (*done) { + return; + } + + if (wlr_backend_is_wl(backend)) { + wlr_wl_output_create(backend); + *done = true; + } +#ifdef WLR_HAS_X11_BACKEND + else if (wlr_backend_is_x11(backend)) { + wlr_x11_output_create(backend); + *done = true; + } +#endif +} + +/** + * This command is intended for developer use only. + */ +struct cmd_results *cmd_create_output(int argc, char **argv) { + sway_assert(wlr_backend_is_multi(server.backend), + "Expected a multi backend"); + + bool done = false; + wlr_multi_for_each_backend(server.backend, create_output, &done); + + if (!done) { + return cmd_results_new(CMD_INVALID, "create_output", + "Can only create outputs for Wayland or X11 backends"); + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} diff --git a/sway/config/bar.c b/sway/config/bar.c index f83b37d1..48a632fb 100644 --- a/sway/config/bar.c +++ b/sway/config/bar.c @@ -165,7 +165,7 @@ cleanup: return NULL; } -void invoke_swaybar(struct bar_config *bar) { +static void invoke_swaybar(struct bar_config *bar) { // Pipe to communicate errors int filedes[2]; if (pipe(filedes) == -1) { @@ -219,27 +219,13 @@ void invoke_swaybar(struct bar_config *bar) { close(filedes[1]); } -void load_swaybars() { +void load_swaybars(void) { for (int i = 0; i < config->bars->length; ++i) { struct bar_config *bar = config->bars->items[i]; - bool apply = false; - if (bar->outputs) { - for (int j = 0; j < bar->outputs->length; ++j) { - char *o = bar->outputs->items[j]; - if (!strcmp(o, "*") || output_by_name(o)) { - apply = true; - break; - } - } - } else { - apply = true; - } - if (apply) { - if (bar->pid != 0) { - terminate_swaybar(bar->pid); - } - wlr_log(WLR_DEBUG, "Invoking swaybar for bar id '%s'", bar->id); - invoke_swaybar(bar); + if (bar->pid != 0) { + terminate_swaybar(bar->pid); } + wlr_log(WLR_DEBUG, "Invoking swaybar for bar id '%s'", bar->id); + invoke_swaybar(bar); } } diff --git a/sway/config/input.c b/sway/config/input.c index 9885e85c..ad5b96c8 100644 --- a/sway/config/input.c +++ b/sway/config/input.c @@ -140,6 +140,13 @@ void free_input_config(struct input_config *ic) { return; } free(ic->identifier); + free(ic->xkb_layout); + free(ic->xkb_model); + free(ic->xkb_options); + free(ic->xkb_rules); + free(ic->xkb_variant); + free(ic->mapped_from_region); + free(ic->mapped_to_output); free(ic); } diff --git a/sway/desktop/render.c b/sway/desktop/render.c index 1d2f445d..af4e2905 100644 --- a/sway/desktop/render.c +++ b/sway/desktop/render.c @@ -914,12 +914,17 @@ void output_render(struct sway_output *output, struct timespec *when, struct wlr_output *wlr_output = output->wlr_output; struct wlr_renderer *renderer = - wlr_backend_get_renderer(wlr_output->backend); + wlr_backend_get_renderer(wlr_output->backend); if (!sway_assert(renderer != NULL, "expected the output backend to have a renderer")) { return; } + struct sway_workspace *workspace = output->current.active_workspace; + if (workspace == NULL) { + return; + } + wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height); if (!pixman_region32_not_empty(damage)) { @@ -935,13 +940,11 @@ void output_render(struct sway_output *output, struct timespec *when, pixman_region32_union_rect(damage, damage, 0, 0, width, height); } - struct sway_workspace *workspace = output->current.active_workspace; - struct sway_container *fullscreen_con = workspace->current.fullscreen; - if (output_has_opaque_overlay_layer_surface(output)) { goto render_overlay; } + struct sway_container *fullscreen_con = workspace->current.fullscreen; if (fullscreen_con) { float clear_color[] = {0.0f, 0.0f, 0.0f, 1.0f}; diff --git a/sway/desktop/transaction.c b/sway/desktop/transaction.c index d747e279..797f6b4c 100644 --- a/sway/desktop/transaction.c +++ b/sway/desktop/transaction.c @@ -6,6 +6,7 @@ #include <string.h> #include <time.h> #include <wlr/types/wlr_buffer.h> +#include "sway/config.h" #include "sway/debug.h" #include "sway/desktop.h" #include "sway/desktop/idle_inhibit_v1.h" @@ -390,6 +391,16 @@ static bool should_configure(struct sway_node *node, } struct sway_container_state *cstate = &node->sway_container->current; struct sway_container_state *istate = instruction->container_state; +#ifdef HAVE_XWAYLAND + // Xwayland views are position-aware and need to be reconfigured + // when their position changes. + if (node->sway_container->view->type == SWAY_VIEW_XWAYLAND) { + if (cstate->view_x != istate->view_x || + cstate->view_y != istate->view_y) { + return true; + } + } +#endif if (cstate->view_width == istate->view_width && cstate->view_height == istate->view_height) { return false; diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 52278be2..f054ac9f 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -221,6 +221,8 @@ static const char *describe_container_border(enum sway_container_border border) } static void ipc_json_describe_view(struct sway_container *c, json_object *object) { + json_object_object_add(object, "pid", json_object_new_int(c->view->pid)); + const char *app_id = view_get_app_id(c->view); json_object_object_add(object, "app_id", app_id ? json_object_new_string(app_id) : NULL); diff --git a/sway/main.c b/sway/main.c index fb4f0d8c..3d7cd158 100644 --- a/sway/main.c +++ b/sway/main.c @@ -424,6 +424,7 @@ int main(int argc, char **argv) { } config->active = true; + load_swaybars(); // Execute commands until there are none left wlr_log(WLR_DEBUG, "Running deferred commands"); while (config->cmd_queue->length) { diff --git a/sway/meson.build b/sway/meson.build index 01c83a33..d67a4c64 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -35,6 +35,7 @@ sway_sources = files( 'commands/bind.c', 'commands/border.c', 'commands/client.c', + 'commands/create_output.c', 'commands/default_border.c', 'commands/default_floating_border.c', 'commands/default_orientation.c', diff --git a/sway/tree/output.c b/sway/tree/output.c index 1976ad51..06933dc4 100644 --- a/sway/tree/output.c +++ b/sway/tree/output.c @@ -109,8 +109,6 @@ void output_enable(struct sway_output *output, struct output_config *oc) { wl_signal_emit(&root->events.new_node, &output->node); - load_swaybars(); - arrange_layers(output); arrange_root(); } diff --git a/sway/tree/view.c b/sway/tree/view.c index e4e1c161..f61f5c84 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -470,6 +470,7 @@ static struct sway_workspace *select_workspace(struct sway_view *view) { wl_resource_get_client(view->surface->resource); wl_client_get_credentials(client, &pid, NULL, NULL); #endif + view->pid = pid; ws = root_workspace_for_pid(pid); if (ws) { return ws; @@ -784,14 +785,9 @@ static size_t parse_title_format(struct sway_view *view, char *buffer) { } static char *escape_title(char *buffer) { - int length = escape_markup_text(buffer, NULL, 0); + size_t length = escape_markup_text(buffer, NULL); char *escaped_title = calloc(length + 1, sizeof(char)); - int result = escape_markup_text(buffer, escaped_title, length); - if (result != length) { - wlr_log(WLR_ERROR, "Could not escape title: %s", buffer); - free(escaped_title); - return buffer; - } + escape_markup_text(buffer, escaped_title); free(buffer); return escaped_title; } diff --git a/swaybar/bar.c b/swaybar/bar.c index 3ae730f7..69069f40 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -48,8 +48,13 @@ static void swaybar_output_free(struct swaybar_output *output) { return; } wlr_log(WLR_DEBUG, "Removing output %s", output->name); - zwlr_layer_surface_v1_destroy(output->layer_surface); - wl_surface_destroy(output->surface); + if (output->layer_surface != NULL) { + zwlr_layer_surface_v1_destroy(output->layer_surface); + } + if (output->surface != NULL) { + wl_surface_destroy(output->surface); + } + zxdg_output_v1_destroy(output->xdg_output); wl_output_destroy(output->output); destroy_buffer(&output->buffers[0]); destroy_buffer(&output->buffers[1]); @@ -283,6 +288,37 @@ const struct wl_seat_listener seat_listener = { .name = seat_handle_name, }; +static void add_layer_surface(struct swaybar_output *output) { + if (output->surface != NULL) { + return; + } + struct swaybar *bar = output->bar; + + output->surface = wl_compositor_create_surface(bar->compositor); + assert(output->surface); + output->layer_surface = zwlr_layer_shell_v1_get_layer_surface( + bar->layer_shell, output->surface, output->output, + ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "panel"); + assert(output->layer_surface); + zwlr_layer_surface_v1_add_listener(output->layer_surface, + &layer_surface_listener, output); + zwlr_layer_surface_v1_set_anchor(output->layer_surface, + bar->config->position); +} + +static bool bar_uses_output(struct swaybar *bar, const char *name) { + if (bar->config->all_outputs) { + return true; + } + struct config_output *coutput; + wl_list_for_each(coutput, &bar->config->outputs, link) { + if (strcmp(coutput->name, name) == 0) { + return true; + } + } + return false; +} + static void output_geometry(void *data, struct wl_output *output, int32_t x, int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel, const char *make, const char *model, int32_t transform) { @@ -326,7 +362,22 @@ static void xdg_output_handle_logical_size(void *data, static void xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output) { - // Who cares + struct swaybar_output *output = data; + struct swaybar *bar = output->bar; + + assert(output->name != NULL); + if (!bar_uses_output(bar, output->name)) { + swaybar_output_free(output); + return; + } + + if (wl_list_empty(&output->link)) { + wl_list_remove(&output->link); + wl_list_insert(&bar->outputs, &output->link); + + add_layer_surface(output); + render_frame(bar, output); + } } static void xdg_output_handle_name(void *data, @@ -349,17 +400,15 @@ struct zxdg_output_v1_listener xdg_output_listener = { .description = xdg_output_handle_description, }; -static bool bar_uses_output(struct swaybar *bar, const char *name) { - if (bar->config->all_outputs) { - return true; - } - struct config_output *coutput; - wl_list_for_each(coutput, &bar->config->outputs, link) { - if (strcmp(coutput->name, name) == 0) { - return true; - } +static void add_xdg_output(struct swaybar_output *output) { + if (output->xdg_output != NULL) { + return; } - return false; + assert(output->bar->xdg_output_manager != NULL); + output->xdg_output = zxdg_output_manager_v1_get_xdg_output( + output->bar->xdg_output_manager, output->output); + zxdg_output_v1_add_listener(output->xdg_output, &xdg_output_listener, + output); } static void handle_global(void *data, struct wl_registry *registry, @@ -386,7 +435,10 @@ static void handle_global(void *data, struct wl_registry *registry, output->wl_name = name; wl_list_init(&output->workspaces); wl_list_init(&output->hotspots); - wl_list_insert(&bar->outputs, &output->link); + wl_list_init(&output->link); + if (bar->xdg_output_manager != NULL) { + add_xdg_output(output); + } } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { bar->layer_shell = wl_registry_bind( registry, name, &zwlr_layer_shell_v1_interface, 1); @@ -416,7 +468,9 @@ static const struct wl_registry_listener registry_listener = { static void render_all_frames(struct swaybar *bar) { struct swaybar_output *output; wl_list_for_each(output, &bar->outputs, link) { - render_frame(bar, output); + if (output->surface != NULL) { + render_frame(bar, output); + } } } @@ -443,23 +497,10 @@ void bar_setup(struct swaybar *bar, struct swaybar_output *output; wl_list_for_each(output, &bar->outputs, link) { - output->xdg_output = zxdg_output_manager_v1_get_xdg_output( - bar->xdg_output_manager, output->output); - zxdg_output_v1_add_listener(output->xdg_output, &xdg_output_listener, - output); + add_xdg_output(output); } wl_display_roundtrip(bar->display); - struct swaybar_output *output_tmp; - wl_list_for_each_safe(output, output_tmp, &bar->outputs, link) { - if (!bar_uses_output(bar, output->name)) { - zxdg_output_v1_destroy(output->xdg_output); - wl_output_destroy(output->output); - wl_list_remove(&output->link); - free(output); - } - } - struct swaybar_pointer *pointer = &bar->pointer; int max_scale = 1; @@ -479,18 +520,6 @@ void bar_setup(struct swaybar *bar, pointer->cursor_surface = wl_compositor_create_surface(bar->compositor); assert(pointer->cursor_surface); - wl_list_for_each(output, &bar->outputs, link) { - output->surface = wl_compositor_create_surface(bar->compositor); - assert(output->surface); - output->layer_surface = zwlr_layer_shell_v1_get_layer_surface( - bar->layer_shell, output->surface, output->output, - ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "panel"); - assert(output->layer_surface); - zwlr_layer_surface_v1_add_listener(output->layer_surface, - &layer_surface_listener, output); - zwlr_layer_surface_v1_set_anchor(output->layer_surface, - bar->config->position); - } ipc_get_workspaces(bar); render_all_frames(bar); } @@ -529,6 +558,7 @@ void bar_run(struct swaybar *bar) { } while (1) { event_loop_poll(); + wl_display_flush(bar->display); } } diff --git a/swaybar/event_loop.c b/swaybar/event_loop.c index bc4053be..686b9962 100644 --- a/swaybar/event_loop.c +++ b/swaybar/event_loop.c @@ -105,7 +105,7 @@ bool remove_timer(timer_t timer) { return false; } -void event_loop_poll() { +void event_loop_poll(void) { poll(event_loop.fds.items, event_loop.fds.length, -1); for (int i = 0; i < event_loop.fds.length; ++i) { @@ -146,7 +146,7 @@ void event_loop_poll() { } } -void init_event_loop() { +void init_event_loop(void) { event_loop.fds.length = 0; event_loop.fds.capacity = 10; event_loop.fds.items = malloc( diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c index 0becae5d..325aa61a 100644 --- a/swaybar/i3bar.c +++ b/swaybar/i3bar.c @@ -1,6 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include <json-c/json.h> #include <linux/input-event-codes.h> +#include <ctype.h> #include <stdlib.h> #include <string.h> #include <unistd.h> @@ -24,24 +25,19 @@ void i3bar_block_unref(struct i3bar_block *block) { } } -static bool i3bar_parse_json(struct status_line *status, const char *text) { +static void i3bar_parse_json(struct status_line *status, + struct json_object *json_array) { struct i3bar_block *block, *tmp; wl_list_for_each_safe(block, tmp, &status->blocks, link) { wl_list_remove(&block->link); i3bar_block_unref(block); } - json_object *results = json_tokener_parse(text); - if (!results) { - status_error(status, "[failed to parse i3bar json]"); - return false; - } - wlr_log(WLR_DEBUG, "Got i3bar json: '%s'", text); - for (size_t i = 0; i < json_object_array_length(results); ++i) { + for (size_t i = 0; i < json_object_array_length(json_array); ++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, *markup; - json_object *json = json_object_array_get_idx(results, i); + json_object *json = json_object_array_get_idx(json_array, i); if (!json) { continue; } @@ -110,96 +106,160 @@ static bool i3bar_parse_json(struct status_line *status, const char *text) { json_object_get_int(border_right) : 1; wl_list_insert(&status->blocks, &block->link); } - json_object_put(results); - return true; } bool i3bar_handle_readable(struct status_line *status) { - struct i3bar_protocol_state *state = &status->i3bar_state; - - char *cur = &state->buffer[state->buffer_index]; - ssize_t n = read(status->read_fd, cur, - state->buffer_size - state->buffer_index); - if (n == -1) { - status_error(status, "[failed to read from status command]"); - return false; - } + while (!status->started) { // look for opening bracket + for (size_t c = 0; c < status->buffer_index; ++c) { + if (status->buffer[c] == '[') { + status->started = true; + status->buffer_index -= ++c; + memmove(status->buffer, &status->buffer[c], status->buffer_index); + break; + } else if (!isspace(status->buffer[c])) { + wlr_log(WLR_DEBUG, "Invalid i3bar json: expected '[' but encountered '%c'", + status->buffer[c]); + status_error(status, "[invalid i3bar json]"); + return true; + } + } + if (status->started) { + break; + } - if (n == (ssize_t)(state->buffer_size - state->buffer_index)) { - state->buffer_size = state->buffer_size * 2; - char *new_buffer = realloc(state->buffer, state->buffer_size); - if (!new_buffer) { - free(state->buffer); - status_error(status, "[failed to allocate buffer]"); + errno = 0; + ssize_t read_bytes = read(status->read_fd, status->buffer, status->buffer_size); + if (read_bytes > -1) { + status->buffer_index = read_bytes; + } else if (errno == EAGAIN) { + return false; + } else { + status_error(status, "[error reading from status command]"); return true; } - state->current_node += new_buffer - state->buffer; - cur += new_buffer - state->buffer; - state->buffer = new_buffer; } - cur[n] = '\0'; - bool redraw = false; - while (*cur) { - if (state->nodes[state->depth] == JSON_NODE_STRING) { - if (!state->escape && *cur == '"') { - --state->depth; + struct json_object *last_object = NULL; + struct json_object *test_object; + size_t buffer_pos = 0; + while (true) { + // since the incoming stream is an infinite array + // parsing is split into two parts + // first, attempt to parse the current object, reading more if the + // parser indicates that the current object is incomplete, and failing + // if the parser fails + // second, look for separating comma, ignoring whitespace, failing if + // any other characters are encountered + if (status->expecting_comma) { + for (; buffer_pos < status->buffer_index; ++buffer_pos) { + if (status->buffer[buffer_pos] == ',') { + status->expecting_comma = false; + ++buffer_pos; + break; + } else if (!isspace(status->buffer[buffer_pos])) { + wlr_log(WLR_DEBUG, "Invalid i3bar json: expected ',' but encountered '%c'", + status->buffer[buffer_pos]); + status_error(status, "[invalid i3bar json]"); + return true; + } + } + if (buffer_pos < status->buffer_index) { + continue; // look for new object without reading more input } - state->escape = !state->escape && *cur == '\\'; + buffer_pos = status->buffer_index = 0; } else { - switch (*cur) { - case '[': - ++state->depth; - if (state->depth > - sizeof(state->nodes) / sizeof(state->nodes[0])) { - status_error(status, "[i3bar json too deep]"); - return false; - } - state->nodes[state->depth] = JSON_NODE_ARRAY; - if (state->depth == 1) { - state->current_node = cur; + test_object = json_tokener_parse_ex(status->tokener, + &status->buffer[buffer_pos], status->buffer_index - buffer_pos); + enum json_tokener_error err = json_tokener_get_error(status->tokener); + if (err == json_tokener_success) { + if (json_object_get_type(test_object) == json_type_array) { + if (last_object) { + json_object_put(last_object); + } + last_object = test_object; + } else { + json_object_put(test_object); } - break; - case ']': - if (state->nodes[state->depth] != JSON_NODE_ARRAY) { - status_error(status, "[failed to parse i3bar json]"); - return false; + + // in order to print the json for debugging purposes + // the last character is temporarily replaced with a null character + // (the last character is used in case the buffer is full) + char *last_char_pos = + &status->buffer[buffer_pos + status->tokener->char_offset - 1]; + char last_char = *last_char_pos; + while (isspace(last_char)) { + last_char = *--last_char_pos; } - --state->depth; - if (state->depth == 0) { - // cur[1] is valid since cur[0] != '\0' - char p = cur[1]; - cur[1] = '\0'; - redraw = i3bar_parse_json( - status, state->current_node) || redraw; - cur[1] = p; - memmove(state->buffer, cur, - state->buffer_size - (cur - state->buffer)); - cur = state->buffer; - state->current_node = cur + 1; + *last_char_pos = '\0'; + size_t offset = strspn(&status->buffer[buffer_pos], " \f\n\r\t\v"); + wlr_log(WLR_DEBUG, "Received i3bar json: '%s%c'", + &status->buffer[buffer_pos + offset], last_char); + *last_char_pos = last_char; + + buffer_pos += status->tokener->char_offset; + status->expecting_comma = true; + + if (buffer_pos < status->buffer_index) { + continue; // look for comma without reading more input } - break; - case '"': - ++state->depth; - if (state->depth > - sizeof(state->nodes) / sizeof(state->nodes[0])) { - status_error(status, "[i3bar json too deep]"); - return false; + buffer_pos = status->buffer_index = 0; + } else if (err == json_tokener_continue) { + json_tokener_reset(status->tokener); + if (status->buffer_index < status->buffer_size) { + // move the object to the start of the buffer + status->buffer_index -= buffer_pos; + memmove(status->buffer, &status->buffer[buffer_pos], + status->buffer_index); + buffer_pos = 0; + } else { + // expand buffer + status->buffer_size *= 2; + char *new_buffer = realloc(status->buffer, status->buffer_size); + if (new_buffer) { + status->buffer = new_buffer; + } else { + free(status->buffer); + status_error(status, "[failed to allocate buffer]"); + return true; + } } - state->nodes[state->depth] = JSON_NODE_STRING; - break; + } else { + char last_char = status->buffer[status->buffer_index - 1]; + status->buffer[status->buffer_index - 1] = '\0'; + wlr_log(WLR_DEBUG, "Failed to parse i3bar json - %s: '%s%c'", + json_tokener_error_desc(err), &status->buffer[buffer_pos], last_char); + status_error(status, "[failed to parse i3bar json]"); + return true; } } - ++cur; + + errno = 0; + ssize_t read_bytes = read(status->read_fd, &status->buffer[status->buffer_index], + status->buffer_size - status->buffer_index); + if (read_bytes > -1) { + status->buffer_index += read_bytes; + } else if (errno == EAGAIN) { + break; + } else { + status_error(status, "[error reading from status command]"); + return true; + } + } + + if (last_object) { + wlr_log(WLR_DEBUG, "Rendering last received json"); + i3bar_parse_json(status, last_object); + json_object_put(last_object); + return true; + } else { + return false; } - state->buffer_index = cur - state->buffer; - return redraw; } enum hotspot_event_handling i3bar_block_send_click(struct status_line *status, struct i3bar_block *block, int x, int y, enum x11_button button) { wlr_log(WLR_DEBUG, "block %s clicked", block->name ? block->name : "(nil)"); - if (!block->name || !status->i3bar_state.click_events) { + if (!block->name || !status->click_events) { return HOTSPOT_PROCESS; } @@ -214,7 +274,7 @@ enum hotspot_event_handling i3bar_block_send_click(struct status_line *status, json_object_object_add(event_json, "button", json_object_new_int(button)); json_object_object_add(event_json, "x", json_object_new_int(x)); json_object_object_add(event_json, "y", json_object_new_int(y)); - if (dprintf(status->write_fd, "%s\n", + if (dprintf(status->write_fd, "%s,\n", json_object_to_json_string(event_json)) < 0) { status_error(status, "[failed to write click event]"); } diff --git a/swaybar/render.c b/swaybar/render.c index 2d848bfa..26db80cb 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 200809L +#include <assert.h> #include <limits.h> #include <stdlib.h> #include <stdint.h> @@ -120,14 +121,14 @@ static void i3bar_block_unref_callback(void *data) { } static uint32_t render_status_block(cairo_t *cairo, - struct swaybar_config *config, struct swaybar_output *output, - struct i3bar_block *block, double *x, + struct swaybar_output *output, struct i3bar_block *block, double *x, uint32_t surface_height, bool focused, bool edge) { if (!block->full_text || !*block->full_text) { return 0; } uint32_t height = surface_height * output->scale; + struct swaybar_config *config = output->bar->config; int text_width, text_height; get_text_size(cairo, config->font, &text_width, &text_height, NULL, @@ -177,16 +178,18 @@ static uint32_t render_status_block(cairo_t *cairo, *x -= margin; } - struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot)); - hotspot->x = *x; - hotspot->y = 0; - hotspot->width = width; - hotspot->height = height; - hotspot->callback = block_hotspot_callback; - hotspot->destroy = i3bar_block_unref_callback; - hotspot->data = block; - block->ref_count++; - wl_list_insert(&output->hotspots, &hotspot->link); + if (output->bar->status->click_events) { + struct swaybar_hotspot *hotspot = calloc(1, sizeof(struct swaybar_hotspot)); + hotspot->x = *x; + hotspot->y = 0; + hotspot->width = width; + hotspot->height = height; + hotspot->callback = block_hotspot_callback; + hotspot->destroy = i3bar_block_unref_callback; + hotspot->data = block; + block->ref_count++; + wl_list_insert(&output->hotspots, &hotspot->link); + } double pos = *x; if (block->background) { @@ -268,7 +271,7 @@ static uint32_t render_status_line_i3bar(cairo_t *cairo, bool edge = true; struct i3bar_block *block; wl_list_for_each(block, &status->blocks, link) { - uint32_t h = render_status_block(cairo, config, output, + uint32_t h = render_status_block(cairo, output, block, x, surface_height, focused, edge); max_height = h > max_height ? h : max_height; edge = false; @@ -478,6 +481,8 @@ static uint32_t render_to_cairo(cairo_t *cairo, } void render_frame(struct swaybar *bar, struct swaybar_output *output) { + assert(output->surface != NULL); + struct swaybar_hotspot *hotspot, *tmp; wl_list_for_each_safe(hotspot, tmp, &output->hotspots, link) { if (hotspot->destroy) { @@ -505,7 +510,6 @@ void render_frame(struct swaybar *bar, struct swaybar_output *output) { // TODO: this could infinite loop if the compositor assigns us a // different height than what we asked for wl_surface_commit(output->surface); - wl_display_roundtrip(bar->display); } else if (height > 0) { // Replay recording into shm and send it off output->current_buffer = get_next_buffer(bar->shm, @@ -531,7 +535,6 @@ void render_frame(struct swaybar *bar, struct swaybar_output *output) { wl_surface_damage(output->surface, 0, 0, output->width, output->height); wl_surface_commit(output->surface); - wl_display_roundtrip(bar->display); } cairo_surface_destroy(recorder); cairo_destroy(cairo); diff --git a/swaybar/status_line.c b/swaybar/status_line.c index 3ba990bd..48b43248 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c @@ -7,86 +7,106 @@ #include <unistd.h> #include <wlr/util/log.h> #include "swaybar/config.h" +#include "swaybar/event_loop.h" #include "swaybar/status_line.h" #include "readline.h" +static void status_line_close_fds(struct status_line *status) { + if (status->read_fd != -1) { + remove_event(status->read_fd); + close(status->read_fd); + status->read_fd = -1; + } + if (status->write_fd != -1) { + close(status->write_fd); + status->write_fd = -1; + } +} + void status_error(struct status_line *status, const char *text) { - close(status->read_fd); - close(status->write_fd); + status_line_close_fds(status); status->protocol = PROTOCOL_ERROR; status->text = text; } bool status_handle_readable(struct status_line *status) { - char *line; + ssize_t read_bytes = 1; switch (status->protocol) { - case PROTOCOL_ERROR: - return false; - case PROTOCOL_I3BAR: - if (i3bar_handle_readable(status) > 0) { - return true; - } - break; - case PROTOCOL_TEXT: - line = read_line_buffer(status->read, - status->text_state.buffer, status->text_state.buffer_size); - if (!line) { - status_error(status, "[error reading from status command]"); - } else { - status->text = line; - } - return true; case PROTOCOL_UNDEF: - line = read_line_buffer(status->read, - status->text_state.buffer, status->text_state.buffer_size); - if (!line) { + errno = 0; + read_bytes = getline(&status->buffer, + &status->buffer_size, status->read); + if (errno == EAGAIN) { + clearerr(status->read); + } else if (errno) { status_error(status, "[error reading from status command]"); - return false; + return true; } - if (line[0] == '{') { - json_object *proto = json_tokener_parse(line); - if (proto) { - json_object *version; - if (json_object_object_get_ex(proto, "version", &version) - && json_object_get_int(version) == 1) { - wlr_log(WLR_DEBUG, "Switched to i3bar protocol."); - status->protocol = PROTOCOL_I3BAR; - } - json_object *click_events; - if (json_object_object_get_ex( - proto, "click_events", &click_events) - && json_object_get_boolean(click_events)) { - wlr_log(WLR_DEBUG, "Enabled click events."); - status->i3bar_state.click_events = true; - const char *events_array = "[\n"; - ssize_t len = strlen(events_array); - if (write(status->write_fd, events_array, len) != len) { - status_error(status, - "[failed to write to status command]"); - } + + // the header must be sent completely the first time round + json_object *header, *version; + if (status->buffer[read_bytes - 1] == '\n' + && (header = json_tokener_parse(status->buffer)) + && json_object_object_get_ex(header, "version", &version) + && json_object_get_int(version) == 1) { + wlr_log(WLR_DEBUG, "Using i3bar protocol."); + status->protocol = PROTOCOL_I3BAR; + + json_object *click_events; + if (json_object_object_get_ex(header, "click_events", &click_events) + && json_object_get_boolean(click_events)) { + wlr_log(WLR_DEBUG, "Enabling click events."); + status->click_events = true; + if (write(status->write_fd, "[\n", 2) != 2) { + status_error(status, "[failed to write to status command]"); + json_object_put(header); + return true; } - json_object_put(proto); } + json_object_put(header); - status->protocol = PROTOCOL_I3BAR; - free(status->text_state.buffer); wl_list_init(&status->blocks); - status->i3bar_state.buffer_size = 4096; - status->i3bar_state.buffer = - malloc(status->i3bar_state.buffer_size); - } else { - status->protocol = PROTOCOL_TEXT; - status->text = line; + status->tokener = json_tokener_new(); + read_bytes = getdelim(&status->buffer, &status->buffer_size, EOF, status->read); + if (read_bytes > 0) { + status->buffer_index = read_bytes; + return i3bar_handle_readable(status); + } else { + return false; + } + } + + wlr_log(WLR_DEBUG, "Using text protocol."); + status->protocol = PROTOCOL_TEXT; + status->text = status->buffer; + // intentional fall-through + case PROTOCOL_TEXT: + errno = 0; + while (true) { + if (status->buffer[read_bytes - 1] == '\n') { + status->buffer[read_bytes - 1] = '\0'; + } + read_bytes = getline(&status->buffer, + &status->buffer_size, status->read); + if (errno == EAGAIN) { + clearerr(status->read); + return true; + } else if (errno) { + status_error(status, "[error reading from status command]"); + return true; + } } - return true; + case PROTOCOL_I3BAR: + return i3bar_handle_readable(status); + default: + return false; } - return false; } struct status_line *status_line_init(char *cmd) { struct status_line *status = calloc(1, sizeof(struct status_line)); - status->text_state.buffer_size = 8192; - status->text_state.buffer = malloc(status->text_state.buffer_size); + status->buffer_size = 8192; + status->buffer = malloc(status->buffer_size); int pipe_read_fd[2]; int pipe_write_fd[2]; @@ -123,20 +143,16 @@ struct status_line *status_line_init(char *cmd) { } void status_line_free(struct status_line *status) { - close(status->read_fd); - close(status->write_fd); + status_line_close_fds(status); kill(status->pid, SIGTERM); - switch (status->protocol) { - case PROTOCOL_I3BAR:; + if (status->protocol == PROTOCOL_I3BAR) { struct i3bar_block *block, *tmp; wl_list_for_each_safe(block, tmp, &status->blocks, link) { + wl_list_remove(&block->link); i3bar_block_unref(block); } - free(status->i3bar_state.buffer); - break; - default: - free(status->text_state.buffer); - break; + json_tokener_free(status->tokener); } + free(status->buffer); free(status); } |