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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "perl-core.h"
26 #include "perl-common.h"
27 #include "perl-sources.h"
31 PERL_SCRIPT_REC *script;
34 int once; /* run only once */
40 static GSList *perl_sources;
42 static void perl_source_ref(PERL_SOURCE_REC *rec)
47 static int perl_source_unref(PERL_SOURCE_REC *rec)
49 if (--rec->refcount != 0)
52 SvREFCNT_dec(rec->data);
53 SvREFCNT_dec(rec->func);
58 static void perl_source_destroy(PERL_SOURCE_REC *rec)
60 perl_sources = g_slist_remove(perl_sources, rec);
62 g_source_remove(rec->tag);
65 perl_source_unref(rec);
68 static int perl_source_event(PERL_SOURCE_REC *rec)
77 XPUSHs(sv_mortalcopy(rec->data));
81 retcount = perl_call_sv(rec->func, G_EVAL|G_SCALAR);
85 char *error = g_strdup(SvPV(ERRSV, PL_na));
86 signal_emit("script error", 2, rec->script, error);
90 if (perl_source_unref(rec) && rec->once)
91 perl_source_destroy(rec);
100 int perl_timeout_add(int msecs, SV *func, SV *data, int once)
102 PERL_SCRIPT_REC *script;
103 PERL_SOURCE_REC *rec;
106 pkg = perl_get_package();
107 script = perl_script_find_package(pkg);
108 g_return_val_if_fail(script != NULL, -1);
110 rec = g_new0(PERL_SOURCE_REC, 1);
111 perl_source_ref(rec);
114 rec->script = script;
115 rec->func = perl_func_sv_inc(func, pkg);
116 rec->data = SvREFCNT_inc(data);
117 rec->tag = g_timeout_add(msecs, (GSourceFunc) perl_source_event, rec);
119 perl_sources = g_slist_append(perl_sources, rec);
123 int perl_input_add(int source, int condition, SV *func, SV *data, int once)
125 PERL_SCRIPT_REC *script;
126 PERL_SOURCE_REC *rec;
129 pkg = perl_get_package();
130 script = perl_script_find_package(pkg);
131 g_return_val_if_fail(script != NULL, -1);
133 rec = g_new0(PERL_SOURCE_REC, 1);
134 perl_source_ref(rec);
138 rec->func = perl_func_sv_inc(func, pkg);
139 rec->data = SvREFCNT_inc(data);
141 rec->tag = g_input_add_poll(source, G_PRIORITY_DEFAULT, condition,
142 (GInputFunction) perl_source_event, rec);
144 perl_sources = g_slist_append(perl_sources, rec);
148 void perl_source_remove(int tag)
152 for (tmp = perl_sources; tmp != NULL; tmp = tmp->next) {
153 PERL_SOURCE_REC *rec = tmp->data;
155 if (rec->tag == tag) {
156 perl_source_destroy(rec);
162 void perl_source_remove_script(PERL_SCRIPT_REC *script)
166 for (tmp = perl_sources; tmp != NULL; tmp = next) {
167 PERL_SOURCE_REC *rec = tmp->data;
170 if (rec->script == script)
171 perl_source_destroy(rec);
175 void perl_sources_start(void)
180 void perl_sources_stop(void)
182 /* timeouts and input waits */
183 while (perl_sources != NULL)
184 perl_source_destroy(perl_sources->data);