#/usr/bin/env python import fcntl import os import pty import sys import struct import termios # 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 def main(): # check usage args = sys.argv[1:] if len(args) < 1: print('usage: %s ROM') print('') print(' ROM: the rom file to launch') sys.exit(1) # either detect dimensions from theme or use 80x24 cols, rows = load_dimensions() # path to rom to run rom = args[0] # fork with a new pty pid, fd = pty.fork() if pid == 0: # set TERM to something we can (mostly) handle env = dict(os.environ) env['TERM'] = 'ansi' os.execvpe('bash', ['bash'], env) else: # set the terminal size size = struct.pack("HHHH", rows, cols, 8, 12) 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()) os.execvp('uxnemu', ['uxnemu', rom] + args[1:]) if __name__ == "__main__": main()