robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
38986188ba
master
1#include "nvim/math.h" 2#include "nvim/tui/termkey/termkey.h" 3#include "nvim/vterm/mouse.h" 4#include "nvim/vterm/vterm.h" 5#include "nvim/vterm/vterm_internal_defs.h" 6 7#include "vterm/mouse.c.generated.h" 8 9static void output_mouse (VTermState * state ,int code ,int pressed ,int modifiers ,int col ,int row ) 10{ 11modifiers <<=2 ; 12 13switch (state -> mouse_protocol ) { 14case MOUSE_X10 : 15if (col + 0x21 > 0xff ) { 16col = 0xff - 0x21 ; 17 } 18if (row + 0x21 > 0xff ) { 19row = 0xff - 0x21 ; 20 } 21 22if (!pressed ) { 23code = 3 ; 24 } 25 26if (code & 0x80 ) { 27break ; 28 } 29vterm_push_output_sprintf_ctrl (state -> vt ,C1_CSI ,"M%c%c%c" , 30 (code |modifiers )+ 0x20 ,col + 0x21 ,row + 0x21 ); 31break ; 32 33case MOUSE_UTF8 : { 34char utf8 [18 ]; 35size_t len = 0 ; 36 37if (!pressed ) { 38code = 3 ; 39 } 40 41len += (size_t )fill_utf8 ((code |modifiers )+ 0x20 ,utf8 + len ); 42len += (size_t )fill_utf8 (col + 0x21 ,utf8 + len ); 43len += (size_t )fill_utf8 (row + 0x21 ,utf8 + len ); 44utf8 [len ]= 0 ; 45 46vterm_push_output_sprintf_ctrl (state -> vt ,C1_CSI ,"M%s" ,utf8 ); 47 } 48break ; 49 50case MOUSE_SGR : 51vterm_push_output_sprintf_ctrl (state -> vt ,C1_CSI ,"<%d;%d;%d%c" , 52code |modifiers ,col + 1 ,row + 1 ,pressed ?'M' :'m' ); 53break ; 54 55case MOUSE_RXVT : 56if (!pressed ) { 57code = 3 ; 58 } 59 60vterm_push_output_sprintf_ctrl (state -> vt ,C1_CSI ,"%d;%d;%dM" , 61code |modifiers ,col + 1 ,row + 1 ); 62break ; 63 } 64} 65 66void vterm_mouse_move (VTerm * vt ,int row ,int col ,VTermModifier mod ) 67{ 68VTermState * state = vt -> state ; 69 70if (col == state -> mouse_col && row == state -> mouse_row ) { 71return ; 72 } 73 74state -> mouse_col = col ; 75state -> mouse_row = row ; 76 77if ((state -> mouse_flags & MOUSE_WANT_DRAG && state -> mouse_buttons ) 78|| (state -> mouse_flags & MOUSE_WANT_MOVE )) { 79if (state -> mouse_buttons ) { 80int button = xctz ((uint64_t )state -> mouse_buttons )+ 1 ; 81if (button < 4 ) { 82output_mouse (state ,button - 1 + 0x20 ,1 , (int )mod ,col ,row ); 83 }else if (button >=8 && button < 12 ) { 84output_mouse (state ,button - 8 + 0x80 + 0x20 ,1 , (int )mod ,col ,row ); 85 } 86 }else { 87output_mouse (state ,3 + 0x20 ,1 , (int )mod ,col ,row ); 88 } 89 } 90} 91 92void vterm_mouse_button (VTerm * vt ,int button ,bool pressed ,VTermModifier mod ) 93{ 94VTermState * state = vt -> state ; 95 96int old_buttons = state -> mouse_buttons ; 97 98if ((button > 0 && button <=3 )|| (button >=8 && button <=11 )) { 99if (pressed ) { 100state -> mouse_buttons |= (1 << (button - 1 )); 101 }else { 102state -> mouse_buttons &= ~(1 << (button - 1 )); 103 } 104 } 105 106// Most of the time we don't get button releases from 4/5/6/7 107if (state -> mouse_buttons == old_buttons && (button < 4 || button > 7 )) { 108return ; 109 } 110 111if (!state -> mouse_flags ) { 112return ; 113 } 114 115if (button < 4 ) { 116output_mouse (state ,button - 1 ,pressed , (int )mod ,state -> mouse_col ,state -> mouse_row ); 117 }else if (button < 8 ) { 118output_mouse (state ,button - 4 + 0x40 ,pressed , (int )mod ,state -> mouse_col ,state -> mouse_row ); 119 }else if (button < 12 ) { 120output_mouse (state ,button - 8 + 0x80 ,pressed , (int )mod ,state -> mouse_col ,state -> mouse_row ); 121 } 122}