diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.asm | 4 | ||||
| -rw-r--r-- | src/utils.asm | 50 |
2 files changed, 54 insertions, 0 deletions
diff --git a/src/main.asm b/src/main.asm index 0464364..ff509ed 100644 --- a/src/main.asm +++ b/src/main.asm @@ -43,6 +43,8 @@ section .text extern hide_cursor extern show_cursor extern read_input + extern set_terminal_options + extern restore_terminal_options global _start global exit @@ -226,6 +228,7 @@ _loop_return: ret _start: + call set_terminal_options call clear_screen call draw_border call hide_cursor @@ -278,6 +281,7 @@ game_over: exit: ; exit syscall with return code 0 call show_cursor call clear_screen + call restore_terminal_options mov rax, 60 xor rdi, rdi syscall diff --git a/src/utils.asm b/src/utils.asm index dfdb09f..a7e4fa6 100644 --- a/src/utils.asm +++ b/src/utils.asm @@ -22,6 +22,14 @@ section .data hide_cursor_code db 0x1B, '[', '?', '2', '5', 'l', 0 ; Escape sequence to hide the cursor show_cursor_code db 0x1B, '[', '?', '2', '5', 'h', 0 ; Escape sequence to show the cursor + ; termios struct for ioctl + termios dd 0 ; c_iflag + dd 0 ; c_oflag + dd 0 ; c_cflag + c_lflag dd 0 ; c_lflag + db 0 ; c_lin + c_cc times 19 db 0 ; c_cc + section .bss ; adress for syscall for write_byte wrapper byte_to_write resb 1 @@ -37,8 +45,50 @@ section .text global write_game_over_message global write_pause_message global write_start_message + global set_terminal_options + global restore_terminal_options extern height +set_terminal_options: + ; get current termios struct + mov rax, 0x10 ; ioctl + mov rdi, 0 ; stdin + mov rsi, 0x5401 ; TCGETS + lea rdx, [termios] + syscall + + ; unset ECHO and ICANON flags + mov ebx, [c_lflag] + and ebx, 0xFFFFFFF5 ; ~(ICANON | ECHO) + xchg ebx, [c_lflag] ; exchange to store previous value + + ; set MIN control character to 0 and store previous value + mov r12b, 0 + xchg r12b, [c_cc + 6] + + ; set the updated values + mov rax, 0x10 ; ioctl + mov rdi, 0 ; stdin + mov rsi, 0x5402 ; TCSETS + lea rdx, [termios] + syscall + + ; write old values to memory to later restore the previous settings + mov [c_lflag], ebx + mov [c_cc + 6], r12b + + ret + +restore_terminal_options: + ; restore the previously saved termios + mov rax, 0x10 ; ioctl + mov rdi, 0 ; stdin + mov rsi, 0x5402 ; TCSETS + lea rdx, [termios] + syscall + + ret + hide_cursor: push rdi |