1 /****************************************************************************
2 * Copyright (c) 1998-2002,2003 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-2003 *
33 ****************************************************************************/
36 * This module is intended to encapsulate ncurses's interface to pointing
39 * The first method used is xterm's internal mouse-tracking facility.
40 * The second is Alessandro Rubini's GPM server.
42 * Notes for implementors of new mouse-interface methods:
44 * The code is logically split into a lower level that accepts event reports
45 * in a device-dependent format and an upper level that parses mouse gestures
46 * and filters events. The mediating data structure is a circular queue of
49 * Functionally, the lower level's job is to pick up primitive events and
50 * put them on the circular queue. This can happen in one of two ways:
51 * either (a) _nc_mouse_event() detects a series of incoming mouse reports
52 * and queues them, or (b) code in lib_getch.c detects the kmous prefix in
53 * the keyboard input stream and calls _nc_mouse_inline to queue up a series
54 * of adjacent mouse reports.
56 * In either case, _nc_mouse_parse() should be called after the series is
57 * accepted to parse the digested mouse reports (low-level MEVENTs) into
58 * a gesture (a high-level or composite MEVENT).
60 * Don't be too shy about adding new event types or modifiers, if you can find
61 * room for them in the 32-bit mask. The API is written so that users get
62 * feedback on which theoretical event types they won't see when they call
63 * mousemask. There's one bit per button (the RESERVED_EVENT bit) not being
64 * used yet, and a couple of bits open at the high end.
73 # define INCL_DOSPROCESS
74 # include <os2.h> /* Need to include before the others */
77 #include <curses.priv.h>
79 MODULE_ID("$Id: lib_mouse.c,v 1.68 2003/11/08 21:50:50 tom Exp $")
85 #ifndef LINT /* don't need this for llib-lncurses */
86 #undef buttons /* term.h defines this, and gpm uses it! */
88 #include <linux/keyboard.h> /* defines KG_* macros */
93 #undef buttons /* symbol conflict in consio.h */
94 #undef mouse_info /* symbol conflict in consio.h */
95 #include <osreldate.h>
96 #if (__FreeBSD_version >= 400017)
97 #include <sys/consio.h>
100 #include <machine/console.h>
102 #endif /* use_SYSMOUSE */
104 #define MY_TRACE TRACE_ICALLS|TRACE_IEVENT
106 #define MASK_RELEASE(x) ((001 << (6 * ((x) - 1))))
107 #define MASK_PRESS(x) ((002 << (6 * ((x) - 1))))
108 #define MASK_CLICK(x) ((004 << (6 * ((x) - 1))))
109 #define MASK_DOUBLE_CLICK(x) ((010 << (6 * ((x) - 1))))
110 #define MASK_TRIPLE_CLICK(x) ((020 << (6 * ((x) - 1))))
111 #define MASK_RESERVED_EVENT(x) ((040 << (6 * ((x) - 1))))
113 #define BUTTON_CLICKED (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED)
114 #define BUTTON_PRESSED (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED)
115 #define BUTTON_RELEASED (BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON3_RELEASED)
117 #define INVALID_EVENT -1
118 #define NORMAL_EVENT 0
122 static Gpm_Connect gpm_connect;
126 static mmask_t eventmask; /* current event mask */
128 static bool _nc_mouse_parse(int);
129 static void _nc_mouse_resume(SCREEN *);
130 static void _nc_mouse_wrap(SCREEN *);
132 /* maintain a circular list of mouse events */
134 /* The definition of the circular list size (EV_MAX), is in curses.priv.h, so
135 * wgetch() may refer to the size and call _nc_mouse_parse() before circular
138 static MEVENT events[EV_MAX]; /* hold the last mouse event seen */
139 static MEVENT *eventp = events; /* next free slot in event queue */
142 #define NEXT(ep) ((ep == events + EV_MAX - 1) ? events : ep + 1)
145 #define PREV(ep) ((ep == events) ? events + EV_MAX - 1 : ep - 1)
149 _trace_slot(const char *tag)
155 for (ep = events; ep < events + EV_MAX; ep++)
156 _tracef("mouse event queue slot %ld = %s",
157 (long) (ep - events),
167 static int mouse_wfd;
168 static int mouse_thread;
169 static int mouse_activated;
170 static char mouse_buttons[] =
173 # define M_FD(sp) sp->_mouse_fd
176 write_event(int down, int button, int x, int y)
179 unsigned long ignore;
181 strncpy(buf, key_mouse, 3); /* should be "\033[M" */
182 buf[3] = ' ' + (button - 1) + (down ? 0 : 0x40);
183 buf[4] = ' ' + x - LEFT_COL + 1;
184 buf[5] = ' ' + y - TOP_ROW + 1;
185 DosWrite(mouse_wfd, buf, 6, &ignore);
189 mouse_server(unsigned long ignored GCC_UNUSED)
191 unsigned short fWait = MOU_WAIT;
192 /* NOPTRRECT mourt = { 0,0,24,79 }; */
195 unsigned short mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;
201 /* open the handle for the mouse */
202 if (MouOpen(NULL, &hmou) == 0) {
203 rc = MouSetEventMask(&mask, hmou);
204 if (rc) { /* retry with 2 buttons */
205 mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN;
206 rc = MouSetEventMask(&mask, hmou);
209 if (rc == 0 && MouDrawPtr(hmou) == 0) {
211 /* sit and wait on the event queue */
212 rc = MouReadEventQue(&mouev, &fWait, hmou);
214 sprintf(err, "Error reading mouse queue, rc=%lu.\r\n", rc);
217 if (!mouse_activated)
221 * OS/2 numbers a 3-button mouse inconsistently from other
227 if ((mouev.fs ^ oldstate) & MOUSE_BN1_DOWN)
228 write_event(mouev.fs & MOUSE_BN1_DOWN,
229 mouse_buttons[1], mouev.col, mouev.row);
230 if ((mouev.fs ^ oldstate) & MOUSE_BN2_DOWN)
231 write_event(mouev.fs & MOUSE_BN2_DOWN,
232 mouse_buttons[3], mouev.col, mouev.row);
233 if ((mouev.fs ^ oldstate) & MOUSE_BN3_DOWN)
234 write_event(mouev.fs & MOUSE_BN3_DOWN,
235 mouse_buttons[2], mouev.col, mouev.row);
241 sprintf(err, "Error setting event mask, buttons=%d, rc=%lu.\r\n",
244 DosWrite(2, err, strlen(err), &rc);
247 DosExit(EXIT_THREAD, 0L);
251 server_state(const int state)
252 { /* It would be nice to implement pointer-off and stop looping... */
253 mouse_activated = state;
256 #endif /* USE_EMX_MOUSE */
260 handle_sysmouse(int sig GCC_UNUSED)
262 struct mouse_info the_mouse;
265 the_mouse.operation = MOUSE_GETINFO;
267 && SP->_mouse_fd >= 0
268 && SP->_sysmouse_tail < FIFO_SIZE
269 && ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
271 if (SP->_sysmouse_head > SP->_sysmouse_tail) {
272 SP->_sysmouse_tail = 0;
273 SP->_sysmouse_head = 0;
275 work = &(SP->_sysmouse_fifo[SP->_sysmouse_tail]);
276 memset(work, 0, sizeof(*work));
277 work->id = NORMAL_EVENT; /* there's only one mouse... */
279 SP->_sysmouse_old_buttons = SP->_sysmouse_new_buttons;
280 SP->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
282 if (SP->_sysmouse_new_buttons) {
283 if (SP->_sysmouse_new_buttons & 1)
284 work->bstate |= BUTTON1_PRESSED;
285 if (SP->_sysmouse_new_buttons & 2)
286 work->bstate |= BUTTON2_PRESSED;
287 if (SP->_sysmouse_new_buttons & 4)
288 work->bstate |= BUTTON3_PRESSED;
290 if (SP->_sysmouse_old_buttons & 1)
291 work->bstate |= BUTTON1_RELEASED;
292 if (SP->_sysmouse_old_buttons & 2)
293 work->bstate |= BUTTON2_RELEASED;
294 if (SP->_sysmouse_old_buttons & 4)
295 work->bstate |= BUTTON3_RELEASED;
298 /* for cosmetic bug in syscons.c on FreeBSD 3.[34] */
299 the_mouse.operation = MOUSE_HIDE;
300 ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse);
301 the_mouse.operation = MOUSE_SHOW;
302 ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse);
305 * We're only interested if the button is pressed or released.
306 * FIXME: implement continuous event-tracking.
308 if (SP->_sysmouse_new_buttons != SP->_sysmouse_old_buttons) {
309 SP->_sysmouse_tail += 1;
311 work->x = the_mouse.u.data.x / SP->_sysmouse_char_width;
312 work->y = the_mouse.u.data.y / SP->_sysmouse_char_height;
317 static int initialized;
320 init_xterm_mouse(void)
322 SP->_mouse_type = M_XTERM;
323 SP->_mouse_xtermcap = tigetstr("XM");
324 if (!VALID_STRING(SP->_mouse_xtermcap))
325 SP->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
330 enable_xterm_mouse(int enable)
332 putp(tparm(SP->_mouse_xtermcap, enable));
334 #endif /* !USE_EMX_MOUSE */
337 initialize_mousetype(void)
339 static const char *xterm_kmous = "\033[M";
341 /* Try gpm first, because gpm may be configured to run in xterm */
343 /* GPM does printf's without checking if stdout is a terminal */
344 if (isatty(fileno(stdout))) {
345 /* GPM: initialize connection to gpm server */
346 gpm_connect.eventMask = GPM_DOWN | GPM_UP;
347 gpm_connect.defaultMask = ~(gpm_connect.eventMask | GPM_HARD);
348 gpm_connect.minMod = 0;
349 gpm_connect.maxMod = ~((1 << KG_SHIFT) | (1 << KG_SHIFTL) | (1 << KG_SHIFTR));
350 if (Gpm_Open(&gpm_connect, 0) >= 0) { /* returns the file-descriptor */
351 SP->_mouse_type = M_GPM;
352 SP->_mouse_fd = gpm_fd;
361 && strstr(cur_term->type.term_names, "xterm") == 0
365 if (pipe(handles) < 0) {
366 perror("mouse pipe error");
371 if (!mouse_buttons[0]) {
372 char *s = getenv("MOUSE_BUTTONS_123");
374 mouse_buttons[0] = 1;
375 if (s && strlen(s) >= 3) {
376 mouse_buttons[1] = s[0] - '0';
377 mouse_buttons[2] = s[1] - '0';
378 mouse_buttons[3] = s[2] - '0';
381 mouse_wfd = handles[1];
382 M_FD(SP) = handles[0];
384 setmode(handles[0], O_BINARY);
385 setmode(handles[1], O_BINARY);
386 /* Do not use CRT functions, we may single-threaded. */
387 rc = DosCreateThread((unsigned long *) &mouse_thread,
388 mouse_server, 0, 0, 8192);
390 printf("mouse thread error %d=%#x", rc, rc);
393 SP->_mouse_type = M_XTERM;
402 struct mouse_info the_mouse;
403 char *the_device = 0;
405 if (isatty(SP->_ifd))
406 the_device = ttyname(SP->_ifd);
408 the_device = "/dev/tty";
410 SP->_mouse_fd = open(the_device, O_RDWR);
412 if (SP->_mouse_fd >= 0) {
414 * sysmouse does not have a usable user interface for obtaining
415 * mouse events. The logical way to proceed (reading data on a
416 * stream) only works if one opens the device as root. Even in
417 * that mode, careful examination shows we lose events
418 * occasionally. The interface provided for user programs is to
419 * establish a signal handler. really.
421 * Take over SIGUSR2 for this purpose since SIGUSR1 is more
422 * likely to be used by an application. getch() will have to
423 * handle the misleading EINTR's.
425 signal(SIGUSR2, SIG_IGN);
426 the_mouse.operation = MOUSE_MODE;
427 the_mouse.u.mode.mode = 0;
428 the_mouse.u.mode.signal = SIGUSR2;
429 if (ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
430 signal(SIGUSR2, handle_sysmouse);
431 the_mouse.operation = MOUSE_SHOW;
432 ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse);
434 #if defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) /* FreeBSD > 2.x */
436 #ifndef FBIO_GETMODE /* FreeBSD 3.x */
437 #define FBIO_GETMODE CONS_GET
438 #define FBIO_MODEINFO CONS_MODEINFO
439 #endif /* FBIO_GETMODE */
440 video_info_t the_video;
442 if (ioctl(SP->_mouse_fd,
444 &the_video.vi_mode) != -1
445 && ioctl(SP->_mouse_fd,
448 SP->_sysmouse_char_width = the_video.vi_cwidth;
449 SP->_sysmouse_char_height = the_video.vi_cheight;
452 #endif /* defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) */
454 if (SP->_sysmouse_char_width <= 0)
455 SP->_sysmouse_char_width = 8;
456 if (SP->_sysmouse_char_height <= 0)
457 SP->_sysmouse_char_height = 16;
458 SP->_mouse_type = M_SYSMOUSE;
463 #endif /* USE_SYSMOUSE */
465 /* we know how to recognize mouse events under "xterm" */
466 if (key_mouse != 0) {
467 if (!strcmp(key_mouse, xterm_kmous)) {
471 } else if (strstr(cur_term->type.term_names, "xterm") != 0) {
472 (void) _nc_add_to_try(&(SP->_keytry), xterm_kmous, KEY_MOUSE);
480 /* initialize the mouse */
487 TR(MY_TRACE, ("_nc_mouse_init() called"));
489 for (i = 0; i < EV_MAX; i++)
490 events[i].id = INVALID_EVENT;
492 initialize_mousetype();
494 T(("_nc_mouse_init() set mousetype to %d", SP->_mouse_type));
499 * Query to see if there is a pending mouse event. This is called from
500 * fifo_push() in lib_getch.c
503 _nc_mouse_event(SCREEN * sp GCC_UNUSED)
507 switch (SP->_mouse_type) {
509 /* xterm: never have to query, mouse events are in the keyboard stream */
514 int i, res = read(M_FD(sp), &kbuf, 3); /* Eat the prefix */
516 printf("Got %d chars instead of 3 for prefix.\n", res);
517 for (i = 0; i < res; i++) {
518 if (kbuf[i] != key_mouse[i])
519 printf("Got char %d instead of %d for prefix.\n",
520 (int) kbuf[i], (int) key_mouse[i]);
524 #endif /* USE_EMX_MOUSE */
530 /* query server for event, return TRUE if we find one */
533 if (Gpm_GetEvent(&ev) == 1) {
534 /* there's only one mouse... */
535 eventp->id = NORMAL_EVENT;
538 switch (ev.type & 0x0f) {
540 if (ev.buttons & GPM_B_LEFT)
541 eventp->bstate |= BUTTON1_PRESSED;
542 if (ev.buttons & GPM_B_MIDDLE)
543 eventp->bstate |= BUTTON2_PRESSED;
544 if (ev.buttons & GPM_B_RIGHT)
545 eventp->bstate |= BUTTON3_PRESSED;
548 if (ev.buttons & GPM_B_LEFT)
549 eventp->bstate |= BUTTON1_RELEASED;
550 if (ev.buttons & GPM_B_MIDDLE)
551 eventp->bstate |= BUTTON2_RELEASED;
552 if (ev.buttons & GPM_B_RIGHT)
553 eventp->bstate |= BUTTON3_RELEASED;
559 eventp->x = ev.x - 1;
560 eventp->y = ev.y - 1;
563 /* bump the next-free pointer into the circular list */
564 eventp = NEXT(eventp);
573 if (SP->_sysmouse_head < SP->_sysmouse_tail) {
574 *eventp = SP->_sysmouse_fifo[SP->_sysmouse_head];
577 * Point the fifo-head to the next possible location. If there
578 * are none, reset the indices. This may be interrupted by the
579 * signal handler, doing essentially the same reset.
581 SP->_sysmouse_head += 1;
582 if (SP->_sysmouse_head == SP->_sysmouse_tail) {
583 SP->_sysmouse_tail = 0;
584 SP->_sysmouse_head = 0;
587 /* bump the next-free pointer into the circular list */
588 eventp = NEXT(eventp);
592 #endif /* USE_SYSMOUSE */
598 return result; /* true if we found an event */
602 _nc_mouse_inline(SCREEN * sp)
603 /* mouse report received in the keyboard stream -- parse its info */
607 TR(MY_TRACE, ("_nc_mouse_inline() called"));
609 if (SP->_mouse_type == M_XTERM) {
610 unsigned char kbuf[4];
615 /* This code requires that your xterm entry contain the kmous
616 * capability and that it be set to the \E[M documented in the
617 * Xterm Control Sequences reference. This is how we
618 * arrange for mouse events to be reported via a KEY_MOUSE
619 * return value from wgetch(). After this value is received,
620 * _nc_mouse_inline() gets called and is immediately
621 * responsible for parsing the mouse status information
622 * following the prefix.
624 * The following quotes from the ctrlseqs.ms document in the
625 * X distribution, describing the X mouse tracking feature:
627 * Parameters for all mouse tracking escape sequences
628 * generated by xterm encode numeric parameters in a single
629 * character as value+040. For example, ! is 1.
631 * On button press or release, xterm sends ESC [ M CbCxCy.
632 * The low two bits of Cb encode button information: 0=MB1
633 * pressed, 1=MB2 pressed, 2=MB3 pressed, 3=release. The
634 * upper bits encode what modifiers were down when the
635 * button was pressed and are added together. 4=Shift,
636 * 8=Meta, 16=Control. Cx and Cy are the x and y coordinates
637 * of the mouse event. The upper left corner is (1,1).
639 * (End quote) By the time we get here, we've eaten the
640 * key prefix. FYI, the loop below is necessary because
641 * mouse click info isn't guaranteed to present as a
642 * single clist item. It always does under Linux but often
643 * fails to under Solaris.
645 for (grabbed = 0; grabbed < 3; grabbed += res) {
647 /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
649 res = read(M_FD(sp) >= 0 ? M_FD(sp) : sp->_ifd, &kbuf, 3);
651 res = read(sp->_ifd, kbuf + grabbed, 3 - grabbed);
659 ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
661 /* there's only one mouse... */
662 eventp->id = NORMAL_EVENT;
664 /* processing code goes here */
666 prev = PREV(eventp)->bstate;
669 #define PRESS_POSITION(n) \
670 eventp->bstate = MASK_PRESS(n); \
671 if (kbuf[0] & 0x40) \
672 eventp->bstate = MASK_RELEASE(n)
674 #define PRESS_POSITION(n) \
675 eventp->bstate = (prev & MASK_PRESS(n) \
676 ? REPORT_MOUSE_POSITION \
680 switch (kbuf[0] & 0x3) {
695 * Release events aren't reported for individual buttons, just for
696 * the button set as a whole. However, because there are normally
697 * no mouse events under xterm that intervene between press and
698 * release, we can infer the button actually released by looking at
699 * the previous event.
701 if (prev & (BUTTON_PRESSED | BUTTON_RELEASED)) {
702 eventp->bstate = BUTTON_RELEASED;
703 if (!(prev & BUTTON1_PRESSED))
704 eventp->bstate &= ~BUTTON1_RELEASED;
705 if (!(prev & BUTTON2_PRESSED))
706 eventp->bstate &= ~BUTTON2_RELEASED;
707 if (!(prev & BUTTON3_PRESSED))
708 eventp->bstate &= ~BUTTON3_RELEASED;
711 * XFree86 xterm will return a stream of release-events to
712 * let the application know where the mouse is going, if the
713 * private mode 1002 or 1003 is enabled.
715 eventp->bstate = REPORT_MOUSE_POSITION;
719 result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
722 eventp->bstate |= BUTTON_SHIFT;
725 eventp->bstate |= BUTTON_ALT;
728 eventp->bstate |= BUTTON_CTRL;
731 eventp->x = (kbuf[1] - ' ') - 1;
732 eventp->y = (kbuf[2] - ' ') - 1;
734 ("_nc_mouse_inline: primitive mouse-event %s has slot %ld",
736 (long) (eventp - events)));
738 /* bump the next-free pointer into the circular list */
739 eventp = NEXT(eventp);
740 #if 0 /* this return would be needed for QNX's mods to lib_getch.c */
749 mouse_activate(bool on)
751 if (!on && !initialized)
758 switch (SP->_mouse_type) {
760 #if NCURSES_EXT_FUNCS
761 keyok(KEY_MOUSE, on);
763 TPUTS_TRACE("xterm mouse initialization");
767 enable_xterm_mouse(1);
772 SP->_mouse_fd = gpm_fd;
777 signal(SIGUSR2, handle_sysmouse);
783 /* Make runtime binding to cut down on object size of applications that
784 * do not use the mouse (e.g., 'clear').
786 SP->_mouse_event = _nc_mouse_event;
787 SP->_mouse_inline = _nc_mouse_inline;
788 SP->_mouse_parse = _nc_mouse_parse;
789 SP->_mouse_resume = _nc_mouse_resume;
790 SP->_mouse_wrap = _nc_mouse_wrap;
794 switch (SP->_mouse_type) {
796 TPUTS_TRACE("xterm mouse deinitialization");
800 enable_xterm_mouse(0);
809 signal(SIGUSR2, SIG_IGN);
819 /**************************************************************************
821 * Device-independent code
823 **************************************************************************/
826 _nc_mouse_parse(int runcount)
827 /* parse a run of atomic mouse events into a gesture */
829 MEVENT *ep, *runp, *next, *prev = PREV(eventp);
833 TR(MY_TRACE, ("_nc_mouse_parse(%d) called", runcount));
836 * When we enter this routine, the event list next-free pointer
837 * points just past a run of mouse events that we know were separated
838 * in time by less than the critical click interval. The job of this
839 * routine is to collapse this run into a single higher-level event
842 * We accomplish this in two passes. The first pass merges press/release
843 * pairs into click events. The second merges runs of click events into
844 * double or triple-click events.
846 * It's possible that the run may not resolve to a single event (for
847 * example, if the user quadruple-clicks). If so, leading events
848 * in the run are ignored.
850 * Note that this routine is independent of the format of the specific
851 * format of the pointing-device's reports. We can use it to parse
852 * gestures on anything that reports press/release events on a per-
853 * button basis, as long as the device-dependent mouse code puts stuff
854 * on the queue in MEVENT format.
858 ("_nc_mouse_parse: returning simple mouse event %s at slot %ld",
860 (long) (prev - events)));
861 return (prev->id >= NORMAL_EVENT)
862 ? ((prev->bstate & eventmask) ? TRUE : FALSE)
866 /* find the start of the run */
868 for (n = runcount; n > 0; n--) {
873 if (_nc_tracing & TRACE_IEVENT) {
874 _trace_slot("before mouse press/release merge:");
875 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
876 (long) (runp - events),
877 (long) ((eventp - events) + (EV_MAX - 1)) % EV_MAX,
882 /* first pass; merge press/release pairs */
885 for (ep = runp; (next = NEXT(ep)) != eventp; ep = next) {
886 if (ep->x == next->x && ep->y == next->y
887 && (ep->bstate & BUTTON_PRESSED)
888 && (!(ep->bstate & BUTTON1_PRESSED)
889 == !(next->bstate & BUTTON1_RELEASED))
890 && (!(ep->bstate & BUTTON2_PRESSED)
891 == !(next->bstate & BUTTON2_RELEASED))
892 && (!(ep->bstate & BUTTON3_PRESSED)
893 == !(next->bstate & BUTTON3_RELEASED))
895 if ((eventmask & BUTTON1_CLICKED)
896 && (ep->bstate & BUTTON1_PRESSED)) {
897 ep->bstate &= ~BUTTON1_PRESSED;
898 ep->bstate |= BUTTON1_CLICKED;
901 if ((eventmask & BUTTON2_CLICKED)
902 && (ep->bstate & BUTTON2_PRESSED)) {
903 ep->bstate &= ~BUTTON2_PRESSED;
904 ep->bstate |= BUTTON2_CLICKED;
907 if ((eventmask & BUTTON3_CLICKED)
908 && (ep->bstate & BUTTON3_PRESSED)) {
909 ep->bstate &= ~BUTTON3_PRESSED;
910 ep->bstate |= BUTTON3_CLICKED;
914 next->id = INVALID_EVENT;
921 if (_nc_tracing & TRACE_IEVENT) {
922 _trace_slot("before mouse click merge:");
923 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
924 (long) (runp - events),
925 (long) ((eventp - events) + (EV_MAX - 1)) % EV_MAX,
931 * Second pass; merge click runs. At this point, click events are
932 * each followed by one invalid event. We merge click events
933 * forward in the queue.
935 * NOTE: There is a problem with this design! If the application
936 * allows enough click events to pile up in the circular queue so
937 * they wrap around, it will cheerfully merge the newest forward
938 * into the oldest, creating a bogus doubleclick and confusing
939 * the queue-traversal logic rather badly. Generally this won't
940 * happen, because calling getmouse() marks old events invalid and
941 * ineligible for merges. The true solution to this problem would
942 * be to timestamp each MEVENT and perform the obvious sanity check,
943 * but the timer element would have to have sub-second resolution,
944 * which would get us into portability trouble.
950 for (ep = runp; (next = NEXT(ep)) != eventp; ep = next)
951 if (ep->id != INVALID_EVENT) {
952 if (next->id != INVALID_EVENT)
954 follower = NEXT(next);
955 if (follower->id == INVALID_EVENT)
958 /* merge click events forward */
959 if ((ep->bstate & BUTTON_CLICKED)
960 && (follower->bstate & BUTTON_CLICKED)) {
961 if ((eventmask & BUTTON1_DOUBLE_CLICKED)
962 && (follower->bstate & BUTTON1_CLICKED)) {
963 follower->bstate &= ~BUTTON1_CLICKED;
964 follower->bstate |= BUTTON1_DOUBLE_CLICKED;
967 if ((eventmask & BUTTON2_DOUBLE_CLICKED)
968 && (follower->bstate & BUTTON2_CLICKED)) {
969 follower->bstate &= ~BUTTON2_CLICKED;
970 follower->bstate |= BUTTON2_DOUBLE_CLICKED;
973 if ((eventmask & BUTTON3_DOUBLE_CLICKED)
974 && (follower->bstate & BUTTON3_CLICKED)) {
975 follower->bstate &= ~BUTTON3_CLICKED;
976 follower->bstate |= BUTTON3_DOUBLE_CLICKED;
980 ep->id = INVALID_EVENT;
983 /* merge double-click events forward */
985 (BUTTON1_DOUBLE_CLICKED
986 | BUTTON2_DOUBLE_CLICKED
987 | BUTTON3_DOUBLE_CLICKED))
988 && (follower->bstate & BUTTON_CLICKED)) {
989 if ((eventmask & BUTTON1_TRIPLE_CLICKED)
990 && (follower->bstate & BUTTON1_CLICKED)) {
991 follower->bstate &= ~BUTTON1_CLICKED;
992 follower->bstate |= BUTTON1_TRIPLE_CLICKED;
995 if ((eventmask & BUTTON2_TRIPLE_CLICKED)
996 && (follower->bstate & BUTTON2_CLICKED)) {
997 follower->bstate &= ~BUTTON2_CLICKED;
998 follower->bstate |= BUTTON2_TRIPLE_CLICKED;
1001 if ((eventmask & BUTTON3_TRIPLE_CLICKED)
1002 && (follower->bstate & BUTTON3_CLICKED)) {
1003 follower->bstate &= ~BUTTON3_CLICKED;
1004 follower->bstate |= BUTTON3_TRIPLE_CLICKED;
1008 ep->id = INVALID_EVENT;
1015 if (_nc_tracing & TRACE_IEVENT) {
1016 _trace_slot("before mouse event queue compaction:");
1017 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1018 (long) (runp - events),
1019 (long) ((eventp - events) + (EV_MAX - 1)) % EV_MAX,
1025 * Now try to throw away trailing events flagged invalid, or that
1026 * don't match the current event mask.
1028 for (; runcount; prev = PREV(eventp), runcount--)
1029 if (prev->id == INVALID_EVENT || !(prev->bstate & eventmask)) {
1033 if (_nc_tracing & TRACE_IEVENT) {
1034 _trace_slot("after mouse event queue compaction:");
1035 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1036 (long) (runp - events),
1037 (long) ((eventp - events) + (EV_MAX - 1)) % EV_MAX,
1040 for (ep = runp; ep != eventp; ep = NEXT(ep))
1041 if (ep->id != INVALID_EVENT)
1043 ("_nc_mouse_parse: returning composite mouse event %s at slot %ld",
1045 (long) (ep - events)));
1048 /* after all this, do we have a valid event? */
1049 return (PREV(eventp)->id != INVALID_EVENT);
1053 _nc_mouse_wrap(SCREEN * sp GCC_UNUSED)
1054 /* release mouse -- called by endwin() before shellout/exit */
1056 TR(MY_TRACE, ("_nc_mouse_wrap() called"));
1058 switch (SP->_mouse_type) {
1061 mouse_activate(FALSE);
1064 /* GPM: pass all mouse events to next client */
1070 mouse_activate(FALSE);
1079 _nc_mouse_resume(SCREEN * sp GCC_UNUSED)
1080 /* re-connect to mouse -- called by doupdate() after shellout */
1082 TR(MY_TRACE, ("_nc_mouse_resume() called"));
1084 switch (SP->_mouse_type) {
1086 /* xterm: re-enable reporting */
1088 mouse_activate(TRUE);
1093 /* GPM: reclaim our event set */
1099 mouse_activate(TRUE);
1107 /**************************************************************************
1109 * Mouse interface entry points for the API
1111 **************************************************************************/
1114 getmouse(MEVENT * aevent)
1115 /* grab a copy of the current mouse event */
1117 T((T_CALLED("getmouse(%p)"), aevent));
1119 if (aevent && (SP->_mouse_type != M_NONE)) {
1120 /* compute the current-event pointer */
1121 MEVENT *prev = PREV(eventp);
1123 /* copy the event we find there */
1126 TR(TRACE_IEVENT, ("getmouse: returning event %s from slot %ld",
1128 (long) (prev - events)));
1130 prev->id = INVALID_EVENT; /* so the queue slot becomes free */
1137 ungetmouse(MEVENT * aevent)
1138 /* enqueue a synthesized mouse event to be seen by the next wgetch() */
1140 T((T_CALLED("ungetmouse(%p)"), aevent));
1142 /* stick the given event in the next-free slot */
1145 /* bump the next-free pointer into the circular list */
1146 eventp = NEXT(eventp);
1148 /* push back the notification event on the keyboard queue */
1149 returnCode(ungetch(KEY_MOUSE));
1152 NCURSES_EXPORT(mmask_t)
1153 mousemask(mmask_t newmask, mmask_t * oldmask)
1154 /* set the mouse event mask */
1158 T((T_CALLED("mousemask(%#lx,%p)"), newmask, oldmask));
1161 *oldmask = eventmask;
1163 if (!newmask && !initialized)
1167 if (SP->_mouse_type != M_NONE) {
1168 eventmask = newmask &
1169 (REPORT_MOUSE_POSITION | BUTTON_ALT | BUTTON_CTRL | BUTTON_SHIFT
1173 | BUTTON1_DOUBLE_CLICKED | BUTTON1_TRIPLE_CLICKED
1174 | BUTTON2_DOUBLE_CLICKED | BUTTON2_TRIPLE_CLICKED
1175 | BUTTON3_DOUBLE_CLICKED | BUTTON3_TRIPLE_CLICKED);
1177 mouse_activate(eventmask != 0);
1185 NCURSES_EXPORT(bool)
1186 wenclose(const WINDOW *win, int y, int x)
1187 /* check to see if given window encloses given screen location */
1189 bool result = FALSE;
1191 T((T_CALLED("wenclose(%p,%d,%d)"), win, y, x));
1195 result = ((win->_begy <= y &&
1197 (win->_begx + win->_maxx) >= x &&
1198 (win->_begy + win->_maxy) >= y) ? TRUE : FALSE);
1204 mouseinterval(int maxclick)
1205 /* set the maximum mouse interval within which to recognize a click */
1209 T((T_CALLED("mouseinterval(%d)"), maxclick));
1212 oldval = SP->_maxclick;
1214 SP->_maxclick = maxclick;
1216 oldval = DEFAULT_MAXCLICK;
1222 /* This may be used by other routines to ask for the existence of mouse
1227 return (SP->_mouse_type == M_NONE ? 0 : 1);
1230 NCURSES_EXPORT(bool)
1231 wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
1233 bool result = FALSE;
1235 T((T_CALLED("wmouse_trafo(%p,%p,%p,%d)"), win, pY, pX, to_screen));
1237 if (win && pY && pX) {
1242 y += win->_begy + win->_yoffset;
1244 if (wenclose(win, y, x))
1247 if (wenclose(win, y, x)) {
1248 y -= (win->_begy + win->_yoffset);