* Copyright (C) 2003, 2004, 2006 Ethan Blanton <eblanton@cs.purdue.edu> * This file is dual-licensed under the two sets of terms below. You may * use, redistribute, or modify it pursuant to either the set of conditions * under "TERMS 1" or "TERMS 2", at your discretion. The DISCLAIMER * applies to both sets of terms. * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * 1. Redistributions of source code must contain the above copyright * notice and this comment block in their entirety. * 2. Redistributions in binary form must reproduce the above copyright * notice and the text of this comment block in their entirety in * the documentation and/or other materials provided with the * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * This file was developed for the Purple project. It inserts the Tcl * event loop into the glib2 event loop for the purposes of providing * Tcl bindings in a glib2 (e.g. Gtk2) program. To use it, simply * link it into your executable, include tcl_glib.h, and call the * function tcl_glib_init() before creating or using any Tcl * interpreters. Then go ahead and use Tcl, Tk, whatever to your * tcl_wait_for_event seems to have a bug that makes vwait not work so * well... I'm not sure why, yet, but I haven't put much time into * it. Hopefully I will figure it out soon. In the meantime, this * means that Tk's bgerror function (which is called when there is an * error in a callback function) causes some Bad Mojo -- you should * override it with a function that does not use Tk struct tcl_file_handler { static gboolean tcl_timer_pending; static GHashTable *tcl_file_handlers; static void tcl_set_timer(Tcl_Time *timePtr); static int tcl_wait_for_event(Tcl_Time *timePtr); static void tcl_create_file_handler(int fd, int mask, Tcl_FileProc *proc, ClientData data); static void tcl_delete_file_handler(int fd); static gboolean tcl_kick(gpointer data); static gboolean tcl_file_callback(GIOChannel *source, GIOCondition condition, gpointer data); static int tcl_file_event_callback(Tcl_Event *event, int flags); ClientData Tcl_InitNotifier() Tcl_NotifierProcs notifier; memset(¬ifier, 0, sizeof(notifier)); notifier.createFileHandlerProc = tcl_create_file_handler; notifier.deleteFileHandlerProc = tcl_delete_file_handler; notifier.setTimerProc = tcl_set_timer; notifier.waitForEventProc = tcl_wait_for_event; Tcl_SetNotifier(¬ifier); Tcl_SetServiceMode(TCL_SERVICE_ALL); tcl_timer_pending = FALSE; tcl_file_handlers = g_hash_table_new(g_direct_hash, g_direct_equal); static void tcl_set_timer(Tcl_Time *timePtr) g_source_remove(tcl_timer); tcl_timer_pending = FALSE; interval = timePtr->sec * 1000 + (timePtr->usec ? timePtr->usec / 1000 : 0); tcl_timer = g_timeout_add(interval, tcl_kick, NULL); tcl_timer_pending = TRUE; static int tcl_wait_for_event(Tcl_Time *timePtr) if (!timePtr || (timePtr->sec == 0 && timePtr->usec == 0)) { g_main_context_iteration(NULL, FALSE); g_main_context_iteration(NULL, TRUE); static void tcl_create_file_handler(int fd, int mask, Tcl_FileProc *proc, ClientData data) struct tcl_file_handler *tfh = g_new0(struct tcl_file_handler, 1); if (g_hash_table_lookup(tcl_file_handlers, GINT_TO_POINTER(fd))) tcl_delete_file_handler(fd); if (mask & TCL_EXCEPTION) cond |= G_IO_ERR|G_IO_HUP|G_IO_NVAL; channel = g_io_channel_unix_new(fd); tfh->source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond, tcl_file_callback, tfh, g_free); g_io_channel_unref(channel); g_hash_table_insert(tcl_file_handlers, GINT_TO_POINTER(fd), tfh); static void tcl_delete_file_handler(int fd) struct tcl_file_handler *tfh = g_hash_table_lookup(tcl_file_handlers, GINT_TO_POINTER(fd)); g_source_remove(tfh->source); g_hash_table_remove(tcl_file_handlers, GINT_TO_POINTER(fd)); static gboolean tcl_kick(gpointer data) tcl_timer_pending = FALSE; static gboolean tcl_file_callback(GIOChannel *source, GIOCondition condition, gpointer data) struct tcl_file_handler *tfh = data; struct tcl_file_event *fev; if (condition & G_IO_OUT) if (condition & (G_IO_ERR|G_IO_HUP|G_IO_NVAL)) if (!(tfh->mask & (mask & ~tfh->pending))) fev = (struct tcl_file_event *)ckalloc(sizeof(struct tcl_file_event)); memset(fev, 0, sizeof(struct tcl_file_event)); fev->header.proc = tcl_file_event_callback; Tcl_QueueEvent((Tcl_Event *)fev, TCL_QUEUE_TAIL); int tcl_file_event_callback(Tcl_Event *event, int flags) struct tcl_file_handler *tfh; struct tcl_file_event *fev = (struct tcl_file_event *)event; if (!(flags & TCL_FILE_EVENTS)) { tfh = g_hash_table_lookup(tcl_file_handlers, GINT_TO_POINTER(fev->fd)); mask = tfh->mask & tfh->pending; (*tfh->proc)(tfh->data, mask);