1#!/usr/bin/env python3

2# -*- coding: utf-8 -*-

3#

4# brightness_test.py

5#

6# This file is part of Checkbox.

7#

8# Copyright 2012-2018 Canonical Ltd.

9#

10# Authors:

11# Alberto Milone <alberto.milone@canonical.com>

12# Sylvain Pineau <sylvain.pineau@canonical.com>

13#

14# Checkbox is free software: you can redistribute it and/or modify

15# it under the terms of the GNU General Public License version 3,

16# as published by the Free Software Foundation.

17

18#

19# Checkbox is distributed in the hope that it will be useful,

20# but WITHOUT ANY WARRANTY; without even the implied warranty of

21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

22# GNU General Public License for more details.

23#

24# You should have received a copy of the GNU General Public License

25# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

26

27 import sys

28 import os

29 import time

30

31 from glob import glob

32

33

34 class Brightness(object):

35 def __init__(self, path="/sys/class/backlight"):

36 self.sysfs_path = path

37 self.interfaces = self._get_interfaces_from_path()

38

39 def read_value(self, path):

40 """Read the value from a file"""

41 # See if the source is a file or a file object

42 # and act accordingly

43 file = path

44 if file is None:

45 lines_list = []

46 else:

47 # It's a file

48 if not hasattr(file, "write"):

49 myfile = open(file, "r")

50 lines_list = myfile.readlines()

51 myfile.close()

52 # It's a file object

53 else:

54 lines_list = file.readlines()

55

56 return int("".join(lines_list).strip())

57

58 def write_value(self, value, path, test=None):

59 """Write a value to a file"""

60 value = "%d" % value

61 # It's a file

62 if not hasattr(path, "write"):

63 if test:

64 path = open(path, "a")

65 else:

66 path = open(path, "w")

67 path.write(value)

68 path.close()

69 # It's a file object

70 else:

71 path.write(value)

72

73 def get_max_brightness(self, path):

74 full_path = os.path.join(path, "max_brightness")

75

76 return self.read_value(full_path)

77

78 def get_actual_brightness(self, path):

79 full_path = os.path.join(path, "actual_brightness")

80

81 return self.read_value(full_path)

82

83 def get_last_set_brightness(self, path):

84 full_path = os.path.join(path, "brightness")

85

86 return self.read_value(full_path)

87

88 def _get_interfaces_from_path(self):

89 """check all the files in a directory looking for quirks"""

90 interfaces = []

91 if os.path.isdir(self.sysfs_path):

92 for d in glob(os.path.join(self.sysfs_path, "*")):

93 if os.path.isdir(d):

94 interfaces.append(d)

95

96 return interfaces

97

98 def was_brightness_applied(self, interface):

99 """See if the selected brightness was applied

100

101 Note: this doesn't guarantee that screen brightness

102 changed.

103 """

104 if (

105 abs(

106 self.get_actual_brightness(interface)

107 - self.get_last_set_brightness(interface)

108 )

109 > 1

110 ):

111 return 1

112 else:

113 return 0

114

115

116 def main():

117 brightness = Brightness()

118

119 # Make sure that we have root privileges

120 if os.geteuid() != 0:

121 print("Error: please run this program as root", file=sys.stderr)

122 exit(1)

123

124 # If no backlight interface can be found

125 if len(brightness.interfaces) == 0:

126 exit(1)

127

128 exit_status = 0

129 for interface in brightness.interfaces:

130

131 # Get the current brightness which we can restore later

132 current_brightness = brightness.get_actual_brightness(interface)

133

134 # Get the maximum value for brightness

135 max_brightness = brightness.get_max_brightness(interface)

136

137 # Set the brightness to half the max value

138 brightness.write_value(

139 max_brightness / 2, os.path.join(interface, "brightness")

140 )

141

142 # Check that "actual_brightness" reports the same value we

143 # set "brightness" to

144 exit_status += brightness.was_brightness_applied(interface)

145

146 # Wait a little bit before going back to the original value

147 time.sleep(2)

148

149 # Set the brightness back to its original value

150 brightness.write_value(

151 current_brightness, os.path.join(interface, "brightness")

152 )

153

154 exit(exit_status)

155

156

157 if __name__ == "__main__":

158 main()