summaryrefslogtreecommitdiff
path: root/src/VBox/Additions/x11/VBoxClient/clipboard.cpp
blob: 24ba7e9d2c69d378916e3b45fb852a3dd95c6447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/** $Id: clipboard.cpp $ */
/** @file
 * Guest Additions - X11 Shared Clipboard.
 */

/*
 * Copyright (C) 2007-2012 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

/****************************************************************************
*   Header Files                                                            *
****************************************************************************/
#include <iprt/alloc.h>
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/initterm.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/process.h>
#include <iprt/semaphore.h>

#include <VBox/log.h>
#include <VBox/VBoxGuestLib.h>
#include <VBox/HostServices/VBoxClipboardSvc.h>
#include <VBox/GuestHost/SharedClipboard.h>

#include "VBoxClient.h"

/****************************************************************************
*   Global Variables                                                        *
****************************************************************************/

/**
 * Global clipboard context information.
 */
struct _VBOXCLIPBOARDCONTEXT
{
    /** Client ID for the clipboard subsystem */
    uint32_t client;

    /** Pointer to the X11 clipboard backend */
    CLIPBACKEND *pBackend;
};

/** Only one client is supported. There seems to be no need for more clients. */
static VBOXCLIPBOARDCONTEXT g_ctx;


/**
 * Transfer clipboard data from the guest to the host.
 *
 * @returns VBox result code
 * @param   u32Format The format of the data being sent
 * @param   pv        Pointer to the data being sent
 * @param   cb        Size of the data being sent in bytes
 */
static int vboxClipboardSendData(uint32_t u32Format, void *pv, uint32_t cb)
{
    int rc;
    LogRelFlowFunc(("u32Format=%d, pv=%p, cb=%d\n", u32Format, pv, cb));
    rc = VbglR3ClipboardWriteData(g_ctx.client, u32Format, pv, cb);
    LogRelFlowFunc(("rc=%Rrc\n", rc));
    return rc;
}


/**
 * Get clipboard data from the host.
 *
 * @returns VBox result code
 * @param   u32Format The format of the data being requested
 * @retval  ppv       On success and if pcb > 0, this will point to a buffer
 *                    to be freed with RTMemFree containing the data read.
 * @retval  pcb       On success, this contains the number of bytes of data
 *                    returned
 */
int ClipRequestDataForX11(VBOXCLIPBOARDCONTEXT *pCtx, uint32_t u32Format,
                          void **ppv, uint32_t *pcb)
{
    int rc = VINF_SUCCESS;
    uint32_t cb = 1024;
    void *pv = RTMemAlloc(cb);

    *ppv = 0;
    LogRelFlowFunc(("u32Format=%u\n", u32Format));
    if (RT_UNLIKELY(!pv))
        rc = VERR_NO_MEMORY;
    if (RT_SUCCESS(rc))
        rc = VbglR3ClipboardReadData(g_ctx.client, u32Format, pv, cb, pcb);
    if (RT_SUCCESS(rc) && (rc != VINF_BUFFER_OVERFLOW))
        *ppv = pv;
    /* A return value of VINF_BUFFER_OVERFLOW tells us to try again with a
     * larger buffer.  The size of the buffer needed is placed in *pcb.
     * So we start all over again. */
    if (rc == VINF_BUFFER_OVERFLOW)
    {
        cb = *pcb;
        RTMemFree(pv);
        pv = RTMemAlloc(cb);
        if (RT_UNLIKELY(!pv))
            rc = VERR_NO_MEMORY;
        if (RT_SUCCESS(rc))
            rc = VbglR3ClipboardReadData(g_ctx.client, u32Format, pv, cb, pcb);
        if (RT_SUCCESS(rc) && (rc != VINF_BUFFER_OVERFLOW))
            *ppv = pv;
    }
    /* Catch other errors. This also catches the case in which the buffer was
     * too small a second time, possibly because the clipboard contents
     * changed half-way through the operation.  Since we can't say whether or
     * not this is actually an error, we just return size 0.
     */
    if (RT_FAILURE(rc) || (VINF_BUFFER_OVERFLOW == rc))
    {
        *pcb = 0;
        if (pv != NULL)
            RTMemFree(pv);
    }
    LogRelFlowFunc(("returning %Rrc\n", rc));
    if (RT_SUCCESS(rc))
        LogRelFlow(("    *pcb=%d\n", *pcb));
    return rc;
}

/** Opaque data structure describing a request from the host for clipboard
 * data, passed in when the request is forwarded to the X11 backend so that
 * it can be completed correctly. */
struct _CLIPREADCBREQ
{
    /** The data format that was requested. */
    uint32_t u32Format;
};

/**
 * Tell the host that new clipboard formats are available.
 *
 * @param u32Formats      The formats to advertise
 */
void ClipReportX11Formats(VBOXCLIPBOARDCONTEXT *pCtx, uint32_t u32Formats)
{
    int rc;
    LogRelFlowFunc(("u32Formats=%d\n", u32Formats));
    rc = VbglR3ClipboardReportFormats(g_ctx.client, u32Formats);
    LogRelFlowFunc(("rc=%Rrc\n", rc));
}

