[hackers] [dmenu] atexit cleanup || Connor Lane Smith

From: <hg_AT_suckless.org>
Date: Fri, 2 Jul 2010 02:44:18 +0000 (UTC)

changeset: 335:324c8e12115a
tag: tip
user: Connor Lane Smith <cls_AT_lubutu.com>
date: Fri Jul 02 03:44:01 2010 +0100
files: dinput.c dmenu.c
description:
atexit cleanup

diff -r 563ecedef589 -r 324c8e12115a dinput.c
--- a/dinput.c Wed Jun 30 22:42:15 2010 +0100
+++ b/dinput.c Fri Jul 02 03:44:01 2010 +0100
@@ -26,7 +26,7 @@
 static Bool grabkeyboard(void);
 static void kpress(XKeyEvent *e);
 static void run(void);
-static void setup(Bool topbar);
+static void setup(void);
 
 #include "config.h"
 
@@ -34,14 +34,13 @@
 static char *prompt = NULL;
 static char text[4096];
 static int promptw = 0;
-static int ret = 0;
 static int screen;
 static unsigned int cursor = 0;
 static unsigned int numlockmask = 0;
 static unsigned int mw, mh;
 static unsigned long normcol[ColLast];
 static unsigned long selcol[ColLast];
-static Bool running = True;
+static Bool topbar = True;
 static DC dc;
 static Display *dpy;
 static Window win, root;
@@ -51,6 +50,7 @@
         cleanupdraw(&dc);
         XDestroyWindow(dpy, win);
         XUngrabKeyboard(dpy, CurrentTime);
+ XCloseDisplay(dpy);
 }
 
 void
@@ -81,7 +81,6 @@
         drawtext(&dc, *text ? text : NULL, normcol, False);
         drawcursor();
         XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
- XFlush(dpy);
 }
 
 Bool
@@ -200,9 +199,7 @@
                 cursor = len;
                 break;
         case XK_Escape:
- ret = 1;
- running = False;
- return;
+ exit(EXIT_FAILURE);
         case XK_Home:
                 cursor = 0;
                 break;
@@ -214,8 +211,7 @@
         case XK_Return:
                 fprintf(stdout, "%s", text);
                 fflush(stdout);
- running = False;
- return;
+ exit(EXIT_SUCCESS);
         case XK_Right:
                 if(cursor == len)
                         return;
@@ -230,7 +226,8 @@
         XEvent ev;
 
         /* main event loop */
- while(running && !XNextEvent(dpy, &ev))
+ XSync(dpy, False);
+ while(!XNextEvent(dpy, &ev))
                 switch(ev.type) {
                 case KeyPress:
                         kpress(&ev.xkey);
@@ -240,14 +237,15 @@
                                 drawinput();
                         break;
                 case VisibilityNotify:
- if (ev.xvisibility.state != VisibilityUnobscured)
+ if(ev.xvisibility.state != VisibilityUnobscured)
                                 XRaiseWindow(dpy, win);
                         break;
                 }
+ exit(EXIT_FAILURE);
 }
 
 void
