1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 * and: Juergen Pfeifer 2008 *
34 ****************************************************************************/
37 * This module is intended to encapsulate ncurses's interface to pointing
40 * The primary method used is xterm's internal mouse-tracking facility.
41 * Additional methods depend on the platform:
42 * Alessandro Rubini's GPM server (Linux)
44 * special-purpose mouse interface for OS/2 EMX.
46 * Notes for implementors of new mouse-interface methods:
48 * The code is logically split into a lower level that accepts event reports
49 * in a device-dependent format and an upper level that parses mouse gestures
50 * and filters events. The mediating data structure is a circular queue of
53 * Functionally, the lower level's job is to pick up primitive events and
54 * put them on the circular queue. This can happen in one of two ways:
55 * either (a) _nc_mouse_event() detects a series of incoming mouse reports
56 * and queues them, or (b) code in lib_getch.c detects the kmous prefix in
57 * the keyboard input stream and calls _nc_mouse_inline to queue up a series
58 * of adjacent mouse reports.
60 * In either case, _nc_mouse_parse() should be called after the series is
61 * accepted to parse the digested mouse reports (low-level MEVENTs) into
62 * a gesture (a high-level or composite MEVENT).
64 * Don't be too shy about adding new event types or modifiers, if you can find
65 * room for them in the 32-bit mask. The API is written so that users get
66 * feedback on which theoretical event types they won't see when they call
67 * mousemask. There's one bit per button (the RESERVED_EVENT bit) not being
68 * used yet, and a couple of bits open at the high end.
77 # define INCL_DOSPROCESS
78 # include <os2.h> /* Need to include before the others */
81 #include <curses.priv.h>
84 #define CUR SP_TERMTYPE
87 MODULE_ID("$Id: lib_mouse.c,v 1.173 2017/04/30 01:22:04 tom Exp $")
92 #include <linux/keyboard.h> /* defines KG_* macros */
95 /* use dynamic loader to avoid linkage dependency */
99 #define my_RTLD RTLD_NOW
102 #define my_RTLD RTLD_LAZY
106 #endif /* RTLD_NOW */
107 #endif /* HAVE_LIBDL */
109 #endif /* USE_GPM_SUPPORT */
112 #undef buttons /* symbol conflict in consio.h */
113 #undef mouse_info /* symbol conflict in consio.h */
114 #include <osreldate.h>
115 #if defined(__DragonFly_version) || (defined(__FreeBSD__) && (__FreeBSD_version >= 400017))
116 #include <sys/consio.h>
117 #include <sys/fbio.h>
119 #include <machine/console.h>
121 #endif /* use_SYSMOUSE */
124 #include <sys/socket.h>
125 #define pipe(handles) socketpair(AF_LOCAL, SOCK_STREAM, 0, handles)
126 #define DosWrite(hfile, pbuffer, cbwrite, pcbactual) \
127 write(hfile, pbuffer, cbwrite)
128 #define DosExit(action, result ) /* do nothing */
129 #define DosCreateThread(ptid, pfn, param, flag, cbStack) \
130 (*(ptid) = _beginthread(pfn, NULL, cbStack, \
131 (void *)param), (*(ptid) == -1))
134 #define MY_TRACE TRACE_ICALLS|TRACE_IEVENT
136 #define MASK_RELEASE(x) (mmask_t) NCURSES_MOUSE_MASK(x, 001)
137 #define MASK_PRESS(x) (mmask_t) NCURSES_MOUSE_MASK(x, 002)
138 #define MASK_CLICK(x) (mmask_t) NCURSES_MOUSE_MASK(x, 004)
139 #define MASK_DOUBLE_CLICK(x) (mmask_t) NCURSES_MOUSE_MASK(x, 010)
140 #define MASK_TRIPLE_CLICK(x) (mmask_t) NCURSES_MOUSE_MASK(x, 020)
141 #define MASK_RESERVED_EVENT(x) (mmask_t) NCURSES_MOUSE_MASK(x, 040)
143 #if NCURSES_MOUSE_VERSION == 1
144 #define BUTTON_CLICKED (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED | BUTTON4_CLICKED)
145 #define BUTTON_PRESSED (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED | BUTTON4_PRESSED)
146 #define BUTTON_RELEASED (BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON3_RELEASED | BUTTON4_RELEASED)
147 #define BUTTON_DOUBLE_CLICKED (BUTTON1_DOUBLE_CLICKED | BUTTON2_DOUBLE_CLICKED | BUTTON3_DOUBLE_CLICKED | BUTTON4_DOUBLE_CLICKED)
148 #define BUTTON_TRIPLE_CLICKED (BUTTON1_TRIPLE_CLICKED | BUTTON2_TRIPLE_CLICKED | BUTTON3_TRIPLE_CLICKED | BUTTON4_TRIPLE_CLICKED)
149 #define MAX_BUTTONS 4
151 #define BUTTON_CLICKED (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED | BUTTON4_CLICKED | BUTTON5_CLICKED)
152 #define BUTTON_PRESSED (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED | BUTTON4_PRESSED | BUTTON5_PRESSED)
153 #define BUTTON_RELEASED (BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON3_RELEASED | BUTTON4_RELEASED | BUTTON5_RELEASED)
154 #define BUTTON_DOUBLE_CLICKED (BUTTON1_DOUBLE_CLICKED | BUTTON2_DOUBLE_CLICKED | BUTTON3_DOUBLE_CLICKED | BUTTON4_DOUBLE_CLICKED | BUTTON5_DOUBLE_CLICKED)
155 #define BUTTON_TRIPLE_CLICKED (BUTTON1_TRIPLE_CLICKED | BUTTON2_TRIPLE_CLICKED | BUTTON3_TRIPLE_CLICKED | BUTTON4_TRIPLE_CLICKED | BUTTON5_TRIPLE_CLICKED)
156 #define MAX_BUTTONS 5
159 #define INVALID_EVENT -1
160 #define NORMAL_EVENT 0
162 #define ValidEvent(ep) ((ep)->id != INVALID_EVENT)
163 #define Invalidate(ep) (ep)->id = INVALID_EVENT
167 #ifndef LIBGPM_SONAME
168 #define LIBGPM_SONAME "libgpm.so"
171 #define GET_DLSYM(name) (my_##name = (TYPE_##name) dlsym(sp->_dlopen_gpm, #name))
173 #endif /* USE_GPM_SUPPORT */
175 static bool _nc_mouse_parse(SCREEN *, int);
176 static void _nc_mouse_resume(SCREEN *);
177 static void _nc_mouse_wrap(SCREEN *);
179 /* maintain a circular list of mouse events */
181 #define FirstEV(sp) ((sp)->_mouse_events)
182 #define LastEV(sp) ((sp)->_mouse_events + EV_MAX - 1)
185 #define NEXT(ep) ((ep >= LastEV(SP_PARM)) \
190 #define PREV(ep) ((ep <= FirstEV(SP_PARM)) \
194 #define IndexEV(sp, ep) (ep - FirstEV(sp))
196 #define RunParams(sp, eventp, runp) \
197 (long) IndexEV(sp, runp), \
198 (long) (IndexEV(sp, eventp) + (EV_MAX - 1)) % EV_MAX
202 _trace_slot(SCREEN *sp, const char *tag)
208 for (ep = FirstEV(sp); ep <= LastEV(sp); ep++)
209 _tracef("mouse event queue slot %ld = %s",
210 (long) IndexEV(sp, ep),
211 _nc_tracemouse(sp, ep));
220 # define M_FD(sp) sp->_mouse_fd
223 write_event(SCREEN *sp, int down, int button, int x, int y)
226 unsigned long ignore;
228 _nc_STRCPY(buf, "\033[M", sizeof(buf)); /* should be the same as key_mouse */
229 buf[3] = ' ' + (button - 1) + (down ? 0 : 0x40);
230 buf[4] = ' ' + x - LEFT_COL + 1;
231 buf[5] = ' ' + y - TOP_ROW + 1;
232 DosWrite(sp->_emxmouse_wfd, buf, 6, &ignore);
237 mouse_server(void *param)
239 mouse_server(unsigned long param)
242 SCREEN *sp = (SCREEN *) param;
243 unsigned short fWait = MOU_WAIT;
244 /* NOPTRRECT mourt = { 0,0,24,79 }; */
247 unsigned short mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;
253 /* open the handle for the mouse */
254 if (MouOpen(NULL, &hmou) == 0) {
255 rc = MouSetEventMask(&mask, hmou);
256 if (rc) { /* retry with 2 buttons */
257 mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN;
258 rc = MouSetEventMask(&mask, hmou);
261 if (rc == 0 && MouDrawPtr(hmou) == 0) {
263 /* sit and wait on the event queue */
264 rc = MouReadEventQue(&mouev, &fWait, hmou);
266 _nc_SPRINTF(err, _nc_SLIMIT(sizeof(err))
267 "Error reading mouse queue, rc=%lu.\r\n", rc);
270 if (!sp->_emxmouse_activated)
274 * OS/2 numbers a 3-button mouse inconsistently from other
280 if ((mouev.fs ^ oldstate) & MOUSE_BN1_DOWN)
281 write_event(sp, mouev.fs & MOUSE_BN1_DOWN,
282 sp->_emxmouse_buttons[1], mouev.col, mouev.row);
283 if ((mouev.fs ^ oldstate) & MOUSE_BN2_DOWN)
284 write_event(sp, mouev.fs & MOUSE_BN2_DOWN,
285 sp->_emxmouse_buttons[3], mouev.col, mouev.row);
286 if ((mouev.fs ^ oldstate) & MOUSE_BN3_DOWN)
287 write_event(sp, mouev.fs & MOUSE_BN3_DOWN,
288 sp->_emxmouse_buttons[2], mouev.col, mouev.row);
294 _nc_SPRINTF(err, _nc_SLIMIT(sizeof(err))
295 "Error setting event mask, buttons=%d, rc=%lu.\r\n",
299 DosWrite(2, err, strlen(err), &rc);
302 DosExit(EXIT_THREAD, 0L);
305 #endif /* USE_EMX_MOUSE */
309 sysmouse_server(SCREEN *sp)
311 struct mouse_info the_mouse;
314 the_mouse.operation = MOUSE_GETINFO;
316 && sp->_mouse_fd >= 0
317 && sp->_sysmouse_tail < FIFO_SIZE
318 && ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
320 if (sp->_sysmouse_head > sp->_sysmouse_tail) {
321 sp->_sysmouse_tail = 0;
322 sp->_sysmouse_head = 0;
324 work = &(sp->_sysmouse_fifo[sp->_sysmouse_tail]);
325 memset(work, 0, sizeof(*work));
326 work->id = NORMAL_EVENT; /* there's only one mouse... */
328 sp->_sysmouse_old_buttons = sp->_sysmouse_new_buttons;
329 sp->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
331 if (sp->_sysmouse_new_buttons) {
332 if (sp->_sysmouse_new_buttons & 1)
333 work->bstate |= BUTTON1_PRESSED;
334 if (sp->_sysmouse_new_buttons & 2)
335 work->bstate |= BUTTON2_PRESSED;
336 if (sp->_sysmouse_new_buttons & 4)
337 work->bstate |= BUTTON3_PRESSED;
339 if (sp->_sysmouse_old_buttons & 1)
340 work->bstate |= BUTTON1_RELEASED;
341 if (sp->_sysmouse_old_buttons & 2)
342 work->bstate |= BUTTON2_RELEASED;
343 if (sp->_sysmouse_old_buttons & 4)
344 work->bstate |= BUTTON3_RELEASED;
347 /* for cosmetic bug in syscons.c on FreeBSD 3.[34] */
348 the_mouse.operation = MOUSE_HIDE;
349 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
350 the_mouse.operation = MOUSE_SHOW;
351 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
354 * We're only interested if the button is pressed or released.
355 * FIXME: implement continuous event-tracking.
357 if (sp->_sysmouse_new_buttons != sp->_sysmouse_old_buttons) {
358 sp->_sysmouse_tail += 1;
360 work->x = the_mouse.u.data.x / sp->_sysmouse_char_width;
361 work->y = the_mouse.u.data.y / sp->_sysmouse_char_height;
366 handle_sysmouse(int sig GCC_UNUSED)
368 sysmouse_server(CURRENT_SCREEN);
370 #endif /* USE_SYSMOUSE */
372 #ifndef USE_TERM_DRIVER
373 #define xterm_kmous "\033[M"
376 init_xterm_mouse(SCREEN *sp)
378 sp->_mouse_type = M_XTERM;
379 sp->_mouse_format = MF_X10;
380 sp->_mouse_xtermcap = tigetstr("XM");
381 if (VALID_STRING(sp->_mouse_xtermcap)) {
382 char *code = strstr(sp->_mouse_xtermcap, "[?");
385 while ((*code >= '0') && (*code <= '9')) {
387 while ((*next >= '0') && (*next <= '9')) {
390 if (!strncmp(code, "1006", (size_t) (next - code))) {
391 sp->_mouse_format = MF_SGR1006;
393 #ifdef EXP_XTERM_1005
394 if (!strncmp(code, "1005", (size_t) (next - code))) {
395 sp->_mouse_format = MF_XTERM_1005;
399 while (*next == ';') {
409 int code = tigetnum("XM");
417 sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
423 enable_xterm_mouse(SCREEN *sp, int enable)
426 sp->_emxmouse_activated = enable;
428 NCURSES_PUTP2("xterm-mouse", TPARM_1(sp->_mouse_xtermcap, enable));
430 sp->_mouse_active = enable;
435 allow_gpm_mouse(SCREEN *sp GCC_UNUSED)
440 /* Danger Robinson: do not use dlopen for libgpm if already loaded */
442 if (!sp->_mouse_gpm_loaded) {
443 T(("GPM library was already dlopen'd, not by us"));
447 /* GPM does printf's without checking if stdout is a terminal */
448 if (NC_ISATTY(fileno(stdout))) {
449 const char *list = getenv("NCURSES_GPM_TERMS");
450 const char *env = getenv("TERM");
453 result = _nc_name_match(list, env, "|:");
456 /* GPM checks the beginning of the $TERM variable to decide if it
457 * should pass xterm events through. There is no real advantage in
458 * allowing GPM to do this. Recent versions relax that check, and
459 * pretend that GPM can work with any terminal having the kmous
460 * capability. Perhaps that works for someone. If so, they can
461 * set the environment variable (above).
463 if (env != 0 && strstr(env, "linux") != 0) {
473 unload_gpm_library(SCREEN *sp)
475 if (sp->_dlopen_gpm != 0) {
476 T(("unload GPM library"));
477 sp->_mouse_gpm_loaded = FALSE;
479 dlclose(sp->_dlopen_gpm);
485 load_gpm_library(SCREEN *sp)
487 sp->_mouse_gpm_found = FALSE;
488 if ((sp->_dlopen_gpm = dlopen(LIBGPM_SONAME, my_RTLD)) != 0) {
489 #pragma GCC diagnostic push
490 #if (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__)
491 #pragma GCC diagnostic ignored "-Wpedantic"
493 if (GET_DLSYM(gpm_fd) == 0 ||
494 GET_DLSYM(Gpm_Open) == 0 ||
495 GET_DLSYM(Gpm_Close) == 0 ||
496 GET_DLSYM(Gpm_GetEvent) == 0) {
497 #pragma GCC diagnostic pop
498 T(("GPM initialization failed: %s", dlerror()));
499 unload_gpm_library(sp);
501 sp->_mouse_gpm_found = TRUE;
502 sp->_mouse_gpm_loaded = TRUE;
509 enable_gpm_mouse(SCREEN *sp, bool enable)
513 T((T_CALLED("enable_gpm_mouse(%d)"), enable));
515 if (enable && !sp->_mouse_active) {
517 if (sp->_mouse_gpm_found && !sp->_mouse_gpm_loaded) {
518 load_gpm_library(sp);
521 if (sp->_mouse_gpm_loaded) {
524 /* GPM: initialize connection to gpm server */
525 sp->_mouse_gpm_connect.eventMask = GPM_DOWN | GPM_UP;
526 sp->_mouse_gpm_connect.defaultMask =
527 (unsigned short) (~(sp->_mouse_gpm_connect.eventMask | GPM_HARD));
528 sp->_mouse_gpm_connect.minMod = 0;
529 sp->_mouse_gpm_connect.maxMod =
530 (unsigned short) (~((1 << KG_SHIFT) |
534 * Note: GPM hardcodes \E[?1001s and \E[?1000h during its open.
535 * The former is recognized by wscons (SunOS), and the latter by
536 * xterm. Those will not show up in ncurses' traces.
538 code = my_Gpm_Open(&sp->_mouse_gpm_connect, 0);
539 result = (code >= 0);
542 * GPM can return a -2 if it is trying to do something with xterm.
543 * Ignore that, since it conflicts with our use of stdin.
551 sp->_mouse_active = result;
552 T(("GPM open %s", result ? "succeeded" : "failed"));
554 if (!enable && sp->_mouse_active) {
555 /* GPM: close connection to gpm server */
557 sp->_mouse_active = FALSE;
564 unload_gpm_library(sp);
569 #endif /* USE_GPM_SUPPORT */
572 initialize_mousetype(SCREEN *sp)
574 T((T_CALLED("initialize_mousetype()")));
576 /* Try gpm first, because gpm may be configured to run in xterm */
578 if (allow_gpm_mouse(sp)) {
579 if (!sp->_mouse_gpm_loaded) {
581 load_gpm_library(sp);
582 #else /* !HAVE_LIBDL */
583 sp->_mouse_gpm_found = TRUE;
584 sp->_mouse_gpm_loaded = TRUE;
589 * The gpm_fd file-descriptor may be negative (xterm). So we have to
590 * maintain our notion of whether the mouse connection is active
591 * without testing the file-descriptor.
593 if (sp->_mouse_gpm_found && enable_gpm_mouse(sp, TRUE)) {
594 sp->_mouse_type = M_GPM;
595 sp->_mouse_fd = *(my_gpm_fd);
596 T(("GPM mouse_fd %d", sp->_mouse_fd));
600 #endif /* USE_GPM_SUPPORT */
604 if (!sp->_emxmouse_thread
605 && strstr(TerminalOf(sp)->type.term_names, "xterm") == 0
609 if (pipe(handles) < 0) {
610 perror("mouse pipe error");
615 if (!sp->_emxmouse_buttons[0]) {
616 const char *s = getenv("MOUSE_BUTTONS_123");
618 sp->_emxmouse_buttons[0] = 1;
619 if (s && strlen(s) >= 3) {
620 sp->_emxmouse_buttons[1] = s[0] - '0';
621 sp->_emxmouse_buttons[2] = s[1] - '0';
622 sp->_emxmouse_buttons[3] = s[2] - '0';
624 sp->_emxmouse_buttons[1] = 1;
625 sp->_emxmouse_buttons[2] = 3;
626 sp->_emxmouse_buttons[3] = 2;
629 sp->_emxmouse_wfd = handles[1];
630 M_FD(sp) = handles[0];
632 setmode(handles[0], O_BINARY);
633 setmode(handles[1], O_BINARY);
634 /* Do not use CRT functions, we may single-threaded. */
635 rc = DosCreateThread((unsigned long *) &sp->_emxmouse_thread,
636 mouse_server, (long) sp, 0, 8192);
638 printf("mouse thread error %d=%#x", rc, rc);
640 sp->_mouse_type = M_XTERM;
645 #endif /* USE_EMX_MOUSE */
649 static char dev_tty[] = "/dev/tty";
650 struct mouse_info the_mouse;
651 char *the_device = 0;
653 if (NC_ISATTY(sp->_ifd))
654 the_device = ttyname(sp->_ifd);
656 the_device = dev_tty;
658 sp->_mouse_fd = open(the_device, O_RDWR);
660 if (sp->_mouse_fd >= 0) {
662 * sysmouse does not have a usable user interface for obtaining
663 * mouse events. The logical way to proceed (reading data on a
664 * stream) only works if one opens the device as root. Even in
665 * that mode, careful examination shows we lose events
666 * occasionally. The interface provided for user programs is to
667 * establish a signal handler. really.
669 * Take over SIGUSR2 for this purpose since SIGUSR1 is more
670 * likely to be used by an application. getch() will have to
671 * handle the misleading EINTR's.
673 signal(SIGUSR2, SIG_IGN);
674 the_mouse.operation = MOUSE_MODE;
675 the_mouse.u.mode.mode = 0;
676 the_mouse.u.mode.signal = SIGUSR2;
677 if (ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
678 signal(SIGUSR2, handle_sysmouse);
679 the_mouse.operation = MOUSE_SHOW;
680 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
682 #if defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) /* FreeBSD > 2.x */
684 #ifndef FBIO_GETMODE /* FreeBSD 3.x */
685 #define FBIO_GETMODE CONS_GET
686 #define FBIO_MODEINFO CONS_MODEINFO
687 #endif /* FBIO_GETMODE */
688 video_info_t the_video;
690 if (ioctl(sp->_mouse_fd,
692 &the_video.vi_mode) != -1
693 && ioctl(sp->_mouse_fd,
696 sp->_sysmouse_char_width = the_video.vi_cwidth;
697 sp->_sysmouse_char_height = the_video.vi_cheight;
700 #endif /* defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) */
702 if (sp->_sysmouse_char_width <= 0)
703 sp->_sysmouse_char_width = 8;
704 if (sp->_sysmouse_char_height <= 0)
705 sp->_sysmouse_char_height = 16;
706 sp->_mouse_type = M_SYSMOUSE;
711 #endif /* USE_SYSMOUSE */
713 #ifdef USE_TERM_DRIVER
714 CallDriver(sp, td_initmouse);
716 /* we know how to recognize mouse events under "xterm" */
717 if (key_mouse != 0) {
718 if (!strcmp(key_mouse, xterm_kmous)
719 || strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
720 init_xterm_mouse(sp);
722 } else if (strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
723 if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
724 init_xterm_mouse(sp);
732 _nc_mouse_init(SCREEN *sp)
733 /* initialize the mouse */
738 if (!sp->_mouse_initialized) {
741 sp->_mouse_initialized = TRUE;
743 TR(MY_TRACE, ("_nc_mouse_init() called"));
745 sp->_mouse_eventp = FirstEV(sp);
746 for (i = 0; i < EV_MAX; i++)
747 Invalidate(sp->_mouse_events + i);
749 initialize_mousetype(sp);
751 T(("_nc_mouse_init() set mousetype to %d", sp->_mouse_type));
753 result = sp->_mouse_initialized;
759 * Query to see if there is a pending mouse event. This is called from
760 * fifo_push() in lib_getch.c
763 _nc_mouse_event(SCREEN *sp)
765 MEVENT *eventp = sp->_mouse_eventp;
770 switch (sp->_mouse_type) {
772 /* xterm: never have to query, mouse events are in the keyboard stream */
777 int i, res = read(M_FD(sp), &kbuf, 3); /* Eat the prefix */
779 printf("Got %d chars instead of 3 for prefix.\n", res);
780 for (i = 0; i < res; i++) {
781 if (kbuf[i] != key_mouse[i])
782 printf("Got char %d instead of %d for prefix.\n",
783 (int) kbuf[i], (int) key_mouse[i]);
787 #endif /* USE_EMX_MOUSE */
792 if (sp->_mouse_fd >= 0) {
793 /* query server for event, return TRUE if we find one */
796 switch (my_Gpm_GetEvent(&ev)) {
798 /* Connection closed, drop the mouse. */
802 /* there's only one mouse... */
803 eventp->id = NORMAL_EVENT;
806 switch (ev.type & 0x0f) {
808 if (ev.buttons & GPM_B_LEFT)
809 eventp->bstate |= BUTTON1_PRESSED;
810 if (ev.buttons & GPM_B_MIDDLE)
811 eventp->bstate |= BUTTON2_PRESSED;
812 if (ev.buttons & GPM_B_RIGHT)
813 eventp->bstate |= BUTTON3_PRESSED;
816 if (ev.buttons & GPM_B_LEFT)
817 eventp->bstate |= BUTTON1_RELEASED;
818 if (ev.buttons & GPM_B_MIDDLE)
819 eventp->bstate |= BUTTON2_RELEASED;
820 if (ev.buttons & GPM_B_RIGHT)
821 eventp->bstate |= BUTTON3_RELEASED;
824 eventp->bstate |= REPORT_MOUSE_POSITION;
828 eventp->x = ev.x - 1;
829 eventp->y = ev.y - 1;
832 /* bump the next-free pointer into the circular list */
833 sp->_mouse_eventp = NEXT(eventp);
843 if (sp->_sysmouse_head < sp->_sysmouse_tail) {
844 *eventp = sp->_sysmouse_fifo[sp->_sysmouse_head];
847 * Point the fifo-head to the next possible location. If there
848 * are none, reset the indices. This may be interrupted by the
849 * signal handler, doing essentially the same reset.
851 sp->_sysmouse_head += 1;
852 if (sp->_sysmouse_head == sp->_sysmouse_tail) {
853 sp->_sysmouse_tail = 0;
854 sp->_sysmouse_head = 0;
857 /* bump the next-free pointer into the circular list */
858 sp->_mouse_eventp = eventp = NEXT(eventp);
862 #endif /* USE_SYSMOUSE */
864 #ifdef USE_TERM_DRIVER
866 while (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
867 *eventp = sp->_drv_mouse_fifo[sp->_drv_mouse_head];
870 * Point the fifo-head to the next possible location. If there
871 * are none, reset the indices.
873 sp->_drv_mouse_head += 1;
874 if (sp->_drv_mouse_head == sp->_drv_mouse_tail) {
875 sp->_drv_mouse_tail = 0;
876 sp->_drv_mouse_head = 0;
879 /* bump the next-free pointer into the circular list */
880 sp->_mouse_eventp = eventp = NEXT(eventp);
890 return result; /* true if we found an event */
894 #define PRESS_POSITION(n) \
896 eventp->bstate = MASK_PRESS(n); \
897 sp->_mouse_bstate |= MASK_PRESS(n); \
898 if (button & 0x40) { \
899 eventp->bstate = MASK_RELEASE(n); \
900 sp->_mouse_bstate &= ~MASK_PRESS(n); \
904 #define PRESS_POSITION(n) \
906 eventp->bstate = (mmask_t) ((sp->_mouse_bstate & MASK_PRESS(n)) \
907 ? REPORT_MOUSE_POSITION \
909 sp->_mouse_bstate |= MASK_PRESS(n); \
914 handle_wheel(SCREEN *sp, MEVENT * eventp, int button, int wheel)
918 switch (button & 3) {
921 eventp->bstate = MASK_PRESS(4);
922 /* Do not record in sp->_mouse_bstate; there will be no
923 * corresponding release event.
931 #if NCURSES_MOUSE_VERSION == 2
932 eventp->bstate = MASK_PRESS(5);
933 /* See comment above for button 4 */
935 /* Ignore this event as it is not a true press of the button */
936 eventp->bstate = REPORT_MOUSE_POSITION;
953 decode_X10_bstate(SCREEN *sp, MEVENT * eventp, unsigned intro)
959 if (!handle_wheel(sp, eventp, (int) intro, (intro & 96) == 96)) {
962 * Release events aren't reported for individual buttons, just for
963 * the button set as a whole. However, because there are normally
964 * no mouse events under xterm that intervene between press and
965 * release, we can infer the button actually released by looking at
966 * the previous event.
968 if (sp->_mouse_bstate & BUTTON_PRESSED) {
971 eventp->bstate = BUTTON_RELEASED;
972 for (b = 1; b <= MAX_BUTTONS; ++b) {
973 if (!(sp->_mouse_bstate & MASK_PRESS(b)))
974 eventp->bstate &= ~MASK_RELEASE(b);
976 sp->_mouse_bstate = 0;
979 * xterm will return a stream of release-events to let the
980 * application know where the mouse is going, if private mode
981 * 1002 or 1003 is enabled.
983 eventp->bstate = REPORT_MOUSE_POSITION;
988 eventp->bstate |= BUTTON_SHIFT;
991 eventp->bstate |= BUTTON_ALT;
994 eventp->bstate |= BUTTON_CTRL;
996 result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
1000 /* This code requires that your xterm entry contain the kmous capability and
1001 * that it be set to the \E[M documented in the Xterm Control Sequences
1002 * reference. This is how we arrange for mouse events to be reported via a
1003 * KEY_MOUSE return value from wgetch(). After this value is received,
1004 * _nc_mouse_inline() gets called and is immediately responsible for parsing
1005 * the mouse status information following the prefix.
1007 * The following quotes from the ctlseqs.ms document in the XTerm distribution,
1008 * describing the mouse tracking feature:
1010 * Parameters for all mouse tracking escape sequences generated by xterm encode
1011 * numeric parameters in a single character as value+040. For example, ! is
1014 * On button press or release, xterm sends ESC [ M CbCxCy. The low two bits of
1015 * Cb encode button information: 0=MB1 pressed, 1=MB2 pressed, 2=MB3 pressed,
1016 * 3=release. The upper bits encode what modifiers were down when the button
1017 * was pressed and are added together. 4=Shift, 8=Meta, 16=Control. Cx and Cy
1018 * are the x and y coordinates of the mouse event. The upper left corner is
1021 * (End quote) By the time we get here, we've eaten the key prefix. FYI, the
1022 * loop below is necessary because mouse click info isn't guaranteed to present
1023 * as a single clist item.
1025 * Wheel mice may return buttons 4 and 5 when the wheel is turned. We encode
1026 * those as button presses.
1029 decode_xterm_X10(SCREEN *sp, MEVENT * eventp)
1031 unsigned char kbuf[4];
1036 # if USE_PTHREADS_EINTR
1037 # if USE_WEAK_SYMBOLS
1038 if ((pthread_self) && (pthread_kill) && (pthread_equal))
1040 _nc_globals.read_thread = pthread_self();
1042 for (grabbed = 0; grabbed < 3; grabbed += (size_t) res) {
1044 /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
1047 (M_FD(sp) >= 0) ? M_FD(sp) : sp->_ifd,
1051 kbuf + grabbed, 3 - grabbed);
1055 #if USE_PTHREADS_EINTR
1056 _nc_globals.read_thread = 0;
1061 ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
1063 /* there's only one mouse... */
1064 eventp->id = NORMAL_EVENT;
1066 result = decode_X10_bstate(sp, eventp, kbuf[0]);
1068 eventp->x = (kbuf[1] - ' ') - 1;
1069 eventp->y = (kbuf[2] - ' ') - 1;
1074 #ifdef EXP_XTERM_1005
1076 * This is identical to X10/X11 responses except that there are two UTF-8
1077 * characters storing the ordinates instead of two bytes.
1080 decode_xterm_1005(SCREEN *sp, MEVENT * eventp)
1084 size_t limit = (sizeof(kbuf) - 1);
1091 # if USE_PTHREADS_EINTR
1092 # if USE_WEAK_SYMBOLS
1093 if ((pthread_self) && (pthread_kill) && (pthread_equal))
1095 _nc_globals.read_thread = pthread_self();
1097 for (grabbed = 0; grabbed < limit;) {
1102 (M_FD(sp) >= 0) ? M_FD(sp) : sp->_ifd,
1109 grabbed += (size_t) res;
1114 for (n = 0; n < 2; ++n) {
1117 if (check >= grabbed)
1119 rc = _nc_conv_to_utf32(&coords[n], kbuf + check, (unsigned)
1123 check += (size_t) rc;
1129 #if USE_PTHREADS_EINTR
1130 _nc_globals.read_thread = 0;
1134 ("_nc_mouse_inline sees the following xterm data: %s",
1135 _nc_visbufn(kbuf, (int) grabbed)));
1137 /* there's only one mouse... */
1138 eventp->id = NORMAL_EVENT;
1140 result = decode_X10_bstate(sp, eventp, UChar(kbuf[0]));
1142 eventp->x = (int) (coords[0] - ' ') - 1;
1143 eventp->y = (int) (coords[1] - ' ') - 1;
1147 #endif /* EXP_XTERM_1005 */
1150 * ECMA-48 section 5.4
1152 #define isInter(c) ((c) >= 0x20 && (c) <= 0x2f)
1153 #define isParam(c) ((c) >= 0x30 && (c) <= 0x3f)
1154 #define isFinal(c) ((c) >= 0x40 && (c) <= 0x7e)
1156 #define MAX_PARAMS 9
1159 int nerror; /* nonzero if there are unexpected chars */
1160 int nparam; /* number of numeric parameters */
1161 int params[MAX_PARAMS];
1162 int final; /* the final-character */
1166 read_SGR(SCREEN *sp, SGR_DATA * result)
1168 char kbuf[80]; /* bigger than any possible mouse response */
1174 memset(result, 0, sizeof(*result));
1175 # if USE_PTHREADS_EINTR
1176 # if USE_WEAK_SYMBOLS
1177 if ((pthread_self) && (pthread_kill) && (pthread_equal))
1179 _nc_globals.read_thread = pthread_self();
1187 (M_FD(sp) >= 0) ? M_FD(sp) : sp->_ifd,
1194 if ((grabbed + 3) >= (int) sizeof(kbuf)) {
1198 ch = UChar(kbuf[grabbed]);
1199 kbuf[grabbed + 1] = 0;
1213 result->nparam = (now + 1);
1216 result->params[now] = (result->params[now] * 10) + (ch - '0');
1221 result->nparam = (now + 1);
1226 if (ch < 32 || ch > 126) {
1228 * Technically other characters could be interspersed in the
1229 * response. Ignore those for now.
1233 } else if (isFinal(ch)) {
1244 } while (!isFinal(ch));
1245 #if USE_PTHREADS_EINTR
1246 _nc_globals.read_thread = 0;
1249 kbuf[++grabbed] = 0;
1251 ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
1252 return (grabbed > 0) && (result->nerror == 0);
1256 decode_xterm_SGR1006(SCREEN *sp, MEVENT * eventp)
1259 bool result = FALSE;
1260 if (read_SGR(sp, &data)) {
1261 int b = data.params[0];
1262 int b3 = 1 + (b & 3);
1264 eventp->id = NORMAL_EVENT;
1265 if (data.final == 'M') {
1266 (void) handle_wheel(sp, eventp, b, (b & 64) == 64);
1268 mmask_t pressed = (mmask_t) NCURSES_MOUSE_MASK(b3, NCURSES_BUTTON_PRESSED);
1269 mmask_t release = (mmask_t) NCURSES_MOUSE_MASK(b3, NCURSES_BUTTON_RELEASED);
1270 if (sp->_mouse_bstate & pressed) {
1271 eventp->bstate = release;
1272 sp->_mouse_bstate &= ~pressed;
1274 eventp->bstate = REPORT_MOUSE_POSITION;
1277 result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
1278 eventp->x = (data.params[1] ? (data.params[1] - 1) : 0);
1279 eventp->y = (data.params[2] ? (data.params[2] - 1) : 0);
1285 _nc_mouse_inline(SCREEN *sp)
1286 /* mouse report received in the keyboard stream -- parse its info */
1288 bool result = FALSE;
1289 MEVENT *eventp = sp->_mouse_eventp;
1291 TR(MY_TRACE, ("_nc_mouse_inline() called"));
1293 if (sp->_mouse_type == M_XTERM) {
1294 switch (sp->_mouse_format) {
1296 result = decode_xterm_X10(sp, eventp);
1299 result = decode_xterm_SGR1006(sp, eventp);
1301 #ifdef EXP_XTERM_1005
1303 result = decode_xterm_1005(sp, eventp);
1309 ("_nc_mouse_inline: primitive mouse-event %s has slot %ld",
1310 _nc_tracemouse(sp, eventp),
1311 (long) IndexEV(sp, eventp)));
1313 /* bump the next-free pointer into the circular list */
1314 sp->_mouse_eventp = NEXT(eventp);
1317 /* If this event is from a wheel-mouse, treat it like position
1318 * reports and avoid waiting for the release-events which will
1321 if (eventp->bstate & BUTTON_PRESSED) {
1324 for (b = 4; b <= MAX_BUTTONS; ++b) {
1325 if ((eventp->bstate & MASK_PRESS(b))) {
1338 mouse_activate(SCREEN *sp, int on)
1340 if (!on && !sp->_mouse_initialized)
1343 if (!_nc_mouse_init(sp))
1347 sp->_mouse_bstate = 0;
1348 switch (sp->_mouse_type) {
1350 #if NCURSES_EXT_FUNCS
1351 NCURSES_SP_NAME(keyok) (NCURSES_SP_ARGx KEY_MOUSE, on);
1353 TPUTS_TRACE("xterm mouse initialization");
1354 enable_xterm_mouse(sp, 1);
1358 if (enable_gpm_mouse(sp, TRUE)) {
1359 sp->_mouse_fd = *(my_gpm_fd);
1360 T(("GPM mouse_fd %d", sp->_mouse_fd));
1366 signal(SIGUSR2, handle_sysmouse);
1367 sp->_mouse_active = TRUE;
1370 #ifdef USE_TERM_DRIVER
1372 sp->_mouse_active = TRUE;
1378 /* Make runtime binding to cut down on object size of applications that
1379 * do not use the mouse (e.g., 'clear').
1381 sp->_mouse_event = _nc_mouse_event;
1382 sp->_mouse_inline = _nc_mouse_inline;
1383 sp->_mouse_parse = _nc_mouse_parse;
1384 sp->_mouse_resume = _nc_mouse_resume;
1385 sp->_mouse_wrap = _nc_mouse_wrap;
1388 switch (sp->_mouse_type) {
1390 TPUTS_TRACE("xterm mouse deinitialization");
1391 enable_xterm_mouse(sp, 0);
1395 enable_gpm_mouse(sp, FALSE);
1400 signal(SIGUSR2, SIG_IGN);
1401 sp->_mouse_active = FALSE;
1404 #ifdef USE_TERM_DRIVER
1406 sp->_mouse_active = FALSE;
1413 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
1416 /**************************************************************************
1418 * Device-independent code
1420 **************************************************************************/
1423 _nc_mouse_parse(SCREEN *sp, int runcount)
1424 /* parse a run of atomic mouse events into a gesture */
1426 MEVENT *eventp = sp->_mouse_eventp;
1428 MEVENT *first_valid = NULL;
1429 MEVENT *first_invalid = NULL;
1435 TR(MY_TRACE, ("_nc_mouse_parse(%d) called", runcount));
1438 * When we enter this routine, the event list next-free pointer
1439 * points just past a run of mouse events that we know were separated
1440 * in time by less than the critical click interval. The job of this
1441 * routine is to collapse this run into a single higher-level event
1444 * We accomplish this in two passes. The first pass merges press/release
1445 * pairs into click events. The second merges runs of click events into
1446 * double or triple-click events.
1448 * It's possible that the run may not resolve to a single event (for
1449 * example, if the user quadruple-clicks). If so, leading events
1450 * in the run are ignored if user does not call getmouse in a loop (getting
1451 * them from newest to older).
1453 * Note that this routine is independent of the format of the specific
1454 * format of the pointing-device's reports. We can use it to parse
1455 * gestures on anything that reports press/release events on a per-
1456 * button basis, as long as the device-dependent mouse code puts stuff
1457 * on the queue in MEVENT format.
1461 * Reset all events that were not set, in case the user sometimes calls
1462 * getmouse only once and other times until there are no more events in
1465 * This also allows reaching the beginning of the run.
1468 for (n = runcount; n < EV_MAX; n++) {
1474 if (USE_TRACEF(TRACE_IEVENT)) {
1475 _trace_slot(sp, "before mouse press/release merge:");
1476 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1477 RunParams(sp, eventp, ep),
1479 _nc_unlock_global(tracef);
1483 /* first pass; merge press/release pairs */
1487 if (next == eventp) {
1488 /* Will end the loop, but compact before */
1492 #define MASK_CHANGED(x) (!(ep->bstate & MASK_PRESS(x)) \
1493 == !(next->bstate & MASK_RELEASE(x)))
1495 if (ValidEvent(ep) && ValidEvent(next)
1496 && ep->x == next->x && ep->y == next->y
1497 && (ep->bstate & BUTTON_PRESSED)
1498 && (!(next->bstate & BUTTON_PRESSED))) {
1499 bool changed = TRUE;
1501 for (b = 1; b <= MAX_BUTTONS; ++b) {
1502 if (!MASK_CHANGED(b)) {
1510 for (b = 1; b <= MAX_BUTTONS; ++b) {
1511 if ((sp->_mouse_mask & MASK_CLICK(b))
1512 && (ep->bstate & MASK_PRESS(b))) {
1513 next->bstate &= ~MASK_RELEASE(b);
1514 next->bstate |= MASK_CLICK(b);
1525 /* Compact valid events */
1526 if (!ValidEvent(ep)) {
1527 if ((first_valid != NULL) && (first_invalid == NULL)) {
1531 if (first_valid == NULL) {
1533 } else if (first_invalid != NULL) {
1534 *first_invalid = *ep;
1536 first_invalid = NEXT(first_invalid);
1543 if (first_invalid != NULL) {
1544 eventp = first_invalid;
1547 if (USE_TRACEF(TRACE_IEVENT)) {
1548 _trace_slot(sp, "before mouse click merge:");
1549 if (first_valid == NULL) {
1550 _tracef("_nc_mouse_parse: no valid event");
1552 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1553 RunParams(sp, eventp, first_valid),
1555 _nc_unlock_global(tracef);
1561 * Second pass; merge click runs. We merge click events forward in the
1562 * queue. For example, double click can be changed to triple click.
1564 * NOTE: There is a problem with this design! If the application
1565 * allows enough click events to pile up in the circular queue so
1566 * they wrap around, it will cheerfully merge the newest forward
1567 * into the oldest, creating a bogus doubleclick and confusing
1568 * the queue-traversal logic rather badly. Generally this won't
1569 * happen, because calling getmouse() marks old events invalid and
1570 * ineligible for merges. The true solution to this problem would
1571 * be to timestamp each MEVENT and perform the obvious sanity check,
1572 * but the timer element would have to have sub-second resolution,
1573 * which would get us into portability trouble.
1575 first_invalid = NULL;
1576 endLoop = (first_valid == NULL);
1581 if (next == eventp) {
1582 /* Will end the loop, but check event type and compact before */
1584 } else if (!ValidEvent(next)) {
1587 /* merge click events forward */
1588 if ((ep->bstate & BUTTON_CLICKED)
1589 && (next->bstate & BUTTON_CLICKED)) {
1591 for (b = 1; b <= MAX_BUTTONS; ++b) {
1592 if ((sp->_mouse_mask & MASK_DOUBLE_CLICK(b))
1593 && (ep->bstate & MASK_CLICK(b))
1594 && (next->bstate & MASK_CLICK(b))) {
1595 next->bstate &= ~MASK_CLICK(b);
1596 next->bstate |= MASK_DOUBLE_CLICK(b);
1605 /* merge double-click events forward */
1606 if ((ep->bstate & BUTTON_DOUBLE_CLICKED)
1607 && (next->bstate & BUTTON_CLICKED)) {
1609 for (b = 1; b <= MAX_BUTTONS; ++b) {
1610 if ((sp->_mouse_mask & MASK_TRIPLE_CLICK(b))
1611 && (ep->bstate & MASK_DOUBLE_CLICK(b))
1612 && (next->bstate & MASK_CLICK(b))) {
1613 next->bstate &= ~MASK_CLICK(b);
1614 next->bstate |= MASK_TRIPLE_CLICK(b);
1624 /* Discard event if it does not match event mask */
1625 if (!(ep->bstate & sp->_mouse_mask2)) {
1629 /* Compact valid events */
1630 if (!ValidEvent(ep)) {
1631 if (ep == first_valid) {
1633 } else if (first_invalid == NULL) {
1636 } else if (first_invalid != NULL) {
1637 *first_invalid = *ep;
1639 first_invalid = NEXT(first_invalid);
1645 if (first_invalid == NULL) {
1646 first_invalid = eventp;
1648 sp->_mouse_eventp = first_invalid;
1651 if (first_valid != NULL) {
1652 if (USE_TRACEF(TRACE_IEVENT)) {
1653 _trace_slot(sp, "after mouse event queue compaction:");
1654 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1655 RunParams(sp, first_invalid, first_valid),
1657 _nc_unlock_global(tracef);
1659 for (ep = first_valid; ep != first_invalid; ep = NEXT(ep)) {
1662 ("_nc_mouse_parse: returning composite mouse event %s at slot %ld",
1663 _nc_tracemouse(sp, ep),
1664 (long) IndexEV(sp, ep)));
1669 /* after all this, do we have a valid event? */
1670 return ValidEvent(PREV(first_invalid));
1674 _nc_mouse_wrap(SCREEN *sp)
1675 /* release mouse -- called by endwin() before shellout/exit */
1677 TR(MY_TRACE, ("_nc_mouse_wrap() called"));
1679 switch (sp->_mouse_type) {
1681 if (sp->_mouse_mask)
1682 mouse_activate(sp, FALSE);
1685 /* GPM: pass all mouse events to next client */
1687 if (sp->_mouse_mask)
1688 mouse_activate(sp, FALSE);
1693 mouse_activate(sp, FALSE);
1696 #ifdef USE_TERM_DRIVER
1698 mouse_activate(sp, FALSE);
1707 _nc_mouse_resume(SCREEN *sp)
1708 /* re-connect to mouse -- called by doupdate() after shellout */
1710 TR(MY_TRACE, ("_nc_mouse_resume() called"));
1712 switch (sp->_mouse_type) {
1714 /* xterm: re-enable reporting */
1715 if (sp->_mouse_mask)
1716 mouse_activate(sp, TRUE);
1721 /* GPM: reclaim our event set */
1722 if (sp->_mouse_mask)
1723 mouse_activate(sp, TRUE);
1729 mouse_activate(sp, TRUE);
1733 #ifdef USE_TERM_DRIVER
1735 mouse_activate(sp, TRUE);
1744 /**************************************************************************
1746 * Mouse interface entry points for the API
1748 **************************************************************************/
1751 NCURSES_SP_NAME(getmouse) (NCURSES_SP_DCLx MEVENT * aevent)
1756 T((T_CALLED("getmouse(%p,%p)"), (void *) SP_PARM, (void *) aevent));
1758 if ((aevent != 0) &&
1760 (SP_PARM->_mouse_type != M_NONE) &&
1761 (eventp = SP_PARM->_mouse_eventp) != 0) {
1762 /* compute the current-event pointer */
1763 MEVENT *prev = PREV(eventp);
1766 * Discard events not matching mask (there could be still some if
1767 * _nc_mouse_parse was not called, e.g., when _nc_mouse_inline returns
1770 while (ValidEvent(prev) && (!(prev->bstate & SP_PARM->_mouse_mask2))) {
1774 if (ValidEvent(prev)) {
1775 /* copy the event we find there */
1778 TR(TRACE_IEVENT, ("getmouse: returning event %s from slot %ld",
1779 _nc_tracemouse(SP_PARM, prev),
1780 (long) IndexEV(SP_PARM, prev)));
1782 Invalidate(prev); /* so the queue slot becomes free */
1783 SP_PARM->_mouse_eventp = prev;
1786 /* Reset the provided event */
1797 #if NCURSES_SP_FUNCS
1798 /* grab a copy of the current mouse event */
1800 getmouse(MEVENT * aevent)
1802 return NCURSES_SP_NAME(getmouse) (CURRENT_SCREEN, aevent);
1807 NCURSES_SP_NAME(ungetmouse) (NCURSES_SP_DCLx MEVENT * aevent)
1812 T((T_CALLED("ungetmouse(%p,%p)"), (void *) SP_PARM, (void *) aevent));
1816 (eventp = SP_PARM->_mouse_eventp) != 0) {
1818 /* stick the given event in the next-free slot */
1821 /* bump the next-free pointer into the circular list */
1822 SP_PARM->_mouse_eventp = NEXT(eventp);
1824 /* push back the notification event on the keyboard queue */
1825 result = NCURSES_SP_NAME(ungetch) (NCURSES_SP_ARGx KEY_MOUSE);
1830 #if NCURSES_SP_FUNCS
1831 /* enqueue a synthesized mouse event to be seen by the next wgetch() */
1833 ungetmouse(MEVENT * aevent)
1835 return NCURSES_SP_NAME(ungetmouse) (CURRENT_SCREEN, aevent);
1839 NCURSES_EXPORT(mmask_t)
1840 NCURSES_SP_NAME(mousemask) (NCURSES_SP_DCLx mmask_t newmask, mmask_t * oldmask)
1841 /* set the mouse event mask */
1845 T((T_CALLED("mousemask(%p,%#lx,%p)"),
1847 (unsigned long) newmask,
1852 *oldmask = SP_PARM->_mouse_mask;
1854 if (newmask || SP_PARM->_mouse_initialized) {
1855 _nc_mouse_init(SP_PARM);
1857 if (SP_PARM->_mouse_type != M_NONE) {
1861 (REPORT_MOUSE_POSITION
1868 | BUTTON_DOUBLE_CLICKED
1869 | BUTTON_TRIPLE_CLICKED);
1871 mouse_activate(SP_PARM, (bool) (result != 0));
1873 SP_PARM->_mouse_mask = result;
1874 SP_PARM->_mouse_mask2 = result;
1877 * Make a mask corresponding to the states we will need to
1878 * retain (temporarily) while building up the state that the
1881 for (b = 1; b <= MAX_BUTTONS; ++b) {
1882 if (SP_PARM->_mouse_mask2 & MASK_TRIPLE_CLICK(b))
1883 SP_PARM->_mouse_mask2 |= MASK_DOUBLE_CLICK(b);
1884 if (SP_PARM->_mouse_mask2 & MASK_DOUBLE_CLICK(b))
1885 SP_PARM->_mouse_mask2 |= MASK_CLICK(b);
1886 if (SP_PARM->_mouse_mask2 & MASK_CLICK(b))
1887 SP_PARM->_mouse_mask2 |= (MASK_PRESS(b) |
1893 returnMMask(result);
1896 #if NCURSES_SP_FUNCS
1897 NCURSES_EXPORT(mmask_t)
1898 mousemask(mmask_t newmask, mmask_t * oldmask)
1900 return NCURSES_SP_NAME(mousemask) (CURRENT_SCREEN, newmask, oldmask);
1904 NCURSES_EXPORT(bool)
1905 wenclose(const WINDOW *win, int y, int x)
1906 /* check to see if given window encloses given screen location */
1908 bool result = FALSE;
1910 T((T_CALLED("wenclose(%p,%d,%d)"), (const void *) win, y, x));
1914 result = ((win->_begy <= y &&
1916 (win->_begx + win->_maxx) >= x &&
1917 (win->_begy + win->_maxy) >= y) ? TRUE : FALSE);
1923 NCURSES_SP_NAME(mouseinterval) (NCURSES_SP_DCLx int maxclick)
1924 /* set the maximum mouse interval within which to recognize a click */
1928 T((T_CALLED("mouseinterval(%p,%d)"), (void *) SP_PARM, maxclick));
1931 oldval = SP_PARM->_maxclick;
1933 SP_PARM->_maxclick = maxclick;
1935 oldval = DEFAULT_MAXCLICK;
1941 #if NCURSES_SP_FUNCS
1943 mouseinterval(int maxclick)
1945 return NCURSES_SP_NAME(mouseinterval) (CURRENT_SCREEN, maxclick);
1949 /* This may be used by other routines to ask for the existence of mouse
1951 NCURSES_EXPORT(bool)
1952 _nc_has_mouse(SCREEN *sp)
1954 return (((0 == sp) || (sp->_mouse_type == M_NONE)) ? FALSE : TRUE);
1957 NCURSES_EXPORT(bool)
1958 NCURSES_SP_NAME(has_mouse) (NCURSES_SP_DCL0)
1960 return _nc_has_mouse(SP_PARM);
1963 #if NCURSES_SP_FUNCS
1964 NCURSES_EXPORT(bool)
1967 return _nc_has_mouse(CURRENT_SCREEN);
1971 NCURSES_EXPORT(bool)
1972 wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
1974 bool result = FALSE;
1976 T((T_CALLED("wmouse_trafo(%p,%p,%p,%d)"),
1982 if (win && pY && pX) {
1987 y += win->_begy + win->_yoffset;
1989 if (wenclose(win, y, x))
1992 if (wenclose(win, y, x)) {
1993 y -= (win->_begy + win->_yoffset);