tuikit/driver_sdl.py
author Radek Brich <radek.brich@devl.cz>
Sun, 27 Jan 2013 12:54:52 +0100
changeset 69 4e7be77bafff
parent 62 2f61931520c9
child 70 db2eab0beb45
permissions -rw-r--r--
Add sdlterm to setup.py. Simplify color description, allow missing bg.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
'''SDL driver.
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
Requires C++ extension module.
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
See sdlterm directory.
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
'''
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
from tuikit.driver import Driver
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
import logging
54
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    12
import subprocess
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
import sys
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
sys.path.insert(0, 'sdlterm/build/lib.linux-x86_64-3.2')
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
from sdlterm import SDLTerminal
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
class DriverSDL(Driver):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
    '''SDL driver class'''
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
50
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    23
    color_map = {
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    24
        'black'         : 0,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    25
        'blue'          : 1,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    26
        'green'         : 2,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    27
        'cyan'          : 3,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    28
        'red'           : 4,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    29
        'magenta'       : 5,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    30
        'brown'         : 6,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    31
        'lightgray'     : 7,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    32
        'gray'          : 8,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    33
        'lightblue'     : 9,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    34
        'lightgreen'    : 10,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    35
        'lightcyan'     : 11,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    36
        'lightred'      : 12,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    37
        'lightmagenta'  : 13,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    38
        'yellow'        : 14,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    39
        'white'         : 15,
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    40
    }
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    41
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    42
    style_map = {
61
15088f62c4ac DriverSDL: Fix.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    43
        'bold'      : 1 << 24,  # bold font
15088f62c4ac DriverSDL: Fix.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    44
        'underline' : 1 << 25,  # underline text
15088f62c4ac DriverSDL: Fix.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    45
        'standout'  : 1 << 26,  # inverse bg/fg
15088f62c4ac DriverSDL: Fix.
Radek Brich <radek.brich@devl.cz>
parents: 57
diff changeset
    46
        'blink'     : 1 << 27,  # blinking text
50
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    47
    }
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    48
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
    def __init__(self):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
        '''Initialize instance attributes'''
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
        Driver.__init__(self)
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
        self.log = logging.getLogger('tuikit')
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
        self.sdlterm = SDLTerminal()
50
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    54
        self.colors = {}     # maps names to attributes
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
    55
        self.colorstack = [] # pushcolor/popcolor puts or gets attributes from this
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
    def start(self, mainfunc):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
        '''Start driver and run mainfunc.'''
54
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
    59
        self.select_system_font()
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
        self.sdlterm.resize(800, 600)
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
        self.size.w, self.size.h = self.sdlterm.width, self.sdlterm.height
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
        mainfunc()
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
    ## input ##
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
    def getevents(self, timeout=None):
57
911927edbdde DriverSDL: Add support for timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
    68
        '''Process input, return list of events.
911927edbdde DriverSDL: Add support for timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
    69
911927edbdde DriverSDL: Add support for timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
    70
        timeout -- float, in seconds
911927edbdde DriverSDL: Add support for timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
    71
911927edbdde DriverSDL: Add support for timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
    72
        '''
911927edbdde DriverSDL: Add support for timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
    73
        if timeout is not None:
911927edbdde DriverSDL: Add support for timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
    74
            timeout = int(timeout*1000)
911927edbdde DriverSDL: Add support for timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
    75
        event = self.sdlterm.wait_event(timeout)
911927edbdde DriverSDL: Add support for timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
    76
        if not event:
911927edbdde DriverSDL: Add support for timeouts.
Radek Brich <radek.brich@devl.cz>
parents: 54
diff changeset
    77
            return []
53
c4263588b716 DriverSDL: Implement cursor, handle window resize.
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
    78
        if event[0] == 'resize':
c4263588b716 DriverSDL: Implement cursor, handle window resize.
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
    79
            self.size.w, self.size.h = self.sdlterm.width, self.sdlterm.height
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
        return [event]
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    81
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
    ## drawing ##
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
    def erase(self):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
        '''Clear screen.'''
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
        self.sdlterm.erase()
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
    def putch(self, x, y, c):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
        '''Output one unicode character to specified coordinates.'''
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
        if not self.clipstack.test(x, y):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
            return
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
        self.sdlterm.putch(x, y, c)
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
    def commit(self):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
        '''Commit changes to the screen.'''
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
        self.sdlterm.commit()
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
    ## colors ##
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
    def setcolor(self, name, desc):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   103
        '''Define color name.
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   104
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   105
        name - name of color (e.g. 'normal', 'active')
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   106
        desc - color description - foreground, background, attributes (e.g. 'black on white, bold')
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   107
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   108
        '''
