fork(1) download
  1. #!/usr/bin/python
  2.  
  3. # Brightness changer script for Linux systems that don't really support brightness.
  4. # Created by Oleh 'BlaXpirit' Prypin.
  5.  
  6. # Tested with Python 2.7 and 3.2, but Python 3 is recommended.
  7.  
  8. usage="""
  9. brightness
  10. - get current brightness setting
  11.  
  12. brightness restore
  13. - set the brightness to the last used value
  14.  
  15. brightness <value>
  16. - set the brightness to *value*
  17.  
  18. brightness inc [step]
  19. brightness dec [step]
  20. - increase or decrease the brightness by *step* (if it's not specified, a default value is used from the configuration file, usually 10% of maximal brightness)
  21. """
  22.  
  23. config_file='/etc/bx_brightness.conf'
  24.  
  25. ########################################
  26.  
  27. import sys,os,collections,subprocess
  28.  
  29. def find_backlight_device():
  30. path='/sys/class/backlight/'
  31. try:
  32. return os.path.join(path,os.listdir(path)[0])
  33. except:
  34. print('No backlight device found!')
  35. exit(2)
  36.  
  37. # Settings: (name,(type,default))
  38. s=collections.OrderedDict((
  39. ('device_path' ,(str,find_backlight_device)),
  40. ('min_brightness',(int,lambda:0)),
  41. ('max_brightness',(int,lambda:int(open(os.path.join(s['device_path'],'max_brightness')).read()))),
  42. ('step' ,(int,lambda:max(1,(s['max_brightness']-s['min_brightness'])//10))),
  43. ('brightness' ,(int,lambda:int(open(os.path.join(s['device_path'],'brightness')).read()))),
  44. ))
  45.  
  46. # Read settings from config file
  47. try:
  48. for l in open(config_file).read().split('\n'):
  49. l=l.split('=',1)
  50. if len(l)==2 and l[0] in s:
  51. s[l[0]]=s[l[0]][0](l[1])
  52. except:
  53. pass
  54.  
  55. # Replace the settings that couldn't be read with defaults
  56. for k,v in s.items():
  57. if isinstance(v,tuple):
  58. s[k]=v[1]()
  59.  
  60.  
  61. # Makes the number *v* be in range [*minv*, *maxv*]
  62. def btw(minv,v,maxv):
  63. if v<minv: v=minv
  64. if v>maxv: v=maxv
  65. return v
  66. # Sets the brightess setting and writes it to the 'brightness' file
  67. def set_brightness(b):
  68. s['brightness']=btw(s['min_brightness'],b,s['max_brightness'])
  69. with open(os.path.join(s['device_path'],'brightness'),'w') as f:
  70. f.write(str(s['brightness']))
  71.  
  72. p=sys.argv[1:]
  73. # brightness
  74. if len(p)==0:
  75. print('{}/{}'.format(s['brightness'],s['max_brightness']))
  76. exit(0)
  77. # brightness restore
  78. elif len(p)==1 and p[0]=='restore':
  79. subprocess.call(['chmod','666',os.path.join(s['device_path'],'brightness')])
  80. set_brightness(s['brightness'])
  81. # brightness <value>
  82. elif len(p)==1 and p[0].isdigit():
  83. set_brightness(int(p[0]))
  84. # brightness inc [step]
  85. # brightness dec [step]
  86. elif p[0] in ('inc','dec'):
  87. try:
  88. step=int(p[1])
  89. except:
  90. step=s['step']
  91. set_brightness(s['brightness']+step*(1 if p[0]=='inc' else -1))
  92. else:
  93. print(usage)
  94. exit(1)
  95.  
  96.  
  97. # Save settings
  98. e=os.path.exists(config_file)
  99. with open(config_file,'w') as f:
  100. f.write('\n'.join('{}={}'.format(*i) for i in s.items()))
  101. if not e: subprocess.call(['chmod','666',config_file])
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty