Introduce get_existing_workspace_by_num

This commit is contained in:
Orestis Floros 2018-03-27 22:36:51 +03:00
parent 6a2728ba79
commit 0b5799412a
No known key found for this signature in database
GPG Key ID: E9AD9F32E401E38F
4 changed files with 26 additions and 27 deletions

View File

@ -31,6 +31,13 @@
*/ */
Con *get_existing_workspace_by_name(const char *name); Con *get_existing_workspace_by_name(const char *name);
/**
* Returns the workspace with the given number or NULL if such a workspace does
* not exist.
*
*/
Con *get_existing_workspace_by_num(int num);
/** /**
* Returns a pointer to the workspace with the given number (starting at 0), * Returns a pointer to the workspace with the given number (starting at 0),
* creating the workspace if necessary (by allocating the necessary amount of * creating the workspace if necessary (by allocating the necessary amount of

View File

@ -394,21 +394,15 @@ void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *_no
} }
LOG("should move window to workspace %s\n", which); LOG("should move window to workspace %s\n", which);
/* get the workspace */
Con *output, *ws = NULL;
long parsed_num = ws_name_to_number(which); long parsed_num = ws_name_to_number(which);
if (parsed_num == -1) { if (parsed_num == -1) {
LOG("Could not parse initial part of \"%s\" as a number.\n", which); LOG("Could not parse initial part of \"%s\" as a number.\n", which);
yerror("Could not parse number \"%s\"", which); yerror("Could not parse number \"%s\"", which);
return; return;
} }
TAILQ_FOREACH(output, &(croot->nodes_head), nodes) Con *ws = get_existing_workspace_by_num(parsed_num);
GREP_FIRST(ws, output_get_content(output),
child->num == parsed_num);
if (!ws) { if (!ws) {
ws = workspace_get(which, NULL); ws = workspace_get(which, NULL);
} }
@ -901,7 +895,6 @@ void cmd_workspace(I3_CMD, const char *which) {
*/ */
void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) { void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL); const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
Con *output, *workspace = NULL;
if (con_get_fullscreen_con(croot, CF_GLOBAL)) { if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
LOG("Cannot switch workspace while in global fullscreen\n"); LOG("Cannot switch workspace while in global fullscreen\n");
@ -910,17 +903,13 @@ void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_a
} }
long parsed_num = ws_name_to_number(which); long parsed_num = ws_name_to_number(which);
if (parsed_num == -1) { if (parsed_num == -1) {
LOG("Could not parse initial part of \"%s\" as a number.\n", which); LOG("Could not parse initial part of \"%s\" as a number.\n", which);
yerror("Could not parse number \"%s\"", which); yerror("Could not parse number \"%s\"", which);
return; return;
} }
TAILQ_FOREACH(output, &(croot->nodes_head), nodes) Con *workspace = get_existing_workspace_by_num(parsed_num);
GREP_FIRST(workspace, output_get_content(output),
child->num == parsed_num);
if (!workspace) { if (!workspace) {
LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num); LOG("There is no workspace with number %ld, creating a new one.\n", parsed_num);
ysuccess(true); ysuccess(true);

View File

@ -265,13 +265,9 @@ void manage_window(xcb_window_t window, xcb_get_window_attributes_cookie_t cooki
Con *assigned_ws = NULL; Con *assigned_ws = NULL;
if (assignment->type == A_TO_WORKSPACE_NUMBER) { if (assignment->type == A_TO_WORKSPACE_NUMBER) {
Con *output = NULL;
long parsed_num = ws_name_to_number(assignment->dest.workspace); long parsed_num = ws_name_to_number(assignment->dest.workspace);
/* This will only work for workspaces that already exist. */ assigned_ws = get_existing_workspace_by_num(parsed_num);
TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
GREP_FIRST(assigned_ws, output_get_content(output), child->num == parsed_num);
}
} }
/* A_TO_WORKSPACE type assignment or fallback from A_TO_WORKSPACE_NUMBER /* A_TO_WORKSPACE type assignment or fallback from A_TO_WORKSPACE_NUMBER
* when the target workspace number does not exist yet. */ * when the target workspace number does not exist yet. */

View File

@ -33,6 +33,20 @@ Con *get_existing_workspace_by_name(const char *name) {
return workspace; return workspace;
} }
/*
* Returns the workspace with the given number or NULL if such a workspace does
* not exist.
*
*/
Con *get_existing_workspace_by_num(int num) {
Con *output, *workspace = NULL;
TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
GREP_FIRST(workspace, output_get_content(output), child->num == num);
}
return workspace;
}
/* /*
* 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
@ -225,17 +239,10 @@ Con *create_workspace_on_output(Output *output, Con *content) {
DLOG("Getting next unused workspace by number\n"); DLOG("Getting next unused workspace by number\n");
int c = 0; int c = 0;
while (exists) { while (exists) {
c++; exists = (get_existing_workspace_by_num(++c) != NULL);
ws->num = c;
Con *out, *current = NULL;
TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
GREP_FIRST(current, output_get_content(out), child->num == ws->num);
exists = (current != NULL);
DLOG("result for ws %d: exists = %d\n", c, exists); DLOG("result for ws %d: exists = %d\n", c, exists);
} }
ws->num = c;
sasprintf(&(ws->name), "%d", c); sasprintf(&(ws->name), "%d", c);
} }
con_attach(ws, content, false); con_attach(ws, content, false);