Last active
April 13, 2020 08:06
-
-
Save paresy/8f92cc229311ff862728fff9c69eec6c to your computer and use it in GitHub Desktop.
Monitor X11 display activity and disable HDMI on inactivity (Raspberry Pi)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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