xref: /titanic_44/usr/src/lib/libcurses/screen/mouse.c (revision 49b225e1cfa7bbf7738d4df0a03f18e3283426eb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include <sys/types.h>
45 #include "curses_inc.h"
46 
47 int
48 mouse_set(long int mbe)
49 {
50 	if (get_mouse) {
51 		SP->_trap_mbe = mbe;
52 		(void) tputs(tparm_p1(get_mouse, mbe), 1, _outch);
53 		(void) fflush(SP->term_file);
54 		return (OK);
55 	}
56 	return (ERR);
57 }
58 
59 int
60 mouse_on(long int mbe)
61 {
62 	if (get_mouse) {
63 		SP->_trap_mbe |= mbe;
64 		(void) tputs(tparm_p1(get_mouse, (long) SP->_trap_mbe),
65 		    1, _outch);
66 		(void) fflush(SP->term_file);
67 		return (OK);
68 	}
69 	return (ERR);
70 }
71 
72 int
73 mouse_off(long int mbe)
74 {
75 	if (get_mouse) {
76 		SP->_trap_mbe &= ~mbe;
77 		(void) tputs(tparm_p1(get_mouse, (long) SP->_trap_mbe),
78 		    1, _outch);
79 		(void) fflush(SP->term_file);
80 		return (OK);
81 	}
82 	return (ERR);
83 }
84 
85 
86 int
87 request_mouse_pos(void)
88 {
89 	int i;
90 
91 	if (req_mouse_pos) {
92 		(void) tputs(req_mouse_pos, 1, _outch);
93 		(void) fflush(SP->term_file);
94 
95 		/* we now must wait for report of mouse position. How do  */
96 		/* we know that this is mouse position report an not any- */
97 		/* thing else?  thetch() returns KEY_MOUSE and the status */
98 		/* off all the buttons remains unchanged.		  */
99 		/* just to avoid going into infinite loop, we have a	  */
100 		/* counter.  if 1000 responses won't have what we need,	  */
101 		/* we'll return error					  */
102 
103 		for (i = 0; i < 1000; i++) {
104 			if ((tgetch(1) == KEY_MOUSE) && MOUSE_POS_REPORT)
105 				break;
106 		}
107 		if (i == 1000)
108 			return (ERR);
109 		return (OK);
110 	}
111 	return (ERR);
112 }
113 
114 void
115 wmouse_position(WINDOW *win, int *x, int *y)
116 {
117 	/* mouse pointer outside the window, store -1's into x and y */
118 
119 	if (win->_begy > MOUSE_Y_POS || win->_begx > MOUSE_X_POS ||
120 	    win->_begy+win->_maxy < MOUSE_Y_POS ||
121 	    win->_begx+win->_maxx < MOUSE_X_POS) {
122 		*x = -1;  *y = -1;
123 	} else {
124 		*x = MOUSE_X_POS - win->_begx;
125 		*y = MOUSE_Y_POS - win->_begy;
126 	}
127 }
128 
129 
130 int
131 map_button(unsigned long a)
132 {
133 	SP->_map_mbe_to_key = a;
134 	return (OK);
135 }
136 
137 
138 unsigned long
139 getmouse(void)
140 {
141 	return (SP->_trap_mbe);
142 }
143 
144 
145 unsigned long
146 getbmap(void)
147 {
148 	return (SP->_map_mbe_to_key);
149 }
150