Skip to content

Instantly share code, notes, and snippets.

@paresy
Last active April 13, 2020 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paresy/8f92cc229311ff862728fff9c69eec6c to your computer and use it in GitHub Desktop.
Save paresy/8f92cc229311ff862728fff9c69eec6c to your computer and use it in GitHub Desktop.
Monitor X11 display activity and disable HDMI on inactivity (Raspberry Pi)
//Install dependencies: sudo apt install libxss-dev
//Compile with: gcc -o switchOff switchOff.c -lXss -lX11
#include <X11/extensions/scrnsaver.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {
Display *dpy = NULL;
while(!dpy) {
dpy = XOpenDisplay(":0");
usleep(1000 * 1000);
}
int on = 1;
while(1) {
XScreenSaverInfo *info = XScreenSaverAllocInfo();
XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
//Disable after 5 minutes
if(info->idle >= 5 * 60 * 1000) {
if(on) {
printf("Off\n");
system("/usr/bin/vcgencmd display_power 0");
on = 0;
}
} else {
if(!on) {
printf("On\n");
system("/usr/bin/vcgencmd display_power 1");
on = 1;
}
}
XFree(info);
usleep(100 * 1000);
}
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment