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