xref: /titanic_44/usr/src/tools/cscope-fast/mouse.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /* common mouse interface functions */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <stdio.h>	/* NULL */
36*7c478bd9Sstevel@tonic-gate #include <stdlib.h>	/* NULL */
37*7c478bd9Sstevel@tonic-gate #include <string.h>	/* NULL */
38*7c478bd9Sstevel@tonic-gate #include <ctype.h>	/* isdigit */
39*7c478bd9Sstevel@tonic-gate #include "global.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #define	ctrl(x)			(x & 037)
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate MOUSETYPE mouse;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static	MOUSEMENU *loadedmenu;
46*7c478bd9Sstevel@tonic-gate static	BOOL	changemenu = YES;
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /* see if there is a mouse interface */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate void
initmouse(void)51*7c478bd9Sstevel@tonic-gate initmouse(void)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	char	*s, *term;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	if ((term = getenv("TERM")) == NULL) {
56*7c478bd9Sstevel@tonic-gate 		return;
57*7c478bd9Sstevel@tonic-gate 	}
58*7c478bd9Sstevel@tonic-gate 	if (strcmp(term, "emacsterm") == 0 || strcmp(term, "viterm") == 0) {
59*7c478bd9Sstevel@tonic-gate 		mouse = EMACSTERM;
60*7c478bd9Sstevel@tonic-gate 	} else if ((s = getenv("MOUSE")) != NULL && strcmp(s, "myx") == 0) {
61*7c478bd9Sstevel@tonic-gate 		/*
62*7c478bd9Sstevel@tonic-gate 		 * the MOUSE enviroment variable is for 5620 terminal
63*7c478bd9Sstevel@tonic-gate 		 * programs that have mouse support but the TERM environment
64*7c478bd9Sstevel@tonic-gate 		 * variable is the same as a terminal without a mouse, such
65*7c478bd9Sstevel@tonic-gate 		 * as myx
66*7c478bd9Sstevel@tonic-gate 		 */
67*7c478bd9Sstevel@tonic-gate 		mouse = MYX;
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 	if ((s = getenv("MOUSEMENU")) != NULL && strcmp(s, "none") == 0) {
70*7c478bd9Sstevel@tonic-gate 		changemenu = NO;
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 	initmenu();
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate /* reinitialize the mouse in case curses changed the attributes */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate void
reinitmouse(void)78*7c478bd9Sstevel@tonic-gate reinitmouse(void)
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate 	if (mouse == EMACSTERM) {
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 		/*
83*7c478bd9Sstevel@tonic-gate 		 * enable the mouse click and sweep coordinate control
84*7c478bd9Sstevel@tonic-gate 		 * sequence
85*7c478bd9Sstevel@tonic-gate 		 */
86*7c478bd9Sstevel@tonic-gate 		(void) printf("\033{2");
87*7c478bd9Sstevel@tonic-gate 		if (changemenu) {
88*7c478bd9Sstevel@tonic-gate 			(void) printf("\033#2");	/* switch to menu 2 */
89*7c478bd9Sstevel@tonic-gate 		}
90*7c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate /* restore any original mouse attributes not handled by terminfo */
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate void
cleanupmouse(void)97*7c478bd9Sstevel@tonic-gate cleanupmouse(void)
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	int	i;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	if (mouse == MYX && loadedmenu != NULL) {
102*7c478bd9Sstevel@tonic-gate 		/* remove the mouse menu */
103*7c478bd9Sstevel@tonic-gate 		(void) printf("\033[6;0X\033[9;0X");
104*7c478bd9Sstevel@tonic-gate 		for (i = 0; loadedmenu[i].text != NULL; ++i) {
105*7c478bd9Sstevel@tonic-gate 			(void) printf("\033[0;0x");
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate 		loadedmenu = NULL;
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate /* download a mouse menu */
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate void
downloadmenu(MOUSEMENU * menu)114*7c478bd9Sstevel@tonic-gate downloadmenu(MOUSEMENU *menu)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	int	i;
117*7c478bd9Sstevel@tonic-gate 	int	len;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	switch (mouse) {
120*7c478bd9Sstevel@tonic-gate 	case EMACSTERM:
121*7c478bd9Sstevel@tonic-gate 		reinitmouse();
122*7c478bd9Sstevel@tonic-gate 		(void) printf("\033V1");	/* display the scroll bar */
123*7c478bd9Sstevel@tonic-gate 		if (changemenu) {
124*7c478bd9Sstevel@tonic-gate 			(void) printf("\033M0@%s@%s@", menu[0].text,
125*7c478bd9Sstevel@tonic-gate 			    menu[0].value);
126*7c478bd9Sstevel@tonic-gate 			for (i = 1; menu[i].text != NULL; ++i) {
127*7c478bd9Sstevel@tonic-gate 				(void) printf("\033M@%s@%s@", menu[i].text,
128*7c478bd9Sstevel@tonic-gate 				    menu[i].value);
129*7c478bd9Sstevel@tonic-gate 			}
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
132*7c478bd9Sstevel@tonic-gate 		break;
133*7c478bd9Sstevel@tonic-gate 	case MYX:
134*7c478bd9Sstevel@tonic-gate 		if (changemenu) {
135*7c478bd9Sstevel@tonic-gate 			cleanupmouse();
136*7c478bd9Sstevel@tonic-gate 			(void) printf("\033[6;1X\033[9;1X");
137*7c478bd9Sstevel@tonic-gate 			for (i = 0; menu[i].text != NULL; ++i) {
138*7c478bd9Sstevel@tonic-gate 				len = strlen(menu[i].text);
139*7c478bd9Sstevel@tonic-gate 				(void) printf("\033[%d;%dx%s%s", len,
140*7c478bd9Sstevel@tonic-gate 				    len + strlen(menu[i].value),
141*7c478bd9Sstevel@tonic-gate 				    menu[i].text, menu[i].value);
142*7c478bd9Sstevel@tonic-gate 			}
143*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
144*7c478bd9Sstevel@tonic-gate 			loadedmenu = menu;
145*7c478bd9Sstevel@tonic-gate 		}
146*7c478bd9Sstevel@tonic-gate 		break;
147*7c478bd9Sstevel@tonic-gate 	case NONE:
148*7c478bd9Sstevel@tonic-gate 	case PC7300:
149*7c478bd9Sstevel@tonic-gate 		break;
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate }
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate /* draw the scroll bar */
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate void
drawscrollbar(int top,int bot,int total)156*7c478bd9Sstevel@tonic-gate drawscrollbar(int top, int bot, int total)
157*7c478bd9Sstevel@tonic-gate {
158*7c478bd9Sstevel@tonic-gate 	int	p1, p2;
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	if (mouse == EMACSTERM) {
161*7c478bd9Sstevel@tonic-gate 		if (bot > top && total > 0) {
162*7c478bd9Sstevel@tonic-gate 			p1 = 16 + (top - 1) * 100 / total;
163*7c478bd9Sstevel@tonic-gate 			p2 = 16 + (bot - 1) * 100 / total;
164*7c478bd9Sstevel@tonic-gate 			if (p2 > 116) {
165*7c478bd9Sstevel@tonic-gate 				p2 = 116;
166*7c478bd9Sstevel@tonic-gate 			}
167*7c478bd9Sstevel@tonic-gate 			if (p1 < 16) {
168*7c478bd9Sstevel@tonic-gate 				p1 = 16;
169*7c478bd9Sstevel@tonic-gate 			}
170*7c478bd9Sstevel@tonic-gate 			/*
171*7c478bd9Sstevel@tonic-gate 			 * don't send ^S or ^Q to avoid hanging a layer using
172*7c478bd9Sstevel@tonic-gate 			 * cu(1)
173*7c478bd9Sstevel@tonic-gate 			 */
174*7c478bd9Sstevel@tonic-gate 			if (p1 == ctrl('Q') || p1 == ctrl('S')) {
175*7c478bd9Sstevel@tonic-gate 				++p1;
176*7c478bd9Sstevel@tonic-gate 			}
177*7c478bd9Sstevel@tonic-gate 			if (p2 == ctrl('Q') || p2 == ctrl('S')) {
178*7c478bd9Sstevel@tonic-gate 				++p2;
179*7c478bd9Sstevel@tonic-gate 			}
180*7c478bd9Sstevel@tonic-gate 		} else {
181*7c478bd9Sstevel@tonic-gate 			p1 = p2 = 16;
182*7c478bd9Sstevel@tonic-gate 		}
183*7c478bd9Sstevel@tonic-gate 		(void) printf("\033W%c%c", p1, p2);
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate }
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate /* translate a mouse click or sweep to a selection */
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate int
mouseselection(MOUSEEVENT * p,int offset,int maxselection)190*7c478bd9Sstevel@tonic-gate mouseselection(MOUSEEVENT *p, int offset, int maxselection)
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate 	int	i;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	i = p->y1 - offset;
195*7c478bd9Sstevel@tonic-gate 	if (i < 0) {
196*7c478bd9Sstevel@tonic-gate 		i = 0;
197*7c478bd9Sstevel@tonic-gate 	} else if (i >= maxselection) {
198*7c478bd9Sstevel@tonic-gate 		i = maxselection - 1;
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 	return (i);
201*7c478bd9Sstevel@tonic-gate }
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate /* get the mouse event */
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate MOUSEEVENT *
getmouseevent(void)206*7c478bd9Sstevel@tonic-gate getmouseevent(void)
207*7c478bd9Sstevel@tonic-gate {
208*7c478bd9Sstevel@tonic-gate 	static	MOUSEEVENT	m;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	if (mouse == EMACSTERM) {
211*7c478bd9Sstevel@tonic-gate 		switch (mygetch()) {
212*7c478bd9Sstevel@tonic-gate 		case ctrl('_'):		/* click */
213*7c478bd9Sstevel@tonic-gate 			if ((m.button = mygetch()) == '0') { /* if scroll bar */
214*7c478bd9Sstevel@tonic-gate 				m.percent = getpercent();
215*7c478bd9Sstevel@tonic-gate 			} else {
216*7c478bd9Sstevel@tonic-gate 				m.x1 = getcoordinate();
217*7c478bd9Sstevel@tonic-gate 				m.y1 = getcoordinate();
218*7c478bd9Sstevel@tonic-gate 				m.x2 = m.y2 = -1;
219*7c478bd9Sstevel@tonic-gate 			}
220*7c478bd9Sstevel@tonic-gate 			break;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 		case ctrl(']'):		/* sweep */
223*7c478bd9Sstevel@tonic-gate 			m.button = mygetch();
224*7c478bd9Sstevel@tonic-gate 			m.x1 = getcoordinate();
225*7c478bd9Sstevel@tonic-gate 			m.y1 = getcoordinate();
226*7c478bd9Sstevel@tonic-gate 			m.x2 = getcoordinate();
227*7c478bd9Sstevel@tonic-gate 			m.y2 = getcoordinate();
228*7c478bd9Sstevel@tonic-gate 			break;
229*7c478bd9Sstevel@tonic-gate 		default:
230*7c478bd9Sstevel@tonic-gate 			return (NULL);
231*7c478bd9Sstevel@tonic-gate 		}
232*7c478bd9Sstevel@tonic-gate 		return (&m);
233*7c478bd9Sstevel@tonic-gate 	}
234*7c478bd9Sstevel@tonic-gate 	return (NULL);
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate /* get a row or column coordinate from a mouse button click or sweep */
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate int
getcoordinate(void)240*7c478bd9Sstevel@tonic-gate getcoordinate(void)
241*7c478bd9Sstevel@tonic-gate {
242*7c478bd9Sstevel@tonic-gate 	int  c, next;
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	c = mygetch();
245*7c478bd9Sstevel@tonic-gate 	next = 0;
246*7c478bd9Sstevel@tonic-gate 	if (c == ctrl('A')) {
247*7c478bd9Sstevel@tonic-gate 		next = 95;
248*7c478bd9Sstevel@tonic-gate 		c = mygetch();
249*7c478bd9Sstevel@tonic-gate 	}
250*7c478bd9Sstevel@tonic-gate 	if (c < ' ') {
251*7c478bd9Sstevel@tonic-gate 		return (0);
252*7c478bd9Sstevel@tonic-gate 	}
253*7c478bd9Sstevel@tonic-gate 	return (next + c - ' ');
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate /* get a percentage */
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate int
getpercent(void)259*7c478bd9Sstevel@tonic-gate getpercent(void)
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	int c;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	c = mygetch();
264*7c478bd9Sstevel@tonic-gate 	if (c < 16) {
265*7c478bd9Sstevel@tonic-gate 		return (0);
266*7c478bd9Sstevel@tonic-gate 	}
267*7c478bd9Sstevel@tonic-gate 	if (c > 120) {
268*7c478bd9Sstevel@tonic-gate 		return (100);
269*7c478bd9Sstevel@tonic-gate 	}
270*7c478bd9Sstevel@tonic-gate 	return (c - 16);
271*7c478bd9Sstevel@tonic-gate }
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate /* update the window label area */
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate int
labelarea(char * s)276*7c478bd9Sstevel@tonic-gate labelarea(char *s)
277*7c478bd9Sstevel@tonic-gate {
278*7c478bd9Sstevel@tonic-gate 	static	BOOL	labelon;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	switch (mouse) {
281*7c478bd9Sstevel@tonic-gate 	case EMACSTERM:
282*7c478bd9Sstevel@tonic-gate 		if (labelon == NO) {
283*7c478bd9Sstevel@tonic-gate 			labelon = YES;
284*7c478bd9Sstevel@tonic-gate 			(void) printf("\033L1");	/* force it on */
285*7c478bd9Sstevel@tonic-gate 		}
286*7c478bd9Sstevel@tonic-gate 		(void) printf("\033L!%s!", s);
287*7c478bd9Sstevel@tonic-gate 		return (1);
288*7c478bd9Sstevel@tonic-gate 	case MYX:
289*7c478bd9Sstevel@tonic-gate 		(void) printf("\033[?%dv%s", strlen(s), s);
290*7c478bd9Sstevel@tonic-gate 		return (1);
291*7c478bd9Sstevel@tonic-gate 	case NONE:
292*7c478bd9Sstevel@tonic-gate 	case PC7300:
293*7c478bd9Sstevel@tonic-gate 	default:
294*7c478bd9Sstevel@tonic-gate 		return (0);	/* no label area */
295*7c478bd9Sstevel@tonic-gate 	}
296*7c478bd9Sstevel@tonic-gate }
297