From 312d009f656e86401cda1c6198b929e4cbd85d2f Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Mon, 24 Sep 2018 18:42:25 +0100 Subject: [PATCH] swaybar: fail if bar id is invalid --- include/swaybar/bar.h | 4 +--- include/swaybar/ipc.h | 2 +- swaybar/bar.c | 7 +++++-- swaybar/ipc.c | 18 +++++++++++++++--- swaybar/main.c | 5 ++++- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/include/swaybar/bar.h b/include/swaybar/bar.h index 29e96159..20a51809 100644 --- a/include/swaybar/bar.h +++ b/include/swaybar/bar.h @@ -95,9 +95,7 @@ struct swaybar_workspace { bool urgent; }; -void bar_setup(struct swaybar *bar, - const char *socket_path, - const char *bar_id); +bool bar_setup(struct swaybar *bar, const char *socket_path, const char *bar_id); void bar_run(struct swaybar *bar); void bar_teardown(struct swaybar *bar); diff --git a/include/swaybar/ipc.h b/include/swaybar/ipc.h index a1696bcf..81e48a6b 100644 --- a/include/swaybar/ipc.h +++ b/include/swaybar/ipc.h @@ -3,7 +3,7 @@ #include #include "swaybar/bar.h" -void ipc_initialize(struct swaybar *bar, const char *bar_id); +bool ipc_initialize(struct swaybar *bar, const char *bar_id); bool handle_ipc_readable(struct swaybar *bar); void ipc_get_workspaces(struct swaybar *bar); void ipc_send_workspace_command(struct swaybar *bar, const char *ws); diff --git a/swaybar/bar.c b/swaybar/bar.c index ab307fd4..809416dd 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -478,14 +478,16 @@ static void render_all_frames(struct swaybar *bar) { } } -void bar_setup(struct swaybar *bar, +bool bar_setup(struct swaybar *bar, const char *socket_path, const char *bar_id) { bar_init(bar); init_event_loop(); bar->ipc_socketfd = ipc_open_socket(socket_path); bar->ipc_event_socketfd = ipc_open_socket(socket_path); - ipc_initialize(bar, bar_id); + if (!ipc_initialize(bar, bar_id)) { + return false; + } if (bar->config->status_command) { bar->status = status_line_init(bar->config->status_command); } @@ -526,6 +528,7 @@ void bar_setup(struct swaybar *bar, ipc_get_workspaces(bar); render_all_frames(bar); + return true; } static void display_in(int fd, short mask, void *data) { diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 0e60c10c..2b50528d 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -141,9 +141,16 @@ static void ipc_parse_colors( } } -static void ipc_parse_config( +static bool ipc_parse_config( struct swaybar_config *config, const char *payload) { json_object *bar_config = json_tokener_parse(payload); + json_object *success; + if (json_object_object_get_ex(bar_config, "success", &success) + && !json_object_get_boolean(success)) { + wlr_log(WLR_ERROR, "No bar with that ID. Use 'swaymsg -t get_bar_config to get the available bar configs."); + json_object_put(bar_config); + return false; + } json_object *markup, *mode, *hidden_bar, *position, *status_command; json_object *font, *bar_height, *wrap_scroll, *workspace_buttons, *strip_workspace_numbers; json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol, *outputs; @@ -226,6 +233,7 @@ static void ipc_parse_config( } json_object_put(bar_config); + return true; } void ipc_get_workspaces(struct swaybar *bar) { @@ -312,11 +320,14 @@ static void ipc_get_outputs(struct swaybar *bar) { free(res); } -void ipc_initialize(struct swaybar *bar, const char *bar_id) { +bool ipc_initialize(struct swaybar *bar, const char *bar_id) { uint32_t len = strlen(bar_id); char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_BAR_CONFIG, bar_id, &len); - ipc_parse_config(bar->config, res); + if (!ipc_parse_config(bar->config, res)) { + free(res); + return false; + } free(res); ipc_get_outputs(bar); @@ -324,6 +335,7 @@ void ipc_initialize(struct swaybar *bar, const char *bar_id) { len = strlen(subscribe); free(ipc_single_command(bar->ipc_event_socketfd, IPC_SUBSCRIBE, subscribe, &len)); + return true; } bool handle_ipc_readable(struct swaybar *bar) { diff --git a/swaybar/main.c b/swaybar/main.c index 60e4b37c..d2c579db 100644 --- a/swaybar/main.c +++ b/swaybar/main.c @@ -96,7 +96,10 @@ int main(int argc, char **argv) { signal(SIGTERM, sig_handler); - bar_setup(&swaybar, socket_path, bar_id); + if (!bar_setup(&swaybar, socket_path, bar_id)) { + free(socket_path); + return 1; + } free(socket_path); free(bar_id);