/* ClutterScript viewer, a viewer for displaying clutter scripts or fragments * of clutterscript. * * Copyright 2007 OpenedHand Ltd * Copyright 2010 Intel Corp * * Authored by Øyvind Kolås * * Licensed under the GPL v2 or greater. */ #include #include #include #include #include #include /* Global structure containing information parsed from commandline parameters */ static struct { gboolean fullscreen; gchar *bg_color; gint width, height; gchar *path; gchar *id; gchar *timeline; gchar *png; } args = { FALSE, "gray", 640, 480, NULL, "root", NULL, NULL }; /* using global variables, this is needed at least for the ClutterScript to avoid * possible behaviours to be destroyed when the script is destroyed. */ static ClutterActor *stage; static ClutterActor *actor = NULL; static ClutterTimeline *timeline = NULL; static ClutterScript *script = NULL; gboolean parse_args (gchar **argv) { gchar **arg = argv + 1; while (*arg) { if (g_str_equal (*arg, "-h") || g_str_equal (*arg, "--help")) { usage: g_print ("\nUsage: %s [options] %s\n\n", argv[0], args.path ? args.path : ""); g_print (" -s stage size (%ix%i)\n", args.width, args.height); g_print (" -fs run fullscreen (%s)\n", args.fullscreen ? "TRUE" : "FALSE"); g_print (" -bg stage color (%s)\n", args.bg_color); g_print (" -id which actor id to show (%s)\n", args.id ? args.id : "NULL"); g_print (" -timeline a timeline to play (%s)\n", args.timeline ? args.timeline : "NULL"); g_print (" -o write screenshot, then quit (%s)\n", args.png? args.png: "NULL"); g_print (" -h this help\n\n"); return FALSE; } else if (g_str_equal (*arg, "-s")) { arg++; g_assert (*arg); args.width = atoi (*arg); if (strstr (*arg, "x")) args.height = atoi (strstr (*arg, "x") + 1); } else if (g_str_equal (*arg, "-bg")) { arg++; g_assert (*arg); args.bg_color = *arg; } else if (g_str_equal (*arg, "-id")) { arg++; g_assert (*arg); args.id = *arg; } else if (g_str_equal (*arg, "-timeline")) { arg++; g_assert (*arg); args.timeline = *arg; } else if (g_str_equal (*arg, "-o")) { arg++; g_assert (*arg); args.png = *arg; } else if (g_str_equal (*arg, "-fs")) { args.fullscreen = TRUE; } else { args.path = *arg; } arg++; } if (args.path == NULL) { g_print ("Error parsing commandline: no clutterscript provided\n"); goto usage; } return TRUE; } static ClutterActor * initialize_stage () { ClutterActor *stage; ClutterColor color; stage = clutter_stage_get_default (); clutter_actor_set_size (stage, args.width, args.height); clutter_color_from_string (&color, args.bg_color); clutter_stage_set_color (CLUTTER_STAGE (stage), &color); clutter_stage_set_fullscreen (CLUTTER_STAGE (stage), args.fullscreen); return stage; } static void load_script (const gchar *path) { GError *error = NULL; g_assert (CLUTTER_IS_SCRIPT (script)); clutter_script_load_from_file (script, path, &error); if (error) { ClutterColor error_color = { 0xff, 0, 0, 0xff }; actor = clutter_text_new_with_text ("Sans 20px", error->message); clutter_text_set_color (CLUTTER_TEXT (actor), &error_color); clutter_actor_set_size (actor, clutter_actor_get_width (stage), 200); g_print ("%s\n", error->message); clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor); clutter_actor_show_all (stage); g_clear_error (&error); return; } actor = CLUTTER_ACTOR (clutter_script_get_object (script, args.id)); if (actor == NULL) { ClutterColor error_color = { 0xff, 0, 0, 0xff }; gchar message[256]; g_sprintf (message, "No actor with \"id\"=\"%s\" found", args.id); actor = clutter_text_new_with_text ("Sans 30px", message); clutter_text_set_color (CLUTTER_TEXT (actor), &error_color); } else { clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor); clutter_actor_show_all (stage); if (args.timeline != NULL) { timeline = CLUTTER_TIMELINE (clutter_script_get_object ( script, args.timeline)); if (timeline) clutter_timeline_start (timeline); } } } static gboolean watch_file (gpointer data) { static struct stat stat_buf; static time_t previous_ctime = 0; g_stat (args.path, &stat_buf); if (stat_buf.st_ctime != previous_ctime) { if (script != NULL) g_object_unref (script); script = clutter_script_new (); if (actor != NULL) { clutter_actor_destroy (actor); } if (timeline != NULL) { timeline = NULL; } load_script (args.path); } previous_ctime = stat_buf.st_ctime; return TRUE; } gint main (gint argc, gchar **argv) { clutter_init (&argc, &argv); if (!parse_args (argv)) return -1; stage = initialize_stage (); g_timeout_add (1000, watch_file, NULL); clutter_main (); return 0; }