1 | /* $Id: VBoxGuestR3LibClipboard.cpp 69496 2017-10-28 14:55:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Clipboard.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include "VBoxGuestR3LibInternal.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Connects to the clipboard service.
|
---|
39 | *
|
---|
40 | * @returns VBox status code
|
---|
41 | * @param pidClient Where to put the client id on success. The client id
|
---|
42 | * must be passed to all the other clipboard calls.
|
---|
43 | */
|
---|
44 | VBGLR3DECL(int) VbglR3ClipboardConnect(HGCMCLIENTID *pidClient)
|
---|
45 | {
|
---|
46 | int rc = VbglR3HGCMConnect("VBoxSharedClipboard", pidClient);
|
---|
47 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND)
|
---|
48 | rc = VINF_PERMISSION_DENIED;
|
---|
49 | return rc;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Disconnect from the clipboard service.
|
---|
55 | *
|
---|
56 | * @returns VBox status code.
|
---|
57 | * @param idClient The client id returned by VbglR3ClipboardConnect().
|
---|
58 | */
|
---|
59 | VBGLR3DECL(int) VbglR3ClipboardDisconnect(HGCMCLIENTID idClient)
|
---|
60 | {
|
---|
61 | return VbglR3HGCMDisconnect(idClient);
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Get a host message.
|
---|
67 | *
|
---|
68 | * This will block until a message becomes available.
|
---|
69 | *
|
---|
70 | * @returns VBox status code.
|
---|
71 | * @param idClient The client id returned by VbglR3ClipboardConnect().
|
---|
72 | * @param pidMsg Where to store the message id.
|
---|
73 | * @param pfFormats Where to store the format(s) the message applies to.
|
---|
74 | */
|
---|
75 | VBGLR3DECL(int) VbglR3ClipboardGetHostMsg(HGCMCLIENTID idClient, uint32_t *pidMsg, uint32_t *pfFormats)
|
---|
76 | {
|
---|
77 | VBoxClipboardGetHostMsg Msg;
|
---|
78 |
|
---|
79 | VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG, 2);
|
---|
80 | VbglHGCMParmUInt32Set(&Msg.msg, 0);
|
---|
81 | VbglHGCMParmUInt32Set(&Msg.formats, 0);
|
---|
82 |
|
---|
83 | int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
|
---|
84 | if (RT_SUCCESS(rc))
|
---|
85 | {
|
---|
86 | int rc2 = VbglHGCMParmUInt32Get(&Msg.msg, pidMsg);
|
---|
87 | if (RT_SUCCESS(rc))
|
---|
88 | {
|
---|
89 | rc2 = VbglHGCMParmUInt32Get(&Msg.formats, pfFormats);
|
---|
90 | if (RT_SUCCESS(rc2))
|
---|
91 | return rc;
|
---|
92 | }
|
---|
93 | rc = rc2;
|
---|
94 | }
|
---|
95 | *pidMsg = UINT32_MAX - 1;
|
---|
96 | *pfFormats = UINT32_MAX;
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Reads data from the host clipboard.
|
---|
103 | *
|
---|
104 | * @returns VBox status code.
|
---|
105 | * @retval VINF_BUFFER_OVERFLOW If there is more data available than the caller provided buffer space for.
|
---|
106 | *
|
---|
107 | * @param idClient The client id returned by VbglR3ClipboardConnect().
|
---|
108 | * @param fFormat The format we're requesting the data in.
|
---|
109 | * @param pv Where to store the data.
|
---|
110 | * @param cb The size of the buffer pointed to by pv.
|
---|
111 | * @param pcb The actual size of the host clipboard data. May be larger than cb.
|
---|
112 | */
|
---|
113 | VBGLR3DECL(int) VbglR3ClipboardReadData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcb)
|
---|
114 | {
|
---|
115 | VBoxClipboardReadData Msg;
|
---|
116 |
|
---|
117 | VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_READ_DATA, 3);
|
---|
118 | VbglHGCMParmUInt32Set(&Msg.format, fFormat);
|
---|
119 | VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
|
---|
120 | VbglHGCMParmUInt32Set(&Msg.size, 0);
|
---|
121 |
|
---|
122 | int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
|
---|
123 | if (RT_SUCCESS(rc))
|
---|
124 | {
|
---|
125 | uint32_t cbActual;
|
---|
126 | int rc2 = VbglHGCMParmUInt32Get(&Msg.size, &cbActual);
|
---|
127 | if (RT_SUCCESS(rc2))
|
---|
128 | {
|
---|
129 | *pcb = cbActual;
|
---|
130 | if (cbActual > cb)
|
---|
131 | return VINF_BUFFER_OVERFLOW;
|
---|
132 | return rc;
|
---|
133 | }
|
---|
134 | rc = rc2;
|
---|
135 | }
|
---|
136 | return rc;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Advertises guest clipboard formats to the host.
|
---|
142 | *
|
---|
143 | * @returns VBox status code.
|
---|
144 | * @param idClient The client id returned by VbglR3ClipboardConnect().
|
---|
145 | * @param fFormats The formats to advertise.
|
---|
146 | */
|
---|
147 | VBGLR3DECL(int) VbglR3ClipboardReportFormats(HGCMCLIENTID idClient, uint32_t fFormats)
|
---|
148 | {
|
---|
149 | VBoxClipboardFormats Msg;
|
---|
150 |
|
---|
151 | VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_FORMATS, 1);
|
---|
152 | VbglHGCMParmUInt32Set(&Msg.formats, fFormats);
|
---|
153 |
|
---|
154 | return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Send guest clipboard data to the host.
|
---|
160 | *
|
---|
161 | * This is usually called in reply to a VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA message
|
---|
162 | * from the host.
|
---|
163 | *
|
---|
164 | * @returns VBox status code.
|
---|
165 | * @param idClient The client id returned by VbglR3ClipboardConnect().
|
---|
166 | * @param fFormat The format of the data.
|
---|
167 | * @param pv The data.
|
---|
168 | * @param cb The size of the data.
|
---|
169 | */
|
---|
170 | VBGLR3DECL(int) VbglR3ClipboardWriteData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb)
|
---|
171 | {
|
---|
172 | VBoxClipboardWriteData Msg;
|
---|
173 | VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA, 2);
|
---|
174 | VbglHGCMParmUInt32Set(&Msg.format, fFormat);
|
---|
175 | VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
|
---|
176 |
|
---|
177 | return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
|
---|
178 | }
|
---|
179 |
|
---|