Class: DockDriver::I3

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Defined in:
lib/dock_driver/i3.rb

Overview

A class to provide IPC communication with i3wm.

Constant Summary collapse

COMMAND =

:nodoc:

0
GET_WORKSPACES =

:nodoc:

1
SUBSCRIBE =

:nodoc:

2
GET_OUTPUTS =

:nodoc:

3
MAGIC =

:nodoc:

'i3-ipc'
EVENT_MASK =

:nodoc:

(1 << 31)
EVENT_WORKSPACE =

:nodoc:

(EVENT_MASK | 0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket = '~/.i3/ipc.sock') ⇒ I3

A constructor OF DOOM!



30
31
32
33
# File 'lib/dock_driver/i3.rb', line 30

def initialize( socket = '~/.i3/ipc.sock' )
    @path = Pathname( socket ).expand_path.to_s 
    @ipc = UNIXSocket.open( @path )
end

Instance Attribute Details

#ipcObject

The IPC socket object.



24
25
26
# File 'lib/dock_driver/i3.rb', line 24

def ipc
  @ipc
end

#pathObject

The IPC socket path.



27
28
29
# File 'lib/dock_driver/i3.rb', line 27

def path
  @path
end

Instance Method Details

#command(cmd) ⇒ Object

Send a command to i3.



36
37
38
# File 'lib/dock_driver/i3.rb', line 36

def command( cmd )
    self.send_data( COMMAND, cmd )
end

#get_workspacesObject

Return an array of current workspace state.



41
42
43
44
# File 'lib/dock_driver/i3.rb', line 41

def get_workspaces
    self.send_data( GET_WORKSPACES )
    return self.read_data( GET_WORKSPACES )
end

#subscribe_workspacesObject

Register interest in workspace change events.



47
48
49
50
51
# File 'lib/dock_driver/i3.rb', line 47

def subscribe_workspaces
    self.send_data( SUBSCRIBE, JSON.generate(['workspace']))
    result = self.read_data( SUBSCRIBE )
    return result[ 'success' ]
end

#wait_for_event(type, timeout = nil) {|msg| ... } ⇒ Object

Do a blocking select, yielding an event when one is seen.

Yields:

  • (msg)


54
55
56
57
58
59
# File 'lib/dock_driver/i3.rb', line 54

def wait_for_event( type, timeout=nil )
    sock = select( [self.ipc], nil, nil, timeout )
    return nil unless sock and type and msg = self.read_data( type )
    yield msg if block_given?
    return msg
end