File: jack_term.py

package info (click to toggle)
jack 3.1.1+cvs20050801-31
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,316 kB
  • sloc: python: 6,358; ansic: 1,910; makefile: 6; sh: 5
file content (200 lines) | stat: -rwxr-xr-x 5,430 bytes parent folder | download
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
### jack_term: terminal specific stuff for
### jack - extract audio from a CD and encode it using 3rd party software
### Copyright (C) 1999-2002  Arne Zellentin <zarne@users.sf.net>

### 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 2 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, write to the Free Software
### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# terminal is one of dumb, curses

import array
import traceback
import fcntl
import sys

import jack_ripstuff
import jack_freedb

from jack_globals import *

# exported functions:
#init
#enable
#disable

# expected functions in tmod:
#update
#getkey
#sig_winch_handler

# exported variables:
enabled = None
initialized = None
size_x, size_y = None, None
orig_size_x, orig_size_y = None, None
term_type = None
sig_winch_cache = None

# variables
xtermset = None
can_getsize = None
geom_changed = None


def init(arg_type="auto", arg_xtermset = 0):
    global initialized
    global geom_changed
    global tmod
    global xtermset
    global term_type
    global size_x, size_y
    global orig_size_x, orig_size_y
    size_x, size_y = None, None
    
    if initialized:
        return

    # import the terminal specific module
    if arg_type == "auto":
        try:
            import jack_t_curses as tmod
            term_type = "curses"
        except:
            import jack_t_dumb as tmod
            term_type = "dump"
    elif arg_type == "dumb":
        import jack_t_dumb as tmod
        term_type = "dump"
    elif arg_type == "curses":
        import jack_t_curses as tmod
        term_type = "curses"

    if not tmod:
        error("invalid terminal type `%s'" % term_type)

    xtermset = arg_xtermset

    initialized = 1
    geom_changed = 0
    size_x, size_y = 80, 24     # fallback value

    oldsize = getsize()
    if oldsize != (None, None):
        orig_size_x, orig_size_y = oldsize
        size_x, size_y = oldsize
    del oldsize

def xtermset_enable():
    global xtermset
    global geom_changed
    if xtermset:
        import os
        want_x = 80 - len("track_00") + jack_ripstuff.max_name_len
        want_y = len(jack_ripstuff.all_tracks_todo_sorted) + 3
        if term_type == "curses":
            want_y = want_y - 1
        if jack_freedb.names_available:
            want_y = want_y + 1
        want_y += 7 # for the help panel
        if (size_x, size_y) != (want_x, want_y):
            try:
                os.system("xtermset -geom %dx%d" % (want_x, want_y))
                geom_changed = 1
                resize()
            except:
                warning("failed to call xtermset, is it really installed?")
                xtermset = 0
        del want_x, want_y

def xtermset_disable():
    import os
    global geom_changed
    if xtermset and geom_changed:
        try:
            os.system("xtermset -restore -geom %dx%d" % (orig_size_x, orig_size_y))
            geom_changed = 0
        except:
            pass

def getsize():
    global can_getsize
    if can_getsize == 0:
        return None, None

    if can_getsize == None:
        try:
            from IOCTLS import TIOCGWINSZ
        except ImportError:
            try:
                from termios import TIOCGWINSZ
            except ImportError:
                # TIOCGWINSZ = 0x5413 # linux, ix86. Anyone else?
                can_getsize = 0
                warning("""could not find a module which exports
TIOCGWINSZ.  This means I can't determine your terminal's geometry, so please
don't resize it. Use Tools/scripts/h2py.py from the Python source distribution
to convert /usr/include/asm/ioctls.h to IOCTLS.py and install it.""")
                return None, None
    try:
        # to get the size, we will have to do an ioctl which will return a
        # struct winsize {
        #         unsigned short ws_row;
        #         unsigned short ws_col;
        #         unsigned short ws_xpixel;
        #         unsigned short ws_ypixel;
        # };
        # (according to _I386_TERMIOS_H, /usr/include/asm/termios.h)

        winsize = array.array("H")
        data = " " * (winsize.itemsize * 4)
        data = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, data)
        # unpack the data, I hope this is portable:
        winsize.fromstring(data)
        new_y, new_x, xpixel, ypixel = winsize.tolist()
    except:
        can_getsize = 0
        return None, None
    return new_x, new_y

def resize():
    global size_x, size_y
    x, y = getsize()
    if (x, y) != (None, None):
        size_x, size_y = x, y

def enable(all = 1):
    global enabled

    if not initialized:
        return

    if enabled:
        return

    if all:
        xtermset_enable()
    tmod.enable()
    enabled = 1

def disable(all = 1):
    global enabled
    import os

    if not enabled or not initialized:
        return

    tmod.disable()
    if all:
        xtermset_disable()
    enabled = 0