2007-08-08 10:22:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-11-07 19:32:28 +00:00
|
|
|
#include <gmodule.h>
|
2007-08-08 10:22:00 +00:00
|
|
|
#include <clutter/clutter.h>
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
ClutterActor *stage;
|
|
|
|
ClutterActor *label;
|
2008-05-14 17:26:59 +00:00
|
|
|
ClutterActor *progress;
|
2007-08-08 10:22:00 +00:00
|
|
|
|
|
|
|
ClutterTimeline *timeline;
|
|
|
|
|
|
|
|
volatile gboolean cancelled;
|
|
|
|
} TestThreadData;
|
|
|
|
|
|
|
|
static TestThreadData *
|
|
|
|
test_thread_data_new (void)
|
|
|
|
{
|
|
|
|
TestThreadData *data;
|
|
|
|
|
|
|
|
data = g_new0 (TestThreadData, 1);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_thread_data_free (TestThreadData *data)
|
|
|
|
{
|
2008-05-14 17:26:59 +00:00
|
|
|
g_object_unref (data->progress);
|
2007-08-08 10:22:00 +00:00
|
|
|
g_object_unref (data->label);
|
|
|
|
g_object_unref (data->stage);
|
|
|
|
g_object_unref (data->timeline);
|
|
|
|
|
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
test_thread_done_idle (gpointer user_data)
|
|
|
|
{
|
|
|
|
TestThreadData *data = user_data;
|
|
|
|
|
|
|
|
g_print ("Thread completed\n");
|
|
|
|
|
2008-12-11 11:37:46 +00:00
|
|
|
clutter_text_set_text (CLUTTER_TEXT (data->label), "Completed");
|
2007-08-08 10:22:00 +00:00
|
|
|
clutter_timeline_stop (data->timeline);
|
|
|
|
|
|
|
|
test_thread_data_free (data);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GStaticPrivate test_thread_data = G_STATIC_PRIVATE_INIT;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gint count;
|
|
|
|
TestThreadData *thread_data;
|
|
|
|
} TestUpdate;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
update_label_idle (gpointer data)
|
|
|
|
{
|
|
|
|
TestUpdate *update = data;
|
2008-05-14 17:26:59 +00:00
|
|
|
guint width;
|
2007-08-08 10:22:00 +00:00
|
|
|
gchar *text;
|
|
|
|
|
|
|
|
text = g_strdup_printf ("Count to %d", update->count);
|
|
|
|
|
2008-12-11 11:37:46 +00:00
|
|
|
clutter_text_set_text (CLUTTER_TEXT (update->thread_data->label), text);
|
2008-05-14 16:47:38 +00:00
|
|
|
clutter_actor_set_width (update->thread_data->label, -1);
|
2007-08-08 10:22:00 +00:00
|
|
|
|
2008-05-14 17:26:59 +00:00
|
|
|
if (update->count == 0)
|
|
|
|
width = 0;
|
|
|
|
else if (update->count == 100)
|
|
|
|
width = 350;
|
|
|
|
else
|
|
|
|
width = (guint) (update->count / 100.0 * 350.0);
|
|
|
|
|
|
|
|
clutter_actor_set_width (update->thread_data->progress, width);
|
|
|
|
|
2007-08-08 10:22:00 +00:00
|
|
|
g_free (text);
|
|
|
|
g_free (update);
|
2008-05-14 16:47:38 +00:00
|
|
|
|
2007-08-08 10:22:00 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
do_something_very_slow (void)
|
|
|
|
{
|
|
|
|
TestThreadData *data;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
data = (TestThreadData *) g_static_private_get (&test_thread_data);
|
|
|
|
if (data->cancelled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < 100; i++)
|
|
|
|
{
|
2008-05-14 16:47:38 +00:00
|
|
|
gint msecs;
|
|
|
|
|
2008-05-14 17:26:59 +00:00
|
|
|
msecs = 1 + (int) (100.0 * rand () / ((RAND_MAX + 1.0) / 3));
|
2008-05-14 16:47:38 +00:00
|
|
|
|
2007-08-08 10:22:00 +00:00
|
|
|
/* sleep for a while */
|
2008-05-14 16:47:38 +00:00
|
|
|
g_usleep (msecs * 1000);
|
2007-08-08 10:22:00 +00:00
|
|
|
|
2008-05-14 16:47:38 +00:00
|
|
|
if ((i % 10) == 0)
|
2007-08-08 10:22:00 +00:00
|
|
|
{
|
|
|
|
TestUpdate *update;
|
|
|
|
|
|
|
|
update = g_new (TestUpdate, 1);
|
|
|
|
update->count = i;
|
|
|
|
update->thread_data = data;
|
2008-05-14 16:47:38 +00:00
|
|
|
|
2008-05-14 17:26:59 +00:00
|
|
|
clutter_threads_add_idle_full (G_PRIORITY_DEFAULT + 30,
|
|
|
|
update_label_idle,
|
|
|
|
update, NULL);
|
2007-08-08 10:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
test_thread_func (gpointer user_data)
|
|
|
|
{
|
|
|
|
TestThreadData *data;
|
|
|
|
|
|
|
|
data = user_data;
|
|
|
|
g_static_private_set (&test_thread_data, data, NULL);
|
|
|
|
|
|
|
|
do_something_very_slow ();
|
|
|
|
|
2008-05-14 17:26:59 +00:00
|
|
|
clutter_threads_add_idle_full (G_PRIORITY_DEFAULT + 30,
|
|
|
|
test_thread_done_idle,
|
|
|
|
data, NULL);
|
2007-08-08 10:22:00 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-05-14 17:26:59 +00:00
|
|
|
static ClutterTimeline *timeline = NULL;
|
|
|
|
static ClutterActor *count_label = NULL;
|
|
|
|
static ClutterActor *help_label = NULL;
|
|
|
|
static ClutterActor *progress_rect = NULL;
|
2007-08-08 10:22:00 +00:00
|
|
|
|
2009-06-06 14:27:37 +00:00
|
|
|
static gboolean
|
|
|
|
on_key_press_event (ClutterStage *stage,
|
|
|
|
ClutterEvent *event,
|
|
|
|
gpointer user_data)
|
2007-08-08 10:22:00 +00:00
|
|
|
{
|
|
|
|
TestThreadData *data;
|
|
|
|
|
2009-06-06 14:27:37 +00:00
|
|
|
switch (clutter_event_get_key_symbol (event))
|
2007-08-08 10:22:00 +00:00
|
|
|
{
|
2010-09-10 12:41:49 +00:00
|
|
|
case CLUTTER_KEY_s:
|
2008-12-11 11:37:46 +00:00
|
|
|
clutter_text_set_text (CLUTTER_TEXT (help_label), "Press 'q' to quit");
|
2008-05-14 16:47:38 +00:00
|
|
|
|
2007-08-08 10:22:00 +00:00
|
|
|
clutter_timeline_start (timeline);
|
|
|
|
|
|
|
|
data = test_thread_data_new ();
|
|
|
|
data->stage = g_object_ref (stage);
|
|
|
|
data->label = g_object_ref (count_label);
|
2008-05-14 17:26:59 +00:00
|
|
|
data->progress = g_object_ref (progress_rect);
|
2007-08-08 10:22:00 +00:00
|
|
|
data->timeline = g_object_ref (timeline);
|
|
|
|
g_thread_create (test_thread_func, data, FALSE, NULL);
|
2009-06-06 14:27:37 +00:00
|
|
|
return TRUE;
|
|
|
|
|
2010-09-10 12:41:49 +00:00
|
|
|
case CLUTTER_KEY_q:
|
2007-08-08 10:22:00 +00:00
|
|
|
clutter_main_quit ();
|
2009-06-06 14:27:37 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
default:
|
2007-08-08 10:22:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-06-06 14:27:37 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2007-08-08 10:22:00 +00:00
|
|
|
}
|
|
|
|
|
2008-11-07 19:32:28 +00:00
|
|
|
G_MODULE_EXPORT int
|
|
|
|
test_threads_main (int argc, char *argv[])
|
2007-08-08 10:22:00 +00:00
|
|
|
{
|
|
|
|
ClutterActor *stage;
|
|
|
|
ClutterActor *rect;
|
|
|
|
ClutterColor stage_color = { 0xcc, 0xcc, 0xcc, 0xff };
|
|
|
|
ClutterColor rect_color = { 0xee, 0x55, 0x55, 0x99 };
|
2008-05-14 17:26:59 +00:00
|
|
|
ClutterColor progress_color = { 0x55, 0xee, 0x55, 0xbb };
|
2008-05-14 16:47:38 +00:00
|
|
|
ClutterBehaviour *r_behaviour, *p_behaviour;
|
2008-12-05 13:13:37 +00:00
|
|
|
ClutterAlpha *alpha;
|
2008-05-14 16:47:38 +00:00
|
|
|
const ClutterKnot knots[] = {
|
|
|
|
{ 75, 150 },
|
|
|
|
{ 400, 150 }
|
|
|
|
};
|
2007-08-08 10:22:00 +00:00
|
|
|
|
|
|
|
g_thread_init (NULL);
|
|
|
|
clutter_threads_init ();
|
|
|
|
clutter_init (&argc, &argv);
|
|
|
|
|
|
|
|
stage = clutter_stage_get_default ();
|
|
|
|
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
|
2008-05-14 17:26:59 +00:00
|
|
|
clutter_actor_set_size (stage, 600, 300);
|
2007-08-08 10:22:00 +00:00
|
|
|
|
2008-12-11 11:37:46 +00:00
|
|
|
count_label = clutter_text_new_with_text ("Mono 12", "Counter");
|
2007-08-08 10:22:00 +00:00
|
|
|
clutter_actor_set_position (count_label, 350, 50);
|
|
|
|
|
2008-12-11 11:37:46 +00:00
|
|
|
help_label = clutter_text_new_with_text ("Mono 12", "Press 's' to start");
|
2008-05-14 16:47:38 +00:00
|
|
|
clutter_actor_set_position (help_label, 50, 50);
|
2007-08-08 10:22:00 +00:00
|
|
|
|
|
|
|
rect = clutter_rectangle_new_with_color (&rect_color);
|
2008-05-14 16:47:38 +00:00
|
|
|
clutter_actor_set_position (rect, 75, 150);
|
|
|
|
clutter_actor_set_size (rect, 50, 50);
|
|
|
|
clutter_actor_set_anchor_point (rect, 25, 25);
|
2007-08-08 10:22:00 +00:00
|
|
|
|
2008-05-14 17:26:59 +00:00
|
|
|
progress_rect = clutter_rectangle_new_with_color (&progress_color);
|
|
|
|
clutter_actor_set_position (progress_rect, 50, 225);
|
|
|
|
clutter_actor_set_size (progress_rect, 350, 50);
|
|
|
|
|
2007-08-08 10:22:00 +00:00
|
|
|
clutter_container_add (CLUTTER_CONTAINER (stage),
|
2008-05-14 16:47:38 +00:00
|
|
|
count_label, help_label,
|
2008-05-14 17:26:59 +00:00
|
|
|
rect, progress_rect,
|
2007-08-08 10:22:00 +00:00
|
|
|
NULL);
|
|
|
|
|
2009-06-04 12:05:12 +00:00
|
|
|
timeline = clutter_timeline_new (3000);
|
2007-08-08 10:22:00 +00:00
|
|
|
clutter_timeline_set_loop (timeline, TRUE);
|
2009-01-16 13:42:06 +00:00
|
|
|
|
|
|
|
alpha = clutter_alpha_new_full (timeline, CLUTTER_LINEAR);
|
|
|
|
r_behaviour = clutter_behaviour_rotate_new (alpha,
|
2008-05-14 16:47:38 +00:00
|
|
|
CLUTTER_Z_AXIS,
|
|
|
|
CLUTTER_ROTATE_CW,
|
|
|
|
0.0, 360.0);
|
|
|
|
clutter_behaviour_apply (r_behaviour, rect);
|
|
|
|
|
2009-01-16 13:42:06 +00:00
|
|
|
alpha = clutter_alpha_new_full (timeline, CLUTTER_LINEAR);
|
2008-12-05 13:13:37 +00:00
|
|
|
p_behaviour = clutter_behaviour_path_new_with_knots (alpha,
|
|
|
|
knots,
|
|
|
|
G_N_ELEMENTS (knots));
|
2008-05-14 16:47:38 +00:00
|
|
|
clutter_behaviour_apply (p_behaviour, rect);
|
2007-08-08 10:22:00 +00: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);
|
|
|
|
|
2008-05-14 16:47:38 +00:00
|
|
|
clutter_threads_enter ();
|
2007-08-08 10:22:00 +00:00
|
|
|
clutter_main ();
|
|
|
|
clutter_threads_leave ();
|
|
|
|
|
2008-05-14 16:47:38 +00:00
|
|
|
g_object_unref (p_behaviour);
|
|
|
|
g_object_unref (r_behaviour);
|
2007-08-08 10:22:00 +00:00
|
|
|
g_object_unref (timeline);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|