]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_mouse.c
ncurses 6.4 - patch 20240420
[ncurses.git] / ncurses / base / lib_mouse.c
1 /****************************************************************************
2  * Copyright 2018-2023,2024 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Thomas E. Dickey                        1996-on                 *
34  *     and: Juergen Pfeifer                         2008                    *
35  ****************************************************************************/
36
37 /*
38  * This module is intended to encapsulate ncurses's interface to pointing
39  * devices.
40  *
41  * The primary method used is xterm's internal mouse-tracking facility.
42  * Additional methods depend on the platform:
43  *      Alessandro Rubini's GPM server (Linux)
44  *      sysmouse (FreeBSD)
45  *      special-purpose mouse interface for OS/2 EMX.
46  *
47  * Notes for implementors of new mouse-interface methods:
48  *
49  * The code is logically split into a lower level that accepts event reports
50  * in a device-dependent format and an upper level that parses mouse gestures
51  * and filters events.  The mediating data structure is a circular queue of
52  * MEVENT structures.
53  *
54  * Functionally, the lower level's job is to pick up primitive events and
55  * put them on the circular queue.  This can happen in one of two ways:
56  * either (a) _nc_mouse_event() detects a series of incoming mouse reports
57  * and queues them, or (b) code in lib_getch.c detects the kmous prefix in
58  * the keyboard input stream and calls _nc_mouse_inline to queue up a series
59  * of adjacent mouse reports.
60  *
61  * In either case, _nc_mouse_parse() should be called after the series is
62  * accepted to parse the digested mouse reports (low-level MEVENTs) into
63  * a gesture (a high-level or composite MEVENT).
64  *
65  * Don't be too shy about adding new event types or modifiers, if you can find
66  * room for them in the 32-bit mask.  The API is written so that users get
67  * feedback on which theoretical event types they won't see when they call
68  * mousemask. There's one bit per button (the RESERVED_EVENT bit) not being
69  * used yet, and a couple of bits open at the high end.
70  */
71
72 #ifdef __EMX__
73 #  include <io.h>
74 #  define  INCL_DOS
75 #  define  INCL_VIO
76 #  define  INCL_KBD
77 #  define  INCL_MOU
78 #  define  INCL_DOSPROCESS
79 #  include <os2.h>              /* Need to include before the others */
80 #endif
81
82 #include <curses.priv.h>
83
84 #ifndef CUR
85 #define CUR SP_TERMTYPE
86 #endif
87
88 MODULE_ID("$Id: lib_mouse.c,v 1.200 2024/02/17 21:13:01 tom Exp $")
89
90 #include <tic.h>
91
92 #if USE_GPM_SUPPORT
93 #include <linux/keyboard.h>     /* defines KG_* macros */
94
95 #ifdef HAVE_LIBDL
96 /* use dynamic loader to avoid linkage dependency */
97 #include <dlfcn.h>
98
99 #ifdef RTLD_NOW
100 #define my_RTLD RTLD_NOW
101 #else
102 #ifdef RTLD_LAZY
103 #define my_RTLD RTLD_LAZY
104 #else
105 make an error
106 #endif
107 #endif                          /* RTLD_NOW */
108 #endif                          /* HAVE_LIBDL */
109
110 #endif                          /* USE_GPM_SUPPORT */
111
112 #if USE_SYSMOUSE
113 #undef buttons                  /* symbol conflict in consio.h */
114 #undef mouse_info               /* symbol conflict in consio.h */
115 #include <osreldate.h>
116 #if defined(__DragonFly_version) || (defined(__FreeBSD__) && (__FreeBSD_version >= 400017))
117 #include <sys/consio.h>
118 #include <sys/fbio.h>
119 #else
120 #include <machine/console.h>
121 #endif
122 #endif                          /* use_SYSMOUSE */
123
124 #if USE_KLIBC_MOUSE
125 #include <sys/socket.h>
126 #define pipe(handles) socketpair(AF_LOCAL, SOCK_STREAM, 0, handles)
127 #define DosWrite(hfile, pbuffer, cbwrite, pcbactual) \
128                 write(hfile, pbuffer, cbwrite)
129 #define DosExit(action, result )        /* do nothing */
130 #define DosCreateThread(ptid, pfn, param, flag, cbStack) \
131                 (*(ptid) = _beginthread(pfn, NULL, cbStack, \
132                                         (void *)param), (*(ptid) == -1))
133 #endif
134
135 #define MY_TRACE TRACE_ICALLS|TRACE_IEVENT
136
137 #define MASK_RELEASE(x)         (mmask_t) NCURSES_MOUSE_MASK(x, 001)
138 #define MASK_PRESS(x)           (mmask_t) NCURSES_MOUSE_MASK(x, 002)
139 #define MASK_CLICK(x)           (mmask_t) NCURSES_MOUSE_MASK(x, 004)
140 #define MASK_DOUBLE_CLICK(x)    (mmask_t) NCURSES_MOUSE_MASK(x, 010)
141 #define MASK_TRIPLE_CLICK(x)    (mmask_t) NCURSES_MOUSE_MASK(x, 020)
142 #define MASK_RESERVED_EVENT(x)  (mmask_t) NCURSES_MOUSE_MASK(x, 040)
143
144 #if NCURSES_MOUSE_VERSION == 1
145
146 #define BUTTON_CLICKED        (BUTTON1_CLICKED        | BUTTON2_CLICKED        | BUTTON3_CLICKED        | BUTTON4_CLICKED)
147 #define BUTTON_PRESSED        (BUTTON1_PRESSED        | BUTTON2_PRESSED        | BUTTON3_PRESSED        | BUTTON4_PRESSED)
148 #define BUTTON_RELEASED       (BUTTON1_RELEASED       | BUTTON2_RELEASED       | BUTTON3_RELEASED       | BUTTON4_RELEASED)
149 #define BUTTON_DOUBLE_CLICKED (BUTTON1_DOUBLE_CLICKED | BUTTON2_DOUBLE_CLICKED | BUTTON3_DOUBLE_CLICKED | BUTTON4_DOUBLE_CLICKED)
150 #define BUTTON_TRIPLE_CLICKED (BUTTON1_TRIPLE_CLICKED | BUTTON2_TRIPLE_CLICKED | BUTTON3_TRIPLE_CLICKED | BUTTON4_TRIPLE_CLICKED)
151
152 #define MAX_BUTTONS  4
153
154 #else
155
156 #define BUTTON_CLICKED        (BUTTON1_CLICKED        | BUTTON2_CLICKED        | BUTTON3_CLICKED        | BUTTON4_CLICKED        | BUTTON5_CLICKED)
157 #define BUTTON_PRESSED        (BUTTON1_PRESSED        | BUTTON2_PRESSED        | BUTTON3_PRESSED        | BUTTON4_PRESSED        | BUTTON5_PRESSED)
158 #define BUTTON_RELEASED       (BUTTON1_RELEASED       | BUTTON2_RELEASED       | BUTTON3_RELEASED       | BUTTON4_RELEASED       | BUTTON5_RELEASED)
159 #define BUTTON_DOUBLE_CLICKED (BUTTON1_DOUBLE_CLICKED | BUTTON2_DOUBLE_CLICKED | BUTTON3_DOUBLE_CLICKED | BUTTON4_DOUBLE_CLICKED | BUTTON5_DOUBLE_CLICKED)
160 #define BUTTON_TRIPLE_CLICKED (BUTTON1_TRIPLE_CLICKED | BUTTON2_TRIPLE_CLICKED | BUTTON3_TRIPLE_CLICKED | BUTTON4_TRIPLE_CLICKED | BUTTON5_TRIPLE_CLICKED)
161
162 #if NCURSES_MOUSE_VERSION == 2
163 #define MAX_BUTTONS  5
164 #else
165 #define MAX_BUTTONS  11
166 #endif
167
168 #endif
169
170 #define INVALID_EVENT   -1
171 #define NORMAL_EVENT    0
172
173 #define ValidEvent(ep) ((ep)->id != INVALID_EVENT)
174 #define Invalidate(ep) (ep)->id = INVALID_EVENT
175
176 #if USE_GPM_SUPPORT
177
178 #ifndef LIBGPM_SONAME
179 #define LIBGPM_SONAME "libgpm.so"
180 #endif
181
182 #define GET_DLSYM(name) (my_##name = (TYPE_##name) dlsym(sp->_dlopen_gpm, #name))
183
184 #endif                          /* USE_GPM_SUPPORT */
185
186 static bool _nc_mouse_parse(SCREEN *, int);
187 static void _nc_mouse_resume(SCREEN *);
188 static void _nc_mouse_wrap(SCREEN *);
189
190 /* maintain a circular list of mouse events */
191
192 #define FirstEV(sp)     ((sp)->_mouse_events)
193 #define LastEV(sp)      ((sp)->_mouse_events + EV_MAX - 1)
194
195 #undef  NEXT
196 #define NEXT(ep)        ((ep >= LastEV(SP_PARM)) \
197                          ? FirstEV(SP_PARM) \
198                          : ep + 1)
199
200 #undef  PREV
201 #define PREV(ep)        ((ep <= FirstEV(SP_PARM)) \
202                          ? LastEV(SP_PARM) \
203                          : ep - 1)
204
205 #define IndexEV(sp, ep) (ep - FirstEV(sp))
206
207 #define RunParams(sp, eventp, runp) \
208                 (long) IndexEV(sp, runp), \
209                 (long) (IndexEV(sp, eventp) + (EV_MAX - 1)) % EV_MAX
210
211 #ifdef TRACE
212 static void
213 _trace_slot(SCREEN *sp, const char *tag)
214 {
215     MEVENT *ep;
216
217     _tracef("%s", tag);
218
219     for (ep = FirstEV(sp); ep <= LastEV(sp); ep++)
220         _tracef("mouse event queue slot %ld = %s",
221                 (long) IndexEV(sp, ep),
222                 _nc_tracemouse(sp, ep));
223 }
224 #endif
225
226 #if USE_EMX_MOUSE
227
228 #  define TOP_ROW          0
229 #  define LEFT_COL         0
230
231 #  define M_FD(sp) sp->_mouse_fd
232
233 static void
234 write_event(SCREEN *sp, int down, int button, int x, int y)
235 {
236     char buf[6];
237     unsigned long ignore;
238
239     _nc_STRCPY(buf, "\033[M", sizeof(buf));     /* should be the same as key_mouse */
240     buf[3] = ' ' + (button - 1) + (down ? 0 : 0x40);
241     buf[4] = ' ' + x - LEFT_COL + 1;
242     buf[5] = ' ' + y - TOP_ROW + 1;
243     DosWrite(sp->_emxmouse_wfd, buf, 6, &ignore);
244 }
245
246 static void
247 #if USE_KLIBC_MOUSE
248 mouse_server(void *param)
249 #else
250 mouse_server(unsigned long param)
251 #endif
252 {
253     SCREEN *sp = (SCREEN *) param;
254     unsigned short fWait = MOU_WAIT;
255     /* NOPTRRECT mourt = { 0,0,24,79 }; */
256     MOUEVENTINFO mouev;
257     HMOU hmou;
258     unsigned short mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;
259     int nbuttons = 3;
260     int oldstate = 0;
261     char err[80];
262     unsigned long rc;
263
264     /* open the handle for the mouse */
265     if (MouOpen(NULL, &hmou) == 0) {
266         rc = MouSetEventMask(&mask, hmou);
267         if (rc) {               /* retry with 2 buttons */
268             mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN;
269             rc = MouSetEventMask(&mask, hmou);
270             nbuttons = 2;
271         }
272         if (rc == 0 && MouDrawPtr(hmou) == 0) {
273             for (;;) {
274                 /* sit and wait on the event queue */
275                 rc = MouReadEventQue(&mouev, &fWait, hmou);
276                 if (rc) {
277                     _nc_SPRINTF(err, _nc_SLIMIT(sizeof(err))
278                                 "Error reading mouse queue, rc=%lu.\r\n", rc);
279                     break;
280                 }
281                 if (!sp->_emxmouse_activated)
282                     goto finish;
283
284                 /*
285                  * OS/2 numbers a 3-button mouse inconsistently from other
286                  * platforms:
287                  *      1 = left
288                  *      2 = right
289                  *      3 = middle.
290                  */
291                 if ((mouev.fs ^ oldstate) & MOUSE_BN1_DOWN)
292                     write_event(sp, mouev.fs & MOUSE_BN1_DOWN,
293                                 sp->_emxmouse_buttons[1], mouev.col, mouev.row);
294                 if ((mouev.fs ^ oldstate) & MOUSE_BN2_DOWN)
295                     write_event(sp, mouev.fs & MOUSE_BN2_DOWN,
296                                 sp->_emxmouse_buttons[3], mouev.col, mouev.row);
297                 if ((mouev.fs ^ oldstate) & MOUSE_BN3_DOWN)
298                     write_event(sp, mouev.fs & MOUSE_BN3_DOWN,
299                                 sp->_emxmouse_buttons[2], mouev.col, mouev.row);
300
301               finish:
302                 oldstate = mouev.fs;
303             }
304         } else {
305             _nc_SPRINTF(err, _nc_SLIMIT(sizeof(err))
306                         "Error setting event mask, buttons=%d, rc=%lu.\r\n",
307                         nbuttons, rc);
308         }
309
310         DosWrite(2, err, strlen(err), &rc);
311         MouClose(hmou);
312     }
313     DosExit(EXIT_THREAD, 0L);
314 }
315
316 #endif /* USE_EMX_MOUSE */
317
318 #if USE_SYSMOUSE
319 static void
320 sysmouse_server(SCREEN *sp)
321 {
322     struct mouse_info the_mouse;
323     MEVENT *work;
324
325     the_mouse.operation = MOUSE_GETINFO;
326     if (sp != 0
327         && sp->_mouse_fd >= 0
328         && sp->_sysmouse_tail < FIFO_SIZE
329         && ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
330
331         if (sp->_sysmouse_head > sp->_sysmouse_tail) {
332             sp->_sysmouse_tail = 0;
333             sp->_sysmouse_head = 0;
334         }
335         work = &(sp->_sysmouse_fifo[sp->_sysmouse_tail]);
336         memset(work, 0, sizeof(*work));
337         work->id = NORMAL_EVENT;        /* there's only one mouse... */
338
339         sp->_sysmouse_old_buttons = sp->_sysmouse_new_buttons;
340         sp->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
341
342         if (sp->_sysmouse_new_buttons) {
343             if (sp->_sysmouse_new_buttons & 1)
344                 work->bstate |= BUTTON1_PRESSED;
345             if (sp->_sysmouse_new_buttons & 2)
346                 work->bstate |= BUTTON2_PRESSED;
347             if (sp->_sysmouse_new_buttons & 4)
348                 work->bstate |= BUTTON3_PRESSED;
349         } else {
350             if (sp->_sysmouse_old_buttons & 1)
351                 work->bstate |= BUTTON1_RELEASED;
352             if (sp->_sysmouse_old_buttons & 2)
353                 work->bstate |= BUTTON2_RELEASED;
354             if (sp->_sysmouse_old_buttons & 4)
355                 work->bstate |= BUTTON3_RELEASED;
356         }
357
358         /* for cosmetic bug in syscons.c on FreeBSD 3.[34] */
359         the_mouse.operation = MOUSE_HIDE;
360         ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
361         the_mouse.operation = MOUSE_SHOW;
362         ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
363
364         /*
365          * We're only interested if the button is pressed or released.
366          * FIXME: implement continuous event-tracking.
367          */
368         if (sp->_sysmouse_new_buttons != sp->_sysmouse_old_buttons) {
369             sp->_sysmouse_tail += 1;
370         }
371         work->x = the_mouse.u.data.x / sp->_sysmouse_char_width;
372         work->y = the_mouse.u.data.y / sp->_sysmouse_char_height;
373     }
374 }
375
376 static void
377 handle_sysmouse(int sig GCC_UNUSED)
378 {
379     sysmouse_server(CURRENT_SCREEN);
380 }
381 #endif /* USE_SYSMOUSE */
382
383 #if !defined(USE_TERM_DRIVER) || defined(EXP_WIN32_DRIVER)
384 #define xterm_kmous "\033[M"
385
386 static void
387 init_xterm_mouse(SCREEN *sp)
388 {
389     sp->_mouse_type = M_XTERM;
390     sp->_mouse_format = MF_X10;
391     sp->_mouse_xtermcap = tigetstr("XM");
392     if (VALID_STRING(sp->_mouse_xtermcap)) {
393         char *code = strstr(sp->_mouse_xtermcap, "[?");
394         if (code != 0) {
395             code += 2;
396             while ((*code >= '0') && (*code <= '9')) {
397                 char *next = code;
398                 while ((*next >= '0') && (*next <= '9')) {
399                     ++next;
400                 }
401                 if (!strncmp(code, "1006", (size_t) (next - code))) {
402                     sp->_mouse_format = MF_SGR1006;
403                 }
404 #ifdef EXP_XTERM_1005
405                 if (!strncmp(code, "1005", (size_t) (next - code))) {
406                     sp->_mouse_format = MF_XTERM_1005;
407                 }
408 #endif
409                 if (*next == ';') {
410                     while (*next == ';') {
411                         ++next;
412                     }
413                     code = next;
414                 } else {
415                     break;
416                 }
417             }
418         }
419     } else {
420         int code = tigetnum("XM");
421         switch (code) {
422 #ifdef EXP_XTERM_1005
423         case 1005:
424             /* see "xterm+sm+1005" */
425             sp->_mouse_xtermcap = "\033[?1005;1000%?%p1%{1}%=%th%el%;";
426             sp->_mouse_format = MF_XTERM_1005;
427             break;
428 #endif
429         case 1006:
430             /* see "xterm+sm+1006" */
431             sp->_mouse_xtermcap = "\033[?1006;1000%?%p1%{1}%=%th%el%;";
432             sp->_mouse_format = MF_SGR1006;
433             break;
434         default:
435             sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
436             break;
437         }
438     }
439 }
440 #endif
441
442 static void
443 enable_xterm_mouse(SCREEN *sp, int enable)
444 {
445     TPUTS_TRACE(enable
446                 ? "xterm mouse initialization"
447                 : "xterm mouse deinitialization");
448 #if USE_EMX_MOUSE
449     sp->_emxmouse_activated = enable;
450 #else
451     NCURSES_PUTP2("xterm-mouse", TIPARM_1(sp->_mouse_xtermcap, enable));
452 #endif
453     sp->_mouse_active = enable;
454 }
455
456 #if defined(USE_TERM_DRIVER)
457 static void
458 enable_win32_mouse(SCREEN *sp, int enable)
459 {
460 #if defined(EXP_WIN32_DRIVER)
461     enable_xterm_mouse(sp, enable);
462 #else
463     sp->_mouse_active = enable;
464 #endif
465 }
466 #endif
467
468 #if USE_GPM_SUPPORT
469 static bool
470 allow_gpm_mouse(SCREEN *sp GCC_UNUSED)
471 {
472     bool result = FALSE;
473
474 #if USE_WEAK_SYMBOLS
475     /* Danger Robinson: do not use dlopen for libgpm if already loaded */
476     if ((Gpm_Wgetch) != 0) {
477         if (!sp->_mouse_gpm_loaded) {
478             T(("GPM library was already dlopen'd, not by us"));
479         }
480     } else
481 #endif
482         /* GPM does printf's without checking if stdout is a terminal */
483     if (NC_ISATTY(fileno(stdout))) {
484         const char *list = getenv("NCURSES_GPM_TERMS");
485         const char *env = getenv("TERM");
486         if (list != 0) {
487             if (env != 0) {
488                 result = _nc_name_match(list, env, "|:");
489             }
490         } else {
491             /* GPM checks the beginning of the $TERM variable to decide if it
492              * should pass xterm events through.  There is no real advantage in
493              * allowing GPM to do this.  Recent versions relax that check, and
494              * pretend that GPM can work with any terminal having the kmous
495              * capability.  Perhaps that works for someone.  If so, they can
496              * set the environment variable (above).
497              */
498             if (env != 0 && strstr(env, "linux") != 0) {
499                 result = TRUE;
500             }
501         }
502     }
503     return result;
504 }
505
506 #ifdef HAVE_LIBDL
507 static void
508 unload_gpm_library(SCREEN *sp)
509 {
510     if (sp->_dlopen_gpm != 0) {
511         T(("unload GPM library"));
512         sp->_mouse_gpm_loaded = FALSE;
513         sp->_mouse_fd = -1;
514     }
515 }
516
517 static void
518 load_gpm_library(SCREEN *sp)
519 {
520     sp->_mouse_gpm_found = FALSE;
521
522     /*
523      * If we already had a successful dlopen, reuse it.
524      */
525     if (sp->_dlopen_gpm != 0) {
526         sp->_mouse_gpm_found = TRUE;
527         sp->_mouse_gpm_loaded = TRUE;
528     } else if ((sp->_dlopen_gpm = dlopen(LIBGPM_SONAME, my_RTLD)) != 0) {
529 #if (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__)
530 #pragma GCC diagnostic push
531 #pragma GCC diagnostic ignored "-Wpedantic"
532 #endif
533         if (GET_DLSYM(gpm_fd) == 0 ||
534             GET_DLSYM(Gpm_Open) == 0 ||
535             GET_DLSYM(Gpm_Close) == 0 ||
536             GET_DLSYM(Gpm_GetEvent) == 0) {
537 #if (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__)
538 #pragma GCC diagnostic pop
539 #endif
540             T(("GPM initialization failed: %s", dlerror()));
541             unload_gpm_library(sp);
542             dlclose(sp->_dlopen_gpm);
543             sp->_dlopen_gpm = 0;
544         } else {
545             sp->_mouse_gpm_found = TRUE;
546             sp->_mouse_gpm_loaded = TRUE;
547         }
548     }
549 }
550 #endif /* HAVE_LIBDL */
551
552 static bool
553 enable_gpm_mouse(SCREEN *sp, bool enable)
554 {
555     bool result;
556
557     T((T_CALLED("enable_gpm_mouse(%d)"), enable));
558
559     if (enable && !sp->_mouse_active) {
560 #ifdef HAVE_LIBDL
561         if (sp->_mouse_gpm_found && !sp->_mouse_gpm_loaded) {
562             load_gpm_library(sp);
563         }
564 #endif
565         if (sp->_mouse_gpm_loaded) {
566             int code;
567
568             /* GPM: initialize connection to gpm server */
569             sp->_mouse_gpm_connect.eventMask = GPM_DOWN | GPM_UP;
570             sp->_mouse_gpm_connect.defaultMask =
571                 (unsigned short) (~(sp->_mouse_gpm_connect.eventMask | GPM_HARD));
572             sp->_mouse_gpm_connect.minMod = 0;
573             sp->_mouse_gpm_connect.maxMod =
574                 (unsigned short) (~((1 << KG_SHIFT) |
575                                     (1 << KG_SHIFTL) |
576                                     (1 << KG_SHIFTR)));
577             /*
578              * Note: GPM hardcodes \E[?1001s and \E[?1000h during its open.
579              * The former is recognized by wscons (SunOS), and the latter by
580              * xterm.  Those will not show up in ncurses' traces.
581              */
582             code = my_Gpm_Open(&sp->_mouse_gpm_connect, 0);
583             result = (code >= 0);
584
585             /*
586              * GPM can return a -2 if it is trying to do something with xterm.
587              * Ignore that, since it conflicts with our use of stdin.
588              */
589             if (code == -2) {
590                 my_Gpm_Close();
591             }
592         } else {
593             result = FALSE;
594         }
595         sp->_mouse_active = result;
596         T(("GPM open %s", result ? "succeeded" : "failed"));
597     } else {
598         if (!enable && sp->_mouse_active) {
599             /* GPM: close connection to gpm server */
600             my_Gpm_Close();
601             sp->_mouse_active = FALSE;
602             T(("GPM closed"));
603         }
604         result = enable;
605     }
606 #ifdef HAVE_LIBDL
607     if (!result) {
608         unload_gpm_library(sp);
609     }
610 #endif
611     returnBool(result);
612 }
613 #endif /* USE_GPM_SUPPORT */
614
615 static void
616 initialize_mousetype(SCREEN *sp)
617 {
618     T((T_CALLED("initialize_mousetype()")));
619
620     /* Try gpm first, because gpm may be configured to run in xterm */
621 #if USE_GPM_SUPPORT
622     if (allow_gpm_mouse(sp)) {
623         if (!sp->_mouse_gpm_loaded) {
624 #ifdef HAVE_LIBDL
625             load_gpm_library(sp);
626 #else /* !HAVE_LIBDL */
627             sp->_mouse_gpm_found = TRUE;
628             sp->_mouse_gpm_loaded = TRUE;
629 #endif
630         }
631
632         /*
633          * The gpm_fd file-descriptor may be negative (xterm).  So we have to
634          * maintain our notion of whether the mouse connection is active
635          * without testing the file-descriptor.
636          */
637         if (sp->_mouse_gpm_found && enable_gpm_mouse(sp, TRUE)) {
638             sp->_mouse_type = M_GPM;
639             sp->_mouse_fd = *(my_gpm_fd);
640             T(("GPM mouse_fd %d", sp->_mouse_fd));
641             returnVoid;
642         }
643     }
644 #endif /* USE_GPM_SUPPORT */
645
646     /* OS/2 VIO */
647 #if USE_EMX_MOUSE
648     if (!sp->_emxmouse_thread
649         && strstr(SP_TERMTYPE term_names, "xterm") == 0
650         && NonEmpty(key_mouse)) {
651         int handles[2];
652
653         if (pipe(handles) < 0) {
654             perror("mouse pipe error");
655             returnVoid;
656         } else {
657             int rc;
658
659             if (!sp->_emxmouse_buttons[0]) {
660                 const char *s = getenv("MOUSE_BUTTONS_123");
661
662                 sp->_emxmouse_buttons[0] = 1;
663                 if (s && strlen(s) >= 3) {
664                     sp->_emxmouse_buttons[1] = s[0] - '0';
665                     sp->_emxmouse_buttons[2] = s[1] - '0';
666                     sp->_emxmouse_buttons[3] = s[2] - '0';
667                 } else {
668                     sp->_emxmouse_buttons[1] = 1;
669                     sp->_emxmouse_buttons[2] = 3;
670                     sp->_emxmouse_buttons[3] = 2;
671                 }
672             }
673             sp->_emxmouse_wfd = handles[1];
674             M_FD(sp) = handles[0];
675             /* Needed? */
676             setmode(handles[0], O_BINARY);
677             setmode(handles[1], O_BINARY);
678             /* Do not use CRT functions, we may single-threaded. */
679             rc = DosCreateThread((unsigned long *) &sp->_emxmouse_thread,
680                                  mouse_server, (long) sp, 0, 8192);
681             if (rc) {
682                 printf("mouse thread error %d=%#x", rc, rc);
683             } else {
684                 sp->_mouse_type = M_XTERM;
685             }
686             returnVoid;
687         }
688     }
689 #endif /* USE_EMX_MOUSE */
690
691 #if USE_SYSMOUSE
692     {
693         static char dev_tty[] = "/dev/tty";
694         struct mouse_info the_mouse;
695         char *the_device = 0;
696
697         if (NC_ISATTY(sp->_ifd))
698             the_device = ttyname(sp->_ifd);
699         if (the_device == 0)
700             the_device = dev_tty;
701
702         sp->_mouse_fd = open(the_device, O_RDWR);
703
704         if (sp->_mouse_fd >= 0) {
705             /*
706              * sysmouse does not have a usable user interface for obtaining
707              * mouse events.  The logical way to proceed (reading data on a
708              * stream) only works if one opens the device as root.  Even in
709              * that mode, careful examination shows we lose events
710              * occasionally.  The interface provided for user programs is to
711              * establish a signal handler.  really.
712              *
713              * Take over SIGUSR2 for this purpose since SIGUSR1 is more
714              * likely to be used by an application.  getch() will have to
715              * handle the misleading EINTR's.
716              */
717             signal(SIGUSR2, SIG_IGN);
718             the_mouse.operation = MOUSE_MODE;
719             the_mouse.u.mode.mode = 0;
720             the_mouse.u.mode.signal = SIGUSR2;
721             if (ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
722                 signal(SIGUSR2, handle_sysmouse);
723                 the_mouse.operation = MOUSE_SHOW;
724                 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
725
726 #if defined(FBIO_MODEINFO) || defined(CONS_MODEINFO)    /* FreeBSD > 2.x */
727                 {
728 #ifndef FBIO_GETMODE            /* FreeBSD 3.x */
729 #define FBIO_GETMODE    CONS_GET
730 #define FBIO_MODEINFO   CONS_MODEINFO
731 #endif /* FBIO_GETMODE */
732                     video_info_t the_video;
733
734                     if (ioctl(sp->_mouse_fd,
735                               FBIO_GETMODE,
736                               &the_video.vi_mode) != -1
737                         && ioctl(sp->_mouse_fd,
738                                  FBIO_MODEINFO,
739                                  &the_video) != -1) {
740                         sp->_sysmouse_char_width = the_video.vi_cwidth;
741                         sp->_sysmouse_char_height = the_video.vi_cheight;
742                     }
743                 }
744 #endif /* defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) */
745
746                 if (sp->_sysmouse_char_width <= 0)
747                     sp->_sysmouse_char_width = 8;
748                 if (sp->_sysmouse_char_height <= 0)
749                     sp->_sysmouse_char_height = 16;
750                 sp->_mouse_type = M_SYSMOUSE;
751                 returnVoid;
752             }
753         }
754     }
755 #endif /* USE_SYSMOUSE */
756
757 #ifdef USE_TERM_DRIVER
758     CallDriver(sp, td_initmouse);
759 #endif
760 #if !defined(USE_TERM_DRIVER) || defined(EXP_WIN32_DRIVER)
761     /* we know how to recognize mouse events under "xterm" */
762     if (NonEmpty(key_mouse)) {
763         init_xterm_mouse(sp);
764     } else if (strstr(SP_TERMTYPE term_names, "xterm") != 0) {
765         if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
766             init_xterm_mouse(sp);
767     }
768 #endif
769
770     returnVoid;
771 }
772
773 static bool
774 _nc_mouse_init(SCREEN *sp)
775 /* initialize the mouse */
776 {
777     bool result = FALSE;
778
779     T((T_CALLED("_nc_mouse_init(%p)"), (void *) sp));
780
781     if (sp != 0) {
782         if (!sp->_mouse_initialized) {
783             int i;
784
785             sp->_mouse_initialized = TRUE;
786
787             TR(MY_TRACE, ("set _mouse_initialized"));
788
789             sp->_mouse_eventp = FirstEV(sp);
790             for (i = 0; i < EV_MAX; i++)
791                 Invalidate(sp->_mouse_events + i);
792
793             initialize_mousetype(sp);
794
795             T(("set _mouse_type to %d", sp->_mouse_type));
796         }
797         result = sp->_mouse_initialized;
798     }
799     returnCode(result);
800 }
801
802 /*
803  * Query to see if there is a pending mouse event.  This is called from
804  * fifo_push() in lib_getch.c
805  */
806 static bool
807 _nc_mouse_event(SCREEN *sp)
808 {
809     MEVENT *eventp = sp->_mouse_eventp;
810     bool result = FALSE;
811
812     (void) eventp;
813
814     switch (sp->_mouse_type) {
815     case M_XTERM:
816         /* xterm: never have to query, mouse events are in the keyboard stream */
817 #if USE_EMX_MOUSE
818         {
819             char kbuf[3];
820
821             int i, res = read(M_FD(sp), &kbuf, 3);      /* Eat the prefix */
822             if (res != 3)
823                 printf("Got %d chars instead of 3 for prefix.\n", res);
824             for (i = 0; i < res; i++) {
825                 if (kbuf[i] != key_mouse[i])
826                     printf("Got char %d instead of %d for prefix.\n",
827                            (int) kbuf[i], (int) key_mouse[i]);
828             }
829             result = TRUE;
830         }
831 #endif /* USE_EMX_MOUSE */
832         break;
833
834 #if USE_GPM_SUPPORT
835     case M_GPM:
836         if (sp->_mouse_fd >= 0) {
837             /* query server for event, return TRUE if we find one */
838             Gpm_Event ev;
839
840             switch (my_Gpm_GetEvent(&ev)) {
841             case 0:
842                 /* Connection closed, drop the mouse. */
843                 sp->_mouse_fd = -1;
844                 break;
845             case 1:
846                 /* there's only one mouse... */
847                 eventp->id = NORMAL_EVENT;
848
849                 eventp->bstate = 0;
850                 switch (ev.type & 0x0f) {
851                 case (GPM_DOWN):
852                     if (ev.buttons & GPM_B_LEFT)
853                         eventp->bstate |= BUTTON1_PRESSED;
854                     if (ev.buttons & GPM_B_MIDDLE)
855                         eventp->bstate |= BUTTON2_PRESSED;
856                     if (ev.buttons & GPM_B_RIGHT)
857                         eventp->bstate |= BUTTON3_PRESSED;
858                     break;
859                 case (GPM_UP):
860                     if (ev.buttons & GPM_B_LEFT)
861                         eventp->bstate |= BUTTON1_RELEASED;
862                     if (ev.buttons & GPM_B_MIDDLE)
863                         eventp->bstate |= BUTTON2_RELEASED;
864                     if (ev.buttons & GPM_B_RIGHT)
865                         eventp->bstate |= BUTTON3_RELEASED;
866                     break;
867                 default:
868                     eventp->bstate |= REPORT_MOUSE_POSITION;
869                     break;
870                 }
871
872                 eventp->x = ev.x - 1;
873                 eventp->y = ev.y - 1;
874                 eventp->z = 0;
875
876                 /* bump the next-free pointer into the circular list */
877                 sp->_mouse_eventp = NEXT(eventp);
878                 result = TRUE;
879                 break;
880             }
881         }
882         break;
883 #endif
884
885 #if USE_SYSMOUSE
886     case M_SYSMOUSE:
887         if (sp->_sysmouse_head < sp->_sysmouse_tail) {
888             *eventp = sp->_sysmouse_fifo[sp->_sysmouse_head];
889
890             /*
891              * Point the fifo-head to the next possible location.  If there
892              * are none, reset the indices.  This may be interrupted by the
893              * signal handler, doing essentially the same reset.
894              */
895             sp->_sysmouse_head += 1;
896             if (sp->_sysmouse_head == sp->_sysmouse_tail) {
897                 sp->_sysmouse_tail = 0;
898                 sp->_sysmouse_head = 0;
899             }
900
901             /* bump the next-free pointer into the circular list */
902             sp->_mouse_eventp = eventp = NEXT(eventp);
903             result = TRUE;
904         }
905         break;
906 #endif /* USE_SYSMOUSE */
907
908 #ifdef USE_TERM_DRIVER
909     case M_TERM_DRIVER:
910         while (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
911             *eventp = sp->_drv_mouse_fifo[sp->_drv_mouse_head];
912
913             /*
914              * Point the fifo-head to the next possible location.  If there
915              * are none, reset the indices.
916              */
917             sp->_drv_mouse_head += 1;
918             if (sp->_drv_mouse_head == sp->_drv_mouse_tail) {
919                 sp->_drv_mouse_tail = 0;
920                 sp->_drv_mouse_head = 0;
921             }
922
923             /* bump the next-free pointer into the circular list */
924             sp->_mouse_eventp = eventp = NEXT(eventp);
925             result = TRUE;
926         }
927         break;
928 #endif
929
930     case M_NONE:
931         break;
932     }
933
934     return result;              /* true if we found an event */
935 }
936
937 #if USE_EMX_MOUSE
938 #define PRESS_POSITION(n) \
939     do { \
940             eventp->bstate = MASK_PRESS(n); \
941             sp->_mouse_bstate |= MASK_PRESS(n); \
942             if (button & 0x40) { \
943                     eventp->bstate = MASK_RELEASE(n); \
944                     sp->_mouse_bstate &= ~MASK_PRESS(n); \
945             } \
946     } while (0)
947 #else
948 #define PRESS_POSITION(n) \
949     do { \
950             eventp->bstate = (mmask_t) ((sp->_mouse_bstate & MASK_PRESS(n)) \
951                                     ? REPORT_MOUSE_POSITION \
952                                     : MASK_PRESS(n)); \
953             sp->_mouse_bstate |= MASK_PRESS(n); \
954     } while (0)
955 #endif
956
957 static bool
958 handle_wheel(SCREEN *sp, MEVENT * eventp, int button, int wheel)
959 {
960     bool result = TRUE;
961
962     switch (button & 3) {
963     case 0:
964         if (wheel) {
965             eventp->bstate = MASK_PRESS(4);
966             /* Do not record in sp->_mouse_bstate; there will be no
967              * corresponding release event.
968              */
969         } else {
970             PRESS_POSITION(1);
971         }
972         break;
973     case 1:
974         if (wheel) {
975 #if NCURSES_MOUSE_VERSION >= 2
976             eventp->bstate = MASK_PRESS(5);
977             /* See comment above for button 4 */
978 #else
979             /* Ignore this event as it is not a true press of the button */
980             eventp->bstate = REPORT_MOUSE_POSITION;
981 #endif
982         } else {
983             PRESS_POSITION(2);
984         }
985         break;
986     case 2:
987         PRESS_POSITION(3);
988         break;
989     default:
990         /*
991          * case 3 is sent when the mouse buttons are released.
992          *
993          * If the terminal uses xterm mode 1003, a continuous series of
994          * button-release events is sent as the mouse moves around the screen,
995          * or as the wheel mouse is rotated.
996          *
997          * Return false in this case, so that when running in X10 mode, we will
998          * recalculate bstate.
999          */
1000         eventp->bstate = REPORT_MOUSE_POSITION;
1001         result = FALSE;
1002         break;
1003     }
1004     return result;
1005 }
1006
1007 static bool
1008 decode_X10_bstate(SCREEN *sp, MEVENT * eventp, unsigned intro)
1009 {
1010     bool result;
1011     int button = 0;
1012     int wheel = (intro & 96) == 96;
1013
1014     eventp->bstate = 0;
1015
1016     if (intro >= 96) {
1017         if (intro >= 160) {
1018             button = (int) (intro - 152);       /* buttons 8-11 */
1019         } else {
1020             button = (int) (intro - 92);        /* buttons 4-7 */
1021         }
1022     } else {
1023         button = (intro & 3);
1024     }
1025
1026     if (button > MAX_BUTTONS) {
1027         eventp->bstate = REPORT_MOUSE_POSITION;
1028     } else if (!handle_wheel(sp, eventp, (int) intro, wheel)) {
1029
1030         /*
1031          * Release events aren't reported for individual buttons, just for
1032          * the button set as a whole.  However, because there are normally
1033          * no mouse events under xterm that intervene between press and
1034          * release, we can infer the button actually released by looking at
1035          * the previous event.
1036          */
1037         if (sp->_mouse_bstate & BUTTON_PRESSED) {
1038             int b;
1039
1040             eventp->bstate = BUTTON_RELEASED;
1041             for (b = 1; b <= MAX_BUTTONS; ++b) {
1042                 if (!(sp->_mouse_bstate & MASK_PRESS(b)))
1043                     eventp->bstate &= ~MASK_RELEASE(b);
1044             }
1045             sp->_mouse_bstate = 0;
1046         } else {
1047             /*
1048              * xterm will return a stream of release-events to let the
1049              * application know where the mouse is going, if private mode
1050              * 1002 or 1003 is enabled.
1051              */
1052             eventp->bstate = REPORT_MOUSE_POSITION;
1053         }
1054     }
1055
1056     if (intro & 4) {
1057         eventp->bstate |= BUTTON_SHIFT;
1058     }
1059     if (intro & 8) {
1060         eventp->bstate |= BUTTON_ALT;
1061     }
1062     if (intro & 16) {
1063         eventp->bstate |= BUTTON_CTRL;
1064     }
1065     result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
1066     return result;
1067 }
1068
1069 /* This code requires that your xterm entry contain the kmous capability and
1070  * that it be set to the \E[M documented in the Xterm Control Sequences
1071  * reference.  This is how we arrange for mouse events to be reported via a
1072  * KEY_MOUSE return value from wgetch().  After this value is received,
1073  * _nc_mouse_inline() gets called and is immediately responsible for parsing
1074  * the mouse status information following the prefix.
1075  *
1076  * The following quotes from the ctlseqs.ms document in the XTerm distribution,
1077  * describing the mouse tracking feature:
1078  *
1079  * Parameters for all mouse tracking escape sequences generated by xterm encode
1080  * numeric parameters in a single character as value+040.  For example, ! is
1081  * 1.
1082  *
1083  * On button press or release, xterm sends ESC [ M CbCxCy.  The low two bits of
1084  * Cb encode button information:  0=MB1 pressed, 1=MB2 pressed, 2=MB3 pressed,
1085  * 3=release.  The upper bits encode what modifiers were down when the button
1086  * was pressed and are added together.  4=Shift, 8=Meta, 16=Control.  Cx and Cy
1087  * are the x and y coordinates of the mouse event.  The upper left corner is
1088  * (1,1).
1089  *
1090  * (End quote) By the time we get here, we've eaten the key prefix.  FYI, the
1091  * loop below is necessary because mouse click info isn't guaranteed to present
1092  * as a single clist item.
1093  *
1094  * Wheel mice may return buttons 4 and 5 when the wheel is turned.  We encode
1095  * those as button presses.
1096  */
1097 static bool
1098 decode_xterm_X10(SCREEN *sp, MEVENT * eventp)
1099 {
1100 #define MAX_KBUF 3
1101     unsigned char kbuf[MAX_KBUF + 1];
1102     size_t grabbed;
1103     int res;
1104     bool result;
1105
1106     _nc_set_read_thread(TRUE);
1107     for (grabbed = 0; grabbed < MAX_KBUF; grabbed += (size_t) res) {
1108
1109         /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
1110         res = (int) read(
1111 #if USE_EMX_MOUSE
1112                             (M_FD(sp) >= 0) ? M_FD(sp) : sp->_ifd,
1113 #else
1114                             sp->_ifd,
1115 #endif
1116                             kbuf + grabbed, (size_t) (MAX_KBUF - (int) grabbed));
1117         if (res == -1)
1118             break;
1119     }
1120     _nc_set_read_thread(FALSE);
1121     kbuf[MAX_KBUF] = '\0';
1122
1123     TR(TRACE_IEVENT,
1124        ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
1125
1126     /* there's only one mouse... */
1127     eventp->id = NORMAL_EVENT;
1128
1129     result = decode_X10_bstate(sp, eventp, kbuf[0]);
1130
1131     eventp->x = (kbuf[1] - ' ') - 1;
1132     eventp->y = (kbuf[2] - ' ') - 1;
1133
1134     return result;
1135 }
1136
1137 #ifdef EXP_XTERM_1005
1138 /*
1139  * This is identical to X10/X11 responses except that there are two UTF-8
1140  * characters storing the ordinates instead of two bytes.
1141  */
1142 static bool
1143 decode_xterm_1005(SCREEN *sp, MEVENT * eventp)
1144 {
1145     char kbuf[80];
1146     size_t grabbed;
1147     size_t limit = (sizeof(kbuf) - 1);
1148     unsigned coords[2];
1149     bool result;
1150
1151     coords[0] = 0;
1152     coords[1] = 0;
1153
1154     _nc_set_read_thread(TRUE);
1155     for (grabbed = 0; grabbed < limit;) {
1156         int res;
1157
1158         res = (int) read(
1159 #if USE_EMX_MOUSE
1160                             (M_FD(sp) >= 0) ? M_FD(sp) : sp->_ifd,
1161 #else
1162                             sp->_ifd,
1163 #endif
1164                             (kbuf + grabbed), (size_t) 1);
1165         if (res == -1)
1166             break;
1167         grabbed += (size_t) res;
1168         if (grabbed > 1) {
1169             size_t check = 1;
1170             int n;
1171
1172             for (n = 0; n < 2; ++n) {
1173                 int rc;
1174
1175                 if (check >= grabbed)
1176                     break;
1177                 rc = _nc_conv_to_utf32(&coords[n], kbuf + check, (unsigned)
1178                                        (grabbed - check));
1179                 if (!rc)
1180                     break;
1181                 check += (size_t) rc;
1182             }
1183             if (n >= 2)
1184                 break;
1185         }
1186     }
1187     _nc_set_read_thread(FALSE);
1188
1189     TR(TRACE_IEVENT,
1190        ("_nc_mouse_inline sees the following xterm data: %s",
1191         _nc_visbufn(kbuf, (int) grabbed)));
1192
1193     /* there's only one mouse... */
1194     eventp->id = NORMAL_EVENT;
1195
1196     result = decode_X10_bstate(sp, eventp, UChar(kbuf[0]));
1197
1198     eventp->x = (int) (coords[0] - ' ') - 1;
1199     eventp->y = (int) (coords[1] - ' ') - 1;
1200
1201     return result;
1202 }
1203 #endif /* EXP_XTERM_1005 */
1204
1205 /*
1206  * ECMA-48 section 5.4
1207  */
1208 #define isInter(c) ((c) >= 0x20 && (c) <= 0x2f)
1209 #define isParam(c) ((c) >= 0x30 && (c) <= 0x3f)
1210 #define isFinal(c) ((c) >= 0x40 && (c) <= 0x7e)
1211
1212 #define MAX_PARAMS 9
1213
1214 typedef struct {
1215     int nerror;                 /* nonzero if there are unexpected chars */
1216     int nparam;                 /* number of numeric parameters */
1217     int params[MAX_PARAMS];
1218     int final;                  /* the final-character */
1219 } SGR_DATA;
1220
1221 static bool
1222 read_SGR(SCREEN *sp, SGR_DATA * result)
1223 {
1224     char kbuf[80];              /* bigger than any possible mouse response */
1225     int grabbed = 0;
1226     int ch = 0;
1227     int now = -1;
1228     int marker = 1;
1229
1230     memset(result, 0, sizeof(*result));
1231     _nc_set_read_thread(TRUE);
1232
1233     do {
1234         int res;
1235
1236         res = (int) read(
1237 #if USE_EMX_MOUSE
1238                             (M_FD(sp) >= 0) ? M_FD(sp) : sp->_ifd,
1239 #else
1240                             sp->_ifd,
1241 #endif
1242                             (kbuf + grabbed), (size_t) 1);
1243         if (res == -1)
1244             break;
1245         if ((grabbed + MAX_KBUF) >= (int) sizeof(kbuf)) {
1246             result->nerror++;
1247             break;
1248         }
1249         ch = UChar(kbuf[grabbed]);
1250         kbuf[grabbed + 1] = 0;
1251         switch (ch) {
1252         case '0':
1253         case '1':
1254         case '2':
1255         case '3':
1256         case '4':
1257         case '5':
1258         case '6':
1259         case '7':
1260         case '8':
1261         case '9':
1262             if (marker) {
1263                 ++now;
1264                 result->nparam = (now + 1);
1265             }
1266             marker = 0;
1267             result->params[now] = (result->params[now] * 10) + (ch - '0');
1268             break;
1269         case ';':
1270             if (marker) {
1271                 ++now;
1272                 result->nparam = (now + 1);
1273             }
1274             marker = 1;
1275             break;
1276         default:
1277             if (ch < 32 || ch > 126) {
1278                 /*
1279                  * Technically other characters could be interspersed in the
1280                  * response.  Ignore those for now.
1281                  */
1282                 result->nerror++;
1283                 continue;
1284             } else if (isFinal(ch)) {
1285                 if (marker) {
1286                     result->nparam++;
1287                 }
1288                 result->final = ch;
1289             } else {
1290                 result->nerror++;
1291             }
1292             break;
1293         }
1294         ++grabbed;
1295     } while (!isFinal(ch));
1296     _nc_set_read_thread(FALSE);
1297
1298     kbuf[++grabbed] = 0;
1299     TR(TRACE_IEVENT,
1300        ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
1301     return (grabbed > 0) && (result->nerror == 0);
1302 }
1303
1304 static bool
1305 decode_xterm_SGR1006(SCREEN *sp, MEVENT * eventp)
1306 {
1307     SGR_DATA data;
1308     bool result = FALSE;
1309     if (read_SGR(sp, &data)) {
1310         int b = data.params[0];
1311         int b3 = 1 + (b & 3);
1312         int wheel = ((b & 64) == 64);
1313
1314         if (b >= 132) {
1315             b3 = MAX_BUTTONS + 1;
1316         } else if (b >= 128) {
1317             b3 = (b - 120);     /* buttons 8-11 */
1318         } else if (b >= 64) {
1319             b3 = (b - 60);      /* buttons 6-7 */
1320         }
1321
1322         eventp->id = NORMAL_EVENT;
1323         if (data.final == 'M') {
1324             (void) handle_wheel(sp, eventp, b, wheel);
1325         } else if (b3 > MAX_BUTTONS) {
1326             eventp->bstate = REPORT_MOUSE_POSITION;
1327         } else {
1328             mmask_t pressed = (mmask_t) NCURSES_MOUSE_MASK(b3, NCURSES_BUTTON_PRESSED);
1329             mmask_t release = (mmask_t) NCURSES_MOUSE_MASK(b3, NCURSES_BUTTON_RELEASED);
1330             if (sp->_mouse_bstate & pressed) {
1331                 eventp->bstate = release;
1332                 sp->_mouse_bstate &= ~pressed;
1333             } else {
1334                 eventp->bstate = REPORT_MOUSE_POSITION;
1335             }
1336         }
1337         if (b & 4) {
1338             eventp->bstate |= BUTTON_SHIFT;
1339         }
1340         if (b & 8) {
1341             eventp->bstate |= BUTTON_ALT;
1342         }
1343         if (b & 16) {
1344             eventp->bstate |= BUTTON_CTRL;
1345         }
1346         result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
1347         eventp->x = (data.params[1] ? (data.params[1] - 1) : 0);
1348         eventp->y = (data.params[2] ? (data.params[2] - 1) : 0);
1349     }
1350     return result;
1351 }
1352
1353 static bool
1354 _nc_mouse_inline(SCREEN *sp)
1355 /* mouse report received in the keyboard stream -- parse its info */
1356 {
1357     bool result = FALSE;
1358     MEVENT *eventp = sp->_mouse_eventp;
1359
1360     TR(MY_TRACE, ("_nc_mouse_inline() called"));
1361
1362     if (sp->_mouse_type == M_XTERM) {
1363         switch (sp->_mouse_format) {
1364         case MF_X10:
1365             result = decode_xterm_X10(sp, eventp);
1366             break;
1367         case MF_SGR1006:
1368             result = decode_xterm_SGR1006(sp, eventp);
1369             break;
1370 #ifdef EXP_XTERM_1005
1371         case MF_XTERM_1005:
1372             result = decode_xterm_1005(sp, eventp);
1373             break;
1374 #endif
1375         }
1376
1377         TR(MY_TRACE,
1378            ("_nc_mouse_inline: primitive mouse-event %s has slot %ld",
1379             _nc_tracemouse(sp, eventp),
1380             (long) IndexEV(sp, eventp)));
1381
1382         /* bump the next-free pointer into the circular list */
1383         sp->_mouse_eventp = NEXT(eventp);
1384
1385         if (!result) {
1386             /* If this event is from a wheel-mouse, treat it like position
1387              * reports and avoid waiting for the release-events which will
1388              * never come.
1389              */
1390             if (eventp->bstate & BUTTON_PRESSED) {
1391                 int b;
1392
1393                 for (b = 4; b <= MAX_BUTTONS; ++b) {
1394                     if ((eventp->bstate & MASK_PRESS(b))) {
1395                         result = TRUE;
1396                         break;
1397                     }
1398                 }
1399             }
1400         }
1401     }
1402
1403     return (result);
1404 }
1405
1406 static void
1407 mouse_activate(SCREEN *sp, int on)
1408 {
1409     T((T_CALLED("mouse_activate(%p,%s)"),
1410        (void *) SP_PARM, on ? "on" : "off"));
1411
1412     if (!on && !sp->_mouse_initialized)
1413         returnVoid;
1414
1415     if (!_nc_mouse_init(sp))
1416         returnVoid;
1417
1418     if (on) {
1419         sp->_mouse_bstate = 0;
1420         switch (sp->_mouse_type) {
1421         case M_XTERM:
1422 #if NCURSES_EXT_FUNCS
1423             NCURSES_SP_NAME(keyok) (NCURSES_SP_ARGx KEY_MOUSE, on);
1424 #endif
1425             enable_xterm_mouse(sp, 1);
1426             break;
1427 #if USE_GPM_SUPPORT
1428         case M_GPM:
1429             if (enable_gpm_mouse(sp, TRUE)) {
1430                 sp->_mouse_fd = *(my_gpm_fd);
1431                 T(("GPM mouse_fd %d", sp->_mouse_fd));
1432             }
1433             break;
1434 #endif
1435 #if USE_SYSMOUSE
1436         case M_SYSMOUSE:
1437             signal(SIGUSR2, handle_sysmouse);
1438             sp->_mouse_active = TRUE;
1439             break;
1440 #endif
1441 #ifdef USE_TERM_DRIVER
1442         case M_TERM_DRIVER:
1443             enable_win32_mouse(sp, TRUE);
1444             break;
1445 #endif
1446         case M_NONE:
1447             returnVoid;
1448         default:
1449             T(("unexpected mouse mode"));
1450             break;
1451         }
1452         /* Make runtime binding to cut down on object size of applications that
1453          * do not use the mouse (e.g., 'clear').
1454          */
1455         /* *INDENT-EQLS* */
1456         sp->_mouse_event  = _nc_mouse_event;
1457         sp->_mouse_inline = _nc_mouse_inline;
1458         sp->_mouse_parse  = _nc_mouse_parse;
1459         sp->_mouse_resume = _nc_mouse_resume;
1460         sp->_mouse_wrap   = _nc_mouse_wrap;
1461     } else {
1462
1463         switch (sp->_mouse_type) {
1464         case M_XTERM:
1465             enable_xterm_mouse(sp, 0);
1466             break;
1467 #if USE_GPM_SUPPORT
1468         case M_GPM:
1469             enable_gpm_mouse(sp, FALSE);
1470             break;
1471 #endif
1472 #if USE_SYSMOUSE
1473         case M_SYSMOUSE:
1474             signal(SIGUSR2, SIG_IGN);
1475             sp->_mouse_active = FALSE;
1476             break;
1477 #endif
1478 #ifdef USE_TERM_DRIVER
1479         case M_TERM_DRIVER:
1480             enable_win32_mouse(sp, FALSE);
1481             break;
1482 #endif
1483         case M_NONE:
1484             returnVoid;
1485         default:
1486             T(("unexpected mouse mode"));
1487             break;
1488         }
1489     }
1490     NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
1491     returnVoid;
1492 }
1493
1494 /**************************************************************************
1495  *
1496  * Device-independent code
1497  *
1498  **************************************************************************/
1499
1500 static bool
1501 _nc_mouse_parse(SCREEN *sp, int runcount)
1502 /* parse a run of atomic mouse events into a gesture */
1503 {
1504     MEVENT *eventp = sp->_mouse_eventp;
1505     MEVENT *next, *ep;
1506     MEVENT *first_valid = NULL;
1507     MEVENT *first_invalid = NULL;
1508     int n;
1509     int b;
1510     bool merge;
1511     bool endLoop;
1512
1513     TR(MY_TRACE, ("_nc_mouse_parse(%d) called", runcount));
1514
1515     /*
1516      * When we enter this routine, the event list next-free pointer
1517      * points just past a run of mouse events that we know were separated
1518      * in time by less than the critical click interval. The job of this
1519      * routine is to collapse this run into a single higher-level event
1520      * or gesture.
1521      *
1522      * We accomplish this in two passes.  The first pass merges press/release
1523      * pairs into click events.  The second merges runs of click events into
1524      * double or triple-click events.
1525      *
1526      * It's possible that the run may not resolve to a single event (for
1527      * example, if the user quadruple-clicks).  If so, leading events
1528      * in the run are ignored if user does not call getmouse in a loop (getting
1529      * them from newest to older).
1530      *
1531      * Note that this routine is independent of the format of the specific
1532      * format of the pointing-device's reports.  We can use it to parse
1533      * gestures on anything that reports press/release events on a per-
1534      * button basis, as long as the device-dependent mouse code puts stuff
1535      * on the queue in MEVENT format.
1536      */
1537
1538     /*
1539      * Reset all events that were not set, in case the user sometimes calls
1540      * getmouse only once and other times until there are no more events in
1541      * queue.
1542      *
1543      * This also allows reaching the beginning of the run.
1544      */
1545     ep = eventp;
1546     for (n = runcount; n < EV_MAX; n++) {
1547         Invalidate(ep);
1548         ep = NEXT(ep);
1549     }
1550
1551 #ifdef TRACE
1552     if (USE_TRACEF(TRACE_IEVENT)) {
1553         _trace_slot(sp, "before mouse press/release merge:");
1554         _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1555                 RunParams(sp, eventp, ep),
1556                 runcount);
1557         _nc_unlock_global(tracef);
1558     }
1559 #endif /* TRACE */
1560
1561     /* first pass; merge press/release pairs */
1562     endLoop = FALSE;
1563     while (!endLoop) {
1564         next = NEXT(ep);
1565         if (next == eventp) {
1566             /* Will end the loop, but compact before */
1567             endLoop = TRUE;
1568         } else {
1569
1570 #define MASK_CHANGED(x) (!(ep->bstate & MASK_PRESS(x)) \
1571                       == !(next->bstate & MASK_RELEASE(x)))
1572
1573             if (ValidEvent(ep) && ValidEvent(next)
1574                 && ep->x == next->x && ep->y == next->y
1575                 && (ep->bstate & BUTTON_PRESSED)
1576                 && (!(next->bstate & BUTTON_PRESSED))) {
1577                 bool changed = TRUE;
1578
1579                 for (b = 1; b <= MAX_BUTTONS; ++b) {
1580                     if (!MASK_CHANGED(b)) {
1581                         changed = FALSE;
1582                         break;
1583                     }
1584                 }
1585
1586                 if (changed) {
1587                     merge = FALSE;
1588                     for (b = 1; b <= MAX_BUTTONS; ++b) {
1589                         if ((sp->_mouse_mask2 & MASK_CLICK(b))
1590                             && (ep->bstate & MASK_PRESS(b))) {
1591                             next->bstate &= ~MASK_RELEASE(b);
1592                             next->bstate |= MASK_CLICK(b);
1593                             merge = TRUE;
1594                         }
1595                     }
1596                     if (merge) {
1597                         Invalidate(ep);
1598                     }
1599                 }
1600             }
1601         }
1602
1603         /* Compact valid events */
1604         if (!ValidEvent(ep)) {
1605             if ((first_valid != NULL) && (first_invalid == NULL)) {
1606                 first_invalid = ep;
1607             }
1608         } else {
1609             if (first_valid == NULL) {
1610                 first_valid = ep;
1611             } else if (first_invalid != NULL) {
1612                 *first_invalid = *ep;
1613                 Invalidate(ep);
1614                 first_invalid = NEXT(first_invalid);
1615             }
1616         }
1617
1618         ep = next;
1619     }
1620
1621     if (first_invalid != NULL) {
1622         eventp = first_invalid;
1623     }
1624 #ifdef TRACE
1625     if (USE_TRACEF(TRACE_IEVENT)) {
1626         _trace_slot(sp, "before mouse click merge:");
1627         if (first_valid == NULL) {
1628             _tracef("_nc_mouse_parse: no valid event");
1629         } else {
1630             _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1631                     RunParams(sp, eventp, first_valid),
1632                     runcount);
1633             _nc_unlock_global(tracef);
1634         }
1635     }
1636 #endif /* TRACE */
1637
1638     /*
1639      * Second pass; merge click runs.  We merge click events forward in the
1640      * queue.  For example, double click can be changed to triple click.
1641      *
1642      * NOTE: There is a problem with this design!  If the application
1643      * allows enough click events to pile up in the circular queue so
1644      * they wrap around, it will cheerfully merge the newest forward
1645      * into the oldest, creating a bogus doubleclick and confusing
1646      * the queue-traversal logic rather badly.  Generally this won't
1647      * happen, because calling getmouse() marks old events invalid and
1648      * ineligible for merges.  The true solution to this problem would
1649      * be to timestamp each MEVENT and perform the obvious sanity check,
1650      * but the timer element would have to have sub-second resolution,
1651      * which would get us into portability trouble.
1652      */
1653     first_invalid = NULL;
1654     endLoop = (first_valid == NULL);
1655     ep = first_valid;
1656     while (!endLoop) {
1657         next = NEXT(ep);
1658
1659         if (next == eventp) {
1660             /* Will end the loop, but check event type and compact before */
1661             endLoop = TRUE;
1662         } else if (!ValidEvent(next)) {
1663             continue;
1664         } else {
1665             /* merge click events forward */
1666             if ((ep->bstate & BUTTON_CLICKED)
1667                 && (next->bstate & BUTTON_CLICKED)) {
1668                 merge = FALSE;
1669                 for (b = 1; b <= MAX_BUTTONS; ++b) {
1670                     if ((sp->_mouse_mask2 & MASK_DOUBLE_CLICK(b))
1671                         && (ep->bstate & MASK_CLICK(b))
1672                         && (next->bstate & MASK_CLICK(b))) {
1673                         next->bstate &= ~MASK_CLICK(b);
1674                         next->bstate |= MASK_DOUBLE_CLICK(b);
1675                         merge = TRUE;
1676                     }
1677                 }
1678                 if (merge) {
1679                     Invalidate(ep);
1680                 }
1681             }
1682
1683             /* merge double-click events forward */
1684             if ((ep->bstate & BUTTON_DOUBLE_CLICKED)
1685                 && (next->bstate & BUTTON_CLICKED)) {
1686                 merge = FALSE;
1687                 for (b = 1; b <= MAX_BUTTONS; ++b) {
1688                     if ((sp->_mouse_mask2 & MASK_TRIPLE_CLICK(b))
1689                         && (ep->bstate & MASK_DOUBLE_CLICK(b))
1690                         && (next->bstate & MASK_CLICK(b))) {
1691                         next->bstate &= ~MASK_CLICK(b);
1692                         next->bstate |= MASK_TRIPLE_CLICK(b);
1693                         merge = TRUE;
1694                     }
1695                 }
1696                 if (merge) {
1697                     Invalidate(ep);
1698                 }
1699             }
1700         }
1701
1702         /* Discard event if it does not match event mask */
1703         if (!(ep->bstate & sp->_mouse_mask2)) {
1704             Invalidate(ep);
1705         }
1706
1707         /* Compact valid events */
1708         if (!ValidEvent(ep)) {
1709             if (ep == first_valid) {
1710                 first_valid = next;
1711             } else if (first_invalid == NULL) {
1712                 first_invalid = ep;
1713             }
1714         } else if (first_invalid != NULL) {
1715             *first_invalid = *ep;
1716             Invalidate(ep);
1717             first_invalid = NEXT(first_invalid);
1718         }
1719
1720         ep = next;
1721     }
1722
1723     if (first_invalid == NULL) {
1724         first_invalid = eventp;
1725     }
1726     sp->_mouse_eventp = first_invalid;
1727
1728 #ifdef TRACE
1729     if (first_valid != NULL) {
1730         if (USE_TRACEF(TRACE_IEVENT)) {
1731             _trace_slot(sp, "after mouse event queue compaction:");
1732             _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1733                     RunParams(sp, first_invalid, first_valid),
1734                     runcount);
1735             _nc_unlock_global(tracef);
1736         }
1737         for (ep = first_valid; ep != first_invalid; ep = NEXT(ep)) {
1738             if (ValidEvent(ep))
1739                 TR(MY_TRACE,
1740                    ("_nc_mouse_parse: returning composite mouse event %s at slot %ld",
1741                     _nc_tracemouse(sp, ep),
1742                     (long) IndexEV(sp, ep)));
1743         }
1744     }
1745 #endif /* TRACE */
1746
1747     /* after all this, do we have a valid event? */
1748     ep = PREV(first_invalid);
1749     return ValidEvent(ep) && ((ep->bstate & sp->_mouse_mask) != 0);
1750 }
1751
1752 static void
1753 _nc_mouse_wrap(SCREEN *sp)
1754 /* release mouse -- called by endwin() before shellout/exit */
1755 {
1756     TR(MY_TRACE, ("_nc_mouse_wrap() called"));
1757
1758     switch (sp->_mouse_type) {
1759     case M_XTERM:
1760         if (sp->_mouse_mask)
1761             mouse_activate(sp, FALSE);
1762         break;
1763 #if USE_GPM_SUPPORT
1764         /* GPM: pass all mouse events to next client */
1765     case M_GPM:
1766         if (sp->_mouse_mask)
1767             mouse_activate(sp, FALSE);
1768         break;
1769 #endif
1770 #if USE_SYSMOUSE
1771     case M_SYSMOUSE:
1772         mouse_activate(sp, FALSE);
1773         break;
1774 #endif
1775 #ifdef USE_TERM_DRIVER
1776     case M_TERM_DRIVER:
1777         mouse_activate(sp, FALSE);
1778         break;
1779 #endif
1780     case M_NONE:
1781         break;
1782     }
1783 }
1784
1785 static void
1786 _nc_mouse_resume(SCREEN *sp)
1787 /* re-connect to mouse -- called by doupdate() after shellout */
1788 {
1789     TR(MY_TRACE, ("_nc_mouse_resume() called"));
1790
1791     switch (sp->_mouse_type) {
1792     case M_XTERM:
1793         /* xterm: re-enable reporting */
1794         if (sp->_mouse_mask)
1795             mouse_activate(sp, TRUE);
1796         break;
1797
1798 #if USE_GPM_SUPPORT
1799     case M_GPM:
1800         /* GPM: reclaim our event set */
1801         if (sp->_mouse_mask)
1802             mouse_activate(sp, TRUE);
1803         break;
1804 #endif
1805
1806 #if USE_SYSMOUSE
1807     case M_SYSMOUSE:
1808         mouse_activate(sp, TRUE);
1809         break;
1810 #endif
1811
1812 #ifdef USE_TERM_DRIVER
1813     case M_TERM_DRIVER:
1814         mouse_activate(sp, TRUE);
1815         break;
1816 #endif
1817
1818     case M_NONE:
1819         break;
1820     }
1821 }
1822
1823 /**************************************************************************
1824  *
1825  * Mouse interface entry points for the API
1826  *
1827  **************************************************************************/
1828
1829 NCURSES_EXPORT(int)
1830 NCURSES_SP_NAME(getmouse) (NCURSES_SP_DCLx MEVENT * aevent)
1831 {
1832     int result = ERR;
1833     MEVENT *eventp;
1834
1835     T((T_CALLED("getmouse(%p,%p)"), (void *) SP_PARM, (void *) aevent));
1836
1837     if ((aevent != 0) &&
1838         (SP_PARM != 0) &&
1839         (SP_PARM->_mouse_type != M_NONE) &&
1840         (eventp = SP_PARM->_mouse_eventp) != 0) {
1841         /* compute the current-event pointer */
1842         MEVENT *prev = PREV(eventp);
1843
1844         /*
1845          * Discard events not matching mask (there could be still some if
1846          * _nc_mouse_parse was not called, e.g., when _nc_mouse_inline returns
1847          * false).
1848          */
1849         while (ValidEvent(prev) && (!(prev->bstate & SP_PARM->_mouse_mask2))) {
1850             Invalidate(prev);
1851             prev = PREV(prev);
1852         }
1853         if (ValidEvent(prev)) {
1854             /* copy the event we find there */
1855             *aevent = *prev;
1856
1857             TR(TRACE_IEVENT, ("getmouse: returning event %s from slot %ld",
1858                               _nc_tracemouse(SP_PARM, prev),
1859                               (long) IndexEV(SP_PARM, prev)));
1860
1861             Invalidate(prev);   /* so the queue slot becomes free */
1862             SP_PARM->_mouse_eventp = prev;
1863             result = OK;
1864         } else {
1865             /* Reset the provided event */
1866             aevent->bstate = 0;
1867             Invalidate(aevent);
1868             aevent->x = 0;
1869             aevent->y = 0;
1870             aevent->z = 0;
1871         }
1872     }
1873     returnCode(result);
1874 }
1875
1876 #if NCURSES_SP_FUNCS
1877 /* grab a copy of the current mouse event */
1878 NCURSES_EXPORT(int)
1879 getmouse(MEVENT * aevent)
1880 {
1881     return NCURSES_SP_NAME(getmouse) (CURRENT_SCREEN, aevent);
1882 }
1883 #endif
1884
1885 NCURSES_EXPORT(int)
1886 NCURSES_SP_NAME(ungetmouse) (NCURSES_SP_DCLx MEVENT * aevent)
1887 {
1888     int result = ERR;
1889     MEVENT *eventp;
1890
1891     T((T_CALLED("ungetmouse(%p,%p)"), (void *) SP_PARM, (void *) aevent));
1892
1893     if (aevent != 0 &&
1894         SP_PARM != 0 &&
1895         (eventp = SP_PARM->_mouse_eventp) != 0) {
1896
1897         /* stick the given event in the next-free slot */
1898         *eventp = *aevent;
1899
1900         /* bump the next-free pointer into the circular list */
1901         SP_PARM->_mouse_eventp = NEXT(eventp);
1902
1903         /* push back the notification event on the keyboard queue */
1904         result = NCURSES_SP_NAME(ungetch) (NCURSES_SP_ARGx KEY_MOUSE);
1905     }
1906     returnCode(result);
1907 }
1908
1909 #if NCURSES_SP_FUNCS
1910 /* enqueue a synthesized mouse event to be seen by the next wgetch() */
1911 NCURSES_EXPORT(int)
1912 ungetmouse(MEVENT * aevent)
1913 {
1914     return NCURSES_SP_NAME(ungetmouse) (CURRENT_SCREEN, aevent);
1915 }
1916 #endif
1917
1918 NCURSES_EXPORT(mmask_t)
1919 NCURSES_SP_NAME(mousemask) (NCURSES_SP_DCLx mmask_t newmask, mmask_t * oldmask)
1920 /* set the mouse event mask */
1921 {
1922     mmask_t result = 0;
1923
1924     T((T_CALLED("mousemask(%p,%#lx,%p)"),
1925        (void *) SP_PARM,
1926        (unsigned long) newmask,
1927        (void *) oldmask));
1928
1929     if (SP_PARM != 0) {
1930         if (oldmask)
1931             *oldmask = SP_PARM->_mouse_mask;
1932
1933         if (newmask || SP_PARM->_mouse_initialized) {
1934             _nc_mouse_init(SP_PARM);
1935
1936             if (SP_PARM->_mouse_type != M_NONE) {
1937                 int b;
1938
1939                 result = newmask &
1940                     (REPORT_MOUSE_POSITION
1941                      | BUTTON_ALT
1942                      | BUTTON_CTRL
1943                      | BUTTON_SHIFT
1944                      | BUTTON_PRESSED
1945                      | BUTTON_RELEASED
1946                      | BUTTON_CLICKED
1947                      | BUTTON_DOUBLE_CLICKED
1948                      | BUTTON_TRIPLE_CLICKED);
1949
1950                 mouse_activate(SP_PARM, (bool) (result != 0));
1951
1952                 SP_PARM->_mouse_mask = result;
1953                 SP_PARM->_mouse_mask2 = result;
1954
1955                 /*
1956                  * Make a mask corresponding to the states we will need to
1957                  * retain (temporarily) while building up the state that the
1958                  * user asked for.
1959                  */
1960                 for (b = 1; b <= MAX_BUTTONS; ++b) {
1961                     if (SP_PARM->_mouse_mask2 & MASK_TRIPLE_CLICK(b))
1962                         SP_PARM->_mouse_mask2 |= MASK_DOUBLE_CLICK(b);
1963                     if (SP_PARM->_mouse_mask2 & MASK_DOUBLE_CLICK(b))
1964                         SP_PARM->_mouse_mask2 |= MASK_CLICK(b);
1965                     if (SP_PARM->_mouse_mask2 & MASK_CLICK(b))
1966                         SP_PARM->_mouse_mask2 |= (MASK_PRESS(b) |
1967                                                   MASK_RELEASE(b));
1968                 }
1969             }
1970         }
1971     }
1972     returnMMask(result);
1973 }
1974
1975 #if NCURSES_SP_FUNCS
1976 NCURSES_EXPORT(mmask_t)
1977 mousemask(mmask_t newmask, mmask_t * oldmask)
1978 {
1979     return NCURSES_SP_NAME(mousemask) (CURRENT_SCREEN, newmask, oldmask);
1980 }
1981 #endif
1982
1983 NCURSES_EXPORT(bool)
1984 wenclose(const WINDOW *win, int y, int x)
1985 /* check to see if given window encloses given screen location */
1986 {
1987     bool result = FALSE;
1988
1989     T((T_CALLED("wenclose(%p,%d,%d)"), (const void *) win, y, x));
1990
1991     if (win != 0) {
1992         y -= win->_yoffset;
1993         if (IS_PAD(win)) {
1994             if (win->_pad._pad_y >= 0 &&
1995                 win->_pad._pad_x >= 0 &&
1996                 win->_pad._pad_top >= 0 &&
1997                 win->_pad._pad_left >= 0 &&
1998                 win->_pad._pad_right >= 0 &&
1999                 win->_pad._pad_bottom >= 0) {
2000                 result = ((win->_pad._pad_top <= y &&
2001                            win->_pad._pad_left <= x &&
2002                            win->_pad._pad_right >= x &&
2003                            win->_pad._pad_bottom >= y) ? TRUE : FALSE);
2004             }
2005         } else {
2006             result = ((win->_begy <= y &&
2007                        win->_begx <= x &&
2008                        (win->_begx + win->_maxx) >= x &&
2009                        (win->_begy + win->_maxy) >= y) ? TRUE : FALSE);
2010         }
2011     }
2012     returnBool(result);
2013 }
2014
2015 NCURSES_EXPORT(int)
2016 NCURSES_SP_NAME(mouseinterval) (NCURSES_SP_DCLx int maxclick)
2017 /* set the maximum mouse interval within which to recognize a click */
2018 {
2019     int oldval;
2020
2021     T((T_CALLED("mouseinterval(%p,%d)"), (void *) SP_PARM, maxclick));
2022
2023     if (SP_PARM != 0) {
2024         oldval = SP_PARM->_maxclick;
2025         if (maxclick >= 0)
2026             SP_PARM->_maxclick = maxclick;
2027     } else {
2028         oldval = DEFAULT_MAXCLICK;
2029     }
2030
2031     returnCode(oldval);
2032 }
2033
2034 #if NCURSES_SP_FUNCS
2035 NCURSES_EXPORT(int)
2036 mouseinterval(int maxclick)
2037 {
2038     return NCURSES_SP_NAME(mouseinterval) (CURRENT_SCREEN, maxclick);
2039 }
2040 #endif
2041
2042 /* This may be used by other routines to ask for the existence of mouse
2043    support */
2044 NCURSES_EXPORT(bool)
2045 _nc_has_mouse(SCREEN *sp)
2046 {
2047     return (((0 == sp) || (sp->_mouse_type == M_NONE)) ? FALSE : TRUE);
2048 }
2049
2050 NCURSES_EXPORT(bool)
2051 NCURSES_SP_NAME(has_mouse) (NCURSES_SP_DCL0)
2052 {
2053     return _nc_has_mouse(SP_PARM);
2054 }
2055
2056 #if NCURSES_SP_FUNCS
2057 NCURSES_EXPORT(bool)
2058 has_mouse(void)
2059 {
2060     return _nc_has_mouse(CURRENT_SCREEN);
2061 }
2062 #endif
2063
2064 NCURSES_EXPORT(bool)
2065 wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
2066 {
2067     bool result = FALSE;
2068
2069     T((T_CALLED("wmouse_trafo(%p,%p,%p,%d)"),
2070        (const void *) win,
2071        (void *) pY,
2072        (void *) pX,
2073        to_screen));
2074
2075     if (win && pY && pX) {
2076         int y = *pY;
2077         int x = *pX;
2078
2079         T(("transform input %d,%d", y, x));
2080         if (to_screen) {
2081             y += win->_begy + win->_yoffset;
2082             x += win->_begx;
2083             if (wenclose(win, y, x))
2084                 result = TRUE;
2085         } else {
2086             if (wenclose(win, y, x)) {
2087                 y -= (win->_begy + win->_yoffset);
2088                 x -= win->_begx;
2089                 result = TRUE;
2090             }
2091         }
2092         if (result) {
2093             *pX = x;
2094             *pY = y;
2095             T(("output transform %d,%d", y, x));
2096         }
2097     }
2098     returnBool(result);
2099 }