nxu/term.py

45 lines
999 B
Python
Raw 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
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)
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-03 23:13:44 -05:00
###cols, rows = 79, 40
cols, rows = 80, 24
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()