Display timeout setting and run loop improvements.

This commit is contained in:
2026-09-03 21:34:04 -07:00
parent 3f9a2b9e15
commit 7f2ec329c4
3 changed files with 301 additions and 123 deletions
+2
View File
@@ -3,6 +3,8 @@
static const char *user = "";
static const char *group = "";
static const int displaytimeout = 5; /* 0 to disable */
static const char *colorname[NUMCOLS] = {
[INIT] = "black", /* after initialization */
[INPUT] = "#005577", /* during input */
+36
View File
@@ -0,0 +1,36 @@
/* user and group to drop privileges to */
/* NOTE: Leave these empty if you want them to be determined automatically. */
static const char *user = "";
static const char *group = "";
static const int displaytimeout = 5; /* 0 to disable */
static const char *colorname[NUMCOLS] = {
[INIT] = "black", /* after initialization */
[INPUT] = "#005577", /* during input */
[FAILED] = "#CC3333", /* wrong password */
};
/* treat a cleared input like a wrong password (color) */
static const int failonclear = 1;
static const Passthrough passthroughs[] = {
/* Modifier Key */
{ 0, XF86XK_AudioRaiseVolume },
{ 0, XF86XK_AudioLowerVolume },
{ 0, XF86XK_AudioMute },
{ 0, XF86XK_AudioPause },
{ 0, XF86XK_AudioStop },
{ 0, XF86XK_AudioNext },
{ 0, XF86XK_AudioPrev },
{ 0, XF86XK_MonBrightnessUp },
{ 0, XF86XK_MonBrightnessDown },
};
/*static const char *dspoffcmd[] = { "xset", "dpms", "force", "off", NULL };*/
static const Key keys[] = {
/* modifier key function argument */
0
/*{ 0, XK_Escape, spawn, {.v = dspoffcmd } }*/
};
+164 -24
View File
@@ -14,6 +14,7 @@
#include <string.h>
#include <unistd.h>
#include <spawn.h>
#include <time.h>
#include <sys/types.h>
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/dpms.h>
@@ -96,21 +97,90 @@ die(const char *errstr, ...)
#include <fcntl.h>
#include <linux/oom.h>
int is_screen_on(Display *dpy)
XRRScreenResources *crtcres = 0;
static int crtccount = 0;
static XRRCrtcInfo *crtcinfos[16];
void update_crtc_infos(Display *dpy)
{
CARD16 power_level;
BOOL state;
if(crtcres) {
/* TODO: Turn on displays here first? */
int i;
for(i = 0; i < crtccount; i++) {
XRRFreeCrtcInfo(crtcinfos[i]);
}
memset(crtcinfos, 0, sizeof(XRRCrtcInfo *)*16);
XRRFreeScreenResources(crtcres);
crtcres = 0;
crtccount = 0;
} else {
Window root;
int i;
if (DPMSInfo(dpy, &power_level, &state)) {
if(state) {
if(power_level == DPMSModeOn)
return 1;
root = DefaultRootWindow(dpy);
crtcres = XRRGetScreenResources(dpy, root);
for(i = 0; i < crtcres->ncrtc; i++) { /* TODO: FIX: Choose minimum of ncrtc and my crtcinfos value.
Could it be that the OS disables displays after inactivity? */
RRCrtc crtc;
XRRCrtcInfo *info;
crtc = crtcres->crtcs[i];
info = XRRGetCrtcInfo(dpy, crtcres, crtc);
crtcinfos[i] = info;
}
}
return 0;
}
// TODO: Error-prone. See which of these can return errors.
void set_screen_on(Display *dpy)
{
if(crtcres) {
int i;
for(i = 0; i < crtcres->ncrtc; i++) {
RRCrtc crtc;
static XRRCrtcInfo *info;
crtc = crtcres->crtcs[i];
info = crtcinfos[i];
XRRSetCrtcConfig(dpy, crtcres, crtcres->crtcs[i], CurrentTime,
info->x, info->y,
info->mode, info->rotation, info->outputs,
info->noutput);
}
}
}
// TODO: Error-prone. See which of these can return errors.
void set_screen_off(Display *dpy)
{
if(crtcres) {
int i;
for(i = 0; i < crtcres->ncrtc; i++) {
RRCrtc crtc;
static XRRCrtcInfo *info;
crtc = crtcres->crtcs[i];
info = crtcinfos[i];
if(info->mode != None) {
Status s =
XRRSetCrtcConfig(dpy, crtcres, crtcres->crtcs[i],
CurrentTime, 0, 0, None, RR_Rotate_0,
NULL, 0);
if(s != RRSetConfigSuccess) {
/* TODO: Failed to disable... */
}
}
}
}
}
/* TODO: When new CRTCs are connected when the screen is off. Turn on all
* screens and then update the structure. We want the displays on when new
* one connects. */
static void
dontkillme(void)
{
@@ -180,26 +250,60 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
{
XRRScreenChangeNotifyEvent *rre;
char buf[32], passwd[256], *inputhash;
int num, screen, running, failure, oldc, i, passing, dspon;
int num, screen, running, failure, oldc, i, passing, dson, xfd;
unsigned int len, color;
struct timeval tv;
KeySym ksym;
XEvent ev;
Bool dpmsavailable;
CARD16 dpmspowerlevel;
BOOL dpmsstate;
CARD16 dpmsstandby, dpmssuspend, dpmsoff;
len = 0;
running = 1;
failure = 0;
oldc = INIT;
dspon = 1;
dson = 1;
while (running && !XNextEvent(dpy, &ev)) {
DPMSForceLevel(dpy, dspon ? DPMSModeOn : DPMSModeOff);
xfd = ConnectionNumber(dpy);
if (ev.type == KeyPress || ev.type == MotionNotify)
dspon = 1;
update_crtc_infos(dpy);
if (ev.type == KeyPress) {
if(DPMSCapable(dpy) && DPMSInfo(dpy, &dpmspowerlevel, &dpmsstate) &&
DPMSGetTimeouts(dpy, &dpmsstandby, &dpmssuspend, &dpmsoff))
{
dpmsavailable = 1;
if(dpmsstate)
DPMSDisable(dpy);
XSync(dpy, 0);
}
/* TODO: Disable the DPMS timeout, save settings, and restore. */
while(running) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(xfd, &fds);
XFlush(dpy);
while(XPending(dpy)) {
XNextEvent(dpy, &ev);
if(ev.type == MotionNotify) {
dson = 1;
set_screen_on(dpy);
} else if (ev.type == KeyPress) {
explicit_bzero(&buf, sizeof(buf));
num = XLookupString(&ev.xkey, buf, sizeof(buf), &ksym, 0);
if(ksym != XK_Escape) {
dson = 1;
set_screen_on(dpy);
}
if (IsKeypadKey(ksym)) {
if (ksym == XK_KP_Enter)
ksym = XK_Return;
@@ -214,7 +318,7 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
continue;
passing = 0;
for (i = 0; i < LENGTH(passthroughs); i++) {
for (i = 0; i < (int)LENGTH(passthroughs); i++) { /* WARNING: Unsafe cast. */
if (ksym == passthroughs[i].keysym &&
CLEANMASK(passthroughs[i].mod) == CLEANMASK(ev.xkey.state)) {
passing = 1;
@@ -226,7 +330,7 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
if(passing)
continue;
for (i = 0; i < LENGTH(keys); i++) {
for (i = 0; i < (int)LENGTH(keys); i++) { /* WARNING: Unsafe cast. */
Key key = keys[i];
if (ksym == key.keysym &&
@@ -240,7 +344,11 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
continue;
if(len == 0 && ksym == XK_Escape) {
dspon = !is_screen_on(dpy);
dson = !dson;
if(dson)
set_screen_on(dpy);
else
set_screen_off(dpy);
continue;
}
@@ -281,7 +389,7 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
break;
}
color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
if (running && oldc != color) {
if (running && oldc != (int)color) { /* WARNING: Unsafe cast. */
for (screen = 0; screen < nscreens; screen++) {
XSetWindowBackground(dpy,
locks[screen]->win,
@@ -296,20 +404,49 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
if (locks[screen]->win == rre->window) {
if (rre->rotation == RR_Rotate_90 ||
rre->rotation == RR_Rotate_270)
{
XResizeWindow(dpy, locks[screen]->win,
rre->height, rre->width);
else
} else {
XResizeWindow(dpy, locks[screen]->win,
rre->width, rre->height);
}
XClearWindow(dpy, locks[screen]->win);
break;
}
}
} else if(rr->active && ev.type == rr->evbase + RRNotify) {
/* TODO: Update crts variables and detect if new displays
* are connected. XRRNotifyEvent->subtype,
* XRRCrtcChangeNotifyEvent, XRROutputChangeNotifyEvent,
* RRNotify_OutputProperty*/
} else {
for (screen = 0; screen < nscreens; screen++)
for(screen = 0; screen < nscreens; screen++)
XRaiseWindow(dpy, locks[screen]->win);
}
}
int ret;
if(displaytimeout > 0 && dson) {
tv.tv_sec = displaytimeout;
tv.tv_usec = 0;
ret = select(xfd + 1, &fds, 0, 0, &tv);
} else {
ret = select(xfd + 1, &fds, 0, 0, 0);
}
if (ret == 0) {
dson = 0;
set_screen_off(dpy);
} else if (ret > 0 && FD_ISSET(xfd, &fds)) {
}
}
if(dpmsavailable && dpmsstate)
{
DPMSSetTimeouts(dpy, dpmsstandby, dpmssuspend, dpmsoff);
DPMSEnable(dpy);
XSync(dpy, 0);
}
}
static struct lock *
@@ -349,6 +486,11 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
&color, &color, 0, 0);
XDefineCursor(dpy, lock->win, invisible);
XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask |
RRCrtcChangeNotifyMask |
RROutputChangeNotifyMask |
RROutputPropertyNotifyMask);
/* Try to grab mouse pointer *and* keyboard for 600ms, else fail the lock */
for (i = 0, ptgrab = kbgrab = -1; i < 6; i++) {
if (ptgrab != GrabSuccess) {
@@ -427,8 +569,6 @@ main(int argc, char **argv) {
groupname = username;
}
printf("%s:%s\n", username, groupname);
/* validate drop-user and -group */
errno = 0;
if (!(pwd = getpwnam(username)))