comparison plugins/tcl/tcl_glib.c @ 6694:2d2f04c5c7d2

[gaim-migrate @ 7220] Sean probably won't think this is contact support. This is in fact a Tcl script plugin loader. That's probably what he'll think it is. committer: Tailor Script <tailor@pidgin.im>
author Ethan Blanton <elb@pidgin.im>
date Tue, 02 Sep 2003 03:34:37 +0000
parents
children ea4f65164307
comparison
equal deleted inserted replaced
6693:8c1b5dd87fbf 6694:2d2f04c5c7d2
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
82 notifier.createFileHandlerProc = tcl_create_file_handler;
83 notifier.deleteFileHandlerProc = tcl_delete_file_handler;
84 notifier.setTimerProc = tcl_set_timer;
85 notifier.waitForEventProc = tcl_wait_for_event;
86
87 Tcl_SetNotifier(&notifier);
88 Tcl_SetServiceMode(TCL_SERVICE_ALL);
89
90 tcl_timer_pending = FALSE;
91 tcl_file_handlers = g_hash_table_new(g_direct_hash, g_direct_equal);
92 }
93
94 static void tcl_set_timer(Tcl_Time *timePtr)
95 {
96 guint interval;
97
98 if (tcl_timer_pending)
99 g_source_remove(tcl_timer);
100
101 if (timePtr == NULL) {
102 tcl_timer_pending = FALSE;
103 return;
104 }
105
106 interval = timePtr->sec * 1000 + (timePtr->usec ? timePtr->usec / 1000 : 0);
107 tcl_timer = g_timeout_add(interval, tcl_kick, NULL);
108 tcl_timer_pending = TRUE;
109 }
110
111 static int tcl_wait_for_event(Tcl_Time *timePtr)
112 {
113 if (!timePtr || (timePtr->sec == 0 && timePtr->usec == 0)) {
114 g_main_context_iteration(NULL, FALSE);
115 return 1;
116 } else {
117 tcl_set_timer(timePtr);
118 }
119
120 g_main_context_iteration(NULL, TRUE);
121
122 return 1;
123 }
124
125 static void tcl_create_file_handler(int fd, int mask, Tcl_FileProc *proc, ClientData data)
126 {
127 struct tcl_file_handler *tfh = g_new0(struct tcl_file_handler, 1);
128 GIOChannel *channel;
129 GIOCondition cond = 0;
130
131 if (g_hash_table_lookup(tcl_file_handlers, (gpointer)fd))
132 tcl_delete_file_handler(fd);
133
134 if (mask & TCL_READABLE)
135 cond |= G_IO_IN;
136 if (mask & TCL_WRITABLE)
137 cond |= G_IO_OUT;
138 if (mask & TCL_EXCEPTION)
139 cond |= G_IO_ERR|G_IO_HUP|G_IO_NVAL;
140
141 tfh->fd = fd;
142 tfh->mask = mask;
143 tfh->proc = proc;
144 tfh->data = data;
145
146 channel = g_io_channel_unix_new(fd);
147 tfh->source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, tcl_file_callback, tfh, g_free);
148 g_io_channel_unref(channel);
149
150 g_hash_table_insert(tcl_file_handlers, (gpointer)fd, tfh);
151
152 Tcl_ServiceAll();
153 }
154
155 static void tcl_delete_file_handler(int fd)
156 {
157 struct tcl_file_handler *tfh = g_hash_table_lookup(tcl_file_handlers, (gpointer)fd);
158
159 if (tfh == NULL)
160 return;
161
162 g_source_remove(tfh->source);
163 g_hash_table_remove(tcl_file_handlers, (gpointer)fd);
164
165 Tcl_ServiceAll();
166 }
167
168 static gboolean tcl_kick(gpointer data)
169 {
170 tcl_timer_pending = FALSE;
171
172 Tcl_ServiceAll();
173
174 return FALSE;
175 }
176
177 static gboolean tcl_file_callback(GIOChannel *source, GIOCondition condition, gpointer data)
178 {
179 struct tcl_file_handler *tfh = data;
180 struct tcl_file_event *fev;
181 int mask = 0;
182
183 if (condition & G_IO_IN)
184 mask |= TCL_READABLE;
185 if (condition & G_IO_OUT)
186 mask |= TCL_WRITABLE;
187 if (condition & (G_IO_ERR|G_IO_HUP|G_IO_NVAL))
188 mask |= TCL_EXCEPTION;
189
190 if (!(tfh->mask & (mask & ~tfh->pending)))
191 return TRUE;
192
193 tfh->pending |= mask;
194 fev = (struct tcl_file_event *)ckalloc(sizeof(struct tcl_file_event));
195 memset(fev, 0, sizeof(struct tcl_file_event));
196 fev->header.proc = tcl_file_event_callback;
197 fev->fd = tfh->fd;
198 Tcl_QueueEvent((Tcl_Event *)fev, TCL_QUEUE_TAIL);
199
200 Tcl_ServiceAll();
201
202 return TRUE;
203 }
204
205 int tcl_file_event_callback(Tcl_Event *event, int flags)
206 {
207 struct tcl_file_handler *tfh;
208 struct tcl_file_event *fev = (struct tcl_file_event *)event;
209 int mask;
210
211 if (!(flags & TCL_FILE_EVENTS)) {
212 return 0;
213 }
214
215 tfh = g_hash_table_lookup(tcl_file_handlers, (gpointer)fev->fd);
216 if (tfh == NULL)
217 return 1;
218
219 mask = tfh->mask & tfh->pending;
220 if (mask)
221 (*tfh->proc)(tfh->data, mask);
222 tfh->pending = 0;
223
224 return 1;
225 }