Codebase list i3-gaps / f63a4be
cmd_bar improvements (#4014) - 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 Orestis Floros authored 4 years ago GitHub committed 4 years ago
6 changed file(s) with 68 addition(s) and 97 deletion(s). Raw diff Collapse all Expand all
1616 • make dock client order deterministic (sorted by class/instance) as a
1717 side effect, i3bars without an explicit bar-id will be sorted according
1818 to their definition order in the config file
19 • update i3bar config when necessary (reduces redraws on bar mode changes)
1920 • mention rofi in default config file
2021
2122 ┌────────────────────────────┐
314314 void cmd_rename_workspace(I3_CMD, const char *old_name, const char *new_name);
315315
316316 /**
317 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
318 *
319 */
320 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id);
317 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
318 *
319 */
320 void cmd_bar_mode(I3_CMD, const char *bar_mode, const char *bar_id);
321
322 /**
323 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
324 *
325 */
326 void cmd_bar_hidden_state(I3_CMD, const char *bar_hidden_state, const char *bar_id);
321327
322328 /**
323329 * Implementation of 'shmlog <size>|toggle|on|off'
424424 *
425425 */
426426 void ungrab_all_keys(xcb_connection_t *conn);
427
428 /**
429 * Sends the current bar configuration as an event to all barconfig_update listeners.
430 *
431 */
432 void update_barconfig(void);
459459 -> BAR_MODE
460460
461461 state BAR_HIDDEN_STATE:
462 bar_value = 'hide', 'show', 'toggle'
463 -> BAR_W_ID
462 bar_hidden_state = 'hide', 'show', 'toggle'
463 ->
464 bar_id = word
465 ->
466 end
467 -> call cmd_bar_hidden_state($bar_hidden_state, $bar_id)
464468
465469 state BAR_MODE:
466470 bar_value = 'dock', 'hide', 'invisible', 'toggle'
467 -> BAR_W_ID
468
469 state BAR_W_ID:
471 ->
470472 bar_id = word
471473 ->
472474 end
473 -> call cmd_bar($bar_type, $bar_value, $bar_id)
475 -> call cmd_bar_mode($bar_value, $bar_id)
16201620 x_set_i3_atoms();
16211621 /* Send an IPC event just in case the ws names have changed */
16221622 ipc_send_workspace_event("reload", NULL, NULL);
1623 /* Send an update event for the barconfig just in case it has changed */
1624 update_barconfig();
1623 /* Send an update event for each barconfig just in case it has changed */
1624 Barconfig *current;
1625 TAILQ_FOREACH (current, &barconfigs, configs) {
1626 ipc_send_barconfig_update_event(current);
1627 }
16251628
16261629 // XXX: default reply for now, make this a better reply
16271630 ysuccess(true);
20742077 * Implementation of 'bar mode dock|hide|invisible|toggle [<bar_id>]'
20752078 *
20762079 */
2077 static bool cmd_bar_mode(const char *bar_mode, const char *bar_id) {
2080 void cmd_bar_mode(I3_CMD, const char *bar_mode, const char *bar_id) {
20782081 int mode = M_DOCK;
20792082 bool toggle = false;
20802083 if (strcmp(bar_mode, "dock") == 0)
20872090 toggle = true;
20882091 else {
20892092 ELOG("Unknown bar mode \"%s\", this is a mismatch between code and parser spec.\n", bar_mode);
2090 return false;
2091 }
2092
2093 bool changed_sth = false;
2093 assert(false);
2094 }
2095
20942096 Barconfig *current = NULL;
20952097 TAILQ_FOREACH (current, &barconfigs, configs) {
2096 if (bar_id && strcmp(current->id, bar_id) != 0)
2098 if (strcmp(current->id, bar_id) != 0) {
20972099 continue;
2098
2099 if (toggle)
2100 }
2101
2102 if (toggle) {
21002103 mode = (current->mode + 1) % 2;
2101
2102 DLOG("Changing bar mode of bar_id '%s' to '%s (%d)'\n", current->id, bar_mode, mode);
2103 current->mode = mode;
2104 changed_sth = true;
2105
2106 if (bar_id)
2107 break;
2108 }
2109
2110 if (bar_id && !changed_sth) {
2111 DLOG("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
2112 return false;
2113 }
2114
2115 return true;
2104 }
2105
2106 DLOG("Changing bar mode of bar_id '%s' from '%d' to '%s (%d)'\n",
2107 current->id, current->mode, bar_mode, mode);
2108 if ((int)current->mode != mode) {
2109 current->mode = mode;
2110 ipc_send_barconfig_update_event(current);
2111 }
2112
2113 ysuccess(true);
2114 return;
2115 }
2116
2117 yerror("Changing bar mode of bar_id %s failed, bar_id not found.\n", bar_id);
21162118 }
21172119
21182120 /*
21192121 * Implementation of 'bar hidden_state hide|show|toggle [<bar_id>]'
21202122 *
21212123 */
2122 static bool cmd_bar_hidden_state(const char *bar_hidden_state, const char *bar_id) {
2124 void cmd_bar_hidden_state(I3_CMD, const char *bar_hidden_state, const char *bar_id) {
21232125 int hidden_state = S_SHOW;
21242126 bool toggle = false;
21252127 if (strcmp(bar_hidden_state, "hide") == 0)
21302132 toggle = true;
21312133 else {
21322134 ELOG("Unknown bar state \"%s\", this is a mismatch between code and parser spec.\n", bar_hidden_state);
2133 return false;
2134 }
2135
2136 bool changed_sth = false;
2135 assert(false);
2136 }
2137
21372138 Barconfig *current = NULL;
21382139 TAILQ_FOREACH (current, &barconfigs, configs) {
2139 if (bar_id && strcmp(current->id, bar_id) != 0)
2140 if (strcmp(current->id, bar_id) != 0) {
21402141 continue;
2141
2142 if (toggle)
2142 }
2143
2144 if (toggle) {
21432145 hidden_state = (current->hidden_state + 1) % 2;
2144
2145 DLOG("Changing bar hidden_state of bar_id '%s' to '%s (%d)'\n", current->id, bar_hidden_state, hidden_state);
2146 current->hidden_state = hidden_state;
2147 changed_sth = true;
2148
2149 if (bar_id)
2150 break;
2151 }
2152
2153 if (bar_id && !changed_sth) {
2154 DLOG("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
2155 return false;
2156 }
2157
2158 return true;
2159 }
2160
2161 /*
2162 * Implementation of 'bar (hidden_state hide|show|toggle)|(mode dock|hide|invisible|toggle) [<bar_id>]'
2163 *
2164 */
2165 void cmd_bar(I3_CMD, const char *bar_type, const char *bar_value, const char *bar_id) {
2166 bool ret;
2167 if (strcmp(bar_type, "mode") == 0)
2168 ret = cmd_bar_mode(bar_value, bar_id);
2169 else if (strcmp(bar_type, "hidden_state") == 0)
2170 ret = cmd_bar_hidden_state(bar_value, bar_id);
2171 else {
2172 ELOG("Unknown bar option type \"%s\", this is a mismatch between code and parser spec.\n", bar_type);
2173 ret = false;
2174 }
2175
2176 ysuccess(ret);
2177 if (!ret)
2178 return;
2179
2180 update_barconfig();
2146 }
2147
2148 DLOG("Changing bar hidden_state of bar_id '%s' from '%d' to '%s (%d)'\n",
2149 current->id, current->hidden_state, bar_hidden_state, hidden_state);
2150 if ((int)current->hidden_state != hidden_state) {
2151 current->hidden_state = hidden_state;
2152 ipc_send_barconfig_update_event(current);
2153 }
2154
2155 ysuccess(true);
2156 return;
2157 }
2158
2159 yerror("Changing bar hidden_state of bar_id %s failed, bar_id not found.\n", bar_id);
21812160 }
21822161
21832162 /*
2525 void ungrab_all_keys(xcb_connection_t *conn) {
2626 DLOG("Ungrabbing all keys\n");
2727 xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
28 }
29
30 /*
31 * Sends the current bar configuration as an event to all barconfig_update listeners.
32 *
33 */
34 void update_barconfig(void) {
35 Barconfig *current;
36 TAILQ_FOREACH (current, &barconfigs, configs) {
37 ipc_send_barconfig_update_event(current);
38 }
3928 }
4029
4130 static void free_configuration(void) {