2014-04-28 23:55:10 +00:00
|
|
|
/* radare - LGPL - Copyright 2013-2014 - pancake */
|
2013-08-25 22:51:36 +00:00
|
|
|
|
|
|
|
#include <r_cons.h>
|
|
|
|
|
2014-04-29 01:53:48 +00:00
|
|
|
#define W(y) r_cons_canvas_write(c,y)
|
|
|
|
#define G(x,y) r_cons_canvas_gotoxy(c,x,y)
|
|
|
|
|
2013-08-25 22:51:36 +00:00
|
|
|
R_API void r_cons_canvas_free (RConsCanvas *c) {
|
|
|
|
free (c->b);
|
|
|
|
free (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_cons_canvas_clear (RConsCanvas *c) {
|
|
|
|
int y;
|
|
|
|
memset (c->b, '\n', c->blen);
|
|
|
|
c->b[c->blen] = 0;
|
2014-05-05 21:28:57 +00:00
|
|
|
for (y = 0; y<c->h; y++)
|
2013-08-25 22:51:36 +00:00
|
|
|
c->b[ y * c->w ] = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API RConsCanvas* r_cons_canvas_new (int w, int h) {
|
|
|
|
RConsCanvas *c;
|
|
|
|
if (w<1||h<1)
|
|
|
|
return NULL;
|
|
|
|
c = R_NEW0 (RConsCanvas);
|
|
|
|
if (!c) return NULL;
|
2014-05-05 01:15:28 +00:00
|
|
|
c->sx = 0;
|
|
|
|
c->sy = 0;
|
2013-08-25 22:51:36 +00:00
|
|
|
c->blen = (w+1)*h;
|
|
|
|
c->b = malloc (c->blen+1);
|
|
|
|
if (!c->b) {
|
|
|
|
free (c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
c->w = w;
|
|
|
|
c->h = h;
|
|
|
|
c->x = c->y = 0;
|
|
|
|
r_cons_canvas_clear (c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2014-05-05 01:15:28 +00:00
|
|
|
R_API int r_cons_canvas_gotoxy(RConsCanvas *c, int x, int y) {
|
|
|
|
int ret = R_TRUE;
|
|
|
|
x += c->sx;
|
|
|
|
y += c->sy;
|
2014-07-31 22:54:04 +00:00
|
|
|
if (x >= c->w) {
|
2014-05-05 01:15:28 +00:00
|
|
|
c->x = c->w;
|
|
|
|
ret = R_FALSE;
|
|
|
|
}
|
2014-07-31 22:54:04 +00:00
|
|
|
if (y >= c->h) {
|
2014-05-05 01:15:28 +00:00
|
|
|
c->y = c->h;
|
|
|
|
ret = R_FALSE;
|
|
|
|
}
|
2014-07-31 22:54:04 +00:00
|
|
|
if (x <0) {
|
2014-05-05 01:15:28 +00:00
|
|
|
c->x = 0;
|
|
|
|
ret = R_FALSE;
|
|
|
|
}
|
2014-07-31 22:54:04 +00:00
|
|
|
if (y <0) {
|
2014-05-08 00:26:42 +00:00
|
|
|
c->y = 0;
|
2014-05-05 01:15:28 +00:00
|
|
|
ret = R_FALSE;
|
|
|
|
}
|
|
|
|
if (x<c->w && x>=0) c->x = x;
|
|
|
|
if (y<c->h && y>=0) c->y = y;
|
|
|
|
return ret;
|
2013-08-25 22:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static char *getptr(RConsCanvas *c, int *left) {
|
|
|
|
if (left) *left = c->w - c->x;
|
|
|
|
return c->b + (c->y * c->w) + c->x;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char *getrow (char *p, char **n) {
|
|
|
|
char *q = strchr (p, '\n');
|
|
|
|
if (n) *n = NULL;
|
|
|
|
if (q) {
|
|
|
|
*q = 0;
|
|
|
|
if (n) *n = q+1;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *prefixline(RConsCanvas *c, int *left) {
|
|
|
|
int x;
|
|
|
|
char *p = c->b + (c->y * c->w);
|
|
|
|
for (x = 0; x<c->x; x++) {
|
|
|
|
if (p[x] == '\n')
|
|
|
|
p[x] = ' ';
|
|
|
|
}
|
|
|
|
if (left) *left = c->w - c->x;
|
|
|
|
return p+x;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_cons_canvas_write(RConsCanvas *c, const char *_s) {
|
2014-05-16 12:34:36 +00:00
|
|
|
int left, slen, i;
|
2013-08-25 22:51:36 +00:00
|
|
|
char *p, *s, *str;
|
2014-05-16 12:34:36 +00:00
|
|
|
char *line, *n;
|
|
|
|
|
2013-08-25 22:51:36 +00:00
|
|
|
str = s = strdup (_s);
|
2014-05-16 12:34:36 +00:00
|
|
|
for (i=0;;i++) {
|
2013-08-25 22:51:36 +00:00
|
|
|
line = getrow (s, &n);
|
|
|
|
p = prefixline (c, &left);
|
|
|
|
slen = R_MIN (left, strlen (line));
|
2014-05-09 01:06:05 +00:00
|
|
|
if (!G (c->x-c->sx+slen, c->y-c->sy)) {
|
|
|
|
// TODO : chop slen
|
|
|
|
slen = (c->w - (c->x-c->sx));
|
2014-05-16 12:34:36 +00:00
|
|
|
continue;
|
2014-05-09 01:06:05 +00:00
|
|
|
}
|
|
|
|
if (!G (c->x-c->sx-slen, c->y-c->sy))
|
2014-05-16 12:34:36 +00:00
|
|
|
continue;
|
2014-05-03 01:35:42 +00:00
|
|
|
memcpy (p, line, slen);
|
2013-08-25 22:51:36 +00:00
|
|
|
if (!n) break;
|
|
|
|
s = n;
|
2014-05-05 01:15:28 +00:00
|
|
|
if (!G (c->x-c->sx, c->y+1-c->sy))
|
|
|
|
break;
|
2013-08-25 22:51:36 +00:00
|
|
|
}
|
|
|
|
free (str);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API char *r_cons_canvas_to_string(RConsCanvas *c) {
|
|
|
|
int x, y, olen = 0;
|
|
|
|
char *o = malloc (c->blen+(c->h+1));
|
|
|
|
char *b = c->b;
|
|
|
|
for (y = 0; y<c->h; y++) {
|
|
|
|
for (x = 0; x<c->w; x++) {
|
|
|
|
int p = x + (y*c->w);
|
|
|
|
if (!b[p] || b[p]=='\n')
|
|
|
|
break;
|
|
|
|
o[olen++] = b[p];
|
|
|
|
}
|
|
|
|
o[olen++] = '\n';
|
|
|
|
}
|
|
|
|
o[olen] = 0;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_cons_canvas_print(RConsCanvas *c) {
|
|
|
|
char *o = r_cons_canvas_to_string (c);
|
|
|
|
r_cons_strcat (o);
|
|
|
|
free (o);
|
|
|
|
}
|
2014-04-28 23:55:10 +00:00
|
|
|
|
2014-05-05 01:15:28 +00:00
|
|
|
R_API int r_cons_canvas_resize(RConsCanvas *c, int w, int h) {
|
2014-05-05 21:28:57 +00:00
|
|
|
char *b = NULL;
|
|
|
|
if (w < 0) return R_FALSE;
|
|
|
|
|
|
|
|
b = realloc (c->b, (w+1)*h);
|
2014-05-05 01:15:28 +00:00
|
|
|
if (!b) return R_FALSE;
|
|
|
|
c->b = b;
|
|
|
|
c->w = w;
|
|
|
|
c->h = h;
|
|
|
|
c->x = 0;
|
|
|
|
c->y = 0;
|
|
|
|
r_cons_canvas_clear (c);
|
|
|
|
return R_TRUE;
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_cons_canvas_box(RConsCanvas *c, int x, int y, int w, int h) {
|
|
|
|
int i;
|
2014-04-29 01:53:48 +00:00
|
|
|
int roundcorners = 0;
|
2014-05-05 21:28:57 +00:00
|
|
|
char *row = NULL;
|
2014-04-29 01:53:48 +00:00
|
|
|
char corner = '=';
|
|
|
|
|
2014-05-05 21:28:57 +00:00
|
|
|
if (w < 0) return;
|
|
|
|
|
|
|
|
row = malloc (w+1);
|
2014-04-29 01:53:48 +00:00
|
|
|
row[0] = roundcorners?'.':corner;
|
2014-04-28 23:55:10 +00:00
|
|
|
memset (row+1, '-', w-2);
|
2014-04-29 01:53:48 +00:00
|
|
|
row[w-1] = roundcorners?'.':corner;
|
2014-04-28 23:55:10 +00:00
|
|
|
row[w] = 0;
|
2014-05-05 01:15:28 +00:00
|
|
|
if (G(x, y)) W(row);
|
|
|
|
if (G(x, y+h-1)) {
|
|
|
|
row[0] = roundcorners?'\'':corner;
|
|
|
|
row[w-1] = roundcorners?'\'':corner;
|
|
|
|
W(row);
|
|
|
|
}
|
2014-04-28 23:55:10 +00:00
|
|
|
|
|
|
|
for (i=1;i<h-1;i++) {
|
2014-05-05 01:15:28 +00:00
|
|
|
if (G(x, y+i)) W("|");
|
|
|
|
if (G(x+w-1, y+i)) W("|");
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
2014-05-05 21:28:57 +00:00
|
|
|
free (row);
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_cons_canvas_fill(RConsCanvas *c, int x, int y, int w, int h, char ch, int replace) {
|
|
|
|
int i;
|
2014-05-05 21:28:57 +00:00
|
|
|
char *row = NULL;
|
|
|
|
|
|
|
|
if (w < 0) return;
|
|
|
|
|
|
|
|
row = malloc (w+1);
|
2014-04-29 01:53:48 +00:00
|
|
|
memset (row, ch, w);
|
2014-04-28 23:55:10 +00:00
|
|
|
row[w] = 0;
|
|
|
|
|
2014-04-29 01:53:48 +00:00
|
|
|
for (i=0;i<h;i++) {
|
2014-05-05 01:15:28 +00:00
|
|
|
if (G(x, y+i))
|
|
|
|
W(row);
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
2014-05-05 21:28:57 +00:00
|
|
|
free (row);
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_cons_canvas_line (RConsCanvas *c, int x, int y, int x2, int y2, int style) {
|
2014-05-08 00:26:42 +00:00
|
|
|
int i, onscreen;
|
2014-04-28 23:55:10 +00:00
|
|
|
switch (style) {
|
|
|
|
// vertical arrow line
|
|
|
|
case 0: //
|
2014-05-05 01:15:28 +00:00
|
|
|
if (G (x, y))
|
|
|
|
W ("v");
|
|
|
|
if (G (x2, y2))
|
|
|
|
W ("V");
|
2014-04-28 23:55:10 +00:00
|
|
|
if (x==x2) {
|
|
|
|
int min = R_MIN (y,y2)+1;
|
|
|
|
int max = R_MAX (y,y2);
|
2014-05-03 01:35:42 +00:00
|
|
|
for (i=min; i<max; i++) {
|
2014-05-05 01:15:28 +00:00
|
|
|
if (G (x,i))
|
|
|
|
W ("|");
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// --
|
|
|
|
// TODO: find if there's any collision in this line
|
2014-05-05 01:15:28 +00:00
|
|
|
int hl = R_ABS (y-y2) / 2;
|
|
|
|
int hl2 = R_ABS (y-y2)-hl;
|
2014-04-28 23:55:10 +00:00
|
|
|
hl--;
|
|
|
|
if (y2 > (y+1)) {
|
|
|
|
for (i=0;i<hl;i++) {
|
2014-05-05 01:15:28 +00:00
|
|
|
if (G (x,y+i+1))
|
|
|
|
W ("|");
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
|
|
|
for (i=0;i<hl2;i++) {
|
2014-05-05 01:15:28 +00:00
|
|
|
if (G (x2, y+hl+i+1))
|
|
|
|
W ("|");
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
|
|
|
int w = R_ABS (x-x2);
|
|
|
|
char *row = malloc (w+2);
|
|
|
|
if (x>x2) {
|
|
|
|
w++;
|
|
|
|
row[0] = '.';
|
|
|
|
if (w>2)
|
|
|
|
memset (row+1, '-', w-2);
|
|
|
|
row[w-1] = '\'';
|
|
|
|
row[w] = 0;
|
2014-05-08 00:58:20 +00:00
|
|
|
onscreen = G (x2+w,y+hl+1);
|
|
|
|
i = G (x2, y+hl+1);
|
|
|
|
if (!onscreen)
|
|
|
|
onscreen = i;
|
2014-04-28 23:55:10 +00:00
|
|
|
} else {
|
|
|
|
row[0] = '`';
|
|
|
|
if (w>1)
|
|
|
|
memset (row+1, '-', w-1);
|
|
|
|
row[w] = '.';
|
|
|
|
row[w+1] = 0;
|
2014-05-08 00:58:20 +00:00
|
|
|
onscreen = G (x+w,y+1+hl);
|
|
|
|
i = G (x,y+1+hl);
|
|
|
|
if (!onscreen)
|
|
|
|
onscreen = i;
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
2014-05-08 00:26:42 +00:00
|
|
|
if (onscreen)
|
|
|
|
W (row);
|
2014-05-10 21:06:15 +00:00
|
|
|
free (row);
|
2014-04-28 23:55:10 +00:00
|
|
|
} else {
|
|
|
|
int minx = R_MIN (x, x2);
|
|
|
|
//if (y >= y2) {
|
|
|
|
int rl = R_ABS (x-x2)/2;
|
|
|
|
int rl2 = R_ABS (x-x2)-rl+1;
|
|
|
|
int vl = (R_ABS(y-y2))+1;
|
2014-04-29 01:53:48 +00:00
|
|
|
if (y+1==y2)
|
2014-04-28 23:55:10 +00:00
|
|
|
vl--;
|
|
|
|
|
|
|
|
for (i=0;i<vl; i++) {
|
2014-05-08 00:26:42 +00:00
|
|
|
if (G (minx+rl,y2+i))
|
|
|
|
W ("|");
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int w = rl;
|
|
|
|
char *row = malloc (w+1);
|
|
|
|
if (x>x2) {
|
|
|
|
row[0] = '.';
|
|
|
|
if (w>2)
|
|
|
|
memset (row+1, '-', w-2);
|
2014-05-09 01:06:05 +00:00
|
|
|
if (w>0)
|
|
|
|
row[w-1] = '.';
|
2014-04-28 23:55:10 +00:00
|
|
|
row[w] = 0;
|
2014-05-08 00:26:42 +00:00
|
|
|
onscreen = G (x2,y2-1);
|
2014-04-28 23:55:10 +00:00
|
|
|
} else {
|
|
|
|
row[0] = '`';
|
|
|
|
if (w>2)
|
|
|
|
memset (row+1, '-', w-2);
|
2014-05-09 01:06:05 +00:00
|
|
|
if (w>0)
|
|
|
|
row[w-1] = '\'';
|
2014-04-28 23:55:10 +00:00
|
|
|
row[w] = 0;
|
2014-05-08 00:26:42 +00:00
|
|
|
onscreen = G (x+1,y+1);
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
2014-05-08 00:26:42 +00:00
|
|
|
if (onscreen)
|
|
|
|
W (row);
|
2014-04-28 23:55:10 +00:00
|
|
|
w = rl2;
|
2014-05-08 00:58:20 +00:00
|
|
|
free (row);
|
|
|
|
row = malloc (rl2+1);
|
2014-04-28 23:55:10 +00:00
|
|
|
if (x>x2) {
|
|
|
|
row[0] = '`';
|
|
|
|
memset (row+1, '-', w-2);
|
|
|
|
row[w-1] = '\'';
|
|
|
|
row[w] = 0;
|
2014-05-08 00:58:20 +00:00
|
|
|
onscreen = G (x2+rl, y+1);
|
2014-04-28 23:55:10 +00:00
|
|
|
} else {
|
|
|
|
row[0] = '.';
|
|
|
|
memset (row+1, '-', w-2);
|
|
|
|
row[w-1] = '.';
|
|
|
|
row[w] = 0;
|
2014-05-08 00:58:20 +00:00
|
|
|
onscreen = G (x+rl, y2-1);
|
2014-04-28 23:55:10 +00:00
|
|
|
}
|
2014-05-08 00:58:20 +00:00
|
|
|
if (onscreen)
|
|
|
|
W (row);
|
2014-04-28 23:55:10 +00:00
|
|
|
free (row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|