3 * Taken from Palacios console display in MINIX ( by Erik Van der Kouwe )
5 * (c) Peter Dinda, 2011 (Scan code encoding)
11 #include <sys/ioctl.h>
20 #include <linux/keyboard.h>
25 static int use_curses = 0;
26 static int debug_enable = 0;
32 typedef enum { CONSOLE_CURS_SET = 1,
36 CONSOLE_RESOLUTION = 5 } console_op_t;
46 struct termios termios_old;
47 unsigned char old_kbd_mode;
56 } __attribute__((packed));
58 struct character_msg {
63 } __attribute__((packed));
67 } __attribute__((packed));
69 struct resolution_msg {
72 } __attribute__((packed));
78 struct cursor_msg cursor;
79 struct character_msg character;
80 struct scroll_msg scroll;
81 struct resolution_msg resolution;
83 } __attribute__((packed));
88 static int handle_char_set(struct character_msg * msg) {
92 fprintf(stderr, "setting char (%c), at (x=%d, y=%d)\n", c, msg->x, msg->y);
100 if ((c < ' ') || (c >= 127)) {
102 fprintf(stderr, "unexpected control character %d\n", c);
108 /* clip whatever falls outside the visible area to avoid errors */
109 if ((msg->x < 0) || (msg->y < 0) ||
110 (msg->x > console.win->_maxx) ||
111 (msg->y > console.win->_maxy)) {
114 fprintf(stderr, "Char out of range (x=%d,y=%d) MAX:(x=%d,y=%d)\n",
115 msg->x, msg->y, console.win->_maxx, console.win->_maxy);
121 if ((msg->x == console.win->_maxx) &&
122 (msg->y == console.win->_maxy)) {
126 wattron(console.win, COLOR_PAIR(msg->style));
127 mvwaddch(console.win, msg->y, msg->x, c);
128 wattroff(console.win, COLOR_PAIR(msg->style));
131 //stdout text display
132 while (console.y < msg->y) {
138 while (console.x < msg->x) {
146 assert(console.x <= console.cols);
148 if (console.x == console.cols) {
158 int handle_curs_set(struct cursor_msg * msg) {
160 fprintf(stderr, "cursor set: (x=%d, y=%d)\n", msg->x, msg->y);
164 /* nothing to do now, cursor is set before update to make sure it isn't
165 * affected by character_set
176 int handle_scroll(struct scroll_msg * msg) {
177 int lines = msg->lines;
180 fprintf(stderr, "scroll: %d lines\n", lines);
196 int handle_text_resolution(struct resolution_msg * msg) {
198 fprintf(stderr, "text resolution: rows=%d, cols=%d\n", msg->rows, msg->cols);
202 console.rows = msg->rows;
203 console.cols = msg->cols;
208 int handle_update( void ) {
210 fprintf(stderr, "update\n");
215 if ( (console.x >= 0) && (console.y >= 0) &&
216 (console.x <= console.win->_maxx) &&
217 (console.y <= console.win->_maxy) ) {
219 wmove(console.win, console.y, console.x);
223 wrefresh(console.win);
230 int handle_console_msg(int cons_fd) {
234 ret = read(cons_fd, &msg, sizeof(struct cons_msg));
237 case CONSOLE_CURS_SET:
238 // printf("Console cursor set (x=%d, y=%d)\n", msg.cursor.x, msg.cursor.y);
239 handle_curs_set(&(msg.cursor));
241 case CONSOLE_CHAR_SET:
242 handle_char_set(&(msg.character));
243 /* printf("Console character set (x=%d, y=%d, c=%c, style=%c)\n",
244 msg.character.x, msg.character.y, msg.character.c, msg.character.style);*/
247 // printf("Console scroll (lines=%d)\n", msg.scroll.lines);
248 handle_scroll(&(msg.scroll));
251 // printf("Console update\n");
254 case CONSOLE_RESOLUTION:
255 handle_text_resolution(&(msg.resolution));
258 printf("Invalid console message operation (%d)\n", msg.op);
266 int send_key(int cons_fd, char can_code) {
273 void handle_exit(void) {
274 if ( debug_enable ) {
275 fprintf(stderr, "Exiting from console terminal\n");
283 // tcsetattr(STDIN_FILENO, TCSANOW, &console.termios_old);
285 // ioctl(STDIN_FILENO, KDSKBMODE, K_XLATE);
289 #define NO_KEY { 0, 0 }
292 unsigned char scan_code;
293 unsigned char capital;
296 static const struct key_code ascii_to_key_code[] = { // ASCII Value Serves as Index
297 NO_KEY, NO_KEY, {0x50, 0}, {0x48, 0}, // 0x00 - 0x03
298 {0x4B, 0}, {0x4D, 0}, NO_KEY, { 0x0E, 0 }, // 0x04 - 0x07
299 { 0x0E, 0 }, { 0x0F, 0 }, { 0x1C, 0 }, NO_KEY, // 0x08 - 0x0B
300 NO_KEY, { 0x1C, 0 }, NO_KEY, NO_KEY, // 0x0C - 0x0F
301 NO_KEY, NO_KEY, NO_KEY, NO_KEY, // 0x10 - 0x13
302 NO_KEY, NO_KEY, NO_KEY, NO_KEY, // 0x14 - 0x17
303 NO_KEY, NO_KEY, NO_KEY, { 0x01, 0 }, // 0x18 - 0x1B
304 NO_KEY, NO_KEY, NO_KEY, NO_KEY, // 0x1C - 0x1F
305 { 0x39, 0 }, { 0x02, 1 }, { 0x28, 1 }, { 0x04, 1 }, // 0x20 - 0x23
306 { 0x05, 1 }, { 0x06, 1 }, { 0x08, 1 }, { 0x28, 0 }, // 0x24 - 0x27
307 { 0x0A, 1 }, { 0x0B, 1 }, { 0x09, 1 }, { 0x0D, 1 }, // 0x28 - 0x2B
308 { 0x33, 0 }, { 0x0C, 0 }, { 0x34, 0 }, { 0x35, 0 }, // 0x2C - 0x2F
309 { 0x0B, 0 }, { 0x02, 0 }, { 0x03, 0 }, { 0x04, 0 }, // 0x30 - 0x33
310 { 0x05, 0 }, { 0x06, 0 }, { 0x07, 0 }, { 0x08, 0 }, // 0x34 - 0x37
311 { 0x09, 0 }, { 0x0A, 0 }, { 0x27, 1 }, { 0x27, 0 }, // 0x38 - 0x3B
312 { 0x33, 1 }, { 0x0D, 0 }, { 0x34, 1 }, { 0x35, 1 }, // 0x3C - 0x3F
313 { 0x03, 1 }, { 0x1E, 1 }, { 0x30, 1 }, { 0x2E, 1 }, // 0x40 - 0x43
314 { 0x20, 1 }, { 0x12, 1 }, { 0x21, 1 }, { 0x22, 1 }, // 0x44 - 0x47
315 { 0x23, 1 }, { 0x17, 1 }, { 0x24, 1 }, { 0x25, 1 }, // 0x48 - 0x4B
316 { 0x26, 1 }, { 0x32, 1 }, { 0x31, 1 }, { 0x18, 1 }, // 0x4C - 0x4F
317 { 0x19, 1 }, { 0x10, 1 }, { 0x13, 1 }, { 0x1F, 1 }, // 0x50 - 0x53
318 { 0x14, 1 }, { 0x16, 1 }, { 0x2F, 1 }, { 0x11, 1 }, // 0x54 - 0x57
319 { 0x2D, 1 }, { 0x15, 1 }, { 0x2C, 1 }, { 0x1A, 0 }, // 0x58 - 0x5B
320 { 0x2B, 0 }, { 0x1B, 0 }, { 0x07, 1 }, { 0x0C, 1 }, // 0x5C - 0x5F
321 { 0x29, 0 }, { 0x1E, 0 }, { 0x30, 0 }, { 0x2E, 0 }, // 0x60 - 0x63
322 { 0x20, 0 }, { 0x12, 0 }, { 0x21, 0 }, { 0x22, 0 }, // 0x64 - 0x67
323 { 0x23, 0 }, { 0x17, 0 }, { 0x24, 0 }, { 0x25, 0 }, // 0x68 - 0x6B
324 { 0x26, 0 }, { 0x32, 0 }, { 0x31, 0 }, { 0x18, 0 }, // 0x6C - 0x6F
325 { 0x19, 0 }, { 0x10, 0 }, { 0x13, 0 }, { 0x1F, 0 }, // 0x70 - 0x73
326 { 0x14, 0 }, { 0x16, 0 }, { 0x2F, 0 }, { 0x11, 0 }, // 0x74 - 0x77
327 { 0x2D, 0 }, { 0x15, 0 }, { 0x2C, 0 }, { 0x1A, 1 }, // 0x78 - 0x7B
328 { 0x2B, 1 }, { 0x1B, 1 }, { 0x29, 1 }, { 0x0E, 0 } // 0x7C - 0x7F
333 #define writeit(fd,c) do { if (debug_enable) { fprintf(stderr,"scancode 0x%x\n",(c));} if (write((fd),&(c),1)!=1) { return -1; } } while (0)
335 int send_char_to_palacios_as_scancodes(int fd, TermKeyKey * kc)
340 fprintf(stderr,"key '%c'\n",kc->code.number);
343 if (kc->code.number == 'x' &&
344 kc->modifiers & TERMKEY_KEYMOD_CTRL &&
345 kc->modifiers & TERMKEY_KEYMOD_ALT) {
347 } else if (kc->code.number == 'c' &&
348 //kc->modifiers & TERMKEY_KEYMOD_CTRL &&
349 kc->modifiers & TERMKEY_KEYMOD_ALT) {
353 if (kc->code.number<0x80) {
354 struct key_code k = ascii_to_key_code[kc->code.number];
356 if (k.scan_code==0 && k.capital==0) {
358 fprintf(stderr,"Cannot send key '%c' to palacios as it maps to no scancode\n",kc->code.number);
362 if (kc->modifiers & TERMKEY_KEYMOD_CTRL) {
367 if (kc->modifiers & TERMKEY_KEYMOD_ALT) {
374 sc = 0x2a ; // left shift down
380 writeit(fd,sc); // key down
382 sc |= 0x80; // key up
391 if (kc->modifiers & TERMKEY_KEYMOD_CTRL) {
392 sc = 0x1d | 0x80; // left ctrl up
396 if (kc->modifiers & TERMKEY_KEYMOD_ALT) {
397 sc = 0x38 | 0x80; // left alt up
406 fprintf(stderr,"Cannot send key '%c' to palacios because it is >=0x80\n",kc->code.number);
415 static int handle_fnkey (int fd, TermKeyKey * kc) {
419 switch (kc->code.number) {
451 // this is a ctrl+c sequence
452 if (kc->modifiers & TERMKEY_KEYMOD_CTRL) {
458 // this is an exit sequence
459 if (kc->modifiers & TERMKEY_KEYMOD_CTRL) {
465 fprintf(stderr, "Unknown function key <%x>\n", kc->code.number);
478 static int handle_symkey (int fd, TermKeyKey * kc) {
481 switch (kc->code.sym) {
482 case TERMKEY_SYM_BACKSPACE:
485 case TERMKEY_SYM_TAB:
488 case TERMKEY_SYM_ENTER:
491 case TERMKEY_SYM_ESCAPE:
494 case TERMKEY_SYM_SPACE:
497 case TERMKEY_SYM_INSERT:
500 case TERMKEY_SYM_DEL:
506 case TERMKEY_SYM_DOWN:
509 case TERMKEY_SYM_LEFT:
512 case TERMKEY_SYM_RIGHT:
515 case TERMKEY_SYM_PAGEUP:
518 case TERMKEY_SYM_PAGEDOWN:
521 case TERMKEY_SYM_HOME:
524 case TERMKEY_SYM_END:
527 case TERMKEY_SYM_KP0:
530 case TERMKEY_SYM_KP1:
533 case TERMKEY_SYM_KP2:
536 case TERMKEY_SYM_KP3:
539 case TERMKEY_SYM_KP4:
542 case TERMKEY_SYM_KP5:
545 case TERMKEY_SYM_KP6:
548 case TERMKEY_SYM_KP7:
551 case TERMKEY_SYM_KP8:
554 case TERMKEY_SYM_KP9:
557 case TERMKEY_SYM_KPENTER:
560 case TERMKEY_SYM_KPPLUS:
563 case TERMKEY_SYM_KPMINUS:
566 case TERMKEY_SYM_KPMULT:
569 case TERMKEY_SYM_KPDIV:
573 fprintf(stderr, "Unknown sym key\n");
588 #define MIN_TTY_COLS 80
589 #define MIN_TTY_ROWS 25
590 int check_terminal_size (void)
592 unsigned short n_cols = 0;
593 unsigned short n_rows = 0;
594 struct winsize winsz;
596 ioctl (fileno(stdin), TIOCGWINSZ, &winsz);
597 n_cols = winsz.ws_col;
598 n_rows = winsz.ws_row;
600 if (n_cols < MIN_TTY_COLS || n_rows < MIN_TTY_ROWS) {
601 printf ("Your window is not large enough.\n");
602 printf ("It must be at least %dx%d, but yours is %dx%d\n",
603 MIN_TTY_COLS, MIN_TTY_ROWS, n_cols, n_rows);
618 for (i = 0; i < 0x100; i++) {
619 init_pair(i, i & 0xf, (i >> 4) & 0xf);
625 int main(int argc, char* argv[]) {
631 char * vm_dev = NULL;
632 struct termios termios;
633 TERMKEY_CHECK_VERSION;
634 TermKeyFormat format = TERMKEY_FORMAT_VIM;
635 tk = termkey_new(0, TERMKEY_FLAG_SPACESYMBOL|TERMKEY_FLAG_CTRLC);
638 fprintf(stderr, "Could not allocate termkey instance\n");
642 if (termkey_get_flags(tk) & TERMKEY_FLAG_UTF8) {
644 printf("Termkey in UTF-8 mode\n");
646 } else if (termkey_get_flags(tk) & TERMKEY_FLAG_RAW) {
648 printf("Termkey in RAW mode\n");
656 printf("\033[?%dhMouse mode active\n", mouse);
658 printf("\033[?%dh", mouse_proto);
664 printf("usage: v3_cons_sc <vm_device>\n\n"
665 "NOTE: to use CTRL-C within the terminal, use Alt-C\n"
666 " to exit the terminal, use CTRL-ALT-x\n\n");
670 /* Check for minimum Terminal size at start */
671 if (0 != check_terminal_size()) {
672 printf ("Error: terminal too small!\n");
678 vm_fd = open(vm_dev, O_RDONLY);
681 printf("Error opening VM device: %s\n", vm_dev);
685 cons_fd = ioctl(vm_fd, V3_VM_CONSOLE_CONNECT, NULL);
687 /* Close the file descriptor. */
691 printf("Error opening stream Console\n");
704 console.win = initscr();
706 if (console.win == NULL) {
707 fprintf(stderr, "Error initialization curses screen\n");
711 scrollok(console.win, 1);
721 keypad(console.win, TRUE);
730 FD_SET(cons_fd, &rset);
731 FD_SET(STDIN_FILENO, &rset);
733 ret = select(cons_fd + 1, &rset, NULL, NULL, NULL);
737 } else if (ret == -1) {
738 perror("Select returned error...\n");
742 if (FD_ISSET(cons_fd, &rset)) {
743 if (handle_console_msg(cons_fd) == -1) {
744 printf("Console Error\n");
749 if (FD_ISSET(STDIN_FILENO, &rset)) {
751 ret = termkey_waitkey(tk, &key);
753 if(ret == TERMKEY_RES_KEY) {
755 termkey_strfkey(tk, buffer, sizeof buffer, &key, format);
759 case TERMKEY_TYPE_UNICODE: {
761 int ret = send_char_to_palacios_as_scancodes(cons_fd, &key);
764 printf("Error sending key to console\n");
766 } else if (ret == VCONS_CTRLC) {
769 sc = 0x1d; // left ctrl down
773 sc = 0x2e | 0x80; // c up
775 sc = 0x1d | 0x80; // left ctrl up
778 } else if (ret == VCONS_EXIT) {
784 case TERMKEY_TYPE_FUNCTION:
785 handle_fnkey(cons_fd, &key);
787 case TERMKEY_TYPE_KEYSYM:
788 handle_symkey(cons_fd, &key);
791 fprintf(stderr, "Unknown key type\n");
800 printf("Console terminated\n");