~jkakar/console-presenter/two-line-separator

« back to all changes in this revision

Viewing changes to unix_eventqueue.py

  • Committer: Elliot Murphy
  • Date: 2008-11-11 17:27:14 UTC
  • Revision ID: elliot@elliotmurphy.com-20081111172714-t6se3183s4dzof2f
Imported Michaels console presenter tool.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#   Copyright 2000-2004 Michael Hudson mwh@python.net
 
2
#
 
3
#                        All Rights Reserved
 
4
#
 
5
#
 
6
# Permission to use, copy, modify, and distribute this software and
 
7
# its documentation for any purpose is hereby granted without fee,
 
8
# provided that the above copyright notice appear in all copies and
 
9
# that both that copyright notice and this permission notice appear in
 
10
# supporting documentation.
 
11
#
 
12
# THE AUTHOR MICHAEL HUDSON DISCLAIMS ALL WARRANTIES WITH REGARD TO
 
13
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 
14
# AND FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
 
15
# INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 
16
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 
17
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 
18
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
19
 
 
20
# Bah, this would be easier to test if curses/terminfo didn't have so
 
21
# much non-introspectable global state.
 
22
 
 
23
import keymap
 
24
from console import Event
 
25
import curses
 
26
from termios import tcgetattr, VERASE
 
27
 
 
28
_keynames = {
 
29
    "delete" : "kdch1",
 
30
    "down" : "kcud1",
 
31
    "end" : "kend",
 
32
    "enter" : "kent",
 
33
    "f1"  : "kf1",    "f2"  : "kf2",    "f3"  : "kf3",    "f4"  : "kf4",
 
34
    "f5"  : "kf5",    "f6"  : "kf6",    "f7"  : "kf7",    "f8"  : "kf8",
 
35
    "f9"  : "kf9",    "f10" : "kf10",   "f11" : "kf11",   "f12" : "kf12",
 
36
    "f13" : "kf13",   "f14" : "kf14",   "f15" : "kf15",   "f16" : "kf16",
 
37
    "f17" : "kf17",   "f18" : "kf18",   "f19" : "kf19",   "f20" : "kf20",
 
38
    "home" : "khome",
 
39
    "insert" : "kich1",
 
40
    "left" : "kcub1",
 
41
    "page down" : "knp",
 
42
    "page up"   : "kpp",
 
43
    "right" : "kcuf1",
 
44
    "up" : "kcuu1",
 
45
    }
 
46
 
 
47
class EventQueue(object):
 
48
    def __init__(self, fd):
 
49
        our_keycodes = {}
 
50
        for key, tiname in _keynames.items():
 
51
            keycode = curses.tigetstr(tiname)
 
52
            if keycode:
 
53
                our_keycodes[keycode] = unicode(key)
 
54
        our_keycodes[tcgetattr(fd)[6][VERASE]] = u'backspace'
 
55
        self.k = self.ck = keymap.compile_keymap(our_keycodes)
 
56
        self.events = []
 
57
        self.buf = []
 
58
    def get(self):
 
59
        if self.events:
 
60
            return self.events.pop(0)
 
61
        else:
 
62
            return None
 
63
    def empty(self):
 
64
        return not self.events
 
65
    def insert(self, event):
 
66
        self.events.append(event)
 
67
    def push(self, char):
 
68
        if char in self.k:
 
69
            k = self.k[char]
 
70
            if isinstance(k, dict):
 
71
                self.buf.append(char)
 
72
                self.k = k
 
73
            else:
 
74
                self.events.append(Event('key', k, ''.join(self.buf) + char))
 
75
                self.buf = []
 
76
                self.k = self.ck
 
77
        elif self.buf:
 
78
            self.events.extend([Event('key', c, c) for c in self.buf])
 
79
            self.buf = []
 
80
            self.k = self.ck
 
81
            self.push(char)
 
82
        else:
 
83
            self.events.append(Event('key', char, char))