WIP: Refactoring of the library and symbols

This commit is contained in:
Přemysl Eric Janouch 2010-09-25 16:03:48 +02:00
parent 25668c2073
commit 5da5689541
8 changed files with 251 additions and 22 deletions

View File

@ -90,7 +90,8 @@ set (logdiag_SOURCES
src/ld-canvas.c
src/ld-symbol.c
src/ld-symbol-category.c
src/ld-symbol-library.c)
src/ld-symbol-library.c
src/ld-lua.c)
set (logdiag_HEADERS
${CMAKE_CURRENT_BINARY_DIR}/config.h
src/ld-marshal.h
@ -99,7 +100,8 @@ set (logdiag_HEADERS
src/ld-canvas.h
src/ld-symbol.h
src/ld-symbol-category.h
src/ld-symbol-library.h)
src/ld-symbol-library.h
src/ld-lua.h)
# Generate a configure file
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/config.h.in

195
src/ld-lua.c Normal file
View File

@ -0,0 +1,195 @@
/*
* ld-lua.c
*
* This file is a part of logdiag.
* Copyright Přemysl Janouch 2010. All rights reserved.
*
* See the file LICENSE for licensing information.
*
*/
#include <gtk/gtk.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "config.h"
#include "ld-symbol-library.h"
#include "ld-lua.h"
/* TODO */
/* A virtual superclass can be made for this. */
/* Lua state belongs to the library.
* One Lua file should be able to register multiple symbols.
*/
/* How does the application call the function for rendering?
* logdiag.symbols -- readonly table (from lua) -- this can be probably
* accomplished using a custom metatable that errors out on newindex,
* items will be added to this table only in C.
* logdiag.symbols[ident].render(cr) -- here "ident" is the full path
* to this symbol
* logdiag.symbols[ident].names[lang, area, terminals] -- these
* subarrays need not be in this array
*
*/
static void *
ld_lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize);
static int
ld_lua_logdiag_register (lua_State *L);
static luaL_Reg ld_lua_logdiag_lib[] =
{
{"register", ld_lua_logdiag_register},
{NULL, NULL}
};
static int
ld_lua_cairo_move_to (lua_State *L);
static int
ld_lua_cairo_line_to (lua_State *L);
static int
ld_lua_cairo_stroke (lua_State *L);
static int
ld_lua_cairo_stroke_preserve (lua_State *L);
static int
ld_lua_cairo_fill (lua_State *L);
static int
ld_lua_cairo_fill_preserve (lua_State *L);
static luaL_Reg ld_lua_cairo_table[] =
{
{"move_to", ld_lua_cairo_move_to},
{"line_to", ld_lua_cairo_line_to},
{"stroke", ld_lua_cairo_stroke},
{"stroke_preserve", ld_lua_cairo_stroke_preserve},
{"fill", ld_lua_cairo_fill},
{"fill_preserve", ld_lua_cairo_fill_preserve},
{NULL, NULL}
};
/* ===== Generic =========================================================== */
lua_State *
ld_lua_init (void)
{
lua_State *L;
L = lua_newstate (ld_lua_alloc, NULL);
g_return_val_if_fail (L != NULL, NULL);
/* TODO: lua_atpanic () */
/* Load some safe libraries. */
lua_pushcfunction (L, luaopen_string);
lua_call (L, 0, 0);
lua_pushcfunction (L, luaopen_table);
lua_call (L, 0, 0);
lua_pushcfunction (L, luaopen_math);
lua_call (L, 0, 0);
/* Load the application library. */
luaL_register (L, "logdiag", ld_lua_logdiag_lib);
}
void
ld_lua_destroy (lua_State *L)
{
lua_close (L);
}
static void *
ld_lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize)
{
if (!nsize)
{
g_free (ptr);
return NULL;
}
else
return g_try_realloc (ptr, nsize);
}
/* ===== Application library =============================================== */
static int
ld_lua_logdiag_register (lua_State *L)
{
/* TODO: Create a symbol. */
/* XXX: Shouldn't this function be a closure with LdLibrary userdata?
* It is also possible to have the userdata in the "logdiag" table.
*/
#if 0
lua_newtable (L);
/* TODO: Push a function. */
lua_call (L, 1, 0);
#endif /* 0 */
}
/* ===== Cairo ============================================================= */
static void
push_cairo_object (lua_State *L, cairo_t *cr)
{
luaL_Reg *fn;
/* Create a table. */
lua_newtable (L);
/* Add methods. */
for (fn = ld_lua_cairo_table; fn->name; fn++)
{
lua_pushlightuserdata (L, cr);
lua_pushcclosure (L, fn->func, 1);
lua_setfield (L, -2, fn->name);
}
}
static int
ld_lua_cairo_move_to (lua_State *L)
{
return 0;
}
static int
ld_lua_cairo_line_to (lua_State *L)
{
return 0;
}
static int
ld_lua_cairo_stroke (lua_State *L)
{
return 0;
}
static int
ld_lua_cairo_stroke_preserve (lua_State *L)
{
return 0;
}
static int
ld_lua_cairo_fill (lua_State *L)
{
return 0;
}
static int
ld_lua_cairo_fill_preserve (lua_State *L)
{
return 0;
}

