Xinqi Bao's Git
1 /* See LICENSE file for license details. */
2 #define _XOPEN_SOURCE 500
14 #include <sys/types.h>
15 #include <X11/keysym.h>
17 #include <X11/Xutil.h>
18 #include <X11/extensions/dpms.h>
21 #include <login_cap.h>
26 die(const char *errstr
, ...) {
30 vfprintf(stderr
, errstr
, ap
);
37 get_password() { /* only run as root */
42 die("slock: cannot retrieve password entry (make sure to suid slock)\n");
43 pw
= getpwuid(getuid());
50 sp
= getspnam(getenv("USER"));
57 if(setgid(pw
->pw_gid
) < 0 || setuid(pw
->pw_uid
) < 0)
58 die("slock: cannot drop privileges\n");
64 main(int argc
, char **argv
) {
65 char curs
[] = {0, 0, 0, 0, 0, 0, 0, 0};
66 char buf
[32], passwd
[256];
81 XSetWindowAttributes wa
;
83 if((argc
== 2) && !strcmp("-v", argv
[1]))
84 die("slock-"VERSION
", © 2006-2008 Anselm R Garbe\n");
86 die("usage: slock [-v]\n");
92 if(!(dpy
= XOpenDisplay(0)))
93 die("slock: cannot open display\n");
94 screen
= DefaultScreen(dpy
);
95 root
= RootWindow(dpy
, screen
);
98 wa
.override_redirect
= 1;
99 wa
.background_pixel
= BlackPixel(dpy
, screen
);
100 w
= XCreateWindow(dpy
, root
, 0, 0, DisplayWidth(dpy
, screen
), DisplayHeight(dpy
, screen
),
101 0, DefaultDepth(dpy
, screen
), CopyFromParent
,
102 DefaultVisual(dpy
, screen
), CWOverrideRedirect
| CWBackPixel
, &wa
);
103 XAllocNamedColor(dpy
, DefaultColormap(dpy
, screen
), "black", &black
, &dummy
);
104 pmap
= XCreateBitmapFromData(dpy
, w
, curs
, 8, 8);
105 invisible
= XCreatePixmapCursor(dpy
, pmap
, pmap
, &black
, &black
, 0, 0);
106 XDefineCursor(dpy
, w
, invisible
);
108 for(len
= 1000; len
; len
--) {
109 if(XGrabPointer(dpy
, root
, False
, ButtonPressMask
| ButtonReleaseMask
| PointerMotionMask
,
110 GrabModeAsync
, GrabModeAsync
, None
, invisible
, CurrentTime
) == GrabSuccess
)
114 if((running
= running
&& (len
> 0))) {
115 for(len
= 1000; len
; len
--) {
116 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
126 /* main event loop */
127 while(running
&& !XNextEvent(dpy
, &ev
)) {
128 if(len
== 0 && DPMSCapable(dpy
)) {
130 DPMSForceLevel(dpy
, DPMSModeOff
);
132 if(ev
.type
== KeyPress
) {
134 num
= XLookupString(&ev
.xkey
, buf
, sizeof buf
, &ksym
, 0);
135 if(IsKeypadKey(ksym
)) {
136 if(ksym
== XK_KP_Enter
)
138 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
139 ksym
= (ksym
- XK_KP_0
) + XK_0
;
141 if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
142 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
143 || IsPrivateKeypadKey(ksym
))
149 running
= !auth_userokay(getlogin(), NULL
, "auth-xlock", passwd
);
151 running
= strcmp(crypt(passwd
, pws
), pws
);
165 if(num
&& !iscntrl((int) buf
[0]) && (len
+ num
< sizeof passwd
)) {
166 memcpy(passwd
+ len
, buf
, num
);
173 XUngrabPointer(dpy
, CurrentTime
);
174 XFreePixmap(dpy
, pmap
);
175 XDestroyWindow(dpy
, w
);