aboutsummaryrefslogtreecommitdiff
path: root/src/line/mode.cc
blob: 9eb54b10ab985179d71c694afb4ab1a7f745ec91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

#include "line/mode.h"

#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <print>

using namespace std;

namespace ov4
{

void disable_raw()
{
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr);
}

void enable_raw()
{
    termios attr_raw;
    tcgetattr(STDIN_FILENO, &attr);
    tcgetattr(STDIN_FILENO, &attr_raw);

    // LOCAL

    // disable
    // echo
    // canonical
    // signal
    // disable post-input processing, aka control-V
    attr_raw.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);

    // INPUT

    // disable 
    // software flow control, aka control-S/Q
    // \n and \r conversion
    attr_raw.c_iflag &= ~(IXON | ICRNL);

    // OUTPUT

    // disable
    // post-output processing
    attr_raw.c_oflag &= ~(OPOST);

    // misc
    attr_raw.c_iflag &= ~(BRKINT | INPCK | ISTRIP);
    attr_raw.c_cflag |= ~(CS8);

    atexit(disable_raw);

    // TCSAFLUSH
    // the change shall occur after all output written to fildes is transmitted
    // and all input so far received but not read shall be discarded before the change is made
    tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr_raw);
}

void clear_below()
{
    print("\x1b[0J");
}

string readline()
{
    string buffer;
    char c;
    while (read(STDIN_FILENO, &c, 1) == 1)
    {
        if (c == 'Q') break;
        else if (c == 'C') 
        {
            clear_below();
        }
        else if (c == 'P')
        {
            get_cursor_position();
        }
        else if (iscntrl(c))
        {
            print("control: {}\r\n", static_cast<unsigned char>(c));
            fflush(stdout);
            if (c == 13) // \n
            {
                //print("line: {}\r\n", buffer);
                return buffer;
            }
        }
        else
        {
            buffer += c;
            print_override(buffer);

            // esc sequence
            if (buffer.size() <= 2 && buffer[0] == '\x1b' && buffer[1] == '[')
            {
                ;
            }
        }
    }
    return buffer;
}

T_position get_cursor_position()
{
    char c;
    string buffer;
    T_position pos;
    int row, col;
    tcflush(STDIN_FILENO, TCIFLUSH);
    cout << "\x1b[6n" << flush;
    while (read(STDIN_FILENO, &c, 1) == 1)
    {
        if (c == 'R') break;
        buffer += c;
    }
    if (buffer.size() <= 2 || buffer[0] != '\x1b' || buffer[1] != '[')
    {
        pos.row = -1;
        pos.col = -1;
        return pos;
    }
    if ((sscanf(buffer.c_str(), "\x1b[%d;%d", &row, &col)) != 2)
    {
        pos.row = -1;
        pos.col = -1;
        return pos;
    }
    pos.row = row;
    pos.col = col;
    print("row {}, col {}\r\n", row, col);
    fflush(stdout);
    return pos;
} 

void print_override(const std::string &s)
{
    print("\r");
    fflush(stdout);
    print("{}", s);
    fflush(stdout);
}

}