wmww / gtk4-layer-shell

A library to create panels and other desktop components for Wayland using the Layer Shell protocol and GTK4
MIT License
128 stars 4 forks source link

Added simple lua example #6

Closed A-Cloud-Ninja closed 1 year ago

A-Cloud-Ninja commented 1 year ago

This adds an example for Lua which uses: ffi from luajit to "preload" the library. lgi to actually load and use both gtk and gtk4-layer-shell.

wmww commented 1 year ago

Can you give me a hand testing this? I don't have any experience with lua but always happy to have more examples.

On Ubuntu 22.04 I run:

$ sudo apt install lua5.4 luajit lua-lgi-dev
$ lua examples/simple-example.lua

It no worky:

lua: examples/simple-example.lua:1: module 'ffi' not found:
    no field package.preload['ffi']
    no file '/usr/local/share/lua/5.4/ffi.lua'
    no file '/usr/local/share/lua/5.4/ffi/init.lua'
    no file '/usr/local/lib/lua/5.4/ffi.lua'
    no file '/usr/local/lib/lua/5.4/ffi/init.lua'
    no file '/usr/share/lua/5.4/ffi.lua'
    no file '/usr/share/lua/5.4/ffi/init.lua'
    no file './ffi.lua'
    no file './ffi/init.lua'
    no file '/usr/local/lib/lua/5.4/ffi.so'
    no file '/usr/lib/x86_64-linux-gnu/lua/5.4/ffi.so'
    no file '/usr/lib/lua/5.4/ffi.so'
    no file '/usr/local/lib/lua/5.4/loadall.so'
    no file './ffi.so'
stack traceback:
    [C]: in function 'require'
    examples/simple-example.lua:1: in main chunk
    [C]: in ?
A-Cloud-Ninja commented 1 year ago

To use Luajits FFI, you have to use Luajit as the interpreter. So swap lua for luajit when running the file. I "target" luajit here. To make it work with another Lua version, it would just require a simple swap out with another ffi library like Alien.

wmww commented 1 year ago

Hmm

$ luajit examples/simple-example.lua
luajit: /usr/share/lua/5.1/lgi/override/Gdk.lua:23: bad argument #1 to 'registerlock' (userdata expected, got nil)
stack traceback:
    [C]: in function 'registerlock'
    /usr/share/lua/5.1/lgi/override/Gdk.lua:23: in main chunk
    [C]: in function 'require'
    /usr/share/lua/5.1/lgi/namespace.lua:183: in function 'require'
    /usr/share/lua/5.1/lgi/namespace.lua:170: in function 'require'
    examples/simple-example.lua:7: in main chunk
    [C]: at 0x560d50c102e0
A-Cloud-Ninja commented 1 year ago

I believe this may be due to release versions of LGI being outdated, and you would have to use a more up to date package per lgi PR 305 https://github.com/lgi-devs/lgi/pull/305 using luarocks or otherwise building/installing manually.

A-Cloud-Ninja commented 1 year ago

If there's anything more I can do to help test this for you please reach out!

wmww commented 1 year ago

Nice, I got it working. I've also added smoke tests for all examples, and I added a smoke test for the Lua example in #17