1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
4
# Copyright (C) 2015 Canonical
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
26
class BrightnessEnvironmentFixture(fixtures.Fixture):
28
def __init__(self, auto_brightness='disable', brightness_value=255):
29
self.auto_brightness = auto_brightness
30
self.brightness_value = str(brightness_value)
34
self.useFixture(AutoBrightness(state=self.auto_brightness))
35
self.useFixture(Brightness(value=self.brightness_value))
38
def _get_auto_brightness_current_setting():
39
"""Return current auto brightness state from gsettings."""
40
command = 'gsettings get com.ubuntu.touch.system auto-brightness'
41
return subprocess.check_output(shlex.split(command),
42
universal_newlines=True).strip()
45
def _set_auto_brightness_setting(value):
46
"""Change auto brightness from gsettings."""
47
command = 'gsettings set com.ubuntu.touch.system ' \
48
'auto-brightness {}'.format(value)
49
return subprocess.check_call(shlex.split(command))
52
class AutoBrightness(fixtures.Fixture):
54
def __init__(self, state):
59
old_value = _get_auto_brightness_current_setting()
60
self.addCleanup(_set_auto_brightness_setting, old_value)
62
if self.request == 'enable':
63
_set_auto_brightness_setting('true')
64
elif self.request == 'disable':
65
_set_auto_brightness_setting('false')
68
def _set_brightness_value(value):
69
"""Set brightness through powerd, should be between 10 and 255."""
70
command = 'powerd-cli brightness {}'.format(value)
71
subprocess.check_call(shlex.split(command),
72
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
75
def _get_current_brightness():
76
"""Return the current brightness value."""
77
command = 'gsettings get com.ubuntu.touch.system brightness'
78
return subprocess.check_output(shlex.split(command),
79
universal_newlines=True).strip()
82
class Brightness(fixtures.Fixture):
83
"""Fixture to set custom screen brightness."""
85
def __init__(self, value):
90
old_value = _get_current_brightness()
91
_set_brightness_value(self.value)
92
self.addCleanup(_set_brightness_value, old_value)