249 lines · 4.8 KB · Text
RawDownload
.TH MOUSE 2.SH NAMEinitmouse, readmouse, closemouse, moveto, getrect, drawgetrect, menuhit, setcursor \- mouse control.SH SYNOPSIS.nf.B#include <u.h>.B#include <libc.h>.B#include <draw.h>.B#include <thread.h>.B#include <mouse.h>.B#include <cursor.h>.PP.BMousectl	*initmouse(char *file, Image *i).PP.Bint		readmouse(Mousectl *mc).PP.Bint		atomouse();.PP.Bvoid		closemouse(Mousectl *mc).PP.Bvoid		moveto(Mousectl *mc, Point pt).PP.Bvoid		setcursor(Mousectl *mc, Cursor *c).PP.BRectangle	getrect(int but, Mousectl *mc).PP.Bvoid		drawgetrect(Rectangle r, int up).PP.Bint		menuhit(int but, Mousectl *mc, Menu *menu, Screen *scr).fi.SH DESCRIPTIONThese functions access and control a mouse in a multi-threaded environment.They use the message-passing.B Channelinterface in the threads library(see.IR thread (2));programs that wish a more event-driven, single-threaded approach should use.IR event (2)..PPThe state of the mouse is recorded in a structure,.BR Mouse ,defined in.BR <mouse.h> :.IP.EX.ta 6n +\w'Rectangle 'u +\w'buttons;   'utypedef struct Mouse Mouse;struct Mouse{	int	buttons;	/* bit array: LMR=124 */	Point	xy;	ulong	msec;};.EE.PPThe.B Point.B xyrecords the position of the cursor,.B buttonsthe state of the buttons (three bits representing, from bit 0 up, the buttons from left to right,0 if the button is released, 1 if it is pressed),and.BR msec ,a millisecond time stamp..PPThe routine.B initmousereturns a structure through which one may access the mouse:.IP.EXtypedef struct Mousectl Mousectl;struct Mousectl{	Mouse;	Channel	*c;	/* chan(Mouse)[16] */	Channel	*resizec;	/* chan(int)[2] */ 	char	*file;	int	mfd;		/* to mouse file */	int	cfd;		/* to cursor file */	int	pid;		/* of slave proc */	Image*	image;	/* of associated window/display */};.EE.PPThe arguments to.I initmouseare a.I filenaming the device file connected to the mouse and an.I Image(see.IR draw (2))on which the mouse will be visible.Typically the file isnil,which requests the default.BR /dev/mouse ;and the image is the window in which the program is running, held in the variable.B screenafter a call to.IR initdraw ..PPOnce the.B Mousectlis set up,mouse motion will be reported by messages of type.B Mousesent on the.B Channel.BR Mousectl.c .Typically, a message will be sent every time a read of.B /dev/mousesucceeds, which is every time the state of the mouse changes..PPWhen the window is resized, a message is sent on.BR Mousectl.resizec .The actual value sent may be discarded; the receipt of the messagetells the program that it should call.B getwindow(see.IR graphics (2))to reconnect to the window..PP.I Readmouseupdates the.B Mousestructure held in the.BR Mousectl ,blocking if the state has not changed since the last.I readmouseor message sent on the channel.It calls.B flushimage(see.IR graphics (2))before blocking, so any buffered graphics requests are displayed..PP.I Closemousecloses the file descriptors associated with the mouse, kills the slave processes,and frees the.B Mousectlstructure..PP.I Movetomoves the mouse cursor on the display to the position specified by.IR pt ..PP.I Setcursorsets the image of the cursor to that specified by.IR c .If.I cis nil, the cursor is set to the default.The format of the cursor data is spelled out in.B <cursor.h>and described in.IR graphics (2)..PP.I Getrectreturns the dimensions of a rectangle swept by the user, using the mouse,in the manner.IR rio (1)or.IR sam (1)uses to create a new window.The.I butargument specifies which button the user must press to sweep the window;any other button press cancels the action.The returned rectangle is all zeros if the user cancels..PP.I Getrectuses successive calls to.I drawgetrectto maintain the red rectangle showing the sweep-in-progress.The rectangle to be drawn is specified by.I rcand the.I upparameter says whether to draw (1) or erase (0) the rectangle..PP.I Menuhitprovides a simple menu mechanism.It uses a.B Menustructure defined in.BR <mouse.h> :.IP.EXtypedef struct Menu Menu;struct Menu{	char	**item;	char	*(*gen)(int);	int	lasthit;};.EE.PP.IR Menuhitbehaves the same as its namesake.I emenuhitdescribed in.IR event (2),with two exceptions.First, it uses a.B Mousectlto access the mouse rather than using the event interface;and second,it creates the menu as a true window on the.B Screen.I scr(see.IR window (2)),permitting the menu to be displayed in parallel with other activities on the display.If.I scris null,.I menuhitbehaves like.IR emenuhit ,creating backing store for the menu, writing the menu directly on the display, andrestoring the display when the menu is removed..PP.SH SOURCE.B /sys/src/libdraw.SH SEE ALSO.IR graphics (2),.IR draw (2),.IR event (2),.IR keyboard (2),.IR thread (2).