Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2016-09-28 23:05:57 +03:00
parent 9175caa94d
commit c477498102
2 changed files with 118 additions and 50 deletions

151
moltenbar
View File

@ -1,29 +1,49 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'yaml' $PROGRAM_NAME = "moltenbar"
require 'erb'
$queue = Queue.new # Base worker class
$panel_data = Hash.new("") class Worker
$children = [] def initialize(config, name)
@config = config
class ModBasic @name = name
def mainloop()
thread = Thread.new {
self.data_loop()
}
return thread
end end
def data_loop() def run()
while true do @wpid = fork do
t = Time.new $PROGRAM_NAME = "moltenbar: worker (#{@name})"
$panel_data[:dummy] = "#{t}: I am a dummy!" Signal.trap("INT") do; end
$queue << 1
@pipe_in = File.open(@config['fifo_in'], "w")
@pipe_in.sync = true
mainloop()
end
return @wpid
end
def mainloop()
loop do
@pipe_in.puts "#{@name}\0I am a dummy\n"
sleep(1) sleep(1)
end end
end end
def get_pid()
return @wpid
end
def kill()
begin
if Process.getpgid(@wpid) == Process.getpgid($$)
puts "Killing pid: #{@wpid}"
Process.kill("HUP", @wpid)
end
rescue Errno::ESRCH => err
puts "#{err}: #{@wpid}"
end
end
end end
class ::String class ::String
@ -32,16 +52,34 @@ class ::String
end end
end end
module Modules
@modules = {}
def self.add(name, class_name)
@modules[name] = class_name
end
def self.get
return @modules
end
end
# Kill the workers.
def cleanup(workers)
for w in workers do
w.kill
end
end
def get_x_dims def get_x_dims
return `xrandr`.scan(/current (\d+) x (\d+)/).flatten return `xrandr`.scan(/current (\d+) x (\d+)/).flatten
end end
at_exit do require 'erb'
$children.each do |pid| require 'yaml'
puts "Killing pid: #{pid}"
Process.kill("TERM", pid) workers = []
end panel_data = {}
end
if ENV["XDG_CONFIG_HOME"] == nil if ENV["XDG_CONFIG_HOME"] == nil
conf_dir = ENV["HOME"] + "/.config/moltenbar" conf_dir = ENV["HOME"] + "/.config/moltenbar"
@ -52,44 +90,63 @@ end
config_file = conf_dir + "/rc.yaml" config_file = conf_dir + "/rc.yaml"
mod_dir = conf_dir + "/mod" mod_dir = conf_dir + "/mod"
$config = YAML.load_file(config_file) puts config_file
Dir["#{mod_dir}/*"].each do |file| config = YAML.load_file(config_file)
load file
# Create a named pipe unless one exists
File.mkfifo(config['fifo_in']) unless File.exist?(config['fifo_in'])
File.mkfifo(config['fifo_out']) unless File.exist?(config['fifo_out'])
for m in config['modules'] do
load("#{mod_dir}/#{m}")
end end
if $config['geometry'] == nil Modules.get.each do |k, v|
$config['gap'] ||= 0 m = Object.const_get(v).new(config, k)
$config['height'] ||= 14 workers << m
gap = $config['gap'].to_i
if $config['width'] == nil m.run
end
Signal.trap("INT") do cleanup(workers); end
Signal.trap("TERM") do cleanup(workers); end
pipe_in = File.open(config['fifo_in'], "r")
if config['geometry'] == nil
config['gap'] ||= 0
config['height'] ||= 14
gap = config['gap'].to_i
if config['width'] == nil
y, x = get_x_dims() y, x = get_x_dims()
$config['width'] = y.to_i - (gap * 2) config['width'] = y.to_i - (gap * 2)
end end
$config['geometry'] = "#{$config['width']}x#{$config['height']}+#{gap}+#{gap}" config['geometry'] = "#{config['width']}x#{config['height']}+#{gap}+#{gap}"
end end
lemonbar_cmd = [ lemonbar_cmd = [
"lemonbar", "lemonbar",
"-g", $config['geometry'], "-g", config['geometry'],
"-f", $config['fontspec'], "-f", config['fontspec'],
"-n", $config['window_name'], "-n", config['window_name'],
"-a", $config['active_areas'], "-a", config['active_areas'],
"-F", $config['colours']['fg'], "-F", config['colours']['fg'],
"-B", $config['colours']['bg'] "-B", config['colours']['bg']
] ]
puts "Launching lemonbar as: " + lemonbar_cmd.join(" ") puts "Launching lemonbar as: " + lemonbar_cmd.join(" ")
trap("SIGINT") { exit 0 } IO.popen(lemonbar_cmd, "w") do |pipe_out|
trap("SIGUSR1") { $queue << 1 } pipe_out.sync = true
IO.popen(lemonbar_cmd, "w") do |pipe| pipe_in.each do |line|
while true do mod_name, data = line.chomp.split("\0")
data = $queue.pop panel_data[mod_name] = data
pipe.puts $config['format'].erb
pipe_out.puts config['format'].erb
end end
end end

