blob: 5ffd3d283174735a37c49957619dee3cfa2e9118 [file] [log] [blame]
fei.dengf7a0cd32023-08-29 09:36:37 +00001/*
2 * Copyright (C) 2021 Amlogic Corporation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <cstring>
17#include "wstclient_wayland.h"
fei.dengb9a1a572023-09-13 01:33:57 +000018#include "ErrorCode.h"
fei.dengf7a0cd32023-08-29 09:36:37 +000019#include "Logger.h"
fei.dengb9a1a572023-09-13 01:33:57 +000020#include "wstclient_plugin.h"
fei.dengf7a0cd32023-08-29 09:36:37 +000021
22#ifndef MAX
23# define MAX(a,b) ((a) > (b)? (a) : (b))
24# define MIN(a,b) ((a) < (b)? (a) : (b))
25#endif
26
27#define WESTEROS_UNUSED(x) ((void)(x))
28
fei.dengb9a1a572023-09-13 01:33:57 +000029#define TAG "rlib:wstclient_wayland"
30
fei.dengf7a0cd32023-08-29 09:36:37 +000031#define DEFAULT_WINDOW_X (0)
32#define DEFAULT_WINDOW_Y (0)
33#define DEFAULT_WINDOW_WIDTH (1920)
34#define DEFAULT_WINDOW_HEIGHT (1080)
35//discuss with display team,outpu resolution is a fixed value(1920x1080)
36#define DEFAULT_OUTPUT_WIDTH (1920)
37#define DEFAULT_OUTPUT_HEIGHT (1080)
38
39#define AV_SYNC_SESSION_V_MONO 64
40
41enum
42{
43 ZOOM_NONE,
44 ZOOM_DIRECT,
45 ZOOM_NORMAL,
46 ZOOM_16_9_STRETCH,
47 ZOOM_4_3_PILLARBOX,
48 ZOOM_ZOOM,
49 ZOOM_GLOBAL
50};
51
52#define needBounds() (mForceAspectRatio || (mZoomMode != ZOOM_NONE))
53
54void WstClientWayland::shellSurfaceId(void *data,
55 struct wl_simple_shell *wl_simple_shell,
56 struct wl_surface *surface,
57 uint32_t surfaceId)
58{
59 WstClientWayland *self = static_cast<WstClientWayland *>(data);
60 self->mWlShellSurfaceId = surfaceId;
61 char name[32];
62 wl_fixed_t z, op;
63 WESTEROS_UNUSED(wl_simple_shell);
64 WESTEROS_UNUSED(surface);
65
66 sprintf( name, "westeros-surface-%x", surfaceId );
67 wl_simple_shell_set_name(self->mWlShell, surfaceId, name);
68 if ( (self->mWindowWidth == 0) || (self->mWindowHeight == 0) )
69 {
70 wl_simple_shell_set_visible(self->mWlShell, self->mWlShellSurfaceId, false);
71 }
72 else
73 {
74 wl_simple_shell_set_visible(self->mWlShell, self->mWlShellSurfaceId, true);
75 if (!self->mWlVpc)
76 {
77 wl_simple_shell_set_geometry(self->mWlShell, self->mWlShellSurfaceId, self->mWindowX, self->mWindowY, self->mWindowWidth, self->mWindowHeight );
78 }
79 }
80
81 z = wl_fixed_from_double(self->mZorder);
82 wl_simple_shell_set_zorder(self->mWlShell, self->mWlShellSurfaceId, z);
83 op = wl_fixed_from_double(self->mOpacity);
84 wl_simple_shell_set_opacity(self->mWlShell, self->mWlShellSurfaceId, op);
85 wl_simple_shell_get_status(self->mWlShell, self->mWlShellSurfaceId);
86
87 wl_display_flush(self->mWlDisplay);
88}
89
90void WstClientWayland::shellSurfaceCreated(void *data,
91 struct wl_simple_shell *wl_simple_shell,
92 uint32_t surfaceId,
93 const char *name)
94{
95 WESTEROS_UNUSED(data);
96 WESTEROS_UNUSED(wl_simple_shell);
97 WESTEROS_UNUSED(surfaceId);
98 WESTEROS_UNUSED(name);
99}
100
101void WstClientWayland::shellSurfaceDestroyed(void *data,
102 struct wl_simple_shell *wl_simple_shell,
103 uint32_t surfaceId,
104 const char *name)
105{
106 WESTEROS_UNUSED(data);
107 WESTEROS_UNUSED(wl_simple_shell);
108 WESTEROS_UNUSED(surfaceId);
109 WESTEROS_UNUSED(name);
110}
111
112void WstClientWayland::shellSurfaceStatus(void *data,
113 struct wl_simple_shell *wl_simple_shell,
114 uint32_t surfaceId,
115 const char *name,
116 uint32_t visible,
117 int32_t x,
118 int32_t y,
119 int32_t width,
120 int32_t height,
121 wl_fixed_t opacity,
122 wl_fixed_t zorder)
123{
124 WstClientWayland *self = static_cast<WstClientWayland *>(data);
125 WESTEROS_UNUSED(wl_simple_shell);
126 WESTEROS_UNUSED(surfaceId);
127 WESTEROS_UNUSED(name);
128 WESTEROS_UNUSED(x);
129 WESTEROS_UNUSED(y);
130 WESTEROS_UNUSED(width);
131 WESTEROS_UNUSED(height);
132
fei.dengb9a1a572023-09-13 01:33:57 +0000133 INFO(self->mLogCategory,"opacity: %d,zorder:%d", opacity,zorder);
fei.dengf7a0cd32023-08-29 09:36:37 +0000134 self->mWindowChange = true;
135 self->mOpacity = opacity;
136 self->mZorder = zorder;
137}
138
139void WstClientWayland::shellGetSurfacesDone(void *data, struct wl_simple_shell *wl_simple_shell)
140{
141 WESTEROS_UNUSED(data);
142 WESTEROS_UNUSED(wl_simple_shell);
143}
144
145static const struct wl_simple_shell_listener shellListener =
146{
147 WstClientWayland::shellSurfaceId,
148 WstClientWayland::shellSurfaceCreated,
149 WstClientWayland::shellSurfaceDestroyed,
150 WstClientWayland::shellSurfaceStatus,
151 WstClientWayland::shellGetSurfacesDone
152};
153
154void WstClientWayland::vpcVideoPathChange(void *data,
155 struct wl_vpc_surface *wl_vpc_surface,
156 uint32_t new_pathway )
157{
158 WESTEROS_UNUSED(wl_vpc_surface);
159 WstClientWayland *self = static_cast<WstClientWayland *>(data);
160
sheng.liua7baed72024-01-24 07:31:24 +0000161 INFO(self->mLogCategory,"new pathway: %d", new_pathway);
fei.dengf7a0cd32023-08-29 09:36:37 +0000162 self->setVideoPath(new_pathway == WL_VPC_SURFACE_PATHWAY_GRAPHICS);
163}
164
165void WstClientWayland::vpcVideoXformChange(void *data,
166 struct wl_vpc_surface *wl_vpc_surface,
167 int32_t x_translation,
168 int32_t y_translation,
169 uint32_t x_scale_num,
170 uint32_t x_scale_denom,
171 uint32_t y_scale_num,
172 uint32_t y_scale_denom,
173 uint32_t output_width,
174 uint32_t output_height)
175{
176 WESTEROS_UNUSED(wl_vpc_surface);
177 WstClientWayland *self = static_cast<WstClientWayland *>(data);
178
fei.dengb9a1a572023-09-13 01:33:57 +0000179 TRACE(self->mLogCategory,"x_trans:%d,y_trans:%d,x_scale_num:%d,x_scale_denom:%d,y_scale_num:%d,y_scale_denom:%d",\
fei.dengf7a0cd32023-08-29 09:36:37 +0000180 x_translation,y_translation,x_scale_num,x_scale_denom,y_scale_num,y_scale_denom);
fei.dengb9a1a572023-09-13 01:33:57 +0000181 TRACE(self->mLogCategory,"output_width:%d,output_height:%d",output_width,output_height);
fei.dengf7a0cd32023-08-29 09:36:37 +0000182 self->mTransX= x_translation;
183 self->mTransY= y_translation;
184 if ( x_scale_denom )
185 {
186 self->mScaleXNum= x_scale_num;
187 self->mScaleXDenom= x_scale_denom;
188 }
189 if ( y_scale_denom )
190 {
191 self->mScaleYNum = y_scale_num;
192 self->mScaleYDenom = y_scale_denom;
193 }
194 self->mOutputWidth= (int)output_width;
195 self->mOutputHeight= (int)output_height;
196
fei.dengb9a1a572023-09-13 01:33:57 +0000197 Tls::Mutex::Autolock _l(self->mMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +0000198 self->updateVideoPosition();
199}
200
201static const struct wl_vpc_surface_listener vpcListener= {
202 WstClientWayland::vpcVideoPathChange,
203 WstClientWayland::vpcVideoXformChange
204};
205
206void WstClientWayland::outputHandleGeometry( void *data,
207 struct wl_output *output,
208 int x,
209 int y,
210 int mmWidth,
211 int mmHeight,
212 int subPixel,
213 const char *make,
214 const char *model,
215 int transform )
216{
217 WESTEROS_UNUSED(data);
218 WESTEROS_UNUSED(output);
219 WESTEROS_UNUSED(x);
220 WESTEROS_UNUSED(y);
221 WESTEROS_UNUSED(mmWidth);
222 WESTEROS_UNUSED(mmHeight);
223 WESTEROS_UNUSED(subPixel);
224 WESTEROS_UNUSED(make);
225 WESTEROS_UNUSED(model);
226 WESTEROS_UNUSED(transform);
227}
228
229void WstClientWayland::outputHandleMode( void *data,
230 struct wl_output *output,
231 uint32_t flags,
232 int width,
233 int height,
234 int refreshRate )
235{
236 WstClientWayland *self = static_cast<WstClientWayland *>(data);
237
238 if ( flags & WL_OUTPUT_MODE_CURRENT )
239 {
240 //output resolution is a fixed value(1920x1080)
241 width = DEFAULT_OUTPUT_WIDTH;
242 height = DEFAULT_OUTPUT_HEIGHT;
fei.dengb9a1a572023-09-13 01:33:57 +0000243 Tls::Mutex::Autolock _l(self->mMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +0000244 self->mDisplayWidth = width;
245 self->mDisplayHeight = height;
sheng.liua7baed72024-01-24 07:31:24 +0000246 DEBUG(self->mLogCategory,"compositor sets window to (%dx%d)", width, height);
fei.dengf7a0cd32023-08-29 09:36:37 +0000247 if (!self->mWindowSet)
248 {
249 self->mWindowWidth = width;
250 self->mWindowHeight = height;
251 if (self->mWlVpcSurface)
252 {
fei.dengb9a1a572023-09-13 01:33:57 +0000253 TRACE(self->mLogCategory,"set window geometry:x:%d,y:%d,w:%d,h:%d",self->mWindowX,self->mWindowY,self->mWindowWidth,self->mWindowHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000254 wl_vpc_surface_set_geometry(self->mWlVpcSurface, self->mWindowX, self->mWindowY, self->mWindowWidth, self->mWindowHeight);
255 }
256 }
257 }
258}
259
260void WstClientWayland::outputHandleDone( void *data,
261 struct wl_output *output )
262{
263 WESTEROS_UNUSED(data);
264 WESTEROS_UNUSED(output);
265}
266
267void WstClientWayland::outputHandleScale( void *data,
268 struct wl_output *output,
269 int32_t scale )
270{
271 WESTEROS_UNUSED(data);
272 WESTEROS_UNUSED(output);
273 WESTEROS_UNUSED(scale);
274}
275
276static const struct wl_output_listener outputListener = {
277 WstClientWayland::outputHandleGeometry,
278 WstClientWayland::outputHandleMode,
279 WstClientWayland::outputHandleDone,
280 WstClientWayland::outputHandleScale
281};
282
283void WstClientWayland::sbFormat(void *data, struct wl_sb *wl_sb, uint32_t format)
284{
285 WstClientWayland *self = static_cast<WstClientWayland *>(data);
286 WESTEROS_UNUSED(wl_sb);
287 WESTEROS_UNUSED(data);
sheng.liua7baed72024-01-24 07:31:24 +0000288 TRACE(self->mLogCategory,"registry: sbFormat: %X", format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000289}
290
291static const struct wl_sb_listener sbListener = {
292 WstClientWayland::sbFormat
293};
294
295void
296WstClientWayland::registryHandleGlobal (void *data, struct wl_registry *registry,
297 uint32_t id, const char *interface, uint32_t version)
298{
299 WstClientWayland *self = static_cast<WstClientWayland *>(data);
fei.dengb9a1a572023-09-13 01:33:57 +0000300 TRACE(self->mLogCategory,"registryHandleGlobal,interface:%s,version:%d",interface,version);
fei.dengf7a0cd32023-08-29 09:36:37 +0000301
302 if (strcmp (interface, "wl_compositor") == 0) {
303 self->mWlCompositor = (struct wl_compositor *)wl_registry_bind (registry, id, &wl_compositor_interface, 1/*MIN (version, 3)*/);
304 wl_proxy_set_queue((struct wl_proxy*)self->mWlCompositor, self->mWlQueue);
fei.dengb9a1a572023-09-13 01:33:57 +0000305 TRACE(self->mLogCategory,"registry: compositor %p\n", (void*)self->mWlCompositor);
fei.dengf7a0cd32023-08-29 09:36:37 +0000306 } else if (strcmp (interface, "wl_simple_shell") == 0) {
307 self->mWlShell = (struct wl_simple_shell*)wl_registry_bind(registry, id, &wl_simple_shell_interface, 1);
308 wl_proxy_set_queue((struct wl_proxy*)self->mWlShell, self->mWlQueue);
309 wl_simple_shell_add_listener(self->mWlShell, &shellListener, (void *)self);
fei.dengb9a1a572023-09-13 01:33:57 +0000310 TRACE(self->mLogCategory,"registry: simple shell %p\n", (void*)self->mWlShell);
fei.dengf7a0cd32023-08-29 09:36:37 +0000311 } else if (strcmp (interface, "wl_vpc") == 0) {
312 self->mWlVpc = (struct wl_vpc*)wl_registry_bind(registry, id, &wl_vpc_interface, 1);
313 wl_proxy_set_queue((struct wl_proxy*)self->mWlVpc, self->mWlQueue);
fei.dengb9a1a572023-09-13 01:33:57 +0000314 TRACE(self->mLogCategory,"registry: vpc %p\n", (void*)self->mWlVpc);
fei.dengf7a0cd32023-08-29 09:36:37 +0000315 } else if (strcmp (interface, "wl_output") == 0) {
316 self->mWlOutput = (struct wl_output*)wl_registry_bind(registry, id, &wl_output_interface, 2);
317 wl_proxy_set_queue((struct wl_proxy*)self->mWlOutput, self->mWlQueue);
318 wl_output_add_listener(self->mWlOutput, &outputListener, (void *)self);
fei.dengb9a1a572023-09-13 01:33:57 +0000319 TRACE(self->mLogCategory,"registry: output %p\n", (void*)self->mWlOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +0000320 } else if (strcmp (interface, "wl_sb") == 0) {
321 self->mWlSb = (struct wl_sb*)wl_registry_bind(registry, id, &wl_sb_interface, version);
322 wl_proxy_set_queue((struct wl_proxy*)self->mWlSb, self->mWlQueue);
323 wl_sb_add_listener(self->mWlSb, &sbListener, (void *)self);
fei.dengb9a1a572023-09-13 01:33:57 +0000324 TRACE(self->mLogCategory,"registry: sb %p\n", (void*)self->mWlSb);
fei.dengf7a0cd32023-08-29 09:36:37 +0000325 }
326}
327
328void
329WstClientWayland::registryHandleGlobalRemove (void *data, struct wl_registry *registry, uint32_t name)
330{
331 WstClientWayland *self = static_cast<WstClientWayland *>(data);
332 /* temporarily do nothing */
fei.dengb9a1a572023-09-13 01:33:57 +0000333 DEBUG(self->mLogCategory,"wayland display remove registry handle global");
fei.dengf7a0cd32023-08-29 09:36:37 +0000334}
335
336static const struct wl_registry_listener registry_listener = {
337 WstClientWayland::registryHandleGlobal,
338 WstClientWayland::registryHandleGlobalRemove
339};
340
fei.dengb9a1a572023-09-13 01:33:57 +0000341WstClientWayland::WstClientWayland(WstClientPlugin *plugin, int logCategory)
342 :mMutex("bufferMutex"),
343 mPlugin(plugin),
344 mLogCategory(logCategory)
fei.dengf7a0cd32023-08-29 09:36:37 +0000345{
fei.dengb9a1a572023-09-13 01:33:57 +0000346 TRACE(mLogCategory,"construct WstClientWayland");
fei.dengf7a0cd32023-08-29 09:36:37 +0000347 mWlDisplay = NULL;
348 mWlQueue = NULL;
349 mWlRegistry = NULL;
350 mWlCompositor = NULL;
351 mWlSurface = NULL;
352 mWlVpc = NULL;
353 mWlVpcSurface = NULL;
354 mWlOutput = NULL;
355 mWlShell = NULL;
356 mWlSb = NULL;
357 mDisplayWidth = DEFAULT_OUTPUT_WIDTH;
358 mDisplayHeight = DEFAULT_OUTPUT_HEIGHT;
359 mWindowX = DEFAULT_WINDOW_X;
360 mWindowY = DEFAULT_WINDOW_Y;
361 mWindowWidth = DEFAULT_WINDOW_WIDTH;
362 mWindowHeight = DEFAULT_WINDOW_HEIGHT;
363 mVideoX = mWindowX;
364 mVideoY = mWindowY;
365 mVideoWidth = mWindowWidth;
366 mVideoHeight = mWindowHeight;
367 mWlShellSurfaceId = 0;
368 mWindowChange = false;
369 mWindowSet = false;
370 mWindowSizeOverride = false;
371 mZoomMode = ZOOM_NONE;
372 mZoomModeGlobal = false;
373 mAllow4kZoom = false;
374 mFrameWidth = 0;
375 mFrameHeight = 0;
376 mForceAspectRatio = false;
377 mPixelAspectRatio = 1.0;
378 mScaleXDenom = 0;
379 mScaleYDenom = 0;
380 mForceFullScreen = false;
sheng.liua7baed72024-01-24 07:31:24 +0000381 mVideoPaused = false;
fei.dengb9a1a572023-09-13 01:33:57 +0000382 mPoll = new Tls::Poll(true);
fei.dengf7a0cd32023-08-29 09:36:37 +0000383}
384
385WstClientWayland::~WstClientWayland()
386{
fei.dengb9a1a572023-09-13 01:33:57 +0000387 TRACE(mLogCategory,"deconstruct WstClientWayland");
388 if (mPoll) {
389 delete mPoll;
390 mPoll = NULL;
391 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000392}
393
394int WstClientWayland::connectToWayland()
395{
396 const char *xdgEnv = NULL;
397 const char *displayEnv = NULL;
398 const char *displayName = "rlib-display";
399 const char *fullScreenEnV = NULL;
fei.dengb9a1a572023-09-13 01:33:57 +0000400 DEBUG(mLogCategory,"in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000401 xdgEnv = getenv("XDG_RUNTIME_DIR");
fei.dengb9a1a572023-09-13 01:33:57 +0000402 INFO(mLogCategory,"XDG_RUNTIME_DIR:%s",xdgEnv != NULL?xdgEnv:"NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +0000403 if (!xdgEnv) {
404 xdgEnv = "/run";
fei.dengb9a1a572023-09-13 01:33:57 +0000405 ERROR(mLogCategory,"XDG_RUNTIME_DIR is not set,set default %s",xdgEnv);
fei.dengf7a0cd32023-08-29 09:36:37 +0000406 }
407 displayEnv= getenv("WAYLAND_DISPLAY");
fei.dengb9a1a572023-09-13 01:33:57 +0000408 INFO(mLogCategory,"WAYLAND_DISPLAY:%s",displayEnv != NULL?displayEnv:"NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +0000409
410 fullScreenEnV= getenv("VIDEO_RENDER_FORCE_FULLSCREEN");
fei.dengb9a1a572023-09-13 01:33:57 +0000411 INFO(mLogCategory,"VIDEO_RENDER_FORCE_FULLSCREEN:%s",fullScreenEnV != NULL?fullScreenEnV:"NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +0000412 if (fullScreenEnV) {
413 int force = atoi(fullScreenEnV);
414 mForceFullScreen = force != 0? true: false;
fei.dengb9a1a572023-09-13 01:33:57 +0000415 INFO(mLogCategory,"VIDEO_RENDER_FORCE_FULLSCREEN:%d",mForceFullScreen);
fei.dengf7a0cd32023-08-29 09:36:37 +0000416 }
417
418 mWlDisplay = wl_display_connect(NULL);
419 if (!mWlDisplay) {
420 char waylandDisplay[64];
fei.dengb9a1a572023-09-13 01:33:57 +0000421 ERROR(mLogCategory,"Failed connect to default wayland display");
fei.dengf7a0cd32023-08-29 09:36:37 +0000422 memset(waylandDisplay, 0, 64);
423 strcpy(waylandDisplay, xdgEnv);
424 strcat(waylandDisplay, "/" );
425 strcat(waylandDisplay, displayName);
fei.dengb9a1a572023-09-13 01:33:57 +0000426 INFO(mLogCategory,"detect rlib wayland display %s",waylandDisplay);
fei.dengf7a0cd32023-08-29 09:36:37 +0000427 if (access(waylandDisplay,F_OK) == 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000428 INFO(mLogCategory,"try to connect to %s",waylandDisplay);
fei.dengf7a0cd32023-08-29 09:36:37 +0000429 mWlDisplay = wl_display_connect(displayName);
430 }
431 /*try to create wayland display my self*/
432 if (!mWlDisplay && xdgEnv) {
fei.dengb9a1a572023-09-13 01:33:57 +0000433 WARNING(mLogCategory,"try to create and connect rlib-display display");
fei.dengf7a0cd32023-08-29 09:36:37 +0000434 std::string cmdCreateDisplay = R"(curl 'http://127.0.0.1:9998/jsonrpc' -d '{"jsonrpc": "2.0","id": 4,"method":
435 "org.rdk.RDKShell.1.createDisplay","params": { "client": "rlib-display", "displayName": "rlib-display" }}';echo)";
436 std::string cmdMoveDisplay = R"(curl 'http://127.0.0.1:9998/jsonrpc' -d '{"jsonrpc": "2.0","id": 4,"method":
437 "org.rdk.RDKShell.1.moveToBack", "params": { "client": "rlib-display" }}';echo)";
438
439 executeCmd(cmdCreateDisplay.c_str());
440 executeCmd(cmdMoveDisplay.c_str());
441 //try to connect again
442 mWlDisplay = wl_display_connect(displayName);
443 }
444
445 if (!mWlDisplay) {
fei.dengb9a1a572023-09-13 01:33:57 +0000446 ERROR(mLogCategory, "wayland connect rlib-display fail");
447 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000448 }
fei.dengb9a1a572023-09-13 01:33:57 +0000449 INFO(mLogCategory,"wayland connected to rlib-display");
fei.dengf7a0cd32023-08-29 09:36:37 +0000450 }
451
452 mWlQueue = wl_display_create_queue (mWlDisplay);
453 wl_proxy_set_queue ((struct wl_proxy *)mWlDisplay, mWlQueue);
454
455 mWlRegistry = wl_display_get_registry (mWlDisplay);
456 wl_registry_add_listener (mWlRegistry, &registry_listener, (void *)this);
457
458 /* we need exactly 2 roundtrips to discover global objects and their state */
459 for (int i = 0; i < 2; i++) {
460 if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000461 ERROR(mLogCategory,"Error communicating with the wayland display");
462 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000463 }
464 }
465
466 //create surface from compositor
467 if (mWlCompositor) {
468 mWlSurface = wl_compositor_create_surface(mWlCompositor);
469 wl_proxy_set_queue((struct wl_proxy*)mWlSurface, mWlQueue);
470 wl_display_flush( mWlDisplay );
471 }
472
473 if (mWlVpc && mWlSurface) {
474 mWlVpcSurface = wl_vpc_get_vpc_surface( mWlVpc, mWlSurface );
475 if (mWlVpcSurface) {
476 wl_vpc_surface_add_listener( mWlVpcSurface, &vpcListener, (void *)this);
477 wl_proxy_set_queue((struct wl_proxy*)mWlVpcSurface, mWlQueue);
478 wl_vpc_surface_set_geometry( mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight);
479 wl_display_flush(mWlDisplay);
480 }
481 }
482
fei.dengb9a1a572023-09-13 01:33:57 +0000483 INFO(mLogCategory,"out");
484 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000485}
486
487void WstClientWayland::disconnectFromWayland()
488{
fei.dengb9a1a572023-09-13 01:33:57 +0000489 INFO(mLogCategory,"in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000490
491 if (isRunning()) {
fei.dengb9a1a572023-09-13 01:33:57 +0000492 TRACE(mLogCategory,"try stop dispatch thread");
493 if (mPoll) {
494 mPoll->setFlushing(true);
495 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000496 requestExitAndWait();
497 }
498
499 if (mWlShell) {
500 wl_simple_shell_destroy(mWlShell);
501 mWlShell = NULL;
502 }
503
504 if (mWlVpcSurface) {
505 wl_vpc_surface_destroy(mWlVpcSurface);
506 mWlVpcSurface = NULL;
507 }
508
509 if (mWlVpc) {
510 wl_vpc_destroy(mWlVpc);
511 mWlVpc = NULL;
512 }
513
514 if (mWlSurface) {
515 wl_surface_destroy(mWlSurface);
516 mWlSurface = NULL;
517 }
518
519 if (mWlOutput) {
520 wl_output_destroy(mWlOutput);
521 mWlOutput = NULL;
522 }
523
524 if (mWlSb) {
525 wl_sb_destroy(mWlSb);
526 mWlSb = NULL;
527 }
528
529 if (mWlCompositor) {
530 wl_compositor_destroy (mWlCompositor);
531 mWlCompositor = NULL;
532 }
533
534 if (mWlRegistry) {
535 wl_registry_destroy (mWlRegistry);
536 mWlRegistry= NULL;
537 }
538
539 if (mWlQueue) {
540 wl_event_queue_destroy (mWlQueue);
541 mWlQueue = NULL;
542 }
543
544 if (mWlDisplay) {
545 wl_display_flush (mWlDisplay);
546 wl_display_disconnect (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +0000547 INFO(mLogCategory,"wl display disconnect");
fei.dengf7a0cd32023-08-29 09:36:37 +0000548 mWlDisplay = NULL;
549 }
550
fei.dengb9a1a572023-09-13 01:33:57 +0000551 INFO(mLogCategory,"out");
fei.dengf7a0cd32023-08-29 09:36:37 +0000552}
553
554void WstClientWayland::setWindowSize(int x, int y, int w, int h)
555{
556 if (x == 0 && y == 0 && w == 0 && h == 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000557 WARNING(mLogCategory, "set full screen? %dx%dx%dx%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +0000558 return;
559 }
560 mWindowX = x;
561 mWindowY = y;
562 mWindowWidth = w;
563 mWindowHeight = h;
564 mWindowChange = true;
565 mWindowSet = true;
566 mWindowSizeOverride = true;
fei.dengb9a1a572023-09-13 01:33:57 +0000567 DEBUG(mLogCategory,"set window size:x:%d,y:%d,w:%d,h:%d",x,y,w,h);
sheng.liua7baed72024-01-24 07:31:24 +0000568
fei.dengf7a0cd32023-08-29 09:36:37 +0000569 if (mWlVpcSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +0000570 DEBUG(mLogCategory, "wl_vpc_surface_set_geometry(%d,%d,%d,%d)",mWindowX,mWindowY,mWindowWidth,mWindowHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000571 wl_vpc_surface_set_geometry(mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight);
572 }
573 //if window size is updated, update video position
sheng.liua7baed72024-01-24 07:31:24 +0000574 if (mVideoPaused && mWlVpcSurface && mWindowChange && mScaleXDenom != 0 && mScaleYDenom != 0) {
fei.dengf7a0cd32023-08-29 09:36:37 +0000575 mWindowChange = false;
576 updateVideoPosition();
577 }
sheng.liua7baed72024-01-24 07:31:24 +0000578
579 if (mWlDisplay) {
580 wl_display_flush( mWlDisplay );
581 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000582}
583
584void WstClientWayland::setFrameSize(int frameWidth, int frameHeight) {
585 mFrameWidth = frameWidth;
586 mFrameHeight = frameHeight;
fei.dengb9a1a572023-09-13 01:33:57 +0000587 DEBUG(mLogCategory, "set frame size:%dx%d",mFrameWidth, mFrameHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000588}
589
590void WstClientWayland::setZoomMode(int zoomMode, bool globalZoomActive, bool allow4kZoom) {
fei.dengb9a1a572023-09-13 01:33:57 +0000591 DEBUG(mLogCategory, "zoomMode:%d, globalZoomActive:%d, allow4kZoom:%d",zoomMode,globalZoomActive,allow4kZoom);
fei.dengf7a0cd32023-08-29 09:36:37 +0000592 mZoomModeGlobal = globalZoomActive;
593 if ( !globalZoomActive )
594 {
595 mZoomMode = ZOOM_NONE;
596 }
597 mAllow4kZoom= allow4kZoom;
598 if ( mZoomModeGlobal == true )
599 {
600 if ( (zoomMode >= ZOOM_NONE) && (zoomMode <= ZOOM_ZOOM) )
601 {
602 mZoomMode = zoomMode;
603 mPixelAspectRatioChanged = true;
604 }
605 } else {
fei.dengb9a1a572023-09-13 01:33:57 +0000606 DEBUG(mLogCategory, "global zoom disabled: ignore server value");
fei.dengf7a0cd32023-08-29 09:36:37 +0000607 }
608}
609
610void WstClientWayland::getVideoBounds(int *x, int *y, int *w, int *h)
611{
612 int vx, vy, vw, vh;
613 int frameWidth, frameHeight;
614 int zoomMode;
615 double contentWidth, contentHeight;
616 double roix, roiy, roiw, roih;
617 double arf, ard;
618 double hfactor= 1.0, vfactor= 1.0;
619
620 //if wayland open fail,use the default window size
621 if (!mWlDisplay) {
622 *x = mWindowX;
623 *y = mWindowY;
624 *w = mWindowWidth;
625 *h = mWindowHeight;
fei.dengb9a1a572023-09-13 01:33:57 +0000626 TRACE(mLogCategory, "no wldisplay, %d,%d,%d,%d",mVideoX,mVideoY,mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000627 return;
628 }
629
630 //if window size is updated, update video position
631 if (mWlVpcSurface && mWindowChange) {
632 mWindowChange = false;
633 updateVideoPosition();
634 }
635
636 vx = mVideoX;
637 vy = mVideoY;
638 vw = mVideoWidth;
639 vh = mVideoHeight;
640
fei.dengb9a1a572023-09-13 01:33:57 +0000641 TRACE(mLogCategory, "videoX:%d,videoY:%d,videoW:%d,videoH:%d",mVideoX,mVideoY,mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000642 //if the window size is smaller than display size,we scall video to map window size
643 if (mWindowWidth != mDisplayWidth || mWindowHeight != mDisplayHeight) {
644 *x= vx;
645 *y= vy;
646 *w= vw;
647 *h= vh;
fei.dengb9a1a572023-09-13 01:33:57 +0000648 TRACE(mLogCategory, "small window vrect %d, %d, %d, %d", vx, vy, vw, vh);
fei.dengf7a0cd32023-08-29 09:36:37 +0000649 return;
650 }
651 if (mForceFullScreen) {
652 *x= vx;
653 *y= vy;
654 *w= vw;
655 *h= vh;
fei.dengb9a1a572023-09-13 01:33:57 +0000656 TRACE(mLogCategory, "force window vrect %d, %d, %d, %d", vx, vy, vw, vh);
fei.dengf7a0cd32023-08-29 09:36:37 +0000657 return;
658 }
659
660 frameWidth = mFrameWidth;
661 frameHeight = mFrameHeight;
662 contentWidth = frameWidth * mPixelAspectRatio;
663 contentHeight = frameHeight;
664
665 if (mPixelAspectRatioChanged)
fei.dengb9a1a572023-09-13 01:33:57 +0000666 DEBUG(mLogCategory,"pixelAspectRatio: %f zoom-mode %d", mPixelAspectRatio, mZoomMode);
fei.dengf7a0cd32023-08-29 09:36:37 +0000667
668 ard = (double)mVideoWidth/(double)mVideoHeight;
669 arf = (double)contentWidth/(double)contentHeight;
670
fei.dengb9a1a572023-09-13 01:33:57 +0000671 TRACE(mLogCategory, "frameWidth:%d,frameHeight:%d,contentWidth:%f,contentHeight:%f",frameWidth,frameHeight,contentWidth,contentHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000672
673 /* Establish region of interest */
674 roix = 0;
675 roiy = 0;
676 roiw = contentWidth;
677 roih = contentHeight;
678
679 zoomMode = mZoomMode;
680 if ((mFrameWidth > 1920) || (mFrameHeight > 1080))
681 {
682 zoomMode= ZOOM_NORMAL;
683 }
684 //if (mPixelAspectRatioChanged )
fei.dengb9a1a572023-09-13 01:33:57 +0000685 TRACE(mLogCategory, "ard %f arf %f", ard, arf);
fei.dengf7a0cd32023-08-29 09:36:37 +0000686 switch ( zoomMode )
687 {
688 case ZOOM_NORMAL:
689 {
690 if ( arf >= ard )
691 {
692 vw = mVideoWidth * (1.0);
693 vh = (roih * vw) / roiw;
694 vx = vx+(mVideoWidth-vw)/2;
695 vy = vy+(mVideoHeight-vh)/2;
696 }
697 else
698 {
699 vh = mVideoHeight * (1.0);
700 vw = (roiw * vh) / roih;
701 vx = vx+(mVideoWidth-vw)/2;
702 vy = vy+(mVideoHeight-vh)/2;
703 }
704 }
705 break;
706 case ZOOM_NONE:
707 case ZOOM_DIRECT:
708 {
709 if ( arf >= ard )
710 {
711 vh = (contentHeight * mVideoWidth) / contentWidth;
712 vy = vy+(mVideoHeight-vh)/2;
713 }
714 else
715 {
716 vw = (contentWidth * mVideoHeight) / contentHeight;
717 vx = vx+(mVideoWidth-vw)/2;
718 }
719 }
720 break;
721 case ZOOM_16_9_STRETCH:
722 {
723 if ( approxEqual(arf, ard) && approxEqual(arf, 1.777) )
724 {
725 /* For 16:9 content on a 16:9 display, stretch as though 4:3 */
726 hfactor= 4.0/3.0;
727 if (mPixelAspectRatioChanged )
fei.dengb9a1a572023-09-13 01:33:57 +0000728 DEBUG(mLogCategory, "stretch apply vfactor %f hfactor %f", vfactor, hfactor);
fei.dengf7a0cd32023-08-29 09:36:37 +0000729 }
730 vh = mVideoHeight * (1.0);
731 vw = vh*hfactor*16/9;
732 vx = vx+(mVideoWidth-vw)/2;
733 vy = vy+(mVideoHeight-vh)/2;
734 }
735 break;
736 case ZOOM_4_3_PILLARBOX:
737 {
738 vh = mVideoHeight * (1.0);
739 vw = vh*4/3;
740 vx = vx+(mVideoWidth-vw)/2;
741 vy = vy+(mVideoHeight-vh)/2;
742 }
743 break;
744 case ZOOM_ZOOM:
745 {
746 if ( arf >= ard )
747 {
748 if (approxEqual(arf, ard) && approxEqual( arf, 1.777) )
749 {
750 /* For 16:9 content on a 16:9 display, enlarge as though 4:3 */
751 vfactor= 4.0/3.0;
752 hfactor= 1.0;
753 if (mPixelAspectRatioChanged )
fei.dengb9a1a572023-09-13 01:33:57 +0000754 DEBUG(mLogCategory, "zoom apply vfactor %f hfactor %f", vfactor, hfactor);
fei.dengf7a0cd32023-08-29 09:36:37 +0000755 }
756 vh = mVideoHeight * vfactor * (1.0);
757 vw = (roiw * vh) * hfactor / roih;
758 vx = vx+(mVideoWidth-vw)/2;
759 vy = vy+(mVideoHeight-vh)/2;
760 }
761 else
762 {
763 vw = mVideoWidth * (1.0);
764 vh = (roih * vw) / roiw;
765 vx = vx+(mVideoWidth-vw)/2;
766 vy = vy+(mVideoHeight-vh)/2;
767 }
768 }
769 break;
770 }
fei.dengb9a1a572023-09-13 01:33:57 +0000771 if (mPixelAspectRatioChanged) DEBUG(mLogCategory, "vrect %d, %d, %d, %d", vx, vy, vw, vh);
fei.dengf7a0cd32023-08-29 09:36:37 +0000772 if (mPixelAspectRatioChanged)
773 {
774 if (mWlDisplay && mWlVpcSurface )
775 {
776 wl_vpc_surface_set_geometry(mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight);
777 wl_display_flush(mWlDisplay);
778 }
779 }
780 mPixelAspectRatioChanged = false;
781 *x= vx;
782 *y= vy;
783 *w= vw;
784 *h= vh;
fei.dengb9a1a572023-09-13 01:33:57 +0000785 TRACE(mLogCategory, "vrect %d, %d, %d, %d", vx, vy, vw, vh);
fei.dengf7a0cd32023-08-29 09:36:37 +0000786}
787
788void WstClientWayland::setTextureCrop(int vx, int vy, int vw, int vh)
789{
fei.dengb9a1a572023-09-13 01:33:57 +0000790 DEBUG(mLogCategory, "vx %d vy %d vw %d vh %d window(%d, %d, %d, %d) display(%dx%d)", \
fei.dengf7a0cd32023-08-29 09:36:37 +0000791 vx, vy, vw, vh, mWindowX, mWindowY, mWindowWidth, mWindowHeight, mDisplayWidth, mDisplayHeight);
792 if ( (mDisplayWidth != -1) && (mDisplayHeight != -1) &&
793 ( (vx < 0) || (vx+vw > mDisplayWidth) ||
794 (vy < 0) || (vy+vh > mDisplayHeight) ) )
795 {
796 int cropx, cropy, cropw, croph;
797 int wx1, wx2, wy1, wy2;
798 cropx= 0;
799 cropw= mWindowWidth;
800 cropy= 0;
801 croph= mWindowHeight;
802 if ( (vx < mWindowX) || (vx+vw > mWindowX+mWindowWidth) )
803 {
804 cropx= (mWindowX-vx)*mWindowWidth/vw;
805 cropw= (mWindowX+mWindowWidth-vx)*mWindowWidth/vw - cropx;
806 }
807 else if ( vx < 0 )
808 {
809 cropx= -vx*mWindowWidth/vw;
810 cropw= (vw+vx)*mWindowWidth/vw;
811 }
812 else if ( vx+vw > mWindowWidth )
813 {
814 cropx= 0;
815 cropw= (mWindowWidth-vx)*mWindowWidth/vw;
816 }
817
818 if ( (vy < mWindowY) || (vy+vh > mWindowY+mWindowHeight) )
819 {
820 cropy= (mWindowY-vy)*mWindowHeight/vh;
821 croph= (mWindowY+mWindowHeight-vy)*mWindowHeight/vh - cropy;
822 }
823 else if ( vy < 0 )
824 {
825 cropy= -vy*mWindowHeight/vh;
826 croph= (vh+vy)*mWindowHeight/vh;
827 }
828 else if ( vy+vh > mWindowHeight )
829 {
830 cropy= 0;
831 croph= (mWindowHeight-vy)*mWindowHeight/vh;
832 }
833
834 wx1 = vx;
835 wx2 = vx+vw;
836 wy1 = vy;
837 wy2 = vy+vh;
838 vx = mWindowX;
839 vy = mWindowY;
840 vw = mWindowWidth;
841 vh = mWindowHeight;
842 if ( (wx1 > vx) && (wx1 > 0) )
843 {
844 vx= wx1;
845 }
846 else if ( (wx1 >= vx) && (wx1 < 0) )
847 {
848 vw += wx1;
849 vx= 0;
850 }
851 else if ( wx2 < vx+vw )
852 {
853 vw= wx2-vx;
854 }
855 if ( (wx1 >= 0) && (wx2 > vw) )
856 {
857 vw= vw-wx1;
858 }
859 else if ( wx2 < vx+vw )
860 {
861 vw= wx2-vx;
862 }
863
864 if ( (wy1 > vy) && (wy1 > 0) )
865 {
866 vy= wy1;
867 }
868 else if ( (wy1 >= vy) && (wy1 < 0) )
869 {
870 vy= 0;
871 }
872 else if ( (wy1 < vy) && (wy1 > 0) )
873 {
874 vh -= wy1;
875 }
876 if ( (wy1 >= 0) && (wy2 > vh) )
877 {
878 vh= vh-wy1;
879 }
880 else if ( wy2 < vy+vh )
881 {
882 vh= wy2-vy;
883 }
884 if ( vw < 0 ) vw= 0;
885 if ( vh < 0 ) vh= 0;
886 cropx= (cropx*WL_VPC_SURFACE_CROP_DENOM)/mWindowWidth;
887 cropy= (cropy*WL_VPC_SURFACE_CROP_DENOM)/mWindowHeight;
888 cropw= (cropw*WL_VPC_SURFACE_CROP_DENOM)/mWindowWidth;
889 croph= (croph*WL_VPC_SURFACE_CROP_DENOM)/mWindowHeight;
fei.dengb9a1a572023-09-13 01:33:57 +0000890 DEBUG(mLogCategory,"%d, %d, %d, %d - %d, %d, %d, %d", vx, vy, vw, vh, cropx, cropy, cropw, croph);
fei.dengf7a0cd32023-08-29 09:36:37 +0000891 if (mWlVpcSurface) {
892 wl_vpc_surface_set_geometry_with_crop(mWlVpcSurface, vx, vy, vw, vh, cropx, cropy, cropw, croph );
893 }
894 }
895 else
896 {
897 if (mWlVpcSurface) {
898 wl_vpc_surface_set_geometry(mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight);
899 }
900 }
901}
902
903void WstClientWayland::setForceAspectRatio(bool force)
904{
fei.dengb9a1a572023-09-13 01:33:57 +0000905 DEBUG(mLogCategory, "force aspect ratio:%d",force);
fei.dengf7a0cd32023-08-29 09:36:37 +0000906 mForceAspectRatio = force;
907}
908
909void WstClientWayland::setPixelAspectRatio(double ratio)
910{
911 mPixelAspectRatio = ratio;
912 mPixelAspectRatioChanged = true;
fei.dengb9a1a572023-09-13 01:33:57 +0000913 INFO(mLogCategory, "set aspect ratio:%f",ratio);
fei.dengf7a0cd32023-08-29 09:36:37 +0000914}
915
916void WstClientWayland::updateVideoPosition()
917{
918 bool needUpdate= true;
919 int vx, vy, vw, vh;
920 vx= mVideoX;
921 vy= mVideoY;
922 vw= mVideoWidth;
923 vh= mVideoHeight;
924
925 if (mWindowSizeOverride)
926 {
927 mVideoX= ((mWindowX*mScaleXNum)/mScaleXDenom) + mTransX;
928 mVideoY= ((mWindowY*mScaleYNum)/mScaleYDenom) + mTransY;
929 mVideoWidth= (mWindowWidth*mScaleXNum)/mScaleXDenom;
930 mVideoHeight= (mWindowHeight*mScaleYNum)/mScaleYDenom;
fei.dengb9a1a572023-09-13 01:33:57 +0000931 DEBUG(mLogCategory, "window override video rectangle(%d,%d,%d,%d)",mVideoX,mVideoY,mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000932 }
933 else
934 {
935 mVideoX = mTransX;
936 mVideoY = mTransY;
937 mVideoWidth = (mOutputWidth*mScaleXNum)/mScaleXDenom;
938 mVideoHeight = (mOutputHeight*mScaleYNum)/mScaleYDenom;
fei.dengb9a1a572023-09-13 01:33:57 +0000939 DEBUG(mLogCategory, "video rectangle(%d,%d,%d,%d)",mVideoX,mVideoY,mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000940 }
941
942 if (vx == mVideoX && vy == mVideoY && vw == mVideoWidth && vh == mVideoHeight) {
943 needUpdate = false;
944 }
945
946 if (needUpdate) {
947 /* Send a buffer to compositor to update hole punch geometry */
948 if (mWlSb)
949 {
950 struct wl_buffer *buff;
951
952 buff = wl_sb_create_buffer( mWlSb,
953 0,
954 mWindowWidth,
955 mWindowHeight,
956 mWindowWidth*4,
957 WL_SB_FORMAT_ARGB8888 );
958 wl_surface_attach(mWlSurface, buff, mWindowX, mWindowY);
959 wl_surface_damage(mWlSurface, 0, 0, mWindowWidth, mWindowHeight);
960 wl_surface_commit(mWlSurface);
961 }
sheng.liua7baed72024-01-24 07:31:24 +0000962 }
963 if (mPlugin)
964 {
965 mPlugin->setVideoRect(mVideoX, mVideoY, mVideoWidth, mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000966 }
967}
968
969void WstClientWayland::setVideoPath(bool useGfxPath )
970{
fei.dengb9a1a572023-09-13 01:33:57 +0000971 INFO(mLogCategory,"useGfxPath:%d",useGfxPath);
fei.dengf7a0cd32023-08-29 09:36:37 +0000972 if ( needBounds() && mWlVpcSurface )
973 {
974 /* Use nominal display size provided to us by
975 * the compositor to calculate the video bounds
976 * we should use when we transition to graphics path.
977 * Save and restore current HW video rectangle. */
978 int vx, vy, vw, vh;
979 int tx, ty, tw, th;
980 tx = mVideoX;
981 ty = mVideoY;
982 tw = mVideoWidth;
983 th = mVideoHeight;
984 mVideoX = mWindowX;
985 mVideoY = mWindowY;
986 mVideoWidth = mWindowWidth;
987 mVideoHeight = mWindowHeight;
988
fei.dengb9a1a572023-09-13 01:33:57 +0000989 DEBUG(mLogCategory, "video rect (%d,%d,%d,%d)",mVideoX,mVideoY,mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000990
991 if (mFrameWidth > 0 && mFrameHeight > 0) {
992 getVideoBounds(&vx, &vy, &vw, &vh);
993 setTextureCrop(vx, vy, vw, vh);
994 }
995
996 mVideoX = tx;
997 mVideoY = ty;
998 mVideoWidth = tw;
999 mVideoHeight = th;
1000 }
1001}
1002
1003bool WstClientWayland::approxEqual( double v1, double v2 )
1004{
1005 bool result= false;
1006 if ( v1 >= v2 )
1007 {
1008 if ( (v1-v2) < 0.001 )
1009 {
1010 result= true;
1011 }
1012 }
1013 else
1014 {
1015 if ( (v2-v1) < 0.001 )
1016 {
1017 result= true;
1018 }
1019 }
1020 return result;
1021}
1022
1023void WstClientWayland::readyToRun()
1024{
1025 mFd = wl_display_get_fd (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001026 if (mPoll) {
1027 mPoll->addFd(mFd);
1028 mPoll->setFdReadable(mFd, true);
1029 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001030}
1031
1032bool WstClientWayland::threadLoop()
1033{
1034 struct pollfd pfd;
1035 int ret;
1036
1037 while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) {
1038 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1039 }
1040
fei.dengf7a0cd32023-08-29 09:36:37 +00001041 wl_display_flush (mWlDisplay);
1042
fei.dengb9a1a572023-09-13 01:33:57 +00001043 /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed,
1044 so do use -1 to wait for ever*/
1045 ret = mPoll->wait(-1); //wait for ever
fei.dengf7a0cd32023-08-29 09:36:37 +00001046 if (ret < 0) { //poll error
fei.dengb9a1a572023-09-13 01:33:57 +00001047 WARNING(mLogCategory,"poll error");
fei.dengf7a0cd32023-08-29 09:36:37 +00001048 wl_display_cancel_read(mWlDisplay);
1049 return false;
1050 } else if (ret == 0) { //poll time out
1051 return true; //run loop
1052 }
1053
1054 if (wl_display_read_events (mWlDisplay) == -1) {
1055 goto tag_error;
1056 }
1057
1058 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1059 return true;
1060tag_error:
fei.dengb9a1a572023-09-13 01:33:57 +00001061 ERROR(mLogCategory,"Error communicating with the wayland server");
fei.dengf7a0cd32023-08-29 09:36:37 +00001062 return false;
1063}
1064
1065void WstClientWayland::executeCmd(const char *cmd) {
fei.dengb9a1a572023-09-13 01:33:57 +00001066 INFO(mLogCategory,"%s", cmd);
fei.dengf7a0cd32023-08-29 09:36:37 +00001067 FILE* pFile = popen(cmd, "r");
1068 char buf[128];
1069 char* retStr = fgets(buf, sizeof(buf), pFile);
fei.dengb9a1a572023-09-13 01:33:57 +00001070 INFO(mLogCategory,"ret= %s", retStr);
fei.dengf7a0cd32023-08-29 09:36:37 +00001071 pclose(pFile);
1072}