简体   繁体   中英

Block X events when DPMS is active

I am trying to block the function XNextEvent from returning when DPMS is actively blanking the screen, so that I do not receive keyboard or mouse inputs while my screen is black.

I want to block the function from returning entirely, because I have already tried to query DPMS for its state once the function returns, but it seems that the DPMS system is somehow processing all of my inputs before X receives them, because every query within XNextEvent is telling me that DPMS is no longer blanking:

while (running && !XNextEvent(dpy, &ev))
{
    if (!DPMSInfo(dpy, &pwr, &state))
    {
        fprintf(stderr, "%s: DPMSInfo failed!\n", argv[0]);
        running = 0;
    }
    else if (!state || pwr == DPMSModeOn)
    {
        // sans a critical error, this will ALWAYS return true
        // regardless of the current state of the screen
        if (ev.type == KeyPress)
        {
            // now I am handling keyboard input
            // while the screen is still potentially blank
        }
    }

}

Is there some underlying feature of DPMS or X that will block all inputs while blanked, and require at least one input to awaken the display before continuing to listen to them?

I've found a way to make use of the XScreenSaver extension by reading the code for the xssstart program:

if (!XScreenSaverQueryExtension(dpy, &evbase, &errbase))
    die("failed to query XScreenSaver extension!\n");

XScreenSaverSelectInput(dpy, DefaultRootWindow(dpy), ScreenSaverNotifyMask);

int ready = 1;

while (running && !XNextEvent(dpy, &ev))
{
    if (((XScreenSaverNotifyEvent *) &ev)->state == ScreenSaverOn)
    {
        ready = 0; // DPMS is now blanking
        XUngrabKeyboard(dpy, CurrentTime);
    }
    else if (((XScreenSaverNotifyEvent *) &ev)->state == ScreenSaverOff)
    {
        ready = 1; // DPMS is no longer blanking
        XGrabKeyboard(dpy, win, True, GrabModeAsync, GrabModeAsync, CurrentTime);
    }
    if (ready && ev.type == KeyPress)
    {
        // now I am only handling keyboard input when not blanked
    }
}

Note that the major drawback of this solution is that when the keyboard is relinquished, the keyboard input will be fed into any other random application in the background (in the case of a screen locker such as slock ). This is ultimately the subject of another question though. I might be able to focus on a fake window and purge the input there...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM