--[[

 24-03-2026 Scripted by Koentje  (remon@cobrasoft.nl)

 Creates needles for a VU meter. Works with output of cava.

 v2.5

]]--


require 'cairo'
local t = os.execute("./vu-cava-loop gocava &")


function conky_gauge(p_unit, p_measure, p_max, p_xr, p_yc)
--[[ NOTES:
  -- "measure" is the type of stat to display; you can choose from 'cpu', 'memperc'... in Conky you would write ${cpu cpu0}
  -- "max" is the maximum value of gauge
  -- "xr" & "yc" are the x/row y/col coordinates for center of gauge
  -- for colors: regular rgb color prefixed with 0x as in 0x######
--]]

  local cs = cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  local cr = cairo_create(cs)
  cairo_surface_destroy(cs)

  --- needle settings ------------------
  local needle_color = 0xff8800                 -- color of indicator needle (default orange) ---
  local needle_alpha = 1
  local needle_radius = 30                      -- bottom of needle rests here --
  local needle_width = 55                       -- length of needle --
  local needle_thickness = 0.02                   -- give needle a thickness (actually width?) so it is visible --
  local start_angle = -140                   -- note: 0 is at "3 o-clock"... example: to start at top/12 o-clock use -90 --
  local end_angle = -39                   -- note: 0 is at "3 o-clock"... example: to start at top/12 o-clock use -90 --
  local angle_0 = (start_angle)*(math.pi/180)
  local angle_f = (end_angle)*(math.pi/180)
  --- --------------------------------------------

  --- individual items ---------------------------
  local measure = p_measure
  local max = p_max
  local xr = p_xr
  local yc = p_yc

  local str = conky_parse(measure)
  local value = tonumber(str)
  if value == nil then value = 250 end
  local pct = value/max
  local pct_needle = (value/max)-needle_thickness     -- we do a minus because needle starts at value & goes back amount set --

  local t_arc = pct*(angle_f-angle_0)
  local t_arc_needle = pct_needle*(angle_f-angle_0)
  -- ---------------------------------------------

  --- draw indicator needle ---
  cairo_arc(cr,xr,yc,needle_radius,angle_0+t_arc_needle,angle_0+t_arc)
  cairo_set_line_width(cr,needle_width)
  cairo_set_source_rgba(cr,rgb_to_r_g_b(needle_color,needle_alpha))
  cairo_stroke(cr)
  cairo_destroy(cr)

return ""
end


-- Color RGB routine
function rgb_to_r_g_b(color,alpha)
  return ((color / 0x10000) % 0x100) / 255., ((color / 0x100) % 0x100) / 255., (color % 0x100) / 255., alpha
end   --- end function rgb_to_r_g_b ---


-- Exit cava-loop.sh
do return end
