fix client_focused()
[wmiirc-lua.git] / luaeventloop / lel_main.c
blob97cc7c70fade3d745cc7cdbe79529aa1cb7307ea
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <time.h>
7 #include <signal.h>
9 #include <lua.h>
10 #include <lauxlib.h>
12 #include "lel_debug.h"
13 #include "lel_util.h"
14 #include "lel_instance.h"
17 /* ------------------------------------------------------------------------
18 * lua: x = eventloop.new() -- create a new eventloop object
20 static int l_new (lua_State *L)
22 struct lel_eventloop *el;
24 DBGF("** eventloop.new () **\n");
26 el = (struct lel_eventloop*)lua_newuserdata(L, sizeof (struct lel_eventloop));
28 luaL_getmetatable (L, L_EVENTLOOP_MT);
29 lua_setmetatable (L, -2);
31 memset (el, 0, sizeof(*el));
32 FD_ZERO (&el->all_fds);
34 return 1;
37 static int l_eventloop_gc (lua_State *L)
39 struct lel_eventloop *el;
41 el = lel_checkeventloop (L, 1);
43 DBGF("** eventloop:__gc (%p) **\n", el);
45 return 0;
48 /* ------------------------------------------------------------------------
49 * the class method table
51 static const luaL_reg class_table[] =
53 { "new", l_new },
55 { NULL, NULL },
58 /* ------------------------------------------------------------------------
59 * the instance method table
61 static const luaL_reg instance_table[] =
63 { "__tostring", l_eventloop_tostring },
64 { "__gc", l_eventloop_gc },
66 { "add_exec", l_eventloop_add_exec },
67 { "check_exec", l_eventloop_check_exec },
68 { "kill_exec", l_eventloop_kill_exec },
70 { "run_loop", l_eventloop_run_loop },
72 { "kill_all", l_eventloop_kill_all },
74 { NULL, NULL },
77 /* ------------------------------------------------------------------------
78 * the class metatable
80 static int lel_init_eventloop_class (lua_State *L)
82 luaL_newmetatable(L, L_EVENTLOOP_MT);
84 // setup the __index and __gc field
85 lua_pushstring (L, "__index");
86 lua_pushvalue (L, -2); // pushes the new metatable
87 lua_settable (L, -3); // metatable.__index = metatable
89 luaL_openlib (L, NULL, instance_table, 0);
90 luaL_openlib (L, "eventloop", class_table, 0);
92 #if 0
93 luaL_register (L, MYNAME, R);
94 lua_pushliteral (L, "version");
95 lua_pushliteral (L, MYVERSION);
96 lua_settable (L, -3);
97 #endif
98 return 1;
101 /* ------------------------------------------------------------------------
102 * library entry
104 LUALIB_API int luaopen_eventloop (lua_State *L)
106 return lel_init_eventloop_class (L);