awful.mouse: load finder
[awesome.git] / timer.c
blob0ba3579fdef5952cb6554fafc342717e78bdca06
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 <ev.h>
24 #include "globalconf.h"
25 #include "luaa.h"
26 #include "timer.h"
27 #include "common/luaobject.h"
29 typedef struct
31 LUA_OBJECT_HEADER
32 bool started;
33 struct ev_timer timer;
34 } atimer_t;
36 static lua_class_t timer_class;
37 LUA_OBJECT_FUNCS(timer_class, atimer_t, timer)
39 static void
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);
47 static int
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);
54 return 1;
57 static int
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);
63 return 0;
66 static int
67 luaA_timer_get_timeout(lua_State *L, atimer_t *timer)
69 lua_pushnumber(L, timer->timer.repeat);
70 return 1;
73 static int
74 luaA_timer_start(lua_State *L)
76 atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
77 if(timer->started)
78 luaA_warn(L, "timer already started");
79 else
81 luaA_object_ref(L, 1);
82 ev_timer_start(globalconf.loop, &timer->timer);
83 timer->started = true;
85 return 0;
88 static int
89 luaA_timer_stop(lua_State *L)
91 atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
92 if(timer->started)
94 ev_timer_stop(globalconf.loop, &timer->timer);
95 luaA_object_unref(L, timer);
96 timer->started = false;
98 else
99 luaA_warn(L, "timer not started");
100 return 0;
103 LUA_OBJECT_EXPORT_PROPERTY(timer, atimer_t, started, lua_pushboolean)
105 void
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 },
112 { NULL, NULL }
115 static const struct luaL_reg timer_meta[] =
117 LUA_OBJECT_META(timer)
118 LUA_CLASS_META
119 { "start", luaA_timer_start },
120 { "stop", luaA_timer_stop },
121 { "__gc", luaA_object_gc },
122 { NULL, NULL },
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,
133 NULL,
134 (lua_class_propfunc_t) luaA_timer_get_started,
135 NULL);
138 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80