Mercurial > pidgin
annotate src/event.c @ 5228:1a53330dfd34
[gaim-migrate @ 5598]
People have been talking about doing it, but nobody has done it. Since I
want to clean up some header files today:
list.[ch] -> blist.[ch]
gtklist.h -> gtkblist.h
buddy.c -> gtkblist.c
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sat, 26 Apr 2003 17:56:23 +0000 |
parents | 5160333a80df |
children | 779ffffb584a |
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 | |
163 int | |
164 gaim_event_broadcast(GaimEvent event, ...) | |
165 { | |
166 GList *c; | |
167 GList *l; | |
168 GaimSignalCallback *g; | |
169 GaimSignalBroadcaster *broadcaster; | |
170 va_list arrg; | |
171 void *arg1 = NULL, *arg2 = NULL, *arg3 = NULL, *arg4 = NULL; | |
172 | |
173 for (c = callbacks; c != NULL; c = c->next) { | |
174 void (*cbfunc)(void *, ...); | |
175 | |
176 g = (GaimSignalCallback *)c->data; | |
177 | |
178 if (g->event == event && g->function != NULL) { | |
179 time_t time; | |
180 int id; | |
181 cbfunc = g->function; | |
182 | |
183 va_start(arrg, event); | |
184 | |
185 switch (event) { | |
186 /* no args */ | |
187 case event_blist_update: | |
188 case event_quit: | |
189 cbfunc(g->data); | |
190 break; | |
191 | |
192 /* one arg */ | |
193 case event_signon: | |
194 case event_signoff: | |
195 case event_new_conversation: | |
196 case event_del_conversation: | |
197 case event_error: | |
198 case event_connecting: | |
199 arg1 = va_arg(arrg, void *); | |
200 cbfunc(arg1, g->data); | |
201 break; | |
202 | |
203 /* two args */ | |
204 case event_buddy_signon: | |
205 case event_buddy_signoff: | |
206 case event_buddy_away: | |
207 case event_buddy_back: | |
208 case event_buddy_idle: | |
209 case event_buddy_unidle: | |
210 case event_set_info: | |
211 case event_draw_menu: | |
212 case event_got_typing: | |
213 arg1 = va_arg(arrg, void *); | |
214 arg2 = va_arg(arrg, void *); | |
215 cbfunc(arg1, arg2, g->data); | |
216 break; | |
217 | |
218 case event_chat_leave: | |
219 arg1 = va_arg(arrg, void*); | |
220 id = va_arg(arrg, int); | |
221 cbfunc(arg1, id, g->data); | |
222 break; | |
223 | |
224 /* three args */ | |
225 case event_im_send: | |
226 case event_im_displayed_sent: | |
227 case event_away: | |
228 arg1 = va_arg(arrg, void *); | |
229 arg2 = va_arg(arrg, void *); | |
230 arg3 = va_arg(arrg, void *); | |
231 cbfunc(arg1, arg2, arg3, g->data); | |
232 break; | |
233 | |
234 case event_chat_buddy_join: | |
235 case event_chat_buddy_leave: | |
236 case event_chat_send: | |
237 case event_chat_join: | |
238 arg1 = va_arg(arrg, void*); | |
239 id = va_arg(arrg, int); | |
240 arg3 = va_arg(arrg, void*); | |
241 cbfunc(arg1, id, arg3, g->data); | |
242 break; | |
243 | |
244 case event_warned: | |
245 arg1 = va_arg(arrg, void*); | |
246 arg2 = va_arg(arrg, void*); | |
247 id = va_arg(arrg, int); | |
248 cbfunc(arg1, arg2, id, g->data); | |
249 break; | |
250 | |
251 /* four args */ | |
252 case event_im_recv: | |
253 case event_chat_invited: | |
254 arg1 = va_arg(arrg, void *); | |
255 arg2 = va_arg(arrg, void *); | |
256 arg3 = va_arg(arrg, void *); | |
257 arg4 = va_arg(arrg, void *); | |
258 cbfunc(arg1, arg2, arg3, arg4, g->data); | |
259 break; | |
260 | |
261 case event_chat_recv: | |
262 case event_chat_send_invite: | |
263 arg1 = va_arg(arrg, void *); | |
264 id = va_arg(arrg, int); | |
265 | |
266 arg3 = va_arg(arrg, void *); | |
267 arg4 = va_arg(arrg, void *); | |
268 cbfunc(arg1, id, arg3, arg4, g->data); | |
269 break; | |
270 | |
271 /* five args */ | |
272 case event_im_displayed_rcvd: | |
273 arg1 = va_arg(arrg, void *); | |
274 arg2 = va_arg(arrg, void *); | |
275 arg3 = va_arg(arrg, void *); | |
276 arg4 = va_arg(arrg, void *); | |
277 time = va_arg(arrg, time_t); | |
278 cbfunc(arg1, arg2, arg3, arg4, time, g->data); | |
279 break; | |
280 | |
281 default: | |
5211
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
282 gaim_debug(GAIM_DEBUG_WARNING, "events", |
0241d6b6702d
[gaim-migrate @ 5581]
Christian Hammond <chipx86@chipx86.com>
parents:
5205
diff
changeset
|
283 "Unknown event %d\n", event); |
5205 | 284 break; |
285 } | |
286 | |
287 va_end(arrg); | |
288 } | |
289 } | |
290 | |
291 for (l = broadcasters; l != NULL; l = l->next) { | |
292 broadcaster = l->data; | |
293 | |
294 va_start(arrg, event); | |
295 broadcaster->func(event, broadcaster->data, arrg); | |
296 } | |
297 | |
298 return 0; | |
299 } | |
300 | |
301 const char * | |
302 gaim_event_get_name(GaimEvent event) | |
303 { | |
304 static char buf[128]; | |
305 | |
306 switch (event) { | |
307 case event_signon: | |
308 snprintf(buf, sizeof(buf), "event_signon"); | |
309 break; | |
310 case event_signoff: | |
311 snprintf(buf, sizeof(buf), "event_signoff"); | |
312 break; | |
313 case event_away: | |
314 snprintf(buf, sizeof(buf), "event_away"); | |
315 break; | |
316 case event_back: | |
317 snprintf(buf, sizeof(buf), "event_back"); | |
318 break; | |
319 case event_im_recv: | |
320 snprintf(buf, sizeof(buf), "event_im_recv"); | |
321 break; | |
322 case event_im_send: | |
323 snprintf(buf, sizeof(buf), "event_im_send"); | |
324 break; | |
325 case event_buddy_signon: | |
326 snprintf(buf, sizeof(buf), "event_buddy_signon"); | |
327 break; | |
328 case event_buddy_signoff: | |
329 snprintf(buf, sizeof(buf), "event_buddy_signoff"); | |
330 break; | |
331 case event_buddy_away: | |
332 snprintf(buf, sizeof(buf), "event_buddy_away"); | |
333 break; | |
334 case event_buddy_back: | |
335 snprintf(buf, sizeof(buf), "event_buddy_back"); | |
336 break; | |
337 case event_buddy_idle: | |
338 snprintf(buf, sizeof(buf), "event_buddy_idle"); | |
339 break; | |
340 case event_buddy_unidle: | |
341 snprintf(buf, sizeof(buf), "event_buddy_unidle"); | |
342 break; | |
343 case event_blist_update: | |
344 snprintf(buf, sizeof(buf), "event_blist_update"); | |
345 break; | |
346 case event_chat_invited: | |
347 snprintf(buf, sizeof(buf), "event_chat_invited"); | |
348 break; | |
349 case event_chat_join: | |
350 snprintf(buf, sizeof(buf), "event_chat_join"); | |
351 break; | |
352 case event_chat_leave: | |
353 snprintf(buf, sizeof(buf), "event_chat_leave"); | |
354 break; | |
355 case event_chat_buddy_join: | |
356 snprintf(buf, sizeof(buf), "event_chat_buddy_join"); | |
357 break; | |
358 case event_chat_buddy_leave: | |
359 snprintf(buf, sizeof(buf), "event_chat_buddy_leave"); | |
360 break; | |
361 case event_chat_recv: | |
362 snprintf(buf, sizeof(buf), "event_chat_recv"); | |
363 break; | |
364 case event_chat_send: | |
365 snprintf(buf, sizeof(buf), "event_chat_send"); | |
366 break; | |
367 case event_warned: | |
368 snprintf(buf, sizeof(buf), "event_warned"); | |
369 break; | |
370 case event_error: | |
371 snprintf(buf, sizeof(buf), "event_error"); | |
372 break; | |
373 case event_quit: | |
374 snprintf(buf, sizeof(buf), "event_quit"); | |
375 break; | |
376 case event_new_conversation: | |
377 snprintf(buf, sizeof(buf), "event_new_conversation"); | |
378 break; | |
379 case event_set_info: | |
380 snprintf(buf, sizeof(buf), "event_set_info"); | |
381 break; | |
382 case event_draw_menu: | |
383 snprintf(buf, sizeof(buf), "event_draw_menu"); | |
384 break; | |
385 case event_im_displayed_sent: | |
386 snprintf(buf, sizeof(buf), "event_im_displayed_sent"); | |
387 break; | |
388 case event_im_displayed_rcvd: | |
389 snprintf(buf, sizeof(buf), "event_im_displayed_rcvd"); | |
390 break; | |
391 case event_chat_send_invite: | |
392 snprintf(buf, sizeof(buf), "event_chat_send_invite"); | |
393 break; | |
394 case event_got_typing: | |
395 snprintf(buf, sizeof(buf), "event_got_typing"); | |
396 break; | |
397 case event_del_conversation: | |
398 snprintf(buf, sizeof(buf), "event_del_conversation"); | |
399 break; | |
400 case event_connecting: | |
401 snprintf(buf, sizeof(buf), "event_connecting"); | |
402 break; | |
403 default: | |
404 snprintf(buf, sizeof(buf), "event_unknown"); | |
405 break; | |
406 } | |
407 | |
408 return buf; | |
409 } | |
410 | |
411 GList * | |
412 gaim_get_callbacks(void) | |
413 { | |
414 return callbacks; | |
415 } | |
416 |