SimpleConkyScript/lua/main.lua

92 lines
3.6 KiB
Lua

-- HorlogeSkynet's Lua script (for Conky) -- Main entry points
-- <https://git.forestier.app/HorlogeSkynet/SimpleConkyScript.git>
local NULL = require('cjson').null
local signal = require('posix.signal')
-- These module references will be populated below.
local config, drawing, loading, system
-- This value will be populated below too.
local _config_timers
-- Conky's `lua_startup_hook`.
function conky_startup(project_path)
-- Check whether this required global variable has been already populated by Conky.
if not conky_config then return end
-- Load the Cairo library here.
require('cairo')
-- Load the `posix.signal` module here.
signal = require('posix.signal')
-- Instantiate and populate a `config` singleton that will be passed to other Lua modules.
config = loadfile(project_path .. "lua/config.lua")(project_path)
-- Load our own Lua modules and populate internal references.
system = loadfile(project_path .. "lua/system.lua")(config)
drawing = loadfile(project_path .. "lua/drawing.lua")(config)
loading = loadfile(project_path .. "lua/loading.lua")(config, system, drawing)
-- Store the `_config_timers` value, defined by the user.
_config_timers = config.get_entries('settings').timers
end
-- Conky's `lua_draw_hook_pre`.
function conky_main(project_path, width, height)
-- When this Lua script is reloaded by Conky, old references are broken BUT `conky_startup` hook is not called.
-- Let's rely on the `_config_timers` variable to detect and handle this behavior.
if not _config_timers then conky_startup(project_path) end
-- In order to create a Cairo surface, we'll need this global variable.
if not conky_window then return end
local surface = cairo_xlib_surface_create(
conky_window.display,
conky_window.drawable, conky_window.visual,
conky_window.width, conky_window.height)
if not surface then return end
local display = cairo_create(surface)
if not display then return end
-- Evaluate the current number of updates once.
local number_of_updates = tonumber(conky_parse("${updates}"))
-- While the startup threshold has not been exceeded, stop the main execution here.
if number_of_updates <= _config_timers.startup_threshold then return end
-- We are ready to go ! Load each configures entities here.
loading.load_entities(display, number_of_updates)
-- Explanations for below statements :
-- When conditions are met, the script is making Conky reload its own configuration with a SIGHUP signal.
-- Has the user specified any configuration auto-reloading ?
if (_config_timers.config_autoreload ~= NULL and
number_of_updates % _config_timers.config_autoreload == 0) and
config.load_config(project_path) then
signal.raise(signal.SIGHUP)
-- Has the user specified any screen resolution change auto-detection ?
elseif (_config_timers.resolution_autodetect ~= NULL and
number_of_updates % _config_timers.resolution_autodetect == 0) then
local screen_width, screen_height = system.get_screen_resolution()
-- Detect changes between "old" and "new" resolutions.
-- "old" resolution is passed as `conky_main` parameters during `lua_draw_hook_pre` call.
if screen_width ~= tonumber(width) or
screen_height ~= tonumber(height) then
signal.raise(signal.SIGHUP)
end
end
-- -----------------------------------
-- Delete display and surface before ending this iteration.
cairo_destroy(display)
cairo_surface_destroy(surface)
end