VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/wayland-helper.h

最後變更 在這個檔案是 106786,由 vboxsync 提交於 5 月 前

Additions: Linux/Wayland: Rename VBClClipboardThreadStart to vbcl_wayland_thread_start and put it into common place, bugref:10796.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 12.9 KB
 
1/* $Id: wayland-helper.h 106786 2024-10-30 11:33:02Z vboxsync $ */
2/** @file
3 * Guest Additions - Definitions for Wayland helpers.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.alldomusa.eu.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef GA_INCLUDED_SRC_x11_VBoxClient_wayland_helper_h
29#define GA_INCLUDED_SRC_x11_VBoxClient_wayland_helper_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#include <iprt/asm.h>
35#include <iprt/time.h>
36
37#include <VBox/VBoxGuestLib.h>
38#include <VBox/GuestHost/clipboard-helper.h>
39
40#include "clipboard.h"
41
42/** Helper capabilities list. */
43
44/** Indicates that helper does not support any functionality (initializer). */
45#define VBOX_WAYLAND_HELPER_CAP_NONE (0)
46/** Indicates that helper supported shared clipboard functionality. */
47#define VBOX_WAYLAND_HELPER_CAP_CLIPBOARD RT_BIT(1)
48/** Indicates that helper supported drag-and-drop functionality. */
49#define VBOX_WAYLAND_HELPER_CAP_DND RT_BIT(2)
50
51/** Default time interval to wait for value to arrive over IPC. */
52#define VBCL_WAYLAND_VALUE_WAIT_TIMEOUT_MS (1000)
53/** Default time interval to wait for clipboard content to arrive over IPC. */
54#define VBCL_WAYLAND_DATA_WAIT_TIMEOUT_MS (2000)
55/** Generic relax interval while polling value changes. */
56#define VBCL_WAYLAND_RELAX_INTERVAL_MS (50)
57/** Maximum number of participants who can join session. */
58#define VBCL_WAYLAND_SESSION_USERS_MAX (10)
59/** Value which determines if session structure was initialized. */
60#define VBCL_WAYLAND_SESSION_MAGIC (0xDEADBEEF)
61
62/** Session states. */
63typedef enum
64{
65 /** Session is not active. */
66 VBCL_WL_SESSION_STATE_IDLE,
67 /** Session is being initialized. */
68 VBCL_WL_SESSION_STATE_STARTING,
69 /** Session has started and now can be joined. */
70 VBCL_WL_SESSION_STATE_STARTED,
71 /** Session is terminating. */
72 VBCL_WL_SESSION_STATE_TERMINATING
73} vbcl_wl_session_state_t;
74
75/** Session type.
76 *
77 * Type determines the purpose of session. It is
78 * also serves a sanity check purpose when different
79 * participants join session with certain intention.
80 * Session type can only be set when session is
81 * in STARTING state and reset when session is TERMINATING.
82 */
83typedef enum
84{
85 /** Initializer. */
86 VBCL_WL_SESSION_TYPE_INVALID,
87 /** Copy clipboard data to the guest. */
88 VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_GUEST,
89 /** Announce clipboard formats to the host. */
90 VBCL_WL_CLIPBOARD_SESSION_TYPE_ANNOUNCE_TO_HOST,
91 /** Copy clipboard data to the host. */
92 VBCL_WL_CLIPBOARD_SESSION_TYPE_COPY_TO_HOST
93} vbcl_wl_session_type_t;
94
95/** Session private data. */
96typedef struct
97{
98 /** Magic number which indicates if session
99 * was previously initialized. */
100 volatile uint32_t u32Magic;
101
102 /** Session state, synchronization element. */
103 volatile vbcl_wl_session_state_t enmState;
104
105 /** Session type. */
106 vbcl_wl_session_type_t enmType;
107
108 /** Session description, used for logging purpose
109 * to distinguish between operations flow. */
110 const char *pcszDesc;
111
112 /** Current number of session users. When session
113 * switches into TERMINATING state, it will wait
114 * number of participants to drop to 1 before releasing
115 * session resources and resetting internal data to
116 * default values. */
117 volatile uint32_t cUsers;
118} vbcl_wl_session_t;
119
120/** Session state change callback.
121 *
122 * Data which belongs to a session must be accessed from
123 * session callback only. It ensures session data integrity
124 * and prevents access to data when session is not yet
125 * initialized or already terminated.
126 *
127 * @returns IPRT status code.
128 * @param enmSessionType Session type, provided for consistency
129 * check to make sure given callback is
130 * intended to be triggered in context of
131 * given session type.
132 * @param pvUser Optional user data.
133 */
134typedef DECLCALLBACKTYPE(int, FNVBCLWLSESSIONCB, (vbcl_wl_session_type_t enmSessionType, void *pvUser));
135/** Pointer to FNVBCLWLSESSIONCB. */
136typedef FNVBCLWLSESSIONCB *PFNVBCLWLSESSIONCB;
137
138/**
139 * Wayland Desktop Environment helper clipboard operations.
140 */
141typedef struct
142{
143
144 /**
145 * Initialization callback.
146 *
147 * @returns IPRT status code.
148 */
149 DECLCALLBACKMEMBER(int, pfnInit, (void));
150
151 /**
152 * Termination callback.
153 *
154 * @returns IPRT status code.
155 */
156 DECLCALLBACKMEMBER(int, pfnTerm, (void));
157
158 /**
159 * Callback to set host clipboard connection handle.
160 *
161 * @param pCtx Host service connection context.
162 */
163 DECLCALLBACKMEMBER(void, pfnSetClipboardCtx, (PVBGLR3SHCLCMDCTX pCtx));
164
165 /**
166 * Callback to force guest to announce its clipboard content.
167 *
168 * @returns IPRT status code.
169 */
170 DECLCALLBACKMEMBER(int, pfnPopup, (void));
171
172 /** A callback to notify guest about new content in host clipboard. */
173 PFNHOSTCLIPREPORTFMTS pfnHGClipReport;
174
175 /** Callback to notify guest that host wants to read clipboard data in specified format. */
176 PFNHOSTCLIPREAD pfnGHClipRead;
177
178} VBCLWAYLANDHELPER_CLIPBOARD;
179
180/**
181 * Wayland Desktop Environment helper DnD operations.
182 */
183typedef struct
184{
185
186 /**
187 * Initialization callback.
188 *
189 * @returns IPRT status code.
190 */
191 DECLCALLBACKMEMBER(int, pfnInit, (void));
192
193 /**
194 * Termination callback.
195 *
196 * @returns IPRT status code.
197 */
198 DECLCALLBACKMEMBER(int, pfnTerm, (void));
199
200} VBCLWAYLANDHELPER_DND;
201
202/**
203 * Wayland Desktop Environment helper definition structure.
204 */
205typedef struct
206{
207 /** A short helper name. 16 chars maximum (RTTHREAD_NAME_LEN). */
208 const char *pszName;
209
210 /**
211 * Probing callback.
212 *
213 * Called in attempt to detect if user is currently running Desktop Environment
214 * which is compatible with the helper.
215 *
216 * @returns Helper capabilities bitmask as described by VBOX_WAYLAND_HELPER_CAP_XXX.
217 */
218 DECLCALLBACKMEMBER(int, pfnProbe, (void));
219
220 /** Helper functions for clipboard operations. */
221 VBCLWAYLANDHELPER_CLIPBOARD clip;
222
223 /** Helper functions for DnD operations. */
224 VBCLWAYLANDHELPER_DND dnd;
225
226} VBCLWAYLANDHELPER;
227
228namespace vbcl
229{
230 /**
231 * This is abstract one-shot data type which can be set by writer and waited
232 * by reader in a thread-safe way.
233 *
234 * Method wait() will wait within predefined interval of time until
235 * value of this type will be changed to anything different from default
236 * value which is defined during initialization. Reader must compare
237 * returned value with what defaults() method returns in order to make
238 * sure that value was actually set by writer.
239 *
240 * Method reset() will atomically reset current value to defaults and
241 * return previous value. This is useful when writer has previously
242 * dynamically allocated chunk of memory and reader needs to deallocete
243 * it in the end.
244 */
245 template <class T> class Waitable
246 {
247 public:
248
249 Waitable()
250 {};
251
252 /**
253 * Initialize data type.
254 *
255 * @param default_value Default value, used while
256 * waiting for value change.
257 * @param timeoutMs Time interval to wait for
258 * value change before returning.
259 */
260 void init(T default_value, uint64_t timeoutMs)
261 {
262 m_Value = default_value;
263 m_Default = default_value;
264 m_TimeoutMs = timeoutMs;
265 }
266
267 /**
268 * Atomically set value.
269 *
270 * @param value Value to set.
271 */
272 void set(T value)
273 {
274 ASMAtomicWriteU64(&m_Value, value);
275 }
276
277 /**
278 * Atomically reset value to defaults and return previous value.
279 *
280 * @returns Value which was assigned before reset.
281 */
282 uint64_t reset()
283 {
284 return ASMAtomicXchgU64(&m_Value, m_Default);
285 }
286
287 /**
288 * Wait until value will be changed from defaults and return it.
289 *
290 * @returns Current value.
291 */
292 T wait()
293 {
294 uint64_t tsStart = RTTimeMilliTS();
295
296 while( (RTTimeMilliTS() - tsStart) < m_TimeoutMs
297 && (ASMAtomicReadU64(&m_Value)) == m_Default)
298 {
299 RTThreadSleep(VBCL_WAYLAND_RELAX_INTERVAL_MS);
300 }
301
302 return m_Value;
303 }
304
305 /**
306 * Get default value which was set during initialization.
307 *
308 * @returns Default value.
309 */
310 T defaults()
311 {
312 return m_Default;
313 }
314
315 protected:
316
317 /** Value itself. */
318 uint64_t m_Value;
319 /** Default value. */
320 uint64_t m_Default;
321 /** Value change waiting timeout. */
322 uint64_t m_TimeoutMs;
323 };
324}
325
326/**
327 * Initialize session.
328 *
329 * This function should be called only once, during initialization step.
330 *
331 * @param pSession A pointer to session data.
332 */
333RTDECL(void) vbcl_wayland_session_init(vbcl_wl_session_t *pSession);
334
335/**
336 * Start new session.
337 *
338 * Attempt to change session state from IDLE to STARTED and
339 * execute initialization callback in between. If current
340 * session state is different from IDLE, state transition will
341 * not be possible and error will be returned.
342 *
343 * @returns IPRT status code.
344 * @param pSession Session object.
345 * @param enmType Session type.
346 * @param pfnStart Initialization callback.
347 * @param pvUser User data to pass to initialization callback.
348 */
349RTDECL(int) vbcl_wayland_session_start(vbcl_wl_session_t *pSession,
350 vbcl_wl_session_type_t enmType,
351 PFNVBCLWLSESSIONCB pfnStart,
352 void *pvUser);
353
354/**
355 * Join session.
356 *
357 * Attempt to grab a reference to a session, execute provided
358 * callback while holding a reference and release reference.
359 * This function will fail if current session state is different
360 * from STARTED.
361 *
362 * @returns IPRT status code.
363 * @param pSession Session object.
364 * @param pfnJoin A callback to run while holding session reference.
365 * @param pvUser User data to pass to callback.
366 * @param pcszCallee Text tag which corresponds to calling function (only
367 * for logging)
368 */
369RTDECL(int) vbcl_wayland_session_join_ex(vbcl_wl_session_t *pSession,
370 PFNVBCLWLSESSIONCB pfnJoin, void *pvUser,
371 const char *pcszCallee);
372
373/**
374 * Join session (wrapper for vbcl_wayland_session_join_ex).
375 */
376#define vbcl_wayland_session_join(pSession, pfnJoin, pvUser) \
377 vbcl_wayland_session_join_ex(pSession, pfnJoin, pvUser, __func__)
378
379/**
380 * End session.
381 *
382 * Attempt to wait until session is no longer in use, execute
383 * terminating callback and reset session to IDLE state.
384 *
385 * @returns IPRT status code.
386 * @param pSession Session object.
387 * @param pfnEnd Termination callback.
388 * @param pvUser User data to pass to termination callback.
389 */
390RTDECL(int) vbcl_wayland_session_end(vbcl_wl_session_t *pSession,
391 PFNVBCLWLSESSIONCB pfnEnd, void *pvUser);
392
393/**
394 * Check if session was started.
395 *
396 * @returns True if session is started, False otherwise.
397 * @param pSession Session object.
398 */
399RTDECL(bool) vbcl_wayland_session_is_started(vbcl_wl_session_t *pSession);
400
401/**
402 * Create thread and wait until it started.
403 *
404 * @returns IPRT status code.
405 * @param pThread Pointer to thread data.
406 * @param pfnThread Pointer to thread main loop function.
407 * @param pszName Thread name.
408 * @param pvUser User data.
409 */
410RTDECL(int) vbcl_wayland_thread_start(PRTTHREAD pThread, PFNRTTHREAD pfnThread, const char *pszName, void *pvUser);
411
412/** Wayland helper which uses GTK library. */
413extern const VBCLWAYLANDHELPER g_WaylandHelperGtk;
414/** Wayland helper which uses Data Control Protocol. */
415extern const VBCLWAYLANDHELPER g_WaylandHelperDcp;
416
417#endif /* !GA_INCLUDED_SRC_x11_VBoxClient_wayland_helper_h */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette