Subversion Repositories mmkeyboard2k

Rev

Rev 3 | Blame | Compare with Previous | Last modification | View Log | RSS feed

# MMKeyboard2k On Screen Display Plugin
# Copyright (C) 2007  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 pyosd

color = '#CFCF00'
timeout = 3
position = pyosd.POS_BOT
shadow = True
font = "-adobe-helvetica-bold-r-*-*-*-240-100-*-*-*-*-*"


class Plugin:
    osd = None
    maxlines = 1
    
    options = ('text', 'type', 'linecount')
    stdin = 'text'
    
    @staticmethod
    def config(c = None, t = None, p = None, s = None, f = None):
        global color, timeout, position, shadow, font
        
        if c:
            color = c
        if t:
            timeout = t
        if p:
            position = p
        if s:
            shadow = s
        if f:
            font = f
    
    def __init__(self, text, type = 'text', linecount = 1):
        type = type.lower()
        linecount = int(linecount)
        
        assert type in ('text', 'percent')
        
        if type == 'percent' and linecount == 1:
            linecount = 2
        
        self.text = text
        self.type = type
        self.linecount = linecount
        if linecount > self.maxlines:
            self.__class__.maxlines = linecount
    
    def __repr__(self):
        return '<osd.Plugin %r %s %d>'%(self.text, self.type, self.linecount)
    
    def __call__(self):
        if self.osd is None:
            self.__class__.osd = pyosd.osd(font=font, colour = color, timeout = timeout, pos = position, shadow = shadow, lines = self.maxlines, offset=3)
        
        if callable(self.text):
            text = str(self.text())
        else:
            text = str(self.text)
        
        if text is None:
            return
        
        if self.type == 'text':
            for id, line in enumerate([""]*(self.maxlines-self.linecount)+([""]*self.linecount+text.splitlines())[-self.linecount:]):
                self.osd.display(line, line = id)
        else:
            for id in xrange(self.maxlines - 2):
                self.osd.display("", line = id)
            s, v = (text+'\n0').splitlines()[:2]
            v = int(v)
            self.osd.display("%s(%d%%)"%(s, v), line = self.maxlines - 2)
            self.osd.display(v, type = pyosd.TYPE_PERCENT, line = self.maxlines - 1)