Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cairo Native Interop? #317

Open
A-Cloud-Ninja opened this issue Apr 16, 2024 · 3 comments
Open

Cairo Native Interop? #317

A-Cloud-Ninja opened this issue Apr 16, 2024 · 3 comments

Comments

@A-Cloud-Ninja
Copy link

Taking this as my basic example for lack of better explanation

local lgi = require("lgi")
local Gtk = lgi.require("Gtk", "3.0")
local cairo = lgi.cairo
local window = Gtk.Window {
	title = "Drawing Area",
	default_width = 600,
	default_height = 600,
	on_destroy = Gtk.main_quit
}

function window:on_draw(cr)
	--Here's the context `cr`
	--Lets assume this is passed by a controlling C process:
	local ptr = cr._native
	--Lets try to get an LGI context from the pointer:
	local ctx = cairo.Context(ptr) -- This will fail, because it expects a surface
end

local da = Gtk.DrawingArea {
	expand = true
}

window:add(da)
window:show_all()
Gtk:main()

Is there a proper way of retrieving the actual cairo.Context object being used with lgi here, or should a workaround be found?

@A-Cloud-Ninja
Copy link
Author

I should add, removing the override from lgi/overrides/cairo.lua for the create method does indeed allow cairo.Context(ptr) to work appropriately here.

function window:on_draw(__ctx)
	local ptr = __ctx._native
	local cr = cairo.Context(ptr)
	cr:set_source_rgb(0, 0, 0)
	cr:paint()
	cr:set_source_rgb(1, 1, 1)
	cr:rectangle(10, 10, 100, 100)
	cr:fill()
	return true
end

@A-Cloud-Ninja
Copy link
Author

Found a workaround for at least this specific issue:

local lgi = require("lgi")
local cairo = lgi.cairo
local core = require("lgi.core")


local ctx_ptr = some_C_controlled_call()
local context = core.record.new(cairo.Context,ctx_ptr)

And it works! But it also seems like a janky workaround. Feel free to close if this is the best intended method.

@psychon
Copy link
Collaborator

psychon commented Apr 17, 2024

I actually do not have Lua installed anymore and cannot try this myself, but AwesomeWM does the following:
https://github.com/awesomeWM/awesome/blob/8b1f8958b46b3e75618bc822d512bb4d449a89aa/lib/gears/surface.lua#L66-L67
No idea where that true comes from. I don't think something like this exists in cairo's C API. And from a quick browse through doc/, I also did not find anything else.

Besides that: Dunno. Your guess is as good as mine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants