nxu/term.py

70 lines
1.7 KiB
Python
Raw Permalink Normal View History

2023-01-21 22:54:20 -05:00
#/usr/bin/env python
2023-01-21 15:41:27 -05:00
2023-01-21 22:54:20 -05:00
import fcntl
import os
import pty
2023-01-21 15:41:27 -05:00
import sys
2023-01-21 22:54:20 -05:00
import struct
import termios
2023-01-21 15:41:27 -05:00
# return dimensions (cols x rows)
def load_dimensions():
# default to 80x24
dims = 80, 24
if not os.path.exists('.theme'):
return dims
try:
with open('.theme', 'rb') as f:
data = f.read(8)
if len(data) < 8:
return dims
vals = struct.unpack('HHHBB', data)
x, y = int(vals[3]), int(vals[4])
if x < 80 or y < 24 or x > 200 or y > 100:
print('invalid width/height: %d/%d' % (x, y))
sys.exit(1)
dims = x, y
except Exception(e):
print('error in .theme file: %s' % e)
sys.exit(1)
return dims
2023-01-21 15:41:27 -05:00
def main():
2023-01-21 22:54:20 -05:00
# check usage
2023-01-21 15:41:27 -05:00
args = sys.argv[1:]
2023-01-21 22:54:20 -05:00
if len(args) < 1:
print('usage: %s ROM')
2023-01-21 15:58:02 -05:00
print('')
2023-01-21 22:54:20 -05:00
print(' ROM: the rom file to launch')
2023-01-21 15:41:27 -05:00
sys.exit(1)
# either detect dimensions from theme or use 80x24
cols, rows = load_dimensions()
2023-01-21 22:54:20 -05:00
# path to rom to run
rom = args[0]
2023-01-21 15:41:27 -05:00
2023-01-21 22:54:20 -05:00
# fork with a new pty
pid, fd = pty.fork()
2023-02-03 23:13:44 -05:00
2023-01-21 22:54:20 -05:00
if pid == 0:
2023-01-23 23:16:46 -05:00
# set TERM to something we can (mostly) handle
2023-01-21 22:54:20 -05:00
env = dict(os.environ)
2023-01-23 16:26:08 -05:00
env['TERM'] = 'ansi'
os.execvpe('bash', ['bash'], env)
2023-01-21 22:54:20 -05:00
else:
# set the terminal size
2023-02-04 16:02:21 -05:00
size = struct.pack("HHHH", rows, cols, 8, 12)
2023-01-21 22:54:20 -05:00
fcntl.ioctl(fd, termios.TIOCSWINSZ, size)
# use fd for the terminals stdin/stdout
os.dup2(fd, sys.stdin.fileno())
os.dup2(os.dup(fd), sys.stdout.fileno())
2023-01-23 23:16:46 -05:00
os.execvp('uxnemu', ['uxnemu', rom] + args[1:])
2023-02-03 23:13:44 -05:00
2023-01-21 15:41:27 -05:00
if __name__ == "__main__":
main()