Introduction

NoteEarlier publication
 

Parts of this document first appeared in Linux Magazine under a ninety day exclusivity.

Mice are conceptually one of the simplest device interfaces in the Linux operating system. Not all mice are handled by the kernel. Instead there is a two layer abstraction.

The kernel mouse drivers and userspace drivers for the serial mice are all managed by a system daemon called gpm - the general purpose mouse driver. gpm handles cutting and pasting on the text consoles. It provides a general library for mouse-aware applications and it handles the sharing of mouse services with the X Window System user interface.

Sometimes a mouse speaks a sufficiently convoluted protocol that the protocol is handled by Gpm itself. Most of the mouse drivers follow a common interface called the bus mouse protocol.

Each read from a bus mouse interface device returns a block of data. The first three bytes of each read are defined as follows:

Table 1. Mouse Data Encoding

Byte 00x80 + the buttons currently down.
Byte 1A signed value for the shift in X position
Byte 2A signed value for the shift in Y position
An application can choose to read more than 3 bytes. The rest of the bytes will be zero, or may optionally return some additional device-specific information.

The position values are truncated if they exceed the 8bit range (that is -127 <= delta <= 127). While the value -128 does fit into a byte is not allowed.

The buttons are numbered left to right as 0, 1, 2, 3.. and each button sets the relevant bit. So a user pressing the left and right button of a three button mouse will set bits 0 and 2.

All mice are required to support the poll operation. Indeed pretty much every user of a mouse device uses poll to wait for mouse events to occur.

Finally the mice support asynchronous I/O. This is a topic we have not yet covered but which I will explain after looking at a simple mouse driver.