summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar progandy <code@progandy>2015-12-17 22:28:41 +0100
committerLibravatar progandy <code@progandy>2015-12-18 01:21:31 +0100
commit8f1ac1ef2c77665e7c17eaa6a46ea1b6d68e5c22 (patch)
tree83806713b8d0f1c562b70ca2f2a87bde30095191
parentMerge pull request #351 from mikkeloscar/swaygrab-default-file (diff)
downloadsway-8f1ac1ef2c77665e7c17eaa6a46ea1b6d68e5c22.tar.gz
sway-8f1ac1ef2c77665e7c17eaa6a46ea1b6d68e5c22.tar.zst
sway-8f1ac1ef2c77665e7c17eaa6a46ea1b6d68e5c22.zip
sway: enable workspace selection by number
-rw-r--r--include/workspace.h1
-rw-r--r--sway/commands.c15
-rw-r--r--sway/workspace.c22
3 files changed, 34 insertions, 4 deletions
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;
10char *workspace_next_name(void); 10char *workspace_next_name(void);
11swayc_t *workspace_create(const char*); 11swayc_t *workspace_create(const char*);
12swayc_t *workspace_by_name(const char*); 12swayc_t *workspace_by_name(const char*);
13swayc_t *workspace_by_number(const char*);
13bool workspace_switch(swayc_t*); 14bool workspace_switch(swayc_t*);
14swayc_t *workspace_output_next(); 15swayc_t *workspace_output_next();
15swayc_t *workspace_next(); 16swayc_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/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
19char *prev_workspace_name = NULL; 19char *prev_workspace_name = NULL;
20struct workspace_by_number_data {
21 int len;
22 const char *cset;
23 const char *name;
24};
20 25
21char *workspace_next_name(void) { 26char *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
141static 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}
149swayc_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.