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 const gchar *direction;
410 PangoDirection dir = PANGO_DIRECTION_LTR;
412 direction = g_getenv ("CLUTTER_TEXT_DIRECTION");
413 if (direction && *direction != '\0')
415 if (strcmp (direction, "rtl") == 0)
416 dir = PANGO_DIRECTION_RTL;
418 dir = PANGO_DIRECTION_LTR;
421 dir = PANGO_DIRECTION_LTR;
427 update_pango_context (ClutterBackend *backend,
428 PangoContext *context)
430 PangoFontDescription *font_desc;
431 cairo_font_options_t *font_options;
432 const gchar *font_name;
435 /* update the text direction */
436 pango_context_set_base_dir (context, clutter_text_direction);
438 /* get the configuration for the PangoContext from the backend */
439 font_name = clutter_backend_get_font_name (backend);
440 font_options = clutter_backend_get_font_options (backend);
441 resolution = clutter_backend_get_resolution (backend);
443 font_desc = pango_font_description_from_string (font_name);
446 resolution = 96.0; /* fall back */
448 pango_context_set_font_description (context, font_desc);
449 pango_cairo_context_set_font_options (context, font_options);
450 pango_cairo_context_set_resolution (context, resolution);
452 pango_font_description_free (font_desc);
456 _clutter_context_get_pango_context (ClutterMainContext *self)
458 if (G_UNLIKELY (self->pango_context == NULL))
460 PangoContext *context;
462 context = cogl_pango_font_map_create_context (self->font_map);
463 self->pango_context = context;
465 g_signal_connect (self->backend, "resolution-changed",
466 G_CALLBACK (update_pango_context),
467 self->pango_context);
468 g_signal_connect (self->backend, "font-changed",
469 G_CALLBACK (update_pango_context),
470 self->pango_context);
473 update_pango_context (self->backend, self->pango_context);
475 return self->pango_context;
479 _clutter_context_create_pango_context (ClutterMainContext *self)
481 PangoContext *context;
483 context = cogl_pango_font_map_create_context (self->font_map);
484 update_pango_context (self->backend, context);
492 * Terminates the Clutter mainloop.
495 clutter_main_quit (void)
497 g_return_if_fail (main_loops != NULL);
499 g_main_loop_quit (main_loops->data);
503 * clutter_main_level:
505 * Retrieves the depth of the Clutter mainloop.
507 * Return value: The level of the mainloop.
510 clutter_main_level (void)
512 return clutter_main_loop_level;
518 * Starts the Clutter mainloop.
525 /* Make sure there is a context */
528 if (!clutter_is_initialized)
530 g_warning ("Called clutter_main() but Clutter wasn't initialised. "
531 "You must call clutter_init() first.");
537 clutter_main_loop_level++;
539 loop = g_main_loop_new (NULL, TRUE);
540 main_loops = g_slist_prepend (main_loops, loop);
542 #ifdef HAVE_CLUTTER_FRUITY
543 /* clutter fruity creates an application that forwards events and manually
546 clutter_fruity_main ();
548 if (g_main_loop_is_running (main_loops->data))
550 clutter_threads_leave ();
551 g_main_loop_run (loop);
552 clutter_threads_enter ();
556 main_loops = g_slist_remove (main_loops, loop);
558 g_main_loop_unref (loop);
560 clutter_main_loop_level--;
566 clutter_threads_impl_lock (void)
568 if (clutter_threads_mutex)
569 g_mutex_lock (clutter_threads_mutex);
573 clutter_threads_impl_unlock (void)
575 if (clutter_threads_mutex)
576 g_mutex_unlock (clutter_threads_mutex);
580 * clutter_threads_init:
582 * Initialises the Clutter threading mechanism, so that Clutter API can be
583 * called by multiple threads, using clutter_threads_enter() and
584 * clutter_threads_leave() to mark the critical sections.
586 * You must call g_thread_init() before this function.
588 * This function must be called before clutter_init().
593 clutter_threads_init (void)
595 if (!g_thread_supported ())
596 g_error ("g_thread_init() must be called before clutter_threads_init()");
598 clutter_threads_mutex = g_mutex_new ();
600 if (!clutter_threads_lock)
601 clutter_threads_lock = clutter_threads_impl_lock;
603 if (!clutter_threads_unlock)
604 clutter_threads_unlock = clutter_threads_impl_unlock;
608 * clutter_threads_set_lock_functions:
609 * @enter_fn: function called when aquiring the Clutter main lock
610 * @leave_fn: function called when releasing the Clutter main lock
612 * Allows the application to replace the standard method that
613 * Clutter uses to protect its data structures. Normally, Clutter
614 * creates a single #GMutex that is locked by clutter_threads_enter(),
615 * and released by clutter_threads_leave(); using this function an
616 * application provides, instead, a function @enter_fn that is
617 * called by clutter_threads_enter() and a function @leave_fn that is
618 * called by clutter_threads_leave().
620 * The functions must provide at least same locking functionality
621 * as the default implementation, but can also do extra application
622 * specific processing.
624 * As an example, consider an application that has its own recursive
625 * lock that when held, holds the Clutter lock as well. When Clutter
626 * unlocks the Clutter lock when entering a recursive main loop, the
627 * application must temporarily release its lock as well.
629 * Most threaded Clutter apps won't need to use this method.
631 * This method must be called before clutter_threads_init(), and cannot
632 * be called multiple times.
637 clutter_threads_set_lock_functions (GCallback enter_fn,
640 g_return_if_fail (clutter_threads_lock == NULL &&
641 clutter_threads_unlock == NULL);
643 clutter_threads_lock = enter_fn;
644 clutter_threads_unlock = leave_fn;
651 GDestroyNotify notify;
652 } ClutterThreadsDispatch;
655 clutter_threads_dispatch (gpointer data)
657 ClutterThreadsDispatch *dispatch = data;
658 gboolean ret = FALSE;
660 clutter_threads_enter ();
662 if (!g_source_is_destroyed (g_main_current_source ()))
663 ret = dispatch->func (dispatch->data);
665 clutter_threads_leave ();
671 clutter_threads_dispatch_free (gpointer data)
673 ClutterThreadsDispatch *dispatch = data;
675 /* XXX - we cannot hold the thread lock here because the main loop
676 * might destroy a source while still in the dispatcher function; so
677 * knowing whether the lock is being held or not is not known a priori.
679 * see bug: http://bugzilla.gnome.org/show_bug.cgi?id=459555
681 if (dispatch->notify)
682 dispatch->notify (dispatch->data);
684 g_slice_free (ClutterThreadsDispatch, dispatch);
688 * clutter_threads_add_idle_full:
689 * @priority: the priority of the timeout source. Typically this will be in the
690 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE
691 * @func: function to call
692 * @data: data to pass to the function
693 * @notify: functio to call when the idle source is removed
695 * Adds a function to be called whenever there are no higher priority
696 * events pending. If the function returns %FALSE it is automatically
697 * removed from the list of event sources and will not be called again.
699 * This function can be considered a thread-safe variant of g_idle_add_full():
700 * it will call @function while holding the Clutter lock. It is logically
701 * equivalent to the following implementation:
705 * idle_safe_callback (gpointer data)
707 * SafeClosure *closure = data;
708 * gboolean res = FALSE;
710 * /* mark the critical section */
712 * clutter_threads_enter();
714 * /* the callback does not need to acquire the Clutter
715 * * lock itself, as it is held by the this proxy handler
717 * res = closure->callback (closure->data);
719 * clutter_threads_leave();
724 * add_safe_idle (GSourceFunc callback,
727 * SafeClosure *closure = g_new0 (SafeClosure, 1);
729 * closure->callback = callback;
730 * closure->data = data;
732 * return g_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
733 * idle_safe_callback,
739 * This function should be used by threaded applications to make sure
740 * that @func is emitted under the Clutter threads lock and invoked
741 * from the same thread that started the Clutter main loop. For instance,
742 * it can be used to update the UI using the results from a worker
747 * update_ui (gpointer data)
749 * SomeClosure *closure = data;
751 * /* it is safe to call Clutter API from this function because
752 * * it is invoked from the same thread that started the main
753 * * loop and under the Clutter thread lock
755 * clutter_label_set_text (CLUTTER_LABEL (closure->label),
758 * g_object_unref (closure->label);
764 * /* within another thread */
765 * closure = g_new0 (SomeClosure, 1);
766 * /* always take a reference on GObject instances */
767 * closure->label = g_object_ref (my_application->label);
768 * closure->text = g_strdup (processed_text_to_update_the_label);
770 * clutter_threads_add_idle_full (G_PRIORITY_HIGH_IDLE,
776 * Return value: the ID (greater than 0) of the event source.
781 clutter_threads_add_idle_full (gint priority,
784 GDestroyNotify notify)
786 ClutterThreadsDispatch *dispatch;
788 g_return_val_if_fail (func != NULL, 0);
790 dispatch = g_slice_new (ClutterThreadsDispatch);
791 dispatch->func = func;
792 dispatch->data = data;
793 dispatch->notify = notify;
795 return g_idle_add_full (priority,
796 clutter_threads_dispatch, dispatch,
797 clutter_threads_dispatch_free);
801 * clutter_threads_add_idle:
802 * @func: function to call
803 * @data: data to pass to the function
805 * Simple wrapper around clutter_threads_add_idle_full() using the
808 * Return value: the ID (greater than 0) of the event source.
813 clutter_threads_add_idle (GSourceFunc func,
816 g_return_val_if_fail (func != NULL, 0);
818 return clutter_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
824 * clutter_threads_add_timeout_full:
825 * @priority: the priority of the timeout source. Typically this will be in the
826 * range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
827 * @interval: the time between calls to the function, in milliseconds
828 * @func: function to call
829 * @data: data to pass to the function
830 * @notify: function to call when the timeout source is removed
832 * Sets a function to be called at regular intervals holding the Clutter
833 * threads lock, with the given priority. The function is called repeatedly
834 * until it returns %FALSE, at which point the timeout is automatically
835 * removed and the function will not be called again. The @notify function
836 * is called when the timeout is removed.
838 * The first call to the function will be at the end of the first @interval.
840 * It is important to note that, due to how the Clutter main loop is
841 * implemented, the timing will not be accurate and it will not try to
842 * "keep up" with the interval. A more reliable source is available
843 * using clutter_threads_add_frame_source_full(), which is also internally
844 * used by #ClutterTimeline.
846 * See also clutter_threads_add_idle_full().
848 * Return value: the ID (greater than 0) of the event source.
853 clutter_threads_add_timeout_full (gint priority,
857 GDestroyNotify notify)
859 ClutterThreadsDispatch *dispatch;
861 g_return_val_if_fail (func != NULL, 0);
863 dispatch = g_slice_new (ClutterThreadsDispatch);
864 dispatch->func = func;
865 dispatch->data = data;
866 dispatch->notify = notify;
868 return g_timeout_add_full (priority,
870 clutter_threads_dispatch, dispatch,
871 clutter_threads_dispatch_free);
875 * clutter_threads_add_timeout:
876 * @interval: the time between calls to the function, in milliseconds
877 * @func: function to call
878 * @data: data to pass to the function
880 * Simple wrapper around clutter_threads_add_timeout_full().
882 * Return value: the ID (greater than 0) of the event source.
887 clutter_threads_add_timeout (guint interval,
891 g_return_val_if_fail (func != NULL, 0);
893 return clutter_threads_add_timeout_full (G_PRIORITY_DEFAULT,
900 * clutter_threads_add_frame_source_full:
901 * @priority: the priority of the frame source. Typically this will be in the
902 * range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
903 * @interval: the time between calls to the function, in milliseconds
904 * @func: function to call
905 * @data: data to pass to the function
906 * @notify: function to call when the timeout source is removed
908 * Sets a function to be called at regular intervals holding the Clutter
909 * threads lock, with the given priority. The function is called repeatedly
910 * until it returns %FALSE, at which point the timeout is automatically
911 * removed and the function will not be called again. The @notify function
912 * is called when the timeout is removed.
914 * This function is similar to clutter_threads_add_timeout_full()
915 * except that it will try to compensate for delays. For example, if
916 * @func takes half the interval time to execute then the function
917 * will be called again half the interval time after it finished. In
918 * contrast clutter_threads_add_timeout_full() would not fire until a
919 * full interval after the function completes so the delay between
920 * calls would be @interval * 1.5. This function does not however try
921 * to invoke the function multiple times to catch up missing frames if
922 * @func takes more than @interval ms to execute.
924 * See also clutter_threads_add_idle_full().
926 * Return value: the ID (greater than 0) of the event source.
931 clutter_threads_add_frame_source_full (gint priority,
935 GDestroyNotify notify)
937 ClutterThreadsDispatch *dispatch;
939 g_return_val_if_fail (func != NULL, 0);
941 dispatch = g_slice_new (ClutterThreadsDispatch);
942 dispatch->func = func;
943 dispatch->data = data;
944 dispatch->notify = notify;
946 return clutter_frame_source_add_full (priority,
948 clutter_threads_dispatch, dispatch,
949 clutter_threads_dispatch_free);
953 * clutter_threads_add_frame_source:
954 * @interval: the time between calls to the function, in milliseconds
955 * @func: function to call
956 * @data: data to pass to the function
958 * Simple wrapper around clutter_threads_add_frame_source_full().
960 * Return value: the ID (greater than 0) of the event source.
965 clutter_threads_add_frame_source (guint interval,
969 g_return_val_if_fail (func != NULL, 0);
971 return clutter_threads_add_frame_source_full (G_PRIORITY_DEFAULT,
978 * clutter_threads_enter:
980 * Locks the Clutter thread lock.
985 clutter_threads_enter (void)
987 if (clutter_threads_lock)
988 (* clutter_threads_lock) ();
992 * clutter_threads_leave:
994 * Unlocks the Clutter thread lock.
999 clutter_threads_leave (void)
1001 if (clutter_threads_unlock)
1002 (* clutter_threads_unlock) ();
1007 * clutter_get_debug_enabled:
1009 * Check if clutter has debugging turned on.
1011 * Return value: TRUE if debugging is turned on, FALSE otherwise.
1014 clutter_get_debug_enabled (void)
1016 #ifdef CLUTTER_ENABLE_DEBUG
1017 return clutter_debug_flags != 0;
1023 ClutterMainContext *
1024 clutter_context_get_default (void)
1026 if (G_UNLIKELY(!ClutterCntx))
1028 ClutterMainContext *ctx;
1030 ClutterCntx = ctx = g_new0 (ClutterMainContext, 1);
1031 ctx->backend = g_object_new (_clutter_backend_impl_get_type (), NULL);
1033 ctx->is_initialized = FALSE;
1034 ctx->motion_events_per_actor = TRUE;
1036 #ifdef CLUTTER_ENABLE_DEBUG
1037 ctx->timer = g_timer_new ();
1038 g_timer_start (ctx->timer);
1046 * clutter_get_timestamp:
1048 * Returns the approximate number of microseconds passed since clutter was
1051 * Return value: Number of microseconds since clutter_init() was called.
1054 clutter_get_timestamp (void)
1056 #ifdef CLUTTER_ENABLE_DEBUG
1057 ClutterMainContext *ctx;
1060 ctx = clutter_context_get_default ();
1062 /* FIXME: may need a custom timer for embedded setups */
1063 seconds = g_timer_elapsed (ctx->timer, NULL);
1065 return (gulong)(seconds / 1.0e-6);
1072 clutter_arg_direction_cb (const char *key,
1076 clutter_text_direction =
1077 (strcmp (value, "rtl") == 0) ? PANGO_DIRECTION_RTL
1078 : PANGO_DIRECTION_LTR;
1083 #ifdef CLUTTER_ENABLE_DEBUG
1085 clutter_arg_debug_cb (const char *key,
1089 clutter_debug_flags |=
1090 g_parse_debug_string (value,
1092 G_N_ELEMENTS (clutter_debug_keys));
1097 clutter_arg_no_debug_cb (const char *key,
1101 clutter_debug_flags &=
1102 ~g_parse_debug_string (value,
1104 G_N_ELEMENTS (clutter_debug_keys));
1107 #endif /* CLUTTER_ENABLE_DEBUG */
1110 clutter_init_error_quark (void)
1112 return g_quark_from_static_string ("clutter-init-error-quark");
1115 static ClutterInitError
1116 clutter_init_real (GError **error)
1118 ClutterMainContext *ctx;
1119 ClutterActor *stage;
1121 ClutterBackend *backend;
1123 /* Note, creates backend if not already existing, though parse args will
1124 * have likely created it
1126 ctx = clutter_context_get_default ();
1127 backend = ctx->backend;
1129 if (!ctx->options_parsed)
1131 g_set_error (error, CLUTTER_INIT_ERROR,
1132 CLUTTER_INIT_ERROR_INTERNAL,
1133 "When using clutter_get_option_group_without_init() "
1134 "you must parse options before calling clutter_init()");
1136 return CLUTTER_INIT_ERROR_INTERNAL;
1140 * Call backend post parse hooks.
1142 if (!_clutter_backend_post_parse (backend, error))
1143 return CLUTTER_INIT_ERROR_BACKEND;
1146 * Resolution requires display to be open, so can only be queried after
1147 * the post_parse hooks run.
1149 ctx->font_map = COGL_PANGO_FONT_MAP (cogl_pango_font_map_new ());
1151 resolution = clutter_backend_get_resolution (ctx->backend);
1152 cogl_pango_font_map_set_resolution (ctx->font_map, resolution);
1153 cogl_pango_font_map_set_use_mipmapping (ctx->font_map, TRUE);
1155 clutter_text_direction = clutter_get_text_direction ();
1157 /* Stage will give us a GL Context etc */
1158 stage = clutter_stage_get_default ();
1162 g_set_error (error, CLUTTER_INIT_ERROR,
1163 CLUTTER_INIT_ERROR_INTERNAL,
1164 "Unable to create the default stage");
1166 g_critical ("Unable to create the default stage");
1168 return CLUTTER_INIT_ERROR_INTERNAL;
1171 clutter_actor_realize (stage);
1173 if (!CLUTTER_ACTOR_IS_REALIZED (stage))
1176 g_set_error (error, CLUTTER_INIT_ERROR,
1177 CLUTTER_INIT_ERROR_INTERNAL,
1178 "Unable to realize the default stage");
1180 g_critical ("Unable to realize the default stage");
1182 return CLUTTER_INIT_ERROR_INTERNAL;
1185 /* Now we can safely assume we have a valid GL context and can
1186 * start issueing cogl commands
1189 /* Figure out framebuffer masks used for pick */
1190 cogl_get_bitmasks (&ctx->fb_r_mask, &ctx->fb_g_mask, &ctx->fb_b_mask, NULL);
1192 ctx->fb_r_mask_used = ctx->fb_r_mask;
1193 ctx->fb_g_mask_used = ctx->fb_g_mask;
1194 ctx->fb_b_mask_used = ctx->fb_b_mask;
1196 #ifndef HAVE_CLUTTER_FRUITY
1197 /* We always do fuzzy picking for the fruity backend */
1198 if (g_getenv ("CLUTTER_FUZZY_PICK") != NULL)
1201 ctx->fb_r_mask_used--;
1202 ctx->fb_g_mask_used--;
1203 ctx->fb_b_mask_used--;
1206 /* Initiate event collection */
1207 _clutter_backend_init_events (ctx->backend);
1209 /* finally features - will call to backend and cogl */
1210 _clutter_feature_init ();
1212 clutter_stage_set_title (CLUTTER_STAGE (stage), g_get_prgname ());
1214 clutter_is_initialized = TRUE;
1215 ctx->is_initialized = TRUE;
1217 return CLUTTER_INIT_SUCCESS;
1220 static GOptionEntry clutter_args[] = {
1221 { "clutter-show-fps", 0, 0, G_OPTION_ARG_NONE, &clutter_show_fps,
1222 "Show frames per second", NULL },
1223 { "clutter-default-fps", 0, 0, G_OPTION_ARG_INT, &clutter_default_fps,
1224 "Default frame rate", "FPS" },
1225 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &clutter_fatal_warnings,
1226 "Make all warnings fatal", NULL },
1227 { "clutter-text-direction", 0, 0, G_OPTION_ARG_CALLBACK,
1228 clutter_arg_direction_cb,
1229 "Direction for the text", "DIRECTION" },
1230 #ifdef CLUTTER_ENABLE_DEBUG
1231 { "clutter-debug", 0, 0, G_OPTION_ARG_CALLBACK, clutter_arg_debug_cb,
1232 "Clutter debugging flags to set", "FLAGS" },
1233 { "clutter-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, clutter_arg_no_debug_cb,
1234 "Clutter debugging flags to unset", "FLAGS" },
1235 #endif /* CLUTTER_ENABLE_DEBUG */
1239 /* pre_parse_hook: initialise variables depending on environment
1240 * variables; these variables might be overridden by the command
1241 * line arguments that are going to be parsed after.
1244 pre_parse_hook (GOptionContext *context,
1245 GOptionGroup *group,
1249 ClutterMainContext *clutter_context;
1250 ClutterBackend *backend;
1251 const char *env_string;
1253 if (clutter_is_initialized)
1256 if (setlocale (LC_ALL, "") == NULL)
1257 g_warning ("Locale not supported by C library.\n"
1258 "Using the fallback 'C' locale.");
1260 clutter_context = clutter_context_get_default ();
1262 clutter_context->id_pool = clutter_id_pool_new (256);
1264 backend = clutter_context->backend;
1265 g_assert (CLUTTER_IS_BACKEND (backend));
1267 #ifdef CLUTTER_ENABLE_DEBUG
1268 env_string = g_getenv ("CLUTTER_DEBUG");
1269 if (env_string != NULL)
1271 clutter_debug_flags =
1272 g_parse_debug_string (env_string,
1274 G_N_ELEMENTS (clutter_debug_keys));
1277 #endif /* CLUTTER_ENABLE_DEBUG */
1279 env_string = g_getenv ("CLUTTER_SHOW_FPS");
1281 clutter_show_fps = TRUE;
1283 env_string = g_getenv ("CLUTTER_DEFAULT_FPS");
1286 gint default_fps = g_ascii_strtoll (env_string, NULL, 10);
1288 clutter_default_fps = CLAMP (default_fps, 1, 1000);
1291 return _clutter_backend_pre_parse (backend, error);
1294 /* post_parse_hook: initialise the context and data structures
1295 * and opens the X display
1298 post_parse_hook (GOptionContext *context,
1299 GOptionGroup *group,
1303 ClutterMainContext *clutter_context;
1304 ClutterBackend *backend;
1306 if (clutter_is_initialized)
1309 clutter_context = clutter_context_get_default ();
1310 backend = clutter_context->backend;
1311 g_assert (CLUTTER_IS_BACKEND (backend));
1313 if (clutter_fatal_warnings)
1315 GLogLevelFlags fatal_mask;
1317 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1318 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1319 g_log_set_always_fatal (fatal_mask);
1322 clutter_context->frame_rate = clutter_default_fps;
1323 clutter_context->options_parsed = TRUE;
1326 * If not asked to defer display setup, call clutter_init_real(),
1327 * which in turn calls the backend post parse hooks.
1329 if (!clutter_context->defer_display_setup)
1330 return clutter_init_real (error);
1336 * clutter_get_option_group:
1338 * Returns a #GOptionGroup for the command line arguments recognized
1339 * by Clutter. You should add this group to your #GOptionContext with
1340 * g_option_context_add_group(), if you are using g_option_context_parse()
1341 * to parse your commandline arguments.
1343 * Calling g_option_context_parse() with Clutter's #GOptionGroup will result
1344 * in Clutter's initialization. That is, the following code:
1347 * g_option_context_set_main_group (context, clutter_get_option_group ());
1348 * res = g_option_context_parse (context, &argc, &argc, NULL);
1351 * is functionally equivalent to:
1354 * clutter_init (&argc, &argv);
1357 * After g_option_context_parse() on a #GOptionContext containing the
1358 * Clutter #GOptionGroup has returned %TRUE, Clutter is guaranteed to be
1361 * Return value: a #GOptionGroup for the commandline arguments
1362 * recognized by Clutter
1367 clutter_get_option_group (void)
1369 ClutterMainContext *context;
1370 GOptionGroup *group;
1372 clutter_base_init ();
1374 context = clutter_context_get_default ();
1376 group = g_option_group_new ("clutter",
1378 "Show Clutter Options",
1382 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
1383 g_option_group_add_entries (group, clutter_args);
1385 /* add backend-specific options */
1386 _clutter_backend_add_options (context->backend, group);
1392 * clutter_get_option_group_without_init:
1394 * Returns a #GOptionGroup for the command line arguments recognized
1395 * by Clutter. You should add this group to your #GOptionContext with
1396 * g_option_context_add_group(), if you are using g_option_context_parse()
1397 * to parse your commandline arguments. Unlike clutter_get_option_group(),
1398 * calling g_option_context_parse() with the #GOptionGroup returned by this
1399 * function requires a subsequent explicit call to clutter_init(); use this
1400 * function when needing to set foreign display connection with
1401 * clutter_x11_set_display(), or with gtk_clutter_init().
1403 * Return value: a #GOptionGroup for the commandline arguments
1404 * recognized by Clutter
1409 clutter_get_option_group_without_init (void)
1411 ClutterMainContext *context;
1412 GOptionGroup *group;
1414 clutter_base_init ();
1416 context = clutter_context_get_default ();
1417 context->defer_display_setup = TRUE;
1419 group = clutter_get_option_group ();
1425 * clutter_init_with_args:
1426 * @argc: a pointer to the number of command line arguments
1427 * @argv: a pointer to the array of command line arguments
1428 * @parameter_string: a string which is displayed in the
1429 * first line of <option>--help</option> output, after
1430 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
1431 * @entries: a %NULL terminated array of #GOptionEntry<!-- -->s
1432 * describing the options of your program
1433 * @translation_domain: a translation domain to use for translating
1434 * the <option>--help</option> output for the options in @entries
1435 * with gettext(), or %NULL
1436 * @error: a return location for a #GError
1438 * This function does the same work as clutter_init(). Additionally,
1439 * it allows you to add your own command line options, and it
1440 * automatically generates nicely formatted <option>--help</option>
1441 * output. Note that your program will be terminated after writing
1442 * out the help output. Also note that, in case of error, the
1443 * error message will be placed inside @error instead of being
1444 * printed on the display.
1446 * Return value: %CLUTTER_INIT_SUCCESS if Clutter has been successfully
1447 * initialised, or other values or #ClutterInitError in case of
1453 clutter_init_with_args (int *argc,
1455 const char *parameter_string,
1456 GOptionEntry *entries,
1457 const char *translation_domain,
1460 GOptionContext *context;
1461 GOptionGroup *group;
1463 ClutterMainContext *ctx;
1465 if (clutter_is_initialized)
1466 return CLUTTER_INIT_SUCCESS;
1468 clutter_base_init ();
1470 ctx = clutter_context_get_default ();
1472 if (!ctx->defer_display_setup)
1474 if (argc && *argc > 0 && *argv)
1475 g_set_prgname ((*argv)[0]);
1477 group = clutter_get_option_group ();
1478 context = g_option_context_new (parameter_string);
1480 g_option_context_add_group (context, group);
1483 g_option_context_add_main_entries (context, entries, translation_domain);
1485 res = g_option_context_parse (context, argc, argv, error);
1486 g_option_context_free (context);
1488 /* if res is FALSE, the error is filled for
1489 * us by g_option_context_parse()
1493 /* if there has been an error in the initialization, the
1494 * error id will be preserved inside the GError code
1496 if (error && *error)
1497 return (*error)->code;
1499 return CLUTTER_INIT_ERROR_INTERNAL;
1502 return CLUTTER_INIT_SUCCESS;
1505 return clutter_init_real (error);
1509 clutter_parse_args (int *argc,
1512 GOptionContext *option_context;
1513 GOptionGroup *clutter_group;
1514 GError *error = NULL;
1515 gboolean ret = TRUE;
1517 if (clutter_is_initialized)
1520 option_context = g_option_context_new (NULL);
1521 g_option_context_set_ignore_unknown_options (option_context, TRUE);
1522 g_option_context_set_help_enabled (option_context, FALSE);
1524 /* Initiate any command line options from the backend */
1526 clutter_group = clutter_get_option_group ();
1527 g_option_context_set_main_group (option_context, clutter_group);
1529 if (!g_option_context_parse (option_context, argc, argv, &error))
1533 g_warning ("%s", error->message);
1534 g_error_free (error);
1540 g_option_context_free (option_context);
1547 * @argc: The number of arguments in @argv
1548 * @argv: A pointer to an array of arguments.
1550 * It will initialise everything needed to operate with Clutter and
1551 * parses some standard command line options. @argc and @argv are
1552 * adjusted accordingly so your own code will never see those standard
1555 * Return value: 1 on success, < 0 on failure.
1558 clutter_init (int *argc,
1561 ClutterMainContext *ctx;
1562 GError *error = NULL;
1564 if (clutter_is_initialized)
1565 return CLUTTER_INIT_SUCCESS;
1567 clutter_base_init ();
1569 ctx = clutter_context_get_default ();
1571 if (!ctx->defer_display_setup)
1573 if (argc && *argc > 0 && *argv)
1574 g_set_prgname ((*argv)[0]);
1576 /* parse_args will trigger backend creation and things like
1577 * DISPLAY connection etc.
1579 if (clutter_parse_args (argc, argv) == FALSE)
1581 CLUTTER_NOTE (MISC, "failed to parse arguments.");
1582 return CLUTTER_INIT_ERROR_INTERNAL;
1585 return CLUTTER_INIT_SUCCESS;
1588 return clutter_init_real (&error);
1592 _clutter_boolean_handled_accumulator (GSignalInvocationHint *ihint,
1593 GValue *return_accu,
1594 const GValue *handler_return,
1597 gboolean continue_emission;
1598 gboolean signal_handled;
1600 signal_handled = g_value_get_boolean (handler_return);
1601 g_value_set_boolean (return_accu, signal_handled);
1602 continue_emission = !signal_handled;
1604 return continue_emission;
1608 event_click_count_generate (ClutterEvent *event)
1610 /* multiple button click detection */
1611 static gint click_count = 0;
1612 static gint previous_x = -1;
1613 static gint previous_y = -1;
1614 static guint32 previous_time = 0;
1615 static gint previous_button_number = -1;
1617 ClutterBackend *backend;
1618 guint double_click_time;
1619 guint double_click_distance;
1621 backend = clutter_context_get_default ()->backend;
1622 double_click_distance = clutter_backend_get_double_click_distance (backend);
1623 double_click_time = clutter_backend_get_double_click_time (backend);
1625 if (event->button.device != NULL)
1627 click_count = event->button.device->click_count;
1628 previous_x = event->button.device->previous_x;
1629 previous_y = event->button.device->previous_y;
1630 previous_time = event->button.device->previous_time;
1631 previous_button_number = event->button.device->previous_button_number;
1634 switch (event->type)
1636 case CLUTTER_BUTTON_PRESS:
1637 case CLUTTER_SCROLL:
1638 /* check if we are in time and within distance to increment an
1639 * existing click count
1641 if (event->button.time < previous_time + double_click_time &&
1642 (ABS (event->button.x - previous_x) <= double_click_distance) &&
1643 (ABS (event->button.y - previous_y) <= double_click_distance)
1644 && event->button.button == previous_button_number)
1648 else /* start a new click count*/
1651 previous_button_number = event->button.button;
1654 /* store time and position for this click for comparison with
1657 previous_time = event->button.time;
1658 previous_x = event->button.x;
1659 previous_y = event->button.y;
1662 case CLUTTER_BUTTON_RELEASE:
1663 event->button.click_count=click_count;
1669 if (event->button.device != NULL)
1671 event->button.device->click_count = click_count;
1672 event->button.device->previous_x = previous_x;
1673 event->button.device->previous_y = previous_y;
1674 event->button.device->previous_time = previous_time;
1675 event->button.device->previous_button_number = previous_button_number;
1681 emit_event (ClutterEvent *event,
1682 gboolean is_key_event)
1684 #define MAX_EVENT_DEPTH 512
1686 static ClutterActor **event_tree = NULL;
1687 static gboolean lock = FALSE;
1689 ClutterActor *actor;
1690 gint i = 0, n_tree_events = 0;
1692 if (!event->any.source)
1694 g_warning ("No event source set, discarding event");
1698 /* reentrancy check */
1704 /* Sorry Mr Bassi. */
1705 if (G_UNLIKELY (event_tree == NULL))
1706 event_tree = g_new0 (ClutterActor *, MAX_EVENT_DEPTH);
1708 actor = event->any.source;
1710 /* Build 'tree' of emitters for the event */
1711 while (actor && n_tree_events < MAX_EVENT_DEPTH)
1713 ClutterActor *parent;
1715 parent = clutter_actor_get_parent (actor);
1717 if (clutter_actor_get_reactive (actor) ||
1718 parent == NULL || /* stage gets all events */
1719 is_key_event) /* keyboard events are always emitted */
1721 event_tree[n_tree_events++] = g_object_ref (actor);
1728 for (i = n_tree_events-1; i >= 0; i--)
1729 if (clutter_actor_event (event_tree[i], event, TRUE))
1733 for (i = 0; i < n_tree_events; i++)
1734 if (clutter_actor_event (event_tree[i], event, FALSE))
1739 for (i = 0; i < n_tree_events; i++)
1740 g_object_unref (event_tree[i]);
1744 #undef MAX_EVENT_DEPTH
1748 * Emits a pointer event after having prepared the event for delivery (setting
1749 * source, computing click_count, generating enter/leave etc.).
1753 emit_pointer_event (ClutterEvent *event,
1754 ClutterInputDevice *device)
1756 /* Using the global variable directly, since it has to be initialized
1759 ClutterMainContext *context = ClutterCntx;
1761 if (G_UNLIKELY (context->pointer_grab_actor != NULL &&
1765 clutter_actor_event (context->pointer_grab_actor, event, FALSE);
1767 else if (G_UNLIKELY (device != NULL &&
1768 device->pointer_grab_actor != NULL))
1770 /* per device grab */
1771 clutter_actor_event (device->pointer_grab_actor, event, FALSE);
1775 /* no grab, time to capture and bubble */
1776 emit_event (event, FALSE);
1781 emit_keyboard_event (ClutterEvent *event)
1783 ClutterMainContext *context = ClutterCntx;
1785 if (G_UNLIKELY (context->keyboard_grab_actor != NULL))
1786 clutter_actor_event (context->keyboard_grab_actor, event, FALSE);
1788 emit_event (event, TRUE);
1792 unset_motion_last_actor (ClutterActor *actor, ClutterInputDevice *dev)
1794 ClutterMainContext *context = ClutterCntx;
1797 context->motion_last_actor = NULL;
1799 dev->motion_last_actor = NULL;
1802 static ClutterInputDevice * clutter_event_get_device (ClutterEvent *event);
1804 /* This function should perhaps be public and in clutter-event.c ?
1806 static ClutterInputDevice *
1807 clutter_event_get_device (ClutterEvent *event)
1809 g_return_val_if_fail (event != NULL, NULL);
1811 switch (event->type)
1813 case CLUTTER_NOTHING:
1814 case CLUTTER_STAGE_STATE:
1815 case CLUTTER_DESTROY_NOTIFY:
1816 case CLUTTER_CLIENT_MESSAGE:
1817 case CLUTTER_DELETE:
1822 case CLUTTER_BUTTON_PRESS:
1823 case CLUTTER_BUTTON_RELEASE:
1824 return event->button.device;
1825 case CLUTTER_MOTION:
1826 return event->motion.device;
1827 case CLUTTER_SCROLL:
1828 return event->scroll.device;
1830 case CLUTTER_KEY_PRESS:
1831 case CLUTTER_KEY_RELEASE:
1838 generate_enter_leave_events (ClutterEvent *event)
1840 ClutterMainContext *context = ClutterCntx;
1841 ClutterActor *motion_current_actor = event->motion.source;
1842 ClutterActor *last_actor = context->motion_last_actor;
1843 ClutterInputDevice *device = clutter_event_get_device (event);
1846 last_actor = device->motion_last_actor;
1848 if (last_actor != motion_current_actor)
1850 if (motion_current_actor)
1855 cev.crossing.device = device;
1856 clutter_event_get_coords (event, &x, &y);
1858 if (context->motion_last_actor)
1860 cev.crossing.type = CLUTTER_LEAVE;
1861 cev.crossing.time = event->any.time;
1862 cev.crossing.flags = 0;
1865 cev.crossing.source = last_actor;
1866 cev.crossing.stage = event->any.stage;
1867 cev.crossing.related = motion_current_actor;
1869 emit_pointer_event (&cev, device);
1872 cev.crossing.type = CLUTTER_ENTER;
1873 cev.crossing.time = event->any.time;
1874 cev.crossing.flags = 0;
1877 cev.crossing.source = motion_current_actor;
1878 cev.crossing.stage = event->any.stage;
1880 if (context->motion_last_actor)
1881 cev.crossing.related = last_actor;
1883 cev.crossing.related = NULL;
1885 emit_pointer_event (&cev, device);
1889 if (last_actor && last_actor != motion_current_actor)
1891 g_signal_handlers_disconnect_by_func
1893 G_CALLBACK (unset_motion_last_actor),
1897 if (motion_current_actor && last_actor != motion_current_actor)
1899 g_signal_connect (motion_current_actor, "destroy",
1900 G_CALLBACK (unset_motion_last_actor),
1905 device->motion_last_actor = motion_current_actor;
1907 context->motion_last_actor = motion_current_actor;
1912 * @event: a #ClutterEvent.
1914 * Processes an event. This function should never be called by applications.
1919 clutter_do_event (ClutterEvent *event)
1921 /* FIXME: This should probably be clutter_cook_event() - it would
1922 * take a raw event from the backend and 'cook' it so its more tasty.
1925 ClutterMainContext *context;
1926 ClutterBackend *backend;
1927 ClutterActor *stage;
1928 ClutterInputDevice *device = NULL;
1929 static gint32 motion_last_time = 0L;
1930 gint32 local_motion_time;
1932 context = clutter_context_get_default ();
1933 backend = context->backend;
1934 stage = CLUTTER_ACTOR(event->any.stage);
1939 CLUTTER_TIMESTAMP (EVENT, "Event received");
1941 switch (event->type)
1943 case CLUTTER_NOTHING:
1944 event->any.source = stage;
1949 emit_pointer_event (event, event->crossing.device);
1952 case CLUTTER_DESTROY_NOTIFY:
1953 case CLUTTER_DELETE:
1954 event->any.source = stage;
1955 /* the stage did not handle the event, so we just quit */
1956 if (!clutter_stage_event (CLUTTER_STAGE (stage), event))
1958 if (stage == clutter_stage_get_default())
1959 clutter_main_quit ();
1961 clutter_actor_destroy (stage);
1966 case CLUTTER_KEY_PRESS:
1967 case CLUTTER_KEY_RELEASE:
1969 ClutterActor *actor = NULL;
1971 /* check that we're not a synthetic event with source set */
1972 if (event->any.source == NULL)
1974 actor = clutter_stage_get_key_focus (CLUTTER_STAGE (stage));
1975 event->any.source = actor;
1976 if (G_UNLIKELY (actor == NULL))
1978 g_warning ("No key focus set, discarding");
1983 emit_keyboard_event (event);
1987 case CLUTTER_MOTION:
1988 device = event->motion.device;
1991 local_motion_time = device->motion_last_time;
1993 local_motion_time = motion_last_time;
1995 /* avoid rate throttling for synthetic motion events or if
1996 * the per-actor events are disabled
1998 if (!(event->any.flags & CLUTTER_EVENT_FLAG_SYNTHETIC) ||
1999 !context->motion_events_per_actor)
2001 gint32 frame_rate, delta;
2003 /* avoid issuing too many motion events, which leads to many
2004 * redraws in pick mode (performance penalty)
2006 frame_rate = clutter_get_motion_events_frequency ();
2007 delta = 1000 / frame_rate;
2009 CLUTTER_NOTE (EVENT,
2010 "skip motion event: %s (last:%d, delta:%d, time:%d)",
2011 (event->any.time < (local_motion_time + delta) ? "yes" : "no"),
2016 /* we need to guard against roll-overs and the
2017 * case where the time is rolled backwards and
2018 * the backend is not ensuring a monotonic clock
2022 * http://bugzilla.openedhand.com/show_bug.cgi?id=1130
2024 if (event->any.time >= local_motion_time &&
2025 event->any.time < (local_motion_time + delta))
2028 local_motion_time = event->any.time;
2032 device->motion_last_time = local_motion_time;
2034 motion_last_time = local_motion_time;
2036 /* Only stage gets motion events if clutter_set_motion_events is TRUE,
2037 * and the event is not a synthetic event with source set.
2039 if (!context->motion_events_per_actor &&
2040 event->any.source == NULL)
2042 /* Only stage gets motion events */
2043 event->any.source = stage;
2046 if (context->pointer_grab_actor != NULL)
2048 clutter_actor_event (context->pointer_grab_actor,
2052 else if (device != NULL && device->pointer_grab_actor != NULL)
2054 clutter_actor_event (device->pointer_grab_actor,
2059 /* Trigger handlers on stage in both capture .. */
2060 if (!clutter_actor_event (stage, event, TRUE))
2062 /* and bubbling phase */
2063 clutter_actor_event (stage, event, FALSE);
2070 case CLUTTER_BUTTON_PRESS:
2071 case CLUTTER_BUTTON_RELEASE:
2072 case CLUTTER_SCROLL:
2074 ClutterActor *actor;
2077 clutter_event_get_coords (event, &x, &y);
2079 /* Only do a pick to find the source if source is not already set
2080 * (as it could be in a synthetic event)
2082 if (event->any.source == NULL)
2084 /* Handle release off stage */
2085 if ((x >= clutter_actor_get_width (stage) ||
2086 y >= clutter_actor_get_height (stage) ||
2089 if (event->type == CLUTTER_BUTTON_RELEASE)
2091 CLUTTER_NOTE (EVENT,
2092 "Release off stage received at %i, %i",
2095 event->button.source = stage;
2096 emit_pointer_event (event, event->button.device);
2101 /* Map the event to a reactive actor */
2102 actor = _clutter_do_pick (CLUTTER_STAGE (stage),
2104 CLUTTER_PICK_REACTIVE);
2106 event->any.source = actor;
2112 /* use the source already set in the synthetic event */
2113 actor = event->any.source;
2117 /* FIXME: for an optimisation should check if there are
2118 * actually any reactive actors and avoid the pick all togeather
2119 * (signalling just the stage). Should be big help for gles.
2122 CLUTTER_NOTE (EVENT, "Reactive event received at %i, %i - actor: %p",
2125 /* Create, enter/leave events if needed */
2126 generate_enter_leave_events (event);
2128 if (event->type != CLUTTER_MOTION)
2130 /* Generate click count */
2131 event_click_count_generate (event);
2136 switch (event->type)
2138 case CLUTTER_BUTTON_PRESS:
2139 case CLUTTER_BUTTON_RELEASE:
2140 device = event->button.device;
2142 case CLUTTER_SCROLL:
2143 device = event->scroll.device;
2145 case CLUTTER_MOTION:
2146 /* already handled in the MOTION case of the switch */
2152 emit_pointer_event (event, device);
2156 case CLUTTER_STAGE_STATE:
2157 /* fullscreen / focus - forward to stage */
2158 event->any.source = stage;
2159 clutter_stage_event (CLUTTER_STAGE (stage), event);
2162 case CLUTTER_CLIENT_MESSAGE:
2168 * clutter_get_actor_by_gid
2169 * @id: a #ClutterActor ID.
2171 * Retrieves the #ClutterActor with @id.
2173 * Return value: the actor with the passed id or %NULL. The returned
2174 * actor does not have its reference count increased.
2179 clutter_get_actor_by_gid (guint32 id)
2181 ClutterMainContext *context;
2183 context = clutter_context_get_default ();
2185 g_return_val_if_fail (context != NULL, NULL);
2187 return CLUTTER_ACTOR (clutter_id_pool_lookup (context->id_pool, id));
2191 clutter_base_init (void)
2193 static gboolean initialised = FALSE;
2197 GType foo; /* Quiet gcc */
2201 /* initialise GLib type system */
2204 /* CLUTTER_TYPE_ACTOR */
2205 foo = clutter_actor_get_type ();
2210 * clutter_get_default_frame_rate:
2212 * Retrieves the default frame rate used when creating #ClutterTimeline<!--
2215 * This value is also used to compute the default frequency of motion
2218 * Return value: the default frame rate
2223 clutter_get_default_frame_rate (void)
2225 ClutterMainContext *context;
2227 context = clutter_context_get_default ();
2229 return context->frame_rate;
2233 * clutter_set_default_frame_rate:
2234 * @frames_per_sec: the new default frame rate
2236 * Sets the default frame rate to be used when creating #ClutterTimeline<!--
2242 clutter_set_default_frame_rate (guint frames_per_sec)
2244 ClutterMainContext *context;
2246 context = clutter_context_get_default ();
2248 if (context->frame_rate != frames_per_sec)
2249 context->frame_rate = frames_per_sec;
2254 on_pointer_grab_weak_notify (gpointer data,
2255 GObject *where_the_object_was)
2257 ClutterInputDevice *dev = (ClutterInputDevice *)data;
2258 ClutterMainContext *context;
2260 context = clutter_context_get_default ();
2264 dev->pointer_grab_actor = NULL;
2265 clutter_ungrab_pointer_for_device (dev->id);
2269 context->pointer_grab_actor = NULL;
2270 clutter_ungrab_pointer ();
2275 * clutter_grab_pointer:
2276 * @actor: a #ClutterActor
2278 * Grabs pointer events, after the grab is done all pointer related events
2279 * (press, motion, release, enter, leave and scroll) are delivered to this
2280 * actor directly. The source set in the event will be the actor that would
2281 * have received the event if the pointer grab was not in effect.
2283 * If you wish to grab all the pointer events for a specific input device,
2284 * you should use clutter_grab_pointer_for_device().
2289 clutter_grab_pointer (ClutterActor *actor)
2291 ClutterMainContext *context;
2293 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2295 context = clutter_context_get_default ();
2297 if (context->pointer_grab_actor == actor)
2300 if (context->pointer_grab_actor)
2302 g_object_weak_unref (G_OBJECT (context->pointer_grab_actor),
2303 on_pointer_grab_weak_notify,
2305 context->pointer_grab_actor = NULL;
2310 context->pointer_grab_actor = actor;
2312 g_object_weak_ref (G_OBJECT (actor),
2313 on_pointer_grab_weak_notify,
2319 * clutter_grab_pointer_for_device:
2320 * @actor: a #ClutterActor
2321 * @id: a device id, or -1
2323 * Grabs all the pointer events coming from the device @id for @actor.
2325 * If @id is -1 then this function is equivalent to clutter_grab_pointer().
2330 clutter_grab_pointer_for_device (ClutterActor *actor,
2333 ClutterInputDevice *dev;
2335 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2337 /* essentially a global grab */
2340 clutter_grab_pointer (actor);
2344 dev = clutter_get_input_device_for_id (id);
2349 if (dev->pointer_grab_actor == actor)
2352 if (dev->pointer_grab_actor)
2354 g_object_weak_unref (G_OBJECT (dev->pointer_grab_actor),
2355 on_pointer_grab_weak_notify,
2357 dev->pointer_grab_actor = NULL;
2362 dev->pointer_grab_actor = actor;
2364 g_object_weak_ref (G_OBJECT (actor),
2365 on_pointer_grab_weak_notify,
2372 * clutter_ungrab_pointer:
2374 * Removes an existing grab of the pointer.
2379 clutter_ungrab_pointer (void)
2381 clutter_grab_pointer (NULL);
2385 * clutter_ungrab_pointer_for_device:
2388 * Removes an existing grab of the pointer events for device @id.
2393 clutter_ungrab_pointer_for_device (gint id)
2395 clutter_grab_pointer_for_device (NULL, id);
2400 * clutter_get_pointer_grab:
2402 * Queries the current pointer grab of clutter.
2404 * Return value: the actor currently holding the pointer grab, or NULL if there is no grab.
2409 clutter_get_pointer_grab (void)
2411 ClutterMainContext *context;
2412 context = clutter_context_get_default ();
2414 return context->pointer_grab_actor;
2419 on_keyboard_grab_weak_notify (gpointer data,
2420 GObject *where_the_object_was)
2422 ClutterMainContext *context;
2424 context = clutter_context_get_default ();
2425 context->keyboard_grab_actor = NULL;
2427 clutter_ungrab_keyboard ();
2431 * clutter_grab_keyboard:
2432 * @actor: a #ClutterActor
2434 * Grabs keyboard events, after the grab is done keyboard events ("key-press-event"
2435 * and "key-release-event") are delivered to this actor directly. The source
2436 * set in the event will be the actor that would have received the event if the
2437 * keyboard grab was not in effect.
2442 clutter_grab_keyboard (ClutterActor *actor)
2444 ClutterMainContext *context;
2446 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2448 context = clutter_context_get_default ();
2450 if (context->keyboard_grab_actor == actor)
2453 if (context->keyboard_grab_actor)
2455 g_object_weak_unref (G_OBJECT (context->keyboard_grab_actor),
2456 on_keyboard_grab_weak_notify,
2458 context->keyboard_grab_actor = NULL;
2463 context->keyboard_grab_actor = actor;
2465 g_object_weak_ref (G_OBJECT (actor),
2466 on_keyboard_grab_weak_notify,
2472 * clutter_ungrab_keyboard:
2474 * Removes an existing grab of the keyboard.
2479 clutter_ungrab_keyboard (void)
2481 clutter_grab_keyboard (NULL);
2485 * clutter_get_keyboard_grab:
2487 * Queries the current keyboard grab of clutter.
2489 * Return value: the actor currently holding the keyboard grab, or NULL if there is no grab.
2494 clutter_get_keyboard_grab (void)
2496 ClutterMainContext *context;
2497 context = clutter_context_get_default ();
2499 return context->keyboard_grab_actor;
2503 * clutter_get_motion_events_frequency:
2505 * Retrieves the number of motion events per second that are delivered
2508 * See clutter_set_motion_events_frequency().
2510 * Return value: the number of motion events per second
2515 clutter_get_motion_events_frequency (void)
2517 ClutterMainContext *context = clutter_context_get_default ();
2519 if (G_LIKELY (context->motion_frequency == 0))
2523 frequency = clutter_default_fps / 4;
2524 frequency = CLAMP (frequency, 20, 45);
2529 return context->motion_frequency;
2533 * clutter_set_motion_events_frequency:
2534 * @frequency: the number of motion events per second, or 0 for the
2537 * Sets the motion events frequency. Setting this to a non-zero value
2538 * will override the default setting, so it should be rarely used.
2540 * Motion events are delivered from the default backend to the stage
2541 * and are used to generate the enter/leave events pair. This might lead
2542 * to a performance penalty due to the way the actors are identified.
2543 * Using this function is possible to reduce the frequency of the motion
2544 * events delivery to the stage.
2549 clutter_set_motion_events_frequency (guint frequency)
2551 ClutterMainContext *context = clutter_context_get_default ();
2553 /* never allow the motion events to exceed the default frame rate */
2554 context->motion_frequency = CLAMP (frequency, 1, clutter_default_fps);
2558 * clutter_clear_glyph_cache:
2560 * Clears the internal cache of glyphs used by the Pango
2561 * renderer. This will free up some memory and GL texture
2562 * resources. The cache will be automatically refilled as more text is
2568 clutter_clear_glyph_cache (void)
2570 if (CLUTTER_CONTEXT ()->font_map)
2571 cogl_pango_font_map_clear_glyph_cache (CLUTTER_CONTEXT ()->font_map);
2575 * clutter_set_use_mipmapped_text:
2576 * @value: %TRUE to enable mipmapping or %FALSE to disable.
2578 * Sets whether subsequent text rendering operations will use
2579 * mipmapped textures or not. Using mipmapped textures will improve
2580 * the quality for scaled down text but will use more texture memory.
2585 clutter_set_use_mipmapped_text (gboolean value)
2587 if (CLUTTER_CONTEXT ()->font_map)
2588 cogl_pango_font_map_set_use_mipmapping (CLUTTER_CONTEXT ()->font_map,
2593 * clutter_get_use_mipmapped_text:
2595 * Gets whether mipmapped textures are used in text operations.
2596 * See clutter_set_use_mipmapped_text().
2598 * Return value: %TRUE if text operations should use mipmapped
2604 clutter_get_use_mipmapped_text (void)
2606 CoglPangoFontMap *font_map = NULL;
2608 font_map = CLUTTER_CONTEXT ()->font_map;
2610 if (G_LIKELY (font_map))
2611 return cogl_pango_font_map_get_use_mipmapping (font_map);
2617 * clutter_get_input_device_for_id:
2620 * Retrieves the #ClutterInputDevice from its id.
2622 * Return value: a #ClutterInputDevice, or %NULL
2626 ClutterInputDevice *
2627 clutter_get_input_device_for_id (gint id)
2630 ClutterInputDevice *device = NULL;
2631 ClutterMainContext *context;
2633 context = clutter_context_get_default ();
2635 for (item = context->input_devices;
2639 device = item->data;
2641 if (device->id == id)
2649 * clutter_get_font_map:
2651 * Retrieves the #PangoFontMap instance used by Clutter.
2652 * You can use the global font map object with the COGL
2655 * Return value: the #PangoFontMap instance. The returned
2656 * value is owned by Clutter and it should never be
2662 clutter_get_font_map (void)
2664 if (CLUTTER_CONTEXT ()->font_map)
2665 return PANGO_FONT_MAP (CLUTTER_CONTEXT ()->font_map);