Skip to content

Commit

Permalink
swaybar: show hidden bar on urgency
Browse files Browse the repository at this point in the history
This commit also renames some variables and functions.
  • Loading branch information
ianyfan committed Oct 8, 2018
1 parent 756e021 commit d68bc58
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 56 deletions.
2 changes: 1 addition & 1 deletion include/sway/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ struct bar_config {
* In "show" mode, it will always be shown on top of the active workspace.
*/
char *hidden_state;
bool visible; // denotes actual visibility when in "hide" mode & state
bool visible_by_modifier; // when in "hide" mode & state
/**
* Id name used to identify the bar through IPC.
*
Expand Down
6 changes: 4 additions & 2 deletions include/swaybar/bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ struct swaybar {
char *id;
char *mode;
bool mode_pango_markup;
bool visible_by_modifier;
bool visible_by_urgency;
bool visible;

struct wl_display *display;
struct wl_compositor *compositor;
Expand Down Expand Up @@ -69,8 +72,7 @@ bool bar_setup(struct swaybar *bar, const char *socket_path);
void bar_run(struct swaybar *bar);
void bar_teardown(struct swaybar *bar);

void show_bar(struct swaybar *bar);
void hide_bar(struct swaybar *bar, bool stop_command);
bool determine_bar_visibility(struct swaybar *bar, bool moving_layer);
void free_workspaces(struct wl_list *list);

#endif
2 changes: 1 addition & 1 deletion include/swaybar/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

bool ipc_initialize(struct swaybar *bar);
bool handle_ipc_readable(struct swaybar *bar);
void ipc_get_workspaces(struct swaybar *bar);
bool ipc_get_workspaces(struct swaybar *bar);
void ipc_send_workspace_command(struct swaybar *bar, const char *ws);

#endif
1 change: 0 additions & 1 deletion include/swaybar/status_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ struct status_line {
const char *text;
struct wl_list blocks; // i3bar_block::link

bool stopped;
int stop_signal;
int cont_signal;

Expand Down
4 changes: 2 additions & 2 deletions sway/input/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ static void determine_bar_visibility(uint32_t modifiers) {
struct bar_config *bar = config->bars->items[i];
if (strcmp(bar->mode, bar->hidden_state) == 0) { // both are "hide"
bool should_be_visible = (~modifiers & bar->modifier) == 0;
if (bar->visible != should_be_visible) {
bar->visible = should_be_visible;
if (bar->visible_by_modifier != should_be_visible) {
bar->visible_by_modifier = should_be_visible;
ipc_event_bar_state_update(bar);
}
}
Expand Down
4 changes: 2 additions & 2 deletions sway/ipc-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ void ipc_event_bar_state_update(struct bar_config *bar) {

json_object *json = json_object_new_object();
json_object_object_add(json, "id", json_object_new_string(bar->id));
json_object_object_add(json, "visible",
json_object_new_boolean(bar->visible));
json_object_object_add(json, "visible_by_modifier",
json_object_new_boolean(bar->visible_by_modifier));

const char *json_string = json_object_to_json_string(json);
ipc_send_event(json_string, IPC_EVENT_BAR_STATE_UPDATE);
Expand Down
58 changes: 38 additions & 20 deletions swaybar/bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,42 @@ static void destroy_layer_surface(struct swaybar_output *output) {
output->frame_scheduled = false;
}

void show_bar(struct swaybar *bar) {
struct swaybar_output *output;
wl_list_for_each(output, &bar->outputs, link) {
add_layer_surface(output);
bool determine_bar_visibility(struct swaybar *bar, bool moving_layer) {
bool visible_before = bar->visible;
struct swaybar_config *config = bar->config;
bool visible = !(strcmp(config->mode, "invisible") == 0 ||
(strcmp(config->mode, config->hidden_state) == 0 // both "hide"
&& !bar->visible_by_modifier && !bar->visible_by_urgency));

if (!visible && !visible_before) {
return false;
}
if (bar->status->stopped) {
wlr_log(WLR_DEBUG, "Sending cont signal to status");
if ((kill(bar->status->pid, bar->status->cont_signal)) == 0) {
bar->status->stopped = false;

struct swaybar_output *output;
if (visible && visible_before) {
if (moving_layer) {
// need to destroy layer surface to move to a different layer
wl_list_for_each(output, &bar->outputs, link) {
destroy_layer_surface(output);
add_layer_surface(output);
}
}
return true;
}
}

void hide_bar(struct swaybar *bar, bool stop_command) {
struct swaybar_output *output;
bar->visible = visible;
wl_list_for_each(output, &bar->outputs, link) {
destroy_layer_surface(output);
}
if (stop_command) {
wlr_log(WLR_DEBUG, "Sending stop signal to status");
if ((kill(bar->status->pid, bar->status->stop_signal)) == 0) {
bar->status->stopped = true;
if (visible) {
add_layer_surface(output);
} else {
destroy_layer_surface(output);
}
}
wlr_log(WLR_DEBUG, "Sending %s signal to status command",
visible ? "cont" : "stop");
kill(bar->status->pid,
visible ? bar->status->cont_signal : bar->status->stop_signal);
return visible;
}

static bool bar_uses_output(struct swaybar *bar, const char *name) {
Expand Down Expand Up @@ -226,7 +238,9 @@ static void xdg_output_handle_done(void *data,

output->surface = wl_compositor_create_surface(bar->compositor);
assert(output->surface);
add_layer_surface(output);
if (bar->visible) {
add_layer_surface(output);
}
}
}

Expand Down Expand Up @@ -369,8 +383,12 @@ bool bar_setup(struct swaybar *bar, const char *socket_path) {
pointer->cursor_surface = wl_compositor_create_surface(bar->compositor);
assert(pointer->cursor_surface);

ipc_get_workspaces(bar);
set_bar_dirty(bar);
if (bar->config->workspace_buttons) {
ipc_get_workspaces(bar);
}
if (determine_bar_visibility(bar, false)) {
set_bar_dirty(bar);
}
return true;
}

Expand Down
42 changes: 15 additions & 27 deletions swaybar/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static bool ipc_parse_config(
return true;
}

void ipc_get_workspaces(struct swaybar *bar) {
bool ipc_get_workspaces(struct swaybar *bar) {
struct swaybar_output *output;
wl_list_for_each(output, &bar->outputs, link) {
free_workspaces(&output->workspaces);
Expand All @@ -256,8 +256,10 @@ void ipc_get_workspaces(struct swaybar *bar) {
json_object *results = json_tokener_parse(res);
if (!results) {
free(res);
return;
return false;
}

bar->visible_by_urgency = false;
size_t length = json_object_array_length(results);
json_object *ws_json;
json_object *num, *name, *visible, *focused, *out, *urgent;
Expand All @@ -284,12 +286,16 @@ void ipc_get_workspaces(struct swaybar *bar) {
output->focused = true;
}
ws->urgent = json_object_get_boolean(urgent);
if (ws->urgent) {
bar->visible_by_urgency = true;
}
wl_list_insert(&output->workspaces, &ws->link);
}
}
}
json_object_put(results);
free(res);
return determine_bar_visibility(bar, false);
}

static void ipc_get_outputs(struct swaybar *bar) {
Expand Down Expand Up @@ -357,14 +363,10 @@ static bool handle_bar_state_update(struct swaybar *bar, json_object *event) {
return false;
}

json_object *visible;
json_object_object_get_ex(event, "visible", &visible);
if (json_object_get_boolean(visible)) {
show_bar(bar);
} else {
hide_bar(bar, true);
}
return true;
json_object *visible_by_modifier;
json_object_object_get_ex(event, "visible_by_modifier", &visible_by_modifier);
bar->visible_by_modifier = json_object_get_boolean(visible_by_modifier);
return determine_bar_visibility(bar, false);
}

static bool handle_barconfig_update(struct swaybar *bar,
Expand All @@ -386,14 +388,7 @@ static bool handle_barconfig_update(struct swaybar *bar,
wlr_log(WLR_DEBUG, "Changing bar hidden state to %s", new_state);
free(old_state);
config->hidden_state = strdup(new_state);
if (strcmp(config->mode, "hide") == 0) {
if (strcmp(config->hidden_state, "hide") == 0) {
hide_bar(bar, true);
} else {
show_bar(bar);
}
}
return true;
return determine_bar_visibility(bar, false);
}

free(config->mode);
Expand All @@ -402,14 +397,7 @@ static bool handle_barconfig_update(struct swaybar *bar,
config->mode = strdup(json_object_get_string(json_mode));
wlr_log(WLR_DEBUG, "Changing bar mode to %s", config->mode);

// always need to destroy layer surface to move to a different layer
bool hiding_bar = strcmp(config->mode, "invisible") == 0 ||
strcmp(config->mode, config->hidden_state) == 0; // both are "hide"
hide_bar(bar, hiding_bar);
if (hiding_bar) {
show_bar(bar);
}
return hiding_bar;
return determine_bar_visibility(bar, true);
}

bool handle_ipc_readable(struct swaybar *bar) {
Expand All @@ -428,7 +416,7 @@ bool handle_ipc_readable(struct swaybar *bar) {
bool success = true;
switch (resp->type) {
case IPC_EVENT_WORKSPACE:
ipc_get_workspaces(bar);
success = ipc_get_workspaces(bar);
break;
case IPC_EVENT_MODE: {
json_object *json_change, *json_pango_markup;
Expand Down

0 comments on commit d68bc58

Please sign in to comment.