How do mouse events work in linux?
This is mostly out of curiosity, I’m trying to understand how event handling works on a low level, so please don’t reference me to a software that’ll do it for me. If for example I want to write a program in C/C++ that reacts to mouse clicks, I assume I need to use a system call to hook some function to the kernel, or maybe you need to just constantly check the status of the mouse, I don’t know. I assume it’s possible since just about everything is possible in C/C++, being so low level, I’m mostly interested in how it works, even though I’ll probably never have to implement it myself. The question is how it works in linux, are there certain system calls, c libraries, etc.?
2 Answers 2
If you’re writing a real-world program that uses the mouse in Linux, you’re most likely writing an X application, and in that case you should ask the X server for mouse events. Qt, GTK, and libsdl are some popular C libraries that provide functions for accessing mouse, keyboard, graphics, timers, and other features needed to write GUI programs. Ncurses is a similar library for terminal applications.
But if you’re exploring your system, or you can’t use X for whatever reason, here is how it works at the kernel interface.
A core idea in the UNIX philosophy is that «everything is a file». More specifically, as many things as possible should be accessible through the same system calls that you use to work with files. And so the kernel interface to the mouse is a device file. You open() it, optionally call poll() or select() on it to see if there’s incoming data, and read() to read the data.
In pre-USB times, the specific device file was often a serial port, e.g. /dev/ttyS0 , or a PS/2 port, /dev/psaux . You talked to the mouse using whatever hardware protocol was built into the mouse. These days, the /dev/input/* subsystem is preferred, as it provides a unified, device-independent way of handling many different input devices. In particular, /dev/input/mice will give you events from any mouse attached to your system, and /dev/input/mouseN will give you events from a particular mouse. In most modern Linux distributions, these files are created dynamically when you plug in a mouse.
For more information about exactly what you would read or write to the mouse device file, you can start with input/input.txt in the kernel documentation. Look in particular at sections 3.2.2 (mousedev) and 3.2.4 (evdev), and also sections 4 and 5.
Linux input linux mouse
mousetype Indicates what type of mouse is attached. The following types are supported: * ms , Microsoft : Two button Microsoft compatible mice. * ms3 , IntelliMouse , mman+ : Serial Intellimouse or compatible. Supports four buttons for mice like the Logitech MouseMan+. * MouseSystems , msc : MouseSystems compatible (used in most 3-button mice). * Logitech , logi : Used in older Logitech serial mice. * mm , MMSeries : MM series mice. * Sun : Sun mice. The protocol is the same as ‘msc’ protocol except it used only 3 bytes per packet. * MouseMan , mman : MouseMan protocol. * BusMouse , bm : Non-PS/2 busmice. * LinuxUSB , lnxusb : IMPS2 that doesn’t choke on USB (no init attempted). * ps2 , PS/2 : PS/2 mice (most non-serial mice use this protocol) * imps2 , IMPS/2 : PS/2 IntelliMouse and compatible (not USB, see lnxusb for that.) * mmanps2 , MouseManPlusPS/2 : PS/2 Logitech MouseMan+ and compatible.
Most of the strings supported by GPM, SVGAlib and XFree can be specified. If you find one that isn’t, please report.
The special value auto tells input-linux-mouse to read ~/.ggi/input/linux-mouse , and if that fails, $sysconfdir/ggi/input/linux-mouse . If none of the two files are found, input-linux-mouse tries to auto-detect the mouse, and as a last resort uses the mouse information specified in /etc/vga/libvga.config .
file The file to read mouse protocol data from; defaults to /dev/mouse .
baud Baud rate of mouse device.
rts Turns off RTS if set to 0 ; turns RTS on if set to a number greater than 0.
dtr Turns off DTR if set to 0 ; turns DTR on if set to a number greater than 0.
FEATURES
FILES
mouse mousetype mdev file
where mousetype and file has the same semantics as the invocation options of the same names.
Index
Linux input linux mouse
mousetype Indicates what type of mouse is attached. The following types are supported: * ms , Microsoft : Two button Microsoft compatible mice. * ms3 , IntelliMouse , mman+ : Serial Intellimouse or compatible. Supports four buttons for mice like the Logitech MouseMan+. * MouseSystems , msc : MouseSystems compatible (used in most 3-button mice). * Logitech , logi : Used in older Logitech serial mice. * mm , MMSeries : MM series mice. * Sun : Sun mice. The protocol is the same as ‘msc’ protocol except it used only 3 bytes per packet. * MouseMan , mman : MouseMan protocol. * BusMouse , bm : Non-PS/2 busmice. * LinuxUSB , lnxusb : IMPS2 that doesn’t choke on USB (no init attempted). * ps2 , PS/2 : PS/2 mice (most non-serial mice use this protocol) * imps2 , IMPS/2 : PS/2 IntelliMouse and compatible (not USB, see lnxusb for that.) * mmanps2 , MouseManPlusPS/2 : PS/2 Logitech MouseMan+ and compatible.
Most of the strings supported by GPM, SVGAlib and XFree can be specified. If you find one that isn’t, please report.
The special value auto tells input-linux-mouse to read ~/.ggi/input/linux-mouse , and if that fails, $sysconfdir/ggi/input/linux-mouse . If none of the two files are found, input-linux-mouse tries to auto-detect the mouse, and as a last resort uses the mouse information specified in /etc/vga/libvga.config .
file The file to read mouse protocol data from; defaults to /dev/mouse .
baud Baud rate of mouse device.
rts Turns off RTS if set to 0 ; turns RTS on if set to a number greater than 0.
dtr Turns off DTR if set to 0 ; turns DTR on if set to a number greater than 0.
FEATURES
FILES
mouse mousetype mdev file
where mousetype and file has the same semantics as the invocation options of the same names.
Index
Linux input linux mouse
mousetype Indicates what type of mouse is attached. The following types are supported: * ms , Microsoft : Two button Microsoft compatible mice. * ms3 , IntelliMouse , mman+ : Serial Intellimouse or compatible. Supports four buttons for mice like the Logitech MouseMan+. * MouseSystems , msc : MouseSystems compatible (used in most 3-button mice). * Logitech , logi : Used in older Logitech serial mice. * mm , MMSeries : MM series mice. * Sun : Sun mice. The protocol is the same as ‘msc’ protocol except it used only 3 bytes per packet. * MouseMan , mman : MouseMan protocol. * BusMouse , bm : Non-PS/2 busmice. * LinuxUSB , lnxusb : IMPS2 that doesn’t choke on USB (no init attempted). * ps2 , PS/2 : PS/2 mice (most non-serial mice use this protocol) * imps2 , IMPS/2 : PS/2 IntelliMouse and compatible (not USB, see lnxusb for that.) * mmanps2 , MouseManPlusPS/2 : PS/2 Logitech MouseMan+ and compatible.
Most of the strings supported by GPM, SVGAlib and XFree can be specified. If you find one that isn’t, please report.
The special value auto tells input-linux-mouse to read ~/.ggi/input/linux-mouse , and if that fails, $sysconfdir/ggi/input/linux-mouse . If none of the two files are found, input-linux-mouse tries to auto-detect the mouse, and as a last resort uses the mouse information specified in /etc/vga/libvga.config .
file The file to read mouse protocol data from; defaults to /dev/mouse .
baud Baud rate of mouse device.
rts Turns off RTS if set to 0 ; turns RTS on if set to a number greater than 0.
dtr Turns off DTR if set to 0 ; turns DTR on if set to a number greater than 0.
FEATURES
FILES
mouse mousetype mdev file
where mousetype and file has the same semantics as the invocation options of the same names.
Index