XOPENDEVICE(3)

πŸ’– XOPENDEVICE β€” bestie edition πŸ’–

okay so we're doing XOpenDevice(3) and XCloseDevice(3) today β€” open or close an extension input device. yes it's giving very "let me in" / "ok bye" energy and honestly? iconic. πŸŽ€


✨ NAME ✨

XOpenDevice, XCloseDevice β€” open or close an extension input device. that's the whole vibe bestie, one opens, one closes, they're a matched set fr fr πŸ¦‹


🌸 SYNOPSIS 🌸

#include <X11/extensions/XInput.h>

XDevice *XOpenDevice( Display *display,
                      XID device_id);

XCloseDevice( Display *display,
              XDevice *device);
  • display β€” the connection to the X server. your lil phone line to the server, basically πŸ“ž
  • device_id β€” the id of the device you wanna open πŸ‘‘
  • device β€” the device you wanna close 🫢

πŸ’… DESCRIPTION πŸ’…

so the XOpenDevice request makes an input device accessible to a client through input extension protocol requests. if it works out, it hands you back a pointer to an XDevice structure. like the server said "yes queen, here's your pointer" ✨

then there's XCloseDevice, which makes an input device inaccessible to a client through input extension protocol requests. so real talk: before you terminate, any client that has opened input devices through the input extension should close them via CloseDevice. don't just ghost the device, that's rude 😭

when a client makes an XCloseDevice request, any active grabs that the client has on the device are released. any event selections that the client has are deleted, and so are any passive grabs. and if the requesting client is the last client accessing the device, the server may disable all access by X to the device. periodt.

also XOpenDevice and XCloseDevice can generate a BadDevice error. noted. πŸ’€

🧚 Structures 🧚

the XDevice structure returned by XOpenDevice contains:

typedef struct {
XID device_id;
int num_classes;
XInputClassInfo *classes;
} XDevice;

the classes field is a pointer to an array of XInputClassInfo structures. every element in that array has an event type base for a class of input supported by the specified device. and num_classes tells you how many elements are in the classes array. it's giving "here's the list, here's the count, we're organized" πŸ’–

the XInputClassInfo structure contains:

typedef struct {
    unsigned char input_class;
    unsigned char event_type_base;
} XInputClassInfo;

the input_class field identifies one class of input supported by the device. defined types include KeyClass, ButtonClass, ValuatorClass, ProximityClass, FeedbackClass, FocusClass, and OtherClass. the event_type_base identifies the event type of the first event in that class. slay. πŸ”₯

the info inside the XInputClassInfo structure is used by macros to obtain the event classes that clients use in making XSelectExtensionEvent requests. currently defined macros include:

  • DeviceKeyPress
  • DeviceKeyRelease
  • DeviceButtonPress
  • DeviceButtonRelease
  • DeviceMotionNotify
  • DeviceFocusIn
  • DeviceFocusOut
  • ProximityIn
  • ProximityOut
  • DeviceStateNotify
  • DeviceMappingNotify
  • ChangeDeviceNotify
  • DevicePointerMotionHint
  • DeviceButton1Motion
  • DeviceButton2Motion
  • DeviceButton3Motion
  • DeviceButton4Motion
  • DeviceButton5Motion
  • DeviceButtonMotion
  • DeviceOwnerGrabButton
  • DeviceButtonPressGrab
  • NoExtensionEvent

to obtain the proper event class for a particular device, you invoke one of the above macros using the XDevice structure for that device. for example:

DeviceKeyPress (*device, type, eventclass);

that returns the DeviceKeyPress event type and the eventclass for DeviceKeyPress events from the specified device. look at her go πŸ’…

this eventclass can then be used in an XSelectExtensionEvent request to ask the server to send DeviceKeyPress events from this device. and when a selected event is received via XNextEvent, the type can be used for comparison with the type in the event. iykyk 🌈


😭 DIAGNOSTICS 😭

  • BadDevice β€” an invalid device was specified. the specified device does not exist, or is the X keyboard or X pointer. this error may also occur if some other client has caused the specified device to become the X keyboard or X pointer device via the XChangeKeyboardDevice or XChangePointerDevice requests. so like… not your fault but also kind of your fault. sorry bestie 🫢

lowkey the whole thing is: open the device, do your thing, close the device, don't leave grabs lying around like dirty laundry. no cap. πŸŽ€

okay and for the cute sign-off: this lil masterpiece is dated 05/18/2026, and yes it lives under the name XOPENDEVICE(3) β€” literally the same name at the top and the bottom, symmetrical queen behavior πŸ‘‘βœ¨ bye bestie, go open a device πŸ’–πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from