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 /* common mouse interface functions */
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate #include <stdio.h> /* NULL */
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h> /* NULL */
35*7c478bd9Sstevel@tonic-gate #include <string.h> /* NULL */
36*7c478bd9Sstevel@tonic-gate #include <ctype.h> /* isdigit */
37*7c478bd9Sstevel@tonic-gate #include "global.h"
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate #define ctrl(x) (x & 037)
40*7c478bd9Sstevel@tonic-gate
41*7c478bd9Sstevel@tonic-gate MOUSETYPE mouse;
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate static MOUSEMENU *loadedmenu;
44*7c478bd9Sstevel@tonic-gate static BOOL changemenu = YES;
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate /* see if there is a mouse interface */
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate void
initmouse(void)49*7c478bd9Sstevel@tonic-gate initmouse(void)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate char *s, *term;
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate if ((term = getenv("TERM")) == NULL) {
54*7c478bd9Sstevel@tonic-gate return;
55*7c478bd9Sstevel@tonic-gate }
56*7c478bd9Sstevel@tonic-gate if (strcmp(term, "emacsterm") == 0 || strcmp(term, "viterm") == 0) {
57*7c478bd9Sstevel@tonic-gate mouse = EMACSTERM;
58*7c478bd9Sstevel@tonic-gate } else if ((s = getenv("MOUSE")) != NULL && strcmp(s, "myx") == 0) {
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate * the MOUSE enviroment variable is for 5620 terminal
61*7c478bd9Sstevel@tonic-gate * programs that have mouse support but the TERM environment
62*7c478bd9Sstevel@tonic-gate * variable is the same as a terminal without a mouse, such
63*7c478bd9Sstevel@tonic-gate * as myx
64*7c478bd9Sstevel@tonic-gate */
65*7c478bd9Sstevel@tonic-gate mouse = MYX;
66*7c478bd9Sstevel@tonic-gate }
67*7c478bd9Sstevel@tonic-gate if ((s = getenv("MOUSEMENU")) != NULL && strcmp(s, "none") == 0) {
68*7c478bd9Sstevel@tonic-gate changemenu = NO;
69*7c478bd9Sstevel@tonic-gate }
70*7c478bd9Sstevel@tonic-gate initmenu();
71*7c478bd9Sstevel@tonic-gate }
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate /* reinitialize the mouse in case curses changed the attributes */
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate void
reinitmouse(void)76*7c478bd9Sstevel@tonic-gate reinitmouse(void)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate if (mouse == EMACSTERM) {
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate /*
81*7c478bd9Sstevel@tonic-gate * enable the mouse click and sweep coordinate control
82*7c478bd9Sstevel@tonic-gate * sequence
83*7c478bd9Sstevel@tonic-gate */
84*7c478bd9Sstevel@tonic-gate (void) printf("\033{2");
85*7c478bd9Sstevel@tonic-gate if (changemenu) {
86*7c478bd9Sstevel@tonic-gate (void) printf("\033#2"); /* switch to menu 2 */
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate (void) fflush(stdout);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate
92*7c478bd9Sstevel@tonic-gate /* restore any original mouse attributes not handled by terminfo */
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate void
cleanupmouse(void)95*7c478bd9Sstevel@tonic-gate cleanupmouse(void)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate int i;
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate if (mouse == MYX && loadedmenu != NULL) {
100*7c478bd9Sstevel@tonic-gate /* remove the mouse menu */
101*7c478bd9Sstevel@tonic-gate (void) printf("\033[6;0X\033[9;0X");
102*7c478bd9Sstevel@tonic-gate for (i = 0; loadedmenu[i].text != NULL; ++i) {
103*7c478bd9Sstevel@tonic-gate (void) printf("\033[0;0x");
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate loadedmenu = NULL;
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate
109*7c478bd9Sstevel@tonic-gate /* download a mouse menu */
110*7c478bd9Sstevel@tonic-gate
111*7c478bd9Sstevel@tonic-gate void
downloadmenu(MOUSEMENU * menu)112*7c478bd9Sstevel@tonic-gate downloadmenu(MOUSEMENU *menu)
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate int i;
115*7c478bd9Sstevel@tonic-gate int len;
116*7c478bd9Sstevel@tonic-gate
117*7c478bd9Sstevel@tonic-gate switch (mouse) {
118*7c478bd9Sstevel@tonic-gate case EMACSTERM:
119*7c478bd9Sstevel@tonic-gate reinitmouse();
120*7c478bd9Sstevel@tonic-gate (void) printf("\033V1"); /* display the scroll bar */
121*7c478bd9Sstevel@tonic-gate if (changemenu) {
122*7c478bd9Sstevel@tonic-gate (void) printf("\033M0@%s@%s@", menu[0].text,
123*7c478bd9Sstevel@tonic-gate menu[0].value);
124*7c478bd9Sstevel@tonic-gate for (i = 1; menu[i].text != NULL; ++i) {
125*7c478bd9Sstevel@tonic-gate (void) printf("\033M@%s@%s@", menu[i].text,
126*7c478bd9Sstevel@tonic-gate menu[i].value);
127*7c478bd9Sstevel@tonic-gate }
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate (void) fflush(stdout);
130*7c478bd9Sstevel@tonic-gate break;
131*7c478bd9Sstevel@tonic-gate case MYX:
132*7c478bd9Sstevel@tonic-gate if (changemenu) {
133*7c478bd9Sstevel@tonic-gate cleanupmouse();
134*7c478bd9Sstevel@tonic-gate (void) printf("\033[6;1X\033[9;1X");
135*7c478bd9Sstevel@tonic-gate for (i = 0; menu[i].text != NULL; ++i) {
136*7c478bd9Sstevel@tonic-gate len = strlen(menu[i].text);
137*7c478bd9Sstevel@tonic-gate (void) printf("\033[%d;%dx%s%s", len,
138*7c478bd9Sstevel@tonic-gate len + strlen(menu[i].value),
139*7c478bd9Sstevel@tonic-gate menu[i].text, menu[i].value);
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate (void) fflush(stdout);
142*7c478bd9Sstevel@tonic-gate loadedmenu = menu;
143*7c478bd9Sstevel@tonic-gate }
144*7c478bd9Sstevel@tonic-gate break;
145*7c478bd9Sstevel@tonic-gate case NONE:
146*7c478bd9Sstevel@tonic-gate case PC7300:
147*7c478bd9Sstevel@tonic-gate break;
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate
151*7c478bd9Sstevel@tonic-gate /* draw the scroll bar */
152*7c478bd9Sstevel@tonic-gate
153*7c478bd9Sstevel@tonic-gate void
drawscrollbar(int top,int bot,int total)154*7c478bd9Sstevel@tonic-gate drawscrollbar(int top, int bot, int total)
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate int p1, p2;
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate if (mouse == EMACSTERM) {
159*7c478bd9Sstevel@tonic-gate if (bot > top && total > 0) {
160*7c478bd9Sstevel@tonic-gate p1 = 16 + (top - 1) * 100 / total;
161*7c478bd9Sstevel@tonic-gate p2 = 16 + (bot - 1) * 100 / total;
162*7c478bd9Sstevel@tonic-gate if (p2 > 116) {
163*7c478bd9Sstevel@tonic-gate p2 = 116;
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate if (p1 < 16) {
166*7c478bd9Sstevel@tonic-gate p1 = 16;
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate * don't send ^S or ^Q to avoid hanging a layer using
170*7c478bd9Sstevel@tonic-gate * cu(1)
171*7c478bd9Sstevel@tonic-gate */
172*7c478bd9Sstevel@tonic-gate if (p1 == ctrl('Q') || p1 == ctrl('S')) {
173*7c478bd9Sstevel@tonic-gate ++p1;
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate if (p2 == ctrl('Q') || p2 == ctrl('S')) {
176*7c478bd9Sstevel@tonic-gate ++p2;
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate } else {
179*7c478bd9Sstevel@tonic-gate p1 = p2 = 16;
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate (void) printf("\033W%c%c", p1, p2);
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gate /* translate a mouse click or sweep to a selection */
186*7c478bd9Sstevel@tonic-gate
187*7c478bd9Sstevel@tonic-gate int
mouseselection(MOUSEEVENT * p,int offset,int maxselection)188*7c478bd9Sstevel@tonic-gate mouseselection(MOUSEEVENT *p, int offset, int maxselection)
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate int i;
191*7c478bd9Sstevel@tonic-gate
192*7c478bd9Sstevel@tonic-gate i = p->y1 - offset;
193*7c478bd9Sstevel@tonic-gate if (i < 0) {
194*7c478bd9Sstevel@tonic-gate i = 0;
195*7c478bd9Sstevel@tonic-gate } else if (i >= maxselection) {
196*7c478bd9Sstevel@tonic-gate i = maxselection - 1;
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate return (i);
199*7c478bd9Sstevel@tonic-gate }
200*7c478bd9Sstevel@tonic-gate
201*7c478bd9Sstevel@tonic-gate /* get the mouse event */
202*7c478bd9Sstevel@tonic-gate
203*7c478bd9Sstevel@tonic-gate MOUSEEVENT *
getmouseevent(void)204*7c478bd9Sstevel@tonic-gate getmouseevent(void)
205*7c478bd9Sstevel@tonic-gate {
206*7c478bd9Sstevel@tonic-gate static MOUSEEVENT m;
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate if (mouse == EMACSTERM) {
209*7c478bd9Sstevel@tonic-gate switch (mygetch()) {
210*7c478bd9Sstevel@tonic-gate case ctrl('_'): /* click */
211*7c478bd9Sstevel@tonic-gate if ((m.button = mygetch()) == '0') { /* if scroll bar */
212*7c478bd9Sstevel@tonic-gate m.percent = getpercent();
213*7c478bd9Sstevel@tonic-gate } else {
214*7c478bd9Sstevel@tonic-gate m.x1 = getcoordinate();
215*7c478bd9Sstevel@tonic-gate m.y1 = getcoordinate();
216*7c478bd9Sstevel@tonic-gate m.x2 = m.y2 = -1;
217*7c478bd9Sstevel@tonic-gate }
218*7c478bd9Sstevel@tonic-gate break;
219*7c478bd9Sstevel@tonic-gate
220*7c478bd9Sstevel@tonic-gate case ctrl(']'): /* sweep */
221*7c478bd9Sstevel@tonic-gate m.button = mygetch();
222*7c478bd9Sstevel@tonic-gate m.x1 = getcoordinate();
223*7c478bd9Sstevel@tonic-gate m.y1 = getcoordinate();
224*7c478bd9Sstevel@tonic-gate m.x2 = getcoordinate();
225*7c478bd9Sstevel@tonic-gate m.y2 = getcoordinate();
226*7c478bd9Sstevel@tonic-gate break;
227*7c478bd9Sstevel@tonic-gate default:
228*7c478bd9Sstevel@tonic-gate return (NULL);
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate return (&m);
231*7c478bd9Sstevel@tonic-gate }
232*7c478bd9Sstevel@tonic-gate return (NULL);
233*7c478bd9Sstevel@tonic-gate }
234*7c478bd9Sstevel@tonic-gate
235*7c478bd9Sstevel@tonic-gate /* get a row or column coordinate from a mouse button click or sweep */
236*7c478bd9Sstevel@tonic-gate
237*7c478bd9Sstevel@tonic-gate int
getcoordinate(void)238*7c478bd9Sstevel@tonic-gate getcoordinate(void)
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate int c, next;
241*7c478bd9Sstevel@tonic-gate
242*7c478bd9Sstevel@tonic-gate c = mygetch();
243*7c478bd9Sstevel@tonic-gate next = 0;
244*7c478bd9Sstevel@tonic-gate if (c == ctrl('A')) {
245*7c478bd9Sstevel@tonic-gate next = 95;
246*7c478bd9Sstevel@tonic-gate c = mygetch();
247*7c478bd9Sstevel@tonic-gate }
248*7c478bd9Sstevel@tonic-gate if (c < ' ') {
249*7c478bd9Sstevel@tonic-gate return (0);
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate return (next + c - ' ');
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate
254*7c478bd9Sstevel@tonic-gate /* get a percentage */
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate int
getpercent(void)257*7c478bd9Sstevel@tonic-gate getpercent(void)
258*7c478bd9Sstevel@tonic-gate {
259*7c478bd9Sstevel@tonic-gate int c;
260*7c478bd9Sstevel@tonic-gate
261*7c478bd9Sstevel@tonic-gate c = mygetch();
262*7c478bd9Sstevel@tonic-gate if (c < 16) {
263*7c478bd9Sstevel@tonic-gate return (0);
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate if (c > 120) {
266*7c478bd9Sstevel@tonic-gate return (100);
267*7c478bd9Sstevel@tonic-gate }
268*7c478bd9Sstevel@tonic-gate return (c - 16);
269*7c478bd9Sstevel@tonic-gate }
270*7c478bd9Sstevel@tonic-gate
271*7c478bd9Sstevel@tonic-gate /* update the window label area */
272*7c478bd9Sstevel@tonic-gate
273*7c478bd9Sstevel@tonic-gate int
labelarea(char * s)274*7c478bd9Sstevel@tonic-gate labelarea(char *s)
275*7c478bd9Sstevel@tonic-gate {
276*7c478bd9Sstevel@tonic-gate static BOOL labelon;
277*7c478bd9Sstevel@tonic-gate
278*7c478bd9Sstevel@tonic-gate switch (mouse) {
279*7c478bd9Sstevel@tonic-gate case EMACSTERM:
280*7c478bd9Sstevel@tonic-gate if (labelon == NO) {
281*7c478bd9Sstevel@tonic-gate labelon = YES;
282*7c478bd9Sstevel@tonic-gate (void) printf("\033L1"); /* force it on */
283*7c478bd9Sstevel@tonic-gate }
284*7c478bd9Sstevel@tonic-gate (void) printf("\033L!%s!", s);
285*7c478bd9Sstevel@tonic-gate return (1);
286*7c478bd9Sstevel@tonic-gate case MYX:
287*7c478bd9Sstevel@tonic-gate (void) printf("\033[?%dv%s", strlen(s), s);
288*7c478bd9Sstevel@tonic-gate return (1);
289*7c478bd9Sstevel@tonic-gate case NONE:
290*7c478bd9Sstevel@tonic-gate case PC7300:
291*7c478bd9Sstevel@tonic-gate default:
292*7c478bd9Sstevel@tonic-gate return (0); /* no label area */
293*7c478bd9Sstevel@tonic-gate }
294*7c478bd9Sstevel@tonic-gate }
295