Changing power settings

in elementary OS

The #elementary channel on Freenode got an interesting question today:

How can one change the Suspend / Turn off screen settings to interval smaller than 5 minutes ie. turn off screen after 1 minute and suspend the laptop after 2 minutes when idle?

Despite spending a lot of time working on elementary OS I don't know the answer, so I start with the power menu.

Power settings

⌘+Space opens the elementary applications menu:

/elementary-power/menu-search.png
Searching in the elementary Applications menu for "power" settings.

This menu controls all the power settings for the desktop. It's part of a set of elementary components called Switchboard. I'm looking for an option here to turn the screen after 1 minute.

/elementary-power/power-dialog.png
The minimum interval is 5 minutes.

No such option. I dig deeper and try to find out how Switchboard manages power settings and see if that leads me to a workaround.

Use the source

Every piece of the elementary OS desktop is free software, which means its source code is available for the community to study, change, and re-use. I don't know how the Switchboard power menu works, so I'm going to study the source code to find out.

Finding the source code for power settings

The elementary project has a page on GitHub where I can search for components:

elementary GitHub
Visiting elementary's GitHub organization in the Epiphany web browser

Scrolling down, there's a search box for repositories. I try typing "power" to see what comes up:

found switchboard-plug-power
Searching elementary repositories for "power"

This is the repository where the source code for the power settings is stored. I browse a few files in the repository, looking for the key phrase "Turn off display" which is found in the settings menu:

main view source code
I found the screen_timeout_label in MainView

Now I know there's a component called screen_timeout which we manipulate when we touch the control with that label. But what does it actually do to change the power settings when we select a different option?

Understanding the power settings method of action

I search for "screen_timeout" and scan through the results until I see one that says "connect," suggesting that this is where the settings panel connects to another component that takes action to change settings. The name of this component is dpms_helper.

Back to the elementary GitHub organization! Now I'm looking for the dpms helper to dig further.

search "dpms"
Searching elementary repositories for "dpms"

This repository has the source code for dpms-helper, the tool that I found out Switchboard uses to act on our power settings preferences. How does it work? I search around in its source code and soon find that it's executing another program called xset:

execute xset dpms
dpms-helper in turn executes xset dpms

How can we use xset to achieve the desired goal of turning our screen off after a short time-interval? Now that I know what to look for, let's see what the system manual has to say:

man xset
Searching the system manual for more information.

Solutions & recap

I set out to find out how to set short intervals for power management: 1 minute before turning off the screen, and 2 minutes before suspending. The search took me through:

  • The applications menu and power settings menu

  • The elementary project's GitHub repositories

  • Source code for Switchboard and dpms-helper

  • The system manual

After researching the method of action, I can answer the original question like so:

# power settings: standby after 2 minutes, never suspend,
# and screen off after 1 minute.

# `xset dpms` takes arguments in seconds, in that order.

standby_seconds=$(((2*60)))
screen_off_seconds=$(((1*60)))

xset dpms $standby_seconds 0 $screen_off_seconds

Even better using dpms-helper

The above solution works, but it has two shortcomings:

  1. It doesn't put our settings anywhere that other applications (like Switchboard) can find out about them

  2. It uses a lower-level tool, xset, instead of a standard platform tool.

I'd like to find another solution that leverages the elementary technology better. In the dpms-helper source code, I find this:

standby_time=$(gsettings get io.elementary.dpms standby-time | cut -d" " -f2)

suspend_time=$(gsettings get io.elementary.dpms suspend-time | cut -d" " -f2)

off_time=$(gsettings get io.elementary.dpms off-time | cut -d" " -f2)

It gets the time values for these options from GSettings, a configuration management tool that comes with GNOME. We can use gsettings set to change the time values and then run dpms-helper:

# power settings: standby after 2 minutes, never suspend,
# and screen off after 1 minute.

# gsettings for these take time in seconds

gsettings set io.elementary.dpms standby-time $(((2*60)))
gsettings set io.elementary.dpms off-time $(((1*60)))

io.elementary.dpms-helper

This solution leaves the configuration in GSettings so other apps can find and respect those values.

Support elementary OS

The elementary OS team puts a ton of love into creating a beautiful, accessible, privacy-respecting desktop. The source code is available for everyone to read, study, share and modify, so the whole community is invited learn more about computers and share the work of improving them.

Three ways you can contribute to elementary logo

  1. Spread the word! Share this article and others you find interesting and helpful. The elementary team writes a great blog.

  2. Purchase elementary OS (name your price), apps on AppCenter, and sponsor elementary on GitHub.

  3. Contribute your skills to elementary OS development, starting with our guide for developers.

You can also sponsor me on GitHub to get more practical explorations like this one!