--[[  v1.3
2024-01-19 Scripted by Koentje
2024-09-29 Fixed nil value warning (Koentje)
2025-09-29 Added alpha transparency option

                                            _____________ vertical position in pixels
                                           |  ___________ horizontal position in pixels
                                           | |  _________ width of image
                                           | | |    _____ height of image
                                           | | |   |    _ Blink delay
                                           | | |   |   |
usage:  ${lua conky_image /path/to/picture 2 2 380 425 1}

]]

require 'cairo'
require "imlib2"


function fDrawBlinker(bcr,path,xx,yy,ww,hh,bb)
    local img =  cairo_image_surface_create_from_png(path)
    local w_img = cairo_image_surface_get_width(img)
    local h_img = cairo_image_surface_get_height(img)
    alph = 100
    if bb == nil then bb = 2 end
--    if bb <= 2 then 

    local secs = conky_parse("${updates}")
    local seconds = tonumber(secs)
    local blink = tonumber(bb)

    if blink >= 1 then
       if (seconds % blink == 0) then
         img = img
       else
         img = "./images/blink-empty.png"
       end
    else
       img = "./images/blink-empty.png"
    end

--[[
    local tt = tonumber(tt)
    if tt == nil then tt = 100 end
    if tt <= 100 then
      if tt >= 0 then
        alph = (tt / 100)
      else
        alph = 100
      end
    else
     alph = 100
    end
]]--

    cairo_save(bcr)
    cairo_translate (bcr, xx, yy)
    cairo_scale (bcr, ww/w_img, hh/h_img)
    cairo_set_source_surface (bcr, img, xx + 6, yy + 3)
    cairo_paint_with_alpha (bcr, alph)
    cairo_surface_destroy (img)
    collectgarbage ()
    cairo_restore (bcr)
end


function conky_blink(img,xxx,yyy,www,hhh,ttt)
  if conky_window==nil then return '' end
  local bcs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
  local bcr=cairo_create(bcs)
  local updates=conky_parse('${updates}')
  update_num=tonumber(updates)

    fDrawBlinker(bcr,img,xxx,yyy,www,hhh,ttt)

   cairo_surface_destroy(bcs)
   cairo_destroy(bcr)
  return ''
end
