From bd2b1b83b0d6d306c502ee98c5a41c4481516ee3 Mon Sep 17 00:00:00 2001 From: Martin Duquesnoy Date: Sun, 9 Nov 2008 04:58:10 +0100 Subject: [PATCH] wmfs: Add signal handler for SIGTERM and SIGINT --- src/client.c | 1 - src/event.c | 21 ++++++++++---------- src/wmfs.c | 54 +++++++++++++++++++++++++++++++++++++++++++++------- src/wmfs.h | 2 ++ 4 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/client.c b/src/client.c index 2634bc9..c70223a 100644 --- a/src/client.c +++ b/src/client.c @@ -468,7 +468,6 @@ client_unmanage(Client *c) * and set the withdraw state */ client_detach(c); setwinstate(c->win, WithdrawnState); - XDestroySubwindows(dpy, c->frame); XDestroyWindow(dpy, c->frame); XFree(c->title); diff --git a/src/event.c b/src/event.c index fc9d8b3..47a150a 100644 --- a/src/event.c +++ b/src/event.c @@ -306,7 +306,6 @@ propertynotify(XPropertyEvent *ev) return; } - void unmapnotify(XUnmapEvent *ev) { @@ -328,17 +327,17 @@ getevent(XEvent ev) { switch (ev.type) { - case ButtonPress: buttonpress(&ev.xbutton); break; + case ButtonPress: buttonpress(&ev.xbutton); break; case ConfigureRequest: configurerequest(&ev.xconfigurerequest); break; - case DestroyNotify: destroynotify(&ev.xdestroywindow); break; - case EnterNotify: enternotify(&ev.xcrossing); break; - case Expose: expose(&ev.xexpose); break; - case FocusIn: focusin(&ev.xfocus); break; - case KeyPress: keypress(&ev.xkey); break; - case MapRequest: maprequest(&ev.xmaprequest); break; - case MappingNotify: mapnotify(&ev.xmapping); break; - case PropertyNotify: propertynotify(&ev.xproperty); break; - case UnmapNotify: unmapnotify(&ev.xunmap); break; + case DestroyNotify: destroynotify(&ev.xdestroywindow); break; + case EnterNotify: enternotify(&ev.xcrossing); break; + case Expose: expose(&ev.xexpose); break; + case FocusIn: focusin(&ev.xfocus); break; + case KeyPress: keypress(&ev.xkey); break; + case MapRequest: maprequest(&ev.xmaprequest); break; + case MappingNotify: mapnotify(&ev.xmapping); break; + case PropertyNotify: propertynotify(&ev.xproperty); break; + case UnmapNotify: unmapnotify(&ev.xunmap); break; } return; diff --git a/src/wmfs.c b/src/wmfs.c index b27c537..8c3c99a 100644 --- a/src/wmfs.c +++ b/src/wmfs.c @@ -158,15 +158,24 @@ scan(void) XWindowAttributes wa; if(XQueryTree(dpy, root, &d, &d, &wins, &num)) + { for(i = 0; i < num; i++) { - if(wins[i] && wins[i] != infobar->bar->win) - { - XGetWindowAttributes(dpy, wins[i], &wa); - if(wa.override_redirect && wa.map_state == IsViewable) - client_manage(wins[i], &wa); - } + if(!XGetWindowAttributes(dpy, wins[i], &wa) + || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d)) + continue; + if(wa.map_state == IsViewable || getwinstate(wins[i]) == IconicState) + client_manage(wins[i], &wa); } + for(i = 0; i < num; i++) + { + if(!XGetWindowAttributes(dpy, wins[i], &wa)) + continue; + if(XGetTransientForHint(dpy, wins[i], &d) + && (wa.map_state == IsViewable || getwinstate(wins[i]) == IconicState)) + client_manage(wins[i], &wa); + } + } XFree(wins); arrange(); @@ -174,10 +183,33 @@ scan(void) return; } +void +handle_signal(int signum) +{ + Client *c; + + if(signum == SIGTERM || signum == SIGINT) + { + XSetErrorHandler(errorhandlerdummy); + for(c = clients; c; c = c->next) + { + XReparentWindow(dpy, c->win, root, 0, 0); + XDestroySubwindows(dpy, c->frame); + XDestroyWindow(dpy, c->frame); + } + fprintf(stderr, "\nExit WMFS... Bye !!\n"); + quit(); + exit(EXIT_FAILURE); + } + + return; +} + int main(int argc, char **argv) { int i; + struct sigaction sig; static struct option long_options[] = { @@ -222,6 +254,14 @@ main(int argc, char **argv) exit(EXIT_FAILURE); } + /* Set signal handle */ + sig.sa_handler = handle_signal; + sig.sa_flags = 0; + memset(&sig.sa_mask, 0, sizeof(sigset_t)); + sigaction(SIGTERM, &sig, NULL); + sigaction(SIGINT, &sig, NULL); + + /* Check if an other WM is already running; set the error handler */ XSetErrorHandler(errorhandler); XSetErrorHandler(errorhandlerdummy); @@ -231,7 +271,7 @@ main(int argc, char **argv) init(); scan(); mainloop(); - quit(); + raise(SIGTERM); XCloseDisplay(dpy); diff --git a/src/wmfs.h b/src/wmfs.h index d9cc801..5e9eae0 100644 --- a/src/wmfs.h +++ b/src/wmfs.h @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -205,6 +206,7 @@ int errorhandlerstart(Display *d, XErrorEvent *event); void quit(void); void mainloop(void); void scan(void); +void handle_signal(int signum); void uicb_quit(uicb_t); /* Variables */