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

365 lines
7.6 KiB
C
Raw Normal View History

2019-10-11 18:40:22 +00:00
// sowm
// An itsy bitsy floating window manager
// with roots in catwm.
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
2019-10-13 15:17:39 +00:00
#define WIN (c=list;c;c=c->next)
2019-10-14 05:18:08 +00:00
#define FOC(W) XSetInputFocus(d, W, RevertToParent, CurrentTime);
2019-10-12 17:02:35 +00:00
2019-10-11 11:48:34 +00:00
typedef union {
const char** com;
const int i;
} Arg;
struct key {
unsigned int mod;
KeySym keysym;
void (*function)(const Arg arg);
const Arg arg;
};
typedef struct client client;
struct client{
2019-10-13 12:34:20 +00:00
client *next, *prev;
2019-10-14 05:18:08 +00:00
Window w;
2019-10-13 07:41:35 +00:00
XWindowAttributes a;
int f;
2019-10-11 11:48:34 +00:00
};
2019-10-12 17:02:35 +00:00
typedef struct ws ws;
2019-10-13 07:41:35 +00:00
struct ws{client *list;};
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_grab();
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-12 17:02:35 +00:00
static void win_center(Window w);
static void win_center_current();
2019-10-11 17:56:54 +00:00
static void win_del(Window w);
2019-10-12 13:41:35 +00:00
static void win_fs(Window w);
static void win_fs_current();
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 void ws_save(int i);
static void ws_sel(int i);
2019-10-13 19:21:13 +00:00
static client *list = { 0 };
static ws ws_list[10];
static int desk = 1, sh, sw, s, j;
2019-10-11 17:56:54 +00:00
2019-10-14 05:18:08 +00:00
static Display *d;
2019-10-13 19:21:13 +00:00
static Window root, cur;
2019-10-11 17:56:54 +00:00
static XButtonEvent start;
2019-10-11 15:52:07 +00:00
static XWindowAttributes attr;
2019-10-11 17:56:54 +00:00
#include "config.h"
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-12 18:34:43 +00:00
void notify_destroy(XEvent *e) {
win_del(e->xdestroywindow.window);
2019-10-13 14:57:24 +00:00
2019-10-14 05:18:08 +00:00
if (list) FOC(list->w);
2019-10-12 18:34:43 +00:00
}
void notify_enter(XEvent *e) {
2019-10-13 17:04:34 +00:00
if (e->xcrossing.window != root) FOC(e->xcrossing.window)
2019-10-12 18:34:43 +00:00
}
void notify_motion(XEvent *e) {
2019-10-13 07:47:06 +00:00
client *c;
2019-10-12 18:34:43 +00:00
if (start.subwindow != None) {
2019-10-13 19:41:44 +00:00
int xd = e->xbutton.x_root - start.x_root;
int yd = e->xbutton.y_root - start.y_root;
2019-10-12 18:34:43 +00:00
2019-10-14 05:18:08 +00:00
while(XCheckTypedEvent(d, MotionNotify, e));
2019-10-13 18:59:52 +00:00
2019-10-14 05:18:08 +00:00
XMoveResizeWindow(d, start.subwindow,
2019-10-13 19:41:44 +00:00
attr.x + (start.button==1 ? xd : 0),
attr.y + (start.button==1 ? yd : 0),
attr.width + (start.button==3 ? xd : 0),
attr.height + (start.button==3 ? yd : 0));
2019-10-12 18:34:43 +00:00
}
2019-10-13 07:47:06 +00:00
2019-10-14 05:18:08 +00:00
for WIN if (c->w == start.subwindow) c->f = 0;
2019-10-12 18:34:43 +00:00
}
void key_grab() {
KeyCode code;
2019-10-13 15:32:59 +00:00
for (int i=0; i < sizeof(keys)/sizeof(*keys); ++i)
2019-10-14 05:18:08 +00:00
if ((code = XKeysymToKeycode(d, keys[i].keysym)))
XGrabKey(d, code, keys[i].mod, root,
2019-10-12 18:34:43 +00:00
True, GrabModeAsync, GrabModeAsync);
}
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-13 15:32:59 +00:00
for (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-13 15:37:44 +00:00
if (e->xbutton.subwindow == None) return;
2019-10-12 18:34:43 +00:00
2019-10-14 05:18:08 +00:00
XGetWindowAttributes(d, e->xbutton.subwindow, &attr);
XRaiseWindow(d, e->xbutton.subwindow);
2019-10-13 15:37:44 +00:00
start = e->xbutton;
2019-10-12 18:34:43 +00:00
}
void button_release() {
start.subwindow = None;
}
Window win_current() {
2019-10-14 05:18:08 +00:00
XGetInputFocus(d, &cur, &j);
2019-10-13 15:30:12 +00:00
return cur;
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-14 05:12:49 +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-13 12:34:20 +00:00
c->next = 0;
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-12 15:43:30 +00:00
ws_save(desk);
2019-10-11 11:48:34 +00:00
}
2019-10-12 18:34:43 +00:00
void win_del(Window w) {
2019-10-11 11:48:34 +00:00
client *c;
2019-10-14 05:18:08 +00:00
for WIN if (c->w == w) {
2019-10-13 12:34:20 +00:00
if (!c->prev && !c->next) {
2019-10-13 07:41:35 +00:00
free(list);
list = 0;
2019-10-13 12:34:20 +00:00
ws_save(desk);
return;
}
2019-10-12 17:02:35 +00:00
2019-10-13 12:34:20 +00:00
if (!c->prev) {
2019-10-13 07:41:35 +00:00
list = c->next;
2019-10-13 12:34:20 +00:00
c->next->prev = 0;
} else if (!c->next) {
c->prev->next = 0;
} else {
c->prev->next = c->next;
c->next->prev = c->prev;
2019-10-12 18:34:43 +00:00
}
2019-10-13 12:34:20 +00:00
free(c);
2019-10-13 10:35:45 +00:00
ws_save(desk);
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() {
2019-10-13 15:30:12 +00:00
win_current();
2019-10-12 13:41:35 +00:00
2019-10-14 05:18:08 +00:00
if (cur != root) XKillClient(d, cur);
2019-10-11 11:48:34 +00:00
}
2019-10-12 13:41:35 +00:00
void win_center(Window w) {
2019-10-14 05:18:08 +00:00
XGetWindowAttributes(d, w, &attr);
2019-10-11 17:06:19 +00:00
2019-10-14 05:18:08 +00:00
XMoveWindow(d, w, sw / 2 - attr.width / 2,
sh / 2 - attr.height / 2);
2019-10-11 17:06:19 +00:00
}
2019-10-12 13:41:35 +00:00
void win_fs(Window w) {
client *c;
2019-10-11 17:22:10 +00:00
2019-10-14 05:18:08 +00:00
for WIN if (c->w == w) {
2019-10-13 15:17:39 +00:00
if ((c->f = c->f == 0 ? 1 : 0)) {
2019-10-14 05:18:08 +00:00
XGetWindowAttributes(d, w, &c->a);
XMoveResizeWindow(d, w, 0, 0, sw, sh);
2019-10-11 17:22:10 +00:00
2019-10-13 15:17:39 +00:00
} else
2019-10-14 05:18:08 +00:00
XMoveResizeWindow(d, w, c->a.x, c->a.y, c->a.width, c->a.height);
2019-10-13 15:17:39 +00:00
}
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-13 15:30:12 +00:00
int tmp = desk;
win_current();
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-12 15:43:30 +00:00
win_add(cur);
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-12 15:43:30 +00:00
win_del(cur);
2019-10-14 05:18:08 +00:00
XUnmapWindow(d, cur);
2019-10-12 15:02:09 +00:00
ws_save(tmp);
2019-10-13 14:45:44 +00:00
2019-10-14 05:18:08 +00:00
if (list) FOC(list->w);
2019-10-11 11:48:34 +00:00
}
2019-10-12 18:34:43 +00:00
void win_next() {
2019-10-13 15:30:12 +00:00
win_current();
2019-10-13 12:34:20 +00:00
client *c;
2019-10-13 14:45:44 +00:00
if (list) {
2019-10-14 05:18:08 +00:00
for WIN if (c->w == cur) break;
2019-10-11 15:00:00 +00:00
2019-10-13 14:45:44 +00:00
c = c->next ? c->next : list;
2019-10-11 18:29:41 +00:00
2019-10-14 05:18:08 +00:00
FOC(c->w);
XRaiseWindow(d, c->w);
2019-10-12 18:34:43 +00:00
}
2019-10-11 20:40:30 +00:00
}
2019-10-12 18:34:43 +00:00
void win_fs_current() {
win_fs(win_current());
}
2019-10-11 11:48:34 +00:00
2019-10-12 18:34:43 +00:00
void win_center_current() {
win_center(win_current());
2019-10-11 11:48:34 +00:00
}
2019-10-12 18:34:43 +00:00
void ws_go(const Arg arg) {
client *c;
int tmp = desk;
2019-10-11 11:48:34 +00:00
2019-10-12 18:34:43 +00:00
if (arg.i == desk) return;
2019-10-11 11:48:34 +00:00
2019-10-12 18:34:43 +00:00
ws_save(desk);
ws_sel(arg.i);
2019-10-11 15:00:00 +00:00
2019-10-14 05:18:08 +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
2019-10-14 05:18:08 +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
2019-10-14 05:18:08 +00:00
if (list) FOC(list->w);
2019-10-11 15:00:00 +00:00
}
2019-10-12 18:34:43 +00:00
void ws_save(int i) {
2019-10-13 07:41:35 +00:00
ws_list[i].list = list;
2019-10-11 15:00:00 +00:00
}
2019-10-12 18:34:43 +00:00
void ws_sel(int i) {
2019-10-13 07:41:35 +00:00
list = ws_list[i].list;
2019-10-12 18:34:43 +00:00
desk = i;
}
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;
XWindowChanges wc;
2019-10-13 21:18:41 +00:00
wc.x = ev->x;
wc.y = ev->y;
2019-10-12 18:34:43 +00:00
wc.width = ev->width;
wc.height = ev->height;
wc.sibling = ev->above;
wc.stack_mode = ev->detail;
2019-10-14 05:18:08 +00:00
XConfigureWindow(d, ev->window, ev->value_mask, &wc);
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-14 05:18:08 +00:00
XSelectInput(d, w, PropertyChangeMask|StructureNotifyMask|
EnterWindowMask|FocusChangeMask);
2019-10-13 15:37:44 +00:00
win_center(w);
2019-10-14 05:18:08 +00:00
XMapWindow(d, w);
2019-10-13 15:37:44 +00:00
FOC(w);
win_add(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-14 05:18:08 +00:00
int xerror(Display *d, XErrorEvent *e) {
2019-10-13 11:05:12 +00:00
return 0;
}
2019-10-13 08:03:24 +00:00
int main(void) {
XEvent ev;
2019-10-14 05:18:08 +00:00
if (!(d = XOpenDisplay(0x0))) return 0;
2019-10-13 08:03:24 +00:00
2019-10-11 19:06:01 +00:00
signal(SIGCHLD, SIG_IGN);
2019-10-13 19:29:09 +00:00
XSetErrorHandler(xerror);
2019-10-11 11:48:34 +00:00
2019-10-14 05:18:08 +00:00
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-11 17:56:54 +00:00
key_grab();
2019-10-11 11:48:34 +00:00
2019-10-13 15:32:59 +00:00
for (int i=0; i < sizeof(ws_list)/sizeof(*ws_list); ++i)
2019-10-13 07:41:35 +00:00
ws_list[i].list = 0;
2019-10-11 11:48:34 +00:00
2019-10-13 19:21:13 +00:00
ws_go((Arg){.i = 1});
2019-10-11 11:53:52 +00:00
2019-10-14 05:18:08 +00:00
XSelectInput(d, root, SubstructureNotifyMask|
2019-10-12 20:12:30 +00:00
SubstructureRedirectMask|EnterWindowMask|LeaveWindowMask);
2019-10-12 19:12:20 +00:00
2019-10-14 05:18:08 +00:00
XGrabButton(d, 1, Mod4Mask, root, True,
2019-10-12 20:12:30 +00:00
ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
GrabModeAsync, GrabModeAsync, None, None);
2019-10-11 15:00:00 +00:00
2019-10-14 05:18:08 +00:00
XGrabButton(d, 3, Mod4Mask, root, True,
2019-10-12 20:12:30 +00:00
ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
GrabModeAsync, GrabModeAsync, None, None);
2019-10-11 15:00:00 +00:00
2019-10-14 05:18:08 +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
}