-setup(Bool topbar) {
+setup(void) {
         int i, j, x, y;
 #if XINERAMA
         int n;
@@ -320,7 +318,6 @@
 int
 main(int argc, char *argv[]) {
         unsigned int i;
- Bool topbar = True;
 
         /* command line args */
         progname = "dinput";
@@ -364,15 +361,13 @@
                 fprintf(stderr, "dinput: warning: no locale support\n");
         if(!(dpy = XOpenDisplay(NULL)))
                 eprint("cannot open display\n");
+ if(atexit(&cleanup) != 0)
+ eprint("cannot register cleanup\n");
         screen = DefaultScreen(dpy);
         root = RootWindow(dpy, screen);
 
- running = grabkeyboard();
- setup(topbar);
- drawinput();
- XSync(dpy, False);
+ grabkeyboard();
+ setup();
         run();
- cleanup();
- XCloseDisplay(dpy);
- return ret;
+ return 0;
 }
diff -r 563ecedef589 -r 324c8e12115a dmenu.c
--- a/dmenu.c Wed Jun 30 22:42:15 2010 +0100
+++ b/dmenu.c Fri Jul 02 03:44:01 2010 +0100
@@ -36,7 +36,7 @@
 static void drawmenu(void);
 static void drawmenuh(void);
 static void drawmenuv(void);
-static Bool grabkeyboard(void);
+static void grabkeyboard(void);
 static void kpress(XKeyEvent *e);
 static void match(char *pattern);
 static void readstdin(void);
@@ -52,14 +52,12 @@
 static char text[4096];
 static int cmdw = 0;
 static int promptw = 0;
-static int ret = 0;
 static int screen;
 static unsigned int lines = 0;
 static unsigned int numlockmask = 0;
 static unsigned int mw, mh;
 static unsigned long normcol[ColLast];
 static unsigned long selcol[ColLast];
-static Bool running = True;
 static Bool topbar = True;
 static DC dc;
 static Display *dpy;
@@ -87,15 +85,15 @@
 
 void
 calcoffsetsh(void) {
- unsigned int w;
+ unsigned int x;
 
- w = promptw + cmdw + (2 * spaceitem);
+ x = promptw + cmdw + (2 * spaceitem);
         for(next = curr; next; next = next->right)
- if((w += MIN(textw(&dc, next->text), mw / 3)) > mw)
+ if((x += MIN(textw(&dc, next->text), mw / 3)) > mw)
                         break;
- w = promptw + cmdw + (2 * spaceitem);
+ x = promptw + cmdw + (2 * spaceitem);
         for(prev = curr; prev && prev->left; prev = prev->left)
- if((w += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
+ if((x += MIN(textw(&dc, prev->left->text), mw / 3)) > mw)
                         break;
 }
 
@@ -146,6 +144,7 @@
         cleanupdraw(&dc);
         XDestroyWindow(dpy, win);
         XUngrabKeyboard(dpy, CurrentTime);
+ XCloseDisplay(dpy);
 }
 
 void
@@ -182,7 +181,6 @@
         else if(curr)
                 drawmenuh();
         XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, mw, mh, 0, 0);
- XFlush(dpy);
 }
 
 void
@@ -219,7 +217,7 @@
         XMoveResizeWindow(dpy, win, wa.x, wa.y + (topbar ? 0 : wa.height - mh), mw, mh);
 }
 
-Bool
+void
 grabkeyboard(void) {
         unsigned int len;
 
@@ -229,7 +227,8 @@
                         break;
                 usleep(1000);
         }
- return len > 0;
+ if(!len)
+ exit(EXIT_FAILURE);
 }
 
 void
@@ -326,9 +325,7 @@
                         sel = sel->right;
                 break;
         case XK_Escape:
- ret = 1;
- running = False;
- return;
+ exit(EXIT_FAILURE);
         case XK_Home:
                 sel = curr = item;
                 calcoffsets();
@@ -360,8 +357,7 @@
                         dinput();
                 fprintf(stdout, "%s", sel ? sel->text : text);
                 fflush(stdout);
- running = False;
- return;
+ exit(EXIT_SUCCESS);
         case XK_Right:
         case XK_Down:
                 if(!sel || !sel->right)
@@ -454,7 +450,8 @@
         XEvent ev;
 
         /* main event loop */
- while(running && !XNextEvent(dpy, &ev))
+ XSync(dpy, False);
+ while(!XNextEvent(dpy, &ev))
                 switch(ev.type) {
                 case KeyPress:
                         kpress(&ev.xkey);
@@ -464,10 +461,11 @@
                                 drawmenu();
                         break;
                 case VisibilityNotify:
- if (ev.xvisibility.state != VisibilityUnobscured)
+ if(ev.xvisibility.state != VisibilityUnobscured)
                                 XRaiseWindow(dpy, win);
                         break;
                 }
+ exit(EXIT_FAILURE);
 }
 
 void
@@ -586,13 +584,15 @@
                 }
                 else {
                         fputs("usage: dmenu [-i] [-b] [-l <lines>] [-fn <font>] [-nb <color>]\n"
- " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
+ " [-nf <color>] [-p <prompt>] [-sb <color>] [-sf <color>] [-v]\n", stderr);
                         exit(EXIT_FAILURE);
                 }
         if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
                 fprintf(stderr, "dmenu: warning: no locale support\n");
         if(!(dpy = XOpenDisplay(NULL)))
                 eprint("cannot open display\n");
+ if(atexit(&cleanup) != 0)
+ eprint("cannot register cleanup\n");
         screen = DefaultScreen(dpy);
         root = RootWindow(dpy, screen);
         if(!(argp = malloc(sizeof *argp * (argc+2))))
@@ -600,13 +600,8 @@
         memcpy(argp + 2, argv + 1, sizeof *argp * argc);
 
         readstdin();
- running = grabkeyboard();
-
+ grabkeyboard();
         setup();
- drawmenu();
- XSync(dpy, False);
         run();
- cleanup();
- XCloseDisplay(dpy);
- return ret;
+ return 0;
 }
Received on Fri Jul 02 2010 - 04:44:18 CEST

This archive was generated by hypermail 2.2.0 : Fri Jul 02 2010 - 04:48:04 CEST