1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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 * and: Juergen Pfeifer 2008 *
34 ****************************************************************************/
37 * This module is intended to encapsulate ncurses's interface to pointing
40 * The primary method used is xterm's internal mouse-tracking facility.
41 * Additional methods depend on the platform:
42 * Alessandro Rubini's GPM server (Linux)
44 * special-purpose mouse interface for OS/2 EMX.
46 * Notes for implementors of new mouse-interface methods:
48 * The code is logically split into a lower level that accepts event reports
49 * in a device-dependent format and an upper level that parses mouse gestures
50 * and filters events. The mediating data structure is a circular queue of
53 * Functionally, the lower level's job is to pick up primitive events and
54 * put them on the circular queue. This can happen in one of two ways:
55 * either (a) _nc_mouse_event() detects a series of incoming mouse reports
56 * and queues them, or (b) code in lib_getch.c detects the kmous prefix in
57 * the keyboard input stream and calls _nc_mouse_inline to queue up a series
58 * of adjacent mouse reports.
60 * In either case, _nc_mouse_parse() should be called after the series is
61 * accepted to parse the digested mouse reports (low-level MEVENTs) into
62 * a gesture (a high-level or composite MEVENT).
64 * Don't be too shy about adding new event types or modifiers, if you can find
65 * room for them in the 32-bit mask. The API is written so that users get
66 * feedback on which theoretical event types they won't see when they call
67 * mousemask. There's one bit per button (the RESERVED_EVENT bit) not being
68 * used yet, and a couple of bits open at the high end.
77 # define INCL_DOSPROCESS
78 # include <os2.h> /* Need to include before the others */
81 #include <curses.priv.h>
84 #define CUR SP_TERMTYPE
87 MODULE_ID("$Id: lib_mouse.c,v 1.129 2011/09/17 21:52:30 tom Exp $")
92 #include <linux/keyboard.h> /* defines KG_* macros */
95 /* use dynamic loader to avoid linkage dependency */
99 #define my_RTLD RTLD_NOW
102 #define my_RTLD RTLD_LAZY
106 #endif /* RTLD_NOW */
107 #endif /* HAVE_LIBDL */
109 #endif /* USE_GPM_SUPPORT */
112 #undef buttons /* symbol conflict in consio.h */
113 #undef mouse_info /* symbol conflict in consio.h */
114 #include <osreldate.h>
115 #if (__FreeBSD_version >= 400017)
116 #include <sys/consio.h>
117 #include <sys/fbio.h>
119 #include <machine/console.h>
121 #endif /* use_SYSMOUSE */
123 #define MY_TRACE TRACE_ICALLS|TRACE_IEVENT
125 #define MASK_RELEASE(x) (mmask_t) NCURSES_MOUSE_MASK(x, 001)
126 #define MASK_PRESS(x) (mmask_t) NCURSES_MOUSE_MASK(x, 002)
127 #define MASK_CLICK(x) (mmask_t) NCURSES_MOUSE_MASK(x, 004)
128 #define MASK_DOUBLE_CLICK(x) (mmask_t) NCURSES_MOUSE_MASK(x, 010)
129 #define MASK_TRIPLE_CLICK(x) (mmask_t) NCURSES_MOUSE_MASK(x, 020)
130 #define MASK_RESERVED_EVENT(x) (mmask_t) NCURSES_MOUSE_MASK(x, 040)
132 #if NCURSES_MOUSE_VERSION == 1
133 #define BUTTON_CLICKED (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED | BUTTON4_CLICKED)
134 #define BUTTON_PRESSED (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED | BUTTON4_PRESSED)
135 #define BUTTON_RELEASED (BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON3_RELEASED | BUTTON4_RELEASED)
136 #define BUTTON_DOUBLE_CLICKED (BUTTON1_DOUBLE_CLICKED | BUTTON2_DOUBLE_CLICKED | BUTTON3_DOUBLE_CLICKED | BUTTON4_DOUBLE_CLICKED)
137 #define BUTTON_TRIPLE_CLICKED (BUTTON1_TRIPLE_CLICKED | BUTTON2_TRIPLE_CLICKED | BUTTON3_TRIPLE_CLICKED | BUTTON4_TRIPLE_CLICKED)
138 #define MAX_BUTTONS 4
140 #define BUTTON_CLICKED (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED | BUTTON4_CLICKED | BUTTON5_CLICKED)
141 #define BUTTON_PRESSED (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED | BUTTON4_PRESSED | BUTTON5_PRESSED)
142 #define BUTTON_RELEASED (BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON3_RELEASED | BUTTON4_RELEASED | BUTTON5_RELEASED)
143 #define BUTTON_DOUBLE_CLICKED (BUTTON1_DOUBLE_CLICKED | BUTTON2_DOUBLE_CLICKED | BUTTON3_DOUBLE_CLICKED | BUTTON4_DOUBLE_CLICKED | BUTTON5_DOUBLE_CLICKED)
144 #define BUTTON_TRIPLE_CLICKED (BUTTON1_TRIPLE_CLICKED | BUTTON2_TRIPLE_CLICKED | BUTTON3_TRIPLE_CLICKED | BUTTON4_TRIPLE_CLICKED | BUTTON5_TRIPLE_CLICKED)
145 #define MAX_BUTTONS 5
148 #define INVALID_EVENT -1
149 #define NORMAL_EVENT 0
153 #ifndef LIBGPM_SONAME
154 #define LIBGPM_SONAME "libgpm.so"
157 #define GET_DLSYM(name) (my_##name = (TYPE_##name) dlsym(sp->_dlopen_gpm, #name))
159 #endif /* USE_GPM_SUPPORT */
161 static bool _nc_mouse_parse(SCREEN *, int);
162 static void _nc_mouse_resume(SCREEN *);
163 static void _nc_mouse_wrap(SCREEN *);
165 /* maintain a circular list of mouse events */
167 #define FirstEV(sp) ((sp)->_mouse_events)
168 #define LastEV(sp) ((sp)->_mouse_events + EV_MAX - 1)
171 #define NEXT(ep) ((ep >= LastEV(SP_PARM)) \
176 #define PREV(ep) ((ep <= FirstEV(SP_PARM)) \
180 #define IndexEV(sp, ep) (ep - FirstEV(sp))
182 #define RunParams(sp, eventp, runp) \
183 (long) IndexEV(sp, runp), \
184 (long) (IndexEV(sp, eventp) + (EV_MAX - 1)) % EV_MAX
188 _trace_slot(SCREEN *sp, const char *tag)
194 for (ep = FirstEV(sp); ep <= LastEV(sp); ep++)
195 _tracef("mouse event queue slot %ld = %s",
196 (long) IndexEV(sp, ep),
197 _nc_tracemouse(sp, ep));
206 # define M_FD(sp) sp->_mouse_fd
209 write_event(SCREEN *sp, int down, int button, int x, int y)
212 unsigned long ignore;
214 strncpy(buf, key_mouse, 3); /* should be "\033[M" */
215 buf[3] = ' ' + (button - 1) + (down ? 0 : 0x40);
216 buf[4] = ' ' + x - LEFT_COL + 1;
217 buf[5] = ' ' + y - TOP_ROW + 1;
218 DosWrite(sp->_emxmouse_wfd, buf, 6, &ignore);
222 mouse_server(unsigned long param)
224 SCREEN *sp = (SCREEN *) param;
225 unsigned short fWait = MOU_WAIT;
226 /* NOPTRRECT mourt = { 0,0,24,79 }; */
229 unsigned short mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;
235 /* open the handle for the mouse */
236 if (MouOpen(NULL, &hmou) == 0) {
237 rc = MouSetEventMask(&mask, hmou);
238 if (rc) { /* retry with 2 buttons */
239 mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN;
240 rc = MouSetEventMask(&mask, hmou);
243 if (rc == 0 && MouDrawPtr(hmou) == 0) {
245 /* sit and wait on the event queue */
246 rc = MouReadEventQue(&mouev, &fWait, hmou);
248 sprintf(err, "Error reading mouse queue, rc=%lu.\r\n", rc);
251 if (!sp->_emxmouse_activated)
255 * OS/2 numbers a 3-button mouse inconsistently from other
261 if ((mouev.fs ^ oldstate) & MOUSE_BN1_DOWN)
262 write_event(sp, mouev.fs & MOUSE_BN1_DOWN,
263 sp->_emxmouse_buttons[1], mouev.col, mouev.row);
264 if ((mouev.fs ^ oldstate) & MOUSE_BN2_DOWN)
265 write_event(sp, mouev.fs & MOUSE_BN2_DOWN,
266 sp->_emxmouse_buttons[3], mouev.col, mouev.row);
267 if ((mouev.fs ^ oldstate) & MOUSE_BN3_DOWN)
268 write_event(sp, mouev.fs & MOUSE_BN3_DOWN,
269 sp->_emxmouse_buttons[2], mouev.col, mouev.row);
275 sprintf(err, "Error setting event mask, buttons=%d, rc=%lu.\r\n",
278 DosWrite(2, err, strlen(err), &rc);
281 DosExit(EXIT_THREAD, 0L);
284 #endif /* USE_EMX_MOUSE */
288 sysmouse_server(SCREEN *sp)
290 struct mouse_info the_mouse;
293 the_mouse.operation = MOUSE_GETINFO;
295 && sp->_mouse_fd >= 0
296 && sp->_sysmouse_tail < FIFO_SIZE
297 && ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
299 if (sp->_sysmouse_head > sp->_sysmouse_tail) {
300 sp->_sysmouse_tail = 0;
301 sp->_sysmouse_head = 0;
303 work = &(sp->_sysmouse_fifo[sp->_sysmouse_tail]);
304 memset(work, 0, sizeof(*work));
305 work->id = NORMAL_EVENT; /* there's only one mouse... */
307 sp->_sysmouse_old_buttons = sp->_sysmouse_new_buttons;
308 sp->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
310 if (sp->_sysmouse_new_buttons) {
311 if (sp->_sysmouse_new_buttons & 1)
312 work->bstate |= BUTTON1_PRESSED;
313 if (sp->_sysmouse_new_buttons & 2)
314 work->bstate |= BUTTON2_PRESSED;
315 if (sp->_sysmouse_new_buttons & 4)
316 work->bstate |= BUTTON3_PRESSED;
318 if (sp->_sysmouse_old_buttons & 1)
319 work->bstate |= BUTTON1_RELEASED;
320 if (sp->_sysmouse_old_buttons & 2)
321 work->bstate |= BUTTON2_RELEASED;
322 if (sp->_sysmouse_old_buttons & 4)
323 work->bstate |= BUTTON3_RELEASED;
326 /* for cosmetic bug in syscons.c on FreeBSD 3.[34] */
327 the_mouse.operation = MOUSE_HIDE;
328 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
329 the_mouse.operation = MOUSE_SHOW;
330 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
333 * We're only interested if the button is pressed or released.
334 * FIXME: implement continuous event-tracking.
336 if (sp->_sysmouse_new_buttons != sp->_sysmouse_old_buttons) {
337 sp->_sysmouse_tail += 1;
339 work->x = the_mouse.u.data.x / sp->_sysmouse_char_width;
340 work->y = the_mouse.u.data.y / sp->_sysmouse_char_height;
345 handle_sysmouse(int sig GCC_UNUSED)
347 sysmouse_server(CURRENT_SCREEN);
349 #endif /* USE_SYSMOUSE */
351 #ifndef USE_TERM_DRIVER
352 #define xterm_kmous "\033[M"
355 init_xterm_mouse(SCREEN *sp)
357 sp->_mouse_type = M_XTERM;
358 sp->_mouse_xtermcap = tigetstr("XM");
359 if (!VALID_STRING(sp->_mouse_xtermcap))
360 sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
365 enable_xterm_mouse(SCREEN *sp, int enable)
368 sp->_emxmouse_activated = enable;
370 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
372 TPARM_1(sp->_mouse_xtermcap, enable));
374 sp->_mouse_active = enable;
379 allow_gpm_mouse(void)
383 /* GPM does printf's without checking if stdout is a terminal */
384 if (isatty(fileno(stdout))) {
385 char *list = getenv("NCURSES_GPM_TERMS");
386 char *env = getenv("TERM");
389 result = _nc_name_match(list, env, "|:");
392 /* GPM checks the beginning of the $TERM variable to decide if it
393 * should pass xterm events through. There is no real advantage in
394 * allowing GPM to do this. Recent versions relax that check, and
395 * pretend that GPM can work with any terminal having the kmous
396 * capability. Perhaps that works for someone. If so, they can
397 * set the environment variable (above).
399 if (env != 0 && strstr(env, "linux") != 0) {
409 unload_gpm_library(SCREEN *sp)
411 if (sp->_dlopen_gpm != 0) {
412 T(("unload GPM library"));
413 sp->_mouse_gpm_loaded = FALSE;
415 dlclose(sp->_dlopen_gpm);
421 load_gpm_library(SCREEN *sp)
423 sp->_mouse_gpm_found = FALSE;
424 if ((sp->_dlopen_gpm = dlopen(LIBGPM_SONAME, my_RTLD)) != 0) {
425 if (GET_DLSYM(gpm_fd) == 0 ||
426 GET_DLSYM(Gpm_Open) == 0 ||
427 GET_DLSYM(Gpm_Close) == 0 ||
428 GET_DLSYM(Gpm_GetEvent) == 0) {
429 T(("GPM initialization failed: %s", dlerror()));
430 unload_gpm_library(sp);
432 sp->_mouse_gpm_found = TRUE;
433 sp->_mouse_gpm_loaded = TRUE;
440 enable_gpm_mouse(SCREEN *sp, bool enable)
444 T((T_CALLED("enable_gpm_mouse(%d)"), enable));
446 if (enable && !sp->_mouse_active) {
448 if (sp->_mouse_gpm_found && !sp->_mouse_gpm_loaded) {
449 load_gpm_library(sp);
452 if (sp->_mouse_gpm_loaded) {
455 /* GPM: initialize connection to gpm server */
456 sp->_mouse_gpm_connect.eventMask = GPM_DOWN | GPM_UP;
457 sp->_mouse_gpm_connect.defaultMask =
458 (unsigned short) (~(sp->_mouse_gpm_connect.eventMask | GPM_HARD));
459 sp->_mouse_gpm_connect.minMod = 0;
460 sp->_mouse_gpm_connect.maxMod =
461 (unsigned short) (~((1 << KG_SHIFT) |
465 * Note: GPM hardcodes \E[?1001s and \E[?1000h during its open.
466 * The former is recognized by wscons (SunOS), and the latter by
467 * xterm. Those will not show up in ncurses' traces.
469 code = my_Gpm_Open(&sp->_mouse_gpm_connect, 0);
470 result = (code >= 0);
473 * GPM can return a -2 if it is trying to do something with xterm.
474 * Ignore that, since it conflicts with our use of stdin.
482 sp->_mouse_active = result;
483 T(("GPM open %s", result ? "succeeded" : "failed"));
485 if (!enable && sp->_mouse_active) {
486 /* GPM: close connection to gpm server */
488 sp->_mouse_active = FALSE;
495 unload_gpm_library(sp);
500 #endif /* USE_GPM_SUPPORT */
503 initialize_mousetype(SCREEN *sp)
505 T((T_CALLED("initialize_mousetype()")));
507 /* Try gpm first, because gpm may be configured to run in xterm */
509 if (allow_gpm_mouse()) {
510 if (!sp->_mouse_gpm_loaded) {
512 load_gpm_library(sp);
513 #else /* !HAVE_LIBDL */
514 sp->_mouse_gpm_found = TRUE;
515 sp->_mouse_gpm_loaded = TRUE;
520 * The gpm_fd file-descriptor may be negative (xterm). So we have to
521 * maintain our notion of whether the mouse connection is active
522 * without testing the file-descriptor.
524 if (sp->_mouse_gpm_found && enable_gpm_mouse(sp, TRUE)) {
525 sp->_mouse_type = M_GPM;
526 sp->_mouse_fd = *(my_gpm_fd);
527 T(("GPM mouse_fd %d", sp->_mouse_fd));
531 #endif /* USE_GPM_SUPPORT */
535 if (!sp->_emxmouse_thread
536 && strstr(TerminalOf(sp)->type.term_names, "xterm") == 0
540 if (pipe(handles) < 0) {
541 perror("mouse pipe error");
546 if (!sp->_emxmouse_buttons[0]) {
547 char *s = getenv("MOUSE_BUTTONS_123");
549 sp->_emxmouse_buttons[0] = 1;
550 if (s && strlen(s) >= 3) {
551 sp->_emxmouse_buttons[1] = s[0] - '0';
552 sp->_emxmouse_buttons[2] = s[1] - '0';
553 sp->_emxmouse_buttons[3] = s[2] - '0';
555 sp->_emxmouse_buttons[1] = 1;
556 sp->_emxmouse_buttons[2] = 3;
557 sp->_emxmouse_buttons[3] = 2;
560 sp->_emxmouse_wfd = handles[1];
561 M_FD(sp) = handles[0];
563 setmode(handles[0], O_BINARY);
564 setmode(handles[1], O_BINARY);
565 /* Do not use CRT functions, we may single-threaded. */
566 rc = DosCreateThread((unsigned long *) &sp->_emxmouse_thread,
567 mouse_server, (long) sp, 0, 8192);
569 printf("mouse thread error %d=%#x", rc, rc);
571 sp->_mouse_type = M_XTERM;
576 #endif /* USE_EMX_MOUSE */
580 struct mouse_info the_mouse;
581 char *the_device = 0;
583 if (isatty(sp->_ifd))
584 the_device = ttyname(sp->_ifd);
586 the_device = "/dev/tty";
588 sp->_mouse_fd = open(the_device, O_RDWR);
590 if (sp->_mouse_fd >= 0) {
592 * sysmouse does not have a usable user interface for obtaining
593 * mouse events. The logical way to proceed (reading data on a
594 * stream) only works if one opens the device as root. Even in
595 * that mode, careful examination shows we lose events
596 * occasionally. The interface provided for user programs is to
597 * establish a signal handler. really.
599 * Take over SIGUSR2 for this purpose since SIGUSR1 is more
600 * likely to be used by an application. getch() will have to
601 * handle the misleading EINTR's.
603 signal(SIGUSR2, SIG_IGN);
604 the_mouse.operation = MOUSE_MODE;
605 the_mouse.u.mode.mode = 0;
606 the_mouse.u.mode.signal = SIGUSR2;
607 if (ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
608 signal(SIGUSR2, handle_sysmouse);
609 the_mouse.operation = MOUSE_SHOW;
610 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
612 #if defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) /* FreeBSD > 2.x */
614 #ifndef FBIO_GETMODE /* FreeBSD 3.x */
615 #define FBIO_GETMODE CONS_GET
616 #define FBIO_MODEINFO CONS_MODEINFO
617 #endif /* FBIO_GETMODE */
618 video_info_t the_video;
620 if (ioctl(sp->_mouse_fd,
622 &the_video.vi_mode) != -1
623 && ioctl(sp->_mouse_fd,
626 sp->_sysmouse_char_width = the_video.vi_cwidth;
627 sp->_sysmouse_char_height = the_video.vi_cheight;
630 #endif /* defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) */
632 if (sp->_sysmouse_char_width <= 0)
633 sp->_sysmouse_char_width = 8;
634 if (sp->_sysmouse_char_height <= 0)
635 sp->_sysmouse_char_height = 16;
636 sp->_mouse_type = M_SYSMOUSE;
641 #endif /* USE_SYSMOUSE */
643 #ifdef USE_TERM_DRIVER
644 CallDriver(sp, initmouse);
646 /* we know how to recognize mouse events under "xterm" */
647 if (key_mouse != 0) {
648 if (!strcmp(key_mouse, xterm_kmous)
649 || strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
650 init_xterm_mouse(sp);
652 } else if (strstr(TerminalOf(sp)->type.term_names, "xterm") != 0) {
653 if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
654 init_xterm_mouse(sp);
662 _nc_mouse_init(SCREEN *sp)
663 /* initialize the mouse */
669 if (!sp->_mouse_initialized) {
670 sp->_mouse_initialized = TRUE;
672 TR(MY_TRACE, ("_nc_mouse_init() called"));
674 sp->_mouse_eventp = FirstEV(sp);
675 for (i = 0; i < EV_MAX; i++)
676 sp->_mouse_events[i].id = INVALID_EVENT;
678 initialize_mousetype(sp);
680 T(("_nc_mouse_init() set mousetype to %d", sp->_mouse_type));
682 result = sp->_mouse_initialized;
688 * Query to see if there is a pending mouse event. This is called from
689 * fifo_push() in lib_getch.c
692 _nc_mouse_event(SCREEN *sp)
694 MEVENT *eventp = sp->_mouse_eventp;
699 switch (sp->_mouse_type) {
701 /* xterm: never have to query, mouse events are in the keyboard stream */
706 int i, res = read(M_FD(sp), &kbuf, 3); /* Eat the prefix */
708 printf("Got %d chars instead of 3 for prefix.\n", res);
709 for (i = 0; i < res; i++) {
710 if (kbuf[i] != key_mouse[i])
711 printf("Got char %d instead of %d for prefix.\n",
712 (int) kbuf[i], (int) key_mouse[i]);
716 #endif /* USE_EMX_MOUSE */
721 if (sp->_mouse_fd >= 0) {
722 /* query server for event, return TRUE if we find one */
725 switch (my_Gpm_GetEvent(&ev)) {
727 /* Connection closed, drop the mouse. */
731 /* there's only one mouse... */
732 eventp->id = NORMAL_EVENT;
735 switch (ev.type & 0x0f) {
737 if (ev.buttons & GPM_B_LEFT)
738 eventp->bstate |= BUTTON1_PRESSED;
739 if (ev.buttons & GPM_B_MIDDLE)
740 eventp->bstate |= BUTTON2_PRESSED;
741 if (ev.buttons & GPM_B_RIGHT)
742 eventp->bstate |= BUTTON3_PRESSED;
745 if (ev.buttons & GPM_B_LEFT)
746 eventp->bstate |= BUTTON1_RELEASED;
747 if (ev.buttons & GPM_B_MIDDLE)
748 eventp->bstate |= BUTTON2_RELEASED;
749 if (ev.buttons & GPM_B_RIGHT)
750 eventp->bstate |= BUTTON3_RELEASED;
753 eventp->bstate |= REPORT_MOUSE_POSITION;
757 eventp->x = ev.x - 1;
758 eventp->y = ev.y - 1;
761 /* bump the next-free pointer into the circular list */
762 sp->_mouse_eventp = NEXT(eventp);
772 if (sp->_sysmouse_head < sp->_sysmouse_tail) {
773 *eventp = sp->_sysmouse_fifo[sp->_sysmouse_head];
776 * Point the fifo-head to the next possible location. If there
777 * are none, reset the indices. This may be interrupted by the
778 * signal handler, doing essentially the same reset.
780 sp->_sysmouse_head += 1;
781 if (sp->_sysmouse_head == sp->_sysmouse_tail) {
782 sp->_sysmouse_tail = 0;
783 sp->_sysmouse_head = 0;
786 /* bump the next-free pointer into the circular list */
787 sp->_mouse_eventp = eventp = NEXT(eventp);
791 #endif /* USE_SYSMOUSE */
793 #ifdef USE_TERM_DRIVER
795 while (sp->_drv_mouse_head < sp->_drv_mouse_tail) {
796 *eventp = sp->_drv_mouse_fifo[sp->_drv_mouse_head];
799 * Point the fifo-head to the next possible location. If there
800 * are none, reset the indices.
802 sp->_drv_mouse_head += 1;
803 if (sp->_drv_mouse_head == sp->_drv_mouse_tail) {
804 sp->_drv_mouse_tail = 0;
805 sp->_drv_mouse_head = 0;
808 /* bump the next-free pointer into the circular list */
809 sp->_mouse_eventp = eventp = NEXT(eventp);
819 return result; /* true if we found an event */
823 _nc_mouse_inline(SCREEN *sp)
824 /* mouse report received in the keyboard stream -- parse its info */
828 MEVENT *eventp = sp->_mouse_eventp;
830 TR(MY_TRACE, ("_nc_mouse_inline() called"));
832 if (sp->_mouse_type == M_XTERM) {
833 unsigned char kbuf[4];
837 /* This code requires that your xterm entry contain the kmous
838 * capability and that it be set to the \E[M documented in the
839 * Xterm Control Sequences reference. This is how we
840 * arrange for mouse events to be reported via a KEY_MOUSE
841 * return value from wgetch(). After this value is received,
842 * _nc_mouse_inline() gets called and is immediately
843 * responsible for parsing the mouse status information
844 * following the prefix.
846 * The following quotes from the ctrlseqs.ms document in the
847 * X distribution, describing the X mouse tracking feature:
849 * Parameters for all mouse tracking escape sequences
850 * generated by xterm encode numeric parameters in a single
851 * character as value+040. For example, ! is 1.
853 * On button press or release, xterm sends ESC [ M CbCxCy.
854 * The low two bits of Cb encode button information: 0=MB1
855 * pressed, 1=MB2 pressed, 2=MB3 pressed, 3=release. The
856 * upper bits encode what modifiers were down when the
857 * button was pressed and are added together. 4=Shift,
858 * 8=Meta, 16=Control. Cx and Cy are the x and y coordinates
859 * of the mouse event. The upper left corner is (1,1).
861 * (End quote) By the time we get here, we've eaten the
862 * key prefix. FYI, the loop below is necessary because
863 * mouse click info isn't guaranteed to present as a
866 * Wheel mice may return buttons 4 and 5 when the wheel is turned.
867 * We encode those as button presses.
869 # if USE_PTHREADS_EINTR
870 # if USE_WEAK_SYMBOLS
871 if ((pthread_self) && (pthread_kill) && (pthread_equal))
873 _nc_globals.read_thread = pthread_self();
875 for (grabbed = 0; grabbed < 3; grabbed += (size_t) res) {
877 /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
879 res = (int) read(M_FD(sp) >= 0 ? M_FD(sp) : sp->_ifd, &kbuf, 3);
881 res = (int) read(sp->_ifd, kbuf + grabbed, 3 - grabbed);
886 #if USE_PTHREADS_EINTR
887 _nc_globals.read_thread = 0;
892 ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
894 /* there's only one mouse... */
895 eventp->id = NORMAL_EVENT;
897 /* processing code goes here */
901 #define PRESS_POSITION(n) \
903 eventp->bstate = MASK_PRESS(n); \
904 sp->_mouse_bstate |= MASK_PRESS(n); \
905 if (kbuf[0] & 0x40) { \
906 eventp->bstate = MASK_RELEASE(n); \
907 sp->_mouse_bstate &= ~MASK_PRESS(n); \
911 #define PRESS_POSITION(n) \
913 eventp->bstate = (mmask_t) (sp->_mouse_bstate & MASK_PRESS(n) \
914 ? REPORT_MOUSE_POSITION \
916 sp->_mouse_bstate |= MASK_PRESS(n); \
920 switch (kbuf[0] & 0x3) {
922 if ((kbuf[0] & 96) == 96) {
923 eventp->bstate = MASK_PRESS(4);
924 /* Do not record in sp->_mouse_bstate; there will be no
925 * corresponding release event.
933 if ((kbuf[0] & 96) == 96) {
934 #if NCURSES_MOUSE_VERSION == 2
935 eventp->bstate = MASK_PRESS(5);
936 /* See comment above for button 4 */
938 /* Ignore this event as it is not a true press of the button */
939 eventp->bstate = REPORT_MOUSE_POSITION;
952 * Release events aren't reported for individual buttons, just for
953 * the button set as a whole. However, because there are normally
954 * no mouse events under xterm that intervene between press and
955 * release, we can infer the button actually released by looking at
956 * the previous event.
958 if (sp->_mouse_bstate & BUTTON_PRESSED) {
959 eventp->bstate = BUTTON_RELEASED;
960 for (b = 1; b <= MAX_BUTTONS; ++b) {
961 if (!(sp->_mouse_bstate & MASK_PRESS(b)))
962 eventp->bstate &= ~MASK_RELEASE(b);
964 sp->_mouse_bstate = 0;
967 * XFree86 xterm will return a stream of release-events to
968 * let the application know where the mouse is going, if the
969 * private mode 1002 or 1003 is enabled.
971 eventp->bstate = REPORT_MOUSE_POSITION;
975 result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
978 eventp->bstate |= BUTTON_SHIFT;
981 eventp->bstate |= BUTTON_ALT;
984 eventp->bstate |= BUTTON_CTRL;
987 eventp->x = (kbuf[1] - ' ') - 1;
988 eventp->y = (kbuf[2] - ' ') - 1;
990 ("_nc_mouse_inline: primitive mouse-event %s has slot %ld",
991 _nc_tracemouse(sp, eventp),
992 (long) IndexEV(sp, eventp)));
994 /* bump the next-free pointer into the circular list */
995 sp->_mouse_eventp = NEXT(eventp);
996 #if 0 /* this return would be needed for QNX's mods to lib_getch.c */
1005 mouse_activate(SCREEN *sp, bool on)
1007 if (!on && !sp->_mouse_initialized)
1010 if (!_nc_mouse_init(sp))
1014 sp->_mouse_bstate = 0;
1015 switch (sp->_mouse_type) {
1017 #if NCURSES_EXT_FUNCS
1018 NCURSES_SP_NAME(keyok) (NCURSES_SP_ARGx KEY_MOUSE, on);
1020 TPUTS_TRACE("xterm mouse initialization");
1021 enable_xterm_mouse(sp, 1);
1025 if (enable_gpm_mouse(sp, TRUE)) {
1026 sp->_mouse_fd = *(my_gpm_fd);
1027 T(("GPM mouse_fd %d", sp->_mouse_fd));
1033 signal(SIGUSR2, handle_sysmouse);
1034 sp->_mouse_active = TRUE;
1037 #ifdef USE_TERM_DRIVER
1039 sp->_mouse_active = TRUE;
1045 /* Make runtime binding to cut down on object size of applications that
1046 * do not use the mouse (e.g., 'clear').
1048 sp->_mouse_event = _nc_mouse_event;
1049 sp->_mouse_inline = _nc_mouse_inline;
1050 sp->_mouse_parse = _nc_mouse_parse;
1051 sp->_mouse_resume = _nc_mouse_resume;
1052 sp->_mouse_wrap = _nc_mouse_wrap;
1055 switch (sp->_mouse_type) {
1057 TPUTS_TRACE("xterm mouse deinitialization");
1058 enable_xterm_mouse(sp, 0);
1062 enable_gpm_mouse(sp, FALSE);
1067 signal(SIGUSR2, SIG_IGN);
1068 sp->_mouse_active = FALSE;
1071 #ifdef USE_TERM_DRIVER
1073 sp->_mouse_active = FALSE;
1080 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
1083 /**************************************************************************
1085 * Device-independent code
1087 **************************************************************************/
1090 _nc_mouse_parse(SCREEN *sp, int runcount)
1091 /* parse a run of atomic mouse events into a gesture */
1093 MEVENT *eventp = sp->_mouse_eventp;
1095 MEVENT *first_valid = NULL;
1096 MEVENT *first_invalid = NULL;
1102 TR(MY_TRACE, ("_nc_mouse_parse(%d) called", runcount));
1105 * When we enter this routine, the event list next-free pointer
1106 * points just past a run of mouse events that we know were separated
1107 * in time by less than the critical click interval. The job of this
1108 * routine is to collapse this run into a single higher-level event
1111 * We accomplish this in two passes. The first pass merges press/release
1112 * pairs into click events. The second merges runs of click events into
1113 * double or triple-click events.
1115 * It's possible that the run may not resolve to a single event (for
1116 * example, if the user quadruple-clicks). If so, leading events
1117 * in the run are ignored if user does not call getmouse in a loop (getting
1118 * them from newest to older).
1120 * Note that this routine is independent of the format of the specific
1121 * format of the pointing-device's reports. We can use it to parse
1122 * gestures on anything that reports press/release events on a per-
1123 * button basis, as long as the device-dependent mouse code puts stuff
1124 * on the queue in MEVENT format.
1128 * Reset all events that were not set, in case the user sometimes calls
1129 * getmouse only once and other times until there are no more events in
1132 * This also allows reaching the beginning of the run.
1135 for (n = runcount; n < EV_MAX; n++) {
1136 ep->id = INVALID_EVENT;
1141 if (USE_TRACEF(TRACE_IEVENT)) {
1142 _trace_slot(sp, "before mouse press/release merge:");
1143 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1144 RunParams(sp, eventp, ep),
1146 _nc_unlock_global(tracef);
1150 /* first pass; merge press/release pairs */
1154 if (next == eventp) {
1155 /* Will end the loop, but compact before */
1159 #define MASK_CHANGED(x) (!(ep->bstate & MASK_PRESS(x)) \
1160 == !(next->bstate & MASK_RELEASE(x)))
1162 if (ep->id != INVALID_EVENT && next->id != INVALID_EVENT
1163 && ep->x == next->x && ep->y == next->y
1164 && (ep->bstate & BUTTON_PRESSED)
1165 && (!(next->bstate & BUTTON_PRESSED))) {
1166 bool changed = TRUE;
1168 for (b = 1; b <= MAX_BUTTONS; ++b) {
1169 if (!MASK_CHANGED(b)) {
1177 for (b = 1; b <= MAX_BUTTONS; ++b) {
1178 if ((sp->_mouse_mask & MASK_CLICK(b))
1179 && (ep->bstate & MASK_PRESS(b))) {
1180 next->bstate &= ~MASK_RELEASE(b);
1181 next->bstate |= MASK_CLICK(b);
1186 ep->id = INVALID_EVENT;
1191 /* Compact valid events */
1192 if (ep->id == INVALID_EVENT) {
1193 if ((first_valid != NULL) && (first_invalid == NULL)) {
1197 if (first_valid == NULL) {
1199 } else if (first_invalid != NULL) {
1200 *first_invalid = *ep;
1201 ep->id = INVALID_EVENT;
1202 first_invalid = NEXT(first_invalid);
1209 if (first_invalid != NULL) {
1210 eventp = first_invalid;
1213 if (USE_TRACEF(TRACE_IEVENT)) {
1214 _trace_slot(sp, "before mouse click merge:");
1215 if (first_valid == NULL) {
1216 _tracef("_nc_mouse_parse: no valid event");
1218 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1219 RunParams(sp, eventp, first_valid),
1221 _nc_unlock_global(tracef);
1227 * Second pass; merge click runs. We merge click events forward in the
1228 * queue. For example, double click can be changed to triple click.
1230 * NOTE: There is a problem with this design! If the application
1231 * allows enough click events to pile up in the circular queue so
1232 * they wrap around, it will cheerfully merge the newest forward
1233 * into the oldest, creating a bogus doubleclick and confusing
1234 * the queue-traversal logic rather badly. Generally this won't
1235 * happen, because calling getmouse() marks old events invalid and
1236 * ineligible for merges. The true solution to this problem would
1237 * be to timestamp each MEVENT and perform the obvious sanity check,
1238 * but the timer element would have to have sub-second resolution,
1239 * which would get us into portability trouble.
1241 first_invalid = NULL;
1242 endLoop = (first_valid == NULL);
1247 if (next == eventp) {
1248 /* Will end the loop, but check event type and compact before */
1251 /* merge click events forward */
1252 if ((ep->bstate & BUTTON_CLICKED)
1253 && (next->bstate & BUTTON_CLICKED)) {
1255 for (b = 1; b <= MAX_BUTTONS; ++b) {
1256 if ((sp->_mouse_mask & MASK_DOUBLE_CLICK(b))
1257 && (ep->bstate & MASK_CLICK(b))
1258 && (next->bstate & MASK_CLICK(b))) {
1259 next->bstate &= ~MASK_CLICK(b);
1260 next->bstate |= MASK_DOUBLE_CLICK(b);
1265 ep->id = INVALID_EVENT;
1268 /* merge double-click events forward */
1269 if ((ep->bstate & BUTTON_DOUBLE_CLICKED)
1270 && (next->bstate & BUTTON_CLICKED)) {
1272 for (b = 1; b <= MAX_BUTTONS; ++b) {
1273 if ((sp->_mouse_mask & MASK_TRIPLE_CLICK(b))
1274 && (ep->bstate & MASK_DOUBLE_CLICK(b))
1275 && (next->bstate & MASK_CLICK(b))) {
1276 next->bstate &= ~MASK_CLICK(b);
1277 next->bstate |= MASK_TRIPLE_CLICK(b);
1282 ep->id = INVALID_EVENT;
1286 /* Discard event if it does not match event mask */
1287 if (!(ep->bstate & sp->_mouse_mask)) {
1288 ep->id = INVALID_EVENT;
1291 /* Compact valid events */
1292 if (ep->id == INVALID_EVENT) {
1293 if (ep == first_valid) {
1295 } else if (first_invalid == NULL) {
1298 } else if (first_invalid != NULL) {
1299 *first_invalid = *ep;
1300 ep->id = INVALID_EVENT;
1301 first_invalid = NEXT(first_invalid);
1307 if (first_invalid == NULL) {
1308 first_invalid = eventp;
1310 sp->_mouse_eventp = first_invalid;
1313 if (first_valid != NULL) {
1314 if (USE_TRACEF(TRACE_IEVENT)) {
1315 _trace_slot(sp, "after mouse event queue compaction:");
1316 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1317 RunParams(sp, first_invalid, first_valid),
1319 _nc_unlock_global(tracef);
1321 for (ep = first_valid; ep != first_invalid; ep = NEXT(ep)) {
1322 if (ep->id != INVALID_EVENT)
1324 ("_nc_mouse_parse: returning composite mouse event %s at slot %ld",
1325 _nc_tracemouse(sp, ep),
1326 (long) IndexEV(sp, ep)));
1331 /* after all this, do we have a valid event? */
1332 return (PREV(first_invalid)->id != INVALID_EVENT);
1336 _nc_mouse_wrap(SCREEN *sp)
1337 /* release mouse -- called by endwin() before shellout/exit */
1339 TR(MY_TRACE, ("_nc_mouse_wrap() called"));
1341 switch (sp->_mouse_type) {
1343 if (sp->_mouse_mask)
1344 mouse_activate(sp, FALSE);
1347 /* GPM: pass all mouse events to next client */
1349 if (sp->_mouse_mask)
1350 mouse_activate(sp, FALSE);
1355 mouse_activate(sp, FALSE);
1358 #ifdef USE_TERM_DRIVER
1360 mouse_activate(sp, FALSE);
1369 _nc_mouse_resume(SCREEN *sp)
1370 /* re-connect to mouse -- called by doupdate() after shellout */
1372 TR(MY_TRACE, ("_nc_mouse_resume() called"));
1374 switch (sp->_mouse_type) {
1376 /* xterm: re-enable reporting */
1377 if (sp->_mouse_mask)
1378 mouse_activate(sp, TRUE);
1383 /* GPM: reclaim our event set */
1384 if (sp->_mouse_mask)
1385 mouse_activate(sp, TRUE);
1391 mouse_activate(sp, TRUE);
1395 #ifdef USE_TERM_DRIVER
1397 mouse_activate(sp, TRUE);
1406 /**************************************************************************
1408 * Mouse interface entry points for the API
1410 **************************************************************************/
1413 NCURSES_SP_NAME(getmouse) (NCURSES_SP_DCLx MEVENT * aevent)
1417 T((T_CALLED("getmouse(%p,%p)"), (void *) SP_PARM, (void *) aevent));
1419 if ((aevent != 0) && (SP_PARM != 0) && (SP_PARM->_mouse_type != M_NONE)) {
1420 MEVENT *eventp = SP_PARM->_mouse_eventp;
1421 /* compute the current-event pointer */
1422 MEVENT *prev = PREV(eventp);
1425 * Discard events not matching mask (there could be still some if
1426 * _nc_mouse_parse was not called, e.g., when _nc_mouse_inline returns
1429 while ((prev->id != INVALID_EVENT) && (!(prev->bstate & SP_PARM->_mouse_mask))) {
1430 prev->id = INVALID_EVENT;
1433 if (prev->id != INVALID_EVENT) {
1434 /* copy the event we find there */
1437 TR(TRACE_IEVENT, ("getmouse: returning event %s from slot %ld",
1438 _nc_tracemouse(SP_PARM, prev),
1439 (long) IndexEV(SP_PARM, prev)));
1441 prev->id = INVALID_EVENT; /* so the queue slot becomes free */
1442 SP_PARM->_mouse_eventp = prev;
1445 /* Reset the provided event */
1447 aevent->id = INVALID_EVENT;
1456 #if NCURSES_SP_FUNCS
1457 /* grab a copy of the current mouse event */
1459 getmouse(MEVENT * aevent)
1461 return NCURSES_SP_NAME(getmouse) (CURRENT_SCREEN, aevent);
1466 NCURSES_SP_NAME(ungetmouse) (NCURSES_SP_DCLx MEVENT * aevent)
1470 T((T_CALLED("ungetmouse(%p,%p)"), (void *) SP_PARM, (void *) aevent));
1472 if (aevent != 0 && SP_PARM != 0) {
1473 MEVENT *eventp = SP_PARM->_mouse_eventp;
1475 /* stick the given event in the next-free slot */
1478 /* bump the next-free pointer into the circular list */
1479 SP_PARM->_mouse_eventp = NEXT(eventp);
1481 /* push back the notification event on the keyboard queue */
1482 result = NCURSES_SP_NAME(ungetch) (NCURSES_SP_ARGx KEY_MOUSE);
1487 #if NCURSES_SP_FUNCS
1488 /* enqueue a synthesized mouse event to be seen by the next wgetch() */
1490 ungetmouse(MEVENT * aevent)
1492 return NCURSES_SP_NAME(ungetmouse) (CURRENT_SCREEN, aevent);
1496 NCURSES_EXPORT(mmask_t)
1497 NCURSES_SP_NAME(mousemask) (NCURSES_SP_DCLx mmask_t newmask, mmask_t * oldmask)
1498 /* set the mouse event mask */
1502 T((T_CALLED("mousemask(%p,%#lx,%p)"),
1504 (unsigned long) newmask,
1509 *oldmask = SP_PARM->_mouse_mask;
1511 if (newmask || SP_PARM->_mouse_initialized) {
1512 _nc_mouse_init(SP_PARM);
1513 if (SP_PARM->_mouse_type != M_NONE) {
1515 (REPORT_MOUSE_POSITION
1522 | BUTTON_DOUBLE_CLICKED
1523 | BUTTON_TRIPLE_CLICKED);
1525 mouse_activate(SP_PARM, (bool) (result != 0));
1527 SP_PARM->_mouse_mask = result;
1531 returnMMask(result);
1534 #if NCURSES_SP_FUNCS
1535 NCURSES_EXPORT(mmask_t)
1536 mousemask(mmask_t newmask, mmask_t * oldmask)
1538 return NCURSES_SP_NAME(mousemask) (CURRENT_SCREEN, newmask, oldmask);
1542 NCURSES_EXPORT(bool)
1543 wenclose(const WINDOW *win, int y, int x)
1544 /* check to see if given window encloses given screen location */
1546 bool result = FALSE;
1548 T((T_CALLED("wenclose(%p,%d,%d)"), (const void *) win, y, x));
1552 result = ((win->_begy <= y &&
1554 (win->_begx + win->_maxx) >= x &&
1555 (win->_begy + win->_maxy) >= y) ? TRUE : FALSE);
1561 NCURSES_SP_NAME(mouseinterval) (NCURSES_SP_DCLx int maxclick)
1562 /* set the maximum mouse interval within which to recognize a click */
1566 T((T_CALLED("mouseinterval(%p,%d)"), (void *) SP_PARM, maxclick));
1569 oldval = SP_PARM->_maxclick;
1571 SP_PARM->_maxclick = maxclick;
1573 oldval = DEFAULT_MAXCLICK;
1579 #if NCURSES_SP_FUNCS
1581 mouseinterval(int maxclick)
1583 return NCURSES_SP_NAME(mouseinterval) (CURRENT_SCREEN, maxclick);
1587 /* This may be used by other routines to ask for the existence of mouse
1589 NCURSES_EXPORT(bool)
1590 _nc_has_mouse(SCREEN *sp)
1592 return (((0 == sp) || (sp->_mouse_type == M_NONE)) ? FALSE : TRUE);
1595 NCURSES_EXPORT(bool)
1596 NCURSES_SP_NAME(has_mouse) (NCURSES_SP_DCL0)
1598 return _nc_has_mouse(SP_PARM);
1601 #if NCURSES_SP_FUNCS
1602 NCURSES_EXPORT(bool)
1605 return _nc_has_mouse(CURRENT_SCREEN);
1609 NCURSES_EXPORT(bool)
1610 wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
1612 bool result = FALSE;
1614 T((T_CALLED("wmouse_trafo(%p,%p,%p,%d)"),
1620 if (win && pY && pX) {
1625 y += win->_begy + win->_yoffset;
1627 if (wenclose(win, y, x))
1630 if (wenclose(win, y, x)) {
1631 y -= (win->_begy + win->_yoffset);