source: lib/events_glib.c @ 6f10697

Last change on this file since 6f10697 was 6f10697, checked in by dequis <dx@…>, at 2015-01-16T19:50:23Z

Fix incorrect Free Software Foundation address

  • Property mode set to 100644
File size: 3.8 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 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., 51 Franklin St.,
25  Fifth Floor, Boston, MA  02110-1301  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 <sys/socket.h>
34#include <netdb.h>
35#include <netinet/in.h>
36#include <arpa/inet.h>
37#include <unistd.h>
38#include <fcntl.h>
39#include <errno.h>
40#include "proxy.h"
41
42typedef struct _GaimIOClosure {
43        b_event_handler function;
44        gpointer data;
45        guint flags;
46} GaimIOClosure;
47
48static GMainLoop *loop = NULL;
49
50void b_main_init()
51{
52        if( loop == NULL )
53                loop = g_main_new( FALSE );
54}
55
56void b_main_run()
57{
58        g_main_run( loop );
59}
60
61void b_main_quit()
62{
63        g_main_quit( loop );
64}
65
66static gboolean gaim_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
67{
68        GaimIOClosure *closure = data;
69        b_input_condition gaim_cond = 0;
70        gboolean st;
71       
72        if (condition & G_IO_NVAL)
73                return FALSE;
74
75        if (condition & GAIM_READ_COND)
76                gaim_cond |= B_EV_IO_READ;
77        if (condition & GAIM_WRITE_COND)
78                gaim_cond |= B_EV_IO_WRITE;
79       
80        event_debug( "gaim_io_invoke( %d, %d, 0x%x )\n", g_io_channel_unix_get_fd(source), condition, data );
81
82        st = closure->function(closure->data, g_io_channel_unix_get_fd(source), gaim_cond);
83       
84        if( !st )
85                event_debug( "Returned FALSE, cancelling.\n" );
86       
87        if (closure->flags & B_EV_FLAG_FORCE_ONCE)
88                return FALSE;
89        else if (closure->flags & B_EV_FLAG_FORCE_REPEAT)
90                return TRUE;
91        else
92                return st;
93}
94
95static void gaim_io_destroy(gpointer data)
96{
97        event_debug( "gaim_io_destroy( 0x%x )\n", data );
98        g_free(data);
99}
100
101gint b_input_add(gint source, b_input_condition condition, b_event_handler function, gpointer data)
102{
103        GaimIOClosure *closure = g_new0(GaimIOClosure, 1);
104        GIOChannel *channel;
105        GIOCondition cond = 0;
106        int st;
107       
108        closure->function = function;
109        closure->data = data;
110        closure->flags = condition;
111       
112        if (condition & B_EV_IO_READ)
113                cond |= GAIM_READ_COND;
114        if (condition & B_EV_IO_WRITE)
115                cond |= GAIM_WRITE_COND;
116       
117        channel = g_io_channel_unix_new(source);
118        st = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
119                                 gaim_io_invoke, closure, gaim_io_destroy);
120       
121        event_debug( "b_input_add( %d, %d, 0x%x, 0x%x ) = %d (%p)\n", source, condition, function, data, st, closure );
122       
123        g_io_channel_unref(channel);
124        return st;
125}
126
127gint b_timeout_add(gint timeout, b_event_handler func, gpointer data)
128{
129        /* GSourceFunc and the BitlBee event handler function aren't
130           really the same, but they're "compatible". ;-) It will do
131           for now, BitlBee only looks at the "data" argument. */
132        gint st = g_timeout_add(timeout, (GSourceFunc) func, data);
133       
134        event_debug( "b_timeout_add( %d, %d, %d ) = %d\n", timeout, func, data, st );
135       
136        return st;
137}
138
139void b_event_remove(gint tag)
140{
141        event_debug( "b_event_remove( %d )\n", tag );
142       
143        if (tag > 0)
144                g_source_remove(tag);
145}
146
147void closesocket( int fd )
148{
149        close( fd );
150}
Note: See TracBrowser for help on using the repository browser.