Xinqi Bao's Git

applied Ali Gholami Rudi's patch regarding DPMS timeout customization and persistence
[slock.git] / slock.c
1 /* See LICENSE file for license details. */
2 #define _XOPEN_SOURCE 500
3 #if HAVE_SHADOW_H
4 #include <shadow.h>
5 #endif
6
7 #include <ctype.h>
8 #include <pwd.h>
9 #include <stdarg.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <X11/keysym.h>
16 #include <X11/Xlib.h>
17 #include <X11/Xutil.h>
18 #include <X11/extensions/dpms.h>
19
20 #if HAVE_BSD_AUTH
21 #include <login_cap.h>
22 #include <bsd_auth.h>
23 #endif
24
25 static void
26 die(const char *errstr, ...) {
27 va_list ap;
28
29 va_start(ap, errstr);
30 vfprintf(stderr, errstr, ap);
31 va_end(ap);
32 exit(EXIT_FAILURE);
33 }
34
35 #ifndef HAVE_BSD_AUTH
36 static const char *
37 get_password() { /* only run as root */
38 const char *rval;
39 struct passwd *pw;
40
41 if(geteuid() != 0)
42 die("slock: cannot retrieve password entry (make sure to suid slock)\n");
43 pw = getpwuid(getuid());
44 endpwent();
45 rval = pw->pw_passwd;
46
47 #if HAVE_SHADOW_H
48 {
49 struct spwd *sp;
50 sp = getspnam(getenv("USER"));
51 endspent();
52 rval = sp->sp_pwdp;
53 }
54 #endif
55
56 /* drop privileges */
57 if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0)
58 die("slock: cannot drop privileges\n");
59 return rval;
60 }
61 #endif
62
63 int
64 main(int argc, char **argv) {
65 char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
66 char buf[32], passwd[256];
67 int num, screen;
68
69 #ifndef HAVE_BSD_AUTH
70 const char *pws;
71 #endif
72 unsigned int len;
73 Bool running = True;
74 Cursor invisible;
75 Display *dpy;
76 KeySym ksym;
77 Pixmap pmap;
78 Window root, w;
79 XColor black, dummy;
80 XEvent ev;
81 XSetWindowAttributes wa;
82 CARD16 standby, suspend, off;
83
84 if((argc == 2) && !strcmp("-v", argv[1]))
85 die("slock-"VERSION", © 2006-2008 Anselm R Garbe\n");
86 else if(argc != 1)
87 die("usage: slock [-v]\n");
88
89 #ifndef HAVE_BSD_AUTH
90 pws = get_password();
91 #endif
92
93 if(!(dpy = XOpenDisplay(0)))
94 die("slock: cannot open display\n");
95 screen = DefaultScreen(dpy);
96 root = RootWindow(dpy, screen);
97
98 /* init */
99 wa.override_redirect = 1;
100 wa.background_pixel = BlackPixel(dpy, screen);
101 w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
102 0, DefaultDepth(dpy, screen), CopyFromParent,
103 DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
104 XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
105 pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
106 invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
107 XDefineCursor(dpy, w, invisible);
108 XMapRaised(dpy, w);
109 for(len = 1000; len; len--) {
110 if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
111 GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
112 break;
113 usleep(1000);
114 }
115 if((running = running && (len > 0))) {
116 for(len = 1000; len; len--) {
117 if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
118 == GrabSuccess)
119 break;
120 usleep(1000);
121 }
122 running = (len > 0);
123 }
124 len = 0;
125 XSync(dpy, False);
126
127 if(DPMSCapable(dpy)) { /* save and customize DPMS settings */
128 DPMSGetTimeouts(dpy, &standby, &suspend, &off);
129 DPMSSetTimeouts(dpy, 10, 30, 90);
130 }
131
132 /* main event loop */
133 while(running && !XNextEvent(dpy, &ev)) {
134 if(ev.type == KeyPress) {
135 buf[0] = 0;
136 num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
137 if(IsKeypadKey(ksym)) {
138 if(ksym == XK_KP_Enter)
139 ksym = XK_Return;
140 else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
141 ksym = (ksym - XK_KP_0) + XK_0;
142 }
143 if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
144 || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
145 || IsPrivateKeypadKey(ksym))
146 continue;
147 switch(ksym) {
148 case XK_Return:
149 passwd[len] = 0;
150 #ifdef HAVE_BSD_AUTH
151 running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
152 #else
153 running = strcmp(crypt(passwd, pws), pws);
154 #endif
155 if (running != 0)
156 XBell(dpy, 100);
157 len = 0;
158 break;
159 case XK_Escape:
160 len = 0;
161 break;
162 case XK_BackSpace:
163 if(len)
164 --len;
165 break;
166 default:
167 if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
168 memcpy(passwd + len, buf, num);
169 len += num;
170 }
171 break;
172 }
173 }
174 }
175 if(DPMSCapable(dpy)) { /* restore DPMS settings */
176 DPMSSetTimeouts(dpy, standby, suspend, off);
177 }
178 XUngrabPointer(dpy, CurrentTime);
179 XFreePixmap(dpy, pmap);
180 XDestroyWindow(dpy, w);
181 XCloseDisplay(dpy);
182 return 0;
183 }