Skip to content

Commit

Permalink
move_cursor, sgr: use same approach as termbox-go
Browse files Browse the repository at this point in the history
  • Loading branch information
rofl0r committed Jun 20, 2012
1 parent 5c941c8 commit 154abc8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 54 deletions.
12 changes: 0 additions & 12 deletions src/term.c
Expand Up @@ -13,12 +13,10 @@ static const char *Eterm_funcs[] = {
[T_SHOW_CURSOR] = "\033[?25h",
[T_HIDE_CURSOR] = "\033[?25l",
[T_CLEAR_SCREEN] = "\033[H\033[2J",
[T_SGR] = "\033[3%;4%m",
[T_SGR0] = "\033[m",
[T_UNDERLINE] = "\033[4m",
[T_BOLD] = "\033[1m",
[T_BLINK] = "\033[5m",
[T_MOVE_CURSOR] = "\033[%;%H",
[T_ENTER_KEYPAD] = "",
[T_EXIT_KEYPAD] = "",
};
Expand All @@ -33,12 +31,10 @@ static const char *screen_funcs[] = {
[T_SHOW_CURSOR] = "\033[34h\033[?25h",
[T_HIDE_CURSOR] = "\033[?25l",
[T_CLEAR_SCREEN] = "\033[H\033[J",
[T_SGR] = "\033[3%;4%m",
[T_SGR0] = "\033[m",
[T_UNDERLINE] = "\033[4m",
[T_BOLD] = "\033[1m",
[T_BLINK] = "\033[5m",
[T_MOVE_CURSOR] = "\033[%;%H",
[T_ENTER_KEYPAD] = "\033[?1h\033=",
[T_EXIT_KEYPAD] = "\033[?1l\033>",
};
Expand All @@ -53,12 +49,10 @@ static const char *xterm_funcs[] = {
[T_SHOW_CURSOR] = "\033[?12l\033[?25h",
[T_HIDE_CURSOR] = "\033[?25l",
[T_CLEAR_SCREEN] = "\033[H\033[2J",
[T_SGR] = "\033[3%;4%m",
[T_SGR0] = "\033(B\033[m",
[T_UNDERLINE] = "\033[4m",
[T_BOLD] = "\033[1m",
[T_BLINK] = "\033[5m",
[T_MOVE_CURSOR] = "\033[%;%H",
[T_ENTER_KEYPAD] = "\033[?1h\033=",
[T_EXIT_KEYPAD] = "\033[?1l\033>",
};
Expand All @@ -73,12 +67,10 @@ static const char *rxvt_unicode_funcs[] = {
[T_SHOW_CURSOR] = "\033[?25h",
[T_HIDE_CURSOR] = "\033[?25l",
[T_CLEAR_SCREEN] = "\033[H\033[2J",
[T_SGR] = "\033[3%;4%m",
[T_SGR0] = "\033[m\033(B",
[T_UNDERLINE] = "\033[4m",
[T_BOLD] = "\033[1m",
[T_BLINK] = "\033[5m",
[T_MOVE_CURSOR] = "\033[%;%H",
[T_ENTER_KEYPAD] = "\033=",
[T_EXIT_KEYPAD] = "\033>",
};
Expand All @@ -93,12 +85,10 @@ static const char *linux_funcs[] = {
[T_SHOW_CURSOR] = "\033[?25h\033[?0c",
[T_HIDE_CURSOR] = "\033[?25l\033[?1c",
[T_CLEAR_SCREEN] = "\033[H\033[J",
[T_SGR] = "\033[3%;4%m",
[T_SGR0] = "\033[0;10m",
[T_UNDERLINE] = "\033[4m",
[T_BOLD] = "\033[1m",
[T_BLINK] = "\033[5m",
[T_MOVE_CURSOR] = "\033[%;%H",
[T_ENTER_KEYPAD] = "",
[T_EXIT_KEYPAD] = "",
};
Expand All @@ -113,12 +103,10 @@ static const char *rxvt_256color_funcs[] = {
[T_SHOW_CURSOR] = "\033[?25h",
[T_HIDE_CURSOR] = "\033[?25l",
[T_CLEAR_SCREEN] = "\033[H\033[2J",
[T_SGR] = "\033[3%;4%m",
[T_SGR0] = "\033[m",
[T_UNDERLINE] = "\033[4m",
[T_BOLD] = "\033[1m",
[T_BLINK] = "\033[5m",
[T_MOVE_CURSOR] = "\033[%;%H",
[T_ENTER_KEYPAD] = "\033=",
[T_EXIT_KEYPAD] = "\033>",
};
Expand Down
2 changes: 0 additions & 2 deletions src/term.h
Expand Up @@ -10,12 +10,10 @@ enum {
T_SHOW_CURSOR,
T_HIDE_CURSOR,
T_CLEAR_SCREEN,
T_SGR,
T_SGR0,
T_UNDERLINE,
T_BOLD,
T_BLINK,
T_MOVE_CURSOR,
T_ENTER_KEYPAD,
T_EXIT_KEYPAD
};
Expand Down
84 changes: 44 additions & 40 deletions src/termbox.c
Expand Up @@ -55,6 +55,9 @@ static int cursor_y = -1;
static uint16_t background = TB_BLACK;
static uint16_t foreground = TB_WHITE;

static void write_cursor(unsigned x, unsigned y);
static void write_sgr(uint32_t fg, uint32_t bg);

