|
|
@ -17,149 +17,147 @@ static char * brightnessfile = "/sys/devices/platform/backlight/backlight/backli |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void updatepineled(int red, int brightness) { |
|
|
|
void updatepineled(int red, int brightness) { |
|
|
|
sprintf( |
|
|
|
sprintf( |
|
|
|
pineledcommand,
|
|
|
|
pineledcommand,
|
|
|
|
"sh -c 'echo %d > /sys/class/leds/%s:indicator/brightness'", |
|
|
|
"sh -c 'echo %d > /sys/class/leds/%s:indicator/brightness'", |
|
|
|
brightness,
|
|
|
|
brightness,
|
|
|
|
red ? "red" : "blue" |
|
|
|
red ? "red" : "blue" |
|
|
|
); |
|
|
|
); |
|
|
|
system(pineledcommand); |
|
|
|
system(pineledcommand); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void updatescreenon(int on) { |
|
|
|
void updatescreenon(int on) { |
|
|
|
int b = on ? oldbrightness : 0; |
|
|
|
int b = on ? oldbrightness : 0; |
|
|
|
sprintf(screentogglecommand, "sh -c 'echo %d > %s'", b, brightnessfile); |
|
|
|
sprintf(screentogglecommand, "sh -c 'echo %d > %s'", b, brightnessfile); |
|
|
|
system(screentogglecommand); |
|
|
|
system(screentogglecommand); |
|
|
|
updatepineled(0, b ? 1 : 0); |
|
|
|
updatepineled(0, b ? 1 : 0); |
|
|
|
updatepineled(1, b ? 0 : 1); |
|
|
|
updatepineled(1, b ? 0 : 1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void cleanup() { |
|
|
|
void cleanup() { |
|
|
|
updatescreenon(1); |
|
|
|
updatescreenon(1); |
|
|
|
updatepineled(1, 0); |
|
|
|
updatepineled(1, 0); |
|
|
|
updatepineled(0, 0); |
|
|
|
updatepineled(0, 0); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void die(const char *err, ...) { |
|
|
|
static void die(const char *err, ...) { |
|
|
|
fprintf(stderr, "Error: %s", err); |
|
|
|
fprintf(stderr, "Error: %s", err); |
|
|
|
cleanup(); |
|
|
|
cleanup(); |
|
|
|
exit(1); |
|
|
|
exit(1); |
|
|
|
} |
|
|
|
} |
|
|
|
static void usage(void) { |
|
|
|
static void usage(void) { |
|
|
|
die("usage: slock [-v] [cmd [arg ...]]\n"); |
|
|
|
die("usage: slock [-v] [cmd [arg ...]]\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Loosely derived from suckless' slock's lockscreen binding logic but
|
|
|
|
// Loosely derived from suckless' slock's lockscreen binding logic but
|
|
|
|
// alot more coarse, intentionally so can be triggered while grab_key
|
|
|
|
// alot more coarse, intentionally so can be triggered while grab_key
|
|
|
|
// for dwm multikey path already holding..
|
|
|
|
// for dwm multikey path already holding..
|
|
|
|
void lockscreen(Display *dpy, int screen) { |
|
|
|
void lockscreen(Display *dpy, int screen) { |
|
|
|
int i, ptgrab, kbgrab; |
|
|
|
int i, ptgrab, kbgrab; |
|
|
|
//XSetWindowAttributes wa;
|
|
|
|
//XSetWindowAttributes wa;
|
|
|
|
Window root; |
|
|
|
Window root; |
|
|
|
//win,
|
|
|
|
//win,
|
|
|
|
root = RootWindow(dpy, screen); |
|
|
|
root = RootWindow(dpy, screen); |
|
|
|
//wa.override_redirect = 1;
|
|
|
|
//wa.override_redirect = 1;
|
|
|
|
//win = XCreateWindow(dpy, root, 0, 0,
|
|
|
|
//win = XCreateWindow(dpy, root, 0, 0,
|
|
|
|
// DisplayWidth(dpy, screen),
|
|
|
|
// DisplayWidth(dpy, screen),
|
|
|
|
// DisplayHeight(dpy, screen),
|
|
|
|
// DisplayHeight(dpy, screen),
|
|
|
|
// 0, DefaultDepth(dpy, screen),
|
|
|
|
// 0, DefaultDepth(dpy, screen),
|
|
|
|
// CopyFromParent,
|
|
|
|
// CopyFromParent,
|
|
|
|
// DefaultVisual(dpy, screen),
|
|
|
|
// DefaultVisual(dpy, screen),
|
|
|
|
// CWOverrideRedirect | CWBackPixel, &wa);
|
|
|
|
// CWOverrideRedirect | CWBackPixel, &wa);
|
|
|
|
for (i = 0, ptgrab = kbgrab = -1; i < 9999999; i++) { |
|
|
|
for (i = 0, ptgrab = kbgrab = -1; i < 9999999; i++) { |
|
|
|
if (ptgrab != GrabSuccess) { |
|
|
|
if (ptgrab != GrabSuccess) { |
|
|
|
ptgrab = XGrabPointer(dpy, root, False, |
|
|
|
ptgrab = XGrabPointer(dpy, root, False, |
|
|
|
ButtonPressMask | ButtonReleaseMask | |
|
|
|
ButtonPressMask | ButtonReleaseMask | |
|
|
|
PointerMotionMask, GrabModeAsync, |
|
|
|
PointerMotionMask, GrabModeAsync, |
|
|
|
GrabModeAsync, None, None, CurrentTime); |
|
|
|
GrabModeAsync, None, None, CurrentTime); |
|
|
|
} |
|
|
|
} |
|
|
|
if (kbgrab != GrabSuccess) { |
|
|
|
if (kbgrab != GrabSuccess) { |
|
|
|
kbgrab = XGrabKeyboard(dpy, root, True, |
|
|
|
kbgrab = XGrabKeyboard(dpy, root, True, |
|
|
|
GrabModeAsync, GrabModeAsync, CurrentTime); |
|
|
|
GrabModeAsync, GrabModeAsync, CurrentTime); |
|
|
|
} |
|
|
|
} |
|
|
|
if (ptgrab == GrabSuccess && kbgrab == GrabSuccess) { |
|
|
|
if (ptgrab == GrabSuccess && kbgrab == GrabSuccess) { |
|
|
|
XSelectInput(dpy, root, SubstructureNotifyMask); |
|
|
|
XSelectInput(dpy, root, SubstructureNotifyMask); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
usleep(100000); |
|
|
|
usleep(100000); |
|
|
|
} |
|
|
|
} |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
readinputloop(Display *dpy, int screen) { |
|
|
|
readinputloop(Display *dpy, int screen) { |
|
|
|
KeySym keysym; |
|
|
|
KeySym keysym; |
|
|
|
XEvent ev; |
|
|
|
XEvent ev; |
|
|
|
char buf[32]; |
|
|
|
char buf[32]; |
|
|
|
|
|
|
|
|
|
|
|
while (running && !XNextEvent(dpy, &ev)) { |
|
|
|
while (running && !XNextEvent(dpy, &ev)) { |
|
|
|
if (ev.type == KeyPress) { |
|
|
|
if (ev.type == KeyPress) { |
|
|
|
XLookupString(&ev.xkey, buf, sizeof(buf), &keysym, 0); |
|
|
|
XLookupString(&ev.xkey, buf, sizeof(buf), &keysym, 0); |
|
|
|
if (lastkeysym == keysym) { |
|
|
|
if (lastkeysym == keysym) { |
|
|
|
lastkeyn++; |
|
|
|
lastkeyn++; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
lastkeysym = keysym; |
|
|
|
lastkeysym = keysym; |
|
|
|
lastkeyn = 1; |
|
|
|
lastkeyn = 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (lastkeyn < 3) |
|
|
|
if (lastkeyn < 3) |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
lastkeyn = 0; |
|
|
|
lastkeyn = 0; |
|
|
|
lastkeysym = NULL; |
|
|
|
lastkeysym = NULL; |
|
|
|
|
|
|
|
|
|
|
|
switch (keysym) { |
|
|
|
switch (keysym) { |
|
|
|
case XF86XK_AudioRaiseVolume: |
|
|
|
case XF86XK_AudioRaiseVolume: |
|
|
|
case XF86XK_AudioLowerVolume: |
|
|
|
case XF86XK_AudioLowerVolume: |
|
|
|
screenon = !screenon; |
|
|
|
screenon = !screenon; |
|
|
|
updatescreenon(screenon); |
|
|
|
updatescreenon(screenon); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case XF86XK_PowerOff: |
|
|
|
case XF86XK_PowerOff: |
|
|
|
cleanup(); |
|
|
|
cleanup(); |
|
|
|
running = 0; |
|
|
|
running = 0; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
|
int |
|
|
|
getoldbrightness() { |
|
|
|
getoldbrightness() { |
|
|
|
char * buffer = 0; |
|
|
|
char * buffer = 0; |
|
|
|
long length; |
|
|
|
long length; |
|
|
|
FILE * f = fopen(brightnessfile, "rb"); |
|
|
|
FILE * f = fopen(brightnessfile, "rb"); |
|
|
|
if (f) { |
|
|
|
if (f) { |
|
|
|
fseek(f, 0, SEEK_END); |
|
|
|
fseek(f, 0, SEEK_END); |
|
|
|
length = ftell(f); |
|
|
|
length = ftell(f); |
|
|
|
fseek(f, 0, SEEK_SET); |
|
|
|
fseek(f, 0, SEEK_SET); |
|
|
|
buffer = malloc(length); |
|
|
|
buffer = malloc(length); |
|
|
|
if (buffer) { |
|
|
|
if (buffer) { |
|
|
|
fread(buffer, 1, length, f); |
|
|
|
fread(buffer, 1, length, f); |
|
|
|
} |
|
|
|
} |
|
|
|
fclose(f); |
|
|
|
fclose(f); |
|
|
|
} |
|
|
|
} |
|
|
|
if (buffer) { |
|
|
|
if (buffer) { |
|
|
|
oldbrightness = atoi(buffer); |
|
|
|
oldbrightness = atoi(buffer); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
|
int |
|
|
|
main(int argc, char **argv) { |
|
|
|
main(int argc, char **argv) { |
|
|
|
Display *dpy; |
|
|
|
Display *dpy; |
|
|
|
Screen *screen; |
|
|
|
Screen *screen; |
|
|
|
|
|
|
|
|
|
|
|
if (setuid(0)) |
|
|
|
if (setuid(0)) |
|
|
|
die("setuid(0) failed\n"); |
|
|
|
die("setuid(0) failed\n"); |
|
|
|
if (!(dpy = XOpenDisplay(NULL))) |
|
|
|
if (!(dpy = XOpenDisplay(NULL))) |
|
|
|
die("Cannot open display\n"); |
|
|
|
die("Cannot open display\n"); |
|
|
|
|
|
|
|
|
|
|
|
screen = XDefaultScreen(dpy); |
|
|
|
screen = XDefaultScreen(dpy); |
|
|
|
XSync(dpy, 0); |
|
|
|
XSync(dpy, 0); |
|
|
|
getoldbrightness(); |
|
|
|
getoldbrightness(); |
|
|
|
lockscreen(dpy, screen); |
|
|
|
lockscreen(dpy, screen); |
|
|
|
updatescreenon(1); |
|
|
|
updatescreenon(1); |
|
|
|
readinputloop(dpy, screen); |
|
|
|
readinputloop(dpy, screen); |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|