Permalink
Browse files

1139

Reindent termbox.
  • Loading branch information...
akkartik committed Apr 23, 2015
1 parent a6defa5 commit 9a31c34f0f2aab901be29e0844ae2fcb9b3f4a13
Showing with 864 additions and 864 deletions.
  1. +46 −46 cpp/termbox/bytebuffer.inl
  2. +107 −107 cpp/termbox/input.inl
  3. +213 −213 cpp/termbox/term.inl
  4. +440 −440 cpp/termbox/termbox.c
  5. +11 −11 cpp/termbox/termbox.h
  6. +47 −47 cpp/termbox/utf8.c
View
@@ -1,79 +1,79 @@
struct bytebuffer {
char *buf;
int len;
int cap;
char *buf;
int len;
int cap;
};
static void bytebuffer_reserve(struct bytebuffer *b, int cap) {
if (b->cap >= cap) {
return;
}
if (b->cap >= cap) {
return;
}
// prefer doubling capacity
if (b->cap * 2 >= cap) {
cap = b->cap * 2;
}
// prefer doubling capacity
if (b->cap * 2 >= cap) {
cap = b->cap * 2;
}
char *newbuf = malloc(cap);
if (b->len > 0) {
// copy what was there, b->len > 0 assumes b->buf != null
memcpy(newbuf, b->buf, b->len);
}
if (b->buf) {
// in case there was an allocated buffer, free it
free(b->buf);
}
b->buf = newbuf;
b->cap = cap;
char *newbuf = malloc(cap);
if (b->len > 0) {
// copy what was there, b->len > 0 assumes b->buf != null
memcpy(newbuf, b->buf, b->len);
}
if (b->buf) {
// in case there was an allocated buffer, free it
free(b->buf);
}
b->buf = newbuf;
b->cap = cap;
}
static void bytebuffer_init(struct bytebuffer *b, int cap) {
b->cap = 0;
b->len = 0;
b->buf = 0;
b->cap = 0;
b->len = 0;
b->buf = 0;
if (cap > 0) {
b->cap = cap;
b->buf = malloc(cap); // just assume malloc works always
}
if (cap > 0) {
b->cap = cap;
b->buf = malloc(cap); // just assume malloc works always
}
}
static void bytebuffer_free(struct bytebuffer *b) {
if (b->buf)
free(b->buf);
if (b->buf)
free(b->buf);
}
static void bytebuffer_clear(struct bytebuffer *b) {
b->len = 0;
b->len = 0;
}
static void bytebuffer_append(struct bytebuffer *b, const char *data, int len) {
bytebuffer_reserve(b, b->len + len);
memcpy(b->buf + b->len, data, len);
b->len += len;
bytebuffer_reserve(b, b->len + len);
memcpy(b->buf + b->len, data, len);
b->len += len;
}
static void bytebuffer_puts(struct bytebuffer *b, const char *str) {
bytebuffer_append(b, str, strlen(str));
bytebuffer_append(b, str, strlen(str));
}
static void bytebuffer_resize(struct bytebuffer *b, int len) {
bytebuffer_reserve(b, len);
b->len = len;
bytebuffer_reserve(b, len);
b->len = len;
}
static void bytebuffer_flush(struct bytebuffer *b, int fd) {
int yyy = write(fd, b->buf, b->len);
int yyy = write(fd, b->buf, b->len);
(void) yyy;
bytebuffer_clear(b);
bytebuffer_clear(b);
}
static void bytebuffer_truncate(struct bytebuffer *b, int n) {
if (n <= 0)
return;
if (n > b->len)
n = b->len;
const int nmove = b->len - n;
memmove(b->buf, b->buf+n, nmove);
b->len -= n;
if (n <= 0)
return;
if (n > b->len)
n = b->len;
const int nmove = b->len - n;
memmove(b->buf, b->buf+n, nmove);
b->len -= n;
}
View
@@ -3,130 +3,130 @@
// s2 should be null-terminated
static bool starts_with(const char *s1, int len, const char *s2)
{
int n = 0;
while (*s2 && n < len) {
if (*s1++ != *s2++)
return false;
n++;
}
return *s2 == 0;
int n = 0;
while (*s2 && n < len) {
if (*s1++ != *s2++)
return false;
n++;
}
return *s2 == 0;
}
// convert escape sequence to event, and return consumed bytes on success (failure == 0)
static int parse_escape_seq(struct tb_event *event, const char *buf, int len)
{
if (len >= 6 && starts_with(buf, len, "\033[M")) {
if (len >= 6 && starts_with(buf, len, "\033[M")) {
switch (buf[3] & 3) {
case 0:
if (buf[3] == 0x60)
event->key = TB_KEY_MOUSE_WHEEL_UP;
else
event->key = TB_KEY_MOUSE_LEFT;
break;
case 1:
if (buf[3] == 0x61)
event->key = TB_KEY_MOUSE_WHEEL_DOWN;
else
event->key = TB_KEY_MOUSE_MIDDLE;
break;
case 2:
event->key = TB_KEY_MOUSE_RIGHT;
break;
case 3:
event->key = TB_KEY_MOUSE_RELEASE;
break;
default:
return -6;
}
event->type = TB_EVENT_MOUSE; // TB_EVENT_KEY by default
switch (buf[3] & 3) {
case 0:
if (buf[3] == 0x60)
event->key = TB_KEY_MOUSE_WHEEL_UP;
else
event->key = TB_KEY_MOUSE_LEFT;
break;
case 1:
if (buf[3] == 0x61)
event->key = TB_KEY_MOUSE_WHEEL_DOWN;
else
event->key = TB_KEY_MOUSE_MIDDLE;
break;
case 2:
event->key = TB_KEY_MOUSE_RIGHT;
break;
case 3:
event->key = TB_KEY_MOUSE_RELEASE;
break;
default:
return -6;
}
event->type = TB_EVENT_MOUSE; // TB_EVENT_KEY by default
// the coord is 1,1 for upper left
event->x = buf[4] - 1 - 32;
event->y = buf[5] - 1 - 32;
// the coord is 1,1 for upper left
event->x = buf[4] - 1 - 32;
event->y = buf[5] - 1 - 32;
return 6;
}
return 6;
}
// it's pretty simple here, find 'starts_with' match and return
// success, else return failure
int i;
for (i = 0; keys[i]; i++) {
if (starts_with(buf, len, keys[i])) {
event->ch = 0;
event->key = 0xFFFF-i;
return strlen(keys[i]);
}
}
return 0;
// it's pretty simple here, find 'starts_with' match and return
// success, else return failure
int i;
for (i = 0; keys[i]; i++) {
if (starts_with(buf, len, keys[i])) {
event->ch = 0;
event->key = 0xFFFF-i;
return strlen(keys[i]);
}
}
return 0;
}
static bool extract_event(struct tb_event *event, struct bytebuffer *inbuf, int inputmode)
{
const char *buf = inbuf->buf;
const int len = inbuf->len;
if (len == 0)
return false;
const char *buf = inbuf->buf;
const int len = inbuf->len;
if (len == 0)
return false;
if (buf[0] == '\033') {
int n = parse_escape_seq(event, buf, len);
if (n != 0) {
bool success = true;
if (n < 0) {
success = false;
n = -n;
}
bytebuffer_truncate(inbuf, n);
return success;
} else {
// it's not escape sequence, then it's ALT or ESC,
// check inputmode
if (inputmode&TB_INPUT_ESC) {
// if we're in escape mode, fill ESC event, pop
// buffer, return success
event->ch = 0;
event->key = TB_KEY_ESC;
event->mod = 0;
bytebuffer_truncate(inbuf, 1);
return true;
}
if (inputmode&TB_INPUT_ALT) {
// if we're in alt mode, set ALT modifier to
// event and redo parsing
event->mod = TB_MOD_ALT;
bytebuffer_truncate(inbuf, 1);
return extract_event(event, inbuf, inputmode);
}
assert(!"never got here");
}
}
if (buf[0] == '\033') {
int n = parse_escape_seq(event, buf, len);
if (n != 0) {
bool success = true;
if (n < 0) {
success = false;
n = -n;
}
bytebuffer_truncate(inbuf, n);
return success;
} else {
// it's not escape sequence, then it's ALT or ESC,
// check inputmode
if (inputmode&TB_INPUT_ESC) {
// if we're in escape mode, fill ESC event, pop
// buffer, return success
event->ch = 0;
event->key = TB_KEY_ESC;
event->mod = 0;
bytebuffer_truncate(inbuf, 1);
return true;
}
if (inputmode&TB_INPUT_ALT) {
// if we're in alt mode, set ALT modifier to
// event and redo parsing
event->mod = TB_MOD_ALT;
bytebuffer_truncate(inbuf, 1);
return extract_event(event, inbuf, inputmode);
}
assert(!"never got here");
}
}
// if we're here, this is not an escape sequence and not an alt sequence
// so, it's a FUNCTIONAL KEY or a UNICODE character
// if we're here, this is not an escape sequence and not an alt sequence
// so, it's a FUNCTIONAL KEY or a UNICODE character
// first of all check if it's a functional key
if ((unsigned char)buf[0] <= TB_KEY_SPACE ||
(unsigned char)buf[0] == TB_KEY_BACKSPACE2)
{
// fill event, pop buffer, return success */
event->ch = 0;
event->key = (uint16_t)buf[0];
bytebuffer_truncate(inbuf, 1);
return true;
}
// first of all check if it's a functional key
if ((unsigned char)buf[0] <= TB_KEY_SPACE ||
(unsigned char)buf[0] == TB_KEY_BACKSPACE2)
{
// fill event, pop buffer, return success */
event->ch = 0;
event->key = (uint16_t)buf[0];
bytebuffer_truncate(inbuf, 1);
return true;
}
// feh... we got utf8 here
// feh... we got utf8 here
// check if there is all bytes
if (len >= tb_utf8_char_length(buf[0])) {
/* everything ok, fill event, pop buffer, return success */
tb_utf8_char_to_unicode(&event->ch, buf);
event->key = 0;
bytebuffer_truncate(inbuf, tb_utf8_char_length(buf[0]));
return true;
}
// check if there is all bytes
if (len >= tb_utf8_char_length(buf[0])) {
/* everything ok, fill event, pop buffer, return success */
tb_utf8_char_to_unicode(&event->ch, buf);
event->key = 0;
bytebuffer_truncate(inbuf, tb_utf8_char_length(buf[0]));
return true;
}
// event isn't recognized, perhaps there is not enough bytes in utf8
// sequence
return false;
// event isn't recognized, perhaps there is not enough bytes in utf8
// sequence
return false;
}
Oops, something went wrong.

0 comments on commit 9a31c34

Please sign in to comment.