/usr/lib/plainbox-provider-checkbox/bin/brightness_test is in plainbox-provider-checkbox 0.25-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | #!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# brightness_test
#
# This file is part of Checkbox.
#
# Copyright 2012 Canonical Ltd.
#
# Authors: Alberto Milone <alberto.milone@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import time
from sys import stdout, stderr
from glob import glob
class Brightness(object):
def __init__(self, path='/sys/class/backlight'):
self.sysfs_path = path
self._interfaces = self._get_interfaces_from_path()
def read_value(self, path):
'''Read the value from a file'''
# See if the source is a file or a file object
# and act accordingly
file = path
if file == None:
lines_list = []
else:
# It's a file
if not hasattr(file, 'write'):
myfile = open(file, 'r')
lines_list = myfile.readlines()
myfile.close()
# It's a file object
else:
lines_list = file.readlines()
return int(''.join(lines_list).strip())
def write_value(self, value, path, test=None):
'''Write a value to a file'''
value = '%d' % value
# It's a file
if not hasattr(path, 'write'):
if test:
path = open(path, 'a')
else:
path = open(path, 'w')
path.write(value)
path.close()
# It's a file object
else:
path.write(value)
def get_max_brightness(self, path):
full_path = os.path.join(path, 'max_brightness')
return self.read_value(full_path)
def get_actual_brightness(self, path):
full_path = os.path.join(path, 'actual_brightness')
return self.read_value(full_path)
def get_last_set_brightness(self, path):
full_path = os.path.join(path, 'brightness')
return self.read_value(full_path)
def _get_interfaces_from_path(self):
'''check all the files in a directory looking for quirks'''
interfaces = []
if os.path.isdir(self.sysfs_path):
for d in glob(os.path.join(self.sysfs_path, '*')):
if os.path.isdir(d):
interfaces.append(d)
return interfaces
def get_best_interface(self):
'''Get the best acpi interface'''
# Follow the heuristic in https://www.kernel.org/doc/Documentation/
#ABI/stable/sysfs-class-backlight
if len(self._interfaces) == 0:
return None
else:
interfaces = {}
for interface in self._interfaces:
try:
with open(interface + '/type') as type_file:
iface_type = type_file.read().strip()
except IOError:
continue
interfaces[iface_type] = interface
if interfaces.get('firmware'):
return interfaces.get('firmware')
elif interfaces.get('platform'):
return interfaces.get('platform')
elif interfaces.get('raw'):
return interfaces.get('raw')
else:
fallback_type = sorted(interfaces.keys())[0]
print("WARNING: no interface of type firmware/platform/raw "
"found. Using {} of type {}".format(
interfaces[fallback_type],
fallback_type))
return interfaces[fallback_type]
def was_brightness_applied(self, interface):
'''See if the selected brightness was applied
Note: this doesn't guarantee that screen brightness
changed.
'''
if (abs(self.get_actual_brightness(interface) -
self.get_last_set_brightness(interface)) > 1):
return False
else:
return True
def main():
brightness = Brightness()
# Make sure that we have root privileges
if os.geteuid() != 0:
print('Error: please run this program as root',
file=sys.stderr)
exit(1)
interface = brightness.get_best_interface()
# If no backlight interface can be found
if not interface:
exit(1)
# Get the current brightness which we can restore later
current_brightness = brightness.get_actual_brightness(interface)
# Get the maximum value for brightness
max_brightness = brightness.get_max_brightness(interface)
# Set the brightness to half the max value
brightness.write_value(max_brightness / 2,
os.path.join(interface,
'brightness'))
# Check that "actual_brightness" reports the same value we
# set "brightness" to
exit_status = not brightness.was_brightness_applied(interface)
# Wait a little bit before going back to the original value
time.sleep(2)
# Set the brightness back to its original value
brightness.write_value(current_brightness,
os.path.join(interface,
'brightness'))
exit(exit_status)
if __name__ == '__main__':
main()
|