Class: LCDProc::Client

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

Constant Summary collapse

MAX_MESSAGES =

The maximum number of messages allowed to stay in memory. This works much like a circular array with the oldest messages being overwritten.

512
RESPONSE_PROCESS_TIME =

The number of milliseconds to wait before processing new responses.

25
DEBUG =

Whether or not debugging output should be displayed.

false
@@client_count =

The number of LCDProc-Ruby clients currently connected to the server.

0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_options = {}) ⇒ Client

Create a new client.

If :host is left blank, it will attempt to connect to “localhost” on port 13666. You may also specify another port through :port.

  • :host - The host to which the client should attempt to connect. Defaults to “localhost”.

  • :port - The port on which the client should attempt to connect. Defaults to 13666.

  • :name - The name that should be associated with this client. Defaults to “Client_” + a sequence number.

Example:

my_client = Client.new

or

my_client = Client.new( :host => "my.remote-host.com", :port => 13667, :name => 'MyGamingRig' )


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lcdproc/client.rb', line 62

def initialize( user_options = {} )
  if self.respond_to?( :before_initialize ) then self.send( :before_initialize, user_options ) end
  
  @messages = []
  @commands = []
  @response_queue = []
  @screens = []
  @menu_events = []
  @global_key_events = []
  @current_screen = nil
  
  options = {}
  
  options[:host] = "localhost"
  options[:port] = 13666
  options[:name] = "Client_#{@@client_count}"
  
  # Update our defaults with anything that they passed in
  options.update( user_options )
  
  @daemon_socket = connect( options[:host], options[:port] )
  
  if @daemon_socket.nil?
    raise messages[0]
  end
  
  @thread = Thread.new do
    while true do
      sleep( RESPONSE_PROCESS_TIME.to_f / 1000.0 )
      process_responses
    end
  end
  
  response = send_command( Command.new( "hello" ) )
  response = response.message.split(' ')
  
  @version      = response[2]
  @protocol     = response[4]
  @width        = response[7]
  @height       = response[9]
  @cell_width   = response[11]
  @cell_height  = response[13]
  
  @menu = Menu.new( self )
  
  response = send_command( Command.new( "info" ) )
  driver_info = response.message
  
  LCDProc::Devices.find_all_that_drive( driver_info ).each do |device|
    LCDProc::Client.send( :include, device )
  end
  
  @name = options[:name]
  
  response = send_command( Command.new( "client_set -name #{@name}" ) )
  
  @@client_count += 1
  
  if self.respond_to?( :after_initialize ) then self.send( :after_initialize, options ) end
end

Instance Attribute Details

#cell_heightObject (readonly)

Returns the value of attribute cell_height.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def cell_height
  @cell_height
end

#cell_widthObject (readonly)

Returns the value of attribute cell_width.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def cell_width
  @cell_width
end

#commandsObject (readonly)

Returns the value of attribute commands.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def commands
  @commands
end

#heightObject (readonly)

Returns the value of attribute height.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def height
  @height
end

#key_eventsObject (readonly)

Returns the value of attribute key_events.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def key_events
  @key_events
end

Returns the value of attribute menu.



44
45
46
# File 'lib/lcdproc/client.rb', line 44

def menu
  @menu
end

Returns the value of attribute menu_events.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def menu_events
  @menu_events
end

#messagesObject (readonly)

Returns the value of attribute messages.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def messages
  @messages
end

#nameObject

Returns the value of attribute name.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def name
  @name
end

#response_queueObject (readonly)

Returns the value of attribute response_queue.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def response_queue
  @response_queue
end

#screensObject (readonly)

Returns the value of attribute screens.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def screens
  @screens
end

#widthObject (readonly)

Returns the value of attribute width.



43
44
45
# File 'lib/lcdproc/client.rb', line 43

def width
  @width
end

Instance Method Details

#add_message(msg) ⇒ Object

Adds a message to the client’s message list. Used seperately from the responses returned by the LCDd server. If there are more than MAX_MESSAGES, the oldest message will be removed from the stack.

  • msg - The message to be added.

Example:

client.add_message( "Woah Man!!! Something is wrong!!!" )


133
134
135
136
137
138
139
# File 'lib/lcdproc/client.rb', line 133

