2 * timer.c - Timer signals management
4 * Copyright © 2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "globalconf.h"
27 #include "common/luaobject.h"
33 struct ev_timer timer
;
36 static lua_class_t timer_class
;
37 LUA_OBJECT_FUNCS(timer_class
, atimer_t
, timer
)
40 ev_timer_emit_signal(struct ev_loop
*loop
, struct ev_timer
*w
, int revents
)
42 luaA_object_push(globalconf
.L
, w
->data
);
43 luaA_object_emit_signal(globalconf
.L
, -1, "timeout", 0);
44 lua_pop(globalconf
.L
, 1);
48 luaA_timer_new(lua_State
*L
)
50 luaA_class_new(L
, &timer_class
);
51 atimer_t
*timer
= luaA_checkudata(L
, -1, &timer_class
);
52 timer
->timer
.data
= timer
;
53 ev_set_cb(&timer
->timer
, ev_timer_emit_signal
);
58 luaA_timer_set_timeout(lua_State
*L
, atimer_t
*timer
)
60 double timeout
= luaL_checknumber(L
, -1);
61 ev_timer_set(&timer
->timer
, timeout
, timeout
);
62 luaA_object_emit_signal(L
, -3, "property::timeout", 0);
67 luaA_timer_get_timeout(lua_State
*L
, atimer_t
*timer
)
69 lua_pushnumber(L
, timer
->timer
.repeat
);
74 luaA_timer_start(lua_State
*L
)
76 atimer_t
*timer
= luaA_checkudata(L
, 1, &timer_class
);
78 luaA_warn(L
, "timer already started");
81 luaA_object_ref(L
, 1);
82 ev_timer_start(globalconf
.loop
, &timer
->timer
);
83 timer
->started
= true;
89 luaA_timer_stop(lua_State
*L
)
91 atimer_t
*timer
= luaA_checkudata(L
, 1, &timer_class
);
94 ev_timer_stop(globalconf
.loop
, &timer
->timer
);
95 luaA_object_unref(L
, timer
);
96 timer
->started
= false;
99 luaA_warn(L
, "timer not started");
103 LUA_OBJECT_EXPORT_PROPERTY(timer
, atimer_t
, started
, lua_pushboolean
)
106 timer_class_setup(lua_State
*L
)
108 static const struct luaL_reg timer_methods
[] =
110 LUA_CLASS_METHODS(timer
)
111 { "__call", luaA_timer_new
},
115 static const struct luaL_reg timer_meta
[] =
117 LUA_OBJECT_META(timer
)
119 { "start", luaA_timer_start
},
120 { "stop", luaA_timer_stop
},
121 { "__gc", luaA_object_gc
},
125 luaA_class_setup(L
, &timer_class
, "timer", (lua_class_allocator_t
) timer_new
,
126 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
127 timer_methods
, timer_meta
);
128 luaA_class_add_property(&timer_class
, A_TK_TIMEOUT
,
129 (lua_class_propfunc_t
) luaA_timer_set_timeout
,
130 (lua_class_propfunc_t
) luaA_timer_get_timeout
,
131 (lua_class_propfunc_t
) luaA_timer_set_timeout
);
132 luaA_class_add_property(&timer_class
, A_TK_STARTED
,
134 (lua_class_propfunc_t
) luaA_timer_get_started
,
138 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80