VirtualBox

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

Last change on this file since 21845 was 21211, checked in by vboxsync, 16 years ago

VBoxGuest.h,VBoxGuestLib: Moved the VbglR3 API out of VBoxGuest.h and did some cleanup.

  • 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.0 KB
Line 
1/* $Id: VBoxGuestR3LibClipboard.cpp 21211 2009-07-03 15:12:24Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Clipboard.
4 */
5
6/*
7 * Copyright (C) 2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include <VBox/HostServices/VBoxClipboardSvc.h>
27#include <iprt/assert.h>
28#include <iprt/string.h>
29#include "VBGLR3Internal.h"
30
31
32/**
33 * Connects to the clipboard service.
34 *
35 * @returns VBox status code
36 * @param pu32ClientId Where to put the client id on success. The client id
37 * must be passed to all the other clipboard calls.
38 */
39VBGLR3DECL(int) VbglR3ClipboardConnect(uint32_t *pu32ClientId)
40{
41 VBoxGuestHGCMConnectInfo Info;
42 Info.result = VERR_WRONG_ORDER;
43 Info.Loc.type = VMMDevHGCMLoc_LocalHost_Existing;
44 memset(&Info.Loc.u, 0, sizeof(Info.Loc.u));
45 strcpy(Info.Loc.u.host.achName, "VBoxSharedClipboard");
46 Info.u32ClientID = UINT32_MAX; /* try make valgrid shut up. */
47
48 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CONNECT, &Info, sizeof(Info));
49 if (RT_SUCCESS(rc))
50 {
51 rc = Info.result;
52 if (RT_SUCCESS(rc))
53 *pu32ClientId = Info.u32ClientID;
54 }
55 return rc;
56}
57
58
59/**
60 * Disconnect from the clipboard service.
61 *
62 * @returns VBox status code.
63 * @param u32ClientId The client id returned by VbglR3ClipboardConnect().
64 */
65VBGLR3DECL(int) VbglR3ClipboardDisconnect(uint32_t u32ClientId)
66{
67 VBoxGuestHGCMDisconnectInfo Info;
68 Info.result = VERR_WRONG_ORDER;
69 Info.u32ClientID = u32ClientId;
70
71 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_DISCONNECT, &Info, sizeof(Info));
72 if (RT_SUCCESS(rc))
73 rc = Info.result;
74 return rc;
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 u32ClientId 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(uint32_t u32ClientId, uint32_t *pMsg, uint32_t *pfFormats)
89{
90 VBoxClipboardGetHostMsg Msg;
91
92 Msg.hdr.result = VERR_WRONG_ORDER;
93 Msg.hdr.u32ClientID = u32ClientId;
94 Msg.hdr.u32Function = VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG;
95 Msg.hdr.cParms = 2;
96 VbglHGCMParmUInt32Set(&Msg.msg, 0);
97 VbglHGCMParmUInt32Set(&Msg.formats, 0);
98
99 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
100 if (RT_SUCCESS(rc))
101 {
102 rc = Msg.hdr.result;
103 if (RT_SUCCESS(rc))
104 {
105 uint32_t u32Msg;
106 rc = VbglHGCMParmUInt32Get(&Msg.msg, &u32Msg);
107 if (RT_SUCCESS(rc))
108 {
109 uint32_t fFormats;
110 rc = VbglHGCMParmUInt32Get(&Msg.formats, &fFormats);
111 if (RT_SUCCESS(rc))
112 {
113 *pMsg = u32Msg;
114 *pfFormats = fFormats;
115 return Msg.hdr.result;
116 }
117 }
118 }
119 }
120
121 return rc;
122}
123
124
125/**
126 * Reads data from the host clipboard.
127 *
128 * @returns VBox status code.
129 * @retval VINF_BUFFER_OVERFLOW If there is more data available than the caller provided buffer space for.
130 *
131 * @param u32ClientId The client id returned by VbglR3ClipboardConnect().
132 * @param fFormat The format we're requesting the data in.
133 * @param pv Where to store the data.
134 * @param cb The size of the buffer pointed to by pv.
135 * @param pcb The actual size of the host clipboard data. May be larger than cb.
136 */
137VBGLR3DECL(int) VbglR3ClipboardReadData(uint32_t u32ClientId, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcb)
138{
139 VBoxClipboardReadData Msg;
140
141 Msg.hdr.result = VERR_WRONG_ORDER;
142 Msg.hdr.u32ClientID = u32ClientId;
143 Msg.hdr.u32Function = VBOX_SHARED_CLIPBOARD_FN_READ_DATA;
144 Msg.hdr.cParms = 3;
145 VbglHGCMParmUInt32Set(&Msg.format, fFormat);
146 VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
147 VbglHGCMParmUInt32Set(&Msg.size, 0);
148
149 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
150 if (RT_SUCCESS(rc))
151 {
152 rc = Msg.hdr.result;
153 if (RT_SUCCESS(rc))
154 {
155 uint32_t cbActual;
156 rc = VbglHGCMParmUInt32Get(&Msg.size, &cbActual);
157 if (RT_SUCCESS(rc))
158 {
159 *pcb = cbActual;
160 if (cbActual > cb)
161 return VINF_BUFFER_OVERFLOW;
162 return Msg.hdr.result;
163 }
164 }
165 }
166 return rc;
167}
168
169
170/**
171 * Advertises guest clipboard formats to the host.
172 *
173 * @returns VBox status code.
174 * @param u32ClientId The client id returned by VbglR3ClipboardConnect().
175 * @param fFormats The formats to advertise.
176 */
177VBGLR3DECL(int) VbglR3ClipboardReportFormats(uint32_t u32ClientId, uint32_t fFormats)
178{
179 VBoxClipboardFormats Msg;
180
181 Msg.hdr.result = VERR_WRONG_ORDER;
182 Msg.hdr.u32ClientID = u32ClientId;
183 Msg.hdr.u32Function = VBOX_SHARED_CLIPBOARD_FN_FORMATS;
184 Msg.hdr.cParms = 1;
185 VbglHGCMParmUInt32Set(&Msg.formats, fFormats);
186
187 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
188 if (RT_SUCCESS(rc))
189 rc = Msg.hdr.result;
190 return rc;
191}
192
193
194/**
195 * Send guest clipboard data to the host.
196 *
197 * This is usually called in reply to a VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA message
198 * from the host.
199 *
200 * @returns VBox status code.
201 * @param u32ClientId The client id returned by VbglR3ClipboardConnect().
202 * @param fFormat The format of the data.
203 * @param pv The data.
204 * @param cb The size of the data.
205 */
206VBGLR3DECL(int) VbglR3ClipboardWriteData(uint32_t u32ClientId, uint32_t fFormat, void *pv, uint32_t cb)
207{
208 VBoxClipboardWriteData Msg;
209 Msg.hdr.result = VERR_WRONG_ORDER;
210 Msg.hdr.u32ClientID = u32ClientId;
211 Msg.hdr.u32Function = VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA;
212 Msg.hdr.cParms = 2;
213 VbglHGCMParmUInt32Set(&Msg.format, fFormat);
214 VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
215
216 int rc = vbglR3DoIOCtl(VBOXGUEST_IOCTL_HGCM_CALL(sizeof(Msg)), &Msg, sizeof(Msg));
217 if (RT_SUCCESS(rc))
218 rc = Msg.hdr.result;
219 return rc;
220}
221
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