source: protocols/events_libevent.c @ 309cb9e

Last change on this file since 309cb9e was fc2ee0f, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-05-13T10:29:53Z

It works, it works! \o/

  • Property mode set to 100644
File size: 4.0 KB
Line 
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
38static guint id_next;
39static GHashTable *id_hash;
40
41struct b_event_data
42{
43        guint id;
44        struct event evinfo;
45        b_event_handler function;
46        void *data;
47};
48
49void 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
57void b_main_run()
58{
59        event_dispatch();
60}
61
62void b_main_quit()
63{
64        struct timeval tv;
65       
66        memset( &tv, 0, sizeof( struct timeval ) );
67        event_loopexit( &tv );
68}
69
70static 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        int id;
75       
76        if( fd >= 0 )
77        {
78                if( event & EV_READ )
79                        cond |= GAIM_INPUT_READ;
80                if( event & EV_WRITE )
81                        cond |= GAIM_INPUT_WRITE;
82        }
83       
84        event_debug( "b_event_passthrough( %d, %d, 0x%x ) (%d)\n", fd, event, (int) data, b_ev->id );
85       
86        /* Since the called function might cancel this handler already
87           (which free()s b_ev, we have to remember the ID here. */
88        id = b_ev->id;
89       
90        if( !b_ev->function( b_ev->data, fd, cond ) )
91        {
92                event_debug( "Handler returned FALSE: " );
93                b_event_remove( id );
94        }
95}
96
97gint b_input_add( gint fd, b_input_condition condition, b_event_handler function, gpointer data )
98{
99        struct b_event_data *b_ev = g_new0( struct b_event_data, 1 );
100        GIOCondition out_cond;
101       
102        b_ev->id = id_next++;
103        b_ev->function = function;
104        b_ev->data = data;
105       
106        out_cond = EV_PERSIST;
107        if( condition & GAIM_INPUT_READ )
108                out_cond |= EV_READ;
109        if( condition & GAIM_INPUT_WRITE )
110                out_cond |= EV_WRITE;
111       
112        event_set( &b_ev->evinfo, fd, out_cond, b_event_passthrough, b_ev );
113        event_add( &b_ev->evinfo, NULL );
114       
115        event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) = %d\n", fd, condition, function, data, b_ev->id );
116       
117        g_hash_table_insert( id_hash, &b_ev->id, b_ev );
118       
119        return b_ev->id;
120}
121
122/* TODO: Persistence for timers! */
123gint b_timeout_add( gint timeout, b_event_handler function, gpointer data )
124{
125        struct b_event_data *b_ev = g_new0( struct b_event_data, 1 );
126        struct timeval tv;
127       
128        b_ev->id = id_next++;
129        b_ev->function = function;
130        b_ev->data = data;
131       
132        tv.tv_sec = timeout / 1000;
133        tv.tv_usec = ( timeout % 1000 ) * 1000;
134       
135        evtimer_set( &b_ev->evinfo, b_event_passthrough, b_ev );
136        evtimer_add( &b_ev->evinfo, &tv );
137       
138        event_debug( "b_timeout_add( %d, %d, 0x%x ) = %d\n", timeout, function, data, b_ev->id );
139       
140        g_hash_table_insert( id_hash, &b_ev->id, b_ev );
141       
142        return b_ev->id;
143}
144
145void b_event_remove( gint id )
146{
147        struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &id );
148       
149        event_debug( "b_event_remove( %d )\n", id );
150        if( b_ev )
151        {
152                event_del( &b_ev->evinfo );
153                g_hash_table_remove( id_hash, &b_ev->id );
154                g_free( b_ev );
155        }
156        else
157        {
158                event_debug( "Double remove?\n" );
159        }
160}
161
162gboolean b_event_remove_by_data( gpointer data )
163{
164        /* FIXME! */
165        event_debug( "FALSE!\n" );
166        return FALSE;
167}
Note: See TracBrowser for help on using the repository browser.