def add_message( msg )
  @messages.unshift( msg )
  
  if @messages.length > MAX_MESSAGES
    @messages.pop
  end
end

#attach(screen) ⇒ Object

Attaches a screen to this client.

  • screen - Any screen which contains any number of widgets.

Example:

s = Screen.new( 'MyTestScreen' )
client.attach( s )

or

client.attach( Screen.new( 'MyTestScreen' ) )


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/lcdproc/client.rb', line 154

def attach( screen )
  
  if @screens.select{ |s| s.id == screen.id }.empty?
    if screen.attach_to( self )
      @screens << screen
      screen.update
      return true
    else
      return false
    end
  else
    return false
  end
  
end

#detach(screen) ⇒ Object

Detaches a screen from the client but leaves it in memory so that it may later be attached to another client or re-attached to this client.

  • screen - Any screen currently attached to this client.

Example:

client.detach( 'TestScreen' )


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/lcdproc/client.rb', line 179

def detach( screen )
  if @screens.include? screen
    
    if screen.detach
      @screens.delete( screen )
      return true
    else
      return false
    end
    
  else
    add_message "Error: Screen '#{screen.id}' is not attached to client '#{self.name}'"
    return false
  end
end

#messageObject

Returns the last received message from the @messages array (internally more like a circular array).

Example:

puts client.message


201
202
203
# File 'lib/lcdproc/client.rb', line 201

def message
  @messages.first
end

#register_key_event(key_event) ⇒ Object

Register a new button event for the client to process and reserves the key.



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/lcdproc/client.rb', line 230

def register_key_event( key_event )
  
  if key_event and valid_keys.include? key_event.key
    response = send_command( Command.new( "client_add_key -exclusively #{key_event.key}" ) )
    
    if response.successful?
      @global_key_events << key_event
      return key_event
    end
  end
  
  return nil
end

#register_menu_event(menu_event) ⇒ Object

Register a new menu event for the client to process



246
247
248
249
250
251
252
253
254
255
# File 'lib/lcdproc/client.rb', line 246

def register_menu_event( menu_event )
  
  if menu_event
    @menu_events << menu_event
    return menu_event
  else
    return nil
  end
  
end

#send_command(command) ⇒ Object

Sends a command to the server and returns a Response object. This is generally used for internal purposes, but has been opened to the public API in case LCDProc changes it’s behaviour or adds new features. You should NOT use this to bypass the default behaviour such as setting a client’s name.

  • command - The Command object that you wish to send to the server.

Example:

client.send_command( Command.new( ‘info’ ) )



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/lcdproc/client.rb', line 267

def send_command( command )
  
  puts "Command: " + command.message unless not DEBUG
  if command.message.kind_of? String
    
    @daemon_socket.write( command.message )
    @commands << command
    
    # NOTE: This extra get_response is here because LCDd will get confused if sent two messages very
    # quickly when trying to add menu items to a menu. Hopefully it will be removed when they have their
    # commands queued properly.
    if command.message =~ /^menu_add_item/
      r1 = get_response
      
      if r1.message =~ /^success/
        get_response
      end
      
      return r1
    else
      return get_response
    end
    
  elsif command.message.kind_of? Array
    
    @daemon_socket.write( command.message.join("\n") )
    @commands << command
    return get_response( command.message.length )
    
  end
end

#unregister_key_event(key_event) ⇒ Object

Unregisters a button event from the client



301
302
303
304
305
306
307
308
# File 'lib/lcdproc/client.rb', line 301

def unregister_key_event( key_event )
  if @global_key_events.include? key_event
    @global_key_events.delete key_event
    return key_event
  else
    return nil
  end
end

#unregister_menu_event(menu_event) ⇒ Object

Register a new menu event for the client to process



312
313
314
315
316
317
318
319
320
# File 'lib/lcdproc/client.rb', line 312

def unregister_menu_event( menu_event )
  
  if @menu_events.include? menu_event
    @menu_events.delete( menu_event )
    return menu_event
  else
    return nil
  end
end

#valid_keysObject

Returns the keys that may be registered for the client



324
325
326
327
328
329
330
# File 'lib/lcdproc/client.rb', line 324

def valid_keys
  if VALID_KEYS
    VALID_KEYS
  else
    []
  end
end