Class: I3Ipc::Connection

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

Overview

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

Examples:

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

Instance Method Summary collapse

Constructor Details

#initialize(protocol = Protocol.new, autoconnect = true) ⇒ Connection

Returns a new instance of Connection.



30
31
32
33
# File 'lib/i3ipc/connection.rb', line 30

def initialize(protocol = Protocol.new, autoconnect = true)
  @protocol = protocol
  open if autoconnect
end

Instance Method Details

#bar_configObject



82
83
84
# File 'lib/i3ipc/connection.rb', line 82

def bar_config
  reply_for(6)
end

#closeObject



39
40
41
# File 'lib/i3ipc/connection.rb', line 39

def close
  @protocol.disconnect
end

#command(cmds) ⇒ Object



43
44
45
# File 'lib/i3ipc/connection.rb', line 43

def command(cmds)
  reply_for(0, cmds)
end

#marksObject



78
79
80
# File 'lib/i3ipc/connection.rb', line 78

def marks
  reply_for(5)
end

#openObject



35
36
37
# File 'lib/i3ipc/connection.rb', line 35

def open
  @protocol.connect
end

#outputsObject



70
71
72
# File 'lib/i3ipc/connection.rb', line 70

def outputs
  reply_for(3)
end

#subscribe(event, block) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

#treeObject



74
75
76
# File 'lib/i3ipc/connection.rb', line 74

def tree
  reply_for(4)
end

#versionObject



86
87
88
# File 'lib/i3ipc/connection.rb', line 86

def version
  reply_for(7)
end

#workspacesObject



47
48
49
# File 'lib/i3ipc/connection.rb', line 47

def workspaces
  reply_for(1)
end