blob: 62da0839010d5b3b2b5cee3309781fec56d35ca9 [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
fei.dengb9a1a572023-09-13 01:33:57 +0000161 INFO(self->mLogCategory,"new pathway: %d\n", 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;
fei.dengb9a1a572023-09-13 01:33:57 +0000246 DEBUG(self->mLogCategory,"compositor sets window to (%dx%d)\n", 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);
fei.dengb9a1a572023-09-13 01:33:57 +0000288 TRACE(self->mLogCategory,"registry: sbFormat: %X\n", 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;
fei.dengb9a1a572023-09-13 01:33:57 +0000381 mPoll = new Tls::Poll(true);
fei.dengf7a0cd32023-08-29 09:36:37 +0000382}
383
384WstClientWayland::~WstClientWayland()
385{
fei.dengb9a1a572023-09-13 01:33:57 +0000386 TRACE(mLogCategory,"deconstruct WstClientWayland");
387 if (mPoll) {
388 delete mPoll;
389 mPoll = NULL;
390 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000391}
392
393int WstClientWayland::connectToWayland()
394{
395 const char *xdgEnv = NULL;
396 const char *displayEnv = NULL;
397 const char *displayName = "rlib-display";
398 const char *fullScreenEnV = NULL;
fei.dengb9a1a572023-09-13 01:33:57 +0000399 DEBUG(mLogCategory,"in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000400 xdgEnv = getenv("XDG_RUNTIME_DIR");
fei.dengb9a1a572023-09-13 01:33:57 +0000401 INFO(mLogCategory,"XDG_RUNTIME_DIR:%s",xdgEnv != NULL?xdgEnv:"NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +0000402 if (!xdgEnv) {
403 xdgEnv = "/run";
fei.dengb9a1a572023-09-13 01:33:57 +0000404 ERROR(mLogCategory,"XDG_RUNTIME_DIR is not set,set default %s",xdgEnv);
fei.dengf7a0cd32023-08-29 09:36:37 +0000405 }
406 displayEnv= getenv("WAYLAND_DISPLAY");
fei.dengb9a1a572023-09-13 01:33:57 +0000407 INFO(mLogCategory,"WAYLAND_DISPLAY:%s",displayEnv != NULL?displayEnv:"NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +0000408
409 fullScreenEnV= getenv("VIDEO_RENDER_FORCE_FULLSCREEN");
fei.dengb9a1a572023-09-13 01:33:57 +0000410 INFO(mLogCategory,"VIDEO_RENDER_FORCE_FULLSCREEN:%s",fullScreenEnV != NULL?fullScreenEnV:"NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +0000411 if (fullScreenEnV) {
412 int force = atoi(fullScreenEnV);
413 mForceFullScreen = force != 0? true: false;
fei.dengb9a1a572023-09-13 01:33:57 +0000414 INFO(mLogCategory,"VIDEO_RENDER_FORCE_FULLSCREEN:%d",mForceFullScreen);
fei.dengf7a0cd32023-08-29 09:36:37 +0000415 }
416
417 mWlDisplay = wl_display_connect(NULL);
418 if (!mWlDisplay) {
419 char waylandDisplay[64];
fei.dengb9a1a572023-09-13 01:33:57 +0000420 ERROR(mLogCategory,"Failed connect to default wayland display");
fei.dengf7a0cd32023-08-29 09:36:37 +0000421 memset(waylandDisplay, 0, 64);
422 strcpy(waylandDisplay, xdgEnv);
423 strcat(waylandDisplay, "/" );
424 strcat(waylandDisplay, displayName);
fei.dengb9a1a572023-09-13 01:33:57 +0000425 INFO(mLogCategory,"detect rlib wayland display %s",waylandDisplay);
fei.dengf7a0cd32023-08-29 09:36:37 +0000426 if (access(waylandDisplay,F_OK) == 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000427 INFO(mLogCategory,"try to connect to %s",waylandDisplay);
fei.dengf7a0cd32023-08-29 09:36:37 +0000428 mWlDisplay = wl_display_connect(displayName);
429 }
430 /*try to create wayland display my self*/
431 if (!mWlDisplay && xdgEnv) {
fei.dengb9a1a572023-09-13 01:33:57 +0000432 WARNING(mLogCategory,"try to create and connect rlib-display display");
fei.dengf7a0cd32023-08-29 09:36:37 +0000433 std::string cmdCreateDisplay = R"(curl 'http://127.0.0.1:9998/jsonrpc' -d '{"jsonrpc": "2.0","id": 4,"method":
434 "org.rdk.RDKShell.1.createDisplay","params": { "client": "rlib-display", "displayName": "rlib-display" }}';echo)";
435 std::string cmdMoveDisplay = R"(curl 'http://127.0.0.1:9998/jsonrpc' -d '{"jsonrpc": "2.0","id": 4,"method":
436 "org.rdk.RDKShell.1.moveToBack", "params": { "client": "rlib-display" }}';echo)";
437
438 executeCmd(cmdCreateDisplay.c_str());
439 executeCmd(cmdMoveDisplay.c_str());
440 //try to connect again
441 mWlDisplay = wl_display_connect(displayName);
442 }
443
444 if (!mWlDisplay) {
fei.dengb9a1a572023-09-13 01:33:57 +0000445 ERROR(mLogCategory, "wayland connect rlib-display fail");
446 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000447 }
fei.dengb9a1a572023-09-13 01:33:57 +0000448 INFO(mLogCategory,"wayland connected to rlib-display");
fei.dengf7a0cd32023-08-29 09:36:37 +0000449 }
450
451 mWlQueue = wl_display_create_queue (mWlDisplay);
452 wl_proxy_set_queue ((struct wl_proxy *)mWlDisplay, mWlQueue);
453
454 mWlRegistry = wl_display_get_registry (mWlDisplay);
455 wl_registry_add_listener (mWlRegistry, &registry_listener, (void *)this);
456
457 /* we need exactly 2 roundtrips to discover global objects and their state */
458 for (int i = 0; i < 2; i++) {
459 if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000460 ERROR(mLogCategory,"Error communicating with the wayland display");
461 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000462 }
463 }
464
465 //create surface from compositor
466 if (mWlCompositor) {
467 mWlSurface = wl_compositor_create_surface(mWlCompositor);
468 wl_proxy_set_queue((struct wl_proxy*)mWlSurface, mWlQueue);
469 wl_display_flush( mWlDisplay );
470 }
471
472 if (mWlVpc && mWlSurface) {
473 mWlVpcSurface = wl_vpc_get_vpc_surface( mWlVpc, mWlSurface );
474 if (mWlVpcSurface) {
475 wl_vpc_surface_add_listener( mWlVpcSurface, &vpcListener, (void *)this);
476 wl_proxy_set_queue((struct wl_proxy*)mWlVpcSurface, mWlQueue);
477 wl_vpc_surface_set_geometry( mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight);
478 wl_display_flush(mWlDisplay);
479 }
480 }
481
fei.dengb9a1a572023-09-13 01:33:57 +0000482 INFO(mLogCategory,"out");
483 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000484}
485
486void WstClientWayland::disconnectFromWayland()
487{
fei.dengb9a1a572023-09-13 01:33:57 +0000488 INFO(mLogCategory,"in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000489
490 if (isRunning()) {
fei.dengb9a1a572023-09-13 01:33:57 +0000491 TRACE(mLogCategory,"try stop dispatch thread");
492 if (mPoll) {
493 mPoll->setFlushing(true);
494 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000495 requestExitAndWait();
496 }
497
498 if (mWlShell) {
499 wl_simple_shell_destroy(mWlShell);
500 mWlShell = NULL;
501 }
502
503 if (mWlVpcSurface) {
504 wl_vpc_surface_destroy(mWlVpcSurface);
505 mWlVpcSurface = NULL;
506 }
507
508 if (mWlVpc) {
509 wl_vpc_destroy(mWlVpc);
510 mWlVpc = NULL;
511 }
512
513 if (mWlSurface) {
514 wl_surface_destroy(mWlSurface);
515 mWlSurface = NULL;
516 }
517
518 if (mWlOutput) {
519 wl_output_destroy(mWlOutput);
520 mWlOutput = NULL;
521 }
522
523 if (mWlSb) {
524 wl_sb_destroy(mWlSb);
525 mWlSb = NULL;
526 }
527
528 if (mWlCompositor) {
529 wl_compositor_destroy (mWlCompositor);
530 mWlCompositor = NULL;
531 }
532
533 if (mWlRegistry) {
534 wl_registry_destroy (mWlRegistry);
535 mWlRegistry= NULL;
536 }
537
538 if (mWlQueue) {
539 wl_event_queue_destroy (mWlQueue);
540 mWlQueue = NULL;
541 }
542
543 if (mWlDisplay) {
544 wl_display_flush (mWlDisplay);
545 wl_display_disconnect (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +0000546 INFO(mLogCategory,"wl display disconnect");
fei.dengf7a0cd32023-08-29 09:36:37 +0000547 mWlDisplay = NULL;
548 }
549
fei.dengb9a1a572023-09-13 01:33:57 +0000550 INFO(mLogCategory,"out");
fei.dengf7a0cd32023-08-29 09:36:37 +0000551}
552
553void WstClientWayland::setWindowSize(int x, int y, int w, int h)
554{
555 if (x == 0 && y == 0 && w == 0 && h == 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000556 WARNING(mLogCategory, "set full screen? %dx%dx%dx%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +0000557 return;
558 }
559 mWindowX = x;
560 mWindowY = y;
561 mWindowWidth = w;
562 mWindowHeight = h;
563 mWindowChange = true;
564 mWindowSet = true;
565 mWindowSizeOverride = true;
fei.dengb9a1a572023-09-13 01:33:57 +0000566 DEBUG(mLogCategory,"set window size:x:%d,y:%d,w:%d,h:%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +0000567 if (mWlVpcSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +0000568 DEBUG(mLogCategory, "wl_vpc_surface_set_geometry(%d,%d,%d,%d)",mWindowX,mWindowY,mWindowWidth,mWindowHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000569 wl_vpc_surface_set_geometry(mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight);
570 }
571 //if window size is updated, update video position
572 if (mWlVpcSurface && mWindowChange && mScaleXDenom != 0 && mScaleYDenom != 0) {
573 mWindowChange = false;
574 updateVideoPosition();
575 }
576}
577
578void WstClientWayland::setFrameSize(int frameWidth, int frameHeight) {
579 mFrameWidth = frameWidth;
580 mFrameHeight = frameHeight;
fei.dengb9a1a572023-09-13 01:33:57 +0000581 DEBUG(mLogCategory, "set frame size:%dx%d",mFrameWidth, mFrameHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000582}
583
584void WstClientWayland::setZoomMode(int zoomMode, bool globalZoomActive, bool allow4kZoom) {
fei.dengb9a1a572023-09-13 01:33:57 +0000585 DEBUG(mLogCategory, "zoomMode:%d, globalZoomActive:%d, allow4kZoom:%d",zoomMode,globalZoomActive,allow4kZoom);
fei.dengf7a0cd32023-08-29 09:36:37 +0000586 mZoomModeGlobal = globalZoomActive;
587 if ( !globalZoomActive )
588 {
589 mZoomMode = ZOOM_NONE;
590 }
591 mAllow4kZoom= allow4kZoom;
592 if ( mZoomModeGlobal == true )
593 {
594 if ( (zoomMode >= ZOOM_NONE) && (zoomMode <= ZOOM_ZOOM) )
595 {
596 mZoomMode = zoomMode;
597 mPixelAspectRatioChanged = true;
598 }
599 } else {
fei.dengb9a1a572023-09-13 01:33:57 +0000600 DEBUG(mLogCategory, "global zoom disabled: ignore server value");
fei.dengf7a0cd32023-08-29 09:36:37 +0000601 }
602}
603
604void WstClientWayland::getVideoBounds(int *x, int *y, int *w, int *h)
605{
606 int vx, vy, vw, vh;
607 int frameWidth, frameHeight;
608 int zoomMode;
609 double contentWidth, contentHeight;
610 double roix, roiy, roiw, roih;
611 double arf, ard;
612 double hfactor= 1.0, vfactor= 1.0;
613
614 //if wayland open fail,use the default window size
615 if (!mWlDisplay) {
616 *x = mWindowX;
617 *y = mWindowY;
618 *w = mWindowWidth;
619 *h = mWindowHeight;
fei.dengb9a1a572023-09-13 01:33:57 +0000620 TRACE(mLogCategory, "no wldisplay, %d,%d,%d,%d",mVideoX,mVideoY,mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000621 return;
622 }
623
624 //if window size is updated, update video position
625 if (mWlVpcSurface && mWindowChange) {
626 mWindowChange = false;
627 updateVideoPosition();
628 }
629
630 vx = mVideoX;
631 vy = mVideoY;
632 vw = mVideoWidth;
633 vh = mVideoHeight;
634
fei.dengb9a1a572023-09-13 01:33:57 +0000635 TRACE(mLogCategory, "videoX:%d,videoY:%d,videoW:%d,videoH:%d",mVideoX,mVideoY,mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000636 //if the window size is smaller than display size,we scall video to map window size
637 if (mWindowWidth != mDisplayWidth || mWindowHeight != mDisplayHeight) {
638 *x= vx;
639 *y= vy;
640 *w= vw;
641 *h= vh;
fei.dengb9a1a572023-09-13 01:33:57 +0000642 TRACE(mLogCategory, "small window vrect %d, %d, %d, %d", vx, vy, vw, vh);
fei.dengf7a0cd32023-08-29 09:36:37 +0000643 return;
644 }
645 if (mForceFullScreen) {
646 *x= vx;
647 *y= vy;
648 *w= vw;
649 *h= vh;
fei.dengb9a1a572023-09-13 01:33:57 +0000650 TRACE(mLogCategory, "force window vrect %d, %d, %d, %d", vx, vy, vw, vh);
fei.dengf7a0cd32023-08-29 09:36:37 +0000651 return;
652 }
653
654 frameWidth = mFrameWidth;
655 frameHeight = mFrameHeight;
656 contentWidth = frameWidth * mPixelAspectRatio;
657 contentHeight = frameHeight;
658
659 if (mPixelAspectRatioChanged)
fei.dengb9a1a572023-09-13 01:33:57 +0000660 DEBUG(mLogCategory,"pixelAspectRatio: %f zoom-mode %d", mPixelAspectRatio, mZoomMode);
fei.dengf7a0cd32023-08-29 09:36:37 +0000661
662 ard = (double)mVideoWidth/(double)mVideoHeight;
663 arf = (double)contentWidth/(double)contentHeight;
664
fei.dengb9a1a572023-09-13 01:33:57 +0000665 TRACE(mLogCategory, "frameWidth:%d,frameHeight:%d,contentWidth:%f,contentHeight:%f",frameWidth,frameHeight,contentWidth,contentHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000666
667 /* Establish region of interest */
668 roix = 0;
669 roiy = 0;
670 roiw = contentWidth;
671 roih = contentHeight;
672
673 zoomMode = mZoomMode;
674 if ((mFrameWidth > 1920) || (mFrameHeight > 1080))
675 {
676 zoomMode= ZOOM_NORMAL;
677 }
678 //if (mPixelAspectRatioChanged )
fei.dengb9a1a572023-09-13 01:33:57 +0000679 TRACE(mLogCategory, "ard %f arf %f", ard, arf);
fei.dengf7a0cd32023-08-29 09:36:37 +0000680 switch ( zoomMode )
681 {
682 case ZOOM_NORMAL:
683 {
684 if ( arf >= ard )
685 {
686 vw = mVideoWidth * (1.0);
687 vh = (roih * vw) / roiw;
688 vx = vx+(mVideoWidth-vw)/2;
689 vy = vy+(mVideoHeight-vh)/2;
690 }
691 else
692 {
693 vh = mVideoHeight * (1.0);
694 vw = (roiw * vh) / roih;
695 vx = vx+(mVideoWidth-vw)/2;
696 vy = vy+(mVideoHeight-vh)/2;
697 }
698 }
699 break;
700 case ZOOM_NONE:
701 case ZOOM_DIRECT:
702 {
703 if ( arf >= ard )
704 {
705 vh = (contentHeight * mVideoWidth) / contentWidth;
706 vy = vy+(mVideoHeight-vh)/2;
707 }
708 else
709 {
710 vw = (contentWidth * mVideoHeight) / contentHeight;
711 vx = vx+(mVideoWidth-vw)/2;
712 }
713 }
714 break;
715 case ZOOM_16_9_STRETCH:
716 {
717 if ( approxEqual(arf, ard) && approxEqual(arf, 1.777) )
718 {
719 /* For 16:9 content on a 16:9 display, stretch as though 4:3 */
720 hfactor= 4.0/3.0;
721 if (mPixelAspectRatioChanged )
fei.dengb9a1a572023-09-13 01:33:57 +0000722 DEBUG(mLogCategory, "stretch apply vfactor %f hfactor %f", vfactor, hfactor);
fei.dengf7a0cd32023-08-29 09:36:37 +0000723 }
724 vh = mVideoHeight * (1.0);
725 vw = vh*hfactor*16/9;
726 vx = vx+(mVideoWidth-vw)/2;
727 vy = vy+(mVideoHeight-vh)/2;
728 }
729 break;
730 case ZOOM_4_3_PILLARBOX:
731 {
732 vh = mVideoHeight * (1.0);
733 vw = vh*4/3;
734 vx = vx+(mVideoWidth-vw)/2;
735 vy = vy+(mVideoHeight-vh)/2;
736 }
737 break;
738 case ZOOM_ZOOM:
739 {
740 if ( arf >= ard )
741 {
742 if (approxEqual(arf, ard) && approxEqual( arf, 1.777) )
743 {
744 /* For 16:9 content on a 16:9 display, enlarge as though 4:3 */
745 vfactor= 4.0/3.0;
746 hfactor= 1.0;
747 if (mPixelAspectRatioChanged )
fei.dengb9a1a572023-09-13 01:33:57 +0000748 DEBUG(mLogCategory, "zoom apply vfactor %f hfactor %f", vfactor, hfactor);
fei.dengf7a0cd32023-08-29 09:36:37 +0000749 }
750 vh = mVideoHeight * vfactor * (1.0);
751 vw = (roiw * vh) * hfactor / roih;
752 vx = vx+(mVideoWidth-vw)/2;
753 vy = vy+(mVideoHeight-vh)/2;
754 }
755 else
756 {
757 vw = mVideoWidth * (1.0);
758 vh = (roih * vw) / roiw;
759 vx = vx+(mVideoWidth-vw)/2;
760 vy = vy+(mVideoHeight-vh)/2;
761 }
762 }
763 break;
764 }
fei.dengb9a1a572023-09-13 01:33:57 +0000765 if (mPixelAspectRatioChanged) DEBUG(mLogCategory, "vrect %d, %d, %d, %d", vx, vy, vw, vh);
fei.dengf7a0cd32023-08-29 09:36:37 +0000766 if (mPixelAspectRatioChanged)
767 {
768 if (mWlDisplay && mWlVpcSurface )
769 {
770 wl_vpc_surface_set_geometry(mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight);
771 wl_display_flush(mWlDisplay);
772 }
773 }
774 mPixelAspectRatioChanged = false;
775 *x= vx;
776 *y= vy;
777 *w= vw;
778 *h= vh;
fei.dengb9a1a572023-09-13 01:33:57 +0000779 TRACE(mLogCategory, "vrect %d, %d, %d, %d", vx, vy, vw, vh);
fei.dengf7a0cd32023-08-29 09:36:37 +0000780}
781
782void WstClientWayland::setTextureCrop(int vx, int vy, int vw, int vh)
783{
fei.dengb9a1a572023-09-13 01:33:57 +0000784 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 +0000785 vx, vy, vw, vh, mWindowX, mWindowY, mWindowWidth, mWindowHeight, mDisplayWidth, mDisplayHeight);
786 if ( (mDisplayWidth != -1) && (mDisplayHeight != -1) &&
787 ( (vx < 0) || (vx+vw > mDisplayWidth) ||
788 (vy < 0) || (vy+vh > mDisplayHeight) ) )
789 {
790 int cropx, cropy, cropw, croph;
791 int wx1, wx2, wy1, wy2;
792 cropx= 0;
793 cropw= mWindowWidth;
794 cropy= 0;
795 croph= mWindowHeight;
796 if ( (vx < mWindowX) || (vx+vw > mWindowX+mWindowWidth) )
797 {
798 cropx= (mWindowX-vx)*mWindowWidth/vw;
799 cropw= (mWindowX+mWindowWidth-vx)*mWindowWidth/vw - cropx;
800 }
801 else if ( vx < 0 )
802 {
803 cropx= -vx*mWindowWidth/vw;
804 cropw= (vw+vx)*mWindowWidth/vw;
805 }
806 else if ( vx+vw > mWindowWidth )
807 {
808 cropx= 0;
809 cropw= (mWindowWidth-vx)*mWindowWidth/vw;
810 }
811
812 if ( (vy < mWindowY) || (vy+vh > mWindowY+mWindowHeight) )
813 {
814 cropy= (mWindowY-vy)*mWindowHeight/vh;
815 croph= (mWindowY+mWindowHeight-vy)*mWindowHeight/vh - cropy;
816 }
817 else if ( vy < 0 )
818 {
819 cropy= -vy*mWindowHeight/vh;
820 croph= (vh+vy)*mWindowHeight/vh;
821 }
822 else if ( vy+vh > mWindowHeight )
823 {
824 cropy= 0;
825 croph= (mWindowHeight-vy)*mWindowHeight/vh;
826 }
827
828 wx1 = vx;
829 wx2 = vx+vw;
830 wy1 = vy;
831 wy2 = vy+vh;
832 vx = mWindowX;
833 vy = mWindowY;
834 vw = mWindowWidth;
835 vh = mWindowHeight;
836 if ( (wx1 > vx) && (wx1 > 0) )
837 {
838 vx= wx1;
839 }
840 else if ( (wx1 >= vx) && (wx1 < 0) )
841 {
842 vw += wx1;
843 vx= 0;
844 }
845 else if ( wx2 < vx+vw )
846 {
847 vw= wx2-vx;
848 }
849 if ( (wx1 >= 0) && (wx2 > vw) )
850 {
851 vw= vw-wx1;
852 }
853 else if ( wx2 < vx+vw )
854 {
855 vw= wx2-vx;
856 }
857
858 if ( (wy1 > vy) && (wy1 > 0) )
859 {
860 vy= wy1;
861 }
862 else if ( (wy1 >= vy) && (wy1 < 0) )
863 {
864 vy= 0;
865 }
866 else if ( (wy1 < vy) && (wy1 > 0) )
867 {
868 vh -= wy1;
869 }
870 if ( (wy1 >= 0) && (wy2 > vh) )
871 {
872 vh= vh-wy1;
873 }
874 else if ( wy2 < vy+vh )
875 {
876 vh= wy2-vy;
877 }
878 if ( vw < 0 ) vw= 0;
879 if ( vh < 0 ) vh= 0;
880 cropx= (cropx*WL_VPC_SURFACE_CROP_DENOM)/mWindowWidth;
881 cropy= (cropy*WL_VPC_SURFACE_CROP_DENOM)/mWindowHeight;
882 cropw= (cropw*WL_VPC_SURFACE_CROP_DENOM)/mWindowWidth;
883 croph= (croph*WL_VPC_SURFACE_CROP_DENOM)/mWindowHeight;
fei.dengb9a1a572023-09-13 01:33:57 +0000884 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 +0000885 if (mWlVpcSurface) {
886 wl_vpc_surface_set_geometry_with_crop(mWlVpcSurface, vx, vy, vw, vh, cropx, cropy, cropw, croph );
887 }
888 }
889 else
890 {
891 if (mWlVpcSurface) {
892 wl_vpc_surface_set_geometry(mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight);
893 }
894 }
895}
896
897void WstClientWayland::setForceAspectRatio(bool force)
898{
fei.dengb9a1a572023-09-13 01:33:57 +0000899 DEBUG(mLogCategory, "force aspect ratio:%d",force);
fei.dengf7a0cd32023-08-29 09:36:37 +0000900 mForceAspectRatio = force;
901}
902
903void WstClientWayland::setPixelAspectRatio(double ratio)
904{
905 mPixelAspectRatio = ratio;
906 mPixelAspectRatioChanged = true;
fei.dengb9a1a572023-09-13 01:33:57 +0000907 INFO(mLogCategory, "set aspect ratio:%f",ratio);
fei.dengf7a0cd32023-08-29 09:36:37 +0000908}
909
910void WstClientWayland::updateVideoPosition()
911{
912 bool needUpdate= true;
913 int vx, vy, vw, vh;
914 vx= mVideoX;
915 vy= mVideoY;
916 vw= mVideoWidth;
917 vh= mVideoHeight;
918
919 if (mWindowSizeOverride)
920 {
921 mVideoX= ((mWindowX*mScaleXNum)/mScaleXDenom) + mTransX;
922 mVideoY= ((mWindowY*mScaleYNum)/mScaleYDenom) + mTransY;
923 mVideoWidth= (mWindowWidth*mScaleXNum)/mScaleXDenom;
924 mVideoHeight= (mWindowHeight*mScaleYNum)/mScaleYDenom;
fei.dengb9a1a572023-09-13 01:33:57 +0000925 DEBUG(mLogCategory, "window override video rectangle(%d,%d,%d,%d)",mVideoX,mVideoY,mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000926 }
927 else
928 {
929 mVideoX = mTransX;
930 mVideoY = mTransY;
931 mVideoWidth = (mOutputWidth*mScaleXNum)/mScaleXDenom;
932 mVideoHeight = (mOutputHeight*mScaleYNum)/mScaleYDenom;
fei.dengb9a1a572023-09-13 01:33:57 +0000933 DEBUG(mLogCategory, "video rectangle(%d,%d,%d,%d)",mVideoX,mVideoY,mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000934 }
935
936 if (vx == mVideoX && vy == mVideoY && vw == mVideoWidth && vh == mVideoHeight) {
937 needUpdate = false;
938 }
939
940 if (needUpdate) {
941 /* Send a buffer to compositor to update hole punch geometry */
942 if (mWlSb)
943 {
944 struct wl_buffer *buff;
945
946 buff = wl_sb_create_buffer( mWlSb,
947 0,
948 mWindowWidth,
949 mWindowHeight,
950 mWindowWidth*4,
951 WL_SB_FORMAT_ARGB8888 );
952 wl_surface_attach(mWlSurface, buff, mWindowX, mWindowY);
953 wl_surface_damage(mWlSurface, 0, 0, mWindowWidth, mWindowHeight);
954 wl_surface_commit(mWlSurface);
955 }
956 if (mVideoPaused && mPlugin)
957 {
958 mPlugin->setVideoRect(mVideoX, mVideoY, mVideoWidth, mVideoHeight);
959 }
960 }
961}
962
963void WstClientWayland::setVideoPath(bool useGfxPath )
964{
fei.dengb9a1a572023-09-13 01:33:57 +0000965 INFO(mLogCategory,"useGfxPath:%d",useGfxPath);
fei.dengf7a0cd32023-08-29 09:36:37 +0000966 if ( needBounds() && mWlVpcSurface )
967 {
968 /* Use nominal display size provided to us by
969 * the compositor to calculate the video bounds
970 * we should use when we transition to graphics path.
971 * Save and restore current HW video rectangle. */
972 int vx, vy, vw, vh;
973 int tx, ty, tw, th;
974 tx = mVideoX;
975 ty = mVideoY;
976 tw = mVideoWidth;
977 th = mVideoHeight;
978 mVideoX = mWindowX;
979 mVideoY = mWindowY;
980 mVideoWidth = mWindowWidth;
981 mVideoHeight = mWindowHeight;
982
fei.dengb9a1a572023-09-13 01:33:57 +0000983 DEBUG(mLogCategory, "video rect (%d,%d,%d,%d)",mVideoX,mVideoY,mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +0000984
985 if (mFrameWidth > 0 && mFrameHeight > 0) {
986 getVideoBounds(&vx, &vy, &vw, &vh);
987 setTextureCrop(vx, vy, vw, vh);
988 }
989
990 mVideoX = tx;
991 mVideoY = ty;
992 mVideoWidth = tw;
993 mVideoHeight = th;
994 }
995}
996
997bool WstClientWayland::approxEqual( double v1, double v2 )
998{
999 bool result= false;
1000 if ( v1 >= v2 )
1001 {
1002 if ( (v1-v2) < 0.001 )
1003 {
1004 result= true;
1005 }
1006 }
1007 else
1008 {
1009 if ( (v2-v1) < 0.001 )
1010 {
1011 result= true;
1012 }
1013 }
1014 return result;
1015}
1016
1017void WstClientWayland::readyToRun()
1018{
1019 mFd = wl_display_get_fd (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001020 if (mPoll) {
1021 mPoll->addFd(mFd);
1022 mPoll->setFdReadable(mFd, true);
1023 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001024}
1025
1026bool WstClientWayland::threadLoop()
1027{
1028 struct pollfd pfd;
1029 int ret;
1030
1031 while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) {
1032 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1033 }
1034
fei.dengf7a0cd32023-08-29 09:36:37 +00001035 wl_display_flush (mWlDisplay);
1036
fei.dengb9a1a572023-09-13 01:33:57 +00001037 /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed,
1038 so do use -1 to wait for ever*/
1039 ret = mPoll->wait(-1); //wait for ever
fei.dengf7a0cd32023-08-29 09:36:37 +00001040 if (ret < 0) { //poll error
fei.dengb9a1a572023-09-13 01:33:57 +00001041 WARNING(mLogCategory,"poll error");
fei.dengf7a0cd32023-08-29 09:36:37 +00001042 wl_display_cancel_read(mWlDisplay);
1043 return false;
1044 } else if (ret == 0) { //poll time out
1045 return true; //run loop
1046 }
1047
1048 if (wl_display_read_events (mWlDisplay) == -1) {
1049 goto tag_error;
1050 }
1051
1052 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1053 return true;
1054tag_error:
fei.dengb9a1a572023-09-13 01:33:57 +00001055 ERROR(mLogCategory,"Error communicating with the wayland server");
fei.dengf7a0cd32023-08-29 09:36:37 +00001056 return false;
1057}
1058
1059void WstClientWayland::executeCmd(const char *cmd) {
fei.dengb9a1a572023-09-13 01:33:57 +00001060 INFO(mLogCategory,"%s", cmd);
fei.dengf7a0cd32023-08-29 09:36:37 +00001061 FILE* pFile = popen(cmd, "r");
1062 char buf[128];
1063 char* retStr = fgets(buf, sizeof(buf), pFile);
fei.dengb9a1a572023-09-13 01:33:57 +00001064 INFO(mLogCategory,"ret= %s", retStr);
fei.dengf7a0cd32023-08-29 09:36:37 +00001065 pclose(pFile);
1066}