From 12c80a3eabbf6d2fc9c84c9271240cd3ce6a34c4 Mon Sep 17 00:00:00 2001 From: hkc Date: Tue, 9 Apr 2024 18:33:54 +0300 Subject: [PATCH] Yay working Lua API --- Makefile | 4 ++-- example.lua | 10 ++++++---- src/api_draw.c | 30 ++++++++++++++++++++++++++++++ src/api_draw.h | 9 +++++++++ src/common.c | 3 +++ src/common.h | 13 +++++++++++++ src/main.c | 39 +++++++++++++++++++++++++++++++-------- 7 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 src/api_draw.c create mode 100644 src/api_draw.h create mode 100644 src/common.c create mode 100644 src/common.h diff --git a/Makefile b/Makefile index a7d7a6b..1a91546 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/example.lua b/example.lua index 2313e55..139dd79 100644 --- a/example.lua +++ b/example.lua @@ -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() diff --git a/src/api_draw.c b/src/api_draw.c new file mode 100644 index 0000000..28f4fe7 --- /dev/null +++ b/src/api_draw.c @@ -0,0 +1,30 @@ +#include "common.h" +#include "api_draw.h" +#include +#include +#include + +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; +} diff --git a/src/api_draw.h b/src/api_draw.h new file mode 100644 index 0000000..b90002c --- /dev/null +++ b/src/api_draw.h @@ -0,0 +1,9 @@ +#ifndef _API_DRAW_H_ +#define _API_DRAW_H_ + +#include +#include + +bool loadapi_draw(lua_State *lua); + +#endif diff --git a/src/common.c b/src/common.c new file mode 100644 index 0000000..4bd5701 --- /dev/null +++ b/src/common.c @@ -0,0 +1,3 @@ +#include "common.h" + +struct global_context global_context = { 0 }; diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..d4ae88e --- /dev/null +++ b/src/common.h @@ -0,0 +1,13 @@ +#ifndef _COMMON_H_ +#define _COMMON_H_ + +#include +#include "cairo_context.h" + +extern struct global_context { + lua_State *lua; + struct cairo_ctx cairo_context; + double time; +} global_context; + +#endif diff --git a/src/main.c b/src/main.c index 67bc0f3..d6ab851 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,7 @@ +#include +#include +#include +#include #include #include #include @@ -5,13 +9,14 @@ #include #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); }