foo-wm

(Archived) Experimental Zooming IPC-based WM
git clone http://milesalan.com/git/foo-wm
Log | Files | Refs | Mirror | README | LICENSE

commit adc903e6964c40e1ef0d2d5c47afe7a79816719b
parent 832c9d4e6be41f34c2b110f0942eec81e50b2e84
Author: Miles Sandlar <miles.sandlar@gmail.com>
Date:   Mon,  9 Jul 2012 10:33:20 -0400

More concise and cleaner token parsing

Diffstat:
Msrc/commands.c | 195+++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/commands.h | 4++++
2 files changed, 100 insertions(+), 99 deletions(-)

diff --git a/src/commands.c b/src/commands.c @@ -8,6 +8,102 @@ #include "util.h" +char * nextToken(char ** tokenString) { + char *command; + command = strsep(tokenString, " "); + + if (command == NULL) return NULL; + + char *newLine = strchr(command, '\n'); + if (newLine != NULL) + *newLine = '\0'; + + return command; +} + +void handleCommand(char* request) { + fprintf(stderr, "Recv from FIFO: %s", request); + + char *tokens[5]; + + char *token; int i = 0; + while ((token = nextToken(&request)) != NULL) { + tokens[i] = token; + i++; + } + + if (!strcmp(tokens[0], "dump")) { + dumpTree(); + } else if (!strcmp(tokens[0], "layout")) { + fprintf(stderr, "Setting layout to: %s", tokens[1]); + if (!strcmp(tokens[1], "vertical")) focusedNode -> parent -> layout = 0; + else if (!strcmp(tokens[1], "horizontal")) focusedNode -> parent -> layout = 1; + else if (!strcmp(tokens[1], "grid")) focusedNode -> parent -> layout = 2; + placeNode(focusedNode -> parent, + (focusedNode -> parent) -> x, (focusedNode -> parent) -> y, + (focusedNode -> parent) -> width, (focusedNode -> parent) -> height); + + } else if (!strcmp(tokens[0], "focus")) { + if (!strcmp(tokens[1], "left")) focus(0); + else if (!strcmp(tokens[1], "up")) focus(1); + else if (!strcmp(tokens[1], "right")) focus(2); + else if (!strcmp(tokens[1], "down")) focus(3); + + } else if (!strcmp(tokens[0], "select")) { + if (!strcmp(tokens[1], "parent")) { + fprintf(stderr, "Selecting parent node\n"); + if (selectedNode == NULL) { + fprintf(stderr, "Selected Node is NULL\n"); + selectNode(focusedNode -> parent, True); + } else { + if (selectedNode -> parent != NULL) { + fprintf(stderr, "Selecting from Parent\n"); + selectNode(selectedNode -> parent, True); + } + } + } else if (!strcmp(tokens[1], "next")) { + + } else if (!strcmp(tokens[1], "previous")) { + + } + + } else if (!strcmp(tokens[0], "containerize")) { + + if (!strcmp(tokens[1], "client")) containerize(); + + } else if (!strcmp(tokens[0], "view")) { + + if (!strcmp(tokens[1], "parent")) { + if (viewNode -> parent != NULL) { + unmapNode(viewNode); + viewNode = viewNode -> parent; + placeNode(viewNode, rootX, rootY, rootWidth, rootHeight); + } + + } else if (!strcmp(tokens[1], "child")) { + + if (focusedNode != viewNode) { + Node *n = focusedNode; + while (n -> parent != viewNode && n != NULL) { n = n -> parent; } + + unmapNode(viewNode); + viewNode = n; + placeNode(viewNode, rootX, rootY, rootWidth, rootHeight); + } + } + } else if (!strcmp(tokens[0], "kill")) { + if (!strcmp(tokens[1], "client")) { + kill(); + } else if (!strcmp(tokens[1], "container")) { + dumpTree(); + fprintf(stderr, "Destroy Container %p\n", focusedNode -> parent); + //destroyContainer(currentContainer); + dumpTree(); + } + } +} + + void focus(int direction) { /* //Direction: 0 - Left, 1 - Up, 2 - Right, 3 - Down @@ -134,103 +230,4 @@ void kill() { -void handleCommand(char* request) { - fprintf(stderr, "Recv from FIFO: %s", request); - - //Array to contain pointers to tokens, max of 5 tokens - char *tokens[5]; - - int counter = 0; - char *lastToken, *token; - - int totalTokens = 0; char *d; - for (d = request; *d != '\0'; d++) { - if (*d == ' ') totalTokens++; - } - fprintf(stderr, "Have %d spaces/tokens", totalTokens); - - - for ( token = strtok_r(request, " ", &lastToken); token; token = strtok_r(NULL, " ", &lastToken)) { - tokens[counter++] = token; - - if (counter - 1 == totalTokens) { - fprintf(stderr, "Running %s", token); - char *c = token; - c = c + strlen(token); - *(c - 1) = '\0'; - } - - fprintf(stderr, "Adding Token [%s]\n", token); - } - - if (!strcmp(tokens[0], "dump")) { - dumpTree(); - } else if (!strcmp(tokens[0], "layout")) { - fprintf(stderr, "Setting layout to: %s", tokens[1]); - if (!strcmp(tokens[1], "vertical")) focusedNode -> parent -> layout = 0; - else if (!strcmp(tokens[1], "horizontal")) focusedNode -> parent -> layout = 1; - else if (!strcmp(tokens[1], "grid")) focusedNode -> parent -> layout = 2; - placeNode(focusedNode -> parent, - (focusedNode -> parent) -> x, (focusedNode -> parent) -> y, - (focusedNode -> parent) -> width, (focusedNode -> parent) -> height); - - } else if (!strcmp(tokens[0], "focus")) { - if (!strcmp(tokens[1], "left")) focus(0); - else if (!strcmp(tokens[1], "up")) focus(1); - else if (!strcmp(tokens[1], "right")) focus(2); - else if (!strcmp(tokens[1], "down")) focus(3); - - } else if (!strcmp(tokens[0], "select")) { - if (!strcmp(tokens[1], "parent")) { - fprintf(stderr, "Selecting parent node\n"); - if (selectedNode == NULL) { - fprintf(stderr, "Selected Node is NULL\n"); - selectNode(focusedNode -> parent, True); - } else { - if (selectedNode -> parent != NULL) { - fprintf(stderr, "Selecting from Parent\n"); - selectNode(selectedNode -> parent, True); - } - } - } else if (!strcmp(tokens[1], "next")) { - - } else if (!strcmp(tokens[1], "previous")) { - - } - - } else if (!strcmp(tokens[0], "containerize")) { - - if (!strcmp(tokens[1], "client")) containerize(); - - } else if (!strcmp(tokens[0], "view")) { - - if (!strcmp(tokens[1], "parent")) { - if (viewNode -> parent != NULL) { - unmapNode(viewNode); - viewNode = viewNode -> parent; - placeNode(viewNode, rootX, rootY, rootWidth, rootHeight); - } - - } else if (!strcmp(tokens[1], "child")) { - - if (focusedNode != viewNode) { - Node *n = focusedNode; - while (n -> parent != viewNode && n != NULL) { n = n -> parent; } - - unmapNode(viewNode); - viewNode = n; - placeNode(viewNode, rootX, rootY, rootWidth, rootHeight); - } - } - } else if (!strcmp(tokens[0], "kill")) { - if (!strcmp(tokens[1], "client")) { - kill(); - } else if (!strcmp(tokens[1], "container")) { - dumpTree(); - fprintf(stderr, "Destroy Container %p\n", focusedNode -> parent); - //destroyContainer(currentContainer); - dumpTree(); - } - } -} diff --git a/src/commands.h b/src/commands.h @@ -1 +1,5 @@ +char * nextToken(char ** tokenString); void handleCommand(char* request); +void focus(int direction); +void containerize(); +void kill();