clutter-script-parser.c 64.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Copyright (C) 2006, 2007, 2008 OpenedHand Ltd
 * Copyright (C) 2009 Intel Corportation
 *
 * 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 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
20 21
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
22 23 24 25 26
 * Original author:
 *
 *      Emmanuele Bassi <ebassi@linux.intel.com>
 */

27
#ifdef HAVE_CONFIG_H
28
#include "clutter-build-config.h"
29 30 31 32 33 34
#endif

#include <string.h>
#include <stdlib.h>

#include <glib.h>
35
#include <gmodule.h>
36

37 38
#define CLUTTER_DISABLE_DEPRECATION_WARNINGS
#include "deprecated/clutter-container.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
39
#include "deprecated/clutter-alpha.h"
40

41
#include "clutter-actor.h"
42 43
#include "clutter-debug.h"
#include "clutter-enum-types.h"
44 45 46 47 48

#include "clutter-script.h"
#include "clutter-script-private.h"
#include "clutter-scriptable.h"

49 50
#include "clutter-stage-manager.h"

51 52
#include "clutter-private.h"

53 54 55 56
static void clutter_script_parser_object_end (JsonParser *parser,
                                              JsonObject *object);
static void clutter_script_parser_parse_end  (JsonParser *parser);

57 58
#define clutter_script_parser_get_type  _clutter_script_parser_get_type

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
G_DEFINE_TYPE (ClutterScriptParser, clutter_script_parser, JSON_TYPE_PARSER);

static void
clutter_script_parser_class_init (ClutterScriptParserClass *klass)
{
  JsonParserClass *parser_class = JSON_PARSER_CLASS (klass);

  parser_class->object_end = clutter_script_parser_object_end;
  parser_class->parse_end = clutter_script_parser_parse_end;
}

static void
clutter_script_parser_init (ClutterScriptParser *parser)
{
}

75
GType
76
_clutter_script_get_type_from_symbol (const gchar *symbol)
77 78 79 80 81 82
{
  static GModule *module = NULL;
  GTypeGetFunc func;
  GType gtype = G_TYPE_INVALID;

  if (!module)
83
    module = g_module_open (NULL, 0);
84 85 86 87 88 89 90 91
  
  if (g_module_symbol (module, symbol, (gpointer)&func))
    gtype = func ();
  
  return gtype;
}

GType
92
_clutter_script_get_type_from_class (const gchar *name)
93 94
{
  static GModule *module = NULL;
95
  GString *symbol_name = g_string_sized_new (64);
96
  GType gtype = G_TYPE_INVALID;
97 98 99
  GTypeGetFunc func;
  gchar *symbol;
  gint i;
100

101
  if (G_UNLIKELY (!module))
102
    module = g_module_open (NULL, 0);
103 104 105
  
  for (i = 0; name[i] != '\0'; i++)
    {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      gchar c = name[i];

      /* the standard naming policy for GObject-based libraries
       * is:
       *
       *   NAME := INITIAL_WORD WORD+
       *   INITIAL_WORD := [A-Z][a-z0-9]*
       *   WORD := [A-Z]{1,2}[a-z0-9]+ | [A-Z]{2,}
       *
       * for instance:
       *
       *   GString -> g_string
       *   GtkCTree -> gtk_ctree
       *   ClutterX11TexturePixmap -> clutter_x11_texture_pixmap
       *
       * see:
       *
       * http://mail.gnome.org/archives/gtk-devel-list/2007-June/msg00022.html
124 125 126 127
       *
       * and:
       *
       * http://git.gnome.org/cgit/gtk+/plain/gtk/gtkbuilderparser.c
128 129
       */

130
      if ((c == g_ascii_toupper (c) &&
131 132 133 134
           i > 0 && name[i - 1] != g_ascii_toupper (name[i - 1])) ||
          (i > 2 && name[i] == g_ascii_toupper (name[i]) &&
           name[i - 1] == g_ascii_toupper (name[i - 1]) &&
           name[i - 2] == g_ascii_toupper (name[i - 2])))
135
        g_string_append_c (symbol_name, '_');
136

137 138
      g_string_append_c (symbol_name, g_ascii_tolower (c));
    }
139

140 141 142 143 144
  g_string_append (symbol_name, "_get_type");
  
  symbol = g_string_free (symbol_name, FALSE);

  if (g_module_symbol (module, symbol, (gpointer)&func))
145 146 147 148
    {
      CLUTTER_NOTE (SCRIPT, "Type function: %s", symbol);
      gtype = func ();
    }
149 150 151 152 153 154
  
  g_free (symbol);

  return gtype;
}

155 156 157 158
/*
 * clutter_script_enum_from_string:
 * @type: a #GType for an enumeration type
 * @string: the enumeration value as a string
159
 * @enum_value: (out): return location for the enumeration value as an integer
160 161 162 163 164 165 166 167 168
 *
 * Converts an enumeration value inside @string into a numeric
 * value and places it into @enum_value.
 *
 * The enumeration value can be an integer, the enumeration nick
 * or the enumeration name, as part of the #GEnumValue structure.
 *
 * Return value: %TRUE if the conversion was successfull.
 */
169
gboolean
170 171 172
_clutter_script_enum_from_string (GType        type,
                                  const gchar *string,
                                  gint        *enum_value)
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
{
  GEnumClass *eclass;
  GEnumValue *ev;
  gchar *endptr;
  gint value;
  gboolean retval = TRUE;
  
  g_return_val_if_fail (G_TYPE_IS_ENUM (type), 0);
  g_return_val_if_fail (string != NULL, 0);
  
  value = strtoul (string, &endptr, 0);
  if (endptr != string) /* parsed a number */
    *enum_value = value;
  else
    {
      eclass = g_type_class_ref (type);
      ev = g_enum_get_value_by_name (eclass, string);
      if (!ev)
	ev = g_enum_get_value_by_nick (eclass, string);

      if (ev)
	*enum_value = ev->value;
      else
        retval = FALSE;
      
      g_type_class_unref (eclass);
    }

  return retval;
}

gboolean
205 206 207
_clutter_script_flags_from_string (GType        type,
                                   const gchar *string,
                                   gint        *flags_value)
208 209 210 211 212 213 214 215
{
  gchar *endptr, *prevptr;
  guint i, j, ret, value;
  gchar *flagstr;
  GFlagsValue *fv;
  const gchar *flag;

  g_return_val_if_fail (G_TYPE_IS_FLAGS (type), 0);
216
  g_return_val_if_fail (string != NULL, 0);
217 218 219 220 221 222 223 224

  ret = TRUE;
  
  value = strtoul (string, &endptr, 0);
  if (endptr != string) /* parsed a number */
    *flags_value = value;
  else
    {
225 226
      GFlagsClass *fclass;

227 228 229 230 231
      fclass = g_type_class_ref (type);

      flagstr = g_strdup (string);
      for (value = i = j = 0; ; i++)
	{
232
          gboolean eos = (flagstr[i] == '\0') ? TRUE : FALSE;
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	  
	  if (!eos && flagstr[i] != '|')
	    continue;
	  
	  flag = &flagstr[j];
	  endptr = &flagstr[i];
	  
	  if (!eos)
	    {
	      flagstr[i++] = '\0';
	      j = i;
	    }
	  
	  /* trim spaces */
	  for (;;)
	    {
249
	      gunichar ch = g_utf8_get_char (flag);
250 251
	      if (!g_unichar_isspace (ch))
		break;
252

253 254 255 256 257
	      flag = g_utf8_next_char (flag);
	    }
	  
	  while (endptr > flag)
	    {
258 259
              gunichar ch;

260
	      prevptr = g_utf8_prev_char (endptr);
261

262 263 264
	      ch = g_utf8_get_char (prevptr);
	      if (!g_unichar_isspace (ch))
		break;
265

266 267 268 269 270 271
	      endptr = prevptr;
	    }
	  
	  if (endptr > flag)
	    {
	      *endptr = '\0';
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
	      fv = g_flags_get_value_by_name (fclass, flag);
	      
	      if (!fv)
		fv = g_flags_get_value_by_nick (fclass, flag);
	      
	      if (fv)
		value |= fv->value;
	      else
		{
		  ret = FALSE;
		  break;
		}
	    }
	  
	  if (eos)
	    {
	      *flags_value = value;
	      break;
	    }
	}
      
      g_free (flagstr);
      
      g_type_class_unref (fclass);
    }

  return ret;
}

