diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-12-17 19:48:55 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-12-17 19:48:55 -0500 |
commit | f994f00d0059039d952d7dcfb72a683f303878bc (patch) | |
tree | 32c96595026c36cbb88a7c57544396b74c9128f0 | |
parent | Merge pull request #351 from mikkeloscar/swaygrab-default-file (diff) | |
parent | sway: insert numbered workspaces in order (diff) | |
download | sway-f994f00d0059039d952d7dcfb72a683f303878bc.tar.gz sway-f994f00d0059039d952d7dcfb72a683f303878bc.tar.zst sway-f994f00d0059039d952d7dcfb72a683f303878bc.zip |
Merge pull request #352 from progandy/workspace-numbers
Workspace numbers
-rw-r--r-- | include/layout.h | 4 | ||||
-rw-r--r-- | include/workspace.h | 1 | ||||
-rw-r--r-- | sway/commands.c | 15 | ||||
-rw-r--r-- | sway/container.c | 20 | ||||
-rw-r--r-- | sway/workspace.c | 22 |
5 files changed, 57 insertions, 5 deletions
diff --git a/include/layout.h b/include/layout.h index 62e4c202..b7731031 100644 --- a/include/layout.h +++ b/include/layout.h | |||
@@ -24,6 +24,10 @@ int index_child(const swayc_t *child); | |||
24 | // parent must be of type C_WORKSPACE or C_CONTAINER | 24 | // parent must be of type C_WORKSPACE or C_CONTAINER |
25 | void add_child(swayc_t *parent, swayc_t *child); | 25 | void add_child(swayc_t *parent, swayc_t *child); |
26 | 26 | ||
27 | // Adds child to parent at index, if parent has no focus, it is set to child | ||
28 | // parent must be of type C_WORKSPACE or C_CONTAINER | ||
29 | void insert_child(swayc_t *parent, swayc_t *child, int index); | ||
30 | |||
27 | // Adds child as floating window to ws, if there is no focus it is set to child. | 31 | // Adds child as floating window to ws, if there is no focus it is set to child. |
28 | // ws must be of type C_WORKSPACE | 32 | // ws must be of type C_WORKSPACE |
29 | void add_floating(swayc_t *ws, swayc_t *child); | 33 | void add_floating(swayc_t *ws, swayc_t *child); |
diff --git a/include/workspace.h b/include/workspace.h index b916f715..c69ccdbb 100644 --- a/include/workspace.h +++ b/include/workspace.h | |||
@@ -10,6 +10,7 @@ extern char *prev_workspace_name; | |||
10 | char *workspace_next_name(void); | 10 | char *workspace_next_name(void); |
11 | swayc_t *workspace_create(const char*); | 11 | swayc_t *workspace_create(const char*); |
12 | swayc_t *workspace_by_name(const char*); | 12 | swayc_t *workspace_by_name(const char*); |
13 | swayc_t *workspace_by_number(const char*); | ||
13 | bool workspace_switch(swayc_t*); | 14 | bool workspace_switch(swayc_t*); |
14 | swayc_t *workspace_output_next(); | 15 | swayc_t *workspace_output_next(); |
15 | swayc_t *workspace_next(); | 16 | swayc_t *workspace_next(); |
diff --git a/sway/commands.c b/sway/commands.c index b40cdb6a..24d56052 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -649,12 +649,15 @@ static struct cmd_results *cmd_move(int argc, char **argv) { | |||
649 | } | 649 | } |
650 | 650 | ||
651 | const char *ws_name = argv[3]; | 651 | const char *ws_name = argv[3]; |
652 | if (argc == 5) { | 652 | swayc_t *ws; |
653 | if (argc == 5 && strcasecmp(ws_name, "number") == 0) { | ||
653 | // move "container to workspace number x" | 654 | // move "container to workspace number x" |
654 | ws_name = argv[4]; | 655 | ws_name = argv[4]; |
656 | ws = workspace_by_number(ws_name); | ||
657 | } else { | ||
658 | ws = workspace_by_name(ws_name); | ||
655 | } | 659 | } |
656 | 660 | ||
657 | swayc_t *ws = workspace_by_name(ws_name); | ||
658 | if (ws == NULL) { | 661 | if (ws == NULL) { |
659 | ws = workspace_create(ws_name); | 662 | ws = workspace_create(ws_name); |
660 | } | 663 | } |
@@ -1435,13 +1438,17 @@ static struct cmd_results *cmd_workspace(int argc, char **argv) { | |||
1435 | if ((error = checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1))) { | 1438 | if ((error = checkarg(argc, "workspace", EXPECTED_AT_LEAST, 1))) { |
1436 | return error; | 1439 | return error; |
1437 | } | 1440 | } |
1438 | if (argc == 1) { | 1441 | if (argc == 1 || (argc == 2 && strcasecmp(argv[0], "number") == 0) ) { |
1439 | if (config->reading || !config->active) { | 1442 | if (config->reading || !config->active) { |
1440 | return cmd_results_new(CMD_DEFER, "workspace", NULL); | 1443 | return cmd_results_new(CMD_DEFER, "workspace", NULL); |
1441 | } | 1444 | } |
1442 | // Handle workspace next/prev | 1445 | // Handle workspace next/prev |
1443 | swayc_t *ws = NULL; | 1446 | swayc_t *ws = NULL; |
1444 | if (strcasecmp(argv[0], "next") == 0) { | 1447 | if (argc == 2) { |
1448 | if (!(ws=workspace_by_number(argv[1]))) { | ||
1449 | ws = workspace_create(argv[1]); | ||
1450 | } | ||
1451 | }else if (strcasecmp(argv[0], "next") == 0) { | ||
1445 | ws = workspace_next(); | 1452 | ws = workspace_next(); |
1446 | } else if (strcasecmp(argv[0], "prev") == 0) { | 1453 | } else if (strcasecmp(argv[0], "prev") == 0) { |
1447 | ws = workspace_prev(); | 1454 | ws = workspace_prev(); |
diff --git a/sway/container.c b/sway/container.c index 8165bbad..36056ff7 100644 --- a/sway/container.c +++ b/sway/container.c | |||
@@ -1,3 +1,4 @@ | |||
1 | #include <ctype.h> | ||
1 | #include <stdlib.h> | 2 | #include <stdlib.h> |
2 | #include <stdbool.h> | 3 | #include <stdbool.h> |
3 | #include <strings.h> | 4 | #include <strings.h> |
@@ -168,7 +169,24 @@ swayc_t *new_workspace(swayc_t *output, const char *name) { | |||
168 | workspace->visible = false; | 169 | workspace->visible = false; |
169 | workspace->floating = create_list(); | 170 | workspace->floating = create_list(); |
170 | 171 | ||
171 | add_child(output, workspace); | 172 | if (isdigit(workspace->name[0])) { |
173 | // find position for numbered workspace | ||
174 | // order: ascending numbers, insert before same number | ||
175 | // numbers before unnumbered | ||
176 | int num = strtol(workspace->name, NULL, 10); | ||
177 | int i; | ||
178 | for (i = 0; i < output->children->length; ++i) { | ||
179 | char *name = ((swayc_t *)output->children->items[i])->name; | ||
180 | if (!isdigit(name[0]) || num <= strtol(name, NULL, 10)) { | ||
181 | break; | ||
182 | } | ||
183 | } | ||
184 | insert_child(output, workspace, i); | ||
185 | |||
186 | } else { | ||
187 | // append new unnumbered to the end | ||
188 | add_child(output, workspace); | ||
189 | } | ||
172 | return workspace; | 190 | return workspace; |
173 | } | 191 | } |
174 | 192 | ||
diff --git a/sway/workspace.c b/sway/workspace.c index 5e6ea799..761a5f4e 100644 --- a/sway/workspace.c +++ b/sway/workspace.c | |||
@@ -17,6 +17,11 @@ | |||
17 | #include "ipc.h" | 17 | #include "ipc.h" |
18 | 18 | ||
19 | char *prev_workspace_name = NULL; | 19 | char *prev_workspace_name = NULL; |
20 | struct workspace_by_number_data { | ||
21 | int len; | ||
22 | const char *cset; | ||
23 | const char *name; | ||
24 | }; | ||
20 | 25 | ||
21 | char *workspace_next_name(void) { | 26 | char *workspace_next_name(void) { |
22 | sway_log(L_DEBUG, "Workspace: Generating new name"); | 27 | sway_log(L_DEBUG, "Workspace: Generating new name"); |
@@ -133,6 +138,23 @@ swayc_t *workspace_by_name(const char* name) { | |||
133 | } | 138 | } |
134 | } | 139 | } |
135 | 140 | ||
141 | static bool _workspace_by_number(swayc_t *view, void *data) { | ||
142 | if (view->type != C_WORKSPACE) { | ||
143 | return false; | ||
144 | } | ||
145 | struct workspace_by_number_data *wbnd = data; | ||
146 | int a = strspn(view->name, wbnd->cset); | ||
147 | return a == wbnd->len && strncmp(view->name, wbnd->name, a) == 0; | ||
148 | } | ||
149 | swayc_t *workspace_by_number(const char* name) { | ||
150 | struct workspace_by_number_data wbnd = {0, "1234567890", name}; | ||
151 | wbnd.len = strspn(name, wbnd.cset); | ||
152 | if (wbnd.len <= 0) { | ||
153 | return NULL; | ||
154 | } | ||
155 | return swayc_by_test(&root_container, _workspace_by_number, (void *) &wbnd); | ||
156 | } | ||
157 | |||
136 | /** | 158 | /** |
137 | * Get the previous or next workspace on the specified output. | 159 | * Get the previous or next workspace on the specified output. |
138 | * Wraps around at the end and beginning. | 160 | * Wraps around at the end and beginning. |