client: emit class signal on focus
[awesome.git] / timer.c
blobc8922670f794c453eb14a456f7cd6cc77c5f0796
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 "structs.h"
25 #include "luaa.h"
26 #include "timer.h"
28 typedef struct
30 LUA_OBJECT_HEADER
31 bool started;
32 struct ev_timer timer;
33 } atimer_t;
35 static lua_class_t timer_class;
36 LUA_OBJECT_FUNCS(timer_class, atimer_t, timer, "timer")
38 static void
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);
46 static int
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);
53 return 1;
56 static int
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);
62 return 0;
65 static int
66 luaA_timer_get_timeout(lua_State *L, atimer_t *timer)
68 lua_pushnumber(L, timer->timer.repeat);
69 return 1;
72 static int
73 luaA_timer_start(lua_State *L)
75 atimer_t *timer = luaL_checkudata(L, 1, "timer");
76 if(timer->started)
77 luaA_warn(L, "timer already started");
78 else
80 luaA_object_ref(L, 1);
81 ev_timer_start(globalconf.loop, &timer->timer);
82 timer->started = true;
84 return 0;
87 static int
88 luaA_timer_stop(lua_State *L)
90 atimer_t *timer = luaL_checkudata(L, 1, "timer");
91 if(timer->started)
93 ev_timer_stop(globalconf.loop, &timer->timer);
94 luaA_object_unref(L, timer);
95 timer->started = false;
97 else
98 luaA_warn(L, "timer not started");
99 return 0;
102 LUA_OBJECT_EXPORT_PROPERTY(timer, atimer_t, started, lua_pushboolean)
104 void
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 },
111 { NULL, NULL }
114 static const struct luaL_reg timer_meta[] =
116 LUA_OBJECT_META(timer)
117 LUA_CLASS_META
118 { "start", luaA_timer_start },
119 { "stop", luaA_timer_stop },
120 { "__gc", luaA_object_gc },
121 { NULL, NULL },
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",
131 NULL,
132 (lua_class_propfunc_t) luaA_timer_get_started,
133 NULL);
136 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80