static gboolean
parse_knot_from_array (JsonArray   *array,
                       ClutterKnot *knot)
{
306
  if (json_array_get_length (array) != 2)
307 308
    return FALSE;

309 310
  knot->x = json_array_get_int_element (array, 0);
  knot->y = json_array_get_int_element (array, 1);
311 312 313 314 315 316 317 318

  return TRUE;
}

static gboolean
parse_knot_from_object (JsonObject  *object,
                        ClutterKnot *knot)
{
319 320 321 322
  if (json_object_has_member (object, "x"))
    knot->x = json_object_get_int_member (object, "x");
  else
    knot->x = 0;
323

324 325 326 327
  if (json_object_has_member (object, "y"))
    knot->y = json_object_get_int_member (object, "y");
  else
    knot->y = 0;
328 329 330 331 332

  return TRUE;
}

gboolean
333 334 335
_clutter_script_parse_knot (ClutterScript *script,
                            JsonNode      *node,
                            ClutterKnot   *knot)
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
{
  g_return_val_if_fail (CLUTTER_IS_SCRIPT (script), FALSE);
  g_return_val_if_fail (node != NULL, FALSE);
  g_return_val_if_fail (knot != NULL, FALSE);

  switch (JSON_NODE_TYPE (node))
    {
    case JSON_NODE_ARRAY:
      return parse_knot_from_array (json_node_get_array (node), knot);

    case JSON_NODE_OBJECT:
      return parse_knot_from_object (json_node_get_object (node), knot);

    default:
      break;
    }

  return FALSE;
}

static gboolean
parse_geometry_from_array (JsonArray       *array,
                           ClutterGeometry *geometry)
{
360
  if (json_array_get_length (array) != 4)
361 362
    return FALSE;

363 364 365 366
  geometry->x = json_array_get_int_element (array, 0);
  geometry->y = json_array_get_int_element (array, 1);
  geometry->width = json_array_get_int_element (array, 2);
  geometry->height = json_array_get_int_element (array, 3);
367 368 369 370 371 372 373 374

  return TRUE;
}

static gboolean
parse_geometry_from_object (JsonObject      *object,
                            ClutterGeometry *geometry)
{
375 376 377 378
  if (json_object_has_member (object, "x"))
    geometry->x = json_object_get_int_member (object, "x");
  else
    geometry->x = 0;
379

380 381 382 383
  if (json_object_has_member (object, "y"))
    geometry->y = json_object_get_int_member (object, "y");
  else
    geometry->y = 0;
384

385 386 387 388
  if (json_object_has_member (object, "width"))
    geometry->width = json_object_get_int_member (object, "width");
  else
    geometry->width = 0;
389

390 391 392 393
  if (json_object_has_member (object, "height"))
    geometry->height = json_object_get_int_member (object, "height");
  else
    geometry->height = 0;
394 395 396 397 398

  return TRUE;
}

gboolean
399 400 401
_clutter_script_parse_geometry (ClutterScript   *script,
                                JsonNode        *node,
                                ClutterGeometry *geometry)
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
{
  g_return_val_if_fail (CLUTTER_IS_SCRIPT (script), FALSE);
  g_return_val_if_fail (node != NULL, FALSE);
  g_return_val_if_fail (geometry != NULL, FALSE);

  switch (JSON_NODE_TYPE (node))
    {
    case JSON_NODE_ARRAY:
      return parse_geometry_from_array (json_node_get_array (node), geometry);

    case JSON_NODE_OBJECT:
      return parse_geometry_from_object (json_node_get_object (node), geometry);

    default:
      break;
    }

  return FALSE;
}
421 422 423 424 425

static gboolean
parse_color_from_array (JsonArray    *array,
                        ClutterColor *color)
{
426 427
  if (json_array_get_length (array) != 3 ||
      json_array_get_length (array) != 4)
428 429
    return FALSE;

430 431 432
  color->red   = CLAMP (json_array_get_int_element (array, 0), 0, 255);
  color->green = CLAMP (json_array_get_int_element (array, 1), 0, 255);
  color->blue  = CLAMP (json_array_get_int_element (array, 2), 0, 255);
433

434 435 436 437
  if (json_array_get_length (array) == 4)
    color->alpha = CLAMP (json_array_get_int_element (array, 3), 0, 255);
  else
    color->alpha = 255;
438 439 440 441 442 443 444 445

  return TRUE;
}

static gboolean
parse_color_from_object (JsonObject   *object,
                         ClutterColor *color)
{
446 447 448 449
  if (json_object_has_member (object, "red"))
    color->red = CLAMP (json_object_get_int_member (object, "red"), 0, 255);
  else
    color->red = 0;
450

451 452 453 454
  if (json_object_has_member (object, "green"))
    color->green = CLAMP (json_object_get_int_member (object, "green"), 0, 255);
  else
    color->green = 0;
455

456 457 458 459
  if (json_object_has_member (object, "blue"))
    color->blue = CLAMP (json_object_get_int_member (object, "blue"), 0, 255);
  else
    color->blue = 0;
460

461 462 463 464
  if (json_object_has_member (object, "alpha"))
    color->alpha = CLAMP (json_object_get_int_member (object, "alpha"), 0, 255);
  else
    color->alpha = 255;
465 466 467 468 469

  return TRUE;
}

gboolean
470 471 472
_clutter_script_parse_color (ClutterScript *script,
                             JsonNode      *node,
                             ClutterColor  *color)
473 474 475 476 477 478 479 480 481 482 483 484 485
{
  g_return_val_if_fail (CLUTTER_IS_SCRIPT (script), FALSE);
  g_return_val_if_fail (node != NULL, FALSE);
  g_return_val_if_fail (color != NULL, FALSE);

  switch (JSON_NODE_TYPE (node))
    {
    case JSON_NODE_ARRAY:
      return parse_color_from_array (json_node_get_array (node), color);

    case JSON_NODE_OBJECT:
      return parse_color_from_object (json_node_get_object (node), color);

486 487 488
    case JSON_NODE_VALUE:
      return clutter_color_from_string (color, json_node_get_string (node));

489 490 491 492 493 494
    default:
      break;
    }

  return FALSE;
}
495

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
static gboolean
parse_point_from_array (JsonArray    *array,
                        ClutterPoint *point)
{
  if (json_array_get_length (array) != 2)
    return FALSE;

  point->x = json_array_get_double_element (array, 0);
  point->y = json_array_get_double_element (array, 1);

  return TRUE;
}

static gboolean
parse_point_from_object (JsonObject   *object,
                         ClutterPoint *point)
{
  if (json_object_has_member (object, "x"))
    point->x = json_object_get_double_member (object, "x");
  else
    point->x = 0.f;

  if (json_object_has_member (object, "y"))
    point->y = json_object_get_double_member (object, "y");
  else
    point->y = 0.f;

  return TRUE;
}

gboolean
_clutter_script_parse_point (ClutterScript *script,
                             JsonNode      *node,
                             ClutterPoint  *point)
{
  g_return_val_if_fail (CLUTTER_IS_SCRIPT (script), FALSE);
  g_return_val_if_fail (node != NULL, FALSE);
  g_return_val_if_fail (point != NULL, FALSE);

  switch (JSON_NODE_TYPE (node))
    {
    case JSON_NODE_ARRAY:
      return parse_point_from_array (json_node_get_array (node), point);

    case JSON_NODE_OBJECT:
      return parse_point_from_object (json_node_get_object (node), point);

    default:
      break;
    }

  return FALSE;
}

