|
|
@ -3,10 +3,13 @@ |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <string.h> |
|
|
|
#include <string.h> |
|
|
|
|
|
|
|
#include <unistd.h> |
|
|
|
#include <X11/keysym.h> |
|
|
|
#include <X11/keysym.h> |
|
|
|
#include <X11/XF86keysym.h> |
|
|
|
#include <X11/XF86keysym.h> |
|
|
|
#include <X11/Xlib.h> |
|
|
|
#include <X11/Xlib.h> |
|
|
|
|
|
|
|
#include <X11/Xutil.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Types
|
|
|
|
enum State { |
|
|
|
enum State { |
|
|
|
StateNoInput, // Screen on / input lock
|
|
|
|
StateNoInput, // Screen on / input lock
|
|
|
|
StateNoInputNoScreen, // Screen off / input lock
|
|
|
|
StateNoInputNoScreen, // Screen off / input lock
|
|
|
@ -14,7 +17,6 @@ enum State { |
|
|
|
StateSuspendPending, // Suspend 'woken up', must leave state in <10s, or kicks to StateSuspend
|
|
|
|
StateSuspendPending, // Suspend 'woken up', must leave state in <10s, or kicks to StateSuspend
|
|
|
|
StateDead // Exit the appliation
|
|
|
|
StateDead // Exit the appliation
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
enum Color { |
|
|
|
enum Color { |
|
|
|
Red, |
|
|
|
Red, |
|
|
|
Blue, |
|
|
|
Blue, |
|
|
@ -22,67 +24,80 @@ enum Color { |
|
|
|
Off |
|
|
|
Off |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static Display *dpy; |
|
|
|
// Fn declarations
|
|
|
|
static enum State state = StateNoInput; |
|
|
|
void configuresuspendsettingsandwakeupsources(); |
|
|
|
static int lastkeysym = NULL; |
|
|
|
void die(const char *err, ...); |
|
|
|
static int lastkeyn = 0; |
|
|
|
int getoldbrightness(); |
|
|
|
static char oldbrightness[10] = "200"; |
|
|
|
void lockscreen(Display *dpy, int screen); |
|
|
|
static char * brightnessfile = "/sys/devices/platform/backlight/backlight/backlight/brightness"; |
|
|
|
void readinputloop(Display *dpy, int screen); |
|
|
|
static char * powerstatefile = "/sys/power/state"; |
|
|
|
void setpineled(enum Color c); |
|
|
|
|
|
|
|
void syncstate(); |
|
|
|
|
|
|
|
void writefile(char *filepath, char *str); |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
// Variables
|
|
|
|
writefile(char *filepath, char *str) |
|
|
|
Display *dpy; |
|
|
|
{ |
|
|
|
enum State state = StateNoInput; |
|
|
|
int f; |
|
|
|
KeySym lastkeysym = XK_Cancel; |
|
|
|
f = open(filepath, O_WRONLY); |
|
|
|
int lastkeyn = 0; |
|
|
|
if (f != NULL) { |
|
|
|
char oldbrightness[10] = "200"; |
|
|
|
write(f, str, strlen(str)); |
|
|
|
char * brightnessfile = "/sys/devices/platform/backlight/backlight/backlight/brightness"; |
|
|
|
close(f); |
|
|
|
char * powerstatefile = "/sys/power/state"; |
|
|
|
} else { |
|
|
|
|
|
|
|
fprintf(stderr, "Couldn't open filepath <%s>\n", filepath); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
setpineled(enum Color c) |
|
|
|
configuresuspendsettingsandwakeupsources() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (c == Red) { |
|
|
|
// Disable all wakeup sources
|
|
|
|
writefile("/sys/class/leds/red:indicator/brightness", "1"); |
|
|
|
struct dirent *wakeupsource; |
|
|
|
writefile("/sys/class/leds/blue:indicator/brightness", "0"); |
|
|
|
char wakeuppath[100]; |
|
|
|
} else if (c == Blue) { |
|
|
|
DIR *wakeupsources = opendir("/sys/class/wakeup"); |
|
|
|
writefile("/sys/class/leds/red:indicator/brightness", "0"); |
|
|
|
if (wakeupsources == NULL) |
|
|
|
writefile("/sys/class/leds/blue:indicator/brightness", "1"); |
|
|
|
die("Couldn't open directory /sys/class/wakeup\n"); |
|
|
|
} else if (c == Purple) { |
|
|
|
while ((wakeupsource = readdir(wakeupsources)) != NULL) { |
|
|
|
writefile("/sys/class/leds/red:indicator/brightness", "1"); |
|
|
|
sprintf( |
|
|
|
writefile("/sys/class/leds/blue:indicator/brightness", "1"); |
|
|
|
wakeuppath,
|
|
|
|
} else if (c == Off) { |
|
|
|
"/sys/class/wakeup/%s/device/power/wakeup", |
|
|
|
writefile("/sys/class/leds/red:indicator/brightness", "0"); |
|
|
|
wakeupsource->d_name |
|
|
|
writefile("/sys/class/leds/blue:indicator/brightness", "0"); |
|
|
|
); |
|
|
|
} |
|
|
|
fprintf(stderr, "Disabling wakeup source: %s", wakeupsource->d_name); |
|
|
|
|
|
|
|
writefile(wakeuppath, "disabled"); |
|
|
|
|
|
|
|
fprintf(stderr, ".. ok\n"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
closedir(wakeupsources); |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
// Enable powerbutton wakeup source
|
|
|
|
syncstate() |
|
|
|
fprintf(stderr, "Enable powerbutton wakeup source\n"); |
|
|
|
{ |
|
|
|
writefile( |
|
|
|
if (state == StateSuspend) { |
|
|
|
"/sys/devices/platform/soc/1f03400.rsb/sunxi-rsb-3a3/axp221-pek/power/wakeup", |
|
|
|
setpineled(Red); |
|
|
|
"enabled" |
|
|
|
configuresuspendsettingsandwakeupsources(); |
|
|
|
); |
|
|
|
writefile(powerstatefile, "mem"); |
|
|
|
|
|
|
|
state = StateSuspendPending; |
|
|
|
// Enable rtc wakeup source
|
|
|
|
syncstate(); |
|
|
|
fprintf(stderr, "Enable rtc wakeup source\n"); |
|
|
|
} else if (state == StateNoInput) { |
|
|
|
writefile( |
|
|
|
setpineled(Blue); |
|
|
|
"/sys/devices/platform/soc/1f00000.rtc/power/wakeup", |
|
|
|
writefile(brightnessfile, oldbrightness); |
|
|
|
"enabled" |
|
|
|
} else if (state == StateNoInputNoScreen || state == StateSuspendPending) { |
|
|
|
); |
|
|
|
setpineled(Purple); |
|
|
|
|
|
|
|
writefile(brightnessfile, "0"); |
|
|
|
// Temporary hack to disable USB driver that doesn't suspend
|
|
|
|
} else if (state == StateDead) { |
|
|
|
fprintf(stderr, "Disabling buggy USB driver\n"); |
|
|
|
writefile(brightnessfile, oldbrightness); |
|
|
|
writefile( |
|
|
|
setpineled(Off); |
|
|
|
"/sys/devices/platform/soc/1c19000.usb/driver/unbind", |
|
|
|
} |
|
|
|
"1c19000.usb" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Temporary hack to disable Bluetooth driver that crashes on suspend 1/5th the time
|
|
|
|
|
|
|
|
fprintf(stderr, "Disabling buggy Bluetooth driver\n"); |
|
|
|
|
|
|
|
writefile( |
|
|
|
|
|
|
|
"/sys/bus/serial/drivers/hci_uart_h5/unbind", |
|
|
|
|
|
|
|
"serial0-0" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// E.g. make sure we're using CRUST
|
|
|
|
|
|
|
|
fprintf(stderr, "Flip mem_sleep setting to use crust\n"); |
|
|
|
|
|
|
|
writefile("/sys/power/mem_sleep", "deep"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void |
|
|
|
void |
|
|
|
die(const char *err, ...) |
|
|
|
die(const char *err, ...) |
|
|
|
{ |
|
|
|
{ |
|
|
|
fprintf(stderr, "Error: %s", err); |
|
|
|
fprintf(stderr, "Error: %s", err); |
|
|
@ -91,12 +106,33 @@ die(const char *err, ...) |
|
|
|
exit(1); |
|
|
|
exit(1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Loosely derived from suckless' slock's lockscreen binding logic but
|
|
|
|
int |
|
|
|
// alot more coarse, intentionally so can be triggered while grab_key
|
|
|
|
getoldbrightness() { |
|
|
|
// for dwm multikey path already holding..
|
|
|
|
char * buffer = 0; |
|
|
|
|
|
|
|
long length; |
|
|
|
|
|
|
|
FILE * f = fopen(brightnessfile, "r"); |
|
|
|
|
|
|
|
if (f) { |
|
|
|
|
|
|
|
fseek(f, 0, SEEK_END); |
|
|
|
|
|
|
|
length = ftell(f); |
|
|
|
|
|
|
|
fseek(f, 0, SEEK_SET); |
|
|
|
|
|
|
|
buffer = malloc(length); |
|
|
|
|
|
|
|
if (buffer) { |
|
|
|
|
|
|
|
fread(buffer, 1, length, f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fclose(f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (buffer) { |
|
|
|
|
|
|
|
sprintf(oldbrightness, "%d", atoi(buffer)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
lockscreen(Display *dpy, int screen) |
|
|
|
lockscreen(Display *dpy, int screen) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
// Loosely derived from suckless' slock's lockscreen binding logic but
|
|
|
|
|
|
|
|
// alot more coarse, intentionally so can be triggered while grab_key
|
|
|
|
|
|
|
|
// for dwm multikey path already holding..
|
|
|
|
int i, ptgrab, kbgrab; |
|
|
|
int i, ptgrab, kbgrab; |
|
|
|
Window root; |
|
|
|
Window root; |
|
|
|
root = RootWindow(dpy, screen); |
|
|
|
root = RootWindow(dpy, screen); |
|
|
@ -153,7 +189,7 @@ readinputloop(Display *dpy, int screen) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
lastkeyn = 0; |
|
|
|
lastkeyn = 0; |
|
|
|
lastkeysym = NULL; |
|
|
|
lastkeysym = XK_Cancel; |
|
|
|
switch (keysym) { |
|
|
|
switch (keysym) { |
|
|
|
case XF86XK_AudioRaiseVolume: |
|
|
|
case XF86XK_AudioRaiseVolume: |
|
|
|
state = StateSuspend; |
|
|
|
state = StateSuspend; |
|
|
@ -176,83 +212,61 @@ readinputloop(Display *dpy, int screen) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
|
void |
|
|
|
getoldbrightness() { |
|
|
|
setpineled(enum Color c) |
|
|
|
char * buffer = 0; |
|
|
|
{ |
|
|
|
long length; |
|
|
|
if (c == Red) { |
|
|
|
FILE * f = fopen(brightnessfile, "r"); |
|
|
|
writefile("/sys/class/leds/red:indicator/brightness", "1"); |
|
|
|
if (f) { |
|
|
|
writefile("/sys/class/leds/blue:indicator/brightness", "0"); |
|
|
|
fseek(f, 0, SEEK_END); |
|
|
|
} else if (c == Blue) { |
|
|
|
length = ftell(f); |
|
|
|
writefile("/sys/class/leds/red:indicator/brightness", "0"); |
|
|
|
fseek(f, 0, SEEK_SET); |
|
|
|
writefile("/sys/class/leds/blue:indicator/brightness", "1"); |
|
|
|
buffer = malloc(length); |
|
|
|
} else if (c == Purple) { |
|
|
|
if (buffer) { |
|
|
|
writefile("/sys/class/leds/red:indicator/brightness", "1"); |
|
|
|
fread(buffer, 1, length, f); |
|
|
|
writefile("/sys/class/leds/blue:indicator/brightness", "1"); |
|
|
|
|
|
|
|
} else if (c == Off) { |
|
|
|
|
|
|
|
writefile("/sys/class/leds/red:indicator/brightness", "0"); |
|
|
|
|
|
|
|
writefile("/sys/class/leds/blue:indicator/brightness", "0"); |
|
|
|
} |
|
|
|
} |
|
|
|
fclose(f); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (buffer) { |
|
|
|
|
|
|
|
sprintf(oldbrightness, "%d", atoi(buffer)); |
|
|
|
void |
|
|
|
|
|
|
|
syncstate() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (state == StateSuspend) { |
|
|
|
|
|
|
|
setpineled(Red); |
|
|
|
|
|
|
|
configuresuspendsettingsandwakeupsources(); |
|
|
|
|
|
|
|
writefile(powerstatefile, "mem"); |
|
|
|
|
|
|
|
state = StateSuspendPending; |
|
|
|
|
|
|
|
syncstate(); |
|
|
|
|
|
|
|
} else if (state == StateNoInput) { |
|
|
|
|
|
|
|
setpineled(Blue); |
|
|
|
|
|
|
|
writefile(brightnessfile, oldbrightness); |
|
|
|
|
|
|
|
} else if (state == StateNoInputNoScreen || state == StateSuspendPending) { |
|
|
|
|
|
|
|
setpineled(Purple); |
|
|
|
|
|
|
|
writefile(brightnessfile, "0"); |
|
|
|
|
|
|
|
} else if (state == StateDead) { |
|
|
|
|
|
|
|
writefile(brightnessfile, oldbrightness); |
|
|
|
|
|
|
|
setpineled(Off); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
void |
|
|
|
configuresuspendsettingsandwakeupsources() |
|
|
|
writefile(char *filepath, char *str) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Disable all wakeup sources
|
|
|
|
int f; |
|
|
|
struct dirent *wakeupsource; |
|
|
|
f = open(filepath, O_WRONLY); |
|
|
|
char wakeuppath[100]; |
|
|
|
if (f != -1) { |
|
|
|
DIR *wakeupsources = opendir("/sys/class/wakeup"); |
|
|
|
write(f, str, strlen(str)); |
|
|
|
if (wakeupsources == NULL) |
|
|
|
close(f); |
|
|
|
die("Couldn't open directory /sys/class/wakeup\n"); |
|
|
|
} else { |
|
|
|
while ((wakeupsource = readdir(wakeupsources)) != NULL) { |
|
|
|
fprintf(stderr, "Couldn't open filepath <%s>\n", filepath); |
|
|
|
sprintf( |
|
|
|
|
|
|
|
wakeuppath,
|
|
|
|
|
|
|
|
"/sys/class/wakeup/%s/device/power/wakeup", |
|
|
|
|
|
|
|
wakeupsource->d_name |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
fprintf(stderr, "Disabling wakeup source: %s", wakeupsource->d_name); |
|
|
|
|
|
|
|
writefile(wakeuppath, "disabled"); |
|
|
|
|
|
|
|
fprintf(stderr, ".. ok\n"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
closedir(wakeupsources); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Enable powerbutton wakeup source
|
|
|
|
|
|
|
|
fprintf(stderr, "Enable powerbutton wakeup source\n"); |
|
|
|
|
|
|
|
writefile( |
|
|
|
|
|
|
|
"/sys/devices/platform/soc/1f03400.rsb/sunxi-rsb-3a3/axp221-pek/power/wakeup", |
|
|
|
|
|
|
|
"enabled" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Enable rtc wakeup source
|
|
|
|
|
|
|
|
fprintf(stderr, "Enable rtc wakeup source\n"); |
|
|
|
|
|
|
|
writefile( |
|
|
|
|
|
|
|
"/sys/devices/platform/soc/1f00000.rtc/power/wakeup", |
|
|
|
|
|
|
|
"enabled" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Temporary hack to disable USB driver that doesn't suspend
|
|
|
|
|
|
|
|
fprintf(stderr, "Disabling buggy USB driver\n"); |
|
|
|
|
|
|
|
writefile( |
|
|
|
|
|
|
|
"/sys/devices/platform/soc/1c19000.usb/driver/unbind", |
|
|
|
|
|
|
|
"1c19000.usb" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Temporary hack to disable Bluetooth driver that crashes on suspend 1/5th the time
|
|
|
|
|
|
|
|
fprintf(stderr, "Disabling buggy Bluetooth driver\n"); |
|
|
|
|
|
|
|
writefile( |
|
|
|
|
|
|
|
"/sys/bus/serial/drivers/hci_uart_h5/unbind", |
|
|
|
|
|
|
|
"serial0-0" |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// E.g. make sure we're using CRUST
|
|
|
|
|
|
|
|
fprintf(stderr, "Flip mem_sleep setting to use crust\n"); |
|
|
|
|
|
|
|
writefile("/sys/power/mem_sleep", "deep"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
|
int |
|
|
|
main(int argc, char **argv) { |
|
|
|
main(int argc, char **argv) { |
|
|
|
Screen *screen; |
|
|
|
int screen; |
|
|
|
|
|
|
|
|
|
|
|
if (setuid(0)) |
|
|
|
if (setuid(0)) |
|
|
|
die("setuid(0) failed\n"); |
|
|
|
die("setuid(0) failed\n"); |
|
|
|