PageRenderTime 52ms CodeModel.GetById 30ms app.highlight 7ms RepoModel.GetById 13ms app.codeStats 0ms

/src/ftk_source_idle.c

http://ftk.googlecode.com/
C | 125 lines | 76 code | 20 blank | 29 comment | 10 complexity | 69200b0d3f4dfd9ad592ece4882f1ee2 MD5 | raw file
  1/*
  2 * File: ftk_source_idle.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   idle source 
  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_source_idle.h"
 32
 33typedef struct _SourceIdlePrivInfo
 34{
 35	FtkIdle action;
 36	void* user_data;
 37}PrivInfo;
 38
 39static int ftk_source_idle_get_fd(FtkSource* thiz)
 40{
 41	return -1;
 42}
 43
 44static int ftk_source_idle_check(FtkSource* thiz)
 45{
 46	return 0;
 47}
 48
 49static Ret ftk_source_idle_dispatch(FtkSource* thiz)
 50{
 51	DECL_PRIV(thiz, priv);
 52
 53	return_val_if_fail(priv->action != NULL, RET_REMOVE);
 54
 55	if(thiz->disable > 0)
 56	{
 57		return RET_OK;
 58	}
 59
 60	return priv->action(priv->user_data);
 61}
 62
 63static void ftk_source_idle_destroy(FtkSource* thiz)
 64{
 65	FTK_ZFREE(thiz, sizeof(FtkSource) + sizeof(PrivInfo));
 66
 67	return;
 68}
 69
 70FtkSource* ftk_source_idle_create(FtkIdle action, void* user_data)
 71{
 72	FtkSource* thiz = NULL;
 73	return_val_if_fail(action != NULL, NULL);
 74
 75	thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
 76	if(thiz != NULL)
 77	{
 78		DECL_PRIV(thiz, priv);
 79		thiz->get_fd   = ftk_source_idle_get_fd;
 80		thiz->check    = ftk_source_idle_check;
 81		thiz->dispatch = ftk_source_idle_dispatch;
 82		thiz->destroy  = ftk_source_idle_destroy;
 83
 84		thiz->ref = 1;
 85		priv->action = action;
 86		priv->user_data = user_data;
 87	}
 88
 89	return thiz;
 90}
 91
 92#ifdef FTK_SOURCE_IDLE_TEST
 93
 94Ret my_action(void* user_data)
 95{
 96	int *p = (int*)user_data;
 97	(*p)++;
 98
 99	return *p == 10 ? RET_REMOVE : RET_OK;
100}
101
102int main(int argc, char* argv[])
103{
104	int n = 0;
105	FtkSource* thiz = ftk_source_idle_create(my_action, &n);
106
107	while(1)
108	{
109		assert(ftk_source_check(thiz) == 0);
110		assert(ftk_source_get_fd(thiz) < 0);
111		if(n < 9)
112		{
113			assert(ftk_source_dispatch(thiz) == RET_OK);
114		}
115		else
116		{
117			assert(ftk_source_dispatch(thiz) == RET_REMOVE);
118			break;
119		}
120	}
121	ftk_source_destroy(thiz);
122
123	return 0;
124}
125#endif/*FTK_SOURCE_IDLE_TEST*/