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.
22 #include "globalconf.h"
25 #include "common/luaobject.h"
35 static lua_class_t timer_class
;
36 LUA_OBJECT_FUNCS(timer_class
, atimer_t
, timer
)
39 timer_emit_signal(gpointer data
)
41 luaA_object_push(globalconf
.L
, data
);
42 luaA_object_emit_signal(globalconf
.L
, -1, "timeout", 0);
43 lua_pop(globalconf
.L
, 1);
48 luaA_timer_new(lua_State
*L
)
50 luaA_class_new(L
, &timer_class
);
55 luaA_timer_set_timeout(lua_State
*L
, atimer_t
*timer
)
57 double timeout
= luaL_checknumber(L
, -1);
58 timer
->timeout
= timeout
;
59 luaA_object_emit_signal(L
, -3, "property::timeout", 0);
64 luaA_timer_get_timeout(lua_State
*L
, atimer_t
*timer
)
66 lua_pushnumber(L
, timer
->timeout
);
71 luaA_timer_start(lua_State
*L
)
73 atimer_t
*timer
= luaA_checkudata(L
, 1, &timer_class
);
75 luaA_warn(L
, "timer already started");
78 luaA_object_ref(L
, 1);
79 timer
->started
= true;
80 timer
->source_id
= g_timeout_add(timer
->timeout
* 1000, timer_emit_signal
, timer
);
86 luaA_timer_stop(lua_State
*L
)
88 atimer_t
*timer
= luaA_checkudata(L
, 1, &timer_class
);
91 g_source_remove(timer
->source_id
);
92 luaA_object_unref(L
, timer
);
93 timer
->started
= false;
96 luaA_warn(L
, "timer not started");
101 luaA_timer_again(lua_State
*L
)
103 atimer_t
*timer
= luaA_checkudata(L
, 1, &timer_class
);
106 g_source_remove(timer
->source_id
);
108 luaA_object_ref(L
, 1);
109 timer
->started
= true;
110 timer
->source_id
= g_timeout_add(timer
->timeout
* 1000, timer_emit_signal
, timer
);
115 LUA_OBJECT_EXPORT_PROPERTY(timer
, atimer_t
, started
, lua_pushboolean
)
118 timer_class_setup(lua_State
*L
)
120 static const struct luaL_Reg timer_methods
[] =
122 LUA_CLASS_METHODS(timer
)
123 { "__call", luaA_timer_new
},
127 static const struct luaL_Reg timer_meta
[] =
129 LUA_OBJECT_META(timer
)
131 { "start", luaA_timer_start
},
132 { "stop", luaA_timer_stop
},
133 { "again", luaA_timer_again
},
137 luaA_class_setup(L
, &timer_class
, "timer", NULL
,
138 (lua_class_allocator_t
) timer_new
, NULL
, NULL
,
139 luaA_class_index_miss_property
, luaA_class_newindex_miss_property
,
140 timer_methods
, timer_meta
);
141 luaA_class_add_property(&timer_class
, "timeout",
142 (lua_class_propfunc_t
) luaA_timer_set_timeout
,
143 (lua_class_propfunc_t
) luaA_timer_get_timeout
,
144 (lua_class_propfunc_t
) luaA_timer_set_timeout
);
145 luaA_class_add_property(&timer_class
, "started",
147 (lua_class_propfunc_t
) luaA_timer_get_started
,
150 signal_add(&timer_class
.signals
, "property::timeout");
151 signal_add(&timer_class
.signals
, "timeout");
154 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80