50
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   109
        parts = desc.split(',')
69
4e7be77bafff Add sdlterm to setup.py. Simplify color description, allow missing bg.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   110
        fgbg = parts[0].split(' on ', 1)
4e7be77bafff Add sdlterm to setup.py. Simplify color description, allow missing bg.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   111
        fg = fgbg[0]
4e7be77bafff Add sdlterm to setup.py. Simplify color description, allow missing bg.
Radek Brich <radek.brich@devl.cz>
parents: 62
diff changeset
   112
        bg = fgbg[1:] and fgbg[1] or 'black'
50
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   113
        style = parts[1:]
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   114
        fg = self._color_by_name(fg)
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   115
        bg = self._color_by_name(bg)
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   116
        style = self._style_by_names(style)
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   117
        self.colors[name] = self.sdlterm.prepare_attr(fg, bg, style);
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   118
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   119
    def pushcolor(self, name):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   120
        '''Add color on top of stack and use this color for following output.'''
50
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   121
        if len(self.colorprefix):
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   122
            prefixname = self.colorprefix[-1] + name
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   123
            if prefixname in self.colors:
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   124
                name = prefixname
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   125
        attr = self.colors[name]
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   126
        self.sdlterm.set_attr(attr)
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   127
        self.colorstack.append(attr)
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   128
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   129
    def popcolor(self):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   130
        '''Remove color from top of stack and use new top color for following output.'''
50
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   131
        self.colorstack.pop()
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   132
        if len(self.colorstack):
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   133
            attr = self.colorstack[-1]
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   134
        else:
62
2f61931520c9 Rework layouts: Layout is now normal Container which places its children upon resize event.
Radek Brich <radek.brich@devl.cz>
parents: 61
diff changeset
   135
            attr = 7
50
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   136
        self.sdlterm.set_attr(attr)
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   137
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   138
    def _color_by_name(self, name):
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   139
        name = name.lower().strip()
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   140
        return self.color_map[name]
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   141
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   142
    def _style_by_names(self, names):
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   143
        style = 0
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   144
        for name in names:
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   145
            name = name.lower().strip()
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   146
            style |= self.style_map[name]
c5b8b9d2da95 DriverSDL: Implement colors.
Radek Brich <radek.brich@devl.cz>
parents: 48
diff changeset
   147
        return style
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
    ## cursor ##
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   151
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
    def showcursor(self, x, y):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
        '''Set cursor to be shown at x, y coordinates.'''
53
c4263588b716 DriverSDL: Implement cursor, handle window resize.
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   154
        if not self.clipstack.test(x, y):
c4263588b716 DriverSDL: Implement cursor, handle window resize.
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   155
            return
c4263588b716 DriverSDL: Implement cursor, handle window resize.
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   156
        self.sdlterm.show_cursor(x, y)
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   157
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   158
    def hidecursor(self):
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   159
        '''Hide cursor.'''
53
c4263588b716 DriverSDL: Implement cursor, handle window resize.
Radek Brich <radek.brich@devl.cz>
parents: 50
diff changeset
   160
        self.sdlterm.hide_cursor()
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
54
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   162
    ## private utility ##
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   163
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   164
    def _lookup_system_font(self, family, style):
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   165
        """Find system font by family name and style.
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   166
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   167
        style -- font style: 'normal', 'bold'
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   168
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   169
        Returns font file name.
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   170
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   171
        Uses fontconfig, it must be installed in system.
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   172
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   173
        """
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   174
        return subprocess.check_output([
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   175
            'fc-match',
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   176
            '%s:style=%s:fontformat=truetype:spacing=mono' % (family, style),
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   177
            '-f%{file}'])
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   178
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   179
    def select_system_font(self, family='monospace'):
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   180
        """Search for system fonts by family name and use them.
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   181
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   182
        Example family names:
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   183
            'monospace' - default monospace system font
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   184
            'DejaVu Sans Mono'
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   185
            'Liberation Mono'
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   186
        """
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   187
        fname_regular = self._lookup_system_font(family, 'normal')
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   188
        fname_bold = self._lookup_system_font(family, 'bold')
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   189
        self.log.info('DriverSDL: using fonts:\n%s\n%s', fname_regular.decode(), fname_bold.decode())
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   190
        self.sdlterm.select_font(fname_regular, fname_bold, 12)
86b84535726e DriverSDL: system font lookup.
Radek Brich <radek.brich@devl.cz>
parents: 53
diff changeset
   191
48
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   192
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   193
driverclass = DriverSDL
1f00e90fd72a Add SDL driver prototype. Update sdlterm: Handle keyboard, mouse events. Add glyph cache.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   194