VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/clipboard-common.cpp@ 103365

Last change on this file since 103365 was 103365, checked in by vboxsync, 3 months ago

Shared Clipboard/Additions: Removed lots of code duplication for reading clipboard data from the host (partly introduced by r159772).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 3.8 KB
Line 
1/** $Id: clipboard-common.cpp 103365 2024-02-14 18:13:38Z vboxsync $ */
2/** @file
3 * Guest Additions - Shared Clipboard common code.
4 */
5
6/*
7 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.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
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32
33#ifdef LOG_GROUP
34# undef LOG_GROUP
35#endif
36#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
37#include <iprt/log.h>
38#include <iprt/mem.h>
39
40#include <VBox/err.h>
41
42#include "VBoxClient.h"
43#include "clipboard.h"
44
45RTDECL(int) VBClClipboardThreadStart(PRTTHREAD pThread, PFNRTTHREAD pfnThread, const char *pszName, void *pvUser)
46{
47 int rc;
48
49 rc = RTThreadCreate(pThread, pfnThread, pvUser, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, pszName);
50 if (RT_SUCCESS(rc))
51 rc = RTThreadUserWait(*pThread, RT_MS_30SEC /* msTimeout */);
52
53 if (RT_SUCCESS(rc))
54 VBClLogVerbose(1, "started %s thread\n", pszName);
55 else
56 LogRel(("unable to start %s thread, rc=%Rrc\n", pszName, rc));
57
58 return rc;
59}
60
61RTDECL(int) VBClClipboardReadHostEvent(PSHCLCONTEXT pCtx, const PFNHOSTCLIPREPORTFMTS pfnHGClipReport,
62 const PFNHOSTCLIPREAD pfnGHClipRead)
63{
64 int rc;
65
66 uint32_t idMsg = 0;
67 uint32_t cParms = 0;
68
69 AssertPtrReturn(pfnHGClipReport, VERR_INVALID_PARAMETER);
70 AssertPtrReturn(pfnGHClipRead, VERR_INVALID_PARAMETER);
71
72 PVBGLR3CLIPBOARDEVENT pEvent = (PVBGLR3CLIPBOARDEVENT)RTMemAllocZ(sizeof(VBGLR3CLIPBOARDEVENT));
73 AssertPtrReturn(pEvent, VERR_NO_MEMORY);
74
75 rc = VbglR3ClipboardMsgPeekWait(&pCtx->CmdCtx, &idMsg, &cParms, NULL /* pidRestoreCheck */);
76 if (RT_SUCCESS(rc))
77 rc = VbglR3ClipboardEventGetNext(idMsg, cParms, &pCtx->CmdCtx, pEvent);
78
79 if (RT_SUCCESS(rc))
80 {
81 switch (pEvent->enmType)
82 {
83 /* Host reports new clipboard data is now available. */
84 case VBGLR3CLIPBOARDEVENTTYPE_REPORT_FORMATS:
85 {
86 rc = pfnHGClipReport(pEvent->u.fReportedFormats);
87 break;
88 }
89
90 /* Host wants to read data from guest clipboard. */
91 case VBGLR3CLIPBOARDEVENTTYPE_READ_DATA:
92 {
93 rc = pfnGHClipRead(pEvent->u.fReadData);
94 break;
95 }
96
97 default:
98 {
99 AssertMsgFailedBreakStmt(("Event type %RU32 not implemented\n", pEvent->enmType), rc = VERR_NOT_SUPPORTED);
100 }
101 }
102 }
103 else
104 LogFlowFunc(("Getting next event failed with %Rrc\n", rc));
105
106 VbglR3ClipboardEventFree(pEvent);
107
108 LogFlowFuncLeaveRC(rc);
109 return rc;
110}
111
112RTDECL(int) VBClClipboardReadHostClipboard(PVBGLR3SHCLCMDCTX pCtx,
113 SHCLFORMAT uFmt, void **ppvData, uint32_t *pcbData)
114{
115 return VbglR3ClipboardReadDataEx(pCtx, uFmt, ppvData, pcbData);
116}
117
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use