X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=slock.c;h=83d426aaccb9a469dd79587a6b3a9d889903db58;hb=03a87179919eebab7d38c548e3ff8e2911512468;hp=97c7489344d9ea78417296f7f0b664391b00501a;hpb=e378f735d857f7da124177e3540912d920be5022;p=slock.git diff --git a/slock.c b/slock.c index 97c7489..83d426a 100644 --- a/slock.c +++ b/slock.c @@ -46,8 +46,6 @@ typedef struct { static Lock **locks; static int nscreens; -static Bool running = True; -static Bool failure = False; static Bool rr; static int rrevbase; static int rrerrbase; @@ -132,14 +130,15 @@ readpw(Display *dpy, const char *pws) #endif { char buf[32], passwd[256], *encrypted; - int num, screen; + int num, screen, running, failure; unsigned int len, color; KeySym ksym; XEvent ev; static int oldc = INIT; len = 0; - running = True; + running = 1; + failure = 0; /* As "slock" stands for "Simple X display locker", the DPMS settings * had been removed and you can set it with "xset" or some other @@ -234,6 +233,11 @@ unlockscreen(Display *dpy, Lock *lock) static void cleanup(Display *dpy) { + int s; + + for (s = 0; s < nscreens; ++s) + unlockscreen(dpy, locks[s]); + free(locks); XCloseDisplay(dpy); } @@ -248,7 +252,7 @@ lockscreen(Display *dpy, int screen) XSetWindowAttributes wa; Cursor invisible; - if (!running || dpy == NULL || screen < 0 || !(lock = malloc(sizeof(Lock)))) + if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(Lock)))) return NULL; lock->screen = screen; @@ -305,8 +309,6 @@ lockscreen(Display *dpy, int screen) fprintf(stderr, "slock: unable to grab mouse pointer for screen %d\n", screen); if (kbgrab != GrabSuccess) fprintf(stderr, "slock: unable to grab keyboard for screen %d\n", screen); - running = 0; - unlockscreen(dpy, lock); return NULL; } @@ -359,19 +361,20 @@ main(int argc, char **argv) { /* get number of screens in display "dpy" and blank them */ nscreens = ScreenCount(dpy); - if (!(locks = malloc(sizeof(Lock *) * nscreens))) { + if (!(locks = calloc(nscreens, sizeof(Lock *)))) { XCloseDisplay(dpy); die("slock: out of memory\n"); } for (nlocks = 0, s = 0; s < nscreens; s++) { if ((locks[s] = lockscreen(dpy, s)) != NULL) nlocks++; + else + break; } XSync(dpy, 0); - /* did we actually manage to lock anything? */ - if (nlocks == 0) { - /* nothing to protect */ + /* did we manage to lock everything? */ + if (nlocks != nscreens) { cleanup(dpy); return 1; } @@ -400,9 +403,6 @@ main(int argc, char **argv) { #endif /* password ok, unlock everything and quit */ - for (s = 0; s < nscreens; s++) - unlockscreen(dpy, locks[s]); - cleanup(dpy); return 0;