[ecf8fa8] | 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 GLib) |
---|
| 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 | #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" |
---|
| 47 | |
---|
| 48 | typedef struct _GaimIOClosure { |
---|
[ba9edaa] | 49 | b_event_handler function; |
---|
[ecf8fa8] | 50 | gpointer data; |
---|
| 51 | } GaimIOClosure; |
---|
| 52 | |
---|
[ba9edaa] | 53 | static GMainLoop *loop; |
---|
| 54 | |
---|
| 55 | void b_main_init() |
---|
| 56 | { |
---|
| 57 | loop = g_main_new( FALSE ); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | void b_main_run() |
---|
| 61 | { |
---|
| 62 | g_main_run( loop ); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void b_main_quit() |
---|
| 66 | { |
---|
| 67 | g_main_quit( loop ); |
---|
| 68 | } |
---|
| 69 | |
---|
[ecf8fa8] | 70 | static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) |
---|
| 71 | { |
---|
| 72 | GaimIOClosure *closure = data; |
---|
[ba9edaa] | 73 | b_input_condition gaim_cond = 0; |
---|
[fc2ee0f] | 74 | gboolean st; |
---|
[ecf8fa8] | 75 | |
---|
| 76 | if (condition & GAIM_READ_COND) |
---|
| 77 | gaim_cond |= GAIM_INPUT_READ; |
---|
| 78 | if (condition & GAIM_WRITE_COND) |
---|
| 79 | gaim_cond |= GAIM_INPUT_WRITE; |
---|
[fc2ee0f] | 80 | |
---|
| 81 | event_debug( "gaim_io_invoke( %d, %d, 0x%x )\n", g_io_channel_unix_get_fd(source), condition, data ); |
---|
[ecf8fa8] | 82 | |
---|
[fc2ee0f] | 83 | st = closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond); |
---|
| 84 | |
---|
| 85 | if( !st ) |
---|
| 86 | event_debug( "Returned FALSE, cancelling.\n" ); |
---|
| 87 | |
---|
| 88 | return st; |
---|
[ecf8fa8] | 89 | } |
---|
| 90 | |
---|
| 91 | static void gaim_io_destroy(gpointer data) |
---|
| 92 | { |
---|
[fc2ee0f] | 93 | event_debug( "gaim_io_destroy( 0x%x )\n", data ); |
---|
[ecf8fa8] | 94 | g_free(data); |
---|
| 95 | } |
---|
| 96 | |
---|
[ba9edaa] | 97 | gint b_input_add(gint source, b_input_condition condition, b_event_handler function, gpointer data) |
---|
[ecf8fa8] | 98 | { |
---|
| 99 | GaimIOClosure *closure = g_new0(GaimIOClosure, 1); |
---|
| 100 | GIOChannel *channel; |
---|
| 101 | GIOCondition cond = 0; |
---|
[f394500] | 102 | int st; |
---|
[ecf8fa8] | 103 | |
---|
| 104 | closure->function = function; |
---|
| 105 | closure->data = data; |
---|
| 106 | |
---|
| 107 | if (condition & GAIM_INPUT_READ) |
---|
| 108 | cond |= GAIM_READ_COND; |
---|
| 109 | if (condition & GAIM_INPUT_WRITE) |
---|
| 110 | cond |= GAIM_WRITE_COND; |
---|
| 111 | |
---|
| 112 | channel = g_io_channel_unix_new(source); |
---|
[f394500] | 113 | st = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, |
---|
| 114 | gaim_io_invoke, closure, gaim_io_destroy); |
---|
[ecf8fa8] | 115 | |
---|
[f394500] | 116 | event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) = %d (%p)\n", source, condition, function, data, st, closure ); |
---|
[fc2ee0f] | 117 | |
---|
[ecf8fa8] | 118 | g_io_channel_unref(channel); |
---|
[f394500] | 119 | return st; |
---|
[ecf8fa8] | 120 | } |
---|
| 121 | |
---|
[ba9edaa] | 122 | gint b_timeout_add(gint timeout, b_event_handler func, gpointer data) |
---|
| 123 | { |
---|
[04026d4] | 124 | /* GSourceFunc and the BitlBee event handler function aren't |
---|
| 125 | really the same, but they're "compatible". ;-) It will do |
---|
| 126 | for now, BitlBee only looks at the "data" argument. */ |
---|
| 127 | gint st = g_timeout_add(timeout, (GSourceFunc) func, data); |
---|
[fc2ee0f] | 128 | |
---|
| 129 | event_debug( "b_timeout_add( %d, %d, %d ) = %d\n", timeout, func, data, st ); |
---|
| 130 | |
---|
| 131 | return st; |
---|
[ba9edaa] | 132 | } |
---|
| 133 | |
---|
| 134 | void b_event_remove(gint tag) |
---|
[ecf8fa8] | 135 | { |
---|
[782d988] | 136 | event_debug( "b_event_remove( %d )\n", tag ); |
---|
[fc2ee0f] | 137 | |
---|
[ecf8fa8] | 138 | if (tag > 0) |
---|
| 139 | g_source_remove(tag); |
---|
| 140 | } |
---|