Mercurial > pidgin
annotate src/event.c @ 6024:a23a1d51f573
[gaim-migrate @ 6474]
Users can leave MSN chats for real this time. No, I mean it.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sat, 05 Jul 2003 06:53:15 +0000 |
parents | 3b6f48766acb |
children | 80e3641ee9eb |
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: | |
6020 | 203 case event_conversation_switch: |
5205 | 204 case event_error: |
205 case event_connecting: | |
206 arg1 = va_arg(arrg, void *); | |
207 cbfunc(arg1, g->data); | |
208 break; | |
209 | |
210 /* two args */ | |
211 case event_buddy_signon: | |
212 case event_buddy_signoff: | |
213 case event_buddy_away: | |
214 case event_buddy_back: | |
215 case event_buddy_idle: | |
216 case event_buddy_unidle: | |
217 case event_set_info: | |
218 case event_draw_menu: | |
219 case event_got_typing: | |
220 arg1 = va_arg(arrg, void *); | |
221 arg2 = va_arg(arrg, void *); | |
222 cbfunc(arg1, arg2, g->data); | |
223 break; | |
224 | |
225 case event_chat_leave: | |
226 arg1 = va_arg(arrg, void*); | |
227 id = va_arg(arrg, int); | |
228 cbfunc(arg1, id, g->data); | |
229 break; | |
230 | |
231 /* three args */ | |
232 case event_im_send: | |
233 case event_im_displayed_sent: | |
234 case event_away: | |
235 arg1 = va_arg(arrg, void *); | |
236 arg2 = va_arg(arrg, void *); | |
237 arg3 = va_arg(arrg, void *); | |
238 cbfunc(arg1, arg2, arg3, g->data); | |
239 break; | |
240 | |
241 case event_chat_buddy_join: | |
242 case event_chat_buddy_leave: | |
243 case event_chat_send: | |
244 case event_chat_join: | |
245 arg1 = va_arg(arrg, void*); | |
246 id = va_arg(arrg, int); | |
247 arg3 = va_arg(arrg, void*); | |
248 cbfunc(arg1, id, arg3, g->data); | |
249 break; | |
250 | |
251 case event_warned: | |
252 arg1 = va_arg(arrg, void*); | |
253 arg2 = va_arg(arrg, void*); | |
254 id = va_arg(arrg, int); | |
255 cbfunc(arg1, arg2, id, g->data); | |
256 break; | |
257 | |
258 /* four args */ | |
259 case event_im_recv: | |
260 case event_chat_invited: | |
261 arg1 = va_arg(arrg, void *); | |
262 arg2 = va_arg(arrg, void *); | |
263 arg3 = va_arg(arrg, void *); | |
264 arg4 = va_arg(arrg, void *); | |
265 cbfunc(arg1, arg2, arg3, arg4, g->data); | |
266 break; | |
267 | |
268 case event_chat_recv: | |
269 case event_chat_send_invite: | |
270 arg1 = va_arg(arrg, void *); | |
271 id = va_arg(arrg, int); | |
272 | |
273 arg3 = va_arg(arrg, void *); | |
274 arg4 = va_arg(arrg, void *); | |
275 cbfunc(arg1, id, arg3, arg4, g->data); | |
276 break; | |
277 | |
278 /* five args */ | |
279 case event_im_displayed_rcvd: | |
280 arg1 = va_arg(arrg, void *); | |
281 arg2 = va_arg(arrg, void *); | |
282 arg3 = va_arg(arrg, void *); | |
283 arg4 = va_arg(arrg, void *); | |
284 time = va_arg(arrg, time_t); | |
285 cbfunc(arg1, arg2, arg3, arg4, time, g->data); | |
286 break; | |
287 | |
288 default: | |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
289 gaim_debug(GAIM_DEBUG_WARNING, "events", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
290 "Unknown event %d\n", event); |
5205 | 291 break; |
292 } | |
293 | |
294 va_end(arrg); | |
295 } | |
296 } | |
297 | |
298 for (l = broadcasters; l != NULL; l = l->next) { | |
299 broadcaster = l->data; | |
300 | |
301 va_start(arrg, event); | |
302 broadcaster->func(event, broadcaster->data, arrg); | |
303 } | |
304 | |
305 return 0; | |
306 } | |
307 | |
308 const char * | |
309 gaim_event_get_name(GaimEvent event) | |
310 { | |
311 static char buf[128]; | |
312 | |
313 switch (event) { | |
314 case event_signon: | |
315 snprintf(buf, sizeof(buf), "event_signon"); | |
316 break; | |
317 case event_signoff: | |
318 snprintf(buf, sizeof(buf), "event_signoff"); | |
319 break; | |
320 case event_away: | |
321 snprintf(buf, sizeof(buf), "event_away"); | |
322 break; | |
323 case event_back: | |
324 snprintf(buf, sizeof(buf), "event_back"); | |
325 break; | |
326 case event_im_recv: | |
327 snprintf(buf, sizeof(buf), "event_im_recv"); | |
328 break; | |
329 case event_im_send: | |
330 snprintf(buf, sizeof(buf), "event_im_send"); | |
331 break; | |
332 case event_buddy_signon: | |
333 snprintf(buf, sizeof(buf), "event_buddy_signon"); | |
334 break; | |
335 case event_buddy_signoff: | |
336 snprintf(buf, sizeof(buf), "event_buddy_signoff"); | |
337 break; | |
338 case event_buddy_away: | |
339 snprintf(buf, sizeof(buf), "event_buddy_away"); | |
340 break; | |
341 case event_buddy_back: | |
342 snprintf(buf, sizeof(buf), "event_buddy_back"); | |
343 break; | |
344 case event_buddy_idle: | |
345 snprintf(buf, sizeof(buf), "event_buddy_idle"); | |
346 break; | |
347 case event_buddy_unidle: | |
348 snprintf(buf, sizeof(buf), "event_buddy_unidle"); | |
349 break; | |
350 case event_blist_update: | |
351 snprintf(buf, sizeof(buf), "event_blist_update"); | |
352 break; | |
353 case event_chat_invited: | |
354 snprintf(buf, sizeof(buf), "event_chat_invited"); | |
355 break; | |
356 case event_chat_join: | |
357 snprintf(buf, sizeof(buf), "event_chat_join"); | |
358 break; | |
359 case event_chat_leave: | |
360 snprintf(buf, sizeof(buf), "event_chat_leave"); | |
361 break; | |
362 case event_chat_buddy_join: | |
363 snprintf(buf, sizeof(buf), "event_chat_buddy_join"); | |
364 break; | |
365 case event_chat_buddy_leave: | |
366 snprintf(buf, sizeof(buf), "event_chat_buddy_leave"); | |
367 break; | |
368 case event_chat_recv: | |
369 snprintf(buf, sizeof(buf), "event_chat_recv"); | |
370 break; | |
371 case event_chat_send: | |
372 snprintf(buf, sizeof(buf), "event_chat_send"); | |
373 break; | |
374 case event_warned: | |
375 snprintf(buf, sizeof(buf), "event_warned"); | |
376 break; | |
377 case event_error: | |
378 snprintf(buf, sizeof(buf), "event_error"); | |
379 break; | |
380 case event_quit: | |
381 snprintf(buf, sizeof(buf), "event_quit"); | |
382 break; | |
383 case event_new_conversation: | |
384 snprintf(buf, sizeof(buf), "event_new_conversation"); | |
385 break; | |
386 case event_set_info: | |
387 snprintf(buf, sizeof(buf), "event_set_info"); | |
388 break; | |
389 case event_draw_menu: | |
390 snprintf(buf, sizeof(buf), "event_draw_menu"); | |
391 break; | |
392 case event_im_displayed_sent: | |
393 snprintf(buf, sizeof(buf), "event_im_displayed_sent"); | |
394 break; | |
395 case event_im_displayed_rcvd: | |
396 snprintf(buf, sizeof(buf), "event_im_displayed_rcvd"); | |
397 break; | |
398 case event_chat_send_invite: | |
399 snprintf(buf, sizeof(buf), "event_chat_send_invite"); | |
400 break; | |
401 case event_got_typing: | |
402 snprintf(buf, sizeof(buf), "event_got_typing"); | |
403 break; | |
404 case event_del_conversation: | |
405 snprintf(buf, sizeof(buf), "event_del_conversation"); | |
406 break; | |
407 case event_connecting: | |
408 snprintf(buf, sizeof(buf), "event_connecting"); | |
409 break; | |
410 default: | |
411 snprintf(buf, sizeof(buf), "event_unknown"); | |
412 break; | |
413 } | |
414 | |
415 return buf; | |
416 } | |
417 | |
418 |