Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

event.h

Go to the documentation of this file.
00001 /*
00002  * $Id: event.h,v 1.6 2005/08/31 21:37:33 rmanfredi Exp $
00003  *
00004  * Copyright (c) 2002-2003, Richard Eckart
00005  *
00006  *----------------------------------------------------------------------
00007  * This file is part of gtk-gnutella.
00008  *
00009  *  gtk-gnutella is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  gtk-gnutella is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with gtk-gnutella; if not, write to the Free Software
00021  *  Foundation, Inc.:
00022  *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *----------------------------------------------------------------------
00024  */
00025 
00026 #ifndef _event_h_
00027 #define _event_h_
00028 
00029 #include "common.h"
00030 
00031 #include "tm.h"
00032 
00033 typedef enum frequency_type {
00034     FREQ_SECS,
00035     FREQ_UPDATES
00036 } frequency_t;
00037 
00038 struct subscriber {
00039     GCallback           cb;
00040     enum frequency_type f_type;
00041     guint32             f_interval;
00042     time_t              last_call;
00043 };
00044 
00045 typedef struct event {
00046     const gchar *name;
00047     guint32      triggered_count;
00048     GSList      *subscribers;
00049 } event_t;
00050 
00051 struct event *event_new(const gchar *name);
00052 
00053 #define event_destroy(evt) G_STMT_START {                          \
00054     real_event_destroy(evt);                                       \
00055     G_FREE_NULL(evt);                                              \
00056 } G_STMT_END
00057 
00058 void real_event_destroy(struct event *evt);
00059 
00060 void event_add_subscriber(
00061     struct event *evt, GCallback cb, frequency_t t, guint32 interval);
00062 void event_remove_subscriber(struct event *evt, GCallback cb);
00063 
00064 guint event_subscriber_count(struct event *evt);
00065 gboolean event_subscriber_active(struct event *evt);
00066 
00067 /*
00068  * T_VETO:   breaks trigger chain as soon as a subscriber returns
00069  *           a value != 0.
00070  *
00071  * T_NORMAL: will call all subscribers in the chain. Use for
00072  *           callbacks with a void return type.
00073  */
00074 #define T_VETO(sig, params) if (((sig) vars_.s->cb) params ) break;
00075 #define T_NORMAL(sig, params)   ((sig) vars_.s->cb) params ;
00076 
00077 #define event_trigger(ev, callback) G_STMT_START {                          \
00078     struct {                                                                \
00079         GSList *sl;                                                         \
00080         event_t *evt;                                                       \
00081         struct subscriber *s;                                               \
00082         time_t now;                                                         \
00083         gboolean t;                                                         \
00084     } vars_;                                                                \
00085                                                                             \
00086     vars_.evt = (ev);                                                       \
00087     vars_.now = (time_t) -1;                                                \
00088     vars_.sl = vars_.evt->subscribers;                                      \
00089     for (/* NOTHING */; vars_.sl; vars_.sl = g_slist_next(vars_.sl)) {      \
00090         vars_.s = vars_.sl->data;                                           \
00091         vars_.t = 0 == vars_.s->f_interval;                                 \
00092         if (!vars_.t) {                                                     \
00093             switch (vars_.s->f_type) {                                      \
00094             case FREQ_UPDATES:                                              \
00095                 vars_.t = 0 == (vars_.evt->triggered_count %                \
00096                                     vars_.s->f_interval);                   \
00097                 break;                                                      \
00098             case FREQ_SECS:                                                 \
00099                 if ((time_t) -1 == vars_.now)                               \
00100                     vars_.now = tm_time();                                  \
00101                 vars_.t = vars_.s->f_interval <=                            \
00102                         (guint32) delta_time(vars_.now, vars_.s->last_call);\
00103                 break;                                                      \
00104             default:                                                        \
00105                 g_assert_not_reached();                                     \
00106             }                                                               \
00107         }                                                                   \
00108         if (vars_.t) {                                                      \
00109             if (FREQ_SECS == vars_.s->f_type) {                             \
00110                 if ((time_t) -1 == vars_.now)                               \
00111                     vars_.now = tm_time();                                  \
00112                 vars_.s->last_call = vars_.now;                             \
00113             }                                                               \
00114             callback                                                        \
00115         }                                                                   \
00116     }                                                                       \
00117     vars_.evt->triggered_count++;                                           \
00118 } G_STMT_END
00119 
00120 struct event_table {
00121     GHashTable *events;
00122 };
00123 
00124 struct event_table *event_table_new(void);
00125 
00126 #define event_table_destroy(t) G_STMT_START {                      \
00127     real_event_table_destroy(t);                                   \
00128     G_FREE_NULL(t);                                                \
00129 } G_STMT_END
00130 void real_event_table_destroy(struct event_table *t, gboolean cleanup);
00131 
00132 
00133 void event_table_add_event(struct event_table *t, struct event *evt);
00134 void event_table_remove_event(struct event_table *t, struct event *evt);
00135 void event_table_remove_all(struct event_table *t);
00136 
00137 #endif  /* _event_h_ */
00138 
00139 /* vi: set ts=4 sw=4 cindent: */

Generated on Sun Feb 12 10:49:56 2006 for Gtk-Gnutella by doxygen 1.3.6