Fri, 09 Apr 2004 05:29:37 +0000
[gaim-migrate @ 9375]
wow.
| 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 | ||
|
7831
54076c9af6ad
[gaim-migrate @ 8483]
Herman Bloggs <herman@bluedigits.com>
parents:
7822
diff
changeset
|
73 | #undef Tcl_InitNotifier |
|
54076c9af6ad
[gaim-migrate @ 8483]
Herman Bloggs <herman@bluedigits.com>
parents:
7822
diff
changeset
|
74 | |
| 6694 | 75 | ClientData Tcl_InitNotifier() |
| 76 | { | |
| 77 | return NULL; | |
| 78 | } | |
| 79 | ||
| 80 | void tcl_glib_init () | |
| 81 | { | |
| 82 | Tcl_NotifierProcs notifier; | |
| 83 | ||
| 7822 | 84 | memset(¬ifier, 0, sizeof(notifier)); |
| 85 | ||
| 6694 | 86 | notifier.createFileHandlerProc = tcl_create_file_handler; |
| 87 | notifier.deleteFileHandlerProc = tcl_delete_file_handler; | |
| 88 | notifier.setTimerProc = tcl_set_timer; | |
| 89 | notifier.waitForEventProc = tcl_wait_for_event; | |
| 90 | ||
| 91 | Tcl_SetNotifier(¬ifier); | |
| 8116 | 92 | Tcl_SetServiceMode(TCL_SERVICE_ALL); |
| 6694 | 93 | |
| 94 | tcl_timer_pending = FALSE; | |
| 95 | tcl_file_handlers = g_hash_table_new(g_direct_hash, g_direct_equal); | |
| 96 | } | |
| 97 | ||
| 98 | static void tcl_set_timer(Tcl_Time *timePtr) | |
| 99 | { | |
| 100 | guint interval; | |
| 101 | ||
| 102 | if (tcl_timer_pending) | |
| 103 | g_source_remove(tcl_timer); | |
| 104 | ||
| 105 | if (timePtr == NULL) { | |
| 106 | tcl_timer_pending = FALSE; | |
| 107 | return; | |
| 108 | } | |
| 109 | ||
| 110 | interval = timePtr->sec * 1000 + (timePtr->usec ? timePtr->usec / 1000 : 0); | |
| 111 | tcl_timer = g_timeout_add(interval, tcl_kick, NULL); | |
| 112 | tcl_timer_pending = TRUE; | |
| 113 | } | |
| 114 | ||
| 115 | static int tcl_wait_for_event(Tcl_Time *timePtr) | |
| 116 | { | |
| 117 | if (!timePtr || (timePtr->sec == 0 && timePtr->usec == 0)) { | |
| 118 | g_main_context_iteration(NULL, FALSE); | |
| 119 | return 1; | |
| 120 | } else { | |
| 121 | tcl_set_timer(timePtr); | |
| 122 | } | |
| 123 | ||
| 124 | g_main_context_iteration(NULL, TRUE); | |
| 125 | ||
| 126 | return 1; | |
| 127 | } | |
| 128 | ||
| 129 | static void tcl_create_file_handler(int fd, int mask, Tcl_FileProc *proc, ClientData data) | |
| 130 | { | |
| 131 | struct tcl_file_handler *tfh = g_new0(struct tcl_file_handler, 1); | |
| 132 | GIOChannel *channel; | |
| 133 | GIOCondition cond = 0; | |
| 134 | ||
| 135 | if (g_hash_table_lookup(tcl_file_handlers, (gpointer)fd)) | |
| 136 | tcl_delete_file_handler(fd); | |
| 137 | ||
| 138 | if (mask & TCL_READABLE) | |
| 139 | cond |= G_IO_IN; | |
| 140 | if (mask & TCL_WRITABLE) | |
| 141 | cond |= G_IO_OUT; | |
| 142 | if (mask & TCL_EXCEPTION) | |
| 143 | cond |= G_IO_ERR|G_IO_HUP|G_IO_NVAL; | |
| 144 | ||
| 145 | tfh->fd = fd; | |
| 146 | tfh->mask = mask; | |
| 147 | tfh->proc = proc; | |
| 148 | tfh->data = data; | |
| 149 | ||
| 150 | channel = g_io_channel_unix_new(fd); | |
| 151 | tfh->source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, tcl_file_callback, tfh, g_free); | |
| 152 | g_io_channel_unref(channel); | |
| 153 | ||
| 154 | g_hash_table_insert(tcl_file_handlers, (gpointer)fd, tfh); | |
| 155 | ||
| 156 | Tcl_ServiceAll(); | |
| 157 | } | |
| 158 | ||
| 159 | static void tcl_delete_file_handler(int fd) | |
| 160 | { | |
| 161 | struct tcl_file_handler *tfh = g_hash_table_lookup(tcl_file_handlers, (gpointer)fd); | |
| 162 | ||
| 163 | if (tfh == NULL) | |
| 164 | return; | |
| 165 | ||
| 166 | g_source_remove(tfh->source); | |
| 167 | g_hash_table_remove(tcl_file_handlers, (gpointer)fd); | |
| 168 | ||
| 169 | Tcl_ServiceAll(); | |
| 170 | } | |
| 171 | ||
| 172 | static gboolean tcl_kick(gpointer data) | |
| 173 | { | |
| 174 | tcl_timer_pending = FALSE; | |
| 175 | ||
| 176 | Tcl_ServiceAll(); | |
| 177 | ||
| 178 | return FALSE; | |
| 179 | } | |
| 180 | ||
| 181 | static gboolean tcl_file_callback(GIOChannel *source, GIOCondition condition, gpointer data) | |
| 182 | { | |
| 183 | struct tcl_file_handler *tfh = data; | |
| 184 | struct tcl_file_event *fev; | |
| 185 | int mask = 0; | |
| 186 | ||
| 187 | if (condition & G_IO_IN) | |
| 188 | mask |= TCL_READABLE; | |
| 189 | if (condition & G_IO_OUT) | |
| 190 | mask |= TCL_WRITABLE; | |
| 191 | if (condition & (G_IO_ERR|G_IO_HUP|G_IO_NVAL)) | |
| 192 | mask |= TCL_EXCEPTION; | |
| 193 | ||
| 194 | if (!(tfh->mask & (mask & ~tfh->pending))) | |
| 195 | return TRUE; | |
| 196 | ||
| 197 | tfh->pending |= mask; | |
| 198 | fev = (struct tcl_file_event *)ckalloc(sizeof(struct tcl_file_event)); | |
| 199 | memset(fev, 0, sizeof(struct tcl_file_event)); | |
| 200 | fev->header.proc = tcl_file_event_callback; | |
| 201 | fev->fd = tfh->fd; | |
| 202 | Tcl_QueueEvent((Tcl_Event *)fev, TCL_QUEUE_TAIL); | |
| 203 | ||
| 204 | Tcl_ServiceAll(); | |
| 205 | ||
| 206 | return TRUE; | |
| 207 | } | |
| 208 | ||
| 209 | int tcl_file_event_callback(Tcl_Event *event, int flags) | |
| 210 | { | |
| 211 | struct tcl_file_handler *tfh; | |
| 212 | struct tcl_file_event *fev = (struct tcl_file_event *)event; | |
| 213 | int mask; | |
| 214 | ||
| 215 | if (!(flags & TCL_FILE_EVENTS)) { | |
| 216 | return 0; | |
| 217 | } | |
| 218 | ||
| 219 | tfh = g_hash_table_lookup(tcl_file_handlers, (gpointer)fev->fd); | |
| 220 | if (tfh == NULL) | |
| 221 | return 1; | |
| 222 | ||
| 223 | mask = tfh->mask & tfh->pending; | |
| 224 | if (mask) | |
| 225 | (*tfh->proc)(tfh->data, mask); | |
| 226 | tfh->pending = 0; | |
| 227 | ||
| 228 | return 1; | |
| 229 | } |