4 Copyright (C) 1999-2001 Timo Sirainen
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.
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.
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
25 #include "perl-core.h"
26 #include "perl-common.h"
29 PERL_SCRIPT_REC *script;
32 int once; /* run only once */
38 static GSList *perl_sources;
40 static void perl_source_ref(PERL_SOURCE_REC *rec)
45 static int perl_source_unref(PERL_SOURCE_REC *rec)
47 if (--rec->refcount != 0)
50 SvREFCNT_dec(rec->data);
51 SvREFCNT_dec(rec->func);
56 static void perl_source_destroy(PERL_SOURCE_REC *rec)
58 perl_sources = g_slist_remove(perl_sources, rec);
60 g_source_remove(rec->tag);
63 perl_source_unref(rec);
66 static int perl_source_event(PERL_SOURCE_REC *rec)
75 XPUSHs(sv_mortalcopy(rec->data));
79 retcount = perl_call_sv(rec->func, G_EVAL|G_SCALAR);
83 char *error = g_strdup(SvPV(ERRSV, PL_na));
84 signal_emit("script error", 2, rec->script, error);
88 if (perl_source_unref(rec) && rec->once)
89 perl_source_destroy(rec);
98 int perl_timeout_add(int msecs, SV *func, SV *data, int once)
100 PERL_SCRIPT_REC *script;
101 PERL_SOURCE_REC *rec;
104 pkg = perl_get_package();
105 script = perl_script_find_package(pkg);
106 g_return_val_if_fail(script != NULL, -1);
108 rec = g_new0(PERL_SOURCE_REC, 1);
109 perl_source_ref(rec);
112 rec->script = script;
113 rec->func = perl_func_sv_inc(func, pkg);
114 rec->data = SvREFCNT_inc(data);
115 rec->tag = g_timeout_add(msecs, (GSourceFunc) perl_source_event, rec);
117 perl_sources = g_slist_append(perl_sources, rec);
121 int perl_input_add(int source, int condition, SV *func, SV *data, int once)
123 PERL_SCRIPT_REC *script;
124 PERL_SOURCE_REC *rec;
128 pkg = perl_get_package();
129 script = perl_script_find_package(pkg);
130 g_return_val_if_fail(script != NULL, -1);
132 rec = g_new0(PERL_SOURCE_REC, 1);
133 perl_source_ref(rec);
137 rec->func = perl_func_sv_inc(func, pkg);
138 rec->data = SvREFCNT_inc(data);
140 channel = g_io_channel_unix_new(source);
141 rec->tag = g_input_add(channel, condition,
142 (GInputFunction) perl_source_event, rec);
143 g_io_channel_unref(channel);
145 perl_sources = g_slist_append(perl_sources, rec);
149 void perl_source_remove(int tag)
153 for (tmp = perl_sources; tmp != NULL; tmp = tmp->next) {
154 PERL_SOURCE_REC *rec = tmp->data;
156 if (rec->tag == tag) {
157 perl_source_destroy(rec);
163 void perl_source_remove_script(PERL_SCRIPT_REC *script)
167 for (tmp = perl_sources; tmp != NULL; tmp = next) {
168 PERL_SOURCE_REC *rec = tmp->data;
171 if (rec->script == script)
172 perl_source_destroy(rec);
176 void perl_sources_start(void)
181 void perl_sources_stop(void)
183 /* timeouts and input waits */
184 while (perl_sources != NULL)
185 perl_source_destroy(perl_sources->data);