Allow checking the backend type at run-time
authorEmmanuele Bassi <ebassi@linux.intel.com>
Mon, 26 Sep 2011 12:14:26 +0000 (13:14 +0100)
committerEmmanuele Bassi <ebassi@linux.intel.com>
Thu, 3 Nov 2011 13:45:19 +0000 (13:45 +0000)
Portable code should be allowed to check type backend currently being
used, so that it can use platform-specific API (not just Clutter's).

We don't want to go down the GDK route, with public types for
ClutterBackend and ClutterStageWindow implementations, and use the type
system, e.g.:

  #ifdef GDK_WINDOWING_X11
    if (GDK_IS_WINDOW_X11 (window))
      use_x11_api (window);
    else
  #endif
  #ifdef GDK_WINDOWING_WIN32
    if (GDK_IS_WINDOW_WIN32 (window))
      use_win32_api (window);
    else
  #endif
    g_critical ("Unsupported backend");

This system would make us expose the backend system, and we want to
still reserve us the option to change the backend system to increase its
granularity — e.g. choosing different input event systems regardless of
the windowing system.

This commit adds a simple function that checks the backend type against
a symbolic constant — the same constant string that can be used to
select the backend at run-time through the CLUTTER_BACKEND environment
variable.

clutter/clutter-backend.h
clutter/clutter-main.c
clutter/clutter-version.h.in

index 4e5d83e..637dc1e 100644 (file)
@@ -49,6 +49,73 @@ G_BEGIN_DECLS
 #define CLUTTER_IS_BACKEND(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CLUTTER_TYPE_BACKEND))
 
 /**
+ * CLUTTER_OSX_BACKEND:
+ *
+ * Evaluates to the symbolic name of the Quartz Clutter backend.
+ *
+ * This macro should be used with clutter_check_backend().
+ *
+ * Since: 1.10
+ */
+#define CLUTTER_OSX_BACKEND             "osx"
+
+/**
+ * CLUTTER_X11_BACKEND:
+ *
+ * Evaluates to the symbolic name of the X11 Clutter backend.
+ *
+ * This macro should be used with clutter_check_backend().
+ *
+ * Since: 1.10
+ */
+
+#define CLUTTER_X11_BACKEND             "x11"
+
+/**
+ * CLUTTER_WIN32_BACKEND:
+ *
+ * Evaluates to the symbolic name of the Windows Clutter backend.
+ *
+ * This macro should be used with clutter_check_backend().
+ *
+ * Since: 1.10
+ */
+#define CLUTTER_WIN32_BACKEND           "win32"
+
+/**
+ * CLUTTER_EGL_NATIVE_BACKEND:
+ *
+ * Evaluates to the symbolic name of the EGL framebuffer Clutter backend.
+ *
+ * This macro should be used with clutter_check_backend().
+ *
+ * Since: 1.10
+ */
+#define CLUTTER_EGL_NATIVE_BACKEND      "eglnative"
+
+/**
+ * CLUTTER_WAYLAND_BACKEND:
+ *
+ * Evaluates to the symbolic name of the Wayland client Clutter backend.
+ *
+ * This macro should be used with clutter_check_backend().
+ *
+ * Since: 1.10
+ */
+#define CLUTTER_WAYLAND_BACKEND         "wayland"
+
+/**
+ * CLUTTER_GDK_BACKEND:
+ *
+ * Evaluates to the symbolic name of the GDK Clutter backend.
+ *
+ * This macro should be used with clutter_check_backend().
+ *
+ * Since: 1.10
+ */
+#define CLUTTER_GDK_BACKEND             "gdk"
+
+/**
  * ClutterBackend:
  *
  * <structname>ClutterBackend</structname> is an opaque structure whose
index 6ee8986..888d215 100644 (file)
@@ -1326,32 +1326,32 @@ clutter_context_get_default_unlocked (void)
       backend = g_getenv ("CLUTTER_BACKEND");
 
 #ifdef CLUTTER_WINDOWING_OSX
-      if (backend == NULL || strcmp (backend, "osx") == 0)
+      if (backend == NULL || strcmp (backend, CLUTTER_OSX_BACKEND) == 0)
         ctx->backend = g_object_new (CLUTTER_TYPE_BACKEND_OSX, NULL);
       else
 #endif
 #ifdef CLUTTER_WINDOWING_WIN32
-      if (backend == NULL || strcmp (backend, "win32") == 0)
+      if (backend == NULL || strcmp (backend, CLUTTER_WIN32_BACKEND) == 0)
         ctx->backend = g_object_new (CLUTTER_TYPE_BACKEND_WIN32, NULL);
       else
 #endif
 #ifdef CLUTTER_WINDOWING_WAYLAND
-      if (backend == NULL || strcmp (backend, "wayland") == 0)
+      if (backend == NULL || strcmp (backend, CLUTTER_WAYLAND_BACKEND) == 0)
         ctx->backend = g_object_new (CLUTTER_TYPE_BACKEND_WAYLAND, NULL);
       else
 #endif
 #ifdef CLUTTER_WINDOWING_EGL
-      if (backend == NULL || strcmp (backend, "eglnative") == 0)
+      if (backend == NULL || strcmp (backend, CLUTTER_EGL_NATIVE_BACKEND) == 0)
         ctx->backend = g_object_new (CLUTTER_TYPE_BACKEND_EGLNATIVE, NULL);
       else
 #endif
 #ifdef CLUTTER_WINDOWING_X11
-      if (backend == NULL || strcmp (backend, "x11") == 0)
+      if (backend == NULL || strcmp (backend, CLUTTER_X11_BACKEND) == 0)
         ctx->backend = g_object_new (CLUTTER_TYPE_BACKEND_X11, NULL);
       else
 #endif
 #ifdef CLUTTER_WINDOWING_GDK
-      if (backend == NULL || strcmp (backend, "gdk") == 0)
+      if (backend == NULL || strcmp (backend, CLUTTER_GDK_BACKEND) == 0)
         ctx->backend = g_object_new (CLUTTER_TYPE_BACKEND_GDK, NULL);
       else
 #endif
@@ -3610,3 +3610,61 @@ _clutter_context_get_motion_events_enabled (void)
 
   return context->motion_events_per_actor;
 }
+
+/**
+ * clutter_check_backend:
+ * @backend_type: the name of the backend to check
+ *
+ * Checks the run-time name of the Clutter backend, using the symbolic
+ * macros like %CLUTTER_OSX_BACKEND or %CLUTTER_X11_BACKEND.
+ *
+ * Return value: %TRUE if the current Clutter backend is the one checked,
+ *   and %FALSE otherwise
+ *
+ * Since: 1.10
+ */
+gboolean
+clutter_check_backend (const char *backend_type)
+{
+  ClutterMainContext *context = _clutter_context_get_default ();
+
+  g_return_val_if_fail (backend_type != NULL, FALSE);
+
+#ifdef CLUTTER_WINDOWING_OSX
+  if (strcmp (backend_type, CLUTTER_OSX_BACKEND) == 0 &&
+      CLUTTER_IS_BACKEND_OSX (context->backend))
+    return TRUE;
+  else
+#endif
+#ifdef CLUTTER_WINDOWING_WIN32
+  if (strcmp (backend_type, CLUTTER_WIN32_BACKEND) == 0 &&
+      CLUTTER_IS_BACKEND_WIN32 (context->backend))
+    return TRUE;
+  else
+#endif
+#ifdef CLUTTER_WINDOWING_WAYLAND
+  if (strcmp (backend_type, CLUTTER_WAYLAND_BACKEND) == 0 &&
+      CLUTTER_IS_BACKEND_WAYLAND (context->backend))
+    return TRUE;
+  else
+#endif
+#ifdef CLUTTER_WINDOWING_EGL
+  if (strcmp (backend_type, CLUTTER_EGL_BACKEND) == 0 &&
+      CLUTTER_IS_BACKEND_EGL_NATIVE (context->backend))
+    return TRUE;
+  else
+#endif
+#ifdef CLUTTER_WINDOWING_X11
+  if (strcmp (backend_type, CLUTTER_X11_BACKEND) == 0 &&
+      CLUTTER_IS_BACKEND_X11 (context->backend))
+    return TRUE;
+  else
+#endif
+#ifdef CLUTTER_WINDOWING_GDK
+  if (strcmp (backend_type, CLUTTER_GDK_BACKEND) == 0 &&
+      CLUTTER_IS_BACKEND_GDK (context->backend))
+    return TRUE;
+  else
+#endif
+  return FALSE;
+}
index 835e29f..5b38ee0 100644 (file)
@@ -195,6 +195,8 @@ gboolean clutter_check_version (guint major,
                                 guint minor,
                                 guint micro);
 
+gboolean clutter_check_backend (const char *backend_type);
+
 G_END_DECLS
 
 #endif /* __CLUTTER_VERSION_H__ */