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;
37 static GSList *perl_sources;
39 static void perl_source_ref(PERL_SOURCE_REC *rec)
44 static void perl_source_unref(PERL_SOURCE_REC *rec)
46 if (--rec->refcount != 0)
49 SvREFCNT_dec(rec->data);
50 SvREFCNT_dec(rec->func);
54 static void perl_source_destroy(PERL_SOURCE_REC *rec)
56 perl_sources = g_slist_remove(perl_sources, rec);
58 g_source_remove(rec->tag);
61 perl_source_unref(rec);
64 static int perl_source_event(PERL_SOURCE_REC *rec)
72 XPUSHs(sv_mortalcopy(rec->data));
76 perl_call_sv(rec->func, G_EVAL|G_DISCARD);
80 char *error = g_strdup(SvPV(ERRSV, PL_na));
81 signal_emit("script error", 2, rec->script, error);
84 perl_source_unref(rec);
93 int perl_timeout_add(int msecs, SV *func, SV *data)
95 PERL_SCRIPT_REC *script;
99 pkg = perl_get_package();
100 script = perl_script_find_package(pkg);
101 g_return_val_if_fail(script != NULL, -1);
103 rec = g_new0(PERL_SOURCE_REC, 1);
104 perl_source_ref(rec);
106 rec->script = script;
107 rec->func = perl_func_sv_inc(func, pkg);
108 rec->data = SvREFCNT_inc(data);
109 rec->tag = g_timeout_add(msecs, (GSourceFunc) perl_source_event, rec);
111 perl_sources = g_slist_append(perl_sources, rec);
115 int perl_input_add(int source, int condition, SV *func, SV *data)
117 PERL_SCRIPT_REC *script;
118 PERL_SOURCE_REC *rec;
122 pkg = perl_get_package();
123 script = perl_script_find_package(pkg);
124 g_return_val_if_fail(script != NULL, -1);
126 rec = g_new0(PERL_SOURCE_REC, 1);
127 perl_source_ref(rec);
130 rec->func = perl_func_sv_inc(func, pkg);
131 rec->data = SvREFCNT_inc(data);
133 channel = g_io_channel_unix_new(source);
134 rec->tag = g_input_add(channel, condition,
135 (GInputFunction) perl_source_event, rec);
136 g_io_channel_unref(channel);
138 perl_sources = g_slist_append(perl_sources, rec);
142 void perl_source_remove(int tag)
146 for (tmp = perl_sources; tmp != NULL; tmp = tmp->next) {
147 PERL_SOURCE_REC *rec = tmp->data;
149 if (rec->tag == tag) {
150 perl_source_destroy(rec);
156 void perl_source_remove_script(PERL_SCRIPT_REC *script)
160 for (tmp = perl_sources; tmp != NULL; tmp = next) {
161 PERL_SOURCE_REC *rec = tmp->data;
164 if (rec->script == script)
165 perl_source_destroy(rec);
169 void perl_sources_start(void)
174 void perl_sources_stop(void)
176 /* timeouts and input waits */
177 while (perl_sources != NULL)
178 perl_source_destroy(perl_sources->data);