xref: /openbsd-src/lib/libcurses/base/lib_mouse.c (revision 852c4eda9a667a259e9fed88dd1f9d35330b27e1)
1 /*	$OpenBSD: lib_mouse.c,v 1.10 2000/07/10 03:06:14 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37  * This module is intended to encapsulate ncurses's interface to pointing
38  * devices.
39  *
40  * The first method used is xterm's internal mouse-tracking facility.
41  * The second is Alessandro Rubini's GPM server.
42  *
43  * Notes for implementors of new mouse-interface methods:
44  *
45  * The code is logically split into a lower level that accepts event reports
46  * in a device-dependent format and an upper level that parses mouse gestures
47  * and filters events.  The mediating data structure is a circular queue of
48  * MEVENT structures.
49  *
50  * Functionally, the lower level's job is to pick up primitive events and
51  * put them on the circular queue.  This can happen in one of two ways:
52  * either (a) _nc_mouse_event() detects a series of incoming mouse reports
53  * and queues them, or (b) code in lib_getch.c detects the kmous prefix in
54  * the keyboard input stream and calls _nc_mouse_inline to queue up a series
55  * of adjacent mouse reports.
56  *
57  * In either case, _nc_mouse_parse() should be called after the series is
58  * accepted to parse the digested mouse reports (low-level MEVENTs) into
59  * a gesture (a high-level or composite MEVENT).
60  *
61  * Don't be too shy about adding new event types or modifiers, if you can find
62  * room for them in the 32-bit mask.  The API is written so that users get
63  * feedback on which theoretical event types they won't see when they call
64  * mousemask. There's one bit per button (the RESERVED_EVENT bit) not being
65  * used yet, and a couple of bits open at the high end.
66  */
67 
68 #ifdef __EMX__
69 #  include <io.h>
70 #  define  INCL_DOS
71 #  define  INCL_VIO
72 #  define  INCL_KBD
73 #  define  INCL_MOU
74 #  define  INCL_DOSPROCESS
75 #  include <os2.h>		/* Need to include before the others */
76 #endif
77 
78 #include <curses.priv.h>
79 #include <term.h>
80 
81 #if USE_GPM_SUPPORT
82 #ifndef LINT			/* don't need this for llib-lncurses */
83 #undef buttons			/* term.h defines this, and gpm uses it! */
84 #include <gpm.h>
85 #include <linux/keyboard.h>	/* defines KG_* macros */
86 #endif
87 #endif
88 
89 MODULE_ID("$From: lib_mouse.c,v 1.52 2000/06/29 23:02:26 tom Exp $")
90 
91 #define MY_TRACE TRACE_ICALLS|TRACE_IEVENT
92 
93 #define INVALID_EVENT	-1
94 
95 static int mousetype;
96 #define M_XTERM		-1	/* use xterm's mouse tracking? */
97 #define M_NONE		0	/* no mouse device */
98 #define M_GPM		1	/* use GPM */
99 #define M_QNX		2	/* QNX mouse on console */
100 #define M_QNX_TERM	3	/* QNX mouse on pterm/xterm (using qansi-m) */
101 
102 #if USE_GPM_SUPPORT
103 #ifndef LINT
104 static Gpm_Connect gpm_connect;
105 #endif
106 #endif
107 
108 static mmask_t eventmask;	/* current event mask */
109 
110 static bool _nc_mouse_parse(int);
111 static void _nc_mouse_resume(SCREEN *);
112 static void _nc_mouse_wrap(SCREEN *);
113 
114 /* maintain a circular list of mouse events */
115 
116 /* The definition of the circular list size (EV_MAX), is in curses.priv.h, so
117  * wgetch() may refer to the size and call _nc_mouse_parse() before circular
118  * list overflow.
119  */
120 static MEVENT events[EV_MAX];	/* hold the last mouse event seen */
121 static MEVENT *eventp = events;	/* next free slot in event queue */
122 #define NEXT(ep)	((ep == events + EV_MAX - 1) ? events : ep + 1)
123 #define PREV(ep)	((ep == events) ? events + EV_MAX - 1 : ep - 1)
124 
125 #ifdef TRACE
126 static void
127 _trace_slot(const char *tag)
128 {
129     MEVENT *ep;
130 
131     _tracef(tag);
132 
133     for (ep = events; ep < events + EV_MAX; ep++)
134 	_tracef("mouse event queue slot %ld = %s",
135 	    (long) (ep - events),
136 	    _tracemouse(ep));
137 }
138 #endif
139 
140 #ifdef USE_EMX_MOUSE
141 
142 #  define TOP_ROW          0
143 #  define LEFT_COL         0
144 
145 static int mouse_wfd;
146 static int mouse_thread;
147 static int mouse_activated;
148 static char mouse_buttons[] =
149 {0, 1, 3, 2};
150 
151 #  define M_FD(sp) sp->_mouse_fd
152 
153 static void
154 write_event(int down, int button, int x, int y)
155 {
156     char buf[6];
157     unsigned long ignore;
158 
159     strcpy(buf, key_mouse);
160     buf[3] = ' ' + (button - 1) + (down ? 0 : 0x40);
161     buf[4] = ' ' + x - LEFT_COL + 1;
162     buf[5] = ' ' + y - TOP_ROW + 1;
163     DosWrite(mouse_wfd, buf, 6, &ignore);
164 }
165 
166 static void
167 mouse_server(unsigned long ignored GCC_UNUSED)
168 {
169     unsigned short fWait = MOU_WAIT;
170     /* NOPTRRECT mourt = { 0,0,24,79 }; */
171     MOUEVENTINFO mouev;
172     HMOU hmou;
173     unsigned short mask = MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN;
174     int oldstate = 0;
175     char errmess[] = "Unexpected termination of mouse thread\r\n";
176     unsigned long ignore;
177 
178     /* open the handle for the mouse */
179     if (MouOpen(NULL, &hmou) == 0) {
180 
181 	if (MouSetEventMask(&mask, hmou) == 0
182 	    && MouDrawPtr(hmou) == 0) {
183 
184 	    for (;;) {
185 		/* sit and wait on the event queue */
186 		if (MouReadEventQue(&mouev, &fWait, hmou))
187 		    break;
188 		if (!mouse_activated)
189 		    goto finish;
190 
191 		/*
192 		 * OS/2 numbers a 3-button mouse inconsistently from other
193 		 * platforms:
194 		 *      1 = left
195 		 *      2 = right
196 		 *      3 = middle.
197 		 */
198 		if ((mouev.fs ^ oldstate) & MOUSE_BN1_DOWN)
199 		    write_event(mouev.fs & MOUSE_BN1_DOWN,
200 			mouse_buttons[1], mouev.col, mouev.row);
201 		if ((mouev.fs ^ oldstate) & MOUSE_BN2_DOWN)
202 		    write_event(mouev.fs & MOUSE_BN2_DOWN,
203 			mouse_buttons[3], mouev.col, mouev.row);
204 		if ((mouev.fs ^ oldstate) & MOUSE_BN3_DOWN)
205 		    write_event(mouev.fs & MOUSE_BN3_DOWN,
206 			mouse_buttons[2], mouev.col, mouev.row);
207 
208 	      finish:
209 		oldstate = mouev.fs;
210 	    }
211 	}
212 
213 	DosWrite(2, errmess, strlen(errmess), &ignore);
214 	MouClose(hmou);
215     }
216     DosExit(EXIT_THREAD, 0L);
217 }
218 static void
219 server_state(const int state)
220 {				/* It would be nice to implement pointer-off and stop looping... */
221     mouse_activated = state;
222 }
223 
224 #endif
225 
226 static int initialized;
227 
228 static void
229 initialize_mousetype(void)
230 {
231     static const char *xterm_kmous = "\033[M";
232 
233     /* Try gpm first, because gpm may be configured to run in xterm */
234 #if USE_GPM_SUPPORT
235     /* GPM: initialize connection to gpm server */
236     gpm_connect.eventMask = GPM_DOWN | GPM_UP;
237     gpm_connect.defaultMask = ~(gpm_connect.eventMask | GPM_HARD);
238     gpm_connect.minMod = 0;
239     gpm_connect.maxMod = ~((1 << KG_SHIFT) | (1 << KG_SHIFTL) | (1 << KG_SHIFTR));
240     if (Gpm_Open(&gpm_connect, 0) >= 0) {	/* returns the file-descriptor */
241 	mousetype = M_GPM;
242 	SP->_mouse_fd = gpm_fd;
243 	return;
244     }
245 #endif
246 
247     /* OS/2 VIO */
248 #ifdef USE_EMX_MOUSE
249     if (!mouse_thread
250 	&& strstr(cur_term->type.term_names, "xterm") == 0
251 	&& key_mouse) {
252 	int handles[2];
253 	if (pipe(handles) < 0) {
254 	    perror("mouse pipe error");
255 	} else {
256 	    int rc;
257 
258 	    if (!mouse_buttons[0]) {
259 		char *s = getenv("MOUSE_BUTTONS_123");
260 
261 		mouse_buttons[0] = 1;
262 		if (s && strlen(s) >= 3) {
263 		    mouse_buttons[1] = s[0] - '0';
264 		    mouse_buttons[2] = s[1] - '0';
265 		    mouse_buttons[3] = s[2] - '0';
266 		}
267 	    }
268 	    mouse_wfd = handles[1];
269 	    M_FD(SP) = handles[0];
270 	    /* Needed? */
271 	    setmode(handles[0], O_BINARY);
272 	    setmode(handles[1], O_BINARY);
273 	    /* Do not use CRT functions, we may single-threaded. */
274 	    rc = DosCreateThread((unsigned long *) &mouse_thread,
275 		mouse_server, 0, 0, 8192);
276 	    if (rc) {
277 		printf("mouse thread error %d=%#x", rc, rc);
278 	    } else {
279 		mousetype = M_XTERM;
280 		return;
281 	    }
282 	}
283     }
284 #endif
285 
286     /* we know how to recognize mouse events under "xterm" */
287     if (key_mouse != 0) {
288 	if (!strcmp(key_mouse, xterm_kmous)) {
289 	    mousetype = M_XTERM;
290 	    return;
291 	}
292     } else if (strstr(cur_term->type.term_names, "xterm") != 0) {
293 	(void) _nc_add_to_try(&(SP->_keytry), xterm_kmous, KEY_MOUSE);
294 	mousetype = M_XTERM;
295 	return;
296     }
297 }
298 
299 static void
300 _nc_mouse_init(void)
301 /* initialize the mouse */
302 {
303     int i;
304 
305     if (!initialized) {
306 	initialized = TRUE;
307 
308 	TR(MY_TRACE, ("_nc_mouse_init() called"));
309 
310 	for (i = 0; i < EV_MAX; i++)
311 	    events[i].id = INVALID_EVENT;
312 
313 	initialize_mousetype();
314 
315 	T(("_nc_mouse_init() set mousetype to %d", mousetype));
316     }
317 }
318 
319 static bool
320 _nc_mouse_event(SCREEN * sp GCC_UNUSED)
321 /* query to see if there is a pending mouse event */
322 {
323 #if USE_GPM_SUPPORT
324     /* GPM: query server for event, return TRUE if we find one */
325     Gpm_Event ev;
326 
327     if (gpm_fd >= 0
328 	&& (_nc_timed_wait(3, 0, (int *) 0) & 2) != 0
329 	&& Gpm_GetEvent(&ev) == 1) {
330 	eventp->id = 0;		/* there's only one mouse... */
331 
332 	eventp->bstate = 0;
333 	switch (ev.type & 0x0f) {
334 	case (GPM_DOWN):
335 	    if (ev.buttons & GPM_B_LEFT)
336 		eventp->bstate |= BUTTON1_PRESSED;
337 	    if (ev.buttons & GPM_B_MIDDLE)
338 		eventp->bstate |= BUTTON2_PRESSED;
339 	    if (ev.buttons & GPM_B_RIGHT)
340 		eventp->bstate |= BUTTON3_PRESSED;
341 	    break;
342 	case (GPM_UP):
343 	    if (ev.buttons & GPM_B_LEFT)
344 		eventp->bstate |= BUTTON1_RELEASED;
345 	    if (ev.buttons & GPM_B_MIDDLE)
346 		eventp->bstate |= BUTTON2_RELEASED;
347 	    if (ev.buttons & GPM_B_RIGHT)
348 		eventp->bstate |= BUTTON3_RELEASED;
349 	    break;
350 	default:
351 	    break;
352 	}
353 
354 	eventp->x = ev.x - 1;
355 	eventp->y = ev.y - 1;
356 	eventp->z = 0;
357 
358 	/* bump the next-free pointer into the circular list */
359 	eventp = NEXT(eventp);
360 	return (TRUE);
361     }
362 #endif
363 
364     /* xterm: never have to query, mouse events are in the keyboard stream */
365     return (FALSE);		/* no event waiting */
366 }
367 
368 static bool
369 _nc_mouse_inline(SCREEN * sp)
370 /* mouse report received in the keyboard stream -- parse its info */
371 {
372     TR(MY_TRACE, ("_nc_mouse_inline() called"));
373 
374     if (mousetype == M_XTERM) {
375 	unsigned char kbuf[4];
376 	MEVENT *prev;
377 	size_t grabbed;
378 	int res;
379 
380 	/* This code requires that your xterm entry contain the kmous
381 	 * capability and that it be set to the \E[M documented in the
382 	 * Xterm Control Sequences reference.  This is how we
383 	 * arrange for mouse events to be reported via a KEY_MOUSE
384 	 * return value from wgetch().  After this value is received,
385 	 * _nc_mouse_inline() gets called and is immediately
386 	 * responsible for parsing the mouse status information
387 	 * following the prefix.
388 	 *
389 	 * The following quotes from the ctrlseqs.ms document in the
390 	 * X distribution, describing the X mouse tracking feature:
391 	 *
392 	 * Parameters for all mouse tracking escape sequences
393 	 * generated by xterm encode numeric parameters in a single
394 	 * character as value+040.  For example, !  is 1.
395 	 *
396 	 * On button press or release, xterm sends ESC [ M CbCxCy.
397 	 * The low two bits of Cb encode button information: 0=MB1
398 	 * pressed, 1=MB2 pressed, 2=MB3 pressed, 3=release.  The
399 	 * upper bits encode what modifiers were down when the
400 	 * button was pressed and are added together.  4=Shift,
401 	 * 8=Meta, 16=Control.  Cx and Cy are the x and y coordinates
402 	 * of the mouse event.  The upper left corner is (1,1).
403 	 *
404 	 * (End quote)  By the time we get here, we've eaten the
405 	 * key prefix.  FYI, the loop below is necessary because
406 	 * mouse click info isn't guaranteed to present as a
407 	 * single clist item.  It always does under Linux but often
408 	 * fails to under Solaris.
409 	 */
410 	for (grabbed = 0; grabbed < 3; grabbed += res) {
411 
412 	    /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
413 #ifdef USE_EMX_MOUSE
414 	    res = read(M_FD(sp) >= 0 ? M_FD(sp) : sp->_ifd, &kbuf, 3);
415 #else
416 	    res = read(sp->_ifd, kbuf + grabbed, 3 - grabbed);
417 #endif
418 	    if (res == -1)
419 		break;
420 	}
421 	kbuf[3] = '\0';
422 
423 	TR(TRACE_IEVENT,
424 	    ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
425 
426 	eventp->id = 0;		/* there's only one mouse... */
427 
428 	/* processing code goes here */
429 	eventp->bstate = 0;
430 	switch (kbuf[0] & 0x3) {
431 	case 0x0:
432 	    eventp->bstate = BUTTON1_PRESSED;
433 #ifdef USE_EMX_MOUSE
434 	    if (kbuf[0] & 0x40)
435 		eventp->bstate = BUTTON1_RELEASED;
436 #endif
437 	    break;
438 
439 	case 0x1:
440 	    eventp->bstate = BUTTON2_PRESSED;
441 #ifdef USE_EMX_MOUSE
442 	    if (kbuf[0] & 0x40)
443 		eventp->bstate = BUTTON2_RELEASED;
444 #endif
445 	    break;
446 
447 	case 0x2:
448 	    eventp->bstate = BUTTON3_PRESSED;
449 #ifdef USE_EMX_MOUSE
450 	    if (kbuf[0] & 0x40)
451 		eventp->bstate = BUTTON3_RELEASED;
452 #endif
453 	    break;
454 
455 	case 0x3:
456 	    /*
457 	     * Release events aren't reported for individual buttons,
458 	     * just for the button set as a whole...
459 	     */
460 	    eventp->bstate =
461 		(BUTTON1_RELEASED |
462 		BUTTON2_RELEASED |
463 		BUTTON3_RELEASED);
464 	    /*
465 	     * ...however, because there are no kinds of mouse events under
466 	     * xterm that can intervene between press and release, we can
467 	     * deduce which buttons were actually released by looking at the
468 	     * previous event.
469 	     */
470 	    prev = PREV(eventp);
471 	    if (!(prev->bstate & BUTTON1_PRESSED))
472 		eventp->bstate &= ~BUTTON1_RELEASED;
473 	    if (!(prev->bstate & BUTTON2_PRESSED))
474 		eventp->bstate &= ~BUTTON2_RELEASED;
475 	    if (!(prev->bstate & BUTTON3_PRESSED))
476 		eventp->bstate &= ~BUTTON3_RELEASED;
477 	    break;
478 	}
479 
480 	if (kbuf[0] & 4) {
481 	    eventp->bstate |= BUTTON_SHIFT;
482 	}
483 	if (kbuf[0] & 8) {
484 	    eventp->bstate |= BUTTON_ALT;
485 	}
486 	if (kbuf[0] & 16) {
487 	    eventp->bstate |= BUTTON_CTRL;
488 	}
489 
490 	eventp->x = (kbuf[1] - ' ') - 1;
491 	eventp->y = (kbuf[2] - ' ') - 1;
492 	TR(MY_TRACE,
493 	    ("_nc_mouse_inline: primitive mouse-event %s has slot %ld",
494 		_tracemouse(eventp),
495 		(long) (eventp - events)));
496 
497 	/* bump the next-free pointer into the circular list */
498 	eventp = NEXT(eventp);
499 #if 0				/* this return would be needed for QNX's mods to lib_getch.c */
500 	return (TRUE);
501 #endif
502     }
503 
504     return (FALSE);
505 }
506 
507 static void
508 mouse_activate(bool on)
509 {
510     if (!on && !initialized)
511 	return;
512 
513     _nc_mouse_init();
514 
515     if (on) {
516 
517 	switch (mousetype) {
518 	case M_XTERM:
519 #ifdef NCURSES_EXT_FUNCS
520 	    keyok(KEY_MOUSE, on);
521 #endif
522 	    TPUTS_TRACE("xterm mouse initialization");
523 #ifdef USE_EMX_MOUSE
524 	    server_state(1);
525 #else
526 	    putp("\033[?1000h");
527 #endif
528 	    break;
529 #if USE_GPM_SUPPORT
530 	case M_GPM:
531 	    SP->_mouse_fd = gpm_fd;
532 	    break;
533 #endif
534 	}
535 	/* Make runtime binding to cut down on object size of applications that
536 	 * do not use the mouse (e.g., 'clear').
537 	 */
538 	SP->_mouse_event = _nc_mouse_event;
539 	SP->_mouse_inline = _nc_mouse_inline;
540 	SP->_mouse_parse = _nc_mouse_parse;
541 	SP->_mouse_resume = _nc_mouse_resume;
542 	SP->_mouse_wrap = _nc_mouse_wrap;
543 
544     } else {
545 
546 	switch (mousetype) {
547 	case M_XTERM:
548 	    TPUTS_TRACE("xterm mouse deinitialization");
549 #ifdef USE_EMX_MOUSE
550 	    server_state(0);
551 #else
552 	    putp("\033[?1000l");
553 #endif
554 	    break;
555 #if USE_GPM_SUPPORT
556 	case M_GPM:
557 	    break;
558 #endif
559 	}
560     }
561     _nc_flush();
562 }
563 
564 /**************************************************************************
565  *
566  * Device-independent code
567  *
568  **************************************************************************/
569 
570 static bool
571 _nc_mouse_parse(int runcount)
572 /* parse a run of atomic mouse events into a gesture */
573 {
574     MEVENT *ep, *runp, *next, *prev = PREV(eventp);
575     int n;
576     bool merge;
577 
578     TR(MY_TRACE, ("_nc_mouse_parse(%d) called", runcount));
579 
580     /*
581      * When we enter this routine, the event list next-free pointer
582      * points just past a run of mouse events that we know were separated
583      * in time by less than the critical click interval. The job of this
584      * routine is to collaps this run into a single higher-level event
585      * or gesture.
586      *
587      * We accomplish this in two passes.  The first pass merges press/release
588      * pairs into click events.  The second merges runs of click events into
589      * double or triple-click events.
590      *
591      * It's possible that the run may not resolve to a single event (for
592      * example, if the user quadruple-clicks).  If so, leading events
593      * in the run are ignored.
594      *
595      * Note that this routine is independent of the format of the specific
596      * format of the pointing-device's reports.  We can use it to parse
597      * gestures on anything that reports press/release events on a per-
598      * button basis, as long as the device-dependent mouse code puts stuff
599      * on the queue in MEVENT format.
600      */
601     if (runcount == 1) {
602 	TR(MY_TRACE,
603 	    ("_nc_mouse_parse: returning simple mouse event %s at slot %ld",
604 		_tracemouse(prev),
605 		(long) (prev - events)));
606 	return (prev->id >= 0)
607 	    ? ((prev->bstate & eventmask) ? TRUE : FALSE)
608 	    : FALSE;
609     }
610 
611     /* find the start of the run */
612     runp = eventp;
613     for (n = runcount; n > 0; n--) {
614 	runp = PREV(runp);
615     }
616 
617 #ifdef TRACE
618     if (_nc_tracing & TRACE_IEVENT) {
619 	_trace_slot("before mouse press/release merge:");
620 	_tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
621 	    (long) (runp - events),
622 	    (long) ((eventp - events) + (EV_MAX - 1)) % EV_MAX,
623 	    runcount);
624     }
625 #endif /* TRACE */
626 
627     /* first pass; merge press/release pairs */
628     do {
629 	merge = FALSE;
630 	for (ep = runp; next = NEXT(ep), next != eventp; ep = next) {
631 	    if (ep->x == next->x && ep->y == next->y
632 		&& (ep->bstate & (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED))
633 		&& (!(ep->bstate & BUTTON1_PRESSED)
634 		    == !(next->bstate & BUTTON1_RELEASED))
635 		&& (!(ep->bstate & BUTTON2_PRESSED)
636 		    == !(next->bstate & BUTTON2_RELEASED))
637 		&& (!(ep->bstate & BUTTON3_PRESSED)
638 		    == !(next->bstate & BUTTON3_RELEASED))
639 		) {
640 		if ((eventmask & BUTTON1_CLICKED)
641 		    && (ep->bstate & BUTTON1_PRESSED)) {
642 		    ep->bstate &= ~BUTTON1_PRESSED;
643 		    ep->bstate |= BUTTON1_CLICKED;
644 		    merge = TRUE;
645 		}
646 		if ((eventmask & BUTTON2_CLICKED)
647 		    && (ep->bstate & BUTTON2_PRESSED)) {
648 		    ep->bstate &= ~BUTTON2_PRESSED;
649 		    ep->bstate |= BUTTON2_CLICKED;
650 		    merge = TRUE;
651 		}
652 		if ((eventmask & BUTTON3_CLICKED)
653 		    && (ep->bstate & BUTTON3_PRESSED)) {
654 		    ep->bstate &= ~BUTTON3_PRESSED;
655 		    ep->bstate |= BUTTON3_CLICKED;
656 		    merge = TRUE;
657 		}
658 		if (merge)
659 		    next->id = INVALID_EVENT;
660 	    }
661 	}
662     } while
663 	(merge);
664 
665 #ifdef TRACE
666     if (_nc_tracing & TRACE_IEVENT) {
667 	_trace_slot("before mouse click merge:");
668 	_tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
669 	    (long) (runp - events),
670 	    (long) ((eventp - events) + (EV_MAX - 1)) % EV_MAX,
671 	    runcount);
672     }
673 #endif /* TRACE */
674 
675     /*
676      * Second pass; merge click runs.  At this point, click events are
677      * each followed by one invalid event. We merge click events
678      * forward in the queue.
679      *
680      * NOTE: There is a problem with this design!  If the application
681      * allows enough click events to pile up in the circular queue so
682      * they wrap around, it will cheerfully merge the newest forward
683      * into the oldest, creating a bogus doubleclick and confusing
684      * the queue-traversal logic rather badly.  Generally this won't
685      * happen, because calling getmouse() marks old events invalid and
686      * ineligible for merges.  The true solution to this problem would
687      * be to timestamp each MEVENT and perform the obvious sanity check,
688      * but the timer element would have to have sub-second resolution,
689      * which would get us into portability trouble.
690      */
691     do {
692 	MEVENT *follower;
693 
694 	merge = FALSE;
695 	for (ep = runp; next = NEXT(ep), next != eventp; ep = next)
696 	    if (ep->id != INVALID_EVENT) {
697 		if (next->id != INVALID_EVENT)
698 		    continue;
699 		follower = NEXT(next);
700 		if (follower->id == INVALID_EVENT)
701 		    continue;
702 
703 		/* merge click events forward */
704 		if ((ep->bstate &
705 			(BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED))
706 		    && (follower->bstate &
707 			(BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED))) {
708 		    if ((eventmask & BUTTON1_DOUBLE_CLICKED)
709 			&& (follower->bstate & BUTTON1_CLICKED)) {
710 			follower->bstate &= ~BUTTON1_CLICKED;
711 			follower->bstate |= BUTTON1_DOUBLE_CLICKED;
712 			merge = TRUE;
713 		    }
714 		    if ((eventmask & BUTTON2_DOUBLE_CLICKED)
715 			&& (follower->bstate & BUTTON2_CLICKED)) {
716 			follower->bstate &= ~BUTTON2_CLICKED;
717 			follower->bstate |= BUTTON2_DOUBLE_CLICKED;
718 			merge = TRUE;
719 		    }
720 		    if ((eventmask & BUTTON3_DOUBLE_CLICKED)
721 			&& (follower->bstate & BUTTON3_CLICKED)) {
722 			follower->bstate &= ~BUTTON3_CLICKED;
723 			follower->bstate |= BUTTON3_DOUBLE_CLICKED;
724 			merge = TRUE;
725 		    }
726 		    if (merge)
727 			ep->id = INVALID_EVENT;
728 		}
729 
730 		/* merge double-click events forward */
731 		if ((ep->bstate &
732 			(BUTTON1_DOUBLE_CLICKED
733 			    | BUTTON2_DOUBLE_CLICKED
734 			    | BUTTON3_DOUBLE_CLICKED))
735 		    && (follower->bstate &
736 			(BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED))) {
737 		    if ((eventmask & BUTTON1_TRIPLE_CLICKED)
738 			&& (follower->bstate & BUTTON1_CLICKED)) {
739 			follower->bstate &= ~BUTTON1_CLICKED;
740 			follower->bstate |= BUTTON1_TRIPLE_CLICKED;
741 			merge = TRUE;
742 		    }
743 		    if ((eventmask & BUTTON2_TRIPLE_CLICKED)
744 			&& (follower->bstate & BUTTON2_CLICKED)) {
745 			follower->bstate &= ~BUTTON2_CLICKED;
746 			follower->bstate |= BUTTON2_TRIPLE_CLICKED;
747 			merge = TRUE;
748 		    }
749 		    if ((eventmask & BUTTON3_TRIPLE_CLICKED)
750 			&& (follower->bstate & BUTTON3_CLICKED)) {
751 			follower->bstate &= ~BUTTON3_CLICKED;
752 			follower->bstate |= BUTTON3_TRIPLE_CLICKED;
753 			merge = TRUE;
754 		    }
755 		    if (merge)
756 			ep->id = INVALID_EVENT;
757 		}
758 	    }
759     } while
760 	(merge);
761 
762 #ifdef TRACE
763     if (_nc_tracing & TRACE_IEVENT) {
764 	_trace_slot("before mouse event queue compaction:");
765 	_tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
766 	    (long) (runp - events),
767 	    (long) ((eventp - events) + (EV_MAX - 1)) % EV_MAX,
768 	    runcount);
769     }
770 #endif /* TRACE */
771 
772     /*
773      * Now try to throw away trailing events flagged invalid, or that
774      * don't match the current event mask.
775      */
776     for (; runcount; prev = PREV(eventp), runcount--)
777 	if (prev->id == INVALID_EVENT || !(prev->bstate & eventmask)) {
778 	    eventp = prev;
779 	}
780 #ifdef TRACE
781     if (_nc_tracing & TRACE_IEVENT) {
782 	_trace_slot("after mouse event queue compaction:");
783 	_tracef("_nc_mouse_parse: run starts at %ld, ends at %ld, count %d",
784 	    (long) (runp - events),
785 	    (long) ((eventp - events) + (EV_MAX - 1)) % EV_MAX,
786 	    runcount);
787     }
788     for (ep = runp; ep != eventp; ep = NEXT(ep))
789 	if (ep->id != INVALID_EVENT)
790 	    TR(MY_TRACE,
791 		("_nc_mouse_parse: returning composite mouse event %s at slot %ld",
792 		    _tracemouse(ep),
793 		    (long) (ep - events)));
794 #endif /* TRACE */
795 
796     /* after all this, do we have a valid event? */
797     return (PREV(eventp)->id != INVALID_EVENT);
798 }
799 
800 static void
801 _nc_mouse_wrap(SCREEN * sp GCC_UNUSED)
802 /* release mouse -- called by endwin() before shellout/exit */
803 {
804     TR(MY_TRACE, ("_nc_mouse_wrap() called"));
805 
806     switch (mousetype) {
807     case M_XTERM:
808 	if (eventmask)
809 	    mouse_activate(FALSE);
810 	break;
811 #if USE_GPM_SUPPORT
812 	/* GPM: pass all mouse events to next client */
813     case M_GPM:
814 	break;
815 #endif
816     }
817 }
818 
819 static void
820 _nc_mouse_resume(SCREEN * sp GCC_UNUSED)
821 /* re-connect to mouse -- called by doupdate() after shellout */
822 {
823     TR(MY_TRACE, ("_nc_mouse_resume() called"));
824 
825     /* xterm: re-enable reporting */
826     if (mousetype == M_XTERM && eventmask)
827 	mouse_activate(TRUE);
828 
829     /* GPM: reclaim our event set */
830 }
831 
832 /**************************************************************************
833  *
834  * Mouse interface entry points for the API
835  *
836  **************************************************************************/
837 
838 int
839 getmouse(MEVENT * aevent)
840 /* grab a copy of the current mouse event */
841 {
842     T((T_CALLED("getmouse(%p)"), aevent));
843 
844     if (aevent && (mousetype != M_NONE)) {
845 	/* compute the current-event pointer */
846 	MEVENT *prev = PREV(eventp);
847 
848 	/* copy the event we find there */
849 	*aevent = *prev;
850 
851 	TR(TRACE_IEVENT, ("getmouse: returning event %s from slot %ld",
852 		_tracemouse(prev),
853 		(long) (prev - events)));
854 
855 	prev->id = INVALID_EVENT;	/* so the queue slot becomes free */
856 	returnCode(OK);
857     }
858     returnCode(ERR);
859 }
860 
861 int
862 ungetmouse(MEVENT * aevent)
863 /* enqueue a synthesized mouse event to be seen by the next wgetch() */
864 {
865     /* stick the given event in the next-free slot */
866     *eventp = *aevent;
867 
868     /* bump the next-free pointer into the circular list */
869     eventp = NEXT(eventp);
870 
871     /* push back the notification event on the keyboard queue */
872     return ungetch(KEY_MOUSE);
873 }
874 
875 mmask_t
876 mousemask(mmask_t newmask, mmask_t * oldmask)
877 /* set the mouse event mask */
878 {
879     mmask_t result = 0;
880 
881     T((T_CALLED("mousemask(%#lx,%p)"), newmask, oldmask));
882 
883     if (oldmask)
884 	*oldmask = eventmask;
885 
886     if (!newmask && !initialized)
887 	returnCode(0);
888 
889     _nc_mouse_init();
890     if (mousetype != M_NONE) {
891 	eventmask = newmask &
892 	    (BUTTON_ALT | BUTTON_CTRL | BUTTON_SHIFT
893 	    | BUTTON1_PRESSED | BUTTON1_RELEASED | BUTTON1_CLICKED
894 	    | BUTTON1_DOUBLE_CLICKED | BUTTON1_TRIPLE_CLICKED
895 	    | BUTTON2_PRESSED | BUTTON2_RELEASED | BUTTON2_CLICKED
896 	    | BUTTON2_DOUBLE_CLICKED | BUTTON2_TRIPLE_CLICKED
897 	    | BUTTON3_PRESSED | BUTTON3_RELEASED | BUTTON3_CLICKED
898 	    | BUTTON3_DOUBLE_CLICKED | BUTTON3_TRIPLE_CLICKED);
899 
900 	mouse_activate(eventmask != 0);
901 
902 	result = eventmask;
903     }
904 
905     returnCode(result);
906 }
907 
908 bool
909 wenclose(const WINDOW *win, int y, int x)
910 /* check to see if given window encloses given screen location */
911 {
912     if (win) {
913 	y -= win->_yoffset;
914 	return ((win->_begy <= y &&
915 		win->_begx <= x &&
916 		(win->_begx + win->_maxx) >= x &&
917 		(win->_begy + win->_maxy) >= y) ? TRUE : FALSE);
918     }
919     return FALSE;
920 }
921 
922 int
923 mouseinterval(int maxclick)
924 /* set the maximum mouse interval within which to recognize a click */
925 {
926     int oldval;
927 
928     if (SP != 0) {
929 	oldval = SP->_maxclick;
930 	if (maxclick >= 0)
931 	    SP->_maxclick = maxclick;
932     } else {
933 	oldval = DEFAULT_MAXCLICK;
934     }
935 
936     return (oldval);
937 }
938 
939 /* This may be used by other routines to ask for the existence of mouse
940    support */
941 int
942 _nc_has_mouse(void)
943 {
944     return (mousetype == M_NONE ? 0 : 1);
945 }
946 
947 bool
948 wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
949 {
950     bool result = FALSE;
951 
952     if (win && pY && pX) {
953 	int y = *pY;
954 	int x = *pX;
955 
956 	if (to_screen) {
957 	    y += win->_begy + win->_yoffset;
958 	    x += win->_begx;
959 	    if (wenclose(win, y, x))
960 		result = TRUE;
961 	} else {
962 	    if (wenclose(win, y, x)) {
963 		y -= (win->_begy + win->_yoffset);
964 		x -= win->_begx;
965 		result = TRUE;
966 	    }
967 	}
968 	if (result) {
969 	    *pX = x;
970 	    *pY = y;
971 	}
972     }
973     return (result);
974 }
975 
976 /* lib_mouse.c ends here */
977