3 * @author Lunaixsky (lunaxisky@qq.com)
4 * @brief Provides simple terminal support
8 * @copyright Copyright (c) 2023
12 #include <klibc/string.h>
13 #include <lunaix/device.h>
14 #include <lunaix/input.h>
15 #include <lunaix/ioctl.h>
16 #include <lunaix/keyboard.h>
17 #include <lunaix/mm/pmm.h>
18 #include <lunaix/mm/valloc.h>
19 #include <lunaix/mm/vmm.h>
20 #include <lunaix/sched.h>
21 #include <lunaix/signal.h>
22 #include <lunaix/tty/tty.h>
23 #include <lunaix/ds/fifo.h>
24 #include <lunaix/owloysius.h>
30 struct lx_timer* flush_timer;
31 struct fifo_buf output;
32 struct fifo_buf input;
37 static struct console lx_console;
40 __tty_write(struct device* dev, void* buf, size_t offset, size_t len);
43 __tty_read(struct device* dev, void* buf, size_t offset, size_t len);
46 console_write(struct console* console, u8_t* data, size_t size);
57 static waitq_t lx_reader;
59 static volatile pid_t fg_pgid = 0;
62 __lxconsole_listener(struct input_device* dev)
64 u32_t key = dev->current_pkt.sys_code;
65 u32_t type = dev->current_pkt.pkt_type;
66 kbd_kstate_t state = key >> 16;
67 u8_t ttychr = key & 0xff;
70 if (type == PKT_RELEASE) {
74 if ((state & KBD_KEY_FLCTRL_HELD)) {
75 char cntrl = (char)(ttychr | 0x20);
76 if ('a' > cntrl || cntrl > 'z') {
79 ttychr = cntrl - 'a' + 1;
80 } else if (key == KEY_PG_UP) {
83 } else if (key == KEY_PG_DOWN) {
86 } else if ((key & 0xff00) <= KEYPAD) {
92 fifo_putone(&lx_console.input, ttychr);
93 pwake_all(&lx_reader);
96 return INPUT_EVT_NEXT;
100 __tty_write_pg(struct device* dev, void* buf, size_t offset)
102 return __tty_write(dev, buf, offset, PG_SIZE);
106 __tty_read_pg(struct device* dev, void* buf, size_t offset)
108 return __tty_read(dev, buf, offset, PG_SIZE);
112 __tty_write(struct device* dev, void* buf, size_t offset, size_t len)
114 struct console* console = (struct console*)dev->underlay;
115 console_write(console, buf, len);
121 __tty_read(struct device* dev, void* buf, size_t offset, size_t len)
123 struct console* console = (struct console*)dev->underlay;
125 size_t count = fifo_read(&console->input, buf, len);
132 return count + fifo_read(&console->input, buf + count, len - count);
136 __find_next_line(size_t start)
138 size_t p = start - 1;
139 struct fifo_buf* buffer = &lx_console.output;
141 p = (p + 1) % buffer->size;
142 } while (p != buffer->wr_pos && ((char*)buffer->data)[p] != '\n');
143 return p + (((char*)buffer->data)[p] == '\n');
147 __find_prev_line(size_t start)
149 size_t p = start - 1;
150 struct fifo_buf* buffer = &lx_console.output;
153 } while (p < lx_console.wnd_start && p != buffer->wr_pos &&
154 ((char*)buffer->data)[p] != '\n');
156 if (p > lx_console.wnd_start) {
165 struct fifo_buf* buffer = &lx_console.output;
166 mutex_lock(&buffer->lock);
167 fifo_set_rdptr(buffer, __find_prev_line(buffer->rd_pos));
168 buffer->flags |= FIFO_DIRTY;
169 mutex_unlock(&buffer->lock);
177 struct fifo_buf* buffer = &lx_console.output;
178 mutex_lock(&buffer->lock);
180 size_t wnd = lx_console.wnd_start;
181 size_t p = __find_next_line(buffer->rd_pos);
182 fifo_set_rdptr(buffer, p > wnd ? wnd : p);
183 buffer->flags |= FIFO_DIRTY;
184 mutex_unlock(&buffer->lock);
192 if (mutex_on_hold(&lx_console.output.lock)) {
195 if (!(lx_console.output.flags & FIFO_DIRTY)) {
199 size_t rdpos_save = lx_console.output.rd_pos;
200 tty_flush_buffer(&lx_console.output);
201 fifo_set_rdptr(&lx_console.output, rdpos_save);
203 lx_console.output.flags &= ~FIFO_DIRTY;
207 console_write(struct console* console, u8_t* data, size_t size)
209 struct fifo_buf* fbuf = &console->output;
210 mutex_lock(&console->output.lock);
211 fifo_set_rdptr(fbuf, console->wnd_start);
213 u8_t* buffer = fbuf->data;
214 ptr_t ptr = fbuf->wr_pos;
215 ptr_t rd_ptr = fbuf->rd_pos;
218 for (size_t i = 0; i < size; i++) {
225 } else if (c == '\x08') {
226 ptr = ptr ? ptr - 1 : fbuf->size - 1;
230 ptr = (ptr + 1) % fbuf->size;
233 fifo_set_wrptr(fbuf, ptr);
235 while (console->lines >= TTY_HEIGHT) {
236 rd_ptr = __find_next_line(rd_ptr);
240 fifo_set_rdptr(&lx_console.output, rd_ptr);
241 console->wnd_start = rd_ptr;
242 fbuf->flags |= FIFO_DIRTY;
243 mutex_unlock(&fbuf->lock);
245 if (!lx_console.flush_timer) {
251 console_write_str(char* str)
253 console_write(&lx_console, (u8_t*)str, strlen(str));
257 console_write_char(char str)
259 console_write(&lx_console, (u8_t*)&str, 1);
266 memset(&lx_console, 0, sizeof(lx_console));
267 fifo_init(&lx_console.output, valloc(8192), 8192, 0);
268 fifo_init(&lx_console.input, valloc(4096), 4096, 0);
270 lx_console.flush_timer = NULL;
272 owloysius_fetch_init(lxconsole_init, on_earlyboot)
275 lxconsole_spawn_ttydev(struct device_def* devdef)
277 struct device* tty_dev = device_allocseq(NULL, &lx_console);
278 tty_dev->ops.write = __tty_write;
279 tty_dev->ops.write_page = __tty_write_pg;
280 tty_dev->ops.read = __tty_read;
281 tty_dev->ops.read_page = __tty_read_pg;
283 waitq_init(&lx_reader);
284 input_add_listener(__lxconsole_listener);
286 register_device(tty_dev, &devdef->class, "vcon");
288 term_create(tty_dev, "FB");
293 static struct device_def lxconsole_def = {
294 .name = "Lunaix Virtual Console",
295 .class = DEVCLASS(DEVIF_NON, DEVFN_TTY, DEV_BUILTIN),
296 .init = lxconsole_spawn_ttydev
299 EXPORT_DEVICE(lxconsole, &lxconsole_def, load_onboot);