xref: /illumos-gate/usr/src/lib/libcurses/screen/mouse.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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 /*LINTLIBRARY*/
41 
42 #include <sys/types.h>
43 #include "curses_inc.h"
44 
45 int
46 mouse_set(long int mbe)
47 {
48 	if (get_mouse) {
49 		SP->_trap_mbe = mbe;
50 		(void) tputs(tparm_p1(get_mouse, mbe), 1, _outch);
51 		(void) fflush(SP->term_file);
52 		return (OK);
53 	}
54 	return (ERR);
55 }
56 
57 int
58 mouse_on(long int mbe)
59 {
60 	if (get_mouse) {
61 		SP->_trap_mbe |= mbe;
62 		(void) tputs(tparm_p1(get_mouse, (long) SP->_trap_mbe),
63 		    1, _outch);
64 		(void) fflush(SP->term_file);
65 		return (OK);
66 	}
67 	return (ERR);
68 }
69 
70 int
71 mouse_off(long int mbe)
72 {
73 	if (get_mouse) {
74 		SP->_trap_mbe &= ~mbe;
75 		(void) tputs(tparm_p1(get_mouse, (long) SP->_trap_mbe),
76 		    1, _outch);
77 		(void) fflush(SP->term_file);
78 		return (OK);
79 	}
80 	return (ERR);
81 }
82 
83 
84 int
85 request_mouse_pos(void)
86 {
87 	int i;
88 
89 	if (req_mouse_pos) {
90 		(void) tputs(req_mouse_pos, 1, _outch);
91 		(void) fflush(SP->term_file);
92 
93 		/* we now must wait for report of mouse position. How do  */
94 		/* we know that this is mouse position report an not any- */
95 		/* thing else?  thetch() returns KEY_MOUSE and the status */
96 		/* off all the buttons remains unchanged.		  */
97 		/* just to avoid going into infinite loop, we have a	  */
98 		/* counter.  if 1000 responses won't have what we need,	  */
99 		/* we'll return error					  */
100 
101 		for (i = 0; i < 1000; i++) {
102 			if ((tgetch(1) == KEY_MOUSE) && MOUSE_POS_REPORT)
103 				break;
104 		}
105 		if (i == 1000)
106 			return (ERR);
107 		return (OK);
108 	}
109 	return (ERR);
110 }
111 
112 void
113 wmouse_position(WINDOW *win, int *x, int *y)
114 {
115 	/* mouse pointer outside the window, store -1's into x and y */
116 
117 	if (win->_begy > MOUSE_Y_POS || win->_begx > MOUSE_X_POS ||
118 	    win->_begy+win->_maxy < MOUSE_Y_POS ||
119 	    win->_begx+win->_maxx < MOUSE_X_POS) {
120 		*x = -1;  *y = -1;
121 	} else {
122 		*x = MOUSE_X_POS - win->_begx;
123 		*y = MOUSE_Y_POS - win->_begy;
124 	}
125 }
126 
127 
128 int
129 map_button(unsigned long a)
130 {
131 	SP->_map_mbe_to_key = a;
132 	return (OK);
133 }
134 
135 
136 unsigned long
137 getmouse(void)
138 {
139 	return (SP->_trap_mbe);
140 }
141 
142 
143 unsigned long
144 getbmap(void)
145 {
146 	return (SP->_map_mbe_to_key);
147 }
148