1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2006 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* |
---|
8 | * Event handling (using libevent) |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | This program is free software; you can redistribute it and/or modify |
---|
13 | it under the terms of the GNU General Public License as published by |
---|
14 | the Free Software Foundation; either version 2 of the License, or |
---|
15 | (at your option) any later version. |
---|
16 | |
---|
17 | This program is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | GNU General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License with |
---|
23 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
24 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
25 | Suite 330, Boston, MA 02111-1307 USA |
---|
26 | */ |
---|
27 | |
---|
28 | #define BITLBEE_CORE |
---|
29 | #include <stdio.h> |
---|
30 | #include <stdlib.h> |
---|
31 | #include <string.h> |
---|
32 | #include <sys/types.h> |
---|
33 | #include "proxy.h" |
---|
34 | |
---|
35 | #include <sys/time.h> |
---|
36 | #include <event.h> |
---|
37 | |
---|
38 | static guint id_next; |
---|
39 | static GHashTable *id_hash; |
---|
40 | |
---|
41 | struct b_event_data |
---|
42 | { |
---|
43 | guint id; |
---|
44 | struct event evinfo; |
---|
45 | b_event_handler function; |
---|
46 | void *data; |
---|
47 | }; |
---|
48 | |
---|
49 | void b_main_init() |
---|
50 | { |
---|
51 | event_init(); |
---|
52 | |
---|
53 | id_next = 1; |
---|
54 | id_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
55 | } |
---|
56 | |
---|
57 | void b_main_run() |
---|
58 | { |
---|
59 | event_dispatch(); |
---|
60 | } |
---|
61 | |
---|
62 | void b_main_quit() |
---|
63 | { |
---|
64 | struct timeval tv; |
---|
65 | |
---|
66 | memset( &tv, 0, sizeof( struct timeval ) ); |
---|
67 | event_loopexit( &tv ); |
---|
68 | } |
---|
69 | |
---|
70 | static void b_event_passthrough( int fd, short event, void *data ) |
---|
71 | { |
---|
72 | struct b_event_data *b_ev = data; |
---|
73 | b_input_condition cond = 0; |
---|
74 | |
---|
75 | if( fd >= 0 ) |
---|
76 | { |
---|
77 | if( event & EV_READ ) |
---|
78 | cond |= GAIM_INPUT_READ; |
---|
79 | if( event & EV_WRITE ) |
---|
80 | cond |= GAIM_INPUT_WRITE; |
---|
81 | } |
---|
82 | |
---|
83 | if( !b_ev->function( b_ev->data, fd, cond ) ) |
---|
84 | b_event_remove( b_ev->id ); |
---|
85 | } |
---|
86 | |
---|
87 | gint b_input_add( gint source, b_input_condition condition, b_event_handler function, gpointer data ) |
---|
88 | { |
---|
89 | struct b_event_data *b_ev = g_new0( struct b_event_data, 1 ); |
---|
90 | GIOCondition cond; |
---|
91 | |
---|
92 | b_ev->id = id_next++; |
---|
93 | b_ev->function = function; |
---|
94 | b_ev->data = data; |
---|
95 | |
---|
96 | cond = EV_PERSIST; |
---|
97 | if( condition & GAIM_INPUT_READ ) |
---|
98 | cond |= EV_READ; |
---|
99 | if( condition & GAIM_INPUT_WRITE ) |
---|
100 | cond |= EV_WRITE; |
---|
101 | |
---|
102 | event_set( &b_ev->evinfo, source, cond, b_event_passthrough, b_ev ); |
---|
103 | event_add( &b_ev->evinfo, NULL ); |
---|
104 | |
---|
105 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
106 | |
---|
107 | return b_ev->id; |
---|
108 | } |
---|
109 | |
---|
110 | /* TODO: Persistence for timers! */ |
---|
111 | gint b_timeout_add( gint timeout, b_event_handler function, gpointer data ) |
---|
112 | { |
---|
113 | struct b_event_data *b_ev = g_new0( struct b_event_data, 1 ); |
---|
114 | struct timeval tv; |
---|
115 | |
---|
116 | b_ev->id = id_next++; |
---|
117 | b_ev->function = function; |
---|
118 | b_ev->data = data; |
---|
119 | |
---|
120 | tv.tv_sec = timeout / 1000; |
---|
121 | tv.tv_usec = ( timeout % 1000 ) * 1000; |
---|
122 | |
---|
123 | evtimer_set( &b_ev->evinfo, b_event_passthrough, b_ev ); |
---|
124 | evtimer_add( &b_ev->evinfo, &tv ); |
---|
125 | |
---|
126 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
127 | |
---|
128 | return b_ev->id; |
---|
129 | } |
---|
130 | |
---|
131 | void b_event_remove( gint tag ) |
---|
132 | { |
---|
133 | struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &tag ); |
---|
134 | |
---|
135 | if( b_ev ) |
---|
136 | { |
---|
137 | event_del( &b_ev->evinfo ); |
---|
138 | g_hash_table_remove( id_hash, &b_ev->id ); |
---|
139 | g_free( b_ev ); |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | gboolean b_event_remove_by_data( gpointer data ) |
---|
144 | { |
---|
145 | /* FIXME! */ |
---|
146 | return FALSE; |
---|
147 | } |
---|