0

Usually, laptop screens are dim set to the lowest level of brightness when the charger is not present, and brighten up to the max level if the charger is present/connected. However, on my Dell Inspiron 7559 Skylake laptop, this does not happen. My laptop is always on 100% brightness. How can I retrieve this "feature"?

Tony Lancer
  • 1,003
  • Try my answer here: http://askubuntu.com/a/788654/295286 Let me know if this helps or there's any parts that you want adapted to fit your specific case. As for the "dimming/brightness" feature itself, it's not just your laptop. This is simply not implemented in most Linux distributions, but as you can see in my answer it can be scripted – Sergiy Kolodyazhnyy Nov 01 '16 at 13:23
  • @Serg Thanks. Now, your script only dims the screen once. After that, despite me removing my charger and placing it back in, the screen stays at the defined brightness level from the script. – Tony Lancer Nov 01 '16 at 13:41
  • interesting behavior. Which one did you run by the way ? The one in question or the one in my answer ? The one in the question only decreases the brightness ( unless you uncomment certain part ). The one in my answer is supposed to remember battery and AC adapter levels. – Sergiy Kolodyazhnyy Nov 01 '16 at 14:08
  • I followed your answer, most especially the installation procedure. I didn't comment anything or add anything to the script. – Tony Lancer Nov 01 '16 at 14:16
  • Hmmm, very odd. OK, in that case I'll write something else. Might take me a couple days though. Are there any other requirements aside from distinguishing from adapter/battery ? – Sergiy Kolodyazhnyy Nov 01 '16 at 14:25
  • Well, to be simple, all I need is the basic brightness max(charger in), brightness to low(10-30%) function that comes with all OSes on laptop(easy example from Windows). That's pretty much it. If there are requirements that you do need. don't hesitate to comment, I will respond. Also, I do need to get some bash scripting under my belt :) It seems pretty simple. :D Thanks Serg. – Tony Lancer Nov 01 '16 at 14:54
  • Posted an answer, please try it, let me know if it works for you. I'm not quite certain what was causing issues with the old script, but the new one hopefully will work as expected. It relies on UPower daemon and same mechanism for setting brightness. If that still doesn't work, we'll have to troubleshoot this - I'll let you know how if that's the case – Sergiy Kolodyazhnyy Nov 03 '16 at 08:16

1 Answers1

1

Introduction

The script below is modified version of my previous scripts, written in python and using dbus exclusively for polling ac_adapter presence and setting the screen brightness.

Usage

Usage is simple: call from command line as

python ./brightness_control.py

The script defaults to 100% brightness on AC , 10% on battery. Users can use -a and -b to set their desired brightness levels on ac and battery respectively.

AS given by -h option:

$ ./brightness_control.py -h                                                                                                          
usage: brightness_control.py [-h] [-a ADAPTER] [-b BATTERY]

    Simple brightness control for laptops,
    depending on presense of AC power supply


optional arguments:
  -h, --help            show this help message and exit
  -a ADAPTER, --adapter ADAPTER
                        brightness on ac
  -b BATTERY, --battery BATTERY
                        brightness on battery

For example, one can do any of the following:

# set non default for brightness on ac
$ ./brightness_control.py -a 80 
# set non-default value for brightness on battery
$ ./brightness_control.py -b 20 
# set non-default values for both
$ ./brightness_control.py -a 80 -b 20

Source

Also available on GitHub

#!/usr/bin/env python
"""
Author: Serg Kolo <1047481448@qq.com>
Date:   Nov 3rd , 2016
Purpose:Brightness control depending on
        presence of ac adapter
Written for: http://askubuntu.com/q/844193/295286 
"""
import argparse
import dbus
import time
import sys

def get_dbus_property(bus_type, obj, path, iface, prop):
    """ utility:reads properties defined on specific dbus interface"""
    if bus_type == "session":
        bus = dbus.SessionBus()
    if bus_type == "system":
        bus = dbus.SystemBus()
    proxy = bus.get_object(obj, path)
    aux = 'org.freedesktop.DBus.Properties'
    props_iface = dbus.Interface(proxy, aux)
    props = props_iface.Get(iface, prop)
    return props

def get_dbus_method(bus_type, obj, path, interface, method, arg):
    """ utility: executes dbus method on specific interface"""
    if bus_type == "session":
        bus = dbus.SessionBus()
    if bus_type == "system":
        bus = dbus.SystemBus()
    proxy = bus.get_object(obj, path)
    method = proxy.get_dbus_method(method, interface)
    if arg:
        return method(arg)
    else:
        return method()

def on_ac_power():
    adapter = get_adapter_path()
    call = ['system','org.freedesktop.UPower',adapter,
            'org.freedesktop.UPower.Device','Online'
    ]

    if get_dbus_property(*call): return True

def get_adapter_path():
    """ Finds dbus path of the ac adapter device """
    call = ['system', 'org.freedesktop.UPower',
            '/org/freedesktop/UPower','org.freedesktop.UPower',
            'EnumerateDevices',None
    ]
    devices = get_dbus_method(*call)
    for dev in devices:
        call = ['system','org.freedesktop.UPower',dev,
                'org.freedesktop.UPower.Device','Type'
        ]
        if get_dbus_property(*call) == 1:
            return dev

def set_brightness(*args):
    call = ['session','org.gnome.SettingsDaemon.Power', '/org/gnome/SettingsDaemon/Power', 
            'org.gnome.SettingsDaemon.Power.Screen', 'SetPercentage', args[-1]
    ]
    get_dbus_method(*call)

def parse_args():
    info = """
    Simple brightness control for laptops,
    depending on presense of AC power supply
    """
    arg_parser = argparse.ArgumentParser(
                 description=info,
                 formatter_class=argparse.RawTextHelpFormatter)
    arg_parser.add_argument(
               '-a','--adapter',action='store',
               type=int, help='brightness on ac',
               default=100,
               required=False)

    arg_parser.add_argument(
               '-b','--battery',action='store',
               type=int, help='brightness on battery',
               default=10,
               required=False)
    return arg_parser.parse_args()

def main():
    args = parse_args()

    while True:
        if on_ac_power():
            set_brightness(args.adapter)
            while on_ac_power():
                time.sleep(1)
        else:
            set_brightness(args.battery)
            while not on_ac_power():
                time.sleep(1)

if __name__ == "__main__": main()
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Why aren't there triple votes on askU? :) Interesting story. The first script you gave me only worked after I pull out the charger twice. This one works on the go. Now I really want to learn the advanced python, because I would like to add another "feature" where it "remembers" the level of brightness when the charger is plugged so when I remove it and place it back in, instead of going to max, it should go to the saved level state. This is just perfect Serg, I can't thank you enough. :D Would love if you could give me some tips on advanced python, so I can try write or "improve" this one :D – Tony Lancer Nov 04 '16 at 20:40