Python DPMS
There have been several occasions where I have needed to control the power state of my monitor (LCD or CRT) programatically. One time was for a custom LCD digital picture frame that I made. I wanted to turn off the LCD after a certain timeout period and turn it back on whenever a motion sensor produced an event. For that project I used the “xset” command line program and used a command like:
xset dpms force <on|off>
to turn it on and off. That worked, but this time around I wanted to do it programmatically rather than dropping to a system shell. I downloaded the source code for xset and found they were interfacing to the DPMS X11 extension. So I downloaded the source to libXext and found the DPMS API. It is a simple API, so I created some Python bindings to it and have published the results over at github. You can checkout the python-dpms project page over at github or download the source tarball and build it.
It is simple to turn on and off the monitor from python now:
import dpms, time d=dpms.DPMS() time.sleep(1) # this is necessary to prevent your last key press that launches this from waking the monitor right back up d.ForceLevel(dpms.DPMSModeOff) # turn monitor off d.Info() #d.ForceLevel(dpms.DPMSModeOn) # turn it on
You will need to save this to a file and run it non-interactively because the interactive shell will cause it to wake up instantly. When you run it non-interactively, it will turn off your monitor and then you can press any key to wake it back up.
Here is the README file from the project:
ABOUT
=====
This is python bindings to the DPMS X11 extension module for controlling
your monitor power savings state.
The DPMS interface lets you control the power level of your monitor
(On, Standby, Suspend, or Off). It is a simple interface that lets
you get/set timeouts of inactivity to enter these states or you can
force it to enter any of the states.
See example.py on how to use it. This file should be sufficient on how
to use it. It first shows how to query all the different settings and
then shows how to set them.
See also 'man xset' for a program that lets you modify the DPMS state
from the command line.
INSTALL
=======
Install libXext and python development packages installed. For
example, on RedHat/Fedora:
yum install libXext-devel python-devel
On Ubuntu:
apt-get install libxext6-dev python-dev
Then run:
python setup.py build
Then as root, run:
python setup.py install
That's it. Now run:
python example.py
to test it out.
The included example.py shows how easy it is to use:
import dpms
d = dpms.DPMS() # to use the current display, or alternatively DPMS(":1")
# print up the display
print "Display :", d.display()
# query extension, should return True as the first element
print "Query Extension :", d.QueryExtension()
# query Capable, not sure what it does
print "Capable :", d.Capable()
# query current version
print "Version :", d.GetVersion()
# query the current state of things
(level, enabled) = d.Info()
print "DPMS enabled :", enabled
if(level == dpms.DPMSModeOn):
current_level = "On"
elif(level == dpms.DPMSModeStandby):
current_level = "Standby"
elif(level == dpms.DPMSModeSuspend):
current_level = "Suspend"
elif(level == dpms.DPMSModeOff):
current_level = "Off"
else:
current_level = "Unknown (%d)" % level
print "Current Level :", current_level, "(", level, ")"
# query the current timeout settings
(standby, suspend, off) = d.GetTimeouts()
print "Timeouts"
print " Standby :", standby
print " Suspend :", suspend
print " Off :", off
# We have queried everything, now we will set everything back to its
# current state. We set back to the current state so as not to alter
# any state and so that you can see examples of setting state.
# set the timeout settings
d.SetTimeouts(standby, suspend, off)
# enable/disable DPMS
if(enabled):
d.Enable()
else:
d.Disable()
# force DPMS to a certain level
d.ForceLevel(level)
# if you wanted to force the monitor off, you could
#d.ForceLevel(dpms.DPMSModeOff)
# to force it back on
#d.ForceLevel(dpms.DPMSModeOn)
# You can also suspend or standby
#d.ForceLevel(dpms.DPMSModeSuspend)
#d.ForceLevel(dpms.DPMSModeStandby)
If you run python example.py, you will see output like:
Display : :0.0 Query Extension : (True, 0, 0) Capable : True Version : (1, 1) DPMS enabled : True Current Level : On ( 0 ) Timeouts Standby : 0 Suspend : 0 Off : 0
So just like that I can now turn on and off the monitor from my python programs. If you are not using python but C/C++, the /usr/include/X11/extensions/dpms.h shows how simple the API is, so don’t be afraid to use it.