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);
373 /* Disable dithering (if any) when doing the painting in pick mode */
374 dither_was_on = glIsEnabled (GL_DITHER);
375 glDisable (GL_DITHER);
377 /* Render the entire scence in pick mode - just single colored silhouette's
378 * are drawn offscreen (as we never swap buffers)
380 context->pick_mode = mode;
381 clutter_actor_paint (CLUTTER_ACTOR (stage));
382 context->pick_mode = CLUTTER_PICK_NONE;
384 /* Calls should work under both GL and GLES, note GLES needs RGBA */
385 glGetIntegerv(GL_VIEWPORT, viewport);
387 /* Below to be safe, particularly on GL ES. an EGL wait call or full
392 /* Read the color of the screen co-ords pixel */
393 glReadPixels (x, viewport[3] - y -1, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
395 /* Restore whether GL_DITHER was enabled */
397 glEnable (GL_DITHER);
399 if (pixel[0] == 0xff && pixel[1] == 0xff && pixel[2] == 0xff)
400 return CLUTTER_ACTOR (stage);
402 id = _clutter_pixel_to_id (pixel);
404 return clutter_get_actor_by_gid (id);
407 static PangoDirection
408 clutter_get_text_direction (void)
410 PangoDirection dir = PANGO_DIRECTION_LTR;
411 const gchar *direction;
413 direction = g_getenv ("CLUTTER_TEXT_DIRECTION");
414 if (direction && *direction != '\0')
416 if (strcmp (direction, "rtl") == 0)
417 dir = PANGO_DIRECTION_RTL;
418 else if (strcmp (direction, "ltr") == 0)
419 dir = PANGO_DIRECTION_LTR;
423 /* Translate to default:RTL if you want your widgets
424 * to be RTL, otherwise translate to default:LTR.
426 * Do *not* translate it to "predefinito:LTR": if it
427 * it isn't default:LTR or default:RTL it will not work
429 char *e = _("default:LTR");
431 if (strcmp (e, "default:RTL") == 0)
432 dir = PANGO_DIRECTION_RTL;
433 else if (strcmp (e, "default:LTR") == 0)
434 dir = PANGO_DIRECTION_LTR;
436 g_warning ("Whoever translated default:LTR did so wrongly.");
443 update_pango_context (ClutterBackend *backend,
444 PangoContext *context)
446 PangoFontDescription *font_desc;
447 cairo_font_options_t *font_options;
448 const gchar *font_name;
451 /* update the text direction */
452 pango_context_set_base_dir (context, clutter_text_direction);
454 /* get the configuration for the PangoContext from the backend */
455 font_name = clutter_backend_get_font_name (backend);
456 font_options = clutter_backend_get_font_options (backend);
457 resolution = clutter_backend_get_resolution (backend);
459 font_desc = pango_font_description_from_string (font_name);
462 resolution = 96.0; /* fall back */
464 pango_context_set_font_description (context, font_desc);
465 pango_cairo_context_set_font_options (context, font_options);
466 pango_cairo_context_set_resolution (context, resolution);
468 pango_font_description_free (font_desc);
472 _clutter_context_get_pango_context (ClutterMainContext *self)
474 if (G_UNLIKELY (self->pango_context == NULL))
476 PangoContext *context;
478 context = cogl_pango_font_map_create_context (self->font_map);
479 self->pango_context = context;
481 g_signal_connect (self->backend, "resolution-changed",
482 G_CALLBACK (update_pango_context),
483 self->pango_context);
484 g_signal_connect (self->backend, "font-changed",
485 G_CALLBACK (update_pango_context),
486 self->pango_context);
489 update_pango_context (self->backend, self->pango_context);
491 return self->pango_context;
495 _clutter_context_create_pango_context (ClutterMainContext *self)
497 PangoContext *context;
499 context = cogl_pango_font_map_create_context (self->font_map);
500 update_pango_context (self->backend, context);
508 * Terminates the Clutter mainloop.
511 clutter_main_quit (void)
513 g_return_if_fail (main_loops != NULL);
515 g_main_loop_quit (main_loops->data);
519 * clutter_main_level:
521 * Retrieves the depth of the Clutter mainloop.
523 * Return value: The level of the mainloop.
526 clutter_main_level (void)
528 return clutter_main_loop_level;
534 * Starts the Clutter mainloop.
541 /* Make sure there is a context */
544 if (!clutter_is_initialized)
546 g_warning ("Called clutter_main() but Clutter wasn't initialised. "
547 "You must call clutter_init() first.");
553 clutter_main_loop_level++;
555 loop = g_main_loop_new (NULL, TRUE);
556 main_loops = g_slist_prepend (main_loops, loop);
558 #ifdef HAVE_CLUTTER_FRUITY
559 /* clutter fruity creates an application that forwards events and manually
562 clutter_fruity_main ();
564 if (g_main_loop_is_running (main_loops->data))
566 clutter_threads_leave ();
567 g_main_loop_run (loop);
568 clutter_threads_enter ();
572 main_loops = g_slist_remove (main_loops, loop);
574 g_main_loop_unref (loop);
576 clutter_main_loop_level--;
582 clutter_threads_impl_lock (void)
584 if (clutter_threads_mutex)
585 g_mutex_lock (clutter_threads_mutex);
589 clutter_threads_impl_unlock (void)
591 if (clutter_threads_mutex)
592 g_mutex_unlock (clutter_threads_mutex);
596 * clutter_threads_init:
598 * Initialises the Clutter threading mechanism, so that Clutter API can be
599 * called by multiple threads, using clutter_threads_enter() and
600 * clutter_threads_leave() to mark the critical sections.
602 * You must call g_thread_init() before this function.
604 * This function must be called before clutter_init().
609 clutter_threads_init (void)
611 if (!g_thread_supported ())
612 g_error ("g_thread_init() must be called before clutter_threads_init()");
614 clutter_threads_mutex = g_mutex_new ();
616 if (!clutter_threads_lock)
617 clutter_threads_lock = clutter_threads_impl_lock;
619 if (!clutter_threads_unlock)
620 clutter_threads_unlock = clutter_threads_impl_unlock;
624 * clutter_threads_set_lock_functions:
625 * @enter_fn: function called when aquiring the Clutter main lock
626 * @leave_fn: function called when releasing the Clutter main lock
628 * Allows the application to replace the standard method that
629 * Clutter uses to protect its data structures. Normally, Clutter
630 * creates a single #GMutex that is locked by clutter_threads_enter(),
631 * and released by clutter_threads_leave(); using this function an
632 * application provides, instead, a function @enter_fn that is
633 * called by clutter_threads_enter() and a function @leave_fn that is
634 * called by clutter_threads_leave().
636 * The functions must provide at least same locking functionality
637 * as the default implementation, but can also do extra application
638 * specific processing.
640 * As an example, consider an application that has its own recursive
641 * lock that when held, holds the Clutter lock as well. When Clutter
642 * unlocks the Clutter lock when entering a recursive main loop, the
643 * application must temporarily release its lock as well.
645 * Most threaded Clutter apps won't need to use this method.
647 * This method must be called before clutter_threads_init(), and cannot
648 * be called multiple times.
653 clutter_threads_set_lock_functions (GCallback enter_fn,
656 g_return_if_fail (clutter_threads_lock == NULL &&
657 clutter_threads_unlock == NULL);
659 clutter_threads_lock = enter_fn;
660 clutter_threads_unlock = leave_fn;
667 GDestroyNotify notify;
668 } ClutterThreadsDispatch;
671 clutter_threads_dispatch (gpointer data)
673 ClutterThreadsDispatch *dispatch = data;
674 gboolean ret = FALSE;
676 clutter_threads_enter ();
678 if (!g_source_is_destroyed (g_main_current_source ()))
679 ret = dispatch->func (dispatch->data);
681 clutter_threads_leave ();
687 clutter_threads_dispatch_free (gpointer data)
689 ClutterThreadsDispatch *dispatch = data;
691 /* XXX - we cannot hold the thread lock here because the main loop
692 * might destroy a source while still in the dispatcher function; so
693 * knowing whether the lock is being held or not is not known a priori.
695 * see bug: http://bugzilla.gnome.org/show_bug.cgi?id=459555
697 if (dispatch->notify)
698 dispatch->notify (dispatch->data);
700 g_slice_free (ClutterThreadsDispatch, dispatch);
704 * clutter_threads_add_idle_full:
705 * @priority: the priority of the timeout source. Typically this will be in the
706 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE
707 * @func: function to call
708 * @data: data to pass to the function
709 * @notify: functio to call when the idle source is removed
711 * Adds a function to be called whenever there are no higher priority
712 * events pending. If the function returns %FALSE it is automatically
713 * removed from the list of event sources and will not be called again.
715 * This function can be considered a thread-safe variant of g_idle_add_full():
716 * it will call @function while holding the Clutter lock. It is logically
717 * equivalent to the following implementation:
721 * idle_safe_callback (gpointer data)
723 * SafeClosure *closure = data;
724 * gboolean res = FALSE;
726 * /* mark the critical section */
728 * clutter_threads_enter();
730 * /* the callback does not need to acquire the Clutter
731 * * lock itself, as it is held by the this proxy handler
733 * res = closure->callback (closure->data);
735 * clutter_threads_leave();
740 * add_safe_idle (GSourceFunc callback,
743 * SafeClosure *closure = g_new0 (SafeClosure, 1);
745 * closure->callback = callback;
746 * closure->data = data;
748 * return g_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
749 * idle_safe_callback,
755 * This function should be used by threaded applications to make sure
756 * that @func is emitted under the Clutter threads lock and invoked
757 * from the same thread that started the Clutter main loop. For instance,
758 * it can be used to update the UI using the results from a worker
763 * update_ui (gpointer data)
765 * SomeClosure *closure = data;
767 * /* it is safe to call Clutter API from this function because
768 * * it is invoked from the same thread that started the main
769 * * loop and under the Clutter thread lock
771 * clutter_label_set_text (CLUTTER_LABEL (closure->label),
774 * g_object_unref (closure->label);
780 * /* within another thread */
781 * closure = g_new0 (SomeClosure, 1);
782 * /* always take a reference on GObject instances */
783 * closure->label = g_object_ref (my_application->label);
784 * closure->text = g_strdup (processed_text_to_update_the_label);
786 * clutter_threads_add_idle_full (G_PRIORITY_HIGH_IDLE,
792 * Return value: the ID (greater than 0) of the event source.
797 clutter_threads_add_idle_full (gint priority,
800 GDestroyNotify notify)
802 ClutterThreadsDispatch *dispatch;
804 g_return_val_if_fail (func != NULL, 0);
806 dispatch = g_slice_new (ClutterThreadsDispatch);
807 dispatch->func = func;
808 dispatch->data = data;
809 dispatch->notify = notify;
811 return g_idle_add_full (priority,
812 clutter_threads_dispatch, dispatch,
813 clutter_threads_dispatch_free);
817 * clutter_threads_add_idle:
818 * @func: function to call
819 * @data: data to pass to the function
821 * Simple wrapper around clutter_threads_add_idle_full() using the
824 * Return value: the ID (greater than 0) of the event source.
829 clutter_threads_add_idle (GSourceFunc func,
832 g_return_val_if_fail (func != NULL, 0);
834 return clutter_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
840 * clutter_threads_add_timeout_full:
841 * @priority: the priority of the timeout source. Typically this will be in the
842 * range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
843 * @interval: the time between calls to the function, in milliseconds
844 * @func: function to call
845 * @data: data to pass to the function
846 * @notify: function to call when the timeout source is removed
848 * Sets a function to be called at regular intervals holding the Clutter
849 * threads lock, with the given priority. The function is called repeatedly
850 * until it returns %FALSE, at which point the timeout is automatically
851 * removed and the function will not be called again. The @notify function
852 * is called when the timeout is removed.
854 * The first call to the function will be at the end of the first @interval.
856 * It is important to note that, due to how the Clutter main loop is
857 * implemented, the timing will not be accurate and it will not try to
858 * "keep up" with the interval. A more reliable source is available
859 * using clutter_threads_add_frame_source_full(), which is also internally
860 * used by #ClutterTimeline.
862 * See also clutter_threads_add_idle_full().
864 * Return value: the ID (greater than 0) of the event source.
869 clutter_threads_add_timeout_full (gint priority,
873 GDestroyNotify notify)
875 ClutterThreadsDispatch *dispatch;
877 g_return_val_if_fail (func != NULL, 0);
879 dispatch = g_slice_new (ClutterThreadsDispatch);
880 dispatch->func = func;
881 dispatch->data = data;
882 dispatch->notify = notify;
884 return g_timeout_add_full (priority,
886 clutter_threads_dispatch, dispatch,
887 clutter_threads_dispatch_free);
891 * clutter_threads_add_timeout:
892 * @interval: the time between calls to the function, in milliseconds
893 * @func: function to call
894 * @data: data to pass to the function
896 * Simple wrapper around clutter_threads_add_timeout_full().
898 * Return value: the ID (greater than 0) of the event source.
903 clutter_threads_add_timeout (guint interval,
907 g_return_val_if_fail (func != NULL, 0);
909 return clutter_threads_add_timeout_full (G_PRIORITY_DEFAULT,
916 * clutter_threads_add_frame_source_full:
917 * @priority: the priority of the frame source. Typically this will be in the
918 * range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
919 * @interval: the time between calls to the function, in milliseconds
920 * @func: function to call
921 * @data: data to pass to the function
922 * @notify: function to call when the timeout source is removed
924 * Sets a function to be called at regular intervals holding the Clutter
925 * threads lock, with the given priority. The function is called repeatedly
926 * until it returns %FALSE, at which point the timeout is automatically
927 * removed and the function will not be called again. The @notify function
928 * is called when the timeout is removed.
930 * This function is similar to clutter_threads_add_timeout_full()
931 * except that it will try to compensate for delays. For example, if
932 * @func takes half the interval time to execute then the function
933 * will be called again half the interval time after it finished. In
934 * contrast clutter_threads_add_timeout_full() would not fire until a
935 * full interval after the function completes so the delay between
936 * calls would be @interval * 1.5. This function does not however try
937 * to invoke the function multiple times to catch up missing frames if
938 * @func takes more than @interval ms to execute.
940 * See also clutter_threads_add_idle_full().
942 * Return value: the ID (greater than 0) of the event source.
947 clutter_threads_add_frame_source_full (gint priority,
951 GDestroyNotify notify)
953 ClutterThreadsDispatch *dispatch;
955 g_return_val_if_fail (func != NULL, 0);
957 dispatch = g_slice_new (ClutterThreadsDispatch);
958 dispatch->func = func;
959 dispatch->data = data;
960 dispatch->notify = notify;
962 return clutter_frame_source_add_full (priority,
964 clutter_threads_dispatch, dispatch,
965 clutter_threads_dispatch_free);
969 * clutter_threads_add_frame_source:
970 * @interval: the time between calls to the function, in milliseconds
971 * @func: function to call
972 * @data: data to pass to the function
974 * Simple wrapper around clutter_threads_add_frame_source_full().
976 * Return value: the ID (greater than 0) of the event source.
981 clutter_threads_add_frame_source (guint interval,
985 g_return_val_if_fail (func != NULL, 0);
987 return clutter_threads_add_frame_source_full (G_PRIORITY_DEFAULT,
994 * clutter_threads_enter:
996 * Locks the Clutter thread lock.
1001 clutter_threads_enter (void)
1003 if (clutter_threads_lock)
1004 (* clutter_threads_lock) ();
1008 * clutter_threads_leave:
1010 * Unlocks the Clutter thread lock.
1015 clutter_threads_leave (void)
1017 if (clutter_threads_unlock)
1018 (* clutter_threads_unlock) ();
1023 * clutter_get_debug_enabled:
1025 * Check if clutter has debugging turned on.
1027 * Return value: TRUE if debugging is turned on, FALSE otherwise.
1030 clutter_get_debug_enabled (void)
1032 #ifdef CLUTTER_ENABLE_DEBUG
1033 return clutter_debug_flags != 0;
1039 ClutterMainContext *
1040 clutter_context_get_default (void)
1042 if (G_UNLIKELY(!ClutterCntx))
1044 ClutterMainContext *ctx;
1046 ClutterCntx = ctx = g_new0 (ClutterMainContext, 1);
1047 ctx->backend = g_object_new (_clutter_backend_impl_get_type (), NULL);
1049 ctx->is_initialized = FALSE;
1050 ctx->motion_events_per_actor = TRUE;
1052 #ifdef CLUTTER_ENABLE_DEBUG
1053 ctx->timer = g_timer_new ();
1054 g_timer_start (ctx->timer);
1062 * clutter_get_timestamp:
1064 * Returns the approximate number of microseconds passed since clutter was
1067 * Return value: Number of microseconds since clutter_init() was called.
1070 clutter_get_timestamp (void)
1072 #ifdef CLUTTER_ENABLE_DEBUG
1073 ClutterMainContext *ctx;
1076 ctx = clutter_context_get_default ();
1078 /* FIXME: may need a custom timer for embedded setups */
1079 seconds = g_timer_elapsed (ctx->timer, NULL);
1081 return (gulong)(seconds / 1.0e-6);
1088 clutter_arg_direction_cb (const char *key,
1092 clutter_text_direction =
1093 (strcmp (value, "rtl") == 0) ? PANGO_DIRECTION_RTL
1094 : PANGO_DIRECTION_LTR;
1099 #ifdef CLUTTER_ENABLE_DEBUG
1101 clutter_arg_debug_cb (const char *key,
1105 clutter_debug_flags |=
1106 g_parse_debug_string (value,
1108 G_N_ELEMENTS (clutter_debug_keys));
1113 clutter_arg_no_debug_cb (const char *key,
1117 clutter_debug_flags &=
1118 ~g_parse_debug_string (value,
1120 G_N_ELEMENTS (clutter_debug_keys));
1123 #endif /* CLUTTER_ENABLE_DEBUG */
1126 clutter_init_error_quark (void)
1128 return g_quark_from_static_string ("clutter-init-error-quark");
1131 static ClutterInitError
1132 clutter_init_real (GError **error)
1134 ClutterMainContext *ctx;
1135 ClutterActor *stage;
1137 ClutterBackend *backend;
1139 /* Note, creates backend if not already existing, though parse args will
1140 * have likely created it
1142 ctx = clutter_context_get_default ();
1143 backend = ctx->backend;
1145 if (!ctx->options_parsed)
1147 g_set_error (error, CLUTTER_INIT_ERROR,
1148 CLUTTER_INIT_ERROR_INTERNAL,
1149 "When using clutter_get_option_group_without_init() "
1150 "you must parse options before calling clutter_init()");
1152 return CLUTTER_INIT_ERROR_INTERNAL;
1156 * Call backend post parse hooks.
1158 if (!_clutter_backend_post_parse (backend, error))
1159 return CLUTTER_INIT_ERROR_BACKEND;
1161 /* Stage will give us a GL Context etc */
1162 stage = clutter_stage_get_default ();
1166 g_set_error (error, CLUTTER_INIT_ERROR,
1167 CLUTTER_INIT_ERROR_INTERNAL,
1168 "Unable to create the default stage");
1170 g_critical ("Unable to create the default stage");
1172 return CLUTTER_INIT_ERROR_INTERNAL;
1175 clutter_stage_set_title (CLUTTER_STAGE (stage), g_get_prgname ());
1177 clutter_actor_realize (stage);
1179 if (!CLUTTER_ACTOR_IS_REALIZED (stage))
1182 g_set_error (error, CLUTTER_INIT_ERROR,
1183 CLUTTER_INIT_ERROR_INTERNAL,
1184 "Unable to realize the default stage");
1186 g_critical ("Unable to realize the default stage");
1188 return CLUTTER_INIT_ERROR_INTERNAL;
1191 /* Now we can safely assume we have a valid GL context and can
1192 * start issueing cogl commands
1196 * Resolution requires display to be open, so can only be queried after
1197 * the post_parse hooks run.
1199 * NB: cogl_pango requires a Cogl context.
1201 ctx->font_map = COGL_PANGO_FONT_MAP (cogl_pango_font_map_new ());
1203 resolution = clutter_backend_get_resolution (ctx->backend);
1204 cogl_pango_font_map_set_resolution (ctx->font_map, resolution);
1205 cogl_pango_font_map_set_use_mipmapping (ctx->font_map, TRUE);
1207 clutter_text_direction = clutter_get_text_direction ();
1210 /* Figure out framebuffer masks used for pick */
1211 cogl_get_bitmasks (&ctx->fb_r_mask, &ctx->fb_g_mask, &ctx->fb_b_mask, NULL);
1213 ctx->fb_r_mask_used = ctx->fb_r_mask;
1214 ctx->fb_g_mask_used = ctx->fb_g_mask;
1215 ctx->fb_b_mask_used = ctx->fb_b_mask;
1217 #ifndef HAVE_CLUTTER_FRUITY
1218 /* We always do fuzzy picking for the fruity backend */
1219 if (g_getenv ("CLUTTER_FUZZY_PICK") != NULL)
1222 ctx->fb_r_mask_used--;
1223 ctx->fb_g_mask_used--;
1224 ctx->fb_b_mask_used--;
1227 /* Initiate event collection */
1228 _clutter_backend_init_events (ctx->backend);
1230 /* finally features - will call to backend and cogl */
1231 _clutter_feature_init ();
1233 clutter_is_initialized = TRUE;
1234 ctx->is_initialized = TRUE;
1236 return CLUTTER_INIT_SUCCESS;
1239 static GOptionEntry clutter_args[] = {
1240 { "clutter-show-fps", 0, 0, G_OPTION_ARG_NONE, &clutter_show_fps,
1241 N_("Show frames per second"), NULL },
1242 { "clutter-default-fps", 0, 0, G_OPTION_ARG_INT, &clutter_default_fps,
1243 N_("Default frame rate"), "FPS" },
1244 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &clutter_fatal_warnings,
1245 N_("Make all warnings fatal"), NULL },
1246 { "clutter-text-direction", 0, 0, G_OPTION_ARG_CALLBACK,
1247 clutter_arg_direction_cb,
1248 N_("Direction for the text"), "DIRECTION" },
1249 #ifdef CLUTTER_ENABLE_DEBUG
1250 { "clutter-debug", 0, 0, G_OPTION_ARG_CALLBACK, clutter_arg_debug_cb,
1251 N_("Clutter debugging flags to set"), "FLAGS" },
1252 { "clutter-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, clutter_arg_no_debug_cb,
1253 N_("Clutter debugging flags to unset"), "FLAGS" },
1254 #endif /* CLUTTER_ENABLE_DEBUG */
1258 /* pre_parse_hook: initialise variables depending on environment
1259 * variables; these variables might be overridden by the command
1260 * line arguments that are going to be parsed after.
1263 pre_parse_hook (GOptionContext *context,
1264 GOptionGroup *group,
1268 ClutterMainContext *clutter_context;
1269 ClutterBackend *backend;
1270 const char *env_string;
1272 if (clutter_is_initialized)
1275 if (setlocale (LC_ALL, "") == NULL)
1276 g_warning ("Locale not supported by C library.\n"
1277 "Using the fallback 'C' locale.");
1279 clutter_context = clutter_context_get_default ();
1281 clutter_context->id_pool = clutter_id_pool_new (256);
1283 backend = clutter_context->backend;
1284 g_assert (CLUTTER_IS_BACKEND (backend));
1286 #ifdef CLUTTER_ENABLE_DEBUG
1287 env_string = g_getenv ("CLUTTER_DEBUG");
1288 if (env_string != NULL)
1290 clutter_debug_flags =
1291 g_parse_debug_string (env_string,
1293 G_N_ELEMENTS (clutter_debug_keys));
1296 #endif /* CLUTTER_ENABLE_DEBUG */
1298 env_string = g_getenv ("CLUTTER_SHOW_FPS");
1300 clutter_show_fps = TRUE;
1302 env_string = g_getenv ("CLUTTER_DEFAULT_FPS");
1305 gint default_fps = g_ascii_strtoll (env_string, NULL, 10);
1307 clutter_default_fps = CLAMP (default_fps, 1, 1000);
1310 return _clutter_backend_pre_parse (backend, error);
1313 /* post_parse_hook: initialise the context and data structures
1314 * and opens the X display
1317 post_parse_hook (GOptionContext *context,
1318 GOptionGroup *group,
1322 ClutterMainContext *clutter_context;
1323 ClutterBackend *backend;
1325 if (clutter_is_initialized)
1328 clutter_context = clutter_context_get_default ();
1329 backend = clutter_context->backend;
1330 g_assert (CLUTTER_IS_BACKEND (backend));
1332 if (clutter_fatal_warnings)
1334 GLogLevelFlags fatal_mask;
1336 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
1337 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
1338 g_log_set_always_fatal (fatal_mask);
1341 clutter_context->frame_rate = clutter_default_fps;
1342 clutter_context->options_parsed = TRUE;
1345 * If not asked to defer display setup, call clutter_init_real(),
1346 * which in turn calls the backend post parse hooks.
1348 if (!clutter_context->defer_display_setup)
1349 return clutter_init_real (error);
1355 * clutter_get_option_group:
1357 * Returns a #GOptionGroup for the command line arguments recognized
1358 * by Clutter. You should add this group to your #GOptionContext with
1359 * g_option_context_add_group(), if you are using g_option_context_parse()
1360 * to parse your commandline arguments.
1362 * Calling g_option_context_parse() with Clutter's #GOptionGroup will result
1363 * in Clutter's initialization. That is, the following code:
1366 * g_option_context_set_main_group (context, clutter_get_option_group ());
1367 * res = g_option_context_parse (context, &argc, &argc, NULL);
1370 * is functionally equivalent to:
1373 * clutter_init (&argc, &argv);
1376 * After g_option_context_parse() on a #GOptionContext containing the
1377 * Clutter #GOptionGroup has returned %TRUE, Clutter is guaranteed to be
1380 * Return value: a #GOptionGroup for the commandline arguments
1381 * recognized by Clutter
1386 clutter_get_option_group (void)
1388 ClutterMainContext *context;
1389 GOptionGroup *group;
1391 clutter_base_init ();
1393 context = clutter_context_get_default ();
1395 group = g_option_group_new ("clutter",
1396 _("Clutter Options"),
1397 _("Show Clutter Options"),
1401 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
1402 g_option_group_add_entries (group, clutter_args);
1403 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
1405 /* add backend-specific options */
1406 _clutter_backend_add_options (context->backend, group);
1412 * clutter_get_option_group_without_init:
1414 * Returns a #GOptionGroup for the command line arguments recognized
1415 * by Clutter. You should add this group to your #GOptionContext with
1416 * g_option_context_add_group(), if you are using g_option_context_parse()
1417 * to parse your commandline arguments. Unlike clutter_get_option_group(),
1418 * calling g_option_context_parse() with the #GOptionGroup returned by this
1419 * function requires a subsequent explicit call to clutter_init(); use this
1420 * function when needing to set foreign display connection with
1421 * clutter_x11_set_display(), or with gtk_clutter_init().
1423 * Return value: a #GOptionGroup for the commandline arguments
1424 * recognized by Clutter
1429 clutter_get_option_group_without_init (void)
1431 ClutterMainContext *context;
1432 GOptionGroup *group;
1434 clutter_base_init ();
1436 context = clutter_context_get_default ();
1437 context->defer_display_setup = TRUE;
1439 group = clutter_get_option_group ();
1445 * clutter_init_with_args:
1446 * @argc: a pointer to the number of command line arguments
1447 * @argv: a pointer to the array of command line arguments
1448 * @parameter_string: a string which is displayed in the
1449 * first line of <option>--help</option> output, after
1450 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
1451 * @entries: a %NULL terminated array of #GOptionEntry<!-- -->s
1452 * describing the options of your program
1453 * @translation_domain: a translation domain to use for translating
1454 * the <option>--help</option> output for the options in @entries
1455 * with gettext(), or %NULL
1456 * @error: a return location for a #GError
1458 * This function does the same work as clutter_init(). Additionally,
1459 * it allows you to add your own command line options, and it
1460 * automatically generates nicely formatted <option>--help</option>
1461 * output. Note that your program will be terminated after writing
1462 * out the help output. Also note that, in case of error, the
1463 * error message will be placed inside @error instead of being
1464 * printed on the display.
1466 * Return value: %CLUTTER_INIT_SUCCESS if Clutter has been successfully
1467 * initialised, or other values or #ClutterInitError in case of
1473 clutter_init_with_args (int *argc,
1475 const char *parameter_string,
1476 GOptionEntry *entries,
1477 const char *translation_domain,
1480 GOptionContext *context;
1481 GOptionGroup *group;
1483 ClutterMainContext *ctx;
1485 if (clutter_is_initialized)
1486 return CLUTTER_INIT_SUCCESS;
1488 clutter_base_init ();
1490 ctx = clutter_context_get_default ();
1492 if (!ctx->defer_display_setup)
1494 if (argc && *argc > 0 && *argv)
1495 g_set_prgname ((*argv)[0]);
1497 group = clutter_get_option_group ();
1498 context = g_option_context_new (parameter_string);
1500 g_option_context_add_group (context, group);
1503 g_option_context_add_main_entries (context, entries, translation_domain);
1505 res = g_option_context_parse (context, argc, argv, error);
1506 g_option_context_free (context);
1508 /* if res is FALSE, the error is filled for
1509 * us by g_option_context_parse()
1513 /* if there has been an error in the initialization, the
1514 * error id will be preserved inside the GError code
1516 if (error && *error)
1517 return (*error)->code;
1519 return CLUTTER_INIT_ERROR_INTERNAL;
1522 return CLUTTER_INIT_SUCCESS;
1525 return clutter_init_real (error);
1529 clutter_parse_args (int *argc,
1532 GOptionContext *option_context;
1533 GOptionGroup *clutter_group;
1534 GError *error = NULL;
1535 gboolean ret = TRUE;
1537 if (clutter_is_initialized)
1540 option_context = g_option_context_new (NULL);
1541 g_option_context_set_ignore_unknown_options (option_context, TRUE);
1542 g_option_context_set_help_enabled (option_context, FALSE);
1544 /* Initiate any command line options from the backend */
1546 clutter_group = clutter_get_option_group ();
1547 g_option_context_set_main_group (option_context, clutter_group);
1549 if (!g_option_context_parse (option_context, argc, argv, &error))
1553 g_warning ("%s", error->message);
1554 g_error_free (error);
1560 g_option_context_free (option_context);
1567 * @argc: The number of arguments in @argv
1568 * @argv: A pointer to an array of arguments.
1570 * It will initialise everything needed to operate with Clutter and
1571 * parses some standard command line options. @argc and @argv are
1572 * adjusted accordingly so your own code will never see those standard
1575 * Return value: 1 on success, < 0 on failure.
1578 clutter_init (int *argc,
1581 ClutterMainContext *ctx;
1582 GError *error = NULL;
1584 if (clutter_is_initialized)
1585 return CLUTTER_INIT_SUCCESS;
1587 clutter_base_init ();
1589 ctx = clutter_context_get_default ();
1591 if (!ctx->defer_display_setup)
1593 if (argc && *argc > 0 && *argv)
1594 g_set_prgname ((*argv)[0]);
1596 /* parse_args will trigger backend creation and things like
1597 * DISPLAY connection etc.
1599 if (clutter_parse_args (argc, argv) == FALSE)
1601 CLUTTER_NOTE (MISC, "failed to parse arguments.");
1602 return CLUTTER_INIT_ERROR_INTERNAL;
1605 return CLUTTER_INIT_SUCCESS;
1608 return clutter_init_real (&error);
1612 _clutter_boolean_handled_accumulator (GSignalInvocationHint *ihint,
1613 GValue *return_accu,
1614 const GValue *handler_return,
1617 gboolean continue_emission;
1618 gboolean signal_handled;
1620 signal_handled = g_value_get_boolean (handler_return);
1621 g_value_set_boolean (return_accu, signal_handled);
1622 continue_emission = !signal_handled;
1624 return continue_emission;
1628 event_click_count_generate (ClutterEvent *event)
1630 /* multiple button click detection */
1631 static gint click_count = 0;
1632 static gint previous_x = -1;
1633 static gint previous_y = -1;
1634 static guint32 previous_time = 0;
1635 static gint previous_button_number = -1;
1637 ClutterBackend *backend;
1638 guint double_click_time;
1639 guint double_click_distance;
1641 backend = clutter_context_get_default ()->backend;
1642 double_click_distance = clutter_backend_get_double_click_distance (backend);
1643 double_click_time = clutter_backend_get_double_click_time (backend);
1645 if (event->button.device != NULL)
1647 click_count = event->button.device->click_count;
1648 previous_x = event->button.device->previous_x;
1649 previous_y = event->button.device->previous_y;
1650 previous_time = event->button.device->previous_time;
1651 previous_button_number = event->button.device->previous_button_number;
1654 switch (event->type)
1656 case CLUTTER_BUTTON_PRESS:
1657 case CLUTTER_SCROLL:
1658 /* check if we are in time and within distance to increment an
1659 * existing click count
1661 if (event->button.time < previous_time + double_click_time &&
1662 (ABS (event->button.x - previous_x) <= double_click_distance) &&
1663 (ABS (event->button.y - previous_y) <= double_click_distance)
1664 && event->button.button == previous_button_number)
1668 else /* start a new click count*/
1671 previous_button_number = event->button.button;
1674 /* store time and position for this click for comparison with
1677 previous_time = event->button.time;
1678 previous_x = event->button.x;
1679 previous_y = event->button.y;
1682 case CLUTTER_BUTTON_RELEASE:
1683 event->button.click_count=click_count;
1689 if (event->button.device != NULL)
1691 event->button.device->click_count = click_count;
1692 event->button.device->previous_x = previous_x;
1693 event->button.device->previous_y = previous_y;
1694 event->button.device->previous_time = previous_time;
1695 event->button.device->previous_button_number = previous_button_number;
1701 emit_event (ClutterEvent *event,
1702 gboolean is_key_event)
1704 #define MAX_EVENT_DEPTH 512
1706 static ClutterActor **event_tree = NULL;
1707 static gboolean lock = FALSE;
1709 ClutterActor *actor;
1710 gint i = 0, n_tree_events = 0;
1712 if (!event->any.source)
1714 g_warning ("No event source set, discarding event");
1718 /* reentrancy check */
1724 /* Sorry Mr Bassi. */
1725 if (G_UNLIKELY (event_tree == NULL))
1726 event_tree = g_new0 (ClutterActor *, MAX_EVENT_DEPTH);
1728 actor = event->any.source;
1730 /* Build 'tree' of emitters for the event */
1731 while (actor && n_tree_events < MAX_EVENT_DEPTH)
1733 ClutterActor *parent;
1735 parent = clutter_actor_get_parent (actor);
1737 if (clutter_actor_get_reactive (actor) ||
1738 parent == NULL || /* stage gets all events */
1739 is_key_event) /* keyboard events are always emitted */
1741 event_tree[n_tree_events++] = g_object_ref (actor);
1748 for (i = n_tree_events-1; i >= 0; i--)
1749 if (clutter_actor_event (event_tree[i], event, TRUE))
1753 for (i = 0; i < n_tree_events; i++)
1754 if (clutter_actor_event (event_tree[i], event, FALSE))
1759 for (i = 0; i < n_tree_events; i++)
1760 g_object_unref (event_tree[i]);
1764 #undef MAX_EVENT_DEPTH
1768 * Emits a pointer event after having prepared the event for delivery (setting
1769 * source, computing click_count, generating enter/leave etc.).
1773 emit_pointer_event (ClutterEvent *event,
1774 ClutterInputDevice *device)
1776 /* Using the global variable directly, since it has to be initialized
1779 ClutterMainContext *context = ClutterCntx;
1781 if (G_UNLIKELY (context->pointer_grab_actor != NULL &&
1785 clutter_actor_event (context->pointer_grab_actor, event, FALSE);
1787 else if (G_UNLIKELY (device != NULL &&
1788 device->pointer_grab_actor != NULL))
1790 /* per device grab */
1791 clutter_actor_event (device->pointer_grab_actor, event, FALSE);
1795 /* no grab, time to capture and bubble */
1796 emit_event (event, FALSE);
1801 emit_keyboard_event (ClutterEvent *event)
1803 ClutterMainContext *context = ClutterCntx;
1805 if (G_UNLIKELY (context->keyboard_grab_actor != NULL))
1806 clutter_actor_event (context->keyboard_grab_actor, event, FALSE);
1808 emit_event (event, TRUE);
1812 unset_motion_last_actor (ClutterActor *actor, ClutterInputDevice *dev)
1814 ClutterMainContext *context = ClutterCntx;
1817 context->motion_last_actor = NULL;
1819 dev->motion_last_actor = NULL;
1822 static ClutterInputDevice * clutter_event_get_device (ClutterEvent *event);
1824 /* This function should perhaps be public and in clutter-event.c ?
1826 static ClutterInputDevice *
1827 clutter_event_get_device (ClutterEvent *event)
1829 g_return_val_if_fail (event != NULL, NULL);
1831 switch (event->type)
1833 case CLUTTER_NOTHING:
1834 case CLUTTER_STAGE_STATE:
1835 case CLUTTER_DESTROY_NOTIFY:
1836 case CLUTTER_CLIENT_MESSAGE:
1837 case CLUTTER_DELETE:
1842 case CLUTTER_BUTTON_PRESS:
1843 case CLUTTER_BUTTON_RELEASE:
1844 return event->button.device;
1845 case CLUTTER_MOTION:
1846 return event->motion.device;
1847 case CLUTTER_SCROLL:
1848 return event->scroll.device;
1850 case CLUTTER_KEY_PRESS:
1851 case CLUTTER_KEY_RELEASE:
1858 set_motion_last_actor (ClutterActor *motion_current_actor,
1859 ClutterInputDevice *device)
1861 ClutterMainContext *context = ClutterCntx;
1862 ClutterActor *last_actor = context->motion_last_actor;
1865 last_actor = device->motion_last_actor;
1867 if (last_actor && last_actor != motion_current_actor)
1869 g_signal_handlers_disconnect_by_func
1871 G_CALLBACK (unset_motion_last_actor),
1875 if (motion_current_actor && last_actor != motion_current_actor)
1877 g_signal_connect (motion_current_actor, "destroy",
1878 G_CALLBACK (unset_motion_last_actor),
1883 device->motion_last_actor = motion_current_actor;
1885 context->motion_last_actor = motion_current_actor;
1889 generate_enter_leave_events (ClutterEvent *event)
1891 ClutterMainContext *context = ClutterCntx;
1892 ClutterActor *motion_current_actor = event->motion.source;
1893 ClutterActor *last_actor = context->motion_last_actor;
1894 ClutterInputDevice *device = clutter_event_get_device (event);
1897 last_actor = device->motion_last_actor;
1899 if (last_actor != motion_current_actor)
1901 if (motion_current_actor)
1906 cev.crossing.device = device;
1907 clutter_event_get_coords (event, &x, &y);
1909 if (context->motion_last_actor)
1911 cev.crossing.type = CLUTTER_LEAVE;
1912 cev.crossing.time = event->any.time;
1913 cev.crossing.flags = 0;
1916 cev.crossing.source = last_actor;
1917 cev.crossing.stage = event->any.stage;
1918 cev.crossing.related = motion_current_actor;
1920 emit_pointer_event (&cev, device);
1923 cev.crossing.type = CLUTTER_ENTER;
1924 cev.crossing.time = event->any.time;
1925 cev.crossing.flags = 0;
1928 cev.crossing.source = motion_current_actor;
1929 cev.crossing.stage = event->any.stage;
1931 if (context->motion_last_actor)
1932 cev.crossing.related = last_actor;
1934 cev.crossing.related = NULL;
1936 emit_pointer_event (&cev, device);
1940 set_motion_last_actor (motion_current_actor, device);
1945 * @event: a #ClutterEvent.
1947 * Processes an event. This function should never be called by applications.
1952 clutter_do_event (ClutterEvent *event)
1954 /* FIXME: This should probably be clutter_cook_event() - it would
1955 * take a raw event from the backend and 'cook' it so its more tasty.
1958 ClutterMainContext *context;
1959 ClutterBackend *backend;
1960 ClutterActor *stage;
1961 ClutterInputDevice *device = NULL;
1962 static gint32 motion_last_time = 0L;
1963 gint32 local_motion_time;
1965 context = clutter_context_get_default ();
1966 backend = context->backend;
1967 stage = CLUTTER_ACTOR(event->any.stage);
1972 CLUTTER_TIMESTAMP (EVENT, "Event received");
1974 context->last_event_time = clutter_event_get_time (event);
1976 switch (event->type)
1978 case CLUTTER_NOTHING:
1979 event->any.source = stage;
1983 /* The source is set for generated events, not for events
1984 * resulting from the cursor leaving the stage
1986 if (event->any.source == NULL)
1988 ClutterActor *last_actor = context->motion_last_actor;
1990 if (event->crossing.device != NULL)
1991 last_actor = event->crossing.device->motion_last_actor;
1993 event->any.source = last_actor;
1995 set_motion_last_actor (NULL, event->crossing.device);
1999 emit_pointer_event (event, event->crossing.device);
2002 case CLUTTER_DESTROY_NOTIFY:
2003 case CLUTTER_DELETE:
2004 event->any.source = stage;
2005 /* the stage did not handle the event, so we just quit */
2006 if (!clutter_stage_event (CLUTTER_STAGE (stage), event))
2008 if (stage == clutter_stage_get_default())
2009 clutter_main_quit ();
2011 clutter_actor_destroy (stage);
2016 case CLUTTER_KEY_PRESS:
2017 case CLUTTER_KEY_RELEASE:
2019 ClutterActor *actor = NULL;
2021 /* check that we're not a synthetic event with source set */
2022 if (event->any.source == NULL)
2024 actor = clutter_stage_get_key_focus (CLUTTER_STAGE (stage));
2025 event->any.source = actor;
2026 if (G_UNLIKELY (actor == NULL))
2028 g_warning ("No key focus set, discarding");
2033 emit_keyboard_event (event);
2037 case CLUTTER_MOTION:
2038 device = event->motion.device;
2041 local_motion_time = device->motion_last_time;
2043 local_motion_time = motion_last_time;
2045 /* avoid rate throttling for synthetic motion events or if
2046 * the per-actor events are disabled
2048 if (!(event->any.flags & CLUTTER_EVENT_FLAG_SYNTHETIC) ||
2049 !context->motion_events_per_actor)
2051 gint32 frame_rate, delta;
2053 /* avoid issuing too many motion events, which leads to many
2054 * redraws in pick mode (performance penalty)
2056 frame_rate = clutter_get_motion_events_frequency ();
2057 delta = 1000 / frame_rate;
2059 CLUTTER_NOTE (EVENT,
2060 "skip motion event: %s (last:%d, delta:%d, time:%d)",
2061 (event->any.time < (local_motion_time + delta) ? "yes" : "no"),
2066 /* we need to guard against roll-overs and the
2067 * case where the time is rolled backwards and
2068 * the backend is not ensuring a monotonic clock
2072 * http://bugzilla.openedhand.com/show_bug.cgi?id=1130
2074 if (event->any.time >= local_motion_time &&
2075 event->any.time < (local_motion_time + delta))
2078 local_motion_time = event->any.time;
2082 device->motion_last_time = local_motion_time;
2084 motion_last_time = local_motion_time;
2086 /* Only stage gets motion events if clutter_set_motion_events is TRUE,
2087 * and the event is not a synthetic event with source set.
2089 if (!context->motion_events_per_actor &&
2090 event->any.source == NULL)
2092 /* Only stage gets motion events */
2093 event->any.source = stage;
2096 if (context->pointer_grab_actor != NULL)
2098 clutter_actor_event (context->pointer_grab_actor,
2102 else if (device != NULL && device->pointer_grab_actor != NULL)
2104 clutter_actor_event (device->pointer_grab_actor,
2109 /* Trigger handlers on stage in both capture .. */
2110 if (!clutter_actor_event (stage, event, TRUE))
2112 /* and bubbling phase */
2113 clutter_actor_event (stage, event, FALSE);
2120 case CLUTTER_BUTTON_PRESS:
2121 case CLUTTER_BUTTON_RELEASE:
2122 case CLUTTER_SCROLL:
2124 ClutterActor *actor;
2127 clutter_event_get_coords (event, &x, &y);
2129 /* Only do a pick to find the source if source is not already set
2130 * (as it could be in a synthetic event)
2132 if (event->any.source == NULL)
2134 /* Handle release off stage */
2135 if ((x >= clutter_actor_get_width (stage) ||
2136 y >= clutter_actor_get_height (stage) ||
2139 if (event->type == CLUTTER_BUTTON_RELEASE)
2141 CLUTTER_NOTE (EVENT,
2142 "Release off stage received at %i, %i",
2145 event->button.source = stage;
2146 emit_pointer_event (event, event->button.device);
2151 /* Map the event to a reactive actor */
2152 actor = _clutter_do_pick (CLUTTER_STAGE (stage),
2154 CLUTTER_PICK_REACTIVE);
2156 event->any.source = actor;
2162 /* use the source already set in the synthetic event */
2163 actor = event->any.source;
2167 /* FIXME: for an optimisation should check if there are
2168 * actually any reactive actors and avoid the pick all togeather
2169 * (signalling just the stage). Should be big help for gles.
2172 CLUTTER_NOTE (EVENT, "Reactive event received at %i, %i - actor: %p",
2175 /* Create, enter/leave events if needed */
2176 generate_enter_leave_events (event);
2178 if (event->type != CLUTTER_MOTION)
2180 /* Generate click count */
2181 event_click_count_generate (event);
2186 switch (event->type)
2188 case CLUTTER_BUTTON_PRESS:
2189 case CLUTTER_BUTTON_RELEASE:
2190 device = event->button.device;
2192 case CLUTTER_SCROLL:
2193 device = event->scroll.device;
2195 case CLUTTER_MOTION:
2196 /* already handled in the MOTION case of the switch */
2202 emit_pointer_event (event, device);
2206 case CLUTTER_STAGE_STATE:
2207 /* fullscreen / focus - forward to stage */
2208 event->any.source = stage;
2209 clutter_stage_event (CLUTTER_STAGE (stage), event);
2212 case CLUTTER_CLIENT_MESSAGE:
2218 * clutter_get_actor_by_gid
2219 * @id: a #ClutterActor ID.
2221 * Retrieves the #ClutterActor with @id.
2223 * Return value: the actor with the passed id or %NULL. The returned
2224 * actor does not have its reference count increased.
2229 clutter_get_actor_by_gid (guint32 id)
2231 ClutterMainContext *context;
2233 context = clutter_context_get_default ();
2235 g_return_val_if_fail (context != NULL, NULL);
2237 return CLUTTER_ACTOR (clutter_id_pool_lookup (context->id_pool, id));
2241 clutter_base_init (void)
2243 static gboolean initialised = FALSE;
2247 GType foo; /* Quiet gcc */
2251 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
2252 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2254 /* initialise GLib type system */
2257 /* CLUTTER_TYPE_ACTOR */
2258 foo = clutter_actor_get_type ();
2263 * clutter_get_default_frame_rate:
2265 * Retrieves the default frame rate used when creating #ClutterTimeline<!--
2268 * This value is also used to compute the default frequency of motion
2271 * Return value: the default frame rate
2276 clutter_get_default_frame_rate (void)
2278 ClutterMainContext *context;
2280 context = clutter_context_get_default ();
2282 return context->frame_rate;
2286 * clutter_set_default_frame_rate:
2287 * @frames_per_sec: the new default frame rate
2289 * Sets the default frame rate to be used when creating #ClutterTimeline<!--
2295 clutter_set_default_frame_rate (guint frames_per_sec)
2297 ClutterMainContext *context;
2299 context = clutter_context_get_default ();
2301 if (context->frame_rate != frames_per_sec)
2302 context->frame_rate = frames_per_sec;
2307 on_pointer_grab_weak_notify (gpointer data,
2308 GObject *where_the_object_was)
2310 ClutterInputDevice *dev = (ClutterInputDevice *)data;
2311 ClutterMainContext *context;
2313 context = clutter_context_get_default ();
2317 dev->pointer_grab_actor = NULL;
2318 clutter_ungrab_pointer_for_device (dev->id);
2322 context->pointer_grab_actor = NULL;
2323 clutter_ungrab_pointer ();
2328 * clutter_grab_pointer:
2329 * @actor: a #ClutterActor
2331 * Grabs pointer events, after the grab is done all pointer related events
2332 * (press, motion, release, enter, leave and scroll) are delivered to this
2333 * actor directly. The source set in the event will be the actor that would
2334 * have received the event if the pointer grab was not in effect.
2336 * If you wish to grab all the pointer events for a specific input device,
2337 * you should use clutter_grab_pointer_for_device().
2342 clutter_grab_pointer (ClutterActor *actor)
2344 ClutterMainContext *context;
2346 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2348 context = clutter_context_get_default ();
2350 if (context->pointer_grab_actor == actor)
2353 if (context->pointer_grab_actor)
2355 g_object_weak_unref (G_OBJECT (context->pointer_grab_actor),
2356 on_pointer_grab_weak_notify,
2358 context->pointer_grab_actor = NULL;
2363 context->pointer_grab_actor = actor;
2365 g_object_weak_ref (G_OBJECT (actor),
2366 on_pointer_grab_weak_notify,
2372 * clutter_grab_pointer_for_device:
2373 * @actor: a #ClutterActor
2374 * @id: a device id, or -1
2376 * Grabs all the pointer events coming from the device @id for @actor.
2378 * If @id is -1 then this function is equivalent to clutter_grab_pointer().
2383 clutter_grab_pointer_for_device (ClutterActor *actor,
2386 ClutterInputDevice *dev;
2388 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2390 /* essentially a global grab */
2393 clutter_grab_pointer (actor);
2397 dev = clutter_get_input_device_for_id (id);
2402 if (dev->pointer_grab_actor == actor)
2405 if (dev->pointer_grab_actor)
2407 g_object_weak_unref (G_OBJECT (dev->pointer_grab_actor),
2408 on_pointer_grab_weak_notify,
2410 dev->pointer_grab_actor = NULL;
2415 dev->pointer_grab_actor = actor;
2417 g_object_weak_ref (G_OBJECT (actor),
2418 on_pointer_grab_weak_notify,
2425 * clutter_ungrab_pointer:
2427 * Removes an existing grab of the pointer.
2432 clutter_ungrab_pointer (void)
2434 clutter_grab_pointer (NULL);
2438 * clutter_ungrab_pointer_for_device:
2441 * Removes an existing grab of the pointer events for device @id.
2446 clutter_ungrab_pointer_for_device (gint id)
2448 clutter_grab_pointer_for_device (NULL, id);
2453 * clutter_get_pointer_grab:
2455 * Queries the current pointer grab of clutter.
2457 * Return value: the actor currently holding the pointer grab, or NULL if there is no grab.
2462 clutter_get_pointer_grab (void)
2464 ClutterMainContext *context;
2465 context = clutter_context_get_default ();
2467 return context->pointer_grab_actor;
2472 on_keyboard_grab_weak_notify (gpointer data,
2473 GObject *where_the_object_was)
2475 ClutterMainContext *context;
2477 context = clutter_context_get_default ();
2478 context->keyboard_grab_actor = NULL;
2480 clutter_ungrab_keyboard ();
2484 * clutter_grab_keyboard:
2485 * @actor: a #ClutterActor
2487 * Grabs keyboard events, after the grab is done keyboard events ("key-press-event"
2488 * and "key-release-event") are delivered to this actor directly. The source
2489 * set in the event will be the actor that would have received the event if the
2490 * keyboard grab was not in effect.
2495 clutter_grab_keyboard (ClutterActor *actor)
2497 ClutterMainContext *context;
2499 g_return_if_fail (actor == NULL || CLUTTER_IS_ACTOR (actor));
2501 context = clutter_context_get_default ();
2503 if (context->keyboard_grab_actor == actor)
2506 if (context->keyboard_grab_actor)
2508 g_object_weak_unref (G_OBJECT (context->keyboard_grab_actor),
2509 on_keyboard_grab_weak_notify,
2511 context->keyboard_grab_actor = NULL;
2516 context->keyboard_grab_actor = actor;
2518 g_object_weak_ref (G_OBJECT (actor),
2519 on_keyboard_grab_weak_notify,
2525 * clutter_ungrab_keyboard:
2527 * Removes an existing grab of the keyboard.
2532 clutter_ungrab_keyboard (void)
2534 clutter_grab_keyboard (NULL);
2538 * clutter_get_keyboard_grab:
2540 * Queries the current keyboard grab of clutter.
2542 * Return value: the actor currently holding the keyboard grab, or NULL if there is no grab.
2547 clutter_get_keyboard_grab (void)
2549 ClutterMainContext *context;
2550 context = clutter_context_get_default ();
2552 return context->keyboard_grab_actor;
2556 * clutter_get_motion_events_frequency:
2558 * Retrieves the number of motion events per second that are delivered
2561 * See clutter_set_motion_events_frequency().
2563 * Return value: the number of motion events per second
2568 clutter_get_motion_events_frequency (void)
2570 ClutterMainContext *context = clutter_context_get_default ();
2572 if (G_LIKELY (context->motion_frequency == 0))
2576 frequency = clutter_default_fps / 4;
2577 frequency = CLAMP (frequency, 20, 45);
2582 return context->motion_frequency;
2586 * clutter_set_motion_events_frequency:
2587 * @frequency: the number of motion events per second, or 0 for the
2590 * Sets the motion events frequency. Setting this to a non-zero value
2591 * will override the default setting, so it should be rarely used.
2593 * Motion events are delivered from the default backend to the stage
2594 * and are used to generate the enter/leave events pair. This might lead
2595 * to a performance penalty due to the way the actors are identified.
2596 * Using this function is possible to reduce the frequency of the motion
2597 * events delivery to the stage.
2602 clutter_set_motion_events_frequency (guint frequency)
2604 ClutterMainContext *context = clutter_context_get_default ();
2606 /* never allow the motion events to exceed the default frame rate */
2607 context->motion_frequency = CLAMP (frequency, 1, clutter_default_fps);
2611 * clutter_clear_glyph_cache:
2613 * Clears the internal cache of glyphs used by the Pango
2614 * renderer. This will free up some memory and GL texture
2615 * resources. The cache will be automatically refilled as more text is
2621 clutter_clear_glyph_cache (void)
2623 if (CLUTTER_CONTEXT ()->font_map)
2624 cogl_pango_font_map_clear_glyph_cache (CLUTTER_CONTEXT ()->font_map);
2628 * clutter_set_font_flags:
2629 * @flags: The new flags
2631 * Sets the font quality options for subsequent text rendering
2634 * Using mipmapped textures will improve the quality for scaled down
2635 * text but will use more texture memory.
2637 * Enabling hinting improves text quality for static text but may
2638 * introduce some artifacts if the text is animated.
2643 clutter_set_font_flags (ClutterFontFlags flags)
2645 ClutterFontFlags old_flags, changed_flags;
2646 cairo_font_options_t *font_options;
2648 if (CLUTTER_CONTEXT ()->font_map)
2649 cogl_pango_font_map_set_use_mipmapping (CLUTTER_CONTEXT ()->font_map,
2651 & CLUTTER_FONT_MIPMAPPING) != 0);
2653 old_flags = clutter_get_font_flags ();
2655 font_options = clutter_backend_get_font_options (CLUTTER_CONTEXT ()->backend);
2656 font_options = cairo_font_options_copy (font_options);
2658 /* Only set the font options that have actually changed so we don't
2659 override a detailed setting from the backend */
2660 changed_flags = old_flags ^ flags;
2662 if ((changed_flags & CLUTTER_FONT_HINTING))
2663 cairo_font_options_set_hint_style (font_options,
2664 (flags & CLUTTER_FONT_HINTING)
2665 ? CAIRO_HINT_STYLE_FULL
2666 : CAIRO_HINT_STYLE_NONE);
2668 clutter_backend_set_font_options (CLUTTER_CONTEXT ()->backend, font_options);
2670 cairo_font_options_destroy (font_options);
2672 if (CLUTTER_CONTEXT ()->pango_context)
2673 update_pango_context (CLUTTER_CONTEXT ()->backend,
2674 CLUTTER_CONTEXT ()->pango_context);
2678 * clutter_get_font_flags:
2680 * Gets the current font flags for rendering text. See
2681 * clutter_set_font_flags().
2683 * Return value: The font flags
2688 clutter_get_font_flags (void)
2690 ClutterMainContext *ctxt = CLUTTER_CONTEXT ();
2691 CoglPangoFontMap *font_map = NULL;
2692 cairo_font_options_t *font_options;
2693 ClutterFontFlags flags = 0;
2695 font_map = CLUTTER_CONTEXT ()->font_map;
2697 if (G_LIKELY (font_map)
2698 && cogl_pango_font_map_get_use_mipmapping (font_map))
2699 flags |= CLUTTER_FONT_MIPMAPPING;
2701 font_options = clutter_backend_get_font_options (ctxt->backend);
2703 if ((cairo_font_options_get_hint_style (font_options)
2704 != CAIRO_HINT_STYLE_DEFAULT)
2705 && (cairo_font_options_get_hint_style (font_options)
2706 != CAIRO_HINT_STYLE_NONE))
2707 flags |= CLUTTER_FONT_HINTING;
2713 * clutter_get_input_device_for_id:
2716 * Retrieves the #ClutterInputDevice from its id.
2718 * Return value: a #ClutterInputDevice, or %NULL
2722 ClutterInputDevice *
2723 clutter_get_input_device_for_id (gint id)
2726 ClutterInputDevice *device = NULL;
2727 ClutterMainContext *context;
2729 context = clutter_context_get_default ();
2731 for (item = context->input_devices;
2735 device = item->data;
2737 if (device->id == id)
2745 * clutter_get_font_map:
2747 * Retrieves the #PangoFontMap instance used by Clutter.
2748 * You can use the global font map object with the COGL
2751 * Return value: the #PangoFontMap instance. The returned
2752 * value is owned by Clutter and it should never be
2758 clutter_get_font_map (void)
2760 if (CLUTTER_CONTEXT ()->font_map)
2761 return PANGO_FONT_MAP (CLUTTER_CONTEXT ()->font_map);