class I3Ipc::Connection

Entry point for communication with i3-ipc. Able to send/receive messages and convert responses.

@example

con = Connection.new
p con.version.human_readable         # => 4.10.2 (2015-0...
p con.command('focus left').success? # => true
p con.workspaces[0].name             # => 0 Term
# ...
con.close

Public Class Methods

new(protocol = Protocol.new, autoconnect = true) click to toggle source
# File lib/i3ipc/connection.rb, line 30
def initialize(protocol = Protocol.new, autoconnect = true)
  @protocol = protocol
  open if autoconnect
end

Public Instance Methods

bar_config() click to toggle source
# File lib/i3ipc/connection.rb, line 82
def bar_config
  reply_for(6)
end
close() click to toggle source
# File lib/i3ipc/connection.rb, line 39
def close
  @protocol.disconnect
end
command(cmds) click to toggle source
# File lib/i3ipc/connection.rb, line 43
def command(cmds)
  reply_for(0, cmds)
end
marks() click to toggle source
# File lib/i3ipc/connection.rb, line 78
def marks
  reply_for(5)
end
open() click to toggle source
# File lib/i3ipc/connection.rb, line 35
def open
  @protocol.connect
end
outputs() click to toggle source
# File lib/i3ipc/connection.rb, line 70
def outputs
  reply_for(3)
end
subscribe(event, block) click to toggle source
# File lib/i3ipc/connection.rb, line 51
def subscribe(event, block)
  event_number = event_number(event)

  # Send subscription request
  @protocol.send(2, [event])

  reply = Reply.parse(@protocol.receive 2)
  raise WrongEvent.new(event) unless reply.success?

  pid = Thread.new do
    while true
      reply = Reply.parse(@protocol.receive_event event_number)
      block.call(reply)
    end
  end

  pid
end
tree() click to toggle source
# File lib/i3ipc/connection.rb, line 74
def tree
  reply_for(4)
end
version() click to toggle source
# File lib/i3ipc/connection.rb, line 86
def version
  reply_for(7)
end
workspaces() click to toggle source
# File lib/i3ipc/connection.rb, line 47
def workspaces
  reply_for(1)
end

Private Instance Methods

event_number(event) click to toggle source
# File lib/i3ipc/connection.rb, line 98
def event_number(event)
  case event
  when 'workspace'         then 0
  when 'output'            then 1
  when 'mode'              then 2
  when 'window'            then 3
  when 'barconfig_update'  then 4
  when 'binding'           then 5
  else raise WrongEvent.new(event)
  end
end
reply_for(type, message = nil) click to toggle source
# File lib/i3ipc/connection.rb, line 92
def reply_for(type, message = nil)
  @protocol.send(type, message)

  Reply.parse(@protocol.receive type)
end