2012-05-27 19:20:34 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2012 Uli Schlachter
|
|
|
|
-- @release @AWESOME_VERSION@
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local type = type
|
|
|
|
local capi = { awesome = awesome }
|
|
|
|
local cairo = require("lgi").cairo
|
|
|
|
|
Bump minimum lgi dependency to 0.7.0
Before commit 1b2826 in lgi, the get_rgba() function on cairo SolidPatterns was
specified like this:
get_rgba = { ret = cairo.Status,
{ ti.double, dir = 'out' },
{ ti.double, dir = 'out' },
{ ti.double, dir = 'out' } },
The above commit fixed this (without saying so) and the code became:
get_rgba = { ret = cairo.Status,
{ ti.double, dir = 'out' },
{ ti.double, dir = 'out' },
{ ti.double, dir = 'out' },
{ ti.double, dir = 'out' } },
The prototype for the corresponding cairo function is:
cairo_public cairo_status_t
cairo_pattern_get_rgba (cairo_pattern_t *pattern,
double *red, double *green,
double *blue, double *alpha);
As you see, this functions gets four double* as arguments and it will save its
result via those pointers. Old versions of lgi call this function with too few
arguments and this will cause a segmentation fault when cairo dereferences an
invalid pointer.
Signed-off-by: Uli Schlachter <psychon@znc.in>
2014-03-17 17:52:09 +01:00
|
|
|
-- Keep this in sync with build-utils/lgi-check.sh!
|
|
|
|
local ver_major, ver_minor = string.match(require('lgi.version'), '(%d)%.(%d)')
|
|
|
|
if tonumber(ver_major) <= 0 and tonumber(ver_minor) < 7 then
|
|
|
|
error("lgi too old, need at least version 0.7.0")
|
2012-06-05 16:22:04 +02:00
|
|
|
end
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
-- gears.surface
|
|
|
|
local surface = { mt = {} }
|
2014-03-29 21:41:47 +01:00
|
|
|
local surface_cache = setmetatable({}, { __mode = 'v' })
|
2012-05-27 19:20:34 +02:00
|
|
|
|
|
|
|
--- Try to convert the argument into an lgi cairo surface.
|
|
|
|
-- This is usually needed for loading images by file name.
|
2014-03-29 21:41:47 +01:00
|
|
|
function surface.load_uncached(_surface)
|
2012-05-27 19:20:34 +02:00
|
|
|
-- Nil is not changed
|
2012-06-12 10:13:46 +02:00
|
|
|
if not _surface then
|
2012-05-27 19:20:34 +02:00
|
|
|
return nil
|
|
|
|
end
|
2014-03-29 21:41:47 +01:00
|
|
|
-- Remove from cache if it was cached
|
|
|
|
surface_cache[_surface] = nil
|
2012-05-27 19:20:34 +02:00
|
|
|
-- lgi cairo surfaces don't get changed either
|
2012-06-12 10:13:46 +02:00
|
|
|
if cairo.Surface:is_type_of(_surface) then
|
|
|
|
return _surface
|
2012-05-27 19:20:34 +02:00
|
|
|
end
|
|
|
|
-- Strings are assumed to be file names and get loaded
|
2012-06-12 10:13:46 +02:00
|
|
|
if type(_surface) == "string" then
|
|
|
|
_surface = capi.awesome.load_image(_surface)
|
2012-05-27 19:20:34 +02:00
|
|
|
end
|
|
|
|
-- Everything else gets forced into a surface
|
2012-06-12 10:13:46 +02:00
|
|
|
return cairo.Surface(_surface, true)
|
2012-05-27 19:20:34 +02:00
|
|
|
end
|
|
|
|
|
2014-03-29 21:41:47 +01:00
|
|
|
function surface.load(_surface)
|
|
|
|
if type(_surface) == "string" then
|
|
|
|
local cache = surface_cache[_surface]
|
|
|
|
if not cache then
|
|
|
|
cache = surface.load_uncached(_surface)
|
|
|
|
surface_cache[_surface] = cache
|
|
|
|
end
|
|
|
|
return cache
|
|
|
|
end
|
|
|
|
return surface.load_uncached(_surface)
|
|
|
|
end
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
function surface.mt:__call(...)
|
|
|
|
return surface.load(...)
|
|
|
|
end
|
|
|
|
|
2014-03-16 03:00:23 +01:00
|
|
|
--- Get the size of a cairo surface
|
|
|
|
-- @param surf The surface you are interested in
|
|
|
|
-- @return The surface's width and height
|
|
|
|
function surface.get_size(surf)
|
|
|
|
local cr = cairo.Context(surf)
|
|
|
|
local x, y, w, h = cr:clip_extents()
|
|
|
|
return w - x, h - y
|
|
|
|
end
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
return setmetatable(surface, surface.mt)
|
2012-05-27 19:20:34 +02:00
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|