]> khamidou — GitHub mirrors - vice.git/commitdiff
call lua event handler
authorkarim hamidou <khamidou@gmail.com>
Mon, 7 Jul 2014 11:47:54 +0000 (13:47 +0200)
committerkarim hamidou <khamidou@gmail.com>
Mon, 7 Jul 2014 11:47:54 +0000 (13:47 +0200)
Makefile
main.lua [new file with mode: 0644]
vice.c

index a5e3e302b722cd9c129783fbb6dcd306ffaa6a3c..0613beba0594a954d877e491b74cf6804b58ce53 100644 (file)
--- 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 (file)
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 c68d77038fe749cf60f4cdb2f70dfbe946869589..d938b4f8270cd839c592fecc441f13c0d1e9c0aa 100644 (file)
--- a/vice.c
+++ b/vice.c
@@ -7,13 +7,18 @@
 #define PLAT_GTK 1
 #include <ScintillaWidget.h>
 
+#include <lua.h>
+#include <lualib.h>
+#include <lauxlib.h>
+
 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);