summaryrefslogtreecommitdiff
path: root/swfdec/swfdec_player_as.c
blob: 8051be31867b7e680e6d21bc0b065cca99b5ba0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* Swfdec
 * Copyright (C) 2006-2007 Benjamin Otte <otte@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* NB: include this first, it redefines SWFDEC_AS_NATIVE */
#include "swfdec_asnative.h"

#include "swfdec_player_internal.h"
#include "swfdec_as_function.h"
#include "swfdec_as_internal.h"
#include "swfdec_as_native_function.h"
#include "swfdec_as_object.h"
#include "swfdec_as_strings.h"
#include "swfdec_debug.h"
#include "swfdec_internal.h"
#include "swfdec_interval.h"
#include "swfdec_as_frame_internal.h"

/*** INTERVALS ***/

static void
swfdec_player_do_set_interval (gboolean repeat, SwfdecAsContext *cx, guint argc, 
    SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  SwfdecPlayer *player = SWFDEC_PLAYER (cx);
  SwfdecAsObject *object;
  guint id, msecs;
#define MIN_INTERVAL_TIME 10

  if (argc < 2) {
    SWFDEC_WARNING ("setInterval needs at least 2 arguments");
    return;
  }

  if (!SWFDEC_AS_VALUE_IS_COMPOSITE (argv[0]) ||
      (object = SWFDEC_AS_VALUE_GET_COMPOSITE (argv[0])) == NULL) {
    SWFDEC_WARNING ("first argument to setInterval is not an object");
    return;
  }
  if (SWFDEC_IS_AS_FUNCTION (object->relay)) {
    msecs = swfdec_as_value_to_integer (cx, argv[1]);
    if (msecs < MIN_INTERVAL_TIME) {
      SWFDEC_INFO ("interval duration is %u, making it %u msecs", msecs, MIN_INTERVAL_TIME);
      msecs = MIN_INTERVAL_TIME;
    }
    id = swfdec_interval_new_function (player, msecs, repeat, 
	SWFDEC_AS_FUNCTION (object->relay), argc - 2, &argv[2]);
  } else {
    const char *name;
    if (argc < 3) {
      SWFDEC_WARNING ("setInterval needs 3 arguments when not called with function");
      return;
    }
    name = swfdec_as_value_to_string (cx, argv[1]);
    msecs = swfdec_as_value_to_integer (cx, argv[2]);
    if (msecs < MIN_INTERVAL_TIME) {
      SWFDEC_INFO ("interval duration is %u, making it %u msecs", msecs, MIN_INTERVAL_TIME);
      msecs = MIN_INTERVAL_TIME;
    }
    id = swfdec_interval_new_object (player, msecs, repeat, &argv[0], name, argc - 3, &argv[3]);
  }
  *rval = swfdec_as_value_from_integer (cx, id);
}

SWFDEC_AS_NATIVE (2, 0, swfdec_player_ASnew)
void
swfdec_player_ASnew (SwfdecAsContext *cx, SwfdecAsObject *obj, 
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  g_return_if_fail (cx->frame->next != NULL);

  SWFDEC_AS_VALUE_SET_BOOLEAN (rval, cx->frame->next->construct);
}

SWFDEC_AS_NATIVE (250, 0, swfdec_player_setInterval)
void
swfdec_player_setInterval (SwfdecAsContext *cx, SwfdecAsObject *obj, 
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  swfdec_player_do_set_interval (TRUE, cx, argc, argv, rval);
}

SWFDEC_AS_NATIVE (250, 2, swfdec_player_setTimeout)
void
swfdec_player_setTimeout (SwfdecAsContext *cx, SwfdecAsObject *obj, 
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  swfdec_player_do_set_interval (FALSE, cx, argc, argv, rval);
}

SWFDEC_AS_NATIVE (250, 1, swfdec_player_clearInterval)
void
swfdec_player_clearInterval (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  SwfdecPlayer *player = SWFDEC_PLAYER (cx);
  guint id;

  SWFDEC_AS_CHECK (0, NULL, "i", &id);
  
  swfdec_interval_remove (player, id);
}

/*** VARIOUS ***/

SWFDEC_AS_NATIVE (100, 4, swfdec_player_trace)
void
swfdec_player_trace (SwfdecAsContext *cx, SwfdecAsObject *obj,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  SWFDEC_FIXME ("Is _global.trace supposed to do something?");
}

