6694
|
1 /*
|
|
2 * Tcl/Glib glue
|
|
3 *
|
|
4 * Copyright (C) 2003 Ethan Blanton <eblanton@cs.purdue.edu>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 *
|
|
20 * NOTES
|
|
21 *
|
|
22 * This file was developed for the Gaim project. It inserts the Tcl
|
|
23 * event loop into the glib2 event loop for the purposes of providing
|
|
24 * Tcl bindings in a glib2 (e.g. Gtk2) program. To use it, simply
|
|
25 * link it into your executable, include tcl_glib.h, and call the
|
|
26 * function tcl_glib_init() before creating or using any Tcl
|
|
27 * interpreters. Then go ahead and use Tcl, Tk, whatever to your
|
|
28 * heart's content.
|
|
29 *
|
|
30 * BUGS
|
|
31 *
|
|
32 * tcl_wait_for_event seems to have a bug that makes vwait not work so
|
|
33 * well... I'm not sure why, yet, but I haven't put much time into
|
|
34 * it. Hopefully I will figure it out soon. In the meantime, this
|
|
35 * means that Tk's bgerror function (which is called when there is an
|
|
36 * error in a callback function) causes some Bad Mojo -- you should
|
|
37 * override it with a function that does not use Tk
|
|
38 */
|
|
39
|
|
40 #include <tcl.h>
|
|
41 #include <glib.h>
|
|
42 #include <string.h>
|
|
43
|
|
44 #include "tcl_glib.h"
|
|
45
|
|
46 struct tcl_file_handler {
|
|
47 int source;
|
|
48 int fd;
|
|
49 int mask;
|
|
50 int pending;
|
|
51 Tcl_FileProc *proc;
|
|
52 ClientData data;
|
|
53 };
|
|
54
|
|
55 struct tcl_file_event {
|
|
56 Tcl_Event header;
|
|
57 int fd;
|
|
58 };
|
|
59
|
|
60 static guint tcl_timer;
|
|
61 static gboolean tcl_timer_pending;
|
|
62 static GHashTable *tcl_file_handlers;
|
|
63
|
|
64 static void tcl_set_timer(Tcl_Time *timePtr);
|
|
65 static int tcl_wait_for_event(Tcl_Time *timePtr);
|
|
66 static void tcl_create_file_handler(int fd, int mask, Tcl_FileProc *proc, ClientData data);
|
|
67 static void tcl_delete_file_handler(int fd);
|
|
68
|
|
69 static gboolean tcl_kick(gpointer data);
|
|
70 static gboolean tcl_file_callback(GIOChannel *source, GIOCondition condition, gpointer data);
|
|
71 static int tcl_file_event_callback(Tcl_Event *event, int flags);
|
|
72
|
|
73 ClientData Tcl_InitNotifier()
|
|
74 {
|
|
75 return NULL;
|
|
76 }
|
|
77
|
|
78 void tcl_glib_init ()
|
|
79 {
|
|
80 Tcl_NotifierProcs notifier;
|
|
81
|
7822
|
82 memset(¬ifier, 0, sizeof(notifier));
|
|
83
|
6694
|
84 notifier.createFileHandlerProc = tcl_create_file_handler;
|
|
85 notifier.deleteFileHandlerProc = tcl_delete_file_handler;
|
|
86 notifier.setTimerProc = tcl_set_timer;
|
|
87 notifier.waitForEventProc = tcl_wait_for_event;
|
|
88
|
|
89 Tcl_SetNotifier(¬ifier);
|
|
90 Tcl_SetServiceMode(TCL_SERVICE_ALL);
|
|
91
|
|
92 tcl_timer_pending = FALSE;
|
|
93 tcl_file_handlers = g_hash_table_new(g_direct_hash, g_direct_equal);
|
|
94 }
|
|
95
|
|
96 static void tcl_set_timer(Tcl_Time *timePtr)
|
|
97 {
|
|
98 guint interval;
|
|
99
|
|
100 if (tcl_timer_pending)
|
|
101 g_source_remove(tcl_timer);
|
|
102
|
|
103 if (timePtr == NULL) {
|
|
104 tcl_timer_pending = FALSE;
|
|
105 return;
|
|
106 }
|
|
107
|
|
108 interval = timePtr->sec * 1000 + (timePtr->usec ? timePtr->usec / 1000 : 0);
|
|
109 tcl_timer = g_timeout_add(interval, tcl_kick, NULL);
|
|
110 tcl_timer_pending = TRUE;
|
|
111 }
|
|
112
|
|
113 static int tcl_wait_for_event(Tcl_Time *timePtr)
|
|
114 {
|
|
115 if (!timePtr || (timePtr->sec == 0 && timePtr->usec == 0)) {
|
|
116 g_main_context_iteration(NULL, FALSE);
|
|
117 return 1;
|
|
118 } else {
|
|
119 tcl_set_timer(timePtr);
|
|
120 }
|
|
121
|
|
122 g_main_context_iteration(NULL, TRUE);
|
|
123
|
|
124 return 1;
|
|
125 }
|
|
126
|
|
127 static void tcl_create_file_handler(int fd, int mask, Tcl_FileProc *proc, ClientData data)
|
|
128 {
|
|
129 struct tcl_file_handler *tfh = g_new0(struct tcl_file_handler, 1);
|
|
130 GIOChannel *channel;
|
|
131 GIOCondition cond = 0;
|
|
132
|
|
133 if (g_hash_table_lookup(tcl_file_handlers, (gpointer)fd))
|
|
134 tcl_delete_file_handler(fd);
|
|
135
|
|
136 if (mask & TCL_READABLE)
|
|
137 cond |= G_IO_IN;
|
|
138 if (mask & TCL_WRITABLE)
|
|
139 cond |= G_IO_OUT;
|
|
140 if (mask & TCL_EXCEPTION)
|
|
141 cond |= G_IO_ERR|G_IO_HUP|G_IO_NVAL;
|
|
142
|
|
143 tfh->fd = fd;
|
|
144 tfh->mask = mask;
|
|
145 tfh->proc = proc;
|
|
146 tfh->data = data;
|
|
147
|
|
148 channel = g_io_channel_unix_new(fd);
|
|
149 tfh->source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, tcl_file_callback, tfh, g_free);
|
|
150 g_io_channel_unref(channel);
|
|
151
|
|
152 g_hash_table_insert(tcl_file_handlers, (gpointer)fd, tfh);
|
|
153
|
|
154 Tcl_ServiceAll();
|
|
155 }
|
|
156
|
|
157 static void tcl_delete_file_handler(int fd)
|
|
158 {
|
|
159 struct tcl_file_handler *tfh = g_hash_table_lookup(tcl_file_handlers, (gpointer)fd);
|
|
160
|
|
161 if (tfh == NULL)
|
|
162 return;
|
|
163
|
|
164 g_source_remove(tfh->source);
|
|
165 g_hash_table_remove(tcl_file_handlers, (gpointer)fd);
|
|
166
|
|
167 Tcl_ServiceAll();
|
|
168 }
|
|
169
|
|
170 static gboolean tcl_kick(gpointer data)
|
|
171 {
|
|
172 tcl_timer_pending = FALSE;
|
|
173
|
|
174 Tcl_ServiceAll();
|
|
175
|
|
176 return FALSE;
|
|
177 }
|
|
178
|
|
179 static gboolean tcl_file_callback(GIOChannel *source, GIOCondition condition, gpointer data)
|
|
180 {
|
|
181 struct tcl_file_handler *tfh = data;
|
|
182 struct tcl_file_event *fev;
|
|
183 int mask = 0;
|
|
184
|
|
185 if (condition & G_IO_IN)
|
|
186 mask |= TCL_READABLE;
|
|
187 if (condition & G_IO_OUT)
|
|
188 mask |= TCL_WRITABLE;
|
|
189 if (condition & (G_IO_ERR|G_IO_HUP|G_IO_NVAL))
|
|
190 mask |= TCL_EXCEPTION;
|
|
191
|
|
192 if (!(tfh->mask & (mask & ~tfh->pending)))
|
|
193 return TRUE;
|
|
194
|
|
195 tfh->pending |= mask;
|
|
196 fev = (struct tcl_file_event *)ckalloc(sizeof(struct tcl_file_event));
|
|
197 memset(fev, 0, sizeof(struct tcl_file_event));
|
|
198 fev->header.proc = tcl_file_event_callback;
|
|
199 fev->fd = tfh->fd;
|
|
200 Tcl_QueueEvent((Tcl_Event *)fev, TCL_QUEUE_TAIL);
|
|
201
|
|
202 Tcl_ServiceAll();
|
|
203
|
|
204 return TRUE;
|
|
205 }
|
|
206
|
|
207 int tcl_file_event_callback(Tcl_Event *event, int flags)
|
|
208 {
|
|
209 struct tcl_file_handler *tfh;
|
|
210 struct tcl_file_event *fev = (struct tcl_file_event *)event;
|
|
211 int mask;
|
|
212
|
|
213 if (!(flags & TCL_FILE_EVENTS)) {
|
|
214 return 0;
|
|
215 }
|
|
216
|
|
217 tfh = g_hash_table_lookup(tcl_file_handlers, (gpointer)fev->fd);
|
|
218 if (tfh == NULL)
|
|
219 return 1;
|
|
220
|
|
221 mask = tfh->mask & tfh->pending;
|
|
222 if (mask)
|
|
223 (*tfh->proc)(tfh->data, mask);
|
|
224 tfh->pending = 0;
|
|
225
|
|
226 return 1;
|
|
227 }
|