1 | /* $Id: VBoxGuestR3LibClipboard.cpp 68464 2017-08-18 11:27:42Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Clipboard.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2016 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 "VBGLR3Internal.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 pMsg 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 *pMsg, 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 = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
84 | if (RT_SUCCESS(rc))
|
---|
85 | {
|
---|
86 | rc = Msg.hdr.result;
|
---|
87 | if (RT_SUCCESS(rc))
|
---|
88 | {
|
---|
89 | uint32_t u32Msg;
|
---|
90 | rc = VbglHGCMParmUInt32Get(&Msg.msg, &u32Msg);
|
---|
91 | if (RT_SUCCESS(rc))
|
---|
92 | {
|
---|
93 | uint32_t fFormats;
|
---|
94 | rc = VbglHGCMParmUInt32Get(&Msg.formats, &fFormats);
|
---|
95 | if (RT_SUCCESS(rc))
|
---|
96 | {
|
---|
97 | *pMsg = u32Msg;
|
---|
98 | *pfFormats = fFormats;
|
---|
99 | return Msg.hdr.result;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | return rc;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Reads data from the host clipboard.
|
---|
111 | *
|
---|
112 | * @returns VBox status code.
|
---|
113 | * @retval VINF_BUFFER_OVERFLOW If there is more data available than the caller provided buffer space for.
|
---|
114 | *
|
---|
115 | * @param idClient The client id returned by VbglR3ClipboardConnect().
|
---|
116 | * @param fFormat The format we're requesting the data in.
|
---|
117 | * @param pv Where to store the data.
|
---|
118 | * @param cb The size of the buffer pointed to by pv.
|
---|
119 | * @param pcb The actual size of the host clipboard data. May be larger than cb.
|
---|
120 | */
|
---|
121 | VBGLR3DECL(int) VbglR3ClipboardReadData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcb)
|
---|
122 | {
|
---|
123 | VBoxClipboardReadData Msg;
|
---|
124 |
|
---|
125 | VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_READ_DATA, 3);
|
---|
126 | VbglHGCMParmUInt32Set(&Msg.format, fFormat);
|
---|
127 | VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
|
---|
128 | VbglHGCMParmUInt32Set(&Msg.size, 0);
|
---|
129 |
|
---|
130 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
131 | if (RT_SUCCESS(rc))
|
---|
132 | {
|
---|
133 | rc = Msg.hdr.result;
|
---|
134 | if (RT_SUCCESS(rc))
|
---|
135 | {
|
---|
136 | uint32_t cbActual;
|
---|
137 | rc = VbglHGCMParmUInt32Get(&Msg.size, &cbActual);
|
---|
138 | if (RT_SUCCESS(rc))
|
---|
139 | {
|
---|
140 | *pcb = cbActual;
|
---|
141 | if (cbActual > cb)
|
---|
142 | return VINF_BUFFER_OVERFLOW;
|
---|
143 | return Msg.hdr.result;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | return rc;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Advertises guest clipboard formats to the host.
|
---|
153 | *
|
---|
154 | * @returns VBox status code.
|
---|
155 | * @param idClient The client id returned by VbglR3ClipboardConnect().
|
---|
156 | * @param fFormats The formats to advertise.
|
---|
157 | */
|
---|
158 | VBGLR3DECL(int) VbglR3ClipboardReportFormats(HGCMCLIENTID idClient, uint32_t fFormats)
|
---|
159 | {
|
---|
160 | VBoxClipboardFormats Msg;
|
---|
161 |
|
---|
162 | VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_FORMATS, 1);
|
---|
163 | VbglHGCMParmUInt32Set(&Msg.formats, fFormats);
|
---|
164 |
|
---|
165 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
166 | if (RT_SUCCESS(rc))
|
---|
167 | rc = Msg.hdr.result;
|
---|
168 | return rc;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Send guest clipboard data to the host.
|
---|
174 | *
|
---|
175 | * This is usually called in reply to a VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA message
|
---|
176 | * from the host.
|
---|
177 | *
|
---|
178 | * @returns VBox status code.
|
---|
179 | * @param idClient The client id returned by VbglR3ClipboardConnect().
|
---|
180 | * @param fFormat The format of the data.
|
---|
181 | * @param pv The data.
|
---|
182 | * @param cb The size of the data.
|
---|
183 | */
|
---|
184 | VBGLR3DECL(int) VbglR3ClipboardWriteData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb)
|
---|
185 | {
|
---|
186 | VBoxClipboardWriteData Msg;
|
---|
187 | VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA, 2);
|
---|
188 | VbglHGCMParmUInt32Set(&Msg.format, fFormat);
|
---|
189 | VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
|
---|
190 |
|
---|
191 | int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
|
---|
192 | if (RT_SUCCESS(rc))
|
---|
193 | rc = Msg.hdr.result;
|
---|
194 | return rc;
|
---|
195 | }
|
---|
196 |
|
---|