diff options
Diffstat (limited to 'swaybar/main.c')
-rw-r--r-- | swaybar/main.c | 255 |
1 files changed, 10 insertions, 245 deletions
diff --git a/swaybar/main.c b/swaybar/main.c index 6ba7c825..a521fa79 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); + + /* set font */ + state->output->window->font = state->config->font; - bar_ipc_init(desired_output, bar_id); + /* set window height */ + set_window_height(state->output->window, state->config->height); if (state->config->status_command) { int pipefd[2]; |