summaryrefslogtreecommitdiff
path: root/src/tests/wayland-test-clients/fractional-scale.c
blob: b46e63f31485862087786b852be34b2d3d0f555d (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
182
/*
 * Copyright (C) 2023 Collabora, Ltd.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <glib.h>
#include <wayland-client.h>

#include "wayland-test-client-utils.h"

static WaylandDisplay *display;
static struct wl_surface *surface;
static struct xdg_surface *xdg_surface;
static struct xdg_toplevel *xdg_toplevel;
static struct wp_viewport *viewport;
static struct wp_fractional_scale_v1 *fractional_scale_obj;

static gboolean running;
static gboolean waiting_for_configure;
static gboolean waiting_for_scale;
static uint32_t logical_width = 0;
static uint32_t logical_height = 0;
static float fractional_buffer_scale = 1.0;
static int sync_point = 0;

static void
handle_frame_callback (void               *data,
                       struct wl_callback *callback,
                       uint32_t            time)
{
  wl_callback_destroy (callback);
  test_driver_sync_point (display->test_driver, sync_point++, NULL);
}

static const struct wl_callback_listener frame_listener = {
  handle_frame_callback,
};

static void
maybe_redraw (void)
{
  struct wl_callback *callback;
  uint32_t buffer_width;
  uint32_t buffer_height;

  if (waiting_for_configure || waiting_for_scale)
    return;

  g_assert_cmpint (logical_width, >, 0);
  g_assert_cmpint (logical_height, >, 0);
  g_assert_cmpfloat (fractional_buffer_scale, >, 0.0);

  buffer_width = ceilf (logical_width * fractional_buffer_scale);
  buffer_height = ceilf (logical_height * fractional_buffer_scale);

  draw_surface (display, surface, buffer_width, buffer_height, 0x1f109f20);
  wp_viewport_set_destination (viewport, logical_width, logical_height);

  callback = wl_surface_frame (surface);
  wl_callback_add_listener (callback, &frame_listener, NULL);

  wl_surface_commit (surface);

  waiting_for_configure = TRUE;
  waiting_for_scale = TRUE;
}

static void
handle_xdg_toplevel_configure (void                *data,
                               struct xdg_toplevel *xdg_toplevel,
                               int32_t              width,
                               int32_t              height,
                               struct wl_array     *states)
{
  if (width > 0 && height > 0)
    {
      logical_width = width;
      logical_height = height;
      waiting_for_configure = TRUE;
    }
}

static void
handle_xdg_toplevel_close (void                *data,
                           struct xdg_toplevel *xdg_toplevel)
{
  running = FALSE;
}

static const struct xdg_toplevel_listener xdg_toplevel_listener = {
  handle_xdg_toplevel_configure,
  handle_xdg_toplevel_close,
};

static void
handle_xdg_surface_configure (void               *data,
                              struct xdg_surface *xdg_surface,
                              uint32_t            serial)
{
  xdg_surface_ack_configure (xdg_surface, serial);
  waiting_for_configure = FALSE;

  maybe_redraw ();
}

static const struct xdg_surface_listener xdg_surface_listener = {
  handle_xdg_surface_configure,
};

static void handle_preferred_scale (void                          *data,
                                    struct wp_fractional_scale_v1 *fractional_scale_obj,
                                    uint32_t                       wire_scale)
{
  float new_fractional_buffer_scale;

  new_fractional_buffer_scale = wire_scale / 120.0;
  if (G_APPROX_VALUE (new_fractional_buffer_scale,
                      fractional_buffer_scale,
                      FLT_EPSILON))
    return;

  fractional_buffer_scale = new_fractional_buffer_scale;
  waiting_for_scale = FALSE;
  maybe_redraw ();
}

static const struct wp_fractional_scale_v1_listener fractional_scale_listener = {
  .preferred_scale = handle_preferred_scale,
};

int
main (int    argc,
      char **argv)
{
  display = wayland_display_new (WAYLAND_DISPLAY_CAPABILITY_TEST_DRIVER);

  surface = wl_compositor_create_surface (display->compositor);
  xdg_surface = xdg_wm_base_get_xdg_surface (display->xdg_wm_base, surface);
  xdg_surface_add_listener (xdg_surface, &xdg_surface_listener, NULL);
  xdg_toplevel = xdg_surface_get_toplevel (xdg_surface);
  xdg_toplevel_add_listener (xdg_toplevel, &xdg_toplevel_listener, NULL);
  xdg_toplevel_set_title (xdg_toplevel, "fractional-scale");
  xdg_toplevel_set_fullscreen (xdg_toplevel, NULL);

  viewport = wp_viewporter_get_viewport (display->viewporter, surface);
  fractional_scale_obj =
    wp_fractional_scale_manager_v1_get_fractional_scale (display->fractional_scale_mgr,
                                                         surface);
  wp_fractional_scale_v1_add_listener (fractional_scale_obj,
                                       &fractional_scale_listener,
                                       NULL);

  wl_surface_commit (surface);

  waiting_for_configure = TRUE;
  waiting_for_scale = FALSE;

  running = TRUE;
  while (running)
    {
      if (wl_display_dispatch (display->display) == -1)
        return EXIT_FAILURE;
    }

  wl_display_roundtrip (display->display);

  return EXIT_SUCCESS;
}