Wed, 05 Sep 2007 23:05:05 -0400
Completely refactored event_callback creation out into event_callback.
21
728aafac9682
Added missing license header
Thomas Harning Jr <harningt@gmail.com>
parents:
20
diff
changeset
|
1 | /* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com> |
728aafac9682
Added missing license header
Thomas Harning Jr <harningt@gmail.com>
parents:
20
diff
changeset
|
2 | * Licensed as LGPL - See doc/COPYING for details */ |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
3 | #include "event_callback.h" |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
4 | #include <assert.h> |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
5 | #include <lauxlib.h> |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
6 | |
22
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
7 | #define EVENT_CALLBACK_ARG_MT "EVENT_CALLBACK_ARG_MT" |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
8 | |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
9 | void freeCallbackArgs(le_callback* arg, lua_State* L) { |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
10 | if(arg->base) { |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
11 | arg->base = NULL; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
12 | event_del(&arg->ev); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
13 | luaL_unref(L, LUA_REGISTRYINDEX, arg->callbackRef); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
14 | } |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
15 | } |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
16 | /* le_callback is allocated at the beginning of the coroutine in which it |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
17 | is used, no need to manually de-allocate */ |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
18 | |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
19 | /* Index for coroutine is fd as integer for *nix, as lightuserdata for Win */ |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
20 | void luaevent_callback(int fd, short event, void* p) { |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
21 | le_callback* arg = p; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
22 | lua_State* L; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
23 | int ret; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
24 | assert(arg && arg->base && arg->base->loop_L); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
25 | L = arg->base->loop_L; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
26 | lua_rawgeti(L, LUA_REGISTRYINDEX, arg->callbackRef); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
27 | lua_pushinteger(L, event); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
28 | lua_call(L, 1, 1); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
29 | ret = lua_tointeger(L, -1); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
30 | lua_pop(L, 1); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
31 | if(ret == -1) { |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
32 | freeCallbackArgs(arg, L); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
33 | } else { |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
34 | struct event *ev = &arg->ev; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
35 | int newEvent = ret; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
36 | if(newEvent != event) { // Need to hook up new event... |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
37 | event_del(ev); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
38 | event_set(ev, fd, EV_PERSIST | newEvent, luaevent_callback, arg); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
39 | event_add(ev, NULL); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
40 | } |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
41 | } |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
42 | } |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
43 | |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
44 | static int luaevent_cb_gc(lua_State* L) { |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
45 | le_callback* arg = luaL_checkudata(L, 1, EVENT_CALLBACK_ARG_MT); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
46 | freeCallbackArgs(arg, L); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
47 | return 0; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
48 | } |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
49 | |
22
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
50 | le_callback* event_callback_push(lua_State* L, int baseIdx, int callbackIdx) { |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
51 | le_callback* cb; |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
52 | le_base *base = event_base_get(L, baseIdx); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
53 | luaL_checktype(L, callbackIdx, LUA_TFUNCTION); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
54 | cb = lua_newuserdata(L, sizeof(*cb)); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
55 | luaL_getmetatable(L, EVENT_CALLBACK_ARG_MT); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
56 | lua_setmetatable(L, -2); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
57 | |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
58 | lua_pushvalue(L, callbackIdx); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
59 | cb->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX); |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
60 | cb->base = base; |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
61 | return cb; |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
62 | } |
48a109847dce
Completely refactored event_callback creation out into event_callback.
Thomas Harning Jr <harningt@gmail.com>
parents:
21
diff
changeset
|
63 | |
20
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
64 | int event_callback_register(lua_State* L) { |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
65 | luaL_newmetatable(L, EVENT_CALLBACK_ARG_MT); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
66 | lua_pushcfunction(L, luaevent_cb_gc); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
67 | lua_setfield(L, -2, "__gc"); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
68 | lua_newtable(L); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
69 | lua_pushcfunction(L, luaevent_cb_gc); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
70 | lua_setfield(L, -2, "close"); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
71 | lua_setfield(L, -2, "__index"); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
72 | lua_pop(L, 1); |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
73 | return 0; |
71bc2e49366c
Beginning refactoring of the event_callback outside of the core
Thomas Harning Jr <harningt@gmail.com>
parents:
diff
changeset
|
74 | } |