1 #include <klibc/string.h>
2 #include <lunaix/device.h>
3 #include <lunaix/input.h>
4 #include <lunaix/ioctl.h>
5 #include <lunaix/keyboard.h>
6 #include <lunaix/mm/pmm.h>
7 #include <lunaix/mm/valloc.h>
8 #include <lunaix/mm/vmm.h>
9 #include <lunaix/sched.h>
10 #include <lunaix/signal.h>
11 #include <lunaix/tty/tty.h>
12 #include <lunaix/ds/fifo.h>
13 #include <lunaix/owloysius.h>
19 struct lx_timer* flush_timer;
20 struct fifo_buf output;
21 struct fifo_buf input;
26 typedef struct write_cmd {
32 static struct console game_console;
34 static waitq_t lx_reader;
37 __game_listener(struct input_device* dev)
39 u32_t key = dev->current_pkt.sys_code;
40 u32_t type = dev->current_pkt.pkt_type;
41 kbd_kstate_t state = key >> 16;
42 u8_t ttychr = key & 0xff;
45 if (type == PKT_RELEASE) {
49 if ((state & KBD_KEY_FLCTRL_HELD)) {
50 char cntrl = (char)(ttychr | 0x20);
51 if ('a' > cntrl || cntrl > 'z') {
54 ttychr = cntrl - 'a' + 1;
55 } else if ((key & 0xff00) <= KEYPAD) {
61 fifo_putone(&game_console.input, ttychr);
62 pwake_all(&lx_reader);
65 return INPUT_EVT_NEXT;
69 __game_write(struct device* dev, void* buf, size_t offset, size_t len);
72 __game_read(struct device* dev, void* buf, size_t offset, size_t len);
75 __game_write_pg(struct device* dev, void* buf, size_t offset)
77 return __game_write(dev, buf, offset, 0x1000);
81 __game_read_pg(struct device* dev, void* buf, size_t offset)
83 return __game_read(dev, buf, offset, 0x1000);
87 __game_write(struct device* dev, void* buf, size_t offset, size_t len)
89 static bool first = 1;
92 for (int i = 0; i < TTY_HEIGHT; i++)
97 tty_set_cursor(TTY_WIDTH-1, TTY_HEIGHT-1);
100 write_cmd *cmd = (write_cmd *)buf;
101 if (cmd->x==0x0a0d) {
103 memcpy(cmd->data, cmd->data+1, TTY_WIDTH+1);
104 }else if (cmd->y==0x0a0d) {
106 memcpy(cmd->data, cmd->data+1, TTY_WIDTH+1);
109 tty_put_str_at((char*)&(cmd->data), cmd->x, cmd->y);
115 __game_read(struct device* dev, void* buf, size_t offset, size_t len)
117 struct console* console = (struct console*)dev->underlay;
118 size_t count = fifo_read(&console->input, buf, len);
126 return count + fifo_read(&console->input, buf + count, len - count);
130 chargame_init(struct device_def* devdef)
132 struct device* tty_dev = device_allocseq(NULL, &game_console);
133 tty_dev->ops.write = __game_write;
134 tty_dev->ops.write_page = __game_write_pg;
135 tty_dev->ops.read = __game_read;
136 tty_dev->ops.read_page = __game_read_pg;
137 tty_dev->ops.read_async = __game_read;
139 waitq_init(&lx_reader);
140 input_add_listener(__game_listener);
142 register_device(tty_dev, &devdef->class, "game");
144 term_create(tty_dev, "G");
146 memset(&game_console, 0, sizeof(game_console));
147 fifo_init(&game_console.output, valloc(8192), 8192, 0);
148 fifo_init(&game_console.input, valloc(4096), 4096, 0);
150 game_console.flush_timer = NULL;
155 static struct device_def chargame_def = {
156 .name = "Character Game Console",
157 .class = DEVCLASS(DEVIF_NON, DEVFN_TTY, DEV_BUILTIN),
158 .init = chargame_init,
161 EXPORT_DEVICE(chargame, &chargame_def, load_onboot);