From: karim hamidou Date: Mon, 7 Jul 2014 11:47:54 +0000 (+0200) Subject: call lua event handler X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=abea5452b13a0cbe47c123e543805a9d69152b96;p=vice.git call lua event handler --- diff --git a/Makefile b/Makefile index a5e3e30..0613beb 100644 --- a/Makefile +++ b/Makefile @@ -12,8 +12,8 @@ LEXEROBJS=$(wildcard ../scintilla/gtk/Lex*.o) all: vice .c.o: - gcc `pkg-config --cflags gtk+-2.0` $(INCLUDEDIRS) $(CXXFLAGS) -c $< -o $@ + gcc `pkg-config --cflags gtk+-2.0` `lua-config --include` $(INCLUDEDIRS) $(CXXFLAGS) -c $< -o $@ vice: vice.o $(LEXEROBJS) ./scintilla/bin/scintilla.a - gcc -DGTK $^ -o $@ -lstdc++ `pkg-config --libs gtk+-2.0 gthread-2.0 gmodule-2.0` -lm + gcc -DGTK $^ -o $@ -lstdc++ `pkg-config --libs gtk+-2.0 gthread-2.0 gmodule-2.0` `lua-config --libs` -lm clean: rm -rf vice *.o diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..debceec --- /dev/null +++ b/main.lua @@ -0,0 +1,3 @@ +function keyboard_event_handler (keycode) + io.write("Hello world, from ",keycode,"!\n") +end diff --git a/vice.c b/vice.c index c68d770..d938b4f 100644 --- a/vice.c +++ b/vice.c @@ -7,13 +7,18 @@ #define PLAT_GTK 1 #include +#include +#include +#include + GtkWidget *app; GtkWidget *vbox; GtkWidget *minibuffer; - GtkWidget *editor; ScintillaObject *sci; +lua_State *L; + #define SSM(m, w, l) scintilla_send_message(sci, m, w, l) static int exit_app(GtkWidget*w, GdkEventAny*e, gpointer p) { @@ -22,13 +27,17 @@ static int exit_app(GtkWidget*w, GdkEventAny*e, gpointer p) { } static gboolean handle_keypress(GtkWidget *widget, GdkEventKey *event, gpointer data) { - puts("ho"); + lua_pushstring(L, "keyboard_event_handler"); + lua_gettable(L, LUA_GLOBALSINDEX); /* function to be called */ + lua_pushnumber(L, event->keyval); + lua_call(L, 1, 0); + if(event->keyval == GDK_Escape) { gtk_entry_set_text(GTK_ENTRY(minibuffer), "blah"); } else { SSM(SCI_INSERTTEXT, 0, (sptr_t) "blah"); } - + return TRUE; } @@ -51,7 +60,6 @@ int main(int argc, char **argv) { scintilla_set_id(sci, 0); gtk_widget_set_usize(editor, 800, 600); - SSM(SCI_STYLECLEARALL, 0, 0); SSM(SCI_SETLEXER, SCLEX_CPP, 0); SSM(SCI_SETKEYWORDS, 0, (sptr_t)"int char"); @@ -62,6 +70,23 @@ int main(int argc, char **argv) { SSM(SCI_STYLESETFORE, SCE_C_STRING, 0x800080); SSM(SCI_STYLESETBOLD, SCE_C_OPERATOR, 1); int len = 0; + + L = lua_open(); + + luaopen_io(L); // provides io.* + luaopen_base(L); + luaopen_table(L); + luaopen_string(L); + luaopen_math(L); + luaopen_loadlib(L); + + int s = luaL_loadfile(L, "main.lua"); + + if ( s==0 ) { + // execute Lua program + s = lua_pcall(L, 0, LUA_MULTRET, 0); + } + SSM(SCI_GETLENGTH, len, 0); printf("length: %d\n", len);