PageRenderTime 42ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 0ms

/src/ftk_source_timer.c

http://ftk.googlecode.com/
C | 134 lines | 80 code | 25 blank | 29 comment | 8 complexity | c77b4261bc89666dffa7e81af4482fec MD5 | raw file
  1/*
  2 * File: ftk_source_timer.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   timer.
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31#include "ftk_typedef.h"
 32#include "ftk_source_timer.h"
 33
 34typedef struct _SourceTimerPrivInfo
 35{
 36	FtkTimer action;
 37	void* user_data;
 38	size_t interval;
 39	size_t next_time;
 40}PrivInfo;
 41
 42static int ftk_source_timer_get_fd(FtkSource* thiz)
 43{
 44	return -1;
 45}
 46
 47static int ftk_source_timer_check(FtkSource* thiz)
 48{
 49	DECL_PRIV(thiz, priv);
 50	int t = priv->next_time - ftk_get_relative_time();
 51
 52	t = t < 0 ? 0 : t;
 53
 54	return t;
 55}
 56
 57static void ftk_source_timer_calc_timer(PrivInfo* priv)
 58{
 59	priv->next_time = ftk_get_relative_time() + priv->interval;
 60
 61	return;
 62}
 63
 64Ret ftk_source_timer_reset(FtkSource* thiz)
 65{
 66	DECL_PRIV(thiz, priv);
 67	return_val_if_fail(priv != NULL, RET_FAIL);
 68
 69	thiz->disable = 0;
 70	ftk_source_timer_calc_timer(priv);
 71
 72	return RET_OK;
 73}
 74
 75Ret ftk_source_timer_modify(FtkSource* thiz, int interval)
 76{
 77	DECL_PRIV(thiz, priv);
 78	return_val_if_fail(priv != NULL, RET_FAIL);
 79
 80	priv->interval = interval;
 81	ftk_source_timer_calc_timer(priv);
 82
 83	return RET_OK;
 84}
 85
 86static Ret ftk_source_timer_dispatch(FtkSource* thiz)
 87{
 88	Ret ret = RET_FAIL;
 89	DECL_PRIV(thiz, priv);
 90	return_val_if_fail(priv->action != NULL, RET_REMOVE);
 91
 92	if(thiz->disable > 0)
 93	{
 94		ftk_source_timer_calc_timer(priv);
 95		return RET_OK;
 96	}
 97
 98	ret = priv->action(priv->user_data);
 99	ftk_source_timer_calc_timer(priv);
100
101	return ret;
102}
103
104static void ftk_source_timer_destroy(FtkSource* thiz)
105{
106	FTK_ZFREE(thiz, sizeof(FtkSource) + sizeof(PrivInfo));
107
108	return;
109}
110
111FtkSource* ftk_source_timer_create(int interval, FtkTimer action, void* user_data)
112{
113	FtkSource* thiz = NULL;
114	return_val_if_fail(interval > 0 && action != NULL, NULL);
115
116	thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
117	if(thiz != NULL)
118	{
119		DECL_PRIV(thiz, priv);
120		thiz->get_fd   = ftk_source_timer_get_fd;
121		thiz->check    = ftk_source_timer_check;
122		thiz->dispatch = ftk_source_timer_dispatch;
123		thiz->destroy  = ftk_source_timer_destroy;
124
125		thiz->ref = 1;
126		priv->interval  = interval;
127		priv->user_data = user_data;
128		priv->action    = action;
129		ftk_source_timer_calc_timer(priv);
130	}
131
132	return thiz;
133}
134