1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 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.87 2006/12/30 16:30:06 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(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(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),
193 # define M_FD(sp) sp->_mouse_fd
196 write_event(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 ignored GCC_UNUSED)
211 unsigned short fWait = MOU_WAIT;
212 /* NOPTRRECT mourt = { 0,0,24,79 }; */
215 unsigned short mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;
221 /* open the handle for the mouse */
222 if (MouOpen(NULL, &hmou) == 0) {
223 rc = MouSetEventMask(&mask, hmou);
224 if (rc) { /* retry with 2 buttons */
225 mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN;
226 rc = MouSetEventMask(&mask, hmou);
229 if (rc == 0 && MouDrawPtr(hmou) == 0) {
231 /* sit and wait on the event queue */
232 rc = MouReadEventQue(&mouev, &fWait, hmou);
234 sprintf(err, "Error reading mouse queue, rc=%lu.\r\n", rc);
237 if (!SP->_emxmouse_activated)
241 * OS/2 numbers a 3-button mouse inconsistently from other
247 if ((mouev.fs ^ oldstate) & MOUSE_BN1_DOWN)
248 write_event(mouev.fs & MOUSE_BN1_DOWN,
249 SP->_emxmouse_buttons[1], mouev.col, mouev.row);
250 if ((mouev.fs ^ oldstate) & MOUSE_BN2_DOWN)
251 write_event(mouev.fs & MOUSE_BN2_DOWN,
252 SP->_emxmouse_buttons[3], mouev.col, mouev.row);
253 if ((mouev.fs ^ oldstate) & MOUSE_BN3_DOWN)
254 write_event(mouev.fs & MOUSE_BN3_DOWN,
255 SP->_emxmouse_buttons[2], mouev.col, mouev.row);
261 sprintf(err, "Error setting event mask, buttons=%d, rc=%lu.\r\n",
264 DosWrite(2, err, strlen(err), &rc);
267 DosExit(EXIT_THREAD, 0L);
270 #endif /* USE_EMX_MOUSE */
274 handle_sysmouse(int sig GCC_UNUSED)
276 struct mouse_info the_mouse;
279 the_mouse.operation = MOUSE_GETINFO;
281 && SP->_mouse_fd >= 0
282 && SP->_sysmouse_tail < FIFO_SIZE
283 && ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
285 if (SP->_sysmouse_head > SP->_sysmouse_tail) {
286 SP->_sysmouse_tail = 0;
287 SP->_sysmouse_head = 0;
289 work = &(SP->_sysmouse_fifo[SP->_sysmouse_tail]);
290 memset(work, 0, sizeof(*work));
291 work->id = NORMAL_EVENT; /* there's only one mouse... */
293 SP->_sysmouse_old_buttons = SP->_sysmouse_new_buttons;
294 SP->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
296 if (SP->_sysmouse_new_buttons) {
297 if (SP->_sysmouse_new_buttons & 1)
298 work->bstate |= BUTTON1_PRESSED;
299 if (SP->_sysmouse_new_buttons & 2)
300 work->bstate |= BUTTON2_PRESSED;
301 if (SP->_sysmouse_new_buttons & 4)
302 work->bstate |= BUTTON3_PRESSED;
304 if (SP->_sysmouse_old_buttons & 1)
305 work->bstate |= BUTTON1_RELEASED;
306 if (SP->_sysmouse_old_buttons & 2)
307 work->bstate |= BUTTON2_RELEASED;
308 if (SP->_sysmouse_old_buttons & 4)
309 work->bstate |= BUTTON3_RELEASED;
312 /* for cosmetic bug in syscons.c on FreeBSD 3.[34] */
313 the_mouse.operation = MOUSE_HIDE;
314 ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse);
315 the_mouse.operation = MOUSE_SHOW;
316 ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse);
319 * We're only interested if the button is pressed or released.
320 * FIXME: implement continuous event-tracking.
322 if (SP->_sysmouse_new_buttons != SP->_sysmouse_old_buttons) {
323 SP->_sysmouse_tail += 1;
325 work->x = the_mouse.u.data.x / SP->_sysmouse_char_width;
326 work->y = the_mouse.u.data.y / SP->_sysmouse_char_height;
329 #endif /* USE_SYSMOUSE */
332 init_xterm_mouse(void)
334 SP->_mouse_type = M_XTERM;
335 SP->_mouse_xtermcap = tigetstr("XM");
336 if (!VALID_STRING(SP->_mouse_xtermcap))
337 SP->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
341 enable_xterm_mouse(int enable)
344 SP->_emxmouse_activated = enable;
346 putp(TPARM_1(SP->_mouse_xtermcap, enable));
348 SP->_mouse_active = enable;
353 allow_gpm_mouse(void)
355 /* GPM does printf's without checking if stdout is a terminal */
356 if (isatty(fileno(stdout))) {
357 char *env = getenv("TERM");
358 /* GPM checks the beginning of the $TERM variable to decide if
359 * it should pass xterm events through. There is no real advantage
360 * in allowing GPM to do this.
362 if (env == 0 || strncmp(env, "xterm", 5))
369 enable_gpm_mouse(int enable)
373 T((T_CALLED("enable_gpm_mouse(%d)"), enable));
375 if (enable && !SP->_mouse_active) {
376 /* GPM: initialize connection to gpm server */
377 SP->_mouse_gpm_connect.eventMask = GPM_DOWN | GPM_UP;
378 SP->_mouse_gpm_connect.defaultMask =
379 ~(SP->_mouse_gpm_connect.eventMask | GPM_HARD);
380 SP->_mouse_gpm_connect.minMod = 0;
381 SP->_mouse_gpm_connect.maxMod =
382 (unsigned short) (~((1 << KG_SHIFT) |
386 * Note: GPM hardcodes \E[?1001s and \E[?1000h during its open.
387 * The former is recognized by wscons (SunOS), and the latter by
388 * xterm. Those will not show up in ncurses' traces.
390 result = (my_Gpm_Open(&SP->_mouse_gpm_connect, 0) >= 0);
391 SP->_mouse_active = result;
392 T(("GPM open %s", result ? "succeeded" : "failed"));
394 if (!enable && SP->_mouse_active) {
395 /* GPM: close connection to gpm server */
397 SP->_mouse_active = FALSE;
404 #endif /* USE_GPM_SUPPORT */
406 #define xterm_kmous "\033[M"
409 initialize_mousetype(void)
411 T((T_CALLED("initialize_mousetype()")));
413 /* Try gpm first, because gpm may be configured to run in xterm */
415 if (allow_gpm_mouse()) {
416 if (!SP->_mouse_gpm_loaded) {
420 if ((obj = dlopen(LIBGPM_SONAME, my_RTLD)) != 0) {
421 if (GET_DLSYM(gpm_fd) == 0 ||
422 GET_DLSYM(Gpm_Open) == 0 ||
423 GET_DLSYM(Gpm_Close) == 0 ||
424 GET_DLSYM(Gpm_GetEvent) == 0) {
425 T(("GPM initialization failed: %s", dlerror()));
428 SP->_mouse_gpm_found = TRUE;
431 #else /* !HAVE_LIBDL */
432 SP->_mouse_gpm_found = TRUE;
434 SP->_mouse_gpm_loaded = TRUE;
438 * The gpm_fd file-descriptor may be negative (xterm). So we have to
439 * maintain our notion of whether the mouse connection is active
440 * without testing the file-descriptor.
442 if (SP->_mouse_gpm_found && enable_gpm_mouse(TRUE)) {
443 SP->_mouse_type = M_GPM;
444 SP->_mouse_fd = *(my_gpm_fd);
445 T(("GPM mouse_fd %d", SP->_mouse_fd));
449 #endif /* USE_GPM_SUPPORT */
453 if (!SP->_emxmouse_thread
454 && strstr(cur_term->type.term_names, "xterm") == 0
458 if (pipe(handles) < 0) {
459 perror("mouse pipe error");
464 if (!SP->_emxmouse_buttons[0]) {
465 char *s = getenv("MOUSE_BUTTONS_123");
467 SP->_emxmouse_buttons[0] = 1;
468 if (s && strlen(s) >= 3) {
469 SP->_emxmouse_buttons[1] = s[0] - '0';
470 SP->_emxmouse_buttons[2] = s[1] - '0';
471 SP->_emxmouse_buttons[3] = s[2] - '0';
473 SP->_emxmouse_buttons[1] = 1;
474 SP->_emxmouse_buttons[2] = 3;
475 SP->_emxmouse_buttons[3] = 2;
478 SP->_emxmouse_wfd = handles[1];
479 M_FD(SP) = handles[0];
481 setmode(handles[0], O_BINARY);
482 setmode(handles[1], O_BINARY);
483 /* Do not use CRT functions, we may single-threaded. */
484 rc = DosCreateThread((unsigned long *) &SP->_emxmouse_thread,
485 mouse_server, 0, 0, 8192);
487 printf("mouse thread error %d=%#x", rc, rc);
489 SP->_mouse_type = M_XTERM;
494 #endif /* USE_EMX_MOUSE */
498 struct mouse_info the_mouse;
499 char *the_device = 0;
501 if (isatty(SP->_ifd))
502 the_device = ttyname(SP->_ifd);
504 the_device = "/dev/tty";
506 SP->_mouse_fd = open(the_device, O_RDWR);
508 if (SP->_mouse_fd >= 0) {
510 * sysmouse does not have a usable user interface for obtaining
511 * mouse events. The logical way to proceed (reading data on a
512 * stream) only works if one opens the device as root. Even in
513 * that mode, careful examination shows we lose events
514 * occasionally. The interface provided for user programs is to
515 * establish a signal handler. really.
517 * Take over SIGUSR2 for this purpose since SIGUSR1 is more
518 * likely to be used by an application. getch() will have to
519 * handle the misleading EINTR's.
521 signal(SIGUSR2, SIG_IGN);
522 the_mouse.operation = MOUSE_MODE;
523 the_mouse.u.mode.mode = 0;
524 the_mouse.u.mode.signal = SIGUSR2;
525 if (ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
526 signal(SIGUSR2, handle_sysmouse);
527 the_mouse.operation = MOUSE_SHOW;
528 ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse);
530 #if defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) /* FreeBSD > 2.x */
532 #ifndef FBIO_GETMODE /* FreeBSD 3.x */
533 #define FBIO_GETMODE CONS_GET
534 #define FBIO_MODEINFO CONS_MODEINFO
535 #endif /* FBIO_GETMODE */
536 video_info_t the_video;
538 if (ioctl(SP->_mouse_fd,
540 &the_video.vi_mode) != -1
541 && ioctl(SP->_mouse_fd,
544 SP->_sysmouse_char_width = the_video.vi_cwidth;
545 SP->_sysmouse_char_height = the_video.vi_cheight;
548 #endif /* defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) */
550 if (SP->_sysmouse_char_width <= 0)
551 SP->_sysmouse_char_width = 8;
552 if (SP->_sysmouse_char_height <= 0)
553 SP->_sysmouse_char_height = 16;
554 SP->_mouse_type = M_SYSMOUSE;
559 #endif /* USE_SYSMOUSE */
561 /* we know how to recognize mouse events under "xterm" */
562 if (key_mouse != 0) {
563 if (!strcmp(key_mouse, xterm_kmous)
564 || strstr(cur_term->type.term_names, "xterm") != 0) {
567 } else if (strstr(cur_term->type.term_names, "xterm") != 0) {
568 if (_nc_add_to_try(&(SP->_keytry), xterm_kmous, KEY_MOUSE) == OK)
576 /* initialize the mouse */
582 if (!SP->_mouse_initialized) {
583 SP->_mouse_initialized = TRUE;
585 TR(MY_TRACE, ("_nc_mouse_init() called"));
587 SP->_mouse_eventp = SP->_mouse_events;
588 for (i = 0; i < EV_MAX; i++)
589 SP->_mouse_events[i].id = INVALID_EVENT;
591 initialize_mousetype();
593 T(("_nc_mouse_init() set mousetype to %d", SP->_mouse_type));
595 result = SP->_mouse_initialized;
601 * Query to see if there is a pending mouse event. This is called from
602 * fifo_push() in lib_getch.c
605 _nc_mouse_event(SCREEN *sp GCC_UNUSED)
607 MEVENT *eventp = SP->_mouse_eventp;
612 switch (SP->_mouse_type) {
614 /* xterm: never have to query, mouse events are in the keyboard stream */
619 int i, res = read(M_FD(sp), &kbuf, 3); /* Eat the prefix */
621 printf("Got %d chars instead of 3 for prefix.\n", res);
622 for (i = 0; i < res; i++) {
623 if (kbuf[i] != key_mouse[i])
624 printf("Got char %d instead of %d for prefix.\n",
625 (int) kbuf[i], (int) key_mouse[i]);
629 #endif /* USE_EMX_MOUSE */
635 /* query server for event, return TRUE if we find one */
638 if (my_Gpm_GetEvent(&ev) == 1) {
639 /* there's only one mouse... */
640 eventp->id = NORMAL_EVENT;
643 switch (ev.type & 0x0f) {
645 if (ev.buttons & GPM_B_LEFT)
646 eventp->bstate |= BUTTON1_PRESSED;
647 if (ev.buttons & GPM_B_MIDDLE)
648 eventp->bstate |= BUTTON2_PRESSED;
649 if (ev.buttons & GPM_B_RIGHT)
650 eventp->bstate |= BUTTON3_PRESSED;
653 if (ev.buttons & GPM_B_LEFT)
654 eventp->bstate |= BUTTON1_RELEASED;
655 if (ev.buttons & GPM_B_MIDDLE)
656 eventp->bstate |= BUTTON2_RELEASED;
657 if (ev.buttons & GPM_B_RIGHT)
658 eventp->bstate |= BUTTON3_RELEASED;
664 eventp->x = ev.x - 1;
665 eventp->y = ev.y - 1;
668 /* bump the next-free pointer into the circular list */
669 SP->_mouse_eventp = eventp = NEXT(eventp);
678 if (SP->_sysmouse_head < SP->_sysmouse_tail) {
679 *eventp = SP->_sysmouse_fifo[SP->_sysmouse_head];
682 * Point the fifo-head to the next possible location. If there
683 * are none, reset the indices. This may be interrupted by the
684 * signal handler, doing essentially the same reset.
686 SP->_sysmouse_head += 1;
687 if (SP->_sysmouse_head == SP->_sysmouse_tail) {
688 SP->_sysmouse_tail = 0;
689 SP->_sysmouse_head = 0;
692 /* bump the next-free pointer into the circular list */
693 SP->_mouse_eventp = eventp = NEXT(eventp);
697 #endif /* USE_SYSMOUSE */
703 return result; /* true if we found an event */
707 _nc_mouse_inline(SCREEN *sp)
708 /* mouse report received in the keyboard stream -- parse its info */
712 MEVENT *eventp = SP->_mouse_eventp;
714 TR(MY_TRACE, ("_nc_mouse_inline() called"));
716 if (SP->_mouse_type == M_XTERM) {
717 unsigned char kbuf[4];
722 /* This code requires that your xterm entry contain the kmous
723 * capability and that it be set to the \E[M documented in the
724 * Xterm Control Sequences reference. This is how we
725 * arrange for mouse events to be reported via a KEY_MOUSE
726 * return value from wgetch(). After this value is received,
727 * _nc_mouse_inline() gets called and is immediately
728 * responsible for parsing the mouse status information
729 * following the prefix.
731 * The following quotes from the ctrlseqs.ms document in the
732 * X distribution, describing the X mouse tracking feature:
734 * Parameters for all mouse tracking escape sequences
735 * generated by xterm encode numeric parameters in a single
736 * character as value+040. For example, ! is 1.
738 * On button press or release, xterm sends ESC [ M CbCxCy.
739 * The low two bits of Cb encode button information: 0=MB1
740 * pressed, 1=MB2 pressed, 2=MB3 pressed, 3=release. The
741 * upper bits encode what modifiers were down when the
742 * button was pressed and are added together. 4=Shift,
743 * 8=Meta, 16=Control. Cx and Cy are the x and y coordinates
744 * of the mouse event. The upper left corner is (1,1).
746 * (End quote) By the time we get here, we've eaten the
747 * key prefix. FYI, the loop below is necessary because
748 * mouse click info isn't guaranteed to present as a
751 * Wheel mice may return buttons 4 and 5 when the wheel is turned.
752 * We encode those as button presses.
754 for (grabbed = 0; grabbed < 3; grabbed += res) {
756 /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
758 res = read(M_FD(sp) >= 0 ? M_FD(sp) : sp->_ifd, &kbuf, 3);
760 res = read(sp->_ifd, kbuf + grabbed, 3 - grabbed);
768 ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
770 /* there's only one mouse... */
771 eventp->id = NORMAL_EVENT;
773 /* processing code goes here */
775 prev = PREV(eventp)->bstate;
778 #define PRESS_POSITION(n) \
779 eventp->bstate = MASK_PRESS(n); \
780 if (kbuf[0] & 0x40) \
781 eventp->bstate = MASK_RELEASE(n)
783 #define PRESS_POSITION(n) \
784 eventp->bstate = (prev & MASK_PRESS(n) \
785 ? REPORT_MOUSE_POSITION \
789 switch (kbuf[0] & 0x3) {
792 eventp->bstate = MASK_PRESS(4);
798 #if NCURSES_MOUSE_VERSION == 2
800 eventp->bstate = MASK_PRESS(5);
812 * Release events aren't reported for individual buttons, just for
813 * the button set as a whole. However, because there are normally
814 * no mouse events under xterm that intervene between press and
815 * release, we can infer the button actually released by looking at
816 * the previous event.
818 if (prev & (BUTTON_PRESSED | BUTTON_RELEASED)) {
819 eventp->bstate = BUTTON_RELEASED;
820 for (b = 1; b <= MAX_BUTTONS; ++b) {
821 if (!(prev & MASK_PRESS(b)))
822 eventp->bstate &= ~MASK_RELEASE(b);
826 * XFree86 xterm will return a stream of release-events to
827 * let the application know where the mouse is going, if the
828 * private mode 1002 or 1003 is enabled.
830 eventp->bstate = REPORT_MOUSE_POSITION;
834 result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
837 eventp->bstate |= BUTTON_SHIFT;
840 eventp->bstate |= BUTTON_ALT;
843 eventp->bstate |= BUTTON_CTRL;
846 eventp->x = (kbuf[1] - ' ') - 1;
847 eventp->y = (kbuf[2] - ' ') - 1;
849 ("_nc_mouse_inline: primitive mouse-event %s has slot %ld",
851 (long) (eventp - SP->_mouse_events)));
853 /* bump the next-free pointer into the circular list */
854 SP->_mouse_eventp = NEXT(eventp);
855 #if 0 /* this return would be needed for QNX's mods to lib_getch.c */
864 mouse_activate(bool on)
866 if (!on && !SP->_mouse_initialized)
869 if (!_nc_mouse_init())
874 switch (SP->_mouse_type) {
876 #if NCURSES_EXT_FUNCS
877 keyok(KEY_MOUSE, on);
879 TPUTS_TRACE("xterm mouse initialization");
880 enable_xterm_mouse(1);
884 if (enable_gpm_mouse(1)) {
885 SP->_mouse_fd = *(my_gpm_fd);
886 T(("GPM mouse_fd %d", SP->_mouse_fd));
892 signal(SIGUSR2, handle_sysmouse);
893 SP->_mouse_active = TRUE;
899 /* Make runtime binding to cut down on object size of applications that
900 * do not use the mouse (e.g., 'clear').
902 SP->_mouse_event = _nc_mouse_event;
903 SP->_mouse_inline = _nc_mouse_inline;
904 SP->_mouse_parse = _nc_mouse_parse;
905 SP->_mouse_resume = _nc_mouse_resume;
906 SP->_mouse_wrap = _nc_mouse_wrap;
909 switch (SP->_mouse_type) {
911 TPUTS_TRACE("xterm mouse deinitialization");
912 enable_xterm_mouse(0);
921 signal(SIGUSR2, SIG_IGN);
922 SP->_mouse_active = FALSE;
932 /**************************************************************************
934 * Device-independent code
936 **************************************************************************/
939 _nc_mouse_parse(int runcount)
940 /* parse a run of atomic mouse events into a gesture */
942 MEVENT *eventp = SP->_mouse_eventp;
943 MEVENT *ep, *runp, *next, *prev = PREV(eventp);
948 TR(MY_TRACE, ("_nc_mouse_parse(%d) called", runcount));
951 * When we enter this routine, the event list next-free pointer
952 * points just past a run of mouse events that we know were separated
953 * in time by less than the critical click interval. The job of this
954 * routine is to collapse this run into a single higher-level event
957 * We accomplish this in two passes. The first pass merges press/release
958 * pairs into click events. The second merges runs of click events into
959 * double or triple-click events.
961 * It's possible that the run may not resolve to a single event (for
962 * example, if the user quadruple-clicks). If so, leading events
963 * in the run are ignored.
965 * Note that this routine is independent of the format of the specific
966 * format of the pointing-device's reports. We can use it to parse
967 * gestures on anything that reports press/release events on a per-
968 * button basis, as long as the device-dependent mouse code puts stuff
969 * on the queue in MEVENT format.
973 ("_nc_mouse_parse: returning simple mouse event %s at slot %ld",
975 (long) (prev - SP->_mouse_events)));
976 return (prev->id >= NORMAL_EVENT)
977 ? ((prev->bstate & SP->_mouse_mask) ? TRUE : FALSE)
981 /* find the start of the run */
983 for (n = runcount; n > 0; n--) {
988 if (_nc_tracing & TRACE_IEVENT) {
989 _trace_slot("before mouse press/release merge:");
990 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
991 (long) (runp - SP->_mouse_events),
992 (long) ((eventp - SP->_mouse_events) + (EV_MAX - 1)) % EV_MAX,
997 /* first pass; merge press/release pairs */
1000 for (ep = runp; (next = NEXT(ep)) != eventp; ep = next) {
1002 #define MASK_CHANGED(x) (!(ep->bstate & MASK_PRESS(x)) \
1003 == !(next->bstate & MASK_RELEASE(x)))
1005 if (ep->x == next->x && ep->y == next->y
1006 && (ep->bstate & BUTTON_PRESSED)
1011 #if NCURSES_MOUSE_VERSION == 2
1015 for (b = 1; b <= MAX_BUTTONS; ++b) {
1016 if ((SP->_mouse_mask & MASK_CLICK(b))
1017 && (ep->bstate & MASK_PRESS(b))) {
1018 ep->bstate &= ~MASK_PRESS(b);
1019 ep->bstate |= MASK_CLICK(b);
1024 next->id = INVALID_EVENT;
1031 if (_nc_tracing & TRACE_IEVENT) {
1032 _trace_slot("before mouse click merge:");
1033 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1034 (long) (runp - SP->_mouse_events),
1035 (long) ((eventp - SP->_mouse_events) + (EV_MAX - 1)) % EV_MAX,
1041 * Second pass; merge click runs. At this point, click events are
1042 * each followed by one invalid event. We merge click events
1043 * forward in the queue.
1045 * NOTE: There is a problem with this design! If the application
1046 * allows enough click events to pile up in the circular queue so
1047 * they wrap around, it will cheerfully merge the newest forward
1048 * into the oldest, creating a bogus doubleclick and confusing
1049 * the queue-traversal logic rather badly. Generally this won't
1050 * happen, because calling getmouse() marks old events invalid and
1051 * ineligible for merges. The true solution to this problem would
1052 * be to timestamp each MEVENT and perform the obvious sanity check,
1053 * but the timer element would have to have sub-second resolution,
1054 * which would get us into portability trouble.
1060 for (ep = runp; (next = NEXT(ep)) != eventp; ep = next)
1061 if (ep->id != INVALID_EVENT) {
1062 if (next->id != INVALID_EVENT)
1064 follower = NEXT(next);
1065 if (follower->id == INVALID_EVENT)
1068 /* merge click events forward */
1069 if ((ep->bstate & BUTTON_CLICKED)
1070 && (follower->bstate & BUTTON_CLICKED)) {
1071 for (b = 1; b <= MAX_BUTTONS; ++b) {
1072 if ((SP->_mouse_mask & MASK_DOUBLE_CLICK(b))
1073 && (follower->bstate & MASK_CLICK(b))) {
1074 follower->bstate &= ~MASK_CLICK(b);
1075 follower->bstate |= MASK_DOUBLE_CLICK(b);
1080 ep->id = INVALID_EVENT;
1083 /* merge double-click events forward */
1084 if ((ep->bstate & BUTTON_DOUBLE_CLICKED)
1085 && (follower->bstate & BUTTON_CLICKED)) {
1086 for (b = 1; b <= MAX_BUTTONS; ++b) {
1087 if ((SP->_mouse_mask & MASK_TRIPLE_CLICK(b))
1088 && (follower->bstate & MASK_CLICK(b))) {
1089 follower->bstate &= ~MASK_CLICK(b);
1090 follower->bstate |= MASK_TRIPLE_CLICK(b);
1095 ep->id = INVALID_EVENT;
1102 if (_nc_tracing & TRACE_IEVENT) {
1103 _trace_slot("before mouse event queue compaction:");
1104 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1105 (long) (runp - SP->_mouse_events),
1106 (long) ((eventp - SP->_mouse_events) + (EV_MAX - 1)) % EV_MAX,
1112 * Now try to throw away trailing events flagged invalid, or that
1113 * don't match the current event mask.
1115 for (; runcount; prev = PREV(eventp), runcount--)
1116 if (prev->id == INVALID_EVENT || !(prev->bstate & SP->_mouse_mask)) {
1117 SP->_mouse_eventp = eventp = prev;
1120 if (_nc_tracing & TRACE_IEVENT) {
1121 _trace_slot("after mouse event queue compaction:");
1122 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1123 (long) (runp - SP->_mouse_events),
1124 (long) ((eventp - SP->_mouse_events) + (EV_MAX - 1)) % EV_MAX,
1127 for (ep = runp; ep != eventp; ep = NEXT(ep))
1128 if (ep->id != INVALID_EVENT)
1130 ("_nc_mouse_parse: returning composite mouse event %s at slot %ld",
1132 (long) (ep - SP->_mouse_events)));
1135 /* after all this, do we have a valid event? */
1136 return (PREV(eventp)->id != INVALID_EVENT);
1140 _nc_mouse_wrap(SCREEN *sp GCC_UNUSED)
1141 /* release mouse -- called by endwin() before shellout/exit */
1143 TR(MY_TRACE, ("_nc_mouse_wrap() called"));
1145 switch (SP->_mouse_type) {
1147 if (SP->_mouse_mask)
1148 mouse_activate(FALSE);
1151 /* GPM: pass all mouse events to next client */
1153 if (SP->_mouse_mask)
1154 mouse_activate(FALSE);
1159 mouse_activate(FALSE);
1168 _nc_mouse_resume(SCREEN *sp GCC_UNUSED)
1169 /* re-connect to mouse -- called by doupdate() after shellout */
1171 TR(MY_TRACE, ("_nc_mouse_resume() called"));
1173 switch (SP->_mouse_type) {
1175 /* xterm: re-enable reporting */
1176 if (SP->_mouse_mask)
1177 mouse_activate(TRUE);
1182 /* GPM: reclaim our event set */
1183 if (SP->_mouse_mask)
1184 mouse_activate(TRUE);
1190 mouse_activate(TRUE);
1198 /**************************************************************************
1200 * Mouse interface entry points for the API
1202 **************************************************************************/
1205 getmouse(MEVENT * aevent)
1206 /* grab a copy of the current mouse event */
1208 T((T_CALLED("getmouse(%p)"), aevent));
1210 if ((aevent != 0) && (SP != 0) && (SP->_mouse_type != M_NONE)) {
1211 MEVENT *eventp = SP->_mouse_eventp;
1212 /* compute the current-event pointer */
1213 MEVENT *prev = PREV(eventp);
1215 /* copy the event we find there */
1218 TR(TRACE_IEVENT, ("getmouse: returning event %s from slot %ld",
1220 (long) (prev - SP->_mouse_events)));
1222 prev->id = INVALID_EVENT; /* so the queue slot becomes free */
1229 ungetmouse(MEVENT * aevent)
1230 /* enqueue a synthesized mouse event to be seen by the next wgetch() */
1234 T((T_CALLED("ungetmouse(%p)"), aevent));
1236 if (aevent != 0 && SP != 0) {
1237 MEVENT *eventp = SP->_mouse_eventp;
1239 /* stick the given event in the next-free slot */
1242 /* bump the next-free pointer into the circular list */
1243 SP->_mouse_eventp = NEXT(eventp);
1245 /* push back the notification event on the keyboard queue */
1246 result = ungetch(KEY_MOUSE);
1251 NCURSES_EXPORT(mmask_t)
1252 mousemask(mmask_t newmask, mmask_t * oldmask)
1253 /* set the mouse event mask */
1257 T((T_CALLED("mousemask(%#lx,%p)"), (unsigned long) newmask, oldmask));
1261 *oldmask = SP->_mouse_mask;
1263 if (newmask || SP->_mouse_initialized) {
1265 if (SP->_mouse_type != M_NONE) {
1267 (REPORT_MOUSE_POSITION
1274 | BUTTON_DOUBLE_CLICKED
1275 | BUTTON_TRIPLE_CLICKED);
1277 mouse_activate((bool) (result != 0));
1279 SP->_mouse_mask = result;
1284 *oldmask = SP->_mouse_mask;
1289 NCURSES_EXPORT(bool)
1290 wenclose(const WINDOW *win, int y, int x)
1291 /* check to see if given window encloses given screen location */
1293 bool result = FALSE;
1295 T((T_CALLED("wenclose(%p,%d,%d)"), win, y, x));
1299 result = ((win->_begy <= y &&
1301 (win->_begx + win->_maxx) >= x &&
1302 (win->_begy + win->_maxy) >= y) ? TRUE : FALSE);
1308 mouseinterval(int maxclick)
1309 /* set the maximum mouse interval within which to recognize a click */
1313 T((T_CALLED("mouseinterval(%d)"), maxclick));
1316 oldval = SP->_maxclick;
1318 SP->_maxclick = maxclick;
1320 oldval = DEFAULT_MAXCLICK;
1326 /* This may be used by other routines to ask for the existence of mouse
1331 return (SP->_mouse_type == M_NONE ? 0 : 1);
1334 NCURSES_EXPORT(bool)
1335 wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
1337 bool result = FALSE;
1339 T((T_CALLED("wmouse_trafo(%p,%p,%p,%d)"), win, pY, pX, to_screen));
1341 if (win && pY && pX) {
1346 y += win->_begy + win->_yoffset;
1348 if (wenclose(win, y, x))
1351 if (wenclose(win, y, x)) {
1352 y -= (win->_begy + win->_yoffset);