From c477498102247f0385cd85ec6a42b74e0a177193 Mon Sep 17 00:00:00 2001 From: fbt Date: Wed, 28 Sep 2016 23:05:57 +0300 Subject: [PATCH] forks! Signed-off-by: fbt --- moltenbar | 151 +++++++++++++++++++++++++++++++++--------------- rc.yaml.example | 17 +++++- 2 files changed, 118 insertions(+), 50 deletions(-) diff --git a/moltenbar b/moltenbar index 106d866..7d2bd59 100755 --- a/moltenbar +++ b/moltenbar @@ -1,29 +1,49 @@ #!/usr/bin/env ruby -require 'yaml' -require 'erb' +$PROGRAM_NAME = "moltenbar" -$queue = Queue.new -$panel_data = Hash.new("") -$children = [] - -class ModBasic - def mainloop() - thread = Thread.new { - self.data_loop() - } - - return thread +# Base worker class +class Worker + def initialize(config, name) + @config = config + @name = name end - def data_loop() - while true do - t = Time.new - $panel_data[:dummy] = "#{t}: I am a dummy!" - $queue << 1 + def run() + @wpid = fork do + $PROGRAM_NAME = "moltenbar: worker (#{@name})" + Signal.trap("INT") do; end + + @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) 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 class ::String @@ -32,16 +52,34 @@ class ::String 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 return `xrandr`.scan(/current (\d+) x (\d+)/).flatten end -at_exit do - $children.each do |pid| - puts "Killing pid: #{pid}" - Process.kill("TERM", pid) - end -end +require 'erb' +require 'yaml' + +workers = [] +panel_data = {} if ENV["XDG_CONFIG_HOME"] == nil conf_dir = ENV["HOME"] + "/.config/moltenbar" @@ -52,44 +90,63 @@ end config_file = conf_dir + "/rc.yaml" mod_dir = conf_dir + "/mod" -$config = YAML.load_file(config_file) +puts config_file -Dir["#{mod_dir}/*"].each do |file| - load file +config = YAML.load_file(config_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 -if $config['geometry'] == nil - $config['gap'] ||= 0 - $config['height'] ||= 14 - - gap = $config['gap'].to_i +Modules.get.each do |k, v| + m = Object.const_get(v).new(config, k) + workers << m - 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() - $config['width'] = y.to_i - (gap * 2) + config['width'] = y.to_i - (gap * 2) end - $config['geometry'] = "#{$config['width']}x#{$config['height']}+#{gap}+#{gap}" + config['geometry'] = "#{config['width']}x#{config['height']}+#{gap}+#{gap}" end lemonbar_cmd = [ "lemonbar", - "-g", $config['geometry'], - "-f", $config['fontspec'], - "-n", $config['window_name'], - "-a", $config['active_areas'], - "-F", $config['colours']['fg'], - "-B", $config['colours']['bg'] + "-g", config['geometry'], + "-f", config['fontspec'], + "-n", config['window_name'], + "-a", config['active_areas'], + "-F", config['colours']['fg'], + "-B", config['colours']['bg'] ] puts "Launching lemonbar as: " + lemonbar_cmd.join(" ") -trap("SIGINT") { exit 0 } -trap("SIGUSR1") { $queue << 1 } +IO.popen(lemonbar_cmd, "w") do |pipe_out| + pipe_out.sync = true -IO.popen(lemonbar_cmd, "w") do |pipe| - while true do - data = $queue.pop - pipe.puts $config['format'].erb + pipe_in.each do |line| + mod_name, data = line.chomp.split("\0") + panel_data[mod_name] = data + + pipe_out.puts config['format'].erb end end diff --git a/rc.yaml.example b/rc.yaml.example index b9e0979..398aaf0 100644 --- a/rc.yaml.example +++ b/rc.yaml.example @@ -1,17 +1,28 @@ # vim: ft=yaml sw=2 et #geometry: '1902x14+9+9' +bspwm: true height: 14 gap: 9 fontspec: '-*-terminesspowerline-medium-*-normal-*-14-*-*-*-*-*-iso10646-*' window_name: 'moltenbar' 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: fg: '#f9f9f9' fg_focused: '#f9f9f9' bg: '#29303A' bg_focused: '#467EC2' + +# Module settings mod: bspwm_pager: colour_focused: '#f9f9f9' @@ -19,8 +30,8 @@ mod: blacklist: -h bspwm_taskbar: - begin: "%{F<%= $config['colours']['bg'] %>}%{B<%= $config['colours']['bg_focused'] %>}%{F-} " - end: "%{F<%= $config['colours']['bg_focused'] %>} %{B-} " + begin: "%{F<%= config['colours']['bg'] %>}%{B<%= config['colours']['bg_focused'] %>}%{F-} " + end: "%{F<%= config['colours']['bg_focused'] %>} %{B-} " date: format: '%A, %Y.%m.%d %H:%M:%S' load_average: