VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibClipboard.cpp@ 62521

Last change on this file since 62521 was 62521, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: VBoxGuestR3LibClipboard.cpp 62521 2016-07-22 19:16:33Z 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 */
44VBGLR3DECL(int) VbglR3ClipboardConnect(HGCMCLIENTID *pidClient )
45{
46 VBoxGuestHGCMConnectInfo Info;
47 Info.result = VERR_WRONG_ORDER;
48 Info.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
49 RT_ZERO(Info.Loc.u);
50 strcpy(Info.Loc.u.host.achName, "VBoxSharedClipboard");
51 Info.u32ClientID = UINT32_MAX; /* try make valgrind shut up. */
52
53 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CONNECT, &Info, sizeof(Info));
54 if (RT_SUCCESS(rc))
55 {
56 rc = Info.result;
57 if (RT_SUCCESS(rc))
58 *pidClient = Info.u32ClientID;
59 }
60 if (rc == VERR_HGCM_SERVICE_NOT_FOUND)
61 rc = VINF_PERMISSION_DENIED;
62 return rc;
63}
64
65
66/**
67 * Disconnect from the clipboard service.
68 *
69 * @returns VBox status code.
70 * @param idClient The client id returned by VbglR3ClipboardConnect().
71 */
72VBGLR3DECL(int) VbglR3ClipboardDisconnect(HGCMCLIENTID idClient)
73{
74 VBoxGuestHGCMDisconnectInfo Info;
75 Info.result = VERR_WRONG_ORDER;
76 Info.u32ClientID = idClient;
77
78 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_DISCONNECT, &Info, sizeof(Info));
79 if (RT_SUCCESS(rc))
80 rc = Info.result;
81 return rc;
82}
83
84
85/**
86 * Get a host message.
87 *
88 * This will block until a message becomes available.
89 *
90 * @returns VBox status code.
91 * @param idClient The client id returned by VbglR3ClipboardConnect().
92 * @param pMsg Where to store the message id.
93 * @param pfFormats Where to store the format(s) the message applies to.
94 */
95VBGLR3DECL(int) VbglR3ClipboardGetHostMsg(HGCMCLIENTID idClient, uint32_t *pMsg, uint32_t *pfFormats)
96{
97 VBoxClipboardGetHostMsg Msg;
98
99 Msg.hdr.result = VERR_WRONG_ORDER;
100 Msg.hdr.u32ClientID = idClient;
101 Msg.hdr.u32Function = VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG;
102 Msg.hdr.cParms = 2;
103 VbglHGCMParmUInt32Set(&Msg.msg, 0);
104 VbglHGCMParmUInt32Set(&Msg.formats, 0);
105
106 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
107 if (RT_SUCCESS(rc))
108 {
109 rc = Msg.hdr.result;
110 if (RT_SUCCESS(rc))
111 {
112 uint32_t u32Msg;
113 rc = VbglHGCMParmUInt32Get(&Msg.msg, &u32Msg);
114 if (RT_SUCCESS(rc))
115 {
116 uint32_t fFormats;
117 rc = VbglHGCMParmUInt32Get(&Msg.formats, &fFormats);
118 if (RT_SUCCESS(rc))
119 {
120 *pMsg = u32Msg;
121 *pfFormats = fFormats;
122 return Msg.hdr.result;
123 }
124 }
125 }
126 }
127
128 return rc;
129}
130
131
132/**
133 * Reads data from the host clipboard.
134 *
135 * @returns VBox status code.
136 * @retval VINF_BUFFER_OVERFLOW If there is more data available than the caller provided buffer space for.
137 *
138 * @param idClient The client id returned by VbglR3ClipboardConnect().
139 * @param fFormat The format we're requesting the data in.
140 * @param pv Where to store the data.
141 * @param cb The size of the buffer pointed to by pv.
142 * @param pcb The actual size of the host clipboard data. May be larger than cb.
143 */
144VBGLR3DECL(int) VbglR3ClipboardReadData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcb)
145{
146 VBoxClipboardReadData Msg;
147
148 Msg.hdr.result = VERR_WRONG_ORDER;
149 Msg.hdr.u32ClientID = idClient;
150 Msg.hdr.u32Function = VBOX_SHARED_CLIPBOARD_FN_READ_DATA;
151 Msg.hdr.cParms = 3;
152 VbglHGCMParmUInt32Set(&Msg.format, fFormat);
153 VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
154 VbglHGCMParmUInt32Set(&Msg.size, 0);
155
156 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
157 if (RT_SUCCESS(rc))
158 {
159 rc = Msg.hdr.result;
160 if (RT_SUCCESS(rc))
161 {
162 uint32_t cbActual;
163 rc = VbglHGCMParmUInt32Get(&Msg.size, &cbActual);
164 if (RT_SUCCESS(rc))
165 {
166 *pcb = cbActual;
167 if (cbActual > cb)
168 return VINF_BUFFER_OVERFLOW;
169 return Msg.hdr.result;
170 }
171 }
172 }
173 return rc;
174}
175
176
177/**
178 * Advertises guest clipboard formats to the host.
179 *
180 * @returns VBox status code.
181 * @param idClient The client id returned by VbglR3ClipboardConnect().
182 * @param fFormats The formats to advertise.
183 */
184VBGLR3DECL(int) VbglR3ClipboardReportFormats(HGCMCLIENTID idClient, uint32_t fFormats)
185{
186 VBoxClipboardFormats Msg;
187
188 Msg.hdr.result = VERR_WRONG_ORDER;
189 Msg.hdr.u32ClientID = idClient;
190 Msg.hdr.u32Function = VBOX_SHARED_CLIPBOARD_FN_FORMATS;
191 Msg.hdr.cParms = 1;
192 VbglHGCMParmUInt32Set(&Msg.formats, fFormats);
193
194 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
195 if (RT_SUCCESS(rc))
196 rc = Msg.hdr.result;
197 return rc;
198}
199
200
201/**
202 * Send guest clipboard data to the host.
203 *
204 * This is usually called in reply to a VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA message
205 * from the host.
206 *
207 * @returns VBox status code.
208 * @param idClient The client id returned by VbglR3ClipboardConnect().
209 * @param fFormat The format of the data.
210 * @param pv The data.
211 * @param cb The size of the data.
212 */
213VBGLR3DECL(int) VbglR3ClipboardWriteData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb)
214{
215 VBoxClipboardWriteData Msg;
216 Msg.hdr.result = VERR_WRONG_ORDER;
217 Msg.hdr.u32ClientID = idClient;
218 Msg.hdr.u32Function = VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA;
219 Msg.hdr.cParms = 2;
220 VbglHGCMParmUInt32Set(&Msg.format, fFormat);
221 VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
222
223 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
224 if (RT_SUCCESS(rc))
225 rc = Msg.hdr.result;
226 return rc;
227}
228
Note: See TracBrowser for help on using the repository browser.

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