33

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 Answers2

35

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.

Jander
  • 16,682
14

Generally, information from hardware devices are exposed to applications via device files. For mice, on modern Linux systems, the device is /dev/mice (this device collects events from all connected mice, there are also devices corresponding to each individual mouse).

When you move or click the mouse, the mouse sends an electrical signal to the computer, which causes it to raise an interrupt. A piece of code in the Linux kernel called an interrupt handler in turn reads the event data (e.g. which button was pressed) and triggers a chain of notifications inside the kernel.

If there is any application that is reading from the device file corresponding to this device, that application is told that input is waiting. For example, if the application is blocked in a read system call, then the system call returns.

On a typical system, there is one process reading mice events: the X Window server. That program manages the graphical display and input peripherals. Individual graphical applications are then notified of mouse events through a generic X event protocol, which carries other event types such as key presses, window focus changes, window visibility changes, …

  • Old (earlier than ~2000) mice used PS/2, which is interrupt-driven. USB devices (the majority of mice in use today) are not interrupt driven, but polling (https://stackoverflow.com/questions/7076472/why-is-not-usb-interrupt-driven). USB has a concept officially called "interrupt transfers" which are better described as "periodic transactions" (https://electronics.stackexchange.com/questions/344639/isochronous-and-interrupt-transfers-for-usb-where-to-learn-about-it) – asky Feb 04 '21 at 17:35