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

305 lines
6.8 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 <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
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 ww, wh;
Window w;
} 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);
2019-10-19 05:14:03 +00:00
static void win_center();
2019-10-18 17:12:59 +00:00
static void win_del(Window w);
static void win_fs();
2019-10-11 17:33:03 +00:00
static void win_kill();
2019-12-15 23:53:52 +00:00
static void win_prev();
2019-10-11 17:33:03 +00:00
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
2019-10-18 21:30:59 +00:00
static client *list = {0}, *ws_list[10] = {0}, *cur;
2020-01-14 21:16:06 +00:00
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 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"
2019-10-19 10:40:28 +00:00
#define win (client *t=0, *c=list; c && t!=list->prev; t=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(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);
if (list) win_focus(list->prev);
2019-10-12 18:34:43 +00:00
}
void notify_enter(XEvent *e) {
while(XCheckTypedEvent(d, EnterNotify, e));
for win if (c->w == e->xcrossing.window) win_focus(c);
2019-10-12 18:34:43 +00:00
}
void notify_motion(XEvent *e) {
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) {
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)
if (keys[i].mod == e->xkey.state &&
2019-11-25 23:12:05 +00:00
keys[i].keysym == keysym)
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
}
void button_release() {
mouse.subwindow = 0;
2019-10-12 18:34:43 +00:00
}
2019-10-11 17:33:03 +00:00
void win_add(Window w) {
client *c;
2019-10-11 11:48:34 +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
c->w = w;
2019-10-18 10:45:02 +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
} else {
list = c;
list->prev = list->next = list;
}
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) {
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
2019-10-12 18:34:43 +00:00
void win_kill() {
2019-10-18 21:30:59 +00:00
if (cur) XKillClient(d, cur->w);
2019-10-11 11:48:34 +00:00
}
2019-10-19 05:14:03 +00:00
void win_center() {
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);
2019-10-11 17:06:19 +00:00
2019-10-19 05:14:03 +00:00
XMoveWindow(d, cur->w, (sw - ww) / 2, (sh - wh) / 2);
2019-10-11 17:06:19 +00:00
}
void win_fs() {
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-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
if (list) win_focus(list);
2019-10-11 11:48:34 +00:00
}
2019-12-15 23:53:52 +00:00
void win_prev() {
if (!cur) return;
2019-12-15 23:53:52 +00:00
XRaiseWindow(d, cur->prev->w);
win_focus(cur->prev);
}
2019-10-12 18:34:43 +00:00
void win_next() {
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);
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;
XSelectInput(d, w, StructureNotifyMask|EnterWindowMask);
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-19 05:14:03 +00:00
if (wx + wy == 0) win_center();
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
}
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
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));
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
}