| |
1 /** |
| |
2 * @file gtk_eventloop.c Gaim Event Loop API (gtk implementation) |
| |
3 * @ingroup gtkui |
| |
4 * |
| |
5 * gaim |
| |
6 * |
| |
7 * Gaim is the legal property of its developers, whose names are too numerous |
| |
8 * to list here. Please refer to the COPYRIGHT file distributed with this |
| |
9 * source distribution. |
| |
10 * |
| |
11 * This program is free software; you can redistribute it and/or modify |
| |
12 * it under the terms of the GNU General Public License as published by |
| |
13 * the Free Software Foundation; either version 2 of the License, or |
| |
14 * (at your option) any later version. |
| |
15 * |
| |
16 * This program is distributed in the hope that it will be useful, |
| |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
19 * GNU General Public License for more details. |
| |
20 * |
| |
21 * You should have received a copy of the GNU General Public License |
| |
22 * along with this program; if not, write to the Free Software |
| |
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| |
24 */ |
| |
25 |
| |
26 #include <glib.h> |
| |
27 #include "gtkeventloop.h" |
| |
28 #include "eventloop.h" |
| |
29 #ifdef _WIN32 |
| |
30 #include "win32dep.h" |
| |
31 #endif |
| |
32 |
| |
33 #define GAIM_GTK_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR) |
| |
34 #define GAIM_GTK_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL) |
| |
35 |
| |
36 typedef struct _GaimGtkIOClosure { |
| |
37 GaimInputFunction function; |
| |
38 guint result; |
| |
39 gpointer data; |
| |
40 |
| |
41 } GaimGtkIOClosure; |
| |
42 |
| |
43 static void gaim_gtk_io_destroy(gpointer data) |
| |
44 { |
| |
45 g_free(data); |
| |
46 } |
| |
47 |
| |
48 static gboolean gaim_gtk_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data) |
| |
49 { |
| |
50 GaimGtkIOClosure *closure = data; |
| |
51 GaimInputCondition gaim_cond = 0; |
| |
52 |
| |
53 if (condition & GAIM_GTK_READ_COND) |
| |
54 gaim_cond |= GAIM_INPUT_READ; |
| |
55 if (condition & GAIM_GTK_WRITE_COND) |
| |
56 gaim_cond |= GAIM_INPUT_WRITE; |
| |
57 |
| |
58 #if 0 |
| |
59 gaim_debug(GAIM_DEBUG_MISC, "gtk_eventloop", |
| |
60 "CLOSURE: callback for %d, fd is %d\n", |
| |
61 closure->result, g_io_channel_unix_get_fd(source)); |
| |
62 #endif |
| |
63 |
| |
64 #ifdef _WIN32 |
| |
65 if(! gaim_cond) { |
| |
66 #if DEBUG |
| |
67 gaim_debug(GAIM_DEBUG_MISC, "gtk_eventloop", |
| |
68 "CLOSURE received GIOCondition of 0x%x, which does not" |
| |
69 " match 0x%x (READ) or 0x%x (WRITE)\n", |
| |
70 condition, GAIM_GTK_READ_COND, GAIM_GTK_WRITE_COND); |
| |
71 #endif /* DEBUG */ |
| |
72 |
| |
73 return TRUE; |
| |
74 } |
| |
75 #endif /* _WIN32 */ |
| |
76 |
| |
77 closure->function(closure->data, g_io_channel_unix_get_fd(source), |
| |
78 gaim_cond); |
| |
79 |
| |
80 return TRUE; |
| |
81 } |
| |
82 |
| |
83 static guint gaim_gtk_input_add(gint fd, GaimInputCondition condition, GaimInputFunction function, |
| |
84 gpointer data) |
| |
85 { |
| |
86 GaimGtkIOClosure *closure = g_new0(GaimGtkIOClosure, 1); |
| |
87 GIOChannel *channel; |
| |
88 GIOCondition cond = 0; |
| |
89 |
| |
90 closure->function = function; |
| |
91 closure->data = data; |
| |
92 |
| |
93 if (condition & GAIM_INPUT_READ) |
| |
94 cond |= GAIM_GTK_READ_COND; |
| |
95 if (condition & GAIM_INPUT_WRITE) |
| |
96 cond |= GAIM_GTK_WRITE_COND; |
| |
97 |
| |
98 #ifdef _WIN32 |
| |
99 channel = wgaim_g_io_channel_win32_new_socket(fd); |
| |
100 #else |
| |
101 channel = g_io_channel_unix_new(fd); |
| |
102 #endif |
| |
103 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, |
| |
104 gaim_gtk_io_invoke, closure, gaim_gtk_io_destroy); |
| |
105 |
| |
106 #if 0 |
| |
107 gaim_debug(GAIM_DEBUG_MISC, "gtk_eventloop", |
| |
108 "CLOSURE: adding input watcher %d for fd %d\n", |
| |
109 closure->result, fd); |
| |
110 #endif |
| |
111 |
| |
112 g_io_channel_unref(channel); |
| |
113 return closure->result; |
| |
114 } |
| |
115 |
| |
116 static GaimEventLoopUiOps eventloop_ops = |
| |
117 { |
| |
118 g_timeout_add, |
| |
119 (guint (*)(guint))g_source_remove, |
| |
120 gaim_gtk_input_add, |
| |
121 (guint (*)(guint))g_source_remove |
| |
122 }; |
| |
123 |
| |
124 GaimEventLoopUiOps * |
| |
125 gaim_gtk_eventloop_get_ui_ops(void) |
| |
126 { |
| |
127 return &eventloop_ops; |
| |
128 } |