| 1 |
/* |
| 2 |
* Clutter. |
| 3 |
* |
| 4 |
* An OpenGL based 'interactive canvas' library. |
| 5 |
* |
| 6 |
* Copyright (C) 2014 Canonical Ltd. |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Lesser General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
* Authors: |
| 22 |
* Marco Trevisan <[email protected]> |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifdef HAVE_CONFIG_H |
| 26 |
#include "config.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "clutter-mir.h" |
| 30 |
#include "clutter-stage-mir.h" |
| 31 |
#include "clutter-backend-mir-priv.h" |
| 32 |
#include "clutter-stage-private.h" |
| 33 |
#include "clutter-mir.h" |
| 34 |
#include <cogl/cogl.h> |
| 35 |
|
| 36 |
#define clutter_stage_mir_get_type _clutter_stage_mir_get_type |
| 37 |
|
| 38 |
static ClutterStageWindowIface *clutter_stage_window_parent_iface = NULL; |
| 39 |
|
| 40 |
static void clutter_stage_window_iface_init (ClutterStageWindowIface *iface); |
| 41 |
static void clutter_stage_mir_set_fullscreen (ClutterStageWindow *stage_window, |
| 42 |
gboolean fullscreen); |
| 43 |
static void clutter_stage_mir_set_cursor_visible (ClutterStageWindow *stage_window, |
| 44 |
gboolean cursor_visible); |
| 45 |
|
| 46 |
G_DEFINE_TYPE_WITH_CODE (ClutterStageMir, |
| 47 |
clutter_stage_mir, |
| 48 |
CLUTTER_TYPE_STAGE_COGL, |
| 49 |
G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_STAGE_WINDOW, |
| 50 |
clutter_stage_window_iface_init)); |
| 51 |
|
| 52 |
static void |
| 53 |
on_stage_resized (CoglOnscreen *onscreen, |
| 54 |
int width, |
| 55 |
int height, |
| 56 |
void *user_data) |
| 57 |
{ |
| 58 |
clutter_actor_set_size (CLUTTER_ACTOR (user_data), width, height); |
| 59 |
} |
| 60 |
|
| 61 |
static gboolean |
| 62 |
clutter_stage_mir_realize (ClutterStageWindow *stage_window) |
| 63 |
{ |
| 64 |
ClutterStageMir *stage_mir = CLUTTER_STAGE_MIR (stage_window); |
| 65 |
ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window); |
| 66 |
MirSurface *mir_surface; |
| 67 |
|
| 68 |
if (!clutter_stage_window_parent_iface->realize (stage_window)) |
| 69 |
return FALSE; |
| 70 |
|
| 71 |
cogl_framebuffer_allocate (COGL_FRAMEBUFFER (stage_cogl->onscreen), NULL); |
| 72 |
mir_surface = cogl_mir_onscreen_get_surface (stage_cogl->onscreen); |
| 73 |
|
| 74 |
if (!mir_surface_is_valid (mir_surface)) |
| 75 |
{ |
| 76 |
g_warning ("Realized Mir surface not valid"); |
| 77 |
return FALSE; |
| 78 |
} |
| 79 |
|
| 80 |
if (!stage_mir->foreign_mir_surface) |
| 81 |
{ |
| 82 |
cogl_onscreen_add_resize_callback (stage_cogl->onscreen, on_stage_resized, |
| 83 |
stage_cogl->wrapper, NULL); |
| 84 |
} |
| 85 |
|
| 86 |
if (stage_mir->surface_state == mir_surface_state_fullscreen) |
| 87 |
{ |
| 88 |
clutter_stage_mir_set_fullscreen (stage_window, TRUE); |
| 89 |
stage_mir->surface_state = mir_surface_state_unknown; |
| 90 |
} |
| 91 |
|
| 92 |
if (!stage_mir->cursor_visible) |
| 93 |
{ |
| 94 |
clutter_stage_mir_set_cursor_visible (stage_window, FALSE); |
| 95 |
} |
| 96 |
|
| 97 |
return TRUE; |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
clutter_stage_mir_show (ClutterStageWindow *stage_window, |
| 102 |
gboolean do_raise) |
| 103 |
{ |
| 104 |
ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window); |
| 105 |
|
| 106 |
cogl_onscreen_show (stage_cogl->onscreen); |
| 107 |
clutter_actor_map (CLUTTER_ACTOR (stage_cogl->wrapper)); |
| 108 |
clutter_actor_queue_redraw (CLUTTER_ACTOR (stage_cogl->wrapper)); |
| 109 |
} |
| 110 |
|
| 111 |
static void |
| 112 |
clutter_stage_mir_hide (ClutterStageWindow *stage_window) |
| 113 |
{ |
| 114 |
ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window); |
| 115 |
|
| 116 |
cogl_onscreen_hide (stage_cogl->onscreen); |
| 117 |
clutter_actor_unmap (CLUTTER_ACTOR (stage_cogl->wrapper)); |
| 118 |
clutter_actor_queue_redraw (CLUTTER_ACTOR (stage_cogl->wrapper)); |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
clutter_stage_mir_set_cursor_visible (ClutterStageWindow *stage_window, |
| 123 |
gboolean cursor_visible) |
| 124 |
{ |
| 125 |
ClutterStageMir *stage_mir = CLUTTER_STAGE_MIR (stage_window); |
| 126 |
ClutterActor *actor = _clutter_stage_window_get_wrapper (stage_window); |
| 127 |
MirSurface *surface = clutter_mir_stage_get_mir_surface ((ClutterStage *) actor); |
| 128 |
MirCursorConfiguration *cursor_conf; |
| 129 |
|
| 130 |
if (mir_surface_is_valid (surface)) |
| 131 |
{ |
| 132 |
cursor_conf = mir_cursor_configuration_from_name (cursor_visible ? |
| 133 |
mir_default_cursor_name : |
| 134 |
mir_disabled_cursor_name); |
| 135 |
mir_surface_configure_cursor (surface, cursor_conf); |
| 136 |
mir_cursor_configuration_destroy (cursor_conf); |
| 137 |
} |
| 138 |
|
| 139 |
stage_mir->cursor_visible = cursor_visible; |
| 140 |
} |
| 141 |
|
| 142 |
static void |
| 143 |
clutter_stage_mir_set_fullscreen (ClutterStageWindow *stage_window, |
| 144 |
gboolean fullscreen) |
| 145 |
{ |
| 146 |
ClutterStageMir *stage_mir = CLUTTER_STAGE_MIR (stage_window); |
| 147 |
ClutterActor *actor = _clutter_stage_window_get_wrapper (stage_window); |
| 148 |
MirSurface *surface = clutter_mir_stage_get_mir_surface ((ClutterStage *) actor); |
| 149 |
|
| 150 |
if (!mir_surface_is_valid (surface)) |
| 151 |
{ |
| 152 |
stage_mir->surface_state = fullscreen ? |
| 153 |
mir_surface_state_fullscreen : |
| 154 |
mir_surface_state_unknown; |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
if (fullscreen) |
| 159 |
{ |
| 160 |
stage_mir->surface_state = mir_surface_get_state (surface); |
| 161 |
|
| 162 |
if (stage_mir->surface_state != mir_surface_state_fullscreen) |
| 163 |
mir_wait_for (mir_surface_set_state (surface, |
| 164 |
mir_surface_state_fullscreen)); |
| 165 |
} |
| 166 |
else if (mir_surface_get_state (surface) == mir_surface_state_fullscreen) |
| 167 |
{ |
| 168 |
mir_wait_for (mir_surface_set_state (surface, stage_mir->surface_state)); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
static void |
| 174 |
clutter_stage_mir_resize (ClutterStageWindow *stage_window, |
| 175 |
gint width, |
| 176 |
gint height) |
| 177 |
{ |
| 178 |
ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window); |
| 179 |
|
| 180 |
if (stage_cogl->onscreen) |
| 181 |
{ |
| 182 |
cogl_mir_onscreen_resize (stage_cogl->onscreen, width, height); |
| 183 |
clutter_actor_queue_redraw (CLUTTER_ACTOR (stage_cogl->wrapper)); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
static gboolean |
| 188 |
clutter_stage_mir_can_clip_redraws (ClutterStageWindow *stage_window) |
| 189 |
{ |
| 190 |
return TRUE; |
| 191 |
} |
| 192 |
|
| 193 |
static void |
| 194 |
clutter_stage_mir_init (ClutterStageMir *stage_mir) |
| 195 |
{ |
| 196 |
stage_mir->cursor_visible = TRUE; |
| 197 |
stage_mir->surface_state = mir_surface_state_unknown; |
| 198 |
} |
| 199 |
|
| 200 |
static void |
| 201 |
clutter_stage_window_iface_init (ClutterStageWindowIface *iface) |
| 202 |
{ |
| 203 |
clutter_stage_window_parent_iface = g_type_interface_peek_parent (iface); |
| 204 |
|
| 205 |
iface->realize = clutter_stage_mir_realize; |
| 206 |
iface->show = clutter_stage_mir_show; |
| 207 |
iface->hide = clutter_stage_mir_hide; |
| 208 |
iface->set_fullscreen = clutter_stage_mir_set_fullscreen; |
| 209 |
iface->set_cursor_visible = clutter_stage_mir_set_cursor_visible; |
| 210 |
iface->resize = clutter_stage_mir_resize; |
| 211 |
iface->can_clip_redraws = clutter_stage_mir_can_clip_redraws; |
| 212 |
} |
| 213 |
|
| 214 |
static void |
| 215 |
clutter_stage_mir_class_init (ClutterStageMirClass *klass) |
| 216 |
{ |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* clutter_mir_stage_get_mir_surface: (skip) |
| 221 |
* @stage: a #ClutterStage |
| 222 |
* |
| 223 |
* Access the underlying data structure representing the surface that is |
| 224 |
* backing the #ClutterStage |
| 225 |
* |
| 226 |
* Note: this function can only be called when running on the Mir |
| 227 |
* platform. Calling this function at any other time will return %NULL. |
| 228 |
* |
| 229 |
* Returns: (transfer none): the Mir surface associated with @stage |
| 230 |
* |
| 231 |
* Since: 1.22 |
| 232 |
*/ |
| 233 |
MirSurface * |
| 234 |
clutter_mir_stage_get_mir_surface (ClutterStage *stage) |
| 235 |
{ |
| 236 |
ClutterStageWindow *stage_window = _clutter_stage_get_window (stage); |
| 237 |
ClutterStageCogl *stage_cogl; |
| 238 |
|
| 239 |
if (!CLUTTER_IS_STAGE_COGL (stage_window)) |
| 240 |
return NULL; |
| 241 |
|
| 242 |
stage_cogl = CLUTTER_STAGE_COGL (stage_window); |
| 243 |
|
| 244 |
if (!cogl_is_onscreen (stage_cogl->onscreen)) |
| 245 |
return NULL; |
| 246 |
|
| 247 |
return cogl_mir_onscreen_get_surface (stage_cogl->onscreen); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* clutter_mir_stage_set_mir_surface: |
| 252 |
* @stage: a #ClutterStage |
| 253 |
* @surface: A Mir surface to associate with the @stage. |
| 254 |
* |
| 255 |
* Allows you to explicitly provide an existing Mir surface to associate |
| 256 |
* with @stage, preventing Cogl from allocating a surface and shell surface for |
| 257 |
* the stage automatically. |
| 258 |
* |
| 259 |
* This function must be called before @stage is shown. |
| 260 |
* |
| 261 |
* Note: this function can only be called when running on the Mir |
| 262 |
* platform. Calling this function at any other time has no effect. |
| 263 |
* |
| 264 |
* Since: 1.22 |
| 265 |
*/ |
| 266 |
void |
| 267 |
clutter_mir_stage_set_mir_surface (ClutterStage *stage, |
| 268 |
MirSurface *surface) |
| 269 |
{ |
| 270 |
ClutterStageWindow *stage_window = _clutter_stage_get_window (stage); |
| 271 |
ClutterStageCogl *stage_cogl; |
| 272 |
|
| 273 |
if (!CLUTTER_IS_STAGE_MIR (stage_window)) |
| 274 |
return; |
| 275 |
|
| 276 |
g_return_if_fail (mir_surface_is_valid (surface)); |
| 277 |
|
| 278 |
stage_cogl = CLUTTER_STAGE_COGL (stage_window); |
| 279 |
|
| 280 |
if (stage_cogl->onscreen == NULL) |
| 281 |
{ |
| 282 |
ClutterBackend *backend = clutter_get_default_backend (); |
| 283 |
|
| 284 |
/* Use the same default dimensions as clutter_stage_cogl_realize() */ |
| 285 |
stage_cogl->onscreen = cogl_onscreen_new (backend->cogl_context, |
| 286 |
800, 600); |
| 287 |
|
| 288 |
cogl_mir_onscreen_set_foreign_surface (stage_cogl->onscreen, surface); |
| 289 |
CLUTTER_STAGE_MIR (stage_window)->foreign_mir_surface = TRUE; |
| 290 |
} |
| 291 |
else |
| 292 |
g_warning (G_STRLOC ": cannot set foreign surface for stage"); |
| 293 |
} |