1 # Copyright (C) 2008 Dag Odenhall <dag.odenhall@gmail.com>
2 # Licensed under the Academic Free License version 3.0
5 require 'amazing/options'
6 require 'amazing/x11/display_name'
7 require 'amazing/config'
8 require 'amazing/string'
9 require 'amazing/widget'
10 require 'amazing/proc_file'
11 require 'amazing/widgets'
12 require 'amazing/awesome'
19 # Command line interface runner
25 @log = Logger.new(STDOUT)
26 @options = Options.new(@args)
28 @display = X11::DisplayName.new
29 rescue X11::EmptyDisplayName => e
30 @log.warn("#{e.message}, falling back on :0")
31 @display = X11::DisplayName.new(":0")
32 rescue X11::InvalidDisplayName => e
33 @log.fatal("#{e.message}, exiting")
42 Thread.list.each {|t| t.exit }
45 @log.fatal("Received SIGINT, exiting")
49 show_help if @options[:help]
51 stop_process(true) if @options[:stop]
53 list_widgets if @options[:listwidgets]
54 test_widget if @options[:test]
57 @awesome = Awesome.new(@display.display)
58 explicit_updates unless @options[:update] == []
62 @config[:awesome].each do |awesome|
63 awesome[:widgets].each do |widget|
65 @threads << Thread.new(awesome, widget) do |awesome, widget|
68 Thread.new { update_widget(awesome[:screen], awesome[:statusbar], widget, iteration) }
70 sleep widget[:interval]
76 @threads.each {|t| t.join }
88 @log.level = Logger.const_get(@options[:loglevel].upcase)
90 @log.error("Unsupported log level #{@options[:loglevel].inspect}")
91 @log.level = Logger::INFO
95 def stop_process(quit=false)
97 Process.kill("SIGINT", File.read("#{ENV["HOME"]}/.amazing/pids/#{@display.display}.pid").to_i)
98 @log.warn("Killed older process") unless quit
105 scripts = @options[:include]
106 if @options[:autoinclude]
107 scripts << Dir["#{ENV["HOME"]}/.amazing/widgets/*"]
109 scripts.flatten.each do |script|
110 if File.exist?(script)
111 @log.debug("Loading script #{script.inspect}")
113 Widgets.module_eval(File.read(script), script)
114 rescue SyntaxError => e
115 @log.error("Bad syntax in #{script} at line #{e.to_s.scan(/:(\d+)/)}")
118 @log.error("No such widget script #{script.inspect}")
124 @log.debug("Parsing configuration file")
126 @config = Config.new(@options[:config])
128 @log.fatal("Unable to parse configuration file, exiting")
134 if @options[:listwidgets] == true
135 longest_widget_name = Widgets.constants.inject {|a,b| a.length > b.length ? a : b }.length
136 Widgets.constants.sort.each do |widget|
137 widget_class = Widgets.const_get(widget)
138 puts "%-#{longest_widget_name}s : %s" % [widget, widget_class.description]
141 widget_class = Widgets.const_get(@options[:listwidgets].camel_case)
143 puts "#{@options[:listwidgets].camel_case} - #{widget_class.description}"
145 dependencies = widget_class.dependencies
146 unless dependencies.empty?
147 longest_dependency_name = dependencies.keys.inject {|a,b| a.to_s.length > b.to_s.length ? a : b }.to_s.length
148 longest_dependency_name = 10 if longest_dependency_name < 10
149 longest_description = dependencies.values.inject {|a,b| a.length > b.length ? a : b }.length
150 longest_description = 11 if longest_description < 11
151 puts " %-#{longest_dependency_name}s | DESCRIPTION" % "DEPENDENCY"
152 puts "-" * (longest_dependency_name + longest_description + 5)
153 dependencies.keys.sort.each do |dependency|
154 puts " %-#{longest_dependency_name}s | #{dependencies[dependency]}" % dependency
158 options = widget_class.options
159 unless options.empty?
160 longest_option_name = options.keys.inject {|a,b| a.to_s.length > b.to_s.length ? a : b }.to_s.length
161 longest_option_name = 6 if longest_option_name < 6
162 longest_description = options.values.inject {|a,b| a[:description].length > b[:description].length ? a : b }[:description].length
163 longest_description = 11 if longest_description < 11
164 longest_default = options.values.inject {|a,b| a[:default].inspect.length > b[:default].inspect.length ? a : b }[:default].inspect.length
165 longest_default = 7 if longest_default < 7
166 puts " %-#{longest_option_name}s | %-#{longest_description}s | DEFAULT" % ["OPTION", "DESCRIPTION"]
167 puts "-" * (longest_option_name + longest_description + longest_default + 8)
168 options.keys.sort_by {|option| option.to_s }.each do |option|
169 puts " %-#{longest_option_name}s | %-#{longest_description}s | %s" % [option, options[option][:description], options[option][:default].inspect]
173 fields = widget_class.fields
175 longest_field_name = fields.keys.inject {|a,b| a.to_s.length > b.to_s.length ? a : b }.to_s.length
176 longest_field_name = 5 if longest_field_name < 5
177 longest_description = fields.values.inject {|a,b| a[:description].length > b[:description].length ? a : b }[:description].length
178 longest_description = 11 if longest_description < 11
179 longest_default = fields.values.inject {|a,b| a[:default].inspect.length > b[:default].inspect.length ? a : b }[:default].inspect.length
180 longest_default = 7 if longest_default < 7
181 puts " %-#{longest_field_name + 1}s | %-#{longest_description}s | DEFAULT" % ["FIELD", "DESCRIPTION"]
182 puts "-" * (longest_field_name + longest_description + longest_default + 9)
183 fields.keys.sort_by {|field| field.to_s }.each do |field|
184 puts " @%-#{longest_field_name}s | %-#{longest_description}s | %s" % [field, fields[field][:description], fields[field][:default].inspect]
193 widget = Widgets.const_get(@options[:test].camel_case)
194 settings = YAML.load("{#{ARGV[0]}}")
195 instance = widget.new(settings)
196 longest_field_name = widget.fields.merge({:default => nil}).keys.inject {|a,b| a.to_s.length > b.to_s.length ? a : b }.to_s.length
197 puts "@%-#{longest_field_name}s = %s" % [:default, instance.instance_variable_get(:@default).inspect]
198 widget.fields.keys.sort_by {|field| field.to_s }.each do |field|
199 puts "@%-#{longest_field_name}s = %s" % [field, instance.instance_variable_get("@#{field}".to_sym).inspect]
205 @log.debug("Waiting for awesome control socket for display #{@display.display}")
207 Timeout.timeout(30) do
208 sleep 1 until File.exist?("#{ENV["HOME"]}/.awesome_ctl.#{@display.display}")
209 @log.debug("Got socket for display #{@display.display}")
211 rescue Timeout::Error
212 @log.fatal("Socket for display #{@display.display} not created within 30 seconds, exiting")
218 @config[:awesome].each do |awesome|
219 awesome[:widgets].each do |widget|
220 locator = "%s/%s/%s" % [widget[:identifier], awesome[:statusbar], awesome[:screen]]
221 next unless @options[:update] == :all || @options[:update].include?(locator)
222 @threads << Thread.new(awesome, widget) do |awesome, widget|
223 update_widget(awesome[:screen], awesome[:statusbar], widget)
227 @threads.each {|t| t.join }
232 path = "#{ENV["HOME"]}/.amazing/pids"
233 FileUtils.makedirs(path)
234 File.open("#{path}/#{@display.display}.pid", "w+") do |f|
240 File.delete("#{ENV["HOME"]}/.amazing/pids/#{@display.display}.pid") rescue Errno::ENOENT
243 def update_non_interval
244 @config[:awesome].each do |awesome|
245 awesome[:widgets].each do |widget|
246 next if widget[:interval]
247 @threads << Thread.new(awesome, widget) do |awesome, widget|
248 update_widget(awesome[:screen], awesome[:statusbar], widget)
252 @threads.each {|t| t.join }
255 def update_widget(screen, statusbar, widget, iteration=0)
256 @log.debug("Updating widget #{widget[:identifier]} of type #{widget[:module]} on screen #{screen}")
258 mod = Widgets.const_get(widget[:module]).new(widget.merge(:iteration => iteration))
259 if widget[:properties].empty?
260 Thread.new { @awesome.widget_tell(screen, statusbar, widget[:identifier], widget[:property], mod.formatize) }
262 widget[:properties].each do |property, format|
263 Thread.new { @awesome.widget_tell(screen, statusbar, widget[:identifier], property, mod.formatize(format)) }
265 rescue WidgetError => e
266 @log.error(widget[:module]) { e.message }