summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorArne <78976058+4rneee@users.noreply.github.com>2024-11-23 18:27:38 +0100
committerGitHub <noreply@github.com>2024-11-23 18:27:38 +0100
commit5100ff9dd79f0131064de0a5f2e777e7cd485ea5 (patch)
treec736cb11e93d83a89835a96e02f4b8995281d14c
parent6b2c676369a61a405cb22c855f08e558c7580cbd (diff)
downloadx86_64-Snake-5100ff9dd79f0131064de0a5f2e777e7cd485ea5.tar.gz
x86_64-Snake-5100ff9dd79f0131064de0a5f2e777e7cd485ea5.zip
set/restore terminal options in code (#1)
-rwxr-xr-xset.sh1
-rw-r--r--src/main.asm4
-rw-r--r--src/utils.asm50
-rwxr-xr-xunset.sh1
4 files changed, 54 insertions, 2 deletions
diff --git a/set.sh b/set.sh
deleted file mode 100755
index 74b1a48..0000000
--- a/set.sh
+++ /dev/null
@@ -1 +0,0 @@
-stty -icanon -echo min 0
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
     
diff --git a/unset.sh b/unset.sh
deleted file mode 100755
index b4e3475..0000000
--- a/unset.sh
+++ /dev/null
@@ -1 +0,0 @@
-stty sane