| 1 | module raw_mode_module |
| 2 | use iso_c_binding |
| 3 | implicit none |
| 4 | private |
| 5 | |
| 6 | public :: enable_raw_mode, disable_raw_mode, input_available, read_char_timeout |
| 7 | public :: read_char_escape, input_available_count |
| 8 | |
| 9 | ! C function interfaces |
| 10 | interface |
| 11 | function c_enable_raw_mode() bind(C, name="enable_raw_mode") |
| 12 | import :: c_int |
| 13 | integer(c_int) :: c_enable_raw_mode |
| 14 | end function c_enable_raw_mode |
| 15 | |
| 16 | function c_disable_raw_mode() bind(C, name="disable_raw_mode") |
| 17 | import :: c_int |
| 18 | integer(c_int) :: c_disable_raw_mode |
| 19 | end function c_disable_raw_mode |
| 20 | |
| 21 | function c_input_available() bind(C, name="input_available") |
| 22 | import :: c_int |
| 23 | integer(c_int) :: c_input_available |
| 24 | end function c_input_available |
| 25 | |
| 26 | function c_input_available_count() bind(C, name="input_available_count") |
| 27 | import :: c_int |
| 28 | integer(c_int) :: c_input_available_count |
| 29 | end function c_input_available_count |
| 30 | |
| 31 | function c_read_char_timeout() bind(C, name="read_char_timeout") |
| 32 | import :: c_int |
| 33 | integer(c_int) :: c_read_char_timeout |
| 34 | end function c_read_char_timeout |
| 35 | |
| 36 | function c_read_char_escape() bind(C, name="read_char_escape") |
| 37 | import :: c_int |
| 38 | integer(c_int) :: c_read_char_escape |
| 39 | end function c_read_char_escape |
| 40 | end interface |
| 41 | |
| 42 | contains |
| 43 | |
| 44 | function enable_raw_mode() result(success) |
| 45 | logical :: success |
| 46 | integer(c_int) :: result |
| 47 | |
| 48 | result = c_enable_raw_mode() |
| 49 | success = (result == 0) |
| 50 | end function enable_raw_mode |
| 51 | |
| 52 | function disable_raw_mode() result(success) |
| 53 | logical :: success |
| 54 | integer(c_int) :: result |
| 55 | |
| 56 | result = c_disable_raw_mode() |
| 57 | success = (result == 0) |
| 58 | end function disable_raw_mode |
| 59 | |
| 60 | function input_available() result(available) |
| 61 | logical :: available |
| 62 | integer(c_int) :: result |
| 63 | |
| 64 | result = c_input_available() |
| 65 | available = (result > 0) |
| 66 | end function input_available |
| 67 | |
| 68 | function input_available_count() result(count) |
| 69 | integer :: count |
| 70 | integer(c_int) :: result |
| 71 | |
| 72 | result = c_input_available_count() |
| 73 | count = result |
| 74 | end function input_available_count |
| 75 | |
| 76 | function read_char_timeout() result(ch) |
| 77 | integer :: ch |
| 78 | integer(c_int) :: result |
| 79 | |
| 80 | result = c_read_char_timeout() |
| 81 | ch = result |
| 82 | end function read_char_timeout |
| 83 | |
| 84 | ! Fast read for escape sequences (5ms timeout instead of 50ms) |
| 85 | function read_char_escape() result(ch) |
| 86 | integer :: ch |
| 87 | integer(c_int) :: result |
| 88 | |
| 89 | result = c_read_char_escape() |
| 90 | ch = result |
| 91 | end function read_char_escape |
| 92 | |
| 93 | end module raw_mode_module |