Extract workspace names from bindings before reordering.

fixes #1889
This commit is contained in:
Michael Stapelberg 2015-09-14 09:28:42 +02:00
parent e10b88fb81
commit 840ce51bfd
4 changed files with 74 additions and 19 deletions

View File

@ -25,7 +25,16 @@
*/ */
Con *workspace_get(const char *num, bool *created); Con *workspace_get(const char *num, bool *created);
/* /**
* Extracts workspace names from keybindings (e.g. web from bindsym $mod+1
* workspace web), so that when an output needs a workspace, i3 can start with
* the first configured one. Needs to be called before reorder_bindings() so
* that the config-file order is used, not the i3-internal order.
*
*/
void extract_workspace_names_from_bindings(void);
/**
* Returns a pointer to a new workspace in the given output. The workspace * Returns a pointer to a new workspace in the given output. The workspace
* is created attached to the tree hierarchy through the given content * is created attached to the tree hierarchy through the given content
* container. * container.

View File

@ -996,6 +996,7 @@ bool parse_file(const char *f, bool use_nagbar) {
struct ConfigResultIR *config_output = parse_config(new, context); struct ConfigResultIR *config_output = parse_config(new, context);
yajl_gen_free(config_output->json_gen); yajl_gen_free(config_output->json_gen);
extract_workspace_names_from_bindings();
check_for_duplicate_bindings(context); check_for_duplicate_bindings(context);
reorder_bindings(); reorder_bindings();

View File

@ -17,6 +17,10 @@
* back-and-forth switching. */ * back-and-forth switching. */
static char *previous_workspace_name = NULL; static char *previous_workspace_name = NULL;
/* NULL-terminated list of workspace names (in order) extracted from
* keybindings. */
static char **binding_workspace_names = NULL;
/* /*
* Sets ws->layout to splith/splitv if default_orientation was specified in the * Sets ws->layout to splith/splitv if default_orientation was specified in the
* configfile. Otherwise, it uses splith/splitv depending on whether the output * configfile. Otherwise, it uses splith/splitv depending on whether the output
@ -107,21 +111,21 @@ Con *workspace_get(const char *num, bool *created) {
} }
/* /*
* Returns a pointer to a new workspace in the given output. The workspace * Extracts workspace names from keybindings (e.g. web from bindsym $mod+1
* is created attached to the tree hierarchy through the given content * workspace web), so that when an output needs a workspace, i3 can start with
* container. * the first configured one. Needs to be called before reorder_bindings() so
* that the config-file order is used, not the i3-internal order.
* *
*/ */
Con *create_workspace_on_output(Output *output, Con *content) { void extract_workspace_names_from_bindings(void) {
/* add a workspace to this output */
Con *out, *current;
char *name;
bool exists = true;
Con *ws = con_new(NULL, NULL);
ws->type = CT_WORKSPACE;
/* try the configured workspace bindings first to find a free name */
Binding *bind; Binding *bind;
int n = 0;
if (binding_workspace_names != NULL) {
for (int i = 0; binding_workspace_names[i] != NULL; i++) {
free(binding_workspace_names[i]);
}
FREE(binding_workspace_names);
}
TAILQ_FOREACH(bind, bindings, bindings) { TAILQ_FOREACH(bind, bindings, bindings) {
DLOG("binding with command %s\n", bind->command); DLOG("binding with command %s\n", bind->command);
if (strlen(bind->command) < strlen("workspace ") || if (strlen(bind->command) < strlen("workspace ") ||
@ -152,17 +156,39 @@ Con *create_workspace_on_output(Output *output, Con *content) {
free(target_name); free(target_name);
continue; continue;
} }
FREE(ws->name); DLOG("Saving workspace name \"%s\"\n", target_name);
ws->name = target_name;
DLOG("trying name *%s*\n", ws->name);
binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char*));
binding_workspace_names[n-1] = target_name;
}
binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char*));
binding_workspace_names[n-1] = NULL;
}
/*
* Returns a pointer to a new workspace in the given output. The workspace
* is created attached to the tree hierarchy through the given content
* container.
*
*/
Con *create_workspace_on_output(Output *output, Con *content) {
/* add a workspace to this output */
Con *out, *current;
char *name;
bool exists = true;
Con *ws = con_new(NULL, NULL);
ws->type = CT_WORKSPACE;
/* try the configured workspace bindings first to find a free name */
for (int n = 0; binding_workspace_names[n] != NULL; n++) {
char *target_name = binding_workspace_names[n];
/* Ensure that this workspace is not assigned to a different output — /* Ensure that this workspace is not assigned to a different output —
* otherwise we would create it, then move it over to its output, then * otherwise we would create it, then move it over to its output, then
* find a new workspace, etc */ * find a new workspace, etc */
bool assigned = false; bool assigned = false;
struct Workspace_Assignment *assignment; struct Workspace_Assignment *assignment;
TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) { TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
if (strcmp(assignment->name, ws->name) != 0 || if (strcmp(assignment->name, target_name) != 0 ||
strcmp(assignment->output, output->name) == 0) strcmp(assignment->output, output->name) == 0)
continue; continue;
@ -175,10 +201,10 @@ Con *create_workspace_on_output(Output *output, Con *content) {
current = NULL; current = NULL;
TAILQ_FOREACH(out, &(croot->nodes_head), nodes) TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name)); GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, target_name));
exists = (current != NULL); exists = (current != NULL);
if (!exists) { if (!exists) {
ws->name = sstrdup(target_name);
/* Set ->num to the number of the workspace, if the name actually /* Set ->num to the number of the workspace, if the name actually
* is a number or starts with a number */ * is a number or starts with a number */
ws->num = ws_name_to_number(ws->name); ws->num = ws_name_to_number(ws->name);

View File

@ -106,4 +106,23 @@ is_deeply(\@names, [ '3' ], 'i3 starts on workspace 3 without ;exec foo');
exit_gracefully($pid); exit_gracefully($pid);
################################################################################
# 6: verify internal binding reordering does not affect startup workspace
################################################################################
$config = <<EOT;
# i3 config file (v4)
font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
bindsym Mod1+1 workspace 3
bindsym Shift+Mod1+1 workspace 4
EOT
$pid = launch_with_config($config);
@names = @{get_workspace_names()};
is_deeply(\@names, [ '3' ], 'i3 starts on workspace 3');
exit_gracefully($pid);
done_testing; done_testing;