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