Use less threads, they're expensive [#9 state:closed]
[amazing.git] / lib / amazing / cli / helpers.rb
blob785e6e2f33fbdad833b065213b3f2d55639fa39f
1 # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 #    http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 module Amazing
16   class CLI
17     module Helpers
18       private
20       def parse_options
21         @options.parse
22       end
24       def set_loglevel
25         begin
26           @log.level = Logger.const_get(@options[:loglevel].upcase)
28         rescue NameError
29           @log.error("Unsupported log level #{@options[:loglevel].inspect}")
30           @log.level = Logger::INFO
31         end
32       end
34       def stop_process(log=true)
35         Process.kill("SIGINT", File.read("#{ENV["HOME"]}/.amazing/pids/#{@display.display}.pid").to_i) 
36         @log.warn("Killed older process") if log
37       rescue
38       end
40       def load_scripts
41         scripts = @options[:include]
43         if @options[:autoinclude]
44           scripts << Dir["#{ENV["HOME"]}/.amazing/widgets/*"]
45         end
47         scripts.flatten.each do |script|
48           if File.exist?(script)
49             @log.debug("Loading script #{script.inspect}")
51             begin
52               Widgets.module_eval(File.read(script), script)
54             rescue SyntaxError => e
55               @log.error("Bad syntax in #{script} at line #{e.to_s.scan(/:(\d+)/)}")
56             end
58           else
59             @log.error("No such widget script #{script.inspect}")
60           end
61         end
62       end
64       def parse_config
65         @log.debug("Parsing configuration file")
67         begin
68           @config = Config.new(@options[:config])
70         rescue
71           @log.fatal("Unable to parse configuration file, exiting")
73           exit 1
74         end
75       end
77       def wait_for_sockets
78         @log.debug("Waiting for awesome control socket for display #{@display.display}")
80         begin
81           Timeout.timeout(30) do
82             sleep 1 until File.exist?("#{ENV["HOME"]}/.awesome_ctl.#{@display.display}")
83             @log.debug("Got socket for display #{@display.display}")
84           end
86         rescue Timeout::Error
87           @log.fatal("Socket for display #{@display.display} not created within 30 seconds, exiting")
89           exit 1
90         end
91       end
93       def save_pid
94         path = "#{ENV["HOME"]}/.amazing/pids"
95         FileUtils.makedirs(path)
97         File.open("#{path}/#{@display.display}.pid", "w+") do |f|
98           f.write($$)
99         end
100       end
102       def remove_pid
103         File.delete("#{ENV["HOME"]}/.amazing/pids/#{@display.display}.pid") rescue Errno::ENOENT
104       end
106       def set_traps
107         trap("SIGINT") do
108           @log.fatal("Received SIGINT, exiting")
109           remove_pid
110           exit
111         end
112       end
114       def update_non_interval
115         @config[:awesome].each do |awesome|
116           awesome[:widgets].each do |widget|
117             next if widget[:interval]
119             @threads << Thread.new(awesome, widget) do |awesome, widget|
120               update_widget(awesome[:screen], awesome[:statusbar], widget)
121             end
122           end
123         end
124       end
126       def update_widget(screen, statusbar, widget, iteration=0)
127         @log.debug("Updating widget #{widget[:identifier]} of type #{widget[:module]} on screen #{screen}")
129         begin
130           mod = Widgets.const_get(widget[:module]).new(widget.merge(:iteration => iteration))
132           if widget[:properties].empty?
133             @awesome.widget_tell(screen, statusbar, widget[:identifier], widget[:property], mod.formatize)
134           end
136           widget[:properties].each do |property, format|
137             @awesome.widget_tell(screen, statusbar, widget[:identifier], property, mod.formatize(format))
138           end
140         rescue WidgetError => e
141           @log.error(widget[:module]) { e.message }
142         end
143       end
145       def join_threads
146         @threads.each {|t| t.join }
147       end
148     end
149   end