This repository has been archived on 2021-03-07. You can view files and clone it, but cannot push or open issues or pull requests.
sowm/sowm.c

312 lines
6.9 KiB
C
Raw Normal View History

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>
#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
typedef union {
const char** com;
const int i;
2019-10-15 16:45:24 +00:00
const Window w;
2019-10-11 11:48:34 +00:00
} Arg;
struct key {
unsigned int mod;
KeySym keysym;
void (*function)(const Arg arg);
const Arg arg;
};
typedef struct client {
struct client *next, *prev;
2019-10-16 14:25:40 +00:00
int f, wx, wy;
unsigned int w, ww, wh;
} client;
2019-10-11 11:48:34 +00:00
2019-10-13 19:21:13 +00:00
static void button_press(XEvent *e);
static void button_release();
static void configure_request(XEvent *e);
static void key_press(XEvent *e);
static void map_request(XEvent *e);
2019-10-11 20:40:30 +00:00
static void notify_destroy(XEvent *e);
static void notify_enter(XEvent *e);
2019-10-12 17:02:35 +00:00
static void notify_motion(XEvent *e);
2019-10-13 19:21:13 +00:00
static void run(const Arg arg);
2019-10-11 17:33:03 +00:00
static void win_add(Window w);
static void win_center();
2019-10-18 09:32:36 +00:00
static void win_del(Window w);
static void win_fs();
2019-10-11 17:33:03 +00:00
static void win_kill();
static void win_next();
2019-10-11 17:56:54 +00:00
static void win_to_ws(const Arg arg);
static void ws_go(const Arg arg);
static int xerror() { return 0;}
2019-10-11 17:56:54 +00:00
static client *list = {0}, *ws_list[10] = {0}, *cur = {0};
static int ws = 1, sw, sh, wx, wy;
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 Window root;
2019-10-17 14:02:42 +00:00
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
};
#include "config.h"
#define win (client *c=list;c;c=c->next)
#define ws_save(W) ws_list[W] = list
#define ws_sel(W) list = ws_list[ws = W]
2019-10-16 14:25:40 +00:00
#define win_size(W, gx, gy, gw, gh) \
XGetGeometry(d, W, &(Window){0}, gx, gy, gw, gh, \
&(unsigned int){0}, &(unsigned int){0})
2019-10-16 14:25:40 +00:00
void win_focus(Window w) {
XSetInputFocus(d, w, RevertToParent, CurrentTime);
for win if (c->w == w) cur = c;
2019-10-14 06:06:41 +00:00
}
2019-10-12 18:34:43 +00:00
void notify_destroy(XEvent *e) {
2019-10-18 09:32:36 +00:00
win_del(e->xdestroywindow.window);
2019-10-13 14:57:24 +00:00
if (list) win_focus(list->w);
2019-10-12 18:34:43 +00:00
}
void notify_enter(XEvent *e) {
while(XCheckTypedEvent(d, EnterNotify, e));
win_focus(e->xcrossing.window);
2019-10-12 18:34:43 +00:00
}
void notify_motion(XEvent *e) {
2019-10-17 12:11:14 +00:00
if (mouse.subwindow == 0) return;
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
while(XCheckTypedEvent(d, MotionNotify, e));
2019-10-13 07:47:06 +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-14 05:18:08 +00:00
KeySym keysym = XKeycodeToKeysym(d, e->xkey.keycode, 0);
2019-10-12 18:34:43 +00:00
2019-10-15 16:45:24 +00:00
for (unsigned int i=0; i < sizeof(keys)/sizeof(*keys); ++i)
2019-10-13 15:37:44 +00:00
if (keys[i].keysym == keysym && keys[i].mod == e->xkey.state)
2019-10-12 18:34:43 +00:00
keys[i].function(keys[i].arg);
}
void button_press(XEvent *e) {
2019-10-17 12:11:14 +00:00
if (e->xbutton.subwindow == 0) 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
}
void button_release() {
for win if (c->w == mouse.subwindow) c->f = 0;
2019-10-15 20:44:04 +00:00
2019-10-17 12:11:14 +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-11 15:52:07 +00:00
client *c, *t;
2019-10-11 11:48:34 +00:00
2019-10-18 10:05:25 +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-13 07:41:35 +00:00
if (!list) {
2019-10-15 16:45:24 +00:00
c->next = c->prev = 0;
2019-10-14 05:18:08 +00:00
c->w = w;
2019-10-13 07:41:35 +00:00
list = c;
2019-10-11 15:52:07 +00:00
2019-10-12 20:24:30 +00:00
} else {
2019-10-13 07:41:35 +00:00
for (t=list;t->next;t=t->next);
2019-10-11 11:48:34 +00:00
2019-10-13 12:34:20 +00:00
c->next = 0;
c->prev = t;
2019-10-14 05:18:08 +00:00
c->w = w;
2019-10-11 11:48:34 +00:00
t->next = c;
}
2019-10-14 05:41:51 +00:00
ws_save(ws);
2019-10-11 11:48:34 +00:00
}
2019-10-18 09:32:36 +00:00
void win_del(Window w) {
for win if (c->w == w) {
if (!c->prev && !c->next) {
free(list);
list = 0;
ws_save(ws);
return;
}
if (!c->prev) {
list = c->next;
c->next->prev = 0;
} else if (!c->next) {
c->prev->next = 0;
} else {
c->prev->next = c->next;
c->next->prev = c->prev;
}
free(c);
ws_save(ws);
return;
2019-10-12 18:34:43 +00:00
}
2019-10-12 13:41:35 +00:00
}
2019-10-11 15:00:00 +00:00
2019-10-12 18:34:43 +00:00
void win_kill() {
if (cur) XKillClient(d, cur->w);
2019-10-11 11:48:34 +00:00
}
void win_center() {
if (!cur) return;
2019-10-11 17:06:19 +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
}
void win_fs() {
if (!cur) return;
2019-10-18 08:42:57 +00:00
if ((cur->f = cur->f == 0 ? 1 : 0)) {
win_size(cur->w, &cur->wx, &cur->wy, &cur->ww, &cur->wh);
XMoveResizeWindow(d, cur->w, 0, 0, sw, sh);
} 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);
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);
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
if (list) win_focus(list->w);
2019-10-11 11:48:34 +00:00
}
2019-10-12 18:34:43 +00:00
void win_next() {
if (!list) return;
2019-10-18 08:42:57 +00:00
client *c = cur->next ? cur->next : list;
2019-10-16 12:22:28 +00:00
win_focus(c->w);
XRaiseWindow(d, c->w);
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
if (list) 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
if (list) 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
if (list) win_focus(list->w);
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;
XSelectInput(d, w, StructureNotifyMask|EnterWindowMask);
win_size(w, &wx, &wy, &ww, &wh);
win_add(w);
win_focus(w);
if (wx == 0 && wy == 0) win_center();
2019-10-15 16:45:24 +00:00
2019-10-14 05:18:08 +00:00
XMapWindow(d, w);
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
}
2019-10-13 08:03:24 +00:00
int main(void) {
XEvent ev;
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);
XSetErrorHandler(xerror);
2019-10-11 11:48:34 +00:00
int s = DefaultScreen(d);
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));
2019-10-17 06:16:22 +00:00
for (unsigned int i=0; i < sizeof(keys)/sizeof(*keys); ++i)
XGrabKey(d, XKeysymToKeycode(d, keys[i].keysym), keys[i].mod,
root, True, GrabModeAsync, GrabModeAsync);
for (int i=1; i<4; i+=2)
XGrabButton(d, i, MOD, root, True,
ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
2019-10-17 12:11:14 +00:00
GrabModeAsync, GrabModeAsync, 0, 0);
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
}