Yay working Lua API

This commit is contained in:
Casey 2024-04-09 18:33:54 +03:00
parent 6e9ea2dfbf
commit 12c80a3eab
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
7 changed files with 94 additions and 14 deletions

View File

@ -1,6 +1,6 @@
CFLAGS +=
LDFLAGS := -lm -lcairo -lxcb
OBJECTS := obj/rootwindow.o obj/cairo_context.o
LDFLAGS := -lm -lcairo -lxcb -llua
OBJECTS := obj/common.o obj/rootwindow.o obj/cairo_context.o obj/api_draw.o
livewp: lib
$(CC) $(CFLAGS) $(OBJECTS) src/main.c $(LDFLAGS) -o livewp

View File

@ -1,11 +1,12 @@
local img = assert(Image.load("/home/hkc/images/wallpapers/wallpaper.png"))
local font = assert(Font.load("/usr/share/fonts/TTF/TerminusTTF.ttf"))
-- local img = assert(Image.load("/home/hkc/images/wallpapers/wallpaper.png"))
-- local font = assert(Font.load("/usr/share/fonts/TTF/TerminusTTF.ttf"))
function tick()
function tick(t)
-- Background
Screen.clear(0xFF131313)
Draw.clear(math.floor(t * 255))
--[[
-- Color ARGB, x, y
Draw.pixel(0xFFFF0000, 30, 30)
@ -29,6 +30,7 @@ function tick()
-- image ptr x, y, w, h, src_x, src_y, src_w, src_h
Draw.image( img, 0, 0) --> Rect
]]
end
function unload()

30
src/api_draw.c Normal file
View File

@ -0,0 +1,30 @@
#include "common.h"
#include "api_draw.h"
#include <cairo/cairo.h>
#include <lua5.3/lua.h>
#include <lua5.3/lauxlib.h>
int api_draw_clear(lua_State *L);
bool loadapi_draw(lua_State *lua) {
const struct luaL_Reg api_draw[] = {
{ "clear", api_draw_clear },
};
lua_newtable(lua);
luaL_setfuncs(lua, api_draw, 0);
lua_setglobal(lua, "Draw");
return true;
}
int api_draw_clear(lua_State *L) {
printf("Draw.clear()\n");
int color = luaL_checkinteger(L, 1);
printf("Draw.clear(0x%08x)\n", color);
cairo_t *cr = global_context.cairo_context.cairo;
int r = (color >> 16) & 0xFF,
g = (color >> 8) & 0xFF,
b = color & 0xFF;
cairo_set_source_rgb(cr, r / 255.0, g / 255.0, b / 255.0);
cairo_paint(cr);
return 0;
}

9
src/api_draw.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _API_DRAW_H_
#define _API_DRAW_H_
#include <lua5.3/lua.h>
#include <stdbool.h>
bool loadapi_draw(lua_State *lua);
#endif

3
src/common.c Normal file
View File

@ -0,0 +1,3 @@
#include "common.h"
struct global_context global_context = { 0 };

13
src/common.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef _COMMON_H_
#define _COMMON_H_
#include <lua5.3/lua.h>
#include "cairo_context.h"
extern struct global_context {
lua_State *lua;
struct cairo_ctx cairo_context;
double time;
} global_context;
#endif

View File

@ -1,3 +1,7 @@
#include <lua5.2/lualib.h>
#include <lua5.3/lua.h>
#include <lua5.3/luaconf.h>
#include <lua5.3/lauxlib.h>
#include <signal.h>
#include <stdio.h>
#include <math.h>
@ -5,13 +9,14 @@
#include <unistd.h>
#include "cairo_context.h"
struct cairo_ctx ctx;
#include "common.h"
#include "api_draw.h"
volatile bool running = true;
void cleanup(void) {
cairo_ctx_close(&ctx);
cairo_ctx_close(&global_context.cairo_context);
lua_close(global_context.lua);
}
void sighandler(int sig) {
@ -19,21 +24,39 @@ void sighandler(int sig) {
}
int main(void) {
if (!cairo_ctx_create(NULL, &ctx)) {
if (!cairo_ctx_create(NULL, &global_context.cairo_context)) {
fprintf(stderr, "Failed creating window or something\n");
return EXIT_FAILURE;
}
global_context.lua = luaL_newstate();
luaL_openlibs(global_context.lua);
loadapi_draw(global_context.lua);
if (luaL_dofile(global_context.lua, "example.lua") == LUA_OK) {
lua_pop(global_context.lua, lua_gettop(global_context.lua));
}
atexit(cleanup);
signal(SIGINT, sighandler);
cairo_t *cr = global_context.cairo_context.cairo;
for (float t = 0.0; running; t += 1.0 / 60.0) {
cairo_begin(&ctx);
global_context.time = t;
cairo_begin(&global_context.cairo_context);
{
cairo_set_source_rgb(ctx.cairo, 0.0, 0.5 - sin(t) * 0.5, 0.5 - cos(t) * 0.5);
cairo_paint(ctx.cairo);
lua_getglobal(global_context.lua, "tick");
if (lua_isfunction(global_context.lua, -1)) {
lua_pushnumber(global_context.lua, (lua_Number)t);
if (lua_pcall(global_context.lua, 1, 0, 0) == LUA_OK) {
lua_pop(global_context.lua, lua_gettop(global_context.lua));
} else {
printf("ERROR: %s\n", lua_tostring(global_context.lua, lua_gettop(global_context.lua)));
lua_pop(global_context.lua, lua_gettop(global_context.lua));
}
}
}
cairo_flush(&ctx);
cairo_flush(&global_context.cairo_context);
usleep(1000000 / 60.0);
}