/** This is called by the backend to tell us that a request for data from
 * X11 has completed.
 * @param  pCtx      Our context information
 * @param  rc        the iprt result code of the request
 * @param  pReq      the request structure that we passed in when we started
 *                   the request.  We RTMemFree() this in this function.
 * @param  pv        the clipboard data returned from X11 if the request
 *                   succeeded (see @a rc)
 * @param  cb        the size of the data in @a pv
 */
void ClipCompleteDataRequestFromX11(VBOXCLIPBOARDCONTEXT *pCtx, int rc,
                                    CLIPREADCBREQ *pReq, void *pv,
                                    uint32_t cb)
{
    if (RT_SUCCESS(rc))
        vboxClipboardSendData(pReq->u32Format, pv, cb);
    else
        vboxClipboardSendData(0, NULL, 0);
    RTMemFree(pReq);
}

/**
 * Connect the guest clipboard to the host.
 *
 * @returns VBox status code
 */
int vboxClipboardConnect(void)
{
    int rc = VINF_SUCCESS;
    LogRelFlowFunc(("\n"));

    /* Sanity */
    AssertReturn(g_ctx.client == 0, VERR_WRONG_ORDER);
    g_ctx.pBackend = ClipConstructX11(&g_ctx, false);
    if (!g_ctx.pBackend)
        rc = VERR_NO_MEMORY;
    if (RT_SUCCESS(rc))
        rc = ClipStartX11(g_ctx.pBackend);
    if (RT_SUCCESS(rc))
    {
        rc = VbglR3ClipboardConnect(&g_ctx.client);
        if (RT_FAILURE(rc))
            LogRel(("Error connecting to host. rc=%Rrc\n", rc));
        else if (!g_ctx.client)
        {
            LogRel(("Invalid client ID of 0\n"));
            rc = VERR_NOT_SUPPORTED;
        }
    }

    if (RT_FAILURE(rc) && g_ctx.pBackend)
        ClipDestructX11(g_ctx.pBackend);
    LogRelFlowFunc(("g_ctx.client=%u rc=%Rrc\n", g_ctx.client, rc));
    return rc;
}

/**
 * The main loop of our clipboard reader.
 */
int vboxClipboardMain(void)
{
    int rc;
    LogRelFlowFunc(("Starting guest clipboard service\n"));
    bool fExiting = false;

    while (!fExiting)
    {
        uint32_t Msg;
        uint32_t fFormats;
        rc = VbglR3ClipboardGetHostMsg(g_ctx.client, &Msg, &fFormats);
        if (RT_SUCCESS(rc))
        {
            switch (Msg)
            {
                case VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS:
                {
                    /* The host has announced available clipboard formats.
                     * Save the information so that it is available for
                     * future requests from guest applications.
                     */
                    LogRelFlowFunc(("VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS fFormats=%x\n", fFormats));
                    ClipAnnounceFormatToX11(g_ctx.pBackend, fFormats);
                    break;
                }

                case VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA:
                {
                    /* The host needs data in the specified format. */
                    LogRelFlowFunc(("VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA fFormats=%x\n", fFormats));
                    CLIPREADCBREQ *pReq;
                    pReq = (CLIPREADCBREQ *)RTMemAllocZ(sizeof(*pReq));
                    if (!pReq)
                    {
                        rc = VERR_NO_MEMORY;
                        fExiting = true;
                    }
                    else
                    {
                        pReq->u32Format = fFormats;
                        ClipRequestDataFromX11(g_ctx.pBackend, fFormats,
                                               pReq);
                    }
                    break;
                }

                case VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT:
                {
                    /* The host is terminating. */
                    LogRelFlowFunc(("VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT\n"));
                    if (RT_SUCCESS(ClipStopX11(g_ctx.pBackend)))
                        ClipDestructX11(g_ctx.pBackend);
                    fExiting = true;
                    break;
                }

                default:
                    LogRel2(("Unsupported message from host!!!\n"));
            }
        }

        LogRelFlow(("processed host event rc = %d\n", rc));
    }
    LogRelFlowFunc(("rc=%d\n", rc));
    return rc;
}

class ClipboardService : public VBoxClient::Service
{
public:
    virtual const char *getPidFilePath()
    {
        return ".vboxclient-clipboard.pid";
    }
    virtual int run(bool fDaemonised /* = false */)
    {
        int rc = vboxClipboardConnect();
        if (RT_SUCCESS(rc))
            rc = vboxClipboardMain();
        if (RT_FAILURE(rc))
            LogRelFunc(("guest clipboard service terminated abnormally: return code %Rrc\n", rc));
        return rc;
    }
    virtual void cleanup()
    {
        /* Nothing to do. */
    }
};

VBoxClient::Service *VBoxClient::GetClipboardService()
{
    return new ClipboardService;
}