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

557 lines
14 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 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-16 14:25:40 +00:00
int f, wx, wy;
unsigned int ww, wh;
2019-10-11 11:48:34 +00:00
};
2019-10-14 05:41:51 +00:00
typedef struct desktop desktop;
struct desktop{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_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(const Arg arg);
2019-10-11 17:56:54 +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);
2019-10-16 11:15:34 +00:00
static void ws_save(int i);
static void ws_sel(int i);
2019-10-11 17:56:54 +00:00
2019-10-16 14:25:40 +00:00
static client *list = {0};
static desktop ws_list[10];
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-14 05:18:08 +00:00
static Display *d;
2019-10-13 19:21:13 +00:00
static Window root, cur;
2019-10-14 05:41:51 +00:00
static XButtonEvent mouse;
2019-10-11 17:56:54 +00:00
2019-10-15 11:30:48 +00:00
/*
The list of events to subscribe to and the paired functions
to call on an event.
*/
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-16 14:25:40 +00:00
/*
Iterate over the current desktop's client list.
*/
2019-10-16 11:15:34 +00:00
#define win (client *c=list;c;c=c->next)
2019-10-16 14:25:40 +00:00
/*
Focus the given window.
*/
#define win_focus(W) XSetInputFocus(d, W, RevertToParent, CurrentTime);
2019-10-16 14:25:40 +00:00
/*
This function stores the desired window's geometry.
This previously used 'XGetWindowAttributes' which
returned too much information. We only need x, y, w, h and
not a list of 20 or so attributes of differing types.
'XGetWindowAttributes' also calls 'XGetGeometry' to return
this information, so lets just call it directly and skip
the middleman.
*/
#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-15 10:21:58 +00:00
/*
'sowm' doesn't keep track of the currently focused window
2019-10-16 06:19:56 +00:00
and instead grabs the window under the cursor when needed.
2019-10-15 10:21:58 +00:00
This is a super lazy way of handling current focus, however
it aligns perfectly with mouse-follows-focus.
2019-10-16 06:19:56 +00:00
Logic elsewhere will select a real window if this function
2019-10-15 10:21:58 +00:00
returns the 'root' window.
This function returns the current window while at the same
time defining a global variable to contain its value. This
allows for stupidily simple usage.
Example: if (win_current() != root) XKillClient(d, cur);
The value can be used as function output and then
the same value can be used as a variable directly afterwards.
*/
2019-10-14 06:06:41 +00:00
Window win_current() {
2019-10-15 16:45:24 +00:00
XGetInputFocus(d, &cur, (int[]){1});
2019-10-14 06:06:41 +00:00
return cur;
}
2019-10-15 10:21:58 +00:00
/*
2019-10-15 10:30:13 +00:00
When a window is destroyed it is first removed from the
current desktop's window list and finally focus is shifted.
2019-10-15 10:21:58 +00:00
2019-10-15 10:30:13 +00:00
Focus goes to the window under the cursor if it is *not*
the root window. If it is the root window, focus goes to
the first window in the desktop.
2019-10-15 10:21:58 +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
if (list) win_focus(win_current() == root ? list->w : cur);
2019-10-12 18:34:43 +00:00
}
2019-10-15 10:21:58 +00:00
/*
2019-10-15 10:30:13 +00:00
When the mouse enters or leaves a window this function
handles which window shall be focused next.
2019-10-15 10:21:58 +00:00
2019-10-15 10:30:13 +00:00
The while loop firstly compresses all 'EnterNotify'
events down to only the latest which is an optimization
when focus changes very quickly (e.g a desktop focus).
2019-10-15 10:21:58 +00:00
2019-10-15 10:30:13 +00:00
There's no use in computing each and every event as we
only really care about the newest one.
2019-10-15 10:21:58 +00:00
2019-10-15 10:30:13 +00:00
Focus is only then changed if the mouse has entered a
window which is *not* the root window.
2019-10-15 10:21:58 +00:00
*/
2019-10-12 18:34:43 +00:00
void notify_enter(XEvent *e) {
while(XCheckTypedEvent(d, EnterNotify, e));
2019-10-17 06:16:22 +00:00
win_focus(e->xcrossing.window)
2019-10-12 18:34:43 +00:00
}
2019-10-15 10:30:13 +00:00
/*
When the mouse is moved and the paired modifier is
pressed this function handles a window move or a window
resize.
'mouse' is defined on a modifier+mouse press and then
discarded on a modifier+mouse release.
The while loop firstly compresses all 'MotionNotify'
events down to only the latest which is an optimization
when motion happens very quickly.
There's no use in computing each and every event as we
only really care about the newest one.
2019-10-15 20:44:04 +00:00
The window is then moved or resized.
2019-10-15 10:30:13 +00:00
*/
2019-10-12 18:34:43 +00:00
void notify_motion(XEvent *e) {
2019-10-15 20:44:04 +00:00
if (mouse.subwindow == None) 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
}
2019-10-15 10:36:28 +00:00
/*
This function fires on a key press and checks to see if there
is a matching and defined key binding. If there is a match the
function bound to the key is executed.
The deprecated 'XKeycodeToKeysym' is used as the replacement
requires an additional include and I want to keep them to a
minimum.
I highly doubt this deprecated function goes away any time soon
and worst case, I simply update this code. Win-win.
*/
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);
}
2019-10-15 10:40:54 +00:00
/*
On a mouse button press the window below the cursor's
attributes are stored, the window is raised and the 'mouse'
global is set.
Setting the 'mouse' global tells the motion handling function
that it should operate on the window as the user desires a move
or resize.
*/
2019-10-12 18:34:43 +00:00
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-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
}
2019-10-15 10:40:54 +00:00
/*
On a mouse button release we simply unset the 'mouse' global
as all of this mouse pointer nonsense is done.
2019-10-15 20:44:04 +00:00
We also reset the current window's fullscreen state as it is
no longer at 0,0+[screen_width]X[screen_height].
2019-10-15 10:40:54 +00:00
*/
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-14 05:41:51 +00:00
mouse.subwindow = None;
2019-10-12 18:34:43 +00:00
}
2019-10-15 10:50:43 +00:00
/*
This function is called whenever a window is mapped to the
screen or moved to another desktop.
Memory is allocated for the new window and the current
desktop's window list is updated.
*/
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-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-15 10:50:43 +00:00
/*
This function is called whenever a window is destoyed
or moved to another desktop.
Memory is freed and the current desktop's window list
is updated.
*/
2019-10-12 18:34:43 +00:00
void win_del(Window w) {
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-14 05:41:51 +00:00
ws_save(ws);
2019-10-13 12:34:20 +00:00
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-14 05:41:51 +00:00
ws_save(ws);
2019-10-13 10:35:45 +00:00
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-15 10:50:43 +00:00
/*
This function is called from a key binding to
close the currently focused window.
This differs from other window managers as we skip
the questions and go straight to the killing of
the window.
When I want to close a window I'm not asking, I
want the window closed and so it should immediately
close.
"Shoot first and don't ask questions later?.."
*/
2019-10-12 18:34:43 +00:00
void win_kill() {
if (win_current() != root) XKillClient(d, cur);
2019-10-11 11:48:34 +00:00
}
2019-10-15 11:03:12 +00:00
/*
This function simply centers the window passed as
an argument. If the argument is '0', use the
currently focused window.
2019-10-15 11:03:12 +00:00
*/
void win_center(const Arg arg) {
2019-10-15 16:45:24 +00:00
Window w = arg.w ? arg.w : win_current();
2019-10-16 14:25:40 +00:00
win_size(w, &wx, &wy, &ww, &wh);
2019-10-11 17:06:19 +00:00
2019-10-16 14:25:40 +00:00
XMoveWindow(d, w, sw / 2 - ww / 2,
sh / 2 - wh / 2);
2019-10-11 17:06:19 +00:00
}
2019-10-15 11:03:12 +00:00
/*
This function toggles the fullscreen state for the
2019-10-15 11:03:12 +00:00
window passed as an argument.
The window's data stucture holds an integer which
is set to '0' for False and '1' for True.
When a window is set to fullscreen it is simply
resized to fit the screen and the prior size and
positioning is stored so it can be restored when
the window is un-fullscreened.
*/
void win_fs() {
win_current();
for win if (c->w == cur) {
2019-10-13 15:17:39 +00:00
if ((c->f = c->f == 0 ? 1 : 0)) {
2019-10-16 14:25:40 +00:00
win_size(cur, &c->wx, &c->wy, &c->ww, &c->wh);
XMoveResizeWindow(d, cur, 0, 0, sw, sh);
2019-10-11 17:22:10 +00:00
2019-10-13 15:17:39 +00:00
} else
2019-10-16 14:25:40 +00:00
XMoveResizeWindow(d, cur, c->wx, c->wy, c->ww, c->wh);
2019-10-13 15:17:39 +00:00
}
2019-10-11 17:22:10 +00:00
}
2019-10-15 11:03:12 +00:00
/*
This function simply moves the focused window to
the desired desktop.
It firstly adds the window to the destination
desktop's window list and secondly deletes it
from the current desktop's window list.
The window is then unmapped from the screen and
the focus is shifted to the first window in the
list.
*/
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-13 15:30:12 +00:00
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
if (list) win_focus(list->w);
2019-10-11 11:48:34 +00:00
}
2019-10-15 11:03:12 +00:00
/*
This function focuses the next window in the
current desktop's window list.
If the end of the window list is reached it
wraps back around to the start of the list.
The newly focused window is then raised to
the top of the stack.
*/
2019-10-12 18:34:43 +00:00
void win_next() {
2019-10-13 15:30:12 +00:00
win_current();
2019-10-16 12:22:28 +00:00
2019-10-16 11:15:34 +00:00
for win if (c->w == cur) {
2019-10-13 14:45:44 +00:00
c = c->next ? c->next : list;
2019-10-11 18:29:41 +00:00
win_focus(c->w);
2019-10-14 05:18:08 +00:00
XRaiseWindow(d, c->w);
2019-10-16 11:15:34 +00:00
return;
2019-10-12 18:34:43 +00:00
}
2019-10-11 20:40:30 +00:00
}
2019-10-15 11:24:53 +00:00
/*
This function changes the focus to another desktop.
To make this operation invisible the destination
desktop's windows are mapped first and the previous
desktop's windows are then unmapped afterwards.
Finally, focus is shifted to the first window on the
destination desktop's window list.
*/
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-16 11:15:34 +00:00
/*
This function saves the current desktop's window list.
Simple, nothing to see here.
*/
void ws_save(int i) {
ws_list[i].list = list;
}
/*
This function restores a saved desktop's window list.
Simple, nothing to see here.
*/
void ws_sel(int i) {
list = ws_list[i].list;
ws = i;
}
2019-10-15 11:24:53 +00:00
/*
This function allows a window to request a size,
position and other attributes.
This is required so programs like Firefox or MPV
are able to display and function correctly.
*/
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-15 11:24:53 +00:00
/*
This function is executed whenever a window is mapped to
the screen.
The window is centered, mapped to the screen, focused and
finally added to the current desktop's window list.
'XSelectInput' is called to subscribe to various events
related to the window. For example, this is used to get
focus-follows-cursor to work.
*/
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:16:22 +00:00
XSelectInput(d, w, StructureNotifyMask|EnterWindowMask|LeaveWindowMask);
2019-10-15 16:45:24 +00:00
win_center((Arg){.i = w});
2019-10-14 05:18:08 +00:00
XMapWindow(d, w);
win_focus(w);
2019-10-13 15:37:44 +00:00
win_add(w);
2019-10-11 11:48:34 +00:00
}
2019-10-15 11:24:53 +00:00
/*
This function is executed by keybindings to run the
specified program. Simple enough.
*/
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-15 11:24:53 +00:00
/*
Initialize the window manager by registering all
keybindings, setting some globals and starting the
event loop.
2019-10-16 06:16:11 +00:00
There's no 'XCloseDisplay' or clean up as the only
way to exit this window manager is to kill the process.
2019-10-15 11:24:53 +00:00
This fires up Xorg's internal clean up which covers
everything allocated and executed here. It's free!
*/
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-17 06:03:03 +00:00
XSetErrorHandler(0);
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 06:16:22 +00:00
XSelectInput(d, root, SubstructureNotifyMask|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,
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
}