static gboolean
parse_size_from_array (JsonArray   *array,
                       ClutterSize *size)
{
  if (json_array_get_length (array) != 2)
    return FALSE;

  size->width = json_array_get_double_element (array, 0);
  size->height = json_array_get_double_element (array, 1);

  return TRUE;
}

static gboolean
parse_size_from_object (JsonObject  *object,
                        ClutterSize *size)
{
  if (json_object_has_member (object, "width"))
    size->width = json_object_get_double_member (object, "width");
  else
    size->width = 0.f;

  if (json_object_has_member (object, "height"))
    size->height = json_object_get_double_member (object, "height");
  else
    size->height = 0.f;

  return TRUE;
}

gboolean
_clutter_script_parse_size (ClutterScript *script,
                            JsonNode      *node,
                            ClutterSize   *size)
{
  g_return_val_if_fail (CLUTTER_IS_SCRIPT (script), FALSE);
  g_return_val_if_fail (node != NULL, FALSE);
  g_return_val_if_fail (size != NULL, FALSE);

  switch (JSON_NODE_TYPE (node))
    {
    case JSON_NODE_ARRAY:
      return parse_size_from_array (json_node_get_array (node), size);

    case JSON_NODE_OBJECT:
      return parse_size_from_object (json_node_get_object (node), size);

    default:
      break;
    }

  return FALSE;
}

604 605
const gchar *
_clutter_script_get_id_from_node (JsonNode *node)
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
{
  JsonObject *object;

  switch (JSON_NODE_TYPE (node))
    {
    case JSON_NODE_OBJECT:
      object = json_node_get_object (node);
      if (json_object_has_member (object, "id"))
        return json_object_get_string_member (object, "id");
      break;

    case JSON_NODE_VALUE:
      return json_node_get_string (node);

    default:
      break;
    }

  return NULL;
}

static GList *
parse_children (ObjectInfo *oinfo,
                JsonNode   *node)
{
  JsonArray *array;
  GList *retval;
  guint array_len, i;

  if (JSON_NODE_TYPE (node) != JSON_NODE_ARRAY)
    return NULL;

  retval = oinfo->children;

  array = json_node_get_array (node);
  array_len = json_array_get_length (array);

  for (i = 0; i < array_len; i++)
    {
      JsonNode *child = json_array_get_element (array, i);
646
      const gchar *id_;
647

648 649 650
      id_ = _clutter_script_get_id_from_node (child);
      if (id_ != NULL)
        retval = g_list_prepend (retval, g_strdup (id_));
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    }

  return g_list_reverse (retval);
}

static GList *
parse_signals (ClutterScript *script,
               ObjectInfo    *oinfo,
               JsonNode      *node)
{
  JsonArray *array;
  GList *retval;
  guint array_len, i;

  if (JSON_NODE_TYPE (node) != JSON_NODE_ARRAY)
    {
      _clutter_script_warn_invalid_value (script, "signals", "Array", node);
      return NULL;
    }

  retval = oinfo->signals;
  array = json_node_get_array (node);
  array_len = json_array_get_length (array);

  for (i = 0; i < array_len; i++)
    {
      JsonNode *val = json_array_get_element (array, i);
678
      SignalInfo *sinfo = NULL;
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
      JsonObject *object;
      const gchar *name;

      if (JSON_NODE_TYPE (val) != JSON_NODE_OBJECT)
        {
          _clutter_script_warn_invalid_value (script,
                                              "signals array", "Object",
                                              node);
          continue;
        }

      object = json_node_get_object (val);

      /* mandatory: "name" */
      if (!json_object_has_member (object, "name"))
        {
          _clutter_script_warn_missing_attribute (script, NULL, "name");
          continue;
        }
      else
        {
          name = json_object_get_string_member (object, "name");
          if (!name)
            {
              _clutter_script_warn_invalid_value (script,
                                                  "name", "string",
                                                  val);
              continue;
            }
        }

710 711
      /* mandatory: "target-state" or "handler" */
      if (json_object_has_member (object, "target-state"))
712
        {
713 714
          const gchar *state = NULL;
          const gchar *target = NULL;
715
          gboolean warp_to = FALSE;
716 717 718 719 720 721 722 723 724 725

          target = json_object_get_string_member (object, "target-state");
          if (target == NULL)
            {
              _clutter_script_warn_invalid_value (script,
                                                  "target-state", "string",
                                                  val);
              continue;
            }

726 727
          if (json_object_has_member (object, "states"))
            state = json_object_get_string_member (object, "states");
728

729 730 731
          if (json_object_has_member (object, "warp"))
            warp_to = json_object_get_boolean_member (object, "warp");

732
          CLUTTER_NOTE (SCRIPT,
733
                        "Added signal '%s' (states:%s, target-state:%s, warp:%s)",
734
                        name,
735 736
                        state != NULL ? state : "<default>", target,
                        warp_to ? "true" : "false");
737 738 739 740 741 742

          sinfo = g_slice_new0 (SignalInfo);
          sinfo->is_handler = FALSE;
          sinfo->name = g_strdup (name);
          sinfo->state = g_strdup (state);
          sinfo->target = g_strdup (target);
743
          sinfo->warp_to = warp_to;
744
        }
745
      else if (json_object_has_member (object, "handler"))
746
        {
747 748 749 750
          const gchar *handler;
          const gchar *connect;
          GConnectFlags flags = 0;

751
          handler = json_object_get_string_member (object, "handler");
752
          if (handler == NULL)
753 754 755 756 757 758 759
            {
              _clutter_script_warn_invalid_value (script,
                                                  "handler", "string",
                                                  val);
              continue;
            }

760 761 762 763 764
          /* optional: "object" */
          if (json_object_has_member (object, "object"))
            connect = json_object_get_string_member (object, "object");
          else
            connect = NULL;
765

766 767 768 769 770 771
          /* optional: "after" */
          if (json_object_has_member (object, "after"))
            {
              if (json_object_get_boolean_member (object, "after"))
                flags |= G_CONNECT_AFTER;
            }
772

773 774 775 776 777 778
          /* optional: "swapped" */
          if (json_object_has_member (object, "swapped"))
            {
              if (json_object_get_boolean_member (object, "swapped"))
                flags |= G_CONNECT_SWAPPED;
            }
779

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
          CLUTTER_NOTE (SCRIPT,
                        "Added signal '%s' (handler:%s, object:%s, flags:%d)",
                        name,
                        handler, connect, flags);

          sinfo = g_slice_new0 (SignalInfo);
          sinfo->is_handler = TRUE;
          sinfo->name = g_strdup (name);
          sinfo->handler = g_strdup (handler);
          sinfo->object = g_strdup (connect);
          sinfo->flags = flags;
        }
      else
        _clutter_script_warn_missing_attribute (script,
                                                NULL,
                                                "handler or state");
796 797
      if (sinfo != NULL)
        retval = g_list_prepend (retval, sinfo);
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
    }

  return retval;
}

