Merge heads.
[pidgin-git.git] / libpurple / eventloop.c
blob23847356db28b7fc05e0fd1c559e692fae8b1007
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include "internal.h"
22 #include "eventloop.h"
24 #define PURPLE_GLIB_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
25 #define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
27 typedef struct {
28 PurpleInputFunction function;
29 guint result;
30 gpointer data;
31 } PurpleIOClosure;
33 static gboolean
34 purple_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
36 PurpleIOClosure *closure = data;
37 PurpleInputCondition purple_cond = 0;
39 if (condition & PURPLE_GLIB_READ_COND)
40 purple_cond |= PURPLE_INPUT_READ;
41 if (condition & PURPLE_GLIB_WRITE_COND)
42 purple_cond |= PURPLE_INPUT_WRITE;
44 #ifdef _WIN32
45 if(!purple_cond) {
46 return TRUE;
48 #endif /* _WIN32 */
50 closure->function(closure->data, g_io_channel_unix_get_fd(source),
51 purple_cond);
53 return TRUE;
56 guint
57 purple_input_add(int source, PurpleInputCondition condition, PurpleInputFunction func, gpointer user_data)
59 PurpleIOClosure *closure = g_new0(PurpleIOClosure, 1);
60 GIOChannel *channel;
61 GIOCondition cond = 0;
63 closure->function = func;
64 closure->data = user_data;
66 if (condition & PURPLE_INPUT_READ)
67 cond |= PURPLE_GLIB_READ_COND;
68 if (condition & PURPLE_INPUT_WRITE)
69 cond |= PURPLE_GLIB_WRITE_COND;
71 #ifdef _WIN32
72 channel = g_io_channel_win32_new_socket(source);
73 #else
74 channel = g_io_channel_unix_new(source);
75 #endif
77 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT,
78 cond, purple_io_invoke, closure, g_free);
80 g_io_channel_unref(channel);
81 return closure->result;
84 gboolean
85 purple_input_remove(guint tag)
87 return g_source_remove(tag);
90 int
91 purple_input_pipe(int pipefd[2])
93 #ifdef _WIN32
94 return wpurple_input_pipe(pipefd);
95 #else
96 return pipe(pipefd);
97 #endif