X-Git-Url: http://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Fbase%2Flib_mouse.c;h=8a0e4c1f32320e7fd6eed52aa7785b847ca2b641;hp=cd46d0c9e21250db77404b061c7f9b2812341ee3;hb=06078d3fa68db669ed37178c01873546b4b28745;hpb=83e9a85c4205c920cc9992bfc96f6546cd4fff22 diff --git a/ncurses/base/lib_mouse.c b/ncurses/base/lib_mouse.c index cd46d0c9..8a0e4c1f 100644 --- a/ncurses/base/lib_mouse.c +++ b/ncurses/base/lib_mouse.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 1998-2013,2014 Free Software Foundation, Inc. * + * Copyright (c) 1998-2016,2017 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -84,7 +84,7 @@ #define CUR SP_TERMTYPE #endif -MODULE_ID("$Id: lib_mouse.c,v 1.162 2014/10/12 00:03:37 tom Exp $") +MODULE_ID("$Id: lib_mouse.c,v 1.176 2017/11/18 22:12:06 Vassili.Courzakis Exp $") #include @@ -225,7 +225,7 @@ write_event(SCREEN *sp, int down, int button, int x, int y) char buf[6]; unsigned long ignore; - strcpy(buf, "\033[M"); /* should be the same as key_mouse */ + _nc_STRCPY(buf, "\033[M", sizeof(buf)); /* should be the same as key_mouse */ buf[3] = ' ' + (button - 1) + (down ? 0 : 0x40); buf[4] = ' ' + x - LEFT_COL + 1; buf[5] = ' ' + y - TOP_ROW + 1; @@ -432,7 +432,7 @@ enable_xterm_mouse(SCREEN *sp, int enable) #if USE_GPM_SUPPORT static bool -allow_gpm_mouse(SCREEN *sp) +allow_gpm_mouse(SCREEN *sp GCC_UNUSED) { bool result = FALSE; @@ -446,8 +446,8 @@ allow_gpm_mouse(SCREEN *sp) #endif /* GPM does printf's without checking if stdout is a terminal */ if (NC_ISATTY(fileno(stdout))) { - char *list = getenv("NCURSES_GPM_TERMS"); - char *env = getenv("TERM"); + const char *list = getenv("NCURSES_GPM_TERMS"); + const char *env = getenv("TERM"); if (list != 0) { if (env != 0) { result = _nc_name_match(list, env, "|:"); @@ -486,10 +486,17 @@ load_gpm_library(SCREEN *sp) { sp->_mouse_gpm_found = FALSE; if ((sp->_dlopen_gpm = dlopen(LIBGPM_SONAME, my_RTLD)) != 0) { +#if (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpedantic" +#endif if (GET_DLSYM(gpm_fd) == 0 || GET_DLSYM(Gpm_Open) == 0 || GET_DLSYM(Gpm_Close) == 0 || GET_DLSYM(Gpm_GetEvent) == 0) { +#if (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__) +#pragma GCC diagnostic pop +#endif T(("GPM initialization failed: %s", dlerror())); unload_gpm_library(sp); } else { @@ -597,7 +604,7 @@ initialize_mousetype(SCREEN *sp) /* OS/2 VIO */ #if USE_EMX_MOUSE if (!sp->_emxmouse_thread - && strstr(TerminalOf(sp)->type.term_names, "xterm") == 0 + && strstr(SP_TERMTYPE term_names, "xterm") == 0 && key_mouse) { int handles[2]; @@ -608,7 +615,7 @@ initialize_mousetype(SCREEN *sp) int rc; if (!sp->_emxmouse_buttons[0]) { - char *s = getenv("MOUSE_BUTTONS_123"); + const char *s = getenv("MOUSE_BUTTONS_123"); sp->_emxmouse_buttons[0] = 1; if (s && strlen(s) >= 3) { @@ -641,13 +648,14 @@ initialize_mousetype(SCREEN *sp) #if USE_SYSMOUSE { + static char dev_tty[] = "/dev/tty"; struct mouse_info the_mouse; char *the_device = 0; if (NC_ISATTY(sp->_ifd)) the_device = ttyname(sp->_ifd); if (the_device == 0) - the_device = "/dev/tty"; + the_device = dev_tty; sp->_mouse_fd = open(the_device, O_RDWR); @@ -710,10 +718,10 @@ initialize_mousetype(SCREEN *sp) /* we know how to recognize mouse events under "xterm" */ if (key_mouse != 0) { if (!strcmp(key_mouse, xterm_kmous) - || strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) { + || strstr(SP_TERMTYPE term_names, "xterm") != 0) { init_xterm_mouse(sp); } - } else if (strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) { + } else if (strstr(SP_TERMTYPE term_names, "xterm") != 0) { if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK) init_xterm_mouse(sp); } @@ -727,10 +735,11 @@ _nc_mouse_init(SCREEN *sp) /* initialize the mouse */ { bool result = FALSE; - int i; if (sp != 0) { if (!sp->_mouse_initialized) { + int i; + sp->_mouse_initialized = TRUE; TR(MY_TRACE, ("_nc_mouse_init() called")); @@ -888,7 +897,7 @@ _nc_mouse_event(SCREEN *sp) do { \ eventp->bstate = MASK_PRESS(n); \ sp->_mouse_bstate |= MASK_PRESS(n); \ - if (kbuf[0] & 0x40) { \ + if (button & 0x40) { \ eventp->bstate = MASK_RELEASE(n); \ sp->_mouse_bstate &= ~MASK_PRESS(n); \ } \ @@ -896,7 +905,7 @@ _nc_mouse_event(SCREEN *sp) #else #define PRESS_POSITION(n) \ do { \ - eventp->bstate = (mmask_t) (sp->_mouse_bstate & MASK_PRESS(n) \ + eventp->bstate = (mmask_t) ((sp->_mouse_bstate & MASK_PRESS(n)) \ ? REPORT_MOUSE_POSITION \ : MASK_PRESS(n)); \ sp->_mouse_bstate |= MASK_PRESS(n); \ @@ -946,11 +955,11 @@ static bool decode_X10_bstate(SCREEN *sp, MEVENT * eventp, unsigned intro) { bool result; - int b; eventp->bstate = 0; if (!handle_wheel(sp, eventp, (int) intro, (intro & 96) == 96)) { + /* * Release events aren't reported for individual buttons, just for * the button set as a whole. However, because there are normally @@ -959,6 +968,8 @@ decode_X10_bstate(SCREEN *sp, MEVENT * eventp, unsigned intro) * the previous event. */ if (sp->_mouse_bstate & BUTTON_PRESSED) { + int b; + eventp->bstate = BUTTON_RELEASED; for (b = 1; b <= MAX_BUTTONS; ++b) { if (!(sp->_mouse_bstate & MASK_PRESS(b))) @@ -1019,7 +1030,8 @@ decode_X10_bstate(SCREEN *sp, MEVENT * eventp, unsigned intro) static bool decode_xterm_X10(SCREEN *sp, MEVENT * eventp) { - unsigned char kbuf[4]; +#define MAX_KBUF 3 + unsigned char kbuf[MAX_KBUF + 1]; size_t grabbed; int res; bool result; @@ -1030,7 +1042,7 @@ decode_xterm_X10(SCREEN *sp, MEVENT * eventp) # endif _nc_globals.read_thread = pthread_self(); # endif - for (grabbed = 0; grabbed < 3; grabbed += (size_t) res) { + for (grabbed = 0; grabbed < MAX_KBUF; grabbed += (size_t) res) { /* For VIO mouse we add extra bit 64 to disambiguate button-up. */ res = (int) read( @@ -1039,14 +1051,14 @@ decode_xterm_X10(SCREEN *sp, MEVENT * eventp) #else sp->_ifd, #endif - kbuf + grabbed, 3 - grabbed); + kbuf + grabbed, (size_t) (MAX_KBUF - (int) grabbed)); if (res == -1) break; } #if USE_PTHREADS_EINTR _nc_globals.read_thread = 0; #endif - kbuf[3] = '\0'; + kbuf[MAX_KBUF] = '\0'; TR(TRACE_IEVENT, ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf)); @@ -1074,7 +1086,6 @@ decode_xterm_1005(SCREEN *sp, MEVENT * eventp) size_t grabbed; size_t limit = (sizeof(kbuf) - 1); unsigned coords[2]; - int res; bool result; coords[0] = 0; @@ -1087,6 +1098,7 @@ decode_xterm_1005(SCREEN *sp, MEVENT * eventp) _nc_globals.read_thread = pthread_self(); # endif for (grabbed = 0; grabbed < limit;) { + int res; res = (int) read( #if USE_EMX_MOUSE @@ -1097,18 +1109,21 @@ decode_xterm_1005(SCREEN *sp, MEVENT * eventp) kbuf + grabbed, 1); if (res == -1) break; - grabbed += res; + grabbed += (size_t) res; if (grabbed > 1) { size_t check = 1; int n; - int rc; + for (n = 0; n < 2; ++n) { + int rc; + if (check >= grabbed) break; - rc = _nc_conv_to_utf32(&coords[n], kbuf + check, grabbed - check); + rc = _nc_conv_to_utf32(&coords[n], kbuf + check, (unsigned) + (grabbed - check)); if (!rc) break; - check += rc; + check += (size_t) rc; } if (n >= 2) break; @@ -1127,8 +1142,8 @@ decode_xterm_1005(SCREEN *sp, MEVENT * eventp) result = decode_X10_bstate(sp, eventp, UChar(kbuf[0])); - eventp->x = (coords[0] - ' ') - 1; - eventp->y = (coords[1] - ' ') - 1; + eventp->x = (int) (coords[0] - ' ') - 1; + eventp->y = (int) (coords[1] - ' ') - 1; return result; } @@ -1155,7 +1170,6 @@ read_SGR(SCREEN *sp, SGR_DATA * result) { char kbuf[80]; /* bigger than any possible mouse response */ int grabbed = 0; - int res; int ch = 0; int now = -1; int marker = 1; @@ -1167,7 +1181,10 @@ read_SGR(SCREEN *sp, SGR_DATA * result) # endif _nc_globals.read_thread = pthread_self(); # endif + do { + int res; + res = (int) read( #if USE_EMX_MOUSE (M_FD(sp) >= 0) ? M_FD(sp) : sp->_ifd, @@ -1177,7 +1194,7 @@ read_SGR(SCREEN *sp, SGR_DATA * result) kbuf + grabbed, 1); if (res == -1) break; - if ((grabbed + 3) >= (int) sizeof(kbuf)) { + if ((grabbed + MAX_KBUF) >= (int) sizeof(kbuf)) { result->nerror++; break; } @@ -1247,7 +1264,6 @@ decode_xterm_SGR1006(SCREEN *sp, MEVENT * eventp) int b = data.params[0]; int b3 = 1 + (b & 3); - result = TRUE; eventp->id = NORMAL_EVENT; if (data.final == 'M') { (void) handle_wheel(sp, eventp, b, (b & 64) == 64); @@ -1738,11 +1754,14 @@ NCURSES_EXPORT(int) NCURSES_SP_NAME(getmouse) (NCURSES_SP_DCLx MEVENT * aevent) { int result = ERR; + MEVENT *eventp; T((T_CALLED("getmouse(%p,%p)"), (void *) SP_PARM, (void *) aevent)); - if ((aevent != 0) && (SP_PARM != 0) && (SP_PARM->_mouse_type != M_NONE)) { - MEVENT *eventp = SP_PARM->_mouse_eventp; + if ((aevent != 0) && + (SP_PARM != 0) && + (SP_PARM->_mouse_type != M_NONE) && + (eventp = SP_PARM->_mouse_eventp) != 0) { /* compute the current-event pointer */ MEVENT *prev = PREV(eventp); @@ -1791,11 +1810,13 @@ NCURSES_EXPORT(int) NCURSES_SP_NAME(ungetmouse) (NCURSES_SP_DCLx MEVENT * aevent) { int result = ERR; + MEVENT *eventp; T((T_CALLED("ungetmouse(%p,%p)"), (void *) SP_PARM, (void *) aevent)); - if (aevent != 0 && SP_PARM != 0) { - MEVENT *eventp = SP_PARM->_mouse_eventp; + if (aevent != 0 && + SP_PARM != 0 && + (eventp = SP_PARM->_mouse_eventp) != 0) { /* stick the given event in the next-free slot */ *eventp = *aevent; @@ -1823,7 +1844,6 @@ NCURSES_SP_NAME(mousemask) (NCURSES_SP_DCLx mmask_t newmask, mmask_t * oldmask) /* set the mouse event mask */ { mmask_t result = 0; - int b; T((T_CALLED("mousemask(%p,%#lx,%p)"), (void *) SP_PARM, @@ -1836,7 +1856,10 @@ NCURSES_SP_NAME(mousemask) (NCURSES_SP_DCLx mmask_t newmask, mmask_t * oldmask) if (newmask || SP_PARM->_mouse_initialized) { _nc_mouse_init(SP_PARM); + if (SP_PARM->_mouse_type != M_NONE) { + int b; + result = newmask & (REPORT_MOUSE_POSITION | BUTTON_ALT