static ClutterTimeline *
construct_timeline (ClutterScript *script,
                    JsonObject    *object)
{
  ClutterTimeline *retval = NULL;
  ObjectInfo *oinfo;
  GList *members, *l;

  /* we fake an ObjectInfo so we can reuse clutter_script_construct_object()
   * here; we do not save it inside the hash table, because if this had
   * been a named object then we wouldn't have ended up here in the first
   * place
   */
  oinfo = g_slice_new0 (ObjectInfo);
  oinfo->gtype = CLUTTER_TYPE_TIMELINE;
  oinfo->id = g_strdup ("dummy");

  members = json_object_get_members (object);
  for (l = members; l != NULL; l = l->next)
    {
      const gchar *name = l->data;
      JsonNode *node = json_object_get_member (object, name);
      PropertyInfo *pinfo = g_slice_new0 (PropertyInfo);

      pinfo->name = g_strdelimit (g_strdup (name), G_STR_DELIMITERS, '-');
828
      pinfo->node = json_node_copy (node);
829 830 831 832 833 834

      oinfo->properties = g_list_prepend (oinfo->properties, pinfo);
    }

  g_list_free (members);

835
  _clutter_script_construct_object (script, oinfo);
836
  _clutter_script_apply_properties (script, oinfo);
837
  retval = CLUTTER_TIMELINE (oinfo->object);
838

839 840 841 842
  /* we transfer ownership to the alpha function, so we ref before
   * destroying the ObjectInfo to avoid the timeline going away
   */
  g_object_ref (retval);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
  object_info_free (oinfo);

  return retval;
}

/* define the names of the animation modes to match the ones
 * that developers might be more accustomed to
 */
static const struct
{
  const gchar *name;
  ClutterAnimationMode mode;
} animation_modes[] = {
  { "linear", CLUTTER_LINEAR },
  { "easeInQuad", CLUTTER_EASE_IN_QUAD },
  { "easeOutQuad", CLUTTER_EASE_OUT_QUAD },
  { "easeInOutQuad", CLUTTER_EASE_IN_OUT_QUAD },
  { "easeInCubic", CLUTTER_EASE_IN_CUBIC },
  { "easeOutCubic", CLUTTER_EASE_OUT_CUBIC },
  { "easeInOutCubic", CLUTTER_EASE_IN_OUT_CUBIC },
  { "easeInQuart", CLUTTER_EASE_IN_QUART },
  { "easeOutQuart", CLUTTER_EASE_OUT_QUART },
  { "easeInOutQuart", CLUTTER_EASE_IN_OUT_QUART },
  { "easeInQuint", CLUTTER_EASE_IN_QUINT },
  { "easeOutQuint", CLUTTER_EASE_OUT_QUINT },
  { "easeInOutQuint", CLUTTER_EASE_IN_OUT_QUINT },
  { "easeInSine", CLUTTER_EASE_IN_SINE },
  { "easeOutSine", CLUTTER_EASE_OUT_SINE },
  { "easeInOutSine", CLUTTER_EASE_IN_OUT_SINE },
  { "easeInExpo", CLUTTER_EASE_IN_EXPO },
  { "easeOutExpo", CLUTTER_EASE_OUT_EXPO },
  { "easeInOutExpo", CLUTTER_EASE_IN_OUT_EXPO },
  { "easeInCirc", CLUTTER_EASE_IN_CIRC },
  { "easeOutCirc", CLUTTER_EASE_OUT_CIRC },
  { "easeInOutCirc", CLUTTER_EASE_IN_OUT_CIRC },
  { "easeInElastic", CLUTTER_EASE_IN_ELASTIC },
  { "easeOutElastic", CLUTTER_EASE_OUT_ELASTIC },
  { "easeInOutElastic", CLUTTER_EASE_IN_OUT_ELASTIC },
  { "easeInBack", CLUTTER_EASE_IN_BACK },
  { "easeOutBack", CLUTTER_EASE_OUT_BACK },
  { "easeInOutBack", CLUTTER_EASE_IN_OUT_BACK },
  { "easeInBounce", CLUTTER_EASE_IN_BOUNCE },
  { "easeOutBounce", CLUTTER_EASE_OUT_BOUNCE },
  { "easeInOutBounce", CLUTTER_EASE_IN_OUT_BOUNCE },
};

static const gint n_animation_modes = G_N_ELEMENTS (animation_modes);

gulong
892
_clutter_script_resolve_animation_mode (JsonNode *node)
893
{
894
  gint i, res = CLUTTER_CUSTOM_MODE;
895

896 897 898 899 900 901 902
  if (JSON_NODE_TYPE (node) != JSON_NODE_VALUE)
    return CLUTTER_CUSTOM_MODE;

  if (json_node_get_value_type (node) == G_TYPE_INT64)
    return json_node_get_int (node);

  if (json_node_get_value_type (node) == G_TYPE_STRING)
903
    {
904 905 906 907 908 909 910 911 912 913 914 915 916
      const gchar *name = json_node_get_string (node);

      /* XXX - we might be able to optimize by changing the ordering
       * of the animation_modes array, e.g.
       *  - special casing linear
       *  - tokenizing ('ease', 'In', 'Sine') and matching on token
       *  - binary searching?
       */
      for (i = 0; i < n_animation_modes; i++)
        {
          if (strcmp (animation_modes[i].name, name) == 0)
            return animation_modes[i].mode;
        }
917

918 919 920
      if (_clutter_script_enum_from_string (CLUTTER_TYPE_ANIMATION_MODE,
                                            name,
                                            &res))
921
        return res;
922

923 924
      g_warning ("Unable to find the animation mode '%s'", name);
    }
925 926 927 928 929 930 931 932 933 934 935 936 937

  return CLUTTER_CUSTOM_MODE;
}

static ClutterAlphaFunc
resolve_alpha_func (const gchar *name)
{
  static GModule *module = NULL;
  ClutterAlphaFunc func;

  CLUTTER_NOTE (SCRIPT, "Looking up '%s' alpha function", name);

  if (G_UNLIKELY (!module))
938
    module = g_module_open (NULL, 0);
939 940 941 942 943 944 945 946 947 948 949 950

  if (g_module_symbol (module, name, (gpointer) &func))
    {
      CLUTTER_NOTE (SCRIPT, "Found '%s' alpha function in the symbols table",
                    name);
      return func;
    }

  return NULL;
}

GObject *
951 952
_clutter_script_parse_alpha (ClutterScript *script,
                             JsonNode      *node)
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
{
  GObject *retval = NULL;
  JsonObject *object;
  ClutterTimeline *timeline = NULL;
  ClutterAlphaFunc alpha_func = NULL;
  ClutterAnimationMode mode = CLUTTER_CUSTOM_MODE;
  JsonNode *val;
  gboolean unref_timeline = FALSE;

  if (JSON_NODE_TYPE (node) != JSON_NODE_OBJECT)
    return NULL;

  object = json_node_get_object (node);

  val = json_object_get_member (object, "timeline");
  if (val)
    {
      if (JSON_NODE_TYPE (val) == JSON_NODE_VALUE &&
          json_node_get_string (val) != NULL)
        {
973
          const gchar *id_ = json_node_get_string (val);
974 975

          timeline =
976
            CLUTTER_TIMELINE (clutter_script_get_object (script, id_));
977 978 979 980 981 982 983 984 985
        }
      else if (JSON_NODE_TYPE (val) == JSON_NODE_OBJECT)
        {
          timeline = construct_timeline (script, json_node_get_object (val));
          unref_timeline = TRUE;
        }
    }

  val = json_object_get_member (object, "mode");
986
  if (val != NULL)
987
    mode = _clutter_script_resolve_animation_mode (val);
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018

  if (mode == CLUTTER_CUSTOM_MODE)
    {
      val = json_object_get_member (object, "function");
      if (val && json_node_get_string (val) != NULL)
        {
          alpha_func = resolve_alpha_func (json_node_get_string (val));
          if (!alpha_func)
            {
              g_warning ("Unable to find the function '%s' in the "
                         "Clutter alpha functions or the symbols table",
                         json_node_get_string (val));
            }
        }
    }

  CLUTTER_NOTE (SCRIPT, "Parsed alpha: %s timeline (%p) (mode:%d, func:%p)",
                unref_timeline ? "implicit" : "explicit",
                timeline ? timeline : 0x0,
                mode != CLUTTER_CUSTOM_MODE ? mode : 0,
                alpha_func ? alpha_func : 0x0);

  retval = g_object_new (CLUTTER_TYPE_ALPHA, NULL);

  if (mode != CLUTTER_CUSTOM_MODE)
    clutter_alpha_set_mode (CLUTTER_ALPHA (retval), mode);

  if (alpha_func != NULL)
    clutter_alpha_set_func (CLUTTER_ALPHA (retval), alpha_func, NULL, NULL);

  clutter_alpha_set_timeline (CLUTTER_ALPHA (retval), timeline);
1019 1020 1021 1022

  /* if we created an implicit timeline, the Alpha has full ownership
   * of it now, since it won't be accessible from ClutterScript
   */
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
  if (unref_timeline)
    g_object_unref (timeline);

  return retval;
}

