Mercurial > pidgin.yaz
annotate src/event.c @ 5849:97d1ad7a50cc
[gaim-migrate @ 6280]
Users can now set their Trepia buddy icon.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Fri, 13 Jun 2003 09:11:24 +0000 |
parents | 779ffffb584a |
children | 3b6f48766acb |
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 | |
91 g = (GaimSignalCallback *)c->data; | |
92 | |
93 if (handle == g->handle && func == g->function) { | |
94 callbacks = g_list_remove(callbacks, c->data); | |
95 g_free(g); | |
96 } | |
97 } | |
98 } | |
99 | |
100 void | |
101 gaim_signals_disconnect_by_handle(void *handle) | |
102 { | |
103 GList *c, *c_next; | |
104 GaimSignalCallback *g; | |
105 | |
106 g_return_if_fail(handle != NULL); | |
107 | |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
108 gaim_debug(GAIM_DEBUG_INFO, "signals", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
109 "Disconnecting callbacks. %d to search.\n", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
110 g_list_length(callbacks)); |
5205 | 111 |
112 for (c = callbacks; c != NULL; c = c_next) { | |
113 c_next = c->next; | |
114 g = (GaimSignalCallback *)c->data; | |
115 | |
116 if (g->handle == handle) { | |
117 callbacks = g_list_remove(callbacks, (gpointer)g); | |
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; | |
176 va_list arrg; | |
177 void *arg1 = NULL, *arg2 = NULL, *arg3 = NULL, *arg4 = NULL; | |
178 | |
179 for (c = callbacks; c != NULL; c = c->next) { | |
180 void (*cbfunc)(void *, ...); | |
181 | |
182 g = (GaimSignalCallback *)c->data; | |
183 | |
184 if (g->event == event && g->function != NULL) { | |
185 time_t time; | |
186 int id; | |
187 cbfunc = g->function; | |
188 | |
189 va_start(arrg, event); | |
190 | |
191 switch (event) { | |
192 /* no args */ | |
193 case event_blist_update: | |
194 case event_quit: | |
195 cbfunc(g->data); | |
196 break; | |
197 | |
198 /* one arg */ | |
199 case event_signon: | |
200 case event_signoff: | |
201 case event_new_conversation: | |
202 case event_del_conversation: | |
203 case event_error: | |
204 case event_connecting: | |
205 arg1 = va_arg(arrg, void *); | |
206 cbfunc(arg1, g->data); | |
207 break; | |
208 | |
209 /* two args */ | |
210 case event_buddy_signon: | |
211 case event_buddy_signoff: | |
212 case event_buddy_away: | |
213 case event_buddy_back: | |
214 case event_buddy_idle: | |
215 case event_buddy_unidle: | |
216 case event_set_info: | |
217 case event_draw_menu: | |
218 case event_got_typing: | |
219 arg1 = va_arg(arrg, void *); | |
220 arg2 = va_arg(arrg, void *); | |
221 cbfunc(arg1, arg2, g->data); | |
222 break; | |
223 | |
224 case event_chat_leave: | |
225 arg1 = va_arg(arrg, void*); | |
226 id = va_arg(arrg, int); | |
227 cbfunc(arg1, id, g->data); | |
228 break; | |
229 | |
230 /* three args */ | |
231 case event_im_send: | |
232 case event_im_displayed_sent: | |
233 case event_away: | |
234 arg1 = va_arg(arrg, void *); | |
235 arg2 = va_arg(arrg, void *); | |
236 arg3 = va_arg(arrg, void *); | |
237 cbfunc(arg1, arg2, arg3, g->data); | |
238 break; | |
239 | |
240 case event_chat_buddy_join: | |
241 case event_chat_buddy_leave: | |
242 case event_chat_send: | |
243 case event_chat_join: | |
244 arg1 = va_arg(arrg, void*); | |
245 id = va_arg(arrg, int); | |
246 arg3 = va_arg(arrg, void*); | |
247 cbfunc(arg1, id, arg3, g->data); | |
248 break; | |
249 | |
250 case event_warned: | |
251 arg1 = va_arg(arrg, void*); | |
252 arg2 = va_arg(arrg, void*); | |
253 id = va_arg(arrg, int); | |
254 cbfunc(arg1, arg2, id, g->data); | |
255 break; | |
256 | |
257 /* four args */ | |
258 case event_im_recv: | |
259 case event_chat_invited: | |
260 arg1 = va_arg(arrg, void *); | |
261 arg2 = va_arg(arrg, void *); | |
262 arg3 = va_arg(arrg, void *); | |
263 arg4 = va_arg(arrg, void *); | |
264 cbfunc(arg1, arg2, arg3, arg4, g->data); | |
265 break; | |
266 | |
267 case event_chat_recv: | |
268 case event_chat_send_invite: | |
269 arg1 = va_arg(arrg, void *); | |
270 id = va_arg(arrg, int); | |
271 | |
272 arg3 = va_arg(arrg, void *); | |
273 arg4 = va_arg(arrg, void *); | |
274 cbfunc(arg1, id, arg3, arg4, g->data); | |
275 break; | |
276 | |
277 /* five args */ | |
278 case event_im_displayed_rcvd: | |
279 arg1 = va_arg(arrg, void *); | |
280 arg2 = va_arg(arrg, void *); | |
281 arg3 = va_arg(arrg, void *); | |
282 arg4 = va_arg(arrg, void *); | |
283 time = va_arg(arrg, time_t); | |
284 cbfunc(arg1, arg2, arg3, arg4, time, g->data); | |
285 break; | |
286 | |
287 default: | |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
288 gaim_debug(GAIM_DEBUG_WARNING, "events", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
289 "Unknown event %d\n", event); |
5205 | 290 break; |
291 } | |
292 | |
293 va_end(arrg); | |
294 } | |
295 } | |
296 | |
297 for (l = broadcasters; l != NULL; l = l->next) { | |
298 broadcaster = l->data; | |
299 | |
300 va_start(arrg, event); | |
301 broadcaster->func(event, broadcaster->data, arrg); | |
302 } | |
303 | |
304 return 0; | |
305 } | |
306 | |
307 const char * | |
308 gaim_event_get_name(GaimEvent event) | |
309 { | |
310 static char buf[128]; | |
311 | |
312 switch (event) { | |
313 case event_signon: | |
314 snprintf(buf, sizeof(buf), "event_signon"); | |
315 break; | |
316 case event_signoff: | |
317 snprintf(buf, sizeof(buf), "event_signoff"); | |
318 break; | |
319 case event_away: | |
320 snprintf(buf, sizeof(buf), "event_away"); | |
321 break; | |
322 case event_back: | |
323 snprintf(buf, sizeof(buf), "event_back"); | |
324 break; | |
325 case event_im_recv: | |
326 snprintf(buf, sizeof(buf), "event_im_recv"); | |
327 break; | |
328 case event_im_send: | |
329 snprintf(buf, sizeof(buf), "event_im_send"); | |
330 break; | |
331 case event_buddy_signon: | |
332 snprintf(buf, sizeof(buf), "event_buddy_signon"); | |
333 break; | |
334 case event_buddy_signoff: | |
335 snprintf(buf, sizeof(buf), "event_buddy_signoff"); | |
336 break; | |
337 case event_buddy_away: | |
338 snprintf(buf, sizeof(buf), "event_buddy_away"); | |
339 break; | |
340 case event_buddy_back: | |
341 snprintf(buf, sizeof(buf), "event_buddy_back"); | |
342 break; | |
343 case event_buddy_idle: | |
344 snprintf(buf, sizeof(buf), "event_buddy_idle"); | |
345 break; | |
346 case event_buddy_unidle: | |
347 snprintf(buf, sizeof(buf), "event_buddy_unidle"); | |
348 break; | |
349 case event_blist_update: | |
350 snprintf(buf, sizeof(buf), "event_blist_update"); | |
351 break; | |
352 case event_chat_invited: | |
353 snprintf(buf, sizeof(buf), "event_chat_invited"); | |
354 break; | |
355 case event_chat_join: | |
356 snprintf(buf, sizeof(buf), "event_chat_join"); | |
357 break; | |
358 case event_chat_leave: | |
359 snprintf(buf, sizeof(buf), "event_chat_leave"); | |
360 break; | |
361 case event_chat_buddy_join: | |
362 snprintf(buf, sizeof(buf), "event_chat_buddy_join"); | |
363 break; | |
364 case event_chat_buddy_leave: | |
365 snprintf(buf, sizeof(buf), "event_chat_buddy_leave"); | |
366 break; | |
367 case event_chat_recv: | |
368 snprintf(buf, sizeof(buf), "event_chat_recv"); | |
369 break; | |
370 case event_chat_send: | |
371 snprintf(buf, sizeof(buf), "event_chat_send"); | |
372 break; | |
373 case event_warned: | |
374 snprintf(buf, sizeof(buf), "event_warned"); | |
375 break; | |
376 case event_error: | |
377 snprintf(buf, sizeof(buf), "event_error"); | |
378 break; | |
379 case event_quit: | |
380 snprintf(buf, sizeof(buf), "event_quit"); | |
381 break; | |
382 case event_new_conversation: | |
383 snprintf(buf, sizeof(buf), "event_new_conversation"); | |
384 break; | |
385 case event_set_info: | |
386 snprintf(buf, sizeof(buf), "event_set_info"); | |
387 break; | |
388 case event_draw_menu: | |
389 snprintf(buf, sizeof(buf), "event_draw_menu"); | |
390 break; | |
391 case event_im_displayed_sent: | |
392 snprintf(buf, sizeof(buf), "event_im_displayed_sent"); | |
393 break; | |
394 case event_im_displayed_rcvd: | |
395 snprintf(buf, sizeof(buf), "event_im_displayed_rcvd"); | |
396 break; | |
397 case event_chat_send_invite: | |
398 snprintf(buf, sizeof(buf), "event_chat_send_invite"); | |
399 break; | |
400 case event_got_typing: | |
401 snprintf(buf, sizeof(buf), "event_got_typing"); | |
402 break; | |
403 case event_del_conversation: | |
404 snprintf(buf, sizeof(buf), "event_del_conversation"); | |
405 break; | |
406 case event_connecting: | |
407 snprintf(buf, sizeof(buf), "event_connecting"); | |
408 break; | |
409 default: | |
410 snprintf(buf, sizeof(buf), "event_unknown"); | |
411 break; | |
412 } | |
413 | |
414 return buf; | |
415 } | |
416 | |
417 |