1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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.105 2009/02/28 21:09:20 tom Exp $")
87 #include <linux/keyboard.h> /* defines KG_* macros */
90 /* use dynamic loader to avoid linkage dependency */
94 #define my_RTLD RTLD_NOW
97 #define my_RTLD RTLD_LAZY
101 #endif /* RTLD_NOW */
102 #endif /* HAVE_LIBDL */
104 #endif /* USE_GPM_SUPPORT */
107 #undef buttons /* symbol conflict in consio.h */
108 #undef mouse_info /* symbol conflict in consio.h */
109 #include <osreldate.h>
110 #if (__FreeBSD_version >= 400017)
111 #include <sys/consio.h>
112 #include <sys/fbio.h>
114 #include <machine/console.h>
116 #endif /* use_SYSMOUSE */
118 #define MY_TRACE TRACE_ICALLS|TRACE_IEVENT
120 #define MASK_RELEASE(x) NCURSES_MOUSE_MASK(x, 001)
121 #define MASK_PRESS(x) NCURSES_MOUSE_MASK(x, 002)
122 #define MASK_CLICK(x) NCURSES_MOUSE_MASK(x, 004)
123 #define MASK_DOUBLE_CLICK(x) NCURSES_MOUSE_MASK(x, 010)
124 #define MASK_TRIPLE_CLICK(x) NCURSES_MOUSE_MASK(x, 020)
125 #define MASK_RESERVED_EVENT(x) NCURSES_MOUSE_MASK(x, 040)
127 #if NCURSES_MOUSE_VERSION == 1
128 #define BUTTON_CLICKED (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED | BUTTON4_CLICKED)
129 #define BUTTON_PRESSED (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED | BUTTON4_PRESSED)
130 #define BUTTON_RELEASED (BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON3_RELEASED | BUTTON4_RELEASED)
131 #define BUTTON_DOUBLE_CLICKED (BUTTON1_DOUBLE_CLICKED | BUTTON2_DOUBLE_CLICKED | BUTTON3_DOUBLE_CLICKED | BUTTON4_DOUBLE_CLICKED)
132 #define BUTTON_TRIPLE_CLICKED (BUTTON1_TRIPLE_CLICKED | BUTTON2_TRIPLE_CLICKED | BUTTON3_TRIPLE_CLICKED | BUTTON4_TRIPLE_CLICKED)
133 #define MAX_BUTTONS 4
135 #define BUTTON_CLICKED (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED | BUTTON4_CLICKED | BUTTON5_CLICKED)
136 #define BUTTON_PRESSED (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED | BUTTON4_PRESSED | BUTTON5_PRESSED)
137 #define BUTTON_RELEASED (BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON3_RELEASED | BUTTON4_RELEASED | BUTTON5_RELEASED)
138 #define BUTTON_DOUBLE_CLICKED (BUTTON1_DOUBLE_CLICKED | BUTTON2_DOUBLE_CLICKED | BUTTON3_DOUBLE_CLICKED | BUTTON4_DOUBLE_CLICKED | BUTTON5_DOUBLE_CLICKED)
139 #define BUTTON_TRIPLE_CLICKED (BUTTON1_TRIPLE_CLICKED | BUTTON2_TRIPLE_CLICKED | BUTTON3_TRIPLE_CLICKED | BUTTON4_TRIPLE_CLICKED | BUTTON5_TRIPLE_CLICKED)
140 #define MAX_BUTTONS 5
143 #define INVALID_EVENT -1
144 #define NORMAL_EVENT 0
148 #ifndef LIBGPM_SONAME
149 #define LIBGPM_SONAME "libgpm.so"
152 #define GET_DLSYM(name) (my_##name = (TYPE_##name) dlsym(SP_PARM->_dlopen_gpm, #name))
154 #endif /* USE_GPM_SUPPORT */
156 static bool _nc_mouse_parse(SCREEN *, int);
157 static void _nc_mouse_resume(SCREEN *);
158 static void _nc_mouse_wrap(SCREEN *);
160 /* maintain a circular list of mouse events */
162 #define FirstEV(sp) ((sp)->_mouse_events)
163 #define LastEV(sp) ((sp)->_mouse_events + EV_MAX - 1)
166 #define NEXT(ep) ((ep >= LastEV(SP_PARM)) \
171 #define PREV(ep) ((ep <= FirstEV(SP_PARM)) \
175 #define IndexEV(sp, ep) (ep - FirstEV(sp))
177 #define RunParams(sp, eventp, runp) \
178 (long) IndexEV(sp, runp), \
179 (long) (IndexEV(sp, eventp) + (EV_MAX - 1)) % EV_MAX
183 _trace_slot(SCREEN *sp, const char *tag)
189 for (ep = FirstEV(sp); ep <= LastEV(sp); ep++)
190 _tracef("mouse event queue slot %ld = %s",
191 (long) IndexEV(sp, ep),
192 _nc_tracemouse(sp, ep));
201 # define M_FD(sp) sp->_mouse_fd
204 write_event(SCREEN *sp, int down, int button, int x, int y)
207 unsigned long ignore;
209 strncpy(buf, key_mouse, 3); /* should be "\033[M" */
210 buf[3] = ' ' + (button - 1) + (down ? 0 : 0x40);
211 buf[4] = ' ' + x - LEFT_COL + 1;
212 buf[5] = ' ' + y - TOP_ROW + 1;
213 DosWrite(sp->_emxmouse_wfd, buf, 6, &ignore);
217 mouse_server(unsigned long param)
219 SCREEN *sp = (SCREEN *) param;
220 unsigned short fWait = MOU_WAIT;
221 /* NOPTRRECT mourt = { 0,0,24,79 }; */
224 unsigned short mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;
230 /* open the handle for the mouse */
231 if (MouOpen(NULL, &hmou) == 0) {
232 rc = MouSetEventMask(&mask, hmou);
233 if (rc) { /* retry with 2 buttons */
234 mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN;
235 rc = MouSetEventMask(&mask, hmou);
238 if (rc == 0 && MouDrawPtr(hmou) == 0) {
240 /* sit and wait on the event queue */
241 rc = MouReadEventQue(&mouev, &fWait, hmou);
243 sprintf(err, "Error reading mouse queue, rc=%lu.\r\n", rc);
246 if (!sp->_emxmouse_activated)
250 * OS/2 numbers a 3-button mouse inconsistently from other
256 if ((mouev.fs ^ oldstate) & MOUSE_BN1_DOWN)
257 write_event(sp, mouev.fs & MOUSE_BN1_DOWN,
258 sp->_emxmouse_buttons[1], mouev.col, mouev.row);
259 if ((mouev.fs ^ oldstate) & MOUSE_BN2_DOWN)
260 write_event(sp, mouev.fs & MOUSE_BN2_DOWN,
261 sp->_emxmouse_buttons[3], mouev.col, mouev.row);
262 if ((mouev.fs ^ oldstate) & MOUSE_BN3_DOWN)
263 write_event(sp, mouev.fs & MOUSE_BN3_DOWN,
264 sp->_emxmouse_buttons[2], mouev.col, mouev.row);
270 sprintf(err, "Error setting event mask, buttons=%d, rc=%lu.\r\n",
273 DosWrite(2, err, strlen(err), &rc);
276 DosExit(EXIT_THREAD, 0L);
279 #endif /* USE_EMX_MOUSE */
283 sysmouse_server(SCREEN *sp)
285 struct mouse_info the_mouse;
288 the_mouse.operation = MOUSE_GETINFO;
290 && sp->_mouse_fd >= 0
291 && sp->_sysmouse_tail < FIFO_SIZE
292 && ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
294 if (sp->_sysmouse_head > sp->_sysmouse_tail) {
295 sp->_sysmouse_tail = 0;
296 sp->_sysmouse_head = 0;
298 work = &(sp->_sysmouse_fifo[sp->_sysmouse_tail]);
299 memset(work, 0, sizeof(*work));
300 work->id = NORMAL_EVENT; /* there's only one mouse... */
302 sp->_sysmouse_old_buttons = sp->_sysmouse_new_buttons;
303 sp->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
305 if (sp->_sysmouse_new_buttons) {
306 if (sp->_sysmouse_new_buttons & 1)
307 work->bstate |= BUTTON1_PRESSED;
308 if (sp->_sysmouse_new_buttons & 2)
309 work->bstate |= BUTTON2_PRESSED;
310 if (sp->_sysmouse_new_buttons & 4)
311 work->bstate |= BUTTON3_PRESSED;
313 if (sp->_sysmouse_old_buttons & 1)
314 work->bstate |= BUTTON1_RELEASED;
315 if (sp->_sysmouse_old_buttons & 2)
316 work->bstate |= BUTTON2_RELEASED;
317 if (sp->_sysmouse_old_buttons & 4)
318 work->bstate |= BUTTON3_RELEASED;
321 /* for cosmetic bug in syscons.c on FreeBSD 3.[34] */
322 the_mouse.operation = MOUSE_HIDE;
323 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
324 the_mouse.operation = MOUSE_SHOW;
325 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
328 * We're only interested if the button is pressed or released.
329 * FIXME: implement continuous event-tracking.
331 if (sp->_sysmouse_new_buttons != sp->_sysmouse_old_buttons) {
332 sp->_sysmouse_tail += 1;
334 work->x = the_mouse.u.data.x / sp->_sysmouse_char_width;
335 work->y = the_mouse.u.data.y / sp->_sysmouse_char_height;
340 handle_sysmouse(int sig GCC_UNUSED)
342 sysmouse_server(CURRENT_SCREEN);
344 #endif /* USE_SYSMOUSE */
347 init_xterm_mouse(SCREEN *sp)
349 sp->_mouse_type = M_XTERM;
350 sp->_mouse_xtermcap = tigetstr("XM");
351 if (!VALID_STRING(sp->_mouse_xtermcap))
352 sp->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
356 enable_xterm_mouse(SCREEN *sp, int enable)
359 sp->_emxmouse_activated = enable;
361 NCURSES_SP_NAME(_nc_putp) (NCURSES_SP_ARGx
363 TPARM_1(sp->_mouse_xtermcap, enable));
365 sp->_mouse_active = enable;
370 allow_gpm_mouse(void)
374 /* GPM does printf's without checking if stdout is a terminal */
375 if (isatty(fileno(stdout))) {
376 char *list = getenv("NCURSES_GPM_TERMS");
377 char *env = getenv("TERM");
380 result = _nc_name_match(list, env, "|:");
383 /* GPM checks the beginning of the $TERM variable to decide if it
384 * should pass xterm events through. There is no real advantage in
385 * allowing GPM to do this. Recent versions relax that check, and
386 * pretend that GPM can work with any terminal having the kmous
387 * capability. Perhaps that works for someone. If so, they can
388 * set the environment variable (above).
390 if (env != 0 && strstr(env, "linux") != 0) {
400 unload_gpm_library(SCREEN *sp)
402 if (sp->_dlopen_gpm != 0) {
403 T(("unload GPM library"));
404 sp->_mouse_gpm_loaded = FALSE;
406 dlclose(sp->_dlopen_gpm);
412 load_gpm_library(SCREEN *sp)
414 sp->_mouse_gpm_found = FALSE;
415 if ((sp->_dlopen_gpm = dlopen(LIBGPM_SONAME, my_RTLD)) != 0) {
416 if (GET_DLSYM(gpm_fd) == 0 ||
417 GET_DLSYM(Gpm_Open) == 0 ||
418 GET_DLSYM(Gpm_Close) == 0 ||
419 GET_DLSYM(Gpm_GetEvent) == 0) {
420 T(("GPM initialization failed: %s", dlerror()));
421 unload_gpm_library(sp);
423 sp->_mouse_gpm_found = TRUE;
424 sp->_mouse_gpm_loaded = TRUE;
431 enable_gpm_mouse(SCREEN *sp, bool enable)
435 T((T_CALLED("enable_gpm_mouse(%d)"), enable));
437 if (enable && !sp->_mouse_active) {
439 if (sp->_mouse_gpm_found && !sp->_mouse_gpm_loaded) {
440 load_gpm_library(sp);
443 if (sp->_mouse_gpm_loaded) {
444 /* GPM: initialize connection to gpm server */
445 sp->_mouse_gpm_connect.eventMask = GPM_DOWN | GPM_UP;
446 sp->_mouse_gpm_connect.defaultMask =
447 (unsigned short) (~(sp->_mouse_gpm_connect.eventMask | GPM_HARD));
448 sp->_mouse_gpm_connect.minMod = 0;
449 sp->_mouse_gpm_connect.maxMod =
450 (unsigned short) (~((1 << KG_SHIFT) |
454 * Note: GPM hardcodes \E[?1001s and \E[?1000h during its open.
455 * The former is recognized by wscons (SunOS), and the latter by
456 * xterm. Those will not show up in ncurses' traces.
458 result = (my_Gpm_Open(&sp->_mouse_gpm_connect, 0) >= 0);
462 sp->_mouse_active = result;
463 T(("GPM open %s", result ? "succeeded" : "failed"));
465 if (!enable && sp->_mouse_active) {
466 /* GPM: close connection to gpm server */
468 sp->_mouse_active = FALSE;
475 unload_gpm_library(sp);
480 #endif /* USE_GPM_SUPPORT */
482 #define xterm_kmous "\033[M"
485 initialize_mousetype(SCREEN *sp)
487 T((T_CALLED("initialize_mousetype()")));
489 /* Try gpm first, because gpm may be configured to run in xterm */
491 if (allow_gpm_mouse()) {
492 if (!sp->_mouse_gpm_loaded) {
494 load_gpm_library(sp);
495 #else /* !HAVE_LIBDL */
496 sp->_mouse_gpm_found = TRUE;
497 sp->_mouse_gpm_loaded = TRUE;
502 * The gpm_fd file-descriptor may be negative (xterm). So we have to
503 * maintain our notion of whether the mouse connection is active
504 * without testing the file-descriptor.
506 if (sp->_mouse_gpm_found && enable_gpm_mouse(sp, TRUE)) {
507 sp->_mouse_type = M_GPM;
508 sp->_mouse_fd = *(my_gpm_fd);
509 T(("GPM mouse_fd %d", sp->_mouse_fd));
513 #endif /* USE_GPM_SUPPORT */
517 if (!sp->_emxmouse_thread
518 && strstr(cur_term->type.term_names, "xterm") == 0
522 if (pipe(handles) < 0) {
523 perror("mouse pipe error");
528 if (!sp->_emxmouse_buttons[0]) {
529 char *s = getenv("MOUSE_BUTTONS_123");
531 sp->_emxmouse_buttons[0] = 1;
532 if (s && strlen(s) >= 3) {
533 sp->_emxmouse_buttons[1] = s[0] - '0';
534 sp->_emxmouse_buttons[2] = s[1] - '0';
535 sp->_emxmouse_buttons[3] = s[2] - '0';
537 sp->_emxmouse_buttons[1] = 1;
538 sp->_emxmouse_buttons[2] = 3;
539 sp->_emxmouse_buttons[3] = 2;
542 sp->_emxmouse_wfd = handles[1];
543 M_FD(sp) = handles[0];
545 setmode(handles[0], O_BINARY);
546 setmode(handles[1], O_BINARY);
547 /* Do not use CRT functions, we may single-threaded. */
548 rc = DosCreateThread((unsigned long *) &sp->_emxmouse_thread,
549 mouse_server, (long) sp, 0, 8192);
551 printf("mouse thread error %d=%#x", rc, rc);
553 sp->_mouse_type = M_XTERM;
558 #endif /* USE_EMX_MOUSE */
562 struct mouse_info the_mouse;
563 char *the_device = 0;
565 if (isatty(sp->_ifd))
566 the_device = ttyname(sp->_ifd);
568 the_device = "/dev/tty";
570 sp->_mouse_fd = open(the_device, O_RDWR);
572 if (sp->_mouse_fd >= 0) {
574 * sysmouse does not have a usable user interface for obtaining
575 * mouse events. The logical way to proceed (reading data on a
576 * stream) only works if one opens the device as root. Even in
577 * that mode, careful examination shows we lose events
578 * occasionally. The interface provided for user programs is to
579 * establish a signal handler. really.
581 * Take over SIGUSR2 for this purpose since SIGUSR1 is more
582 * likely to be used by an application. getch() will have to
583 * handle the misleading EINTR's.
585 signal(SIGUSR2, SIG_IGN);
586 the_mouse.operation = MOUSE_MODE;
587 the_mouse.u.mode.mode = 0;
588 the_mouse.u.mode.signal = SIGUSR2;
589 if (ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
590 signal(SIGUSR2, handle_sysmouse);
591 the_mouse.operation = MOUSE_SHOW;
592 ioctl(sp->_mouse_fd, CONS_MOUSECTL, &the_mouse);
594 #if defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) /* FreeBSD > 2.x */
596 #ifndef FBIO_GETMODE /* FreeBSD 3.x */
597 #define FBIO_GETMODE CONS_GET
598 #define FBIO_MODEINFO CONS_MODEINFO
599 #endif /* FBIO_GETMODE */
600 video_info_t the_video;
602 if (ioctl(sp->_mouse_fd,
604 &the_video.vi_mode) != -1
605 && ioctl(sp->_mouse_fd,
608 sp->_sysmouse_char_width = the_video.vi_cwidth;
609 sp->_sysmouse_char_height = the_video.vi_cheight;
612 #endif /* defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) */
614 if (sp->_sysmouse_char_width <= 0)
615 sp->_sysmouse_char_width = 8;
616 if (sp->_sysmouse_char_height <= 0)
617 sp->_sysmouse_char_height = 16;
618 sp->_mouse_type = M_SYSMOUSE;
623 #endif /* USE_SYSMOUSE */
625 /* we know how to recognize mouse events under "xterm" */
626 if (key_mouse != 0) {
627 if (!strcmp(key_mouse, xterm_kmous)
628 || strstr(cur_term->type.term_names, "xterm") != 0) {
629 init_xterm_mouse(sp);
631 } else if (strstr(cur_term->type.term_names, "xterm") != 0) {
632 if (_nc_add_to_try(&(sp->_keytry), xterm_kmous, KEY_MOUSE) == OK)
633 init_xterm_mouse(sp);
639 _nc_mouse_init(SCREEN *sp)
640 /* initialize the mouse */
646 if (!sp->_mouse_initialized) {
647 sp->_mouse_initialized = TRUE;
649 TR(MY_TRACE, ("_nc_mouse_init() called"));
651 sp->_mouse_eventp = FirstEV(sp);
652 for (i = 0; i < EV_MAX; i++)
653 sp->_mouse_events[i].id = INVALID_EVENT;
655 initialize_mousetype(sp);
657 T(("_nc_mouse_init() set mousetype to %d", sp->_mouse_type));
659 result = sp->_mouse_initialized;
665 * Query to see if there is a pending mouse event. This is called from
666 * fifo_push() in lib_getch.c
669 _nc_mouse_event(SCREEN *sp GCC_UNUSED)
671 MEVENT *eventp = sp->_mouse_eventp;
676 switch (sp->_mouse_type) {
678 /* xterm: never have to query, mouse events are in the keyboard stream */
683 int i, res = read(M_FD(sp), &kbuf, 3); /* Eat the prefix */
685 printf("Got %d chars instead of 3 for prefix.\n", res);
686 for (i = 0; i < res; i++) {
687 if (kbuf[i] != key_mouse[i])
688 printf("Got char %d instead of %d for prefix.\n",
689 (int) kbuf[i], (int) key_mouse[i]);
693 #endif /* USE_EMX_MOUSE */
698 if (sp->_mouse_fd >= 0) {
699 /* query server for event, return TRUE if we find one */
702 switch (my_Gpm_GetEvent(&ev)) {
704 /* Connection closed, drop the mouse. */
708 /* there's only one mouse... */
709 eventp->id = NORMAL_EVENT;
712 switch (ev.type & 0x0f) {
714 if (ev.buttons & GPM_B_LEFT)
715 eventp->bstate |= BUTTON1_PRESSED;
716 if (ev.buttons & GPM_B_MIDDLE)
717 eventp->bstate |= BUTTON2_PRESSED;
718 if (ev.buttons & GPM_B_RIGHT)
719 eventp->bstate |= BUTTON3_PRESSED;
722 if (ev.buttons & GPM_B_LEFT)
723 eventp->bstate |= BUTTON1_RELEASED;
724 if (ev.buttons & GPM_B_MIDDLE)
725 eventp->bstate |= BUTTON2_RELEASED;
726 if (ev.buttons & GPM_B_RIGHT)
727 eventp->bstate |= BUTTON3_RELEASED;
733 eventp->x = ev.x - 1;
734 eventp->y = ev.y - 1;
737 /* bump the next-free pointer into the circular list */
738 sp->_mouse_eventp = eventp = NEXT(eventp);
748 if (sp->_sysmouse_head < sp->_sysmouse_tail) {
749 *eventp = sp->_sysmouse_fifo[sp->_sysmouse_head];
752 * Point the fifo-head to the next possible location. If there
753 * are none, reset the indices. This may be interrupted by the
754 * signal handler, doing essentially the same reset.
756 sp->_sysmouse_head += 1;
757 if (sp->_sysmouse_head == sp->_sysmouse_tail) {
758 sp->_sysmouse_tail = 0;
759 sp->_sysmouse_head = 0;
762 /* bump the next-free pointer into the circular list */
763 sp->_mouse_eventp = eventp = NEXT(eventp);
767 #endif /* USE_SYSMOUSE */
773 return result; /* true if we found an event */
777 _nc_mouse_inline(SCREEN *sp)
778 /* mouse report received in the keyboard stream -- parse its info */
782 MEVENT *eventp = sp->_mouse_eventp;
784 TR(MY_TRACE, ("_nc_mouse_inline() called"));
786 if (sp->_mouse_type == M_XTERM) {
787 unsigned char kbuf[4];
792 /* This code requires that your xterm entry contain the kmous
793 * capability and that it be set to the \E[M documented in the
794 * Xterm Control Sequences reference. This is how we
795 * arrange for mouse events to be reported via a KEY_MOUSE
796 * return value from wgetch(). After this value is received,
797 * _nc_mouse_inline() gets called and is immediately
798 * responsible for parsing the mouse status information
799 * following the prefix.
801 * The following quotes from the ctrlseqs.ms document in the
802 * X distribution, describing the X mouse tracking feature:
804 * Parameters for all mouse tracking escape sequences
805 * generated by xterm encode numeric parameters in a single
806 * character as value+040. For example, ! is 1.
808 * On button press or release, xterm sends ESC [ M CbCxCy.
809 * The low two bits of Cb encode button information: 0=MB1
810 * pressed, 1=MB2 pressed, 2=MB3 pressed, 3=release. The
811 * upper bits encode what modifiers were down when the
812 * button was pressed and are added together. 4=Shift,
813 * 8=Meta, 16=Control. Cx and Cy are the x and y coordinates
814 * of the mouse event. The upper left corner is (1,1).
816 * (End quote) By the time we get here, we've eaten the
817 * key prefix. FYI, the loop below is necessary because
818 * mouse click info isn't guaranteed to present as a
821 * Wheel mice may return buttons 4 and 5 when the wheel is turned.
822 * We encode those as button presses.
824 for (grabbed = 0; grabbed < 3; grabbed += (size_t) res) {
826 /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
828 res = read(M_FD(sp) >= 0 ? M_FD(sp) : sp->_ifd, &kbuf, 3);
830 res = read(sp->_ifd, kbuf + grabbed, 3 - grabbed);
838 ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
840 /* there's only one mouse... */
841 eventp->id = NORMAL_EVENT;
843 /* processing code goes here */
845 prev = PREV(eventp)->bstate;
848 #define PRESS_POSITION(n) \
849 eventp->bstate = MASK_PRESS(n); \
850 if (kbuf[0] & 0x40) \
851 eventp->bstate = MASK_RELEASE(n)
853 #define PRESS_POSITION(n) \
854 eventp->bstate = (mmask_t) (prev & MASK_PRESS(n) \
855 ? REPORT_MOUSE_POSITION \
859 switch (kbuf[0] & 0x3) {
862 eventp->bstate = MASK_PRESS(4);
868 #if NCURSES_MOUSE_VERSION == 2
870 eventp->bstate = MASK_PRESS(5);
882 * Release events aren't reported for individual buttons, just for
883 * the button set as a whole. However, because there are normally
884 * no mouse events under xterm that intervene between press and
885 * release, we can infer the button actually released by looking at
886 * the previous event.
888 if (prev & (BUTTON_PRESSED | BUTTON_RELEASED)) {
889 eventp->bstate = BUTTON_RELEASED;
890 for (b = 1; b <= MAX_BUTTONS; ++b) {
891 if (!(prev & MASK_PRESS(b)))
892 eventp->bstate &= ~MASK_RELEASE(b);
896 * XFree86 xterm will return a stream of release-events to
897 * let the application know where the mouse is going, if the
898 * private mode 1002 or 1003 is enabled.
900 eventp->bstate = REPORT_MOUSE_POSITION;
904 result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
907 eventp->bstate |= BUTTON_SHIFT;
910 eventp->bstate |= BUTTON_ALT;
913 eventp->bstate |= BUTTON_CTRL;
916 eventp->x = (kbuf[1] - ' ') - 1;
917 eventp->y = (kbuf[2] - ' ') - 1;
919 ("_nc_mouse_inline: primitive mouse-event %s has slot %ld",
920 _nc_tracemouse(sp, eventp),
921 (long) IndexEV(sp, eventp)));
923 /* bump the next-free pointer into the circular list */
924 sp->_mouse_eventp = NEXT(eventp);
925 #if 0 /* this return would be needed for QNX's mods to lib_getch.c */
934 mouse_activate(SCREEN *sp, bool on)
936 if (!on && !sp->_mouse_initialized)
939 if (!_nc_mouse_init(sp))
944 switch (sp->_mouse_type) {
946 #if NCURSES_EXT_FUNCS
947 NCURSES_SP_NAME(keyok) (NCURSES_SP_ARGx KEY_MOUSE, on);
949 TPUTS_TRACE("xterm mouse initialization");
950 enable_xterm_mouse(sp, 1);
954 if (enable_gpm_mouse(sp, TRUE)) {
955 sp->_mouse_fd = *(my_gpm_fd);
956 T(("GPM mouse_fd %d", sp->_mouse_fd));
962 signal(SIGUSR2, handle_sysmouse);
963 sp->_mouse_active = TRUE;
969 /* Make runtime binding to cut down on object size of applications that
970 * do not use the mouse (e.g., 'clear').
972 sp->_mouse_event = _nc_mouse_event;
973 sp->_mouse_inline = _nc_mouse_inline;
974 sp->_mouse_parse = _nc_mouse_parse;
975 sp->_mouse_resume = _nc_mouse_resume;
976 sp->_mouse_wrap = _nc_mouse_wrap;
979 switch (sp->_mouse_type) {
981 TPUTS_TRACE("xterm mouse deinitialization");
982 enable_xterm_mouse(sp, 0);
986 enable_gpm_mouse(sp, FALSE);
991 signal(SIGUSR2, SIG_IGN);
992 sp->_mouse_active = FALSE;
999 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
1002 /**************************************************************************
1004 * Device-independent code
1006 **************************************************************************/
1009 _nc_mouse_parse(SCREEN *sp, int runcount)
1010 /* parse a run of atomic mouse events into a gesture */
1012 MEVENT *eventp = sp->_mouse_eventp;
1013 MEVENT *ep, *runp, *next, *prev = PREV(eventp);
1018 TR(MY_TRACE, ("_nc_mouse_parse(%d) called", runcount));
1021 * When we enter this routine, the event list next-free pointer
1022 * points just past a run of mouse events that we know were separated
1023 * in time by less than the critical click interval. The job of this
1024 * routine is to collapse this run into a single higher-level event
1027 * We accomplish this in two passes. The first pass merges press/release
1028 * pairs into click events. The second merges runs of click events into
1029 * double or triple-click events.
1031 * It's possible that the run may not resolve to a single event (for
1032 * example, if the user quadruple-clicks). If so, leading events
1033 * in the run are ignored.
1035 * Note that this routine is independent of the format of the specific
1036 * format of the pointing-device's reports. We can use it to parse
1037 * gestures on anything that reports press/release events on a per-
1038 * button basis, as long as the device-dependent mouse code puts stuff
1039 * on the queue in MEVENT format.
1041 if (runcount == 1) {
1043 ("_nc_mouse_parse: returning simple mouse event %s at slot %ld",
1044 _nc_tracemouse(sp, prev),
1045 (long) IndexEV(sp, prev)));
1046 return (prev->id >= NORMAL_EVENT)
1047 ? ((prev->bstate & sp->_mouse_mask) ? TRUE : FALSE)
1051 /* find the start of the run */
1053 for (n = runcount; n > 0; n--) {
1058 if (USE_TRACEF(TRACE_IEVENT)) {
1059 _trace_slot(sp, "before mouse press/release merge:");
1060 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1061 RunParams(sp, eventp, runp),
1063 _nc_unlock_global(tracef);
1067 /* first pass; merge press/release pairs */
1070 for (ep = runp; (next = NEXT(ep)) != eventp; ep = next) {
1072 #define MASK_CHANGED(x) (!(ep->bstate & MASK_PRESS(x)) \
1073 == !(next->bstate & MASK_RELEASE(x)))
1075 if (ep->x == next->x && ep->y == next->y
1076 && (ep->bstate & BUTTON_PRESSED)
1081 #if NCURSES_MOUSE_VERSION == 2
1085 for (b = 1; b <= MAX_BUTTONS; ++b) {
1086 if ((sp->_mouse_mask & MASK_CLICK(b))
1087 && (ep->bstate & MASK_PRESS(b))) {
1088 ep->bstate &= ~MASK_PRESS(b);
1089 ep->bstate |= MASK_CLICK(b);
1094 next->id = INVALID_EVENT;
1101 if (USE_TRACEF(TRACE_IEVENT)) {
1102 _trace_slot(sp, "before mouse click merge:");
1103 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1104 RunParams(sp, eventp, runp),
1106 _nc_unlock_global(tracef);
1111 * Second pass; merge click runs. At this point, click events are
1112 * each followed by one invalid event. We merge click events
1113 * forward in the queue.
1115 * NOTE: There is a problem with this design! If the application
1116 * allows enough click events to pile up in the circular queue so
1117 * they wrap around, it will cheerfully merge the newest forward
1118 * into the oldest, creating a bogus doubleclick and confusing
1119 * the queue-traversal logic rather badly. Generally this won't
1120 * happen, because calling getmouse() marks old events invalid and
1121 * ineligible for merges. The true solution to this problem would
1122 * be to timestamp each MEVENT and perform the obvious sanity check,
1123 * but the timer element would have to have sub-second resolution,
1124 * which would get us into portability trouble.
1130 for (ep = runp; (next = NEXT(ep)) != eventp; ep = next)
1131 if (ep->id != INVALID_EVENT) {
1132 if (next->id != INVALID_EVENT)
1134 follower = NEXT(next);
1135 if (follower->id == INVALID_EVENT)
1138 /* merge click events forward */
1139 if ((ep->bstate & BUTTON_CLICKED)
1140 && (follower->bstate & BUTTON_CLICKED)) {
1141 for (b = 1; b <= MAX_BUTTONS; ++b) {
1142 if ((sp->_mouse_mask & MASK_DOUBLE_CLICK(b))
1143 && (follower->bstate & MASK_CLICK(b))) {
1144 follower->bstate &= ~MASK_CLICK(b);
1145 follower->bstate |= MASK_DOUBLE_CLICK(b);
1150 ep->id = INVALID_EVENT;
1153 /* merge double-click events forward */
1154 if ((ep->bstate & BUTTON_DOUBLE_CLICKED)
1155 && (follower->bstate & BUTTON_CLICKED)) {
1156 for (b = 1; b <= MAX_BUTTONS; ++b) {
1157 if ((sp->_mouse_mask & MASK_TRIPLE_CLICK(b))
1158 && (follower->bstate & MASK_CLICK(b))) {
1159 follower->bstate &= ~MASK_CLICK(b);
1160 follower->bstate |= MASK_TRIPLE_CLICK(b);
1165 ep->id = INVALID_EVENT;
1172 if (USE_TRACEF(TRACE_IEVENT)) {
1173 _trace_slot(sp, "before mouse event queue compaction:");
1174 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1175 RunParams(sp, eventp, runp),
1177 _nc_unlock_global(tracef);
1182 * Now try to throw away trailing events flagged invalid, or that
1183 * don't match the current event mask.
1185 for (; runcount; prev = PREV(eventp), runcount--)
1186 if (prev->id == INVALID_EVENT || !(prev->bstate & sp->_mouse_mask)) {
1187 sp->_mouse_eventp = eventp = prev;
1190 if (USE_TRACEF(TRACE_IEVENT)) {
1191 _trace_slot(sp, "after mouse event queue compaction:");
1192 _tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
1193 RunParams(sp, eventp, runp),
1195 _nc_unlock_global(tracef);
1197 for (ep = runp; ep != eventp; ep = NEXT(ep))
1198 if (ep->id != INVALID_EVENT)
1200 ("_nc_mouse_parse: returning composite mouse event %s at slot %ld",
1201 _nc_tracemouse(sp, ep),
1202 (long) IndexEV(sp, ep)));
1205 /* after all this, do we have a valid event? */
1206 return (PREV(eventp)->id != INVALID_EVENT);
1210 _nc_mouse_wrap(SCREEN *sp)
1211 /* release mouse -- called by endwin() before shellout/exit */
1213 TR(MY_TRACE, ("_nc_mouse_wrap() called"));
1215 switch (sp->_mouse_type) {
1217 if (sp->_mouse_mask)
1218 mouse_activate(sp, FALSE);
1221 /* GPM: pass all mouse events to next client */
1223 if (sp->_mouse_mask)
1224 mouse_activate(sp, FALSE);
1229 mouse_activate(sp, FALSE);
1238 _nc_mouse_resume(SCREEN *sp)
1239 /* re-connect to mouse -- called by doupdate() after shellout */
1241 TR(MY_TRACE, ("_nc_mouse_resume() called"));
1243 switch (sp->_mouse_type) {
1245 /* xterm: re-enable reporting */
1246 if (sp->_mouse_mask)
1247 mouse_activate(sp, TRUE);
1252 /* GPM: reclaim our event set */
1253 if (sp->_mouse_mask)
1254 mouse_activate(sp, TRUE);
1260 mouse_activate(sp, TRUE);
1268 /**************************************************************************
1270 * Mouse interface entry points for the API
1272 **************************************************************************/
1275 NCURSES_SP_NAME(getmouse) (NCURSES_SP_DCLx MEVENT * aevent)
1277 T((T_CALLED("getmouse(%p,%p)"), SP_PARM, aevent));
1279 if ((aevent != 0) && (SP_PARM != 0) && (SP_PARM->_mouse_type != M_NONE)) {
1280 MEVENT *eventp = SP_PARM->_mouse_eventp;
1281 /* compute the current-event pointer */
1282 MEVENT *prev = PREV(eventp);
1284 /* copy the event we find there */
1287 TR(TRACE_IEVENT, ("getmouse: returning event %s from slot %ld",
1288 _nc_tracemouse(SP_PARM, prev),
1289 (long) IndexEV(SP_PARM, prev)));
1291 prev->id = INVALID_EVENT; /* so the queue slot becomes free */
1297 #if NCURSES_SP_FUNCS
1298 /* grab a copy of the current mouse event */
1300 getmouse(MEVENT * aevent)
1302 return NCURSES_SP_NAME(getmouse) (CURRENT_SCREEN, aevent);
1307 NCURSES_SP_NAME(ungetmouse) (NCURSES_SP_DCLx MEVENT * aevent)
1311 T((T_CALLED("ungetmouse(%p,%p)"), SP_PARM, aevent));
1313 if (aevent != 0 && SP_PARM != 0) {
1314 MEVENT *eventp = SP_PARM->_mouse_eventp;
1316 /* stick the given event in the next-free slot */
1319 /* bump the next-free pointer into the circular list */
1320 SP_PARM->_mouse_eventp = NEXT(eventp);
1322 /* push back the notification event on the keyboard queue */
1323 result = NCURSES_SP_NAME(ungetch) (NCURSES_SP_ARGx KEY_MOUSE);
1328 #if NCURSES_SP_FUNCS
1329 /* enqueue a synthesized mouse event to be seen by the next wgetch() */
1331 ungetmouse(MEVENT * aevent)
1333 return NCURSES_SP_NAME(ungetmouse) (CURRENT_SCREEN, aevent);
1337 NCURSES_EXPORT(mmask_t)
1338 NCURSES_SP_NAME(mousemask) (NCURSES_SP_DCLx mmask_t newmask, mmask_t * oldmask)
1339 /* set the mouse event mask */
1343 T((T_CALLED("mousemask(%p,%#lx,%p)"), SP_PARM, (unsigned long) newmask, oldmask));
1347 *oldmask = SP_PARM->_mouse_mask;
1349 if (newmask || SP_PARM->_mouse_initialized) {
1350 _nc_mouse_init(SP_PARM);
1351 if (SP_PARM->_mouse_type != M_NONE) {
1353 (REPORT_MOUSE_POSITION
1360 | BUTTON_DOUBLE_CLICKED
1361 | BUTTON_TRIPLE_CLICKED);
1363 mouse_activate(SP_PARM, (bool) (result != 0));
1365 SP_PARM->_mouse_mask = result;
1372 #if NCURSES_SP_FUNCS
1373 NCURSES_EXPORT(mmask_t)
1374 mousemask(mmask_t newmask, mmask_t * oldmask)
1376 return NCURSES_SP_NAME(mousemask) (CURRENT_SCREEN, newmask, oldmask);
1380 NCURSES_EXPORT(bool)
1381 wenclose(const WINDOW *win, int y, int x)
1382 /* check to see if given window encloses given screen location */
1384 bool result = FALSE;
1386 T((T_CALLED("wenclose(%p,%d,%d)"), win, y, x));
1390 result = ((win->_begy <= y &&
1392 (win->_begx + win->_maxx) >= x &&
1393 (win->_begy + win->_maxy) >= y) ? TRUE : FALSE);
1399 NCURSES_SP_NAME(mouseinterval) (NCURSES_SP_DCLx int maxclick)
1400 /* set the maximum mouse interval within which to recognize a click */
1404 T((T_CALLED("mouseinterval(%p,%d)"), SP_PARM, maxclick));
1407 oldval = SP_PARM->_maxclick;
1409 SP_PARM->_maxclick = maxclick;
1411 oldval = DEFAULT_MAXCLICK;
1417 #if NCURSES_SP_FUNCS
1419 mouseinterval(int maxclick)
1421 return NCURSES_SP_NAME(mouseinterval) (CURRENT_SCREEN, maxclick);
1425 /* This may be used by other routines to ask for the existence of mouse
1427 NCURSES_EXPORT(bool)
1428 _nc_has_mouse(SCREEN *sp)
1430 return (((0 == sp) || (sp->_mouse_type == M_NONE)) ? FALSE : TRUE);
1433 NCURSES_EXPORT(bool)
1434 NCURSES_SP_NAME(has_mouse) (NCURSES_SP_DCL0)
1436 return _nc_has_mouse(SP_PARM);
1439 #if NCURSES_SP_FUNCS
1440 NCURSES_EXPORT(bool)
1443 return _nc_has_mouse(CURRENT_SCREEN);
1447 NCURSES_EXPORT(bool)
1448 wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
1450 bool result = FALSE;
1452 T((T_CALLED("wmouse_trafo(%p,%p,%p,%d)"), win, pY, pX, to_screen));
1454 if (win && pY && pX) {
1459 y += win->_begy + win->_yoffset;
1461 if (wenclose(win, y, x))
1464 if (wenclose(win, y, x)) {
1465 y -= (win->_begy + win->_yoffset);