Ubuntu 13.04 不能保存屏幕亮度的补丁

安装Ubuntu后,再安装屏幕亮度指示器(Indicator Brightness), 发现每次重启后亮度都会恢复到最大,无法记忆上次设置的屏幕亮度.经查看,这个指示器是python写的,脚本路径是/opt/extras.ubuntu.com/indicator-brightness/indicator-brightness, 决定在修改脚本,增加自动保存亮度的代码.

使用任何文件编辑器打开它, 在开头添加如下两个方法来保存和加载亮度数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import os
import os.path
filepath = os.path.expanduser('~')+"/.brightness"
def save_to_file(rec):
try:
savefile = open(filepath, "w")
savefile.write("%s" % rec)
savefile.close()
except:
savefile = ""
def read_from_file():
lastBrightness = 0
try:
savefile = open(filepath, "r")
lastBrightness = int(savefile.read())
savefile.close()
except:
lastBrightness = 0
return lastBrightness

这里,我把当前亮度保存$HOME/.brightness文件里.
再添加对这两个方法的调用
if __name__ == "__main__":后面添加

1
2
3
rec = read_from_file()
if rec != 0:
set_brightness(rec)

读取保存的亮度记录,并设置

同时修改set_brightness方法为:

1
2
3
4
def set_brightness(brightness):
subprocess.call(['pkexec','/usr/lib/gnome-settings-daemon/gsd-backlight-helper','--set-brightness',"%s" % brightness])
save_to_file(brightness)
create_menu(ind)

保存indicator-brightness文件,并修复权限

sudo chown root:root indicator-brightness
sudo chmod 755 indicator-brightness

注销,重新登录,生效

附件为我修改的indicator-brightness文件, 这里下载Download