static void
clutter_script_parser_object_end (JsonParser *json_parser,
                                  JsonObject *object)
{
  ClutterScriptParser *parser = CLUTTER_SCRIPT_PARSER (json_parser);
  ClutterScript *script = parser->script;
  ObjectInfo *oinfo;
  JsonNode *val;
1037
  const gchar *id_;
1038 1039
  GList *members, *l;

1040 1041 1042
  /* if the object definition does not have an 'id' field we'll
   * fake one for it...
   */
1043 1044 1045 1046
  if (!json_object_has_member (object, "id"))
    {
      gchar *fake;

1047 1048 1049 1050
      /* ... unless it doesn't even have a type - in which case
       * it is an internal object definition and we're not
       * supposed to touch it
       */
1051 1052 1053 1054
      if (!json_object_has_member (object, "type"))
        return;

      fake = _clutter_script_generate_fake_id (script);
1055
      json_object_set_string_member (object, "id", fake);
1056

1057 1058 1059 1060 1061
      CLUTTER_NOTE (SCRIPT,
                    "Adding fake id '%s' to object of type '%s'",
                    json_object_get_string_member (object, "id"),
                    json_object_get_string_member (object, "type"));

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
      g_free (fake);
    }

  if (!json_object_has_member (object, "type"))
    {
      val = json_object_get_member (object, "id");

      _clutter_script_warn_missing_attribute (script,
                                              json_node_get_string (val),
                                              "type");
      return;
    }

1075 1076
  id_ = json_object_get_string_member (object, "id");
  CLUTTER_NOTE (SCRIPT, "Getting object info for object '%s'", id_);
1077

1078
  oinfo = _clutter_script_get_object_info (script, id_);
1079
  if (oinfo == NULL)
1080 1081 1082 1083 1084
    {
      const gchar *class_name;

      oinfo = g_slice_new0 (ObjectInfo);
      oinfo->merge_id = _clutter_script_get_last_merge_id (script);
1085
      oinfo->id = g_strdup (id_);
1086
      oinfo->has_unresolved = TRUE;
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

      class_name = json_object_get_string_member (object, "type");
      oinfo->class_name = g_strdup (class_name);

      if (json_object_has_member (object, "type_func"))
        {
          const gchar *type_func;

          type_func = json_object_get_string_member (object, "type_func");
          oinfo->type_func = g_strdup (type_func);

1098 1099 1100
          /* remove the type_func member; we don't want it to
           * pollute the object members
           */
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
          json_object_remove_member (object, "type_func");
        }
    }

  if (json_object_has_member (object, "children"))
    {
      val = json_object_get_member (object, "children");
      oinfo->children = parse_children (oinfo, val);

      json_object_remove_member (object, "children");
1111 1112

      oinfo->has_unresolved = TRUE;
1113 1114 1115 1116 1117 1118 1119 1120 1121
    }

  if (json_object_has_member (object, "signals"))
    {
      val = json_object_get_member (object, "signals");
      oinfo->signals = parse_signals (script, oinfo, val);

      json_object_remove_member (object, "signals");

1122 1123
      oinfo->has_unresolved = TRUE;
    }
1124

1125 1126 1127
  if (strcmp (oinfo->class_name, "ClutterStage") == 0 &&
      json_object_has_member (object, "is-default"))
    {
1128 1129
      oinfo->is_actor = TRUE;
      oinfo->is_stage = TRUE;
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
      oinfo->is_stage_default =
        json_object_get_boolean_member (object, "is-default");

      json_object_remove_member (object, "is-default");
    }
  else
    oinfo->is_stage_default = FALSE;

  members = json_object_get_members (object);
  for (l = members; l; l = l->next)
    {
      const gchar *name = l->data;
      PropertyInfo *pinfo;
      JsonNode *node;

1145 1146 1147 1148
      CLUTTER_NOTE (SCRIPT, "Object '%s' member '%s'",
                    oinfo->id,
                    name);

1149 1150 1151 1152 1153
      /* we have already parsed these */
      if (strcmp (name, "id") == 0 || strcmp (name, "type") == 0)
        continue;

      node = json_object_get_member (object, name);
1154 1155 1156 1157 1158 1159

      /* this should not really happen; we're getting a list of
       * member names, and if one does not map a real member
       * value then it's likely that something has gone wrong
       */
      if (G_UNLIKELY (node == NULL))
1160 1161 1162 1163 1164 1165 1166 1167
        {
          CLUTTER_NOTE (SCRIPT,
                        "Empty node for member '%s' of object '%s' (type: %s)",
                        name,
                        oinfo->id,
                        oinfo->class_name);
          continue;
        }
1168 1169 1170 1171 1172 1173

      pinfo = g_slice_new (PropertyInfo);

      pinfo->name = g_strdup (name);
      pinfo->node = json_node_copy (node);
      pinfo->pspec = NULL;
1174
      pinfo->is_child = g_str_has_prefix (name, "child::") ? TRUE : FALSE;
1175
      pinfo->is_layout = g_str_has_prefix (name, "layout::") ? TRUE : FALSE;
1176 1177

      oinfo->properties = g_list_prepend (oinfo->properties, pinfo);
1178
      oinfo->has_unresolved = TRUE;
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    }

  g_list_free (members);

  CLUTTER_NOTE (SCRIPT,
                "Added object '%s' (type:%s, id:%d, props:%d, signals:%d)",
                oinfo->id,
                oinfo->class_name,
                oinfo->merge_id,
                g_list_length (oinfo->properties),
                g_list_length (oinfo->signals));

  _clutter_script_add_object_info (script, oinfo);
1192
  _clutter_script_construct_object (script, oinfo);
1193 1194 1195 1196 1197 1198 1199 1200
}

static void
clutter_script_parser_parse_end (JsonParser *parser)
{
  clutter_script_ensure_objects (CLUTTER_SCRIPT_PARSER (parser)->script);
}

1201 1202 1203 1204 1205 1206
gboolean
_clutter_script_parse_translatable_string (ClutterScript *script,
                                           JsonNode      *node,
                                           char         **str)
{
  JsonObject *obj;
1207
  const char *string, *domain, *context;
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
  const char *res;
  gboolean translatable;

  if (!JSON_NODE_HOLDS_OBJECT (node))
    return FALSE;

  obj = json_node_get_object (node);
  if (!(json_object_has_member (obj, "translatable") &&
        json_object_has_member (obj, "string")))
    return FALSE;

  translatable = json_object_get_boolean_member (obj, "translatable");

  string = json_object_get_string_member (obj, "string");
  if (string == NULL || *string == '\0')
    return FALSE;

1225 1226 1227 1228 1229
  if (json_object_has_member (obj, "context"))
    context = json_object_get_string_member (obj, "context");
  else
    context = NULL;

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
  if (json_object_has_member (obj, "domain"))
    domain = json_object_get_string_member (obj, "domain");
  else
    domain = NULL;

  if (domain == NULL || *domain == '\0')
    domain = clutter_script_get_translation_domain (script);

  if (translatable)
    {
1240
      if (context != NULL && *context != '\0')
1241
        res = g_dpgettext2 (domain, context, string);
1242
      else
1243
        res = g_dgettext (domain, string);
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    }
  else
    res = string;

  if (str != NULL)
    *str = g_strdup (res);

  return TRUE;
}

