1 /*===========================================================================
2 Copyright (c) 1998-2000, The Santa Cruz Operation
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 *Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 *Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
15 *Neither name of The Santa Cruz Operation nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20 IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 =========================================================================*/
33 /* cscope - interactive C symbol cross-reference
40 BOOL mouse = NO; /* mouse interface */
42 #ifdef UNIXPC /* build command requires #ifdef instead of #if */
43 #include <sys/window.h>
44 BOOL unixpcmouse = NO; /* running with a mouse on the Unix PC? */
45 static int uw_hs, uw_vs; /* character height and width */
48 static char const rcsid[] = "$Id: mouse.c,v 1.4 2001/07/05 14:31:00 broeker Exp $";
50 typedef struct { /* menu */
55 static MENU mainmenu[] = { /* main menu */
56 {"Send", "##\033s##\r"},
67 static MENU changemenu[] = { /* change mode menu */
71 {"No Change", "\033"},
78 static MENU *loaded; /* menu loaded */
79 static BOOL emacsviterm = NO; /* terminal type */
81 static void loadmenu(MENU *menu);
82 static int getcoordinate(void);
83 static int getpercent(void);
85 /* see if there is a mouse interface */
92 /* see if this is emacsterm or viterm */
93 term = mygetenv("TERM", "");
94 if (strcmp(term, "emacsterm") == 0 ||
95 strcmp(term, "viterm") == 0) {
99 /* the MOUSE enviroment variable is for 5620 terminal programs that have
100 mouse support but the TERM environment variable is the same as a
101 terminal without a mouse, such as myx */
102 else if (strcmp(mygetenv("MOUSE", ""), "myx") == 0) {
106 else if (strcmp(term,"s4") == 0 ||
107 strcmp(term,"s120") == 0 ||
108 strcmp(term,"s90") == 0) {
110 struct uwdata uwd; /* Window data structure */
111 struct umdata umd; /* Mouse data structure */
113 /* Ask for character size info */
115 retval = ioctl(1,WIOCGETD,&uwd);
116 if(retval || uwd.uw_hs <= 0 || uwd.uw_vs <= 0) {
117 /**************************************************
118 * something wrong with the kernel, so fake it...
119 **************************************************/
120 if(!strcmp(term,"s4")) {
130 /* Kernel is working and knows about this font */
135 /**************************************************
136 * Now turn on mouse reporting so we can actually
137 * make use of all this stuff.
138 **************************************************/
139 if((retval = ioctl(1,WIOCGETMOUSE,&umd)) != -1) {
140 umd.um_flags= MSDOWN+MSUP;
141 ioctl(1,WIOCSETMOUSE,&umd);
151 /* load the correct mouse menu */
157 if (changing == YES) {
158 loadmenu(changemenu);
166 /* download a menu */
173 if (emacsviterm == YES) {
175 (void) printf("\033V1"); /* display the scrollbar */
176 (void) printf("\033M0@%s@%s@", menu[0].text, menu[0].value);
177 for (i = 1; menu[i].text != NULL; ++i) {
178 (void) printf("\033M@%s@%s@", menu[i].text, menu[i].value);
185 (void) printf("\033[6;1X\033[9;1X");
186 for (i = 0; menu[i].text != NULL; ++i) {
187 len = strlen(menu[i].text);
188 (void) printf("\033[%d;%dx%s%s", len,
189 (int) (len + strlen(menu[i].value)),
190 menu[i].text, menu[i].value);
194 (void) fflush(stdout);
197 /* reinitialize the mouse in case curses changed the attributes */
202 if (emacsviterm == YES) {
204 /* enable the mouse click and sweep coordinate control sequence */
205 /* and switch to menu 2 */
206 (void) printf("\033{2\033#2");
207 (void) fflush(stdout);
211 /* restore the mouse attributes */
218 if (loaded != NULL) { /* only true for myx */
220 /* remove the mouse menu */
221 (void) printf("\033[6;0X\033[9;0X");
222 for (i = 0; loaded[i].text != NULL; ++i) {
223 (void) printf("\033[0;0x");
229 /* draw the scrollbar */
232 drawscrollbar(int top, int bot)
236 if (emacsviterm == YES) {
238 p1 = 16 + (top - 1) * 100 / totallines;
239 p2 = 16 + (bot - 1) * 100 / totallines;
246 /* don't send ^S or ^Q because it will hang a layer using cu(1) */
247 if (p1 == ctrl('Q') || p1 == ctrl('S')) {
250 if (p2 == ctrl('Q') || p2 == ctrl('S')) {
257 (void) printf("\033W%c%c", p1, p2);
261 /* get the mouse information */
264 getmouseaction(char leading_char)
270 if(unixpcmouse == YES && leading_char == ESC) {
272 /* Called if cscope received an ESC character. See if it is
273 * a mouse report and if so, decipher it. A mouse report
274 * looks like: "<ESC>[?xx;yy;b;rM"
276 int x = 0, y = 0, button = 0, reason = 0;
279 /* Get a mouse report. The form is: XX;YY;B;RM where
280 * XX is 1,2, or 3 decimal digits with the X pixel position.
281 * Similarly for YY. B is a single decimal digit with the
282 * button number (4 for one, 2 for two, and 1 for three).
283 * R is the reason for the mouse report.
285 * In general, the input is read until the mouse report has
286 * been completely read in or we have discovered that this
287 * escape sequence is NOT a mouse report. In the latter case
288 * return the last character read to the input stream with
292 /* Check for "[?" being next 2 chars */
293 if(((i = mygetch()) != '[') || ((i = mygetch()) != '?')) {
298 /* Grab the X position (in pixels) */
299 while(isdigit(i = mygetch())) {
300 x = (x*10) + (i - '0');
304 return(NULL); /* not a mouse report after all */
307 /* Grab the Y position (in pixels) */
308 while(isdigit(i = mygetch())) {
309 y = (y*10) + (i - '0');
316 /* Get which button */
317 if((button = mygetch()) > '4') {
321 if((i = mygetch()) != ';') {
326 /* Get the reason for this mouse report */
327 if((reason = mygetch()) > '8') {
332 /* sequence should terminate with an 'M' */
333 if((i = mygetch()) != 'M') {
339 /* OK. We get a mouse report whenever a button is depressed
340 * or released. Let's ignore the report whenever the button
341 * is depressed until when I am ready to implement sweeping.
344 return(NULL); /* '2' means button is released */
347 /************************************************************
348 * Always indicate button 1 irregardless of which button was
350 ************************************************************/
353 /************************************************************
354 * Convert pixel coordinates to line and column coords.
355 * The height and width are obtained using an ioctl() call
356 * in mouseinit(). This assumes that variable width chars
357 * are not being used ('though it would probably work anyway).
358 ************************************************************/
360 m.x1 = x/uw_hs; /* pixel/horizontal_spacing */
361 m.y1 = y/uw_vs; /* pixel/vertical_spacing */
363 /* "null" out the other fields */
364 m.percent = m.x2 = m.y2 = -1;
367 #endif /* not UNIXPC */
369 if (mouse == YES && leading_char == ctrl('X')) {
372 case ctrl('_'): /* click */
373 if ((m.button = mygetch()) == '0') { /* if scrollbar */
374 m.percent = getpercent();
377 m.x1 = getcoordinate();
378 m.y1 = getcoordinate();
383 case ctrl(']'): /* sweep */
384 m.button = mygetch();
385 m.x1 = getcoordinate();
386 m.y1 = getcoordinate();
387 m.x2 = getcoordinate();
388 m.y2 = getcoordinate();
399 /* get a row or column coordinate from a mouse button click or sweep */
408 if (c == ctrl('A')) {
415 return (next + c - ' ');
418 /* get a percentage */