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