PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/src/ftk_source_primary.c

http://ftk.googlecode.com/
C | 146 lines | 94 code | 22 blank | 30 comment | 13 complexity | e1b7f705c41e8aff045d7cf4f7eedc65 MD5 | raw file
  1/*
  2 * File: ftk_source_primary.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   gui primary 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 * 2010-10-02 Jiao JinXing <jiaojinxing1987@gmail.com> add ftk_pipe_check for rt-thread support.
 30 *
 31 */
 32
 33#include "ftk_pipe.h"
 34#include "ftk_globals.h"
 35#include "ftk_source_primary.h"
 36#include "ftk_sources_manager.h"
 37
 38#define MAX_EVENTS 128
 39
 40typedef struct _SourcePrimaryPrivInfo
 41{
 42	FtkPipe* pipe;
 43	FtkOnEvent on_event;
 44	void* user_data;
 45}PrivInfo;
 46
 47static int ftk_source_primary_get_fd(FtkSource* thiz)
 48{
 49	DECL_PRIV(thiz, priv);
 50	return_val_if_fail(priv != NULL, -1);
 51
 52	return ftk_pipe_get_read_handle(priv->pipe);
 53}
 54
 55static int ftk_source_primary_check(FtkSource* thiz)
 56{
 57#ifndef RT_THREAD
 58	return -1;
 59#else
 60	DECL_PRIV(thiz, priv);
 61	return_val_if_fail(priv != NULL, -1);
 62
 63	return ftk_pipe_check(priv->pipe);
 64#endif
 65}
 66
 67static Ret ftk_source_primary_dispatch(FtkSource* thiz)
 68{
 69	FtkEvent event;
 70	DECL_PRIV(thiz, priv);
 71	int ret = ftk_pipe_read(priv->pipe, &event, sizeof(FtkEvent));
 72	return_val_if_fail(ret == sizeof(FtkEvent), RET_REMOVE);
 73
 74	switch(event.type)
 75	{
 76		case FTK_EVT_NOP:
 77		{
 78			break;
 79		}
 80		case FTK_EVT_ADD_SOURCE:
 81		{
 82			ftk_sources_manager_add(ftk_default_sources_manager(), (FtkSource*)event.u.extra);
 83			break;
 84		}
 85		case FTK_EVT_REMOVE_SOURCE:
 86		{
 87			ftk_sources_manager_remove(ftk_default_sources_manager(), (FtkSource*)event.u.extra);
 88			break;
 89		}
 90		default:
 91		{
 92			if(priv->on_event != NULL)
 93			{
 94				priv->on_event(priv->user_data, &event);
 95			}
 96		}
 97	}
 98
 99	return RET_OK;
100}
101
102static void ftk_source_primary_destroy(FtkSource* thiz)
103{
104	if(thiz != NULL)
105	{
106		DECL_PRIV(thiz, priv);
107		ftk_pipe_destroy(priv->pipe);
108		FTK_ZFREE(thiz, sizeof(FtkSource) + sizeof(PrivInfo));
109	}
110
111	return;
112}
113
114FtkSource* ftk_source_primary_create(FtkOnEvent on_event, void* user_data)
115{
116	FtkSource* thiz = (FtkSource*)FTK_ZALLOC(sizeof(FtkSource) + sizeof(PrivInfo));
117
118	if(thiz != NULL)
119	{
120		DECL_PRIV(thiz, priv);
121		
122		thiz->get_fd   = ftk_source_primary_get_fd;
123		thiz->check    = ftk_source_primary_check;
124		thiz->dispatch = ftk_source_primary_dispatch;
125		thiz->destroy  = ftk_source_primary_destroy;
126
127		thiz->ref = 1;
128		priv->pipe = ftk_pipe_create();
129		priv->on_event  = on_event;
130		priv->user_data = user_data;
131	}
132
133	return thiz;
134}
135
136Ret ftk_source_queue_event(FtkSource* thiz, FtkEvent* event)
137{
138	int ret = 0;
139	DECL_PRIV(thiz, priv);
140	return_val_if_fail(thiz != NULL && event != NULL, RET_FAIL);
141
142	ret = ftk_pipe_write(priv->pipe, event, sizeof(FtkEvent));
143
144	return ret == sizeof(FtkEvent) ? RET_OK : RET_FAIL;
145}
146