Subversion Repositories mmkeyboard2k

Rev

Blame | Last modification | View Log | RSS feed

# MMKeyboard2k ACPI Control Plugin
# Copyright (C) 2011  Jan Janech

# 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 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 os.path
import subprocess

class Plugin:
    options = ('property', 'value')
    
    MIN_BRIGHTNESS=0
    
    try:
        MAX_BRIGHTNESS=int(file('/sys/class/backlight/acpi_video0/max_brightness', 'r').read())
    except:
        MAX_BRIGHTNESS=0
    
    def __init__(self, property, value, text = 'brightness'):
        self.property = property.lower()
        self.text = text
        
        if value[0] == '+':
            self.value = ('+', int(value[1:]))
        elif value[0] == '-':
            self.value = ('-', int(value[1:]))
        else:
            self.value = ('=', int(value))
    
    def __repr__(self):
        return '<acpi.Plugin %s=%r>'%(self.property, self.value)
    
    def __call__(self):
        if self.property == 'brightness':
            if self.MAX_BRIGHTNESS <= self.MIN_BRIGHTNESS:
                return None
            val = old_val = int(file('/sys/class/backlight/acpi_video0/brightness', 'r').read())
            
            if self.value[0] == '+':
                val += self.value[1]
            elif self.value[0] == '-':
                val -= self.value[1]
            else:
                val = self.value[1]
            
            if val < self.MIN_BRIGHTNESS:
                val = self.MIN_BRIGHTNESS
            if val > self.MAX_BRIGHTNESS:
                val = self.MAX_BRIGHTNESS
            
            if val != old_val:
                file('/sys/class/backlight/acpi_video0/brightness', 'w').write(str(val))
            
            return self.text + '\n' + str(val)