foo-wm

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

commit 10f917d14a282eb0eebd40d58b91e78d8c9bfeeb
parent 1f86ce1cfcea0d18f450615ba68594e76b55aab6
Author: Miles Sandlar <miles.sandlar@gmail.com>
Date:   Mon, 25 Jun 2012 23:50:36 -0400

Broke up fifo-wm.c: commands, tree, util, window
Modified Makefile accordingly

Diffstat:
MMakefile | 2+-
Asrc/commands.c | 36++++++++++++++++++++++++++++++++++++
Asrc/commands.h | 1+
Msrc/events.c | 10+++++++++-
Msrc/events.h | 1+
Msrc/fifo-wm.c | 249++-----------------------------------------------------------------------------
Msrc/fifo-wm.h | 11-----------
Asrc/tree.c | 144+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/tree.h | 6++++++
Asrc/util.c | 24++++++++++++++++++++++++
Asrc/util.h | 2++
Asrc/window.c | 39+++++++++++++++++++++++++++++++++++++++
Asrc/window.h | 2++
13 files changed, 269 insertions(+), 258 deletions(-)

diff --git a/Makefile b/Makefile @@ -3,7 +3,7 @@ CFLAGS=-lX11 -g VPATH=./src all:fifo-wm -fifo-wm: events.o +fifo-wm: events.o tree.o commands.o util.o window.o clean: rm -rf fifo-wm *.o diff --git a/src/commands.c b/src/commands.c @@ -0,0 +1,36 @@ +#include <stdio.h> +#include <string.h> + +#include "fifo-wm.h" +#include "commands.h" + +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; + for ( token = strtok_r(request, " ", &lastToken); token; token = strtok_r(NULL, " ", &lastToken)) { + tokens[counter++] = token; + fprintf(stderr, "Adding Token %s\n", token); + } + + + fprintf(stderr, "Last Token %s"); + + if (!strcmp(tokens[0], "kill")) { + fprintf(stderr, "Killing Client"); + } else 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")) { + lastContainer -> layout = 0; + } else if (!strcmp(tokens[1], "horizontal")) { + lastContainer -> layout = 1; + } + } + +} diff --git a/src/commands.h b/src/commands.h @@ -0,0 +1 @@ +void handleCommand(char* request); diff --git a/src/events.c b/src/events.c @@ -3,8 +3,16 @@ #include <string.h> #include <X11/Xlib.h> -#include "events.h" #include "fifo-wm.h" +#include "events.h" + +void handleXEvent(XEvent *event) { + switch (event -> type) { + case MapRequest: eMapRequest(event); break; + case ButtonPress: eButtonPress(event); break; + default: break; + } +} void eMapRequest(XEvent *event) { Client *newClient; diff --git a/src/events.h b/src/events.h @@ -1,2 +1,3 @@ +void handleXEvent(XEvent *event); void eMapRequest(XEvent *event); void eButtonPress(XEvent *event); diff --git a/src/fifo-wm.c b/src/fifo-wm.c @@ -8,241 +8,10 @@ #include "config.h" #include "fifo-wm.h" #include "events.h" - -/* Tracks the current container */ -void crawlContainer(Container * container, int level) { - Container *c; - for (c = container; c != NULL; c = c -> next) { - int j; - for (j = level; j > 0; j--) { fprintf(stderr, "\t"); } - char *or = c -> layout == 0 ? "Vertical" : "Horizontal"; - fprintf(stderr, "[%d]=> Container (%s)\n", level, or); - if (c -> client != NULL) { - Client *d; - for (d = c -> client; d != NULL; d = d -> next) { - int h; - for (h = level + 1; h > 0; h--) { fprintf(stderr, "\t"); } - fprintf(stderr, "Client\n"); - } - } - if (c -> child != NULL) { - crawlContainer(c -> child, level + 1); - } - } -} - - -void dumpTree() { - fprintf(stderr, "Printing the tree\n"); - crawlContainer(currentContainer , 0); -} - - -/* ========================= - * Handling of FIFO Commands - * ========================= */ -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; - for ( token = strtok_r(request, " ", &lastToken); token; token = strtok_r(NULL, " ", &lastToken)) { - tokens[counter++] = token; - fprintf(stderr, "Adding Token %s\n", token); - } - - - fprintf(stderr, "Last Token %s"); - - if (!strcmp(tokens[0], "kill")) { - fprintf(stderr, "Killing Client"); - } else 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")) { - lastContainer -> layout = 0; - } else if (!strcmp(tokens[1], "horizontal")) { - lastContainer -> layout = 1; - } - } - -} - - -/* =================== - * Tree Related - * =================== */ - -int parentClient(Client * child, Container * parent) { - /* First client to be added to container */ - if (parent == NULL) { return 0; } - - child -> parent = parent; - - if (parent -> client == NULL) { - fprintf(stderr, "Addng client\n"); - parent -> client = child; - - } else { - Client *c = parent -> client; - while (c -> next != NULL) { c = c -> next; } - c -> next = child; - child -> previous = c; - child -> parent = parent; - } -} - -int parentContainer(Container * child, Container * parent) { - if (parent == NULL) { return 0; } - - child -> parent = parent; - - if (parent -> child == NULL) { - parent -> child = child; - - } else { - Container *c = parent -> child; - while (c -> next != NULL) { c = c -> next; } - c -> next = child; - child -> previous = c; - child -> parent = parent; - } -} - -int placeContainer(Container * container, int x, int y, int width, int height) { - - //Count up children - int children = 0; - Client *a = malloc(sizeof(Client)); - Container *b = malloc(sizeof(Container)); - for (a = container -> client; a != NULL; a = a -> next) { children++; } - for (b = container -> child; b != NULL; b = b -> next) { children++; } - - - /* Recursive call to placeContainer */ - int i = 0; - for (b = container -> child; b != NULL; b = b -> next, i++) { - switch (container -> layout) { - case 0: - placeContainer(b, - x + (i * (width/children)), y, - (width / children), height); - break; - - case 1: - placeContainer(b, - x, y + (i * (height/children)), - width, (height / children)); - break; - - default: - break; - } - } - - - for (a = container -> client; a != NULL; a = a -> next, i++) { - XMapWindow(display, a -> window); - XSetWindowBorderWidth(display, a -> window, 1); - XSetWindowBorder(display, a -> window, unfocusedColor); - - switch (container -> layout) { - case 0: - XMoveResizeWindow(display, a -> window, - (x + (i * (width / children))) + padding, - y + padding, - (width / children) + padding, - height + padding); - break; - case 1: - XMoveResizeWindow(display, a -> window, - x + padding, - (y + (i * (height / children))) + padding, - width + padding, - (height / children) + padding); - break; - default: - break; - } - - } - - free(a), free(b); - return 0; -} - -//Returns the client associated with given window -Client * getClientByWindow(Window * window) { - Lookup *node; - int win = *window; - for (node = lookup; node != NULL; node = node -> previous) - if (win == node -> window) - return node -> client; - - free(node); -} - - -//Thank you DWM ;) -unsigned long getColor(const char *colstr) { - Colormap cmap = DefaultColormap(display, activeScreen); - XColor color; - - if(!XAllocNamedColor(display, cmap, colstr, &color, &color)) { return 0; } - return color.pixel; -} - - -void centerPointer(Window *window) { - //Get Window Attributes - XWindowAttributes windowAttributes; - XGetWindowAttributes(display, *window, &windowAttributes); - - int centerX = windowAttributes.width / 2, - centerY = windowAttributes.height / 2; - - //Warp to Center - XWarpPointer(display, None, *window, 0, 0, 0, 0, centerX,centerY); -} - - -void focusWindow(Window * window) { - - XSetWindowBorderWidth(display, *window, 2); - XSetWindowBorder(display, *window, focusedColor); - - //Focuses window - XSelectInput( - display, *window, - FocusChangeMask | KeyPressMask | ButtonPressMask | LeaveWindowMask | OwnerGrabButtonMask - ); - XGrabButton( - display, - AnyButton, - AnyModifier, - *window, - False, - OwnerGrabButtonMask | ButtonPressMask, - GrabModeSync, - GrabModeSync, - None, - None); - -} - - -void handleXEvent(XEvent *event) { - switch (event -> type) { - case MapRequest: eMapRequest(event); break; - case ButtonPress: eButtonPress(event); break; - default: break; - } -} - +#include "tree.h" +#include "commands.h" +#include "util.h" +#include "window.h" void handleEvents() { int fifoFd, xFd; //File Descriptors for FIFO and X @@ -284,16 +53,6 @@ void handleEvents() { close(fifoFd); } } - -int xError(XErrorEvent *e) { - char err[500]; - - XGetErrorText(display, e -> request_code, err, 500); - fprintf(stderr, "XErrorEvent of Request Code: %d and Error Code of %d\n", e -> request_code, e -> error_code); - fprintf(stderr, "%s\n", err); - return 0; -} - int main() { diff --git a/src/fifo-wm.h b/src/fifo-wm.h @@ -14,16 +14,5 @@ struct timeval tv; long unfocusedColor; long focusedColor; -void crawlContainer(Container * container, int level); -void dumpTree(); void handleCommand(char* request); -int parentClient(Client * child, Container * parent); -int parentContainer(Container * child, Container * parent); -int placeContainer(Container * container, int x, int y, int width, int height); -Client * getClientByWindow(Window * window); -unsigned long getColor(const char *colstr); -void centerPointer(Window *window); -void focusWindow(Window * window); -void handleXEvent(XEvent *event); void handleEvents(); -int xError(XErrorEvent *e); diff --git a/src/tree.c b/src/tree.c @@ -0,0 +1,144 @@ +#include <X11/Xlib.h> +#include <stdio.h> +#include <stdlib.h> + +#include "fifo-wm.h" +#include "tree.h" + +/* Tracks the current container */ +void crawlContainer(Container * container, int level) { + Container *c; + for (c = container; c != NULL; c = c -> next) { + int j; + for (j = level; j > 0; j--) { fprintf(stderr, "\t"); } + char *or = c -> layout == 0 ? "Vertical" : "Horizontal"; + fprintf(stderr, "[%d]=> Container (%s)\n", level, or); + if (c -> client != NULL) { + Client *d; + for (d = c -> client; d != NULL; d = d -> next) { + int h; + for (h = level + 1; h > 0; h--) { fprintf(stderr, "\t"); } + fprintf(stderr, "Client\n"); + } + } + if (c -> child != NULL) { + crawlContainer(c -> child, level + 1); + } + } +} + + +void dumpTree() { + fprintf(stderr, "Printing the tree\n"); + crawlContainer(currentContainer , 0); +} + + +int parentClient(Client * child, Container * parent) { + /* First client to be added to container */ + if (parent == NULL) { return 0; } + + child -> parent = parent; + + if (parent -> client == NULL) { + fprintf(stderr, "Addng client\n"); + parent -> client = child; + + } else { + Client *c = parent -> client; + while (c -> next != NULL) { c = c -> next; } + c -> next = child; + child -> previous = c; + child -> parent = parent; + } +} + +int parentContainer(Container * child, Container * parent) { + if (parent == NULL) { return 0; } + + child -> parent = parent; + + if (parent -> child == NULL) { + parent -> child = child; + + } else { + Container *c = parent -> child; + while (c -> next != NULL) { c = c -> next; } + c -> next = child; + child -> previous = c; + child -> parent = parent; + } +} + +int placeContainer(Container * container, int x, int y, int width, int height) { + + //Count up children + int children = 0; + Client *a = malloc(sizeof(Client)); + Container *b = malloc(sizeof(Container)); + for (a = container -> client; a != NULL; a = a -> next) { children++; } + for (b = container -> child; b != NULL; b = b -> next) { children++; } + + + /* Recursive call to placeContainer */ + int i = 0; + for (b = container -> child; b != NULL; b = b -> next, i++) { + switch (container -> layout) { + case 0: + placeContainer(b, + x + (i * (width/children)), y, + (width / children), height); + break; + + case 1: + placeContainer(b, + x, y + (i * (height/children)), + width, (height / children)); + break; + + default: + break; + } + } + + + for (a = container -> client; a != NULL; a = a -> next, i++) { + XMapWindow(display, a -> window); + XSetWindowBorderWidth(display, a -> window, 1); + XSetWindowBorder(display, a -> window, unfocusedColor); + + switch (container -> layout) { + case 0: + XMoveResizeWindow(display, a -> window, + (x + (i * (width / children))) + padding, + y + padding, + (width / children) + padding, + height + padding); + break; + case 1: + XMoveResizeWindow(display, a -> window, + x + padding, + (y + (i * (height / children))) + padding, + width + padding, + (height / children) + padding); + break; + default: + break; + } + + } + + free(a), free(b); + return 0; +} + +//Returns the client associated with given window +Client * getClientByWindow(Window * window) { + Lookup *node; + int win = *window; + for (node = lookup; node != NULL; node = node -> previous) + if (win == node -> window) + return node -> client; + + free(node); +} diff --git a/src/tree.h b/src/tree.h @@ -0,0 +1,6 @@ +void crawlContainer(Container * container, int level); +void dumpTree(); +int parentClient(Client * child, Container * parent); +int parentContainer(Container * child, Container * parent); +int placeContainer(Container * container, int x, int y, int width, int height); +Client * getClientByWindow(Window * window); diff --git a/src/util.c b/src/util.c @@ -0,0 +1,24 @@ +#include <X11/Xlib.h> +#include <stdio.h> + +#include "fifo-wm.h" +#include "util.h" + +//Thank you DWM ;) +unsigned long getColor(const char *colstr) { + Colormap cmap = DefaultColormap(display, activeScreen); + XColor color; + + if(!XAllocNamedColor(display, cmap, colstr, &color, &color)) { return 0; } + return color.pixel; +} + + +int xError(XErrorEvent *e) { + char err[500]; + + XGetErrorText(display, e -> request_code, err, 500); + fprintf(stderr, "XErrorEvent of Request Code: %d and Error Code of %d\n", e -> request_code, e -> error_code); + fprintf(stderr, "%s\n", err); + return 0; +} diff --git a/src/util.h b/src/util.h @@ -0,0 +1,2 @@ +unsigned long getColor(const char *colstr); +int xError(XErrorEvent *e); diff --git a/src/window.c b/src/window.c @@ -0,0 +1,39 @@ +#include <X11/Xlib.h> + +#include "fifo-wm.h" +#include "window.h" + +void centerPointer(Window *window) { + //Get Window Attributes + XWindowAttributes windowAttributes; + XGetWindowAttributes(display, *window, &windowAttributes); + + int centerX = windowAttributes.width / 2, + centerY = windowAttributes.height / 2; + + //Warp to Center + XWarpPointer(display, None, *window, 0, 0, 0, 0, centerX,centerY); +} + + +void focusWindow(Window * window) { +XSetWindowBorderWidth(display, *window, 2); + XSetWindowBorder(display, *window, focusedColor); + + //Focuses window + XSelectInput( + display, *window, + FocusChangeMask | KeyPressMask | ButtonPressMask | LeaveWindowMask | OwnerGrabButtonMask + ); + XGrabButton( + display, + AnyButton, + AnyModifier, + *window, + False, + OwnerGrabButtonMask | ButtonPressMask, + GrabModeSync, + GrabModeSync, + None, + None); +} diff --git a/src/window.h b/src/window.h @@ -0,0 +1,2 @@ +void centerPointer(Window *window); +void focusWindow(Window * window);