Detecting X11 events and more using the xlib-objects gem

Installation

Examples

Finding out the screen resolution

require 'xlib-objects'

display = XlibObj::Display.new(':0')

main_screen = display.screens.first

root_win = main_screen.root_window
root_win.width  #=> 1366
root_win.height #=> 768

# Fetch the names of the attached displays

outputs = main_screen.crtcs.map(&:outputs).flatten
outputs.first.name # => e.g. 'LVDS1'
outputs.last.name  # => e.g. 'DVI-I-2'

Note: When I queried it only 1 display was returned which is what I would expect

Querying the attached input devices

display = XlibObj::Display.new(':0')

display.input_devices.each do |device|
  puts <<-DEVICE
#{device.id} #{device.name}
  type: #{device.keyboard? ? "keyboard (focus: #{device.focus.id})" : 'pointer'}
  hierarchy: #{device.master? ? 'master' : "slave (under: #{device.master.id})"} #{device.floating? ? '(floating)' : ''}
  enabled? #{device.enabled?}
  DEVICE
end

Output:

2 Virtual core pointer
  type: pointer
  hierarchy: master 
  enabled? true
3 Virtual core keyboard
  type: keyboard (focus: 46137349)
  hierarchy: master 
  enabled? true
4 Virtual core XTEST pointer
  type: pointer
  hierarchy: slave (under: 2) 
  enabled? true
5 Virtual core XTEST keyboard
  type: keyboard (focus: 160)
  hierarchy: slave (under: 3) 
  enabled? true
6 Power Button
  type: keyboard (focus: 160)
  hierarchy: slave (under: 3) 
  enabled? true
7 Video Bus
  type: keyboard (focus: 160)
  hierarchy: slave (under: 3) 
  enabled? true
8 Power Button
  type: keyboard (focus: 160)
  hierarchy: slave (under: 3) 
  enabled? true
9 Sleep Button
  type: keyboard (focus: 160)
  hierarchy: slave (under: 3) 
  enabled? true
10 Microsoft Wired Keyboard 600
  type: keyboard (focus: 160)
  hierarchy: slave (under: 3) 
  enabled? true
11 Microsoft Wired Keyboard 600
  type: pointer
  hierarchy: slave (under: 2) 
  enabled? true
12 USB Optical Mouse
  type: pointer
  hierarchy: slave (under: 2) 
  enabled? true
13 HD WebCam
  type: keyboard (focus: 160)
  hierarchy: slave (under: 3) 
  enabled? true
14 AT Translated Set 2 keyboard
  type: keyboard (focus: 160)
  hierarchy: slave (under: 3) 
  enabled? true
15 SynPS/2 Synaptics TouchPad
  type: pointer
  hierarchy: slave (under: 2) 
  enabled? true
16 Acer WMI hotkeys
  type: keyboard (focus: 160)
  hierarchy: slave (under: 3) 
  enabled? true

Resources