1 # Copyright (C) 2008 Dag Odenhall <dag.odenhall@gmail.com>
2 # Licensed under the Academic Free License version 3.0
13 # Command line interface runner
20 @log = Logger.new(STDOUT)
21 @options = Options.new(@args)
23 @display = X11::DisplayName.new
24 rescue X11::EmptyDisplayName => e
25 @log.warn("#{e.message}, falling back on :0")
26 @display = X11::DisplayName.new(":0")
27 rescue X11::InvalidDisplayName => e
28 @log.fatal("#{e.message}, exiting")
35 @log.fatal("Received SIGINT, exiting")
40 show_help if @options[:help]
42 stop_process(true) if @options[:stop]
45 list_widgets if @options[:listwidgets]
46 test_widget if @options[:test]
48 @awesome = Awesome.new(@display.display)
49 explicit_updates unless @options[:update] == []
55 @config["awesome"].each do |screen, widgets|
56 widgets.each do |widget_name, settings|
57 if settings["interval"] && count % settings["interval"] == 0
58 update_widget(screen, widget_name)
76 @log.level = Logger.const_get(@options[:loglevel].upcase)
78 @log.error("Unsupported log level #{@options[:loglevel].inspect}")
79 @log.level = Logger::INFO
83 def stop_process(quit=false)
85 Process.kill("SIGINT", File.read("#{ENV["HOME"]}/.amazing/pids/#{@display.display}.pid").to_i)
86 @log.warn("Killed older process") unless quit
93 scripts = @options[:include]
94 @config["include"].each do |script|
95 script = File.expand_path(script, File.dirname(@options[:config]))
98 if @options[:autoinclude]
99 scripts << Dir["#{ENV["HOME"]}/.amazing/widgets/*"]
101 scripts.flatten.each do |script|
102 if File.exist?(script)
103 @log.debug("Loading script #{script.inspect}")
105 Widgets.module_eval(File.read(script), script)
106 rescue SyntaxError => e
107 @log.error("Bad syntax in #{script} at line #{e.to_s.scan(/:(\d+)/)}")
110 @log.error("No such widget script #{script.inspect}")
116 if @options[:listwidgets] == true
117 longest_widget_name = Widgets.constants.inject {|a,b| a.length > b.length ? a : b }.length
118 Widgets.constants.sort.each do |widget|
119 widget_class = Widgets.const_get(widget)
120 puts "%-#{longest_widget_name}s : %s" % [widget, widget_class.description]
123 widget_class = Widgets.const_get(@options[:listwidgets].camel_case)
125 puts "#{@options[:listwidgets].camel_case} - #{widget_class.description}"
127 dependencies = widget_class.dependencies
128 unless dependencies.empty?
129 longest_dependency_name = dependencies.keys.inject {|a,b| a.to_s.length > b.to_s.length ? a : b }.to_s.length
130 longest_dependency_name = 10 if longest_dependency_name < 10
131 longest_description = dependencies.values.inject {|a,b| a.length > b.length ? a : b }.length
132 longest_description = 11 if longest_description < 11
133 puts " %-#{longest_dependency_name}s | DESCRIPTION" % "DEPENDENCY"
134 puts "-" * (longest_dependency_name + longest_description + 5)
135 dependencies.keys.sort.each do |dependency|
136 puts " %-#{longest_dependency_name}s | #{dependencies[dependency]}" % dependency
140 options = widget_class.options
141 unless options.empty?
142 longest_option_name = options.keys.inject {|a,b| a.to_s.length > b.to_s.length ? a : b }.to_s.length
143 longest_option_name = 6 if longest_option_name < 6
144 longest_description = options.values.inject {|a,b| a[:description].length > b[:description].length ? a : b }[:description].length
145 longest_description = 11 if longest_description < 11
146 longest_default = options.values.inject {|a,b| a[:default].inspect.length > b[:default].inspect.length ? a : b }[:default].inspect.length
147 longest_default = 7 if longest_default < 7
148 puts " %-#{longest_option_name}s | %-#{longest_description}s | DEFAULT" % ["OPTION", "DESCRIPTION"]
149 puts "-" * (longest_option_name + longest_description + longest_default + 8)
150 options.keys.sort_by {|option| option.to_s }.each do |option|
151 puts " %-#{longest_option_name}s | %-#{longest_description}s | %s" % [option, options[option][:description], options[option][:default].inspect]
155 fields = widget_class.fields
157 longest_field_name = fields.keys.inject {|a,b| a.to_s.length > b.to_s.length ? a : b }.to_s.length
158 longest_field_name = 5 if longest_field_name < 5
159 longest_description = fields.values.inject {|a,b| a[:description].length > b[:description].length ? a : b }[:description].length
160 longest_description = 11 if longest_description < 11
161 longest_default = fields.values.inject {|a,b| a[:default].inspect.length > b[:default].inspect.length ? a : b }[:default].inspect.length
162 longest_default = 7 if longest_default < 7
163 puts " %-#{longest_field_name + 1}s | %-#{longest_description}s | DEFAULT" % ["FIELD", "DESCRIPTION"]
164 puts "-" * (longest_field_name + longest_description + longest_default + 9)
165 fields.keys.sort_by {|field| field.to_s }.each do |field|
166 puts " @%-#{longest_field_name}s | %-#{longest_description}s | %s" % [field, fields[field][:description], fields[field][:default].inspect]
175 widget = Widgets.const_get(@options[:test].camel_case)
176 settings = YAML.load("{#{ARGV[0]}}")
177 instance = widget.new("test", settings)
178 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
179 puts "@%-#{longest_field_name}s = %s" % [:default, instance.instance_variable_get(:@default).inspect]
180 widget.fields.keys.sort_by {|field| field.to_s }.each do |field|
181 puts "@%-#{longest_field_name}s = %s" % [field, instance.instance_variable_get("@#{field}".to_sym).inspect]
187 @log.debug("Parsing configuration file")
189 @config = YAML.load_file(@options[:config])
191 @log.fatal("Unable to parse configuration file, exiting")
194 @config["include"] ||= []
198 @log.debug("Waiting for awesome control socket for display #{@display.display}")
200 Timeout.timeout(30) do
201 sleep 1 until File.exist?("#{ENV["HOME"]}/.awesome_ctl.#{@display.display}")
202 @log.debug("Got socket for display #{@display.display}")
204 rescue Timeout::Error
205 @log.fatal("Socket for display #{@display.display} not created within 30 seconds, exiting")
211 @config["awesome"].each do |screen, widgets|
212 widgets.each_key do |widget_name|
213 next unless @options[:update] == :all || @options[:update].include?(widget_name)
214 update_widget(screen, widget_name, false)
221 path = "#{ENV["HOME"]}/.amazing/pids"
222 FileUtils.makedirs(path)
223 File.open("#{path}/#{@display.display}.pid", "w+") do |f|
229 File.delete("#{ENV["HOME"]}/.amazing/pids/#{@display.display}.pid") rescue Errno::ENOENT
232 def update_non_interval
233 @config["awesome"].each do |screen, widgets|
234 widgets.each do |widget_name, settings|
235 next if settings["interval"]
236 update_widget(screen, widget_name)
241 def update_widget(screen, widget_name, threaded=true)
242 settings = @config["awesome"][screen][widget_name]
243 type = settings["type"].camel_case
244 @log.debug("Updating widget #{widget_name} of type #{type} on screen #{screen}")
247 widget = Widgets.const_get(type).new(widget_name, settings)
248 @awesome.widget_tell(screen, widget_name, widget.formatize)
249 rescue WidgetError => e
250 @log.error(type) { e.message }