00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00069
00070
00071
00072
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 (; 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
00138
00139