VirtualBox

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

Last change on this file since 68462 was 68462, checked in by vboxsync, 7 years ago

VBoxGuestLib: Use common VbglR3HGCMDisconnect to disconnect.

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1/* $Id: VBoxGuestR3LibClipboard.cpp 68462 2017-08-18 11:23:02Z 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 return VbglR3HGCMDisconnect(idClient);
75}
76
77
78/**
79 * Get a host message.
80 *
81 * This will block until a message becomes available.
82 *
83 * @returns VBox status code.
84 * @param idClient The client id returned by VbglR3ClipboardConnect().
85 * @param pMsg Where to store the message id.
86 * @param pfFormats Where to store the format(s) the message applies to.
87 */
88VBGLR3DECL(int) VbglR3ClipboardGetHostMsg(HGCMCLIENTID idClient, uint32_t *pMsg, uint32_t *pfFormats)
89{
90 VBoxClipboardGetHostMsg Msg;
91
92 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG, 2);
93 VbglHGCMParmUInt32Set(&Msg.msg, 0);
94 VbglHGCMParmUInt32Set(&Msg.formats, 0);
95
96 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
97 if (RT_SUCCESS(rc))
98 {
99 rc = Msg.hdr.result;
100 if (RT_SUCCESS(rc))
101 {
102 uint32_t u32Msg;
103 rc = VbglHGCMParmUInt32Get(&Msg.msg, &u32Msg);
104 if (RT_SUCCESS(rc))
105 {
106 uint32_t fFormats;
107 rc = VbglHGCMParmUInt32Get(&Msg.formats, &fFormats);
108 if (RT_SUCCESS(rc))
109 {
110 *pMsg = u32Msg;
111 *pfFormats = fFormats;
112 return Msg.hdr.result;
113 }
114 }
115 }
116 }
117
118 return rc;
119}
120
121
122/**
123 * Reads data from the host clipboard.
124 *
125 * @returns VBox status code.
126 * @retval VINF_BUFFER_OVERFLOW If there is more data available than the caller provided buffer space for.
127 *
128 * @param idClient The client id returned by VbglR3ClipboardConnect().
129 * @param fFormat The format we're requesting the data in.
130 * @param pv Where to store the data.
131 * @param cb The size of the buffer pointed to by pv.
132 * @param pcb The actual size of the host clipboard data. May be larger than cb.
133 */
134VBGLR3DECL(int) VbglR3ClipboardReadData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcb)
135{
136 VBoxClipboardReadData Msg;
137
138 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_READ_DATA, 3);
139 VbglHGCMParmUInt32Set(&Msg.format, fFormat);
140 VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
141 VbglHGCMParmUInt32Set(&Msg.size, 0);
142
143 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
144 if (RT_SUCCESS(rc))
145 {
146 rc = Msg.hdr.result;
147 if (RT_SUCCESS(rc))
148 {
149 uint32_t cbActual;
150 rc = VbglHGCMParmUInt32Get(&Msg.size, &cbActual);
151 if (RT_SUCCESS(rc))
152 {
153 *pcb = cbActual;
154 if (cbActual > cb)
155 return VINF_BUFFER_OVERFLOW;
156 return Msg.hdr.result;
157 }
158 }
159 }
160 return rc;
161}
162
163
164/**
165 * Advertises guest clipboard formats to the host.
166 *
167 * @returns VBox status code.
168 * @param idClient The client id returned by VbglR3ClipboardConnect().
169 * @param fFormats The formats to advertise.
170 */
171VBGLR3DECL(int) VbglR3ClipboardReportFormats(HGCMCLIENTID idClient, uint32_t fFormats)
172{
173 VBoxClipboardFormats Msg;
174
175 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_FORMATS, 1);
176 VbglHGCMParmUInt32Set(&Msg.formats, fFormats);
177
178 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
179 if (RT_SUCCESS(rc))
180 rc = Msg.hdr.result;
181 return rc;
182}
183
184
185/**
186 * Send guest clipboard data to the host.
187 *
188 * This is usually called in reply to a VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA message
189 * from the host.
190 *
191 * @returns VBox status code.
192 * @param idClient The client id returned by VbglR3ClipboardConnect().
193 * @param fFormat The format of the data.
194 * @param pv The data.
195 * @param cb The size of the data.
196 */
197VBGLR3DECL(int) VbglR3ClipboardWriteData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb)
198{
199 VBoxClipboardWriteData Msg;
200 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA, 2);
201 VbglHGCMParmUInt32Set(&Msg.format, fFormat);
202 VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
203
204 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
205 if (RT_SUCCESS(rc))
206 rc = Msg.hdr.result;
207 return rc;
208}
209
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