27
src/ld-lua.h Normal file
View File

@ -0,0 +1,27 @@
/*
* ld-lua.h
*
* This file is a part of logdiag.
* Copyright Přemysl Janouch 2010. All rights reserved.
*
* See the file LICENSE for licensing information.
*
*/
#ifndef __LD_LUA_H__
#define __LD_LUA_H__
G_BEGIN_DECLS
lua_State *
ld_lua_init (void);
void
ld_lua_destroy (lua_State *L);
G_END_DECLS
#endif /* ! __LD_LUA_H__ */

View File

@ -38,6 +38,10 @@ typedef struct _LdSymbolCategoryClass LdSymbolCategoryClass;
* @image_path: Path to the image for this category.
* @children: Children of this category.
*/
/* TODO: Make the public fields private and set them as properties
* + implement setters and getters. On change the category
* shall emit a "changed" signal in the Library.
*/
struct _LdSymbolCategory
{
/*< private >*/

View File

@ -9,8 +9,6 @@
*/
#include <gtk/gtk.h>
#include <lua.h>
/* #include <lauxlib.h> */
#include "config.h"
@ -29,11 +27,11 @@
/*
* LdSymbolLibraryPrivate:
* @lua_state: Lua state.
* @script_state: State of the scripting language.
*/
struct _LdSymbolLibraryPrivate
{
lua_State *lua_state;
gpointer script_state;
};
G_DEFINE_TYPE (LdSymbolLibrary, ld_symbol_library, G_TYPE_OBJECT);
@ -70,8 +68,8 @@ ld_symbol_library_init (LdSymbolLibrary *self)
self->priv = G_TYPE_INSTANCE_GET_PRIVATE
(self, LD_TYPE_SYMBOL_LIBRARY, LdSymbolLibraryPrivate);
/* TODO: lua */
self->priv->lua_state = NULL;
/* TODO */
self->priv->script_state = NULL;
self->categories = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free, (GDestroyNotify) g_object_unref);
@ -151,6 +149,7 @@ ld_symbol_library_load (LdSymbolLibrary *self, const char *path)
{
GDir *dir;
const gchar *item;
gboolean changed = FALSE;
g_return_val_if_fail (LD_IS_SYMBOL_LIBRARY (self), FALSE);
g_return_val_if_fail (path != NULL, FALSE);
@ -169,8 +168,15 @@ ld_symbol_library_load (LdSymbolLibrary *self, const char *path)
if (cat)
g_hash_table_insert (self->categories, cat->name, cat);
g_free (categ_path);
changed = TRUE;
}
g_dir_close (dir);
if (changed)
g_signal_emit (self,
LD_SYMBOL_LIBRARY_GET_CLASS (self)->changed_signal, 0);
return TRUE;
}
@ -186,6 +192,8 @@ ld_symbol_library_clear (LdSymbolLibrary *self)
g_return_if_fail (LD_IS_SYMBOL_LIBRARY (self));
g_hash_table_remove_all (self->categories);
return;
g_signal_emit (self,
LD_SYMBOL_LIBRARY_GET_CLASS (self)->changed_signal, 0);
}

View File

@ -51,6 +51,7 @@ struct _LdSymbolLibraryClass
{
/*< private >*/
GObjectClass parent_class;
guint changed_signal;
};

View File

@ -79,13 +79,11 @@ ld_symbol_finalize (GObject *gobject)
*
* Load a symbol from a file into the library.
*/
LdSymbol *ld_symbol_new (LdSymbolLibrary *library,
const gchar *filename)
LdSymbol *ld_symbol_new (LdSymbolLibrary *library)
{
LdSymbol *symbol;
symbol = g_object_new (LD_TYPE_SYMBOL, NULL);
/* TODO: Use the filename, Luke. */
symbol->priv->library = library;
g_object_ref (library);
@ -107,17 +105,13 @@ ld_symbol_build_identifier (LdSymbol *self)
/**
* ld_symbol_draw:
* @self: A symbol object.
* @surface: A cairo surface to be drawn on.
* @cr: A cairo surface to be drawn on.
* @param: Parameters for the symbol in a table.
* @x: The X coordinate on the surface.
* @y: The Y coordinate on the surface.
* @zoom: Zoom ratio.
*
* Draw the symbol onto a Cairo surface.
*/
void
ld_symbol_draw (LdSymbol *self, cairo_t *surface,
GHashTable *param, gint x, gint y, gdouble zoom)
ld_symbol_draw (LdSymbol *self, cairo_t *cr, GHashTable *param)
{
return;
}

View File

@ -53,11 +53,9 @@ struct _LdSymbolClass
GType ld_symbol_get_type (void) G_GNUC_CONST;
LdSymbol *ld_symbol_new (LdSymbolLibrary *library,
const gchar *filename);
LdSymbol *ld_symbol_new (LdSymbolLibrary *library);
gchar *ld_symbol_build_identifier (LdSymbol *self);
void ld_symbol_draw (LdSymbol *self, cairo_t *surface,
GHashTable *param, gint x, gint y, gdouble zoom);
void ld_symbol_draw (LdSymbol *self, cairo_t *cr, GHashTable *param);
/* TODO: An interface for symbol terminals. */