diff options
author | Peng Wu <epico@dhcp-65-116.nay.redhat.com> | 2010-04-11 15:01:11 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2010-05-19 10:09:32 +0800 |
commit | 7505cd8f7c7298f6db1dc1ad665b082810234944 (patch) | |
tree | 37c8b313588b274a672a5c3a21cc18236c365357 /lua | |
parent | 07351a7e974d192a1b93679d518be75cb764fdd2 (diff) | |
download | ibus-libpinyin-7505cd8f7c7298f6db1dc1ad665b082810234944.tar.gz ibus-libpinyin-7505cd8f7c7298f6db1dc1ad665b082810234944.tar.xz ibus-libpinyin-7505cd8f7c7298f6db1dc1ad665b082810234944.zip |
add gobject interface for lua-plugin.
Diffstat (limited to 'lua')
-rw-r--r-- | lua/Makefile.am | 1 | ||||
-rw-r--r-- | lua/lua-plugin-init.c | 27 | ||||
-rw-r--r-- | lua/lua-plugin.c | 50 | ||||
-rw-r--r-- | lua/lua-plugin.h | 52 | ||||
-rw-r--r-- | lua/test-lua-plugin.c | 12 |
5 files changed, 115 insertions, 27 deletions
diff --git a/lua/Makefile.am b/lua/Makefile.am index bcf3803..4700bf2 100644 --- a/lua/Makefile.am +++ b/lua/Makefile.am @@ -24,6 +24,7 @@ noinst_LTLIBRARIES = \ libpylua_la_SOURCES = \ lua-plugin.h \ + lua-plugin.c \ lua-plugin-init.c \ $(NULL) diff --git a/lua/lua-plugin-init.c b/lua/lua-plugin-init.c index 641e869..403be44 100644 --- a/lua/lua-plugin-init.c +++ b/lua/lua-plugin-init.c @@ -29,35 +29,38 @@ void lua_plugin_openlibs (lua_State *L) { } } -static GArray * g_lua_commands = NULL; +int lua_plugin_init(IBusEnginePluginPrivate * plugin){ + g_assert(NULL == plugin->L); + /* initialize Lua */ + plugin->L = lua_open(); -int lua_plugin_init(lua_State *L){ /* enable libs in sandbox */ - lua_plugin_openlibs(L); + lua_plugin_openlibs(plugin->L); - if ( NULL == g_lua_commands ) - g_lua_commands = g_array_new(TRUE, TRUE, sizeof(lua_command_t)); + if ( NULL == plugin->lua_commands ) + plugin->lua_commands = g_array_new(TRUE, TRUE, sizeof(lua_command_t)); return 0; } -int lua_plugin_fini(lua_State *L){ +int lua_plugin_fini(IBusEnginePluginPrivate * plugin){ size_t i; lua_command_t * command; - lua_close(L); - if ( g_lua_commands ){ - for ( i = 0; i < g_lua_commands->len; ++i){ - command = &g_array_index(g_lua_commands, lua_command_t, i); + if ( plugin->lua_commands ){ + for ( i = 0; i < plugin->lua_commands->len; ++i){ + command = &g_array_index(plugin->lua_commands, lua_command_t, i); g_free((gpointer)command->command_name); g_free((gpointer)command->lua_function_name); g_free((gpointer)command->description); g_free((gpointer)command->leading); g_free((gpointer)command->help); } - g_array_free(g_lua_commands, TRUE); - g_lua_commands = NULL; + g_array_free(plugin->lua_commands, TRUE); + plugin->lua_commands = NULL; } + + lua_close(plugin->L); return 0; } diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c new file mode 100644 index 0000000..db8aa43 --- /dev/null +++ b/lua/lua-plugin.c @@ -0,0 +1,50 @@ +#include <lua.h> + +#include "lua-plugin.h" + +#define IBUS_ENGINE_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), IBUS_TYPE_ENGINE_PLUGIN, IBusEnginePluginPrivate)) + +G_DEFINE_TYPE (IBusEnginePlugin, ibus_engine_plugin, G_TYPE_OBJECT); + +static void +ibus_engine_plugin_dispose (GObject *gobject) +{ + IBusEnginePlugin *self = IBUS_ENGINE_PLUGIN (gobject); + + /* do some cleaning here. */ + + /* Chain up to the parent class */ + G_OBJECT_CLASS (ibus_engine_plugin_parent_class)->dispose(gobject); +} + +static void +ibus_engine_plugin_finalize (GObject *gobject) +{ + IBusEnginePlugin *self = IBUS_ENGINE_PLUGIN (gobject); + + /* Chain up to the parent class */ + G_OBJECT_CLASS (ibus_engine_plugin_parent_class)->dispose(gobject); +} + +static void +ibus_engine_plugin_class_init (IBusEnginePluginClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->dispose = ibus_engine_plugin_dispose; + gobject_class->finalize = ibus_engine_plugin_finalize; + + g_type_class_add_private (klass, sizeof (IBusEnginePluginPrivate)); +} + +static void +ibus_engine_plugin_init (IBusEnginePlugin *self) +{ + IBusEnginePluginPrivate *priv; + + self->priv = priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE (self); + + priv->L = NULL; + priv->lua_commands = NULL; +} + diff --git a/lua/lua-plugin.h b/lua/lua-plugin.h index 3aa73d6..1964101 100644 --- a/lua/lua-plugin.h +++ b/lua/lua-plugin.h @@ -1,8 +1,7 @@ #ifndef LUA_PLUGIN_H #define LUA_PLUGIN_H -int lua_plugin_init(lua_State * L); -int lua_plugin_fini(lua_State * L); +#include <glib-object.h> #define LUA_IMELIBNAME "ime" LUALIB_API int (luaopen_ime) (lua_State * L); @@ -15,29 +14,62 @@ typedef struct{ const char * help; /* optional. */ } lua_command_t; -typedef struct{ - lua_State * L; - GArray * lua_commands; /* Array of lua_command_t. */ -} lua_plugin_context_t; +/* + * Type macros. + */ + +#define IBUS_TYPE_ENGINE_PLUGIN (ibus_engine_plugin_get_type ()) +#define IBUS_ENGINE_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), IBUS_TYPE_ENGINE_PLUGIN, IBusEnginePlugin)) +#define IBUS_IS_ENGINE_PLUGIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), IBUS_TYPE_ENGINE_PLUGIN)) +#define IBUS_ENGINE_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), IBUS_TYPE_ENGINE_PLUGIN, IBusEnginePluginClass)) +#define IBUS_IS_ENGINE_PLUGIN_CLASS(klass) (G_TYPE_CHECK_CLASS ((klass), IBUS_TYPE_ENGINE_PLUGIN)) +#define IBUS_ENGINE_PLUGIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), IBUS_TYPE_ENGINE_PLUGIN, IBusEnginePluginClass)) + +typedef struct _IBusEnginePlugin IBusEnginePlugin; +typedef struct _IBusEnginePluginClass IBusEnginePluginClass; +typedef struct _IBusEnginePluginPrivate IBusEnginePluginPrivate; + +struct _IBusEnginePlugin +{ + GObject parent_instance; + + /*< private >*/ + IBusEnginePluginPrivate *priv; +}; + +struct _IBusEnginePluginClass +{ + GObjectClass parent_class; +}; + +GType ibus_engine_plugin_get_type(void); /** * retrieve all available lua plugin commands. * return array of command informations of type lua_command_t. */ -GArray * lua_plugin_ime_get_available_commands(lua_State * L); +GArray * lua_plugin_ime_get_available_commands(IBusEnginePlugin * plugin); /** * retval int: only support string or string array. */ -int lua_plugin_ime_call(lua_State * L, const lua_command_t * command, const char * argument /*optional, maybe NULL.*/); +int lua_plugin_ime_call(IBusEnginePlugin * plugin, const lua_command_t * command, const char * argument /*optional, maybe NULL.*/); /** * retrieve the retval string value. (value has been copied.) */ -const char * lua_plugin_ime_get_retval(lua_State * L); +const char * lua_plugin_ime_get_retval(IBusEnginePlugin * plugin); /** * retrieve the array of string values. (string values have been copied.) */ -GArray * lua_plugin_ime_get_retvals(lua_State * L); +GArray * lua_plugin_ime_get_retvals(IBusEnginePlugin * plugin); +/*< private >*/ +int lua_plugin_init(IBusEnginePluginPrivate * private); +int lua_plugin_fini(IBusEnginePluginPrivate * private); + +struct _IBusEnginePluginPrivate{ + lua_State * L; + GArray * lua_commands; /* Array of lua_command_t. */ +}; #endif diff --git a/lua/test-lua-plugin.c b/lua/test-lua-plugin.c index 34bbef5..44bce4a 100644 --- a/lua/test-lua-plugin.c +++ b/lua/test-lua-plugin.c @@ -36,13 +36,15 @@ static int run_test(lua_State *L, const char * filename){ int main(int argc, char * argv[]){ printf("starting test...\n"); - /* initialize Lua */ - L = lua_open(); + IBusEnginePluginPrivate priv; - lua_plugin_init(L); + priv.L = NULL; + priv.lua_commands = NULL; - run_test(L, "test.lua"); + lua_plugin_init(&priv); + + run_test(priv.L, "test.lua"); - lua_plugin_fini(L); + lua_plugin_fini(&priv); return 0; } |