source: lib/events_glib.c @ 92a9c68

Last change on this file since 92a9c68 was 851a8c2, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-03-22T12:02:50Z

Taught GLib-mode subprocesses how to die. (Closes: #374)

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