Mercurial > pidgin
annotate src/event.c @ 5223:e2e5bc3ca705
[gaim-migrate @ 5593]
debug_printf -> gaim_debug
My last one for the night.
committer: Tailor Script <tailor@pidgin.im>
| author | Christian Hammond <chipx86@chipx86.com> |
|---|---|
| date | Sat, 26 Apr 2003 09:05:51 +0000 |
| parents | 0241d6b6702d |
| children | 5160333a80df |
| rev | line source |
|---|---|
| 5205 | 1 /** |
| 2 * @file event.c Event API | |
| 3 * @ingroup core | |
| 4 * | |
| 5 * gaim | |
| 6 * | |
| 7 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org> | |
| 8 * | |
| 9 * This program is free software; you can redistribute it and/or modify | |
| 10 * it under the terms of the GNU General Public License as published by | |
| 11 * the Free Software Foundation; either version 2 of the License, or | |
| 12 * (at your option) any later version. | |
| 13 * | |
| 14 * This program is distributed in the hope that it will be useful, | |
| 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 * GNU General Public License for more details. | |
| 18 * | |
| 19 * You should have received a copy of the GNU General Public License | |
| 20 * along with this program; if not, write to the Free Software | |
| 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 22 */ | |
| 23 #include "event.h" | |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
24 #include "debug.h" |
| 5205 | 25 |
| 26 #include <sys/time.h> | |
| 27 #include <stdarg.h> | |
| 28 #include <stdio.h> | |
| 29 #include <string.h> | |
| 30 | |
| 31 /** | |
| 32 * A signal callback. | |
| 33 */ | |
| 34 typedef struct | |
| 35 { | |
| 36 void *handle; /**< The plugin module handle. */ | |
| 37 GaimEvent event; /**< The event type. */ | |
| 38 void *function; /**< The function to call. */ | |
| 39 void *data; /**< The data to pass to the function. */ | |
| 40 | |
| 41 } GaimSignalCallback; | |
| 42 | |
| 43 /** | |
| 44 * A broadcast function. | |
| 45 */ | |
| 46 typedef struct | |
| 47 { | |
| 48 GaimSignalBroadcastFunc func; | |
| 49 void *data; | |
| 50 | |
| 51 } GaimSignalBroadcaster; | |
| 52 | |
| 53 static GList *callbacks = NULL; | |
| 54 static GList *broadcasters = NULL; | |
| 55 | |
| 56 void | |
| 57 gaim_signal_connect(void *handle, GaimEvent event, | |
| 58 void *func, void *data) | |
| 59 { | |
| 60 GaimSignalCallback *call; | |
| 61 | |
| 62 g_return_if_fail(func != NULL); | |
| 63 | |
| 64 call = g_new0(GaimSignalCallback, 1); | |
| 65 call->handle = handle; | |
| 66 call->event = event; | |
| 67 call->function = func; | |
| 68 call->data = data; | |
| 69 | |
| 70 callbacks = g_list_append(callbacks, call); | |
| 71 | |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
72 gaim_debug(GAIM_DEBUG_INFO, "signals", |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
73 "Adding callback %d\n", g_list_length(callbacks)); |
| 5205 | 74 } |
| 75 | |
| 76 void | |
| 77 gaim_signal_disconnect(void *handle, GaimEvent event, void *func) | |
| 78 { | |
| 79 GList *c, *next_c; | |
| 80 GaimSignalCallback *g = NULL; | |
| 81 | |
| 82 g_return_if_fail(func != NULL); | |
| 83 | |
| 84 for (c = callbacks; c != NULL; c = next_c) { | |
| 85 next_c = c->next; | |
| 86 | |
| 87 g = (GaimSignalCallback *)c->data; | |
| 88 | |
| 89 if (handle == g->handle && func == g->function) { | |
| 90 callbacks = g_list_remove(callbacks, c->data); | |
| 91 g_free(g); | |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 void | |
| 97 gaim_signals_disconnect_by_handle(void *handle) | |
| 98 { | |
| 99 GList *c, *c_next; | |
| 100 GaimSignalCallback *g; | |
| 101 | |
| 102 g_return_if_fail(handle != NULL); | |
| 103 | |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
104 gaim_debug(GAIM_DEBUG_INFO, "signals", |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
105 "Disconnecting callbacks. %d to search.\n", |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
106 g_list_length(callbacks)); |
| 5205 | 107 |
| 108 for (c = callbacks; c != NULL; c = c_next) { | |
| 109 c_next = c->next; | |
| 110 g = (GaimSignalCallback *)c->data; | |
| 111 | |
| 112 if (g->handle == handle) { | |
| 113 callbacks = g_list_remove(callbacks, (gpointer)g); | |
| 114 | |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
115 gaim_debug(GAIM_DEBUG_INFO, "signals", |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
116 "Removing callback. %d remain.\n", |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
117 g_list_length(callbacks)); |
| 5205 | 118 } |
| 119 } | |
| 120 } | |
| 121 | |
| 122 void | |
| 123 gaim_signals_register_broadcast_func(GaimSignalBroadcastFunc func, | |
| 124 void *data) | |
| 125 { | |
| 126 GaimSignalBroadcaster *broadcaster; | |
| 127 | |
| 128 g_return_if_fail(func != NULL); | |
| 129 | |
| 130 broadcaster = g_new0(GaimSignalBroadcaster, 1); | |
| 131 | |
| 132 broadcaster->func = func; | |
| 133 broadcaster->data = data; | |
| 134 | |
| 135 broadcasters = g_list_append(broadcasters, broadcaster); | |
| 136 } | |
| 137 | |
| 138 void | |
| 139 gaim_signals_unregister_broadcast_func(GaimSignalBroadcastFunc func) | |
| 140 { | |
| 141 GList *l; | |
| 142 GaimSignalBroadcaster *broadcaster; | |
| 143 | |
| 144 g_return_if_fail(func != NULL); | |
| 145 | |
| 146 for (l = broadcasters; l != NULL; l = l->next) { | |
| 147 broadcaster = l->data; | |
| 148 | |
| 149 if (broadcaster->func == func) { | |
| 150 broadcasters = g_list_remove(broadcasters, broadcaster); | |
| 151 | |
| 152 g_free(broadcaster); | |
| 153 | |
| 154 break; | |
| 155 } | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 int | |
| 160 gaim_event_broadcast(GaimEvent event, ...) | |
| 161 { | |
| 162 GList *c; | |
| 163 GList *l; | |
| 164 GaimSignalCallback *g; | |
| 165 GaimSignalBroadcaster *broadcaster; | |
| 166 va_list arrg; | |
| 167 void *arg1 = NULL, *arg2 = NULL, *arg3 = NULL, *arg4 = NULL; | |
| 168 | |
| 169 for (c = callbacks; c != NULL; c = c->next) { | |
| 170 void (*cbfunc)(void *, ...); | |
| 171 | |
| 172 g = (GaimSignalCallback *)c->data; | |
| 173 | |
| 174 if (g->event == event && g->function != NULL) { | |
| 175 time_t time; | |
| 176 int id; | |
| 177 cbfunc = g->function; | |
| 178 | |
| 179 va_start(arrg, event); | |
| 180 | |
| 181 switch (event) { | |
| 182 /* no args */ | |
| 183 case event_blist_update: | |
| 184 case event_quit: | |
| 185 cbfunc(g->data); | |
| 186 break; | |
| 187 | |
| 188 /* one arg */ | |
| 189 case event_signon: | |
| 190 case event_signoff: | |
| 191 case event_new_conversation: | |
| 192 case event_del_conversation: | |
| 193 case event_error: | |
| 194 case event_connecting: | |
| 195 arg1 = va_arg(arrg, void *); | |
| 196 cbfunc(arg1, g->data); | |
| 197 break; | |
| 198 | |
| 199 /* two args */ | |
| 200 case event_buddy_signon: | |
| 201 case event_buddy_signoff: | |
| 202 case event_buddy_away: | |
| 203 case event_buddy_back: | |
| 204 case event_buddy_idle: | |
| 205 case event_buddy_unidle: | |
| 206 case event_set_info: | |
| 207 case event_draw_menu: | |
| 208 case event_got_typing: | |
| 209 arg1 = va_arg(arrg, void *); | |
| 210 arg2 = va_arg(arrg, void *); | |
| 211 cbfunc(arg1, arg2, g->data); | |
| 212 break; | |
| 213 | |
| 214 case event_chat_leave: | |
| 215 arg1 = va_arg(arrg, void*); | |
| 216 id = va_arg(arrg, int); | |
| 217 cbfunc(arg1, id, g->data); | |
| 218 break; | |
| 219 | |
| 220 /* three args */ | |
| 221 case event_im_send: | |
| 222 case event_im_displayed_sent: | |
| 223 case event_away: | |
| 224 arg1 = va_arg(arrg, void *); | |
| 225 arg2 = va_arg(arrg, void *); | |
| 226 arg3 = va_arg(arrg, void *); | |
| 227 cbfunc(arg1, arg2, arg3, g->data); | |
| 228 break; | |
| 229 | |
| 230 case event_chat_buddy_join: | |
| 231 case event_chat_buddy_leave: | |
| 232 case event_chat_send: | |
| 233 case event_chat_join: | |
| 234 arg1 = va_arg(arrg, void*); | |
| 235 id = va_arg(arrg, int); | |
| 236 arg3 = va_arg(arrg, void*); | |
| 237 cbfunc(arg1, id, arg3, g->data); | |
| 238 break; | |
| 239 | |
| 240 case event_warned: | |
| 241 arg1 = va_arg(arrg, void*); | |
| 242 arg2 = va_arg(arrg, void*); | |
| 243 id = va_arg(arrg, int); | |
| 244 cbfunc(arg1, arg2, id, g->data); | |
| 245 break; | |
| 246 | |
| 247 /* four args */ | |
| 248 case event_im_recv: | |
| 249 case event_chat_invited: | |
| 250 arg1 = va_arg(arrg, void *); | |
| 251 arg2 = va_arg(arrg, void *); | |
| 252 arg3 = va_arg(arrg, void *); | |
| 253 arg4 = va_arg(arrg, void *); | |
| 254 cbfunc(arg1, arg2, arg3, arg4, g->data); | |
| 255 break; | |
| 256 | |
| 257 case event_chat_recv: | |
| 258 case event_chat_send_invite: | |
| 259 arg1 = va_arg(arrg, void *); | |
| 260 id = va_arg(arrg, int); | |
| 261 | |
| 262 arg3 = va_arg(arrg, void *); | |
| 263 arg4 = va_arg(arrg, void *); | |
| 264 cbfunc(arg1, id, arg3, arg4, g->data); | |
| 265 break; | |
| 266 | |
| 267 /* five args */ | |
| 268 case event_im_displayed_rcvd: | |
| 269 arg1 = va_arg(arrg, void *); | |
| 270 arg2 = va_arg(arrg, void *); | |
| 271 arg3 = va_arg(arrg, void *); | |
| 272 arg4 = va_arg(arrg, void *); | |
| 273 time = va_arg(arrg, time_t); | |
| 274 cbfunc(arg1, arg2, arg3, arg4, time, g->data); | |
| 275 break; | |
| 276 | |
| 277 default: | |
|
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
278 gaim_debug(GAIM_DEBUG_WARNING, "events", |
|
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
279 "Unknown event %d\n", event); |
| 5205 | 280 break; |
| 281 } | |
| 282 | |
| 283 va_end(arrg); | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 for (l = broadcasters; l != NULL; l = l->next) { | |
| 288 broadcaster = l->data; | |
| 289 | |
| 290 va_start(arrg, event); | |
| 291 broadcaster->func(event, broadcaster->data, arrg); | |
| 292 } | |
| 293 | |
| 294 return 0; | |
| 295 } | |
| 296 | |
| 297 const char * | |
| 298 gaim_event_get_name(GaimEvent event) | |
| 299 { | |
| 300 static char buf[128]; | |
| 301 | |
| 302 switch (event) { | |
| 303 case event_signon: | |
| 304 snprintf(buf, sizeof(buf), "event_signon"); | |
| 305 break; | |
| 306 case event_signoff: | |
| 307 snprintf(buf, sizeof(buf), "event_signoff"); | |
| 308 break; | |
| 309 case event_away: | |
| 310 snprintf(buf, sizeof(buf), "event_away"); | |
| 311 break; | |
| 312 case event_back: | |
| 313 snprintf(buf, sizeof(buf), "event_back"); | |
| 314 break; | |
| 315 case event_im_recv: | |
| 316 snprintf(buf, sizeof(buf), "event_im_recv"); | |
| 317 break; | |
| 318 case event_im_send: | |
| 319 snprintf(buf, sizeof(buf), "event_im_send"); | |
| 320 break; | |
| 321 case event_buddy_signon: | |
| 322 snprintf(buf, sizeof(buf), "event_buddy_signon"); | |
| 323 break; | |
| 324 case event_buddy_signoff: | |
| 325 snprintf(buf, sizeof(buf), "event_buddy_signoff"); | |
| 326 break; | |
| 327 case event_buddy_away: | |
| 328 snprintf(buf, sizeof(buf), "event_buddy_away"); | |
| 329 break; | |
| 330 case event_buddy_back: | |
| 331 snprintf(buf, sizeof(buf), "event_buddy_back"); | |
| 332 break; | |
| 333 case event_buddy_idle: | |
| 334 snprintf(buf, sizeof(buf), "event_buddy_idle"); | |
| 335 break; | |
| 336 case event_buddy_unidle: | |
| 337 snprintf(buf, sizeof(buf), "event_buddy_unidle"); | |
| 338 break; | |
| 339 case event_blist_update: | |
| 340 snprintf(buf, sizeof(buf), "event_blist_update"); | |
| 341 break; | |
| 342 case event_chat_invited: | |
| 343 snprintf(buf, sizeof(buf), "event_chat_invited"); | |
| 344 break; | |
| 345 case event_chat_join: | |
| 346 snprintf(buf, sizeof(buf), "event_chat_join"); | |
| 347 break; | |
| 348 case event_chat_leave: | |
| 349 snprintf(buf, sizeof(buf), "event_chat_leave"); | |
| 350 break; | |
| 351 case event_chat_buddy_join: | |
| 352 snprintf(buf, sizeof(buf), "event_chat_buddy_join"); | |
| 353 break; | |
| 354 case event_chat_buddy_leave: | |
| 355 snprintf(buf, sizeof(buf), "event_chat_buddy_leave"); | |
| 356 break; | |
| 357 case event_chat_recv: | |
| 358 snprintf(buf, sizeof(buf), "event_chat_recv"); | |
| 359 break; | |
| 360 case event_chat_send: | |
| 361 snprintf(buf, sizeof(buf), "event_chat_send"); | |
| 362 break; | |
| 363 case event_warned: | |
| 364 snprintf(buf, sizeof(buf), "event_warned"); | |
| 365 break; | |
| 366 case event_error: | |
| 367 snprintf(buf, sizeof(buf), "event_error"); | |
| 368 break; | |
| 369 case event_quit: | |
| 370 snprintf(buf, sizeof(buf), "event_quit"); | |
| 371 break; | |
| 372 case event_new_conversation: | |
| 373 snprintf(buf, sizeof(buf), "event_new_conversation"); | |
| 374 break; | |
| 375 case event_set_info: | |
| 376 snprintf(buf, sizeof(buf), "event_set_info"); | |
| 377 break; | |
| 378 case event_draw_menu: | |
| 379 snprintf(buf, sizeof(buf), "event_draw_menu"); | |
| 380 break; | |
| 381 case event_im_displayed_sent: | |
| 382 snprintf(buf, sizeof(buf), "event_im_displayed_sent"); | |
| 383 break; | |
| 384 case event_im_displayed_rcvd: | |
| 385 snprintf(buf, sizeof(buf), "event_im_displayed_rcvd"); | |
| 386 break; | |
| 387 case event_chat_send_invite: | |
| 388 snprintf(buf, sizeof(buf), "event_chat_send_invite"); | |
| 389 break; | |
| 390 case event_got_typing: | |
| 391 snprintf(buf, sizeof(buf), "event_got_typing"); | |
| 392 break; | |
| 393 case event_del_conversation: | |
| 394 snprintf(buf, sizeof(buf), "event_del_conversation"); | |
| 395 break; | |
| 396 case event_connecting: | |
| 397 snprintf(buf, sizeof(buf), "event_connecting"); | |
| 398 break; | |
| 399 default: | |
| 400 snprintf(buf, sizeof(buf), "event_unknown"); | |
| 401 break; | |
| 402 } | |
| 403 | |
| 404 return buf; | |
| 405 } | |
| 406 | |
| 407 GList * | |
| 408 gaim_get_callbacks(void) | |
| 409 { | |
| 410 return callbacks; | |
| 411 } | |
| 412 |
