summaryrefslogtreecommitdiff
path: root/Widgets/TerminalWidget.cpp
blob: 56782eba37e8eb4cfa2149b561592155fa4a0f2b (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "TerminalWidget.h"
#include "Font.h"
#include "Painter.h"
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <fcntl.h>

extern int g_fd;
TerminalWidget* g_tw;

TerminalWidget::TerminalWidget(Widget* parent)
    : Widget(parent)
{
    g_tw = this;

    setWindowRelativeRect({ 0, 0, (columns() * font().glyphWidth()) + 4, (rows() * font().glyphHeight()) + 4 });

    printf("rekt: %d x %d\n", width(), height());
    m_screen = new CharacterWithAttributes[rows() * columns()]; 
    for (unsigned row = 0; row < m_rows; ++row) {
        for (unsigned column = 0; column < m_columns; ++column) {
            at(row, column).character = ' ';
            at(row, column).attribute = 0x07;
        }
    }
    g_fd = getpt();
    grantpt(g_fd);
    unlockpt(g_fd);
    char buf[1024];
    ptsname_r(g_fd, buf, sizeof(buf));

    if (fork() == 0) {
        close(g_fd);
        setsid();
        int fd = open(buf, O_RDWR);
        dup2(fd, 0);
        dup2(fd, 1);
        dup2(fd, 2);
        signal(SIGWINCH, SIG_IGN);
        ioctl(fd, TIOCSCTTY);
        execl("/bin/bash", "bash", nullptr);
        ASSERT_NOT_REACHED();
    }

    signal(SIGCHLD, SIG_IGN);
}

TerminalWidget::~TerminalWidget()
{
}

CharacterWithAttributes& TerminalWidget::at(unsigned row, unsigned column)
{
    ASSERT(m_screen);
    ASSERT(row < m_rows);
    ASSERT(column < m_columns);
    return m_screen[row * columns() + column];
}

void TerminalWidget::paintEvent(PaintEvent&)
{
    Painter painter(*this);
    painter.fillRect(rect(), Color::Black);

    char buf[2] = { 0, 0 };
    for (unsigned row = 0; row < m_rows; ++row) {
        int y = row * font().glyphHeight();
        for (unsigned column = 0; column < m_columns; ++column) {
            int x = column * font().glyphWidth();
            buf[0] = at(row, column).character;
            painter.drawText({ x + 2, y + 2, width(), font().glyphHeight() }, buf, Painter::TextAlignment::TopLeft, Color(0xa0, 0xa0, 0xa0));
        }
    }

    if (m_belling)
        painter.drawRect(rect(), Color::Red);
}

void TerminalWidget::onReceive(const ByteBuffer& buffer)
{
    for (unsigned i = 0; i < buffer.size(); ++i) {
        onReceive(buffer[i]);
    }
}

void TerminalWidget::onReceive(byte ch)
{
    //printf("receive %02x\n", ch);
    auto scrollScreen = [&] () {
        memmove(m_screen, m_screen + columns(), (m_rows - 1) * columns() * sizeof(CharacterWithAttributes));
        memset(m_screen + (m_rows - 1) * columns(), ' ', columns() * sizeof(CharacterWithAttributes));
    };

    auto addChar = [&] (byte ch) {
        at(m_cursorRow, m_cursorColumn).character = ch;
        if (++m_cursorColumn >= m_columns) {
            m_cursorColumn = 0;
            if (m_cursorRow < (m_rows - 1)) {
                ++m_cursorRow;
            } else {
                scrollScreen();
            }
        }
    };

    switch (ch) {
        case '\n':
            if (m_cursorRow < (m_rows - 1)) {
                ++m_cursorRow;
            } else {
                scrollScreen();
            }
            break;
        case '\r':
            m_cursorColumn = 0;
            break;
        case '\t':
            // FIXME: Respect terminal tab stops.
            while ((m_cursorColumn % 8) != 0 && m_cursorColumn < m_columns) {
                addChar(' ');
            break;
        case '\a':
            bell();
            break;
        case 8:
            if (m_cursorColumn > 0) {
                --m_cursorColumn;
                at(m_cursorRow, m_cursorColumn).character = ' ';
            }
            break;
        case 27:
            printf("TerminalWidget: got escape!\n");
            break;
        default:
            addChar(ch);
            break;
        }
    }
    update();
}

void TerminalWidget::keyDownEvent(KeyEvent& event)
{
    if (event.text().isEmpty())
        return;
    write(g_fd, event.text().characters(), event.text().length());
}

void TerminalWidget::keyUpEvent(KeyEvent& event)
{
    return Widget::keyUpEvent(event);
}

void TerminalWidget::bell()
{
    if (m_belling)
        stopTimer();
    startTimer(250);
    m_belling = true;
    update();
}

void TerminalWidget::timerEvent(TimerEvent&)
{
    m_belling = false;
    stopTimer();
    update();
}