2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2015-11-09 02:41:20 +01:00
|
|
|
gbx_c_timer.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-09 02:41:20 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-09 02:41:20 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-09 02:41:20 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_C_TIMER_C
|
|
|
|
|
|
|
|
#include "gbx_info.h"
|
|
|
|
|
|
|
|
#ifndef GBX_INFO
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gbx_watch.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_event.h"
|
|
|
|
#include "gbx_c_timer.h"
|
|
|
|
|
2021-03-01 20:21:42 +01:00
|
|
|
#define EXT(_ob) ((CTIMER_EXT *)((CTIMER *)_ob)->ext)
|
|
|
|
|
2022-01-14 12:34:02 +01:00
|
|
|
int CTIMER_active_count = 0;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2021-03-01 20:21:42 +01:00
|
|
|
DECLARE_EVENT(EVENT_Timer);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2022-01-14 12:34:02 +01:00
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
static void enable_timer(CTIMER *_object, bool on)
|
|
|
|
{
|
2022-01-14 12:34:02 +01:00
|
|
|
if (on == (THIS->id != 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
HOOK_DEFAULT(timer, WATCH_timer)((GB_TIMER *)THIS, on);
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
if (on && (THIS->id == 0))
|
|
|
|
GB_Error("Too many active timers");
|
2022-01-14 12:34:02 +01:00
|
|
|
|
|
|
|
CTIMER_active_count += on ? 1 : -1;
|
2022-01-14 19:16:11 +01:00
|
|
|
//fprintf(stderr, "enable_timer: %d\n", CTIMER_active_count);
|
2010-12-15 16:55:16 +01:00
|
|
|
}
|
|
|
|
|
2022-01-14 12:34:02 +01:00
|
|
|
|
2011-01-02 19:24:50 +01:00
|
|
|
CTIMER *CTIMER_every(int delay, GB_TIMER_CALLBACK callback, intptr_t param)
|
2011-01-02 18:09:54 +01:00
|
|
|
{
|
|
|
|
CTIMER *timer;
|
2015-11-09 02:41:20 +01:00
|
|
|
|
2016-04-13 21:57:48 +02:00
|
|
|
timer = OBJECT_create(CLASS_Timer, NULL, NULL, 0);
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(timer);
|
2021-03-01 20:21:42 +01:00
|
|
|
|
2011-01-02 18:09:54 +01:00
|
|
|
timer->delay = delay;
|
2021-03-01 20:21:42 +01:00
|
|
|
timer->task = EXEC_task;
|
|
|
|
|
|
|
|
ALLOC_ZERO(&timer->ext, sizeof(CTIMER_EXT));
|
|
|
|
EXT(timer)->callback = callback;
|
|
|
|
EXT(timer)->tag = param;
|
2015-11-09 02:41:20 +01:00
|
|
|
|
2011-01-02 18:09:54 +01:00
|
|
|
enable_timer(timer, TRUE);
|
2015-11-09 02:41:20 +01:00
|
|
|
|
2011-01-02 18:09:54 +01:00
|
|
|
return timer;
|
|
|
|
}
|
2010-12-15 16:55:16 +01:00
|
|
|
|
2022-01-14 12:34:02 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void CTIMER_raise(void *_object)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2021-03-01 20:21:42 +01:00
|
|
|
if (THIS->task == EXEC_task)
|
2011-01-02 18:09:54 +01:00
|
|
|
{
|
2021-03-01 20:21:42 +01:00
|
|
|
if (THIS_EXT && THIS_EXT->callback)
|
|
|
|
{
|
|
|
|
if (!(*(THIS_EXT->callback))(THIS_EXT->tag))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-14 19:16:11 +01:00
|
|
|
void *parent = OBJECT_parent(THIS);
|
|
|
|
if (parent && OBJECT_is_valid(parent) && !GB_Raise(THIS, EVENT_Timer, 0))
|
2021-03-01 20:21:42 +01:00
|
|
|
return;
|
|
|
|
}
|
2011-01-02 18:09:54 +01:00
|
|
|
}
|
2015-11-09 02:41:20 +01:00
|
|
|
|
2011-01-02 18:09:54 +01:00
|
|
|
enable_timer(THIS, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
|
2016-03-25 14:06:09 +01:00
|
|
|
BEGIN_METHOD(Timer_new, GB_INTEGER delay)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2016-03-25 14:06:09 +01:00
|
|
|
int delay;
|
2007-12-30 17:41:49 +01:00
|
|
|
THIS->id = 0;
|
2016-03-25 14:06:09 +01:00
|
|
|
|
|
|
|
delay = VARGOPT(delay, -1);
|
|
|
|
if (delay < 0)
|
|
|
|
delay = 1000;
|
|
|
|
|
2021-03-01 20:21:42 +01:00
|
|
|
THIS->task = EXEC_task;
|
2016-03-25 14:06:09 +01:00
|
|
|
THIS->delay = delay;
|
|
|
|
if (!MISSING(delay))
|
|
|
|
enable_timer(THIS, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Timer_free)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2022-01-14 12:34:02 +01:00
|
|
|
enable_timer(THIS, FALSE);
|
2021-03-01 20:21:42 +01:00
|
|
|
|
|
|
|
if (THIS->ext)
|
|
|
|
IFREE(THIS->ext);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
BEGIN_METHOD_VOID(Timer_Start)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
enable_timer(THIS, TRUE);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Timer_Stop)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
enable_timer(THIS, FALSE);
|
2021-11-05 18:39:35 +01:00
|
|
|
THIS->triggered = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
|
2015-11-09 02:41:20 +01:00
|
|
|
BEGIN_METHOD_VOID(Timer_Restart)
|
|
|
|
|
|
|
|
enable_timer(THIS, FALSE);
|
|
|
|
enable_timer(THIS, TRUE);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
BEGIN_PROPERTY(Timer_Enabled)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB_ReturnBoolean(THIS->id != 0);
|
|
|
|
else
|
2020-04-08 21:27:27 +02:00
|
|
|
enable_timer(THIS, !!VPROP(GB_BOOLEAN));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2010-12-15 16:55:16 +01:00
|
|
|
|
2018-11-07 17:21:38 +01:00
|
|
|
BEGIN_PROPERTY(Timer_Delay)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB_ReturnInteger(THIS->delay);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int delay = VPROP(GB_INTEGER);
|
|
|
|
bool enabled = THIS->id != 0;
|
|
|
|
|
2021-03-01 20:21:42 +01:00
|
|
|
if (delay < 0 || delay >= (1<<30))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2021-03-01 20:21:42 +01:00
|
|
|
GB_Error(GB_ERR_ARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
HOOK_DEFAULT(timer, WATCH_timer)((GB_TIMER *)THIS, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2021-03-01 20:21:42 +01:00
|
|
|
THIS->delay = delay;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2021-03-01 20:21:42 +01:00
|
|
|
if (enabled)
|
|
|
|
HOOK_DEFAULT(timer, WATCH_timer)((GB_TIMER *)THIS, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2012-11-24 19:52:11 +01:00
|
|
|
static void trigger_timer(void *_object)
|
|
|
|
{
|
2021-11-06 15:17:38 +01:00
|
|
|
if (THIS->triggered)
|
|
|
|
{
|
|
|
|
THIS->triggered = FALSE;
|
|
|
|
GB_Raise(THIS, EVENT_Timer, 0);
|
|
|
|
}
|
2021-11-05 18:39:35 +01:00
|
|
|
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF(_object);
|
2012-11-24 19:52:11 +01:00
|
|
|
}
|
2010-12-15 16:55:16 +01:00
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Timer_Trigger)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-11-24 19:52:11 +01:00
|
|
|
if (THIS->triggered)
|
|
|
|
return;
|
2015-11-09 02:41:20 +01:00
|
|
|
|
2012-11-24 19:52:11 +01:00
|
|
|
THIS->triggered = TRUE;
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(THIS);
|
2012-11-24 19:52:11 +01:00
|
|
|
EVENT_post(trigger_timer, (intptr_t)THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
GB_DESC NATIVE_Timer[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2015-11-09 02:41:20 +01:00
|
|
|
GB_DECLARE("Timer", sizeof(CTIMER)),
|
|
|
|
|
2016-03-25 14:06:09 +01:00
|
|
|
GB_METHOD("_new", NULL, Timer_new, "[(Delay)i]"),
|
2015-11-09 02:41:20 +01:00
|
|
|
GB_METHOD("_free", NULL, Timer_free, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-09 02:41:20 +01:00
|
|
|
GB_PROPERTY("Enabled", "b", Timer_Enabled),
|
2018-11-07 17:21:38 +01:00
|
|
|
GB_PROPERTY("Delay", "i", Timer_Delay),
|
2015-11-09 02:41:20 +01:00
|
|
|
//GB_PROPERTY_READ("Timeout", "f", Timer_Timeout),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-09 02:41:20 +01:00
|
|
|
GB_METHOD("Start", NULL, Timer_Start, NULL),
|
|
|
|
GB_METHOD("Stop", NULL, Timer_Stop, NULL),
|
|
|
|
GB_METHOD("Restart", NULL, Timer_Restart, NULL),
|
|
|
|
GB_METHOD("Trigger", NULL, Timer_Trigger, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-09 02:41:20 +01:00
|
|
|
GB_CONSTANT("_IsControl", "b", TRUE),
|
|
|
|
GB_CONSTANT("_IsVirtual", "b", TRUE),
|
|
|
|
GB_CONSTANT("_Group", "s", "Special"),
|
|
|
|
GB_CONSTANT("_Properties", "s", "Enabled,Delay{Range:0;86400000;10;ms}=1000"),
|
|
|
|
GB_CONSTANT("_DefaultEvent", "s", "Timer"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-09 02:41:20 +01:00
|
|
|
GB_EVENT("Timer", NULL, NULL, &EVENT_Timer),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-11-09 02:41:20 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|