Lines Matching refs:tc

11 static inline void invalidate(textcon_t* tc, int x, int y, int w, int h) {
12 tc->invalidate(tc->cookie, x, y, w, h);
14 static inline void movecursor(textcon_t* tc, int x, int y) {
15 tc->movecursor(tc->cookie, x, y);
17 static inline void push_scrollback_line(textcon_t* tc, int y) {
18 tc->push_scrollback_line(tc->cookie, y);
20 static inline void setparam(textcon_t* tc, int param, uint8_t* arg,
22 tc->setparam(tc->cookie, param, arg, arglen);
26 static inline vc_char_t make_vc_char(textcon_t* tc, uint8_t ch) {
28 (((vc_char_t)tc->fg & 15) << 8) |
29 (((vc_char_t)tc->bg & 15) << 12));
32 static vc_char_t* dataxy(textcon_t* tc, int x, int y) {
34 assert(x < tc->w);
36 assert(y < tc->h);
37 return tc->data + y * tc->w + x;
40 static vc_char_t* get_start_of_line(textcon_t* tc, int y) {
42 assert(y <= tc->h);
43 return tc->data + y * tc->w;
46 static int clampx(textcon_t* tc, int x) {
47 return x < 0 ? 0 : x >= tc->w ? tc->w - 1 : x;
50 static int clampxatedge(textcon_t* tc, int x) {
51 return x < 0 ? 0 : x > tc->w ? tc->w : x;
54 static int clampy(textcon_t* tc, int y) {
55 return y < 0 ? 0 : y >= tc->h ? tc->h - 1 : y;
58 static void moveto(textcon_t* tc, int x, int y) {
59 tc->x = clampx(tc, x);
60 tc->y = clampy(tc, y);
63 static inline void moverel(textcon_t* tc, int dx, int dy) {
64 moveto(tc, tc->x + dx, tc->y + dy);
73 static void erase_region(textcon_t* tc, int x0, int y0, int x1, int y1) {
74 if (x0 >= tc->w) {
77 x1 = clampx(tc, x1);
78 vc_char_t* ptr = dataxy(tc, x0, y0);
79 vc_char_t* end = dataxy(tc, x1, y1) + 1;
80 fill(ptr, make_vc_char(tc, ' '), end - ptr);
81 invalidate(tc, x0, y0, x1 - x0 + 1, y1 - y0 + 1);
84 static void erase_screen(textcon_t* tc, int arg) {
87 erase_region(tc, tc->x, tc->y, tc->w - 1, tc->h - 1);
90 erase_region(tc, 0, 0, tc->x, tc->y);
93 erase_region(tc, 0, 0, tc->w - 1, tc->h - 1);
98 static void erase_line(textcon_t* tc, int arg) {
101 erase_region(tc, tc->x, tc->y, tc->w - 1, tc->y);
104 erase_region(tc, 0, tc->y, tc->x, tc->y);
107 erase_region(tc, 0, tc->y, tc->w - 1, tc->y);
112 static void erase_chars(textcon_t* tc, int arg) {
113 if (tc->x >= tc->w) {
119 if (arg > tc->w) {
120 arg = tc->w;
123 vc_char_t* dst = dataxy(tc, tc->x, tc->y);
124 vc_char_t* src = dataxy(tc, tc->x + arg, tc->y);
125 vc_char_t* end = dataxy(tc, tc->x + tc->w, tc->y);
131 *dst++ = make_vc_char(tc, ' ');
134 invalidate(tc, tc->x, tc->y, tc->w - tc->x, 1);
137 void tc_copy_lines(textcon_t* tc, int y_dest, int y_src, int line_count) {
138 vc_char_t* dest = get_start_of_line(tc, y_dest);
139 vc_char_t* src = get_start_of_line(tc, y_src);
140 memmove(dest, src, line_count * tc->w * sizeof(vc_char_t));
143 static void clear_lines(textcon_t* tc, int y, int line_count) {
144 fill(get_start_of_line(tc, y), make_vc_char(tc, ' '), line_count * tc->w);
145 invalidate(tc, 0, y, tc->w, line_count);
151 static void scroll_lines(textcon_t* tc, int y0, int y1, int diff) {
159 push_scrollback_line(tc, y0 + i);
161 tc->copy_lines(tc->cookie, y0, y0 + delta, copy_count);
162 clear_lines(tc, y0 + copy_count, delta);
165 tc->copy_lines(tc->cookie, y0 + delta, y0, copy_count);
166 clear_lines(tc, y0, delta);
170 static void scroll_up(textcon_t* tc) {
171 scroll_lines(tc, tc->scroll_y0, tc->scroll_y1, 1);
175 static void scroll_at_pos(textcon_t* tc, int dir) {
176 if (tc->y < tc->scroll_y0)
178 if (tc->y >= tc->scroll_y1)
181 scroll_lines(tc, tc->y, tc->scroll_y1, dir);
184 void set_scroll(textcon_t* tc, int y0, int y1) {
188 tc->scroll_y0 = (y0 < 0) ? 0 : y0;
189 tc->scroll_y1 = (y1 > tc->h) ? tc->h : y1;
192 static void savecursorpos(textcon_t* tc) {
193 tc->save_x = tc->x;
194 tc->save_y = tc->y;
197 static void restorecursorpos(textcon_t* tc) {
198 tc->x = clampxatedge(tc, tc->save_x);
199 tc->y = clampy(tc, tc->save_y);
202 static void putc_plain(textcon_t* tc, uint8_t c);
203 static void putc_escape2(textcon_t* tc, uint8_t c);
205 static void putc_ignore(textcon_t* tc, uint8_t c) {
206 tc->putc = putc_plain;
209 static void putc_param(textcon_t* tc, uint8_t c) {
221 tc->num = tc->num * 10 + (c - '0');
224 if (tc->argn_count < TC_MAX_ARG) {
225 tc->argn[tc->argn_count++] = tc->num;
227 tc->putc = putc_escape2;
230 if (tc->argn_count < TC_MAX_ARG) {
231 tc->argn[tc->argn_count++] = tc->num;
233 tc->putc = putc_escape2;
234 putc_escape2(tc, c);
239 #define ARG0(def) ((tc->argn_count > 0) ? tc->argn[0] : (def))
240 #define ARG1(def) ((tc->argn_count > 1) ? tc->argn[1] : (def))
242 static void putc_dec(textcon_t* tc, uint8_t c) {
254 tc->num = tc->num * 10 + (c - '0');
257 if (tc->num == 25)
258 setparam(tc, TC_SHOW_CURSOR, NULL, 0);
261 if (tc->num == 25)
262 setparam(tc, TC_HIDE_CURSOR, NULL, 0);
265 putc_plain(tc, c);
268 tc->putc = putc_plain;
280 static void putc_osc2(textcon_t* tc, uint8_t c) {
284 if (param != TC_INVALID && tc->argstr_size)
285 setparam(tc, param, tc->argstr, tc->argstr_size);
286 tc->putc = putc_plain;
290 if (tc->argstr_size < TC_MAX_ARG_LENGTH)
291 tc->argstr[tc->argstr_size++] = c;
296 static void putc_osc(textcon_t* tc, uint8_t c) {
308 tc->num = tc->num * 10 + (c - '0');
311 if (tc->argn_count < TC_MAX_ARG) {
312 tc->argn[tc->argn_count++] = tc->num;
314 memset(tc->argstr, 0, TC_MAX_ARG_LENGTH);
315 tc->argstr_size = 0;
316 tc->putc = putc_osc2;
319 if (tc->argn_count < TC_MAX_ARG) {
320 tc->argn[tc->argn_count++] = tc->num;
322 tc->putc = putc_osc2;
323 putc_osc2(tc, c);
328 static void putc_escape2(textcon_t* tc, uint8_t c) {
341 tc->num = c - '0';
342 tc->putc = putc_param;
345 if (tc->argn_count < TC_MAX_ARG) {
346 tc->argn[tc->argn_count++] = 0;
350 tc->num = 0;
351 tc->argn_count = 0;
352 tc->putc = putc_dec;
355 moverel(tc, 0, -ARG0(1));
358 moverel(tc, 0, ARG0(1));
361 moverel(tc, ARG0(1), 0);
364 moverel(tc, -ARG0(1), 0);
367 moveto(tc, 0, tc->y + ARG0(1));
370 moveto(tc, 0, tc->y - ARG0(1));
374 moveto(tc, x ? (x - 1) : 0, tc->y);
380 moveto(tc, x ? (x - 1) : 0, y ? (y - 1) : 0);
383 erase_screen(tc, ARG0(0));
386 erase_line(tc, ARG0(0));
389 scroll_at_pos(tc, -ARG0(1));
392 scroll_at_pos(tc, ARG0(1));
395 erase_chars(tc, ARG0(1));
399 moveto(tc, tc->x, y ? (y - 1) : 0);
402 if (tc->argn_count == 0) { // no params == default param
403 tc->argn[0] = 0;
404 tc->argn_count = 1;
406 for (int i = 0; i < tc->argn_count; i++) {
407 int n = tc->argn[i];
409 tc->fg = (uint8_t)(n - 30);
411 tc->bg = (uint8_t)(n - 40);
412 } else if ((n == 1) && (tc->fg <= 7)) { // bold
413 tc->fg = (uint8_t)(tc->fg + 8);
415 tc->fg = 0;
416 tc->bg = 15;
418 uint8_t temp = tc->fg;
419 tc->fg = tc->bg;
420 tc->bg = temp;
422 tc->fg = 0;
424 tc->bg = 15;
429 set_scroll(tc, ARG0(1) - 1, ARG1(tc->h));
432 savecursorpos(tc);
435 restorecursorpos(tc);
448 movecursor(tc, tc->x, tc->y);
449 tc->putc = putc_plain;
452 static void putc_escape(textcon_t* tc, uint8_t c) {
461 tc->putc = putc_ignore;
464 tc->num = 0;
465 tc->argn_count = 0;
466 tc->putc = putc_escape2;
469 tc->num = 0;
470 tc->argn_count = 0;
471 tc->putc = putc_osc;
474 savecursorpos(tc);
478 restorecursorpos(tc);
479 movecursor(tc, tc->x, tc->y);
482 tc->x = 0;
485 tc->y++;
486 if (tc->y >= tc->scroll_y1) {
487 tc->y--;
488 scroll_up(tc);
490 movecursor(tc, tc->x, tc->y);
493 tc->y--;
494 if (tc->y < tc->scroll_y0) {
495 tc->y++;
496 scroll_at_pos(tc, -1);
498 movecursor(tc, tc->x, tc->y);
501 tc->putc = putc_plain;
504 static void putc_cr(textcon_t* tc) {
505 tc->x = 0;
508 static void putc_lf(textcon_t* tc) {
509 tc->y++;
510 if (tc->y >= tc->scroll_y1) {
511 tc->y--;
512 scroll_up(tc);
516 static void putc_plain(textcon_t* tc, uint8_t c) {
521 if (tc->x > 0)
522 tc->x--;
525 moveto(tc, (tc->x + 8) & (~7), tc->y);
528 putc_cr(tc); // should we imply this?
529 putc_lf(tc);
532 erase_screen(tc, 2);
535 putc_cr(tc);
538 tc->putc = putc_escape;
544 if (tc->x >= tc->w) {
547 putc_cr(tc);
548 putc_lf(tc);
550 dataxy(tc, tc->x, tc->y)[0] = make_vc_char(tc, c);
551 invalidate(tc, tc->x, tc->y, 1, 1);
552 tc->x++;
555 movecursor(tc, tc->x, tc->y);
558 void tc_init(textcon_t* tc, int w, int h, vc_char_t* data,
560 tc->w = w;
561 tc->h = h;
562 tc->x = cursor_x;
563 tc->y = cursor_y;
564 tc->data = data;
565 tc->scroll_y0 = 0;
566 tc->scroll_y1 = h;
567 tc->save_x = 0;
568 tc->save_y = 0;
569 tc->fg = fg;
570 tc->bg = bg;
571 tc->putc = putc_plain;
574 void tc_seth(textcon_t* tc, int h) {
575 // tc->data must be big enough for the new height
577 tc->h = h;
582 vc_char_t* dst = dataxy(tc, 0, tc->scroll_y0);
583 vc_char_t* src = dataxy(tc, 0, tc->scroll_y0 + old_h - h);
584 vc_char_t* end = dataxy(tc, 0, tc->scroll_y1);
586 push_scrollback_line(tc, y);
589 tc->y -= old_h - h;
592 fill(dataxy(tc, 0, tc->scroll_y1 + y), make_vc_char(tc, ' '), tc->w);
595 tc->y = clampy(tc, tc->y);
598 if (tc->scroll_y0 >= h) {
599 tc->scroll_y0 = 0;
601 if (tc->scroll_y1 == old_h) {
602 tc->scroll_y1 = h;
604 tc->scroll_y1 = tc->scroll_y1 >= h ? h : tc->scroll_y1;
607 invalidate(tc, 0, 0, tc->w, tc->h);
608 movecursor(tc, tc->x, tc->y);