1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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 ****************************************************************************/
36 * This module is intended to encapsulate ncurses's interface to pointing
39 * The primary method used is xterm's internal mouse-tracking facility.
40 * Additional methods depend on the platform:
41 * Alessandro Rubini's GPM server (Linux)
43 * special-purpose mouse interface for OS/2 EMX.
45 * Notes for implementors of new mouse-interface methods:
47 * The code is logically split into a lower level that accepts event reports
48 * in a device-dependent format and an upper level that parses mouse gestures
49 * and filters events. The mediating data structure is a circular queue of
52 * Functionally, the lower level's job is to pick up primitive events and
53 * put them on the circular queue. This can happen in one of two ways:
54 * either (a) _nc_mouse_event() detects a series of incoming mouse reports
55 * and queues them, or (b) code in lib_getch.c detects the kmous prefix in
56 * the keyboard input stream and calls _nc_mouse_inline to queue up a series
57 * of adjacent mouse reports.
59 * In either case, _nc_mouse_parse() should be called after the series is
60 * accepted to parse the digested mouse reports (low-level MEVENTs) into
61 * a gesture (a high-level or composite MEVENT).
63 * Don't be too shy about adding new event types or modifiers, if you can find
64 * room for them in the 32-bit mask. The API is written so that users get
65 * feedback on which theoretical event types they won't see when they call
66 * mousemask. There's one bit per button (the RESERVED_EVENT bit) not being
67 * used yet, and a couple of bits open at the high end.
76 # define INCL_DOSPROCESS
77 # include <os2.h> /* Need to include before the others */
80 #include <curses.priv.h>
82 MODULE_ID("$Id: lib_mouse.c,v 1.94 2008/05/31 20:30:10 tom Exp $")
88 #include <linux/keyboard.h> /* defines KG_* macros */
91 /* use dynamic loader to avoid linkage dependency */
95 #define my_RTLD RTLD_NOW
98 #define my_RTLD RTLD_LAZY
102 #endif /* RTLD_NOW */
103 #endif /* HAVE_LIBDL */
105 #endif /* USE_GPM_SUPPORT */
108 #undef buttons /* symbol conflict in consio.h */
109 #undef mouse_info /* symbol conflict in consio.h */
110 #include <osreldate.h>
111 #if (__FreeBSD_version >= 400017)
112 #include <sys/consio.h>
113 #include <sys/fbio.h>
115 #include <machine/console.h>
117 #endif /* use_SYSMOUSE */
119 #define MY_TRACE TRACE_ICALLS|TRACE_IEVENT
121 #define MASK_RELEASE(x) NCURSES_MOUSE_MASK(x, 001)
122 #define MASK_PRESS(x) NCURSES_MOUSE_MASK(x, 002)
123 #define MASK_CLICK(x) NCURSES_MOUSE_MASK(x, 004)
124 #define MASK_DOUBLE_CLICK(x) NCURSES_MOUSE_MASK(x, 010)
125 #define MASK_TRIPLE_CLICK(x) NCURSES_MOUSE_MASK(x, 020)
126 #define MASK_RESERVED_EVENT(x) NCURSES_MOUSE_MASK(x, 040)
128 #if NCURSES_MOUSE_VERSION == 1
129 #define BUTTON_CLICKED (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED | BUTTON4_CLICKED)
130 #define BUTTON_PRESSED (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED | BUTTON4_PRESSED)
131 #define BUTTON_RELEASED (BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON3_RELEASED | BUTTON4_RELEASED)
132 #define BUTTON_DOUBLE_CLICKED (BUTTON1_DOUBLE_CLICKED | BUTTON2_DOUBLE_CLICKED | BUTTON3_DOUBLE_CLICKED | BUTTON4_DOUBLE_CLICKED)
133 #define BUTTON_TRIPLE_CLICKED (BUTTON1_TRIPLE_CLICKED | BUTTON2_TRIPLE_CLICKED | BUTTON3_TRIPLE_CLICKED | BUTTON4_TRIPLE_CLICKED)
134 #define MAX_BUTTONS 4
136 #define BUTTON_CLICKED (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED | BUTTON4_CLICKED | BUTTON5_CLICKED)
137 #define BUTTON_PRESSED (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED | BUTTON4_PRESSED | BUTTON5_PRESSED)
138 #define BUTTON_RELEASED (BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON3_RELEASED | BUTTON4_RELEASED | BUTTON5_RELEASED)
139 #define BUTTON_DOUBLE_CLICKED (BUTTON1_DOUBLE_CLICKED | BUTTON2_DOUBLE_CLICKED | BUTTON3_DOUBLE_CLICKED | BUTTON4_DOUBLE_CLICKED | BUTTON5_DOUBLE_CLICKED)
140 #define BUTTON_TRIPLE_CLICKED (BUTTON1_TRIPLE_CLICKED | BUTTON2_TRIPLE_CLICKED | BUTTON3_TRIPLE_CLICKED | BUTTON4_TRIPLE_CLICKED | BUTTON5_TRIPLE_CLICKED)
141 #define MAX_BUTTONS 5
144 #define INVALID_EVENT -1
145 #define NORMAL_EVENT 0
149 #ifndef LIBGPM_SONAME
150 #define LIBGPM_SONAME "libgpm.so"
153 #define GET_DLSYM(name) (my_##name = (TYPE_##name) dlsym(obj, #name))
155 #endif /* USE_GPM_SUPPORT */
157 static bool _nc_mouse_parse(SCREEN *, int);
158 static void _nc_mouse_resume(SCREEN *);
159 static void _nc_mouse_wrap(SCREEN *);
161 /* maintain a circular list of mouse events */
164 #define NEXT(ep) ((ep == sp->_mouse_events + EV_MAX - 1) \
165 ? sp->_mouse_events \
169 #define PREV(ep) ((ep == sp->_mouse_events) \
170 ? sp->_mouse_events + EV_MAX - 1 \
175 _trace_slot(SCREEN *sp, const char *tag)
181 for (ep = sp->_mouse_events; ep < sp->_mouse_events + EV_MAX; ep++)
182 _tracef("mouse event queue slot %ld = %s",
183 (long) (ep - sp->_mouse_events),
184 _nc_tracemouse(sp, ep));
193 # define M_FD(sp) sp->_mouse_fd
196 write_event(SCREEN *sp, int down, int button, int x, int y)
199 unsigned long ignore;
201 strncpy(buf, key_mouse, 3); /* should be "\033[M" */
202 buf[3] = ' ' + (button - 1) + (down ? 0 : 0x40);
203 buf[4] = ' ' + x - LEFT_COL + 1;
204 buf[5] = ' ' + y - TOP_ROW + 1;
205 DosWrite(sp->_emxmouse_wfd, buf, 6, &ignore);
209 mouse_server(unsigned long param)
211 SCREEN *sp = (SCREEN *) param;
212 unsigned short fWait = MOU_WAIT;
213 /* NOPTRRECT mourt = { 0,0,24,79 }; */
216 unsigned short mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;
222 /* open the handle for the mouse */
223 if (MouOpen(NULL, &hmou) == 0) {
224 rc = MouSetEventMask(&mask, hmou);
225 if (rc) { /* retry with 2 buttons */
226 mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN;
227 rc = MouSetEventMask(&mask, hmou);
230 if (rc == 0 && MouDrawPtr(hmou) == 0) {
232 /* sit and wait on the event queue */
233 rc = MouReadEventQue(&mouev, &fWait, hmou);
235 sprintf(err, "Error reading mouse queue, rc=%lu.\r\n", rc);
238 if (!sp->_emxmouse_activated)
242 * OS/2 numbers a 3-button mouse inconsistently from other
248 if ((mouev.fs ^ oldstate) & MOUSE_BN1_DOWN)
249 write_event(mouev.fs & MOUSE_BN1_DOWN,
250 sp->_emxmouse_buttons[1], mouev.col, mouev.row);
251 if ((mouev.fs ^ oldstate) & MOUSE_BN2_DOWN)
252 write_event(mouev.fs & MOUSE_BN2_DOWN,
253 sp->_emxmouse_buttons[3], mouev.col, mouev.row);
254 if ((mouev.fs ^ oldstate) & MOUSE_BN3_DOWN)
255 write_event(mouev.fs & MOUSE_BN3_DOWN,
256 sp->_emxmouse_buttons[2], mouev.col, mouev.row);
262 sprintf(err, "Error setting event mask, buttons=%d, rc=%lu.\r\n",
265 DosWrite(2, err, strlen(err), &rc);
268 DosExit(EXIT_THREAD, 0L);
271 #endif /* USE_EMX_MOUSE */
275 sysmouse_server(SCREEN *sp)
277 struct mouse_info the_mouse;
280 the_mouse.operation = MOUSE_GETINFO;
282 && sp->_mouse_fd >= 0
283 && sp->_sysmouse_tail < FIFO_SIZE
284 && ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
286 if (sp->_sysmouse_head > sp->_sysmouse_tail) {
287 sp->_sysmouse_tail = 0;
288 sp->_sysmouse_head = 0;
290 work = &(sp->_sysmouse_fifo[sp->_sysmouse_tail]);
291 memset(work, 0, sizeof(*work));
292 work->id = NORMAL_EVENT; /* there's only one mouse... */
294 sp->_sysmouse_old_buttons = sp->_sysmouse_new_buttons;
295 sp->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
297 if (sp->_sysmouse_new_buttons) {
298 if (sp->_sysmouse_new_buttons & 1)
299 work->bstate |= BUTTON1_PRESSED;
300 if (sp->_sysmouse_new_buttons & 2)
301 work->bstate |= BUTTON2_PRESSED;
302 if (sp->_sysmouse_new_buttons & 4)
303 work->bstate |= BUTTON3_PRESSED;
305 if (sp->_sysmouse_old_buttons & 1)
306 work->bstate |= BUTTON1_RELEASED;
307 if (sp->_sysmouse_old_buttons & 2)
308 work->bstate |= BUTTON2_RELEASED;
309 if (sp->_sysmouse_old_buttons & 4)
310 work->bstate |= BUTTON3_RELEASED;
313 /* for cosmetic bug in syscons.c on FreeBSD 3.[34] */
314 the_mouse.operation = MOUSE_HIDE;
315 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
316 the_mouse.operation = MOUSE_SHOW;
317 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
320 * We're only interested if the button is pressed or released.
321 * FIXME: implement continuous event-tracking.
323 if (sp->_sysmouse_new_buttons != sp->_sysmouse_old_buttons) {
324 sp->_sysmouse_tail += 1;
326 work->x = the_mouse.u.data.x / sp->_sysmouse_char_width;
327 work->y = the_mouse.u.data.y / sp->_sysmouse_char_height;
332 handle_sysmouse(int sig GCC_UNUSED)
336 #endif /* USE_SYSMOUSE */
339 init_xterm_mouse(SCREEN *sp)
341 sp->_mouse_type = M_XTERM;
342 sp->_mouse_xtermcap = tigetstr("XM");
343 if (!VALID_STRING(sp->_mouse_xtermcap))
344 sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
348 enable_xterm_mouse(SCREEN *sp, int enable)
351 sp->_emxmouse_activated = enable;
353 putp(TPARM_1(sp->_mouse_xtermcap, enable));
355 sp->_mouse_active = enable;
360 allow_gpm_mouse(void)
362 /* GPM does printf's without checking if stdout is a terminal */
363 if (isatty(fileno(stdout))) {
364 char *env = getenv("TERM");
365 /* GPM checks the beginning of the $TERM variable to decide if
366 * it should pass xterm events through. There is no real advantage
367 * in allowing GPM to do this.
369 if (env == 0 || strncmp(env, "xterm", 5))
376 enable_gpm_mouse(SCREEN *sp, int enable)
380 T((T_CALLED("enable_gpm_mouse(%d)"), enable));
382 if (enable && !sp->_mouse_active) {
383 /* GPM: initialize connection to gpm server */
384 sp->_mouse_gpm_connect.eventMask = GPM_DOWN | GPM_UP;
385 sp->_mouse_gpm_connect.defaultMask =
386 ~(sp->_mouse_gpm_connect.eventMask | GPM_HARD);
387 sp->_mouse_gpm_connect.minMod = 0;
388 sp->_mouse_gpm_connect.maxMod =
389 (unsigned short) (~((1 << KG_SHIFT) |
393 * Note: GPM hardcodes \E[?1001s and \E[?1000h during its open.
394 * The former is recognized by wscons (SunOS), and the latter by
395 * xterm. Those will not show up in ncurses' traces.
397 result = (my_Gpm_Open(&sp->_mouse_gpm_connect, 0) >= 0);
398 sp->_mouse_active = result;
399 T(("GPM open %s", result ? "succeeded" : "failed"));
401 if (!enable && sp->_mouse_active) {
402 /* GPM: close connection to gpm server */
404 sp->_mouse_active = FALSE;
411 #endif /* USE_GPM_SUPPORT */
413 #define xterm_kmous "\033[M"
416 initialize_mousetype(SCREEN *sp)
418 T((T_CALLED("initialize_mousetype()")));
420 /* Try gpm first, because gpm may be configured to run in xterm */
422 if (allow_gpm_mouse()) {
423 if (!sp->_mouse_gpm_loaded) {
427 if ((obj = dlopen(LIBGPM_SONAME, my_RTLD)) != 0) {
428 if (GET_DLSYM(gpm_fd) == 0 ||
429 GET_DLSYM(Gpm_Open) == 0 ||
430 GET_DLSYM(Gpm_Close) == 0 ||
431 GET_DLSYM(Gpm_GetEvent) == 0) {
432 T(("GPM initialization failed: %s", dlerror()));
435 sp->_mouse_gpm_found = TRUE;
438 #else /* !HAVE_LIBDL */
439 sp->_mouse_gpm_found = TRUE;
441 sp->_mouse_gpm_loaded = TRUE;
445 * The gpm_fd file-descriptor may be negative (xterm). So we have to
446 * maintain our notion of whether the mouse connection is active
447 * without testing the file-descriptor.
449 if (sp->_mouse_gpm_found && enable_gpm_mouse(sp, TRUE)) {
450 sp->_mouse_type = M_GPM;
451 sp->_mouse_fd = *(my_gpm_fd);
452 T(("GPM mouse_fd %d", sp->_mouse_fd));
456 #endif /* USE_GPM_SUPPORT */
460 if (!sp->_emxmouse_thread
461 && strstr(cur_term->type.term_names, "xterm") == 0
465 if (pipe(handles) < 0) {
466 perror("mouse pipe error");
471 if (!sp->_emxmouse_buttons[0]) {
472 char *s = getenv("MOUSE_BUTTONS_123");
474 sp->_emxmouse_buttons[0] = 1;
475 if (s && strlen(s) >= 3) {
476 sp->_emxmouse_buttons[1] = s[0] - '0';
477 sp->_emxmouse_buttons[2] = s[1] - '0';
478 sp->_emxmouse_buttons[3] = s[2] - '0';
480 sp->_emxmouse_buttons[1] = 1;
481 sp->_emxmouse_buttons[2] = 3;
482 sp->_emxmouse_buttons[3] = 2;
485 sp->_emxmouse_wfd = handles[1];
486 M_FD(sp) = handles[0];
488 setmode(handles[0], O_BINARY);
489 setmode(handles[1], O_BINARY);
490 /* Do not use CRT functions, we may single-threaded. */
491 rc = DosCreateThread((unsigned long *) &sp->_emxmouse_thread,
492 mouse_server, sp, 0, 8192);
494 printf("mouse thread error %d=%#x", rc, rc);
496 sp->_mouse_type = M_XTERM;
501 #endif /* USE_EMX_MOUSE */
505 struct mouse_info the_mouse;
506 char *the_device = 0;
508 if (isatty(sp->_ifd))
509 the_device = ttyname(sp->_ifd);
511 the_device = "/dev/tty";
513 sp->_mouse_fd = open(the_device, O_RDWR);
515 if (sp->_mouse_fd >= 0) {
517 * sysmouse does not have a usable user interface for obtaining
518 * mouse events. The logical way to proceed (reading data on a
519 * stream) only works if one opens the device as root. Even in
520 * that mode, careful examination shows we lose events
521 * occasionally. The interface provided for user programs is to
522 * establish a signal handler. really.
524 * Take over SIGUSR2 for this purpose since SIGUSR1 is more
525 * likely to be used by an application. getch() will have to
526 * handle the misleading EINTR's.
528 signal(SIGUSR2, SIG_IGN);
529 the_mouse.operation = MOUSE_MODE;
530 the_mouse.u.mode.mode = 0;
531 the_mouse.u.mode.signal = SIGUSR2;
532 if (ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
533 signal(SIGUSR2, handle_sysmouse);
534 the_mouse.operation = MOUSE_SHOW;
535 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
537 #if defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) /* FreeBSD > 2.x */
539 #ifndef FBIO_GETMODE /* FreeBSD 3.x */
540 #define FBIO_GETMODE CONS_GET
541 #define FBIO_MODEINFO CONS_MODEINFO
542 #endif /* FBIO_GETMODE */
543 video_info_t the_video;
545 if (ioctl(sp->_mouse_fd,
547 &the_video.vi_mode) != -1
548 && ioctl(sp->_mouse_fd,
551 sp->_sysmouse_char_width = the_video.vi_cwidth;
552 sp->_sysmouse_char_height = the_video.vi_cheight;
555 #endif /* defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) */
557 if (sp->_sysmouse_char_width <= 0)
558 sp->_sysmouse_char_width = 8;
559 if (sp->_sysmouse_char_height <= 0)
560 sp->_sysmouse_char_height = 16;
561 sp->_mouse_type = M_SYSMOUSE;
566 #endif /* USE_SYSMOUSE */
568 /* we know how to recognize mouse events under "xterm" */
569 if (key_mouse != 0) {
570 if (!strcmp(key_mouse, xterm_kmous)
571 || strstr(cur_term->type.term_names, "xterm") != 0) {
572 init_xterm_mouse(sp);
574 } else if (strstr(cur_term->type.term_names, "xterm") != 0) {
575 if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
576 init_xterm_mouse(sp);
582 _nc_mouse_init(SCREEN *sp)
583 /* initialize the mouse */
589 if (!sp->_mouse_initialized) {
590 sp->_mouse_initialized = TRUE;
592 TR(MY_TRACE, ("_nc_mouse_init() called"));
594 sp->_mouse_eventp = sp->_mouse_events;
595 for (i = 0; i < EV_MAX; i++)
596 sp->_mouse_events[i].id = INVALID_EVENT;
598 initialize_mousetype(sp);
600 T(("_nc_mouse_init() set mousetype to %d", sp->_mouse_type));
602 result = sp->_mouse_initialized;
608 * Query to see if there is a pending mouse event. This is called from
609 * fifo_push() in lib_getch.c
612 _nc_mouse_event(SCREEN *sp GCC_UNUSED)
614 MEVENT *eventp = sp->_mouse_eventp;
619 switch (sp->_mouse_type) {
621 /* xterm: never have to query, mouse events are in the keyboard stream */
626 int i, res = read(M_FD(sp), &kbuf, 3); /* Eat the prefix */
628 printf("Got %d chars instead of 3 for prefix.\n", res);
629 for (i = 0; i < res; i++) {
630 if (kbuf[i] != key_mouse[i])
631 printf("Got char %d instead of %d for prefix.\n",
632 (int) kbuf[i], (int) key_mouse[i]);
636 #endif /* USE_EMX_MOUSE */
642 /* query server for event, return TRUE if we find one */
645 if (my_Gpm_GetEvent(&ev) == 1) {
646 /* there's only one mouse... */
647 eventp->id = NORMAL_EVENT;
650 switch (ev.type & 0x0f) {
652 if (ev.buttons & GPM_B_LEFT)
653 eventp->bstate |= BUTTON1_PRESSED;
654 if (ev.buttons & GPM_B_MIDDLE)
655 eventp->bstate |= BUTTON2_PRESSED;
656 if (ev.buttons & GPM_B_RIGHT)
657 eventp->bstate |= BUTTON3_PRESSED;
660 if (ev.buttons & GPM_B_LEFT)
661 eventp->bstate |= BUTTON1_RELEASED;
662 if (ev.buttons & GPM_B_MIDDLE)
663 eventp->bstate |= BUTTON2_RELEASED;
664 if (ev.buttons & GPM_B_RIGHT)
665 eventp->bstate |= BUTTON3_RELEASED;
671 eventp->x = ev.x - 1;
672 eventp->y = ev.y - 1;
675 /* bump the next-free pointer into the circular list */
676 sp->_mouse_eventp = eventp = NEXT(eventp);
685 if (sp->_sysmouse_head < sp->_sysmouse_tail) {
686 *eventp = sp->_sysmouse_fifo[sp->_sysmouse_head];
689 * Point the fifo-head to the next possible location. If there
690 * are none, reset the indices. This may be interrupted by the
691 * signal handler, doing essentially the same reset.
693 sp->_sysmouse_head += 1;
694 if (sp->_sysmouse_head == sp->_sysmouse_tail) {
695 sp->_sysmouse_tail = 0;
696 sp->_sysmouse_head = 0;
699 /* bump the next-free pointer into the circular list */
700 sp->_mouse_eventp = eventp = NEXT(eventp);
704 #endif /* USE_SYSMOUSE */
710 return result; /* true if we found an event */
714 _nc_mouse_inline(SCREEN *sp)
715 /* mouse report received in the keyboard stream -- parse its info */
719 MEVENT *eventp = sp->_mouse_eventp;
721 TR(MY_TRACE, ("_nc_mouse_inline() called"));
723 if (sp->_mouse_type == M_XTERM) {
724 unsigned char kbuf[4];
729 /* This code requires that your xterm entry contain the kmous
730 * capability and that it be set to the \E[M documented in the
731 * Xterm Control Sequences reference. This is how we
732 * arrange for mouse events to be reported via a KEY_MOUSE
733 * return value from wgetch(). After this value is received,
734 * _nc_mouse_inline() gets called and is immediately
735 * responsible for parsing the mouse status information
736 * following the prefix.
738 * The following quotes from the ctrlseqs.ms document in the
739 * X distribution, describing the X mouse tracking feature:
741 * Parameters for all mouse tracking escape sequences
742 * generated by xterm encode numeric parameters in a single
743 * character as value+040. For example, ! is 1.
745 * On button press or release, xterm sends ESC [ M CbCxCy.
746 * The low two bits of Cb encode button information: 0=MB1
747 * pressed, 1=MB2 pressed, 2=MB3 pressed, 3=release. The
748 * upper bits encode what modifiers were down when the
749 * button was pressed and are added together. 4=Shift,
750 * 8=Meta, 16=Control. Cx and Cy are the x and y coordinates
751 * of the mouse event. The upper left corner is (1,1).
753 * (End quote) By the time we get here, we've eaten the
754 * key prefix. FYI, the loop below is necessary because
755 * mouse click info isn't guaranteed to present as a
758 * Wheel mice may return buttons 4 and 5 when the wheel is turned.
759 * We encode those as button presses.
761 for (grabbed = 0; grabbed < 3; grabbed += res) {
763 /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
765 res = read(M_FD(sp) >= 0 ? M_FD(sp) : sp->_ifd, &kbuf, 3);
767 res = read(sp->_ifd, kbuf + grabbed, 3 - grabbed);
775 ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
777 /* there's only one mouse... */
778 eventp->id = NORMAL_EVENT;
780 /* processing code goes here */
782 prev = PREV(eventp)->bstate;
785 #define PRESS_POSITION(n) \
786 eventp->bstate = MASK_PRESS(n); \
787 if (kbuf[0] & 0x40) \
788 eventp->bstate = MASK_RELEASE(n)
790 #define PRESS_POSITION(n) \
791 eventp->bstate = (prev & MASK_PRESS(n) \
792 ? REPORT_MOUSE_POSITION \
796 switch (kbuf[0] & 0x3) {
799 eventp->bstate = MASK_PRESS(4);
805 #if NCURSES_MOUSE_VERSION == 2
807 eventp->bstate = MASK_PRESS(5);
819 * Release events aren't reported for individual buttons, just for
820 * the button set as a whole. However, because there are normally
821 * no mouse events under xterm that intervene between press and
822 * release, we can infer the button actually released by looking at
823 * the previous event.
825 if (prev & (BUTTON_PRESSED | BUTTON_RELEASED)) {
826 eventp->bstate = BUTTON_RELEASED;
827 for (b = 1; b <= MAX_BUTTONS; ++b) {
828 if (!(prev & MASK_PRESS(b)))
829 eventp->bstate &= ~MASK_RELEASE(b);
833 * XFree86 xterm will return a stream of release-events to
834 * let the application know where the mouse is going, if the
835 * private mode 1002 or 1003 is enabled.
837 eventp->bstate = REPORT_MOUSE_POSITION;
841 result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
844 eventp->bstate |= BUTTON_SHIFT;
847 eventp->bstate |= BUTTON_ALT;
850 eventp->bstate |= BUTTON_CTRL;
853 eventp->x = (kbuf[1] - ' ') - 1;
854 eventp->y = (kbuf[2] - ' ') - 1;
856 ("_nc_mouse_inline: primitive mouse-event %s has slot %ld",
857 _nc_tracemouse(sp, eventp),
858 (long) (eventp - sp->_mouse_events)));
860 /* bump the next-free pointer into the circular list */
861 sp->_mouse_eventp = NEXT(eventp);
862 #if 0 /* this return would be needed for QNX's mods to lib_getch.c */
871 mouse_activate(SCREEN *sp, bool on)
873 if (!on && !sp->_mouse_initialized)
876 if (!_nc_mouse_init(sp))
881 switch (sp->_mouse_type) {
883 #if NCURSES_EXT_FUNCS
884 keyok(KEY_MOUSE, on);
886 TPUTS_TRACE("xterm mouse initialization");
887 enable_xterm_mouse(sp, 1);
891 if (enable_gpm_mouse(sp, 1)) {
892 sp->_mouse_fd = *(my_gpm_fd);
893 T(("GPM mouse_fd %d", sp->_mouse_fd));
899 signal(SIGUSR2, handle_sysmouse);
900 sp->_mouse_active = TRUE;
906 /* Make runtime binding to cut down on object size of applications that
907 * do not use the mouse (e.g., 'clear').
909 sp->_mouse_event = _nc_mouse_event;
910 sp->_mouse_inline = _nc_mouse_inline;
911 sp->_mouse_parse = _nc_mouse_parse;
912 sp->_mouse_resume = _nc_mouse_resume;
913 sp->_mouse_wrap = _nc_mouse_wrap;
916 switch (sp->_mouse_type) {
918 TPUTS_TRACE("xterm mouse deinitialization");
919 enable_xterm_mouse(sp, 0);
923 enable_gpm_mouse(sp, 0);
928 signal(SIGUSR2, SIG_IGN);
929 sp->_mouse_active = FALSE;
939 /**************************************************************************
941 * Device-independent code
943 **************************************************************************/
946 _nc_mouse_parse(SCREEN *sp, int runcount)
947 /* parse a run of atomic mouse events into a gesture */
949 MEVENT *eventp = sp->_mouse_eventp;
950 MEVENT *ep, *runp, *next, *prev = PREV(eventp);
955 TR(MY_TRACE, ("_nc_mouse_parse(%d) called", runcount));
958 * When we enter this routine, the event list next-free pointer
959 * points just past a run of mouse events that we know were separated
960 * in time by less than the critical click interval. The job of this
961 * routine is to collapse this run into a single higher-level event
964 * We accomplish this in two passes. The first pass merges press/release
965 * pairs into click events. The second merges runs of click events into
966 * double or triple-click events.
968 * It's possible that the run may not resolve to a single event (for
969 * example, if the user quadruple-clicks). If so, leading events
970 * in the run are ignored.
972 * Note that this routine is independent of the format of the specific
973 * format of the pointing-device's reports. We can use it to parse
974 * gestures on anything that reports press/release events on a per-
975 * button basis, as long as the device-dependent mouse code puts stuff
976 * on the queue in MEVENT format.
980 ("_nc_mouse_parse: returning simple mouse event %s at slot %ld",
981 _nc_tracemouse(sp, prev),
982 (long) (prev - sp->_mouse_events)));
983 return (prev->id >= NORMAL_EVENT)
984 ? ((prev->bstate & sp->_mouse_mask) ? TRUE : FALSE)
988 /* find the start of the run */
990 for (n = runcount; n > 0; n--) {
995 if (USE_TRACEF(TRACE_IEVENT)) {
996 _trace_slot(sp, "before mouse press/release merge:");
997 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
998 (long) (runp - sp->_mouse_events),
999 (long) ((eventp - sp->_mouse_events) + (EV_MAX - 1)) % EV_MAX,
1001 _nc_unlock_global(tracef);
1005 /* first pass; merge press/release pairs */
1008 for (ep = runp; (next = NEXT(ep)) != eventp; ep = next) {
1010 #define MASK_CHANGED(x) (!(ep->bstate & MASK_PRESS(x)) \
1011 == !(next->bstate & MASK_RELEASE(x)))
1013 if (ep->x == next->x && ep->y == next->y
1014 && (ep->bstate & BUTTON_PRESSED)
1019 #if NCURSES_MOUSE_VERSION == 2
1023 for (b = 1; b <= MAX_BUTTONS; ++b) {
1024 if ((sp->_mouse_mask & MASK_CLICK(b))
1025 && (ep->bstate & MASK_PRESS(b))) {
1026 ep->bstate &= ~MASK_PRESS(b);
1027 ep->bstate |= MASK_CLICK(b);
1032 next->id = INVALID_EVENT;
1039 if (USE_TRACEF(TRACE_IEVENT)) {
1040 _trace_slot(sp, "before mouse click merge:");
1041 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1042 (long) (runp - sp->_mouse_events),
1043 (long) ((eventp - sp->_mouse_events) + (EV_MAX - 1)) % EV_MAX,
1045 _nc_unlock_global(tracef);
1050 * Second pass; merge click runs. At this point, click events are
1051 * each followed by one invalid event. We merge click events
1052 * forward in the queue.
1054 * NOTE: There is a problem with this design! If the application
1055 * allows enough click events to pile up in the circular queue so
1056 * they wrap around, it will cheerfully merge the newest forward
1057 * into the oldest, creating a bogus doubleclick and confusing
1058 * the queue-traversal logic rather badly. Generally this won't
1059 * happen, because calling getmouse() marks old events invalid and
1060 * ineligible for merges. The true solution to this problem would
1061 * be to timestamp each MEVENT and perform the obvious sanity check,
1062 * but the timer element would have to have sub-second resolution,
1063 * which would get us into portability trouble.
1069 for (ep = runp; (next = NEXT(ep)) != eventp; ep = next)
1070 if (ep->id != INVALID_EVENT) {
1071 if (next->id != INVALID_EVENT)
1073 follower = NEXT(next);
1074 if (follower->id == INVALID_EVENT)
1077 /* merge click events forward */
1078 if ((ep->bstate & BUTTON_CLICKED)
1079 && (follower->bstate & BUTTON_CLICKED)) {
1080 for (b = 1; b <= MAX_BUTTONS; ++b) {
1081 if ((sp->_mouse_mask & MASK_DOUBLE_CLICK(b))
1082 && (follower->bstate & MASK_CLICK(b))) {
1083 follower->bstate &= ~MASK_CLICK(b);
1084 follower->bstate |= MASK_DOUBLE_CLICK(b);
1089 ep->id = INVALID_EVENT;
1092 /* merge double-click events forward */
1093 if ((ep->bstate & BUTTON_DOUBLE_CLICKED)
1094 && (follower->bstate & BUTTON_CLICKED)) {
1095 for (b = 1; b <= MAX_BUTTONS; ++b) {
1096 if ((sp->_mouse_mask & MASK_TRIPLE_CLICK(b))
1097 && (follower->bstate & MASK_CLICK(b))) {
1098 follower->bstate &= ~MASK_CLICK(b);
1099 follower->bstate |= MASK_TRIPLE_CLICK(b);
1104 ep->id = INVALID_EVENT;
1111 if (USE_TRACEF(TRACE_IEVENT)) {
1112 _trace_slot(sp, "before mouse event queue compaction:");
1113 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1114 (long) (runp - sp->_mouse_events),
1115 (long) ((eventp - sp->_mouse_events) + (EV_MAX - 1)) % EV_MAX,
1117 _nc_unlock_global(tracef);
1122 * Now try to throw away trailing events flagged invalid, or that
1123 * don't match the current event mask.
1125 for (; runcount; prev = PREV(eventp), runcount--)
1126 if (prev->id == INVALID_EVENT || !(prev->bstate & sp->_mouse_mask)) {
1127 sp->_mouse_eventp = eventp = prev;
1130 if (USE_TRACEF(TRACE_IEVENT)) {
1131 _trace_slot(sp, "after mouse event queue compaction:");
1132 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1133 (long) (runp - sp->_mouse_events),
1134 (long) ((eventp - sp->_mouse_events) + (EV_MAX - 1)) % EV_MAX,
1136 _nc_unlock_global(tracef);
1138 for (ep = runp; ep != eventp; ep = NEXT(ep))
1139 if (ep->id != INVALID_EVENT)
1141 ("_nc_mouse_parse: returning composite mouse event %s at slot %ld",
1142 _nc_tracemouse(sp, ep),
1143 (long) (ep - sp->_mouse_events)));
1146 /* after all this, do we have a valid event? */
1147 return (PREV(eventp)->id != INVALID_EVENT);
1151 _nc_mouse_wrap(SCREEN *sp)
1152 /* release mouse -- called by endwin() before shellout/exit */
1154 TR(MY_TRACE, ("_nc_mouse_wrap() called"));
1156 switch (sp->_mouse_type) {
1158 if (sp->_mouse_mask)
1159 mouse_activate(sp, FALSE);
1162 /* GPM: pass all mouse events to next client */
1164 if (sp->_mouse_mask)
1165 mouse_activate(sp, FALSE);
1170 mouse_activate(sp, FALSE);
1179 _nc_mouse_resume(SCREEN *sp)
1180 /* re-connect to mouse -- called by doupdate() after shellout */
1182 TR(MY_TRACE, ("_nc_mouse_resume() called"));
1184 switch (sp->_mouse_type) {
1186 /* xterm: re-enable reporting */
1187 if (sp->_mouse_mask)
1188 mouse_activate(sp, TRUE);
1193 /* GPM: reclaim our event set */
1194 if (sp->_mouse_mask)
1195 mouse_activate(sp, TRUE);
1201 mouse_activate(sp, TRUE);
1209 /**************************************************************************
1211 * Mouse interface entry points for the API
1213 **************************************************************************/
1216 _nc_getmouse(SCREEN *sp, MEVENT * aevent)
1218 T((T_CALLED("getmouse(%p)"), aevent));
1220 if ((aevent != 0) && (sp != 0) && (sp->_mouse_type != M_NONE)) {
1221 MEVENT *eventp = sp->_mouse_eventp;
1222 /* compute the current-event pointer */
1223 MEVENT *prev = PREV(eventp);
1225 /* copy the event we find there */
1228 TR(TRACE_IEVENT, ("getmouse: returning event %s from slot %ld",
1229 _nc_tracemouse(sp, prev),
1230 (long) (prev - sp->_mouse_events)));
1232 prev->id = INVALID_EVENT; /* so the queue slot becomes free */
1238 /* grab a copy of the current mouse event */
1240 getmouse(MEVENT * aevent)
1242 return _nc_getmouse(SP, aevent);
1246 _nc_ungetmouse(SCREEN *sp, MEVENT * aevent)
1250 T((T_CALLED("ungetmouse(%p)"), aevent));
1252 if (aevent != 0 && sp != 0) {
1253 MEVENT *eventp = sp->_mouse_eventp;
1255 /* stick the given event in the next-free slot */
1258 /* bump the next-free pointer into the circular list */
1259 sp->_mouse_eventp = NEXT(eventp);
1261 /* push back the notification event on the keyboard queue */
1262 result = _nc_ungetch(sp, KEY_MOUSE);
1267 /* enqueue a synthesized mouse event to be seen by the next wgetch() */
1269 ungetmouse(MEVENT * aevent)
1271 return _nc_ungetmouse(SP, aevent);
1274 NCURSES_EXPORT(mmask_t)
1275 mousemask(mmask_t newmask, mmask_t * oldmask)
1276 /* set the mouse event mask */
1280 T((T_CALLED("mousemask(%#lx,%p)"), (unsigned long) newmask, oldmask));
1284 *oldmask = SP->_mouse_mask;
1286 if (newmask || SP->_mouse_initialized) {
1288 if (SP->_mouse_type != M_NONE) {
1290 (REPORT_MOUSE_POSITION
1297 | BUTTON_DOUBLE_CLICKED
1298 | BUTTON_TRIPLE_CLICKED);
1300 mouse_activate(SP, (bool) (result != 0));
1302 SP->_mouse_mask = result;
1307 *oldmask = SP->_mouse_mask;
1312 NCURSES_EXPORT(bool)
1313 wenclose(const WINDOW *win, int y, int x)
1314 /* check to see if given window encloses given screen location */
1316 bool result = FALSE;
1318 T((T_CALLED("wenclose(%p,%d,%d)"), win, y, x));
1322 result = ((win->_begy <= y &&
1324 (win->_begx + win->_maxx) >= x &&
1325 (win->_begy + win->_maxy) >= y) ? TRUE : FALSE);
1331 mouseinterval(int maxclick)
1332 /* set the maximum mouse interval within which to recognize a click */
1336 T((T_CALLED("mouseinterval(%d)"), maxclick));
1339 oldval = SP->_maxclick;
1341 SP->_maxclick = maxclick;
1343 oldval = DEFAULT_MAXCLICK;
1349 /* This may be used by other routines to ask for the existence of mouse
1354 return (SP->_mouse_type == M_NONE ? 0 : 1);
1357 NCURSES_EXPORT(bool)
1358 wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
1360 bool result = FALSE;
1362 T((T_CALLED("wmouse_trafo(%p,%p,%p,%d)"), win, pY, pX, to_screen));
1364 if (win && pY && pX) {
1369 y += win->_begy + win->_yoffset;
1371 if (wenclose(win, y, x))
1374 if (wenclose(win, y, x)) {
1375 y -= (win->_begy + win->_yoffset);