4 * An OpenGL based 'interactive canvas' library.
6 * Authored By Matthew Allum <mallum@openedhand.com>
8 * Copyright (C) 2006 OpenedHand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
26 * SECTION:clutter-main
27 * @short_description: Various 'global' clutter functions.
29 * Functions to retrieve various global Clutter resources and other utility
30 * functions for mainloops, events and threads
38 #include <glib/gi18n-lib.h>
41 #include "clutter-event.h"
42 #include "clutter-backend.h"
43 #include "clutter-main.h"
44 #include "clutter-feature.h"
45 #include "clutter-actor.h"
46 #include "clutter-stage.h"
47 #include "clutter-private.h"
48 #include "clutter-debug.h"
49 #include "clutter-version.h" /* For flavour define */
50 #include "clutter-frame-source.h"
52 #include "cogl/cogl.h"
53 #include "pango/cogl-pango.h"
56 static ClutterMainContext *ClutterCntx = NULL;
58 /* main lock and locking/unlocking functions */
59 static GMutex *clutter_threads_mutex = NULL;
60 static GCallback clutter_threads_lock = NULL;
61 static GCallback clutter_threads_unlock = NULL;
63 static gboolean clutter_is_initialized = FALSE;
64 static gboolean clutter_show_fps = FALSE;
65 static gboolean clutter_fatal_warnings = FALSE;
67 static guint clutter_default_fps = 60;
69 static guint clutter_main_loop_level = 0;
70 static GSList *main_loops = NULL;
72 static PangoDirection clutter_text_direction = PANGO_DIRECTION_LTR;
74 guint clutter_debug_flags = 0; /* global clutter debug flag */
76 #ifdef CLUTTER_ENABLE_DEBUG
77 static const GDebugKey clutter_debug_keys[] = {
78 { "misc", CLUTTER_DEBUG_MISC },
79 { "actor", CLUTTER_DEBUG_ACTOR },
80 { "texture", CLUTTER_DEBUG_TEXTURE },
81 { "event", CLUTTER_DEBUG_EVENT },
82 { "paint", CLUTTER_DEBUG_PAINT },
83 { "gl", CLUTTER_DEBUG_GL },
84 { "alpha", CLUTTER_DEBUG_ALPHA },
85 { "behaviour", CLUTTER_DEBUG_BEHAVIOUR },
86 { "pango", CLUTTER_DEBUG_PANGO },
87 { "backend", CLUTTER_DEBUG_BACKEND },
88 { "scheduler", CLUTTER_DEBUG_SCHEDULER },
89 { "script", CLUTTER_DEBUG_SCRIPT },
90 { "shader", CLUTTER_DEBUG_SHADER },
91 { "multistage", CLUTTER_DEBUG_MULTISTAGE },
92 { "animation", CLUTTER_DEBUG_ANIMATION }
94 #endif /* CLUTTER_ENABLE_DEBUG */
97 * clutter_get_show_fps:
99 * Returns whether Clutter should print out the frames per second on the
100 * console. You can enable this setting either using the
101 * <literal>CLUTTER_SHOW_FPS</literal> environment variable or passing
102 * the <literal>--clutter-show-fps</literal> command line argument. *
104 * Return value: %TRUE if Clutter should show the FPS.
109 clutter_get_show_fps (void)
111 return clutter_show_fps;
115 _clutter_stage_maybe_relayout (ClutterActor *stage)
117 ClutterUnit natural_width, natural_height;
118 ClutterActorBox box = { 0, };
120 /* avoid reentrancy */
121 if (!(CLUTTER_PRIVATE_FLAGS (stage) & CLUTTER_ACTOR_IN_RELAYOUT))
123 CLUTTER_NOTE (ACTOR, "Recomputing layout");
125 CLUTTER_SET_PRIVATE_FLAGS (stage, CLUTTER_ACTOR_IN_RELAYOUT);
127 natural_width = natural_height = 0;
128 clutter_actor_get_preferred_size (stage,
130 &natural_width, &natural_height);
134 box.x2 = natural_width;
135 box.y2 = natural_height;
137 CLUTTER_NOTE (ACTOR, "Allocating (0, 0 - %d, %d) for the stage",
138 CLUTTER_UNITS_TO_DEVICE (natural_width),
139 CLUTTER_UNITS_TO_DEVICE (natural_height));
141 clutter_actor_allocate (stage, &box, FALSE);
143 CLUTTER_UNSET_PRIVATE_FLAGS (stage, CLUTTER_ACTOR_IN_RELAYOUT);
148 _clutter_stage_maybe_setup_viewport (ClutterStage *stage)
150 if (CLUTTER_PRIVATE_FLAGS (stage) & CLUTTER_ACTOR_SYNC_MATRICES)
152 ClutterPerspective perspective;
155 clutter_actor_get_size (CLUTTER_ACTOR (stage), &width, &height);
156 clutter_stage_get_perspectivex (stage, &perspective);
158 CLUTTER_NOTE (PAINT, "Setting up the viewport");
160 cogl_setup_viewport (width, height,
166 CLUTTER_UNSET_PRIVATE_FLAGS (stage, CLUTTER_ACTOR_SYNC_MATRICES);
173 * Forces a redraw of the entire stage. Applications should never use this
174 * function, but queue a redraw using clutter_actor_queue_redraw().
177 clutter_redraw (ClutterStage *stage)
179 ClutterMainContext *ctx;
180 static GTimer *timer = NULL;
181 static guint timer_n_frames = 0;
183 ctx = clutter_context_get_default ();
185 CLUTTER_TIMESTAMP (SCHEDULER, "Redraw start for stage:%p", stage);
186 CLUTTER_NOTE (PAINT, " Redraw enter for stage:%p", stage);
187 CLUTTER_NOTE (MULTISTAGE, "Redraw called for stage:%p", stage);
189 /* Before we can paint, we have to be sure we have the latest layout */
190 _clutter_stage_maybe_relayout (CLUTTER_ACTOR (stage));
192 _clutter_backend_ensure_context (ctx->backend, stage);
194 /* Setup FPS count - not currently across *all* stages rather than per */
195 if (G_UNLIKELY (clutter_get_show_fps ()))
198 timer = g_timer_new ();
201 /* The code below can't go in stage paint as base actor_paint
202 * will get called before it (and break picking, etc)
204 _clutter_stage_maybe_setup_viewport (stage);
206 /* Call through to the actual backend to do the painting down from
207 * the stage. It will likely need to swap buffers, vblank sync etc
208 * which will be windowing system dependant.
210 _clutter_backend_redraw (ctx->backend, stage);
212 /* Complete FPS info */
213 if (G_UNLIKELY (clutter_get_show_fps ()))
217 if (g_timer_elapsed (timer, NULL) >= 1.0)
219 g_print ("*** FPS: %i ***\n", timer_n_frames);
221 g_timer_start (timer);
225 CLUTTER_NOTE (PAINT, " Redraw leave for stage:%p", stage);
226 CLUTTER_TIMESTAMP (SCHEDULER, "Redraw finish for stage:%p", stage);
230 * clutter_set_motion_events_enabled:
231 * @enable: %TRUE to enable per-actor motion events
233 * Sets whether per-actor motion events should be enabled or not (the
234 * default is to enable them).
236 * If @enable is %FALSE the following events will not work:
238 * <listitem><para>ClutterActor::motion-event, unless on the
239 * #ClutterStage</para></listitem>
240 * <listitem><para>ClutterActor::enter-event</para></listitem>
241 * <listitem><para>ClutterActor::leave-event</para></listitem>
247 clutter_set_motion_events_enabled (gboolean enable)
249 ClutterMainContext *context = clutter_context_get_default ();
251 context->motion_events_per_actor = enable;
255 * clutter_get_motion_events_enabled:
257 * Gets whether the per-actor motion events are enabled.
259 * Return value: %TRUE if the motion events are enabled
264 clutter_get_motion_events_enabled (void)
266 ClutterMainContext *context = clutter_context_get_default ();
268 return context->motion_events_per_actor;
271 guint _clutter_pix_to_id (guchar pixel[4]);
273 static inline void init_bits (void)
275 ClutterMainContext *ctx;
277 static gboolean done = FALSE;
281 ctx = clutter_context_get_default ();
287 _clutter_id_to_color (guint id, ClutterColor *col)
289 ClutterMainContext *ctx;
290 gint red, green, blue;
291 ctx = clutter_context_get_default ();
293 /* compute the numbers we'll store in the components */
294 red = (id >> (ctx->fb_g_mask_used+ctx->fb_b_mask_used))
295 & (0xff >> (8-ctx->fb_r_mask_used));
296 green = (id >> ctx->fb_b_mask_used) & (0xff >> (8-ctx->fb_g_mask_used));
297 blue = (id) & (0xff >> (8-ctx->fb_b_mask_used));
299 /* shift left bits a bit and add one, this circumvents
300 * at least some potential rounding errors in GL/GLES
301 * driver / hw implementation.
303 if (ctx->fb_r_mask_used != ctx->fb_r_mask)
305 if (ctx->fb_g_mask_used != ctx->fb_g_mask)
307 if (ctx->fb_b_mask_used != ctx->fb_b_mask)
310 /* shift up to be full 8bit values */
311 red = (red << (8 - ctx->fb_r_mask)) | (0x7f >> (ctx->fb_r_mask_used));
312 green = (green << (8 - ctx->fb_g_mask)) | (0x7f >> (ctx->fb_g_mask_used));
313 blue = (blue << (8 - ctx->fb_b_mask)) | (0x7f >> (ctx->fb_b_mask_used));
322 _clutter_pixel_to_id (guchar pixel[4])
324 ClutterMainContext *ctx;
325 gint red, green, blue;
328 ctx = clutter_context_get_default ();
330 /* reduce the pixel components to the number of bits actually used of the
333 red = pixel[0] >> (8 - ctx->fb_r_mask);
334 green = pixel[1] >> (8 - ctx->fb_g_mask);
335 blue = pixel[2] >> (8 - ctx->fb_b_mask);
337 /* divide potentially by two if 'fuzzy' */
338 red = red >> (ctx->fb_r_mask - ctx->fb_r_mask_used);
339 green = green >> (ctx->fb_g_mask - ctx->fb_g_mask_used);
340 blue = blue >> (ctx->fb_b_mask - ctx->fb_b_mask_used);
342 /* combine the correct per component values into the final id */
343 id = blue + (green << ctx->fb_b_mask_used)
344 + (red << (ctx->fb_b_mask_used + ctx->fb_g_mask_used));
350 _clutter_do_pick (ClutterStage *stage,
353 ClutterPickMode mode)
355 ClutterMainContext *context;
360 GLboolean dither_was_on;
362 context = clutter_context_get_default ();
364 _clutter_backend_ensure_context (context->backend, stage);
366 /* needed for when a context switch happens */
367 _clutter_stage_maybe_setup_viewport (stage);
369 cogl_color_set_from_4ub (&white, 255, 255, 255, 255);
370 cogl_paint_init (&white);
372 /* Disable dithering (if any) when doing the painting in pick mode */
373 dither_was_on = glIsEnabled (GL_DITHER);
374 glDisable (GL_DITHER);
376 /* Render the entire scence in pick mode - just single colored silhouette's
377 * are drawn offscreen (as we never swap buffers)
379 context->pick_mode = mode;
380 clutter_actor_paint (CLUTTER_ACTOR (stage));
381 context->pick_mode = CLUTTER_PICK_NONE;
383 /* Calls should work under both GL and GLES, note GLES needs RGBA */
384 glGetIntegerv(GL_VIEWPORT, viewport);
386 /* Below to be safe, particularly on GL ES. an EGL wait call or full
391 /* Read the color of the screen co-ords pixel */
392 glReadPixels (x, viewport[3] - y -1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
394 /* Restore whether GL_DITHER was enabled */
396 glEnable (GL_DITHER);
398 if (pixel[0] == 0xff && pixel[1] == 0xff && pixel[2] == 0xff)
399 return CLUTTER_ACTOR (stage);
401 id = _clutter_pixel_to_id (pixel);
403 return clutter_get_actor_by_gid (id);
406 static PangoDirection
407 clutter_get_text_direction (void)
409 PangoDirection dir = PANGO_DIRECTION_LTR;
410 const gchar *direction;
412 direction = g_getenv ("CLUTTER_TEXT_DIRECTION");
413 if (direction && *direction != '\0')
415 if (strcmp (direction, "rtl") == 0)
416 dir = PANGO_DIRECTION_RTL;
417 else if (strcmp (direction, "ltr") == 0)
418 dir = PANGO_DIRECTION_LTR;
422 /* Translate to default:RTL if you want your widgets
423 * to be RTL, otherwise translate to default:LTR.
425 * Do *not* translate it to "predefinito:LTR": if it
426 * it isn't default:LTR or default:RTL it will not work
428 char *e = _("default:LTR");
430 if (strcmp (e, "default:RTL") == 0)
431 dir = PANGO_DIRECTION_RTL;
432 else if (strcmp (e, "default:LTR") == 0)
433 dir = PANGO_DIRECTION_LTR;
435 g_warning ("Whoever translated default:LTR did so wrongly.");
442 update_pango_context (ClutterBackend *backend,
443 PangoContext *context)
445 PangoFontDescription *font_desc;
446 cairo_font_options_t *font_options;
447 const gchar *font_name;
450 /* update the text direction */
451 pango_context_set_base_dir (context, clutter_text_direction);
453 /* get the configuration for the PangoContext from the backend */
454 font_name = clutter_backend_get_font_name (backend);
455 font_options = clutter_backend_get_font_options (backend);
456 resolution = clutter_backend_get_resolution (backend);
458 font_desc = pango_font_description_from_string (font_name);
461 resolution = 96.0; /* fall back */
463 pango_context_set_font_description (context, font_desc);
464 pango_cairo_context_set_font_options (context, font_options);
465 pango_cairo_context_set_resolution (context, resolution);
467 pango_font_description_free (font_desc);
471 _clutter_context_get_pango_context (ClutterMainContext *self)
473 if (G_UNLIKELY (self->pango_context == NULL))
475 PangoContext *context;
477 context = cogl_pango_font_map_create_context (self->font_map);
478 self->pango_context = context;
480 g_signal_connect (self->backend, "resolution-changed",
481 G_CALLBACK (update_pango_context),
482 self->pango_context);
483 g_signal_connect (self->backend, "font-changed",
484 G_CALLBACK (update_pango_context),
485 self->pango_context);
488 update_pango_context (self->backend, self->pango_context);
490 return self->pango_context;
494 _clutter_context_create_pango_context (ClutterMainContext *self)
496 PangoContext *context;
498 context = cogl_pango_font_map_create_context (self->font_map);
499 update_pango_context (self->backend, context);
507 * Terminates the Clutter mainloop.
510 clutter_main_quit (void)
512 g_return_if_fail (main_loops != NULL);
514 g_main_loop_quit (main_loops->data);
518 * clutter_main_level:
520 * Retrieves the depth of the Clutter mainloop.
522 * Return value: The level of the mainloop.
525 clutter_main_level (void)
527 return clutter_main_loop_level;
533 * Starts the Clutter mainloop.
540 /* Make sure there is a context */
543 if (!clutter_is_initialized)
545 g_warning ("Called clutter_main() but Clutter wasn't initialised. "
546 "You must call clutter_init() first.");
552 clutter_main_loop_level++;
554 loop = g_main_loop_new (NULL, TRUE);
555 main_loops = g_slist_prepend (main_loops, loop);
557 #ifdef HAVE_CLUTTER_FRUITY
558 /* clutter fruity creates an application that forwards events and manually
561 clutter_fruity_main ();
563 if (g_main_loop_is_running (main_loops->data))
565 clutter_threads_leave ();
566 g_main_loop_run (loop);
567 clutter_threads_enter ();
571 main_loops = g_slist_remove (main_loops, loop);
573 g_main_loop_unref (loop);
575 clutter_main_loop_level--;
581 clutter_threads_impl_lock (void)
583 if (clutter_threads_mutex)
584 g_mutex_lock (clutter_threads_mutex);
588 clutter_threads_impl_unlock (void)
590 if (clutter_threads_mutex)
591 g_mutex_unlock (clutter_threads_mutex);
595 * clutter_threads_init:
597 * Initialises the Clutter threading mechanism, so that Clutter API can be
598 * called by multiple threads, using clutter_threads_enter() and
599 * clutter_threads_leave() to mark the critical sections.
601 * You must call g_thread_init() before this function.
603 * This function must be called before clutter_init().
608 clutter_threads_init (void)
610 if (!g_thread_supported ())
611 g_error ("g_thread_init() must be called before clutter_threads_init()");
613 clutter_threads_mutex = g_mutex_new ();
615 if (!clutter_threads_lock)
616 clutter_threads_lock = clutter_threads_impl_lock;
618 if (!clutter_threads_unlock)
619 clutter_threads_unlock = clutter_threads_impl_unlock;
623 * clutter_threads_set_lock_functions:
624 * @enter_fn: function called when aquiring the Clutter main lock
625 * @leave_fn: function called when releasing the Clutter main lock
627 * Allows the application to replace the standard method that
628 * Clutter uses to protect its data structures. Normally, Clutter
629 * creates a single #GMutex that is locked by clutter_threads_enter(),
630 * and released by clutter_threads_leave(); using this function an
631 * application provides, instead, a function @enter_fn that is
632 * called by clutter_threads_enter() and a function @leave_fn that is
633 * called by clutter_threads_leave().
635 * The functions must provide at least same locking functionality
636 * as the default implementation, but can also do extra application
637 * specific processing.
639 * As an example, consider an application that has its own recursive
640 * lock that when held, holds the Clutter lock as well. When Clutter
641 * unlocks the Clutter lock when entering a recursive main loop, the
642 * application must temporarily release its lock as well.
644 * Most threaded Clutter apps won't need to use this method.
646 * This method must be called before clutter_threads_init(), and cannot
647 * be called multiple times.
652 clutter_threads_set_lock_functions (GCallback enter_fn,
655 g_return_if_fail (clutter_threads_lock == NULL &&
656 clutter_threads_unlock == NULL);
658 clutter_threads_lock = enter_fn;
659 clutter_threads_unlock = leave_fn;
666 GDestroyNotify notify;
667 } ClutterThreadsDispatch;
670 clutter_threads_dispatch (gpointer data)
672 ClutterThreadsDispatch *dispatch = data;
673 gboolean ret = FALSE;
675 clutter_threads_enter ();
677 if (!g_source_is_destroyed (g_main_current_source ()))
678 ret = dispatch->func (dispatch->data);
680 clutter_threads_leave ();
686 clutter_threads_dispatch_free (gpointer data)
688 ClutterThreadsDispatch *dispatch = data;
690 /* XXX - we cannot hold the thread lock here because the main loop
691 * might destroy a source while still in the dispatcher function; so
692 * knowing whether the lock is being held or not is not known a priori.
694 * see bug: http://bugzilla.gnome.org/show_bug.cgi?id=459555
696 if (dispatch->notify)
697 dispatch->notify (dispatch->data);
699 g_slice_free (ClutterThreadsDispatch, dispatch);
703 * clutter_threads_add_idle_full:
704 * @priority: the priority of the timeout source. Typically this will be in the
705 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE
706 * @func: function to call
707 * @data: data to pass to the function
708 * @notify: functio to call when the idle source is removed
710 * Adds a function to be called whenever there are no higher priority
711 * events pending. If the function returns %FALSE it is automatically
712 * removed from the list of event sources and will not be called again.
714 * This function can be considered a thread-safe variant of g_idle_add_full():
715 * it will call @function while holding the Clutter lock. It is logically
716 * equivalent to the following implementation:
720 * idle_safe_callback (gpointer data)
722 * SafeClosure *closure = data;
723 * gboolean res = FALSE;
725 * /* mark the critical section */
727 * clutter_threads_enter();
729 * /* the callback does not need to acquire the Clutter
730 * * lock itself, as it is held by the this proxy handler
732 * res = closure->callback (closure->data);
734 * clutter_threads_leave();
739 * add_safe_idle (GSourceFunc callback,
742 * SafeClosure *closure = g_new0 (SafeClosure, 1);
744 * closure->callback = callback;
745 * closure->data = data;
747 * return g_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
748 * idle_safe_callback,
754 * This function should be used by threaded applications to make sure
755 * that @func is emitted under the Clutter threads lock and invoked
756 * from the same thread that started the Clutter main loop. For instance,
757 * it can be used to update the UI using the results from a worker
762 * update_ui (gpointer data)
764 * SomeClosure *closure = data;
766 * /* it is safe to call Clutter API from this function because
767 * * it is invoked from the same thread that started the main
768 * * loop and under the Clutter thread lock
770 * clutter_label_set_text (CLUTTER_LABEL (closure->label),
773 * g_object_unref (closure->label);
779 * /* within another thread */
780 * closure = g_new0 (SomeClosure, 1);
781 * /* always take a reference on GObject instances */
782 * closure->label = g_object_ref (my_application->label);
783 * closure->text = g_strdup (processed_text_to_update_the_label);
785 * clutter_threads_add_idle_full (G_PRIORITY_HIGH_IDLE,
791 * Return value: the ID (greater than 0) of the event source.
796 clutter_threads_add_idle_full (gint priority,
799 GDestroyNotify notify)
801 ClutterThreadsDispatch *dispatch;
803 g_return_val_if_fail (func != NULL, 0);
805 dispatch = g_slice_new (ClutterThreadsDispatch);
806 dispatch->func = func;
807 dispatch->data = data;
808 dispatch->notify = notify;
810 return g_idle_add_full (priority,
811 clutter_threads_dispatch, dispatch,
812 clutter_threads_dispatch_free);
816 * clutter_threads_add_idle:
817 * @func: function to call
818 * @data: data to pass to the function
820 * Simple wrapper around clutter_threads_add_idle_full() using the
823 * Return value: the ID (greater than 0) of the event source.
828 clutter_threads_add_idle (GSourceFunc func,
831 g_return_val_if_fail (func != NULL, 0);
833 return clutter_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
839 * clutter_threads_add_timeout_full:
840 * @priority: the priority of the timeout source. Typically this will be in the
841 * range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
842 * @interval: the time between calls to the function, in milliseconds
843 * @func: function to call
844 * @data: data to pass to the function
845 * @notify: function to call when the timeout source is removed
847 * Sets a function to be called at regular intervals holding the Clutter
848 * threads lock, with the given priority. The function is called repeatedly
849 * until it returns %FALSE, at which point the timeout is automatically
850 * removed and the function will not be called again. The @notify function
851 * is called when the timeout is removed.
853 * The first call to the function will be at the end of the first @interval.
855 * It is important to note that, due to how the Clutter main loop is
856 * implemented, the timing will not be accurate and it will not try to
857 * "keep up" with the interval. A more reliable source is available
858 * using clutter_threads_add_frame_source_full(), which is also internally
859 * used by #ClutterTimeline.
861 * See also clutter_threads_add_idle_full().
863 * Return value: the ID (greater than 0) of the event source.
868 clutter_threads_add_timeout_full (gint priority,
872 GDestroyNotify notify)
874 ClutterThreadsDispatch *dispatch;
876 g_return_val_if_fail (func != NULL, 0);
878 dispatch = g_slice_new (ClutterThreadsDispatch);
879 dispatch->func = func;
880 dispatch->data = data;
881 dispatch->notify = notify;
883 return g_timeout_add_full (priority,
885 clutter_threads_dispatch, dispatch,
886 clutter_threads_dispatch_free);
890 * clutter_threads_add_timeout:
891 * @interval: the time between calls to the function, in milliseconds
892 * @func: function to call
893 * @data: data to pass to the function
895 * Simple wrapper around clutter_threads_add_timeout_full().
897 * Return value: the ID (greater than 0) of the event source.
902 clutter_threads_add_timeout (guint interval,
906 g_return_val_if_fail (func != NULL, 0);
908 return clutter_threads_add_timeout_full (G_PRIORITY_DEFAULT,
915 * clutter_threads_add_frame_source_full:
916 * @priority: the priority of the frame source. Typically this will be in the
917 * range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
918 * @interval: the time between calls to the function, in milliseconds
919 * @func: function to call
920 * @data: data to pass to the function
921 * @notify: function to call when the timeout source is removed
923 * Sets a function to be called at regular intervals holding the Clutter
924 * threads lock, with the given priority. The function is called repeatedly
925 * until it returns %FALSE, at which point the timeout is automatically
926 * removed and the function will not be called again. The @notify function
927 * is called when the timeout is removed.
929 * This function is similar to clutter_threads_add_timeout_full()
930 * except that it will try to compensate for delays. For example, if
931 * @func takes half the interval time to execute then the function
932 * will be called again half the interval time after it finished. In
933 * contrast clutter_threads_add_timeout_full() would not fire until a
934 * full interval after the function completes so the delay between
935 * calls would be @interval * 1.5. This function does not however try
936 * to invoke the function multiple times to catch up missing frames if
937 * @func takes more than @interval ms to execute.
939 * See also clutter_threads_add_idle_full().
941 * Return value: the ID (greater than 0) of the event source.
946 clutter_threads_add_frame_source_full (gint priority,
950 GDestroyNotify notify)
952 ClutterThreadsDispatch *dispatch;
954 g_return_val_if_fail (func != NULL, 0);
956 dispatch = g_slice_new (ClutterThreadsDispatch);
957 dispatch->func = func;
958 dispatch->data = data;
959 dispatch->notify = notify;
961 return clutter_frame_source_add_full (priority,
963 clutter_threads_dispatch, dispatch,
964 clutter_threads_dispatch_free);
968 * clutter_threads_add_frame_source:
969 * @interval: the time between calls to the function, in milliseconds
970 * @func: function to call
971 * @data: data to pass to the function
973 * Simple wrapper around clutter_threads_add_frame_source_full().
975 * Return value: the ID (greater than 0) of the event source.
980 clutter_threads_add_frame_source (guint interval,
984 g_return_val_if_fail (func != NULL, 0);
986 return clutter_threads_add_frame_source_full (G_PRIORITY_DEFAULT,
993 * clutter_threads_enter:
995 * Locks the Clutter thread lock.
1000 clutter_threads_enter (void)
1002 if (clutter_threads_lock)
1003 (* clutter_threads_lock) ();
1007 * clutter_threads_leave:
1009 * Unlocks the Clutter thread lock.
1014 clutter_threads_leave (void)
1016 if (clutter_threads_unlock)
1017 (* clutter_threads_unlock) ();
1022 * clutter_get_debug_enabled:
1024 * Check if clutter has debugging turned on.
1026 * Return value: TRUE if debugging is turned on, FALSE otherwise.
1029 clutter_get_debug_enabled (void)
1031 #ifdef CLUTTER_ENABLE_DEBUG
1032 return clutter_debug_flags != 0;
1038 ClutterMainContext *
1039 clutter_context_get_default (void)
1041 if (G_UNLIKELY(!ClutterCntx))
1043 ClutterMainContext *ctx;
1045 ClutterCntx = ctx = g_new0 (ClutterMainContext, 1);
1046 ctx->backend = g_object_new (_clutter_backend_impl_get_type (), NULL);
1048 ctx->is_initialized = FALSE;
1049 ctx->motion_events_per_actor = TRUE;
1051 #ifdef CLUTTER_ENABLE_DEBUG
1052 ctx->timer = g_timer_new ();
1053 g_timer_start (ctx->timer);
1061 * clutter_get_timestamp:
1063 * Returns the approximate number of microseconds passed since clutter was
1066 * Return value: Number of microseconds since clutter_init() was called.
1069 clutter_get_timestamp (void)
1071 #ifdef CLUTTER_ENABLE_DEBUG
1072 ClutterMainContext *ctx;
1075 ctx = clutter_context_get_default ();
1077 /* FIXME: may need a custom timer for embedded setups */
1078 seconds = g_timer_elapsed (ctx->timer, NULL);
1080 return (gulong)(seconds / 1.0e-6);
1087 clutter_arg_direction_cb (const char *key,
1091 clutter_text_direction =
1092 (strcmp (value, "rtl") == 0) ? PANGO_DIRECTION_RTL
1093 : PANGO_DIRECTION_LTR;
1098 #ifdef CLUTTER_ENABLE_DEBUG
1100 clutter_arg_debug_cb (const char *key,
1104 clutter_debug_flags |=
1105 g_parse_debug_string (value,
1107 G_N_ELEMENTS (clutter_debug_keys));
1112 clutter_arg_no_debug_cb (const char *key,
1116 clutter_debug_flags &=
1117 ~g_parse_debug_string (value,
1119 G_N_ELEMENTS (clutter_debug_keys));
1122 #endif /* CLUTTER_ENABLE_DEBUG */
1125 clutter_init_error_quark (void)
1127 return g_quark_from_static_string ("clutter-init-error-quark");
1130 static ClutterInitError
1131 clutter_init_real (GError **error)
1133 ClutterMainContext *ctx;
1134 ClutterActor *stage;
1136 ClutterBackend *backend;
1138 /* Note, creates backend if not already existing, though parse args will
1139 * have likely created it
1141 ctx = clutter_context_get_default ();
1142 backend = ctx->backend;
1144 if (!ctx->options_parsed)
1146 g_set_error (error, CLUTTER_INIT_ERROR,
1147 CLUTTER_INIT_ERROR_INTERNAL,
1148 "When using clutter_get_option_group_without_init() "
1149 "you must parse options before calling clutter_init()");
1151 return CLUTTER_INIT_ERROR_INTERNAL;
1155 * Call backend post parse hooks.
1157 if (!_clutter_backend_post_parse (backend, error))
1158 return CLUTTER_INIT_ERROR_BACKEND;
1160 /* Stage will give us a GL Context etc */
1161 stage = clutter_stage_get_default ();
1165 g_set_error (error, CLUTTER_INIT_ERROR,
1166 CLUTTER_INIT_ERROR_INTERNAL,
1167 "Unable to create the default stage");
1169 g_critical ("Unable to create the default stage");
1171 return CLUTTER_INIT_ERROR_INTERNAL;
1174 clutter_actor_realize (stage);
1176 if (!CLUTTER_ACTOR_IS_REALIZED (stage))
1179 g_set_error (error, CLUTTER_INIT_ERROR,
1180 CLUTTER_INIT_ERROR_INTERNAL,
1181 "Unable to realize the default stage");
1183 g_critical ("Unable to realize the default stage");
1185 return CLUTTER_INIT_ERROR_INTERNAL;
1188 /* Now we can safely assume we have a valid GL context and can
1189 * start issueing cogl commands
1193 * Resolution requires display to be open, so can only be queried after
1194 * the post_parse hooks run.
1196 * NB: cogl_pango requires a Cogl context.
1198 ctx->font_map = COGL_PANGO_FONT_MAP (cogl_pango_font_map_new ());
1200 resolution = clutter_backend_get_resolution (ctx->backend);
1201 cogl_pango_font_map_set_resolution (ctx->font_map, resolution);
1202 cogl_pango_font_map_set_use_mipmapping (ctx->font_map, TRUE);
1204 clutter_text_direction = clutter_get_text_direction ();
1207 /* Figure out framebuffer masks used for pick */
1208 cogl_get_bitmasks (&ctx->fb_r_mask, &ctx->fb_g_mask, &ctx->fb_b_mask, NULL);
1210 ctx->fb_r_mask_used = ctx->fb_r_mask;
1211 ctx->fb_g_mask_used = ctx->fb_g_mask;
1212 ctx->fb_b_mask_used = ctx->fb_b_mask;
1214 #ifndef HAVE_CLUTTER_FRUITY
1215 /* We always do fuzzy picking for the fruity backend */
1216 if (g_getenv ("CLUTTER_FUZZY_PICK") != NULL)
1219 ctx->fb_r_mask_used--;
1220 ctx->fb_g_mask_used--;
1221 ctx->fb_b_mask_used--;
1224 /* Initiate event collection */
1225 _clutter_backend_init_events (ctx->backend);
1227 /* finally features - will call to backend and cogl */
1228 _clutter_feature_init ();
1230 clutter_stage_set_title (CLUTTER_STAGE (stage), g_get_prgname ());
1232 clutter_is_initialized = TRUE;
1233 ctx->is_initialized = TRUE;
1235 return CLUTTER_INIT_SUCCESS;
1238 static GOptionEntry clutter_args[] = {
1239 { "clutter-show-fps", 0, 0, G_OPTION_ARG_NONE, &clutter_show_fps,
1240 N_("Show frames per second"), NULL },
1241 { "clutter-default-fps", 0, 0, G_OPTION_ARG_INT, &clutter_default_fps,
1242 N_("Default frame rate"), "FPS" },
1243 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &clutter_fatal_warnings,
1244 N_("Make all warnings fatal"), NULL },
1245 { "clutter-text-direction", 0, 0, G_OPTION_ARG_CALLBACK,
1246 clutter_arg_direction_cb,
1247 N_("Direction for the text"), "DIRECTION" },
1248 #ifdef CLUTTER_ENABLE_DEBUG
1249 { "clutter-debug", 0, 0, G_OPTION_ARG_CALLBACK, clutter_arg_debug_cb,
1250 N_("Clutter debugging flags to set"), "FLAGS" },
1251 { "clutter-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, clutter_arg_no_debug_cb,
1252 N_("Clutter debugging flags to unset"), "FLAGS" },
1253 #endif /* CLUTTER_ENABLE_DEBUG */
1257 /* pre_parse_hook: initialise variables depending on environment
1258 * variables; these variables might be overridden by the command
1259 * line arguments that are going to be parsed after.
1262 pre_parse_hook (GOptionContext *context,
1263 GOptionGroup *group,
1267 ClutterMainContext *clutter_context;
1268 ClutterBackend *backend;
1269 const char *env_string;
1271 if (clutter_is_initialized)
1274 if (setlocale (LC_ALL, "") == NULL)
1275 g_warning ("Locale not supported by C library.\n"
1276 "Using the fallback 'C' locale.");
1278 clutter_context = clutter_context_get_default ();
1280 clutter_context->id_pool = clutter_id_pool_new (256);
1282 backend = clutter_context->backend;
1283 g_assert (CLUTTER_IS_BACKEND (backend));
1285 #ifdef CLUTTER_ENABLE_DEBUG
1286 env_string = g_getenv ("CLUTTER_DEBUG");
1287 if (env_string != NULL)
1289 clutter_debug_flags =
1290 g_parse_debug_string (env_string,
1292 G_N_ELEMENTS (clutter_debug_keys));
1295 #endif /* CLUTTER_ENABLE_DEBUG */
1297 env_string = g_getenv ("CLUTTER_SHOW_FPS");
1299 clutter_show_fps = TRUE;
1301 env_string = g_getenv ("CLUTTER_DEFAULT_FPS");
1304 gint default_fps = g_ascii_strtoll (env_string, NULL, 10);
1306 clutter_default_fps = CLAMP (default_fps, 1, 1000);
1309 return _clutter_backend_pre_parse (backend, error);
1312 /* post_parse_hook: initialise the context and data structures
1313 * and opens the X display
1316 post_parse_hook (GOptionContext *context,
1317 GOptionGroup *group,
1321 ClutterMainContext *clutter_context;
1322 ClutterBackend *backend;
1324 if (clutter_is_initialized)
1327 clutter_context = clutter_context_get_default ();
1328 backend = clutter_context->backend;
1329 g_assert (CLUTTER_IS_BACKEND (backend));
1331 if (clutter_fatal_warnings)
1333 GLogLevelFlags fatal_mask;
1335 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1336 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1337 g_log_set_always_fatal (fatal_mask);
1340 clutter_context->frame_rate = clutter_default_fps;
1341 clutter_context->options_parsed = TRUE;
1344 * If not asked to defer display setup, call clutter_init_real(),
1345 * which in turn calls the backend post parse hooks.
1347 if (!clutter_context->defer_display_setup)
1348 return clutter_init_real (error);
1354 * clutter_get_option_group:
1356 * Returns a #GOptionGroup for the command line arguments recognized
1357 * by Clutter. You should add this group to your #GOptionContext with
1358 * g_option_context_add_group(), if you are using g_option_context_parse()
1359 * to parse your commandline arguments.
1361 * Calling g_option_context_parse() with Clutter's #GOptionGroup will result
1362 * in Clutter's initialization. That is, the following code:
1365 * g_option_context_set_main_group (context, clutter_get_option_group ());
1366 * res = g_option_context_parse (context, &argc, &argc, NULL);
1369 * is functionally equivalent to:
1372 * clutter_init (&argc, &argv);
1375 * After g_option_context_parse() on a #GOptionContext containing the
1376 * Clutter #GOptionGroup has returned %TRUE, Clutter is guaranteed to be
1379 * Return value: a #GOptionGroup for the commandline arguments
1380 * recognized by Clutter
1385 clutter_get_option_group (void)
1387 ClutterMainContext *context;
1388 GOptionGroup *group;
1390 clutter_base_init ();
1392 context = clutter_context_get_default ();
1394 group = g_option_group_new ("clutter",
1395 _("Clutter Options"),
1396 _("Show Clutter Options"),
1400 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
1401 g_option_group_add_entries (group, clutter_args);
1402 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
1404 /* add backend-specific options */
1405 _clutter_backend_add_options (context->backend, group);
1411 * clutter_get_option_group_without_init:
1413 * Returns a #GOptionGroup for the command line arguments recognized
1414 * by Clutter. You should add this group to your #GOptionContext with
1415 * g_option_context_add_group(), if you are using g_option_context_parse()
1416 * to parse your commandline arguments. Unlike clutter_get_option_group(),
1417 * calling g_option_context_parse() with the #GOptionGroup returned by this
1418 * function requires a subsequent explicit call to clutter_init(); use this
1419 * function when needing to set foreign display connection with
1420 * clutter_x11_set_display(), or with gtk_clutter_init().
1422 * Return value: a #GOptionGroup for the commandline arguments
1423 * recognized by Clutter
1428 clutter_get_option_group_without_init (void)
1430 ClutterMainContext *context;
1431 GOptionGroup *group;
1433 clutter_base_init ();
1435 context = clutter_context_get_default ();
1436 context->defer_display_setup = TRUE;
1438 group = clutter_get_option_group ();
1444 * clutter_init_with_args:
1445 * @argc: a pointer to the number of command line arguments
1446 * @argv: a pointer to the array of command line arguments
1447 * @parameter_string: a string which is displayed in the
1448 * first line of <option>--help</option> output, after
1449 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
1450 * @entries: a %NULL terminated array of #GOptionEntry<!-- -->s
1451 * describing the options of your program
1452 * @translation_domain: a translation domain to use for translating
1453 * the <option>--help</option> output for the options in @entries
1454 * with gettext(), or %NULL
1455 * @error: a return location for a #GError
1457 * This function does the same work as clutter_init(). Additionally,
1458 * it allows you to add your own command line options, and it
1459 * automatically generates nicely formatted <option>--help</option>
1460 * output. Note that your program will be terminated after writing
1461 * out the help output. Also note that, in case of error, the
1462 * error message will be placed inside @error instead of being
1463 * printed on the display.
1465 * Return value: %CLUTTER_INIT_SUCCESS if Clutter has been successfully
1466 * initialised, or other values or #ClutterInitError in case of
1472 clutter_init_with_args (int *argc,
1474 const char *parameter_string,
1475 GOptionEntry *entries,
1476 const char *translation_domain,
1479 GOptionContext *context;
1480 GOptionGroup *group;
1482 ClutterMainContext *ctx;
1484 if (clutter_is_initialized)
1485 return CLUTTER_INIT_SUCCESS;
1487 clutter_base_init ();
1489 ctx = clutter_context_get_default ();
1491 if (!ctx->defer_display_setup)
1493 if (argc && *argc > 0 && *argv)
1494 g_set_prgname ((*argv)[0]);
1496 group = clutter_get_option_group ();
1497 context = g_option_context_new (parameter_string);
1499 g_option_context_add_group (context, group);
1502 g_option_context_add_main_entries (context, entries, translation_domain);
1504 res = g_option_context_parse (context, argc, argv, error);
1505 g_option_context_free (context);
1507 /* if res is FALSE, the error is filled for
1508 * us by g_option_context_parse()
1512 /* if there has been an error in the initialization, the
1513 * error id will be preserved inside the GError code
1515 if (error && *error)
1516 return (*error)->code;
1518 return CLUTTER_INIT_ERROR_INTERNAL;
1521 return CLUTTER_INIT_SUCCESS;
1524 return clutter_init_real (error);
1528 clutter_parse_args (int *argc,
1531 GOptionContext *option_context;
1532 GOptionGroup *clutter_group;
1533 GError *error = NULL;
1534 gboolean ret = TRUE;
1536 if (clutter_is_initialized)
1539 option_context = g_option_context_new (NULL);
1540 g_option_context_set_ignore_unknown_options (option_context, TRUE);
1541 g_option_context_set_help_enabled (option_context, FALSE);
1543 /* Initiate any command line options from the backend */
1545 clutter_group = clutter_get_option_group ();
1546 g_option_context_set_main_group (option_context, clutter_group);
1548 if (!g_option_context_parse (option_context, argc, argv, &error))
1552 g_warning ("%s", error->message);
1553 g_error_free (error);
1559 g_option_context_free (option_context);
1566 * @argc: The number of arguments in @argv
1567 * @argv: A pointer to an array of arguments.
1569 * It will initialise everything needed to operate with Clutter and
1570 * parses some standard command line options. @argc and @argv are
1571 * adjusted accordingly so your own code will never see those standard
1574 * Return value: 1 on success, < 0 on failure.
1577 clutter_init (int *argc,
1580 ClutterMainContext *ctx;
1581 GError *error = NULL;
1583 if (clutter_is_initialized)
1584 return CLUTTER_INIT_SUCCESS;
1586 clutter_base_init ();
1588 ctx = clutter_context_get_default ();
1590 if (!ctx->defer_display_setup)
1592 if (argc && *argc > 0 && *argv)
1593 g_set_prgname ((*argv)[0]);
1595 /* parse_args will trigger backend creation and things like
1596 * DISPLAY connection etc.
1598 if (clutter_parse_args (argc, argv) == FALSE)
1600 CLUTTER_NOTE (MISC, "failed to parse arguments.");
1601 return CLUTTER_INIT_ERROR_INTERNAL;
1604 return CLUTTER_INIT_SUCCESS;
1607 return clutter_init_real (&error);
1611 _clutter_boolean_handled_accumulator (GSignalInvocationHint *ihint,
1612 GValue *return_accu,
1613 const GValue *handler_return,
1616 gboolean continue_emission;
1617 gboolean signal_handled;
1619 signal_handled = g_value_get_boolean (handler_return);
1620 g_value_set_boolean (return_accu, signal_handled);
1621 continue_emission = !signal_handled;
1623 return continue_emission;
1627 event_click_count_generate (ClutterEvent *event)
1629 /* multiple button click detection */
1630 static gint click_count = 0;
1631 static gint previous_x = -1;
1632 static gint previous_y = -1;
1633 static guint32 previous_time = 0;
1634 static gint previous_button_number = -1;
1636 ClutterBackend *backend;
1637 guint double_click_time;
1638 guint double_click_distance;
1640 backend = clutter_context_get_default ()->backend;
1641 double_click_distance = clutter_backend_get_double_click_distance (backend);
1642 double_click_time = clutter_backend_get_double_click_time (backend);
1644 if (event->button.device != NULL)
1646 click_count = event->button.device->click_count;
1647 previous_x = event->button.device->previous_x;
1648 previous_y = event->button.device->previous_y;
1649 previous_time = event->button.device->previous_time;
1650 previous_button_number = event->button.device->previous_button_number;
1653 switch (event->type)
1655 case CLUTTER_BUTTON_PRESS:
1656 case CLUTTER_SCROLL:
1657 /* check if we are in time and within distance to increment an
1658 * existing click count
1660 if (event->button.time < previous_time + double_click_time &&
1661 (ABS (event->button.x - previous_x) <= double_click_distance) &&
1662 (ABS (event->button.y - previous_y) <= double_click_distance)
1663 && event->button.button == previous_button_number)
1667 else /* start a new click count*/
1670 previous_button_number = event->button.button;
1673 /* store time and position for this click for comparison with
1676 previous_time = event->button.time;
1677 previous_x = event->button.x;
1678 previous_y = event->button.y;
1681 case CLUTTER_BUTTON_RELEASE:
1682 event->button.click_count=click_count;
1688 if (event->button.device != NULL)
1690 event->button.device->click_count = click_count;
1691 event->button.device->previous_x = previous_x;
1692 event->button.device->previous_y = previous_y;
1693 event->button.device->previous_time = previous_time;
1694 event->button.device->previous_button_number = previous_button_number;
1700 emit_event (ClutterEvent *event,
1701 gboolean is_key_event)
1703 #define MAX_EVENT_DEPTH 512
1705 static ClutterActor **event_tree = NULL;
1706 static gboolean lock = FALSE;
1708 ClutterActor *actor;
1709 gint i = 0, n_tree_events = 0;
1711 if (!event->any.source)
1713 g_warning ("No event source set, discarding event");
1717 /* reentrancy check */
1723 /* Sorry Mr Bassi. */
1724 if (G_UNLIKELY (event_tree == NULL))
1725 event_tree = g_new0 (ClutterActor *, MAX_EVENT_DEPTH);
1727 actor = event->any.source;
1729 /* Build 'tree' of emitters for the event */
1730 while (actor && n_tree_events < MAX_EVENT_DEPTH)
1732 ClutterActor *parent;
1734 parent = clutter_actor_get_parent (actor);
1736 if (clutter_actor_get_reactive (actor) ||
1737 parent == NULL || /* stage gets all events */
1738 is_key_event) /* keyboard events are always emitted */
1740 event_tree[n_tree_events++] = g_object_ref (actor);
1747 for (i = n_tree_events-1; i >= 0; i--)
1748 if (clutter_actor_event (event_tree[i], event, TRUE))
1752 for (i = 0; i < n_tree_events; i++)
1753 if (clutter_actor_event (event_tree[i], event, FALSE))
1758 for (i = 0; i < n_tree_events; i++)
1759 g_object_unref (event_tree[i]);
1763 #undef MAX_EVENT_DEPTH
1767 * Emits a pointer event after having prepared the event for delivery (setting
1768 * source, computing click_count, generating enter/leave etc.).
1772 emit_pointer_event (ClutterEvent *event,
1773 ClutterInputDevice *device)
1775 /* Using the global variable directly, since it has to be initialized
1778 ClutterMainContext *context = ClutterCntx;
1780 if (G_UNLIKELY (context->pointer_grab_actor != NULL &&
1784 clutter_actor_event (context->pointer_grab_actor, event, FALSE);
1786 else if (G_UNLIKELY (device != NULL &&
1787 device->pointer_grab_actor != NULL))
1789 /* per device grab */
1790 clutter_actor_event (device->pointer_grab_actor, event, FALSE);
1794 /* no grab, time to capture and bubble */
1795 emit_event (event, FALSE);
1800 emit_keyboard_event (ClutterEvent *event)
1802 ClutterMainContext *context = ClutterCntx;
1804 if (G_UNLIKELY (context->keyboard_grab_actor != NULL))
1805 clutter_actor_event (context->keyboard_grab_actor, event, FALSE);
1807 emit_event (event, TRUE);
1811 unset_motion_last_actor (ClutterActor *actor, ClutterInputDevice *dev)
1813 ClutterMainContext *context = ClutterCntx;
1816 context->motion_last_actor = NULL;
1818 dev->motion_last_actor = NULL;
1821 static ClutterInputDevice * clutter_event_get_device (ClutterEvent *event);
1823 /* This function should perhaps be public and in clutter-event.c ?
1825 static ClutterInputDevice *
1826 clutter_event_get_device (ClutterEvent *event)
1828 g_return_val_if_fail (event != NULL, NULL);
1830 switch (event->type)
1832 case CLUTTER_NOTHING:
1833 case CLUTTER_STAGE_STATE:
1834 case CLUTTER_DESTROY_NOTIFY:
1835 case CLUTTER_CLIENT_MESSAGE:
1836 case CLUTTER_DELETE:
1841 case CLUTTER_BUTTON_PRESS:
1842 case CLUTTER_BUTTON_RELEASE:
1843 return event->button.device;
1844 case CLUTTER_MOTION:
1845 return event->motion.device;
1846 case CLUTTER_SCROLL:
1847 return event->scroll.device;
1849 case CLUTTER_KEY_PRESS:
1850 case CLUTTER_KEY_RELEASE:
1857 generate_enter_leave_events (ClutterEvent *event)
1859 ClutterMainContext *context = ClutterCntx;
1860 ClutterActor *motion_current_actor = event->motion.source;
1861 ClutterActor *last_actor = context->motion_last_actor;
1862 ClutterInputDevice *device = clutter_event_get_device (event);
1865 last_actor = device->motion_last_actor;
1867 if (last_actor != motion_current_actor)
1869 if (motion_current_actor)
1874 cev.crossing.device = device;
1875 clutter_event_get_coords (event, &x, &y);
1877 if (context->motion_last_actor)
1879 cev.crossing.type = CLUTTER_LEAVE;
1880 cev.crossing.time = event->any.time;
1881 cev.crossing.flags = 0;
1884 cev.crossing.source = last_actor;
1885 cev.crossing.stage = event->any.stage;
1886 cev.crossing.related = motion_current_actor;
1888 emit_pointer_event (&cev, device);
1891 cev.crossing.type = CLUTTER_ENTER;
1892 cev.crossing.time = event->any.time;
1893 cev.crossing.flags = 0;
1896 cev.crossing.source = motion_current_actor;
1897 cev.crossing.stage = event->any.stage;
1899 if (context->motion_last_actor)
1900 cev.crossing.related = last_actor;
1902 cev.crossing.related = NULL;
1904 emit_pointer_event (&cev, device);
1908 if (last_actor && last_actor != motion_current_actor)
1910 g_signal_handlers_disconnect_by_func
1912 G_CALLBACK (unset_motion_last_actor),
1916 if (motion_current_actor && last_actor != motion_current_actor)
1918 g_signal_connect (motion_current_actor, "destroy",
1919 G_CALLBACK (unset_motion_last_actor),
1924 device->motion_last_actor = motion_current_actor;
1926 context->motion_last_actor = motion_current_actor;
1931 * @event: a #ClutterEvent.
1933 * Processes an event. This function should never be called by applications.
1938 clutter_do_event (ClutterEvent *event)
1940 /* FIXME: This should probably be clutter_cook_event() - it would
1941 * take a raw event from the backend and 'cook' it so its more tasty.
1944 ClutterMainContext *context;
1945 ClutterBackend *backend;
1946 ClutterActor *stage;
1947 ClutterInputDevice *device = NULL;
1948 static gint32 motion_last_time = 0L;
1949 gint32 local_motion_time;
1951 context = clutter_context_get_default ();
1952 backend = context->backend;
1953 stage = CLUTTER_ACTOR(event->any.stage);
1958 CLUTTER_TIMESTAMP (EVENT, "Event received");
1960 switch (event->type)
1962 case CLUTTER_NOTHING:
1963 event->any.source = stage;
1968 emit_pointer_event (event, event->crossing.device);
1971 case CLUTTER_DESTROY_NOTIFY:
1972 case CLUTTER_DELETE:
1973 event->any.source = stage;
1974 /* the stage did not handle the event, so we just quit */
1975 if (!clutter_stage_event (CLUTTER_STAGE (stage), event))
1977 if (stage == clutter_stage_get_default())
1978 clutter_main_quit ();
1980 clutter_actor_destroy (stage);
1985 case CLUTTER_KEY_PRESS:
1986 case CLUTTER_KEY_RELEASE:
1988 ClutterActor *actor = NULL;
1990 /* check that we're not a synthetic event with source set */
1991 if (event->any.source == NULL)
1993 actor = clutter_stage_get_key_focus (CLUTTER_STAGE (stage));
1994 event->any.source = actor;
1995 if (G_UNLIKELY (actor == NULL))
1997 g_warning ("No key focus set, discarding");
2002 emit_keyboard_event (event);
2006 case CLUTTER_MOTION:
2007 device = event->motion.device;
2010 local_motion_time = device->motion_last_time;
2012 local_motion_time = motion_last_time;
2014 /* avoid rate throttling for synthetic motion events or if
2015 * the per-actor events are disabled
2017 if (!(event->any.flags & CLUTTER_EVENT_FLAG_SYNTHETIC) ||
2018 !context->motion_events_per_actor)
2020 gint32 frame_rate, delta;
2022 /* avoid issuing too many motion events, which leads to many
2023 * redraws in pick mode (performance penalty)
2025 frame_rate = clutter_get_motion_events_frequency ();
2026 delta = 1000 / frame_rate;
2028 CLUTTER_NOTE (EVENT,
2029 "skip motion event: %s (last:%d, delta:%d, time:%d)",
2030 (event->any.time < (local_motion_time + delta) ? "yes" : "no"),
2035 /* we need to guard against roll-overs and the
2036 * case where the time is rolled backwards and
2037 * the backend is not ensuring a monotonic clock
2041 * http://bugzilla.openedhand.com/show_bug.cgi?id=1130
2043 if (event->any.time >= local_motion_time &&
2044 event->any.time < (local_motion_time + delta))
2047 local_motion_time = event->any.time;
2051 device->motion_last_time = local_motion_time;
2053 motion_last_time = local_motion_time;
2055 /* Only stage gets motion events if clutter_set_motion_events is TRUE,
2056 * and the event is not a synthetic event with source set.
2058 if (!context->motion_events_per_actor &&
2059 event->any.source == NULL)
2061 /* Only stage gets motion events */
2062 event->any.source = stage;
2065 if (context->pointer_grab_actor != NULL)
2067 clutter_actor_event (context->pointer_grab_actor,
2071 else if (device != NULL && device->pointer_grab_actor != NULL)
2073 clutter_actor_event (device->pointer_grab_actor,
2078 /* Trigger handlers on stage in both capture .. */
2079 if (!clutter_actor_event (stage, event, TRUE))
2081 /* and bubbling phase */
2082 clutter_actor_event (stage, event, FALSE);
2089 case CLUTTER_BUTTON_PRESS:
2090 case CLUTTER_BUTTON_RELEASE:
2091 case CLUTTER_SCROLL:
2093 ClutterActor *actor;
2096 clutter_event_get_coords (event, &x, &y);
2098 /* Only do a pick to find the source if source is not already set
2099 * (as it could be in a synthetic event)
2101 if (event->any.source == NULL)
2103 /* Handle release off stage */
2104 if ((x >= clutter_actor_get_width (stage) ||
2105 y >= clutter_actor_get_height (stage) ||
2108 if (event->type == CLUTTER_BUTTON_RELEASE)
2110 CLUTTER_NOTE (EVENT,
2111 "Release off stage received at %i, %i",
2114 event->button.source = stage;
2115 emit_pointer_event (event, event->button.device);
2120 /* Map the event to a reactive actor */
2121 actor = _clutter_do_pick (CLUTTER_STAGE (stage),
2123 CLUTTER_PICK_REACTIVE);
2125 event->any.source = actor;
2131 /* use the source already set in the synthetic event */
2132 actor = event->any.source;
2136 /* FIXME: for an optimisation should check if there are
2137 * actually any reactive actors and avoid the pick all togeather
2138 * (signalling just the stage). Should be big help for gles.
2141 CLUTTER_NOTE (EVENT, "Reactive event received at %i, %i - actor: %p",
2144 /* Create, enter/leave events if needed */
2145 generate_enter_leave_events (event);
2147 if (event->type != CLUTTER_MOTION)
2149 /* Generate click count */
2150 event_click_count_generate (event);
2155 switch (event->type)
2157 case CLUTTER_BUTTON_PRESS:
2158 case CLUTTER_BUTTON_RELEASE:
2159 device = event->button.device;
2161 case CLUTTER_SCROLL:
2162 device = event->scroll.device;
2164 case CLUTTER_MOTION:
2165 /* already handled in the MOTION case of the switch */
2171 emit_pointer_event (event, device);
2175 case CLUTTER_STAGE_STATE:
2176 /* fullscreen / focus - forward to stage */
2177 event->any.source = stage;
2178 clutter_stage_event (CLUTTER_STAGE (stage), event);
2181 case CLUTTER_CLIENT_MESSAGE:
2187 * clutter_get_actor_by_gid
2188 * @id: a #ClutterActor ID.
2190 * Retrieves the #ClutterActor with @id.
2192 * Return value: the actor with the passed id or %NULL. The returned
2193 * actor does not have its reference count increased.
2198 clutter_get_actor_by_gid (guint32 id)
2200 ClutterMainContext *context;
2202 context = clutter_context_get_default ();
2204 g_return_val_if_fail (context != NULL, NULL);
2206 return CLUTTER_ACTOR (clutter_id_pool_lookup (context->id_pool, id));
2210 clutter_base_init (void)
2212 static gboolean initialised = FALSE;
2216 GType foo; /* Quiet gcc */
2220 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2221 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2223 /* initialise GLib type system */
2226 /* CLUTTER_TYPE_ACTOR */
2227 foo = clutter_actor_get_type ();
2232 * clutter_get_default_frame_rate:
2234 * Retrieves the default frame rate used when creating #ClutterTimeline<!--
2237 * This value is also used to compute the default frequency of motion
2240 * Return value: the default frame rate
2245 clutter_get_default_frame_rate (void)
2247 ClutterMainContext *context;
2249 context = clutter_context_get_default ();
2251 return context->frame_rate;
2255 * clutter_set_default_frame_rate:
2256 * @frames_per_sec: the new default frame rate
2258 * Sets the default frame rate to be used when creating #ClutterTimeline<!--
2264 clutter_set_default_frame_rate (guint frames_per_sec)
2266 ClutterMainContext *context;
2268 context = clutter_context_get_default ();
2270 if (context->frame_rate != frames_per_sec)
2271 context->frame_rate = frames_per_sec;
2276 on_pointer_grab_weak_notify (gpointer data,
2277 GObject *where_the_object_was)
2279 ClutterInputDevice *dev = (ClutterInputDevice *)data;
2280 ClutterMainContext *context;
2282 context = clutter_context_get_default ();
2286 dev->pointer_grab_actor = NULL;
2287 clutter_ungrab_pointer_for_device (dev->id);
2291 context->pointer_grab_actor = NULL;
2292 clutter_ungrab_pointer ();
2297 * clutter_grab_pointer:
2298 * @actor: a #ClutterActor
2300 * Grabs pointer events, after the grab is done all pointer related events
2301 * (press, motion, release, enter, leave and scroll) are delivered to this
2302 * actor directly. The source set in the event will be the actor that would
2303 * have received the event if the pointer grab was not in effect.
2305 * If you wish to grab all the pointer events for a specific input device,
2306 * you should use clutter_grab_pointer_for_device().
2311 clutter_grab_pointer (ClutterActor *actor)
2313 ClutterMainContext *context;
2315 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2317 context = clutter_context_get_default ();
2319 if (context->pointer_grab_actor == actor)
2322 if (context->pointer_grab_actor)
2324 g_object_weak_unref (G_OBJECT (context->pointer_grab_actor),
2325 on_pointer_grab_weak_notify,
2327 context->pointer_grab_actor = NULL;
2332 context->pointer_grab_actor = actor;
2334 g_object_weak_ref (G_OBJECT (actor),
2335 on_pointer_grab_weak_notify,
2341 * clutter_grab_pointer_for_device:
2342 * @actor: a #ClutterActor
2343 * @id: a device id, or -1
2345 * Grabs all the pointer events coming from the device @id for @actor.
2347 * If @id is -1 then this function is equivalent to clutter_grab_pointer().
2352 clutter_grab_pointer_for_device (ClutterActor *actor,
2355 ClutterInputDevice *dev;
2357 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2359 /* essentially a global grab */
2362 clutter_grab_pointer (actor);
2366 dev = clutter_get_input_device_for_id (id);
2371 if (dev->pointer_grab_actor == actor)
2374 if (dev->pointer_grab_actor)
2376 g_object_weak_unref (G_OBJECT (dev->pointer_grab_actor),
2377 on_pointer_grab_weak_notify,
2379 dev->pointer_grab_actor = NULL;
2384 dev->pointer_grab_actor = actor;
2386 g_object_weak_ref (G_OBJECT (actor),
2387 on_pointer_grab_weak_notify,
2394 * clutter_ungrab_pointer:
2396 * Removes an existing grab of the pointer.
2401 clutter_ungrab_pointer (void)
2403 clutter_grab_pointer (NULL);
2407 * clutter_ungrab_pointer_for_device:
2410 * Removes an existing grab of the pointer events for device @id.
2415 clutter_ungrab_pointer_for_device (gint id)
2417 clutter_grab_pointer_for_device (NULL, id);
2422 * clutter_get_pointer_grab:
2424 * Queries the current pointer grab of clutter.
2426 * Return value: the actor currently holding the pointer grab, or NULL if there is no grab.
2431 clutter_get_pointer_grab (void)
2433 ClutterMainContext *context;
2434 context = clutter_context_get_default ();
2436 return context->pointer_grab_actor;
2441 on_keyboard_grab_weak_notify (gpointer data,
2442 GObject *where_the_object_was)
2444 ClutterMainContext *context;
2446 context = clutter_context_get_default ();
2447 context->keyboard_grab_actor = NULL;
2449 clutter_ungrab_keyboard ();
2453 * clutter_grab_keyboard:
2454 * @actor: a #ClutterActor
2456 * Grabs keyboard events, after the grab is done keyboard events ("key-press-event"
2457 * and "key-release-event") are delivered to this actor directly. The source
2458 * set in the event will be the actor that would have received the event if the
2459 * keyboard grab was not in effect.
2464 clutter_grab_keyboard (ClutterActor *actor)
2466 ClutterMainContext *context;
2468 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2470 context = clutter_context_get_default ();
2472 if (context->keyboard_grab_actor == actor)
2475 if (context->keyboard_grab_actor)
2477 g_object_weak_unref (G_OBJECT (context->keyboard_grab_actor),
2478 on_keyboard_grab_weak_notify,
2480 context->keyboard_grab_actor = NULL;
2485 context->keyboard_grab_actor = actor;
2487 g_object_weak_ref (G_OBJECT (actor),
2488 on_keyboard_grab_weak_notify,
2494 * clutter_ungrab_keyboard:
2496 * Removes an existing grab of the keyboard.
2501 clutter_ungrab_keyboard (void)
2503 clutter_grab_keyboard (NULL);
2507 * clutter_get_keyboard_grab:
2509 * Queries the current keyboard grab of clutter.
2511 * Return value: the actor currently holding the keyboard grab, or NULL if there is no grab.
2516 clutter_get_keyboard_grab (void)
2518 ClutterMainContext *context;
2519 context = clutter_context_get_default ();
2521 return context->keyboard_grab_actor;
2525 * clutter_get_motion_events_frequency:
2527 * Retrieves the number of motion events per second that are delivered
2530 * See clutter_set_motion_events_frequency().
2532 * Return value: the number of motion events per second
2537 clutter_get_motion_events_frequency (void)
2539 ClutterMainContext *context = clutter_context_get_default ();
2541 if (G_LIKELY (context->motion_frequency == 0))
2545 frequency = clutter_default_fps / 4;
2546 frequency = CLAMP (frequency, 20, 45);
2551 return context->motion_frequency;
2555 * clutter_set_motion_events_frequency:
2556 * @frequency: the number of motion events per second, or 0 for the
2559 * Sets the motion events frequency. Setting this to a non-zero value
2560 * will override the default setting, so it should be rarely used.
2562 * Motion events are delivered from the default backend to the stage
2563 * and are used to generate the enter/leave events pair. This might lead
2564 * to a performance penalty due to the way the actors are identified.
2565 * Using this function is possible to reduce the frequency of the motion
2566 * events delivery to the stage.
2571 clutter_set_motion_events_frequency (guint frequency)
2573 ClutterMainContext *context = clutter_context_get_default ();
2575 /* never allow the motion events to exceed the default frame rate */
2576 context->motion_frequency = CLAMP (frequency, 1, clutter_default_fps);
2580 * clutter_clear_glyph_cache:
2582 * Clears the internal cache of glyphs used by the Pango
2583 * renderer. This will free up some memory and GL texture
2584 * resources. The cache will be automatically refilled as more text is
2590 clutter_clear_glyph_cache (void)
2592 if (CLUTTER_CONTEXT ()->font_map)
2593 cogl_pango_font_map_clear_glyph_cache (CLUTTER_CONTEXT ()->font_map);
2597 * clutter_set_font_flags:
2598 * @flags: The new flags
2600 * Sets the font quality options for subsequent text rendering
2603 * Using mipmapped textures will improve the quality for scaled down
2604 * text but will use more texture memory.
2606 * Enabling hinting improves text quality for static text but may
2607 * introduce some artifacts if the text is animated.
2612 clutter_set_font_flags (ClutterFontFlags flags)
2614 ClutterFontFlags old_flags, changed_flags;
2615 cairo_font_options_t *font_options;
2617 if (CLUTTER_CONTEXT ()->font_map)
2618 cogl_pango_font_map_set_use_mipmapping (CLUTTER_CONTEXT ()->font_map,
2620 & CLUTTER_FONT_MIPMAPPING) != 0);
2622 old_flags = clutter_get_font_flags ();
2624 font_options = clutter_backend_get_font_options (CLUTTER_CONTEXT ()->backend);
2625 font_options = cairo_font_options_copy (font_options);
2627 /* Only set the font options that have actually changed so we don't
2628 override a detailed setting from the backend */
2629 changed_flags = old_flags ^ flags;
2631 if ((changed_flags & CLUTTER_FONT_HINTING))
2632 cairo_font_options_set_hint_style (font_options,
2633 (flags & CLUTTER_FONT_HINTING)
2634 ? CAIRO_HINT_STYLE_FULL
2635 : CAIRO_HINT_STYLE_NONE);
2637 clutter_backend_set_font_options (CLUTTER_CONTEXT ()->backend, font_options);
2639 cairo_font_options_destroy (font_options);
2641 if (CLUTTER_CONTEXT ()->pango_context)
2642 update_pango_context (CLUTTER_CONTEXT ()->backend,
2643 CLUTTER_CONTEXT ()->pango_context);
2647 * clutter_get_font_flags:
2649 * Gets the current font flags for rendering text. See
2650 * clutter_set_font_flags().
2652 * Return value: The font flags
2657 clutter_get_font_flags (void)
2659 ClutterMainContext *ctxt = CLUTTER_CONTEXT ();
2660 CoglPangoFontMap *font_map = NULL;
2661 cairo_font_options_t *font_options;
2662 ClutterFontFlags flags = 0;
2664 font_map = CLUTTER_CONTEXT ()->font_map;
2666 if (G_LIKELY (font_map)
2667 && cogl_pango_font_map_get_use_mipmapping (font_map))
2668 flags |= CLUTTER_FONT_MIPMAPPING;
2670 font_options = clutter_backend_get_font_options (ctxt->backend);
2672 if ((cairo_font_options_get_hint_style (font_options)
2673 != CAIRO_HINT_STYLE_DEFAULT)
2674 && (cairo_font_options_get_hint_style (font_options)
2675 != CAIRO_HINT_STYLE_NONE))
2676 flags |= CLUTTER_FONT_HINTING;
2682 * clutter_get_input_device_for_id:
2685 * Retrieves the #ClutterInputDevice from its id.
2687 * Return value: a #ClutterInputDevice, or %NULL
2691 ClutterInputDevice *
2692 clutter_get_input_device_for_id (gint id)
2695 ClutterInputDevice *device = NULL;
2696 ClutterMainContext *context;
2698 context = clutter_context_get_default ();
2700 for (item = context->input_devices;
2704 device = item->data;
2706 if (device->id == id)
2714 * clutter_get_font_map:
2716 * Retrieves the #PangoFontMap instance used by Clutter.
2717 * You can use the global font map object with the COGL
2720 * Return value: the #PangoFontMap instance. The returned
2721 * value is owned by Clutter and it should never be
2727 clutter_get_font_map (void)
2729 if (CLUTTER_CONTEXT ()->font_map)
2730 return PANGO_FONT_MAP (CLUTTER_CONTEXT ()->font_map);