2019-06-09 16:50:36 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2019 - pancake */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
#include <r_cons.h>
|
|
|
|
#include <string.h>
|
2015-04-19 23:05:22 +00:00
|
|
|
#if __UNIX__
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2015-01-14 22:31:29 +00:00
|
|
|
|
2018-05-02 13:36:08 +00:00
|
|
|
#define I r_cons_singleton ()
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2019-06-09 16:50:36 +00:00
|
|
|
// TODO: Support binary, use RBuffer and remove globals
|
|
|
|
static char *readbuffer = NULL;
|
|
|
|
static int readbuffer_length = 0;
|
|
|
|
static bool bufactive = true;
|
|
|
|
|
2015-01-26 16:55:12 +00:00
|
|
|
#if 0
|
2012-02-01 01:22:43 +00:00
|
|
|
//__UNIX__
|
|
|
|
#include <poll.h>
|
2019-06-09 16:50:36 +00:00
|
|
|
static int __is_fd_ready(int fd) {
|
2012-02-01 01:22:43 +00:00
|
|
|
fd_set rfds;
|
|
|
|
struct timeval tv;
|
|
|
|
if (fd==-1)
|
|
|
|
return 0;
|
|
|
|
FD_ZERO (&rfds);
|
|
|
|
FD_SET (fd, &rfds);
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 1;
|
|
|
|
if (select (1, &rfds, NULL, NULL, &tv) == -1)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
return !FD_ISSET (0, &rfds);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-19 21:25:17 +00:00
|
|
|
R_API int r_cons_controlz(int ch) {
|
|
|
|
#if __UNIX__
|
|
|
|
if (ch == 0x1a) {
|
2018-01-02 18:31:02 +00:00
|
|
|
r_cons_show_cursor (true);
|
|
|
|
r_cons_enable_mouse (false);
|
2014-11-19 21:25:17 +00:00
|
|
|
r_sys_stop ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2019-07-01 17:07:11 +00:00
|
|
|
// 96 - wheel up
|
|
|
|
// 97 - wheel down
|
|
|
|
// 95 - mouse up
|
|
|
|
// 92 - mouse down
|
2019-06-09 16:50:36 +00:00
|
|
|
static int __parseMouseEvent() {
|
2019-07-01 17:07:11 +00:00
|
|
|
char xpos[32];
|
|
|
|
char ypos[32];
|
2016-05-09 09:13:37 +00:00
|
|
|
int ch = r_cons_readchar ();
|
2019-07-01 17:07:11 +00:00
|
|
|
int ch2 = r_cons_readchar ();
|
|
|
|
|
|
|
|
// [32M - mousedown
|
|
|
|
// [35M - mouseup
|
|
|
|
if (ch2 == ';') {
|
|
|
|
int i;
|
|
|
|
// read until next ;
|
|
|
|
for (i = 0; i < sizeof (xpos); i++) {
|
|
|
|
char ch = r_cons_readchar ();
|
|
|
|
if (ch == ';' || ch == 'M') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xpos[i] = ch;
|
|
|
|
}
|
|
|
|
xpos[i] = 0;
|
|
|
|
for (i = 0; i < sizeof (ypos); i++) {
|
|
|
|
char ch = r_cons_readchar ();
|
|
|
|
if (ch == ';' || ch == 'M') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ypos[i] = ch;
|
|
|
|
}
|
|
|
|
ypos[i] = 0;
|
2019-07-02 01:33:57 +00:00
|
|
|
r_cons_set_click (atoi (xpos), atoi (ypos));
|
2019-07-01 17:07:11 +00:00
|
|
|
ch = r_cons_readchar ();
|
|
|
|
// ignored
|
|
|
|
int ch = r_cons_readchar ();
|
|
|
|
if (ch == 27) {
|
|
|
|
ch = r_cons_readchar (); // '['
|
2019-07-02 01:33:57 +00:00
|
|
|
}
|
|
|
|
if (ch == '[') {
|
|
|
|
do {
|
|
|
|
ch = r_cons_readchar (); // '3'
|
|
|
|
} while (ch != 'M');
|
2016-05-09 09:13:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-21 19:21:36 +00:00
|
|
|
R_API int r_cons_arrow_to_hjkl(int ch) {
|
2019-03-15 04:55:08 +00:00
|
|
|
#if __WINDOWS__
|
2019-07-05 07:49:46 +00:00
|
|
|
return ch < 2 ? 0 : ch;
|
2015-04-07 13:23:03 +00:00
|
|
|
#endif
|
2015-02-10 23:13:04 +00:00
|
|
|
I->mouse_event = 0;
|
2014-11-20 00:39:52 +00:00
|
|
|
/* emacs */
|
|
|
|
switch ((ut8)ch) {
|
2018-10-19 11:42:38 +00:00
|
|
|
case 0xc3: r_cons_readchar (); ch='K'; break; // emacs repag (alt + v)
|
2014-11-20 00:39:52 +00:00
|
|
|
case 0x16: ch='J'; break; // emacs avpag (ctrl + v)
|
|
|
|
case 0x10: ch='k'; break; // emacs up (ctrl + p)
|
|
|
|
case 0x0e: ch='j'; break; // emacs down (ctrl + n)
|
|
|
|
case 0x06: ch='l'; break; // emacs right (ctrl + f)
|
|
|
|
case 0x02: ch='h'; break; // emacs left (ctrl + b)
|
|
|
|
}
|
2017-08-27 17:31:17 +00:00
|
|
|
if (ch != 0x1b) {
|
2014-10-06 00:36:22 +00:00
|
|
|
return ch;
|
2017-08-27 17:31:17 +00:00
|
|
|
}
|
2014-10-06 00:36:22 +00:00
|
|
|
ch = r_cons_readchar ();
|
2017-08-27 17:31:17 +00:00
|
|
|
if (!ch) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-06 00:36:22 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 0x1b:
|
|
|
|
ch = 'q'; // XXX: must be 0x1b (R_CONS_KEY_ESC)
|
|
|
|
break;
|
|
|
|
case 0x4f: // function keys from f1 to f4
|
|
|
|
ch = r_cons_readchar ();
|
2018-11-15 18:06:57 +00:00
|
|
|
#if defined(__HAIKU__)
|
2019-06-20 04:45:00 +00:00
|
|
|
/* Haiku't don use the '[' char for function keys */
|
|
|
|
if (ch > 'O') {/* only in f1..f12 function keys */
|
2015-04-25 02:06:14 +00:00
|
|
|
ch = 0xf1 + (ch&0xf);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-09 09:13:37 +00:00
|
|
|
case '[': // 0x5b function keys (2)
|
2015-04-25 02:06:14 +00:00
|
|
|
/* Haiku need ESC + [ for PageUp and PageDown */
|
2016-05-09 09:13:37 +00:00
|
|
|
if (ch < 'A' || ch == '[') {
|
2015-04-25 02:06:14 +00:00
|
|
|
ch = r_cons_readchar ();
|
2016-05-09 09:13:37 +00:00
|
|
|
}
|
2015-04-25 02:06:14 +00:00
|
|
|
#else
|
2016-05-09 09:13:37 +00:00
|
|
|
ch = 0xf1 + (ch & 0xf);
|
2014-10-06 00:36:22 +00:00
|
|
|
break;
|
|
|
|
case '[': // function keys (2)
|
2010-02-28 14:16:02 +00:00
|
|
|
ch = r_cons_readchar ();
|
2015-04-25 02:06:14 +00:00
|
|
|
#endif
|
2011-06-05 18:36:22 +00:00
|
|
|
switch (ch) {
|
2019-06-21 08:11:51 +00:00
|
|
|
case '<':
|
|
|
|
{
|
|
|
|
char pos[8] = {0};
|
|
|
|
int p = 0;
|
|
|
|
int x = 0;
|
|
|
|
int y = 0;
|
|
|
|
int sc = 0;
|
2019-07-17 16:36:39 +00:00
|
|
|
|
|
|
|
char vel[8] = {0};
|
|
|
|
int vn = 0;
|
2019-06-21 08:11:51 +00:00
|
|
|
do {
|
|
|
|
ch = r_cons_readchar ();
|
2019-07-17 19:01:02 +00:00
|
|
|
// just for debugging
|
2019-07-17 16:36:39 +00:00
|
|
|
//eprintf ( "%c", ch);
|
2019-06-21 08:11:51 +00:00
|
|
|
if (sc > 0) {
|
2019-07-17 19:01:02 +00:00
|
|
|
if (ch >= '0' && ch <= '9') {
|
2019-06-21 08:11:51 +00:00
|
|
|
pos[p++] = ch;
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 16:36:39 +00:00
|
|
|
if (sc < 1) {
|
|
|
|
vel[vn++] = ch;
|
|
|
|
}
|
2019-06-21 08:11:51 +00:00
|
|
|
if (ch == ';') {
|
|
|
|
if (sc == 1) {
|
|
|
|
pos[p++] = 0;
|
|
|
|
x = atoi (pos);
|
|
|
|
}
|
|
|
|
sc++;
|
|
|
|
p = 0;
|
|
|
|
}
|
|
|
|
} while (ch != 'M' && ch != 'm');
|
2019-07-17 19:01:02 +00:00
|
|
|
int nvel = atoi (vel);
|
|
|
|
switch (nvel) {
|
|
|
|
case 2: // right click
|
|
|
|
if (ch == 'M') {
|
|
|
|
return INT8_MAX;
|
|
|
|
}
|
|
|
|
return -INT8_MAX;
|
|
|
|
case 64: // wheel up
|
2019-07-17 16:36:39 +00:00
|
|
|
return 'k';
|
2019-07-17 19:01:02 +00:00
|
|
|
case 65: // wheel down
|
2019-07-17 16:36:39 +00:00
|
|
|
return 'j';
|
|
|
|
}
|
2019-06-21 08:11:51 +00:00
|
|
|
pos[p++] = 0;
|
|
|
|
y = atoi (pos);
|
|
|
|
// M is mouse down , m is mouse up
|
|
|
|
if (ch == 'M') {
|
|
|
|
r_cons_set_click (x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2014-10-06 00:36:22 +00:00
|
|
|
case '[':
|
2011-06-05 18:36:22 +00:00
|
|
|
ch = r_cons_readchar ();
|
2014-10-06 00:36:22 +00:00
|
|
|
switch (ch) {
|
|
|
|
case '2': ch = R_CONS_KEY_F11; break;
|
|
|
|
case 'A': ch = R_CONS_KEY_F1; break;
|
|
|
|
case 'B': ch = R_CONS_KEY_F2; break;
|
|
|
|
case 'C': ch = R_CONS_KEY_F3; break;
|
|
|
|
case 'D': ch = R_CONS_KEY_F4; break;
|
|
|
|
}
|
2011-06-05 18:36:22 +00:00
|
|
|
break;
|
2019-07-01 17:07:11 +00:00
|
|
|
case '9':
|
|
|
|
// handle mouse wheel
|
|
|
|
// __parseWheelEvent();
|
|
|
|
ch = r_cons_readchar ();
|
|
|
|
// 6 is up
|
|
|
|
// 7 is down
|
2019-07-03 22:57:35 +00:00
|
|
|
I->mouse_event = 1;
|
2019-07-01 17:07:11 +00:00
|
|
|
if (ch == '6') {
|
|
|
|
ch = 'k';
|
|
|
|
} else if (ch == '7') {
|
|
|
|
ch = 'j';
|
|
|
|
} else {
|
|
|
|
// unhandled case
|
|
|
|
ch = 0;
|
|
|
|
}
|
|
|
|
int ch2;
|
|
|
|
do {
|
|
|
|
ch2 = r_cons_readchar ();
|
|
|
|
} while (ch2 != 'M');
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
// handle mouse down /up events (35 vs 32)
|
|
|
|
__parseMouseEvent();
|
|
|
|
return 0;
|
|
|
|
break;
|
2014-10-06 00:36:22 +00:00
|
|
|
case '2':
|
2010-02-28 13:49:26 +00:00
|
|
|
ch = r_cons_readchar ();
|
2010-02-28 14:16:02 +00:00
|
|
|
switch (ch) {
|
2014-10-06 00:36:22 +00:00
|
|
|
case 0x7e:
|
|
|
|
ch = R_CONS_KEY_F12;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
r_cons_readchar ();
|
2011-06-05 18:36:22 +00:00
|
|
|
switch (ch) {
|
2014-10-06 00:36:22 +00:00
|
|
|
case '0': ch = R_CONS_KEY_F9; break;
|
|
|
|
case '1': ch = R_CONS_KEY_F10; break;
|
|
|
|
case '3': ch = R_CONS_KEY_F11; break;
|
2011-06-05 18:36:22 +00:00
|
|
|
}
|
|
|
|
break;
|
2014-10-06 00:36:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
ch = r_cons_readchar ();
|
|
|
|
switch (ch) {
|
2018-11-02 17:44:43 +00:00
|
|
|
// Support st/st-256color term and others
|
|
|
|
// for shift+arrows
|
|
|
|
case ';': // arrow+mod
|
|
|
|
ch = r_cons_readchar ();
|
|
|
|
switch (ch) {
|
|
|
|
case '2': // arrow+shift
|
|
|
|
ch = r_cons_readchar ();
|
|
|
|
switch (ch) {
|
|
|
|
case 'A': ch = 'K'; break;
|
|
|
|
case 'B': ch = 'J'; break;
|
|
|
|
case 'C': ch = 'L'; break;
|
|
|
|
case 'D': ch = 'H'; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// add other modifiers
|
|
|
|
}
|
|
|
|
break;
|
2014-10-06 00:36:22 +00:00
|
|
|
case ':': // arrow+shift
|
|
|
|
ch = r_cons_readchar ();
|
2011-06-05 18:36:22 +00:00
|
|
|
ch = r_cons_readchar ();
|
|
|
|
switch (ch) {
|
2014-10-06 00:36:22 +00:00
|
|
|
case 'A': ch = 'K'; break;
|
|
|
|
case 'B': ch = 'J'; break;
|
|
|
|
case 'C': ch = 'L'; break;
|
|
|
|
case 'D': ch = 'H'; break;
|
2011-06-05 18:36:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
/*
|
2014-10-06 00:36:22 +00:00
|
|
|
case '1': ch = R_CONS_KEY_F1; break;
|
|
|
|
case '2': ch = R_CONS_KEY_F2; break;
|
|
|
|
case '3': ch = R_CONS_KEY_F3; break;
|
|
|
|
case '4': ch = R_CONS_KEY_F4; break;
|
2011-06-05 18:36:22 +00:00
|
|
|
*/
|
2018-11-15 18:06:57 +00:00
|
|
|
case '5':
|
2014-10-06 00:36:22 +00:00
|
|
|
r_cons_readchar ();
|
|
|
|
ch = 0xf5;
|
2011-06-05 18:36:22 +00:00
|
|
|
break;
|
2018-11-15 18:06:57 +00:00
|
|
|
case '6':
|
2014-10-06 00:36:22 +00:00
|
|
|
r_cons_readchar ();
|
|
|
|
ch = 0xf7;
|
|
|
|
break;
|
2018-11-15 18:06:57 +00:00
|
|
|
case '7':
|
2014-10-06 00:36:22 +00:00
|
|
|
r_cons_readchar ();
|
|
|
|
ch = 0xf6;
|
|
|
|
break;
|
2018-11-15 18:06:57 +00:00
|
|
|
case '8':
|
2014-10-06 00:36:22 +00:00
|
|
|
r_cons_readchar ();
|
|
|
|
ch = 0xf7;
|
|
|
|
break;
|
2018-11-15 18:06:57 +00:00
|
|
|
case '9':
|
2014-10-06 00:36:22 +00:00
|
|
|
r_cons_readchar ();
|
|
|
|
ch = 0xf8;
|
|
|
|
break;
|
|
|
|
} // F9-F12 not yet supported!!
|
2011-06-05 18:36:22 +00:00
|
|
|
break;
|
2018-10-19 11:42:38 +00:00
|
|
|
case '5': ch = 'K'; r_cons_readchar (); break; // repag
|
|
|
|
case '6': ch = 'J'; r_cons_readchar (); break; // avpag
|
2016-05-09 09:13:37 +00:00
|
|
|
/* arrow keys */
|
|
|
|
case 'A': ch = 'k'; break; // up
|
|
|
|
case 'B': ch = 'j'; break; // down
|
|
|
|
case 'C': ch = 'l'; break; // right
|
|
|
|
case 'D': ch = 'h'; break; // left
|
2018-11-02 17:44:43 +00:00
|
|
|
// Support rxvt-unicode term for shift+arrows
|
|
|
|
case 'a': ch = 'K'; break; // shift+up
|
|
|
|
case 'b': ch = 'J'; break; // shift+down
|
|
|
|
case 'c': ch = 'L'; break; // shift+right
|
|
|
|
case 'd': ch = 'H'; break; // shift+left
|
2019-06-09 16:50:36 +00:00
|
|
|
case 'M': ch = __parseMouseEvent (); break;
|
2010-01-30 13:02:53 +00:00
|
|
|
}
|
2014-10-06 00:36:22 +00:00
|
|
|
break;
|
2010-01-30 13:02:53 +00:00
|
|
|
}
|
|
|
|
return ch;
|
|
|
|
}
|
2010-01-09 01:05:04 +00:00
|
|
|
|
2009-04-07 11:28:22 +00:00
|
|
|
// XXX no control for max length here?!?!
|
2010-02-21 19:21:36 +00:00
|
|
|
R_API int r_cons_fgets(char *buf, int len, int argc, const char **argv) {
|
2014-03-01 23:31:35 +00:00
|
|
|
#define RETURN(x) { ret=x; goto beach; }
|
2010-02-21 19:21:36 +00:00
|
|
|
RCons *cons = r_cons_singleton ();
|
2019-01-18 10:58:49 +00:00
|
|
|
int ret = 0, color = cons->context->pal.input && *cons->context->pal.input;
|
2015-04-16 02:06:02 +00:00
|
|
|
if (cons->echo) {
|
2018-01-02 18:31:02 +00:00
|
|
|
r_cons_set_raw (false);
|
|
|
|
r_cons_show_cursor (true);
|
2015-04-16 02:06:02 +00:00
|
|
|
}
|
2014-09-04 09:06:11 +00:00
|
|
|
#if 0
|
2016-07-12 20:15:19 +00:00
|
|
|
int mouse = r_cons_enable_mouse (false);
|
|
|
|
r_cons_enable_mouse (false);
|
2014-03-01 23:31:35 +00:00
|
|
|
r_cons_flush ();
|
2014-09-04 09:06:11 +00:00
|
|
|
#endif
|
2018-09-17 10:07:00 +00:00
|
|
|
errno = 0;
|
2014-03-03 01:01:40 +00:00
|
|
|
if (cons->user_fgets) {
|
2014-03-01 23:31:35 +00:00
|
|
|
RETURN (cons->user_fgets (buf, len));
|
2014-03-03 01:01:40 +00:00
|
|
|
}
|
2017-03-08 22:36:45 +00:00
|
|
|
printf ("%s", cons->line->prompt);
|
|
|
|
fflush (stdout);
|
2010-02-28 14:16:02 +00:00
|
|
|
*buf = '\0';
|
2013-05-22 02:22:49 +00:00
|
|
|
if (color) {
|
2019-01-18 10:58:49 +00:00
|
|
|
const char *p = cons->context->pal.input;
|
2014-03-01 23:31:35 +00:00
|
|
|
int len = p? strlen (p): 0;
|
2018-09-13 08:17:26 +00:00
|
|
|
if (len > 0) {
|
2013-09-29 23:14:04 +00:00
|
|
|
fwrite (p, len, 1, stdout);
|
2018-09-13 08:17:26 +00:00
|
|
|
}
|
2013-05-22 02:22:49 +00:00
|
|
|
fflush (stdout);
|
|
|
|
}
|
2016-09-19 12:44:47 +00:00
|
|
|
if (!fgets (buf, len, cons->fdin)) {
|
2013-05-22 02:22:49 +00:00
|
|
|
if (color) {
|
|
|
|
printf (Color_RESET);
|
|
|
|
fflush (stdout);
|
|
|
|
}
|
2014-03-01 23:31:35 +00:00
|
|
|
RETURN (-1);
|
2013-05-22 02:22:49 +00:00
|
|
|
}
|
|
|
|
if (feof (cons->fdin)) {
|
2016-11-20 18:20:14 +00:00
|
|
|
if (color) {
|
|
|
|
printf (Color_RESET);
|
|
|
|
}
|
2014-03-01 23:31:35 +00:00
|
|
|
RETURN (-2);
|
2013-05-22 02:22:49 +00:00
|
|
|
}
|
2010-02-28 14:16:02 +00:00
|
|
|
buf[strlen (buf)-1] = '\0';
|
2018-09-13 08:17:26 +00:00
|
|
|
if (color) {
|
|
|
|
printf (Color_RESET);
|
|
|
|
}
|
2014-03-01 23:31:35 +00:00
|
|
|
ret = strlen (buf);
|
|
|
|
beach:
|
2014-09-04 09:06:11 +00:00
|
|
|
//r_cons_enable_mouse (mouse);
|
2014-03-01 23:31:35 +00:00
|
|
|
return ret;
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 22:33:28 +00:00
|
|
|
R_API int r_cons_any_key(const char *msg) {
|
|
|
|
if (msg && *msg) {
|
|
|
|
r_cons_printf ("\n-- %s --\n", msg);
|
|
|
|
} else {
|
2016-06-26 04:16:57 +00:00
|
|
|
r_cons_print ("\n--press any key--\n");
|
2015-03-05 22:33:28 +00:00
|
|
|
}
|
2010-01-30 13:02:53 +00:00
|
|
|
r_cons_flush ();
|
2015-03-05 22:33:28 +00:00
|
|
|
return r_cons_readchar ();
|
2010-01-30 13:02:53 +00:00
|
|
|
//r_cons_strcat ("\x1b[2J\x1b[0;0H"); // wtf?
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 12:34:41 +00:00
|
|
|
extern void resizeWin(void);
|
|
|
|
|
2019-03-15 04:55:08 +00:00
|
|
|
#if __WINDOWS__
|
2019-06-09 16:50:36 +00:00
|
|
|
static int __cons_readchar_w32 (ut32 usec) {
|
2018-02-27 23:26:41 +00:00
|
|
|
int ch = 0;
|
2015-04-07 13:23:03 +00:00
|
|
|
BOOL ret;
|
2015-04-16 02:06:02 +00:00
|
|
|
BOOL bCtrl = FALSE;
|
2015-04-07 13:23:03 +00:00
|
|
|
DWORD mode, out;
|
|
|
|
HANDLE h;
|
2019-07-05 07:49:46 +00:00
|
|
|
INPUT_RECORD irInBuf;
|
2019-04-01 12:34:41 +00:00
|
|
|
int i, o;
|
|
|
|
bool resize = false;
|
|
|
|
void *bed;
|
2015-04-07 13:23:03 +00:00
|
|
|
h = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
GetConsoleMode (h, &mode);
|
2019-04-01 12:34:41 +00:00
|
|
|
SetConsoleMode (h, ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT | ENABLE_EXTENDED_FLAGS);
|
2019-07-05 07:49:46 +00:00
|
|
|
do {
|
|
|
|
bed = r_cons_sleep_begin ();
|
|
|
|
if (usec) {
|
|
|
|
if (WaitForSingleObject (h, usec) == WAIT_TIMEOUT) {
|
|
|
|
r_cons_sleep_end (bed);
|
|
|
|
return -1;
|
|
|
|
}
|
2017-12-29 18:55:34 +00:00
|
|
|
}
|
2019-07-05 07:49:46 +00:00
|
|
|
ret = ReadConsoleInput (h, &irInBuf, 1, &out);
|
|
|
|
r_cons_enable_mouse (true);
|
|
|
|
r_cons_sleep_end (bed);
|
|
|
|
if (ret) {
|
|
|
|
if (irInBuf.EventType == MOUSE_EVENT) {
|
|
|
|
if (irInBuf.Event.MouseEvent.dwEventFlags == MOUSE_MOVED) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (irInBuf.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED) {
|
|
|
|
if (irInBuf.Event.MouseEvent.dwButtonState & 0xFF000000) {
|
2019-04-01 12:34:41 +00:00
|
|
|
ch = bCtrl ? 'J' : 'j';
|
|
|
|
} else {
|
|
|
|
ch = bCtrl ? 'K' : 'k';
|
|
|
|
}
|
2019-07-05 07:49:46 +00:00
|
|
|
I->mouse_event = 1;
|
2015-04-07 13:23:03 +00:00
|
|
|
}
|
2019-07-05 07:49:46 +00:00
|
|
|
switch (irInBuf.Event.MouseEvent.dwButtonState) {
|
2019-04-01 12:34:41 +00:00
|
|
|
case FROM_LEFT_1ST_BUTTON_PRESSED:
|
2019-07-05 07:49:46 +00:00
|
|
|
r_cons_set_click (irInBuf.Event.MouseEvent.dwMousePosition.X + 1, irInBuf.Event.MouseEvent.dwMousePosition.Y + 1);
|
|
|
|
ch = 1;
|
|
|
|
break;
|
|
|
|
case RIGHTMOST_BUTTON_PRESSED:
|
2019-04-01 12:34:41 +00:00
|
|
|
r_cons_enable_mouse (false);
|
2019-07-05 07:49:46 +00:00
|
|
|
break;
|
2019-04-01 12:34:41 +00:00
|
|
|
} // TODO: Handle more buttons?
|
2015-04-07 13:23:03 +00:00
|
|
|
}
|
2019-07-05 07:49:46 +00:00
|
|
|
if (irInBuf.EventType == KEY_EVENT) {
|
|
|
|
if (irInBuf.Event.KeyEvent.bKeyDown) {
|
|
|
|
ch = irInBuf.Event.KeyEvent.uChar.AsciiChar;
|
|
|
|
bCtrl = irInBuf.Event.KeyEvent.dwControlKeyState & 8;
|
|
|
|
if (irInBuf.Event.KeyEvent.uChar.AsciiChar == 0) {
|
2017-12-18 01:44:51 +00:00
|
|
|
ch = 0;
|
2019-07-05 07:49:46 +00:00
|
|
|
switch (irInBuf.Event.KeyEvent.wVirtualKeyCode) {
|
2015-08-31 03:06:01 +00:00
|
|
|
case VK_DOWN: // key down
|
2019-04-01 12:34:41 +00:00
|
|
|
ch = bCtrl ? 'J' : 'j';
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_RIGHT: // key right
|
2019-04-01 12:34:41 +00:00
|
|
|
ch = bCtrl ? 'L' : 'l';
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_UP: // key up
|
2019-04-01 12:34:41 +00:00
|
|
|
ch = bCtrl ? 'K' : 'k';
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_LEFT: // key left
|
2019-04-01 12:34:41 +00:00
|
|
|
ch = bCtrl ? 'H' : 'h';
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_PRIOR: // key home
|
2019-04-01 12:34:41 +00:00
|
|
|
ch = 'K';
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_NEXT: // key end
|
2019-04-01 12:34:41 +00:00
|
|
|
ch = 'J';
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F1:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F1;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F2:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F2;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F3:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F3;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F4:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F4;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F5:
|
2019-04-01 12:34:41 +00:00
|
|
|
ch = bCtrl ? 0xcf5 : R_CONS_KEY_F5;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F6:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F6;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F7:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F7;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F8:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F8;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F9:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F9;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F10:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F10;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F11:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F11;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
case VK_F12:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = R_CONS_KEY_F12;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-02-28 00:24:03 +00:00
|
|
|
ch = 0;
|
2015-08-31 03:06:01 +00:00
|
|
|
break;
|
2015-04-07 13:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 22:31:29 +00:00
|
|
|
}
|
2019-07-05 07:49:46 +00:00
|
|
|
if (irInBuf.EventType == WINDOW_BUFFER_SIZE_EVENT) {
|
2019-04-01 12:34:41 +00:00
|
|
|
resize = true;
|
|
|
|
}
|
2019-07-05 07:49:46 +00:00
|
|
|
if (resize) {
|
|
|
|
resizeWin ();
|
|
|
|
resize = false;
|
|
|
|
}
|
2019-04-01 12:34:41 +00:00
|
|
|
}
|
2019-07-05 07:49:46 +00:00
|
|
|
FlushConsoleInputBuffer (h);
|
|
|
|
} while (ch == 0);
|
2019-04-01 12:34:41 +00:00
|
|
|
SetConsoleMode (h, mode);
|
2019-07-05 07:49:46 +00:00
|
|
|
return ch;
|
2015-01-14 22:31:29 +00:00
|
|
|
}
|
|
|
|
#endif
|
2017-12-18 01:44:51 +00:00
|
|
|
|
|
|
|
R_API int r_cons_readchar_timeout(ut32 usec) {
|
|
|
|
#if __UNIX__
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set fdset, errset;
|
|
|
|
FD_ZERO (&fdset);
|
2017-12-20 14:05:33 +00:00
|
|
|
FD_ZERO (&errset);
|
2017-12-18 01:44:51 +00:00
|
|
|
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
|
2019-06-09 16:50:36 +00:00
|
|
|
return __cons_readchar_w32 (usec);
|
2017-12-18 01:44:51 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-02-27 23:37:19 +00:00
|
|
|
R_API bool r_cons_readpush(const char *str, int len) {
|
2018-03-14 12:08:01 +00:00
|
|
|
char *res = (len + readbuffer_length > 0) ? realloc (readbuffer, len + readbuffer_length) : NULL;
|
2018-02-27 23:37:19 +00:00
|
|
|
if (res) {
|
|
|
|
readbuffer = res;
|
|
|
|
memmove (readbuffer + readbuffer_length, str, len);
|
|
|
|
readbuffer_length += len;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_cons_readflush() {
|
|
|
|
R_FREE (readbuffer);
|
|
|
|
readbuffer_length = 0;
|
2018-02-27 23:26:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 02:56:15 +00:00
|
|
|
R_API void r_cons_switchbuf(bool active) {
|
|
|
|
bufactive = active;
|
|
|
|
}
|
|
|
|
|
2019-03-15 04:55:08 +00:00
|
|
|
#if !__WINDOWS__
|
2018-09-03 21:46:13 +00:00
|
|
|
extern volatile sig_atomic_t sigwinchFlag;
|
|
|
|
#endif
|
|
|
|
|
2010-02-28 14:16:02 +00:00
|
|
|
R_API int r_cons_readchar() {
|
2018-06-11 14:48:51 +00:00
|
|
|
void *bed;
|
2009-02-05 21:08:46 +00:00
|
|
|
char buf[2];
|
2014-11-19 21:25:17 +00:00
|
|
|
buf[0] = -1;
|
2018-02-27 23:37:19 +00:00
|
|
|
if (readbuffer_length > 0) {
|
2018-02-27 23:26:41 +00:00
|
|
|
int ch = *readbuffer;
|
2018-02-27 23:37:19 +00:00
|
|
|
readbuffer_length--;
|
|
|
|
memmove (readbuffer, readbuffer + 1, readbuffer_length);
|
2018-02-27 23:26:41 +00:00
|
|
|
return ch;
|
|
|
|
}
|
2019-03-15 04:55:08 +00:00
|
|
|
#if __WINDOWS__
|
2019-06-09 16:50:36 +00:00
|
|
|
return __cons_readchar_w32 (0);
|
2009-02-05 21:08:46 +00:00
|
|
|
#else
|
2010-01-30 13:02:53 +00:00
|
|
|
r_cons_set_raw (1);
|
2018-06-11 14:48:51 +00:00
|
|
|
bed = r_cons_sleep_begin ();
|
2018-09-03 21:46:13 +00:00
|
|
|
|
|
|
|
// Blocks until either stdin has something to read or a signal happens.
|
|
|
|
// This serves to check if the terminal window was resized. It avoids the race
|
|
|
|
// condition that could happen if we did not use pselect or select in case SIGWINCH
|
|
|
|
// was handled immediately before the blocking call (select or read). The race is
|
|
|
|
// prevented from happening by having SIGWINCH blocked process-wide except for in
|
|
|
|
// pselect (that is what pselect is for).
|
|
|
|
fd_set readfds;
|
|
|
|
sigset_t sigmask;
|
|
|
|
FD_ZERO (&readfds);
|
|
|
|
FD_SET (STDIN_FILENO, &readfds);
|
|
|
|
r_signal_sigmask (0, NULL, &sigmask);
|
|
|
|
sigdelset (&sigmask, SIGWINCH);
|
2018-09-12 09:55:20 +00:00
|
|
|
while (pselect (STDIN_FILENO + 1, &readfds, NULL, NULL, NULL, &sigmask) == -1) {
|
2018-09-20 22:13:19 +00:00
|
|
|
if (errno == EBADF) {
|
2018-10-19 11:42:38 +00:00
|
|
|
eprintf ("r_cons_readchar (): EBADF\n");
|
2018-09-20 22:13:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-09-12 09:55:20 +00:00
|
|
|
if (sigwinchFlag) {
|
|
|
|
sigwinchFlag = 0;
|
|
|
|
resizeWin ();
|
|
|
|
}
|
2018-09-03 21:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t ret = read (STDIN_FILENO, buf, 1);
|
2018-06-11 14:48:51 +00:00
|
|
|
r_cons_sleep_end (bed);
|
|
|
|
if (ret != 1) {
|
2009-02-05 21:08:46 +00:00
|
|
|
return -1;
|
2017-12-18 01:44:51 +00:00
|
|
|
}
|
2018-04-09 02:56:15 +00:00
|
|
|
if (bufactive) {
|
|
|
|
r_cons_set_raw (0);
|
|
|
|
}
|
2014-11-19 21:25:17 +00:00
|
|
|
return r_cons_controlz (buf[0]);
|
2019-06-09 16:50:36 +00:00
|
|
|
#endif
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|
2010-02-28 13:49:26 +00:00
|
|
|
|
2018-12-04 11:49:43 +00:00
|
|
|
R_API bool r_cons_yesno(int def, const char *fmt, ...) {
|
2010-02-28 13:49:26 +00:00
|
|
|
va_list ap;
|
2016-09-18 23:35:36 +00:00
|
|
|
ut8 key = (ut8)def;
|
2010-02-28 13:49:26 +00:00
|
|
|
va_start (ap, fmt);
|
2019-02-18 05:20:01 +00:00
|
|
|
|
|
|
|
if (!r_cons_is_interactive ()) {
|
2019-06-18 14:02:04 +00:00
|
|
|
va_end (ap);
|
2019-02-18 05:20:01 +00:00
|
|
|
return def == 'y';
|
|
|
|
}
|
2010-02-28 13:49:26 +00:00
|
|
|
vfprintf (stderr, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
fflush (stderr);
|
2018-01-02 18:31:02 +00:00
|
|
|
r_cons_set_raw (true);
|
2014-04-30 22:54:48 +00:00
|
|
|
(void)read (0, &key, 1);
|
2018-03-07 00:19:04 +00:00
|
|
|
write (2, " ", 1);
|
|
|
|
write (2, &key, 1);
|
2010-02-28 13:49:26 +00:00
|
|
|
write (2, "\n", 1);
|
2016-09-18 23:35:36 +00:00
|
|
|
if (key == 'Y') {
|
2010-02-28 13:49:26 +00:00
|
|
|
key = 'y';
|
2016-09-18 23:35:36 +00:00
|
|
|
}
|
2018-01-02 18:31:02 +00:00
|
|
|
r_cons_set_raw (false);
|
2016-09-18 23:35:36 +00:00
|
|
|
if (key == '\n' || key == '\r') {
|
2010-02-28 13:49:26 +00:00
|
|
|
key = def;
|
2016-09-18 23:35:36 +00:00
|
|
|
}
|
|
|
|
return key == 'y';
|
2010-02-28 13:49:26 +00:00
|
|
|
}
|
2015-08-31 03:06:01 +00:00
|
|
|
|
2018-10-31 00:52:08 +00:00
|
|
|
R_API char *r_cons_password(const char *msg) {
|
|
|
|
int i = 0;
|
|
|
|
char buf[256] = {0};
|
|
|
|
printf ("\r%s", msg);
|
|
|
|
fflush (stdout);
|
|
|
|
r_cons_set_raw (1);
|
|
|
|
#if __UNIX__
|
2019-02-07 10:07:23 +00:00
|
|
|
RCons *a = r_cons_singleton ();
|
2018-10-31 00:52:08 +00:00
|
|
|
a->term_raw.c_lflag &= ~(ECHO | ECHONL);
|
|
|
|
// // required to make therm/iterm show the key
|
|
|
|
// // cannot read when enabled in this way
|
|
|
|
// a->term_raw.c_lflag |= ICANON;
|
|
|
|
tcsetattr (0, TCSADRAIN, &a->term_raw);
|
|
|
|
signal (SIGTSTP, SIG_IGN);
|
|
|
|
#endif
|
2018-11-07 17:14:08 +00:00
|
|
|
while (i < sizeof (buf) - 1) {
|
2018-10-31 00:52:08 +00:00
|
|
|
int ch = r_cons_readchar ();
|
|
|
|
if (ch == 127) { // backspace
|
|
|
|
if (i < 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch == '\r' || ch == '\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[i++] = ch;
|
|
|
|
}
|
|
|
|
buf[i] = 0;
|
|
|
|
r_cons_set_raw (0);
|
|
|
|
printf ("\n");
|
|
|
|
#if __UNIX__
|
|
|
|
signal (SIGTSTP, SIG_DFL);
|
|
|
|
#endif
|
|
|
|
return strdup (buf);
|
|
|
|
}
|
|
|
|
|
2015-08-31 03:06:01 +00:00
|
|
|
R_API char *r_cons_input(const char *msg) {
|
2018-10-31 00:52:08 +00:00
|
|
|
char *oprompt = r_line_get_prompt ();
|
2016-12-18 16:14:30 +00:00
|
|
|
if (!oprompt) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-31 03:06:01 +00:00
|
|
|
char buf[1024];
|
|
|
|
if (msg) {
|
|
|
|
r_line_set_prompt (msg);
|
|
|
|
} else {
|
|
|
|
r_line_set_prompt ("");
|
|
|
|
}
|
|
|
|
buf[0] = 0;
|
2016-01-04 23:47:41 +00:00
|
|
|
r_cons_fgets (buf, sizeof (buf), 0, NULL);
|
2015-08-31 03:06:01 +00:00
|
|
|
r_line_set_prompt (oprompt);
|
|
|
|
free (oprompt);
|
|
|
|
return strdup (buf);
|
|
|
|
}
|