From adba43320e24bd7103ec01092c488f9ece06be29 Mon Sep 17 00:00:00 2001 From: hippoz Date: Sun, 2 May 2021 17:24:40 +0300 Subject: [PATCH] overhaul code --- Makefile | 4 +- corescripts/_global.lua | 6 +-- lua.c | 52 ++++++++++++++++++++ lua.h | 13 +++++ main.c | 103 +++++++++------------------------------- ui.c | 41 +++++++++++++--- ui.h | 20 ++++---- 7 files changed, 136 insertions(+), 103 deletions(-) create mode 100644 lua.c create mode 100644 lua.h diff --git a/Makefile b/Makefile index cb8d846..cbad54b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC = gcc -main.out: main.c ui.c - $(CC) main.c ui.c -o main.out $(shell pkg-config --cflags --libs gtk+-3.0) $(shell pkg-config --cflags --libs luajit) +main.out: main.c ui.c lua.c + $(CC) -O3 main.c ui.c lua.c -o main.out $(shell pkg-config --cflags --libs gtk+-3.0) $(shell pkg-config --cflags --libs luajit) clean: @rm main.out \ No newline at end of file diff --git a/corescripts/_global.lua b/corescripts/_global.lua index fd55845..ba53a90 100644 --- a/corescripts/_global.lua +++ b/corescripts/_global.lua @@ -6,11 +6,7 @@ function _el(name) end end -h1 = _el("h1") -t = _el("text") -b = _el("b") -input = _el("input") - +t = _el("t") --[[ page module ]]-- diff --git a/lua.c b/lua.c new file mode 100644 index 0000000..c1d9caf --- /dev/null +++ b/lua.c @@ -0,0 +1,52 @@ +#include "ui.h" + +#include +#include +#include +#include + +int lua_pcall_file(lua_State* L, const char* file) { + luaL_loadfile(L, file); + if (lua_pcall(L, 0, 0, 0) != 0) { + fprintf(stderr, "lua: script[%s]: error: %s\n", file, lua_tostring(L, -1)); + return 1; + } + return 0; +} + +int lua_create_c_function(lua_State* L, const char* name, lua_CFunction func) { + lua_pushcfunction(L, func); + lua_setglobal(L, name); +} + +int lua_init_state(lua_State* L, struct application_state* state) { + int status; + + luaL_openlibs(L); // unsafe + + int pageview_append_tag(lua_State* L) { + const char* tag_type = luaL_checkstring(L, 1); + const char* tag_text = luaL_checkstring(L, 2); + switch (tag_type[0]) { + case 't': { + page_application_text_buffer_append_text(state, tag_text); + break; + } + default: { + return luaL_error(L, "ext_error: tried to create tag that does not exist"); + } + } + return 0; + } + + lua_create_c_function(L, "__ext_pageview_append_tag", pageview_append_tag); + + status = lua_pcall_file(L, "corescripts/_global.lua"); + if (status != 0) return 1; + status = lua_pcall_file(L, "pages/test.lua"); + if (status != 0) return 2; + + printf("lua: ready\n"); + + return 0; +} \ No newline at end of file diff --git a/lua.h b/lua.h new file mode 100644 index 0000000..f175afc --- /dev/null +++ b/lua.h @@ -0,0 +1,13 @@ +#ifndef _LUA_H +#define _LUA_H + +#include +#include +#include +#include + +int lua_pcall_file(lua_State* L, const char* file); +int lua_create_c_function(lua_State* L, const char* name, lua_CFunction func); +int lua_init_state(lua_State* L, struct application_state* state); + +#endif \ No newline at end of file diff --git a/main.c b/main.c index 6af785b..25503f8 100644 --- a/main.c +++ b/main.c @@ -1,98 +1,39 @@ #include -#include -#include "ui.h" - #include #include #include #include -static int start_lua(GtkApplication* app) { - int status; +#include "ui.h" +#include "lua.h" + +static void activate(GtkApplication* app, struct application_state* state) { + printf("gtk: ready\n"); + page_application_init(state); + int status = lua_init_state(state->L, state); + if (status != 0) { + printf("error: lua_init_state failed with non-zero error code %d\n", status); + } + page_application_show(state); + lua_close(state->L); +} + +int main(int argc, char** argv) { lua_State* L; - int ret; + int status = 1; - GtkWidget *page_window, *scrolled, *pageview; - GtkTextBuffer* pageview_buffer; - - scrolled = gtk_scrolled_window_new(NULL, NULL); - - pageview_buffer = gtk_text_buffer_new(NULL); - pageview = gtk_text_view_new_with_buffer(pageview_buffer); - gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(pageview), GTK_WRAP_WORD_CHAR); - gtk_text_view_set_editable(pageview, 0); - gtk_text_view_set_cursor_visible(pageview, 0); - - page_window = gtk_application_window_new(app); - gtk_window_set_title(GTK_WINDOW(page_window), "Window"); - gtk_window_set_default_size(GTK_WINDOW(page_window), 200, 200); - - gtk_container_add(GTK_CONTAINER(scrolled), GTK_WIDGET(pageview)); - gtk_container_add(GTK_CONTAINER(page_window), GTK_WIDGET(scrolled)); - - gtk_widget_show_all(page_window); - - L = luaL_newstate(); // open Lua + L = luaL_newstate(); if (!L) { fprintf(stderr, "error: failed to create lua state"); return -1; } - printf("lua: ready\n"); + struct application_state* state = page_application_new(); + state->L = L; - int pageview_append_tag(lua_State* L) { - const char* tag_type = luaL_checkstring(L, 1); - if (strcmp(tag_type, "text") == 0) { - printf(tag_type); - const char* tag_text = luaL_checkstring(L, 2); - GtkTextIter iter; - gtk_text_buffer_get_end_iter(pageview_buffer, &iter); - gtk_text_buffer_insert(pageview_buffer, &iter, tag_text, -1); - } else { - return luaL_error(L, "tried to create tag that does not exist"); - } - return 0; - } - lua_pushcfunction(L, pageview_append_tag); - lua_setglobal(L, "__ext_pageview_append_tag"); - - luaL_openlibs(L); - - status = luaL_loadfile(L, "corescripts/_global.lua"); - ret = lua_pcall(L, 0, 0, 0); - if (ret != 0) { - fprintf(stderr, "lua: corescript: error: %s\n", lua_tostring(L, -1)); - return 1; - } - - status = luaL_loadfile(L, "pages/test.lua"); - ret = lua_pcall(L, 0, 0, 0); - if (ret != 0) { - fprintf(stderr, "lua: error: %s\n", lua_tostring(L, -1)); - return 1; - } - - lua_close(L); - return 0; -} - -static void activate(GtkApplication* app, gpointer user_data) { - printf("gtk: ready\n"); - start_lua(app); -} - -static int start_gtk(int* argc, char*** argv) { - int status = 1; - - struct application_state state = new_application(); - g_signal_connect(state.app, "activate", G_CALLBACK(activate), NULL); - - status = g_application_run(G_APPLICATION(state.app), *argc, *argv); - g_object_unref(state.app); + g_signal_connect(state->gapp, "activate", G_CALLBACK(activate), state); + status = g_application_run(G_APPLICATION(state->gapp), argc, argv); + g_object_unref(state->gapp); return status; } - -int main(int argc, char** argv) { - start_gtk(&argc, &argv); -} diff --git a/ui.c b/ui.c index 65b2e6e..f5aabf5 100644 --- a/ui.c +++ b/ui.c @@ -1,11 +1,38 @@ +#include +#include +#include +#include #include "ui.h" -struct application_state new_application() { - GtkApplication* gapp; - - gapp = gtk_application_new("xyz.hippoz.nettle", G_APPLICATION_FLAGS_NONE); - - struct application_state state = {.app = gapp}; - +struct application_state* page_application_new() { + struct application_state* state = malloc(sizeof(struct application_state)); + state->gapp = gtk_application_new("xyz.hippoz.nettle", G_APPLICATION_FLAGS_NONE); return state; +} + +void page_application_init(struct application_state* state) { + state->page_window = gtk_application_window_new(state->gapp); + gtk_window_set_title(GTK_WINDOW(state->page_window), "Window"); + gtk_window_set_default_size(GTK_WINDOW(state->page_window), 200, 200); + + state->scrolled = gtk_scrolled_window_new(NULL, NULL); + + state->pageview_buffer = gtk_text_buffer_new(NULL); + state->pageview = gtk_text_view_new_with_buffer(GTK_TEXT_BUFFER(state->pageview_buffer)); + gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(state->pageview), GTK_WRAP_WORD_CHAR); + gtk_text_view_set_editable(GTK_TEXT_VIEW(state->pageview), 0); + gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(state->pageview), 0); + + gtk_container_add(GTK_CONTAINER(state->scrolled), GTK_WIDGET(state->pageview)); + gtk_container_add(GTK_CONTAINER(state->page_window), GTK_WIDGET(state->scrolled)); +} + +void page_application_show(struct application_state* state) { + gtk_widget_show_all(state->page_window); +} + +void page_application_text_buffer_append_text(struct application_state* state, const char* display_text) { + GtkTextIter iter; + gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(state->pageview_buffer), &iter); + gtk_text_buffer_insert(GTK_TEXT_BUFFER(state->pageview_buffer), &iter, display_text, -1); } \ No newline at end of file diff --git a/ui.h b/ui.h index e3578af..5e19d13 100644 --- a/ui.h +++ b/ui.h @@ -2,17 +2,21 @@ #define __UI_H #include - -enum ElementType { - Heading1, - Text -}; +#include +#include +#include +#include struct application_state { - GtkApplication* app; + GtkApplication* gapp; + GtkWidget *page_window, *scrolled, *pageview; + GtkTextBuffer* pageview_buffer; + lua_State* L; }; -struct application_state new_application(); -void add_element(enum ElementType elementType); +struct application_state* page_application_new(); +void page_application_init(struct application_state* state); +void page_application_show(struct application_state* state); +void page_application_text_buffer_append_text(struct application_state* state, const char* display_text); #endif \ No newline at end of file