annotate plugins/tcl/tcl_glib.c @ 12679:b18738b03b35

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