mutter/clutter/gdk/clutter-stage-gdk.c
Giovanni Campagna 610a9c17ba Add a new GDK backend
This commit introduces a new flavour for Clutter, that uses GDK
for handling all window system specific interactions (except for
creating the cogl context, as cogl does not know about GDK), including
in particular events. This is not compatible with the X11 (glx)
flavour, and this is reflected by the different soname (libclutter-gdk-1.0.so),
as all X11 specific functions and classes are not available. If you
wish to be compatible, you should check for CLUTTER_WINDOWING_X11.
Other than that, this backend should be on feature parity with X11,
including XInput 2, XSettings and EMWH (with much, much less code)

https://bugzilla.gnome.org/show_bug.cgi?id=657434
2011-11-03 13:42:13 +00:00

602 lines
16 KiB
C

/* Clutter.
* An OpenGL based 'interactive canvas' library.
* Authored By Matthew Allum <mallum@openedhand.com>
* Copyright (C) 2006-2007 OpenedHand
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
#include "config.h"
#include <math.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "clutter-backend-gdk.h"
#include "clutter-stage-gdk.h"
#include "clutter-gdk.h"
#include "clutter-actor-private.h"
#include "clutter-debug.h"
#include "clutter-device-manager-private.h"
#include "clutter-enum-types.h"
#include "clutter-event-translator.h"
#include "clutter-event-private.h"
#include "clutter-feature.h"
#include "clutter-main.h"
#include "clutter-paint-volume-private.h"
#include "clutter-private.h"
#include "clutter-stage-private.h"
#include "cogl/cogl.h"
static void clutter_stage_window_iface_init (ClutterStageWindowIface *iface);
#define clutter_stage_gdk_get_type _clutter_stage_gdk_get_type
G_DEFINE_TYPE_WITH_CODE (ClutterStageGdk,
clutter_stage_gdk,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_STAGE_WINDOW,
clutter_stage_window_iface_init));
enum {
PROP_0,
PROP_WRAPPER,
PROP_BACKEND,
PROP_LAST
};
void
_clutter_stage_gdk_update_foreign_event_mask (CoglOnscreen *onscreen,
guint32 event_mask,
void *user_data)
{
ClutterStageGdk *stage_gdk = user_data;
/* we assume that a GDK event mask is bitwise compatible with X11
event masks */
gdk_window_set_events (stage_gdk->window, event_mask | CLUTTER_STAGE_GDK_EVENT_MASK);
}
static void
clutter_stage_gdk_set_gdk_geometry (ClutterStageGdk *stage)
{
GdkGeometry geometry;
gboolean resize = clutter_stage_get_user_resizable (stage->wrapper);
if (!resize)
{
geometry.min_width = geometry.max_width = gdk_window_get_width (stage->window);
geometry.min_height = geometry.max_height = gdk_window_get_height (stage->window);
gdk_window_set_geometry_hints (stage->window,
&geometry,
GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE);
}
else
{
clutter_stage_get_minimum_size (stage->wrapper,
(guint *)&geometry.min_width,
(guint *)&geometry.min_height);
gdk_window_set_geometry_hints (stage->window,
&geometry,
GDK_HINT_MIN_SIZE);
}
}
static void
clutter_stage_gdk_get_geometry (ClutterStageWindow *stage_window,
ClutterGeometry *geometry)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
if (stage_gdk->window)
{
geometry->width = gdk_window_get_width (stage_gdk->window);
geometry->height = gdk_window_get_height (stage_gdk->window);
}
else
{
geometry->width = 640;
geometry->height = 480;
}
}
static void
clutter_stage_gdk_resize (ClutterStageWindow *stage_window,
gint width,
gint height)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
if (width == 0 || height == 0)
{
/* Should not happen, if this turns up we need to debug it and
* determine the cleanest way to fix.
*/
g_warning ("GDK stage not allowed to have 0 width or height");
width = 1;
height = 1;
}
CLUTTER_NOTE (BACKEND, "New size received: (%d, %d)", width, height);
CLUTTER_SET_PRIVATE_FLAGS (stage_gdk->wrapper,
CLUTTER_IN_RESIZE);
gdk_window_resize (stage_gdk->window, width, height);
}
static void
clutter_stage_gdk_unrealize (ClutterStageWindow *stage_window)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
if (stage_gdk->window)
{
g_object_set_data (G_OBJECT (stage_gdk->window),
"clutter-stage-window", NULL);
if (stage_gdk->foreign_window)
g_object_unref (stage_gdk->window);
else
gdk_window_destroy (stage_gdk->window);
stage_gdk->window = NULL;
}
}
static gboolean
clutter_stage_gdk_realize (ClutterStageWindow *stage_window)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
ClutterBackendGdk *backend_gdk = stage_gdk->backend;
GdkWindowAttr attributes;
gboolean cursor_visible;
gboolean use_alpha;
gfloat width, height;
if (stage_gdk->foreign_window &&
stage_gdk->window)
{
/* complete realizing the stage */
ClutterGeometry geometry;
clutter_stage_gdk_get_geometry (stage_window, &geometry);
clutter_actor_set_geometry (CLUTTER_ACTOR (stage_gdk->wrapper), &geometry);
gdk_window_ensure_native (stage_gdk->window);
gdk_window_set_events (stage_gdk->window,
CLUTTER_STAGE_GDK_EVENT_MASK);
return TRUE;
}
attributes.title = NULL;
g_object_get (stage_gdk->wrapper,
"cursor-visible", &cursor_visible,
"title", &attributes.title,
"width", &width,
"height", &height,
"use-alpha", &use_alpha,
NULL);
attributes.width = width;
attributes.height = height;
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.window_type = GDK_WINDOW_TOPLEVEL;
attributes.event_mask = CLUTTER_STAGE_GDK_EVENT_MASK;
attributes.cursor = NULL;
if (!cursor_visible)
{
if (stage_gdk->blank_cursor == NULL)
stage_gdk->blank_cursor = gdk_cursor_new (GDK_BLANK_CURSOR);
attributes.cursor = stage_gdk->blank_cursor;
}
if (use_alpha)
{
attributes.visual = gdk_screen_get_rgba_visual (backend_gdk->screen);
if (attributes.visual == NULL)
clutter_stage_set_use_alpha (stage_gdk->wrapper, FALSE);
}
else
{
/* This could still be an RGBA visual, although normally it's not */
attributes.visual = gdk_screen_get_system_visual (backend_gdk->screen);
}
if (stage_gdk->window != NULL)
{
g_critical ("Stage realized more than once");
g_object_set_data (G_OBJECT (stage_gdk->window),
"clutter-stage-window", NULL);
if (stage_gdk->foreign_window)
g_object_unref (stage_gdk->window);
else
gdk_window_destroy (stage_gdk->window);
}
stage_gdk->foreign_window = FALSE;
stage_gdk->window = gdk_window_new (NULL, &attributes,
GDK_WA_TITLE | GDK_WA_CURSOR | GDK_WA_VISUAL);
gdk_window_ensure_native (stage_gdk->window);
clutter_stage_gdk_set_gdk_geometry (stage_gdk);
g_object_set_data (G_OBJECT (stage_gdk->window),
"clutter-stage-window", stage_gdk);
g_free (attributes.title);
CLUTTER_NOTE (BACKEND, "Successfully realized stage");
return TRUE;
}
static void
clutter_stage_gdk_set_fullscreen (ClutterStageWindow *stage_window,
gboolean is_fullscreen)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
ClutterStage *stage = stage_gdk->wrapper;
if (stage == NULL || CLUTTER_ACTOR_IN_DESTRUCTION (stage))
return;
if (stage_gdk->window == NULL)
return;
CLUTTER_NOTE (BACKEND, "%ssetting fullscreen", is_fullscreen ? "" : "un");
if (is_fullscreen)
gdk_window_fullscreen (stage_gdk->window);
else
gdk_window_unfullscreen (stage_gdk->window);
}
static void
clutter_stage_gdk_set_cursor_visible (ClutterStageWindow *stage_window,
gboolean cursor_visible)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
if (stage_gdk->window == NULL)
return;
if (cursor_visible)
{
gdk_window_set_cursor (stage_gdk->window, NULL);
}
else
{
if (stage_gdk->blank_cursor == NULL)
stage_gdk->blank_cursor = gdk_cursor_new (GDK_BLANK_CURSOR);
gdk_window_set_cursor (stage_gdk->window, stage_gdk->blank_cursor);
}
}
static void
clutter_stage_gdk_set_title (ClutterStageWindow *stage_window,
const gchar *title)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
if (stage_gdk->window == NULL)
return;
gdk_window_set_title (stage_gdk->window, title);
}
static void
clutter_stage_gdk_set_user_resizable (ClutterStageWindow *stage_window,
gboolean is_resizable)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
GdkWMFunction function;
if (stage_gdk->window == NULL)
return;
function = GDK_FUNC_MOVE | GDK_FUNC_MINIMIZE | GDK_FUNC_CLOSE;
if (is_resizable)
function |= GDK_FUNC_RESIZE | GDK_FUNC_MAXIMIZE;
gdk_window_set_functions (stage_gdk->window, function);
clutter_stage_gdk_set_gdk_geometry (stage_gdk);
}
static void
clutter_stage_gdk_set_accept_focus (ClutterStageWindow *stage_window,
gboolean accept_focus)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
if (stage_gdk->window == NULL)
return;
gdk_window_set_accept_focus (stage_gdk->window, accept_focus);
}
static void
clutter_stage_gdk_show (ClutterStageWindow *stage_window,
gboolean do_raise)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
g_return_if_fail (stage_gdk->window != NULL);
clutter_actor_map (CLUTTER_ACTOR (stage_gdk->wrapper));
if (do_raise)
gdk_window_show (stage_gdk->window);
else
gdk_window_show_unraised (stage_gdk->window);
}
static void
clutter_stage_gdk_hide (ClutterStageWindow *stage_window)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window);
g_return_if_fail (stage_gdk->window != NULL);
clutter_actor_unmap (CLUTTER_ACTOR (stage_gdk->wrapper));
gdk_window_hide (stage_gdk->window);
}
static ClutterActor *
clutter_stage_gdk_get_wrapper (ClutterStageWindow *stage_window)
{
return CLUTTER_ACTOR (CLUTTER_STAGE_GDK (stage_window)->wrapper);
}
static void
clutter_stage_gdk_dispose (GObject *gobject)
{
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (gobject);
if (stage_gdk->window)
{
g_object_set_data (G_OBJECT (stage_gdk->window),
"clutter-stage-window", NULL);
if (stage_gdk->foreign_window)
g_object_unref (stage_gdk->window);
else
gdk_window_destroy (stage_gdk->window);
stage_gdk->window = NULL;
}
if (stage_gdk->blank_cursor)
{
g_object_unref (stage_gdk->blank_cursor);
stage_gdk->blank_cursor = NULL;
}
G_OBJECT_CLASS (clutter_stage_gdk_parent_class)->dispose (gobject);
}
static void
clutter_stage_gdk_set_property (GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
ClutterStageGdk *self = CLUTTER_STAGE_GDK (gobject);
switch (prop_id)
{
case PROP_WRAPPER:
self->wrapper = CLUTTER_STAGE (g_value_get_object (value));
break;
case PROP_BACKEND:
self->backend = CLUTTER_BACKEND_GDK (g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
}
}
static void
clutter_stage_gdk_class_init (ClutterStageGdkClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = clutter_stage_gdk_dispose;
gobject_class->set_property = clutter_stage_gdk_set_property;
g_object_class_install_property (gobject_class, PROP_WRAPPER,
g_param_spec_object ("wrapper",
"Wrapper",
"ClutterStage wrapping this native stage",
CLUTTER_TYPE_STAGE,
CLUTTER_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (gobject_class, PROP_BACKEND,
g_param_spec_object ("backend",
"ClutterBackend",
"The Clutter backend singleton",
CLUTTER_TYPE_BACKEND_GDK,
CLUTTER_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
}
static void
clutter_stage_gdk_init (ClutterStageGdk *stage)
{
/* nothing to do here */
}
static void
clutter_stage_window_iface_init (ClutterStageWindowIface *iface)
{
iface->get_wrapper = clutter_stage_gdk_get_wrapper;
iface->set_title = clutter_stage_gdk_set_title;
iface->set_fullscreen = clutter_stage_gdk_set_fullscreen;
iface->set_cursor_visible = clutter_stage_gdk_set_cursor_visible;
iface->set_user_resizable = clutter_stage_gdk_set_user_resizable;
iface->set_accept_focus = clutter_stage_gdk_set_accept_focus;
iface->show = clutter_stage_gdk_show;
iface->hide = clutter_stage_gdk_hide;
iface->resize = clutter_stage_gdk_resize;
iface->get_geometry = clutter_stage_gdk_get_geometry;
iface->realize = clutter_stage_gdk_realize;
iface->unrealize = clutter_stage_gdk_unrealize;
}
/**
* clutter_gdk_get_stage_window:
* @stage: a #ClutterStage
*
* Gets the stages GdkWindow.
*
* Return value: (transfer none): A GdkWindow* for the stage window.
*
* Since: 0.4
*/
GdkWindow*
clutter_gdk_get_stage_window (ClutterStage *stage)
{
ClutterStageWindow *impl;
g_return_val_if_fail (CLUTTER_IS_STAGE (stage), None);
impl = _clutter_stage_get_window (stage);
g_assert (CLUTTER_IS_STAGE_GDK (impl));
return CLUTTER_STAGE_GDK (impl)->window;
}
/**
* clutter_gdk_get_stage_from_window:
* @window: a #GtkWindow
*
* Gets the stage for a particular X window.
*
* Return value: (transfer none): A #ClutterStage, or% NULL if a stage
* does not exist for the window
*
* Since: 0.8
*/
ClutterStage *
clutter_gdk_get_stage_from_window (GdkWindow *window)
{
ClutterStageGdk *stage_gdk = g_object_get_data (G_OBJECT (window), "clutter-stage-window");
if (stage_gdk != NULL && CLUTTER_IS_STAGE_GDK (stage_gdk))
return stage_gdk->wrapper;
return NULL;
}
typedef struct
{
ClutterStageGdk *stage_gdk;
GdkWindow *window;
} ForeignWindowClosure;
static void
set_foreign_window_callback (ClutterActor *actor,
void *data)
{
ForeignWindowClosure *closure = data;
ClutterStageGdk *stage_gdk = closure->stage_gdk;
stage_gdk->window = closure->window;
stage_gdk->foreign_window = TRUE;
/* calling this with the stage unrealized will unset the stage
* from the GL context; once the stage is realized the GL context
* will be set again
*/
clutter_stage_ensure_current (CLUTTER_STAGE (actor));
}
/**
* clutter_gdk_set_stage_foreign:
* @stage: a #ClutterStage
* @window: an existing #GdkWindow
*
* Target the #ClutterStage to use an existing external #GdkWindow
*
* Return value: %TRUE if foreign window is valid
*
* Since: 0.4
*/
gboolean
clutter_gdk_set_stage_foreign (ClutterStage *stage,
GdkWindow *window)
{
ForeignWindowClosure closure;
ClutterStageGdk *stage_gdk;
ClutterStageWindow *impl;
ClutterActor *actor;
gpointer gtk_data = NULL;
g_return_val_if_fail (CLUTTER_IS_STAGE (stage), FALSE);
g_return_val_if_fail (!CLUTTER_ACTOR_IN_DESTRUCTION (stage), FALSE);
g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
impl = _clutter_stage_get_window (stage);
stage_gdk = CLUTTER_STAGE_GDK (impl);
if (g_object_get_data (G_OBJECT (window), "clutter-stage-window") != NULL)
{
g_critical ("The provided GdkWindow is already in use by another ClutterStage");
return FALSE;
}
gdk_window_get_user_data (window, &gtk_data);
if (gtk_data != NULL)
{
g_critical ("The provided GdkWindow is already in use by a GtkWidget. "
"Use a child GdkWindow for embedding instead");
return FALSE;
}
closure.stage_gdk = stage_gdk;
closure.window = g_object_ref (window);
actor = CLUTTER_ACTOR (stage);
_clutter_actor_rerealize (actor,
set_foreign_window_callback,
&closure);
/* Queue a relayout - so the stage will be allocated the new
* window size.
*
* Note also that when the stage gets allocated the new
* window size that will result in the stage's
* priv->viewport being changed, which will in turn result
* in the Cogl viewport changing when _clutter_do_redraw
* calls _clutter_stage_maybe_setup_viewport().
*/
clutter_actor_queue_relayout (actor);
return TRUE;
}