亮度配置文件 Ubuntu 14.04.5

亮度配置文件 Ubuntu 14.04.5

通常情况下,当没有充电器时,笔记本电脑屏幕会调至最低亮度,如果充电器存在或已连接,则屏幕亮度会调至最高。但是,在我的 Dell Inspiron 7559 Skylake 笔记本电脑上,这种情况不会发生。我的笔记本电脑亮度始终为 100%。我如何才能恢复此“功能”?

答案1

介绍

下面的脚本是我以前的脚本的修改版本,用 python 编写,专门使用 dbus 来轮询 ac_adapter 的存在并设置屏幕亮度。

用法

使用方法很简单:从命令行调用

python ./brightness_control.py

脚本默认交流电亮度为 100%,电池亮度为 10%。用户可以使用-a-b分别设置交流电和电池所需的亮度级别。

AS 选项给出-h

$ ./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

例如,可以执行以下任一操作:

# 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

来源

也可在GitHub

#!/usr/bin/env python
"""
Author: Serg Kolo <[email protected]>
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()

相关内容