Persist screen brightness, enable universal evdev key/lid wake, and add *+# screenshot combo

This commit is contained in:
Luxferre committed 2026-10-27 22:24:39 +02:00
1 parent eef8584a8d
commit 2e2adcc7f2
38 files changed
+574 -161

No files matched your search

+10 -8
View File
@@ -1081,10 +1081,19 @@ handle_builtin_toggle(const char *toggle_name)
int maxv = atoi(max_s);
if (maxv <= 0)
maxv = 255;
int step = MAX(1, maxv / 10);
int step = MAX(1, maxv / 14);
int nextv = (strcmp(toggle_name, "bright_up") == 0) ? MIN(maxv, cur + step) : MAX(1, cur - step);
char out[32];
snprintf(out, sizeof(out), "%d\n", nextv);
write_str_file("/tmp/sxmo_saved_brightness", out);
const char *home = getenv("HOME");
if (!home || !home[0])
home = "/home/user";
char cfgdir[256], cfgpath[256];
snprintf(cfgdir, sizeof(cfgdir), "%s/.config/sxmo", home);
mkdir(cfgdir, 0755);
snprintf(cfgpath, sizeof(cfgpath), "%s/brightness", cfgdir);
write_str_file(cfgpath, out);
if (!write_str_file(bpath, out))
spawn_detached_sh(strcmp(toggle_name, "bright_up") == 0 ? "sxmo_brightness.sh up" : "sxmo_brightness.sh down");
} else {
@@ -2584,7 +2593,6 @@ open_builtin_menu(const char *menu_name, int keep_idx)
const char *home = getenv("HOME");
if (!home || !home[0])
home = "/home/user";
char pbuf[256];
snprintf(label, sizeof(label), "%s Themes", ICON_THM);
add_menu_item(label, "@plugin:themes", 0);
@@ -2603,14 +2611,8 @@ open_builtin_menu(const char *menu_name, int keep_idx)
snprintf(label, sizeof(label), "%s Change Timezone", ICON_CLK);
add_menu_item(label, "@menu:timezone", 1);
snprintf(pbuf, sizeof(pbuf), "%s/.cache/sxmo/sxmo.nosuspend", home);
snprintf(label, sizeof(label), "%s Auto-suspend %s", ICON_ZZZ, (access(pbuf, F_OK) == 0) ? ICON_TOF : ICON_TON);
add_menu_item(label, "@toggle:autosuspend", 1);
snprintf(label, sizeof(label), "%s Hooks", ICON_TRM);
add_menu_item(label, "sxmo_hookmenu.sh", 0);
snprintf(label, sizeof(label), "%s Suspend Blockers", ICON_CFG);
add_menu_item(label, "sxmo_terminal.sh sxmo_debug_suspend.sh", 0);
snprintf(label, sizeof(label), "%s Log", ICON_INF);
snprintf(cmd, sizeof(cmd), "sxmo_terminal.sh tail -n 100 -f %s/sxmo.log", home);
add_menu_item(label, cmd, 0);
+278 -52
View File
@@ -32,6 +32,7 @@
#include <string.h>
#include <unistd.h>
#include <linux/input.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <X11/cursorfont.h>
@@ -328,6 +329,18 @@ static xcb_connection_t *xcon;
static struct HoldKey *holdkeyroot = NULL;
#define MAX_INPUT_FDS 8
static int input_fds[MAX_INPUT_FDS];
static int num_input_fds = 0;
static long long last_wake_ms = 0;
static int star_down = 0;
static int pound_down = 0;
static long long last_screenshot_ms = 0;
static int starpound_pending = 0;
static long long starpound_deadline_ms = 0;
static KeySym starpound_keysym = NoSymbol;
static unsigned int starpound_keycode = 0;
/* configuration, allows nested code to access above variables */
#include "config.h"
@@ -706,6 +719,11 @@ cleanup(void)
for (i = 0; i < LENGTH(colors); i++)
free(scheme[i]);
unload_plugins();
for (i = 0; i < (size_t)num_input_fds; i++) {
if (input_fds[i] >= 0)
close(input_fds[i]);
}
num_input_fds = 0;
XDestroyWindow(dpy, wmcheckwin);
drw_free(drw);
XSync(dpy, False);
@@ -1192,38 +1210,97 @@ read_sysfs_str(const char *path, char *buf, size_t len)
buf[0] = '\0';
}
static void
get_brightness_cfg_path(char *path, size_t len, const char *filename)
{
const char *xdg = getenv("XDG_CONFIG_HOME");
const char *home = getenv("HOME");
if (xdg && xdg[0])
snprintf(path, len, "%s/sxmo/%s", xdg, filename);
else if (home && home[0])
snprintf(path, len, "%s/.config/sxmo/%s", home, filename);
else
snprintf(path, len, "/home/user/.config/sxmo/%s", filename);
}
static void
save_brightness_str(const char *val)
{
char line[24], cfgpath[256];
int fd, len;
if (!val || atoi(val) < 1)
return;
len = snprintf(line, sizeof(line), "%d\n", atoi(val));
fd = open("/tmp/sxmo_saved_brightness", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd >= 0) {
(void)write(fd, line, len);
close(fd);
}
get_brightness_cfg_path(cfgpath, sizeof(cfgpath), "brightness");
fd = open(cfgpath, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd >= 0) {
(void)write(fd, line, len);
close(fd);
}
}
static void
set_hw_backlight(int on)
{
static char saved_brightness[16] = "14";
char cur[16] = "";
char cur[16] = "", cfgpath[256];
if (on) {
read_sysfs_str("/tmp/sxmo_saved_brightness", cur, sizeof(cur));
if (atoi(cur) > 1)
snprintf(saved_brightness, sizeof(saved_brightness), "%s", cur);
else if (atoi(saved_brightness) <= 1) {
read_sysfs_str("/sys/class/backlight/backlight/max_brightness", cur, sizeof(cur));
snprintf(saved_brightness, sizeof(saved_brightness), "%s", (atoi(cur) > 1) ? cur : "14");
if (atoi(cur) < 1) {
get_brightness_cfg_path(cfgpath, sizeof(cfgpath), "brightness");
read_sysfs_str(cfgpath, cur, sizeof(cur));
}
if (atoi(cur) >= 1) {
snprintf(saved_brightness, sizeof(saved_brightness), "%d", atoi(cur));
} else if (atoi(saved_brightness) < 1) {
read_sysfs_str("/sys/class/backlight/backlight/max_brightness", cur, sizeof(cur));
snprintf(saved_brightness, sizeof(saved_brightness), "%d", (atoi(cur) >= 1) ? atoi(cur) : 14);
}
save_brightness_str(saved_brightness);
write_sysfs_str("/sys/class/backlight/backlight/bl_power", "0");
write_sysfs_str("/sys/class/backlight/backlight/brightness", saved_brightness);
write_sysfs_str("/sys/class/leds/white:kbd_backlight/brightness", "255");
cur[0] = '\0';
read_sysfs_str("/tmp/sxmo_saved_kbd_brightness", cur, sizeof(cur));
if (cur[0] == '\0') {
get_brightness_cfg_path(cfgpath, sizeof(cfgpath), "kbd_brightness");
read_sysfs_str(cfgpath, cur, sizeof(cur));
}
write_sysfs_str("/sys/class/leds/white:kbd_backlight/brightness", (cur[0] != '\0') ? cur : "255");
} else {
read_sysfs_str("/sys/class/backlight/backlight/brightness", cur, sizeof(cur));
if (atoi(cur) > 1) {
snprintf(saved_brightness, sizeof(saved_brightness), "%s", cur);
int fd = open("/tmp/sxmo_saved_brightness", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd >= 0) {
(void)write(fd, saved_brightness, strlen(saved_brightness));
close(fd);
}
if (atoi(cur) >= 1) {
snprintf(saved_brightness, sizeof(saved_brightness), "%d", atoi(cur));
save_brightness_str(saved_brightness);
}
write_sysfs_str("/sys/class/leds/white:kbd_backlight/brightness", "0");
write_sysfs_str("/sys/class/backlight/backlight/brightness", "0");
}
}
static void
set_sxmo_state_str(const char *st)
{
const char *rdir = getenv("XDG_RUNTIME_DIR");
char path[256], line[32];
int fd, len;
snprintf(path, sizeof(path), "%s/sxmo.state", (rdir && rdir[0]) ? rdir : "/run/user/10000");
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd >= 0) {
len = snprintf(line, sizeof(line), "%s\n", st);
(void)write(fd, line, len);
close(fd);
}
}
static int
is_phone_locked(void)
{
@@ -1232,6 +1309,10 @@ is_phone_locked(void)
int fd;
ssize_t n;
read_sysfs_str("/sys/class/backlight/backlight/brightness", buf, sizeof(buf));
if (buf[0] != '\0' && atoi(buf) <= 0)
return 1;
snprintf(path, sizeof(path), "%s/sxmo.state", (rdir && rdir[0]) ? rdir : "/run/user/10000");
fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0)
@@ -1275,8 +1356,6 @@ writefocusedwin(Client *c)
}
}
static int lidfd = -1;
void
focus(Client *c)
{
@@ -1962,6 +2041,93 @@ flush_pending_softkey(void)
}
}
static int
is_star_key(KeySym ks, unsigned int keycode)
{
return keycode == 63 || keycode == 530 || ks == XK_asterisk || ks == XK_KP_Multiply;
}
static int
is_pound_key(KeySym ks, unsigned int keycode)
{
return keycode == 106 || keycode == 531 || ks == XK_numbersign || ks == XK_KP_Divide;
}
static void
trigger_screenshot_chord(long long now)
{
const Arg shotcmd = { .v = (const char*[]){ "bananarchy-screenshot", NULL } };
starpound_pending = 0;
starpound_deadline_ms = 0;
if ((now - last_screenshot_ms) < 400)
return;
last_screenshot_ms = now;
spawn(&shotcmd);
}
static void
flush_pending_starpound(void)
{
int pending = starpound_pending;
KeySym ks = starpound_keysym;
unsigned int kc = starpound_keycode;
starpound_pending = 0;
starpound_deadline_ms = 0;
XUngrabKeyboard(dpy, CurrentTime);
XFlush(dpy);
if (!pending || (dwm_now_ms() - last_screenshot_ms) < 400)
return;
if (!selmon->sel && !is_phone_locked())
(void)dispatch_plugin_idle_key(ks, kc, 0);
}
static void
lock_phone_screenoff(void)
{
const Arg offcmd = SHCMD("sxmo_dmenu.sh close; sxmo_state.sh set screenoff");
softkey_pending = 0;
softkey_first_ms = 0;
softkey_deadline_ms = 0;
last_softkey_mask = 0;
last_softkey_ms = 0;
starpound_pending = 0;
starpound_deadline_ms = 0;
star_down = 0;
pound_down = 0;
write_softkey_state(0, 0);
if (is_plugin_loaded("dmenu"))
host_spawn_plugin("dmenu", "close");
set_sxmo_state_str("screenoff");
set_hw_backlight(0);
spawn(&offcmd);
}
static void
wake_phone(void)
{
const Arg oncmd = SHCMD("sxmo_state.sh set unlock");
long long now = dwm_now_ms();
last_wake_ms = now;
softkey_pending = 0;
softkey_first_ms = 0;
softkey_deadline_ms = 0;
last_softkey_mask = 0;
last_softkey_ms = 0;
starpound_pending = 0;
starpound_deadline_ms = 0;
star_down = 0;
pound_down = 0;
write_softkey_state(0, 0);
write_softkey_suppress(now + SOFTKEY_SUPPRESS_MS);
XUngrabKeyboard(dpy, CurrentTime);
XFlush(dpy);
set_sxmo_state_str("unlock");
set_hw_backlight(1);
spawn(&oncmd);
}
void
keypress(XEvent *e)
{
@@ -1972,26 +2138,43 @@ keypress(XEvent *e)
ev = &e->xkey;
keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
if ((keysym == XF86XK_PowerOff || keysym == XF86XK_PickupPhone ||
keysym == XF86XK_Phone || keysym == XK_Menu || keysym == XF86XK_Back ||
keysym == XK_Return ||
keysym == XF86XK_KbdLcdMenu1 || keysym == XF86XK_KbdLcdMenu2) &&
is_phone_locked()) {
const Arg unlockcmd = SHCMD("xset s off s noblank -dpms; sxmo_state.sh set unlock");
softkey_pending = 0;
softkey_first_ms = 0;
softkey_deadline_ms = 0;
last_softkey_mask = 0;
last_softkey_ms = 0;
write_softkey_state(0, 0);
write_softkey_suppress(dwm_now_ms() + SOFTKEY_SUPPRESS_MS);
if (is_phone_locked()) {
wake_phone();
return;
}
if ((dwm_now_ms() - last_wake_ms) < 350) {
XUngrabKeyboard(dpy, CurrentTime);
XFlush(dpy);
set_hw_backlight(1);
spawn(&unlockcmd);
return;
}
if (CLEANMASK(ev->state) == 0 &&
(is_star_key(keysym, ev->keycode) || is_pound_key(keysym, ev->keycode))) {
long long now = dwm_now_ms();
if ((now - last_screenshot_ms) < 400) {
XUngrabKeyboard(dpy, CurrentTime);
XFlush(dpy);
return;
}
if (star_down && pound_down) {
trigger_screenshot_chord(now);
return;
}
if (!selmon->sel && !is_phone_locked()) {
int which = is_star_key(keysym, ev->keycode) ? 1 : 2;
starpound_pending |= which;
if (starpound_pending == 3) {
trigger_screenshot_chord(now);
} else {
starpound_keysym = keysym;
starpound_keycode = ev->keycode;
if (starpound_deadline_ms == 0)
starpound_deadline_ms = now + 100;
}
return;
}
}
if (CLEANMASK(ev->state) == 0 &&
(is_left_softkey(keysym, ev->keycode) || is_right_softkey(keysym, ev->keycode))) {
long long now = dwm_now_ms();
@@ -2079,6 +2262,9 @@ keyrelease(XEvent *e)
markreleased(keysym);
if (is_phone_locked() || (dwm_now_ms() - last_wake_ms) < 350)
return;
if (is_left_softkey(keysym, ev->keycode) || is_right_softkey(keysym, ev->keycode)) {
return;
}
@@ -2503,9 +2689,9 @@ void
run(void)
{
XEvent ev;
struct pollfd fds[2];
struct pollfd fds[1 + MAX_INPUT_FDS];
struct input_event ie;
int nfds, xfd;
int nfds, xfd, i;
/* main event loop */
XSync(dpy, False);
@@ -2523,11 +2709,11 @@ run(void)
fds[0].events = POLLIN;
fds[0].revents = 0;
nfds = 1;
if (lidfd >= 0) {
fds[1].fd = lidfd;
fds[1].events = POLLIN;
fds[1].revents = 0;
nfds = 2;
for (i = 0; i < num_input_fds; i++) {
fds[nfds].fd = input_fds[i];
fds[nfds].events = POLLIN;
fds[nfds].revents = 0;
nfds++;
}
int timeout_ms = -1;
if (softkey_pending && softkey_deadline_ms > 0) {
@@ -2546,22 +2732,51 @@ run(void)
timeout_ms = (int)rem;
}
}
if (starpound_pending && starpound_deadline_ms > 0) {
long long now = dwm_now_ms();
if (star_down && pound_down) {
trigger_screenshot_chord(now);
continue;
}
long long rem = starpound_deadline_ms - now;
if (rem <= 0) {
flush_pending_starpound();
continue;
} else if (timeout_ms < 0 || rem < timeout_ms) {
timeout_ms = (int)rem;
}
}
if (poll(fds, nfds, timeout_ms) < 0) {
if (errno == EINTR)
continue;
break;
}
if (nfds == 2 && (fds[1].revents & POLLIN)) {
while (read(lidfd, &ie, sizeof(ie)) == (ssize_t)sizeof(ie)) {
if (ie.type == EV_SW && ie.code == SW_LID) {
if (ie.value == 1) {
const Arg offcmd = SHCMD("sxmo_dmenu.sh close; sxmo_state.sh set screenoff");
set_hw_backlight(0);
spawn(&offcmd);
} else if (ie.value == 0) {
const Arg oncmd = SHCMD("xset s off s noblank -dpms; sxmo_state.sh set unlock");
set_hw_backlight(1);
spawn(&oncmd);
for (i = 0; i < num_input_fds; i++) {
if (fds[1 + i].revents & POLLIN) {
while (read(input_fds[i], &ie, sizeof(ie)) == (ssize_t)sizeof(ie)) {
if (ie.type == EV_SW && ie.code == SW_LID) {
if (ie.value == 1) {
lock_phone_screenoff();
} else if (ie.value == 0) {
wake_phone();
}
} else if (ie.type == EV_KEY) {
if (ie.code == 522 || ie.code == 55)
star_down = (ie.value != 0);
else if (ie.code == 523 || ie.code == 98)
pound_down = (ie.value != 0);
if (ie.value == 1) {
if (is_phone_locked()) {
wake_phone();
} else if ((ie.code == 522 || ie.code == 55 ||
ie.code == 523 || ie.code == 98) &&
star_down && pound_down) {
trigger_screenshot_chord(dwm_now_ms());
}
} else if (ie.value == 0 && !star_down && !pound_down && starpound_pending) {
flush_pending_starpound();
}
}
}
}
@@ -2799,9 +3014,20 @@ setup(void)
|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask|KeyPressMask|KeyReleaseMask;
XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
XSelectInput(dpy, root, wa.event_mask);
lidfd = open("/dev/input/by-path/platform-gpio-keys-event", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (lidfd < 0)
lidfd = open("/dev/input/event0", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
num_input_fds = 0;
for (i = 0; i < MAX_INPUT_FDS; i++) {
char evpath[64], evname[128] = "";
int evfd;
snprintf(evpath, sizeof(evpath), "/dev/input/event%d", i);
evfd = open(evpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
if (evfd < 0)
continue;
if (ioctl(evfd, EVIOCGNAME(sizeof(evname)), evname) >= 0 && strstr(evname, "Jack") != NULL) {
close(evfd);
continue;
}
input_fds[num_input_fds++] = evfd;
}
unlink("/tmp/sxmo_softkey_suppress");
unlink("/tmp/sxmo_softkey_state");
load_plugins();
+23 -2
View File
@@ -357,13 +357,27 @@ class PhoneController {
"Microphone", false, 0, 1, 1, (dec1 > 0) ? 1 : 0});
// 8. Screen brightness (slider 1..screenMax_)
int scrCur = std::clamp(readIntFile(backlightDir_ + "/brightness", screenMax_), 1, screenMax_);
int rawScr = readIntFile(backlightDir_ + "/brightness", -1);
if (rawScr < 1) {
rawScr = readIntFile("/tmp/sxmo_saved_brightness", -1);
}
if (rawScr < 1) {
rawScr = readIntFile(getSxmoCfgDir() + "/brightness", screenMax_);
}
int scrCur = std::clamp(rawScr, 1, screenMax_);
int scrStep = std::max(1, screenMax_ / 14);
items_.push_back({ControlId::ScreenBright, kIconBrightness, kIconBrightness,
"Screen", true, 1, screenMax_, scrStep, scrCur});
// 9. Keyboard brightness (slider if max_brightness > 1, toggle otherwise)
int kbdCur = std::clamp(readIntFile(kbdDir_ + "/brightness", 0), 0, kbdMax_);
int rawKbd = readIntFile(kbdDir_ + "/brightness", -1);
if (rawKbd < 0) {
rawKbd = readIntFile("/tmp/sxmo_saved_kbd_brightness", -1);
}
if (rawKbd < 0) {
rawKbd = readIntFile(getSxmoCfgDir() + "/kbd_brightness", 0);
}
int kbdCur = std::clamp(rawKbd, 0, kbdMax_);
if (kbdSupportsSlider_) {
int kbdStep = std::max(1, kbdMax_ / 10);
items_.push_back({ControlId::KbdBright, kIconKeyboard, kIconKeyboard,
@@ -593,13 +607,20 @@ class PhoneController {
break;
}
case ControlId::ScreenBright: {
std::string cfg = getSxmoCfgDir();
::mkdir(cfg.c_str(), 0755);
writeIntFile(backlightDir_ + "/bl_power", 0);
writeIntFile(backlightDir_ + "/brightness", it.value);
writeIntFile("/tmp/sxmo_saved_brightness", it.value);
writeIntFile(cfg + "/brightness", it.value);
break;
}
case ControlId::KbdBright: {
int v = kbdSupportsSlider_ ? it.value : (it.value ? kbdMax_ : 0);
std::string cfg = getSxmoCfgDir();
::mkdir(cfg.c_str(), 0755);
writeIntFile("/tmp/sxmo_saved_kbd_brightness", v);
writeIntFile(cfg + "/kbd_brightness", v);
if (!writeIntFile(kbdDir_ + "/brightness", v)) {
char cmd[128];
std::snprintf(cmd, sizeof(cmd),