File: clutter-stage-mir.c

package info (click to toggle)
clutter-1.0 1.26.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,352 kB
  • sloc: ansic: 128,533; sh: 5,580; xml: 1,641; makefile: 1,613; ruby: 149; perl: 142; sed: 16
file content (293 lines) | stat: -rw-r--r-- 9,546 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Copyright (C) 2014 Canonical Ltd.
 *
 * 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/>.
 *
 * Authors:
 *  Marco Trevisan <marco.trevisan@canonical.com>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "clutter-mir.h"
#include "clutter-stage-mir.h"
#include "clutter-backend-mir-priv.h"
#include "clutter-stage-private.h"
#include "clutter-mir.h"
#include <cogl/cogl.h>

#define clutter_stage_mir_get_type _clutter_stage_mir_get_type

static ClutterStageWindowIface *clutter_stage_window_parent_iface = NULL;

static void clutter_stage_window_iface_init (ClutterStageWindowIface *iface);
static void clutter_stage_mir_set_fullscreen (ClutterStageWindow *stage_window,
                                              gboolean            fullscreen);
static void clutter_stage_mir_set_cursor_visible (ClutterStageWindow *stage_window,
                                                  gboolean            cursor_visible);

G_DEFINE_TYPE_WITH_CODE (ClutterStageMir,
                         clutter_stage_mir,
                         CLUTTER_TYPE_STAGE_COGL,
                         G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_STAGE_WINDOW,
                                                clutter_stage_window_iface_init));

static void
on_stage_resized (CoglOnscreen *onscreen,
                  int width,
                  int height,
                  void *user_data)
{
  clutter_actor_set_size (CLUTTER_ACTOR (user_data), width, height);
}

static gboolean
clutter_stage_mir_realize (ClutterStageWindow *stage_window)
{
  ClutterStageMir *stage_mir = CLUTTER_STAGE_MIR (stage_window);
  ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window);
  MirSurface *mir_surface;

  if (!clutter_stage_window_parent_iface->realize (stage_window))
    return FALSE;

  cogl_framebuffer_allocate (COGL_FRAMEBUFFER (stage_cogl->onscreen), NULL);
  mir_surface = cogl_mir_onscreen_get_surface (stage_cogl->onscreen);

  if (!mir_surface_is_valid (mir_surface))
    {
      g_warning ("Realized Mir surface not valid");
      return FALSE;
    }

  if (!stage_mir->foreign_mir_surface)
    {
      cogl_onscreen_add_resize_callback (stage_cogl->onscreen, on_stage_resized,
                                         stage_cogl->wrapper, NULL);
    }

  if (stage_mir->surface_state == mir_surface_state_fullscreen)
    {
      clutter_stage_mir_set_fullscreen (stage_window, TRUE);
      stage_mir->surface_state = mir_surface_state_unknown;
    }

  if (!stage_mir->cursor_visible)
    {
      clutter_stage_mir_set_cursor_visible (stage_window, FALSE);
    }

  return TRUE;
}

static void
clutter_stage_mir_show (ClutterStageWindow *stage_window,
                        gboolean            do_raise)
{
  ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window);

  cogl_onscreen_show (stage_cogl->onscreen);
  clutter_actor_map (CLUTTER_ACTOR (stage_cogl->wrapper));
  clutter_actor_queue_redraw (CLUTTER_ACTOR (stage_cogl->wrapper));
}

static void
clutter_stage_mir_hide (ClutterStageWindow *stage_window)
{
  ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window);

  cogl_onscreen_hide (stage_cogl->onscreen);
  clutter_actor_unmap (CLUTTER_ACTOR (stage_cogl->wrapper));
  clutter_actor_queue_redraw (CLUTTER_ACTOR (stage_cogl->wrapper));
}

static void
clutter_stage_mir_set_cursor_visible (ClutterStageWindow *stage_window,
                                      gboolean            cursor_visible)
{
  ClutterStageMir *stage_mir = CLUTTER_STAGE_MIR (stage_window);
  ClutterActor *actor = _clutter_stage_window_get_wrapper (stage_window);
  MirSurface *surface = clutter_mir_stage_get_mir_surface ((ClutterStage *) actor);
  MirCursorConfiguration *cursor_conf;

  if (mir_surface_is_valid (surface))
    {
      cursor_conf = mir_cursor_configuration_from_name (cursor_visible ?
                                                        mir_default_cursor_name :
                                                        mir_disabled_cursor_name);
      mir_surface_configure_cursor (surface, cursor_conf);
      mir_cursor_configuration_destroy (cursor_conf);
    }

  stage_mir->cursor_visible = cursor_visible;
}

