| 1 | // Copyright (c) 2020-2024 Raúl Hernández. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module ui |
| 5 | |
| 6 | import os |
| 7 | |
| 8 | pub enum KeyCode { |
| 9 | null = 0 |
| 10 | tab = 9 |
| 11 | enter = 10 |
| 12 | escape = 27 |
| 13 | space = 32 |
| 14 | backspace = 127 |
| 15 | exclamation = 33 |
| 16 | double_quote = 34 |
| 17 | hashtag = 35 |
| 18 | dollar = 36 |
| 19 | percent = 37 |
| 20 | ampersand = 38 |
| 21 | single_quote = 39 |
| 22 | left_paren = 40 |
| 23 | right_paren = 41 |
| 24 | asterisk = 42 |
| 25 | plus = 43 |
| 26 | comma = 44 |
| 27 | minus = 45 |
| 28 | period = 46 |
| 29 | slash = 47 |
| 30 | _0 = 48 |
| 31 | _1 = 49 |
| 32 | _2 = 50 |
| 33 | _3 = 51 |
| 34 | _4 = 52 |
| 35 | _5 = 53 |
| 36 | _6 = 54 |
| 37 | _7 = 55 |
| 38 | _8 = 56 |
| 39 | _9 = 57 |
| 40 | colon = 58 |
| 41 | semicolon = 59 |
| 42 | less_than = 60 |
| 43 | equal = 61 |
| 44 | greater_than = 62 |
| 45 | question_mark = 63 |
| 46 | at = 64 |
| 47 | a = 97 |
| 48 | b = 98 |
| 49 | c = 99 |
| 50 | d = 100 |
| 51 | e = 101 |
| 52 | f = 102 |
| 53 | g = 103 |
| 54 | h = 104 |
| 55 | i = 105 |
| 56 | j = 106 |
| 57 | k = 107 |
| 58 | l = 108 |
| 59 | m = 109 |
| 60 | n = 110 |
| 61 | o = 111 |
| 62 | p = 112 |
| 63 | q = 113 |
| 64 | r = 114 |
| 65 | s = 115 |
| 66 | t = 116 |
| 67 | u = 117 |
| 68 | v = 118 |
| 69 | w = 119 |
| 70 | x = 120 |
| 71 | y = 121 |
| 72 | z = 122 |
| 73 | left_square_bracket = 91 |
| 74 | backslash = 92 |
| 75 | right_square_bracket = 93 |
| 76 | caret = 94 |
| 77 | underscore = 95 |
| 78 | backtick = 96 |
| 79 | left_curly_bracket = 123 |
| 80 | vertical_bar = 124 |
| 81 | right_curly_bracket = 125 |
| 82 | tilde = 126 |
| 83 | insert = 260 |
| 84 | delete = 261 |
| 85 | up = 262 |
| 86 | down = 263 |
| 87 | right = 264 |
| 88 | left = 265 |
| 89 | page_up = 266 |
| 90 | page_down = 267 |
| 91 | home = 268 |
| 92 | end = 269 |
| 93 | f1 = 290 |
| 94 | f2 = 291 |
| 95 | f3 = 292 |
| 96 | f4 = 293 |
| 97 | f5 = 294 |
| 98 | f6 = 295 |
| 99 | f7 = 296 |
| 100 | f8 = 297 |
| 101 | f9 = 298 |
| 102 | f10 = 299 |
| 103 | f11 = 300 |
| 104 | f12 = 301 |
| 105 | f13 = 302 |
| 106 | f14 = 303 |
| 107 | f15 = 304 |
| 108 | f16 = 305 |
| 109 | f17 = 306 |
| 110 | f18 = 307 |
| 111 | f19 = 308 |
| 112 | f20 = 309 |
| 113 | f21 = 310 |
| 114 | f22 = 311 |
| 115 | f23 = 312 |
| 116 | f24 = 313 |
| 117 | } |
| 118 | |
| 119 | pub enum Direction { |
| 120 | unknown |
| 121 | up |
| 122 | down |
| 123 | left |
| 124 | right |
| 125 | } |
| 126 | |
| 127 | pub enum MouseButton { |
| 128 | unknown |
| 129 | left |
| 130 | middle |
| 131 | right |
| 132 | } |
| 133 | |
| 134 | pub enum EventType { |
| 135 | unknown |
| 136 | mouse_down |
| 137 | mouse_up |
| 138 | mouse_move |
| 139 | mouse_drag |
| 140 | mouse_scroll |
| 141 | key_down |
| 142 | key_up |
| 143 | resized |
| 144 | } |
| 145 | |
| 146 | @[flag] |
| 147 | pub enum Modifiers { |
| 148 | ctrl |
| 149 | shift |
| 150 | alt |
| 151 | } |
| 152 | |
| 153 | struct TerminalCapabilities { |
| 154 | enable_ansi256 bool = true |
| 155 | supports_alternate_buffer bool = true |
| 156 | supports_sgr_mouse bool = true |
| 157 | supports_sync_updates bool = true |
| 158 | supports_window_title bool = true |
| 159 | } |
| 160 | |
| 161 | fn terminal_capabilities_for(term_name string) TerminalCapabilities { |
| 162 | if term_name == 'linux' { |
| 163 | return TerminalCapabilities{ |
| 164 | enable_ansi256: false |
| 165 | supports_alternate_buffer: false |
| 166 | supports_sgr_mouse: false |
| 167 | supports_sync_updates: false |
| 168 | supports_window_title: false |
| 169 | } |
| 170 | } |
| 171 | return TerminalCapabilities{} |
| 172 | } |
| 173 | |
| 174 | fn current_terminal_capabilities() TerminalCapabilities { |
| 175 | return terminal_capabilities_for(os.getenv('TERM')) |
| 176 | } |
| 177 | |
| 178 | pub struct Event { |
| 179 | pub: |
| 180 | typ EventType |
| 181 | // Mouse event info |
| 182 | x int |
| 183 | y int |
| 184 | button MouseButton |
| 185 | direction Direction |
| 186 | // Keyboard event info |
| 187 | code KeyCode |
| 188 | modifiers Modifiers |
| 189 | ascii u8 |
| 190 | utf8 string |
| 191 | // Resized event info |
| 192 | width int |
| 193 | height int |
| 194 | } |
| 195 | |
| 196 | pub struct Context { |
| 197 | ExtraContext // contains fields specific to an implementation |
| 198 | pub: |
| 199 | cfg Config // the initial configuration, passed to ui.init() |
| 200 | mut: |
| 201 | print_buf []u8 |
| 202 | paused bool |
| 203 | enable_su bool |
| 204 | enable_rgb bool |
| 205 | enable_ansi256 bool = true |
| 206 | supports_alternate_buffer bool = true |
| 207 | supports_sgr_mouse bool = true |
| 208 | supports_sync_updates bool = true |
| 209 | supports_window_title bool = true |
| 210 | pub mut: |
| 211 | frame_count u64 |
| 212 | window_width int |
| 213 | window_height int |
| 214 | } |
| 215 | |
| 216 | pub struct Config { |
| 217 | pub: |
| 218 | user_data voidptr |
| 219 | init_fn ?fn (voidptr) |
| 220 | frame_fn ?fn (voidptr) |
| 221 | cleanup_fn ?fn (voidptr) |
| 222 | event_fn ?fn (&Event, voidptr) |
| 223 | fail_fn ?fn (string) |
| 224 | |
| 225 | buffer_size int = 256 |
| 226 | frame_rate int = 30 |
| 227 | use_x11 bool |
| 228 | |
| 229 | window_title string |
| 230 | hide_cursor bool |
| 231 | capture_events bool |
| 232 | mouse_enabled bool |
| 233 | use_alternate_buffer bool = true |
| 234 | skip_init_checks bool |
| 235 | // All kill signals to set up exit listeners on: |
| 236 | reset []os.Signal = [.hup, .int, .quit, .ill, .abrt, .bus, .fpe, .kill, .segv, .pipe, .alrm, .term, |
| 237 | .stop] |
| 238 | } |
| 239 | |
| 240 | @[inline] |
| 241 | fn (ctx &Context) init() { |
| 242 | f := ctx.cfg.init_fn or { return } |
| 243 | f(ctx.cfg.user_data) |
| 244 | } |
| 245 | |
| 246 | @[inline] |
| 247 | fn (ctx &Context) frame() { |
| 248 | f := ctx.cfg.frame_fn or { return } |
| 249 | f(ctx.cfg.user_data) |
| 250 | } |
| 251 | |
| 252 | @[inline] |
| 253 | fn (ctx &Context) cleanup() { |
| 254 | f := ctx.cfg.cleanup_fn or { return } |
| 255 | f(ctx.cfg.user_data) |
| 256 | } |
| 257 | |
| 258 | @[inline] |
| 259 | fn (ctx &Context) fail(error string) { |
| 260 | f := ctx.cfg.fail_fn or { return } |
| 261 | f(error) |
| 262 | } |
| 263 | |
| 264 | @[inline] |
| 265 | fn (ctx &Context) event(event &Event) { |
| 266 | f := ctx.cfg.event_fn or { return } |
| 267 | f(event, ctx.cfg.user_data) |
| 268 | } |
| 269 | |