Add a "deep" option to awful.util.table.clone
[awesome.git] / objects / timer.c
blob36dc17cfb5d77264f7b2b65097294aefb8d0cc36
1 /*
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"
23 #include "luaa.h"
24 #include "timer.h"
25 #include "common/luaobject.h"
27 typedef struct
29 LUA_OBJECT_HEADER
30 bool started;
31 guint source_id;
32 double timeout;
33 } atimer_t;
35 static lua_class_t timer_class;
36 LUA_OBJECT_FUNCS(timer_class, atimer_t, timer)
38 static gboolean
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);
44 return TRUE;
47 static int
48 luaA_timer_new(lua_State *L)
50 luaA_class_new(L, &timer_class);
51 return 1;
54 static int
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);
60 return 0;
63 static int
64 luaA_timer_get_timeout(lua_State *L, atimer_t *timer)
66 lua_pushnumber(L, timer->timeout);
67 return 1;
70 static int
71 luaA_timer_start(lua_State *L)
73 atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
74 if(timer->started)
75 luaA_warn(L, "timer already started");
76 else
78 luaA_object_ref(L, 1);
79 timer->started = true;
80 timer->source_id = g_timeout_add(timer->timeout * 1000, timer_emit_signal, timer);
82 return 0;
85 static int
86 luaA_timer_stop(lua_State *L)
88 atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
89 if(timer->started)
91 g_source_remove(timer->source_id);
92 luaA_object_unref(L, timer);
93 timer->started = false;
95 else
96 luaA_warn(L, "timer not started");
97 return 0;
100 static int
101 luaA_timer_again(lua_State *L)
103 atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
105 if (timer->started)
106 g_source_remove(timer->source_id);
107 else
108 luaA_object_ref(L, 1);
109 timer->started = true;
110 timer->source_id = g_timeout_add(timer->timeout * 1000, timer_emit_signal, timer);
112 return 0;
115 LUA_OBJECT_EXPORT_PROPERTY(timer, atimer_t, started, lua_pushboolean)
117 void
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 },
124 { NULL, NULL }
127 static const struct luaL_Reg timer_meta[] =
129 LUA_OBJECT_META(timer)
130 LUA_CLASS_META
131 { "start", luaA_timer_start },
132 { "stop", luaA_timer_stop },
133 { "again", luaA_timer_again },
134 { NULL, NULL },
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",
146 NULL,
147 (lua_class_propfunc_t) luaA_timer_get_started,
148 NULL);
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