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.
32 struct ev_timer timer
;
35 static lua_class_t timer_class
;
36 LUA_OBJECT_FUNCS(timer_class
, atimer_t
, timer
, "timer")
39 ev_timer_emit_signal(struct ev_loop
*loop
, struct ev_timer
*w
, int revents
)
41 luaA_object_push(globalconf
.L
, w
->data
);
42 luaA_object_emit_signal(globalconf
.L
, -1, "timeout", 0);
43 lua_pop(globalconf
.L
, 1);
47 luaA_timer_new(lua_State
*L
)
49 luaA_class_new(L
, &timer_class
);
50 atimer_t
*timer
= luaL_checkudata(L
, -1, "timer");
51 timer
->timer
.data
= timer
;
52 ev_set_cb(&timer
->timer
, ev_timer_emit_signal
);
57 luaA_timer_set_timeout(lua_State
*L
, atimer_t
*timer
)
59 double timeout
= luaL_checknumber(L
, -1);
60 ev_timer_set(&timer
->timer
, timeout
, timeout
);
61 luaA_object_emit_signal(L
, -3, "property::timeout", 0);
66 luaA_timer_get_timeout(lua_State
*L
, atimer_t
*timer
)
68 lua_pushnumber(L
, timer
->timer
.repeat
);
73 luaA_timer_start(lua_State
*L
)
75 atimer_t
*timer
= luaL_checkudata(L
, 1, "timer");
77 luaA_warn(L
, "timer already started");
80 luaA_object_ref(L
, 1);
81 ev_timer_start(globalconf
.loop
, &timer
->timer
);
82 timer
->started
= true;
88 luaA_timer_stop(lua_State
*L
)
90 atimer_t
*timer
= luaL_checkudata(L
, 1, "timer");
93 ev_timer_stop(globalconf
.loop
, &timer
->timer
);
94 luaA_object_unref(L
, timer
);
95 timer
->started
= false;
98 luaA_warn(L
, "timer not started");
102 LUA_OBJECT_EXPORT_PROPERTY(timer
, atimer_t
, started
, lua_pushboolean
)
105 timer_class_setup(lua_State
*L
)
107 static const struct luaL_reg timer_methods
[] =
109 LUA_CLASS_METHODS(timer
)
110 { "__call", luaA_timer_new
},
114 static const struct luaL_reg timer_meta
[] =
116 LUA_OBJECT_META(timer
)
118 { "start", luaA_timer_start
},
119 { "stop", luaA_timer_stop
},
120 { "__gc", luaA_object_gc
},
124 luaA_class_setup(L
, &timer_class
, "timer", (lua_class_allocator_t
) timer_new
,
125 timer_methods
, timer_meta
);
126 luaA_class_add_property(&timer_class
, A_TK_TIMEOUT
, "timeout",
127 (lua_class_propfunc_t
) luaA_timer_set_timeout
,
128 (lua_class_propfunc_t
) luaA_timer_get_timeout
,
129 (lua_class_propfunc_t
) luaA_timer_set_timeout
);
130 luaA_class_add_property(&timer_class
, A_TK_STARTED
, "started",
132 (lua_class_propfunc_t
) luaA_timer_get_started
,
136 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80