From eaad0f51525b0d3e08e7cc60c3c7c1b39d8dd495 Mon Sep 17 00:00:00 2001 From: pancake Date: Tue, 2 Jul 2019 03:33:57 +0200 Subject: [PATCH] Add RCons.get_click(), initial integration in visual and panels ##cons --- libr/cons/cons.c | 20 +++++++++++++++----- libr/cons/input.c | 17 ++++++----------- libr/core/cconfig.c | 9 ++++++++- libr/core/panels.c | 34 +++++++++++++++++++++++++++------- libr/core/visual.c | 42 +++++++++++++++++++----------------------- libr/include/r_cons.h | 5 +++++ 6 files changed, 80 insertions(+), 47 deletions(-) diff --git a/libr/cons/cons.c b/libr/cons/cons.c index 9122832ce7..733b4f6316 100644 --- a/libr/cons/cons.c +++ b/libr/cons/cons.c @@ -446,24 +446,34 @@ void resizeWin(void) { } } +R_API void r_cons_set_click(int x, int y) { + I.click_x = x; + I.click_y = y; + I.click_set = true;; +} + R_API bool r_cons_get_click(int *x, int *y) { if (x) { - *x = 0; + *x = I.click_x; } if (y) { - *y = 0; + *y = I.click_y; } - return false; + bool set = I.click_set; + I.click_set = false;; + return set; } R_API bool r_cons_enable_mouse(const bool enable) { #if __UNIX__ const char *click = enable ? "\x1b[?1000;1006;1015h" - : "\x1b[?1000;1006;1015l"; + : "\x1b[?1001r" "\x1b[?1000l"; + // : "\x1b[?1000;1006;1015l"; + // const char *old = enable ? "\x1b[?1001s" "\x1b[?1000h" : "\x1b[?1001r" "\x1b[?1000l"; bool enabled = I.mouse; I.mouse = enable; - write (1, click, strlen (click)); + write (2, click, strlen (click)); return enabled; #elif __WINDOWS__ DWORD mode, mouse; diff --git a/libr/cons/input.c b/libr/cons/input.c index a23854899f..6e7109400b 100644 --- a/libr/cons/input.c +++ b/libr/cons/input.c @@ -48,7 +48,6 @@ R_API int r_cons_controlz(int ch) { // 97 - wheel down // 95 - mouse up // 92 - mouse down - static int __parseMouseEvent() { char xpos[32]; char ypos[32]; @@ -59,7 +58,6 @@ static int __parseMouseEvent() { // [35M - mouseup if (ch2 == ';') { int i; -repeat: // read until next ; for (i = 0; i < sizeof (xpos); i++) { char ch = r_cons_readchar (); @@ -77,20 +75,17 @@ repeat: ypos[i] = ch; } ypos[i] = 0; - eprintf ("CLICK (%s) (%s)\n", xpos, ypos); + r_cons_set_click (atoi (xpos), atoi (ypos)); ch = r_cons_readchar (); // ignored int ch = r_cons_readchar (); if (ch == 27) { ch = r_cons_readchar (); // '[' - eprintf ("CH %c\n", ch); - ch = r_cons_readchar (); // '3' - eprintf ("CH %c\n", ch); - ch = r_cons_readchar (); // '0' - eprintf ("CH %c\n", ch); - ch = r_cons_readchar (); // ';' - eprintf ("CH %c\n", ch); - goto repeat; + } + if (ch == '[') { + do { + ch = r_cons_readchar (); // '3' + } while (ch != 'M'); } } return 0; diff --git a/libr/core/cconfig.c b/libr/core/cconfig.c index 91419d9131..a5bde503e7 100644 --- a/libr/core/cconfig.c +++ b/libr/core/cconfig.c @@ -1938,6 +1938,12 @@ static bool cb_fps(void *user, void *data) { return true; } +static bool cb_scrwheel(void* user, void* data) { + RConfigNode *node = (RConfigNode*) data; + r_cons_enable_mouse (node->i_value); + return true; +} + static bool cb_scrbreakword(void* user, void* data) { RConfigNode *node = (RConfigNode*) data; if (*node->value) { @@ -3410,7 +3416,8 @@ R_API int r_core_config_init(RCore *core) { SETI ("scr.wheel.speed", 4, "Mouse wheel speed"); #endif SETPREF ("scr.wheel.nkey", "false", "Use sn/sp and scr.nkey on wheel instead of scroll"); - SETPREF ("scr.wheel", "true", "Mouse wheel in Visual; temporaryly disable/reenable by right click/Enter)"); + // RENAME TO scr.mouse + SETCB ("scr.wheel", "true", &cb_scrwheel, "Mouse wheel in Visual; temporaryly disable/reenable by right click/Enter)"); // DEPRECATED: USES hex.cols now SETI ("scr.colpos", 80, "Column position of cmd.cprompt in visual"); SETCB ("scr.breakword", "", &cb_scrbreakword, "Emulate console break (^C) when a word is printed (useful for pD)"); SETCB ("scr.breaklines", "false", &cb_breaklines, "Break lines in Visual instead of truncating them"); diff --git a/libr/core/panels.c b/libr/core/panels.c index 917c86380e..81f8792fd3 100644 --- a/libr/core/panels.c +++ b/libr/core/panels.c @@ -5329,22 +5329,42 @@ repeat: __panels_layout_refresh (core); RPanel *cur = __getCurPanel (panels); if (panels->fun == PANEL_FUN_SNOW || panels->fun == PANEL_FUN_SAKURA) { - if (panels->mode != PANEL_MODE_MENU) { - okey = r_cons_readchar_timeout (300); - if (okey == -1) { - cur->view->refresh = true; - goto repeat; - } - } else { + if (panels->mode == PANEL_MODE_MENU) { panels->fun = PANEL_FUN_NOFUN; __resetSnow (panels); __setRefreshAll (core, false, false); goto repeat; } + okey = r_cons_readchar_timeout (300); + if (okey == -1) { + cur->view->refresh = true; + goto repeat; + } } else { okey = r_cons_readchar (); } key = r_cons_arrow_to_hjkl (okey); + if (key == 0) { + int x, y; + if (r_cons_get_click (&x, &y)) { + if (y == 1) { + if (panels->mode == PANEL_MODE_MENU) { + key = '\n'; +} else { + __setMode (core, PANEL_MODE_MENU); + __clearPanelsMenu (core); + __getCurPanel (panels)->view->refresh = true; + key = 'j'; +} + } else if (x < 20) { + key = 9; + } else { + goto repeat; + } + } else { + goto repeat; + } + } r_cons_switchbuf (true); if (panels->mode == PANEL_MODE_MENU) { diff --git a/libr/core/visual.c b/libr/core/visual.c index c8b307b5e8..3649f5526d 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -391,13 +391,7 @@ R_API void r_core_visual_showcursor(RCore *core, int x) { if (core && core->vmode) { r_cons_show_cursor (x); if (!x) { - // TODO: cache this - int wheel = r_config_get_i (core->config, "scr.wheel"); - if (wheel) { - r_cons_enable_mouse (true); - } else { - r_cons_enable_mouse (false); - } + r_cons_enable_mouse (r_config_get_i (core->config, "scr.wheel")); } else { r_cons_enable_mouse (false); } @@ -1473,10 +1467,7 @@ repeat: r_config_set_i (core->config, "asm.bytes", asm_bytes); } r_cons_flush (); - int wheel = r_config_get_i (core->config, "scr.wheel"); - if (wheel > 0) { - r_cons_enable_mouse (true); - } + r_cons_enable_mouse (r_config_get_i (core->config, "scr.wheel")); ch = r_cons_readchar (); ch = r_cons_arrow_to_hjkl (ch); if (ch == ':') { @@ -2253,7 +2244,17 @@ R_API int r_core_visual_cmd(RCore *core, const char *arg) { ch = r_cons_arrow_to_hjkl (ch); ch = visual_nkey (core, ch); if (ch < 2) { - return 1; + int x, y; + if (r_cons_get_click (&x, &y)) { + eprintf ("CLICK %d %d\n", x, y); + if (y == 1 && x < 20) { + ch = ':'; + } else { + return 1; + } + } else { + return 1; + } } if (r_cons_singleton ()->mouse_event) { wheelspeed = r_config_get_i (core->config, "scr.wheel.speed"); @@ -2518,9 +2519,9 @@ R_API int r_core_visual_cmd(RCore *core, const char *arg) { break; case 'A': { - int oce = core->print->cur_enabled; - int oco = core->print->ocur; - int occ = core->print->cur; + const int oce = core->print->cur_enabled; + const int oco = core->print->ocur; + const int occ = core->print->cur; ut64 off = oce? core->offset + core->print->cur: core->offset; core->print->cur_enabled = 0; r_cons_enable_mouse (false); @@ -2528,8 +2529,7 @@ R_API int r_core_visual_cmd(RCore *core, const char *arg) { core->print->cur_enabled = oce; core->print->cur = occ; core->print->ocur = oco; - int wheel = r_config_get_i (core->config, "scr.wheel"); - if (wheel) { + if (r_config_get_i (core->config, "scr.wheel")) { r_cons_enable_mouse (true); } } @@ -4123,7 +4123,7 @@ R_API void r_core_visual_disasm_down(RCore *core, RAsmOp *op, int *cols) { R_API int r_core_visual(RCore *core, const char *input) { const char *teefile; ut64 scrseek; - int wheel, flags, ch; + int flags, ch; bool skip; char arg[2] = { input[0], 0 @@ -4170,7 +4170,6 @@ dodo: r_cons_show_cursor (false); r_cons_set_raw (1); const int ref = r_config_get_i (core->config, "dbg.slow"); - #if 1 // This is why multiple debug views dont work if (core->printidx == R_CORE_VISUAL_MODE_DB) { @@ -4200,11 +4199,8 @@ dodo: printfmtSingle[2] = debugstr; } #endif - wheel = r_config_get_i (core->config, "scr.wheel"); r_cons_show_cursor (false); - if (wheel) { - r_cons_enable_mouse (true); - } + r_cons_enable_mouse (r_config_get_i (core->config, "scr.wheel")); core->cons->event_resize = NULL; // avoid running old event with new data core->cons->event_data = core; core->cons->event_resize = (RConsEvent) visual_refresh_oneshot; diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 3292e67cdd..6899629dc5 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -500,6 +500,9 @@ typedef struct r_cons_t { bool use_tts; bool filter; char* (*rgbstr)(char *str, size_t sz, ut64 addr); + bool click_set; + int click_x; + int click_y; // TODO: move into instance? + avoid unnecessary copies } RCons; @@ -768,6 +771,8 @@ R_API RCons *r_cons_singleton(void); R_API RCons *r_cons_free(void); R_API char *r_cons_lastline(int *size); R_API char *r_cons_lastline_utf8_ansi_len(int *len); +R_API void r_cons_set_click(int x, int y); +R_API bool r_cons_get_click(int *x, int *y); typedef void (*RConsBreak)(void *); R_API void r_cons_break_end(void);