From 7aa3c31d6921fa9bdddff79dba423ec0a634a0bd Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 28 Mar 2018 22:27:23 -0400 Subject: [PATCH] move mouse handling to x11.c. need to move cut and paste functionality to buffer/view --- inc/win.h | 15 +++++++-------- lib/x11.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- tide.c | 49 +++---------------------------------------------- 3 files changed, 58 insertions(+), 57 deletions(-) diff --git a/inc/win.h b/inc/win.h index 89e2213..fe2968f 100644 --- a/inc/win.h +++ b/inc/win.h @@ -28,14 +28,6 @@ typedef struct { View view; } Region; -typedef void (*MouseFunc)(WinRegion id, size_t count, size_t row, size_t col); - -typedef struct { - MouseFunc left; - MouseFunc middle; - MouseFunc right; -} MouseConfig; - void win_init(KeyBinding* bindings, void (*errfn)(char*)); void win_save(char* path); void win_loop(void); @@ -53,3 +45,10 @@ void onupdate(void); void onmouseleft(WinRegion id, bool pressed, size_t row, size_t col); void onmousemiddle(WinRegion id, bool pressed, size_t row, size_t col); void onmouseright(WinRegion id, bool pressed, size_t row, size_t col); + +/* move these to x11.c when possible */ +extern int SearchDir; +extern char* SearchTerm; +void exec(char* cmd); +void cut(char* arg); +void paste(char* arg); diff --git a/lib/x11.c b/lib/x11.c index 85966ca..4581839 100644 --- a/lib/x11.c +++ b/lib/x11.c @@ -23,6 +23,9 @@ static uint32_t special_keys(uint32_t key); static uint32_t getkey(XEvent* e); static void mouse_click(int btn, bool pressed, int x, int y); +static void mouse_left(WinRegion id, bool pressed, size_t row, size_t col); +static void mouse_middle(WinRegion id, bool pressed, size_t row, size_t col); +static void mouse_right(WinRegion id, bool pressed, size_t row, size_t col); static void xupdate(Job* job); static void xfocus(XEvent* e); @@ -120,6 +123,9 @@ static Region Regions[NREGIONS] = {0}; static Rune LastKey; static KeyBinding* Keys = NULL; +int SearchDir = DOWN; +char* SearchTerm = NULL; + void x11_init(XConfig* cfg) { signal(SIGPIPE, SIG_IGN); // Ignore the SIGPIPE signal setlocale(LC_CTYPE, ""); @@ -778,10 +784,49 @@ static void mouse_click(int btn, bool pressed, int x, int y) { size_t row = (y-Regions[id].y) / x11_font_height(CurrFont); size_t col = (x-Regions[id].x) / x11_font_width(CurrFont); switch(btn) { - case MouseLeft: onmouseleft(id, pressed, row, col); break; - case MouseMiddle: onmousemiddle(id, pressed, row, col); break; - case MouseRight: onmouseright(id, pressed, row, col); break; + case MouseLeft: mouse_left(id, pressed, row, col); break; + case MouseMiddle: mouse_middle(id, pressed, row, col); break; + case MouseRight: mouse_right(id, pressed, row, col); break; case MouseWheelUp: view_scroll(win_view(id), -ScrollBy); break; case MouseWheelDn: view_scroll(win_view(id), +ScrollBy); break; } } + +static void mouse_left(WinRegion id, bool pressed, size_t row, size_t col) { + static int count = 0; + static uint64_t before = 0; + if (!pressed) return; + uint64_t now = getmillis(); + count = ((now-before) <= (uint64_t)ClickTime ? count+1 : 1); + before = now; + + if (count == 1) { + view_setcursor(win_view(id)i, row, col, x11_keymodsset(ModShift)); + } else if (count == 2) { + view_select(win_view(id), row, col); + } else if (count == 3) { + view_selword(win_view(id), row, col); + } +} + +static void mouse_middle(WinRegion id, bool pressed, size_t row, size_t col) { + if (pressed) return; + if (win_btnpressed(MouseLeft)) { + cut(NULL); + } else { + char* str = view_fetch(win_view(id), row, col, riscmd); + if (str) exec(str); + free(str); + } +} + +static void mouse_right(WinRegion id, bool pressed, size_t row, size_t col) { + if (pressed) return; + if (win_btnpressed(MouseLeft)) { + paste(NULL); + } else { + SearchDir *= (x11_keymodsset(ModShift) ? -1 : +1); + free(SearchTerm); + SearchTerm = view_fetch(win_view(id), row, col, risfile); + } +} diff --git a/tide.c b/tide.c index 096c305..7aab3c3 100644 --- a/tide.c +++ b/tide.c @@ -12,8 +12,6 @@ typedef struct { } Tag; static Tag Builtins[]; -static int SearchDir = DOWN; -static char* SearchTerm = NULL; static size_t Marks[10] = {0}; /* The shell: Filled in with $SHELL. Used to execute commands */ @@ -67,7 +65,7 @@ static void onpaste(char* text) { view_putstr(win_view(FOCUSED), text); } -static void cut(char* arg) { +void cut(char* arg) { View* view = win_view(FOCUSED); /* select the current line if no selection */ if (!view_selsize(view)) @@ -82,7 +80,7 @@ static void cut(char* arg) { } } -static void paste(char* arg) { +void paste(char* arg) { assert(x11_sel_get(CLIPBOARD, onpaste)); } @@ -270,7 +268,7 @@ static void cmd_execwitharg(char* cmd, char* arg) { free(cmd); } -static void exec(char* cmd) { +void exec(char* cmd) { /* skip leading space */ for (; *cmd && isspace(*cmd); cmd++); if (!*cmd) return; @@ -341,47 +339,6 @@ static void get(char* arg) { view_reload(win_view(EDIT)); } -/* Mouse Handling - ******************************************************************************/ -void onmouseleft(WinRegion id, bool pressed, size_t row, size_t col) { - static int count = 0; - static uint64_t before = 0; - if (!pressed) return; - uint64_t now = getmillis(); - count = ((now-before) <= (uint64_t)ClickTime ? count+1 : 1); - before = now; - - if (count == 1) { - view_setcursor(win_view(id), row, col, x11_keymodsset(ModShift)); - } else if (count == 2) { - view_select(win_view(id), row, col); - } else if (count == 3) { - view_selword(win_view(id), row, col); - } -} - -void onmousemiddle(WinRegion id, bool pressed, size_t row, size_t col) { - if (pressed) return; - if (win_btnpressed(MouseLeft)) { - cut(NULL); - } else { - char* str = view_fetch(win_view(id), row, col, riscmd); - if (str) exec(str); - free(str); - } -} - -void onmouseright(WinRegion id, bool pressed, size_t row, size_t col) { - if (pressed) return; - if (win_btnpressed(MouseLeft)) { - paste(NULL); - } else { - SearchDir *= (x11_keymodsset(ModShift) ? -1 : +1); - free(SearchTerm); - SearchTerm = view_fetch(win_view(id), row, col, risfile); - } -} - /* Keyboard Handling ******************************************************************************/ static void tag_undo(char* arg) { -- 2.51.0