1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
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 ****************************************************************************/
38 * This module is intended to encapsulate ncurses's interface to pointing
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)
45 * special-purpose mouse interface for OS/2 EMX.
47 * Notes for implementors of new mouse-interface methods:
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
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.
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).
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.
78 # define INCL_DOSPROCESS
79 # include <os2.h> /* Need to include before the others */
82 #include <curses.priv.h>
85 #define CUR SP_TERMTYPE
88 MODULE_ID("$Id: lib_mouse.c,v 1.193 2021/03/20 12:56:32 tom Exp $")
93 #include <linux/keyboard.h> /* defines KG_* macros */
96 /* use dynamic loader to avoid linkage dependency */
100 #define my_RTLD RTLD_NOW
103 #define my_RTLD RTLD_LAZY
107 #endif /* RTLD_NOW */
108 #endif /* HAVE_LIBDL */
110 #endif /* USE_GPM_SUPPORT */
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>
120 #include <machine/console.h>
122 #endif /* use_SYSMOUSE */
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))
135 #define MY_TRACE TRACE_ICALLS|TRACE_IEVENT
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)
144 #if NCURSES_MOUSE_VERSION == 1
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)
152 #define MAX_BUTTONS 4
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)
162 #if NCURSES_MOUSE_VERSION == 2
163 #define MAX_BUTTONS 5
165 #define MAX_BUTTONS 11
170 #define INVALID_EVENT -1
171 #define NORMAL_EVENT 0
173 #define ValidEvent(ep) ((ep)->id != INVALID_EVENT)
174 #define Invalidate(ep) (ep)->id = INVALID_EVENT
178 #ifndef LIBGPM_SONAME
179 #define LIBGPM_SONAME "libgpm.so"
182 #define GET_DLSYM(name) (my_##name = (TYPE_##name) dlsym(sp->_dlopen_gpm, #name))
184 #endif /* USE_GPM_SUPPORT */
186 static bool _nc_mouse_parse(SCREEN *, int);
187 static void _nc_mouse_resume(SCREEN *);
188 static void _nc_mouse_wrap(SCREEN *);
190 /* maintain a circular list of mouse events */
192 #define FirstEV(sp) ((sp)->_mouse_events)
193 #define LastEV(sp) ((sp)->_mouse_events + EV_MAX - 1)
196 #define NEXT(ep) ((ep >= LastEV(SP_PARM)) \
201 #define PREV(ep) ((ep <= FirstEV(SP_PARM)) \
205 #define IndexEV(sp, ep) (ep - FirstEV(sp))
207 #define RunParams(sp, eventp, runp) \
208 (long) IndexEV(sp, runp), \
209 (long) (IndexEV(sp, eventp) + (EV_MAX - 1)) % EV_MAX
213 _trace_slot(SCREEN *sp, const char *tag)
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));
231 # define M_FD(sp) sp->_mouse_fd
234 write_event(SCREEN *sp, int down, int button, int x, int y)
237 unsigned long ignore;
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);
248 mouse_server(void *param)
250 mouse_server(unsigned long param)
253 SCREEN *sp = (SCREEN *) param;
254 unsigned short fWait = MOU_WAIT;
255 /* NOPTRRECT mourt = { 0,0,24,79 }; */
258 unsigned short mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;
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);
272 if (rc == 0 && MouDrawPtr(hmou) == 0) {
274 /* sit and wait on the event queue */
275 rc = MouReadEventQue(&mouev, &fWait, hmou);
277 _nc_SPRINTF(err, _nc_SLIMIT(sizeof(err))
278 "Error reading mouse queue, rc=%lu.\r\n", rc);
281 if (!sp->_emxmouse_activated)
285 * OS/2 numbers a 3-button mouse inconsistently from other
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);
305 _nc_SPRINTF(err, _nc_SLIMIT(sizeof(err))
306 "Error setting event mask, buttons=%d, rc=%lu.\r\n",
310 DosWrite(2, err, strlen(err), &rc);
313 DosExit(EXIT_THREAD, 0L);
316 #endif /* USE_EMX_MOUSE */
320 sysmouse_server(SCREEN *sp)
322 struct mouse_info the_mouse;
325 the_mouse.operation = MOUSE_GETINFO;
327 && sp->_mouse_fd >= 0
328 && sp->_sysmouse_tail < FIFO_SIZE
329 && ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
331 if (sp->_sysmouse_head > sp->_sysmouse_tail) {
332 sp->_sysmouse_tail = 0;
333 sp->_sysmouse_head = 0;
335 work = &(sp->_sysmouse_fifo[sp->_sysmouse_tail]);
336 memset(work, 0, sizeof(*work));
337 work->id = NORMAL_EVENT; /* there's only one mouse... */
339 sp->_sysmouse_old_buttons = sp->_sysmouse_new_buttons;
340 sp->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
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;
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;
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);
365 * We're only interested if the button is pressed or released.
366 * FIXME: implement continuous event-tracking.
368 if (sp->_sysmouse_new_buttons != sp->_sysmouse_old_buttons) {
369 sp->_sysmouse_tail += 1;
371 work->x = the_mouse.u.data.x / sp->_sysmouse_char_width;
372 work->y = the_mouse.u.data.y / sp->_sysmouse_char_height;
377 handle_sysmouse(int sig GCC_UNUSED)
379 sysmouse_server(CURRENT_SCREEN);
381 #endif /* USE_SYSMOUSE */
383 #ifndef USE_TERM_DRIVER
384 #define xterm_kmous "\033[M"
387 init_xterm_mouse(SCREEN *sp)
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, "[?");
396 while ((*code >= '0') && (*code <= '9')) {
398 while ((*next >= '0') && (*next <= '9')) {
401 if (!strncmp(code, "1006", (size_t) (next - code))) {
402 sp->_mouse_format = MF_SGR1006;
404 #ifdef EXP_XTERM_1005
405 if (!strncmp(code, "1005", (size_t) (next - code))) {
406 sp->_mouse_format = MF_XTERM_1005;
410 while (*next == ';') {
420 int code = tigetnum("XM");
422 #ifdef EXP_XTERM_1005
424 /* see "xterm+sm+1005" */
425 sp->_mouse_xtermcap = "\033[?1005;1000%?%p1%{1}%=%th%el%;";
426 sp->_mouse_format = MF_XTERM_1005;
430 /* see "xterm+sm+1006" */
431 sp->_mouse_xtermcap = "\033[?1006;1000%?%p1%{1}%=%th%el%;";
432 sp->_mouse_format = MF_SGR1006;
435 sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
443 enable_xterm_mouse(SCREEN *sp, int enable)
446 sp->_emxmouse_activated = enable;
448 NCURSES_PUTP2("xterm-mouse", TIPARM_1(sp->_mouse_xtermcap, enable));
450 sp->_mouse_active = enable;
455 allow_gpm_mouse(SCREEN *sp GCC_UNUSED)
460 /* Danger Robinson: do not use dlopen for libgpm if already loaded */
461 if ((Gpm_Wgetch) != 0) {
462 if (!sp->_mouse_gpm_loaded) {
463 T(("GPM library was already dlopen'd, not by us"));
467 /* GPM does printf's without checking if stdout is a terminal */
468 if (NC_ISATTY(fileno(stdout))) {
469 const char *list = getenv("NCURSES_GPM_TERMS");
470 const char *env = getenv("TERM");
473 result = _nc_name_match(list, env, "|:");
476 /* GPM checks the beginning of the $TERM variable to decide if it
477 * should pass xterm events through. There is no real advantage in
478 * allowing GPM to do this. Recent versions relax that check, and
479 * pretend that GPM can work with any terminal having the kmous
480 * capability. Perhaps that works for someone. If so, they can
481 * set the environment variable (above).
483 if (env != 0 && strstr(env, "linux") != 0) {
493 unload_gpm_library(SCREEN *sp)
495 if (sp->_dlopen_gpm != 0) {
496 T(("unload GPM library"));
497 sp->_mouse_gpm_loaded = FALSE;
503 load_gpm_library(SCREEN *sp)
505 sp->_mouse_gpm_found = FALSE;
508 * If we already had a successful dlopen, reuse it.
510 if (sp->_dlopen_gpm != 0) {
511 sp->_mouse_gpm_found = TRUE;
512 sp->_mouse_gpm_loaded = TRUE;
513 } else if ((sp->_dlopen_gpm = dlopen(LIBGPM_SONAME, my_RTLD)) != 0) {
514 #if (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__)
515 #pragma GCC diagnostic push
516 #pragma GCC diagnostic ignored "-Wpedantic"
518 if (GET_DLSYM(gpm_fd) == 0 ||
519 GET_DLSYM(Gpm_Open) == 0 ||
520 GET_DLSYM(Gpm_Close) == 0 ||
521 GET_DLSYM(Gpm_GetEvent) == 0) {
522 #if (defined(__GNUC__) && (__GNUC__ >= 5)) || defined(__clang__)
523 #pragma GCC diagnostic pop
525 T(("GPM initialization failed: %s", dlerror()));
526 unload_gpm_library(sp);
527 dlclose(sp->_dlopen_gpm);
530 sp->_mouse_gpm_found = TRUE;
531 sp->_mouse_gpm_loaded = TRUE;
535 #endif /* HAVE_LIBDL */
538 enable_gpm_mouse(SCREEN *sp, bool enable)
542 T((T_CALLED("enable_gpm_mouse(%d)"), enable));
544 if (enable && !sp->_mouse_active) {
546 if (sp->_mouse_gpm_found && !sp->_mouse_gpm_loaded) {
547 load_gpm_library(sp);
550 if (sp->_mouse_gpm_loaded) {
553 /* GPM: initialize connection to gpm server */
554 sp->_mouse_gpm_connect.eventMask = GPM_DOWN | GPM_UP;
555 sp->_mouse_gpm_connect.defaultMask =
556 (unsigned short) (~(sp->_mouse_gpm_connect.eventMask | GPM_HARD));
557 sp->_mouse_gpm_connect.minMod = 0;
558 sp->_mouse_gpm_connect.maxMod =
559 (unsigned short) (~((1 << KG_SHIFT) |
563 * Note: GPM hardcodes \E[?1001s and \E[?1000h during its open.
564 * The former is recognized by wscons (SunOS), and the latter by
565 * xterm. Those will not show up in ncurses' traces.
567 code = my_Gpm_Open(&sp->_mouse_gpm_connect, 0);
568 result = (code >= 0);
571 * GPM can return a -2 if it is trying to do something with xterm.
572 * Ignore that, since it conflicts with our use of stdin.
580 sp->_mouse_active = result;
581 T(("GPM open %s", result ? "succeeded" : "failed"));
583 if (!enable && sp->_mouse_active) {
584 /* GPM: close connection to gpm server */
586 sp->_mouse_active = FALSE;
593 unload_gpm_library(sp);
598 #endif /* USE_GPM_SUPPORT */
601 initialize_mousetype(SCREEN *sp)
603 T((T_CALLED("initialize_mousetype()")));
605 /* Try gpm first, because gpm may be configured to run in xterm */
607 if (allow_gpm_mouse(sp)) {
608 if (!sp->_mouse_gpm_loaded) {
610 load_gpm_library(sp);
611 #else /* !HAVE_LIBDL */
612 sp->_mouse_gpm_found = TRUE;
613 sp->_mouse_gpm_loaded = TRUE;
618 * The gpm_fd file-descriptor may be negative (xterm). So we have to
619 * maintain our notion of whether the mouse connection is active
620 * without testing the file-descriptor.
622 if (sp->_mouse_gpm_found && enable_gpm_mouse(sp, TRUE)) {
623 sp->_mouse_type = M_GPM;
624 sp->_mouse_fd = *(my_gpm_fd);
625 T(("GPM mouse_fd %d", sp->_mouse_fd));
629 #endif /* USE_GPM_SUPPORT */
633 if (!sp->_emxmouse_thread
634 && strstr(SP_TERMTYPE term_names, "xterm") == 0
635 && NonEmpty(key_mouse)) {
638 if (pipe(handles) < 0) {
639 perror("mouse pipe error");
644 if (!sp->_emxmouse_buttons[0]) {
645 const char *s = getenv("MOUSE_BUTTONS_123");
647 sp->_emxmouse_buttons[0] = 1;
648 if (s && strlen(s) >= 3) {
649 sp->_emxmouse_buttons[1] = s[0] - '0';
650 sp->_emxmouse_buttons[2] = s[1] - '0';
651 sp->_emxmouse_buttons[3] = s[2] - '0';
653 sp->_emxmouse_buttons[1] = 1;
654 sp->_emxmouse_buttons[2] = 3;
655 sp->_emxmouse_buttons[3] = 2;
658 sp->_emxmouse_wfd = handles[1];
659 M_FD(sp) = handles[0];
661 setmode(handles[0], O_BINARY);
662 setmode(handles[1], O_BINARY);
663 /* Do not use CRT functions, we may single-threaded. */
664 rc = DosCreateThread((unsigned long *) &sp->_emxmouse_thread,
665 mouse_server, (long) sp, 0, 8192);
667 printf("mouse thread error %d=%#x", rc, rc);
669 sp->_mouse_type = M_XTERM;
674 #endif /* USE_EMX_MOUSE */
678 static char dev_tty[] = "/dev/tty";
679 struct mouse_info the_mouse;
680 char *the_device = 0;
682 if (NC_ISATTY(sp->_ifd))
683 the_device = ttyname(sp->_ifd);
685 the_device = dev_tty;
687 sp->_mouse_fd = open(the_device, O_RDWR);
689 if (sp->_mouse_fd >= 0) {
691 * sysmouse does not have a usable user interface for obtaining
692 * mouse events. The logical way to proceed (reading data on a
693 * stream) only works if one opens the device as root. Even in
694 * that mode, careful examination shows we lose events
695 * occasionally. The interface provided for user programs is to
696 * establish a signal handler. really.
698 * Take over SIGUSR2 for this purpose since SIGUSR1 is more
699 * likely to be used by an application. getch() will have to
700 * handle the misleading EINTR's.
702 signal(SIGUSR2, SIG_IGN);
703 the_mouse.operation = MOUSE_MODE;
704 the_mouse.u.mode.mode = 0;
705 the_mouse.u.mode.signal = SIGUSR2;
706 if (ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
707 signal(SIGUSR2, handle_sysmouse);
708 the_mouse.operation = MOUSE_SHOW;
709 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
711 #if defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) /* FreeBSD > 2.x */
713 #ifndef FBIO_GETMODE /* FreeBSD 3.x */
714 #define FBIO_GETMODE CONS_GET
715 #define FBIO_MODEINFO CONS_MODEINFO
716 #endif /* FBIO_GETMODE */
717 video_info_t the_video;
719 if (ioctl(sp->_mouse_fd,
721 &the_video.vi_mode) != -1
722 && ioctl(sp->_mouse_fd,
725 sp->_sysmouse_char_width = the_video.vi_cwidth;
726 sp->_sysmouse_char_height = the_video.vi_cheight;
729 #endif /* defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) */
731 if (sp->_sysmouse_char_width <= 0)
732 sp->_sysmouse_char_width = 8;
733 if (sp->_sysmouse_char_height <= 0)
734 sp->_sysmouse_char_height = 16;
735 sp->_mouse_type = M_SYSMOUSE;
740 #endif /* USE_SYSMOUSE */
742 #ifdef USE_TERM_DRIVER
743 CallDriver(sp, td_initmouse);
745 /* we know how to recognize mouse events under "xterm" */
746 if (NonEmpty(key_mouse)) {
747 init_xterm_mouse(sp);
748 } else if (strstr(SP_TERMTYPE term_names, "xterm") != 0) {
749 if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
750 init_xterm_mouse(sp);
758 _nc_mouse_init(SCREEN *sp)
759 /* initialize the mouse */
764 if (!sp->_mouse_initialized) {
767 sp->_mouse_initialized = TRUE;
769 TR(MY_TRACE, ("_nc_mouse_init() called"));
771 sp->_mouse_eventp = FirstEV(sp);
772 for (i = 0; i < EV_MAX; i++)
773 Invalidate(sp->_mouse_events + i);
775 initialize_mousetype(sp);
777 T(("_nc_mouse_init() set mousetype to %d", sp->_mouse_type));
779 result = sp->_mouse_initialized;
785 * Query to see if there is a pending mouse event. This is called from
786 * fifo_push() in lib_getch.c
789 _nc_mouse_event(SCREEN *sp)
791 MEVENT *eventp = sp->_mouse_eventp;
796 switch (sp->_mouse_type) {
798 /* xterm: never have to query, mouse events are in the keyboard stream */
803 int i, res = read(M_FD(sp), &kbuf, 3); /* Eat the prefix */
805 printf("Got %d chars instead of 3 for prefix.\n", res);
806 for (i = 0; i < res; i++) {
807 if (kbuf[i] != key_mouse[i])
808 printf("Got char %d instead of %d for prefix.\n",
809 (int) kbuf[i], (int) key_mouse[i]);
813 #endif /* USE_EMX_MOUSE */
818 if (sp->_mouse_fd >= 0) {
819 /* query server for event, return TRUE if we find one */
822 switch (my_Gpm_GetEvent(&ev)) {
824 /* Connection closed, drop the mouse. */
828 /* there's only one mouse... */
829 eventp->id = NORMAL_EVENT;
832 switch (ev.type & 0x0f) {
834 if (ev.buttons & GPM_B_LEFT)
835 eventp->bstate |= BUTTON1_PRESSED;
836 if (ev.buttons & GPM_B_MIDDLE)
837 eventp->bstate |= BUTTON2_PRESSED;
838 if (ev.buttons & GPM_B_RIGHT)
839 eventp->bstate |= BUTTON3_PRESSED;
842 if (ev.buttons & GPM_B_LEFT)
843 eventp->bstate |= BUTTON1_RELEASED;
844 if (ev.buttons & GPM_B_MIDDLE)
845 eventp->bstate |= BUTTON2_RELEASED;
846 if (ev.buttons & GPM_B_RIGHT)
847 eventp->bstate |= BUTTON3_RELEASED;
850 eventp->bstate |= REPORT_MOUSE_POSITION;
854 eventp->x = ev.x - 1;
855 eventp->y = ev.y - 1;
858 /* bump the next-free pointer into the circular list */
859 sp->_mouse_eventp = NEXT(eventp);
869 if (sp->_sysmouse_head < sp->_sysmouse_tail) {
870 *eventp = sp->_sysmouse_fifo[sp->_sysmouse_head];
873 * Point the fifo-head to the next possible location. If there
874 * are none, reset the indices. This may be interrupted by the
875 * signal handler, doing essentially the same reset.
877 sp->_sysmouse_head += 1;
878 if (sp->_sysmouse_head == sp->_sysmouse_tail) {
879 sp->_sysmouse_tail = 0;
880 sp->_sysmouse_head = 0;
883 /* bump the next-free pointer into the circular list */
884 sp->_mouse_eventp = eventp = NEXT(eventp);
888 #endif /* USE_SYSMOUSE */
890 #ifdef USE_TERM_DRIVER
892 while (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
893 *eventp = sp->_drv_mouse_fifo[sp->_drv_mouse_head];
896 * Point the fifo-head to the next possible location. If there
897 * are none, reset the indices.
899 sp->_drv_mouse_head += 1;
900 if (sp->_drv_mouse_head == sp->_drv_mouse_tail) {
901 sp->_drv_mouse_tail = 0;
902 sp->_drv_mouse_head = 0;
905 /* bump the next-free pointer into the circular list */
906 sp->_mouse_eventp = eventp = NEXT(eventp);
916 return result; /* true if we found an event */
920 #define PRESS_POSITION(n) \
922 eventp->bstate = MASK_PRESS(n); \
923 sp->_mouse_bstate |= MASK_PRESS(n); \
924 if (button & 0x40) { \
925 eventp->bstate = MASK_RELEASE(n); \
926 sp->_mouse_bstate &= ~MASK_PRESS(n); \
930 #define PRESS_POSITION(n) \
932 eventp->bstate = (mmask_t) ((sp->_mouse_bstate & MASK_PRESS(n)) \
933 ? REPORT_MOUSE_POSITION \
935 sp->_mouse_bstate |= MASK_PRESS(n); \
940 handle_wheel(SCREEN *sp, MEVENT * eventp, int button, int wheel)
944 switch (button & 3) {
947 eventp->bstate = MASK_PRESS(4);
948 /* Do not record in sp->_mouse_bstate; there will be no
949 * corresponding release event.
957 #if NCURSES_MOUSE_VERSION >= 2
958 eventp->bstate = MASK_PRESS(5);
959 /* See comment above for button 4 */
961 /* Ignore this event as it is not a true press of the button */
962 eventp->bstate = REPORT_MOUSE_POSITION;
979 decode_X10_bstate(SCREEN *sp, MEVENT * eventp, unsigned intro)
983 int wheel = (intro & 96) == 96;
989 button = (int) (intro - 152); /* buttons 8-11 */
991 button = (int) (intro - 92); /* buttons 4-7 */
994 button = (intro & 3);
997 if (button > MAX_BUTTONS) {
998 eventp->bstate = REPORT_MOUSE_POSITION;
999 } else if (!handle_wheel(sp, eventp, (int) intro, wheel)) {
1002 * Release events aren't reported for individual buttons, just for
1003 * the button set as a whole. However, because there are normally
1004 * no mouse events under xterm that intervene between press and
1005 * release, we can infer the button actually released by looking at
1006 * the previous event.
1008 if (sp->_mouse_bstate & BUTTON_PRESSED) {
1011 eventp->bstate = BUTTON_RELEASED;
1012 for (b = 1; b <= MAX_BUTTONS; ++b) {
1013 if (!(sp->_mouse_bstate & MASK_PRESS(b)))
1014 eventp->bstate &= ~MASK_RELEASE(b);
1016 sp->_mouse_bstate = 0;
1019 * xterm will return a stream of release-events to let the
1020 * application know where the mouse is going, if private mode
1021 * 1002 or 1003 is enabled.
1023 eventp->bstate = REPORT_MOUSE_POSITION;
1028 eventp->bstate |= BUTTON_SHIFT;
1031 eventp->bstate |= BUTTON_ALT;
1034 eventp->bstate |= BUTTON_CTRL;
1036 result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
1040 /* This code requires that your xterm entry contain the kmous capability and
1041 * that it be set to the \E[M documented in the Xterm Control Sequences
1042 * reference. This is how we arrange for mouse events to be reported via a
1043 * KEY_MOUSE return value from wgetch(). After this value is received,
1044 * _nc_mouse_inline() gets called and is immediately responsible for parsing
1045 * the mouse status information following the prefix.
1047 * The following quotes from the ctlseqs.ms document in the XTerm distribution,
1048 * describing the mouse tracking feature:
1050 * Parameters for all mouse tracking escape sequences generated by xterm encode
1051 * numeric parameters in a single character as value+040. For example, ! is
1054 * On button press or release, xterm sends ESC [ M CbCxCy. The low two bits of
1055 * Cb encode button information: 0=MB1 pressed, 1=MB2 pressed, 2=MB3 pressed,
1056 * 3=release. The upper bits encode what modifiers were down when the button
1057 * was pressed and are added together. 4=Shift, 8=Meta, 16=Control. Cx and Cy
1058 * are the x and y coordinates of the mouse event. The upper left corner is
1061 * (End quote) By the time we get here, we've eaten the key prefix. FYI, the
1062 * loop below is necessary because mouse click info isn't guaranteed to present
1063 * as a single clist item.
1065 * Wheel mice may return buttons 4 and 5 when the wheel is turned. We encode
1066 * those as button presses.
1069 decode_xterm_X10(SCREEN *sp, MEVENT * eventp)
1072 unsigned char kbuf[MAX_KBUF + 1];
1077 # if USE_PTHREADS_EINTR
1078 # if USE_WEAK_SYMBOLS
1079 if ((pthread_self) && (pthread_kill) && (pthread_equal))
1081 _nc_globals.read_thread = pthread_self();
1083 for (grabbed = 0; grabbed < MAX_KBUF; grabbed += (size_t) res) {
1085 /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
1088 (M_FD(sp) >= 0) ? M_FD(sp) : sp->_ifd,
1092 kbuf + grabbed, (size_t) (MAX_KBUF - (int) grabbed));
1096 #if USE_PTHREADS_EINTR
1097 _nc_globals.read_thread = 0;
1099 kbuf[MAX_KBUF] = '\0';
1102 ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
1104 /* there's only one mouse... */
1105 eventp->id = NORMAL_EVENT;
1107 result = decode_X10_bstate(sp, eventp, kbuf[0]);
1109 eventp->x = (kbuf[1] - ' ') - 1;
1110 eventp->y = (kbuf[2] - ' ') - 1;
1115 #ifdef EXP_XTERM_1005
1117 * This is identical to X10/X11 responses except that there are two UTF-8
1118 * characters storing the ordinates instead of two bytes.
1121 decode_xterm_1005(SCREEN *sp, MEVENT * eventp)
1125 size_t limit = (sizeof(kbuf) - 1);
1132 # if USE_PTHREADS_EINTR
1133 # if USE_WEAK_SYMBOLS
1134 if ((pthread_self) && (pthread_kill) && (pthread_equal))
1136 _nc_globals.read_thread = pthread_self();
1138 for (grabbed = 0; grabbed < limit;) {
1143 (M_FD(sp) >= 0) ? M_FD(sp) : sp->_ifd,
1147 (kbuf + grabbed), (size_t) 1);
1150 grabbed += (size_t) res;
1155 for (n = 0; n < 2; ++n) {
1158 if (check >= grabbed)
1160 rc = _nc_conv_to_utf32(&coords[n], kbuf + check, (unsigned)
1164 check += (size_t) rc;
1170 #if USE_PTHREADS_EINTR
1171 _nc_globals.read_thread = 0;
1175 ("_nc_mouse_inline sees the following xterm data: %s",
1176 _nc_visbufn(kbuf, (int) grabbed)));
1178 /* there's only one mouse... */
1179 eventp->id = NORMAL_EVENT;
1181 result = decode_X10_bstate(sp, eventp, UChar(kbuf[0]));
1183 eventp->x = (int) (coords[0] - ' ') - 1;
1184 eventp->y = (int) (coords[1] - ' ') - 1;
1188 #endif /* EXP_XTERM_1005 */
1191 * ECMA-48 section 5.4
1193 #define isInter(c) ((c) >= 0x20 && (c) <= 0x2f)
1194 #define isParam(c) ((c) >= 0x30 && (c) <= 0x3f)
1195 #define isFinal(c) ((c) >= 0x40 && (c) <= 0x7e)
1197 #define MAX_PARAMS 9
1200 int nerror; /* nonzero if there are unexpected chars */
1201 int nparam; /* number of numeric parameters */
1202 int params[MAX_PARAMS];
1203 int final; /* the final-character */
1207 read_SGR(SCREEN *sp, SGR_DATA * result)
1209 char kbuf[80]; /* bigger than any possible mouse response */
1215 memset(result, 0, sizeof(*result));
1216 # if USE_PTHREADS_EINTR
1217 # if USE_WEAK_SYMBOLS
1218 if ((pthread_self) && (pthread_kill) && (pthread_equal))
1220 _nc_globals.read_thread = pthread_self();
1228 (M_FD(sp) >= 0) ? M_FD(sp) : sp->_ifd,
1232 (kbuf + grabbed), (size_t) 1);
1235 if ((grabbed + MAX_KBUF) >= (int) sizeof(kbuf)) {
1239 ch = UChar(kbuf[grabbed]);
1240 kbuf[grabbed + 1] = 0;
1254 result->nparam = (now + 1);
1257 result->params[now] = (result->params[now] * 10) + (ch - '0');
1262 result->nparam = (now + 1);
1267 if (ch < 32 || ch > 126) {
1269 * Technically other characters could be interspersed in the
1270 * response. Ignore those for now.
1274 } else if (isFinal(ch)) {
1285 } while (!isFinal(ch));
1286 #if USE_PTHREADS_EINTR
1287 _nc_globals.read_thread = 0;
1290 kbuf[++grabbed] = 0;
1292 ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
1293 return (grabbed > 0) && (result->nerror == 0);
1297 decode_xterm_SGR1006(SCREEN *sp, MEVENT * eventp)
1300 bool result = FALSE;
1301 if (read_SGR(sp, &data)) {
1302 int b = data.params[0];
1303 int b3 = 1 + (b & 3);
1304 int wheel = ((b & 64) == 64);
1307 b3 = MAX_BUTTONS + 1;
1308 } else if (b >= 128) {
1309 b3 = (b - 120); /* buttons 8-11 */
1310 } else if (b >= 64) {
1311 b3 = (b - 60); /* buttons 6-7 */
1314 eventp->id = NORMAL_EVENT;
1315 if (data.final == 'M') {
1316 (void) handle_wheel(sp, eventp, b, wheel);
1317 } else if (b3 > MAX_BUTTONS) {
1318 eventp->bstate = REPORT_MOUSE_POSITION;
1320 mmask_t pressed = (mmask_t) NCURSES_MOUSE_MASK(b3, NCURSES_BUTTON_PRESSED);
1321 mmask_t release = (mmask_t) NCURSES_MOUSE_MASK(b3, NCURSES_BUTTON_RELEASED);
1322 if (sp->_mouse_bstate & pressed) {
1323 eventp->bstate = release;
1324 sp->_mouse_bstate &= ~pressed;
1326 eventp->bstate = REPORT_MOUSE_POSITION;
1330 eventp->bstate |= BUTTON_SHIFT;
1333 eventp->bstate |= BUTTON_ALT;
1336 eventp->bstate |= BUTTON_CTRL;
1338 result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
1339 eventp->x = (data.params[1] ? (data.params[1] - 1) : 0);
1340 eventp->y = (data.params[2] ? (data.params[2] - 1) : 0);
1346 _nc_mouse_inline(SCREEN *sp)
1347 /* mouse report received in the keyboard stream -- parse its info */
1349 bool result = FALSE;
1350 MEVENT *eventp = sp->_mouse_eventp;
1352 TR(MY_TRACE, ("_nc_mouse_inline() called"));
1354 if (sp->_mouse_type == M_XTERM) {
1355 switch (sp->_mouse_format) {
1357 result = decode_xterm_X10(sp, eventp);
1360 result = decode_xterm_SGR1006(sp, eventp);
1362 #ifdef EXP_XTERM_1005
1364 result = decode_xterm_1005(sp, eventp);
1370 ("_nc_mouse_inline: primitive mouse-event %s has slot %ld",
1371 _nc_tracemouse(sp, eventp),
1372 (long) IndexEV(sp, eventp)));
1374 /* bump the next-free pointer into the circular list */
1375 sp->_mouse_eventp = NEXT(eventp);
1378 /* If this event is from a wheel-mouse, treat it like position
1379 * reports and avoid waiting for the release-events which will
1382 if (eventp->bstate & BUTTON_PRESSED) {
1385 for (b = 4; b <= MAX_BUTTONS; ++b) {
1386 if ((eventp->bstate & MASK_PRESS(b))) {
1399 mouse_activate(SCREEN *sp, int on)
1401 if (!on && !sp->_mouse_initialized)
1404 if (!_nc_mouse_init(sp))
1408 sp->_mouse_bstate = 0;
1409 switch (sp->_mouse_type) {
1411 #if NCURSES_EXT_FUNCS
1412 NCURSES_SP_NAME(keyok) (NCURSES_SP_ARGx KEY_MOUSE, on);
1414 TPUTS_TRACE("xterm mouse initialization");
1415 enable_xterm_mouse(sp, 1);
1419 if (enable_gpm_mouse(sp, TRUE)) {
1420 sp->_mouse_fd = *(my_gpm_fd);
1421 T(("GPM mouse_fd %d", sp->_mouse_fd));
1427 signal(SIGUSR2, handle_sysmouse);
1428 sp->_mouse_active = TRUE;
1431 #ifdef USE_TERM_DRIVER
1433 sp->_mouse_active = TRUE;
1439 /* Make runtime binding to cut down on object size of applications that
1440 * do not use the mouse (e.g., 'clear').
1442 sp->_mouse_event = _nc_mouse_event;
1443 sp->_mouse_inline = _nc_mouse_inline;
1444 sp->_mouse_parse = _nc_mouse_parse;
1445 sp->_mouse_resume = _nc_mouse_resume;
1446 sp->_mouse_wrap = _nc_mouse_wrap;
1449 switch (sp->_mouse_type) {
1451 TPUTS_TRACE("xterm mouse deinitialization");
1452 enable_xterm_mouse(sp, 0);
1456 enable_gpm_mouse(sp, FALSE);
1461 signal(SIGUSR2, SIG_IGN);
1462 sp->_mouse_active = FALSE;
1465 #ifdef USE_TERM_DRIVER
1467 sp->_mouse_active = FALSE;
1474 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
1477 /**************************************************************************
1479 * Device-independent code
1481 **************************************************************************/
1484 _nc_mouse_parse(SCREEN *sp, int runcount)
1485 /* parse a run of atomic mouse events into a gesture */
1487 MEVENT *eventp = sp->_mouse_eventp;
1489 MEVENT *first_valid = NULL;
1490 MEVENT *first_invalid = NULL;
1496 TR(MY_TRACE, ("_nc_mouse_parse(%d) called", runcount));
1499 * When we enter this routine, the event list next-free pointer
1500 * points just past a run of mouse events that we know were separated
1501 * in time by less than the critical click interval. The job of this
1502 * routine is to collapse this run into a single higher-level event
1505 * We accomplish this in two passes. The first pass merges press/release
1506 * pairs into click events. The second merges runs of click events into
1507 * double or triple-click events.
1509 * It's possible that the run may not resolve to a single event (for
1510 * example, if the user quadruple-clicks). If so, leading events
1511 * in the run are ignored if user does not call getmouse in a loop (getting
1512 * them from newest to older).
1514 * Note that this routine is independent of the format of the specific
1515 * format of the pointing-device's reports. We can use it to parse
1516 * gestures on anything that reports press/release events on a per-
1517 * button basis, as long as the device-dependent mouse code puts stuff
1518 * on the queue in MEVENT format.
1522 * Reset all events that were not set, in case the user sometimes calls
1523 * getmouse only once and other times until there are no more events in
1526 * This also allows reaching the beginning of the run.
1529 for (n = runcount; n < EV_MAX; n++) {
1535 if (USE_TRACEF(TRACE_IEVENT)) {
1536 _trace_slot(sp, "before mouse press/release merge:");
1537 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1538 RunParams(sp, eventp, ep),
1540 _nc_unlock_global(tracef);
1544 /* first pass; merge press/release pairs */
1548 if (next == eventp) {
1549 /* Will end the loop, but compact before */
1553 #define MASK_CHANGED(x) (!(ep->bstate & MASK_PRESS(x)) \
1554 == !(next->bstate & MASK_RELEASE(x)))
1556 if (ValidEvent(ep) && ValidEvent(next)
1557 && ep->x == next->x && ep->y == next->y
1558 && (ep->bstate & BUTTON_PRESSED)
1559 && (!(next->bstate & BUTTON_PRESSED))) {
1560 bool changed = TRUE;
1562 for (b = 1; b <= MAX_BUTTONS; ++b) {
1563 if (!MASK_CHANGED(b)) {
1571 for (b = 1; b <= MAX_BUTTONS; ++b) {
1572 if ((sp->_mouse_mask & MASK_CLICK(b))
1573 && (ep->bstate & MASK_PRESS(b))) {
1574 next->bstate &= ~MASK_RELEASE(b);
1575 next->bstate |= MASK_CLICK(b);
1586 /* Compact valid events */
1587 if (!ValidEvent(ep)) {
1588 if ((first_valid != NULL) && (first_invalid == NULL)) {
1592 if (first_valid == NULL) {
1594 } else if (first_invalid != NULL) {
1595 *first_invalid = *ep;
1597 first_invalid = NEXT(first_invalid);
1604 if (first_invalid != NULL) {
1605 eventp = first_invalid;
1608 if (USE_TRACEF(TRACE_IEVENT)) {
1609 _trace_slot(sp, "before mouse click merge:");
1610 if (first_valid == NULL) {
1611 _tracef("_nc_mouse_parse: no valid event");
1613 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1614 RunParams(sp, eventp, first_valid),
1616 _nc_unlock_global(tracef);
1622 * Second pass; merge click runs. We merge click events forward in the
1623 * queue. For example, double click can be changed to triple click.
1625 * NOTE: There is a problem with this design! If the application
1626 * allows enough click events to pile up in the circular queue so
1627 * they wrap around, it will cheerfully merge the newest forward
1628 * into the oldest, creating a bogus doubleclick and confusing
1629 * the queue-traversal logic rather badly. Generally this won't
1630 * happen, because calling getmouse() marks old events invalid and
1631 * ineligible for merges. The true solution to this problem would
1632 * be to timestamp each MEVENT and perform the obvious sanity check,
1633 * but the timer element would have to have sub-second resolution,
1634 * which would get us into portability trouble.
1636 first_invalid = NULL;
1637 endLoop = (first_valid == NULL);
1642 if (next == eventp) {
1643 /* Will end the loop, but check event type and compact before */
1645 } else if (!ValidEvent(next)) {
1648 /* merge click events forward */
1649 if ((ep->bstate & BUTTON_CLICKED)
1650 && (next->bstate & BUTTON_CLICKED)) {
1652 for (b = 1; b <= MAX_BUTTONS; ++b) {
1653 if ((sp->_mouse_mask & MASK_DOUBLE_CLICK(b))
1654 && (ep->bstate & MASK_CLICK(b))
1655 && (next->bstate & MASK_CLICK(b))) {
1656 next->bstate &= ~MASK_CLICK(b);
1657 next->bstate |= MASK_DOUBLE_CLICK(b);
1666 /* merge double-click events forward */
1667 if ((ep->bstate & BUTTON_DOUBLE_CLICKED)
1668 && (next->bstate & BUTTON_CLICKED)) {
1670 for (b = 1; b <= MAX_BUTTONS; ++b) {
1671 if ((sp->_mouse_mask & MASK_TRIPLE_CLICK(b))
1672 && (ep->bstate & MASK_DOUBLE_CLICK(b))
1673 && (next->bstate & MASK_CLICK(b))) {
1674 next->bstate &= ~MASK_CLICK(b);
1675 next->bstate |= MASK_TRIPLE_CLICK(b);
1685 /* Discard event if it does not match event mask */
1686 if (!(ep->bstate & sp->_mouse_mask2)) {
1690 /* Compact valid events */
1691 if (!ValidEvent(ep)) {
1692 if (ep == first_valid) {
1694 } else if (first_invalid == NULL) {
1697 } else if (first_invalid != NULL) {
1698 *first_invalid = *ep;
1700 first_invalid = NEXT(first_invalid);
1706 if (first_invalid == NULL) {
1707 first_invalid = eventp;
1709 sp->_mouse_eventp = first_invalid;
1712 if (first_valid != NULL) {
1713 if (USE_TRACEF(TRACE_IEVENT)) {
1714 _trace_slot(sp, "after mouse event queue compaction:");
1715 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1716 RunParams(sp, first_invalid, first_valid),
1718 _nc_unlock_global(tracef);
1720 for (ep = first_valid; ep != first_invalid; ep = NEXT(ep)) {
1723 ("_nc_mouse_parse: returning composite mouse event %s at slot %ld",
1724 _nc_tracemouse(sp, ep),
1725 (long) IndexEV(sp, ep)));
1730 /* after all this, do we have a valid event? */
1731 return ValidEvent(PREV(first_invalid));
1735 _nc_mouse_wrap(SCREEN *sp)
1736 /* release mouse -- called by endwin() before shellout/exit */
1738 TR(MY_TRACE, ("_nc_mouse_wrap() called"));
1740 switch (sp->_mouse_type) {
1742 if (sp->_mouse_mask)
1743 mouse_activate(sp, FALSE);
1746 /* GPM: pass all mouse events to next client */
1748 if (sp->_mouse_mask)
1749 mouse_activate(sp, FALSE);
1754 mouse_activate(sp, FALSE);
1757 #ifdef USE_TERM_DRIVER
1759 mouse_activate(sp, FALSE);
1768 _nc_mouse_resume(SCREEN *sp)
1769 /* re-connect to mouse -- called by doupdate() after shellout */
1771 TR(MY_TRACE, ("_nc_mouse_resume() called"));
1773 switch (sp->_mouse_type) {
1775 /* xterm: re-enable reporting */
1776 if (sp->_mouse_mask)
1777 mouse_activate(sp, TRUE);
1782 /* GPM: reclaim our event set */
1783 if (sp->_mouse_mask)
1784 mouse_activate(sp, TRUE);
1790 mouse_activate(sp, TRUE);
1794 #ifdef USE_TERM_DRIVER
1796 mouse_activate(sp, TRUE);
1805 /**************************************************************************
1807 * Mouse interface entry points for the API
1809 **************************************************************************/
1812 NCURSES_SP_NAME(getmouse) (NCURSES_SP_DCLx MEVENT * aevent)
1817 T((T_CALLED("getmouse(%p,%p)"), (void *) SP_PARM, (void *) aevent));
1819 if ((aevent != 0) &&
1821 (SP_PARM->_mouse_type != M_NONE) &&
1822 (eventp = SP_PARM->_mouse_eventp) != 0) {
1823 /* compute the current-event pointer */
1824 MEVENT *prev = PREV(eventp);
1827 * Discard events not matching mask (there could be still some if
1828 * _nc_mouse_parse was not called, e.g., when _nc_mouse_inline returns
1831 while (ValidEvent(prev) && (!(prev->bstate & SP_PARM->_mouse_mask2))) {
1835 if (ValidEvent(prev)) {
1836 /* copy the event we find there */
1839 TR(TRACE_IEVENT, ("getmouse: returning event %s from slot %ld",
1840 _nc_tracemouse(SP_PARM, prev),
1841 (long) IndexEV(SP_PARM, prev)));
1843 Invalidate(prev); /* so the queue slot becomes free */
1844 SP_PARM->_mouse_eventp = prev;
1847 /* Reset the provided event */
1858 #if NCURSES_SP_FUNCS
1859 /* grab a copy of the current mouse event */
1861 getmouse(MEVENT * aevent)
1863 return NCURSES_SP_NAME(getmouse) (CURRENT_SCREEN, aevent);
1868 NCURSES_SP_NAME(ungetmouse) (NCURSES_SP_DCLx MEVENT * aevent)
1873 T((T_CALLED("ungetmouse(%p,%p)"), (void *) SP_PARM, (void *) aevent));
1877 (eventp = SP_PARM->_mouse_eventp) != 0) {
1879 /* stick the given event in the next-free slot */
1882 /* bump the next-free pointer into the circular list */
1883 SP_PARM->_mouse_eventp = NEXT(eventp);
1885 /* push back the notification event on the keyboard queue */
1886 result = NCURSES_SP_NAME(ungetch) (NCURSES_SP_ARGx KEY_MOUSE);
1891 #if NCURSES_SP_FUNCS
1892 /* enqueue a synthesized mouse event to be seen by the next wgetch() */
1894 ungetmouse(MEVENT * aevent)
1896 return NCURSES_SP_NAME(ungetmouse) (CURRENT_SCREEN, aevent);
1900 NCURSES_EXPORT(mmask_t)
1901 NCURSES_SP_NAME(mousemask) (NCURSES_SP_DCLx mmask_t newmask, mmask_t * oldmask)
1902 /* set the mouse event mask */
1906 T((T_CALLED("mousemask(%p,%#lx,%p)"),
1908 (unsigned long) newmask,
1913 *oldmask = SP_PARM->_mouse_mask;
1915 if (newmask || SP_PARM->_mouse_initialized) {
1916 _nc_mouse_init(SP_PARM);
1918 if (SP_PARM->_mouse_type != M_NONE) {
1922 (REPORT_MOUSE_POSITION
1929 | BUTTON_DOUBLE_CLICKED
1930 | BUTTON_TRIPLE_CLICKED);
1932 mouse_activate(SP_PARM, (bool) (result != 0));
1934 SP_PARM->_mouse_mask = result;
1935 SP_PARM->_mouse_mask2 = result;
1938 * Make a mask corresponding to the states we will need to
1939 * retain (temporarily) while building up the state that the
1942 for (b = 1; b <= MAX_BUTTONS; ++b) {
1943 if (SP_PARM->_mouse_mask2 & MASK_TRIPLE_CLICK(b))
1944 SP_PARM->_mouse_mask2 |= MASK_DOUBLE_CLICK(b);
1945 if (SP_PARM->_mouse_mask2 & MASK_DOUBLE_CLICK(b))
1946 SP_PARM->_mouse_mask2 |= MASK_CLICK(b);
1947 if (SP_PARM->_mouse_mask2 & MASK_CLICK(b))
1948 SP_PARM->_mouse_mask2 |= (MASK_PRESS(b) |
1954 returnMMask(result);
1957 #if NCURSES_SP_FUNCS
1958 NCURSES_EXPORT(mmask_t)
1959 mousemask(mmask_t newmask, mmask_t * oldmask)
1961 return NCURSES_SP_NAME(mousemask) (CURRENT_SCREEN, newmask, oldmask);
1965 NCURSES_EXPORT(bool)
1966 wenclose(const WINDOW *win, int y, int x)
1967 /* check to see if given window encloses given screen location */
1969 bool result = FALSE;
1971 T((T_CALLED("wenclose(%p,%d,%d)"), (const void *) win, y, x));
1975 result = ((win->_begy <= y &&
1977 (win->_begx + win->_maxx) >= x &&
1978 (win->_begy + win->_maxy) >= y) ? TRUE : FALSE);
1984 NCURSES_SP_NAME(mouseinterval) (NCURSES_SP_DCLx int maxclick)
1985 /* set the maximum mouse interval within which to recognize a click */
1989 T((T_CALLED("mouseinterval(%p,%d)"), (void *) SP_PARM, maxclick));
1992 oldval = SP_PARM->_maxclick;
1994 SP_PARM->_maxclick = maxclick;
1996 oldval = DEFAULT_MAXCLICK;
2002 #if NCURSES_SP_FUNCS
2004 mouseinterval(int maxclick)
2006 return NCURSES_SP_NAME(mouseinterval) (CURRENT_SCREEN, maxclick);
2010 /* This may be used by other routines to ask for the existence of mouse
2012 NCURSES_EXPORT(bool)
2013 _nc_has_mouse(SCREEN *sp)
2015 return (((0 == sp) || (sp->_mouse_type == M_NONE)) ? FALSE : TRUE);
2018 NCURSES_EXPORT(bool)
2019 NCURSES_SP_NAME(has_mouse) (NCURSES_SP_DCL0)
2021 return _nc_has_mouse(SP_PARM);
2024 #if NCURSES_SP_FUNCS
2025 NCURSES_EXPORT(bool)
2028 return _nc_has_mouse(CURRENT_SCREEN);
2032 NCURSES_EXPORT(bool)
2033 wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
2035 bool result = FALSE;
2037 T((T_CALLED("wmouse_trafo(%p,%p,%p,%d)"),
2043 if (win && pY && pX) {
2048 y += win->_begy + win->_yoffset;
2050 if (wenclose(win, y, x))
2053 if (wenclose(win, y, x)) {
2054 y -= (win->_begy + win->_yoffset);