moltenbar/moltenbar

153 lines
2.6 KiB
Ruby
Executable File

#!/usr/bin/env ruby
$PROGRAM_NAME = "moltenbar"
# Base worker class
class Worker
def initialize(config, name)
@config = config
@name = name
end
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
def erb
return ERB.new(self).result
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
require 'erb'
require 'yaml'
workers = []
panel_data = {}
if ENV["XDG_CONFIG_HOME"] == nil
conf_dir = ENV["HOME"] + "/.config/moltenbar"
else
conf_dir = ENV["XDG_CONFIG_HOME"] + "/moltenbar"
end
config_file = conf_dir + "/rc.yaml"
mod_dir = conf_dir + "/mod"
puts config_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
Modules.get.each do |k, v|
m = Object.const_get(v).new(config, k)
workers << m
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)
end
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']
]
puts "Launching lemonbar as: " + lemonbar_cmd.join(" ")
IO.popen(lemonbar_cmd, "w") do |pipe_out|
pipe_out.sync = true
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