Blob


1 .TH MOUSE 3
2 .SH NAME
3 initmouse, readmouse, closemouse, moveto, cursorswitch, getrect, drawgetrect, menuhit, setcursor \- mouse control
4 .SH SYNOPSIS
5 .nf
6 .B
7 #include <u.h>
8 .B
9 #include <libc.h>
10 .B
11 #include <draw.h>
12 .B
13 #include <thread.h>
14 .B
15 #include <mouse.h>
16 .B
17 #include <cursor.h>
18 .PP
19 .B
20 Mousectl *initmouse(char *file, Image *i)
21 .PP
22 .B
23 int readmouse(Mousectl *mc)
24 .PP
25 .B
26 int atomouse();
27 .PP
28 .B
29 void closemouse(Mousectl *mc)
30 .PP
31 .B
32 void moveto(Mousectl *mc, Point pt)
33 .PP
34 .B
35 void setcursor(Mousectl *mc, Cursor *c)
36 .PP
37 .B
38 Rectangle getrect(int but, Mousectl *mc)
39 .PP
40 .B
41 void drawgetrect(Rectangle r, int up)
42 .PP
43 .B
44 int menuhit(int but, Mousectl *mc, Menu *menu, Screen *scr)
45 .fi
46 .SH DESCRIPTION
47 These functions access and control a mouse in a multi-threaded environment.
48 They use the message-passing
49 .B Channel
50 interface in the threads library
51 (see
52 .IR thread (3));
53 programs that wish a more event-driven, single-threaded approach should use
54 .IR event (3).
55 .PP
56 The state of the mouse is recorded in a structure,
57 .BR Mouse ,
58 defined in
59 .BR <mouse.h> :
60 .IP
61 .EX
62 .ta 6n +\w'Rectangle 'u +\w'buttons; 'u
63 typedef struct Mouse Mouse;
64 struct Mouse
65 {
66 int buttons; /* bit array: LMR=124 */
67 Point xy;
68 ulong msec;
69 };
70 .EE
71 .PP
72 The
73 .B Point
74 .B xy
75 records the position of the cursor,
76 .B buttons
77 the state of the buttons (three bits representing, from bit 0 up, the buttons from left to right,
78 0 if the button is released, 1 if it is pressed),
79 and
80 .BR msec ,
81 a millisecond time stamp.
82 .PP
83 The routine
84 .B initmouse
85 returns a structure through which one may access the mouse:
86 .IP
87 .EX
88 typedef struct Mousectl Mousectl;
89 struct Mousectl
90 {
91 Mouse;
92 Channel *c; /* chan(Mouse)[16] */
93 Channel *resizec; /* chan(int)[2] */
95 char *file;
96 int mfd; /* to mouse file */
97 int cfd; /* to cursor file */
98 int pid; /* of slave proc */
99 Image* image; /* of associated window/display */
100 };
101 .EE
102 .PP
103 The arguments to
104 .I initmouse
105 are a
106 .I file
107 naming the device file connected to the mouse and an
108 .I Image
109 (see
110 .IR draw (3))
111 on which the mouse will be visible.
112 Typically the file is
113 nil,
114 which requests the default
115 .BR /dev/mouse ;
116 and the image is the window in which the program is running, held in the variable
117 .B screen
118 after a call to
119 .IR initdraw .
120 .PP
121 Once the
122 .B Mousectl
123 is set up,
124 mouse motion will be reported by messages of type
125 .B Mouse
126 sent on the
127 .B Channel
128 .BR Mousectl.c .
129 Typically, a message will be sent every time a read of
130 .B /dev/mouse
131 succeeds, which is every time the state of the mouse changes.
132 .PP
133 When the window is resized, a message is sent on
134 .BR Mousectl.resizec .
135 The actual value sent may be discarded; the receipt of the message
136 tells the program that it should call
137 .B getwindow
138 (see
139 .IR graphics (3))
140 to reconnect to the window.
141 .PP
142 .I Readmouse
143 updates the
144 .B Mouse
145 structure held in the
146 .BR Mousectl ,
147 blocking if the state has not changed since the last
148 .I readmouse
149 or message sent on the channel.
150 It calls
151 .B flushimage
152 (see
153 .IR graphics (3))
154 before blocking, so any buffered graphics requests are displayed.
155 .PP
156 .I Closemouse
157 closes the file descriptors associated with the mouse, kills the slave processes,
158 and frees the
159 .B Mousectl
160 structure.
161 .PP
162 .I Moveto
163 moves the mouse cursor on the display to the position specified by
164 .IR pt .
165 .PP
166 .I Setcursor
167 sets the image of the cursor to that specified by
168 .IR c .
169 If
170 .I c
171 is nil, the cursor is set to the default.
172 The format of the cursor data is spelled out in
173 .B <cursor.h>
174 and described in
175 .IR graphics (3).
176 .PP
177 .I Getrect
178 returns the dimensions of a rectangle swept by the user, using the mouse,
179 in the manner
180 .IR rio (1)
181 or
182 .IR sam (1)
183 uses to create a new window.
184 The
185 .I but
186 argument specifies which button the user must press to sweep the window;
187 any other button press cancels the action.
188 The returned rectangle is all zeros if the user cancels.
189 .PP
190 .I Getrect
191 uses successive calls to
192 .I drawgetrect
193 to maintain the red rectangle showing the sweep-in-progress.
194 The rectangle to be drawn is specified by
195 .I rc
196 and the
197 .I up
198 parameter says whether to draw (1) or erase (0) the rectangle.
199 .PP
200 .I Menuhit
201 provides a simple menu mechanism.
202 It uses a
203 .B Menu
204 structure defined in
205 .BR <mouse.h> :
206 .IP
207 .EX
208 typedef struct Menu Menu;
209 struct Menu
211 char **item;
212 char *(*gen)(int);
213 int lasthit;
214 };
215 .EE
216 .PP
217 .IR Menuhit
218 behaves the same as its namesake
219 .I emenuhit
220 described in
221 .IR event (3),
222 with two exceptions.
223 First, it uses a
224 .B Mousectl
225 to access the mouse rather than using the event interface;
226 and second,
227 it creates the menu as a true window on the
228 .B Screen
229 .I scr
230 (see
231 .IR window (3)),
232 permitting the menu to be displayed in parallel with other activities on the display.
233 If
234 .I scr
235 is null,
236 .I menuhit
237 behaves like
238 .IR emenuhit ,
239 creating backing store for the menu, writing the menu directly on the display, and
240 restoring the display when the menu is removed.
241 .PP
242 .SH SOURCE
243 .B \*9/src/libdraw
244 .SH SEE ALSO
245 .IR graphics (3),
246 .IR draw (3),
247 .IR event (3),
248 .IR keyboard (3),
249 .IR thread (3).