Xantium
Xantium

Reputation: 11605

Turning off monitor in C

I have an algorithm that I am processing in C. It takes a while to complete, so to save power, I want to turn off the screen.

After a quick search on Google (https://askubuntu.com/questions/62858/turn-off-monitor-using-command-line) I found I can turn off the screen using the simple commands:

xset -display :0.0 dpms force off 

This works a treat, however I would like my C code to turn off the screen automatically, as soon as it starts.

I was thinking something along the lines of system("xset -display :0.0 dpms force off"); however, I've always been discouraged from doing this (and have had some bad experience in the past). Is there a better way to get this done (hopefully a system call or similar)?

I am aware that a bash script may be perfectly suited to the job however, I'm looking to keep everything purely inside my program, if possible.

Upvotes: 0

Views: 1161

Answers (1)

user10678532
user10678532

Reputation:

This does the same thing as xset dpms force off:

#include <X11/Xlib.h>
#include <X11/extensions/dpms.h>
#include <err.h>
int main(void){
        Display *dpy;
        if(!(dpy = XOpenDisplay(0)))
                errx(1, "cannot open display '%s'", XDisplayName(0));
        DPMSEnable(dpy);
        DPMSForceLevel(dpy, DPMSModeOff);
        XSync(dpy, False);
}

compile it with cc xdfo.c -o xdfo -lX11 -lXext.

xset also sleeps 100 ms after the DPMSEnable, I have no idea why it does that.

Upvotes: 5

Related Questions