diff options
-rw-r--r-- | DEPENDENCIES | 2 | ||||
-rw-r--r-- | TODO | 1 | ||||
-rw-r--r-- | dist/archlinux/stable/PKGBUILD | 2 | ||||
-rwxr-xr-x | src/__main__.py | 1 | ||||
-rw-r--r-- | src/backlight.py | 97 |
5 files changed, 102 insertions, 1 deletions
diff --git a/DEPENDENCIES b/DEPENDENCIES index f3f17c6..cffcbe9 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -4,6 +4,8 @@ RUNTIME DEPENDENCIES: libxcb (opt-out, for randr, crtc identification and icc profile listing) libx11 (opt-out, for vidmode) libxxf86vm (opt-out, for vidmode) + adjbacklight (optional, for permission-hasslefree backlight adjustments) + linux (optional, for backlight support) argparser-python (https://github.com/maandree/argparser) @@ -5,7 +5,6 @@ High priority: Medium priority: Fix errors caused by SIGUSR2 - Add backlight support Low priority: Add wayland support diff --git a/dist/archlinux/stable/PKGBUILD b/dist/archlinux/stable/PKGBUILD index 9473ba7..f51a19f 100644 --- a/dist/archlinux/stable/PKGBUILD +++ b/dist/archlinux/stable/PKGBUILD @@ -8,6 +8,8 @@ arch=(i686 x86_64) url="https://github.com/maandree/blueshift" license=('AGPL3' 'GPL3' 'custom:GFDL1.3') depends=(python3 argparser libxcb libxxf86vm libx11) +optdepends=('adjbacklight: for backlight adjustments without root requirements' + 'linux: for backlight support') makedepends=(cython gcc python3 libxcb libxxf86vm libx11 make coreutils sed zip texinfo auto-auto-complete) install=blueshift.install source=($url/archive/$pkgver.tar.gz) diff --git a/src/__main__.py b/src/__main__.py index 1ea51b0..f66f974 100755 --- a/src/__main__.py +++ b/src/__main__.py @@ -40,6 +40,7 @@ from solar import * from curve import * from colour import * from monitor import * +from backlight import * config_file = None diff --git a/src/backlight.py b/src/backlight.py new file mode 100644 index 0000000..54090ae --- /dev/null +++ b/src/backlight.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 + +# Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import os +import sys +from subprocess import Popen + + +def list_backlights(): + ''' + List backlight controllers + + @return list<str> List of all backlight controllers on the system + ''' + return os.listdir('/sys/class/backlight') + + +class Backlight: + ''' + Backlight controller + + @variable maxmimum:int The maximum value, after minimum value modification + ''' + + def __init__(self, controller, adjbacklight = True, minimum = 0): + ''' + Constructor + + @param controller:str The name or path of the backlight controller + @param adjbacklight:bool Whether to the `adjbacklight` commmand when adjusting + @param minimum:int Artificially raise the minimum from zero to avoid the + backlight from getting stuck at zero when reached, + as happens on some controllers + ''' + self.__controller = controller + if '/' not in controller: + self.__controller = '/sys/class/backlight/%s' % controller + + with open('%s/max_brightness' % self.__controller, 'rb') as file: + self.maximum = int(file.read().decode('utf-8', 'replace')[:-1]) - minimum + + self.__minimum = minimum + self.__adjbacklight = adjbacklight + + + @property + def actual(self): + ''' + Get the actual brightness + + @return :int The brightness, can be below zero if the minimum value was modified at contruction + ''' + with open('%s/actual_brightness' % self.__controller, 'rb') as file: + return int(file.read().decode('utf-8', 'replace')[:-1]) - self.__minimum + + + @property + def brightness(self): + ''' + Get the brightness + + @return :int The brightness, can be below zero if the minimum value was modified at contruction + ''' + with open('%s/brightness' % self.__controller, 'rb') as file: + return int(file.read().decode('utf-8', 'replace')[:-1]) - self.__minimum + + + @brightness.setter + def brightness(self, value): + ''' + Set the brightness + + @param value:int The brightness, inside the range [0, `self.maxmimum`], can be below zero, + but should not be below zero, if the minimum value was modified at contruction + ''' + if not self.__adjbacklight: + with open('%s/brightness' % self.__controller, 'wb') as file: + file.write((str(value + self.__minimum) + '\n').encode('utf-8')) + file.flush() + else: + cmd = ['adjbacklight', self.__controller, '--set', str(value + self.__minimum)] + Popen(cmd, stdout = sys.stdout, stderr = sys.stderr).wait() + |