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

379 lines
7.9 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)
#define FOC(W) XSetInputFocus(dis, 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-11 11:48:34 +00:00
Window win;
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-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-11 17:56:54 +00:00
static void key_grab();
static void key_press(XEvent *e);
static void button_press(XEvent *e);
2019-10-12 09:31:32 +00:00
static void button_release();
2019-10-11 17:56:54 +00:00
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);
2019-10-11 11:48:34 +00:00
2019-10-11 17:56:54 +00:00
static void ws_go(const Arg arg);
static void ws_save(int i);
static void ws_sel(int i);
2019-10-11 20:40:30 +00:00
static void configure_request(XEvent *e);
static void map_request(XEvent *e);
static void run(const Arg arg);
2019-10-13 07:41:35 +00:00
static client *list = { 0 };
2019-10-12 17:02:35 +00:00
static ws ws_list[10];
2019-10-11 11:48:34 +00:00
2019-10-13 08:03:24 +00:00
static int desk = 1, sh, sw, s;
2019-10-11 17:56:54 +00:00
2019-10-11 15:52:07 +00:00
static Display *dis;
2019-10-11 17:56:54 +00:00
static Window root;
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-13 15:17:39 +00:00
if (list) FOC(list->win);
2019-10-12 18:34:43 +00:00
}
void notify_enter(XEvent *e) {
2019-10-13 15:17:39 +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-12 20:42:37 +00:00
int xdiff = e->xbutton.x_root - start.x_root;
int ydiff = e->xbutton.y_root - start.y_root;
2019-10-12 18:34:43 +00:00
XMoveResizeWindow(dis, start.subwindow,
attr.x + (start.button==1 ? xdiff : 0),
attr.y + (start.button==1 ? ydiff : 0),
attr.width + (start.button==3 ? xdiff : 0),
attr.height + (start.button==3 ? ydiff : 0));
}
2019-10-13 07:47:06 +00:00
for WIN if (c->win == start.subwindow) c->f = 0;
2019-10-12 18:34:43 +00:00
}
void key_grab() {
KeyCode code;
for(int i=0; i < sizeof(keys)/sizeof(*keys); ++i)
if ((code = XKeysymToKeycode(dis, keys[i].keysym)))
XGrabKey(dis, code, keys[i].mod, root,
True, GrabModeAsync, GrabModeAsync);
}
void key_press(XEvent *e) {
XKeyEvent ke = e->xkey;
KeySym keysym = XKeycodeToKeysym(dis,ke.keycode,0);
2019-10-13 07:48:33 +00:00
for(int i=0; i < sizeof(keys)/sizeof(*keys); ++i)
2019-10-12 18:34:43 +00:00
if (keys[i].keysym == keysym && keys[i].mod == ke.state)
keys[i].function(keys[i].arg);
}
void button_press(XEvent *e) {
XButtonEvent bu = e->xbutton;
if (bu.subwindow != None) {
XGetWindowAttributes(dis, bu.subwindow, &attr);
XRaiseWindow(dis, bu.subwindow);
start = bu;
}
}
void button_release() {
start.subwindow = None;
}
Window win_current() {
Window focused;
int revert_to;
XGetInputFocus(dis, &focused, &revert_to);
return focused;
}
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-11 15:52:07 +00:00
if (!(c = (client *)calloc(1,sizeof(client))))
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-12 15:02:09 +00:00
c->win = 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-12 15:02:09 +00:00
c->win = 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-13 12:34:20 +00:00
for WIN if (c->win == w) {
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() {
Window cur = win_current();
2019-10-12 13:41:35 +00:00
2019-10-13 14:57:24 +00:00
if (cur != root) XKillClient(dis, cur);
2019-10-11 11:48:34 +00:00
}
2019-10-12 13:41:35 +00:00
void win_center(Window w) {
XGetWindowAttributes(dis, w, &attr);
2019-10-11 17:06:19 +00:00
int x = (sw / 2) - (attr.width / 2);
int y = (sh / 2) - (attr.height / 2);
2019-10-12 13:41:35 +00:00
XMoveWindow(dis, w, x, y);
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-13 15:17:39 +00:00
for WIN if (c->win == w) {
if ((c->f = c->f == 0 ? 1 : 0)) {
XGetWindowAttributes(dis, w, &c->a);
XMoveResizeWindow(dis, w, 0, 0, sw, sh);
2019-10-11 17:22:10 +00:00
2019-10-13 15:17:39 +00:00
} else
XMoveResizeWindow(dis, w, c->a.x, c->a.y, c->a.width, c->a.height);
}
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-12 15:43:30 +00:00
int tmp = desk;
Window cur = 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-13 12:34:20 +00:00
XUnmapWindow(dis, cur);
2019-10-12 15:02:09 +00:00
ws_save(tmp);
2019-10-13 14:45:44 +00:00
2019-10-13 15:17:39 +00:00
if (list) FOC(list->win);
2019-10-11 11:48:34 +00:00
}
2019-10-12 18:34:43 +00:00
void win_next() {
Window cur = win_current();
2019-10-13 12:34:20 +00:00
client *c;
2019-10-13 14:45:44 +00:00
if (cur == root) return;
2019-10-13 12:34:20 +00:00
2019-10-13 14:45:44 +00:00
if (list) {
2019-10-13 12:34:20 +00:00
for WIN if (c->win == 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-13 15:17:39 +00:00
FOC(c->win);
2019-10-13 12:34:20 +00:00
XRaiseWindow(dis, c->win);
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-13 07:41:35 +00:00
if (list) for WIN XMapWindow(dis, c->win);
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-13 07:41:35 +00:00
if (list) for WIN XUnmapWindow(dis, c->win);
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-13 15:17:39 +00:00
if (list) FOC(list->win);
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;
wc.width = ev->width;
wc.height = ev->height;
wc.sibling = ev->above;
wc.stack_mode = ev->detail;
XConfigureWindow(dis, 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-11 11:48:34 +00:00
XMapRequestEvent *ev = &e->xmaprequest;
2019-10-12 13:41:35 +00:00
XSelectInput(dis, ev->window, PropertyChangeMask|StructureNotifyMask|
EnterWindowMask|FocusChangeMask);
win_center(ev->window);
XMapWindow(dis, ev->window);
2019-10-13 15:17:39 +00:00
FOC(ev->window);
2019-10-12 15:02:09 +00:00
win_add(ev->window);
2019-10-11 11:48:34 +00:00
}
2019-10-12 18:34:43 +00:00
void run(const Arg arg) {
if (fork()) return;
if (dis) close(ConnectionNumber(dis));
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 11:05:12 +00:00
int xerror(Display *dis, XErrorEvent *ee) {
return 0;
}
2019-10-13 08:03:24 +00:00
int main(void) {
XEvent ev;
if (!(dis = XOpenDisplay(0x0))) return 0;
2019-10-11 19:06:01 +00:00
signal(SIGCHLD, SIG_IGN);
2019-10-13 11:05:12 +00:00
XSetErrorHandler(xerror);
2019-10-11 11:48:34 +00:00
2019-10-13 08:03:24 +00:00
s = DefaultScreen(dis);
root = RootWindow(dis, s);
sw = XDisplayWidth(dis, s);
sh = XDisplayHeight(dis, 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-12 17:02:35 +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
const Arg arg = {.i = 1};
2019-10-11 17:56:54 +00:00
ws_go(arg);
2019-10-11 11:53:52 +00:00
2019-10-12 20:12:30 +00:00
XSelectInput(dis, root, SubstructureNotifyMask|
SubstructureRedirectMask|EnterWindowMask|LeaveWindowMask);
2019-10-12 19:12:20 +00:00
2019-10-12 19:46:51 +00:00
XGrabButton(dis, 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-12 19:46:51 +00:00
XGrabButton(dis, 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-12 19:12:20 +00:00
while(1 && !XNextEvent(dis, &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
}