add blend mode tests
[swfdec.git] / swfdec / swfdec_external_interface.c
blobaf7a0083643c90e5852f7d1d34c2bff1f69c67b4
1 /* Swfdec
2 * Copyright (C) 2007 Pekka Lampila <pekka.lampila@iki.fi>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <string.h>
26 #include "swfdec_as_internal.h"
27 #include "swfdec_as_native_function.h"
28 #include "swfdec_as_strings.h"
29 #include "swfdec_debug.h"
30 #include "swfdec_player_internal.h"
31 #include "swfdec_player_scripting.h"
32 #include "swfdec_xml.h"
34 SWFDEC_AS_NATIVE (14, 0, swfdec_external_interface__initJS)
35 void
36 swfdec_external_interface__initJS (SwfdecAsContext *cx, SwfdecAsObject *object,
37 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
39 /* FIXME: call an init vfunc here? */
42 SWFDEC_AS_NATIVE (14, 1, swfdec_external_interface__objectID)
43 void
44 swfdec_external_interface__objectID (SwfdecAsContext *cx,
45 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
46 SwfdecAsValue *ret)
48 SwfdecPlayer *player = SWFDEC_PLAYER (cx);
49 SwfdecPlayerScripting *scripting = player->priv->scripting;
50 SwfdecPlayerScriptingClass *klass;
52 if (scripting == NULL) {
53 SWFDEC_AS_VALUE_SET_NULL (ret);
54 return;
56 klass = SWFDEC_PLAYER_SCRIPTING_GET_CLASS (scripting);
57 if (klass->js_get_id) {
58 char *s = klass->js_get_id (scripting, player);
59 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx, s));
60 } else {
61 SWFDEC_AS_VALUE_SET_NULL (ret);
65 SWFDEC_AS_NATIVE (14, 2, swfdec_external_interface__addCallback)
66 void
67 swfdec_external_interface__addCallback (SwfdecAsContext *cx,
68 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
69 SwfdecAsValue *ret)
71 SwfdecPlayerPrivate *priv = SWFDEC_PLAYER (cx)->priv;
72 SwfdecAsObject *fun;
73 const char *name;
75 SWFDEC_AS_VALUE_SET_BOOLEAN (ret, FALSE);
76 SWFDEC_AS_CHECK (0, NULL, "so", &name, &fun);
78 /* FIXME: do we allow setting if scripting is unsupported? */
79 if (!SWFDEC_IS_AS_FUNCTION (fun->relay))
80 return;
82 g_hash_table_insert (priv->scripting_callbacks, (gpointer) name, fun);
83 SWFDEC_AS_VALUE_SET_BOOLEAN (ret, TRUE);
86 SWFDEC_AS_NATIVE (14, 3, swfdec_external_interface__evalJS)
87 void
88 swfdec_external_interface__evalJS (SwfdecAsContext *cx, SwfdecAsObject *object,
89 guint argc, SwfdecAsValue *argv, SwfdecAsValue *ret)
91 SwfdecPlayer *player = SWFDEC_PLAYER (cx);
92 SwfdecPlayerScripting *scripting = player->priv->scripting;
93 SwfdecPlayerScriptingClass *klass;
94 const char *s;
96 SWFDEC_AS_VALUE_SET_NULL (ret);
97 SWFDEC_AS_CHECK (0, NULL, "s", &s);
98 if (scripting == NULL)
99 return;
101 klass = SWFDEC_PLAYER_SCRIPTING_GET_CLASS (scripting);
102 if (klass->js_call) {
103 char *t = klass->js_call (scripting, player, s);
104 if (t != NULL) {
105 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx, t));
110 SWFDEC_AS_NATIVE (14, 4, swfdec_external_interface__callOut)
111 void
112 swfdec_external_interface__callOut (SwfdecAsContext *cx,
113 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
114 SwfdecAsValue *ret)
116 SwfdecPlayer *player = SWFDEC_PLAYER (cx);
117 SwfdecPlayerScripting *scripting = player->priv->scripting;
118 SwfdecPlayerScriptingClass *klass;
119 const char *s;
121 SWFDEC_AS_VALUE_SET_NULL (ret);
122 SWFDEC_AS_CHECK (0, NULL, "s", &s);
123 if (scripting == NULL)
124 return;
125 klass = SWFDEC_PLAYER_SCRIPTING_GET_CLASS (scripting);
126 if (klass->xml_call) {
127 char *t = klass->xml_call (scripting, player, s);
128 if (t != NULL) {
129 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx, t));
134 SWFDEC_AS_NATIVE (14, 5, swfdec_external_interface__escapeXML)
135 void
136 swfdec_external_interface__escapeXML (SwfdecAsContext *cx,
137 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
138 SwfdecAsValue *ret)
140 const char *s;
142 SWFDEC_AS_VALUE_SET_NULL (ret);
143 SWFDEC_AS_CHECK (0, NULL, "s", &s);
144 if (s == SWFDEC_AS_STR_EMPTY)
145 return;
147 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx, swfdec_xml_escape (s)));
150 SWFDEC_AS_NATIVE (14, 6, swfdec_external_interface__unescapeXML)
151 void
152 swfdec_external_interface__unescapeXML (SwfdecAsContext *cx,
153 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
154 SwfdecAsValue *ret)
156 const char *s;
158 SWFDEC_AS_VALUE_SET_NULL (ret);
159 SWFDEC_AS_CHECK (0, NULL, "s", &s);
160 if (s == SWFDEC_AS_STR_EMPTY)
161 return;
163 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx,
164 swfdec_xml_unescape_len (cx, s, strlen (s), FALSE)));
167 SWFDEC_AS_NATIVE (14, 7, swfdec_external_interface__jsQuoteString)
168 void
169 swfdec_external_interface__jsQuoteString (SwfdecAsContext *cx,
170 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
171 SwfdecAsValue *ret)
173 const char *s;
174 GString *str;
175 size_t len;
177 SWFDEC_AS_VALUE_SET_NULL (ret);
178 SWFDEC_AS_CHECK (0, NULL, "s", &s);
179 if (s == SWFDEC_AS_STR_EMPTY)
180 return;
182 str = g_string_new ("");
183 do {
184 /* Yay, we don't escape backslashes! */
185 len = strcspn (s, "\n\r\"");
186 g_string_append_len (str, s, len);
187 s += len;
188 if (*s == '\0')
189 break;
190 g_string_append_c (str, '\\');
191 switch (*s) {
192 case '\n':
193 g_string_append_c (str, 'n');
194 break;
195 case '\r':
196 g_string_append_c (str, 'r');
197 break;
198 case '"':
199 g_string_append_c (str, '"');
200 break;
201 default:
202 g_assert_not_reached ();
203 break;
205 s++;
206 } while (TRUE);
207 SWFDEC_AS_VALUE_SET_STRING (ret, swfdec_as_context_give_string (cx, g_string_free (str, FALSE)));
210 SWFDEC_AS_NATIVE (14, 100, swfdec_external_interface_get_available)
211 void
212 swfdec_external_interface_get_available (SwfdecAsContext *cx,
213 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
214 SwfdecAsValue *ret)
216 SWFDEC_AS_VALUE_SET_BOOLEAN (ret, SWFDEC_PLAYER (cx)->priv->scripting != NULL);
219 SWFDEC_AS_NATIVE (14, 101, swfdec_external_interface_set_available)
220 void
221 swfdec_external_interface_set_available (SwfdecAsContext *cx,
222 SwfdecAsObject *object, guint argc, SwfdecAsValue *argv,
223 SwfdecAsValue *ret)
225 /* read-only property */