/* Milkway * * Copyright (C) 2008- Luo Jinghua * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #ifndef MW_SOURCE_H #define MW_SOURCE_H #include #include typedef struct mw_source_type mw_source_type_t; typedef struct mw_source mw_source_t; #define MW_SOURCE_TYPE mw_source_get_type() struct mw_source_type { mw_object_type_t base; mw_bool_t (*prepare) (mw_source_t *self, int *timeout_); mw_bool_t (*check) (mw_source_t *self); mw_bool_t (*dispatch) (mw_source_t *self); mw_bool_t (*action) (mw_source_t *self); }; typedef enum { MW_SOURCE_ACTIVE = 1 << 0, MW_SOURCE_BLOCKED = 1 << 1, MW_SOURCE_READY = 1 << 2, MW_SOURCE_CAN_RECURSE = 1 << 3, MW_SOURCE_DISPATCHING = 1 << 4 } mw_source_flags_t; typedef mw_bool_t (*mw_source_func_t)(mw_source_t *source, mw_pointer_t user_data); struct mw_source { mw_object_t base; /** priority */ int prio; mw_source_flags_t flags; mw_slist_t *fds; void *context; mw_uint_t id; mw_source_t *next; mw_source_t *prev; /** callback */ mw_source_func_t func; mw_pointer_t user_data; mw_destroy_func_t destroy_func; }; #define MW_PRIORITY_HIGH -100 #define MW_PRIORITY_DEFAULT 0 #define MW_PRIORITY_HIGH_IDLE 100 #define MW_PRIORITY_DEFAULT_IDLE 200 #define MW_PRIORITY_LOW 300 mw_public mw_source_type_t* mw_source_get_type(void); mw_public mw_source_t* mw_source_init(mw_source_t *self); static mw_inline mw_bool_t mw_source_prepare(mw_source_t *self, int *timeout_) { mw_source_type_t *type = (mw_source_type_t *)MW_TYPE(self); return type->prepare(self, timeout_); } static mw_inline mw_bool_t mw_source_check(mw_source_t *self) { mw_source_type_t *type = (mw_source_type_t *)MW_TYPE(self); return type->check(self); } static mw_inline mw_bool_t mw_source_dispatch(mw_source_t *self) { mw_source_type_t *type = (mw_source_type_t *)MW_TYPE(self); return type->dispatch(self); } static mw_inline mw_bool_t mw_source_is_active(mw_source_t *self) { return self->flags & MW_SOURCE_ACTIVE; } static mw_inline mw_bool_t mw_source_is_dispatching(mw_source_t *self) { return self->flags & MW_SOURCE_DISPATCHING; } static mw_inline mw_bool_t mw_source_get_can_recurse(mw_source_t *self) { return self->flags & MW_SOURCE_CAN_RECURSE; } static mw_inline void mw_source_set_can_recurse(mw_source_t *self) { self->flags |= MW_SOURCE_CAN_RECURSE; } static mw_inline mw_bool_t mw_source_is_blocked(mw_source_t *self) { return self->flags & MW_SOURCE_DISPATCHING && !(self->flags & MW_SOURCE_CAN_RECURSE); } static mw_inline mw_bool_t mw_source_is_ready(mw_source_t *self) { return self->flags & MW_SOURCE_READY; } static mw_inline void mw_source_set_ready(mw_source_t *self) { self->flags |= MW_SOURCE_READY; } static mw_inline mw_uint_t mw_source_get_id(mw_source_t *self) { return self->id; } mw_public void mw_source_set_priority(mw_source_t *self, int prio); mw_public void mw_source_add_poll (mw_source_t *source, mw_pollfd_t *fd); mw_public void mw_source_remove_poll (mw_source_t *source, mw_pollfd_t *fd); mw_public void mw_source_get_current_time (mw_source_t *source, mw_timeval_t *timeval); mw_public void mw_source_set_callback (mw_source_t *source, mw_source_func_t func, mw_pointer_t user_data, mw_destroy_func_t destroy_func); mw_public void mw_source_destroy(mw_source_t *self); #endif