swaybar: fail if bar id is invalid

This commit is contained in:
Ian Fan 2018-09-24 18:42:25 +01:00
parent 02df1e2b1b
commit 312d009f65
5 changed files with 26 additions and 10 deletions

View file

@ -95,9 +95,7 @@ struct swaybar_workspace {
bool urgent; bool urgent;
}; };
void bar_setup(struct swaybar *bar, bool bar_setup(struct swaybar *bar, const char *socket_path, const char *bar_id);
const char *socket_path,
const char *bar_id);
void bar_run(struct swaybar *bar); void bar_run(struct swaybar *bar);
void bar_teardown(struct swaybar *bar); void bar_teardown(struct swaybar *bar);

View file

@ -3,7 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "swaybar/bar.h" #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); bool handle_ipc_readable(struct swaybar *bar);
void ipc_get_workspaces(struct swaybar *bar); void ipc_get_workspaces(struct swaybar *bar);
void ipc_send_workspace_command(struct swaybar *bar, const char *ws); void ipc_send_workspace_command(struct swaybar *bar, const char *ws);

View file

@ -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) { const char *socket_path, const char *bar_id) {
bar_init(bar); bar_init(bar);
init_event_loop(); init_event_loop();
bar->ipc_socketfd = ipc_open_socket(socket_path); bar->ipc_socketfd = ipc_open_socket(socket_path);
bar->ipc_event_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) { if (bar->config->status_command) {
bar->status = status_line_init(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); ipc_get_workspaces(bar);
render_all_frames(bar); render_all_frames(bar);
return true;
} }
static void display_in(int fd, short mask, void *data) { static void display_in(int fd, short mask, void *data) {

View file

@ -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) { struct swaybar_config *config, const char *payload) {
json_object *bar_config = json_tokener_parse(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 *markup, *mode, *hidden_bar, *position, *status_command;
json_object *font, *bar_height, *wrap_scroll, *workspace_buttons, *strip_workspace_numbers; json_object *font, *bar_height, *wrap_scroll, *workspace_buttons, *strip_workspace_numbers;
json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol, *outputs; json_object *binding_mode_indicator, *verbose, *colors, *sep_symbol, *outputs;
@ -226,6 +233,7 @@ static void ipc_parse_config(
} }
json_object_put(bar_config); json_object_put(bar_config);
return true;
} }
void ipc_get_workspaces(struct swaybar *bar) { void ipc_get_workspaces(struct swaybar *bar) {
@ -312,11 +320,14 @@ static void ipc_get_outputs(struct swaybar *bar) {
free(res); 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); uint32_t len = strlen(bar_id);
char *res = ipc_single_command(bar->ipc_socketfd, char *res = ipc_single_command(bar->ipc_socketfd,
IPC_GET_BAR_CONFIG, bar_id, &len); 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); free(res);
ipc_get_outputs(bar); ipc_get_outputs(bar);
@ -324,6 +335,7 @@ void ipc_initialize(struct swaybar *bar, const char *bar_id) {
len = strlen(subscribe); len = strlen(subscribe);
free(ipc_single_command(bar->ipc_event_socketfd, free(ipc_single_command(bar->ipc_event_socketfd,
IPC_SUBSCRIBE, subscribe, &len)); IPC_SUBSCRIBE, subscribe, &len));
return true;
} }
bool handle_ipc_readable(struct swaybar *bar) { bool handle_ipc_readable(struct swaybar *bar) {

View file

@ -96,7 +96,10 @@ int main(int argc, char **argv) {
signal(SIGTERM, sig_handler); 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(socket_path);
free(bar_id); free(bar_id);