1254
gboolean
1255 1256 1257 1258 1259
_clutter_script_parse_node (ClutterScript *script,
                            GValue        *value,
                            const gchar   *name,
                            JsonNode      *node,
                            GParamSpec    *pspec)
1260
{
1261
  GValue node_value = G_VALUE_INIT;
1262 1263 1264 1265 1266 1267 1268 1269 1270
  gboolean retval = FALSE;

  g_return_val_if_fail (CLUTTER_IS_SCRIPT (script), FALSE);
  g_return_val_if_fail (name != NULL, FALSE);
  g_return_val_if_fail (node != NULL, FALSE);

  switch (JSON_NODE_TYPE (node))
    {
    case JSON_NODE_OBJECT:
1271 1272 1273 1274 1275
      /* if we don't have a GParamSpec we can't infer the type
       * of the property; this usually means that this property
       * is a custom member that will be parsed by the Scriptable
       * interface implementantion
       */
1276
      if (pspec == NULL && !G_IS_VALUE (value))
1277 1278 1279
        return FALSE;
      else
        {
1280
          GType p_type;
1281
          ObjectInfo *oinfo;
1282
          const gchar *id_;
1283

1284 1285 1286 1287 1288 1289 1290
          if (G_IS_VALUE (value))
            p_type = G_VALUE_TYPE (value);
          else
            {
              p_type = G_PARAM_SPEC_VALUE_TYPE (pspec);
              g_value_init (value, p_type);
            }
1291

1292
          if (g_type_is_a (p_type, G_TYPE_OBJECT))
1293
            {
1294 1295 1296 1297 1298
              /* default GObject handling: we get the id and
               * retrieve the ObjectInfo for it; since the object
               * definitions are parsed leaf-first we are guaranteed
               * to have a defined object at this point
               */
1299 1300
              id_ = _clutter_script_get_id_from_node (node);
              if (id_ == NULL || *id_ == '\0')
1301 1302
                return FALSE;

1303
              oinfo = _clutter_script_get_object_info (script, id_);
1304 1305 1306 1307
              if (oinfo == NULL || oinfo->gtype == G_TYPE_INVALID )
                return FALSE;

              if (g_type_is_a (oinfo->gtype, p_type))
1308
                {
1309 1310 1311 1312 1313 1314 1315 1316 1317
                  /* force construction, even though it should
                   * not be necessary; we don't need the properties
                   * to be applied as well: they will when the
                   * ScriptParser finishes
                   */
                  _clutter_script_construct_object (script, oinfo);

                  g_value_set_object (value, oinfo->object);

1318 1319 1320
                  return TRUE;
                }
            }
1321
          else if (p_type == CLUTTER_TYPE_KNOT)
1322 1323 1324 1325 1326
            {
              ClutterKnot knot = { 0, };

              /* knot := { "x" : (int), "y" : (int) } */

1327
              if (_clutter_script_parse_knot (script, node, &knot))
1328 1329 1330 1331 1332
                {
                  g_value_set_boxed (value, &knot);
                  return TRUE;
                }
            }
1333
          else if (p_type == CLUTTER_TYPE_GEOMETRY)
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
            {
              ClutterGeometry geom = { 0, };

              /* geometry := {
               *        "x" : (int),
               *        "y" : (int),
               *        "width" : (int),
               *        "height" : (int)
               * }
               */

1345
              if (_clutter_script_parse_geometry (script, node, &geom))
1346 1347 1348 1349 1350
                {
                  g_value_set_boxed (value, &geom);
                  return TRUE;
                }
            }
1351
          else if (p_type == CLUTTER_TYPE_COLOR)
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
            {
              ClutterColor color = { 0, };

              /* color := {
               *        "red" : (int),
               *        "green" : (int),
               *        "blue" : (int),
               *        "alpha" : (int)
               * }
               */

1363
              if (_clutter_script_parse_color (script, node, &color))
1364 1365 1366 1367 1368
                {
                  g_value_set_boxed (value, &color);
                  return TRUE;
                }
            }
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
          else if (p_type == CLUTTER_TYPE_POINT)
            {
              ClutterPoint point = CLUTTER_POINT_INIT_ZERO;

              if (_clutter_script_parse_point (script, node, &point))
                {
                  g_value_set_boxed (value, &point);
                  return TRUE;
                }
            }
          else if (p_type == CLUTTER_TYPE_SIZE)
            {
              ClutterSize size = CLUTTER_SIZE_INIT_ZERO;

              if (_clutter_script_parse_size (script, node, &size))
                {
                  g_value_set_boxed (value, &size);
                  return TRUE;
                }
            }
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
          else if (p_type == G_TYPE_STRING)
            {
              char *str = NULL;

              if (_clutter_script_parse_translatable_string (script, node, &str))
                {
                  g_value_take_string (value, str);
                  return TRUE;
                }
            }
1399 1400 1401 1402
         }
      return FALSE;

    case JSON_NODE_ARRAY:
1403
      if (!pspec && !G_IS_VALUE (value))
1404 1405 1406
        return FALSE;
      else
        {
1407 1408
          if (!G_IS_VALUE (value))
            g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1409 1410 1411 1412 1413 1414 1415

          if (G_VALUE_HOLDS (value, CLUTTER_TYPE_KNOT))
            {
              ClutterKnot knot = { 0, };

              /* knot := [ (int), (int) ] */

1416
              if (_clutter_script_parse_knot (script, node, &knot))
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
                {
                  g_value_set_boxed (value, &knot);
                  return TRUE;
                }
            }
          else if (G_VALUE_HOLDS (value, CLUTTER_TYPE_GEOMETRY))
            {
              ClutterGeometry geom = { 0, };

              /* geometry := [ (int), (int), (int), (int) ] */

1428
              if (_clutter_script_parse_geometry (script, node, &geom))
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
                {
                  g_value_set_boxed (value, &geom);
                  return TRUE;
                }
            }
          else if (CLUTTER_VALUE_HOLDS_COLOR (value))
            {
              ClutterColor color = { 0, };

              /* color := [ (int), (int), (int), (int) ] */

1440
              if (_clutter_script_parse_color (script, node, &color))
1441 1442 1443 1444 1445
                {
                  g_value_set_boxed (value, &color);
                  return TRUE;
                }
            }
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
          else if (G_VALUE_HOLDS (value, CLUTTER_TYPE_POINT))
            {
              ClutterPoint point = CLUTTER_POINT_INIT_ZERO;

              if (_clutter_script_parse_point (script, node, &point))
                {
                  g_value_set_boxed (value, &point);
                  return TRUE;
                }
            }
          else if (G_VALUE_HOLDS (value, CLUTTER_TYPE_SIZE))
            {
              ClutterSize size = CLUTTER_SIZE_INIT_ZERO;

              if (_clutter_script_parse_size (script, node, &size))
                {
                  g_value_set_boxed (value, &size);
                  return TRUE;
                }
            }
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
          else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
            {
              JsonArray *array = json_node_get_array (node);
              guint i, array_len = json_array_get_length (array);
              GPtrArray *str_array = g_ptr_array_sized_new (array_len);

              /* strv := [ (str), (str), ... ] */

              for (i = 0; i < array_len; i++)
                {
                  JsonNode *val = json_array_get_element (array, i);

                  if (JSON_NODE_TYPE (val) != JSON_NODE_VALUE &&
                      json_node_get_string (val) == NULL)
                    continue;

                  g_ptr_array_add (str_array,
                                   (gpointer) json_node_get_string (val));
                }

              g_value_set_boxed (value, str_array->pdata);
              g_ptr_array_free (str_array, TRUE);

              return TRUE;
            }
        }
      return FALSE;

    case JSON_NODE_NULL:
      return FALSE;

    case JSON_NODE_VALUE:
      json_node_get_value (node, &node_value);

1500
      if (!pspec && !G_IS_VALUE (value))
1501
        g_value_init (value, G_VALUE_TYPE (&node_value));
1502 1503
      else if (pspec)
        g_value_init (value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558

      switch (G_TYPE_FUNDAMENTAL (G_VALUE_TYPE (value)))
        {
        /* fundamental JSON types */
        case G_TYPE_INT64:
        case G_TYPE_DOUBLE:
        case G_TYPE_STRING:
        case G_TYPE_BOOLEAN:
          g_value_copy (&node_value, value);
          retval = TRUE;
          break;

        case G_TYPE_INT:
          g_value_set_int (value, g_value_get_int64 (&node_value));
          retval = TRUE;
          break;

        case G_TYPE_UINT:
          g_value_set_uint (value, (guint) g_value_get_int64 (&node_value));
          retval = TRUE;
          break;

        case G_TYPE_ULONG:
          g_value_set_ulong (value, (gulong) g_value_get_int64 (&node_value));
          retval = TRUE;
          break;

        case G_TYPE_UCHAR:
          g_value_set_uchar (value, (guchar) g_value_get_int64 (&node_value));
          retval = TRUE;
          break;

        case G_TYPE_FLOAT:
          if (G_VALUE_HOLDS (&node_value, G_TYPE_DOUBLE))
            {
              g_value_set_float (value, g_value_get_double (&node_value));
              retval = TRUE;
            }
          else if (G_VALUE_HOLDS (&node_value, G_TYPE_INT64))
            {
              g_value_set_float (value, g_value_get_int64 (&node_value));
              retval = TRUE;
            }
          break;

        case G_TYPE_ENUM:
          if (G_VALUE_HOLDS (&node_value, G_TYPE_INT64))
            {
              g_value_set_enum (value, g_value_get_int64 (&node_value));
              retval = TRUE;
            }
          else if (G_VALUE_HOLDS (&node_value, G_TYPE_STRING))
            {
              gint enum_value;

1559 1560 1561
              retval = _clutter_script_enum_from_string (G_VALUE_TYPE (value),
                                                         g_value_get_string (&node_value),
                                                         &enum_value);
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
              if (retval)
                g_value_set_enum (value, enum_value);
            }
          break;

        case G_TYPE_FLAGS:
          if (G_VALUE_HOLDS (&node_value, G_TYPE_INT64))
            {
              g_value_set_flags (value, g_value_get_int64 (&node_value));
              retval = TRUE;
            }
          else if (G_VALUE_HOLDS (&node_value, G_TYPE_STRING))
            {
              gint flags_value;

1577 1578 1579
              retval = _clutter_script_flags_from_string (G_VALUE_TYPE (value),
                                                          g_value_get_string (&node_value),
                                                          &flags_value);
1580 1581 1582 1583 1584 1585 1586 1587
              if (retval)
                g_value_set_flags (value, flags_value);
            }
          break;

        case G_TYPE_BOXED:
          if (G_VALUE_HOLDS (value, CLUTTER_TYPE_COLOR))
            {
1588
              ClutterColor color = { 0, };
1589

1590
              retval = _clutter_script_parse_color (script, node, &color);
1591 1592
              if (retval)
                clutter_value_set_color (value, &color);
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
            }
          break;

        case G_TYPE_OBJECT:
          if (G_VALUE_HOLDS (&node_value, G_TYPE_STRING))
            {
              const gchar *str = g_value_get_string (&node_value);
              GObject *object = clutter_script_get_object (script, str);
              if (object)
                {
                  CLUTTER_NOTE (SCRIPT,
                                "Assigning '%s' (%s) to property '%s'",
                                str,
                                G_OBJECT_TYPE_NAME (object),
                                name);

                  g_value_set_object (value, object);
                  retval = TRUE;
                }
            }
          break;

        default:
          retval = FALSE;
          break;
        }

1620 1621 1622 1623 1624 1625 1626 1627 1628
      if (G_VALUE_TYPE (value) == G_TYPE_GTYPE &&
          G_VALUE_HOLDS (&node_value, G_TYPE_STRING))
        {
          const gchar *str = g_value_get_string (&node_value);
          GType type = clutter_script_get_type_from_name (script, str);
          g_value_set_gtype (value, type);
          retval = TRUE;
        }

1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
      g_value_unset (&node_value);
      break;
    }

  return retval;
}

static GList *
clutter_script_translate_parameters (ClutterScript  *script,
                                     GObject        *object,
                                     const gchar    *name,
                                     GList          *properties,
                                     GArray        **params)
{
  ClutterScriptable *scriptable = NULL;
  ClutterScriptableIface *iface = NULL;
  GList *l, *unparsed;
  gboolean parse_custom = FALSE;

  *params = g_array_new (FALSE, FALSE, sizeof (GParameter));

  if (CLUTTER_IS_SCRIPTABLE (object))
    {
      scriptable = CLUTTER_SCRIPTABLE (object);
      iface = CLUTTER_SCRIPTABLE_GET_IFACE (scriptable);

      if (iface->parse_custom_node)
        parse_custom = TRUE;
    }

  unparsed = NULL;

  for (l = properties; l != NULL; l = l->next)
    {
      PropertyInfo *pinfo = l->data;
      GParameter param = { NULL };
      gboolean res = FALSE;

1667
      if (pinfo->is_child || pinfo->is_layout)
1668
        {
1669 1670 1671
          CLUTTER_NOTE (SCRIPT, "Skipping %s property '%s'",
                        pinfo->is_child ? "child" : "layout",
                        pinfo->name);
1672 1673 1674 1675
          unparsed = g_list_prepend (unparsed, pinfo);
          continue;
        }

1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
      CLUTTER_NOTE (SCRIPT, "Parsing %s property (id:%s)",
                    pinfo->pspec ? "regular" : "custom",
                    pinfo->name);

      if (parse_custom)
        res = iface->parse_custom_node (scriptable, script, &param.value,
                                        pinfo->name,
                                        pinfo->node);

      if (!res)
1686 1687 1688 1689
        res = _clutter_script_parse_node (script, &param.value,
                                          pinfo->name,
                                          pinfo->node,
                                          pinfo->pspec);
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755

      if (!res)
        {
          CLUTTER_NOTE (SCRIPT, "Property '%s' ignored", pinfo->name);
          unparsed = g_list_prepend (unparsed, pinfo);
          continue;
        }

      param.name = g_strdup (pinfo->name);

      g_array_append_val (*params, param);

      property_info_free (pinfo);
    }

  g_list_free (properties);

  return unparsed;
}

static GList *
clutter_script_construct_parameters (ClutterScript  *script,
                                     GType           gtype,
                                     const gchar    *name,
                                     GList          *properties,
                                     GArray        **construct_params)
{
  GObjectClass *klass;
  GList *l, *unparsed;

  klass = g_type_class_ref (gtype);
  g_assert (klass != NULL);

  *construct_params = g_array_new (FALSE, FALSE, sizeof (GParameter));

  unparsed = NULL;

  for (l = properties; l != NULL; l = l->next)
    {
      PropertyInfo *pinfo = l->data;
      GParameter param = { NULL };
      GParamSpec *pspec = NULL;

      /* we allow custom property names for classes, so if we
       * don't find a corresponding GObject property for this
       * class we just skip it and let the class itself deal
       * with it later on
       */
      pspec = g_object_class_find_property (klass, pinfo->name);
      if (pspec)
        pinfo->pspec = g_param_spec_ref (pspec);
      else
        {
          pinfo->pspec = NULL;
          unparsed = g_list_prepend (unparsed, pinfo);
          continue;
        }

      if (!(pspec->flags & G_PARAM_CONSTRUCT_ONLY))
        {
          unparsed = g_list_prepend (unparsed, pinfo);
          continue;
        }

      param.name = g_strdup (pinfo->name);

1756 1757 1758 1759
      if (!_clutter_script_parse_node (script, &param.value,
                                       pinfo->name,
                                       pinfo->node,
                                       pinfo->pspec))
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
        {
          unparsed = g_list_prepend (unparsed, pinfo);
          continue;
        }

      g_array_append_val (*construct_params, param);

      property_info_free (pinfo);
    }

  g_list_free (properties);

  g_type_class_unref (klass);

  return unparsed;
}

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
static void
apply_layout_properties (ClutterScript    *script,
                         ClutterContainer *container,
                         ClutterActor     *actor,
                         ObjectInfo       *oinfo)
{
  ClutterScriptable *scriptable = NULL;
  ClutterScriptableIface *iface = NULL;
  gboolean parse_custom_node = FALSE;
  GList *l, *unresolved, *properties;
  ClutterLayoutManager *manager;
  GType meta_type;

  manager = g_object_get_data (G_OBJECT (container), "clutter-layout-manager");
  if (manager == NULL)
    return;

  meta_type = _clutter_layout_manager_get_child_meta_type (manager);
  if (meta_type == G_TYPE_INVALID)
    return;

  CLUTTER_NOTE (SCRIPT, "Layout manager of type '%s' with meta type '%s'",
                G_OBJECT_TYPE_NAME (manager),
                g_type_name (meta_type));

  /* shortcut, to avoid typechecking every time */
  if (CLUTTER_IS_SCRIPTABLE (manager))
    {
      scriptable = CLUTTER_SCRIPTABLE (manager);
      iface = CLUTTER_SCRIPTABLE_GET_IFACE (scriptable);

      parse_custom_node = iface->parse_custom_node != NULL ? TRUE : FALSE;
    }

  properties = oinfo->properties;
  oinfo->properties = NULL;

  unresolved = NULL;
  for (l = properties; l != NULL; l = l->next)
    {
      PropertyInfo *pinfo = l->data;
1818
      GValue value = G_VALUE_INIT;
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
      gboolean res = FALSE;
      const gchar *name;

      if (!pinfo->is_layout)
        {
          unresolved = g_list_prepend (unresolved, pinfo);
          continue;
        }

      name = pinfo->name + strlen ("layout::");

      pinfo->pspec =
        clutter_layout_manager_find_child_property (manager, name);

      if (pinfo->pspec != NULL)
        g_param_spec_ref (pinfo->pspec);

      CLUTTER_NOTE (SCRIPT, "Parsing %s layout property (id:%s)",
                    pinfo->pspec != NULL ? "regular" : "custom",
                    name);

      if (parse_custom_node)
        res = iface->parse_custom_node (scriptable, script, &value,
                                        name,
                                        pinfo->node);

      if (!res)
1846 1847 1848 1849
        res = _clutter_script_parse_node (script, &value,
                                          name,
                                          pinfo->node,
                                          pinfo->pspec);
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860

      if (!res)
        {
          CLUTTER_NOTE (SCRIPT, "Layout property '%s' ignored", name);
          unresolved = g_list_prepend (unresolved, pinfo);
          continue;
        }

      CLUTTER_NOTE (SCRIPT,
                    "Setting %s layout property '%s' (type:%s) to "
                    "object '%s' (id:%s)",
1861
                    iface->set_custom_property != NULL ? "custom" : "regular",
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
                    name,
                    g_type_name (G_VALUE_TYPE (&value)),
                    g_type_name (oinfo->gtype),
                    oinfo->id);

      clutter_layout_manager_child_set_property (manager, container, actor,
                                                 name,
                                                 &value);

      g_value_unset (&value);

      property_info_free (pinfo);
    }

  g_list_free (properties);

  oinfo->properties = unresolved;
}

1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
static void
apply_child_properties (ClutterScript    *script,
                        ClutterContainer *container,
                        ClutterActor     *actor,
                        ObjectInfo       *oinfo)
{
  ClutterScriptable *scriptable = NULL;
  ClutterScriptableIface *iface = NULL;
  gboolean parse_custom_node = FALSE;
  GList *l, *unresolved, *properties;
  GObjectClass *klass;
  GType meta_type;

  meta_type = CLUTTER_CONTAINER_GET_IFACE (container)->child_meta_type;
  if (meta_type == G_TYPE_INVALID)
    return;

  klass = G_OBJECT_GET_CLASS (container);

  /* shortcut, to avoid typechecking every time */
  if (CLUTTER_IS_SCRIPTABLE (container))
    {
      scriptable = CLUTTER_SCRIPTABLE (container);
      iface = CLUTTER_SCRIPTABLE_GET_IFACE (scriptable);

      parse_custom_node = iface->parse_custom_node != NULL ? TRUE : FALSE;
    }

  properties = oinfo->properties;
  oinfo->properties = NULL;

  unresolved = NULL;
  for (l = properties; l != NULL; l = l->next)
    {
      PropertyInfo *pinfo = l->data;
1916
      GValue value = G_VALUE_INIT;
1917
      gboolean res = FALSE;
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
      const gchar *name;

      if (!pinfo->is_child)
        {
          unresolved = g_list_prepend (unresolved, pinfo);
          continue;
        }

      name = pinfo->name + strlen ("child::");

      pinfo->pspec =
        clutter_container_class_find_child_property (klass, name);

      if (pinfo->pspec != NULL)
        g_param_spec_ref (pinfo->pspec);

      CLUTTER_NOTE (SCRIPT, "Parsing %s child property (id:%s)",
                    pinfo->pspec != NULL ? "regular" : "custom",
                    name);

      if (parse_custom_node)
        res = iface->parse_custom_node (scriptable, script, &value,
                                        name,
                                        pinfo->node);

      if (!res)
1944 1945 1946 1947
        res = _clutter_script_parse_node (script, &value,
                                          name,
                                          pinfo->node,
                                          pinfo->pspec);
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959

      if (!res)
        {
          CLUTTER_NOTE (SCRIPT, "Child property '%s' ignored", name);
          unresolved = g_list_prepend (unresolved, pinfo);
          continue;
        }

      
      CLUTTER_NOTE (SCRIPT,
                    "Setting %s child property '%s' (type:%s) to "
                    "object '%s' (id:%s)",
1960
                    iface->set_custom_property != NULL ? "custom" : "regular",
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
                    name,
                    g_type_name (G_VALUE_TYPE (&value)),
                    g_type_name (oinfo->gtype),
                    oinfo->id);

      clutter_container_child_set_property (container, actor,
                                            name,
                                            &value);

      g_value_unset (&value);

      property_info_free (pinfo);
    }

  g_list_free (properties);

  oinfo->properties = unresolved;
}

1980
static void
1981 1982
add_children (ClutterScript *script,
              ObjectInfo    *oinfo)
1983
{
1984
  ClutterContainer *container = CLUTTER_CONTAINER (oinfo->object);
1985 1986 1987 1988 1989 1990
  GList *l, *unresolved;

  unresolved = NULL;
  for (l = oinfo->children; l != NULL; l = l->next)
    {
      const gchar *name = l->data;
1991 1992
      GObject *object = NULL;
      ObjectInfo *child_info;
1993

1994 1995
      child_info = _clutter_script_get_object_info (script, name);
      if (child_info != NULL)
1996
        {
1997 1998
          _clutter_script_construct_object (script, child_info);
          object = child_info->object;
1999 2000
        }

2001
      if (object == NULL)
2002 2003 2004
        {
          unresolved = g_list_prepend (unresolved, g_strdup (name));
          continue;
2005 2006 2007 2008 2009