]> git.armaanb.net Git - slock.git/blob - slock.c
added black window
[slock.git] / slock.c
1 /* (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
2  * See LICENSE file for license details.
3  */
4 #define _XOPEN_SOURCE
5 #include <shadow.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <X11/keysym.h>
12 #include <X11/Xlib.h>
13 #include <X11/Xutil.h>
14
15 int
16 main(int argc, char **argv) {
17         char buf[32], passwd[256];
18         int num, prev_nitem, screen;
19         struct spwd *sp;
20         unsigned int i, len;
21         Bool running = True;
22         KeySym ksym;
23         Display *dpy;
24         Window w;
25         XEvent ev;
26         XSetWindowAttributes wa;
27
28         if((argc > 1) && !strncmp(argv[1], "-v", 3)) {
29                 fputs("slock-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
30                 exit(EXIT_SUCCESS);
31         }
32         if(!(sp = getspnam(getenv("USER")))) {
33                 fputs("slock: cannot retrieve password entry (make sure to suid slock)\n", stderr);
34                 exit(EXIT_FAILURE);
35         }
36         endspent();
37         if(!(dpy = XOpenDisplay(0))) {
38                 fputs("slock: cannot open display\n", stderr);
39                 exit(EXIT_FAILURE);
40         }
41         screen = DefaultScreen(dpy);
42
43         /* init */
44         passwd[0] = 0;
45         while(XGrabKeyboard(dpy, RootWindow(dpy, screen), True, GrabModeAsync,
46                          GrabModeAsync, CurrentTime) != GrabSuccess)
47                 usleep(1000);
48
49         wa.override_redirect = 1;
50         wa.background_pixel = BlackPixel(dpy, screen);
51         w = XCreateWindow(dpy, RootWindow(dpy, screen), 0, 0,
52                         DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
53                         0, DefaultDepth(dpy, screen), CopyFromParent,
54                         DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
55         XMapRaised(dpy, w);
56         XSync(dpy, False);
57
58         /* main event loop */
59         while(running && !XNextEvent(dpy, &ev))
60                 if(ev.type == KeyPress) {
61                         len = strlen(passwd);
62                         buf[0] = 0;
63                         num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
64                         if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
65                                         || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
66                                         || IsPrivateKeypadKey(ksym))
67                                 continue;
68                         /* first check if a control mask is omitted */
69                         if(ev.xkey.state & ControlMask) {
70                                 switch (ksym) {
71                                 case XK_h:
72                                 case XK_H: ksym = XK_BackSpace;
73                                         break;
74                                 case XK_u:
75                                 case XK_U: passwd[0] = 0;
76                                         continue;
77                                 }
78                         }
79                         switch(ksym) {
80                         case XK_Return:
81                                 if((running = strncmp(crypt(passwd, sp->sp_pwdp), sp->sp_pwdp, sizeof(passwd))))
82                                         XBell(dpy, 100);
83                                 passwd[0] = 0;
84                                 break;
85                         case XK_Escape:
86                                 passwd[0] = 0;
87                                 break;
88                         case XK_BackSpace:
89                                 if(len)
90                                         passwd[--len] = 0;
91                                 break;
92                         default:
93                                 if(num && !iscntrl((int) buf[0])) {
94                                         buf[num] = 0;
95                                         if(len)
96                                                 strncat(passwd, buf, sizeof(passwd));
97                                         else
98                                                 strncpy(passwd, buf, sizeof(passwd));
99                                 }
100                                 break;
101                         }
102                 }
103         XDestroyWindow(dpy, w);
104         XCloseDisplay(dpy);
105         return 0;
106 }