From 5da5689541aa5bca6b056a2372bcd3410f6475b2 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Sat, 25 Sep 2010 16:03:48 +0200
Subject: WIP: Refactoring of the library and symbols
---
CMakeLists.txt | 6 +-
src/ld-lua.c | 195 +++++++++++++++++++++++++++++++++++++++++++++++
src/ld-lua.h | 27 +++++++
src/ld-symbol-category.h | 4 +
src/ld-symbol-library.c | 22 ++++--
src/ld-symbol-library.h | 1 +
src/ld-symbol.c | 12 +--
src/ld-symbol.h | 6 +-
8 files changed, 251 insertions(+), 22 deletions(-)
create mode 100644 src/ld-lua.c
create mode 100644 src/ld-lua.h
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d602087..bfb41f2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -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
diff --git a/src/ld-lua.c b/src/ld-lua.c
new file mode 100644
index 0000000..02ff7ac
--- /dev/null
+++ b/src/ld-lua.c
@@ -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
+#include
+#include
+#include
+
+#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;
+}
+
+
+
diff --git a/src/ld-lua.h b/src/ld-lua.h
new file mode 100644
index 0000000..2c41579
--- /dev/null
+++ b/src/ld-lua.h
@@ -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__ */
+
diff --git a/src/ld-symbol-category.h b/src/ld-symbol-category.h
index f2451d7..c06f46a 100644
--- a/src/ld-symbol-category.h
+++ b/src/ld-symbol-category.h
@@ -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 >*/
diff --git a/src/ld-symbol-library.c b/src/ld-symbol-library.c
index 2b6738a..2f5f9d3 100644
--- a/src/ld-symbol-library.c
+++ b/src/ld-symbol-library.c
@@ -9,8 +9,6 @@
*/
#include
-#include
-/* #include */
#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);
}
diff --git a/src/ld-symbol-library.h b/src/ld-symbol-library.h
index aea439c..8678201 100644
--- a/src/ld-symbol-library.h
+++ b/src/ld-symbol-library.h
@@ -51,6 +51,7 @@ struct _LdSymbolLibraryClass
{
/*< private >*/
GObjectClass parent_class;
+
guint changed_signal;
};
diff --git a/src/ld-symbol.c b/src/ld-symbol.c
index 15b9ab5..deab3f5 100644
--- a/src/ld-symbol.c
+++ b/src/ld-symbol.c
@@ -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;
}
diff --git a/src/ld-symbol.h b/src/ld-symbol.h
index dfb3a6e..882bde7 100644
--- a/src/ld-symbol.h
+++ b/src/ld-symbol.h
@@ -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. */
--
cgit v1.2.3-70-g09d2