Class: XlibObj::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/display.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Display

Returns a new instance of Display.



22
23
24
# File 'lib/display.rb', line 22

def initialize(name)
  @struct = Xlib::X.open_display(name)
end

Class Method Details

.namesObject



14
15
16
17
18
19
# File 'lib/display.rb', line 14

def names
  Dir['/tmp/.X11-unix/*'].map do |file_name|
    match = file_name.match(/X(\d+)$/)
    ":#{match[1]}" if match
  end.compact
end

Instance Method Details

#closeObject



117
118
119
# File 'lib/display.rb', line 117

def close
  Xlib::X.close_display(self)
end

#extensionsObject



42
43
44
45
46
# File 'lib/display.rb', line 42

def extensions
  @extensions ||= ['CORE', *Xlib::X.list_extensions(self)].map do |name|
    Extension.for(self, name)
  end
end

#flushObject



113
114
115
# File 'lib/display.rb', line 113

def flush
  handle_events # XPending flushes the output buffer: http://tronche.com/gui/x/xlib/event-handling/XFlush.html
end

#focused_windowObject



65
66
67
# File 'lib/display.rb', line 65

def focused_window
  screens.reduce(nil){ |focused_window, s| focused_window or s.focused_window }
end

#handle_eventsObject



69
70
71
72
73
74
# File 'lib/display.rb', line 69

def handle_events
  while Xlib::X.pending(self) > 0
    xevent = Xlib::X.next_event(self)
    Event.new(self, xevent).extension_event.handle
  end
end

#input_devicesObject



48
49
50
51
52
53
54
55
# File 'lib/display.rb', line 48

def input_devices
  device_infos = Xlib::XI.query_device(self, Xlib::XIAllDevices)
  device_ids = device_infos.map{ |dev| dev[:deviceid] }
  Xlib::XI.free_device_info(device_infos.first)
  device_ids.map do |device_id|
    InputDevice.new(self, device_id)
  end
end

#inspectObject



121
122
123
# File 'lib/display.rb', line 121

def inspect
  "#<#{self.class.name}:0x#{'%014x' % __id__} @name=#{name.inspect}>"
end

#keyboardsObject



57
58
59
# File 'lib/display.rb', line 57

def keyboards
  input_devices.select(&:keyboard?)
end

#nameObject



30
31
32
# File 'lib/display.rb', line 30

def name
  @struct[:display_name]
end

#on_error(&callback) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/display.rb', line 86

def on_error(&callback)
  @error_handler = if callback
    FFI::Function.new(:int, [:pointer, :pointer]) do |display_ptr, error_ptr|
      next if display_ptr != to_native
      x_error = Xlib::XErrorEvent.new(error_ptr)
      callback.call Error.new(self, x_error)
    end
  else
    nil
  end

  Xlib.XSetErrorHandler(@error_handler)
end

#on_io_error(&callback) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/display.rb', line 100

def on_io_error(&callback)
  @io_error_handler = if callback
    FFI::Function.new(:pointer, [:pointer]) do |display_ptr|
      next if display_ptr != to_native
      callback.call
    end
  else
    nil
  end

  Xlib.XSetIOErrorHandler(@io_error_handler)
end

#pointersObject



61
62
63
# File 'lib/display.rb', line 61

def pointers
  input_devices.select(&:pointer?)
end

#screensObject



38
39
40
# File 'lib/display.rb', line 38

def screens
  (0..@struct[:nscreens]-1).map{ |number| screen(number) }
end

#selection(*args, &on_receive) ⇒ Object



76
77
78
79
# File 'lib/display.rb', line 76

def selection(*args, &on_receive)
  internal_window.request_selection(*args, &on_receive)
  self
end

#set_selection(*args, &on_request) ⇒ Object



81
82
83
84
# File 'lib/display.rb', line 81

def set_selection(*args, &on_request)
  internal_window.set_selection(*args, &on_request)
  self
end

#socketObject



34
35
36
# File 'lib/display.rb', line 34

def socket
  @socket ||= UNIXSocket.for_fd(Xlib.XConnectionNumber(to_native))
end

#to_nativeObject



26
27
28
# File 'lib/display.rb', line 26

def to_native
  @struct.pointer
end