| 1 |
/* Clutter. |
| 2 |
* An OpenGL based 'interactive canvas' library. |
| 3 |
* Authored By Matthew Allum <[email protected]> |
| 4 |
* Copyright (C) 2006-2007 OpenedHand |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <math.h> |
| 25 |
|
| 26 |
#ifdef HAVE_UNISTD_H |
| 27 |
#include <unistd.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <cogl/cogl.h> |
| 31 |
|
| 32 |
#ifdef COGL_HAS_XLIB_SUPPORT |
| 33 |
#include <cogl/cogl-xlib.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#define GDK_DISABLE_DEPRECATION_WARNINGS |
| 37 |
|
| 38 |
#include <gdk/gdk.h> |
| 39 |
#ifdef GDK_WINDOWING_X11 |
| 40 |
#include <gdk/gdkx.h> |
| 41 |
#endif |
| 42 |
#ifdef GDK_WINDOWING_WAYLAND |
| 43 |
#include <gdk/gdkwayland.h> |
| 44 |
#endif |
| 45 |
#ifdef GDK_WINDOWING_WIN32 |
| 46 |
#include <gdk/gdkwin32.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
#include "clutter-backend-gdk.h" |
| 50 |
#include "clutter-stage-gdk.h" |
| 51 |
#include "clutter-gdk.h" |
| 52 |
|
| 53 |
#include "clutter-actor-private.h" |
| 54 |
#include "clutter-debug.h" |
| 55 |
#include "clutter-device-manager-private.h" |
| 56 |
#include "clutter-enum-types.h" |
| 57 |
#include "clutter-event-translator.h" |
| 58 |
#include "clutter-event-private.h" |
| 59 |
#include "clutter-feature.h" |
| 60 |
#include "clutter-main.h" |
| 61 |
#include "clutter-paint-volume-private.h" |
| 62 |
#include "clutter-private.h" |
| 63 |
#include "clutter-stage-private.h" |
| 64 |
|
| 65 |
static void clutter_stage_window_iface_init (ClutterStageWindowIface *iface); |
| 66 |
static int clutter_stage_gdk_get_scale_factor (ClutterStageWindow *stage_window); |
| 67 |
|
| 68 |
static ClutterStageWindowIface *clutter_stage_window_parent_iface = NULL; |
| 69 |
|
| 70 |
#define clutter_stage_gdk_get_type _clutter_stage_gdk_get_type |
| 71 |
|
| 72 |
G_DEFINE_TYPE_WITH_CODE (ClutterStageGdk, |
| 73 |
clutter_stage_gdk, |
| 74 |
CLUTTER_TYPE_STAGE_COGL, |
| 75 |
G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_STAGE_WINDOW, |
| 76 |
clutter_stage_window_iface_init)); |
| 77 |
|
| 78 |
#if defined(GDK_WINDOWING_X11) && defined(COGL_HAS_XLIB_SUPPORT) |
| 79 |
static void |
| 80 |
clutter_stage_gdk_update_foreign_event_mask (CoglOnscreen *onscreen, |
| 81 |
guint32 event_mask, |
| 82 |
void *user_data) |
| 83 |
{ |
| 84 |
ClutterStageGdk *stage_gdk = user_data; |
| 85 |
|
| 86 |
/* we assume that a GDK event mask is bitwise compatible with X11 |
| 87 |
event masks */ |
| 88 |
gdk_window_set_events (stage_gdk->window, event_mask | CLUTTER_STAGE_GDK_EVENT_MASK); |
| 89 |
} |
| 90 |
#endif |
| 91 |
|
| 92 |
static void |
| 93 |
clutter_stage_gdk_set_gdk_geometry (ClutterStageGdk *stage) |
| 94 |
{ |
| 95 |
GdkGeometry geometry; |
| 96 |
ClutterStage *wrapper = CLUTTER_STAGE_COGL (stage)->wrapper; |
| 97 |
gboolean resize = clutter_stage_get_user_resizable (wrapper); |
| 98 |
|
| 99 |
if (!resize) |
| 100 |
{ |
| 101 |
geometry.min_width = geometry.max_width = gdk_window_get_width (stage->window); |
| 102 |
geometry.min_height = geometry.max_height = gdk_window_get_height (stage->window); |
| 103 |
|
| 104 |
gdk_window_set_geometry_hints (stage->window, |
| 105 |
&geometry, |
| 106 |
GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE); |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
clutter_stage_get_minimum_size (wrapper, |
| 111 |
(guint *)&geometry.min_width, |
| 112 |
(guint *)&geometry.min_height); |
| 113 |
|
| 114 |
gdk_window_set_geometry_hints (stage->window, |
| 115 |
&geometry, |
| 116 |
GDK_HINT_MIN_SIZE); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
static void |
| 121 |
clutter_stage_gdk_get_geometry (ClutterStageWindow *stage_window, |
| 122 |
cairo_rectangle_int_t *geometry) |
| 123 |
{ |
| 124 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 125 |
|
| 126 |
geometry->x = geometry->y = 0; |
| 127 |
|
| 128 |
if (stage_gdk->window != NULL) |
| 129 |
{ |
| 130 |
geometry->width = gdk_window_get_width (stage_gdk->window); |
| 131 |
geometry->height = gdk_window_get_height (stage_gdk->window); |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
geometry->width = 800; |
| 136 |
geometry->height = 600; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
static void |
| 141 |
clutter_stage_gdk_resize (ClutterStageWindow *stage_window, |
| 142 |
gint width, |
| 143 |
gint height) |
| 144 |
{ |
| 145 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 146 |
|
| 147 |
if (width == 0 || height == 0) |
| 148 |
{ |
| 149 |
/* Should not happen, if this turns up we need to debug it and |
| 150 |
* determine the cleanest way to fix. |
| 151 |
*/ |
| 152 |
g_warning ("GDK stage not allowed to have 0 width or height"); |
| 153 |
width = 1; |
| 154 |
height = 1; |
| 155 |
} |
| 156 |
|
| 157 |
CLUTTER_NOTE (BACKEND, "New size received: (%d, %d)", width, height); |
| 158 |
|
| 159 |
/* No need to resize foreign windows, it should be handled by the |
| 160 |
* embedding framework, but on wayland we might need to resize our |
| 161 |
* own subsurface. |
| 162 |
*/ |
| 163 |
if (!stage_gdk->foreign_window) |
| 164 |
gdk_window_resize (stage_gdk->window, width, height); |
| 165 |
#if defined(GDK_WINDOWING_WAYLAND) && defined(COGL_HAS_EGL_PLATFORM_WAYLAND_SUPPORT) |
| 166 |
else if (GDK_IS_WAYLAND_WINDOW (stage_gdk->window)) |
| 167 |
{ |
| 168 |
int scale = clutter_stage_gdk_get_scale_factor (CLUTTER_STAGE_WINDOW (stage_gdk)); |
| 169 |
cogl_wayland_onscreen_resize (CLUTTER_STAGE_COGL (stage_gdk)->onscreen, |
| 170 |
width * scale, height * scale, 0, 0); |
| 171 |
} |
| 172 |
#endif |
| 173 |
} |
| 174 |
|
| 175 |
static void |
| 176 |
clutter_stage_gdk_unrealize (ClutterStageWindow *stage_window) |
| 177 |
{ |
| 178 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 179 |
|
| 180 |
if (stage_gdk->window != NULL) |
| 181 |
{ |
| 182 |
g_object_set_data (G_OBJECT (stage_gdk->window), |
| 183 |
"clutter-stage-window", NULL); |
| 184 |
|
| 185 |
if (stage_gdk->foreign_window) |
| 186 |
{ |
| 187 |
ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window); |
| 188 |
|
| 189 |
g_object_unref (stage_gdk->window); |
| 190 |
|
| 191 |
/* Clutter still uses part of the deprecated stateful API of |
| 192 |
* Cogl (in particulart cogl_set_framebuffer). It means Cogl |
| 193 |
* can keep an internal reference to the onscreen object we |
| 194 |
* rendered to. In the case of foreign window, we want to |
| 195 |
* avoid this, as we don't know what's going to happen to |
| 196 |
* that window. |
| 197 |
* |
| 198 |
* The following call sets the current Cogl framebuffer to a |
| 199 |
* dummy 1x1 one if we're unrealizing the current one, so |
| 200 |
* Cogl doesn't keep any reference to the foreign window. |
| 201 |
*/ |
| 202 |
if (cogl_get_draw_framebuffer () == COGL_FRAMEBUFFER (stage_cogl->onscreen)) |
| 203 |
_clutter_backend_reset_cogl_framebuffer (stage_cogl->backend); |
| 204 |
} |
| 205 |
else |
| 206 |
gdk_window_destroy (stage_gdk->window); |
| 207 |
|
| 208 |
stage_gdk->window = NULL; |
| 209 |
} |
| 210 |
|
| 211 |
#if defined(GDK_WINDOWING_WAYLAND) && defined(COGL_HAS_EGL_PLATFORM_WAYLAND_SUPPORT) |
| 212 |
/* Destroy and clear subsurface as well */ |
| 213 |
if (GDK_IS_WAYLAND_WINDOW (stage_gdk->subsurface)) |
| 214 |
{ |
| 215 |
gdk_window_destroy (stage_gdk->subsurface); |
| 216 |
stage_gdk->subsurface = NULL; |
| 217 |
} |
| 218 |
#endif |
| 219 |
|
| 220 |
clutter_stage_window_parent_iface->unrealize (stage_window); |
| 221 |
} |
| 222 |
|
| 223 |
#if defined(GDK_WINDOWING_WAYLAND) |
| 224 |
static struct wl_surface * |
| 225 |
clutter_stage_gdk_wayland_surface (ClutterStageGdk *stage_gdk) |
| 226 |
{ |
| 227 |
cairo_region_t *input_region; |
| 228 |
cairo_rectangle_int_t empty_rect = { 0, }; |
| 229 |
GdkWindowAttr attributes; |
| 230 |
gint attributes_mask; |
| 231 |
|
| 232 |
if (!stage_gdk->foreign_window || |
| 233 |
gdk_window_get_window_type (stage_gdk->window) != GDK_WINDOW_CHILD) |
| 234 |
{ |
| 235 |
struct wl_surface *surface; |
| 236 |
|
| 237 |
surface = gdk_wayland_window_get_wl_surface (stage_gdk->window); |
| 238 |
wl_surface_set_buffer_scale (surface, |
| 239 |
gdk_window_get_scale_factor (stage_gdk->window)); |
| 240 |
return surface; |
| 241 |
} |
| 242 |
|
| 243 |
if (stage_gdk->subsurface) |
| 244 |
goto out; |
| 245 |
|
| 246 |
/* On Wayland if we render to a foreign window, we setup our own |
| 247 |
* surface to not render in the same buffers as the embedding |
| 248 |
* framework. |
| 249 |
*/ |
| 250 |
attributes.window_type = GDK_WINDOW_SUBSURFACE; |
| 251 |
attributes.wclass = GDK_INPUT_OUTPUT; |
| 252 |
attributes.x = 0; |
| 253 |
attributes.y = 0; |
| 254 |
attributes_mask = GDK_WA_X | GDK_WA_Y; |
| 255 |
|
| 256 |
stage_gdk->subsurface = gdk_window_new (stage_gdk->window, &attributes, attributes_mask); |
| 257 |
gdk_window_set_transient_for (stage_gdk->subsurface, stage_gdk->window); |
| 258 |
|
| 259 |
/* Since we run inside GDK, we can let the embedding framework |
| 260 |
* dispatch the events to Clutter. For that to happen we need to |
| 261 |
* disable input on our surface. */ |
| 262 |
input_region = cairo_region_create_rectangle (&empty_rect); |
| 263 |
gdk_window_input_shape_combine_region (stage_gdk->subsurface, input_region, 0, 0); |
| 264 |
cairo_region_destroy (input_region); |
| 265 |
|
| 266 |
out: |
| 267 |
return gdk_wayland_window_get_wl_surface (stage_gdk->subsurface); |
| 268 |
} |
| 269 |
#endif |
| 270 |
|
| 271 |
void |
| 272 |
_clutter_stage_gdk_notify_configure (ClutterStageGdk *stage_gdk, |
| 273 |
gint x, |
| 274 |
gint y, |
| 275 |
gint width, |
| 276 |
gint height) |
| 277 |
{ |
| 278 |
if (x < 0 || y < 0 || width < 1 || height < 1) |
| 279 |
return; |
| 280 |
|
| 281 |
if (stage_gdk->foreign_window) |
| 282 |
{ |
| 283 |
ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_gdk); |
| 284 |
int scale = clutter_stage_gdk_get_scale_factor (CLUTTER_STAGE_WINDOW (stage_gdk)); |
| 285 |
|
| 286 |
#if defined(GDK_WINDOWING_WAYLAND) && defined(COGL_HAS_EGL_PLATFORM_WAYLAND_SUPPORT) |
| 287 |
if (GDK_IS_WAYLAND_WINDOW (stage_gdk->window) && |
| 288 |
gdk_window_get_window_type (stage_gdk->window) == GDK_WINDOW_CHILD && |
| 289 |
stage_gdk->subsurface) |
| 290 |
{ |
| 291 |
cogl_wayland_onscreen_resize (stage_cogl->onscreen, |
| 292 |
width * scale, height * scale, 0, 0); |
| 293 |
} |
| 294 |
else |
| 295 |
#endif |
| 296 |
#if defined(GDK_WINDOWING_X11) && defined(COGL_HAS_XLIB_SUPPORT) |
| 297 |
if (GDK_IS_X11_WINDOW (stage_gdk->window)) |
| 298 |
{ |
| 299 |
ClutterBackend *backend = CLUTTER_BACKEND (stage_cogl->backend); |
| 300 |
XConfigureEvent xevent = { ConfigureNotify }; |
| 301 |
xevent.window = GDK_WINDOW_XID (stage_gdk->window); |
| 302 |
xevent.width = width * scale; |
| 303 |
xevent.height = height * scale; |
| 304 |
|
| 305 |
/* Ensure cogl knows about the new size immediately, as we will |
| 306 |
* draw before we get the ConfigureNotify response. */ |
| 307 |
cogl_xlib_renderer_handle_event (backend->cogl_renderer, (XEvent *)&xevent); |
| 308 |
} |
| 309 |
else |
| 310 |
#endif |
| 311 |
{ |
| 312 |
/* Currently we only support X11 and Wayland. */ |
| 313 |
g_assert_not_reached(); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
static gboolean |
| 319 |
clutter_stage_gdk_realize (ClutterStageWindow *stage_window) |
| 320 |
{ |
| 321 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 322 |
ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window); |
| 323 |
ClutterBackend *backend = CLUTTER_BACKEND (stage_cogl->backend); |
| 324 |
ClutterBackendGdk *backend_gdk = CLUTTER_BACKEND_GDK (backend); |
| 325 |
GdkWindowAttr attributes; |
| 326 |
gboolean cursor_visible; |
| 327 |
gboolean use_alpha; |
| 328 |
gfloat width, height; |
| 329 |
int scale; |
| 330 |
|
| 331 |
if (backend->cogl_context == NULL) |
| 332 |
{ |
| 333 |
g_warning ("Missing Cogl context: was Clutter correctly initialized?"); |
| 334 |
return FALSE; |
| 335 |
} |
| 336 |
|
| 337 |
if (!stage_gdk->foreign_window) |
| 338 |
{ |
| 339 |
if (stage_gdk->window != NULL) |
| 340 |
{ |
| 341 |
/* complete realizing the stage */ |
| 342 |
cairo_rectangle_int_t geometry; |
| 343 |
|
| 344 |
clutter_stage_gdk_get_geometry (stage_window, &geometry); |
| 345 |
clutter_actor_set_size (CLUTTER_ACTOR (stage_cogl->wrapper), |
| 346 |
geometry.width, |
| 347 |
geometry.height); |
| 348 |
|
| 349 |
gdk_window_ensure_native (stage_gdk->window); |
| 350 |
gdk_window_set_events (stage_gdk->window, CLUTTER_STAGE_GDK_EVENT_MASK); |
| 351 |
|
| 352 |
return TRUE; |
| 353 |
} |
| 354 |
else |
| 355 |
{ |
| 356 |
attributes.title = NULL; |
| 357 |
g_object_get (stage_cogl->wrapper, |
| 358 |
"cursor-visible", &cursor_visible, |
| 359 |
"title", &attributes.title, |
| 360 |
"width", &width, |
| 361 |
"height", &height, |
| 362 |
"use-alpha", &use_alpha, |
| 363 |
NULL); |
| 364 |
|
| 365 |
attributes.width = width; |
| 366 |
attributes.height = height; |
| 367 |
attributes.wclass = GDK_INPUT_OUTPUT; |
| 368 |
attributes.window_type = GDK_WINDOW_TOPLEVEL; |
| 369 |
attributes.event_mask = CLUTTER_STAGE_GDK_EVENT_MASK; |
| 370 |
|
| 371 |
attributes.cursor = NULL; |
| 372 |
if (!cursor_visible) |
| 373 |
{ |
| 374 |
if (stage_gdk->blank_cursor == NULL) |
| 375 |
stage_gdk->blank_cursor = gdk_cursor_new_for_display (backend_gdk->display, GDK_BLANK_CURSOR); |
| 376 |
|
| 377 |
attributes.cursor = stage_gdk->blank_cursor; |
| 378 |
} |
| 379 |
|
| 380 |
/* If the ClutterStage:use-alpha is set, but GDK does not have an |
| 381 |
* RGBA visual, then we unset the property on the Stage |
| 382 |
*/ |
| 383 |
if (use_alpha) |
| 384 |
{ |
| 385 |
if (gdk_screen_get_rgba_visual (backend_gdk->screen) == NULL) |
| 386 |
{ |
| 387 |
clutter_stage_set_use_alpha (stage_cogl->wrapper, FALSE); |
| 388 |
use_alpha = FALSE; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
#if defined(GDK_WINDOWING_X11) && defined(COGL_HAS_XLIB_SUPPORT) |
| 393 |
if (GDK_IS_X11_DISPLAY (backend_gdk->display)) |
| 394 |
{ |
| 395 |
XVisualInfo *xvisinfo = cogl_clutter_winsys_xlib_get_visual_info (); |
| 396 |
if (xvisinfo != NULL) |
| 397 |
{ |
| 398 |
attributes.visual = gdk_x11_screen_lookup_visual (backend_gdk->screen, |
| 399 |
xvisinfo->visualid); |
| 400 |
} |
| 401 |
} |
| 402 |
else |
| 403 |
#endif |
| 404 |
{ |
| 405 |
attributes.visual = use_alpha |
| 406 |
? gdk_screen_get_rgba_visual (backend_gdk->screen) |
| 407 |
: gdk_screen_get_system_visual (backend_gdk->screen); |
| 408 |
} |
| 409 |
|
| 410 |
if (attributes.visual == NULL) |
| 411 |
{ |
| 412 |
/* This could still be an RGBA visual, although normally it's not */ |
| 413 |
attributes.visual = gdk_screen_get_system_visual (backend_gdk->screen); |
| 414 |
} |
| 415 |
|
| 416 |
stage_gdk->foreign_window = FALSE; |
| 417 |
stage_gdk->window = gdk_window_new (NULL, &attributes, |
| 418 |
GDK_WA_TITLE | GDK_WA_CURSOR | GDK_WA_VISUAL); |
| 419 |
|
| 420 |
g_free (attributes.title); |
| 421 |
} |
| 422 |
|
| 423 |
clutter_stage_gdk_set_gdk_geometry (stage_gdk); |
| 424 |
gdk_window_ensure_native (stage_gdk->window); |
| 425 |
} |
| 426 |
else |
| 427 |
{ |
| 428 |
width = gdk_window_get_width (stage_gdk->window); |
| 429 |
height = gdk_window_get_height (stage_gdk->window); |
| 430 |
} |
| 431 |
|
| 432 |
g_object_set_data (G_OBJECT (stage_gdk->window), "clutter-stage-window", stage_gdk); |
| 433 |
|
| 434 |
scale = clutter_stage_gdk_get_scale_factor (CLUTTER_STAGE_WINDOW (stage_gdk)); |
| 435 |
stage_cogl->onscreen = cogl_onscreen_new (backend->cogl_context, |
| 436 |
width * scale, height * scale); |
| 437 |
#if defined(GDK_WINDOWING_X11) && defined(COGL_HAS_XLIB_SUPPORT) |
| 438 |
if (GDK_IS_X11_WINDOW (stage_gdk->window)) |
| 439 |
{ |
| 440 |
cogl_x11_onscreen_set_foreign_window_xid (stage_cogl->onscreen, |
| 441 |
GDK_WINDOW_XID (stage_gdk->window), |
| 442 |
clutter_stage_gdk_update_foreign_event_mask, |
| 443 |
stage_gdk); |
| 444 |
} |
| 445 |
else |
| 446 |
#endif |
| 447 |
#if defined(GDK_WINDOWING_WAYLAND) && defined(COGL_HAS_EGL_PLATFORM_WAYLAND_SUPPORT) |
| 448 |
if (GDK_IS_WAYLAND_WINDOW (stage_gdk->window)) |
| 449 |
{ |
| 450 |
cogl_wayland_onscreen_set_foreign_surface (stage_cogl->onscreen, |
| 451 |
clutter_stage_gdk_wayland_surface (stage_gdk)); |
| 452 |
} |
| 453 |
else |
| 454 |
#endif |
| 455 |
#if defined(GDK_WINDOWING_WIN32) && defined(COGL_HAS_WIN32_SUPPORT) |
| 456 |
if (GDK_IS_WIN32_WINDOW (stage_gdk->window)) |
| 457 |
{ |
| 458 |
cogl_win32_onscreen_set_foreign_window (stage_cogl->onscreen, |
| 459 |
gdk_win32_window_get_handle (stage_gdk->window)); |
| 460 |
} |
| 461 |
else |
| 462 |
#endif |
| 463 |
{ |
| 464 |
g_warning ("Cannot find an appropriate CoglWinsys for a " |
| 465 |
"GdkWindow of type %s", G_OBJECT_TYPE_NAME (stage_gdk->window)); |
| 466 |
|
| 467 |
cogl_object_unref (stage_cogl->onscreen); |
| 468 |
stage_cogl->onscreen = NULL; |
| 469 |
|
| 470 |
if (!stage_gdk->foreign_window) |
| 471 |
gdk_window_destroy (stage_gdk->window); |
| 472 |
|
| 473 |
stage_gdk->window = NULL; |
| 474 |
|
| 475 |
return FALSE; |
| 476 |
} |
| 477 |
|
| 478 |
return clutter_stage_window_parent_iface->realize (stage_window); |
| 479 |
} |
| 480 |
|
| 481 |
static void |
| 482 |
clutter_stage_gdk_set_fullscreen (ClutterStageWindow *stage_window, |
| 483 |
gboolean is_fullscreen) |
| 484 |
{ |
| 485 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 486 |
ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window); |
| 487 |
ClutterStage *stage = CLUTTER_STAGE_COGL (stage_window)->wrapper; |
| 488 |
gboolean swap_throttle; |
| 489 |
|
| 490 |
if (stage == NULL || CLUTTER_ACTOR_IN_DESTRUCTION (stage)) |
| 491 |
return; |
| 492 |
|
| 493 |
if (stage_gdk->window == NULL || stage_gdk->foreign_window) |
| 494 |
return; |
| 495 |
|
| 496 |
CLUTTER_NOTE (BACKEND, "%ssetting fullscreen", is_fullscreen ? "" : "un"); |
| 497 |
|
| 498 |
if (is_fullscreen) |
| 499 |
gdk_window_fullscreen (stage_gdk->window); |
| 500 |
else |
| 501 |
gdk_window_unfullscreen (stage_gdk->window); |
| 502 |
|
| 503 |
/* Full-screen stages are usually unredirected to improve performance |
| 504 |
* by avoiding a copy; when that happens, we need to turn back swap |
| 505 |
* throttling because we won't be managed by the compositor any more, |
| 506 |
*/ |
| 507 |
swap_throttle = is_fullscreen; |
| 508 |
|
| 509 |
#ifdef GDK_WINDOWING_WAYLAND |
| 510 |
{ |
| 511 |
/* Except on Wayland, where there's a deadlock due to both Cogl |
| 512 |
* and GDK attempting to consume the throttling event; see bug |
| 513 |
* https://bugzilla.gnome.org/show_bug.cgi?id=754671#c1 |
| 514 |
*/ |
| 515 |
GdkDisplay *display = clutter_gdk_get_default_display (); |
| 516 |
if (GDK_IS_WAYLAND_DISPLAY (display)) |
| 517 |
swap_throttle = FALSE; |
| 518 |
} |
| 519 |
#endif |
| 520 |
|
| 521 |
cogl_onscreen_set_swap_throttled (stage_cogl->onscreen, swap_throttle); |
| 522 |
} |
| 523 |
|
| 524 |
static void |
| 525 |
clutter_stage_gdk_set_cursor_visible (ClutterStageWindow *stage_window, |
| 526 |
gboolean cursor_visible) |
| 527 |
{ |
| 528 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 529 |
|
| 530 |
if (stage_gdk->window == NULL) |
| 531 |
return; |
| 532 |
|
| 533 |
if (cursor_visible) |
| 534 |
{ |
| 535 |
gdk_window_set_cursor (stage_gdk->window, NULL); |
| 536 |
} |
| 537 |
else |
| 538 |
{ |
| 539 |
if (stage_gdk->blank_cursor == NULL) |
| 540 |
{ |
| 541 |
GdkDisplay *display = clutter_gdk_get_default_display (); |
| 542 |
|
| 543 |
stage_gdk->blank_cursor = gdk_cursor_new_for_display (display, GDK_BLANK_CURSOR); |
| 544 |
} |
| 545 |
|
| 546 |
gdk_window_set_cursor (stage_gdk->window, stage_gdk->blank_cursor); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
static void |
| 551 |
clutter_stage_gdk_set_title (ClutterStageWindow *stage_window, |
| 552 |
const gchar *title) |
| 553 |
{ |
| 554 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 555 |
|
| 556 |
if (stage_gdk->window == NULL || stage_gdk->foreign_window) |
| 557 |
return; |
| 558 |
|
| 559 |
gdk_window_set_title (stage_gdk->window, title); |
| 560 |
} |
| 561 |
|
| 562 |
static void |
| 563 |
clutter_stage_gdk_set_user_resizable (ClutterStageWindow *stage_window, |
| 564 |
gboolean is_resizable) |
| 565 |
{ |
| 566 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 567 |
GdkWMFunction function; |
| 568 |
|
| 569 |
if (stage_gdk->window == NULL || stage_gdk->foreign_window) |
| 570 |
return; |
| 571 |
|
| 572 |
function = GDK_FUNC_MOVE | GDK_FUNC_MINIMIZE | GDK_FUNC_CLOSE; |
| 573 |
if (is_resizable) |
| 574 |
function |= GDK_FUNC_RESIZE | GDK_FUNC_MAXIMIZE; |
| 575 |
|
| 576 |
gdk_window_set_functions (stage_gdk->window, function); |
| 577 |
|
| 578 |
clutter_stage_gdk_set_gdk_geometry (stage_gdk); |
| 579 |
} |
| 580 |
|
| 581 |
static void |
| 582 |
clutter_stage_gdk_set_accept_focus (ClutterStageWindow *stage_window, |
| 583 |
gboolean accept_focus) |
| 584 |
{ |
| 585 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 586 |
|
| 587 |
if (stage_gdk->window == NULL || stage_gdk->foreign_window) |
| 588 |
return; |
| 589 |
|
| 590 |
gdk_window_set_accept_focus (stage_gdk->window, accept_focus); |
| 591 |
} |
| 592 |
|
| 593 |
static void |
| 594 |
clutter_stage_gdk_show (ClutterStageWindow *stage_window, |
| 595 |
gboolean do_raise) |
| 596 |
{ |
| 597 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 598 |
|
| 599 |
g_return_if_fail (stage_gdk->window != NULL); |
| 600 |
|
| 601 |
clutter_actor_map (CLUTTER_ACTOR (CLUTTER_STAGE_COGL (stage_gdk)->wrapper)); |
| 602 |
|
| 603 |
#if defined(GDK_WINDOWING_WAYLAND) && defined(COGL_HAS_EGL_PLATFORM_WAYLAND_SUPPORT) |
| 604 |
if (GDK_IS_WAYLAND_WINDOW (stage_gdk->subsurface)) |
| 605 |
gdk_window_show (stage_gdk->subsurface); |
| 606 |
#endif |
| 607 |
|
| 608 |
/* Foreign window should be shown by the embedding framework. */ |
| 609 |
if (!stage_gdk->foreign_window) |
| 610 |
{ |
| 611 |
if (do_raise) |
| 612 |
gdk_window_show (stage_gdk->window); |
| 613 |
else |
| 614 |
gdk_window_show_unraised (stage_gdk->window); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
static void |
| 619 |
clutter_stage_gdk_hide (ClutterStageWindow *stage_window) |
| 620 |
{ |
| 621 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 622 |
|
| 623 |
g_return_if_fail (stage_gdk->window != NULL); |
| 624 |
|
| 625 |
clutter_actor_unmap (CLUTTER_ACTOR (CLUTTER_STAGE_COGL (stage_gdk)->wrapper)); |
| 626 |
|
| 627 |
/* Foreign window should be hidden by the embedding framework. */ |
| 628 |
#if defined(GDK_WINDOWING_WAYLAND) && defined(COGL_HAS_EGL_PLATFORM_WAYLAND_SUPPORT) |
| 629 |
if (GDK_IS_WAYLAND_WINDOW (stage_gdk->subsurface)) |
| 630 |
gdk_window_hide (stage_gdk->subsurface); |
| 631 |
#endif |
| 632 |
|
| 633 |
if (!stage_gdk->foreign_window) |
| 634 |
gdk_window_hide (stage_gdk->window); |
| 635 |
} |
| 636 |
|
| 637 |
static gboolean |
| 638 |
clutter_stage_gdk_can_clip_redraws (ClutterStageWindow *stage_window) |
| 639 |
{ |
| 640 |
return TRUE; |
| 641 |
} |
| 642 |
|
| 643 |
static int |
| 644 |
clutter_stage_gdk_get_scale_factor (ClutterStageWindow *stage_window) |
| 645 |
{ |
| 646 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 647 |
|
| 648 |
if (stage_gdk->window == NULL) |
| 649 |
return 1; |
| 650 |
|
| 651 |
/* For Wayland using a subsurface, scaling is handled by gdk */ |
| 652 |
#if defined(GDK_WINDOWING_WAYLAND) && defined(COGL_HAS_EGL_PLATFORM_WAYLAND_SUPPORT) |
| 653 |
if (GDK_IS_WAYLAND_WINDOW (stage_gdk->subsurface)) |
| 654 |
return 1; |
| 655 |
else |
| 656 |
#endif |
| 657 |
return gdk_window_get_scale_factor (stage_gdk->window); |
| 658 |
} |
| 659 |
|
| 660 |
static void |
| 661 |
clutter_stage_gdk_redraw (ClutterStageWindow *stage_window) |
| 662 |
{ |
| 663 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 664 |
GdkFrameClock *clock; |
| 665 |
|
| 666 |
if (stage_gdk->window == NULL || |
| 667 |
(clock = gdk_window_get_frame_clock (stage_gdk->window)) == NULL) |
| 668 |
{ |
| 669 |
clutter_stage_window_parent_iface->redraw (stage_window); |
| 670 |
return; |
| 671 |
} |
| 672 |
|
| 673 |
gdk_frame_clock_begin_updating (clock); |
| 674 |
|
| 675 |
clutter_stage_window_parent_iface->redraw (stage_window); |
| 676 |
|
| 677 |
gdk_frame_clock_end_updating (clock); |
| 678 |
} |
| 679 |
|
| 680 |
static void |
| 681 |
clutter_stage_gdk_schedule_update (ClutterStageWindow *stage_window, |
| 682 |
gint sync_delay) |
| 683 |
{ |
| 684 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 685 |
GdkFrameClock *clock; |
| 686 |
|
| 687 |
if (stage_gdk->window == NULL || |
| 688 |
(clock = gdk_window_get_frame_clock (stage_gdk->window)) == NULL) |
| 689 |
{ |
| 690 |
clutter_stage_window_parent_iface->schedule_update (stage_window, sync_delay); |
| 691 |
return; |
| 692 |
} |
| 693 |
|
| 694 |
gdk_frame_clock_request_phase (clock, GDK_FRAME_CLOCK_PHASE_PAINT); |
| 695 |
|
| 696 |
clutter_stage_window_parent_iface->schedule_update (stage_window, sync_delay); |
| 697 |
} |
| 698 |
|
| 699 |
static gint64 |
| 700 |
clutter_stage_gdk_get_update_time (ClutterStageWindow *stage_window) |
| 701 |
{ |
| 702 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); |
| 703 |
GdkFrameClock *frame_clock; |
| 704 |
GdkFrameTimings *frame_timings; |
| 705 |
|
| 706 |
if (stage_gdk->window == NULL || |
| 707 |
(frame_clock = gdk_window_get_frame_clock (stage_gdk->window)) == NULL || |
| 708 |
(frame_timings = gdk_frame_clock_get_current_timings (frame_clock)) == NULL || |
| 709 |
!gdk_frame_timings_get_complete (frame_timings)) |
| 710 |
return -1; /* No data, indefinite */ |
| 711 |
|
| 712 |
return (gdk_frame_timings_get_presentation_time (frame_timings) + |
| 713 |
gdk_frame_timings_get_refresh_interval (frame_timings)); |
| 714 |
} |
| 715 |
|
| 716 |
static void |
| 717 |
clutter_stage_gdk_dispose (GObject *gobject) |
| 718 |
{ |
| 719 |
ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (gobject); |
| 720 |
|
| 721 |
#if defined(GDK_WINDOWING_WAYLAND) && defined(COGL_HAS_EGL_PLATFORM_WAYLAND_SUPPORT) |
| 722 |
if (GDK_IS_WAYLAND_WINDOW (stage_gdk->subsurface)) |
| 723 |
{ |
| 724 |
gdk_window_destroy (stage_gdk->subsurface); |
| 725 |
stage_gdk->subsurface = NULL; |
| 726 |
} |
| 727 |
#endif |
| 728 |
|
| 729 |
if (stage_gdk->window != NULL) |
| 730 |
{ |
| 731 |
g_object_set_data (G_OBJECT (stage_gdk->window), |
| 732 |
"clutter-stage-window", NULL); |
| 733 |
if (stage_gdk->foreign_window) |
| 734 |
g_object_unref (stage_gdk->window); |
| 735 |
else |
| 736 |
gdk_window_destroy (stage_gdk->window); |
| 737 |
stage_gdk->window = NULL; |
| 738 |
} |
| 739 |
|
| 740 |
if (stage_gdk->blank_cursor != NULL) |
| 741 |
{ |
| 742 |
g_object_unref (stage_gdk->blank_cursor); |
| 743 |
stage_gdk->blank_cursor = NULL; |
| 744 |
} |
| 745 |
|
| 746 |
G_OBJECT_CLASS (clutter_stage_gdk_parent_class)->dispose (gobject); |
| 747 |
} |
| 748 |
|
| 749 |
static void |
| 750 |
clutter_stage_gdk_class_init (ClutterStageGdkClass *klass) |
| 751 |
{ |
| 752 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 753 |
|
| 754 |
gobject_class->dispose = clutter_stage_gdk_dispose; |
| 755 |
} |
| 756 |
|
| 757 |
static void |
| 758 |
clutter_stage_gdk_init (ClutterStageGdk *stage) |
| 759 |
{ |
| 760 |
} |
| 761 |
|
| 762 |
static void |
| 763 |
clutter_stage_window_iface_init (ClutterStageWindowIface *iface) |
| 764 |
{ |
| 765 |
clutter_stage_window_parent_iface = g_type_interface_peek_parent (iface); |
| 766 |
|
| 767 |
iface->set_title = clutter_stage_gdk_set_title; |
| 768 |
iface->set_fullscreen = clutter_stage_gdk_set_fullscreen; |
| 769 |
iface->set_cursor_visible = clutter_stage_gdk_set_cursor_visible; |
| 770 |
iface->set_user_resizable = clutter_stage_gdk_set_user_resizable; |
| 771 |
iface->set_accept_focus = clutter_stage_gdk_set_accept_focus; |
| 772 |
iface->show = clutter_stage_gdk_show; |
| 773 |
iface->hide = clutter_stage_gdk_hide; |
| 774 |
iface->resize = clutter_stage_gdk_resize; |
| 775 |
iface->get_geometry = clutter_stage_gdk_get_geometry; |
| 776 |
iface->realize = clutter_stage_gdk_realize; |
| 777 |
iface->unrealize = clutter_stage_gdk_unrealize; |
| 778 |
iface->can_clip_redraws = clutter_stage_gdk_can_clip_redraws; |
| 779 |
iface->get_scale_factor = clutter_stage_gdk_get_scale_factor; |
| 780 |
|
| 781 |
iface->redraw = clutter_stage_gdk_redraw; |
| 782 |
iface->schedule_update = clutter_stage_gdk_schedule_update; |
| 783 |
iface->get_update_time = clutter_stage_gdk_get_update_time; |
| 784 |
} |
| 785 |
|
| 786 |
/** |
| 787 |
* clutter_gdk_get_stage_window: |
| 788 |
* @stage: a #ClutterStage |
| 789 |
* |
| 790 |
* Gets the stages GdkWindow. |
| 791 |
* |
| 792 |
* Return value: (transfer none): A GdkWindow* for the stage window. |
| 793 |
* |
| 794 |
* Since: 1.10 |
| 795 |
*/ |
| 796 |
GdkWindow * |
| 797 |
clutter_gdk_get_stage_window (ClutterStage *stage) |
| 798 |
{ |
| 799 |
ClutterStageWindow *impl; |
| 800 |
|
| 801 |
g_return_val_if_fail (CLUTTER_IS_STAGE (stage), NULL); |
| 802 |
|
| 803 |
impl = _clutter_stage_get_window (stage); |
| 804 |
if (!CLUTTER_IS_STAGE_GDK (impl)) |
| 805 |
{ |
| 806 |
g_critical ("The Clutter backend is not a GDK backend"); |
| 807 |
return NULL; |
| 808 |
} |
| 809 |
|
| 810 |
return CLUTTER_STAGE_GDK (impl)->window; |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* clutter_gdk_get_stage_from_window: |
| 815 |
* @window: a #GtkWindow |
| 816 |
* |
| 817 |
* Gets the stage for a particular X window. |
| 818 |
* |
| 819 |
* Return value: (transfer none): A #ClutterStage, or% NULL if a stage |
| 820 |
* does not exist for the window |
| 821 |
* |
| 822 |
* Since: 1.10 |
| 823 |
*/ |
| 824 |
ClutterStage * |
| 825 |
clutter_gdk_get_stage_from_window (GdkWindow *window) |
| 826 |
{ |
| 827 |
ClutterStageGdk *stage_gdk = g_object_get_data (G_OBJECT (window), "clutter-stage-window"); |
| 828 |
|
| 829 |
if (stage_gdk != NULL && CLUTTER_IS_STAGE_GDK (stage_gdk)) |
| 830 |
return CLUTTER_STAGE_COGL (stage_gdk)->wrapper; |
| 831 |
|
| 832 |
return NULL; |
| 833 |
} |
| 834 |
|
| 835 |
typedef struct |
| 836 |
{ |
| 837 |
ClutterStageGdk *stage_gdk; |
| 838 |
GdkWindow *window; |
| 839 |
} ForeignWindowClosure; |
| 840 |
|
| 841 |
static void |
| 842 |
set_foreign_window_callback (ClutterActor *actor, |
| 843 |
void *data) |
| 844 |
{ |
| 845 |
ForeignWindowClosure *closure = data; |
| 846 |
ClutterStageGdk *stage_gdk = closure->stage_gdk; |
| 847 |
|
| 848 |
stage_gdk->window = closure->window; |
| 849 |
stage_gdk->foreign_window = TRUE; |
| 850 |
|
| 851 |
/* calling this with the stage unrealized will unset the stage |
| 852 |
* from the GL context; once the stage is realized the GL context |
| 853 |
* will be set again |
| 854 |
*/ |
| 855 |
clutter_stage_ensure_current (CLUTTER_STAGE (actor)); |
| 856 |
} |
| 857 |
|
| 858 |
/** |
| 859 |
* clutter_gdk_set_stage_foreign: |
| 860 |
* @stage: a #ClutterStage |
| 861 |
* @window: an existing #GdkWindow |
| 862 |
* |
| 863 |
* Target the #ClutterStage to use an existing external #GdkWindow |
| 864 |
* |
| 865 |
* Return value: %TRUE if foreign window is valid |
| 866 |
* |
| 867 |
* Since: 1.10 |
| 868 |
*/ |
| 869 |
gboolean |
| 870 |
clutter_gdk_set_stage_foreign (ClutterStage *stage, |
| 871 |
GdkWindow *window) |
| 872 |
{ |
| 873 |
ForeignWindowClosure closure; |
| 874 |
ClutterStageGdk *stage_gdk; |
| 875 |
ClutterStageWindow *impl; |
| 876 |
ClutterActor *actor; |
| 877 |
|
| 878 |
g_return_val_if_fail (CLUTTER_IS_STAGE (stage), FALSE); |
| 879 |
g_return_val_if_fail (!CLUTTER_ACTOR_IN_DESTRUCTION (stage), FALSE); |
| 880 |
g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE); |
| 881 |
|
| 882 |
impl = _clutter_stage_get_window (stage); |
| 883 |
if (!CLUTTER_IS_STAGE_GDK (impl)) |
| 884 |
{ |
| 885 |
g_critical ("The Clutter backend is not a GDK backend"); |
| 886 |
return FALSE; |
| 887 |
} |
| 888 |
|
| 889 |
stage_gdk = CLUTTER_STAGE_GDK (impl); |
| 890 |
|
| 891 |
if (g_object_get_data (G_OBJECT (window), "clutter-stage-window") != NULL) |
| 892 |
{ |
| 893 |
g_critical ("The provided GdkWindow is already in use by another ClutterStage"); |
| 894 |
return FALSE; |
| 895 |
} |
| 896 |
|
| 897 |
closure.stage_gdk = stage_gdk; |
| 898 |
closure.window = g_object_ref (window); |
| 899 |
|
| 900 |
actor = CLUTTER_ACTOR (stage); |
| 901 |
|
| 902 |
_clutter_actor_rerealize (actor, |
| 903 |
set_foreign_window_callback, |
| 904 |
&closure); |
| 905 |
|
| 906 |
/* Queue a relayout - so the stage will be allocated the new |
| 907 |
* window size. |
| 908 |
* |
| 909 |
* Note also that when the stage gets allocated the new |
| 910 |
* window size that will result in the stage's |
| 911 |
* priv->viewport being changed, which will in turn result |
| 912 |
* in the Cogl viewport changing when _clutter_do_redraw |
| 913 |
* calls _clutter_stage_maybe_setup_viewport(). |
| 914 |
*/ |
| 915 |
clutter_actor_queue_relayout (actor); |
| 916 |
|
| 917 |
return TRUE; |
| 918 |
} |