Scan media entities as well, not just url entities. This should expand more
[bitlbee.git] / lib / events_glib.c
blob8f53fbbfaf6725a34b73b980e6b3695b271ed657
1 /********************************************************************\
2 * BitlBee -- An IRC to other IM-networks gateway *
3 * *
4 * Copyright 2002-2006 Wilmer van der Gaast and others *
5 \********************************************************************/
7 /*
8 * Event handling (using GLib)
9 */
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.
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.
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
28 #define BITLBEE_CORE
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #ifndef _WIN32
34 #include <sys/socket.h>
35 #include <netdb.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <unistd.h>
39 #else
40 #include "sock.h"
41 #define ETIMEDOUT WSAETIMEDOUT
42 #define EINPROGRESS WSAEINPROGRESS
43 #endif
44 #include <fcntl.h>
45 #include <errno.h>
46 #include "proxy.h"
48 typedef struct _GaimIOClosure {
49 b_event_handler function;
50 gpointer data;
51 guint flags;
52 } GaimIOClosure;
54 static GMainLoop *loop = NULL;
56 void b_main_init()
58 if( loop == NULL )
59 loop = g_main_new( FALSE );
62 void b_main_run()
64 g_main_run( loop );
67 void b_main_quit()
69 g_main_quit( loop );
72 static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
74 GaimIOClosure *closure = data;
75 b_input_condition gaim_cond = 0;
76 gboolean st;
78 if (condition & G_IO_NVAL)
79 return FALSE;
81 if (condition & GAIM_READ_COND)
82 gaim_cond |= B_EV_IO_READ;
83 if (condition & GAIM_WRITE_COND)
84 gaim_cond |= B_EV_IO_WRITE;
86 event_debug( "gaim_io_invoke( %d, %d, 0x%x )\n", g_io_channel_unix_get_fd(source), condition, data );
88 st = closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond);
90 if( !st )
91 event_debug( "Returned FALSE, cancelling.\n" );
93 if (closure->flags & B_EV_FLAG_FORCE_ONCE)
94 return FALSE;
95 else if (closure->flags & B_EV_FLAG_FORCE_REPEAT)
96 return TRUE;
97 else
98 return st;
101 static void gaim_io_destroy(gpointer data)
103 event_debug( "gaim_io_destroy( 0x%x )\n", data );
104 g_free(data);
107 gint b_input_add(gint source, b_input_condition condition, b_event_handler function, gpointer data)
109 GaimIOClosure *closure = g_new0(GaimIOClosure, 1);
110 GIOChannel *channel;
111 GIOCondition cond = 0;
112 int st;
114 closure->function = function;
115 closure->data = data;
116 closure->flags = condition;
118 if (condition & B_EV_IO_READ)
119 cond |= GAIM_READ_COND;
120 if (condition & B_EV_IO_WRITE)
121 cond |= GAIM_WRITE_COND;
123 channel = g_io_channel_unix_new(source);
124 st = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
125 gaim_io_invoke, closure, gaim_io_destroy);
127 event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) = %d (%p)\n", source, condition, function, data, st, closure );
129 g_io_channel_unref(channel);
130 return st;
133 gint b_timeout_add(gint timeout, b_event_handler func, gpointer data)
135 /* GSourceFunc and the BitlBee event handler function aren't
136 really the same, but they're "compatible". ;-) It will do
137 for now, BitlBee only looks at the "data" argument. */
138 gint st = g_timeout_add(timeout, (GSourceFunc) func, data);
140 event_debug( "b_timeout_add( %d, %d, %d ) = %d\n", timeout, func, data, st );
142 return st;
145 void b_event_remove(gint tag)
147 event_debug( "b_event_remove( %d )\n", tag );
149 if (tag > 0)
150 g_source_remove(tag);
153 void closesocket( int fd )
155 close( fd );