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.104 2008/11/30 01:37:27 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(SP->_dlopen_gpm, #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 */
163 #define FirstEV(sp) ((sp)->_mouse_events)
164 #define LastEV(sp) ((sp)->_mouse_events + EV_MAX - 1)
167 #define NEXT(ep) ((ep >= LastEV(sp)) \
172 #define PREV(ep) ((ep <= FirstEV(sp)) \
176 #define IndexEV(sp, ep) (ep - FirstEV(sp))
178 #define RunParams(sp, eventp, runp) \
179 (long) IndexEV(sp, runp), \
180 (long) (IndexEV(sp, eventp) + (EV_MAX - 1)) % EV_MAX
184 _trace_slot(SCREEN *sp, const char *tag)
190 for (ep = FirstEV(sp); ep <= LastEV(sp); ep++)
191 _tracef("mouse event queue slot %ld = %s",
192 (long) IndexEV(sp, ep),
193 _nc_tracemouse(sp, ep));
202 # define M_FD(sp) sp->_mouse_fd
205 write_event(SCREEN *sp, int down, int button, int x, int y)
208 unsigned long ignore;
210 strncpy(buf, key_mouse, 3); /* should be "\033[M" */
211 buf[3] = ' ' + (button - 1) + (down ? 0 : 0x40);
212 buf[4] = ' ' + x - LEFT_COL + 1;
213 buf[5] = ' ' + y - TOP_ROW + 1;
214 DosWrite(sp->_emxmouse_wfd, buf, 6, &ignore);
218 mouse_server(unsigned long param)
220 SCREEN *sp = (SCREEN *) param;
221 unsigned short fWait = MOU_WAIT;
222 /* NOPTRRECT mourt = { 0,0,24,79 }; */
225 unsigned short mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;
231 /* open the handle for the mouse */
232 if (MouOpen(NULL, &hmou) == 0) {
233 rc = MouSetEventMask(&mask, hmou);
234 if (rc) { /* retry with 2 buttons */
235 mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN;
236 rc = MouSetEventMask(&mask, hmou);
239 if (rc == 0 && MouDrawPtr(hmou) == 0) {
241 /* sit and wait on the event queue */
242 rc = MouReadEventQue(&mouev, &fWait, hmou);
244 sprintf(err, "Error reading mouse queue, rc=%lu.\r\n", rc);
247 if (!sp->_emxmouse_activated)
251 * OS/2 numbers a 3-button mouse inconsistently from other
257 if ((mouev.fs ^ oldstate) & MOUSE_BN1_DOWN)
258 write_event(sp, mouev.fs & MOUSE_BN1_DOWN,
259 sp->_emxmouse_buttons[1], mouev.col, mouev.row);
260 if ((mouev.fs ^ oldstate) & MOUSE_BN2_DOWN)
261 write_event(sp, mouev.fs & MOUSE_BN2_DOWN,
262 sp->_emxmouse_buttons[3], mouev.col, mouev.row);
263 if ((mouev.fs ^ oldstate) & MOUSE_BN3_DOWN)
264 write_event(sp, mouev.fs & MOUSE_BN3_DOWN,
265 sp->_emxmouse_buttons[2], mouev.col, mouev.row);
271 sprintf(err, "Error setting event mask, buttons=%d, rc=%lu.\r\n",
274 DosWrite(2, err, strlen(err), &rc);
277 DosExit(EXIT_THREAD, 0L);
280 #endif /* USE_EMX_MOUSE */
284 sysmouse_server(SCREEN *sp)
286 struct mouse_info the_mouse;
289 the_mouse.operation = MOUSE_GETINFO;
291 && sp->_mouse_fd >= 0
292 && sp->_sysmouse_tail < FIFO_SIZE
293 && ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
295 if (sp->_sysmouse_head > sp->_sysmouse_tail) {
296 sp->_sysmouse_tail = 0;
297 sp->_sysmouse_head = 0;
299 work = &(sp->_sysmouse_fifo[sp->_sysmouse_tail]);
300 memset(work, 0, sizeof(*work));
301 work->id = NORMAL_EVENT; /* there's only one mouse... */
303 sp->_sysmouse_old_buttons = sp->_sysmouse_new_buttons;
304 sp->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
306 if (sp->_sysmouse_new_buttons) {
307 if (sp->_sysmouse_new_buttons & 1)
308 work->bstate |= BUTTON1_PRESSED;
309 if (sp->_sysmouse_new_buttons & 2)
310 work->bstate |= BUTTON2_PRESSED;
311 if (sp->_sysmouse_new_buttons & 4)
312 work->bstate |= BUTTON3_PRESSED;
314 if (sp->_sysmouse_old_buttons & 1)
315 work->bstate |= BUTTON1_RELEASED;
316 if (sp->_sysmouse_old_buttons & 2)
317 work->bstate |= BUTTON2_RELEASED;
318 if (sp->_sysmouse_old_buttons & 4)
319 work->bstate |= BUTTON3_RELEASED;
322 /* for cosmetic bug in syscons.c on FreeBSD 3.[34] */
323 the_mouse.operation = MOUSE_HIDE;
324 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
325 the_mouse.operation = MOUSE_SHOW;
326 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
329 * We're only interested if the button is pressed or released.
330 * FIXME: implement continuous event-tracking.
332 if (sp->_sysmouse_new_buttons != sp->_sysmouse_old_buttons) {
333 sp->_sysmouse_tail += 1;
335 work->x = the_mouse.u.data.x / sp->_sysmouse_char_width;
336 work->y = the_mouse.u.data.y / sp->_sysmouse_char_height;
341 handle_sysmouse(int sig GCC_UNUSED)
345 #endif /* USE_SYSMOUSE */
348 init_xterm_mouse(SCREEN *sp)
350 sp->_mouse_type = M_XTERM;
351 sp->_mouse_xtermcap = tigetstr("XM");
352 if (!VALID_STRING(sp->_mouse_xtermcap))
353 sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
357 enable_xterm_mouse(SCREEN *sp, int enable)
360 sp->_emxmouse_activated = enable;
362 putp(TPARM_1(sp->_mouse_xtermcap, enable));
364 sp->_mouse_active = enable;
369 allow_gpm_mouse(void)
373 /* GPM does printf's without checking if stdout is a terminal */
374 if (isatty(fileno(stdout))) {
375 char *list = getenv("NCURSES_GPM_TERMS");
376 char *env = getenv("TERM");
379 result = _nc_name_match(list, env, "|:");
382 /* GPM checks the beginning of the $TERM variable to decide if it
383 * should pass xterm events through. There is no real advantage in
384 * allowing GPM to do this. Recent versions relax that check, and
385 * pretend that GPM can work with any terminal having the kmous
386 * capability. Perhaps that works for someone. If so, they can
387 * set the environment variable (above).
389 if (env != 0 && strstr(env, "linux") != 0) {
399 unload_gpm_library(SCREEN *sp)
401 if (SP->_dlopen_gpm != 0) {
402 T(("unload GPM library"));
403 sp->_mouse_gpm_loaded = FALSE;
405 dlclose(sp->_dlopen_gpm);
411 load_gpm_library(SCREEN *sp)
413 sp->_mouse_gpm_found = FALSE;
414 if ((sp->_dlopen_gpm = dlopen(LIBGPM_SONAME, my_RTLD)) != 0) {
415 if (GET_DLSYM(gpm_fd) == 0 ||
416 GET_DLSYM(Gpm_Open) == 0 ||
417 GET_DLSYM(Gpm_Close) == 0 ||
418 GET_DLSYM(Gpm_GetEvent) == 0) {
419 T(("GPM initialization failed: %s", dlerror()));
420 unload_gpm_library(sp);
422 sp->_mouse_gpm_found = TRUE;
423 sp->_mouse_gpm_loaded = TRUE;
430 enable_gpm_mouse(SCREEN *sp, bool enable)
434 T((T_CALLED("enable_gpm_mouse(%d)"), enable));
436 if (enable && !sp->_mouse_active) {
438 if (sp->_mouse_gpm_found && !sp->_mouse_gpm_loaded) {
439 load_gpm_library(sp);
442 if (sp->_mouse_gpm_loaded) {
443 /* GPM: initialize connection to gpm server */
444 sp->_mouse_gpm_connect.eventMask = GPM_DOWN | GPM_UP;
445 sp->_mouse_gpm_connect.defaultMask =
446 (unsigned short) (~(sp->_mouse_gpm_connect.eventMask | GPM_HARD));
447 sp->_mouse_gpm_connect.minMod = 0;
448 sp->_mouse_gpm_connect.maxMod =
449 (unsigned short) (~((1 << KG_SHIFT) |
453 * Note: GPM hardcodes \E[?1001s and \E[?1000h during its open.
454 * The former is recognized by wscons (SunOS), and the latter by
455 * xterm. Those will not show up in ncurses' traces.
457 result = (my_Gpm_Open(&sp->_mouse_gpm_connect, 0) >= 0);
461 sp->_mouse_active = result;
462 T(("GPM open %s", result ? "succeeded" : "failed"));
464 if (!enable && sp->_mouse_active) {
465 /* GPM: close connection to gpm server */
467 sp->_mouse_active = FALSE;
474 unload_gpm_library(sp);
479 #endif /* USE_GPM_SUPPORT */
481 #define xterm_kmous "\033[M"
484 initialize_mousetype(SCREEN *sp)
486 T((T_CALLED("initialize_mousetype()")));
488 /* Try gpm first, because gpm may be configured to run in xterm */
490 if (allow_gpm_mouse()) {
491 if (!sp->_mouse_gpm_loaded) {
493 load_gpm_library(sp);
494 #else /* !HAVE_LIBDL */
495 sp->_mouse_gpm_found = TRUE;
496 sp->_mouse_gpm_loaded = TRUE;
501 * The gpm_fd file-descriptor may be negative (xterm). So we have to
502 * maintain our notion of whether the mouse connection is active
503 * without testing the file-descriptor.
505 if (sp->_mouse_gpm_found && enable_gpm_mouse(sp, TRUE)) {
506 sp->_mouse_type = M_GPM;
507 sp->_mouse_fd = *(my_gpm_fd);
508 T(("GPM mouse_fd %d", sp->_mouse_fd));
512 #endif /* USE_GPM_SUPPORT */
516 if (!sp->_emxmouse_thread
517 && strstr(cur_term->type.term_names, "xterm") == 0
521 if (pipe(handles) < 0) {
522 perror("mouse pipe error");
527 if (!sp->_emxmouse_buttons[0]) {
528 char *s = getenv("MOUSE_BUTTONS_123");
530 sp->_emxmouse_buttons[0] = 1;
531 if (s && strlen(s) >= 3) {
532 sp->_emxmouse_buttons[1] = s[0] - '0';
533 sp->_emxmouse_buttons[2] = s[1] - '0';
534 sp->_emxmouse_buttons[3] = s[2] - '0';
536 sp->_emxmouse_buttons[1] = 1;
537 sp->_emxmouse_buttons[2] = 3;
538 sp->_emxmouse_buttons[3] = 2;
541 sp->_emxmouse_wfd = handles[1];
542 M_FD(sp) = handles[0];
544 setmode(handles[0], O_BINARY);
545 setmode(handles[1], O_BINARY);
546 /* Do not use CRT functions, we may single-threaded. */
547 rc = DosCreateThread((unsigned long *) &sp->_emxmouse_thread,
548 mouse_server, (long) sp, 0, 8192);
550 printf("mouse thread error %d=%#x", rc, rc);
552 sp->_mouse_type = M_XTERM;
557 #endif /* USE_EMX_MOUSE */
561 struct mouse_info the_mouse;
562 char *the_device = 0;
564 if (isatty(sp->_ifd))
565 the_device = ttyname(sp->_ifd);
567 the_device = "/dev/tty";
569 sp->_mouse_fd = open(the_device, O_RDWR);
571 if (sp->_mouse_fd >= 0) {
573 * sysmouse does not have a usable user interface for obtaining
574 * mouse events. The logical way to proceed (reading data on a
575 * stream) only works if one opens the device as root. Even in
576 * that mode, careful examination shows we lose events
577 * occasionally. The interface provided for user programs is to
578 * establish a signal handler. really.
580 * Take over SIGUSR2 for this purpose since SIGUSR1 is more
581 * likely to be used by an application. getch() will have to
582 * handle the misleading EINTR's.
584 signal(SIGUSR2, SIG_IGN);
585 the_mouse.operation = MOUSE_MODE;
586 the_mouse.u.mode.mode = 0;
587 the_mouse.u.mode.signal = SIGUSR2;
588 if (ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
589 signal(SIGUSR2, handle_sysmouse);
590 the_mouse.operation = MOUSE_SHOW;
591 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
593 #if defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) /* FreeBSD > 2.x */
595 #ifndef FBIO_GETMODE /* FreeBSD 3.x */
596 #define FBIO_GETMODE CONS_GET
597 #define FBIO_MODEINFO CONS_MODEINFO
598 #endif /* FBIO_GETMODE */
599 video_info_t the_video;
601 if (ioctl(sp->_mouse_fd,
603 &the_video.vi_mode) != -1
604 && ioctl(sp->_mouse_fd,
607 sp->_sysmouse_char_width = the_video.vi_cwidth;
608 sp->_sysmouse_char_height = the_video.vi_cheight;
611 #endif /* defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) */
613 if (sp->_sysmouse_char_width <= 0)
614 sp->_sysmouse_char_width = 8;
615 if (sp->_sysmouse_char_height <= 0)
616 sp->_sysmouse_char_height = 16;
617 sp->_mouse_type = M_SYSMOUSE;
622 #endif /* USE_SYSMOUSE */
624 /* we know how to recognize mouse events under "xterm" */
625 if (key_mouse != 0) {
626 if (!strcmp(key_mouse, xterm_kmous)
627 || strstr(cur_term->type.term_names, "xterm") != 0) {
628 init_xterm_mouse(sp);
630 } else if (strstr(cur_term->type.term_names, "xterm") != 0) {
631 if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
632 init_xterm_mouse(sp);
638 _nc_mouse_init(SCREEN *sp)
639 /* initialize the mouse */
645 if (!sp->_mouse_initialized) {
646 sp->_mouse_initialized = TRUE;
648 TR(MY_TRACE, ("_nc_mouse_init() called"));
650 sp->_mouse_eventp = FirstEV(sp);
651 for (i = 0; i < EV_MAX; i++)
652 sp->_mouse_events[i].id = INVALID_EVENT;
654 initialize_mousetype(sp);
656 T(("_nc_mouse_init() set mousetype to %d", sp->_mouse_type));
658 result = sp->_mouse_initialized;
664 * Query to see if there is a pending mouse event. This is called from
665 * fifo_push() in lib_getch.c
668 _nc_mouse_event(SCREEN *sp GCC_UNUSED)
670 MEVENT *eventp = sp->_mouse_eventp;
675 switch (sp->_mouse_type) {
677 /* xterm: never have to query, mouse events are in the keyboard stream */
682 int i, res = read(M_FD(sp), &kbuf, 3); /* Eat the prefix */
684 printf("Got %d chars instead of 3 for prefix.\n", res);
685 for (i = 0; i < res; i++) {
686 if (kbuf[i] != key_mouse[i])
687 printf("Got char %d instead of %d for prefix.\n",
688 (int) kbuf[i], (int) key_mouse[i]);
692 #endif /* USE_EMX_MOUSE */
697 if (sp->_mouse_fd >= 0) {
698 /* query server for event, return TRUE if we find one */
701 switch (my_Gpm_GetEvent(&ev)) {
703 /* Connection closed, drop the mouse. */
707 /* there's only one mouse... */
708 eventp->id = NORMAL_EVENT;
711 switch (ev.type & 0x0f) {
713 if (ev.buttons & GPM_B_LEFT)
714 eventp->bstate |= BUTTON1_PRESSED;
715 if (ev.buttons & GPM_B_MIDDLE)
716 eventp->bstate |= BUTTON2_PRESSED;
717 if (ev.buttons & GPM_B_RIGHT)
718 eventp->bstate |= BUTTON3_PRESSED;
721 if (ev.buttons & GPM_B_LEFT)
722 eventp->bstate |= BUTTON1_RELEASED;
723 if (ev.buttons & GPM_B_MIDDLE)
724 eventp->bstate |= BUTTON2_RELEASED;
725 if (ev.buttons & GPM_B_RIGHT)
726 eventp->bstate |= BUTTON3_RELEASED;
732 eventp->x = ev.x - 1;
733 eventp->y = ev.y - 1;
736 /* bump the next-free pointer into the circular list */
737 sp->_mouse_eventp = eventp = NEXT(eventp);
747 if (sp->_sysmouse_head < sp->_sysmouse_tail) {
748 *eventp = sp->_sysmouse_fifo[sp->_sysmouse_head];
751 * Point the fifo-head to the next possible location. If there
752 * are none, reset the indices. This may be interrupted by the
753 * signal handler, doing essentially the same reset.
755 sp->_sysmouse_head += 1;
756 if (sp->_sysmouse_head == sp->_sysmouse_tail) {
757 sp->_sysmouse_tail = 0;
758 sp->_sysmouse_head = 0;
761 /* bump the next-free pointer into the circular list */
762 sp->_mouse_eventp = eventp = NEXT(eventp);
766 #endif /* USE_SYSMOUSE */
772 return result; /* true if we found an event */
776 _nc_mouse_inline(SCREEN *sp)
777 /* mouse report received in the keyboard stream -- parse its info */
781 MEVENT *eventp = sp->_mouse_eventp;
783 TR(MY_TRACE, ("_nc_mouse_inline() called"));
785 if (sp->_mouse_type == M_XTERM) {
786 unsigned char kbuf[4];
791 /* This code requires that your xterm entry contain the kmous
792 * capability and that it be set to the \E[M documented in the
793 * Xterm Control Sequences reference. This is how we
794 * arrange for mouse events to be reported via a KEY_MOUSE
795 * return value from wgetch(). After this value is received,
796 * _nc_mouse_inline() gets called and is immediately
797 * responsible for parsing the mouse status information
798 * following the prefix.
800 * The following quotes from the ctrlseqs.ms document in the
801 * X distribution, describing the X mouse tracking feature:
803 * Parameters for all mouse tracking escape sequences
804 * generated by xterm encode numeric parameters in a single
805 * character as value+040. For example, ! is 1.
807 * On button press or release, xterm sends ESC [ M CbCxCy.
808 * The low two bits of Cb encode button information: 0=MB1
809 * pressed, 1=MB2 pressed, 2=MB3 pressed, 3=release. The
810 * upper bits encode what modifiers were down when the
811 * button was pressed and are added together. 4=Shift,
812 * 8=Meta, 16=Control. Cx and Cy are the x and y coordinates
813 * of the mouse event. The upper left corner is (1,1).
815 * (End quote) By the time we get here, we've eaten the
816 * key prefix. FYI, the loop below is necessary because
817 * mouse click info isn't guaranteed to present as a
820 * Wheel mice may return buttons 4 and 5 when the wheel is turned.
821 * We encode those as button presses.
823 for (grabbed = 0; grabbed < 3; grabbed += (size_t) res) {
825 /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
827 res = read(M_FD(sp) >= 0 ? M_FD(sp) : sp->_ifd, &kbuf, 3);
829 res = read(sp->_ifd, kbuf + grabbed, 3 - grabbed);
837 ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
839 /* there's only one mouse... */
840 eventp->id = NORMAL_EVENT;
842 /* processing code goes here */
844 prev = PREV(eventp)->bstate;
847 #define PRESS_POSITION(n) \
848 eventp->bstate = MASK_PRESS(n); \
849 if (kbuf[0] & 0x40) \
850 eventp->bstate = MASK_RELEASE(n)
852 #define PRESS_POSITION(n) \
853 eventp->bstate = (mmask_t) (prev & MASK_PRESS(n) \
854 ? REPORT_MOUSE_POSITION \
858 switch (kbuf[0] & 0x3) {
861 eventp->bstate = MASK_PRESS(4);
867 #if NCURSES_MOUSE_VERSION == 2
869 eventp->bstate = MASK_PRESS(5);
881 * Release events aren't reported for individual buttons, just for
882 * the button set as a whole. However, because there are normally
883 * no mouse events under xterm that intervene between press and
884 * release, we can infer the button actually released by looking at
885 * the previous event.
887 if (prev & (BUTTON_PRESSED | BUTTON_RELEASED)) {
888 eventp->bstate = BUTTON_RELEASED;
889 for (b = 1; b <= MAX_BUTTONS; ++b) {
890 if (!(prev & MASK_PRESS(b)))
891 eventp->bstate &= ~MASK_RELEASE(b);
895 * XFree86 xterm will return a stream of release-events to
896 * let the application know where the mouse is going, if the
897 * private mode 1002 or 1003 is enabled.
899 eventp->bstate = REPORT_MOUSE_POSITION;
903 result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
906 eventp->bstate |= BUTTON_SHIFT;
909 eventp->bstate |= BUTTON_ALT;
912 eventp->bstate |= BUTTON_CTRL;
915 eventp->x = (kbuf[1] - ' ') - 1;
916 eventp->y = (kbuf[2] - ' ') - 1;
918 ("_nc_mouse_inline: primitive mouse-event %s has slot %ld",
919 _nc_tracemouse(sp, eventp),
920 (long) IndexEV(sp, eventp)));
922 /* bump the next-free pointer into the circular list */
923 sp->_mouse_eventp = NEXT(eventp);
924 #if 0 /* this return would be needed for QNX's mods to lib_getch.c */
933 mouse_activate(SCREEN *sp, bool on)
935 if (!on && !sp->_mouse_initialized)
938 if (!_nc_mouse_init(sp))
943 switch (sp->_mouse_type) {
945 #if NCURSES_EXT_FUNCS
946 keyok(KEY_MOUSE, on);
948 TPUTS_TRACE("xterm mouse initialization");
949 enable_xterm_mouse(sp, 1);
953 if (enable_gpm_mouse(sp, TRUE)) {
954 sp->_mouse_fd = *(my_gpm_fd);
955 T(("GPM mouse_fd %d", sp->_mouse_fd));
961 signal(SIGUSR2, handle_sysmouse);
962 sp->_mouse_active = TRUE;
968 /* Make runtime binding to cut down on object size of applications that
969 * do not use the mouse (e.g., 'clear').
971 sp->_mouse_event = _nc_mouse_event;
972 sp->_mouse_inline = _nc_mouse_inline;
973 sp->_mouse_parse = _nc_mouse_parse;
974 sp->_mouse_resume = _nc_mouse_resume;
975 sp->_mouse_wrap = _nc_mouse_wrap;
978 switch (sp->_mouse_type) {
980 TPUTS_TRACE("xterm mouse deinitialization");
981 enable_xterm_mouse(sp, 0);
985 enable_gpm_mouse(sp, FALSE);
990 signal(SIGUSR2, SIG_IGN);
991 sp->_mouse_active = FALSE;
1001 /**************************************************************************
1003 * Device-independent code
1005 **************************************************************************/
1008 _nc_mouse_parse(SCREEN *sp, int runcount)
1009 /* parse a run of atomic mouse events into a gesture */
1011 MEVENT *eventp = sp->_mouse_eventp;
1012 MEVENT *ep, *runp, *next, *prev = PREV(eventp);
1017 TR(MY_TRACE, ("_nc_mouse_parse(%d) called", runcount));
1020 * When we enter this routine, the event list next-free pointer
1021 * points just past a run of mouse events that we know were separated
1022 * in time by less than the critical click interval. The job of this
1023 * routine is to collapse this run into a single higher-level event
1026 * We accomplish this in two passes. The first pass merges press/release
1027 * pairs into click events. The second merges runs of click events into
1028 * double or triple-click events.
1030 * It's possible that the run may not resolve to a single event (for
1031 * example, if the user quadruple-clicks). If so, leading events
1032 * in the run are ignored.
1034 * Note that this routine is independent of the format of the specific
1035 * format of the pointing-device's reports. We can use it to parse
1036 * gestures on anything that reports press/release events on a per-
1037 * button basis, as long as the device-dependent mouse code puts stuff
1038 * on the queue in MEVENT format.
1040 if (runcount == 1) {
1042 ("_nc_mouse_parse: returning simple mouse event %s at slot %ld",
1043 _nc_tracemouse(sp, prev),
1044 (long) IndexEV(sp, prev)));
1045 return (prev->id >= NORMAL_EVENT)
1046 ? ((prev->bstate & sp->_mouse_mask) ? TRUE : FALSE)
1050 /* find the start of the run */
1052 for (n = runcount; n > 0; n--) {
1057 if (USE_TRACEF(TRACE_IEVENT)) {
1058 _trace_slot(sp, "before mouse press/release merge:");
1059 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1060 RunParams(sp, eventp, runp),
1062 _nc_unlock_global(tracef);
1066 /* first pass; merge press/release pairs */
1069 for (ep = runp; (next = NEXT(ep)) != eventp; ep = next) {
1071 #define MASK_CHANGED(x) (!(ep->bstate & MASK_PRESS(x)) \
1072 == !(next->bstate & MASK_RELEASE(x)))
1074 if (ep->x == next->x && ep->y == next->y
1075 && (ep->bstate & BUTTON_PRESSED)
1080 #if NCURSES_MOUSE_VERSION == 2
1084 for (b = 1; b <= MAX_BUTTONS; ++b) {
1085 if ((sp->_mouse_mask & MASK_CLICK(b))
1086 && (ep->bstate & MASK_PRESS(b))) {
1087 ep->bstate &= ~MASK_PRESS(b);
1088 ep->bstate |= MASK_CLICK(b);
1093 next->id = INVALID_EVENT;
1100 if (USE_TRACEF(TRACE_IEVENT)) {
1101 _trace_slot(sp, "before mouse click merge:");
1102 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1103 RunParams(sp, eventp, runp),
1105 _nc_unlock_global(tracef);
1110 * Second pass; merge click runs. At this point, click events are
1111 * each followed by one invalid event. We merge click events
1112 * forward in the queue.
1114 * NOTE: There is a problem with this design! If the application
1115 * allows enough click events to pile up in the circular queue so
1116 * they wrap around, it will cheerfully merge the newest forward
1117 * into the oldest, creating a bogus doubleclick and confusing
1118 * the queue-traversal logic rather badly. Generally this won't
1119 * happen, because calling getmouse() marks old events invalid and
1120 * ineligible for merges. The true solution to this problem would
1121 * be to timestamp each MEVENT and perform the obvious sanity check,
1122 * but the timer element would have to have sub-second resolution,
1123 * which would get us into portability trouble.
1129 for (ep = runp; (next = NEXT(ep)) != eventp; ep = next)
1130 if (ep->id != INVALID_EVENT) {
1131 if (next->id != INVALID_EVENT)
1133 follower = NEXT(next);
1134 if (follower->id == INVALID_EVENT)
1137 /* merge click events forward */
1138 if ((ep->bstate & BUTTON_CLICKED)
1139 && (follower->bstate & BUTTON_CLICKED)) {
1140 for (b = 1; b <= MAX_BUTTONS; ++b) {
1141 if ((sp->_mouse_mask & MASK_DOUBLE_CLICK(b))
1142 && (follower->bstate & MASK_CLICK(b))) {
1143 follower->bstate &= ~MASK_CLICK(b);
1144 follower->bstate |= MASK_DOUBLE_CLICK(b);
1149 ep->id = INVALID_EVENT;
1152 /* merge double-click events forward */
1153 if ((ep->bstate & BUTTON_DOUBLE_CLICKED)
1154 && (follower->bstate & BUTTON_CLICKED)) {
1155 for (b = 1; b <= MAX_BUTTONS; ++b) {
1156 if ((sp->_mouse_mask & MASK_TRIPLE_CLICK(b))
1157 && (follower->bstate & MASK_CLICK(b))) {
1158 follower->bstate &= ~MASK_CLICK(b);
1159 follower->bstate |= MASK_TRIPLE_CLICK(b);
1164 ep->id = INVALID_EVENT;
1171 if (USE_TRACEF(TRACE_IEVENT)) {
1172 _trace_slot(sp, "before mouse event queue compaction:");
1173 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1174 RunParams(sp, eventp, runp),
1176 _nc_unlock_global(tracef);
1181 * Now try to throw away trailing events flagged invalid, or that
1182 * don't match the current event mask.
1184 for (; runcount; prev = PREV(eventp), runcount--)
1185 if (prev->id == INVALID_EVENT || !(prev->bstate & sp->_mouse_mask)) {
1186 sp->_mouse_eventp = eventp = prev;
1189 if (USE_TRACEF(TRACE_IEVENT)) {
1190 _trace_slot(sp, "after mouse event queue compaction:");
1191 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1192 RunParams(sp, eventp, runp),
1194 _nc_unlock_global(tracef);
1196 for (ep = runp; ep != eventp; ep = NEXT(ep))
1197 if (ep->id != INVALID_EVENT)
1199 ("_nc_mouse_parse: returning composite mouse event %s at slot %ld",
1200 _nc_tracemouse(sp, ep),
1201 (long) IndexEV(sp, ep)));
1204 /* after all this, do we have a valid event? */
1205 return (PREV(eventp)->id != INVALID_EVENT);
1209 _nc_mouse_wrap(SCREEN *sp)
1210 /* release mouse -- called by endwin() before shellout/exit */
1212 TR(MY_TRACE, ("_nc_mouse_wrap() called"));
1214 switch (sp->_mouse_type) {
1216 if (sp->_mouse_mask)
1217 mouse_activate(sp, FALSE);
1220 /* GPM: pass all mouse events to next client */
1222 if (sp->_mouse_mask)
1223 mouse_activate(sp, FALSE);
1228 mouse_activate(sp, FALSE);
1237 _nc_mouse_resume(SCREEN *sp)
1238 /* re-connect to mouse -- called by doupdate() after shellout */
1240 TR(MY_TRACE, ("_nc_mouse_resume() called"));
1242 switch (sp->_mouse_type) {
1244 /* xterm: re-enable reporting */
1245 if (sp->_mouse_mask)
1246 mouse_activate(sp, TRUE);
1251 /* GPM: reclaim our event set */
1252 if (sp->_mouse_mask)
1253 mouse_activate(sp, TRUE);
1259 mouse_activate(sp, TRUE);
1267 /**************************************************************************
1269 * Mouse interface entry points for the API
1271 **************************************************************************/
1274 _nc_getmouse(SCREEN *sp, MEVENT * aevent)
1276 T((T_CALLED("getmouse(%p)"), aevent));
1278 if ((aevent != 0) && (sp != 0) && (sp->_mouse_type != M_NONE)) {
1279 MEVENT *eventp = sp->_mouse_eventp;
1280 /* compute the current-event pointer */
1281 MEVENT *prev = PREV(eventp);
1283 /* copy the event we find there */
1286 TR(TRACE_IEVENT, ("getmouse: returning event %s from slot %ld",
1287 _nc_tracemouse(sp, prev),
1288 (long) IndexEV(sp, prev)));
1290 prev->id = INVALID_EVENT; /* so the queue slot becomes free */
1296 /* grab a copy of the current mouse event */
1298 getmouse(MEVENT * aevent)
1300 return _nc_getmouse(SP, aevent);
1304 _nc_ungetmouse(SCREEN *sp, MEVENT * aevent)
1308 T((T_CALLED("ungetmouse(%p)"), aevent));
1310 if (aevent != 0 && sp != 0) {
1311 MEVENT *eventp = sp->_mouse_eventp;
1313 /* stick the given event in the next-free slot */
1316 /* bump the next-free pointer into the circular list */
1317 sp->_mouse_eventp = NEXT(eventp);
1319 /* push back the notification event on the keyboard queue */
1320 result = _nc_ungetch(sp, KEY_MOUSE);
1325 /* enqueue a synthesized mouse event to be seen by the next wgetch() */
1327 ungetmouse(MEVENT * aevent)
1329 return _nc_ungetmouse(SP, aevent);
1332 NCURSES_EXPORT(mmask_t)
1333 mousemask(mmask_t newmask, mmask_t * oldmask)
1334 /* set the mouse event mask */
1338 T((T_CALLED("mousemask(%#lx,%p)"), (unsigned long) newmask, oldmask));
1342 *oldmask = SP->_mouse_mask;
1344 if (newmask || SP->_mouse_initialized) {
1346 if (SP->_mouse_type != M_NONE) {
1348 (REPORT_MOUSE_POSITION
1355 | BUTTON_DOUBLE_CLICKED
1356 | BUTTON_TRIPLE_CLICKED);
1358 mouse_activate(SP, (bool) (result != 0));
1360 SP->_mouse_mask = result;
1367 NCURSES_EXPORT(bool)
1368 wenclose(const WINDOW *win, int y, int x)
1369 /* check to see if given window encloses given screen location */
1371 bool result = FALSE;
1373 T((T_CALLED("wenclose(%p,%d,%d)"), win, y, x));
1377 result = ((win->_begy <= y &&
1379 (win->_begx + win->_maxx) >= x &&
1380 (win->_begy + win->_maxy) >= y) ? TRUE : FALSE);
1386 mouseinterval(int maxclick)
1387 /* set the maximum mouse interval within which to recognize a click */
1391 T((T_CALLED("mouseinterval(%d)"), maxclick));
1394 oldval = SP->_maxclick;
1396 SP->_maxclick = maxclick;
1398 oldval = DEFAULT_MAXCLICK;
1404 /* This may be used by other routines to ask for the existence of mouse
1406 NCURSES_EXPORT(bool)
1407 _nc_has_mouse(SCREEN *sp)
1409 return ((sp->_mouse_type == M_NONE) ? FALSE : TRUE);
1412 NCURSES_EXPORT(bool)
1415 return _nc_has_mouse(SP);
1418 NCURSES_EXPORT(bool)
1419 wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
1421 bool result = FALSE;
1423 T((T_CALLED("wmouse_trafo(%p,%p,%p,%d)"), win, pY, pX, to_screen));
1425 if (win && pY && pX) {
1430 y += win->_begy + win->_yoffset;
1432 if (wenclose(win, y, x))
1435 if (wenclose(win, y, x)) {
1436 y -= (win->_begy + win->_yoffset);