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

448 lines
9.2 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 17:56:54 +00:00
#include <X11/XF86keysym.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>
2019-10-11 17:56:54 +00:00
#include <signal.h>
2019-10-11 11:48:34 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
2019-10-11 17:56:54 +00:00
#include <unistd.h>
2019-10-11 11:48:34 +00:00
2019-10-11 15:00:00 +00:00
#define MAX(a, b) ((a) > (b) ? (a) : (b))
2019-10-11 17:56:54 +00:00
#define TABLENGTH(X) (sizeof(X)/sizeof(*X))
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;
};
2019-10-11 17:56:54 +00:00
static void destroy_notify(XEvent *e);
static void map_request(XEvent *e);
static void run(const Arg arg);
static void sig_child(int unused);
static void key_grab();
static void key_press(XEvent *e);
static void button_press(XEvent *e);
static void button_release(XEvent *e);
static void motion_notify(XEvent *e);
2019-10-11 17:33:03 +00:00
static void win_add(Window w);
2019-10-11 17:56:54 +00:00
static void win_del(Window w);
2019-10-11 17:33:03 +00:00
static void win_fs();
static void win_kill();
2019-10-11 18:19:40 +00:00
static void win_center();
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 win_update();
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);
static void wm_init();
static void wm_setup();
static client *current;
static client *head;
static desktop desktops[10];
2019-10-11 11:48:34 +00:00
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 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,
[DestroyNotify] = destroy_notify,
[KeyPress] = key_press,
[MapRequest] = map_request,
[MotionNotify] = motion_notify
2019-10-11 11:48:34 +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-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;
}
2019-10-11 17:56:54 +00:00
void ws_go(const Arg arg) {
2019-10-11 11:48:34 +00:00
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 17:56:54 +00:00
ws_save(curr_desk);
ws_sel(arg.i);
2019-10-11 11:48:34 +00:00
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
2019-10-11 17:56:54 +00:00
win_update();
2019-10-11 11:48:34 +00:00
}
2019-10-11 18:19:40 +00:00
void win_center() {
2019-10-11 17:06:19 +00:00
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:33:03 +00:00
void win_fs() {
2019-10-11 17:22:10 +00:00
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 17:33:03 +00:00
void win_to_ws(const Arg arg) {
2019-10-11 11:48:34 +00:00
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
2019-10-11 17:56:54 +00:00
ws_sel(arg.i);
2019-10-11 17:33:03 +00:00
win_add(tmp->win);
2019-10-11 17:56:54 +00:00
ws_save(arg.i);
2019-10-11 11:48:34 +00:00
// Remove client from current desktop
2019-10-11 17:56:54 +00:00
ws_sel(tmp2);
2019-10-11 16:51:06 +00:00
XUnmapWindow(dis,tmp->win);
2019-10-11 17:33:03 +00:00
win_del(tmp->win);
2019-10-11 17:56:54 +00:00
ws_save(tmp2);
win_update();
2019-10-11 11:48:34 +00:00
}
2019-10-11 17:56:54 +00:00
void destroy_notify(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;
2019-10-11 17:33:03 +00:00
win_del(ev->window);
2019-10-11 17:56:54 +00:00
win_update();
2019-10-11 15:00:00 +00:00
}
2019-10-11 17:56:54 +00:00
void win_update() {
2019-10-11 15:00:00 +00:00
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
}
2019-10-11 17:56:54 +00:00
void key_grab() {
2019-10-11 11:48:34 +00:00
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
}
}
2019-10-11 17:56:54 +00:00
void key_press(XEvent *e) {
2019-10-11 11:48:34 +00:00
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 17:56:54 +00:00
void button_press(XEvent *e) {
2019-10-11 15:00:00 +00:00
XButtonEvent bu = e->xbutton;
if (bu.subwindow != None) {
XGetWindowAttributes(dis, bu.subwindow, &attr);
start = bu;
}
}
2019-10-11 17:56:54 +00:00
void motion_notify(XEvent *e) {
2019-10-11 15:00:00 +00:00
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)));
}
}
2019-10-11 17:56:54 +00:00
void button_release(XEvent *e) {
2019-10-11 15:00:00 +00:00
start.subwindow = None;
}
2019-10-11 17:33:03 +00:00
void win_kill() {
2019-10-11 18:19:40 +00:00
if(current != NULL)
XKillClient(dis, current->win);
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-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;
}
2019-10-11 17:33:03 +00:00
win_add(ev->window);
2019-10-11 18:19:40 +00:00
// win_center();
2019-10-11 11:48:34 +00:00
XMapWindow(dis,ev->window);
2019-10-11 17:56:54 +00:00
win_update();
2019-10-11 11:48:34 +00:00
}
2019-10-11 17:33:03 +00:00
void win_next() {
2019-10-11 11:48:34 +00:00
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 17:56:54 +00:00
win_update();
2019-10-11 11:48:34 +00:00
}
}
2019-10-11 17:33:03 +00:00
void win_del(Window w) {
2019-10-11 11:48:34 +00:00
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;
2019-10-11 17:56:54 +00:00
ws_save(curr_desk);
2019-10-11 16:51:06 +00:00
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);
2019-10-11 17:56:54 +00:00
ws_save(curr_desk);
win_update();
2019-10-11 15:52:07 +00:00
return;
}
2019-10-11 11:48:34 +00:00
}
}
2019-10-11 17:56:54 +00:00
void ws_save(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;
}
2019-10-11 17:56:54 +00:00
void ws_sel(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 17:56:54 +00:00
void wm_setup() {
2019-10-11 15:52:07 +00:00
int i;
2019-10-11 17:56:54 +00:00
sig_child(0);
2019-10-11 11:48:34 +00:00
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
2019-10-11 17:56:54 +00:00
key_grab();
2019-10-11 11:48:34 +00:00
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 17:56:54 +00:00
ws_go(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
}
2019-10-11 17:56:54 +00:00
void sig_child(int unused) {
if (signal(SIGCHLD, sig_child) == SIG_ERR)
2019-10-11 15:52:07 +00:00
exit(1);
2019-10-11 15:00:00 +00:00
2019-10-11 11:48:34 +00:00
while(0 < waitpid(-1, NULL, WNOHANG));
}
2019-10-11 17:35:56 +00:00
void run(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 17:56:54 +00:00
void wm_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
2019-10-11 17:56:54 +00:00
wm_setup();
wm_init();
2019-10-11 11:48:34 +00:00
2019-10-11 17:56:54 +00:00
XCloseDisplay(dis);
2019-10-11 11:48:34 +00:00
return 0;
}