Mercurial > pidgin
annotate src/event.c @ 6240:ac191233b816
[gaim-migrate @ 6734]
Updated the file transfer API to resemble that of the rest of Gaim's API.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sun, 20 Jul 2003 03:30:43 +0000 |
parents | 5823ed7e2583 |
children |
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 | |
5224
5160333a80df
[gaim-migrate @ 5594]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
31 #ifdef _WIN32 |
5160333a80df
[gaim-migrate @ 5594]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
32 #include "win32dep.h" |
5160333a80df
[gaim-migrate @ 5594]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
33 #endif |
5160333a80df
[gaim-migrate @ 5594]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
5211
diff
changeset
|
34 |
5205 | 35 /** |
36 * A signal callback. | |
37 */ | |
38 typedef struct | |
39 { | |
40 void *handle; /**< The plugin module handle. */ | |
41 GaimEvent event; /**< The event type. */ | |
42 void *function; /**< The function to call. */ | |
43 void *data; /**< The data to pass to the function. */ | |
44 | |
45 } GaimSignalCallback; | |
46 | |
47 /** | |
48 * A broadcast function. | |
49 */ | |
50 typedef struct | |
51 { | |
52 GaimSignalBroadcastFunc func; | |
53 void *data; | |
54 | |
55 } GaimSignalBroadcaster; | |
56 | |
57 static GList *callbacks = NULL; | |
58 static GList *broadcasters = NULL; | |
59 | |
60 void | |
61 gaim_signal_connect(void *handle, GaimEvent event, | |
62 void *func, void *data) | |
63 { | |
64 GaimSignalCallback *call; | |
65 | |
66 g_return_if_fail(func != NULL); | |
67 | |
68 call = g_new0(GaimSignalCallback, 1); | |
69 call->handle = handle; | |
70 call->event = event; | |
71 call->function = func; | |
72 call->data = data; | |
73 | |
74 callbacks = g_list_append(callbacks, call); | |
75 | |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
76 gaim_debug(GAIM_DEBUG_INFO, "signals", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
77 "Adding callback %d\n", g_list_length(callbacks)); |
5205 | 78 } |
79 | |
80 void | |
81 gaim_signal_disconnect(void *handle, GaimEvent event, void *func) | |
82 { | |
83 GList *c, *next_c; | |
84 GaimSignalCallback *g = NULL; | |
85 | |
86 g_return_if_fail(func != NULL); | |
87 | |
88 for (c = callbacks; c != NULL; c = next_c) { | |
89 next_c = c->next; | |
90 g = (GaimSignalCallback *)c->data; | |
91 | |
92 if (handle == g->handle && func == g->function) { | |
93 callbacks = g_list_remove(callbacks, c->data); | |
94 g_free(g); | |
95 } | |
96 } | |
97 } | |
98 | |
99 void | |
100 gaim_signals_disconnect_by_handle(void *handle) | |
101 { | |
102 GList *c, *c_next; | |
103 GaimSignalCallback *g; | |
104 | |
105 g_return_if_fail(handle != NULL); | |
106 | |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
107 gaim_debug(GAIM_DEBUG_INFO, "signals", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
108 "Disconnecting callbacks. %d to search.\n", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
109 g_list_length(callbacks)); |
5205 | 110 |
111 for (c = callbacks; c != NULL; c = c_next) { | |
112 c_next = c->next; | |
113 g = (GaimSignalCallback *)c->data; | |
114 | |
115 if (g->handle == handle) { | |
6040 | 116 callbacks = g_list_remove(callbacks, c->data); |
117 g_free(g); | |
5205 | 118 |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
119 gaim_debug(GAIM_DEBUG_INFO, "signals", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
120 "Removing callback. %d remain.\n", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
121 g_list_length(callbacks)); |
5205 | 122 } |
123 } | |
124 } | |
125 | |
126 void | |
127 gaim_signals_register_broadcast_func(GaimSignalBroadcastFunc func, | |
128 void *data) | |
129 { | |
130 GaimSignalBroadcaster *broadcaster; | |
131 | |
132 g_return_if_fail(func != NULL); | |
133 | |
134 broadcaster = g_new0(GaimSignalBroadcaster, 1); | |
135 | |
136 broadcaster->func = func; | |
137 broadcaster->data = data; | |
138 | |
139 broadcasters = g_list_append(broadcasters, broadcaster); | |
140 } | |
141 | |
142 void | |
143 gaim_signals_unregister_broadcast_func(GaimSignalBroadcastFunc func) | |
144 { | |
145 GList *l; | |
146 GaimSignalBroadcaster *broadcaster; | |
147 | |
148 g_return_if_fail(func != NULL); | |
149 | |
150 for (l = broadcasters; l != NULL; l = l->next) { | |
151 broadcaster = l->data; | |
152 | |
153 if (broadcaster->func == func) { | |
154 broadcasters = g_list_remove(broadcasters, broadcaster); | |
155 | |
156 g_free(broadcaster); | |
157 | |
158 break; | |
159 } | |
160 } | |
161 } | |
162 | |
5230
779ffffb584a
[gaim-migrate @ 5600]
Christian Hammond <chipx86@chipx86.com>
parents:
5224
diff
changeset
|
163 GList * |
779ffffb584a
[gaim-migrate @ 5600]
Christian Hammond <chipx86@chipx86.com>
parents:
5224
diff
changeset
|
164 gaim_signals_get_callbacks(void) |
779ffffb584a
[gaim-migrate @ 5600]
Christian Hammond <chipx86@chipx86.com>
parents:
5224
diff
changeset
|
165 { |
779ffffb584a
[gaim-migrate @ 5600]
Christian Hammond <chipx86@chipx86.com>
parents:
5224
diff
changeset
|
166 return callbacks; |
779ffffb584a
[gaim-migrate @ 5600]
Christian Hammond <chipx86@chipx86.com>
parents:
5224
diff
changeset
|
167 } |
779ffffb584a
[gaim-migrate @ 5600]
Christian Hammond <chipx86@chipx86.com>
parents:
5224
diff
changeset
|
168 |
5205 | 169 int |
170 gaim_event_broadcast(GaimEvent event, ...) | |
171 { | |
172 GList *c; | |
173 GList *l; | |
174 GaimSignalCallback *g; | |
175 GaimSignalBroadcaster *broadcaster; | |
6099 | 176 int retval = 0; |
5205 | 177 va_list arrg; |
178 void *arg1 = NULL, *arg2 = NULL, *arg3 = NULL, *arg4 = NULL; | |
179 | |
180 for (c = callbacks; c != NULL; c = c->next) { | |
181 void (*cbfunc)(void *, ...); | |
182 | |
183 g = (GaimSignalCallback *)c->data; | |
184 | |
185 if (g->event == event && g->function != NULL) { | |
186 time_t time; | |
187 int id; | |
188 cbfunc = g->function; | |
189 | |
190 va_start(arrg, event); | |
191 | |
192 switch (event) { | |
193 /* no args */ | |
194 case event_blist_update: | |
195 case event_quit: | |
196 cbfunc(g->data); | |
197 break; | |
198 | |
199 /* one arg */ | |
200 case event_signon: | |
201 case event_signoff: | |
202 case event_new_conversation: | |
203 case event_del_conversation: | |
6020 | 204 case event_conversation_switch: |
5205 | 205 case event_error: |
206 case event_connecting: | |
207 arg1 = va_arg(arrg, void *); | |
208 cbfunc(arg1, g->data); | |
209 break; | |
210 | |
211 /* two args */ | |
212 case event_buddy_signon: | |
213 case event_buddy_signoff: | |
214 case event_buddy_away: | |
215 case event_buddy_back: | |
216 case event_buddy_idle: | |
217 case event_buddy_unidle: | |
218 case event_set_info: | |
219 case event_draw_menu: | |
220 case event_got_typing: | |
221 arg1 = va_arg(arrg, void *); | |
222 arg2 = va_arg(arrg, void *); | |
223 cbfunc(arg1, arg2, g->data); | |
224 break; | |
225 | |
226 case event_chat_leave: | |
227 arg1 = va_arg(arrg, void*); | |
228 id = va_arg(arrg, int); | |
229 cbfunc(arg1, id, g->data); | |
230 break; | |
231 | |
232 /* three args */ | |
233 case event_im_send: | |
234 case event_im_displayed_sent: | |
235 case event_away: | |
236 arg1 = va_arg(arrg, void *); | |
237 arg2 = va_arg(arrg, void *); | |
238 arg3 = va_arg(arrg, void *); | |
239 cbfunc(arg1, arg2, arg3, g->data); | |
240 break; | |
241 | |
242 case event_chat_buddy_join: | |
243 case event_chat_buddy_leave: | |
244 case event_chat_send: | |
245 case event_chat_join: | |
246 arg1 = va_arg(arrg, void*); | |
247 id = va_arg(arrg, int); | |
248 arg3 = va_arg(arrg, void*); | |
249 cbfunc(arg1, id, arg3, g->data); | |
250 break; | |
251 | |
252 case event_warned: | |
253 arg1 = va_arg(arrg, void*); | |
254 arg2 = va_arg(arrg, void*); | |
255 id = va_arg(arrg, int); | |
256 cbfunc(arg1, arg2, id, g->data); | |
257 break; | |
258 | |
259 /* four args */ | |
260 case event_im_recv: | |
261 case event_chat_invited: | |
262 arg1 = va_arg(arrg, void *); | |
263 arg2 = va_arg(arrg, void *); | |
264 arg3 = va_arg(arrg, void *); | |
265 arg4 = va_arg(arrg, void *); | |
266 cbfunc(arg1, arg2, arg3, arg4, g->data); | |
267 break; | |
268 | |
269 case event_chat_recv: | |
270 case event_chat_send_invite: | |
271 arg1 = va_arg(arrg, void *); | |
272 id = va_arg(arrg, int); | |
273 | |
274 arg3 = va_arg(arrg, void *); | |
275 arg4 = va_arg(arrg, void *); | |
276 cbfunc(arg1, id, arg3, arg4, g->data); | |
277 break; | |
278 | |
279 /* five args */ | |
280 case event_im_displayed_rcvd: | |
281 arg1 = va_arg(arrg, void *); | |
282 arg2 = va_arg(arrg, void *); | |
283 arg3 = va_arg(arrg, void *); | |
284 arg4 = va_arg(arrg, void *); | |
285 time = va_arg(arrg, time_t); | |
286 cbfunc(arg1, arg2, arg3, arg4, time, g->data); | |
287 break; | |
288 | |
289 default: | |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
290 gaim_debug(GAIM_DEBUG_WARNING, "events", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
291 "Unknown event %d\n", event); |
5205 | 292 break; |
293 } | |
294 | |
295 va_end(arrg); | |
296 } | |
297 } | |
298 | |
6099 | 299 for (l = broadcasters; l != NULL && retval == 0; l = l->next) { |
5205 | 300 broadcaster = l->data; |
301 | |
302 va_start(arrg, event); | |
6099 | 303 retval = broadcaster->func(event, broadcaster->data, arrg); |
5205 | 304 } |
305 | |
6099 | 306 return retval; |
5205 | 307 } |
308 | |
309 const char * | |
310 gaim_event_get_name(GaimEvent event) | |
311 { | |
312 static char buf[128]; | |
313 | |
314 switch (event) { | |
315 case event_signon: | |
316 snprintf(buf, sizeof(buf), "event_signon"); | |
317 break; | |
318 case event_signoff: | |
319 snprintf(buf, sizeof(buf), "event_signoff"); | |
320 break; | |
321 case event_away: | |
322 snprintf(buf, sizeof(buf), "event_away"); | |
323 break; | |
324 case event_back: | |
325 snprintf(buf, sizeof(buf), "event_back"); | |
326 break; | |
327 case event_im_recv: | |
328 snprintf(buf, sizeof(buf), "event_im_recv"); | |
329 break; | |
330 case event_im_send: | |
331 snprintf(buf, sizeof(buf), "event_im_send"); | |
332 break; | |
333 case event_buddy_signon: | |
334 snprintf(buf, sizeof(buf), "event_buddy_signon"); | |
335 break; | |
336 case event_buddy_signoff: | |
337 snprintf(buf, sizeof(buf), "event_buddy_signoff"); | |
338 break; | |
339 case event_buddy_away: | |
340 snprintf(buf, sizeof(buf), "event_buddy_away"); | |
341 break; | |
342 case event_buddy_back: | |
343 snprintf(buf, sizeof(buf), "event_buddy_back"); | |
344 break; | |
345 case event_buddy_idle: | |
346 snprintf(buf, sizeof(buf), "event_buddy_idle"); | |
347 break; | |
348 case event_buddy_unidle: | |
349 snprintf(buf, sizeof(buf), "event_buddy_unidle"); | |
350 break; | |
351 case event_blist_update: | |
352 snprintf(buf, sizeof(buf), "event_blist_update"); | |
353 break; | |
354 case event_chat_invited: | |
355 snprintf(buf, sizeof(buf), "event_chat_invited"); | |
356 break; | |
357 case event_chat_join: | |
358 snprintf(buf, sizeof(buf), "event_chat_join"); | |
359 break; | |
360 case event_chat_leave: | |
361 snprintf(buf, sizeof(buf), "event_chat_leave"); | |
362 break; | |
363 case event_chat_buddy_join: | |
364 snprintf(buf, sizeof(buf), "event_chat_buddy_join"); | |
365 break; | |
366 case event_chat_buddy_leave: | |
367 snprintf(buf, sizeof(buf), "event_chat_buddy_leave"); | |
368 break; | |
369 case event_chat_recv: | |
370 snprintf(buf, sizeof(buf), "event_chat_recv"); | |
371 break; | |
372 case event_chat_send: | |
373 snprintf(buf, sizeof(buf), "event_chat_send"); | |
374 break; | |
375 case event_warned: | |
376 snprintf(buf, sizeof(buf), "event_warned"); | |
377 break; | |
378 case event_error: | |
379 snprintf(buf, sizeof(buf), "event_error"); | |
380 break; | |
381 case event_quit: | |
382 snprintf(buf, sizeof(buf), "event_quit"); | |
383 break; | |
384 case event_new_conversation: | |
385 snprintf(buf, sizeof(buf), "event_new_conversation"); | |
386 break; | |
387 case event_set_info: | |
388 snprintf(buf, sizeof(buf), "event_set_info"); | |
389 break; | |
390 case event_draw_menu: | |
391 snprintf(buf, sizeof(buf), "event_draw_menu"); | |
392 break; | |
393 case event_im_displayed_sent: | |
394 snprintf(buf, sizeof(buf), "event_im_displayed_sent"); | |
395 break; | |
396 case event_im_displayed_rcvd: | |
397 snprintf(buf, sizeof(buf), "event_im_displayed_rcvd"); | |
398 break; | |
399 case event_chat_send_invite: | |
400 snprintf(buf, sizeof(buf), "event_chat_send_invite"); | |
401 break; | |
402 case event_got_typing: | |
403 snprintf(buf, sizeof(buf), "event_got_typing"); | |
404 break; | |
405 case event_del_conversation: | |
406 snprintf(buf, sizeof(buf), "event_del_conversation"); | |
407 break; | |
408 case event_connecting: | |
409 snprintf(buf, sizeof(buf), "event_connecting"); | |
410 break; | |
6100 | 411 case event_conversation_switch: |
412 snprintf(buf, sizeof(buf), "event_conversation_switch"); | |
413 break; | |
5205 | 414 default: |
415 snprintf(buf, sizeof(buf), "event_unknown"); | |
416 break; | |
417 } | |
418 | |
419 return buf; | |
420 } | |
421 | |
422 |