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/catwm.c

480 lines
11 KiB
C
Raw Normal View History

2019-10-11 11:48:34 +00:00
/*
* /\___/\
* ( o o ) Made by cat...
* ( =^= )
* ( ) ... for cat!
* ( )
2019-10-11 15:52:07 +00:00
* ( ))))))________________
* __________________
2019-10-11 11:48:34 +00:00
*/
#include <X11/Xlib.h>
2019-10-11 11:53:16 +00:00
#include <X11/XKBlib.h>
2019-10-11 11:48:34 +00:00
#include <X11/keysym.h>
#include <X11/XF86keysym.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#define TABLENGTH(X) (sizeof(X)/sizeof(*X))
2019-10-11 15:00:00 +00:00
#define MAX(a, b) ((a) > (b) ? (a) : (b))
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{
client *next;
client *prev;
Window win;
2019-10-11 17:22:10 +00:00
int f, x, y, w, h;
2019-10-11 11:48:34 +00:00
};
typedef struct desktop desktop;
struct desktop{
int mode;
client *head;
client *current;
};
static void add_window(Window w);
2019-10-11 15:00:00 +00:00
static void buttonpress(XEvent *e);
static void buttonrelease(XEvent *e);
2019-10-11 17:06:19 +00:00
static void center_window();
2019-10-11 17:22:10 +00:00
static void fullscreen_toggle();
2019-10-11 11:48:34 +00:00
static void change_desktop(const Arg arg);
static void client_to_desktop(const Arg arg);
static void configurerequest(XEvent *e);
static void destroynotify(XEvent *e);
static void grabkeys();
2019-10-11 15:52:07 +00:00
static void init();
2019-10-11 11:48:34 +00:00
static void keypress(XEvent *e);
static void kill_client();
static void maprequest(XEvent *e);
2019-10-11 15:00:00 +00:00
static void motionnotify(XEvent *e);
2019-10-11 11:48:34 +00:00
static void next_win();
static void remove_window(Window w);
static void save_desktop(int i);
static void select_desktop(int i);
static void send_kill_signal(Window w);
static void setup();
static void sigchld(int unused);
static void spawn(const Arg arg);
2019-10-11 15:00:00 +00:00
static void update_current();
2019-10-11 11:48:34 +00:00
#include "config.h"
static int bool_quit;
2019-10-11 15:52:07 +00:00
static int curr_desk;
2019-10-11 11:48:34 +00:00
static int mode;
2019-10-11 15:52:07 +00:00
static int screen;
2019-10-11 11:48:34 +00:00
static int sh;
static int sw;
2019-10-11 15:52:07 +00:00
static Display *dis;
2019-10-11 11:48:34 +00:00
static Window root;
2019-10-11 15:00:00 +00:00
static XButtonEvent start;
2019-10-11 15:52:07 +00:00
static XWindowAttributes attr;
static client *current;
static client *head;
2019-10-11 16:51:06 +00:00
static desktop desktops[10];
2019-10-11 11:48:34 +00:00
static void (*events[LASTEvent])(XEvent *e) = {
2019-10-11 15:00:00 +00:00
[KeyPress] = keypress,
[MapRequest] = maprequest,
[DestroyNotify] = destroynotify,
[ConfigureRequest] = configurerequest,
[ButtonPress] = buttonpress,
[ButtonRelease] = buttonrelease,
[MotionNotify] = motionnotify
2019-10-11 11:48:34 +00:00
};
void add_window(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-11 15:52:07 +00:00
if (head == NULL) {
2019-10-11 11:48:34 +00:00
c->next = NULL;
c->prev = NULL;
2019-10-11 15:52:07 +00:00
c->win = w;
head = c;
2019-10-11 11:48:34 +00:00
}
2019-10-11 15:52:07 +00:00
2019-10-11 11:48:34 +00:00
else {
for(t=head;t->next;t=t->next);
c->next = NULL;
c->prev = t;
2019-10-11 15:52:07 +00:00
c->win = w;
2019-10-11 11:48:34 +00:00
t->next = c;
}
current = c;
}
void change_desktop(const Arg arg) {
client *c;
2019-10-11 15:52:07 +00:00
if (arg.i == curr_desk)
2019-10-11 11:48:34 +00:00
return;
2019-10-11 15:52:07 +00:00
if (head != NULL)
for(c=head;c;c=c->next) XUnmapWindow(dis,c->win);
2019-10-11 11:48:34 +00:00
2019-10-11 15:52:07 +00:00
save_desktop(curr_desk);
2019-10-11 11:48:34 +00:00
select_desktop(arg.i);
2019-10-11 15:52:07 +00:00
if (head != NULL)
for(c=head;c;c=c->next) XMapWindow(dis,c->win);
2019-10-11 15:00:00 +00:00
update_current();
2019-10-11 11:48:34 +00:00
}
2019-10-11 17:06:19 +00:00
void center_window() {
XGetWindowAttributes(dis, current->win, &attr);
int x = (sw / 2) - (attr.width / 2);
int y = (sh / 2) - (attr.height / 2);
XMoveWindow(dis, current->win, x, y);
}
2019-10-11 17:22:10 +00:00
void fullscreen_toggle() {
if (current->f != 1) {
XGetWindowAttributes(dis, current->win, &attr);
current->f = 1;
current->x = attr.x;
current->y = attr.y;
current->w = attr.width;
current->h = attr.height;
XMoveResizeWindow(dis, current->win, 0, 0, sw, sh);
}
else {
current->f = 0;
XMoveResizeWindow(dis, current->win, current->x, current->y, \
current->w, current->h);
}
}
2019-10-11 11:48:34 +00:00
void client_to_desktop(const Arg arg) {
client *tmp = current;
2019-10-11 15:52:07 +00:00
int tmp2 = curr_desk;
2019-10-11 11:53:52 +00:00
2019-10-11 15:52:07 +00:00
if (arg.i == tmp2 || current == NULL)
2019-10-11 11:48:34 +00:00
return;
// Add client to desktop
select_desktop(arg.i);
add_window(tmp->win);
save_desktop(arg.i);
// Remove client from current desktop
select_desktop(tmp2);
2019-10-11 16:51:06 +00:00
XUnmapWindow(dis,tmp->win);
remove_window(tmp->win);
save_desktop(tmp2);
2019-10-11 15:00:00 +00:00
update_current();
2019-10-11 11:48:34 +00:00
}
void configurerequest(XEvent *e) {
XConfigureRequestEvent *ev = &e->xconfigurerequest;
XWindowChanges wc;
2019-10-11 15:52:07 +00:00
wc.x = ev->x;
wc.y = ev->y;
wc.width = ev->width;
wc.height = ev->height;
2019-10-11 11:48:34 +00:00
wc.border_width = ev->border_width;
2019-10-11 15:52:07 +00:00
wc.sibling = ev->above;
wc.stack_mode = ev->detail;
2019-10-11 11:48:34 +00:00
XConfigureWindow(dis, ev->window, ev->value_mask, &wc);
}
void destroynotify(XEvent *e) {
2019-10-11 15:52:07 +00:00
int i = 0;
2019-10-11 11:48:34 +00:00
client *c;
2019-10-11 15:52:07 +00:00
2019-10-11 11:48:34 +00:00
XDestroyWindowEvent *ev = &e->xdestroywindow;
for(c=head;c;c=c->next)
2019-10-11 15:52:07 +00:00
if(ev->window == c->win) i++;
2019-10-11 11:53:52 +00:00
2019-10-11 11:48:34 +00:00
if(i == 0)
return;
remove_window(ev->window);
2019-10-11 15:00:00 +00:00
update_current();
}
void update_current() {
client *c;
for(c=head;c;c=c->next)
2019-10-11 15:52:07 +00:00
if (current == c) {
XSetInputFocus(dis, c->win, RevertToParent, CurrentTime);
XRaiseWindow(dis, c->win);
2019-10-11 15:00:00 +00:00
}
2019-10-11 11:48:34 +00:00
}
void grabkeys() {
int i;
KeyCode code;
for(i=0;i<TABLENGTH(keys);++i) {
2019-10-11 15:52:07 +00:00
if ((code = XKeysymToKeycode(dis, keys[i].keysym)))
XGrabKey(dis, code, keys[i].mod, root,
True, GrabModeAsync, GrabModeAsync);
2019-10-11 11:48:34 +00:00
}
}
void keypress(XEvent *e) {
int i;
XKeyEvent ke = e->xkey;
2019-10-11 11:53:16 +00:00
KeySym keysym = XkbKeycodeToKeysym(dis,ke.keycode,0,0);
2019-10-11 11:48:34 +00:00
for(i=0;i<TABLENGTH(keys);++i) {
if(keys[i].keysym == keysym && keys[i].mod == ke.state) {
keys[i].function(keys[i].arg);
}
}
}
2019-10-11 15:00:00 +00:00
void buttonpress(XEvent *e) {
XButtonEvent bu = e->xbutton;
if (bu.subwindow != None) {
XGetWindowAttributes(dis, bu.subwindow, &attr);
start = bu;
}
}
void motionnotify(XEvent *e) {
XButtonEvent bu = e->xbutton;
if (start.subwindow != None) {
int xdiff = bu.x_root - start.x_root;
int ydiff = bu.y_root - start.y_root;
XMoveResizeWindow(dis, start.subwindow,
attr.x + (start.button==1 ? xdiff : 0),
attr.y + (start.button==1 ? ydiff : 0),
MAX(1, attr.width + (start.button==3 ? xdiff : 0)),
MAX(1, attr.height + (start.button==3 ? ydiff : 0)));
}
}
void buttonrelease(XEvent *e) {
start.subwindow = None;
}
2019-10-11 11:48:34 +00:00
void kill_client() {
if(current != NULL) {
//send delete signal to window
XEvent ke;
2019-10-11 15:52:07 +00:00
ke.type = ClientMessage;
ke.xclient.window = current->win;
2019-10-11 11:48:34 +00:00
ke.xclient.message_type = XInternAtom(dis, "WM_PROTOCOLS", True);
2019-10-11 15:52:07 +00:00
ke.xclient.format = 32;
ke.xclient.data.l[0] = XInternAtom(dis, "WM_DELETE_WINDOW", True);
ke.xclient.data.l[1] = CurrentTime;
2019-10-11 11:48:34 +00:00
XSendEvent(dis, current->win, False, NoEventMask, &ke);
send_kill_signal(current->win);
}
}
2019-10-11 11:53:52 +00:00
2019-10-11 11:48:34 +00:00
void maprequest(XEvent *e) {
XMapRequestEvent *ev = &e->xmaprequest;
2019-10-11 15:52:07 +00:00
client *c;
2019-10-11 11:48:34 +00:00
// For fullscreen mplayer (and maybe some other program)
for(c=head;c;c=c->next)
if(ev->window == c->win) {
XMapWindow(dis,ev->window);
return;
}
add_window(ev->window);
XMapWindow(dis,ev->window);
2019-10-11 15:00:00 +00:00
update_current();
2019-10-11 11:48:34 +00:00
}
void next_win() {
client *c;
2019-10-11 15:52:07 +00:00
if (current != NULL && head != NULL) {
if (current->next == NULL)
2019-10-11 11:48:34 +00:00
c = head;
else
c = current->next;
current = c;
2019-10-11 15:00:00 +00:00
update_current();
2019-10-11 11:48:34 +00:00
}
}
void remove_window(Window w) {
client *c;
for(c=head;c;c=c->next) {
2019-10-11 16:51:06 +00:00
if(c->win == w) {
if (c->prev == NULL && c->next == NULL) {
free(head);
head = NULL;
current = NULL;
save_desktop(curr_desk);
return;
}
if (c->prev == NULL) {
head = c->next;
c->next->prev = NULL;
current = c->next;
}
else if (c->next == NULL) {
c->prev->next = NULL;
current = c->prev;
}
else {
c->prev->next = c->next;
c->next->prev = c->prev;
current = c->prev;
}
free(c);
save_desktop(curr_desk);
update_current();
2019-10-11 15:52:07 +00:00
return;
}
2019-10-11 11:48:34 +00:00
}
}
void save_desktop(int i) {
2019-10-11 15:52:07 +00:00
desktops[i].mode = mode;
desktops[i].head = head;
2019-10-11 11:48:34 +00:00
desktops[i].current = current;
}
void select_desktop(int i) {
2019-10-11 15:52:07 +00:00
head = desktops[i].head;
current = desktops[i].current;
mode = desktops[i].mode;
curr_desk = i;
2019-10-11 11:48:34 +00:00
}
2019-10-11 11:53:52 +00:00
void send_kill_signal(Window w) {
2019-10-11 11:48:34 +00:00
XEvent ke;
2019-10-11 15:52:07 +00:00
ke.type = ClientMessage;
ke.xclient.window = w;
2019-10-11 11:48:34 +00:00
ke.xclient.message_type = XInternAtom(dis, "WM_PROTOCOLS", True);
2019-10-11 15:52:07 +00:00
ke.xclient.format = 32;
ke.xclient.data.l[0] = XInternAtom(dis, "WM_DELETE_WINDOW", True);
ke.xclient.data.l[1] = CurrentTime;
2019-10-11 11:48:34 +00:00
XSendEvent(dis, w, False, NoEventMask, &ke);
}
void setup() {
2019-10-11 15:52:07 +00:00
int i;
2019-10-11 11:48:34 +00:00
sigchld(0);
screen = DefaultScreen(dis);
2019-10-11 15:00:00 +00:00
root = RootWindow(dis,screen);
sw = XDisplayWidth(dis,screen);
sh = XDisplayHeight(dis,screen);
2019-10-11 11:48:34 +00:00
grabkeys();
2019-10-11 15:00:00 +00:00
mode = 0;
2019-10-11 11:48:34 +00:00
bool_quit = 0;
2019-10-11 15:00:00 +00:00
head = NULL;
current = NULL;
2019-10-11 11:48:34 +00:00
for(i=0;i<TABLENGTH(desktops);++i) {
2019-10-11 15:52:07 +00:00
desktops[i].mode = mode;
desktops[i].head = head;
2019-10-11 11:48:34 +00:00
desktops[i].current = current;
}
const Arg arg = {.i = 1};
2019-10-11 15:52:07 +00:00
curr_desk = arg.i;
2019-10-11 11:48:34 +00:00
change_desktop(arg);
2019-10-11 11:53:52 +00:00
2019-10-11 15:52:07 +00:00
XSelectInput(dis, root, SubstructureNotifyMask|SubstructureRedirectMask);
2019-10-11 11:48:34 +00:00
}
void sigchld(int unused) {
2019-10-11 15:52:07 +00:00
if (signal(SIGCHLD, sigchld) == SIG_ERR)
exit(1);
2019-10-11 15:00:00 +00:00
2019-10-11 11:48:34 +00:00
while(0 < waitpid(-1, NULL, WNOHANG));
}
void spawn(const Arg arg) {
2019-10-11 15:52:07 +00:00
if (fork() == 0) {
if (fork() == 0) {
if (dis) close(ConnectionNumber(dis));
2019-10-11 11:48:34 +00:00
setsid();
execvp((char*)arg.com[0],(char**)arg.com);
}
2019-10-11 15:52:07 +00:00
2019-10-11 11:48:34 +00:00
exit(0);
}
}
2019-10-11 15:00:00 +00:00
void init() {
2019-10-11 11:48:34 +00:00
XEvent ev;
2019-10-11 15:00:00 +00:00
XGrabKey(dis, XKeysymToKeycode(dis, XStringToKeysym("F1")), Mod4Mask,
DefaultRootWindow(dis), True, GrabModeAsync, GrabModeAsync);
XGrabButton(dis, 1, Mod4Mask, DefaultRootWindow(dis), True,
ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None);
XGrabButton(dis, 3, Mod4Mask, DefaultRootWindow(dis), True,
ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None);
start.subwindow = None;
while(!bool_quit && !XNextEvent(dis,&ev))
if (events[ev.type]) events[ev.type](&ev);
2019-10-11 11:48:34 +00:00
}
int main(int argc, char **argv) {
2019-10-11 15:52:07 +00:00
if (!(dis = XOpenDisplay(NULL)))
exit(1);
2019-10-11 11:48:34 +00:00
setup();
2019-10-11 15:00:00 +00:00
init();
2019-10-11 11:48:34 +00:00
XCloseDisplay(dis);
return 0;
}