SWFDEC_AS_NATIVE (9, 0, swfdec_player_updateAfterEvent)
void
swfdec_player_updateAfterEvent (SwfdecAsContext *cx, SwfdecAsObject *obj,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  SWFDEC_STUB ("updateAfterEvent");
}

SWFDEC_AS_NATIVE (1021, 1, swfdec_player_showRedrawRegions)
void
swfdec_player_showRedrawRegions (SwfdecAsContext *cx, SwfdecAsObject *obj,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  SWFDEC_STUB ("showRedrawRegions");
}

static void
swfdec_player_enableDebugConsole (SwfdecAsContext *cx, SwfdecAsObject *obj,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  SWFDEC_STUB ("enableDebugConsole");
}

static void
swfdec_player_do_nothing (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *retval)
{
}

static SwfdecAsFunction *
swfdec_get_asnative (SwfdecAsContext *cx, guint x, guint y)
{
  gboolean x_exists;
  guint i;

  x_exists = FALSE;
  for (i = 0; native_funcs[i].func != NULL; i++) {
    if (native_funcs[i].x == x)
      x_exists = TRUE;
    if (native_funcs[i].x == x && native_funcs[i].y == y) {
      SwfdecAsFunction *fun = swfdec_as_native_function_new (cx, native_funcs[i].name,
	  native_funcs[i].func);
      return fun;
    }
  }
  SWFDEC_WARNING ("no ASnative (%u, %u)", x, y);
  if (x_exists) {
    SwfdecAsFunction *func;
    char *name = g_strdup_printf ("ASnative (%u, %u)", x, y);
    func = swfdec_as_native_function_new (cx, name, swfdec_player_do_nothing);
    g_free (name);
    return func;
  } else {
    return NULL;
  }
}

// same as ASnative, but also sets prototype
static void
swfdec_player_ASconstructor (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  SwfdecAsValue val;
  SwfdecAsObject *proto, *func_object;
  SwfdecAsFunction *func;
  guint x, y;

  SWFDEC_AS_CHECK (0, NULL, "ii", &x, &y);

  func = swfdec_get_asnative (cx, x, y);
  if (func) {
    proto = swfdec_as_object_new (cx, SWFDEC_AS_STR_Object, NULL);
    func_object = swfdec_as_relay_get_as_object (SWFDEC_AS_RELAY (func));

    SWFDEC_AS_VALUE_SET_OBJECT (&val, proto);
    swfdec_as_object_set_variable_and_flags (func_object,
	SWFDEC_AS_STR_prototype, &val,
	SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);

    SWFDEC_AS_VALUE_SET_OBJECT (&val, func_object);
    swfdec_as_object_set_variable_and_flags (proto, SWFDEC_AS_STR_constructor,
	&val, SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);

    SWFDEC_AS_VALUE_SET_OBJECT (rval, func_object);
  }
}

static void
swfdec_player_ASnative (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  SwfdecAsFunction *func;
  guint x, y;

  SWFDEC_AS_CHECK (0, NULL, "ii", &x, &y);

  func = swfdec_get_asnative (cx, x, y);
  if (func) {
    SWFDEC_AS_VALUE_SET_OBJECT (rval, swfdec_as_relay_get_as_object SWFDEC_AS_RELAY (func));
  }
}

SWFDEC_AS_NATIVE (4, 0, ASSetNative)
void
ASSetNative (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  SwfdecAsFunction *function;
  SwfdecAsObject *target;
  SwfdecAsValue val;
  SwfdecAsVariableFlag flags;
  const char *s;
  char **names;
  guint i, x, y;

  SWFDEC_AS_CHECK (0, NULL, "ois", &target, &x, &s);

  if (argc > 3)
    y = swfdec_as_value_to_integer (cx, argv[3]);
  else
    y = 0;
  names = g_strsplit (s, ",", -1);
  for (i = 0; names[i]; i++) {
    s = names[i];
    flags = 0;
    if (s[0] == '6') {
      flags |= SWFDEC_AS_VARIABLE_VERSION_6_UP;
      s++;
    } else if (s[0] == '7') {
      flags |= SWFDEC_AS_VARIABLE_VERSION_7_UP;
      s++;
    } else if (s[0] == '8') {
      flags |= SWFDEC_AS_VARIABLE_VERSION_8_UP;
      s++;
    } else if (s[0] == '9') {
      flags |= SWFDEC_AS_VARIABLE_VERSION_9_UP;
      s++;
    }
    function = swfdec_get_asnative (cx, x, y);
    if (function == NULL)
      break;
    SWFDEC_AS_VALUE_SET_OBJECT (&val, swfdec_as_relay_get_as_object (SWFDEC_AS_RELAY (function)));
    swfdec_as_object_set_variable_and_flags (target,
	swfdec_as_context_get_string (cx, s), &val, flags);
    y++;
  }
  g_strfreev (names);
}

