summaryrefslogtreecommitdiff
path: root/src/backends/meta-screen-cast-area-stream.c
blob: 23d8c982800bf36ec35c1b7d9a20900eff382da1 (plain)
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
/*
 * Copyright (C) 2020 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 */

#include "config.h"

#include "backends/meta-screen-cast-area-stream.h"

#include "backends/meta-screen-cast-area-stream-src.h"

struct _MetaScreenCastAreaStream
{
  MetaScreenCastStream parent;

  ClutterStage *stage;

  MetaRectangle area;
  float scale;
};

G_DEFINE_TYPE (MetaScreenCastAreaStream,
               meta_screen_cast_area_stream,
               META_TYPE_SCREEN_CAST_STREAM)

ClutterStage *
meta_screen_cast_area_stream_get_stage (MetaScreenCastAreaStream *area_stream)
{
  return area_stream->stage;
}

MetaRectangle *
meta_screen_cast_area_stream_get_area (MetaScreenCastAreaStream *area_stream)
{
  return &area_stream->area;
}

float
meta_screen_cast_area_stream_get_scale (MetaScreenCastAreaStream *area_stream)
{
  return area_stream->scale;
}

static gboolean
calculate_scale (ClutterStage  *stage,
                 MetaRectangle *area,
                 float         *out_scale)
{
  GList *l;
  float scale = 0.0;

  for (l = clutter_stage_peek_stage_views (stage); l; l = l->next)
    {
      ClutterStageView *stage_view = l->data;
      MetaRectangle view_layout;

      clutter_stage_view_get_layout (stage_view, &view_layout);
      if (meta_rectangle_overlap (area, &view_layout))
        scale = MAX (clutter_stage_view_get_scale (stage_view), scale);
    }

  if (scale == 0.0)
    return FALSE;

  *out_scale = scale;
  return TRUE;
}

MetaScreenCastAreaStream *
meta_screen_cast_area_stream_new (MetaScreenCastSession     *session,
                                  GDBusConnection           *connection,
                                  MetaRectangle             *area,
                                  ClutterStage              *stage,
                                  MetaScreenCastCursorMode   cursor_mode,
                                  MetaScreenCastFlag         flags,
                                  GError                   **error)
{
  MetaScreenCastAreaStream *area_stream;
  float scale;

  if (!calculate_scale (stage, area, &scale))
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Area is off-screen");
      return NULL;
    }

  area_stream = g_initable_new (META_TYPE_SCREEN_CAST_AREA_STREAM,
                                NULL,
                                error,
                                "session", session,
                                "connection", connection,
                                "cursor-mode", cursor_mode,
                                "flags", flags,
                                NULL);
  if (!area_stream)
    return NULL;

  area_stream->area = *area;
  area_stream->scale = scale;
  area_stream->stage = stage;

  return area_stream;
}

static MetaScreenCastStreamSrc *
meta_screen_cast_area_stream_create_src (MetaScreenCastStream  *stream,
                                         GError               **error)
{
  MetaScreenCastAreaStream *area_stream =
    META_SCREEN_CAST_AREA_STREAM (stream);
  MetaScreenCastAreaStreamSrc *area_stream_src;

  area_stream_src = meta_screen_cast_area_stream_src_new (area_stream,
                                                          error);
  if (!area_stream_src)
    return NULL;

  return META_SCREEN_CAST_STREAM_SRC (area_stream_src);
}

static void
meta_screen_cast_area_stream_set_parameters (MetaScreenCastStream *stream,
                                             GVariantBuilder      *parameters_builder)
{
  MetaScreenCastAreaStream *area_stream =
    META_SCREEN_CAST_AREA_STREAM (stream);

  g_variant_builder_add (parameters_builder, "{sv}",
                         "size",
                         g_variant_new ("(ii)",
                                        area_stream->area.width,
                                        area_stream->area.height));
}

static gboolean
meta_screen_cast_area_stream_transform_position (MetaScreenCastStream *stream,
                                                 double                stream_x,
                                                 double                stream_y,
                                                 double               *x,
                                                 double               *y)
{
  MetaScreenCastAreaStream *area_stream =
    META_SCREEN_CAST_AREA_STREAM (stream);

  *x = area_stream->area.x + (int) roundf (stream_x / area_stream->scale);
  *y = area_stream->area.y + (int) roundf (stream_y / area_stream->scale);

  return TRUE;
}

static void
meta_screen_cast_area_stream_init (MetaScreenCastAreaStream *area_stream)
{
}

static void
meta_screen_cast_area_stream_class_init (MetaScreenCastAreaStreamClass *klass)
{
  MetaScreenCastStreamClass *stream_class =
    META_SCREEN_CAST_STREAM_CLASS (klass);

  stream_class->create_src = meta_screen_cast_area_stream_create_src;
  stream_class->set_parameters = meta_screen_cast_area_stream_set_parameters;
  stream_class->transform_position = meta_screen_cast_area_stream_transform_position;
}