[7007] | 1 | /** $Id: clipboard.cpp 100287 2023-06-26 07:53:49Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[99590] | 3 | * Guest Additions - Common Shared Clipboard wrapper service.
|
---|
[7007] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2007-2023 Oracle and/or its affiliates.
|
---|
[7007] | 8 | *
|
---|
[96407] | 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
|
---|
[7007] | 26 | */
|
---|
| 27 |
|
---|
[57358] | 28 |
|
---|
| 29 | /*********************************************************************************************************************************
|
---|
| 30 | * Header Files *
|
---|
| 31 | *********************************************************************************************************************************/
|
---|
[7007] | 32 | #include <iprt/alloc.h>
|
---|
| 33 | #include <iprt/asm.h>
|
---|
| 34 | #include <iprt/assert.h>
|
---|
| 35 | #include <iprt/initterm.h>
|
---|
| 36 | #include <iprt/mem.h>
|
---|
| 37 | #include <iprt/string.h>
|
---|
[82882] | 38 | #include <iprt/path.h>
|
---|
[7007] | 39 | #include <iprt/process.h>
|
---|
| 40 | #include <iprt/semaphore.h>
|
---|
| 41 |
|
---|
[20552] | 42 | #include <VBox/log.h>
|
---|
[21227] | 43 | #include <VBox/VBoxGuestLib.h>
|
---|
[20552] | 44 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
| 45 | #include <VBox/GuestHost/SharedClipboard.h>
|
---|
[82156] | 46 | #include <VBox/GuestHost/SharedClipboard-x11.h>
|
---|
[7007] | 47 |
|
---|
[18360] | 48 | #include "VBoxClient.h"
|
---|
[7007] | 49 |
|
---|
[86967] | 50 | #include "clipboard.h"
|
---|
[99590] | 51 | #include "clipboard-x11.h"
|
---|
[7007] | 52 |
|
---|
[82287] | 53 |
|
---|
[99590] | 54 | /** Shared Clipboard context.
|
---|
| 55 | * Only one context is supported at a time for now. */
|
---|
[86967] | 56 | SHCLCONTEXT g_Ctx;
|
---|
| 57 |
|
---|
| 58 |
|
---|
[81843] | 59 | /**
|
---|
[86873] | 60 | * @interface_method_impl{VBCLSERVICE,pfnInit}
|
---|
| 61 | */
|
---|
| 62 | static DECLCALLBACK(int) vbclShClInit(void)
|
---|
[81052] | 63 | {
|
---|
[87453] | 64 | int rc;
|
---|
[82315] | 65 |
|
---|
[87453] | 66 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
| 67 | rc = ShClTransferCtxInit(&g_Ctx.TransferCtx);
|
---|
| 68 | #else
|
---|
| 69 | rc = VINF_SUCCESS;
|
---|
| 70 | #endif
|
---|
| 71 |
|
---|
[82315] | 72 | LogFlowFuncLeaveRC(rc);
|
---|
| 73 | return rc;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[86873] | 76 | /**
|
---|
| 77 | * @interface_method_impl{VBCLSERVICE,pfnWorker}
|
---|
| 78 | */
|
---|
| 79 | static DECLCALLBACK(int) vbclShClWorker(bool volatile *pfShutdown)
|
---|
[52562] | 80 | {
|
---|
[86871] | 81 | RT_NOREF(pfShutdown);
|
---|
[52562] | 82 |
|
---|
[99590] | 83 | int rc = VINF_SUCCESS;
|
---|
| 84 |
|
---|
[99977] | 85 | if ( VBClGetDisplayServerType() == VBGHDISPLAYSERVERTYPE_X11
|
---|
| 86 | /* If Wayland w/ X fallback support is installed (also called XWayland), prefer using the X clipboard for now. */
|
---|
| 87 | || VBClGetDisplayServerType() == VBGHDISPLAYSERVERTYPE_XWAYLAND)
|
---|
[81060] | 88 | {
|
---|
[99590] | 89 | rc = VBClX11ClipboardInit();
|
---|
| 90 | if (RT_SUCCESS(rc))
|
---|
| 91 | {
|
---|
| 92 | /* Let the main thread know that it can continue spawning services. */
|
---|
| 93 | RTThreadUserSignal(RTThreadSelf());
|
---|
[86871] | 94 |
|
---|
[99590] | 95 | rc = VBClX11ClipboardMain();
|
---|
| 96 | }
|
---|
[81060] | 97 | }
|
---|
[100063] | 98 | else if (VBClGetDisplayServerType() == VBGHDISPLAYSERVERTYPE_PURE_WAYLAND)
|
---|
[99590] | 99 | {
|
---|
[99977] | 100 | VBClLogError("Shared Clipboard for Wayland not supported yet!\n");
|
---|
[99590] | 101 | }
|
---|
| 102 |
|
---|
[52562] | 103 | if (RT_FAILURE(rc))
|
---|
[81060] | 104 | VBClLogError("Service terminated abnormally with %Rrc\n", rc);
|
---|
| 105 |
|
---|
| 106 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND)
|
---|
| 107 | rc = VINF_SUCCESS; /* Prevent automatic restart by daemon script if host service not available. */
|
---|
| 108 |
|
---|
[52562] | 109 | return rc;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[86873] | 112 | /**
|
---|
| 113 | * @interface_method_impl{VBCLSERVICE,pfnStop}
|
---|
| 114 | */
|
---|
| 115 | static DECLCALLBACK(void) vbclShClStop(void)
|
---|
[54008] | 116 | {
|
---|
[86871] | 117 | /* Disconnect from the host service.
|
---|
| 118 | * This will also send a VBOX_SHCL_HOST_MSG_QUIT from the host so that we can break out from our message worker. */
|
---|
| 119 | VbglR3ClipboardDisconnect(g_Ctx.CmdCtx.idClient);
|
---|
| 120 | g_Ctx.CmdCtx.idClient = 0;
|
---|
| 121 | }
|
---|
[81960] | 122 |
|
---|
[86873] | 123 | /**
|
---|
| 124 | * @interface_method_impl{VBCLSERVICE,pfnTerm}
|
---|
| 125 | */
|
---|
| 126 | static DECLCALLBACK(int) vbclShClTerm(void)
|
---|
[86871] | 127 | {
|
---|
[81960] | 128 | #ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
|
---|
| 129 | ShClTransferCtxDestroy(&g_Ctx.TransferCtx);
|
---|
| 130 | #endif
|
---|
[100287] | 131 | VBClX11ClipboardDestroy();
|
---|
[86871] | 132 |
|
---|
| 133 | return VINF_SUCCESS;
|
---|
[54008] | 134 | }
|
---|
| 135 |
|
---|
[86871] | 136 | VBCLSERVICE g_SvcClipboard =
|
---|
[52562] | 137 | {
|
---|
[86871] | 138 | "shcl", /* szName */
|
---|
| 139 | "Shared Clipboard", /* pszDescription */
|
---|
[98474] | 140 | ".vboxclient-clipboard", /* pszPidFilePathTemplate */
|
---|
[86871] | 141 | NULL, /* pszUsage */
|
---|
| 142 | NULL, /* pszOptions */
|
---|
| 143 | NULL, /* pfnOption */
|
---|
| 144 | vbclShClInit, /* pfnInit */
|
---|
| 145 | vbclShClWorker, /* pfnWorker */
|
---|
| 146 | vbclShClStop, /* pfnStop*/
|
---|
| 147 | vbclShClTerm /* pfnTerm */
|
---|
[18360] | 148 | };
|
---|
| 149 |
|
---|