]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/lib_mouse.c
ncurses 6.0 - patch 20171118
[ncurses.git] / ncurses / base / lib_mouse.c
index c2cbf211a2e28309b91e9f13e30abcbce562b240..8a0e4c1f32320e7fd6eed52aa7785b847ca2b641 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2015,2016 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.169 2016/05/28 23:11:26 tom Exp $")
+MODULE_ID("$Id: lib_mouse.c,v 1.176 2017/11/18 22:12:06 Vassili.Courzakis Exp $")
 
 #include <tic.h>
 
@@ -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;
@@ -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];
 
@@ -711,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);
     }
@@ -1023,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;
@@ -1034,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(
@@ -1043,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));
@@ -1186,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;
        }
@@ -1746,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);
 
@@ -1799,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;