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