[13cc96c] | 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 | |
---|
| 42 | struct b_event_data |
---|
| 43 | { |
---|
| 44 | guint id; |
---|
| 45 | struct event evinfo; |
---|
| 46 | b_event_handler function; |
---|
| 47 | void *data; |
---|
| 48 | }; |
---|
| 49 | |
---|
| 50 | void b_main_init() |
---|
| 51 | { |
---|
| 52 | event_init(); |
---|
| 53 | |
---|
| 54 | id_next = 1; |
---|
| 55 | id_hash = g_hash_table_new( g_int_hash, g_int_equal ); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | void b_main_run() |
---|
| 59 | { |
---|
| 60 | event_dispatch(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void b_main_quit() |
---|
| 64 | { |
---|
| 65 | struct timeval tv; |
---|
| 66 | |
---|
| 67 | memset( &tv, 0, sizeof( struct timeval ) ); |
---|
| 68 | event_loopexit( &tv ); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | static void b_event_passthrough( int fd, short event, void *data ) |
---|
| 72 | { |
---|
| 73 | struct b_event_data *b_ev = data; |
---|
| 74 | b_input_condition cond = 0; |
---|
| 75 | |
---|
| 76 | if( event & EV_READ ) |
---|
| 77 | cond |= GAIM_INPUT_READ; |
---|
| 78 | if( event & EV_WRITE ) |
---|
| 79 | cond |= GAIM_INPUT_WRITE; |
---|
| 80 | |
---|
| 81 | if( !b_ev->function( b_ev->data, fd, cond ) ) |
---|
| 82 | b_event_remove( b_ev->id ); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | gint b_input_add( gint source, b_input_condition condition, b_event_handler function, gpointer data ) |
---|
| 86 | { |
---|
| 87 | struct b_event_data *b_ev = g_new0( struct b_event_data, 1 ); |
---|
| 88 | GIOCondition cond; |
---|
| 89 | |
---|
| 90 | b_ev->id == id_next++; |
---|
| 91 | b_ev->function = function; |
---|
| 92 | b_ev->data = data; |
---|
| 93 | |
---|
| 94 | cond = EV_PERSIST; |
---|
| 95 | if( condition & GAIM_INPUT_READ ) |
---|
| 96 | cond |= EV_READ; |
---|
| 97 | if( condition & GAIM_INPUT_WRITE ) |
---|
| 98 | cond |= EV_WRITE; |
---|
| 99 | |
---|
| 100 | event_set( &b_ev->evinfo, source, cond, b_event_passthrough, b_ev ); |
---|
| 101 | event_add( &b_ev->evinfo, NULL ); |
---|
| 102 | |
---|
| 103 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
| 104 | |
---|
| 105 | return b_ev->id; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | gint b_timeout_add( gint timeout, b_event_handler function, gpointer data ) |
---|
| 109 | { |
---|
| 110 | struct b_event_data *b_ev = g_new0( struct b_event_data, 1 ); |
---|
| 111 | struct timeval tv; |
---|
| 112 | |
---|
| 113 | b_ev->id == id_next++; |
---|
| 114 | b_ev->function = function; |
---|
| 115 | b_ev->data = data; |
---|
| 116 | |
---|
| 117 | tv.tv_sec = timeout / 1000; |
---|
| 118 | tv.tv_usec = ( timeout % 1000 ) * 1000; |
---|
| 119 | |
---|
| 120 | evtimer_set( &b_ev->evinfo, b_event_passthrough, b_ev ); |
---|
| 121 | evtimer_add( &b_ev->evinfo, &tv); |
---|
| 122 | |
---|
| 123 | g_hash_table_insert( id_hash, &b_ev->id, b_ev ); |
---|
| 124 | |
---|
| 125 | return b_ev->id; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void b_event_remove( gint tag ) |
---|
| 129 | { |
---|
| 130 | struct b_event_data *b_ev = g_hash_table_lookup( id_hash, &tag ); |
---|
| 131 | |
---|
| 132 | if( b_ev ) |
---|
| 133 | { |
---|
| 134 | event_del( &b_ev->evinfo ); |
---|
| 135 | g_hash_table_remove( id_hash, &tag ); |
---|
| 136 | g_free( b_ev ); |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | gboolean b_event_remove_by_data( gpointer data ) |
---|
| 141 | { |
---|
| 142 | /* FIXME! */ |
---|
| 143 | return FALSE; |
---|
| 144 | } |
---|