catwm: spring cleaning

This commit is contained in:
Dylan Araps 2019-10-11 18:52:07 +03:00
parent 445d61a269
commit 18b908fe34

120
catwm.c
View File

@ -4,29 +4,8 @@
* ( =^= ) * ( =^= )
* ( ) ... for cat! * ( ) ... for cat!
* ( ) * ( )
* ( ))))))________________ Cute And Tiny Window Manager * ( ))))))________________
* ______________________________________________________________________________ * __________________
*
* Copyright (c) 2010, Rinaldini Julien, julien.rinaldini@heig-vd.ch
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/ */
#include <X11/Xlib.h> #include <X11/Xlib.h>
@ -47,7 +26,6 @@ typedef union {
const int i; const int i;
} Arg; } Arg;
// Structs
struct key { struct key {
unsigned int mod; unsigned int mod;
KeySym keysym; KeySym keysym;
@ -70,7 +48,6 @@ struct desktop{
client *current; client *current;
}; };
// Functions
static void add_window(Window w); static void add_window(Window w);
static void buttonpress(XEvent *e); static void buttonpress(XEvent *e);
static void buttonrelease(XEvent *e); static void buttonrelease(XEvent *e);
@ -78,8 +55,8 @@ static void change_desktop(const Arg arg);
static void client_to_desktop(const Arg arg); static void client_to_desktop(const Arg arg);
static void configurerequest(XEvent *e); static void configurerequest(XEvent *e);
static void destroynotify(XEvent *e); static void destroynotify(XEvent *e);
static void die(const char* e);
static void grabkeys(); static void grabkeys();
static void init();
static void keypress(XEvent *e); static void keypress(XEvent *e);
static void kill_client(); static void kill_client();
static void maprequest(XEvent *e); static void maprequest(XEvent *e);
@ -92,27 +69,24 @@ static void send_kill_signal(Window w);
static void setup(); static void setup();
static void sigchld(int unused); static void sigchld(int unused);
static void spawn(const Arg arg); static void spawn(const Arg arg);
static void init();
static void update_current(); static void update_current();
// Include configuration file (need struct key)
#include "config.h" #include "config.h"
// Variable
static Display *dis;
static int bool_quit; static int bool_quit;
static int current_desktop; static int curr_desk;
static int mode; static int mode;
static int screen;
static int sh; static int sh;
static int sw; static int sw;
static int screen; static Display *dis;
static Window root; static Window root;
static client *head;
static client *current;
static XWindowAttributes attr;
static XButtonEvent start; static XButtonEvent start;
static XWindowAttributes attr;
static client *current;
static client *head;
static desktop desktops[6];
// Events array
static void (*events[LASTEvent])(XEvent *e) = { static void (*events[LASTEvent])(XEvent *e) = {
[KeyPress] = keypress, [KeyPress] = keypress,
[MapRequest] = maprequest, [MapRequest] = maprequest,
@ -123,14 +97,11 @@ static void (*events[LASTEvent])(XEvent *e) = {
[MotionNotify] = motionnotify [MotionNotify] = motionnotify
}; };
// Desktop array
static desktop desktops[10];
void add_window(Window w) { void add_window(Window w) {
client *c, *t; client *c, *t;
if (!(c = (client *)calloc(1,sizeof(client)))) if (!(c = (client *)calloc(1,sizeof(client))))
die("Error calloc!"); exit(1);
if (head == NULL) { if (head == NULL) {
c->next = NULL; c->next = NULL;
@ -138,13 +109,13 @@ void add_window(Window w) {
c->win = w; c->win = w;
head = c; head = c;
} }
else { else {
for(t=head;t->next;t=t->next); for(t=head;t->next;t=t->next);
c->next = NULL; c->next = NULL;
c->prev = t; c->prev = t;
c->win = w; c->win = w;
t->next = c; t->next = c;
} }
@ -154,33 +125,26 @@ void add_window(Window w) {
void change_desktop(const Arg arg) { void change_desktop(const Arg arg) {
client *c; client *c;
if(arg.i == current_desktop) if (arg.i == curr_desk)
return; return;
// Unmap all window
if (head != NULL) if (head != NULL)
for(c=head;c;c=c->next) for(c=head;c;c=c->next) XUnmapWindow(dis,c->win);
XUnmapWindow(dis,c->win);
// Save current "properties" save_desktop(curr_desk);
save_desktop(current_desktop);
// Take "properties" from the new desktop
select_desktop(arg.i); select_desktop(arg.i);
// Map all windows
if (head != NULL) if (head != NULL)
for(c=head;c;c=c->next) for(c=head;c;c=c->next) XMapWindow(dis,c->win);
XMapWindow(dis,c->win);
update_current(); update_current();
} }
void client_to_desktop(const Arg arg) { void client_to_desktop(const Arg arg) {
client *tmp = current; client *tmp = current;
int tmp2 = current_desktop; int tmp2 = curr_desk;
if (arg.i == current_desktop || current == NULL) if (arg.i == tmp2 || current == NULL)
return; return;
// Add client to desktop // Add client to desktop
@ -196,9 +160,9 @@ void client_to_desktop(const Arg arg) {
} }
void configurerequest(XEvent *e) { void configurerequest(XEvent *e) {
// Paste from DWM, thx again \o/
XConfigureRequestEvent *ev = &e->xconfigurerequest; XConfigureRequestEvent *ev = &e->xconfigurerequest;
XWindowChanges wc; XWindowChanges wc;
wc.x = ev->x; wc.x = ev->x;
wc.y = ev->y; wc.y = ev->y;
wc.width = ev->width; wc.width = ev->width;
@ -206,20 +170,19 @@ void configurerequest(XEvent *e) {
wc.border_width = ev->border_width; wc.border_width = ev->border_width;
wc.sibling = ev->above; wc.sibling = ev->above;
wc.stack_mode = ev->detail; wc.stack_mode = ev->detail;
XConfigureWindow(dis, ev->window, ev->value_mask, &wc); XConfigureWindow(dis, ev->window, ev->value_mask, &wc);
} }
void destroynotify(XEvent *e) { void destroynotify(XEvent *e) {
int i = 0; int i = 0;
client *c; client *c;
XDestroyWindowEvent *ev = &e->xdestroywindow; XDestroyWindowEvent *ev = &e->xdestroywindow;
// Uber (and ugly) hack ;)
for(c=head;c;c=c->next) for(c=head;c;c=c->next)
if(ev->window == c->win) if(ev->window == c->win) i++;
i++;
// End of the hack
if(i == 0) if(i == 0)
return; return;
@ -238,20 +201,14 @@ void update_current() {
} }
} }
void die(const char* e) {
fprintf(stdout,"catwm: %s\n",e);
exit(1);
}
void grabkeys() { void grabkeys() {
int i; int i;
KeyCode code; KeyCode code;
// For each shortcuts
for(i=0;i<TABLENGTH(keys);++i) { for(i=0;i<TABLENGTH(keys);++i) {
if((code = XKeysymToKeycode(dis,keys[i].keysym))) { if ((code = XKeysymToKeycode(dis, keys[i].keysym)))
XGrabKey(dis,code,keys[i].mod,root,True,GrabModeAsync,GrabModeAsync); XGrabKey(dis, code, keys[i].mod, root,
} True, GrabModeAsync, GrabModeAsync);
} }
} }
@ -299,12 +256,14 @@ void kill_client() {
if(current != NULL) { if(current != NULL) {
//send delete signal to window //send delete signal to window
XEvent ke; XEvent ke;
ke.type = ClientMessage; ke.type = ClientMessage;
ke.xclient.window = current->win; ke.xclient.window = current->win;
ke.xclient.message_type = XInternAtom(dis, "WM_PROTOCOLS", True); ke.xclient.message_type = XInternAtom(dis, "WM_PROTOCOLS", True);
ke.xclient.format = 32; ke.xclient.format = 32;
ke.xclient.data.l[0] = XInternAtom(dis, "WM_DELETE_WINDOW", True); ke.xclient.data.l[0] = XInternAtom(dis, "WM_DELETE_WINDOW", True);
ke.xclient.data.l[1] = CurrentTime; ke.xclient.data.l[1] = CurrentTime;
XSendEvent(dis, current->win, False, NoEventMask, &ke); XSendEvent(dis, current->win, False, NoEventMask, &ke);
send_kill_signal(current->win); send_kill_signal(current->win);
} }
@ -312,9 +271,9 @@ void kill_client() {
void maprequest(XEvent *e) { void maprequest(XEvent *e) {
XMapRequestEvent *ev = &e->xmaprequest; XMapRequestEvent *ev = &e->xmaprequest;
client *c;
// For fullscreen mplayer (and maybe some other program) // For fullscreen mplayer (and maybe some other program)
client *c;
for(c=head;c;c=c->next) for(c=head;c;c=c->next)
if(ev->window == c->win) { if(ev->window == c->win) {
XMapWindow(dis,ev->window); XMapWindow(dis,ev->window);
@ -343,14 +302,16 @@ void next_win() {
void remove_window(Window w) { void remove_window(Window w) {
client *c; client *c;
// CHANGE THIS UGLY CODE
for(c=head;c;c=c->next) { for(c=head;c;c=c->next) {
if(c->win != w)
return;
if(c->win == w) {
if (c->prev == NULL && c->next == NULL) { if (c->prev == NULL && c->next == NULL) {
free(head); free(head);
head = NULL; head = NULL;
current = NULL; current = NULL;
return; return;
} }
@ -359,10 +320,12 @@ void remove_window(Window w) {
c->next->prev = NULL; c->next->prev = NULL;
current = c->next; current = c->next;
} }
else if (c->next == NULL) { else if (c->next == NULL) {
c->prev->next = NULL; c->prev->next = NULL;
current = c->prev; current = c->prev;
} }
else { else {
c->prev->next = c->next; c->prev->next = c->next;
c->next->prev = c->prev; c->next->prev = c->prev;
@ -373,7 +336,6 @@ void remove_window(Window w) {
return; return;
} }
} }
}
void save_desktop(int i) { void save_desktop(int i) {
desktops[i].mode = mode; desktops[i].mode = mode;
@ -385,21 +347,25 @@ void select_desktop(int i) {
head = desktops[i].head; head = desktops[i].head;
current = desktops[i].current; current = desktops[i].current;
mode = desktops[i].mode; mode = desktops[i].mode;
current_desktop = i; curr_desk = i;
} }
void send_kill_signal(Window w) { void send_kill_signal(Window w) {
XEvent ke; XEvent ke;
ke.type = ClientMessage; ke.type = ClientMessage;
ke.xclient.window = w; ke.xclient.window = w;
ke.xclient.message_type = XInternAtom(dis, "WM_PROTOCOLS", True); ke.xclient.message_type = XInternAtom(dis, "WM_PROTOCOLS", True);
ke.xclient.format = 32; ke.xclient.format = 32;
ke.xclient.data.l[0] = XInternAtom(dis, "WM_DELETE_WINDOW", True); ke.xclient.data.l[0] = XInternAtom(dis, "WM_DELETE_WINDOW", True);
ke.xclient.data.l[1] = CurrentTime; ke.xclient.data.l[1] = CurrentTime;
XSendEvent(dis, w, False, NoEventMask, &ke); XSendEvent(dis, w, False, NoEventMask, &ke);
} }
void setup() { void setup() {
int i;
sigchld(0); sigchld(0);
screen = DefaultScreen(dis); screen = DefaultScreen(dis);
@ -414,7 +380,6 @@ void setup() {
head = NULL; head = NULL;
current = NULL; current = NULL;
int i;
for(i=0;i<TABLENGTH(desktops);++i) { for(i=0;i<TABLENGTH(desktops);++i) {
desktops[i].mode = mode; desktops[i].mode = mode;
desktops[i].head = head; desktops[i].head = head;
@ -422,7 +387,7 @@ void setup() {
} }
const Arg arg = {.i = 1}; const Arg arg = {.i = 1};
current_desktop = arg.i; curr_desk = arg.i;
change_desktop(arg); change_desktop(arg);
XSelectInput(dis, root, SubstructureNotifyMask|SubstructureRedirectMask); XSelectInput(dis, root, SubstructureNotifyMask|SubstructureRedirectMask);
@ -430,7 +395,7 @@ void setup() {
void sigchld(int unused) { void sigchld(int unused) {
if (signal(SIGCHLD, sigchld) == SIG_ERR) if (signal(SIGCHLD, sigchld) == SIG_ERR)
die("Can't install SIGCHLD handler"); exit(1);
while(0 < waitpid(-1, NULL, WNOHANG)); while(0 < waitpid(-1, NULL, WNOHANG));
} }
@ -443,6 +408,7 @@ void spawn(const Arg arg) {
setsid(); setsid();
execvp((char*)arg.com[0],(char**)arg.com); execvp((char*)arg.com[0],(char**)arg.com);
} }
exit(0); exit(0);
} }
} }
@ -467,7 +433,7 @@ void init() {
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (!(dis = XOpenDisplay(NULL))) if (!(dis = XOpenDisplay(NULL)))
die("Cannot open display!"); exit(1);
setup(); setup();
init(); init();