2007-08-08 06:22:00 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <clutter/clutter.h>
|
|
|
|
|
2011-10-13 05:38:36 -04:00
|
|
|
/* our thread-specific data */
|
2007-08-08 06:22:00 -04:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ClutterActor *stage;
|
|
|
|
ClutterActor *label;
|
2008-05-14 13:26:59 -04:00
|
|
|
ClutterActor *progress;
|
2012-06-04 18:16:30 -04:00
|
|
|
ClutterActor *rect;
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
ClutterTransition *flip;
|
|
|
|
ClutterTransition *bounce;
|
2007-08-08 06:22:00 -04:00
|
|
|
} TestThreadData;
|
|
|
|
|
|
|
|
static TestThreadData *
|
|
|
|
test_thread_data_new (void)
|
|
|
|
{
|
|
|
|
TestThreadData *data;
|
|
|
|
|
|
|
|
data = g_new0 (TestThreadData, 1);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-12 04:57:53 -04:00
|
|
|
test_thread_data_free (gpointer _data)
|
2007-08-08 06:22:00 -04:00
|
|
|
{
|
2011-10-12 04:57:53 -04:00
|
|
|
TestThreadData *data = _data;
|
|
|
|
|
|
|
|
if (data == NULL)
|
|
|
|
return;
|
|
|
|
|
2012-07-05 09:21:55 -04:00
|
|
|
g_print ("Removing thread data [%p]\n", _data);
|
|
|
|
|
|
|
|
g_clear_object (&data->progress);
|
|
|
|
g_clear_object (&data->label);
|
|
|
|
g_clear_object (&data->stage);
|
|
|
|
g_clear_object (&data->rect);
|
|
|
|
g_clear_object (&data->flip);
|
|
|
|
g_clear_object (&data->bounce);
|
2007-08-08 06:22:00 -04:00
|
|
|
|
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
test_thread_done_idle (gpointer user_data)
|
|
|
|
{
|
|
|
|
TestThreadData *data = user_data;
|
|
|
|
|
2012-07-05 09:21:55 -04:00
|
|
|
g_print ("Last update [%p]\n", data);
|
|
|
|
|
2008-12-11 06:37:46 -05:00
|
|
|
clutter_text_set_text (CLUTTER_TEXT (data->label), "Completed");
|
2012-06-04 18:16:30 -04:00
|
|
|
|
|
|
|
clutter_actor_remove_transition (data->rect, "bounce");
|
|
|
|
clutter_actor_remove_transition (data->rect, "flip");
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
return G_SOURCE_REMOVE;
|
2007-08-08 06:22:00 -04:00
|
|
|
}
|
|
|
|
|
2011-10-13 05:38:36 -04:00
|
|
|
static void
|
|
|
|
test_thread_data_done (gpointer _data)
|
|
|
|
{
|
2012-07-05 09:21:55 -04:00
|
|
|
if (_data == NULL)
|
|
|
|
return;
|
2011-10-13 05:38:36 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
g_print ("Thread completed\n");
|
|
|
|
|
2011-10-13 05:38:36 -04:00
|
|
|
/* since the TestThreadData structure references Clutter data structures
|
|
|
|
* we need to free it from within the same thread that called clutter_main()
|
|
|
|
* which means using an idle handler in the main loop.
|
|
|
|
*
|
|
|
|
* clutter_threads_add_idle() is guaranteed to run the callback passed to
|
|
|
|
* to it under the Big Clutter Lock.
|
|
|
|
*/
|
2012-07-05 09:21:55 -04:00
|
|
|
clutter_threads_add_idle_full (G_PRIORITY_DEFAULT,
|
|
|
|
test_thread_done_idle,
|
|
|
|
_data,
|
|
|
|
test_thread_data_free);
|
2011-10-13 05:38:36 -04:00
|
|
|
}
|
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
/* thread local storage */
|
2011-10-13 05:38:36 -04:00
|
|
|
static GPrivate test_thread_data = G_PRIVATE_INIT (test_thread_data_done);
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
typedef struct {
|
2007-08-08 06:22:00 -04:00
|
|
|
gint count;
|
|
|
|
TestThreadData *thread_data;
|
|
|
|
} TestUpdate;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
update_label_idle (gpointer data)
|
|
|
|
{
|
|
|
|
TestUpdate *update = data;
|
2008-05-14 13:26:59 -04:00
|
|
|
guint width;
|
2007-08-08 06:22:00 -04:00
|
|
|
gchar *text;
|
|
|
|
|
2012-07-05 09:21:55 -04:00
|
|
|
if (update->thread_data->label == NULL)
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
|
2007-08-08 06:22:00 -04:00
|
|
|
text = g_strdup_printf ("Count to %d", update->count);
|
2008-12-11 06:37:46 -05:00
|
|
|
clutter_text_set_text (CLUTTER_TEXT (update->thread_data->label), text);
|
2012-06-04 18:16:30 -04:00
|
|
|
|
2008-05-14 12:47:38 -04:00
|
|
|
clutter_actor_set_width (update->thread_data->label, -1);
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2008-05-14 13:26:59 -04:00
|
|
|
if (update->count == 0)
|
|
|
|
width = 0;
|
|
|
|
else if (update->count == 100)
|
|
|
|
width = 350;
|
|
|
|
else
|
|
|
|
width = (guint) (update->count / 100.0 * 350.0);
|
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
clutter_actor_save_easing_state (update->thread_data->progress);
|
2008-05-14 13:26:59 -04:00
|
|
|
clutter_actor_set_width (update->thread_data->progress, width);
|
2012-06-04 18:16:30 -04:00
|
|
|
clutter_actor_restore_easing_state (update->thread_data->progress);
|
2008-05-14 13:26:59 -04:00
|
|
|
|
2007-08-08 06:22:00 -04:00
|
|
|
g_free (text);
|
|
|
|
g_free (update);
|
2008-05-14 12:47:38 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
return G_SOURCE_REMOVE;
|
2007-08-08 06:22:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
do_something_very_slow (void)
|
|
|
|
{
|
|
|
|
TestThreadData *data;
|
|
|
|
gint i;
|
|
|
|
|
2011-10-11 12:51:14 -04:00
|
|
|
data = g_private_get (&test_thread_data);
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
for (i = 0; i <= 100; i++)
|
2007-08-08 06:22:00 -04:00
|
|
|
{
|
2008-05-14 12:47:38 -04:00
|
|
|
gint msecs;
|
|
|
|
|
2008-05-14 13:26:59 -04:00
|
|
|
msecs = 1 + (int) (100.0 * rand () / ((RAND_MAX + 1.0) / 3));
|
2008-05-14 12:47:38 -04:00
|
|
|
|
2011-10-13 05:38:36 -04:00
|
|
|
/* sleep for a while, to emulate some work being done */
|
2008-05-14 12:47:38 -04:00
|
|
|
g_usleep (msecs * 1000);
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2008-05-14 12:47:38 -04:00
|
|
|
if ((i % 10) == 0)
|
2007-08-08 06:22:00 -04:00
|
|
|
{
|
|
|
|
TestUpdate *update;
|
|
|
|
|
2011-10-13 05:38:36 -04:00
|
|
|
/* update the UI from within the main loop, making sure that the
|
|
|
|
* Big Clutter Lock is held; only one thread at a time can call
|
2012-07-05 09:21:55 -04:00
|
|
|
* Clutter API, and it's mandatory to do this from the same thread
|
2011-10-13 05:38:36 -04:00
|
|
|
* that called clutter_init()/clutter_main().
|
|
|
|
*/
|
2007-08-08 06:22:00 -04:00
|
|
|
update = g_new (TestUpdate, 1);
|
|
|
|
update->count = i;
|
|
|
|
update->thread_data = data;
|
2008-05-14 12:47:38 -04:00
|
|
|
|
2012-07-05 09:21:55 -04:00
|
|
|
clutter_threads_add_idle_full (G_PRIORITY_HIGH,
|
2008-05-14 13:26:59 -04:00
|
|
|
update_label_idle,
|
|
|
|
update, NULL);
|
2007-08-08 06:22:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
test_thread_func (gpointer user_data)
|
|
|
|
{
|
2011-10-11 12:51:14 -04:00
|
|
|
TestThreadData *data = user_data;
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2011-10-11 12:51:14 -04:00
|
|
|
g_private_set (&test_thread_data, data);
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2011-10-13 05:38:36 -04:00
|
|
|
/* this function will block */
|
2007-08-08 06:22:00 -04:00
|
|
|
do_something_very_slow ();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-14 13:26:59 -04:00
|
|
|
static ClutterActor *count_label = NULL;
|
|
|
|
static ClutterActor *help_label = NULL;
|
|
|
|
static ClutterActor *progress_rect = NULL;
|
2012-06-04 18:16:30 -04:00
|
|
|
static ClutterActor *rect = NULL;
|
|
|
|
static ClutterTransition *flip = NULL;
|
|
|
|
static ClutterTransition *bounce = NULL;
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2009-06-06 10:27:37 -04:00
|
|
|
static gboolean
|
|
|
|
on_key_press_event (ClutterStage *stage,
|
|
|
|
ClutterEvent *event,
|
|
|
|
gpointer user_data)
|
2007-08-08 06:22:00 -04:00
|
|
|
{
|
|
|
|
TestThreadData *data;
|
|
|
|
|
2009-06-06 10:27:37 -04:00
|
|
|
switch (clutter_event_get_key_symbol (event))
|
2007-08-08 06:22:00 -04:00
|
|
|
{
|
2010-09-10 08:41:49 -04:00
|
|
|
case CLUTTER_KEY_s:
|
2008-12-11 06:37:46 -05:00
|
|
|
clutter_text_set_text (CLUTTER_TEXT (help_label), "Press 'q' to quit");
|
2008-05-14 12:47:38 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
/* start the animations */
|
|
|
|
clutter_actor_add_transition (rect, "flip", flip);
|
|
|
|
clutter_actor_add_transition (rect, "bounce", bounce);
|
|
|
|
|
|
|
|
/* the data structure holding all our objects */
|
2007-08-08 06:22:00 -04:00
|
|
|
data = test_thread_data_new ();
|
|
|
|
data->stage = g_object_ref (stage);
|
|
|
|
data->label = g_object_ref (count_label);
|
2008-05-14 13:26:59 -04:00
|
|
|
data->progress = g_object_ref (progress_rect);
|
2012-06-04 18:16:30 -04:00
|
|
|
data->rect = g_object_ref (rect);
|
|
|
|
data->flip = g_object_ref (flip);
|
|
|
|
data->bounce = g_object_ref (bounce);
|
2011-10-11 12:51:14 -04:00
|
|
|
|
2011-10-13 05:38:36 -04:00
|
|
|
/* start the thread that updates the counter and the progress bar */
|
|
|
|
g_thread_new ("counter", test_thread_func, data);
|
2011-10-11 12:51:14 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
return CLUTTER_EVENT_STOP;
|
2009-06-06 10:27:37 -04:00
|
|
|
|
2010-09-10 08:41:49 -04:00
|
|
|
case CLUTTER_KEY_q:
|
2007-08-08 06:22:00 -04:00
|
|
|
clutter_main_quit ();
|
2011-10-11 12:51:14 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
return CLUTTER_EVENT_STOP;
|
2009-06-06 10:27:37 -04:00
|
|
|
|
|
|
|
default:
|
2007-08-08 06:22:00 -04:00
|
|
|
break;
|
|
|
|
}
|
2009-06-06 10:27:37 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
return CLUTTER_EVENT_PROPAGATE;
|
2007-08-08 06:22:00 -04:00
|
|
|
}
|
|
|
|
|
2012-05-01 13:30:10 -04:00
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
2007-08-08 06:22:00 -04:00
|
|
|
{
|
2012-06-04 18:16:30 -04:00
|
|
|
ClutterTransition *transition;
|
2007-08-08 06:22:00 -04:00
|
|
|
ClutterActor *stage;
|
2012-06-04 18:16:30 -04:00
|
|
|
ClutterPoint start = CLUTTER_POINT_INIT (75.f, 150.f);
|
|
|
|
ClutterPoint end = CLUTTER_POINT_INIT (400.f, 150.f);
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2011-02-21 19:19:35 -05:00
|
|
|
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
|
|
|
|
return 1;
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2010-10-11 12:45:30 -04:00
|
|
|
stage = clutter_stage_new ();
|
|
|
|
clutter_stage_set_title (CLUTTER_STAGE (stage), "Threading");
|
2012-02-13 11:47:17 -05:00
|
|
|
clutter_actor_set_background_color (stage, CLUTTER_COLOR_Aluminium3);
|
2008-05-14 13:26:59 -04:00
|
|
|
clutter_actor_set_size (stage, 600, 300);
|
2010-10-11 12:45:30 -04:00
|
|
|
g_signal_connect (stage, "destroy", G_CALLBACK (clutter_main_quit), NULL);
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2008-12-11 06:37:46 -05:00
|
|
|
count_label = clutter_text_new_with_text ("Mono 12", "Counter");
|
2007-08-08 06:22:00 -04:00
|
|
|
clutter_actor_set_position (count_label, 350, 50);
|
2012-06-04 18:16:30 -04:00
|
|
|
clutter_actor_add_child (stage, count_label);
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2008-12-11 06:37:46 -05:00
|
|
|
help_label = clutter_text_new_with_text ("Mono 12", "Press 's' to start");
|
2008-05-14 12:47:38 -04:00
|
|
|
clutter_actor_set_position (help_label, 50, 50);
|
2012-06-04 18:16:30 -04:00
|
|
|
clutter_actor_add_child (stage, help_label);
|
2007-08-08 06:22:00 -04:00
|
|
|
|
2012-06-04 18:16:30 -04:00
|
|
|
/* a progress bar */
|
|
|
|
progress_rect = clutter_actor_new ();
|
|
|
|
clutter_actor_set_background_color (progress_rect, CLUTTER_COLOR_DarkChameleon);
|
|
|
|
clutter_actor_set_position (progress_rect, 50, 225);
|
|
|
|
clutter_actor_set_size (progress_rect, 350, 50);
|
|
|
|
clutter_actor_add_child (stage, progress_rect);
|
|
|
|
|
|
|
|
/* an actor we bounce around */
|
|
|
|
rect = clutter_actor_new ();
|
|
|
|
clutter_actor_set_background_color (rect, CLUTTER_COLOR_LightScarletRed);
|
2008-05-14 12:47:38 -04:00
|
|
|
clutter_actor_set_position (rect, 75, 150);
|
|
|
|
clutter_actor_set_size (rect, 50, 50);
|
2012-07-06 13:06:33 -04:00
|
|
|
clutter_actor_set_pivot_point (rect, .5f, .5f);
|
2010-10-11 12:45:30 -04:00
|
|
|
clutter_actor_set_opacity (rect, 224);
|
2012-06-04 18:16:30 -04:00
|
|
|
clutter_actor_add_child (stage, rect);
|
|
|
|
|
|
|
|
/* two transitions we use to bounce rect around */
|
|
|
|
transition = clutter_property_transition_new ("rotation-angle-z");
|
|
|
|
clutter_transition_set_from (transition, G_TYPE_DOUBLE, 0.0);
|
|
|
|
clutter_transition_set_to (transition, G_TYPE_DOUBLE, 360.0);
|
|
|
|
clutter_timeline_set_repeat_count (CLUTTER_TIMELINE (transition), -1);
|
|
|
|
clutter_timeline_set_auto_reverse (CLUTTER_TIMELINE (transition), TRUE);
|
|
|
|
clutter_timeline_set_duration (CLUTTER_TIMELINE (transition), 3000);
|
|
|
|
flip = transition;
|
|
|
|
|
|
|
|
transition = clutter_property_transition_new ("position");
|
|
|
|
clutter_transition_set_from (transition, CLUTTER_TYPE_POINT, &start);
|
|
|
|
clutter_transition_set_to (transition, CLUTTER_TYPE_POINT, &end);
|
|
|
|
clutter_timeline_set_repeat_count (CLUTTER_TIMELINE (transition), -1);
|
|
|
|
clutter_timeline_set_auto_reverse (CLUTTER_TIMELINE (transition), TRUE);
|
|
|
|
clutter_timeline_set_duration (CLUTTER_TIMELINE (transition), 3000);
|
|
|
|
bounce = transition;
|
2007-08-08 06:22:00 -04:00
|
|
|
|
|
|
|
g_signal_connect (stage,
|
|
|
|
"button-press-event", G_CALLBACK (clutter_main_quit),
|
|
|
|
NULL);
|
|
|
|
g_signal_connect (stage,
|
|
|
|
"key-press-event", G_CALLBACK (on_key_press_event),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
clutter_actor_show (stage);
|
|
|
|
|
|
|
|
clutter_main ();
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|