Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
cmd_bar improvements (#4014)
Browse files Browse the repository at this point in the history
- Split cmd_bar into 2 functions, improving errors and reducing strcmps
- Only update barconfig when something has changed
- Only update barconfig for the specific bar that has changed

Fixes #3958
  • Loading branch information
orestisfl committed May 7, 2020
1 parent 142b3fa commit f63a4be
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 81 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES-next
Expand Up @@ -17,6 +17,7 @@ strongly encouraged to upgrade.
• make dock client order deterministic (sorted by class/instance) as a
side effect, i3bars without an explicit bar-id will be sorted according
to their definition order in the config file
• update i3bar config when necessary (reduces redraws on bar mode changes)
• mention rofi in default config file

┌────────────────────────────┐
Expand Down
10 changes: 8 additions & 2 deletions include/commands.h
Expand Up @@ -315,10 +315,16 @@ void cmd_title_format(I3_CMD, const char *format);
void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name);

/**
* Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
* Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
*
*/
void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id);
void cmd_bar_mode(I3_CMD, const char *bar_mode, const char *bar_id);

/**
* Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
*
*/
void cmd_bar_hidden_state(I3_CMD, const char *bar_hidden_state, const char *bar_id);

/**
* Implementation of 'shmlog <size>|toggle|on|off'
Expand Down
6 changes: 0 additions & 6 deletions include/configuration.h
Expand Up @@ -425,9 +425,3 @@ bool load_configuration(const char *override_configfile, config_load_t load_type
*
*/
void ungrab_all_keys(xcb_connection_t *conn);

/**
* Sends the current bar configuration as an event to all barconfig_update listeners.
*
*/
void update_barconfig(void);
14 changes: 8 additions & 6 deletions parser-specs/commands.spec
Expand Up @@ -460,15 +460,17 @@ state BAR:
-> BAR_MODE

state BAR_HIDDEN_STATE:
bar_value = 'hide', 'show', 'toggle'
-> BAR_W_ID
bar_hidden_state = 'hide', 'show', 'toggle'
->
bar_id = word
->
end
-> call cmd_bar_hidden_state($bar_hidden_state, $bar_id)

state BAR_MODE:
bar_value = 'dock', 'hide', 'invisible', 'toggle'
-> BAR_W_ID

state BAR_W_ID:
->
bar_id = word
->
end
-> call cmd_bar($bar_type, $bar_value, $bar_id)
-> call cmd_bar_mode($bar_value, $bar_id)
91 changes: 35 additions & 56 deletions src/commands.c
Expand Up @@ -1621,8 +1621,11 @@ void cmd_reload(I3_CMD) {
x_set_i3_atoms();
/* Send an IPC event just in case the ws names have changed */
ipc_send_workspace_event("reload", NULL, NULL);
/* Send an update event for the barconfig just in case it has changed */
update_barconfig();
/* Send an update event for each barconfig just in case it has changed */
Barconfig *current;
TAILQ_FOREACH (current, &barconfigs, configs) {
ipc_send_barconfig_update_event(current);
}

// XXX: default reply for now, make this a better reply
ysuccess(true);
Expand Down Expand Up @@ -2075,7 +2078,7 @@ void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name) {
* Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
*
*/
static bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
void cmd_bar_mode(I3_CMD, const char *bar_mode, const char *bar_id) {
int mode = M_DOCK;
bool toggle = false;
if (strcmp(bar_mode, "dock") == 0)
Expand All @@ -2088,39 +2091,38 @@ static bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
toggle = true;
else {
ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
return false;
assert(false);
}

bool changed_sth = false;
Barconfig *current = NULL;
TAILQ_FOREACH (current, &barconfigs, configs) {
if (bar_id && strcmp(current->id, bar_id) != 0)
if (strcmp(current->id, bar_id) != 0) {
continue;
}

if (toggle)
if (toggle) {
mode = (current->mode + 1) % 2;
}

DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
current->mode = mode;
changed_sth = true;

if (bar_id)
break;
}
DLOG("Changing bar mode of bar_id '%s' from '%d' to '%s (%d)'\n",
current->id, current->mode, bar_mode, mode);
if ((int)current->mode != mode) {
current->mode = mode;
ipc_send_barconfig_update_event(current);
}

if (bar_id && !changed_sth) {
DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
return false;
ysuccess(true);
return;
}

return true;
yerror("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
}

/*
* Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
*
*/
static bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
void cmd_bar_hidden_state(I3_CMD, const char *bar_hidden_state, const char *bar_id) {
int hidden_state = S_SHOW;
bool toggle = false;
if (strcmp(bar_hidden_state, "hide") == 0)
Expand All @@ -2131,54 +2133,31 @@ static bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_i
toggle = true;
else {
ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
return false;
assert(false);
}

bool changed_sth = false;
Barconfig *current = NULL;
TAILQ_FOREACH (current, &barconfigs, configs) {
if (bar_id && strcmp(current->id, bar_id) != 0)
if (strcmp(current->id, bar_id) != 0) {
continue;
}

if (toggle)
if (toggle) {
hidden_state = (current->hidden_state + 1) % 2;
}

DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
current->hidden_state = hidden_state;
changed_sth = true;

if (bar_id)
break;
}

if (bar_id && !changed_sth) {
DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
return false;
}

return true;
}

/*
* Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
*
*/
void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
bool ret;
if (strcmp(bar_type, "mode") == 0)
ret = cmd_bar_mode(bar_value, bar_id);
else if (strcmp(bar_type, "hidden_state") == 0)
ret = cmd_bar_hidden_state(bar_value, bar_id);
else {
ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
ret = false;
}
DLOG("Changing bar hidden_state of bar_id '%s' from '%d' to '%s (%d)'\n",
current->id, current->hidden_state, bar_hidden_state, hidden_state);
if ((int)current->hidden_state != hidden_state) {
current->hidden_state = hidden_state;
ipc_send_barconfig_update_event(current);
}

ysuccess(ret);
if (!ret)
ysuccess(true);
return;
}

update_barconfig();
yerror("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
}

/*
Expand Down
11 changes: 0 additions & 11 deletions src/config.c
Expand Up @@ -28,17 +28,6 @@ void ungrab_all_keys(xcb_connection_t *conn) {
xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
}

/*
* Sends the current bar configuration as an event to all barconfig_update listeners.
*
*/
void update_barconfig(void) {
Barconfig *current;
TAILQ_FOREACH (current, &barconfigs, configs) {
ipc_send_barconfig_update_event(current);
}
}

static void free_configuration(void) {
assert(conn != NULL);

Expand Down

0 comments on commit f63a4be

Please sign in to comment.