2021-01-27 15:52:39 +03:00
|
|
|
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
|
2020-11-26 02:28:57 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-11-12 14:12:51 +03:00
|
|
|
module ui
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
#include <termios.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <signal.h>
|
2021-02-21 18:05:03 +03:00
|
|
|
|
2020-11-12 14:12:51 +03:00
|
|
|
fn C.tcgetattr()
|
2021-01-26 17:43:10 +03:00
|
|
|
|
2020-11-12 14:12:51 +03:00
|
|
|
fn C.tcsetattr()
|
2021-01-26 17:43:10 +03:00
|
|
|
|
2020-11-12 14:12:51 +03:00
|
|
|
fn C.ioctl(fd int, request u64, arg voidptr) int
|
|
|
|
|
|
|
|
struct C.termios {
|
|
|
|
mut:
|
|
|
|
c_iflag u32
|
|
|
|
c_lflag u32
|
|
|
|
c_cc [32]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
struct C.winsize {
|
|
|
|
ws_row u16
|
|
|
|
ws_col u16
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
termios_at_startup = get_termios()
|
|
|
|
)
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn get_termios() C.termios {
|
|
|
|
mut t := C.termios{}
|
|
|
|
C.tcgetattr(C.STDIN_FILENO, &t)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn get_terminal_size() (u16, u16) {
|
|
|
|
winsz := C.winsize{}
|
|
|
|
C.ioctl(0, C.TIOCGWINSZ, &winsz)
|
|
|
|
return winsz.ws_row, winsz.ws_col
|
|
|
|
}
|
|
|
|
|
2020-11-13 16:30:47 +03:00
|
|
|
fn restore_terminal_state() {
|
|
|
|
termios_reset()
|
|
|
|
mut c := ctx_ptr
|
|
|
|
if c != 0 {
|
2020-11-14 22:05:22 +03:00
|
|
|
c.paused = true
|
2021-01-27 15:52:39 +03:00
|
|
|
load_title()
|
2020-11-13 16:30:47 +03:00
|
|
|
}
|
2020-11-14 22:05:22 +03:00
|
|
|
os.flush()
|
2020-11-14 10:14:54 +03:00
|
|
|
}
|
2020-11-13 16:30:47 +03:00
|
|
|
|
2020-11-26 02:28:57 +03:00
|
|
|
fn (mut ctx Context) termios_setup() ? {
|
2020-11-14 10:14:54 +03:00
|
|
|
// store the current title, so restore_terminal_state can get it back
|
2021-01-27 15:52:39 +03:00
|
|
|
save_title()
|
2020-11-14 20:39:59 +03:00
|
|
|
|
2020-11-26 02:28:57 +03:00
|
|
|
if !ctx.cfg.skip_init_checks && !(is_atty(C.STDIN_FILENO) != 0 && is_atty(C.STDOUT_FILENO) != 0) {
|
|
|
|
return error('not running under a TTY')
|
|
|
|
}
|
|
|
|
|
2020-11-12 14:12:51 +03:00
|
|
|
mut termios := get_termios()
|
|
|
|
|
|
|
|
if ctx.cfg.capture_events {
|
|
|
|
// Set raw input mode by unsetting ICANON and ECHO,
|
|
|
|
// as well as disable e.g. ctrl+c and ctrl.z
|
|
|
|
termios.c_iflag &= ~u32(C.IGNBRK | C.BRKINT | C.PARMRK | C.IXON)
|
|
|
|
termios.c_lflag &= ~u32(C.ICANON | C.ISIG | C.ECHO | C.IEXTEN | C.TOSTOP)
|
|
|
|
} else {
|
|
|
|
// Set raw input mode by unsetting ICANON and ECHO
|
|
|
|
termios.c_lflag &= ~u32(C.ICANON | C.ECHO)
|
|
|
|
}
|
2020-11-14 10:14:54 +03:00
|
|
|
|
|
|
|
if ctx.cfg.hide_cursor {
|
|
|
|
print('\x1b[?25l')
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.cfg.window_title != '' {
|
|
|
|
print('\x1b]0;$ctx.cfg.window_title\x07')
|
|
|
|
}
|
|
|
|
|
2020-11-26 02:28:57 +03:00
|
|
|
if !ctx.cfg.skip_init_checks {
|
|
|
|
// prevent blocking during the feature detections, but allow enough time for the terminal
|
|
|
|
// to send back the relevant input data
|
|
|
|
termios.c_cc[C.VTIME] = 1
|
|
|
|
termios.c_cc[C.VMIN] = 0
|
|
|
|
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH, &termios)
|
|
|
|
// feature-test the SU spec
|
|
|
|
sx, sy := get_cursor_position()
|
|
|
|
print('$bsu$esu')
|
|
|
|
ex, ey := get_cursor_position()
|
|
|
|
if sx == ex && sy == ey {
|
|
|
|
// the terminal either ignored or handled the sequence properly, enable SU
|
|
|
|
ctx.enable_su = true
|
|
|
|
} else {
|
|
|
|
ctx.draw_line(sx, sy, ex, ey)
|
|
|
|
ctx.set_cursor_position(sx, sy)
|
|
|
|
ctx.flush()
|
|
|
|
}
|
|
|
|
// feature-test rgb (truecolor) support
|
|
|
|
ctx.enable_rgb = supports_truecolor()
|
2020-11-16 15:48:08 +03:00
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
// Prevent stdin from blocking by making its read time 0
|
|
|
|
termios.c_cc[C.VTIME] = 0
|
|
|
|
termios.c_cc[C.VMIN] = 0
|
|
|
|
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH, &termios)
|
2020-11-16 15:48:08 +03:00
|
|
|
// enable mouse input
|
2020-11-14 10:14:54 +03:00
|
|
|
print('\x1b[?1003h\x1b[?1006h')
|
2020-11-14 22:05:22 +03:00
|
|
|
if ctx.cfg.use_alternate_buffer {
|
2020-11-16 15:48:08 +03:00
|
|
|
// switch to the alternate buffer
|
2020-11-14 22:05:22 +03:00
|
|
|
print('\x1b[?1049h')
|
2020-11-16 15:48:08 +03:00
|
|
|
// clear the terminal and set the cursor to the origin
|
|
|
|
print('\x1b[2J\x1b[3J\x1b[1;1H')
|
2020-11-14 22:05:22 +03:00
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
ctx.window_height, ctx.window_width = get_terminal_size()
|
|
|
|
|
|
|
|
// Reset console on exit
|
2020-11-14 10:14:54 +03:00
|
|
|
C.atexit(restore_terminal_state)
|
2020-11-13 16:30:47 +03:00
|
|
|
os.signal(C.SIGTSTP, restore_terminal_state)
|
2020-11-13 11:27:53 +03:00
|
|
|
os.signal(C.SIGCONT, fn () {
|
|
|
|
mut c := ctx_ptr
|
|
|
|
if c != 0 {
|
2021-03-01 02:18:14 +03:00
|
|
|
c.termios_setup() or { panic(err) }
|
2020-11-13 11:27:53 +03:00
|
|
|
c.window_height, c.window_width = get_terminal_size()
|
|
|
|
mut event := &Event{
|
|
|
|
typ: .resized
|
|
|
|
width: c.window_width
|
|
|
|
height: c.window_height
|
|
|
|
}
|
2020-11-14 22:05:22 +03:00
|
|
|
c.paused = false
|
2020-11-13 11:27:53 +03:00
|
|
|
c.event(event)
|
|
|
|
}
|
|
|
|
})
|
2020-11-12 14:12:51 +03:00
|
|
|
for code in ctx.cfg.reset {
|
2021-01-26 17:43:10 +03:00
|
|
|
os.signal(code, fn () {
|
2020-11-12 14:12:51 +03:00
|
|
|
mut c := ctx_ptr
|
|
|
|
if c != 0 {
|
|
|
|
c.cleanup()
|
|
|
|
}
|
|
|
|
exit(0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
os.signal(C.SIGWINCH, fn () {
|
2020-11-12 14:12:51 +03:00
|
|
|
mut c := ctx_ptr
|
|
|
|
if c != 0 {
|
|
|
|
c.window_height, c.window_width = get_terminal_size()
|
|
|
|
|
|
|
|
mut event := &Event{
|
|
|
|
typ: .resized
|
|
|
|
width: c.window_width
|
|
|
|
height: c.window_height
|
|
|
|
}
|
|
|
|
c.event(event)
|
|
|
|
}
|
|
|
|
})
|
2020-11-16 15:48:08 +03:00
|
|
|
|
2020-11-14 22:05:22 +03:00
|
|
|
os.flush()
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
|
2020-11-16 15:48:08 +03:00
|
|
|
fn get_cursor_position() (int, int) {
|
|
|
|
print('\033[6n')
|
2021-02-14 21:31:42 +03:00
|
|
|
mut s := ''
|
2021-01-26 17:43:10 +03:00
|
|
|
unsafe {
|
2021-02-14 21:31:42 +03:00
|
|
|
buf := malloc(25)
|
|
|
|
len := C.read(C.STDIN_FILENO, buf, 24)
|
2021-01-26 17:43:10 +03:00
|
|
|
buf[len] = 0
|
2021-02-14 21:31:42 +03:00
|
|
|
s = tos(buf, len)
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
2020-11-27 01:52:20 +03:00
|
|
|
a := s[2..].split(';')
|
2021-01-26 17:43:10 +03:00
|
|
|
if a.len != 2 {
|
|
|
|
return -1, -1
|
|
|
|
}
|
2020-11-27 01:52:20 +03:00
|
|
|
return a[0].int(), a[1].int()
|
2020-11-16 15:48:08 +03:00
|
|
|
}
|
|
|
|
|
2020-11-26 02:28:57 +03:00
|
|
|
fn supports_truecolor() bool {
|
2020-11-27 01:52:20 +03:00
|
|
|
// faster/simpler, but less reliable, check
|
|
|
|
if os.getenv('COLORTERM') in ['truecolor', '24bit'] {
|
|
|
|
return true
|
|
|
|
}
|
2020-11-26 02:28:57 +03:00
|
|
|
// set the bg color to some arbirtrary value (#010203), assumed not to be the default
|
|
|
|
print('\x1b[48:2:1:2:3m')
|
2020-11-27 01:52:20 +03:00
|
|
|
// andquery the current color
|
2020-11-26 02:28:57 +03:00
|
|
|
print('\x1bP\$qm\x1b\\')
|
2021-02-14 21:31:42 +03:00
|
|
|
mut s := ''
|
2021-01-26 17:43:10 +03:00
|
|
|
unsafe {
|
2021-02-14 21:31:42 +03:00
|
|
|
buf := malloc(25)
|
|
|
|
len := C.read(C.STDIN_FILENO, buf, 24)
|
2021-01-26 17:43:10 +03:00
|
|
|
buf[len] = 0
|
2021-02-14 21:31:42 +03:00
|
|
|
s = tos(buf, len)
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
2020-11-27 01:52:20 +03:00
|
|
|
return '1:2:3' in s
|
2020-11-26 02:28:57 +03:00
|
|
|
}
|
|
|
|
|
2020-11-12 14:12:51 +03:00
|
|
|
fn termios_reset() {
|
2021-01-26 17:43:10 +03:00
|
|
|
// C.TCSANOW ??
|
|
|
|
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH, &ui.termios_at_startup)
|
2020-11-14 22:05:22 +03:00
|
|
|
print('\x1b[?1003l\x1b[?1006l\x1b[?25h')
|
|
|
|
c := ctx_ptr
|
|
|
|
if c != 0 && c.cfg.use_alternate_buffer {
|
|
|
|
print('\x1b[?1049l')
|
|
|
|
}
|
|
|
|
os.flush()
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////
|
|
|
|
// TODO: do multiple sleep/read cycles, rather than one big one
|
|
|
|
fn (mut ctx Context) termios_loop() {
|
|
|
|
frame_time := 1_000_000 / ctx.cfg.frame_rate
|
|
|
|
mut init_called := false
|
|
|
|
mut sw := time.new_stopwatch(auto_start: false)
|
|
|
|
mut sleep_len := 0
|
|
|
|
for {
|
|
|
|
if !init_called {
|
|
|
|
ctx.init()
|
|
|
|
init_called = true
|
|
|
|
}
|
|
|
|
// println('SLEEPING: $sleep_len')
|
|
|
|
if sleep_len > 0 {
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(sleep_len * time.microsecond)
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
2020-11-14 22:05:22 +03:00
|
|
|
if !ctx.paused {
|
|
|
|
sw.restart()
|
|
|
|
if ctx.cfg.event_fn != voidptr(0) {
|
2020-11-27 01:52:20 +03:00
|
|
|
unsafe {
|
2021-01-26 17:43:10 +03:00
|
|
|
len := C.read(C.STDIN_FILENO, byteptr(ctx.read_buf.data) + ctx.read_buf.len,
|
|
|
|
ctx.read_buf.cap - ctx.read_buf.len)
|
2020-11-27 01:52:20 +03:00
|
|
|
ctx.resize_arr(ctx.read_buf.len + len)
|
|
|
|
}
|
|
|
|
if ctx.read_buf.len > 0 {
|
2020-11-14 22:05:22 +03:00
|
|
|
ctx.parse_events()
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
2020-11-14 22:05:22 +03:00
|
|
|
ctx.frame()
|
|
|
|
sw.pause()
|
|
|
|
e := sw.elapsed().microseconds()
|
|
|
|
sleep_len = frame_time - int(e)
|
2020-11-12 14:12:51 +03:00
|
|
|
|
2020-11-14 22:05:22 +03:00
|
|
|
ctx.frame_count++
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut ctx Context) parse_events() {
|
|
|
|
// Stop this from getting stuck in rare cases where something isn't parsed correctly
|
|
|
|
mut nr_iters := 0
|
|
|
|
for ctx.read_buf.len > 0 {
|
|
|
|
nr_iters++
|
2021-01-26 17:43:10 +03:00
|
|
|
if nr_iters > 100 {
|
|
|
|
ctx.shift(1)
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
mut event := &Event(0)
|
|
|
|
if ctx.read_buf[0] == 0x1b {
|
|
|
|
e, len := escape_sequence(ctx.read_buf.bytestr())
|
|
|
|
event = e
|
|
|
|
ctx.shift(len)
|
|
|
|
} else {
|
|
|
|
event = single_char(ctx.read_buf.bytestr())
|
|
|
|
ctx.shift(1)
|
|
|
|
}
|
|
|
|
if event != 0 {
|
|
|
|
ctx.event(event)
|
|
|
|
nr_iters = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn single_char(buf string) &Event {
|
|
|
|
ch := buf[0]
|
|
|
|
|
|
|
|
mut event := &Event{
|
|
|
|
typ: .key_down
|
|
|
|
ascii: ch
|
|
|
|
code: KeyCode(ch)
|
|
|
|
utf8: buf
|
|
|
|
}
|
|
|
|
|
|
|
|
match ch {
|
|
|
|
// special handling for `ctrl + letter`
|
|
|
|
// TODO: Fix assoc in V and remove this workaround :/
|
2021-02-21 17:07:49 +03:00
|
|
|
// 1 ... 26 { event = Event{ ...event, code: KeyCode(96 | ch), modifiers: .ctrl } }
|
|
|
|
// 65 ... 90 { event = Event{ ...event, code: KeyCode(32 | ch), modifiers: .shift } }
|
2020-11-12 14:12:51 +03:00
|
|
|
// The bit `or`s here are really just `+`'s, just written in this way for a tiny performance improvement
|
2020-11-14 20:39:59 +03:00
|
|
|
// don't treat tab, enter as ctrl+i, ctrl+j
|
2021-02-21 18:05:03 +03:00
|
|
|
1...8, 11...26 {
|
|
|
|
event = &Event{
|
2021-01-26 17:43:10 +03:00
|
|
|
typ: event.typ
|
|
|
|
ascii: event.ascii
|
|
|
|
utf8: event.utf8
|
|
|
|
code: KeyCode(96 | ch)
|
2021-02-21 17:07:49 +03:00
|
|
|
modifiers: .ctrl
|
2021-01-26 17:43:10 +03:00
|
|
|
} }
|
|
|
|
65...90 { event = &Event{
|
|
|
|
typ: event.typ
|
|
|
|
ascii: event.ascii
|
|
|
|
utf8: event.utf8
|
|
|
|
code: KeyCode(32 | ch)
|
2021-02-21 17:07:49 +03:00
|
|
|
modifiers: .shift
|
2021-01-26 17:43:10 +03:00
|
|
|
} }
|
2020-11-12 14:12:51 +03:00
|
|
|
else {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets an entire, independent escape sequence from the buffer
|
|
|
|
// Normally, this just means reading until the first letter, but there are some exceptions...
|
|
|
|
fn escape_end(buf string) int {
|
|
|
|
mut i := 0
|
|
|
|
for {
|
2021-01-26 17:43:10 +03:00
|
|
|
if i + 1 == buf.len {
|
|
|
|
return buf.len
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
2020-11-13 16:30:47 +03:00
|
|
|
if buf[i].is_letter() || buf[i] == `~` {
|
2020-11-12 14:12:51 +03:00
|
|
|
if buf[i] == `O` && i + 2 <= buf.len {
|
2021-01-26 17:43:10 +03:00
|
|
|
n := buf[i + 1]
|
2020-11-12 14:12:51 +03:00
|
|
|
if (n >= `A` && n <= `D`) || (n >= `P` && n <= `S`) || n == `F` || n == `H` {
|
|
|
|
return i + 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i + 1
|
2021-01-26 17:43:10 +03:00
|
|
|
// escape hatch to avoid potential issues/crashes, although ideally this should never eval to true
|
|
|
|
} else if buf[i + 1] == 0x1b {
|
|
|
|
return i + 1
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
// this point should be unreachable
|
|
|
|
assert false
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn escape_sequence(buf_ string) (&Event, int) {
|
|
|
|
end := escape_end(buf_)
|
|
|
|
single := buf_[..end] // read until the end of the sequence
|
|
|
|
buf := single[1..] // skip the escape character
|
|
|
|
|
|
|
|
if buf.len == 0 {
|
|
|
|
return &Event{
|
|
|
|
typ: .key_down
|
|
|
|
ascii: 27
|
|
|
|
code: .escape
|
|
|
|
utf8: single
|
|
|
|
}, 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf.len == 1 {
|
|
|
|
c := single_char(buf)
|
2021-02-21 17:07:49 +03:00
|
|
|
mut modifiers := c.modifiers
|
|
|
|
modifiers.set(.alt)
|
2020-11-12 14:12:51 +03:00
|
|
|
return &Event{
|
|
|
|
typ: c.typ
|
|
|
|
ascii: c.ascii
|
|
|
|
code: c.code
|
|
|
|
utf8: single
|
2021-02-21 17:07:49 +03:00
|
|
|
modifiers: modifiers
|
2020-11-12 14:12:51 +03:00
|
|
|
}, 2
|
|
|
|
}
|
|
|
|
// ----------------
|
|
|
|
// Mouse events
|
|
|
|
// ----------------
|
2020-11-14 10:14:54 +03:00
|
|
|
// Documentation: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Tracking
|
2020-11-13 16:30:47 +03:00
|
|
|
if buf.len > 2 && buf[1] == `<` {
|
2020-11-12 14:12:51 +03:00
|
|
|
split := buf[2..].split(';')
|
2021-01-26 17:43:10 +03:00
|
|
|
if split.len < 3 {
|
|
|
|
return &Event(0), 0
|
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
|
|
|
typ, x, y := split[0].int(), split[1].int(), split[2].int()
|
2020-11-14 10:14:54 +03:00
|
|
|
lo := typ & 0b00011
|
|
|
|
hi := typ & 0b11100
|
|
|
|
|
2021-02-21 17:07:49 +03:00
|
|
|
mut modifiers := Modifiers{}
|
2021-01-26 17:43:10 +03:00
|
|
|
if hi & 4 != 0 {
|
2021-02-21 17:07:49 +03:00
|
|
|
modifiers.set(.shift)
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
|
|
|
if hi & 8 != 0 {
|
2021-02-21 17:07:49 +03:00
|
|
|
modifiers.set(.alt)
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
|
|
|
if hi & 16 != 0 {
|
2021-02-21 17:07:49 +03:00
|
|
|
modifiers.set(.ctrl)
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
2020-11-12 14:12:51 +03:00
|
|
|
|
|
|
|
match typ {
|
2020-11-14 10:14:54 +03:00
|
|
|
0...31 {
|
2020-11-12 14:12:51 +03:00
|
|
|
last := buf[buf.len - 1]
|
2020-11-14 10:14:54 +03:00
|
|
|
button := if lo < 3 { MouseButton(lo + 1) } else { MouseButton.unknown }
|
2021-01-26 17:43:10 +03:00
|
|
|
event := if last == `m` || lo == 3 {
|
|
|
|
EventType.mouse_up
|
|
|
|
} else {
|
|
|
|
EventType.mouse_down
|
|
|
|
}
|
2020-11-14 10:14:54 +03:00
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
return &Event{
|
|
|
|
typ: event
|
|
|
|
x: x
|
|
|
|
y: y
|
|
|
|
button: button
|
|
|
|
modifiers: modifiers
|
|
|
|
utf8: single
|
|
|
|
}, end
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
2020-11-14 10:14:54 +03:00
|
|
|
32...63 {
|
|
|
|
button, event := if lo < 3 {
|
2021-01-26 17:43:10 +03:00
|
|
|
MouseButton(lo + 1), EventType.mouse_drag
|
|
|
|
} else {
|
|
|
|
MouseButton.unknown, EventType.mouse_move
|
|
|
|
}
|
2020-11-14 10:14:54 +03:00
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
return &Event{
|
|
|
|
typ: event
|
|
|
|
x: x
|
|
|
|
y: y
|
|
|
|
button: button
|
|
|
|
modifiers: modifiers
|
|
|
|
utf8: single
|
|
|
|
}, end
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
2020-11-14 10:14:54 +03:00
|
|
|
64...95 {
|
|
|
|
direction := if typ & 1 == 0 { Direction.down } else { Direction.up }
|
2021-01-26 17:43:10 +03:00
|
|
|
return &Event{
|
|
|
|
typ: .mouse_scroll
|
|
|
|
x: x
|
|
|
|
y: y
|
|
|
|
direction: direction
|
|
|
|
modifiers: modifiers
|
|
|
|
utf8: single
|
|
|
|
}, end
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return &Event{
|
|
|
|
typ: .unknown
|
|
|
|
utf8: single
|
|
|
|
}, end
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ----------------------------
|
|
|
|
// Special key combinations
|
|
|
|
// ----------------------------
|
|
|
|
|
|
|
|
mut code := KeyCode.null
|
2021-02-21 17:07:49 +03:00
|
|
|
mut modifiers := Modifiers{}
|
2020-11-12 14:12:51 +03:00
|
|
|
match buf {
|
2021-01-26 17:43:10 +03:00
|
|
|
'[A', 'OA' { code = .up }
|
|
|
|
'[B', 'OB' { code = .down }
|
|
|
|
'[C', 'OC' { code = .right }
|
|
|
|
'[D', 'OD' { code = .left }
|
|
|
|
'[5~', '[[5~' { code = .page_up }
|
|
|
|
'[6~', '[[6~' { code = .page_down }
|
2020-11-12 14:12:51 +03:00
|
|
|
'[F', 'OF', '[4~', '[[8~' { code = .end }
|
|
|
|
'[H', 'OH', '[1~', '[[7~' { code = .home }
|
2021-01-26 17:43:10 +03:00
|
|
|
'[2~' { code = .insert }
|
|
|
|
'[3~' { code = .delete }
|
|
|
|
'OP', '[11~' { code = .f1 }
|
|
|
|
'OQ', '[12~' { code = .f2 }
|
|
|
|
'OR', '[13~' { code = .f3 }
|
|
|
|
'OS', '[14~' { code = .f4 }
|
|
|
|
'[15~' { code = .f5 }
|
|
|
|
'[17~' { code = .f6 }
|
|
|
|
'[18~' { code = .f7 }
|
|
|
|
'[19~' { code = .f8 }
|
|
|
|
'[20~' { code = .f9 }
|
|
|
|
'[21~' { code = .f10 }
|
|
|
|
'[23~' { code = .f11 }
|
|
|
|
'[24~' { code = .f12 }
|
|
|
|
else {}
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
|
2020-11-14 20:39:59 +03:00
|
|
|
if buf == '[Z' {
|
|
|
|
code = .tab
|
2021-02-21 17:07:49 +03:00
|
|
|
modifiers.set(.shift)
|
2020-11-14 20:39:59 +03:00
|
|
|
}
|
|
|
|
|
2020-11-14 10:14:54 +03:00
|
|
|
if buf.len == 5 && buf[0] == `[` && buf[1].is_digit() && buf[2] == `;` {
|
2021-02-21 17:07:49 +03:00
|
|
|
match buf[3] {
|
|
|
|
`2` { modifiers = .shift }
|
|
|
|
`3` { modifiers = .alt }
|
|
|
|
`4` { modifiers = .shift | .alt }
|
|
|
|
`5` { modifiers = .ctrl }
|
|
|
|
`6` { modifiers = .ctrl | .shift }
|
|
|
|
`7` { modifiers = .ctrl | .alt }
|
|
|
|
`8` { modifiers = .ctrl | .alt | .shift }
|
|
|
|
else {}
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
|
2020-11-14 10:14:54 +03:00
|
|
|
if buf[1] == `1` {
|
2021-02-21 17:07:49 +03:00
|
|
|
match buf[4] {
|
|
|
|
`A` { code = KeyCode.up }
|
|
|
|
`B` { code = KeyCode.down }
|
|
|
|
`C` { code = KeyCode.right }
|
|
|
|
`D` { code = KeyCode.left }
|
|
|
|
`F` { code = KeyCode.end }
|
|
|
|
`H` { code = KeyCode.home }
|
|
|
|
`P` { code = KeyCode.f1 }
|
|
|
|
`Q` { code = KeyCode.f2 }
|
|
|
|
`R` { code = KeyCode.f3 }
|
|
|
|
`S` { code = KeyCode.f4 }
|
|
|
|
else {}
|
2020-11-14 10:14:54 +03:00
|
|
|
}
|
|
|
|
} else if buf[1] == `5` {
|
|
|
|
code = KeyCode.page_up
|
|
|
|
} else if buf[1] == `6` {
|
|
|
|
code = KeyCode.page_down
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
return &Event{
|
|
|
|
typ: .key_down
|
|
|
|
code: code
|
|
|
|
utf8: single
|
|
|
|
modifiers: modifiers
|
|
|
|
}, end
|
2020-11-12 14:12:51 +03:00
|
|
|
}
|