static void cellbuf_init(struct cellbuf *buf, unsigned int width, unsigned int height);
static void cellbuf_resize(struct cellbuf *buf, unsigned int width, unsigned int height);
static void cellbuf_clear(struct cellbuf *buf);
Expand All @@ -73,35 +76,6 @@ static volatile int buffer_size_change_request;

/* -------------------------------------------------------- */

static unsigned convertnum(uint32_t num, char* buf) {
unsigned i, l = 0;
int ch;
do {
buf[l++] = '0' + (num % 10);
num /= 10;
} while (num);
for(i = 0; i < l / 2; i++) {
ch = buf[i];
buf[i] = buf[l - 1 - i];
buf[l - 1 - i] = ch;
}
return l;
}

static unsigned print2u(const char* seq, uint32_t u1, uint32_t u2, char* bufptr) {
uint32_t numbers[2] = {u1, u2};
char *buf = bufptr;
unsigned idx = 0;
while(*seq) {
if(*seq != '%')
*buf++ = *seq;
else
buf += convertnum(numbers[idx++], buf);
seq++;
}
return buf - bufptr;
}

int tb_init(void)
{
out = fopen("/dev/tty", "w");
Expand Down Expand Up @@ -176,11 +150,6 @@ void tb_shutdown(void)
free_ringbuffer(&inbuf);
}

static void print_move_cursor(unsigned y, unsigned x) {
char buf[32];
memstream_write(&write_buffer, buf, print2u(funcs[T_MOVE_CURSOR], y, x, buf));
}

void tb_present(void)
{
unsigned int x,y;
Expand All @@ -207,7 +176,7 @@ void tb_present(void)
}
}
if (!IS_CURSOR_HIDDEN(cursor_x, cursor_y))
print_move_cursor(cursor_y+1, cursor_x+1);
write_cursor(cursor_x, cursor_y);
memstream_flush(&write_buffer);
}

Expand All @@ -222,7 +191,7 @@ void tb_set_cursor(int cx, int cy)
cursor_x = cx;
cursor_y = cy;
if (!IS_CURSOR_HIDDEN(cursor_x, cursor_y))
print_move_cursor(cursor_y+1, cursor_x+1);
write_cursor(cursor_x, cursor_y);
}

void tb_put_cell(unsigned int x, unsigned int y, const struct tb_cell *cell)
Expand Down Expand Up @@ -301,6 +270,42 @@ void tb_set_clear_attributes(uint16_t fg, uint16_t bg)

/* -------------------------------------------------------- */

static unsigned convertnum(uint32_t num, char* buf) {
unsigned i, l = 0;
int ch;
do {
buf[l++] = '0' + (num % 10);
num /= 10;
} while (num);
for(i = 0; i < l / 2; i++) {
ch = buf[i];
buf[i] = buf[l - 1 - i];
buf[l - 1 - i] = ch;
}
return l;
}

#define WRITE_LITERAL(X) memstream_write(&write_buffer, (X), sizeof(X) -1)
#define WRITE_INT(X) memstream_write(&write_buffer, buf, convertnum((X), buf))

static void write_cursor(unsigned x, unsigned y) {
char buf[32];
WRITE_LITERAL("\033[");
WRITE_INT(y+1);
WRITE_LITERAL(";");
WRITE_INT(x+1);
WRITE_LITERAL("H");
}

static void write_sgr(uint32_t fg, uint32_t bg) {
char buf[32];
WRITE_LITERAL("\033[3");
WRITE_INT(fg);
WRITE_LITERAL(";4");
WRITE_INT(bg);
WRITE_LITERAL("m");
}

static void cellbuf_init(struct cellbuf *buf, unsigned int width, unsigned int height)
{
buf->cells = (struct tb_cell*)malloc(sizeof(struct tb_cell) * width * height);
Expand Down Expand Up @@ -377,10 +382,9 @@ static void send_attr(uint16_t fg, uint16_t bg)
{
#define LAST_ATTR_INIT 0xFFFF
static uint16_t lastfg = LAST_ATTR_INIT, lastbg = LAST_ATTR_INIT;
char buf[32];
if (fg != lastfg || bg != lastbg) {
memstream_puts(&write_buffer, funcs[T_SGR0]);
memstream_write(&write_buffer, buf, print2u(funcs[T_SGR], fg & 0x0F, bg & 0x0F, buf));
write_sgr(fg & 0x0F, bg & 0x0F);
if (fg & TB_BOLD)
memstream_puts(&write_buffer, funcs[T_BOLD]);
if (bg & TB_BOLD)
Expand All @@ -399,7 +403,7 @@ static void send_char(unsigned int x, unsigned int y, uint32_t c)
int bw = utf8_unicode_to_char(buf, c);
buf[bw] = '\0';
if (x-1 != lastx || y != lasty)
print_move_cursor(y+1, x+1);
write_cursor(x, y);
lastx = x; lasty = y;
if(!c) buf[0] = ' '; // replace 0 with whitespace
memstream_puts(&write_buffer, buf);
Expand All @@ -410,7 +414,7 @@ static void send_clear(void)
send_attr(foreground, background);
memstream_puts(&write_buffer, funcs[T_CLEAR_SCREEN]);
if (!IS_CURSOR_HIDDEN(cursor_x, cursor_y))
print_move_cursor(cursor_y+1, cursor_x+1);
write_cursor(cursor_x, cursor_y);
memstream_flush(&write_buffer);

/* we need to invalidate cursor position too and these two vars are
Expand Down

0 comments on commit 154abc8

Please sign in to comment.