How GIMP Distinguishes Stylus and UI Events
The GNU Image Manipulation Program (GIMP) distinguishes hardware stylus inputs from standard user interface events by integrating low-level operating system drivers through the GDK (GIMP Drawing Kit) library, categorizing device types, and routing event structures depending on whether an action targets the canvas or a general interface widget. Through this architecture, GIMP evaluates hardware-specific event attributes—such as sub-pixel positioning, pressure sensitivity, and tilt axes—allowing it to execute complex brush dynamics on the image canvas while treating stylus interactions in menus and toolbars as standard pointer clicks.
Hardware Abstraction via GDK
GIMP does not communicate with tablet hardware directly; instead, it delegates input capture to GDK, the underlying windowing and abstraction layer of GTK. GDK interfaces with platform-specific input APIs—such as XInput2 or Wayland on Linux, Windows Ink and WinTab on Windows, and Cocoa on macOS.
When a physical device triggers an action, the OS driver sends raw
event packets to GDK. GDK wraps these into GdkEvent
structures and flags the event source using the
GdkInputSource enumeration (identifying devices as
GDK_SOURCE_MOUSE, GDK_SOURCE_PEN,
GDK_SOURCE_ERASER, or GDK_SOURCE_TOUCHSCREEN).
This classification provides GIMP with an immediate hardware-level
identity for the event before any graphical processing takes place.
The GimpDeviceManager and Device Contexts
Within GIMP’s core, the GimpDeviceManager keeps an
inventory of all active and historical input devices, mapping each to a
GimpDeviceInfo object. Standard mouse hardware and virtual
desktop pointers are treated as "core pointer" devices, whereas graphics
tablets register as distinct slave or extended devices.
Each GimpDeviceInfo maintains:
- Device Axis Mappings: Channels assigned to pressure, horizontal tilt, vertical tilt, wheel, and rotation.
- Device-Specific Presets: Independent tool, brush, and color assignments (for instance, automatically switching GIMP to the Eraser tool when an inverted stylus tail is detected).
- Hardware Curves: Calibration profiles for pressure and velocity.
When an input event arrives, the core compares the device ID in the event against its registered devices. If the ID corresponds to an extended device rather than the core pointer, GIMP extracts extended device axes rather than standard X/Y display coordinates.
Event Routing: Canvas versus Widgets
The differentiation between a drawing stroke and a UI interaction depends heavily on the target widget receiving the event:
Standard UI Widgets (Menus, Panels, Sliders): When an event hits standard GTK widgets, GIMP defaults to standard widget signal processing. Even if the event originates from a hardware stylus reporting pressure and tilt, the standard widget ignores extra axis data. The event is normalized down to primary button presses, releases, and motion events to drive UI navigation.
The Display Shell Canvas (
GimpDisplayShell): The image canvas is a specialized widget configured to receive all extended input events (GDK_ALL_EVENTS_MASKwith extended device support enabled). When an event occurs over the canvas, the core engine bypasses standard pointer normalization.
Axis Evaluation and Dynamic Dispatch
Once routed to the active tool on the canvas, the core engine processes the event through the following pipeline:
- Event Type Check: The engine verifies whether the
incoming
GdkEventcontains extended axis data by checkinggdk_event_get_axis(). - State Interpolation: If the event is generated by a mouse, pressure is assigned a static default value (typically 1.0), and tilt values remain zero. If generated by a stylus, floating-point values for pressure, tilt, and rotation are extracted from the device's assigned axes.
- Dynamics Processing: GIMP feeds these variables into its Paint Dynamics engine, which recalculates brush properties (such as opacity, size, hardness, and flow) in real time before rasterizing the stroke.
By separating the physical device identity at the driver interface and filtering the incoming event properties based on widget boundaries, the GIMP core engine seamlessly isolates high-precision drawing data from routine interface commands.