2019-10-14 08:31:14 +00:00
|
|
|
// sowm - An itsy bitsy floating window manager.
|
2019-10-11 11:48:34 +00:00
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
2019-10-11 17:56:54 +00:00
|
|
|
#include <X11/XF86keysym.h>
|
2019-10-11 11:48:34 +00:00
|
|
|
#include <X11/keysym.h>
|
2019-10-24 20:33:41 +00:00
|
|
|
#include <X11/XKBlib.h>
|
2019-10-11 11:48:34 +00:00
|
|
|
#include <stdlib.h>
|
2019-10-11 19:06:01 +00:00
|
|
|
#include <signal.h>
|
2019-10-11 17:56:54 +00:00
|
|
|
#include <unistd.h>
|
2019-10-11 11:48:34 +00:00
|
|
|
|
2020-01-23 00:54:13 +00:00
|
|
|
#include "sowm.h"
|
2019-10-11 17:56:54 +00:00
|
|
|
|
2019-10-18 21:30:59 +00:00
|
|
|
static client *list = {0}, *ws_list[10] = {0}, *cur;
|
2020-01-19 23:45:57 +00:00
|
|
|
static int ws = 1, sw, sh, wx, wy, numlock = 0;
|
2019-10-16 14:25:40 +00:00
|
|
|
static unsigned int ww, wh;
|
2019-10-11 17:56:54 +00:00
|
|
|
|
2019-10-17 14:02:42 +00:00
|
|
|
static Display *d;
|
|
|
|
static XButtonEvent mouse;
|
2019-10-11 17:56:54 +00:00
|
|
|
|
2019-10-11 11:48:34 +00:00
|
|
|
static void (*events[LASTEvent])(XEvent *e) = {
|
2019-10-11 17:56:54 +00:00
|
|
|
[ButtonPress] = button_press,
|
|
|
|
[ButtonRelease] = button_release,
|
2019-10-11 18:29:41 +00:00
|
|
|
[ConfigureRequest] = configure_request,
|
2019-10-11 17:56:54 +00:00
|
|
|
[KeyPress] = key_press,
|
|
|
|
[MapRequest] = map_request,
|
2019-10-12 17:02:35 +00:00
|
|
|
[DestroyNotify] = notify_destroy,
|
|
|
|
[EnterNotify] = notify_enter,
|
|
|
|
[MotionNotify] = notify_motion
|
2019-10-11 11:48:34 +00:00
|
|
|
};
|
|
|
|
|
2019-10-16 10:21:36 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2019-10-18 21:54:06 +00:00
|
|
|
void win_focus(client *c) {
|
|
|
|
cur = c;
|
|
|
|
XSetInputFocus(d, cur->w, RevertToParent, CurrentTime);
|
2019-10-14 06:06:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 18:34:43 +00:00
|
|
|
void notify_destroy(XEvent *e) {
|
2019-10-18 17:12:59 +00:00
|
|
|
win_del(e->xdestroywindow.window);
|
|
|
|
|
2019-10-18 21:54:06 +00:00
|
|
|
if (list) win_focus(list->prev);
|
2019-10-12 18:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void notify_enter(XEvent *e) {
|
2019-10-14 17:53:00 +00:00
|
|
|
while(XCheckTypedEvent(d, EnterNotify, e));
|
|
|
|
|
2019-10-18 21:54:06 +00:00
|
|
|
for win if (c->w == e->xcrossing.window) win_focus(c);
|
2019-10-12 18:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void notify_motion(XEvent *e) {
|
2019-11-25 14:30:34 +00:00
|
|
|
if (!mouse.subwindow || cur->f) return;
|
2019-10-19 14:23:26 +00:00
|
|
|
|
|
|
|
while(XCheckTypedEvent(d, MotionNotify, e));
|
2019-10-12 18:34:43 +00:00
|
|
|
|
2019-10-15 20:44:04 +00:00
|
|
|
int xd = e->xbutton.x_root - mouse.x_root;
|
|
|
|
int yd = e->xbutton.y_root - mouse.y_root;
|
2019-10-13 18:59:52 +00:00
|
|
|
|
2019-10-15 20:44:04 +00:00
|
|
|
XMoveResizeWindow(d, mouse.subwindow,
|
2019-10-16 14:25:40 +00:00
|
|
|
wx + (mouse.button == 1 ? xd : 0),
|
|
|
|
wy + (mouse.button == 1 ? yd : 0),
|
|
|
|
ww + (mouse.button == 3 ? xd : 0),
|
|
|
|
wh + (mouse.button == 3 ? yd : 0));
|
2019-10-12 18:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void key_press(XEvent *e) {
|
2019-10-24 20:33:41 +00:00
|
|
|
KeySym keysym = XkbKeycodeToKeysym(d, e->xkey.keycode, 0, 0);
|
2019-10-12 18:34:43 +00:00
|
|
|
|
2020-01-14 21:16:06 +00:00
|
|
|
for (unsigned int i=0; i < sizeof(keys)/sizeof(*keys); ++i)
|
2020-01-19 23:45:57 +00:00
|
|
|
if (keys[i].keysym == keysym &&
|
|
|
|
mod_clean(keys[i].mod) == mod_clean(e->xkey.state))
|
2019-10-12 18:34:43 +00:00
|
|
|
keys[i].function(keys[i].arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void button_press(XEvent *e) {
|
2019-10-19 14:23:26 +00:00
|
|
|
if (!e->xbutton.subwindow) return;
|
2019-10-12 18:34:43 +00:00
|
|
|
|
2019-10-16 14:25:40 +00:00
|
|
|
win_size(e->xbutton.subwindow, &wx, &wy, &ww, &wh);
|
2019-10-14 05:18:08 +00:00
|
|
|
XRaiseWindow(d, e->xbutton.subwindow);
|
2019-10-14 05:41:51 +00:00
|
|
|
mouse = e->xbutton;
|
2019-10-12 18:34:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 15:00:26 +00:00
|
|
|
void button_release(XEvent *e) {
|
2019-11-25 14:30:34 +00:00
|
|
|
mouse.subwindow = 0;
|
2019-10-12 18:34:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:33:03 +00:00
|
|
|
void win_add(Window w) {
|
2019-10-18 21:16:19 +00:00
|
|
|
client *c;
|
2019-10-11 11:48:34 +00:00
|
|
|
|
2019-10-18 21:16:19 +00:00
|
|
|
if (!(c = (client *) calloc(1, sizeof(client))))
|
2019-10-11 15:52:07 +00:00
|
|
|
exit(1);
|
2019-10-11 11:48:34 +00:00
|
|
|
|
2019-10-18 21:16:19 +00:00
|
|
|
c->w = w;
|
2019-10-18 10:45:02 +00:00
|
|
|
|
2019-10-18 21:16:19 +00:00
|
|
|
if (list) {
|
|
|
|
list->prev->next = c;
|
|
|
|
c->prev = list->prev;
|
|
|
|
list->prev = c;
|
|
|
|
c->next = list;
|
2019-10-18 17:12:59 +00:00
|
|
|
|
2019-10-18 21:16:19 +00:00
|
|
|
} else {
|
|
|
|
list = c;
|
|
|
|
list->prev = list->next = list;
|
2019-10-18 14:40:04 +00:00
|
|
|
}
|
2019-10-18 16:55:26 +00:00
|
|
|
|
|
|
|
ws_save(ws);
|
2019-10-11 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 17:12:59 +00:00
|
|
|
void win_del(Window w) {
|
2019-10-18 21:16:19 +00:00
|
|
|
client *x = 0;
|
|
|
|
|
|
|
|
for win if (c->w == w) x = c;
|
|
|
|
|
|
|
|
if (!list || !x) return;
|
|
|
|
if (x->prev == x) list = 0;
|
|
|
|
if (list == x) list = x->next;
|
|
|
|
if (x->next) x->next->prev = x->prev;
|
|
|
|
if (x->prev) x->prev->next = x->next;
|
|
|
|
|
|
|
|
free(x);
|
|
|
|
ws_save(ws);
|
2019-10-12 13:41:35 +00:00
|
|
|
}
|
2019-10-11 15:00:00 +00:00
|
|
|
|
2020-01-23 15:00:26 +00:00
|
|
|
void win_kill(const Arg arg) {
|
2019-10-18 21:30:59 +00:00
|
|
|
if (cur) XKillClient(d, cur->w);
|
2019-10-11 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 15:00:26 +00:00
|
|
|
void win_center(const Arg arg) {
|
2019-10-19 05:14:03 +00:00
|
|
|
if (!cur) return;
|
2019-10-18 17:12:59 +00:00
|
|
|
|
2019-10-19 05:14:03 +00:00
|
|
|
win_size(cur->w, &(int){0}, &(int){0}, &ww, &wh);
|
|
|
|
XMoveWindow(d, cur->w, (sw - ww) / 2, (sh - wh) / 2);
|
2019-10-11 17:06:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 15:00:26 +00:00
|
|
|
void win_fs(const Arg arg) {
|
2019-10-18 21:30:59 +00:00
|
|
|
if (!cur) return;
|
2019-10-18 08:42:57 +00:00
|
|
|
|
2019-10-19 14:23:26 +00:00
|
|
|
if ((cur->f = cur->f ? 0 : 1)) {
|
2019-10-18 21:30:59 +00:00
|
|
|
win_size(cur->w, &cur->wx, &cur->wy, &cur->ww, &cur->wh);
|
|
|
|
XMoveResizeWindow(d, cur->w, 0, 0, sw, sh);
|
2019-10-15 11:49:12 +00:00
|
|
|
|
2019-10-18 21:30:59 +00:00
|
|
|
} else
|
|
|
|
XMoveResizeWindow(d, cur->w, cur->wx, cur->wy, cur->ww, cur->wh);
|
2019-10-11 17:22:10 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:33:03 +00:00
|
|
|
void win_to_ws(const Arg arg) {
|
2019-10-14 05:41:51 +00:00
|
|
|
int tmp = ws;
|
2019-10-11 11:53:52 +00:00
|
|
|
|
2019-10-12 19:12:20 +00:00
|
|
|
if (arg.i == tmp) return;
|
2019-10-11 11:48:34 +00:00
|
|
|
|
2019-10-11 17:56:54 +00:00
|
|
|
ws_sel(arg.i);
|
2019-10-18 21:30:59 +00:00
|
|
|
win_add(cur->w);
|
2019-10-11 17:56:54 +00:00
|
|
|
ws_save(arg.i);
|
2019-10-11 11:48:34 +00:00
|
|
|
|
2019-10-12 13:41:35 +00:00
|
|
|
ws_sel(tmp);
|
2019-10-18 21:30:59 +00:00
|
|
|
win_del(cur->w);
|
|
|
|
XUnmapWindow(d, cur->w);
|
2019-10-12 15:02:09 +00:00
|
|
|
ws_save(tmp);
|
2019-10-13 14:45:44 +00:00
|
|
|
|
2019-10-18 21:54:06 +00:00
|
|
|
if (list) win_focus(list);
|
2019-10-11 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 15:00:26 +00:00
|
|
|
void win_prev(const Arg arg) {
|
2019-12-15 23:53:52 +00:00
|
|
|
if (!cur) return;
|
2019-12-18 11:41:21 +00:00
|
|
|
|
2019-12-15 23:53:52 +00:00
|
|
|
XRaiseWindow(d, cur->prev->w);
|
|
|
|
win_focus(cur->prev);
|
|
|
|
}
|
|
|
|
|
2020-01-23 15:00:26 +00:00
|
|
|
void win_next(const Arg arg) {
|
2019-10-18 21:30:59 +00:00
|
|
|
if (!cur) return;
|
2019-10-18 17:12:59 +00:00
|
|
|
|
2019-10-18 21:30:59 +00:00
|
|
|
XRaiseWindow(d, cur->next->w);
|
2019-10-18 21:54:06 +00:00
|
|
|
win_focus(cur->next);
|
2019-10-11 20:40:30 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 18:34:43 +00:00
|
|
|
void ws_go(const Arg arg) {
|
2019-10-14 05:41:51 +00:00
|
|
|
int tmp = ws;
|
2019-10-11 11:48:34 +00:00
|
|
|
|
2019-10-14 05:41:51 +00:00
|
|
|
if (arg.i == ws) return;
|
2019-10-11 11:48:34 +00:00
|
|
|
|
2019-10-14 05:41:51 +00:00
|
|
|
ws_save(ws);
|
2019-10-12 18:34:43 +00:00
|
|
|
ws_sel(arg.i);
|
2019-10-11 15:00:00 +00:00
|
|
|
|
2019-10-19 10:24:17 +00:00
|
|
|
for win XMapWindow(d, c->w);
|
2019-10-11 15:00:00 +00:00
|
|
|
|
2019-10-12 18:34:43 +00:00
|
|
|
ws_sel(tmp);
|
2019-10-11 15:00:00 +00:00
|
|
|
|
2019-10-19 10:24:17 +00:00
|
|
|
for win XUnmapWindow(d, c->w);
|
2019-10-11 15:00:00 +00:00
|
|
|
|
2019-10-12 18:34:43 +00:00
|
|
|
ws_sel(arg.i);
|
2019-10-13 14:30:09 +00:00
|
|
|
|
2019-10-20 20:34:29 +00:00
|
|
|
if (list) win_focus(list); else cur = 0;
|
2019-10-12 18:34:43 +00:00
|
|
|
}
|
2019-10-12 13:41:35 +00:00
|
|
|
|
2019-10-12 18:34:43 +00:00
|
|
|
void configure_request(XEvent *e) {
|
|
|
|
XConfigureRequestEvent *ev = &e->xconfigurerequest;
|
|
|
|
|
2019-10-16 12:46:09 +00:00
|
|
|
XConfigureWindow(d, ev->window, ev->value_mask, &(XWindowChanges) {
|
|
|
|
.x = ev->x,
|
|
|
|
.y = ev->y,
|
|
|
|
.width = ev->width,
|
|
|
|
.height = ev->height,
|
|
|
|
.sibling = ev->above,
|
|
|
|
.stack_mode = ev->detail
|
|
|
|
});
|
2019-10-11 11:48:34 +00:00
|
|
|
}
|
2019-10-11 11:53:52 +00:00
|
|
|
|
2019-10-11 17:56:54 +00:00
|
|
|
void map_request(XEvent *e) {
|
2019-10-13 15:37:44 +00:00
|
|
|
Window w = e->xmaprequest.window;
|
|
|
|
|
2019-10-17 06:40:24 +00:00
|
|
|
XSelectInput(d, w, StructureNotifyMask|EnterWindowMask);
|
2019-10-17 08:26:31 +00:00
|
|
|
win_size(w, &wx, &wy, &ww, &wh);
|
2019-10-18 17:12:59 +00:00
|
|
|
win_add(w);
|
2019-10-19 07:27:18 +00:00
|
|
|
cur = list->prev;
|
2019-10-18 21:54:06 +00:00
|
|
|
|
2020-01-23 15:00:26 +00:00
|
|
|
if (wx + wy == 0) win_center((Arg){0});
|
2019-10-18 21:54:06 +00:00
|
|
|
|
2019-10-18 21:30:59 +00:00
|
|
|
XMapWindow(d, w);
|
2019-10-19 07:27:18 +00:00
|
|
|
win_focus(list->prev);
|
2019-10-11 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 18:34:43 +00:00
|
|
|
void run(const Arg arg) {
|
|
|
|
if (fork()) return;
|
2019-10-14 05:18:08 +00:00
|
|
|
if (d) close(ConnectionNumber(d));
|
2019-10-11 11:48:34 +00:00
|
|
|
|
2019-10-12 18:34:43 +00:00
|
|
|
setsid();
|
|
|
|
execvp((char*)arg.com[0], (char**)arg.com);
|
2019-10-11 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 23:45:57 +00:00
|
|
|
void input_grab(Window root) {
|
|
|
|
unsigned int i, j, modifiers[] = {0, LockMask, numlock, numlock|LockMask};
|
|
|
|
XModifierKeymap *modmap = XGetModifierMapping(d);
|
|
|
|
KeyCode code;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
2020-01-23 00:54:13 +00:00
|
|
|
for (int k = 0; k < modmap->max_keypermod; k++)
|
|
|
|
if (modmap->modifiermap[i * modmap->max_keypermod + k]
|
2020-01-19 23:45:57 +00:00
|
|
|
== XKeysymToKeycode(d, 0xff7f))
|
|
|
|
numlock = (1 << i);
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(keys)/sizeof(*keys); i++)
|
|
|
|
if ((code = XKeysymToKeycode(d, keys[i].keysym)))
|
|
|
|
for (j = 0; j < sizeof(modifiers)/sizeof(*modifiers); j++)
|
|
|
|
XGrabKey(d, code, keys[i].mod | modifiers[j], root,
|
|
|
|
True, GrabModeAsync, GrabModeAsync);
|
|
|
|
|
|
|
|
for (i = 1; i < 4; i += 2)
|
|
|
|
for (j = 0; j < sizeof(modifiers)/sizeof(*modifiers); j++)
|
|
|
|
XGrabButton(d, i, MOD | modifiers[j], root, True,
|
|
|
|
ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
|
|
|
|
GrabModeAsync, GrabModeAsync, 0, 0);
|
|
|
|
|
|
|
|
XFreeModifiermap(modmap);
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:03:24 +00:00
|
|
|
int main(void) {
|
|
|
|
XEvent ev;
|
|
|
|
|
2019-10-17 12:41:19 +00:00
|
|
|
if (!(d = XOpenDisplay(0))) exit(1);
|
2019-10-13 08:03:24 +00:00
|
|
|
|
2019-10-11 19:06:01 +00:00
|
|
|
signal(SIGCHLD, SIG_IGN);
|
2020-01-24 04:40:37 +00:00
|
|
|
XSetErrorHandler(xerror);
|
2019-10-11 11:48:34 +00:00
|
|
|
|
2019-10-19 04:49:26 +00:00
|
|
|
int s = DefaultScreen(d);
|
|
|
|
Window root = RootWindow(d, s);
|
|
|
|
sw = XDisplayWidth(d, s);
|
|
|
|
sh = XDisplayHeight(d, s);
|
2019-10-11 11:48:34 +00:00
|
|
|
|
2019-10-17 10:43:05 +00:00
|
|
|
XSelectInput(d, root, SubstructureRedirectMask);
|
2019-10-17 06:03:03 +00:00
|
|
|
XDefineCursor(d, root, XCreateFontCursor(d, 68));
|
2020-01-19 23:45:57 +00:00
|
|
|
input_grab(root);
|
2019-10-11 15:00:00 +00:00
|
|
|
|
2019-10-17 11:24:33 +00:00
|
|
|
while (1 && !XNextEvent(d, &ev))
|
2019-10-11 15:00:00 +00:00
|
|
|
if (events[ev.type]) events[ev.type](&ev);
|
2019-10-11 11:48:34 +00:00
|
|
|
}
|