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"
27 #include "perl-sources.h"
30 PERL_SCRIPT_REC *script;
33 int once; /* run only once */
39 static GSList *perl_sources;
41 static void perl_source_ref(PERL_SOURCE_REC *rec)
46 static int perl_source_unref(PERL_SOURCE_REC *rec)
48 if (--rec->refcount != 0)
51 SvREFCNT_dec(rec->data);
52 SvREFCNT_dec(rec->func);
57 static void perl_source_destroy(PERL_SOURCE_REC *rec)
59 perl_sources = g_slist_remove(perl_sources, rec);
61 g_source_remove(rec->tag);
64 perl_source_unref(rec);
67 static int perl_source_event(PERL_SOURCE_REC *rec)
76 XPUSHs(sv_mortalcopy(rec->data));
80 retcount = perl_call_sv(rec->func, G_EVAL|G_SCALAR);
84 char *error = g_strdup(SvPV(ERRSV, PL_na));
85 signal_emit("script error", 2, rec->script, error);
89 if (perl_source_unref(rec) && rec->once)
90 perl_source_destroy(rec);
99 int perl_timeout_add(int msecs, SV *func, SV *data, int once)
101 PERL_SCRIPT_REC *script;
102 PERL_SOURCE_REC *rec;
105 pkg = perl_get_package();
106 script = perl_script_find_package(pkg);
107 g_return_val_if_fail(script != NULL, -1);
109 rec = g_new0(PERL_SOURCE_REC, 1);
110 perl_source_ref(rec);
113 rec->script = script;
114 rec->func = perl_func_sv_inc(func, pkg);
115 rec->data = SvREFCNT_inc(data);
116 rec->tag = g_timeout_add(msecs, (GSourceFunc) perl_source_event, rec);
118 perl_sources = g_slist_append(perl_sources, rec);
122 int perl_input_add(int source, int condition, SV *func, SV *data, int once)
124 PERL_SCRIPT_REC *script;
125 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 channel = g_io_channel_unix_new(source);
142 rec->tag = g_input_add(channel, condition,
143 (GInputFunction) perl_source_event, rec);
144 g_io_channel_unref(channel);
146 perl_sources = g_slist_append(perl_sources, rec);
150 void perl_source_remove(int tag)
154 for (tmp = perl_sources; tmp != NULL; tmp = tmp->next) {
155 PERL_SOURCE_REC *rec = tmp->data;
157 if (rec->tag == tag) {
158 perl_source_destroy(rec);
164 void perl_source_remove_script(PERL_SCRIPT_REC *script)
168 for (tmp = perl_sources; tmp != NULL; tmp = next) {
169 PERL_SOURCE_REC *rec = tmp->data;
172 if (rec->script == script)
173 perl_source_destroy(rec);
177 void perl_sources_start(void)
182 void perl_sources_stop(void)
184 /* timeouts and input waits */
185 while (perl_sources != NULL)
186 perl_source_destroy(perl_sources->data);