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 font_options = cairo_font_options_copy (font_options);
457 resolution = clutter_backend_get_resolution (backend);
459 font_desc = pango_font_description_from_string (font_name);
462 resolution = 96.0; /* fall back */
464 if (CLUTTER_CONTEXT ()->user_font_options)
465 cairo_font_options_merge (font_options,
466 CLUTTER_CONTEXT ()->user_font_options);
468 pango_context_set_font_description (context, font_desc);
469 pango_cairo_context_set_font_options (context, font_options);
470 cairo_font_options_destroy (font_options);
471 pango_cairo_context_set_resolution (context, resolution);
473 pango_font_description_free (font_desc);
477 _clutter_context_get_pango_context (ClutterMainContext *self)
479 if (G_UNLIKELY (self->pango_context == NULL))
481 PangoContext *context;
483 context = cogl_pango_font_map_create_context (self->font_map);
484 self->pango_context = context;
486 g_signal_connect (self->backend, "resolution-changed",
487 G_CALLBACK (update_pango_context),
488 self->pango_context);
489 g_signal_connect (self->backend, "font-changed",
490 G_CALLBACK (update_pango_context),
491 self->pango_context);
494 update_pango_context (self->backend, self->pango_context);
496 return self->pango_context;
500 _clutter_context_create_pango_context (ClutterMainContext *self)
502 PangoContext *context;
504 context = cogl_pango_font_map_create_context (self->font_map);
505 update_pango_context (self->backend, context);
513 * Terminates the Clutter mainloop.
516 clutter_main_quit (void)
518 g_return_if_fail (main_loops != NULL);
520 g_main_loop_quit (main_loops->data);
524 * clutter_main_level:
526 * Retrieves the depth of the Clutter mainloop.
528 * Return value: The level of the mainloop.
531 clutter_main_level (void)
533 return clutter_main_loop_level;
539 * Starts the Clutter mainloop.
546 /* Make sure there is a context */
549 if (!clutter_is_initialized)
551 g_warning ("Called clutter_main() but Clutter wasn't initialised. "
552 "You must call clutter_init() first.");
558 clutter_main_loop_level++;
560 loop = g_main_loop_new (NULL, TRUE);
561 main_loops = g_slist_prepend (main_loops, loop);
563 #ifdef HAVE_CLUTTER_FRUITY
564 /* clutter fruity creates an application that forwards events and manually
567 clutter_fruity_main ();
569 if (g_main_loop_is_running (main_loops->data))
571 clutter_threads_leave ();
572 g_main_loop_run (loop);
573 clutter_threads_enter ();
577 main_loops = g_slist_remove (main_loops, loop);
579 g_main_loop_unref (loop);
581 clutter_main_loop_level--;
587 clutter_threads_impl_lock (void)
589 if (clutter_threads_mutex)
590 g_mutex_lock (clutter_threads_mutex);
594 clutter_threads_impl_unlock (void)
596 if (clutter_threads_mutex)
597 g_mutex_unlock (clutter_threads_mutex);
601 * clutter_threads_init:
603 * Initialises the Clutter threading mechanism, so that Clutter API can be
604 * called by multiple threads, using clutter_threads_enter() and
605 * clutter_threads_leave() to mark the critical sections.
607 * You must call g_thread_init() before this function.
609 * This function must be called before clutter_init().
614 clutter_threads_init (void)
616 if (!g_thread_supported ())
617 g_error ("g_thread_init() must be called before clutter_threads_init()");
619 clutter_threads_mutex = g_mutex_new ();
621 if (!clutter_threads_lock)
622 clutter_threads_lock = clutter_threads_impl_lock;
624 if (!clutter_threads_unlock)
625 clutter_threads_unlock = clutter_threads_impl_unlock;
629 * clutter_threads_set_lock_functions:
630 * @enter_fn: function called when aquiring the Clutter main lock
631 * @leave_fn: function called when releasing the Clutter main lock
633 * Allows the application to replace the standard method that
634 * Clutter uses to protect its data structures. Normally, Clutter
635 * creates a single #GMutex that is locked by clutter_threads_enter(),
636 * and released by clutter_threads_leave(); using this function an
637 * application provides, instead, a function @enter_fn that is
638 * called by clutter_threads_enter() and a function @leave_fn that is
639 * called by clutter_threads_leave().
641 * The functions must provide at least same locking functionality
642 * as the default implementation, but can also do extra application
643 * specific processing.
645 * As an example, consider an application that has its own recursive
646 * lock that when held, holds the Clutter lock as well. When Clutter
647 * unlocks the Clutter lock when entering a recursive main loop, the
648 * application must temporarily release its lock as well.
650 * Most threaded Clutter apps won't need to use this method.
652 * This method must be called before clutter_threads_init(), and cannot
653 * be called multiple times.
658 clutter_threads_set_lock_functions (GCallback enter_fn,
661 g_return_if_fail (clutter_threads_lock == NULL &&
662 clutter_threads_unlock == NULL);
664 clutter_threads_lock = enter_fn;
665 clutter_threads_unlock = leave_fn;
672 GDestroyNotify notify;
673 } ClutterThreadsDispatch;
676 clutter_threads_dispatch (gpointer data)
678 ClutterThreadsDispatch *dispatch = data;
679 gboolean ret = FALSE;
681 clutter_threads_enter ();
683 if (!g_source_is_destroyed (g_main_current_source ()))
684 ret = dispatch->func (dispatch->data);
686 clutter_threads_leave ();
692 clutter_threads_dispatch_free (gpointer data)
694 ClutterThreadsDispatch *dispatch = data;
696 /* XXX - we cannot hold the thread lock here because the main loop
697 * might destroy a source while still in the dispatcher function; so
698 * knowing whether the lock is being held or not is not known a priori.
700 * see bug: http://bugzilla.gnome.org/show_bug.cgi?id=459555
702 if (dispatch->notify)
703 dispatch->notify (dispatch->data);
705 g_slice_free (ClutterThreadsDispatch, dispatch);
709 * clutter_threads_add_idle_full:
710 * @priority: the priority of the timeout source. Typically this will be in the
711 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE
712 * @func: function to call
713 * @data: data to pass to the function
714 * @notify: functio to call when the idle source is removed
716 * Adds a function to be called whenever there are no higher priority
717 * events pending. If the function returns %FALSE it is automatically
718 * removed from the list of event sources and will not be called again.
720 * This function can be considered a thread-safe variant of g_idle_add_full():
721 * it will call @function while holding the Clutter lock. It is logically
722 * equivalent to the following implementation:
726 * idle_safe_callback (gpointer data)
728 * SafeClosure *closure = data;
729 * gboolean res = FALSE;
731 * /* mark the critical section */
733 * clutter_threads_enter();
735 * /* the callback does not need to acquire the Clutter
736 * * lock itself, as it is held by the this proxy handler
738 * res = closure->callback (closure->data);
740 * clutter_threads_leave();
745 * add_safe_idle (GSourceFunc callback,
748 * SafeClosure *closure = g_new0 (SafeClosure, 1);
750 * closure->callback = callback;
751 * closure->data = data;
753 * return g_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
754 * idle_safe_callback,
760 * This function should be used by threaded applications to make sure
761 * that @func is emitted under the Clutter threads lock and invoked
762 * from the same thread that started the Clutter main loop. For instance,
763 * it can be used to update the UI using the results from a worker
768 * update_ui (gpointer data)
770 * SomeClosure *closure = data;
772 * /* it is safe to call Clutter API from this function because
773 * * it is invoked from the same thread that started the main
774 * * loop and under the Clutter thread lock
776 * clutter_label_set_text (CLUTTER_LABEL (closure->label),
779 * g_object_unref (closure->label);
785 * /* within another thread */
786 * closure = g_new0 (SomeClosure, 1);
787 * /* always take a reference on GObject instances */
788 * closure->label = g_object_ref (my_application->label);
789 * closure->text = g_strdup (processed_text_to_update_the_label);
791 * clutter_threads_add_idle_full (G_PRIORITY_HIGH_IDLE,
797 * Return value: the ID (greater than 0) of the event source.
802 clutter_threads_add_idle_full (gint priority,
805 GDestroyNotify notify)
807 ClutterThreadsDispatch *dispatch;
809 g_return_val_if_fail (func != NULL, 0);
811 dispatch = g_slice_new (ClutterThreadsDispatch);
812 dispatch->func = func;
813 dispatch->data = data;
814 dispatch->notify = notify;
816 return g_idle_add_full (priority,
817 clutter_threads_dispatch, dispatch,
818 clutter_threads_dispatch_free);
822 * clutter_threads_add_idle:
823 * @func: function to call
824 * @data: data to pass to the function
826 * Simple wrapper around clutter_threads_add_idle_full() using the
829 * Return value: the ID (greater than 0) of the event source.
834 clutter_threads_add_idle (GSourceFunc func,
837 g_return_val_if_fail (func != NULL, 0);
839 return clutter_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
845 * clutter_threads_add_timeout_full:
846 * @priority: the priority of the timeout source. Typically this will be in the
847 * range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
848 * @interval: the time between calls to the function, in milliseconds
849 * @func: function to call
850 * @data: data to pass to the function
851 * @notify: function to call when the timeout source is removed
853 * Sets a function to be called at regular intervals holding the Clutter
854 * threads lock, with the given priority. The function is called repeatedly
855 * until it returns %FALSE, at which point the timeout is automatically
856 * removed and the function will not be called again. The @notify function
857 * is called when the timeout is removed.
859 * The first call to the function will be at the end of the first @interval.
861 * It is important to note that, due to how the Clutter main loop is
862 * implemented, the timing will not be accurate and it will not try to
863 * "keep up" with the interval. A more reliable source is available
864 * using clutter_threads_add_frame_source_full(), which is also internally
865 * used by #ClutterTimeline.
867 * See also clutter_threads_add_idle_full().
869 * Return value: the ID (greater than 0) of the event source.
874 clutter_threads_add_timeout_full (gint priority,
878 GDestroyNotify notify)
880 ClutterThreadsDispatch *dispatch;
882 g_return_val_if_fail (func != NULL, 0);
884 dispatch = g_slice_new (ClutterThreadsDispatch);
885 dispatch->func = func;
886 dispatch->data = data;
887 dispatch->notify = notify;
889 return g_timeout_add_full (priority,
891 clutter_threads_dispatch, dispatch,
892 clutter_threads_dispatch_free);
896 * clutter_threads_add_timeout:
897 * @interval: the time between calls to the function, in milliseconds
898 * @func: function to call
899 * @data: data to pass to the function
901 * Simple wrapper around clutter_threads_add_timeout_full().
903 * Return value: the ID (greater than 0) of the event source.
908 clutter_threads_add_timeout (guint interval,
912 g_return_val_if_fail (func != NULL, 0);
914 return clutter_threads_add_timeout_full (G_PRIORITY_DEFAULT,
921 * clutter_threads_add_frame_source_full:
922 * @priority: the priority of the frame source. Typically this will be in the
923 * range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
924 * @interval: the time between calls to the function, in milliseconds
925 * @func: function to call
926 * @data: data to pass to the function
927 * @notify: function to call when the timeout source is removed
929 * Sets a function to be called at regular intervals holding the Clutter
930 * threads lock, with the given priority. The function is called repeatedly
931 * until it returns %FALSE, at which point the timeout is automatically
932 * removed and the function will not be called again. The @notify function
933 * is called when the timeout is removed.
935 * This function is similar to clutter_threads_add_timeout_full()
936 * except that it will try to compensate for delays. For example, if
937 * @func takes half the interval time to execute then the function
938 * will be called again half the interval time after it finished. In
939 * contrast clutter_threads_add_timeout_full() would not fire until a
940 * full interval after the function completes so the delay between
941 * calls would be @interval * 1.5. This function does not however try
942 * to invoke the function multiple times to catch up missing frames if
943 * @func takes more than @interval ms to execute.
945 * See also clutter_threads_add_idle_full().
947 * Return value: the ID (greater than 0) of the event source.
952 clutter_threads_add_frame_source_full (gint priority,
956 GDestroyNotify notify)
958 ClutterThreadsDispatch *dispatch;
960 g_return_val_if_fail (func != NULL, 0);
962 dispatch = g_slice_new (ClutterThreadsDispatch);
963 dispatch->func = func;
964 dispatch->data = data;
965 dispatch->notify = notify;
967 return clutter_frame_source_add_full (priority,
969 clutter_threads_dispatch, dispatch,
970 clutter_threads_dispatch_free);
974 * clutter_threads_add_frame_source:
975 * @interval: the time between calls to the function, in milliseconds
976 * @func: function to call
977 * @data: data to pass to the function
979 * Simple wrapper around clutter_threads_add_frame_source_full().
981 * Return value: the ID (greater than 0) of the event source.
986 clutter_threads_add_frame_source (guint interval,
990 g_return_val_if_fail (func != NULL, 0);
992 return clutter_threads_add_frame_source_full (G_PRIORITY_DEFAULT,
999 * clutter_threads_enter:
1001 * Locks the Clutter thread lock.
1006 clutter_threads_enter (void)
1008 if (clutter_threads_lock)
1009 (* clutter_threads_lock) ();
1013 * clutter_threads_leave:
1015 * Unlocks the Clutter thread lock.
1020 clutter_threads_leave (void)
1022 if (clutter_threads_unlock)
1023 (* clutter_threads_unlock) ();
1028 * clutter_get_debug_enabled:
1030 * Check if clutter has debugging turned on.
1032 * Return value: TRUE if debugging is turned on, FALSE otherwise.
1035 clutter_get_debug_enabled (void)
1037 #ifdef CLUTTER_ENABLE_DEBUG
1038 return clutter_debug_flags != 0;
1044 ClutterMainContext *
1045 clutter_context_get_default (void)
1047 if (G_UNLIKELY(!ClutterCntx))
1049 ClutterMainContext *ctx;
1051 ClutterCntx = ctx = g_new0 (ClutterMainContext, 1);
1052 ctx->backend = g_object_new (_clutter_backend_impl_get_type (), NULL);
1054 ctx->is_initialized = FALSE;
1055 ctx->motion_events_per_actor = TRUE;
1057 #ifdef CLUTTER_ENABLE_DEBUG
1058 ctx->timer = g_timer_new ();
1059 g_timer_start (ctx->timer);
1067 * clutter_get_timestamp:
1069 * Returns the approximate number of microseconds passed since clutter was
1072 * Return value: Number of microseconds since clutter_init() was called.
1075 clutter_get_timestamp (void)
1077 #ifdef CLUTTER_ENABLE_DEBUG
1078 ClutterMainContext *ctx;
1081 ctx = clutter_context_get_default ();
1083 /* FIXME: may need a custom timer for embedded setups */
1084 seconds = g_timer_elapsed (ctx->timer, NULL);
1086 return (gulong)(seconds / 1.0e-6);
1093 clutter_arg_direction_cb (const char *key,
1097 clutter_text_direction =
1098 (strcmp (value, "rtl") == 0) ? PANGO_DIRECTION_RTL
1099 : PANGO_DIRECTION_LTR;
1104 #ifdef CLUTTER_ENABLE_DEBUG
1106 clutter_arg_debug_cb (const char *key,
1110 clutter_debug_flags |=
1111 g_parse_debug_string (value,
1113 G_N_ELEMENTS (clutter_debug_keys));
1118 clutter_arg_no_debug_cb (const char *key,
1122 clutter_debug_flags &=
1123 ~g_parse_debug_string (value,
1125 G_N_ELEMENTS (clutter_debug_keys));
1128 #endif /* CLUTTER_ENABLE_DEBUG */
1131 clutter_init_error_quark (void)
1133 return g_quark_from_static_string ("clutter-init-error-quark");
1136 static ClutterInitError
1137 clutter_init_real (GError **error)
1139 ClutterMainContext *ctx;
1140 ClutterActor *stage;
1142 ClutterBackend *backend;
1144 /* Note, creates backend if not already existing, though parse args will
1145 * have likely created it
1147 ctx = clutter_context_get_default ();
1148 backend = ctx->backend;
1150 if (!ctx->options_parsed)
1152 g_set_error (error, CLUTTER_INIT_ERROR,
1153 CLUTTER_INIT_ERROR_INTERNAL,
1154 "When using clutter_get_option_group_without_init() "
1155 "you must parse options before calling clutter_init()");
1157 return CLUTTER_INIT_ERROR_INTERNAL;
1161 * Call backend post parse hooks.
1163 if (!_clutter_backend_post_parse (backend, error))
1164 return CLUTTER_INIT_ERROR_BACKEND;
1166 /* Stage will give us a GL Context etc */
1167 stage = clutter_stage_get_default ();
1171 g_set_error (error, CLUTTER_INIT_ERROR,
1172 CLUTTER_INIT_ERROR_INTERNAL,
1173 "Unable to create the default stage");
1175 g_critical ("Unable to create the default stage");
1177 return CLUTTER_INIT_ERROR_INTERNAL;
1180 clutter_actor_realize (stage);
1182 if (!CLUTTER_ACTOR_IS_REALIZED (stage))
1185 g_set_error (error, CLUTTER_INIT_ERROR,
1186 CLUTTER_INIT_ERROR_INTERNAL,
1187 "Unable to realize the default stage");
1189 g_critical ("Unable to realize the default stage");
1191 return CLUTTER_INIT_ERROR_INTERNAL;
1194 /* Now we can safely assume we have a valid GL context and can
1195 * start issueing cogl commands
1199 * Resolution requires display to be open, so can only be queried after
1200 * the post_parse hooks run.
1202 * NB: cogl_pango requires a Cogl context.
1204 ctx->font_map = COGL_PANGO_FONT_MAP (cogl_pango_font_map_new ());
1206 resolution = clutter_backend_get_resolution (ctx->backend);
1207 cogl_pango_font_map_set_resolution (ctx->font_map, resolution);
1208 cogl_pango_font_map_set_use_mipmapping (ctx->font_map, TRUE);
1210 clutter_text_direction = clutter_get_text_direction ();
1213 /* Figure out framebuffer masks used for pick */
1214 cogl_get_bitmasks (&ctx->fb_r_mask, &ctx->fb_g_mask, &ctx->fb_b_mask, NULL);
1216 ctx->fb_r_mask_used = ctx->fb_r_mask;
1217 ctx->fb_g_mask_used = ctx->fb_g_mask;
1218 ctx->fb_b_mask_used = ctx->fb_b_mask;
1220 #ifndef HAVE_CLUTTER_FRUITY
1221 /* We always do fuzzy picking for the fruity backend */
1222 if (g_getenv ("CLUTTER_FUZZY_PICK") != NULL)
1225 ctx->fb_r_mask_used--;
1226 ctx->fb_g_mask_used--;
1227 ctx->fb_b_mask_used--;
1230 /* Initiate event collection */
1231 _clutter_backend_init_events (ctx->backend);
1233 /* finally features - will call to backend and cogl */
1234 _clutter_feature_init ();
1236 clutter_stage_set_title (CLUTTER_STAGE (stage), g_get_prgname ());
1238 clutter_is_initialized = TRUE;
1239 ctx->is_initialized = TRUE;
1241 return CLUTTER_INIT_SUCCESS;
1244 static GOptionEntry clutter_args[] = {
1245 { "clutter-show-fps", 0, 0, G_OPTION_ARG_NONE, &clutter_show_fps,
1246 N_("Show frames per second"), NULL },
1247 { "clutter-default-fps", 0, 0, G_OPTION_ARG_INT, &clutter_default_fps,
1248 N_("Default frame rate"), "FPS" },
1249 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &clutter_fatal_warnings,
1250 N_("Make all warnings fatal"), NULL },
1251 { "clutter-text-direction", 0, 0, G_OPTION_ARG_CALLBACK,
1252 clutter_arg_direction_cb,
1253 N_("Direction for the text"), "DIRECTION" },
1254 #ifdef CLUTTER_ENABLE_DEBUG
1255 { "clutter-debug", 0, 0, G_OPTION_ARG_CALLBACK, clutter_arg_debug_cb,
1256 N_("Clutter debugging flags to set"), "FLAGS" },
1257 { "clutter-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, clutter_arg_no_debug_cb,
1258 N_("Clutter debugging flags to unset"), "FLAGS" },
1259 #endif /* CLUTTER_ENABLE_DEBUG */
1263 /* pre_parse_hook: initialise variables depending on environment
1264 * variables; these variables might be overridden by the command
1265 * line arguments that are going to be parsed after.
1268 pre_parse_hook (GOptionContext *context,
1269 GOptionGroup *group,
1273 ClutterMainContext *clutter_context;
1274 ClutterBackend *backend;
1275 const char *env_string;
1277 if (clutter_is_initialized)
1280 if (setlocale (LC_ALL, "") == NULL)
1281 g_warning ("Locale not supported by C library.\n"
1282 "Using the fallback 'C' locale.");
1284 clutter_context = clutter_context_get_default ();
1286 clutter_context->id_pool = clutter_id_pool_new (256);
1288 backend = clutter_context->backend;
1289 g_assert (CLUTTER_IS_BACKEND (backend));
1291 #ifdef CLUTTER_ENABLE_DEBUG
1292 env_string = g_getenv ("CLUTTER_DEBUG");
1293 if (env_string != NULL)
1295 clutter_debug_flags =
1296 g_parse_debug_string (env_string,
1298 G_N_ELEMENTS (clutter_debug_keys));
1301 #endif /* CLUTTER_ENABLE_DEBUG */
1303 env_string = g_getenv ("CLUTTER_SHOW_FPS");
1305 clutter_show_fps = TRUE;
1307 env_string = g_getenv ("CLUTTER_DEFAULT_FPS");
1310 gint default_fps = g_ascii_strtoll (env_string, NULL, 10);
1312 clutter_default_fps = CLAMP (default_fps, 1, 1000);
1315 return _clutter_backend_pre_parse (backend, error);
1318 /* post_parse_hook: initialise the context and data structures
1319 * and opens the X display
1322 post_parse_hook (GOptionContext *context,
1323 GOptionGroup *group,
1327 ClutterMainContext *clutter_context;
1328 ClutterBackend *backend;
1330 if (clutter_is_initialized)
1333 clutter_context = clutter_context_get_default ();
1334 backend = clutter_context->backend;
1335 g_assert (CLUTTER_IS_BACKEND (backend));
1337 if (clutter_fatal_warnings)
1339 GLogLevelFlags fatal_mask;
1341 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1342 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1343 g_log_set_always_fatal (fatal_mask);
1346 clutter_context->frame_rate = clutter_default_fps;
1347 clutter_context->options_parsed = TRUE;
1350 * If not asked to defer display setup, call clutter_init_real(),
1351 * which in turn calls the backend post parse hooks.
1353 if (!clutter_context->defer_display_setup)
1354 return clutter_init_real (error);
1360 * clutter_get_option_group:
1362 * Returns a #GOptionGroup for the command line arguments recognized
1363 * by Clutter. You should add this group to your #GOptionContext with
1364 * g_option_context_add_group(), if you are using g_option_context_parse()
1365 * to parse your commandline arguments.
1367 * Calling g_option_context_parse() with Clutter's #GOptionGroup will result
1368 * in Clutter's initialization. That is, the following code:
1371 * g_option_context_set_main_group (context, clutter_get_option_group ());
1372 * res = g_option_context_parse (context, &argc, &argc, NULL);
1375 * is functionally equivalent to:
1378 * clutter_init (&argc, &argv);
1381 * After g_option_context_parse() on a #GOptionContext containing the
1382 * Clutter #GOptionGroup has returned %TRUE, Clutter is guaranteed to be
1385 * Return value: a #GOptionGroup for the commandline arguments
1386 * recognized by Clutter
1391 clutter_get_option_group (void)
1393 ClutterMainContext *context;
1394 GOptionGroup *group;
1396 clutter_base_init ();
1398 context = clutter_context_get_default ();
1400 group = g_option_group_new ("clutter",
1401 _("Clutter Options"),
1402 _("Show Clutter Options"),
1406 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
1407 g_option_group_add_entries (group, clutter_args);
1408 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
1410 /* add backend-specific options */
1411 _clutter_backend_add_options (context->backend, group);
1417 * clutter_get_option_group_without_init:
1419 * Returns a #GOptionGroup for the command line arguments recognized
1420 * by Clutter. You should add this group to your #GOptionContext with
1421 * g_option_context_add_group(), if you are using g_option_context_parse()
1422 * to parse your commandline arguments. Unlike clutter_get_option_group(),
1423 * calling g_option_context_parse() with the #GOptionGroup returned by this
1424 * function requires a subsequent explicit call to clutter_init(); use this
1425 * function when needing to set foreign display connection with
1426 * clutter_x11_set_display(), or with gtk_clutter_init().
1428 * Return value: a #GOptionGroup for the commandline arguments
1429 * recognized by Clutter
1434 clutter_get_option_group_without_init (void)
1436 ClutterMainContext *context;
1437 GOptionGroup *group;
1439 clutter_base_init ();
1441 context = clutter_context_get_default ();
1442 context->defer_display_setup = TRUE;
1444 group = clutter_get_option_group ();
1450 * clutter_init_with_args:
1451 * @argc: a pointer to the number of command line arguments
1452 * @argv: a pointer to the array of command line arguments
1453 * @parameter_string: a string which is displayed in the
1454 * first line of <option>--help</option> output, after
1455 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
1456 * @entries: a %NULL terminated array of #GOptionEntry<!-- -->s
1457 * describing the options of your program
1458 * @translation_domain: a translation domain to use for translating
1459 * the <option>--help</option> output for the options in @entries
1460 * with gettext(), or %NULL
1461 * @error: a return location for a #GError
1463 * This function does the same work as clutter_init(). Additionally,
1464 * it allows you to add your own command line options, and it
1465 * automatically generates nicely formatted <option>--help</option>
1466 * output. Note that your program will be terminated after writing
1467 * out the help output. Also note that, in case of error, the
1468 * error message will be placed inside @error instead of being
1469 * printed on the display.
1471 * Return value: %CLUTTER_INIT_SUCCESS if Clutter has been successfully
1472 * initialised, or other values or #ClutterInitError in case of
1478 clutter_init_with_args (int *argc,
1480 const char *parameter_string,
1481 GOptionEntry *entries,
1482 const char *translation_domain,
1485 GOptionContext *context;
1486 GOptionGroup *group;
1488 ClutterMainContext *ctx;
1490 if (clutter_is_initialized)
1491 return CLUTTER_INIT_SUCCESS;
1493 clutter_base_init ();
1495 ctx = clutter_context_get_default ();
1497 if (!ctx->defer_display_setup)
1499 if (argc && *argc > 0 && *argv)
1500 g_set_prgname ((*argv)[0]);
1502 group = clutter_get_option_group ();
1503 context = g_option_context_new (parameter_string);
1505 g_option_context_add_group (context, group);
1508 g_option_context_add_main_entries (context, entries, translation_domain);
1510 res = g_option_context_parse (context, argc, argv, error);
1511 g_option_context_free (context);
1513 /* if res is FALSE, the error is filled for
1514 * us by g_option_context_parse()
1518 /* if there has been an error in the initialization, the
1519 * error id will be preserved inside the GError code
1521 if (error && *error)
1522 return (*error)->code;
1524 return CLUTTER_INIT_ERROR_INTERNAL;
1527 return CLUTTER_INIT_SUCCESS;
1530 return clutter_init_real (error);
1534 clutter_parse_args (int *argc,
1537 GOptionContext *option_context;
1538 GOptionGroup *clutter_group;
1539 GError *error = NULL;
1540 gboolean ret = TRUE;
1542 if (clutter_is_initialized)
1545 option_context = g_option_context_new (NULL);
1546 g_option_context_set_ignore_unknown_options (option_context, TRUE);
1547 g_option_context_set_help_enabled (option_context, FALSE);
1549 /* Initiate any command line options from the backend */
1551 clutter_group = clutter_get_option_group ();
1552 g_option_context_set_main_group (option_context, clutter_group);
1554 if (!g_option_context_parse (option_context, argc, argv, &error))
1558 g_warning ("%s", error->message);
1559 g_error_free (error);
1565 g_option_context_free (option_context);
1572 * @argc: The number of arguments in @argv
1573 * @argv: A pointer to an array of arguments.
1575 * It will initialise everything needed to operate with Clutter and
1576 * parses some standard command line options. @argc and @argv are
1577 * adjusted accordingly so your own code will never see those standard
1580 * Return value: 1 on success, < 0 on failure.
1583 clutter_init (int *argc,
1586 ClutterMainContext *ctx;
1587 GError *error = NULL;
1589 if (clutter_is_initialized)
1590 return CLUTTER_INIT_SUCCESS;
1592 clutter_base_init ();
1594 ctx = clutter_context_get_default ();
1596 if (!ctx->defer_display_setup)
1598 if (argc && *argc > 0 && *argv)
1599 g_set_prgname ((*argv)[0]);
1601 /* parse_args will trigger backend creation and things like
1602 * DISPLAY connection etc.
1604 if (clutter_parse_args (argc, argv) == FALSE)
1606 CLUTTER_NOTE (MISC, "failed to parse arguments.");
1607 return CLUTTER_INIT_ERROR_INTERNAL;
1610 return CLUTTER_INIT_SUCCESS;
1613 return clutter_init_real (&error);
1617 _clutter_boolean_handled_accumulator (GSignalInvocationHint *ihint,
1618 GValue *return_accu,
1619 const GValue *handler_return,
1622 gboolean continue_emission;
1623 gboolean signal_handled;
1625 signal_handled = g_value_get_boolean (handler_return);
1626 g_value_set_boolean (return_accu, signal_handled);
1627 continue_emission = !signal_handled;
1629 return continue_emission;
1633 event_click_count_generate (ClutterEvent *event)
1635 /* multiple button click detection */
1636 static gint click_count = 0;
1637 static gint previous_x = -1;
1638 static gint previous_y = -1;
1639 static guint32 previous_time = 0;
1640 static gint previous_button_number = -1;
1642 ClutterBackend *backend;
1643 guint double_click_time;
1644 guint double_click_distance;
1646 backend = clutter_context_get_default ()->backend;
1647 double_click_distance = clutter_backend_get_double_click_distance (backend);
1648 double_click_time = clutter_backend_get_double_click_time (backend);
1650 if (event->button.device != NULL)
1652 click_count = event->button.device->click_count;
1653 previous_x = event->button.device->previous_x;
1654 previous_y = event->button.device->previous_y;
1655 previous_time = event->button.device->previous_time;
1656 previous_button_number = event->button.device->previous_button_number;
1659 switch (event->type)
1661 case CLUTTER_BUTTON_PRESS:
1662 case CLUTTER_SCROLL:
1663 /* check if we are in time and within distance to increment an
1664 * existing click count
1666 if (event->button.time < previous_time + double_click_time &&
1667 (ABS (event->button.x - previous_x) <= double_click_distance) &&
1668 (ABS (event->button.y - previous_y) <= double_click_distance)
1669 && event->button.button == previous_button_number)
1673 else /* start a new click count*/
1676 previous_button_number = event->button.button;
1679 /* store time and position for this click for comparison with
1682 previous_time = event->button.time;
1683 previous_x = event->button.x;
1684 previous_y = event->button.y;
1687 case CLUTTER_BUTTON_RELEASE:
1688 event->button.click_count=click_count;
1694 if (event->button.device != NULL)
1696 event->button.device->click_count = click_count;
1697 event->button.device->previous_x = previous_x;
1698 event->button.device->previous_y = previous_y;
1699 event->button.device->previous_time = previous_time;
1700 event->button.device->previous_button_number = previous_button_number;
1706 emit_event (ClutterEvent *event,
1707 gboolean is_key_event)
1709 #define MAX_EVENT_DEPTH 512
1711 static ClutterActor **event_tree = NULL;
1712 static gboolean lock = FALSE;
1714 ClutterActor *actor;
1715 gint i = 0, n_tree_events = 0;
1717 if (!event->any.source)
1719 g_warning ("No event source set, discarding event");
1723 /* reentrancy check */
1729 /* Sorry Mr Bassi. */
1730 if (G_UNLIKELY (event_tree == NULL))
1731 event_tree = g_new0 (ClutterActor *, MAX_EVENT_DEPTH);
1733 actor = event->any.source;
1735 /* Build 'tree' of emitters for the event */
1736 while (actor && n_tree_events < MAX_EVENT_DEPTH)
1738 ClutterActor *parent;
1740 parent = clutter_actor_get_parent (actor);
1742 if (clutter_actor_get_reactive (actor) ||
1743 parent == NULL || /* stage gets all events */
1744 is_key_event) /* keyboard events are always emitted */
1746 event_tree[n_tree_events++] = g_object_ref (actor);
1753 for (i = n_tree_events-1; i >= 0; i--)
1754 if (clutter_actor_event (event_tree[i], event, TRUE))
1758 for (i = 0; i < n_tree_events; i++)
1759 if (clutter_actor_event (event_tree[i], event, FALSE))
1764 for (i = 0; i < n_tree_events; i++)
1765 g_object_unref (event_tree[i]);
1769 #undef MAX_EVENT_DEPTH
1773 * Emits a pointer event after having prepared the event for delivery (setting
1774 * source, computing click_count, generating enter/leave etc.).
1778 emit_pointer_event (ClutterEvent *event,
1779 ClutterInputDevice *device)
1781 /* Using the global variable directly, since it has to be initialized
1784 ClutterMainContext *context = ClutterCntx;
1786 if (G_UNLIKELY (context->pointer_grab_actor != NULL &&
1790 clutter_actor_event (context->pointer_grab_actor, event, FALSE);
1792 else if (G_UNLIKELY (device != NULL &&
1793 device->pointer_grab_actor != NULL))
1795 /* per device grab */
1796 clutter_actor_event (device->pointer_grab_actor, event, FALSE);
1800 /* no grab, time to capture and bubble */
1801 emit_event (event, FALSE);
1806 emit_keyboard_event (ClutterEvent *event)
1808 ClutterMainContext *context = ClutterCntx;
1810 if (G_UNLIKELY (context->keyboard_grab_actor != NULL))
1811 clutter_actor_event (context->keyboard_grab_actor, event, FALSE);
1813 emit_event (event, TRUE);
1817 unset_motion_last_actor (ClutterActor *actor, ClutterInputDevice *dev)
1819 ClutterMainContext *context = ClutterCntx;
1822 context->motion_last_actor = NULL;
1824 dev->motion_last_actor = NULL;
1827 static ClutterInputDevice * clutter_event_get_device (ClutterEvent *event);
1829 /* This function should perhaps be public and in clutter-event.c ?
1831 static ClutterInputDevice *
1832 clutter_event_get_device (ClutterEvent *event)
1834 g_return_val_if_fail (event != NULL, NULL);
1836 switch (event->type)
1838 case CLUTTER_NOTHING:
1839 case CLUTTER_STAGE_STATE:
1840 case CLUTTER_DESTROY_NOTIFY:
1841 case CLUTTER_CLIENT_MESSAGE:
1842 case CLUTTER_DELETE:
1847 case CLUTTER_BUTTON_PRESS:
1848 case CLUTTER_BUTTON_RELEASE:
1849 return event->button.device;
1850 case CLUTTER_MOTION:
1851 return event->motion.device;
1852 case CLUTTER_SCROLL:
1853 return event->scroll.device;
1855 case CLUTTER_KEY_PRESS:
1856 case CLUTTER_KEY_RELEASE:
1863 generate_enter_leave_events (ClutterEvent *event)
1865 ClutterMainContext *context = ClutterCntx;
1866 ClutterActor *motion_current_actor = event->motion.source;
1867 ClutterActor *last_actor = context->motion_last_actor;
1868 ClutterInputDevice *device = clutter_event_get_device (event);
1871 last_actor = device->motion_last_actor;
1873 if (last_actor != motion_current_actor)
1875 if (motion_current_actor)
1880 cev.crossing.device = device;
1881 clutter_event_get_coords (event, &x, &y);
1883 if (context->motion_last_actor)
1885 cev.crossing.type = CLUTTER_LEAVE;
1886 cev.crossing.time = event->any.time;
1887 cev.crossing.flags = 0;
1890 cev.crossing.source = last_actor;
1891 cev.crossing.stage = event->any.stage;
1892 cev.crossing.related = motion_current_actor;
1894 emit_pointer_event (&cev, device);
1897 cev.crossing.type = CLUTTER_ENTER;
1898 cev.crossing.time = event->any.time;
1899 cev.crossing.flags = 0;
1902 cev.crossing.source = motion_current_actor;
1903 cev.crossing.stage = event->any.stage;
1905 if (context->motion_last_actor)
1906 cev.crossing.related = last_actor;
1908 cev.crossing.related = NULL;
1910 emit_pointer_event (&cev, device);
1914 if (last_actor && last_actor != motion_current_actor)
1916 g_signal_handlers_disconnect_by_func
1918 G_CALLBACK (unset_motion_last_actor),
1922 if (motion_current_actor && last_actor != motion_current_actor)
1924 g_signal_connect (motion_current_actor, "destroy",
1925 G_CALLBACK (unset_motion_last_actor),
1930 device->motion_last_actor = motion_current_actor;
1932 context->motion_last_actor = motion_current_actor;
1937 * @event: a #ClutterEvent.
1939 * Processes an event. This function should never be called by applications.
1944 clutter_do_event (ClutterEvent *event)
1946 /* FIXME: This should probably be clutter_cook_event() - it would
1947 * take a raw event from the backend and 'cook' it so its more tasty.
1950 ClutterMainContext *context;
1951 ClutterBackend *backend;
1952 ClutterActor *stage;
1953 ClutterInputDevice *device = NULL;
1954 static gint32 motion_last_time = 0L;
1955 gint32 local_motion_time;
1957 context = clutter_context_get_default ();
1958 backend = context->backend;
1959 stage = CLUTTER_ACTOR(event->any.stage);
1964 CLUTTER_TIMESTAMP (EVENT, "Event received");
1966 switch (event->type)
1968 case CLUTTER_NOTHING:
1969 event->any.source = stage;
1974 emit_pointer_event (event, event->crossing.device);
1977 case CLUTTER_DESTROY_NOTIFY:
1978 case CLUTTER_DELETE:
1979 event->any.source = stage;
1980 /* the stage did not handle the event, so we just quit */
1981 if (!clutter_stage_event (CLUTTER_STAGE (stage), event))
1983 if (stage == clutter_stage_get_default())
1984 clutter_main_quit ();
1986 clutter_actor_destroy (stage);
1991 case CLUTTER_KEY_PRESS:
1992 case CLUTTER_KEY_RELEASE:
1994 ClutterActor *actor = NULL;
1996 /* check that we're not a synthetic event with source set */
1997 if (event->any.source == NULL)
1999 actor = clutter_stage_get_key_focus (CLUTTER_STAGE (stage));
2000 event->any.source = actor;
2001 if (G_UNLIKELY (actor == NULL))
2003 g_warning ("No key focus set, discarding");
2008 emit_keyboard_event (event);
2012 case CLUTTER_MOTION:
2013 device = event->motion.device;
2016 local_motion_time = device->motion_last_time;
2018 local_motion_time = motion_last_time;
2020 /* avoid rate throttling for synthetic motion events or if
2021 * the per-actor events are disabled
2023 if (!(event->any.flags & CLUTTER_EVENT_FLAG_SYNTHETIC) ||
2024 !context->motion_events_per_actor)
2026 gint32 frame_rate, delta;
2028 /* avoid issuing too many motion events, which leads to many
2029 * redraws in pick mode (performance penalty)
2031 frame_rate = clutter_get_motion_events_frequency ();
2032 delta = 1000 / frame_rate;
2034 CLUTTER_NOTE (EVENT,
2035 "skip motion event: %s (last:%d, delta:%d, time:%d)",
2036 (event->any.time < (local_motion_time + delta) ? "yes" : "no"),
2041 /* we need to guard against roll-overs and the
2042 * case where the time is rolled backwards and
2043 * the backend is not ensuring a monotonic clock
2047 * http://bugzilla.openedhand.com/show_bug.cgi?id=1130
2049 if (event->any.time >= local_motion_time &&
2050 event->any.time < (local_motion_time + delta))
2053 local_motion_time = event->any.time;
2057 device->motion_last_time = local_motion_time;
2059 motion_last_time = local_motion_time;
2061 /* Only stage gets motion events if clutter_set_motion_events is TRUE,
2062 * and the event is not a synthetic event with source set.
2064 if (!context->motion_events_per_actor &&
2065 event->any.source == NULL)
2067 /* Only stage gets motion events */
2068 event->any.source = stage;
2071 if (context->pointer_grab_actor != NULL)
2073 clutter_actor_event (context->pointer_grab_actor,
2077 else if (device != NULL && device->pointer_grab_actor != NULL)
2079 clutter_actor_event (device->pointer_grab_actor,
2084 /* Trigger handlers on stage in both capture .. */
2085 if (!clutter_actor_event (stage, event, TRUE))
2087 /* and bubbling phase */
2088 clutter_actor_event (stage, event, FALSE);
2095 case CLUTTER_BUTTON_PRESS:
2096 case CLUTTER_BUTTON_RELEASE:
2097 case CLUTTER_SCROLL:
2099 ClutterActor *actor;
2102 clutter_event_get_coords (event, &x, &y);
2104 /* Only do a pick to find the source if source is not already set
2105 * (as it could be in a synthetic event)
2107 if (event->any.source == NULL)
2109 /* Handle release off stage */
2110 if ((x >= clutter_actor_get_width (stage) ||
2111 y >= clutter_actor_get_height (stage) ||
2114 if (event->type == CLUTTER_BUTTON_RELEASE)
2116 CLUTTER_NOTE (EVENT,
2117 "Release off stage received at %i, %i",
2120 event->button.source = stage;
2121 emit_pointer_event (event, event->button.device);
2126 /* Map the event to a reactive actor */
2127 actor = _clutter_do_pick (CLUTTER_STAGE (stage),
2129 CLUTTER_PICK_REACTIVE);
2131 event->any.source = actor;
2137 /* use the source already set in the synthetic event */
2138 actor = event->any.source;
2142 /* FIXME: for an optimisation should check if there are
2143 * actually any reactive actors and avoid the pick all togeather
2144 * (signalling just the stage). Should be big help for gles.
2147 CLUTTER_NOTE (EVENT, "Reactive event received at %i, %i - actor: %p",
2150 /* Create, enter/leave events if needed */
2151 generate_enter_leave_events (event);
2153 if (event->type != CLUTTER_MOTION)
2155 /* Generate click count */
2156 event_click_count_generate (event);
2161 switch (event->type)
2163 case CLUTTER_BUTTON_PRESS:
2164 case CLUTTER_BUTTON_RELEASE:
2165 device = event->button.device;
2167 case CLUTTER_SCROLL:
2168 device = event->scroll.device;
2170 case CLUTTER_MOTION:
2171 /* already handled in the MOTION case of the switch */
2177 emit_pointer_event (event, device);
2181 case CLUTTER_STAGE_STATE:
2182 /* fullscreen / focus - forward to stage */
2183 event->any.source = stage;
2184 clutter_stage_event (CLUTTER_STAGE (stage), event);
2187 case CLUTTER_CLIENT_MESSAGE:
2193 * clutter_get_actor_by_gid
2194 * @id: a #ClutterActor ID.
2196 * Retrieves the #ClutterActor with @id.
2198 * Return value: the actor with the passed id or %NULL. The returned
2199 * actor does not have its reference count increased.
2204 clutter_get_actor_by_gid (guint32 id)
2206 ClutterMainContext *context;
2208 context = clutter_context_get_default ();
2210 g_return_val_if_fail (context != NULL, NULL);
2212 return CLUTTER_ACTOR (clutter_id_pool_lookup (context->id_pool, id));
2216 clutter_base_init (void)
2218 static gboolean initialised = FALSE;
2222 GType foo; /* Quiet gcc */
2226 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2227 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2229 /* initialise GLib type system */
2232 /* CLUTTER_TYPE_ACTOR */
2233 foo = clutter_actor_get_type ();
2238 * clutter_get_default_frame_rate:
2240 * Retrieves the default frame rate used when creating #ClutterTimeline<!--
2243 * This value is also used to compute the default frequency of motion
2246 * Return value: the default frame rate
2251 clutter_get_default_frame_rate (void)
2253 ClutterMainContext *context;
2255 context = clutter_context_get_default ();
2257 return context->frame_rate;
2261 * clutter_set_default_frame_rate:
2262 * @frames_per_sec: the new default frame rate
2264 * Sets the default frame rate to be used when creating #ClutterTimeline<!--
2270 clutter_set_default_frame_rate (guint frames_per_sec)
2272 ClutterMainContext *context;
2274 context = clutter_context_get_default ();
2276 if (context->frame_rate != frames_per_sec)
2277 context->frame_rate = frames_per_sec;
2282 on_pointer_grab_weak_notify (gpointer data,
2283 GObject *where_the_object_was)
2285 ClutterInputDevice *dev = (ClutterInputDevice *)data;
2286 ClutterMainContext *context;
2288 context = clutter_context_get_default ();
2292 dev->pointer_grab_actor = NULL;
2293 clutter_ungrab_pointer_for_device (dev->id);
2297 context->pointer_grab_actor = NULL;
2298 clutter_ungrab_pointer ();
2303 * clutter_grab_pointer:
2304 * @actor: a #ClutterActor
2306 * Grabs pointer events, after the grab is done all pointer related events
2307 * (press, motion, release, enter, leave and scroll) are delivered to this
2308 * actor directly. The source set in the event will be the actor that would
2309 * have received the event if the pointer grab was not in effect.
2311 * If you wish to grab all the pointer events for a specific input device,
2312 * you should use clutter_grab_pointer_for_device().
2317 clutter_grab_pointer (ClutterActor *actor)
2319 ClutterMainContext *context;
2321 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2323 context = clutter_context_get_default ();
2325 if (context->pointer_grab_actor == actor)
2328 if (context->pointer_grab_actor)
2330 g_object_weak_unref (G_OBJECT (context->pointer_grab_actor),
2331 on_pointer_grab_weak_notify,
2333 context->pointer_grab_actor = NULL;
2338 context->pointer_grab_actor = actor;
2340 g_object_weak_ref (G_OBJECT (actor),
2341 on_pointer_grab_weak_notify,
2347 * clutter_grab_pointer_for_device:
2348 * @actor: a #ClutterActor
2349 * @id: a device id, or -1
2351 * Grabs all the pointer events coming from the device @id for @actor.
2353 * If @id is -1 then this function is equivalent to clutter_grab_pointer().
2358 clutter_grab_pointer_for_device (ClutterActor *actor,
2361 ClutterInputDevice *dev;
2363 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2365 /* essentially a global grab */
2368 clutter_grab_pointer (actor);
2372 dev = clutter_get_input_device_for_id (id);
2377 if (dev->pointer_grab_actor == actor)
2380 if (dev->pointer_grab_actor)
2382 g_object_weak_unref (G_OBJECT (dev->pointer_grab_actor),
2383 on_pointer_grab_weak_notify,
2385 dev->pointer_grab_actor = NULL;
2390 dev->pointer_grab_actor = actor;
2392 g_object_weak_ref (G_OBJECT (actor),
2393 on_pointer_grab_weak_notify,
2400 * clutter_ungrab_pointer:
2402 * Removes an existing grab of the pointer.
2407 clutter_ungrab_pointer (void)
2409 clutter_grab_pointer (NULL);
2413 * clutter_ungrab_pointer_for_device:
2416 * Removes an existing grab of the pointer events for device @id.
2421 clutter_ungrab_pointer_for_device (gint id)
2423 clutter_grab_pointer_for_device (NULL, id);
2428 * clutter_get_pointer_grab:
2430 * Queries the current pointer grab of clutter.
2432 * Return value: the actor currently holding the pointer grab, or NULL if there is no grab.
2437 clutter_get_pointer_grab (void)
2439 ClutterMainContext *context;
2440 context = clutter_context_get_default ();
2442 return context->pointer_grab_actor;
2447 on_keyboard_grab_weak_notify (gpointer data,
2448 GObject *where_the_object_was)
2450 ClutterMainContext *context;
2452 context = clutter_context_get_default ();
2453 context->keyboard_grab_actor = NULL;
2455 clutter_ungrab_keyboard ();
2459 * clutter_grab_keyboard:
2460 * @actor: a #ClutterActor
2462 * Grabs keyboard events, after the grab is done keyboard events ("key-press-event"
2463 * and "key-release-event") are delivered to this actor directly. The source
2464 * set in the event will be the actor that would have received the event if the
2465 * keyboard grab was not in effect.
2470 clutter_grab_keyboard (ClutterActor *actor)
2472 ClutterMainContext *context;
2474 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2476 context = clutter_context_get_default ();
2478 if (context->keyboard_grab_actor == actor)
2481 if (context->keyboard_grab_actor)
2483 g_object_weak_unref (G_OBJECT (context->keyboard_grab_actor),
2484 on_keyboard_grab_weak_notify,
2486 context->keyboard_grab_actor = NULL;
2491 context->keyboard_grab_actor = actor;
2493 g_object_weak_ref (G_OBJECT (actor),
2494 on_keyboard_grab_weak_notify,
2500 * clutter_ungrab_keyboard:
2502 * Removes an existing grab of the keyboard.
2507 clutter_ungrab_keyboard (void)
2509 clutter_grab_keyboard (NULL);
2513 * clutter_get_keyboard_grab:
2515 * Queries the current keyboard grab of clutter.
2517 * Return value: the actor currently holding the keyboard grab, or NULL if there is no grab.
2522 clutter_get_keyboard_grab (void)
2524 ClutterMainContext *context;
2525 context = clutter_context_get_default ();
2527 return context->keyboard_grab_actor;
2531 * clutter_get_motion_events_frequency:
2533 * Retrieves the number of motion events per second that are delivered
2536 * See clutter_set_motion_events_frequency().
2538 * Return value: the number of motion events per second
2543 clutter_get_motion_events_frequency (void)
2545 ClutterMainContext *context = clutter_context_get_default ();
2547 if (G_LIKELY (context->motion_frequency == 0))
2551 frequency = clutter_default_fps / 4;
2552 frequency = CLAMP (frequency, 20, 45);
2557 return context->motion_frequency;
2561 * clutter_set_motion_events_frequency:
2562 * @frequency: the number of motion events per second, or 0 for the
2565 * Sets the motion events frequency. Setting this to a non-zero value
2566 * will override the default setting, so it should be rarely used.
2568 * Motion events are delivered from the default backend to the stage
2569 * and are used to generate the enter/leave events pair. This might lead
2570 * to a performance penalty due to the way the actors are identified.
2571 * Using this function is possible to reduce the frequency of the motion
2572 * events delivery to the stage.
2577 clutter_set_motion_events_frequency (guint frequency)
2579 ClutterMainContext *context = clutter_context_get_default ();
2581 /* never allow the motion events to exceed the default frame rate */
2582 context->motion_frequency = CLAMP (frequency, 1, clutter_default_fps);
2586 * clutter_clear_glyph_cache:
2588 * Clears the internal cache of glyphs used by the Pango
2589 * renderer. This will free up some memory and GL texture
2590 * resources. The cache will be automatically refilled as more text is
2596 clutter_clear_glyph_cache (void)
2598 if (CLUTTER_CONTEXT ()->font_map)
2599 cogl_pango_font_map_clear_glyph_cache (CLUTTER_CONTEXT ()->font_map);
2603 * clutter_set_font_flags:
2604 * @flags: The new flags
2606 * Sets the font quality options for subsequent text rendering
2609 * Using mipmapped textures will improve the quality for scaled down
2610 * text but will use more texture memory.
2612 * Enabling hinting improves text quality for static text but may
2613 * introduce some artifacts if the text is animated. Changing the
2614 * hinting flag will only effect newly created PangoLayouts. So
2615 * #ClutterText actors will not show the change until a property which
2616 * causes it to recreate the layout is also changed.
2621 clutter_set_font_flags (ClutterFontFlags flags)
2623 ClutterFontFlags old_flags, changed_flags;
2625 if (CLUTTER_CONTEXT ()->font_map)
2626 cogl_pango_font_map_set_use_mipmapping (CLUTTER_CONTEXT ()->font_map,
2628 & CLUTTER_FONT_MIPMAPPING) != 0);
2630 old_flags = clutter_get_font_flags ();
2632 if (CLUTTER_CONTEXT ()->user_font_options == NULL)
2633 CLUTTER_CONTEXT ()->user_font_options = cairo_font_options_create ();
2635 /* Only set the font options that have actually changed so we don't
2636 override a detailed setting from the backend */
2637 changed_flags = old_flags ^ flags;
2639 if ((changed_flags & CLUTTER_FONT_HINTING))
2640 cairo_font_options_set_hint_style (CLUTTER_CONTEXT ()->user_font_options,
2641 (flags & CLUTTER_FONT_HINTING)
2642 ? CAIRO_HINT_STYLE_FULL
2643 : CAIRO_HINT_STYLE_NONE);
2645 if (CLUTTER_CONTEXT ()->pango_context)
2646 update_pango_context (CLUTTER_CONTEXT ()->backend,
2647 CLUTTER_CONTEXT ()->pango_context);
2651 * clutter_get_font_flags:
2653 * Gets the current font flags for rendering text. See
2654 * clutter_set_font_flags().
2656 * Return value: The font flags
2661 clutter_get_font_flags (void)
2663 ClutterMainContext *ctxt = CLUTTER_CONTEXT ();
2664 CoglPangoFontMap *font_map = NULL;
2665 cairo_font_options_t *font_options;
2666 ClutterFontFlags flags = 0;
2668 font_map = CLUTTER_CONTEXT ()->font_map;
2670 if (G_LIKELY (font_map)
2671 && cogl_pango_font_map_get_use_mipmapping (font_map))
2672 flags |= CLUTTER_FONT_MIPMAPPING;
2674 font_options = clutter_backend_get_font_options (ctxt->backend);
2675 font_options = cairo_font_options_copy (font_options);
2677 if (ctxt->user_font_options)
2678 cairo_font_options_merge (font_options, ctxt->user_font_options);
2680 if ((cairo_font_options_get_hint_style (font_options)
2681 != CAIRO_HINT_STYLE_DEFAULT)
2682 && (cairo_font_options_get_hint_style (font_options)
2683 != CAIRO_HINT_STYLE_NONE))
2684 flags |= CLUTTER_FONT_HINTING;
2686 cairo_font_options_destroy (font_options);
2692 * clutter_get_input_device_for_id:
2695 * Retrieves the #ClutterInputDevice from its id.
2697 * Return value: a #ClutterInputDevice, or %NULL
2701 ClutterInputDevice *
2702 clutter_get_input_device_for_id (gint id)
2705 ClutterInputDevice *device = NULL;
2706 ClutterMainContext *context;
2708 context = clutter_context_get_default ();
2710 for (item = context->input_devices;
2714 device = item->data;
2716 if (device->id == id)
2724 * clutter_get_font_map:
2726 * Retrieves the #PangoFontMap instance used by Clutter.
2727 * You can use the global font map object with the COGL
2730 * Return value: the #PangoFontMap instance. The returned
2731 * value is owned by Clutter and it should never be
2737 clutter_get_font_map (void)
2739 if (CLUTTER_CONTEXT ()->font_map)
2740 return PANGO_FONT_MAP (CLUTTER_CONTEXT ()->font_map);