SWFDEC_AS_NATIVE (4, 1, ASSetNativeAccessor)
void
ASSetNativeAccessor (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  SwfdecAsFunction *get, *set;
  SwfdecAsObject *target;
  SwfdecAsVariableFlag flags;
  const char *s;
  char **names;
  guint i, x, y = 0;

  SWFDEC_AS_CHECK (0, NULL, "ois|i", &target, &x, &s, &y);

  names = g_strsplit (s, ",", -1);
  for (i = 0; names[i]; i++) {
    s = names[i];
    flags = 0;
    if (s[0] == '6') {
      flags |= SWFDEC_AS_VARIABLE_VERSION_6_UP;
      s++;
    } else if (s[0] == '7') {
      flags |= SWFDEC_AS_VARIABLE_VERSION_7_UP;
      s++;
    } else if (s[0] == '8') {
      flags |= SWFDEC_AS_VARIABLE_VERSION_8_UP;
      s++;
    } else if (s[0] == '9') {
      flags |= SWFDEC_AS_VARIABLE_VERSION_9_UP;
      s++;
    }
    get = swfdec_get_asnative (cx, x, y++);
    set = swfdec_get_asnative (cx, x, y++);
    if (get == NULL) {
      SWFDEC_ERROR ("no getter for %s", s);
      break;
    }
    swfdec_as_object_add_variable (target, swfdec_as_context_get_string (cx, s),
	get, set, flags);
  }
  g_strfreev (names);
}

SWFDEC_AS_NATIVE (101, 8, swfdec_player_object_registerClass)
void
swfdec_player_object_registerClass (SwfdecAsContext *cx, SwfdecAsObject *object,
    guint argc, SwfdecAsValue *argv, SwfdecAsValue *rval)
{
  const char *name;

  SWFDEC_AS_CHECK (0, NULL, "s", &name);

  if (argc < 2 || !SWFDEC_AS_VALUE_IS_OBJECT (argv[1])) {
    SWFDEC_AS_VALUE_SET_BOOLEAN (rval, FALSE);
    return;
  }
  
  swfdec_player_set_export_class (SWFDEC_PLAYER (cx), name, 
      SWFDEC_AS_VALUE_GET_OBJECT (argv[1]));
  SWFDEC_AS_VALUE_SET_BOOLEAN (rval, TRUE);
}

/* This is ran at the beginning of swfdec_as_context_startup.
 * Yes, this is a hack */
void
swfdec_player_preinit_global (SwfdecAsContext *context)
{
  SwfdecAsObject *o;
  SwfdecAsFunction *f;
  SwfdecAsValue val;

  f = swfdec_as_native_function_new_bare (context,
	SWFDEC_AS_STR_ASnative, swfdec_player_ASnative);
  o = swfdec_as_relay_get_as_object (SWFDEC_AS_RELAY (f));
  SWFDEC_AS_VALUE_SET_OBJECT (&val, o);
  swfdec_as_object_set_variable_and_flags (context->global, SWFDEC_AS_STR_ASnative,
      &val, SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);

  f = swfdec_as_native_function_new_bare (context,
	SWFDEC_AS_STR_ASconstructor, swfdec_player_ASconstructor);
  o = swfdec_as_relay_get_as_object (SWFDEC_AS_RELAY (f));
  SWFDEC_AS_VALUE_SET_OBJECT (&val, o);
  swfdec_as_object_set_variable_and_flags (context->global, SWFDEC_AS_STR_ASconstructor,
      &val, SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);

  f = swfdec_as_native_function_new_bare (context,
	SWFDEC_AS_STR_enableDebugConsole, swfdec_player_enableDebugConsole);
  o = swfdec_as_relay_get_as_object (SWFDEC_AS_RELAY (f));
  SWFDEC_AS_VALUE_SET_OBJECT (&val, o);
  swfdec_as_object_set_variable_and_flags (context->global, SWFDEC_AS_STR_enableDebugConsole,
      &val, SWFDEC_AS_VARIABLE_HIDDEN | SWFDEC_AS_VARIABLE_PERMANENT);
}