#include <stdlib.h>
#include <clutter/clutter.h>

static ClutterActor *group, *small_box;

static gboolean
key_release_event_cb (ClutterActor *stage, ClutterEvent *event, gpointer user_data)
{
  guint keysym = clutter_event_get_key_symbol (event);
  ClutterEffect *effect;

  if (keysym == CLUTTER_KEY_d)
    {
      if (clutter_actor_get_effect (group, "desaturate") != NULL)
        clutter_actor_remove_effect_by_name (group, "desaturate");
      else
        {
          effect = clutter_desaturate_effect_new (0.1);
          clutter_actor_add_effect_with_name (group, "desaturate", effect);
        }
    }
  else if (keysym == CLUTTER_KEY_c)
    {
      if (clutter_actor_get_effect (small_box, "desaturate") != NULL)
        {
          clutter_actor_remove_effect_by_name (small_box, "desaturate");
          clutter_actor_queue_relayout (stage);
        }
      else
        {
          effect = clutter_desaturate_effect_new (0.5);
          clutter_actor_add_effect_with_name (small_box, "desaturate", effect);
          clutter_actor_queue_relayout (stage);
        }
    }
}

int
main (int argc, char *argv[])
{
  ClutterActor *stage, *big_box, *text;

  if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
    return 1;

  stage = clutter_stage_get_default ();
  clutter_actor_set_size (stage, 640, 480);
  clutter_stage_set_color (CLUTTER_STAGE (stage), CLUTTER_COLOR_Black);

  group = clutter_group_new ();
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), group);
  clutter_actor_set_position (group, 300, 300);

  big_box = clutter_rectangle_new_with_color (CLUTTER_COLOR_Red);
  clutter_container_add_actor (CLUTTER_CONTAINER (group), big_box);
  clutter_actor_set_size (big_box, 400, 400);

  small_box = clutter_rectangle_new_with_color (CLUTTER_COLOR_Yellow);
  clutter_container_add_actor (CLUTTER_CONTAINER (group), small_box);
  clutter_actor_set_position (small_box, 50, 50);
  clutter_actor_set_size (small_box, 100, 100);

  text = clutter_text_new_full ("Mono 12", "Press 'd' to toggle effect on the container", CLUTTER_COLOR_White);
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), text);
  clutter_actor_set_position (text, 0, 10);

  text = clutter_text_new_full ("Mono 12", "Press 'c' to toggle effect on the child", CLUTTER_COLOR_White);
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), text);
  clutter_actor_set_position (text, 0, 30);

  clutter_actor_show_all (stage);

  g_signal_connect (stage, "key-release-event",
                    G_CALLBACK (key_release_event_cb), NULL);

  clutter_main ();

  return EXIT_SUCCESS;
}
