From 01b86bdabd2da6eefe3aed52389161da6a8b5aaa Mon Sep 17 00:00:00 2001 From: d6 Date: Sat, 11 Feb 2023 19:59:36 -0500 Subject: [PATCH] load dimensions dynamically from config --- term.py | 29 +++++++++++++++++++++++-- term.tal | 64 +++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 74 insertions(+), 19 deletions(-) diff --git a/term.py b/term.py index ce657ae..0eea4b0 100755 --- a/term.py +++ b/term.py @@ -7,6 +7,30 @@ 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 @@ -17,6 +41,9 @@ def main(): 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] @@ -30,8 +57,6 @@ def main(): os.execvpe('bash', ['bash'], env) else: # set the terminal size - ###cols, rows = 79, 40 - cols, rows = 80, 24 size = struct.pack("HHHH", rows, cols, 8, 12) fcntl.ioctl(fd, termios.TIOCSWINSZ, size) diff --git a/term.tal b/term.tal index c0f97c1..51cbeaf 100644 --- a/term.tal +++ b/term.tal @@ -10,7 +10,6 @@ ( b. on insert ) ( c. etc. ) ( 6. add more ansi control seqs ) -( 7. stop hard coding terminal size in both term.py and term.tal ) ( 8. key repeat - not possible in general though ) ( 9. support shift+arrow and alt+arrow ) ( 10. crawl has screen-clearing issues ) @@ -130,13 +129,8 @@ ( 80 cols x 24 rows + 1 col for padding ) #0018 .rows STZ2 #0051 .cols STZ2 - - ( set col-bytes, frequently needed ) - .cols LDZ2 DUP2 ADD2 .col-bytes STZ2 - - ( set max row/col ) - .rows LDZ2 #0001 SUB2 .max-y STZ2 - .cols LDZ2 #0001 SUB2 .max-x STZ2 +( #0024 .rows STZ2 + #005b .cols STZ2 ) ( set initial cursor ) #0000 .cur-x STZ2 @@ -148,16 +142,15 @@ ( confirm no buttons pressed yet ) #00 .lastkey STZ - ( set screen height/width based on rows/cols ) - .cols LDZ2 #30 SFT2 ( width ) #0010 ADD2 .Screen/w DEO2 - .rows LDZ2 #000c MUL2 ( height ) #0010 ADD2 .Screen/h DEO2 - ( set colors ) #07bf .System/r DEO2 #07bf .System/g DEO2 #07bf .System/b DEO2 +( load-theme-old ) load-theme + update-dimensions + ( set starting tint: reverse=0, bg=0, fg=2 ) #02 .attr STZ update-tint @@ -194,9 +187,37 @@ #01 .File1/append DEO BRK +@update-dimensions ( -> ) + ( set col-bytes, frequently needed ) + .cols LDZ2 DUP2 ADD2 .col-bytes STZ2 + + ( set max row/col ) + .rows LDZ2 #0001 SUB2 .max-y STZ2 + .cols LDZ2 #0001 SUB2 .max-x STZ2 + + ( set screen height/width based on rows/cols ) + .cols LDZ2 #30 SFT2 ( width ) #0010 ADD2 .Screen/w DEO2 + .rows LDZ2 #000c MUL2 ( height ) #0010 ADD2 .Screen/h DEO2 + JMP2r + @shell "bash 00 "-i 00 00 @load-theme ( -> ) + ;&path .File1/name DEO2 + #0006 .File1/len DEO2 + ;&r .File1/r DEO2 + .File1/ok DEI2 ORA #01 JCN JMP2r + ,&r LDR2 .System/r DEO2 + ,&g LDR2 .System/g DEO2 + ,&b LDR2 .System/b DEO2 + #0002 .File1/len DEO2 + ;&x .File1/r DEO2 + .File1/ok DEI2 ORA #01 JCN JMP2r + #00 ,&x LDR INC2 ( hack ) .cols STZ2 + #00 ,&y LDR .rows STZ2 + JMP2r [ &path ".theme $1 &r $2 &g $2 &b $2 &x $1 &y $1 ] + +@load-theme-old ( -> ) ;&path .File1/name DEO2 #0002 .File1/len DEO2 ;&r .File1/r DEO2 @@ -712,16 +733,13 @@ @exec1 ( addr* -> ) STH2 #0001 read-arg-1 STH2r JSR2 BRK -( FIXME: hardcoded terminal size ) @exec-status POP #0000 read-arg-1 #0006 NEQ2 ,&done #1b .Console/w DEO LIT "[ .Console/w DEO - LIT "2 .Console/w DEO - LIT "4 .Console/w DEO + .cur-y LDZ2 INC2 emit-dec2 LIT "; .Console/w DEO - LIT "8 .Console/w DEO - LIT "0 .Console/w DEO + .cur-x LDZ2 INC2 emit-dec2 LIT "R .Console/w DEO &done BRK @@ -1110,6 +1128,18 @@ INCr STHkr ?&loop POPr POP2 POP2 JMP2r +( emit a short as a decimal ) +@emit-dec2 ( n* -> ) + LITr 00 ( n [0] ) + &read ( n [k] ) + #000a DIV2k STH2k MUL2 SUB2 STH2r INCr ( n%10 n/10 [k+1] ) + DUP2 ORA ,&read JCN + POP2 ( top element was 0000 ) + &write ( n0 n1 ... nk [k+1] ) + NIP #30 ADD .Console/w DEO LITr 01 SUBr ( n0 ... n{k-1} [k] ) + STHkr ,&write JCN + POPr JMP2r + @debug-log "debug_term.log 00 @scratch $40 &pos $2