File: gui_term.py

package info (click to toggle)
openipmi 2.0.7-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,200 kB
  • ctags: 14,662
  • sloc: ansic: 126,919; sh: 9,454; python: 6,885; perl: 5,838; makefile: 507
file content (307 lines) | stat: -rw-r--r-- 9,780 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# gui_term.py
#
# openipmi GUI terminal handling for SoL
#
# Author: MontaVista Software, Inc.
#         Corey Minyard <minyard@mvista.com>
#         source@mvista.com
#
# Copyright 2006 MontaVista Software Inc.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public License
#  as published by the Free Software Foundation; either version 2 of
#  the License, or (at your option) any later version.
#
#
#  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
#  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
#  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
#  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this program; if not, write to the Free
#  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import Tix
import _term

def gpos(x, y):
    return str(y+1) + "." + str(x)
        
class Terminal(_term.TerminalEmulator):
    def __init__(self, parent):
        self.fsize = 8

        _term.TerminalEmulator.__init__(self)

        self.fonts = ("-family terminal -size " + str(self.fsize)
                      + " -weight normal -underline 0",
                      # Bold
                      "-family terminal -size " + str(self.fsize)
                      + " -weight bold -underline 0",
                      # Underline
                      "-family terminal -size " + str(self.fsize)
                      + " -weight normal -underline 1",
                      # Bold Underline
                      "-family terminal -size " + str(self.fsize)
                      + " -weight bold -underline 1")

        self.colors = ("black",
                       "red",
                       "green",
                       "yellow",
                       "blue",
                       "magenta",
                       "cyan",
                       "white")

        self.text = Tix.Text(parent, width=self.width, height=self.height,
                             state="normal", wrap="none", font=self.fonts[0])
        self.text.pack(side=Tix.TOP, fill=Tix.BOTH, expand=1)

        # We create a tag for every possible terminal configuration, based
        # upon an equation using the colors and font type.
        for f in range(0, len(self.fonts)):
            for fg in range(0, len(self.colors)):
                for bg in range(0, len(self.colors)):
                    idx = (f * 8 * 8) + (fg * 8) + bg;
                    self.text.tag_configure(str(idx), font=self.fonts[f],
                                            foreground=self.colors[fg],
                                            background=self.colors[bg])
                    pass
                pass
            pass

        self.default_tag = str(7 * 8) # white forground, black background

        for i in range(1, self.height+1):
            self.text.insert(str(i) + ".1", "%*s\n" % (self.width, ""),
                             self.default_tag)
            pass

        self.handle_cursor()

        self.text.bind("<Key>", self.HandleChar)
        self.text.bind("<Control-Key>", self.HandleControlChar)

        self.text.focus_set()
        return

    def DrawText(self, fg_color, bg_color, flags, x, y, val):
        if (flags & _term.INVERSE):
            tmp = bg_color
            bg_color = fg_color
            fg_color = tmp
            pass
        # FIXME - we don't handle blinking or concealed
        flags &= 3
        tag = str((flags * 8 * 8) + (fg_color * 8) + bg_color);
        self.text.delete(gpos(x, y), gpos(x+len(val), y))
        self.text.insert(gpos(x, y), val, tag)
        return

    def DrawCursor(self, fg_color, bg_color, flags, x, y, val):
        # Draw cursor as inverse, just flip the background and foreground
        self.DrawText(bg_color, fg_color, flags, x, y, val)
        return

    def ScrollLines(self, y1, y2):
        self.text.delete(gpos(0, y1), gpos(0, y1+1))
        if (y2 == self.height-1):
            self.text.insert("end", "\n%*s" % (self.width, ""),
                             self.default_tag)
            pass
        else:
            # Not (y2+1), because we deleted a line
            pos = gpos(0, y2)
            self.text.insert(pos, "%*s\n" % (self.width, ""),
                             self.default_tag)
            pass
        return
    
    def ScrollLinesUp(self, y1, y2):
        self.text.delete(gpos(0, y2), gpos(0, y2+1))
        if (y1 == self.height-1):
            self.text.insert("end", "\n%*s" % (self.width, ""),
                             self.default_tag)
            pass
        else:
            pos = gpos(0, y1)
            self.text.insert(pos, "%*s\n" % (self.width, ""),
                             self.default_tag)
            pass
        return

    def DeleteChars(self, x, y, len):
        pos = gpos(x, y)
        endpos = gpos(x+len, y)
        self.text.delete(pos, endpos)
        self.text.insert(str(y+1) + ".end", "%*s" % (len, ""),
                         self.default_tag)
        return
    
    def InsertChars(self, x, y, len):
        self.text.delete(gpos(self.width-len, y), gpos(self.width, y))
        self.text.insert(gpos(x, y), "%*s" % (len, ""),
                         self.default_tag)
        return
    
    def SendBack(self, data):
        self.HandleTerminalOutput(data)
        return

    def Bell(self):
        return

    def RequestSizeChange(self, w, h):
        return
    
    def HandleChar(self, event):
        key = event.keysym
        if (len(key) == 1):
            s = key
        elif (key == "Return") or (key == "KP_Enter"):
            s = "\x0d"
        elif (key == "Backspace"):
            s = "\x08"
        elif (key == "Up"):
            s = "\x1b[A"
        elif (key == "Down"):
            s = "\x1b[B"
        elif (key == "Right"):
            s = "\x1b[C"
        elif (key == "Left"):
            s = "\x1b[D"
        elif (key == "Next"):
            s = "\x1b[6~"
        elif (key == "Prior"):
            s = "\x1b[5~"
        elif (key == "Insert"):
            s = "\x1b[2~"
        elif (key == "Home"):
            s = "\x1b[OH"
        elif (key == "End"):
            s = "\x1b[OF"
        elif (key == "Delete"):
            s = "\x1b[3~"
        elif (key == "F1"):
            s = "\x1bOP"
        elif (key == "F2"):
            s = "\x1bOQ"
        elif (key == "F3"):
            s = "\x1bOR"
        elif (key == "F4"):
            s = "\x1bOS"
        elif (key == "F5"):
            s = "\x1b[15~"
        elif (key == "F6"):
            s = "\x1b[17~"
        elif (key == "F7"):
            s = "\x1b[18~"
        elif (key == "F8"):
            s = "\x1b[19~"
        elif (key == "F9"):
            s = "\x1b[20~"
        elif (key == "F10"):
            s = "\x1b[21~"
        elif (key == "F11"):
            s = "\x1b[23~"
        elif (key == "F12"):
            s = "\x1b[24~"
        elif (len(event.char) == 1) and (event.char < chr(255)):
            s = event.char

        # Keypad stuff comes after the check because if numlock is off, the
        # event.char will be empty
        elif (key == "KP_Add"):
            s = "\x1bOl"
        elif (key == "KP_Subtract"):
            s = "\x1bOm"
        elif (key == "KP_Delete"):
            s = "\x1bOn"
        elif (key == "KP_Multiply"):
            s = "\x1bOQ"
        elif (key == "KP_Divide"):
            s = "\x1bOR"
        elif (key == "KP_Insert"):
            s = "\x1bOp"
        elif (key == "KP_End"):
            s = "\x1bOq"
        elif (key == "KP_Down"):
            s = "\x1bOr"
        elif (key == "KP_Next"):
            s = "\x1bOs"
        elif (key == "KP_Left"):
            s = "\x1bOt"
        elif (key == "KP_Begin"):
            s = "\x1bOu"
        elif (key == "KP_Right"):
            s = "\x1bOv"
        elif (key == "KP_Home"):
            s = "\x1bOw"
        elif (key == "KP_Up"):
            s = "\x1bOx"
        elif (key == "KP_Prior"):
            s = "\x1bOy"
        else:
            return "break"
        self.HandleTerminalOutput(s)
        return "break"
    
    def HandleControlChar(self, event):
        key = event.keysym
        if (len(key) != 1):
            return
        if ((key >= 'A') and (key <= 'B')): # '@' 'A' .. 'Z'
            s = "%c" % (ord(key)-64)
            pass
        elif ((key >= 'a') and (key <= 'z')): # 'a' .. 'z'
            s = "%c" % (ord(key)-96)
        elif (key < ' '):
            s = key
            pass
        else:
            return "break"
        self.HandleTerminalOutput(s)
        return "break"
    
    pass

import sys
class TestTerm(Terminal):
    def __init__(self, parent):
        Terminal.__init__(self, parent)
        return

    def HandleTerminalOutput(self, s):
        # FIXME: hacks to remove
        sys.stdout.write(s)
        sys.stdout.flush()
        if (s[0] == '\x01'):
            self.ProcessInput("abcdefghijk")
            return
        self.ProcessInput(s)
        return
    pass

class MyApp(Tix.Tk):
    def __init__(self):
        Tix.Tk.__init__(self)
        term = TestTerm(self)
        return
    
    pass

if __name__ == "__main__":
    app = MyApp()
    app.mainloop()