View File

@ -1,17 +1,28 @@
# vim: ft=yaml sw=2 et # vim: ft=yaml sw=2 et
#geometry: '1902x14+9+9' #geometry: '1902x14+9+9'
bspwm: true
height: 14 height: 14
gap: 9 gap: 9
fontspec: '-*-terminesspowerline-medium-*-normal-*-14-*-*-*-*-*-iso10646-*' fontspec: '-*-terminesspowerline-medium-*-normal-*-14-*-*-*-*-*-iso10646-*'
window_name: 'moltenbar' window_name: 'moltenbar'
active_areas: '128' active_areas: '128'
format: "<%= $panel_data[:bspwm_pager] %><%= $panel_data[:bspwm_taskbar] %>%{r}%{F<%= $config['colours']['bg_focused'] %>}%{F-}%{B<%= $config['colours']['bg_focused'] %>} LA: <%= $panel_data[:load_average] %> Network: <%= $panel_data[:network] %> <%= $panel_data[:date] %> %{B-}" fifo_in: "/run/user/1000/moltenbar_in.fifo"
fifo_out: "/run/user/1000/moltenbar_out.fifo"
modules:
- bspwm_pager
- date
- bspwm_taskbar
- load_average
- network
format: "<%= panel_data['bspwm_pager'] %><%= panel_data['bspwm_taskbar'] %>%{r}%{F<%= config['colours']['bg_focused'] %>}%{F-}%{B<%= config['colours']['bg_focused'] %>} LA: <%= panel_data['load_average'] %> Network: <%= panel_data['network'] %> <%= panel_data['date'] %> %{B-}"
colours: colours:
fg: '#f9f9f9' fg: '#f9f9f9'
fg_focused: '#f9f9f9' fg_focused: '#f9f9f9'
bg: '#29303A' bg: '#29303A'
bg_focused: '#467EC2' bg_focused: '#467EC2'
# Module settings
mod: mod:
bspwm_pager: bspwm_pager:
colour_focused: '#f9f9f9' colour_focused: '#f9f9f9'
@ -19,8 +30,8 @@ mod:
blacklist: blacklist:
-h -h
bspwm_taskbar: bspwm_taskbar:
begin: "%{F<%= $config['colours']['bg'] %>}%{B<%= $config['colours']['bg_focused'] %>}%{F-} " begin: "%{F<%= config['colours']['bg'] %>}%{B<%= config['colours']['bg_focused'] %>}%{F-} "
end: "%{F<%= $config['colours']['bg_focused'] %>} %{B-} " end: "%{F<%= config['colours']['bg_focused'] %>} %{B-} "
date: date:
format: '%A, %Y.%m.%d %H:%M:%S' format: '%A, %Y.%m.%d %H:%M:%S'
load_average: load_average: