glib_source.c
changeset 3 4fd19a188509
parent 2 34b6fedde9eb
child 4 5770be2d5f3f
equal deleted inserted replaced
2:34b6fedde9eb 3:4fd19a188509
     1 
       
     2 #include <lua.h>
       
     3 #include <lauxlib.h>
       
     4 #include <glib.h>
       
     5 
       
     6 #include "glib_types.h"
       
     7 #include "util.h"
       
     8 
       
     9 /// g.source.bless
       
    10 /// Blesses given pointer to g source object.
       
    11 /// A: lightuserdata (pointer to C glib event source object)
       
    12 /// R: g source object
       
    13 static int lglib_source_bless_lua (lua_State *L)
       
    14 {
       
    15 	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "glib source lightuserdata expected");
       
    16 	lglib_source_bless (L, lua_touserdata (L, 1));
       
    17 	return 1;
       
    18 }
       
    19 
       
    20 /// source:context
       
    21 /// Connects event source to main context or returns context,
       
    22 /// to which this source is connected.
       
    23 /// A: lglib main context object
       
    24 /// R: int (source id, when called with args) or g main context object or nil (if called with no args)
       
    25 static int lglib_source_context (lua_State *L)
       
    26 {
       
    27 	lglib_source_t *object = luaL_checklglib_source (L, 1);
       
    28 	if (lua_gettop (L) > 1) { // Attach
       
    29 		lglib_main_context_t *context = luaL_checklglib_main_context (L, 2);
       
    30 		lua_pushnumber (L, g_source_attach (object->source, context->main_context));
       
    31 	} else { // Get
       
    32 		GMainContext *context = g_source_get_context (object->source);
       
    33 		if (context) {
       
    34 			lglib_main_context_bless (L, context);
       
    35 			// XXX g_main_context_unref (context);
       
    36 		} else
       
    37 			lua_pushnil (L);
       
    38 	}
       
    39 	return 1;
       
    40 }
       
    41 
       
    42 /// source callback function
       
    43 /// Does processing of events. Return value indicates, if source should not be destroyed.
       
    44 /// R: boolean (continue)
       
    45 static gboolean lglib_source_callback (lglib_callback_t *cb)
       
    46 {
       
    47 	lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
       
    48 	if (lua_pcall (cb->L, 0, 1, 0)) {
       
    49 		// XXX lua_error (cb->L);
       
    50 		lua_pop (cb->L, 1);
       
    51 		return FALSE;
       
    52 	}
       
    53 	return lua_toboolean (cb->L, -1);
       
    54 }
       
    55 
       
    56 /// source:callback
       
    57 /// Sets callback function for given event source.
       
    58 /// A: source callback function
       
    59 static int lglib_source_set_callback (lua_State *L)
       
    60 {
       
    61 	lglib_source_t *object = luaL_checklglib_source (L, 1);
       
    62 	lglib_callback_t *cb;
       
    63 	luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
       
    64 	
       
    65 	cb = luaL_malloc (L, sizeof (lglib_callback_t));
       
    66 	cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
       
    67 	cb->L         = L;
       
    68 
       
    69 	g_source_set_callback (object->source, (GSourceFunc)lglib_source_callback,
       
    70 			       cb, (GDestroyNotify)lglib_callback_destroy);
       
    71 	return 0;
       
    72 }
       
    73 
       
    74 /// source:priority
       
    75 /// Sets or gets priority of source.
       
    76 /// A: integer (optional priority)
       
    77 /// R: integer (when called with no args)
       
    78 static int lglib_source_priority (lua_State *L)
       
    79 {
       
    80 	lglib_source_t *object = luaL_checklglib_source (L, 1);
       
    81 	if (lua_gettop (L) > 1) {
       
    82 		g_source_set_priority (object->source, luaL_checkint (L, 2));
       
    83 		return 0;
       
    84 	} else {
       
    85 		lua_pushnumber (L, g_source_get_priority (object->source));
       
    86 		return 1;
       
    87 	}
       
    88 }
       
    89 
       
    90 /// source:pointer
       
    91 /// Returns pointer to underlying C structure.
       
    92 /// A: g source object
       
    93 /// R: lightuserdata
       
    94 static int lglib_source_pointer (lua_State *L)
       
    95 {
       
    96 	lglib_source_t *object = luaL_checklglib_source (L, 1);
       
    97 	lua_pushlightuserdata (L, object->source);
       
    98 	return 1;
       
    99 }
       
   100 
       
   101 static int lglib_source_gc (lua_State *L)
       
   102 {
       
   103 	lglib_source_t *object = lua_touserdata (L, 1);
       
   104 	g_source_unref (object->source);
       
   105 	return 0;
       
   106 }
       
   107 
       
   108 const luaL_Reg lglib_source_reg_f[] = {
       
   109 	{ "bless", lglib_source_bless_lua },
       
   110 	{ NULL,    NULL                   },
       
   111 };
       
   112 
       
   113 const luaL_Reg lglib_source_reg_m[] = {
       
   114 	{ "context",  lglib_source_context      },
       
   115 	{ "priority", lglib_source_priority     },
       
   116 	{ "callback", lglib_source_set_callback },
       
   117 	{ "pointer",  lglib_source_pointer      },
       
   118 	{ "__gc",     lglib_source_gc           },
       
   119 	{ NULL,       NULL                      },
       
   120 };
       
   121 
       
   122 int luaopen_glib_source (lua_State *L)
       
   123 {
       
   124 	luaL_newmetatable (L, "glib.source");
       
   125 	lua_pushstring (L, "__index");
       
   126 	lua_pushvalue (L, -2);
       
   127 	lua_settable (L, -3);
       
   128 	luaL_register (L, NULL, lglib_source_reg_m);
       
   129 	lua_pop (L, 1);
       
   130 	luaL_register (L, "g.source", lglib_source_reg_f);
       
   131 	return 1;
       
   132 }
       
   133