3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License (LGPL) as published by the Free Software Foundation.
7 Please refer to the COPYING file for more information.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 Copyright © 2004 Bruno T. C. de Oliveira
19 Copyright © 2006 Pierre Habouzit
33 #include <sys/types.h>
38 #define IS_CONTROL(ch) !((ch) & 0xffffff60UL)
42 C0_SOH, C0_STX, C0_ETX, C0_EOT, C0_ENQ, C0_ACK, C0_BEL,
43 C0_BS , C0_HT , C0_LF , C0_VT , C0_FF , C0_CR , C0_SO , C0_SI ,
44 C0_DLE, C0_DC1, C0_DC2, D0_DC3, C0_DC4, C0_NAK, C0_SYN, C0_ETB,
45 C0_CAN, C0_EM , C0_SUB, C0_ESC, C0_IS4, C0_IS3, C0_IS2, C0_IS1,
50 C1_41 , C1_BPH, C1_NBH, C1_44 , C1_NEL, C1_SSA, C1_ESA,
51 C1_HTS, C1_HTJ, C1_VTS, C1_PLD, C1_PLU, C1_RI , C1_SS2, C1_SS3,
52 C1_DCS, C1_PU1, C1_PU2, C1_STS, C1_CCH, C1_MW , C1_SPA, C1_EPA,
53 C1_SOS, C1_59 , C1_SCI, C1_CSI, CS_ST , C1_OSC, C1_PM , C1_APC,
58 CSI_CUU, CSI_CUD, CSI_CUF, CSI_CUB, CSI_CNL, CSI_CPL, CSI_CHA,
59 CSI_CUP, CSI_CHT, CSI_ED , CSI_EL , CSI_IL , CSI_DL , CSI_EF , CSI_EA ,
60 CSI_DCH, CSI_SEE, CSI_CPR, CSI_SU , CSI_SD , CSI_NP , CSI_PP , CSI_CTC,
61 CSI_ECH, CSI_CVT, CSI_CBT, CSI_SRS, CSI_PTX, CSI_SDS, CSI_SIMD, CSI_5F,
62 CSI_HPA, CSI_HPR, CSI_REP, CSI_DA , CSI_VPA, CSI_VPR, CSI_HVP, CSI_TBC,
63 CSI_SM , CSI_MC , CSI_HPB, CSI_VPB, CSI_RM , CSI_SGR, CSI_DSR, CSI_DAQ,
64 CSI_70 , CSI_71 , CSI_72 , CSI_73 , CSI_74 , CSI_75 , CSI_76 , CSI_77 ,
65 CSI_78 , CSI_79 , CSI_7A , CSI_7B , CSI_7C , CSI_7D , CSI_7E , CSI_7F
74 static char const * const keytable[KEY_MAX+1] = {
80 [KEY_BACKSPACE] = "\177",
85 [KEY_PPAGE] = "\e[5~",
86 [KEY_NPAGE] = "\e[6~",
87 [KEY_SUSPEND] = "\x1A", /* Ctrl+Z gets mapped to this */
93 [KEY_F(6)] = "\e[17~",
94 [KEY_F(7)] = "\e[18~",
95 [KEY_F(8)] = "\e[19~",
96 [KEY_F(9)] = "\e[20~",
97 [KEY_F(10)] = "\e[21~",
100 static void mtty_row_set(mtty_row_t *row, int start, int len, uint16_t attr)
103 wmemset(row->text + start, 0, len);
104 for (int i = start; i < len + start; i++) {
109 static void mtty_row_roll(mtty_row_t *start, mtty_row_t *end, int count)
118 mtty_row_t *buf = alloca(count * sizeof(mtty_row_t));
120 memcpy(buf, start, count * sizeof(mtty_row_t));
121 memmove(start, start + count, (n - count) * sizeof(mtty_row_t));
122 memcpy(end - count, buf, count * sizeof(mtty_row_t));
123 for (mtty_row_t *row = start; row < end; row++) {
129 static void clamp_cursor_to_bounds(madtty_t *rt)
131 if (rt->curs_row < rt->lines) {
132 rt->curs_row = rt->lines;
134 if (rt->curs_row >= rt->lines + rt->rows) {
135 rt->curs_row = rt->lines + rt->rows - 1;
138 if (rt->curs_col < 0) {
141 if (rt->curs_col >= rt->cols) {
142 rt->curs_col = rt->cols - 1;
146 static void cursor_line_down(madtty_t *rt)
149 if (rt->curs_row < rt->scroll_bot)
152 rt->curs_row = rt->scroll_bot - 1;
153 mtty_row_roll(rt->scroll_top, rt->scroll_bot, 1);
154 mtty_row_set(rt->curs_row, 0, rt->cols, 0);
157 __attribute__((const))
158 static uint16_t build_attrs(unsigned curattrs)
160 return ((curattrs & ~A_COLOR) | COLOR_PAIR(curattrs & 0xff))
161 >> NCURSES_ATTR_SHIFT;
164 static void new_escape_sequence(madtty_t *rt)
171 static void cancel_escape_sequence(madtty_t *rt)
178 static bool is_valid_csi_ender(int c)
180 return (c >= 'a' && c <= 'z')
181 || (c >= 'A' && c <= 'Z')
182 || (c == '@' || c == '`');
185 /* interprets a 'set attribute' (SGR) CSI escape sequence */
186 static void interpret_csi_SGR(madtty_t *rt, int param[], int pcount)
191 /* special case: reset attributes */
192 rt->curattrs = A_NORMAL;
196 for (i = 0; i < pcount; i++) {
198 #define CASE(x, op) case x: op; break
199 CASE(0, rt->curattrs = A_NORMAL);
200 CASE(1, rt->curattrs |= A_BOLD);
201 CASE(4, rt->curattrs |= A_UNDERLINE);
202 CASE(5, rt->curattrs |= A_BLINK);
203 CASE(6, rt->curattrs |= A_BLINK);
204 CASE(7, rt->curattrs |= A_REVERSE);
205 CASE(8, rt->curattrs |= A_INVIS);
206 CASE(22, rt->curattrs &= ~A_BOLD);
207 CASE(24, rt->curattrs &= ~A_UNDERLINE);
208 CASE(25, rt->curattrs &= ~A_BLINK);
209 CASE(27, rt->curattrs &= ~A_REVERSE);
210 CASE(28, rt->curattrs &= ~A_INVIS);
213 rt->curattrs &= ~0xf0;
214 rt->curattrs |= (param[i] - 29) << 4;
218 rt->curattrs &= ~0xf0;
222 rt->curattrs &= ~0x0f;
223 rt->curattrs |= (param[i] - 39);
227 rt->curattrs &= ~0x0f;
236 /* interprets an 'erase display' (ED) escape sequence */
237 static void interpret_csi_ED(madtty_t *rt, int param[], int pcount)
239 mtty_row_t *row, *start, *end;
240 attr_t attr = build_attrs(rt->curattrs);
243 if (pcount && param[0] == 2) {
245 end = rt->lines + rt->rows;
247 if (pcount && param[0] == 1) {
250 mtty_row_set(rt->curs_row, 0, rt->curs_col + 1, attr);
252 mtty_row_set(rt->curs_row, rt->curs_col,
253 rt->cols - rt->curs_col, attr);
254 start = rt->curs_row + 1;
255 end = rt->lines + rt->rows;
258 for (row = start; row < end; row++) {
259 mtty_row_set(row, 0, rt->cols, attr);
263 /* interprets a 'move cursor' (CUP) escape sequence */
264 static void interpret_csi_CUP(madtty_t *rt, int param[], int pcount)
268 rt->curs_row = rt->lines;
273 return; /* malformed */
276 rt->curs_row = rt->lines + param[0] - 1; /* convert from 1-based to 0-based */
277 rt->curs_col = param[1] - 1; /* convert from 1-based to 0-based */
279 clamp_cursor_to_bounds(rt);
282 /* Interpret the 'relative mode' sequences: CUU, CUD, CUF, CUB, CNL,
283 * CPL, CHA, HPR, VPA, VPR, HPA */
284 static void interpret_csi_C(madtty_t *rt, char verb, int param[], int pcount)
286 int n = (pcount && param[0] > 0) ? param[0] : 1;
289 case 'A': rt->curs_row -= n; break;
290 case 'B': case 'e': rt->curs_row += n; break;
291 case 'C': case 'a': rt->curs_col += n; break;
292 case 'D': rt->curs_col -= n; break;
293 case 'E': rt->curs_row += n; rt->curs_col = 0; break;
294 case 'F': rt->curs_row -= n; rt->curs_col = 0; break;
295 case 'G': case '`': rt->curs_col = param[0] - 1; break;
296 case 'd': rt->curs_row = rt->lines + param[0] - 1; break;
299 clamp_cursor_to_bounds(rt);
302 /* Interpret the 'erase line' escape sequence */
303 static void interpret_csi_EL(madtty_t *rt, int param[], int pcount)
305 attr_t attr = build_attrs(rt->curattrs);
307 switch (pcount ? param[0] : 0) {
309 mtty_row_set(rt->curs_row, 0, rt->curs_col + 1, attr);
312 mtty_row_set(rt->curs_row, 0, rt->cols, attr);
315 mtty_row_set(rt->curs_row, rt->curs_col, rt->cols - rt->curs_col,
321 /* Interpret the 'insert blanks' sequence (ICH) */
322 static void interpret_csi_ICH(madtty_t *rt, int param[], int pcount)
324 mtty_row_t *row = rt->curs_row;
325 int n = (pcount && param[0] > 0) ? param[0] : 1;
328 if (rt->curs_col + n > rt->cols) {
329 n = rt->cols - rt->curs_col;
332 for (i = rt->cols - 1; i >= rt->curs_col + n; i--) {
333 row->text[i] = row->text[i - n];
334 row->attr[i] = row->attr[i - n];
337 mtty_row_set(row, rt->curs_col, n, build_attrs(rt->curattrs));
340 /* Interpret the 'delete chars' sequence (DCH) */
341 static void interpret_csi_DCH(madtty_t *rt, int param[], int pcount)
343 mtty_row_t *row = rt->curs_row;
344 int n = (pcount && param[0] > 0) ? param[0] : 1;
347 if (rt->curs_col + n > rt->cols) {
348 n = rt->cols - rt->curs_col;
351 for (i = rt->curs_col; i < rt->cols - n; i++) {
352 row->text[i] = row->text[i + n];
353 row->attr[i] = row->attr[i + n];
356 mtty_row_set(row, rt->cols - n, n, build_attrs(rt->curattrs));
359 /* Interpret a 'scroll reverse' (SR) */
360 static void interpret_csi_SR(madtty_t *rt)
362 mtty_row_roll(rt->scroll_top, rt->scroll_bot, -1);
363 mtty_row_set(rt->scroll_top, 0, rt->cols, build_attrs(rt->curattrs));
366 /* Interpret an 'insert line' sequence (IL) */
367 static void interpret_csi_IL(madtty_t *rt, int param[], int pcount)
369 int n = (pcount && param[0] > 0) ? param[0] : 1;
371 if (rt->curs_row + n >= rt->scroll_bot) {
372 for (mtty_row_t *row = rt->curs_row; row < rt->scroll_bot; row++) {
373 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
376 mtty_row_roll(rt->curs_row, rt->scroll_bot, -n);
377 for (mtty_row_t *row = rt->curs_row; row < rt->curs_row + n; row++) {
378 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
383 /* Interpret a 'delete line' sequence (DL) */
384 static void interpret_csi_DL(madtty_t *rt, int param[], int pcount)
386 int n = (pcount && param[0] > 0) ? param[0] : 1;
388 if (rt->curs_row + n >= rt->scroll_bot) {
389 for (mtty_row_t *row = rt->curs_row; row < rt->scroll_bot; row++) {
390 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
393 mtty_row_roll(rt->curs_row, rt->scroll_bot, n);
394 for (mtty_row_t *row = rt->scroll_bot - n; row < rt->scroll_bot; row++) {
395 mtty_row_set(row, 0, rt->cols, build_attrs(rt->curattrs));
400 /* Interpret an 'erase characters' (ECH) sequence */
401 static void interpret_csi_ECH(madtty_t *rt, int param[], int pcount)
403 int n = (pcount && param[0] > 0) ? param[0] : 1;
405 if (rt->curs_col + n < rt->cols) {
406 n = rt->cols - rt->curs_col;
408 mtty_row_set(rt->curs_row, rt->curs_col, n, build_attrs(rt->curattrs));
411 /* Interpret a 'set scrolling region' (DECSTBM) sequence */
412 static void interpret_csi_DECSTBM(madtty_t *rt, int param[], int pcount)
414 int new_top, new_bot;
418 rt->scroll_top = rt->lines;
419 rt->scroll_bot = rt->lines + rt->rows;
422 return; /* malformed */
425 new_top = param[0] - 1;
428 /* clamp to bounds */
431 if (new_top >= rt->rows)
432 new_top = rt->rows - 1;
435 if (new_bot >= rt->rows)
438 /* check for range validity */
439 if (new_top < new_bot) {
440 rt->scroll_top = rt->lines + new_top;
441 rt->scroll_bot = rt->lines + new_bot;
447 static void es_interpret_csi(madtty_t *rt)
449 static int csiparam[MAX_CSI_ES_PARAMS];
451 const char *p = rt->ebuf + 1;
452 char verb = rt->ebuf[rt->elen - 1];
454 p += rt->ebuf[1] == '?'; /* CSI private mode */
456 /* parse numeric parameters */
457 while (isdigit((unsigned char)*p) || *p == ';') {
459 if (param_count >= MAX_CSI_ES_PARAMS) return; /* too long! */
460 csiparam[param_count++] = 0;
462 if (param_count == 0) csiparam[param_count++] = 0;
463 csiparam[param_count - 1] *= 10;
464 csiparam[param_count - 1] += *p - '0';
470 if (rt->ebuf[1] == '?') {
473 if (csiparam[0] == 25)
478 if (csiparam[0] == 25)
484 /* delegate handling depending on command character (verb) */
487 if (param_count == 1 && csiparam[0] == 4) /* insert mode */
491 if (param_count == 1 && csiparam[0] == 4) /* replace mode */
494 case 'm': /* it's a 'set attribute' sequence */
495 interpret_csi_SGR(rt, csiparam, param_count); break;
496 case 'J': /* it's an 'erase display' sequence */
497 interpret_csi_ED(rt, csiparam, param_count); break;
498 case 'H': case 'f': /* it's a 'move cursor' sequence */
499 interpret_csi_CUP(rt, csiparam, param_count); break;
500 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
501 case 'e': case 'a': case 'd': case '`':
502 /* it is a 'relative move' */
503 interpret_csi_C(rt, verb, csiparam, param_count); break;
504 case 'K': /* erase line */
505 interpret_csi_EL(rt, csiparam, param_count); break;
506 case '@': /* insert characters */
507 interpret_csi_ICH(rt, csiparam, param_count); break;
508 case 'P': /* delete characters */
509 interpret_csi_DCH(rt, csiparam, param_count); break;
510 case 'L': /* insert lines */
511 interpret_csi_IL(rt, csiparam, param_count); break;
512 case 'M': /* delete lines */
513 interpret_csi_DL(rt, csiparam, param_count); break;
514 case 'X': /* erase chars */
515 interpret_csi_ECH(rt, csiparam, param_count); break;
516 case 'r': /* set scrolling region */
517 interpret_csi_DECSTBM(rt, csiparam, param_count); break;
518 case 's': /* save cursor location */
519 rt->curs_srow = rt->curs_row - rt->lines;
520 rt->curs_scol = rt->curs_col;
522 case 'u': /* restore cursor location */
523 rt->curs_row = rt->lines + rt->curs_srow;
524 rt->curs_col = rt->curs_scol;
525 clamp_cursor_to_bounds(rt);
532 static void try_interpret_escape_seq(madtty_t *rt)
534 char lastchar = rt->ebuf[rt->elen-1];
541 interpret_csi_SR(rt);
542 cancel_escape_sequence(rt);
551 case ']': /* xterm thing */
552 if (lastchar == '\a')
560 if (is_valid_csi_ender(lastchar)) {
561 es_interpret_csi(rt);
562 cancel_escape_sequence(rt);
568 if (rt->elen + 1 >= (int)sizeof(rt->ebuf)) {
572 fprintf(stderr, "cancelled: \\033");
573 for (i = 0; i < rt->elen; i++) {
575 if (isprint(c) && c >= ' ') {
578 fprintf(stderr, "\\%03o", c);
583 cancel_escape_sequence(rt);
587 static void madtty_process_nonprinting(madtty_t *rt, wchar_t wc)
591 new_escape_sequence(rt);
595 /* do nothing for now... maybe a visual bell would be nice? */
599 if (rt->curs_col > 0)
603 case C0_HT: /* tab */
604 rt->curs_col = (rt->curs_col + 8) & ~7;
605 if (rt->curs_col >= rt->cols)
606 rt->curs_col = rt->cols - 1;
616 cursor_line_down(rt);
619 case C0_SO: /* shift out - acs */
620 rt->graphmode = true;
622 case C0_SI: /* shift in - acs */
623 rt->graphmode = false;
628 void madtty_putc(madtty_t *rt, wchar_t wc)
632 if (!rt->seen_input) {
634 kill(-rt->childpid, SIGWINCH);
638 if (wc == '\n' || (wc >= ' ' && isprint(wc))) {
641 fprintf(stderr, "\\%03o", wc);
646 assert (rt->elen + 1 < (int)sizeof(rt->ebuf));
647 rt->ebuf[rt->elen] = wc;
648 rt->ebuf[++rt->elen] = '\0';
649 try_interpret_escape_seq(rt);
650 } else if (IS_CONTROL(wc)) {
651 madtty_process_nonprinting(rt, wc);
656 // vt100 special graphics and line drawing
657 // 5f-7e standard vt100
658 // 40-5e rxvt extension for extra curses acs chars
659 static uint16_t vt100_0[62] = { // 41 .. 7e
660 0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603, // 41-47 hi mr. snowman!
661 0, 0, 0, 0, 0, 0, 0, 0, // 48-4f
662 0, 0, 0, 0, 0, 0, 0, 0, // 50-57
663 0, 0, 0, 0, 0, 0, 0, 0x0020, // 58-5f
664 0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1, // 60-67
665 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba, // 68-6f
666 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c, // 70-77
667 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, // 78-7e
670 if (wc >= 0x41 && wc <= 0x7e && vt100_0[wc - 0x41]) {
671 wc = vt100_0[wc - 0x41];
675 width = wcwidth(wc) ?: 1;
678 if (width == 2 && rt->curs_col == rt->cols - 1) {
681 tmp->text[rt->curs_col] = 0;
682 tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
686 if (rt->curs_col >= rt->cols) {
688 cursor_line_down(rt);
695 wmemmove(tmp->text + rt->curs_col + width, tmp->text + rt->curs_col,
696 (rt->cols - rt->curs_col - width));
697 memmove(tmp->attr + rt->curs_col + width, tmp->attr + rt->curs_col,
698 (rt->cols - rt->curs_col - width) * sizeof(tmp->attr[0]));
701 tmp->text[rt->curs_col] = wc;
702 tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
705 tmp->text[rt->curs_col] = 0;
706 tmp->attr[rt->curs_col] = build_attrs(rt->curattrs);
712 int madtty_process(madtty_t *rt)
721 res = read(rt->pty, rt->rbuf + rt->rlen, sizeof(rt->rbuf) - rt->rlen);
726 while (pos < rt->rlen) {
730 len = (ssize_t)mbrtowc(&wc, rt->rbuf + pos, rt->rlen - pos, &rt->ps);
733 memmove(rt->rbuf, rt->rbuf + pos, rt->rlen);
742 pos += len ? len : 1;
747 memmove(rt->rbuf, rt->rbuf + pos, rt->rlen);
751 madtty_t *madtty_create(int rows, int cols)
756 if (rows <= 0 || cols <= 0)
759 rt = (madtty_t*)calloc(sizeof(madtty_t), 1);
763 /* record dimensions */
767 /* default mode is replace */
770 /* create the cell matrix */
771 rt->lines = (mtty_row_t*)calloc(sizeof(mtty_row_t), rt->rows);
772 for (i = 0; i < rt->rows; i++) {
773 rt->lines[i].text = (wchar_t *)calloc(sizeof(wchar_t), rt->cols);
774 rt->lines[i].attr = (uint16_t *)calloc(sizeof(uint16_t), rt->cols);
777 rt->pty = -1; /* no pty for now */
779 /* initialization of other public fields */
780 rt->curs_row = rt->lines;
782 rt->curattrs = A_NORMAL; /* white text over black background */
784 /* initial scrolling area is the whole window */
785 rt->scroll_top = rt->lines;
786 rt->scroll_bot = rt->lines + rt->rows;
791 void madtty_destroy(madtty_t *rt)
797 for (i = 0; i < rt->rows; i++) {
798 free(rt->lines[i].text);
799 free(rt->lines[i].attr);
805 void madtty_draw(madtty_t *rt, WINDOW *win, int srow, int scol)
808 for (int i = 0; i < rt->rows; i++) {
809 mtty_row_t *row = rt->lines + i;
815 wmove(win, srow + i, scol);
816 for (int j = 0; j < rt->cols; j++) {
817 if (!j || row->attr[j] != row->attr[j - 1])
818 wattrset(win, (attr_t)row->attr[j] << NCURSES_ATTR_SHIFT);
819 if (row->text[j] >= 128) {
820 char buf[MB_CUR_MAX + 1];
823 len = wcrtomb(buf, row->text[j], NULL);
824 waddnstr(win, buf, len);
825 if (wcwidth(row->text[j]) > 1)
828 waddch(win, row->text[j] > ' ' ? row->text[j] : ' ');
834 wmove(win, srow + rt->curs_row - rt->lines, scol + rt->curs_col);
835 curs_set(!rt->curshid);
838 /******************************************************/
840 pid_t madtty_forkpty(madtty_t *rt, const char *path, const char *argv[])
845 ws.ws_row = rt->rows;
846 ws.ws_col = rt->cols;
847 ws.ws_xpixel = ws.ws_ypixel = 0;
849 pid = forkpty(&rt->pty, NULL, NULL, &ws);
855 setenv("TERM", "rxvt", 1);
856 execv(path, (char *const*)argv);
857 fprintf(stderr, "\nexecv() failed.\nCommand: '%s'\n", argv[0]);
861 return rt->childpid = pid;
864 void madtty_keypress(madtty_t *rt, int keycode)
866 char c = (char)keycode;
871 if (keycode == KEY_F(1)) {
872 #define MIN(a, b) ((a < (b)) ? a : (b))
873 kill(-rt->childpid, SIGWINCH);
874 rt->scroll_bot = MIN(rt->scroll_bot, rt->lines + rt->rows);
875 rt->curs_row = MIN(rt->curs_row, rt->lines + rt->rows);
876 printf(stderr, "%d\n", rt->rows);
880 if (keycode >= 0 && keycode < KEY_MAX && keytable[keycode]) {
881 buf = keytable[keycode];
882 len = strlen(keytable[keycode]);
889 int res = write(rt->pty, buf, len);
890 if (res < 0 && errno != EAGAIN && errno != EINTR)
898 void madtty_initialize(void)
900 setlocale(LC_ALL, "");
903 use_default_colors();
906 nodelay(stdscr, TRUE);
907 keypad(stdscr, TRUE);
910 for (int i = -1; i < 8; i++) {
911 for (int j = -1; j < 8; j++) {
912 init_pair((i + 1) * 16 + j + 1, i, j);