MOUSE(3)
okay so bestie, this is MOUSE(3) from the Library Functions Manual β and it's literally the whole mouse-control situation. buckle up. π±οΈπ
β¨ NAME β¨
initmouse,readmouse,closemouse,moveto,cursorswitch,getrect,drawgetrect,menuhit,setcursorβ mouse control, periodt.
πΈ SYNOPSIS πΈ
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <mouse.h>
#include <cursor.h>
Mousectl *initmouse(char *file, Image *i)
int readmouse(Mousectl *mc)
int atomouse();
void closemouse(Mousectl *mc)
void moveto(Mousectl *mc, Point pt)
void setcursor(Mousectl *mc, Cursor *c)
Rectangle getrect(int but, Mousectl *mc)
void drawgetrect(Rectangle r, int up)
int menuhit(int but, Mousectl *mc, Menu *menu, Screen *scr)
yes,atomouseis in the synopsis andcursorswitchis in the NAME list β I'm not editing the lineup, bestie, I'm just reporting it. π«Ά
π¦ DESCRIPTION π¦
So these functions are your mouse-access-and-control besties in a multi-threaded environment. They ride the message-passing Channel interface from the threads library (see thread(3)), and if you want the more event-driven, single-threaded route, the page points you at event(3).
The state of the mouse is recorded in a structure called Mouse, defined in <mouse.h>:
typedef struct Mouse Mouse;
struct Mouse
{
int buttons; /* bit array: LMR=124 */
Point xy;
ulong msec;
};
Point xy is where the cursor is at. buttons is the button tea: three bits, and from bit 0 upward they represent the buttons from left to right β 0 means released, 1 means pressed. msec is a millisecond time stamp.
The routine initmouse returns a structure through which you can access the mouse:
typedef struct Mousectl Mousectl;
struct Mousectl
{
Mouse m;
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 */
};
The arguments to initmouse are a file naming the device file connected to the mouse and an Image (see draw(3)) on which the mouse will be visible. Typically file is nil, which requests the default /dev/mouse; and the image is the window the program is running in, held in the variable screen after a call to initdraw.
Once the Mousectl is set up, mouse motion gets reported by messages of type Mouse sent on the Channel Mousectl.c. Typically a message is sent every time a read of /dev/mouse succeeds β which is every time the state of the mouse changes.
When the window is resized, a message is sent on Mousectl.resizec. You can discard the actual value sent; the receipt of the message is the signal that the program should call getwindow (see graphics(3)) to reconnect to the window.
Readmouse updates the Mouse structure m held in the Mousectl, blocking if the state has not changed since the last readmouse or message sent on the channel. It calls flushimage (see graphics(3)) before blocking, so any buffered graphics requests are displayed.
Closemouse closes the file descriptors associated with the mouse, kills the slave processes, and frees the Mousectl structure.
Moveto moves the mouse cursor on the display to the position specified by pt.
Setcursor sets the image of the cursor to that specified by c. If c is nil, the cursor is set to the default. The format of the cursor data is spelled out in <cursor.h> and described in graphics(3).
Getrect returns the dimensions of a rectangle swept by the user, using the mouse, in the manner rio(1) or sam(1) uses to create a new window. The but argument 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.
Getrect uses successive calls to drawgetrect to maintain the red rectangle showing the sweep-in-progress. The rectangle to be drawn is specified by rc and the up parameter says whether to draw (1) or erase (0) the rectangle.
Menuhit provides a simple menu mechanism. It uses a Menu structure defined in <mouse.h>:
typedef struct Menu Menu;
struct Menu
{
char **item;
char *(*gen)(int);
int lasthit;
};
Menuhit behaves the same as its namesake emenuhit described in event(3), with two exceptions. First, it uses a Mousectl to access the mouse rather than using the event interface; and second, it creates the menu as a true window on the Screen scr (see window(3)), permitting the menu to be displayed in parallel with other activities on the display. If scr is null, menuhit behaves like emenuhit, creating backing store for the menu, writing the menu directly on the display, and restoring the display when the menu is removed.
π SOURCE π
/src/libdraw
that's the source, bestie β /src/libdraw. π
π SEE ALSO π
cute little footer sign-off: this page is MOUSE(3) from the Library Functions Manual, the source is /src/libdraw, and there's no date stamped on this page β so no fake date from me, bestie. πβ¨π
π¦ where this page comes from
- alpine 3.24: plan9port-doc 0_git20260208-r0 Β· LPL-1.02
- arch rolling: plan9port 20261021-2 Β· custom
- nixos 26.05: plan9port 0-unstable-2025-11-10
- nixos 26.05: plan9port-wayland 0-unstable-2025-11-26
- nixpkgs unstable: plan9port 0-unstable-2025-11-10
- nixpkgs unstable: plan9port-wayland 0-unstable-2025-11-26
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms