From 25776642382b9e780e197b9f85aeb5aeb7852c7b Mon Sep 17 00:00:00 2001 From: Peng Wu Date: Sun, 11 Apr 2010 15:26:30 +0800 Subject: refactor lua-plugin --- lua/lua-plugin.c | 86 ++++++++++++++++++++++++++++++--------------------- lua/lua-plugin.h | 14 ++++++--- lua/test-lua-plugin.c | 16 +++++----- 3 files changed, 68 insertions(+), 48 deletions(-) (limited to 'lua') diff --git a/lua/lua-plugin.c b/lua/lua-plugin.c index beab34c..d78fca9 100644 --- a/lua/lua-plugin.c +++ b/lua/lua-plugin.c @@ -12,40 +12,8 @@ struct _IBusEnginePluginPrivate{ GArray * lua_commands; /* Array of lua_command_t. */ }; - 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 int lua_plugin_init(IBusEnginePluginPrivate * plugin){ g_assert(NULL == plugin->L); @@ -55,9 +23,8 @@ lua_plugin_init(IBusEnginePluginPrivate * plugin){ /* enable libs in sandbox */ lua_plugin_openlibs(plugin->L); - if ( NULL == plugin->lua_commands ) - plugin->lua_commands = g_array_new(TRUE, TRUE, sizeof(lua_command_t)); - + g_assert ( NULL == plugin->lua_commands ); + plugin->lua_commands = g_array_new(TRUE, TRUE, sizeof(lua_command_t)); return 0; } @@ -80,9 +47,42 @@ lua_plugin_fini(IBusEnginePluginPrivate * plugin){ } lua_close(plugin->L); + plugin->L = NULL; return 0; } +static void +ibus_engine_plugin_dispose (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_finalize (GObject *gobject) +{ + IBusEnginePlugin *self = IBUS_ENGINE_PLUGIN (gobject); + + /* do some cleaning here. */ + lua_plugin_fini(self->priv); + + /* 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) { @@ -91,5 +91,21 @@ ibus_engine_plugin_init (IBusEnginePlugin *self) self->priv = priv = IBUS_ENGINE_PLUGIN_GET_PRIVATE (self); memset(priv, 0, sizeof(IBusEnginePluginPrivate)); + + lua_plugin_init(priv); +} + +IBusEnginePlugin * ibus_engine_plugin_new(){ + IBusEnginePlugin * plugin; + + plugin = (IBusEnginePlugin *) g_object_new (IBUS_TYPE_ENGINE_PLUGIN, + NULL); + + return plugin; +} + +/* will drop this function soon. */ +lua_State * ibus_engine_plugin_get_lua_State(IBusEnginePlugin * plugin){ + return plugin->priv->L; } diff --git a/lua/lua-plugin.h b/lua/lua-plugin.h index 677aab3..2d50329 100644 --- a/lua/lua-plugin.h +++ b/lua/lua-plugin.h @@ -46,24 +46,30 @@ struct _IBusEnginePluginClass GType ibus_engine_plugin_get_type(void); +IBusEnginePlugin * ibus_engine_plugin_new(); + /** * retrieve all available lua plugin commands. * return array of command informations of type lua_command_t. */ -GArray * lua_plugin_ime_get_available_commands(IBusEnginePlugin * plugin); +GArray * ibus_engine_plugin_ime_get_available_commands(IBusEnginePlugin * plugin); /** * retval int: only support string or string array. */ -int lua_plugin_ime_call(IBusEnginePlugin * plugin, const lua_command_t * command, const char * argument /*optional, maybe NULL.*/); +int ibus_engine_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(IBusEnginePlugin * plugin); +const char * ibus_engine_plugin_ime_get_retval(IBusEnginePlugin * plugin); /** * retrieve the array of string values. (string values have been copied.) */ -GArray * lua_plugin_ime_get_retvals(IBusEnginePlugin * plugin); +GArray * ibus_engine_plugin_ime_get_retvals(IBusEnginePlugin * plugin); + +/*< private >*/ +/* will drop this function soon. */ +lua_State * ibus_engine_plugin_get_lua_State(IBusEnginePlugin * plugin); #endif diff --git a/lua/test-lua-plugin.c b/lua/test-lua-plugin.c index 44bce4a..c9a5f12 100644 --- a/lua/test-lua-plugin.c +++ b/lua/test-lua-plugin.c @@ -35,16 +35,14 @@ static int run_test(lua_State *L, const char * filename){ int main(int argc, char * argv[]){ printf("starting test...\n"); + g_type_init(); + + IBusEnginePlugin * plugin; + plugin = ibus_engine_plugin_new(); - IBusEnginePluginPrivate priv; - - priv.L = NULL; - priv.lua_commands = NULL; - - lua_plugin_init(&priv); - - run_test(priv.L, "test.lua"); + lua_State * L = ibus_engine_plugin_get_lua_State(plugin); + run_test(L, "test.lua"); - lua_plugin_fini(&priv); + g_object_unref(plugin); return 0; } -- cgit