diff
wm.c | 186 +++++++++++++++++++++++++++++++++---------------------------------
wmc.c | 54 +++++++++----------
2 files changed, 120 insertions(+), 120 deletions(-)
diff --git a/wm.c b/wm.c
index 1067534..1348f96 100644
--- a/wm.c
+++ b/wm.c
@@ -1,72 +1,72 @@
#include <X11/Xlib.h>
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-#define MODKEY Mod1Mask
-#define MAXWINS 4096
-#define CMD_WS 1
-#define CMD_MOVE 2
+#define max_val(a, b) ((a) > (b) ? (a) : (b))
+#define mod_key Mod1Mask
+#define max_windows 4096
+#define cmd_workspace 1
+#define cmd_move 2
-static Window wins[MAXWINS];
-static int ws[MAXWINS], nwin, curws;
+static Window windows[max_windows];
+static int window_workspace[max_windows], window_count, current_workspace;
static int
-getidx(Window w)
+get_window_index(Window window)
{
int i;
- for (i = 0; i < nwin; i++)
- if (wins[i] == w)
+ for (i = 0; i < window_count; i++)
+ if (windows[i] == window)
return i;
- if (w == None || nwin >= MAXWINS)
+ if (window == None || window_count >= max_windows)
return -1;
- wins[nwin] = w;
- ws[nwin] = curws;
- return nwin++;
+ windows[window_count] = window;
+ window_workspace[window_count] = current_workspace;
+ return window_count++;
}
static void
-trackwins(Display *dpy, Window root)
+track_windows(Display *display, Window root_window)
{
- Window rootret, parent, *children;
+ Window root_return, parent, *children;
int i, j;
- if (XQueryTree(dpy, root, &rootret, &parent, &children, (unsigned int *)&j)) {
+ if (XQueryTree(display, root_window, &root_return, &parent, &children, (unsigned int *)&j)) {
for (i = 0; i < j; i++)
- getidx(children[i]);
+ get_window_index(children[i]);
if (children != None)
XFree(children);
}
}
static int
-isrootchild(Display *dpy, Window root, Window w)
+is_root_child(Display *display, Window root_window, Window window)
{
- Window rootret, parent, *children;
- unsigned int nchildren;
+ Window root_return, parent, *children;
+ unsigned int child_count;
- if (w == None || w == PointerRoot || w == root)
+ if (window == None || window == PointerRoot || window == root_window)
return 0;
- if (!XQueryTree(dpy, w, &rootret, &parent, &children, &nchildren))
+ if (!XQueryTree(display, window, &root_return, &parent, &children, &child_count))
return 0;
if (children != None)
XFree(children);
- return parent == root;
+ return parent == root_window;
}
static void
-showws(Display *dpy, Window root)
+show_workspace(Display *display, Window root_window)
{
- Window rootret, parent, *children;
- int i, j, idx;
+ Window root_return, parent, *children;
+ int i, j, window_index;
- if (XQueryTree(dpy, root, &rootret, &parent, &children, (unsigned int *)&j)) {
+ if (XQueryTree(display, root_window, &root_return, &parent, &children, (unsigned int *)&j)) {
for (i = 0; i < j; i++) {
- idx = getidx(children[i]);
- if (idx >= 0) {
- if (ws[idx] == curws)
- XMapWindow(dpy, children[i]);
+ window_index = get_window_index(children[i]);
+ if (window_index >= 0) {
+ if (window_workspace[window_index] == current_workspace)
+ XMapWindow(display, children[i]);
else
- XUnmapWindow(dpy, children[i]);
+ XUnmapWindow(display, children[i]);
}
}
if (children != None)
@@ -77,87 +77,87 @@ showws(Display *dpy, Window root)
int
main(void)
{
- Display *dpy;
- Window root;
- Window dragwin;
- Window focus;
- XWindowAttributes attr;
- XButtonEvent start;
- XEvent ev;
- Atom ctl;
- int dragging;
+ Display *display;
+ Window root_window;
+ Window drag_window;
+ Window focused_window;
+ XWindowAttributes window_attrs;
+ XButtonEvent drag_start;
+ XEvent event;
+ Atom control_atom;
+ int is_dragging;
int i, j;
- dragging = 0;
- dragwin = None;
- if ((dpy = XOpenDisplay(NULL)) == NULL)
+ is_dragging = 0;
+ drag_window = None;
+ if ((display = XOpenDisplay(NULL)) == NULL)
return 1;
- root = DefaultRootWindow(dpy);
- ctl = XInternAtom(dpy, "_WM_CTL", False);
- XSelectInput(dpy, root, SubstructureNotifyMask);
- trackwins(dpy, root);
+ root_window = DefaultRootWindow(display);
+ control_atom = XInternAtom(display, "_WM_CTL", False);
+ XSelectInput(display, root_window, SubstructureNotifyMask);
+ track_windows(display, root_window);
- XGrabButton(dpy, 1, MODKEY, root, True, ButtonPressMask, GrabModeAsync,
+ XGrabButton(display, 1, mod_key, root_window, True, ButtonPressMask, GrabModeAsync,
GrabModeAsync, None, None);
- XGrabButton(dpy, 3, MODKEY, root, True, ButtonPressMask, GrabModeAsync,
+ XGrabButton(display, 3, mod_key, root_window, True, ButtonPressMask, GrabModeAsync,
GrabModeAsync, None, None);
for (;;) {
- XNextEvent(dpy, &ev);
- if (ev.type == ClientMessage && ev.xclient.message_type == ctl) {
- i = (int)ev.xclient.data.l[1];
+ XNextEvent(display, &event);
+ if (event.type == ClientMessage && event.xclient.message_type == control_atom) {
+ i = (int)event.xclient.data.l[1];
if (i >= 0 && i < 9) {
- if (ev.xclient.data.l[0] == CMD_WS && i != curws) {
- trackwins(dpy, root);
- curws = i;
- showws(dpy, root);
- } else if (ev.xclient.data.l[0] == CMD_MOVE) {
- XGetInputFocus(dpy, &focus, &j);
- if (!isrootchild(dpy, root, focus))
+ if (event.xclient.data.l[0] == cmd_workspace && i != current_workspace) {
+ track_windows(display, root_window);
+ current_workspace = i;
+ show_workspace(display, root_window);
+ } else if (event.xclient.data.l[0] == cmd_move) {
+ XGetInputFocus(display, &focused_window, &j);
+ if (!is_root_child(display, root_window, focused_window))
continue;
- j = getidx(focus);
+ j = get_window_index(focused_window);
if (j >= 0) {
- ws[j] = i;
- if (i != curws)
- XUnmapWindow(dpy, focus);
+ window_workspace[j] = i;
+ if (i != current_workspace)
+ XUnmapWindow(display, focused_window);
}
}
}
- } else if (ev.type == ButtonPress && ev.xbutton.subwindow != None) {
- dragwin = ev.xbutton.subwindow;
- XSetInputFocus(dpy, dragwin, RevertToPointerRoot, CurrentTime);
- j = getidx(dragwin);
+ } else if (event.type == ButtonPress && event.xbutton.subwindow != None) {
+ drag_window = event.xbutton.subwindow;
+ XSetInputFocus(display, drag_window, RevertToPointerRoot, CurrentTime);
+ j = get_window_index(drag_window);
if (j >= 0)
- ws[j] = curws;
- XGrabPointer(dpy, dragwin, True,
+ window_workspace[j] = current_workspace;
+ XGrabPointer(display, drag_window, True,
PointerMotionMask | ButtonReleaseMask,
GrabModeAsync, GrabModeAsync, None, None, CurrentTime);
- XGetWindowAttributes(dpy, dragwin, &attr);
- start = ev.xbutton;
- if (start.button == 3) {
- start.x_root = attr.x + attr.width - 1;
- start.y_root = attr.y + attr.height - 1;
- XWarpPointer(dpy, None, root, 0, 0, 0, 0,
- start.x_root, start.y_root);
+ XGetWindowAttributes(display, drag_window, &window_attrs);
+ drag_start = event.xbutton;
+ if (drag_start.button == 3) {
+ drag_start.x_root = window_attrs.x + window_attrs.width - 1;
+ drag_start.y_root = window_attrs.y + window_attrs.height - 1;
+ XWarpPointer(display, None, root_window, 0, 0, 0, 0,
+ drag_start.x_root, drag_start.y_root);
}
- dragging = 1;
- } else if (ev.type == MotionNotify && dragging) {
- int xdiff, ydiff;
+ is_dragging = 1;
+ } else if (event.type == MotionNotify && is_dragging) {
+ int x_diff, y_diff;
- while (XCheckTypedEvent(dpy, MotionNotify, &ev))
+ while (XCheckTypedEvent(display, MotionNotify, &event))
continue;
- xdiff = ev.xmotion.x_root - start.x_root;
- ydiff = ev.xmotion.y_root - start.y_root;
- XMoveResizeWindow(dpy, dragwin,
- attr.x + (start.button == 1 ? xdiff : 0),
- attr.y + (start.button == 1 ? ydiff : 0),
- MAX(1, attr.width + (start.button == 3 ? xdiff : 0)),
- MAX(1, attr.height + (start.button == 3 ? ydiff : 0)));
- } else if (ev.type == ButtonRelease) {
- dragging = 0;
- dragwin = None;
- XUngrabPointer(dpy, CurrentTime);
+ x_diff = event.xmotion.x_root - drag_start.x_root;
+ y_diff = event.xmotion.y_root - drag_start.y_root;
+ XMoveResizeWindow(display, drag_window,
+ window_attrs.x + (drag_start.button == 1 ? x_diff : 0),
+ window_attrs.y + (drag_start.button == 1 ? y_diff : 0),
+ max_val(1, window_attrs.width + (drag_start.button == 3 ? x_diff : 0)),
+ max_val(1, window_attrs.height + (drag_start.button == 3 ? y_diff : 0)));
+ } else if (event.type == ButtonRelease) {
+ is_dragging = 0;
+ drag_window = None;
+ XUngrabPointer(display, CurrentTime);
}
}
}
diff --git a/wmc.c b/wmc.c
index cd1d860..d30bb31 100644
--- a/wmc.c
+++ b/wmc.c
@@ -4,46 +4,46 @@
#include <stdlib.h>
#include <string.h>
-#define CMD_WS 1
-#define CMD_MOVE 2
+#define cmd_workspace 1
+#define cmd_move 2
int
main(int argc, char **argv)
{
- Display *dpy;
- Window root;
- XEvent ev;
- Atom ctl;
- int cmd, ws;
- char *end;
+ Display *display;
+ Window root_window;
+ XEvent event;
+ Atom control_atom;
+ int command, workspace;
+ char *end_ptr;
- cmd = CMD_WS;
+ command = cmd_workspace;
if (argc == 2)
- ws = (int)strtol(argv[1], &end, 10);
+ workspace = (int)strtol(argv[1], &end_ptr, 10);
else if (argc == 3 && !strcmp(argv[1], "move")) {
- cmd = CMD_MOVE;
- ws = (int)strtol(argv[2], &end, 10);
+ command = cmd_move;
+ workspace = (int)strtol(argv[2], &end_ptr, 10);
} else if (argc == 3 && !strcmp(argv[1], "ws"))
- ws = (int)strtol(argv[2], &end, 10);
+ workspace = (int)strtol(argv[2], &end_ptr, 10);
else {
fprintf(stderr, "usage: wmc [ws] 1-9 | wmc move 1-9\n");
return 1;
}
- if (*end != '\0' || ws < 1 || ws > 9)
+ if (*end_ptr != '\0' || workspace < 1 || workspace > 9)
return 1;
- if ((dpy = XOpenDisplay(NULL)) == NULL)
+ if ((display = XOpenDisplay(NULL)) == NULL)
return 1;
- root = DefaultRootWindow(dpy);
- ctl = XInternAtom(dpy, "_WM_CTL", False);
- memset(&ev, 0, sizeof(ev));
- ev.xclient.type = ClientMessage;
- ev.xclient.window = root;
- ev.xclient.message_type = ctl;
- ev.xclient.format = 32;
- ev.xclient.data.l[0] = cmd;
- ev.xclient.data.l[1] = ws - 1;
- XSendEvent(dpy, root, False, SubstructureNotifyMask, &ev);
- XFlush(dpy);
- XCloseDisplay(dpy);
+ root_window = DefaultRootWindow(display);
+ control_atom = XInternAtom(display, "_WM_CTL", False);
+ memset(&event, 0, sizeof(event));
+ event.xclient.type = ClientMessage;
+ event.xclient.window = root_window;
+ event.xclient.message_type = control_atom;
+ event.xclient.format = 32;
+ event.xclient.data.l[0] = command;
+ event.xclient.data.l[1] = workspace - 1;
+ XSendEvent(display, root_window, False, SubstructureNotifyMask, &event);
+ XFlush(display);
+ XCloseDisplay(display);
return 0;
}