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 | event_debug( "b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id ); |
---|
84 | |
---|
85 | if( !b_ev->function( b_ev->data, fd, cond ) ) |
---|
86 | { |
---|
87 | event_debug( "Handler returned FALSE: " ); |
---|
88 | b_event_remove( b_ev->id ); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | gint b_input_add( gint fd, b_input_condition condition, b_event_handler function, gpointer data ) |
---|
93 | { |
---|
94 | struct b_event_data *b_ev = g_new0( struct b_event_data, 1 ); |
---|
95 | GIOCondition out_cond; |
---|
96 | |
---|
97 | b_ev->id = id_next++; |
---|
98 | b_ev->function = function; |
---|
99 | b_ev->data = data; |
---|
100 | |
---|
101 | out_cond = EV_PERSIST; |
---|
102 | if( condition & GAIM_INPUT_READ ) |
---|
103 | out_cond |= EV_READ; |
---|
104 | if( condition & GAIM_INPUT_WRITE ) |
---|
105 | out_cond |= EV_WRITE; |
---|
106 | |
---|
107 | event_set( &b_ev->evinfo, fd, out_cond, b_event_passthrough, b_ev ); |
---|
108 | event_add( &b_ev->evinfo, NULL ); |
---|
109 | |
---|
110 | event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) = %d\n", fd, condition, function, data, b_ev->id ); |
---|
111 | |
---|
112 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
113 | |
---|
114 | return b_ev->id; |
---|
115 | } |
---|
116 | |
---|
117 | /* TODO: Persistence for timers! */ |
---|
118 | gint b_timeout_add( gint timeout, b_event_handler function, gpointer data ) |
---|
119 | { |
---|
120 | struct b_event_data *b_ev = g_new0( struct b_event_data, 1 ); |
---|
121 | struct timeval tv; |
---|
122 | |
---|
123 | b_ev->id = id_next++; |
---|
124 | b_ev->function = function; |
---|
125 | b_ev->data = data; |
---|
126 | |
---|
127 | tv.tv_sec = timeout / 1000; |
---|
128 | tv.tv_usec = ( timeout % 1000 ) * 1000; |
---|
129 | |
---|
130 | evtimer_set( &b_ev->evinfo, b_event_passthrough, b_ev ); |
---|
131 | evtimer_add( &b_ev->evinfo, &tv ); |
---|
132 | |
---|
133 | event_debug( "b_timeout_add( %d, %d, 0x%x ) = %d\n", timeout, function, data, b_ev->id ); |
---|
134 | |
---|
135 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
136 | |
---|
137 | return b_ev->id; |
---|
138 | } |
---|
139 | |
---|
140 | void b_event_remove( gint id ) |
---|
141 | { |
---|
142 | struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &id ); |
---|
143 | |
---|
144 | event_debug( "b_event_remove( %d )\n", id ); |
---|
145 | if( b_ev ) |
---|
146 | { |
---|
147 | event_del( &b_ev->evinfo ); |
---|
148 | g_hash_table_remove( id_hash, &b_ev->id ); |
---|
149 | g_free( b_ev ); |
---|
150 | } |
---|
151 | else |
---|
152 | { |
---|
153 | event_debug( "Invalid?\n" ); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | gboolean b_event_remove_by_data( gpointer data ) |
---|
158 | { |
---|
159 | /* FIXME! */ |
---|
160 | event_debug( "FALSE!\n" ); |
---|
161 | return FALSE; |
---|
162 | } |
---|