static void
clutter_stage_mir_set_fullscreen (ClutterStageWindow *stage_window,
                                  gboolean            fullscreen)
{
  ClutterStageMir *stage_mir = CLUTTER_STAGE_MIR (stage_window);
  ClutterActor *actor = _clutter_stage_window_get_wrapper (stage_window);
  MirSurface *surface = clutter_mir_stage_get_mir_surface ((ClutterStage *) actor);

  if (!mir_surface_is_valid (surface))
    {
      stage_mir->surface_state = fullscreen ?
                                 mir_surface_state_fullscreen :
                                 mir_surface_state_unknown;
    }
  else
    {
      if (fullscreen)
        {
          stage_mir->surface_state = mir_surface_get_state (surface);

          if (stage_mir->surface_state != mir_surface_state_fullscreen)
            mir_wait_for (mir_surface_set_state (surface,
                                                 mir_surface_state_fullscreen));
        }
      else if (mir_surface_get_state (surface) == mir_surface_state_fullscreen)
        {
          mir_wait_for (mir_surface_set_state (surface, stage_mir->surface_state));
        }
    }
}

static void
clutter_stage_mir_resize (ClutterStageWindow *stage_window,
                          gint                width,
                          gint                height)
{
  ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window);

  if (stage_cogl->onscreen)
    {
      cogl_mir_onscreen_resize (stage_cogl->onscreen, width, height);
      clutter_actor_queue_redraw (CLUTTER_ACTOR (stage_cogl->wrapper));
    }
}

static gboolean
clutter_stage_mir_can_clip_redraws (ClutterStageWindow *stage_window)
{
  return TRUE;
}

static void
clutter_stage_mir_init (ClutterStageMir *stage_mir)
{
  stage_mir->cursor_visible = TRUE;
  stage_mir->surface_state = mir_surface_state_unknown;
}

static void
clutter_stage_window_iface_init (ClutterStageWindowIface *iface)
{
  clutter_stage_window_parent_iface = g_type_interface_peek_parent (iface);

  iface->realize = clutter_stage_mir_realize;
  iface->show = clutter_stage_mir_show;
  iface->hide = clutter_stage_mir_hide;
  iface->set_fullscreen = clutter_stage_mir_set_fullscreen;
  iface->set_cursor_visible = clutter_stage_mir_set_cursor_visible;
  iface->resize = clutter_stage_mir_resize;
  iface->can_clip_redraws = clutter_stage_mir_can_clip_redraws;
}

static void
clutter_stage_mir_class_init (ClutterStageMirClass *klass)
{
}

/**
 * clutter_mir_stage_get_mir_surface: (skip)
 * @stage: a #ClutterStage
 *
 * Access the underlying data structure representing the surface that is
 * backing the #ClutterStage
 *
 * Note: this function can only be called when running on the Mir
 * platform. Calling this function at any other time will return %NULL.
 *
 * Returns: (transfer none): the Mir surface associated with @stage
 *
 * Since: 1.22
 */
MirSurface *
clutter_mir_stage_get_mir_surface (ClutterStage *stage)
{
  ClutterStageWindow *stage_window = _clutter_stage_get_window (stage);
  ClutterStageCogl *stage_cogl;

  if (!CLUTTER_IS_STAGE_COGL (stage_window))
    return NULL;

  stage_cogl = CLUTTER_STAGE_COGL (stage_window);

  if (!cogl_is_onscreen (stage_cogl->onscreen))
    return NULL;

  return cogl_mir_onscreen_get_surface (stage_cogl->onscreen);
}

/**
 * clutter_mir_stage_set_mir_surface:
 * @stage: a #ClutterStage
 * @surface: A Mir surface to associate with the @stage.
 *
 * Allows you to explicitly provide an existing Mir surface to associate
 * with @stage, preventing Cogl from allocating a surface and shell surface for
 * the stage automatically.
 *
 * This function must be called before @stage is shown.
 *
 * Note: this function can only be called when running on the Mir
 * platform. Calling this function at any other time has no effect.
 *
 * Since: 1.22
 */
void
clutter_mir_stage_set_mir_surface (ClutterStage *stage,
                                   MirSurface *surface)
{
  ClutterStageWindow *stage_window = _clutter_stage_get_window (stage);
  ClutterStageCogl *stage_cogl;

  if (!CLUTTER_IS_STAGE_MIR (stage_window))
    return;

  g_return_if_fail (mir_surface_is_valid (surface));

  stage_cogl = CLUTTER_STAGE_COGL (stage_window);

  if (stage_cogl->onscreen == NULL)
    {
      ClutterBackend *backend = clutter_get_default_backend ();

      /* Use the same default dimensions as clutter_stage_cogl_realize() */
      stage_cogl->onscreen = cogl_onscreen_new (backend->cogl_context,
                                                800, 600);

      cogl_mir_onscreen_set_foreign_surface (stage_cogl->onscreen, surface);
      CLUTTER_STAGE_MIR (stage_window)->foreign_mir_surface = TRUE;
    }
  else
    g_warning (G_STRLOC ": cannot set foreign surface for stage");
}