Implement r_cons_readchar_timeout. wink wink

This commit is contained in:
pancake 2017-12-18 02:44:51 +01:00
parent 435c7524ef
commit 0851129765
4 changed files with 99 additions and 18 deletions

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2016 - pancake */
/* radare - LGPL - Copyright 2009-2017 - pancake */
#include <r_cons.h>
#include <string.h>
@ -33,7 +33,6 @@ static int is_fd_ready(int fd) {
R_API int r_cons_controlz(int ch) {
#if __UNIX__
if (ch == 0x1a) {
r_cons_set_raw (0);
r_cons_show_cursor (1);
r_cons_enable_mouse (0);
r_sys_stop ();
@ -305,19 +304,13 @@ do_it_again:
ch=irInBuf[i].Event.KeyEvent.uChar.AsciiChar;
bCtrl=irInBuf[i].Event.KeyEvent.dwControlKeyState & 8;
if (irInBuf[i].Event.KeyEvent.uChar.AsciiChar==0) {
ch=0;
ch = 0;
switch (irInBuf[i].Event.KeyEvent.wVirtualKeyCode) {
case VK_DOWN: // key down
if (bCtrl)
ch='J';
else
ch='j';
ch = bCtrl ? 'J': 'j';
break;
case VK_RIGHT: // key right
if (bCtrl)
ch='L';
else
ch='l';
ch = bCtrl ? 'L': 'l';
break;
case VK_UP: // key up
if (bCtrl)
@ -426,8 +419,9 @@ do_it_again:
}
FlushConsoleInputBuffer(h);
SetConsoleMode (h, mode);
if (ch==0)
if (ch == 0) {
goto do_it_again;
}
/*r_cons_gotoxy (1, 2);
r_cons_printf ("\n");
r_cons_printf ("| buf = %x |\n", ch);
@ -437,6 +431,28 @@ do_it_again:
return ch;
}
#endif
R_API int r_cons_readchar_timeout(ut32 usec) {
#if __UNIX__
struct timeval tv;
fd_set fdset, errset;
FD_ZERO (&fdset);
FD_SET (0, &fdset);
tv.tv_sec = 0; // usec / 1000;
tv.tv_usec = 1000 * usec;
r_cons_set_raw (1);
if (select (1, &fdset, NULL, &errset, &tv) == 1) {
return r_cons_readchar ();
}
r_cons_set_raw (0);
// timeout
return -1;
#else
// TODO: unimplemented readchar_timeout for windows
return r_cons_readchar ();
#endif
}
R_API int r_cons_readchar() {
char buf[2];
buf[0] = -1;
@ -451,15 +467,17 @@ R_API int r_cons_readchar() {
GetConsoleMode (h, &mode);
SetConsoleMode (h, 0); // RAW
ret = ReadConsole (h, buf, 1, &out, NULL);
FlushConsoleInputBuffer(h);
if (!ret)
FlushConsoleInputBuffer (h);
if (!ret) {
return -1;
}
SetConsoleMode (h, mode);
#else
r_cons_set_raw (1);
if (read (0, buf, 1)==-1)
if (read (0, buf, 1) == -1) {
return -1;
//r_cons_set_raw (0);
}
r_cons_set_raw (0);
#endif
return r_cons_controlz (buf[0]);
}

View File

@ -8,6 +8,14 @@ static int autoblocksize = 1;
static int disMode = 0;
static void visual_refresh(RCore *core);
static bool snowMode = false;
static RList *snows = NULL;
typedef struct {
int x;
int y;
} Snow;
#define KEY_ALTQ 0xc5
static const char *printfmtSingle[] = {
@ -79,6 +87,41 @@ static void rotateAsmBits(RCore *core) {
}
}
static void printSnow(RCore *core) {
if (!snows) {
snows = r_list_newf (free);
}
int i, h, w = r_cons_get_size (&h);
int amount = rand () % 4;
if (amount > 0) {
for (i = 0; i < amount; i++) {
Snow *snow = R_NEW (Snow);
snow->x = rand () % w;
snow->y = 0;
r_list_append (snows, snow);
}
}
RListIter *iter, *iter2;
Snow *snow;
r_list_foreach_safe (snows, iter, iter2, snow) {
int pos = (rand () % 3) - 1;
snow->x += pos;
snow->y++;
if (snow->x >= w) {
r_list_delete (snows, iter);
continue;
}
if (snow->y > h) {
r_list_delete (snows, iter);
continue;
}
r_cons_gotoxy (snow->x, snow->y);
r_cons_printf ("*");
}
// r_cons_gotoxy (10 , 10);
r_cons_flush ();
}
static void visual_repeat(RCore *core) {
int atport = r_config_get_i (core->config, "scr.atport");
if (atport) {
@ -2305,6 +2348,13 @@ R_API int r_core_visual_cmd(RCore *core, const char *arg) {
}
}
break;
case '(':
snowMode = !snowMode;
if (!snowMode) {
r_list_free (snows);
snows = NULL;
}
break;
case '*':
if (core->print->cur_enabled) {
r_core_cmdf (core, "dr PC=0x%08"PFMT64x, core->offset + core->print->cur);
@ -2839,6 +2889,10 @@ static void visual_refresh(RCore *core) {
core->cons->blankline = true;
core->curtab = 0; // which command are we focusing
//core->seltab = 0; // user selected tab
if (snowMode) {
printSnow (core);
}
}
R_API int r_core_visual(RCore *core, const char *input) {
@ -2959,7 +3013,15 @@ dodo:
goto dodo;
}
if (!skip) {
ch = r_cons_readchar ();
if (snowMode) {
ch = r_cons_readchar_timeout (300);
if (ch == -1) {
skip = 1;
continue;
}
} else {
ch = r_cons_readchar ();
}
if (r_cons_is_breaked()) {
break;
}

View File

@ -590,6 +590,7 @@ R_API void r_cons_cmd_help(const char * help[], bool use_color);
//R_API int r_cons_fgets(char *buf, int len, int argc, const char **argv);
R_API int r_cons_controlz(int ch);
R_API int r_cons_readchar(void);
R_API int r_cons_readchar_timeout(ut32 usec);
R_API int r_cons_any_key(const char *msg);
R_API int r_cons_eof(void);

View File

@ -299,7 +299,7 @@ R_API bool r_socket_connect (RSocket *s, const char *host, const char *port, int
FD_SET (s->fd, &Write);
FD_SET (s->fd, &Err);
select (0, NULL, &Write, &Err, &Timeout);
if(FD_ISSET (s->fd, &Write)) {
if (FD_ISSET (s->fd, &Write)) {
return true;
}
return false;