1 | /** $Id: VBoxServiceClipboard-os2.cpp 28317 2010-04-14 18:06:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Guest Additions Clipboard Service, OS/2.
|
---|
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 | #define INCL_BASE
|
---|
27 | #define INCL_PM
|
---|
28 | #define INCL_ERRORS
|
---|
29 | #include <os2.h>
|
---|
30 |
|
---|
31 | #include <iprt/thread.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include <iprt/semaphore.h>
|
---|
34 | #include <iprt/time.h>
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include <iprt/param.h>
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <VBox/VBoxGuestLib.h>
|
---|
40 | #include <VBox/HostServices/VBoxClipboardSvc.h>
|
---|
41 | #include "VBoxServiceInternal.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Structures and Typedefs *
|
---|
46 | *******************************************************************************/
|
---|
47 | /** Header for Odin32 specific clipboard entries.
|
---|
48 | * (Used to get the correct size of the data.)
|
---|
49 | */
|
---|
50 | typedef struct _Odin32ClipboardHeader
|
---|
51 | {
|
---|
52 | /** magic number */
|
---|
53 | char achMagic[8];
|
---|
54 | /** Size of the following data.
|
---|
55 | * (The interpretation depends on the type.) */
|
---|
56 | unsigned cbData;
|
---|
57 | /** Odin32 format number. */
|
---|
58 | unsigned uFormat;
|
---|
59 | } CLIPHEADER, *PCLIPHEADER;
|
---|
60 |
|
---|
61 | #define CLIPHEADER_MAGIC "Odin\1\0\1"
|
---|
62 |
|
---|
63 |
|
---|
64 | /*******************************************************************************
|
---|
65 | * Global Variables *
|
---|
66 | *******************************************************************************/
|
---|
67 |
|
---|
68 | /** The control thread (main) handle.
|
---|
69 | * Only used to avoid some queue creation trouble. */
|
---|
70 | static RTTHREAD g_ThreadCtrl = NIL_RTTHREAD;
|
---|
71 | /** The HAB of the control thread (main). */
|
---|
72 | static HAB g_habCtrl = NULLHANDLE;
|
---|
73 | /** The HMQ of the control thread (main). */
|
---|
74 | static HMQ g_hmqCtrl = NULLHANDLE;
|
---|
75 |
|
---|
76 | /** The Listener thread handle. */
|
---|
77 | static RTTHREAD g_ThreadListener = NIL_RTTHREAD;
|
---|
78 | /** The HAB of the listener thread. */
|
---|
79 | static HAB g_habListener = NULLHANDLE;
|
---|
80 | /** The HMQ of the listener thread. */
|
---|
81 | static HMQ g_hmqListener = NULLHANDLE;
|
---|
82 | /** Indicator that gets set if the listener thread is successfully initialized. */
|
---|
83 | static bool volatile g_fListenerOkay = false;
|
---|
84 |
|
---|
85 | /** The HAB of the worker thread. */
|
---|
86 | static HAB g_habWorker = NULLHANDLE;
|
---|
87 | /** The HMQ of the worker thread. */
|
---|
88 | static HMQ g_hmqWorker = NULLHANDLE;
|
---|
89 | /** The object window handle. */
|
---|
90 | static HWND g_hwndWorker = NULLHANDLE;
|
---|
91 | /** The timer id returned by WinStartTimer. */
|
---|
92 | static ULONG g_idWorkerTimer = ~0UL;
|
---|
93 | /** The state of the clipboard.
|
---|
94 | * @remark I'm trying out the 'k' prefix from the mac here, bear with me. */
|
---|
95 | static enum
|
---|
96 | {
|
---|
97 | /** The clipboard hasn't been initialized yet. */
|
---|
98 | kClipboardState_Uninitialized = 0,
|
---|
99 | /** WinSetClipbrdViewer call in progress, ignore WM_DRAWCLIPBOARD. */
|
---|
100 | kClipboardState_SettingViewer,
|
---|
101 | /** We're monitoring the clipboard as a viewer. */
|
---|
102 | kClipboardState_Viewer,
|
---|
103 | /** We're monitoring the clipboard using polling.
|
---|
104 | * This usually means something is wrong... */
|
---|
105 | kClipboardState_Polling,
|
---|
106 | /** We're destroying the clipboard content, ignore WM_DESTROYCLIPBOARD. */
|
---|
107 | kClipboardState_Destroying,
|
---|
108 | /** We're owning the clipboard (i.e. we have data on it). */
|
---|
109 | kClipboardState_Owner
|
---|
110 | } g_enmState = kClipboardState_Uninitialized;
|
---|
111 | /** Set if the clipboard was empty the last time we polled it. */
|
---|
112 | static bool g_fEmptyClipboard = false;
|
---|
113 |
|
---|
114 | /** A clipboard format atom for the dummy clipboard data we insert
|
---|
115 | * watching for clipboard changes. If this format is found on the
|
---|
116 | * clipboard, the empty clipboard function has not been called
|
---|
117 | * since we last polled it. */
|
---|
118 | static ATOM g_atomNothingChanged = 0;
|
---|
119 |
|
---|
120 | /** The clipboard connection client ID. */
|
---|
121 | static uint32_t g_u32ClientId;
|
---|
122 | /** Odin32 CF_UNICODETEXT. See user32.cpp. */
|
---|
123 | static ATOM g_atomOdin32UnicodeText = 0;
|
---|
124 | /** Odin32 CF_UNICODETEXT. See user32.cpp. */
|
---|
125 | #define SZFMT_ODIN32_UNICODETEXT (PCSZ)"Odin32 UnicodeText"
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 |
|
---|
130 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
131 | static DECLCALLBACK(int) VBoxServiceClipboardOS2PreInit(void)
|
---|
132 | {
|
---|
133 | return VINF_SUCCESS;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
138 | static DECLCALLBACK(int) VBoxServiceClipboardOS2Option(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
139 | {
|
---|
140 | return -1;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
145 | static DECLCALLBACK(int) VBoxServiceClipboardOS2Init(void)
|
---|
146 | {
|
---|
147 | int rc = VERR_GENERAL_FAILURE;
|
---|
148 | g_ThreadCtrl = RTThreadSelf();
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * Make PM happy.
|
---|
152 | */
|
---|
153 | PPIB pPib;
|
---|
154 | PTIB pTib;
|
---|
155 | DosGetInfoBlocks(&pTib, &pPib);
|
---|
156 | pPib->pib_ultype = 3; /* PM session type */
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Since we have to send shutdown messages and such from the
|
---|
160 | * service controller (main) thread, create a HAB and HMQ for it.
|
---|
161 | */
|
---|
162 | g_habCtrl = WinInitialize(0);
|
---|
163 | if (g_habCtrl == NULLHANDLE)
|
---|
164 | {
|
---|
165 | VBoxServiceError("WinInitialize(0) failed, lasterr=%lx\n", WinGetLastError(NULLHANDLE));
|
---|
166 | return VERR_GENERAL_FAILURE;
|
---|
167 | }
|
---|
168 | g_hmqCtrl = WinCreateMsgQueue(g_habCtrl, 0);
|
---|
169 | if (g_hmqCtrl != NULLHANDLE)
|
---|
170 | {
|
---|
171 | /*
|
---|
172 | * Create the 'nothing-changed' format.
|
---|
173 | */
|
---|
174 | g_atomNothingChanged = WinAddAtom(WinQuerySystemAtomTable(), (PCSZ)"VirtualBox Clipboard Service");
|
---|
175 | LONG lLastError = WinGetLastError(g_habCtrl);
|
---|
176 | if (g_atomNothingChanged == 0)
|
---|
177 | g_atomNothingChanged = WinFindAtom(WinQuerySystemAtomTable(), (PCSZ)"VirtualBox Clipboard Service");
|
---|
178 | if (g_atomNothingChanged)
|
---|
179 | {
|
---|
180 | /*
|
---|
181 | * Connect to the clipboard service.
|
---|
182 | */
|
---|
183 | VBoxServiceVerbose(4, "clipboard: connecting\n");
|
---|
184 | rc = VbglR3ClipboardConnect(&g_u32ClientId);
|
---|
185 | if (RT_SUCCESS(rc))
|
---|
186 | {
|
---|
187 | /*
|
---|
188 | * Create any extra clipboard type atoms, like the odin unicode text.
|
---|
189 | */
|
---|
190 | g_atomOdin32UnicodeText = WinAddAtom(WinQuerySystemAtomTable(), SZFMT_ODIN32_UNICODETEXT);
|
---|
191 | lLastError = WinGetLastError(g_habCtrl);
|
---|
192 | if (g_atomOdin32UnicodeText == 0)
|
---|
193 | g_atomOdin32UnicodeText = WinFindAtom(WinQuerySystemAtomTable(), SZFMT_ODIN32_UNICODETEXT);
|
---|
194 | if (g_atomOdin32UnicodeText == 0)
|
---|
195 | VBoxServiceError("WinAddAtom() failed, lasterr=%lx; WinFindAtom() failed, lasterror=%lx\n",
|
---|
196 | lLastError, WinGetLastError(g_habCtrl));
|
---|
197 |
|
---|
198 | VBoxServiceVerbose(2, "g_u32ClientId=%RX32 g_atomNothingChanged=%#x g_atomOdin32UnicodeText=%#x\n",
|
---|
199 | g_u32ClientId, g_atomNothingChanged, g_atomOdin32UnicodeText);
|
---|
200 | return VINF_SUCCESS;
|
---|
201 | }
|
---|
202 |
|
---|
203 | VBoxServiceError("Failed to connect to the clipboard service, rc=%Rrc!\n", rc);
|
---|
204 | }
|
---|
205 | else
|
---|
206 | VBoxServiceError("WinAddAtom() failed, lasterr=%lx; WinFindAtom() failed, lasterror=%lx\n",
|
---|
207 | lLastError, WinGetLastError(g_habCtrl));
|
---|
208 | }
|
---|
209 | else
|
---|
210 | VBoxServiceError("WinCreateMsgQueue(,0) failed, lasterr=%lx\n", WinGetLastError(g_habCtrl));
|
---|
211 | WinTerminate(g_habCtrl);
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Check that we're still the view / try make us the viewer.
|
---|
218 | */
|
---|
219 | static void VBoxServiceClipboardOS2PollViewer(void)
|
---|
220 | {
|
---|
221 | const int iOrgState = g_enmState;
|
---|
222 |
|
---|
223 | HWND hwndClipboardViewer = WinQueryClipbrdViewer(g_habWorker);
|
---|
224 | if (hwndClipboardViewer == g_hwndWorker)
|
---|
225 | return;
|
---|
226 |
|
---|
227 | if (hwndClipboardViewer == NULLHANDLE)
|
---|
228 | {
|
---|
229 | /* The API will send a WM_DRAWCLIPBOARD message before returning. */
|
---|
230 | g_enmState = kClipboardState_SettingViewer;
|
---|
231 | if (WinSetClipbrdViewer(g_habWorker, g_hwndWorker))
|
---|
232 | g_enmState = kClipboardState_Viewer;
|
---|
233 | else
|
---|
234 | g_enmState = kClipboardState_Polling;
|
---|
235 | }
|
---|
236 | else
|
---|
237 | g_enmState = kClipboardState_Polling;
|
---|
238 | if ((int)g_enmState != iOrgState)
|
---|
239 | {
|
---|
240 | if (g_enmState == kClipboardState_Viewer)
|
---|
241 | VBoxServiceVerbose(3, "clipboard: viewer\n");
|
---|
242 | else
|
---|
243 | VBoxServiceVerbose(3, "clipboard: poller\n");
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Advertise the formats available from the host.
|
---|
250 | */
|
---|
251 | static void VBoxServiceClipboardOS2AdvertiseHostFormats(uint32_t fFormats)
|
---|
252 | {
|
---|
253 | /*
|
---|
254 | * Open the clipboard and switch to 'destruction' mode.
|
---|
255 | * Make sure we stop being viewer. Temporarily also make sure we're
|
---|
256 | * not the owner so that PM won't send us any WM_DESTROYCLIPBOARD message.
|
---|
257 | */
|
---|
258 | if (WinOpenClipbrd(g_habWorker))
|
---|
259 | {
|
---|
260 | if (g_enmState == kClipboardState_Viewer)
|
---|
261 | WinSetClipbrdViewer(g_habWorker, NULLHANDLE);
|
---|
262 | if (g_enmState == kClipboardState_Owner)
|
---|
263 | WinSetClipbrdOwner(g_habWorker, NULLHANDLE);
|
---|
264 |
|
---|
265 | g_enmState = kClipboardState_Destroying;
|
---|
266 | if (WinEmptyClipbrd(g_habWorker))
|
---|
267 | {
|
---|
268 | /*
|
---|
269 | * Take clipboard ownership.
|
---|
270 | */
|
---|
271 | if (WinSetClipbrdOwner(g_habWorker, g_hwndWorker))
|
---|
272 | {
|
---|
273 | g_enmState = kClipboardState_Owner;
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * Do the format advertising.
|
---|
277 | */
|
---|
278 | if (fFormats & (VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT/* | VBOX_SHARED_CLIPBOARD_FMT_HTML ?? */))
|
---|
279 | {
|
---|
280 | if (!WinSetClipbrdData(g_habWorker, 0, CF_TEXT, CFI_POINTER))
|
---|
281 | VBoxServiceError("WinSetClipbrdData(,,CF_TEXT,) failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
|
---|
282 | if ( g_atomOdin32UnicodeText
|
---|
283 | && !WinSetClipbrdData(g_habWorker, 0, g_atomOdin32UnicodeText, CFI_POINTER))
|
---|
284 | VBoxServiceError("WinSetClipbrdData(,,g_atomOdin32UnicodeText,) failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
|
---|
285 | }
|
---|
286 | if (fFormats & VBOX_SHARED_CLIPBOARD_FMT_BITMAP)
|
---|
287 | {
|
---|
288 | /** @todo bitmaps */
|
---|
289 | }
|
---|
290 | }
|
---|
291 | else
|
---|
292 | {
|
---|
293 | VBoxServiceError("WinSetClipbrdOwner failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
|
---|
294 | g_enmState = kClipboardState_Polling;
|
---|
295 | }
|
---|
296 | }
|
---|
297 | else
|
---|
298 | {
|
---|
299 | VBoxServiceError("WinEmptyClipbrd failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
|
---|
300 | g_enmState = kClipboardState_Polling;
|
---|
301 | }
|
---|
302 |
|
---|
303 | if (g_enmState == kClipboardState_Polling)
|
---|
304 | {
|
---|
305 | g_fEmptyClipboard = true;
|
---|
306 | VBoxServiceClipboardOS2PollViewer();
|
---|
307 | }
|
---|
308 |
|
---|
309 | WinCloseClipbrd(g_habWorker);
|
---|
310 | }
|
---|
311 | else
|
---|
312 | VBoxServiceError("VBoxServiceClipboardOS2AdvertiseHostFormats: WinOpenClipbrd failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | static void *VBoxServiceClipboardOs2ConvertToOdin32(uint32_t fFormat, USHORT usFmt, void *pv, uint32_t cb)
|
---|
317 | {
|
---|
318 | PVOID pvPM = NULL;
|
---|
319 | APIRET rc = DosAllocSharedMem(&pvPM, NULL, cb + sizeof(CLIPHEADER), OBJ_GIVEABLE | OBJ_GETTABLE | OBJ_TILE | PAG_READ | PAG_WRITE | PAG_COMMIT);
|
---|
320 | if (rc)
|
---|
321 | {
|
---|
322 | PCLIPHEADER pHdr = (PCLIPHEADER)pvPM;
|
---|
323 | memcpy(pHdr->achMagic, CLIPHEADER_MAGIC, sizeof(pHdr->achMagic));
|
---|
324 | pHdr->cbData = cb;
|
---|
325 | if (usFmt == g_atomOdin32UnicodeText)
|
---|
326 | pHdr->uFormat = usFmt;
|
---|
327 | else
|
---|
328 | AssertFailed();
|
---|
329 | memcpy(pHdr + 1, pv, cb);
|
---|
330 | }
|
---|
331 | else
|
---|
332 | {
|
---|
333 | VBoxServiceError("DosAllocSharedMem(,,%#x,,) -> %ld\n", cb + sizeof(CLIPHEADER), rc);
|
---|
334 | pvPM = NULL;
|
---|
335 | }
|
---|
336 | return pvPM;
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 | static void *VBoxServiceClipboardOs2ConvertToPM(uint32_t fFormat, USHORT usFmt, void *pv, uint32_t cb)
|
---|
341 | {
|
---|
342 | void *pvPM = NULL;
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * The Odin32 stuff is simple, we just assume windows data from the host
|
---|
346 | * and all we need to do is add the header.
|
---|
347 | */
|
---|
348 | if ( usFmt
|
---|
349 | && ( usFmt == g_atomOdin32UnicodeText
|
---|
350 | /* || usFmt == ...*/
|
---|
351 | )
|
---|
352 | )
|
---|
353 | pvPM = VBoxServiceClipboardOs2ConvertToOdin32(fFormat, usFmt, pv, cb);
|
---|
354 | else if (usFmt == CF_TEXT)
|
---|
355 | {
|
---|
356 | /*
|
---|
357 | * Convert the unicode text to the current ctype locale.
|
---|
358 | *
|
---|
359 | * Note that we probably should be using the current PM or DOS codepage
|
---|
360 | * here instead of the LC_CTYPE one which iconv uses by default.
|
---|
361 | * -lazybird
|
---|
362 | */
|
---|
363 | Assert(fFormat & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT);
|
---|
364 | char *pszUtf8;
|
---|
365 | int rc = RTUtf16ToUtf8((PCRTUTF16)pv, &pszUtf8);
|
---|
366 | if (RT_SUCCESS(rc))
|
---|
367 | {
|
---|
368 | char *pszLocale;
|
---|
369 | rc = RTStrUtf8ToCurrentCP(&pszLocale, pszUtf8);
|
---|
370 | if (RT_SUCCESS(rc))
|
---|
371 | {
|
---|
372 | size_t cbPM = strlen(pszLocale) + 1;
|
---|
373 | APIRET rc = DosAllocSharedMem(&pvPM, NULL, cbPM, OBJ_GIVEABLE | OBJ_GETTABLE | OBJ_TILE | PAG_READ | PAG_WRITE | PAG_COMMIT);
|
---|
374 | if (rc == NO_ERROR)
|
---|
375 | memcpy(pvPM, pszLocale, cbPM);
|
---|
376 | else
|
---|
377 | {
|
---|
378 | VBoxServiceError("DosAllocSharedMem(,,%#x,,) -> %ld\n", cb + sizeof(CLIPHEADER), rc);
|
---|
379 | pvPM = NULL;
|
---|
380 | }
|
---|
381 | RTStrFree(pszLocale);
|
---|
382 | }
|
---|
383 | else
|
---|
384 | VBoxServiceError("RTStrUtf8ToCurrentCP() -> %Rrc\n", rc);
|
---|
385 | RTStrFree(pszUtf8);
|
---|
386 | }
|
---|
387 | else
|
---|
388 | VBoxServiceError("RTUtf16ToUtf8() -> %Rrc\n", rc);
|
---|
389 | }
|
---|
390 |
|
---|
391 | return pvPM;
|
---|
392 | }
|
---|
393 |
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Tries to deliver an advertised host format.
|
---|
397 | *
|
---|
398 | * @param usFmt The PM format name.
|
---|
399 | *
|
---|
400 | * @remark We must not try open the clipboard here because WM_RENDERFMT is a
|
---|
401 | * request send synchronously by someone who has already opened the
|
---|
402 | * clipboard. We would enter a deadlock trying to open it here.
|
---|
403 | *
|
---|
404 | */
|
---|
405 | static void VBoxServiceClipboardOS2RenderFormat(USHORT usFmt)
|
---|
406 | {
|
---|
407 | bool fSucceeded = false;
|
---|
408 |
|
---|
409 | /*
|
---|
410 | * Determin which format.
|
---|
411 | */
|
---|
412 | uint32_t fFormat;
|
---|
413 | if ( usFmt == CF_TEXT
|
---|
414 | || usFmt == g_atomOdin32UnicodeText)
|
---|
415 | fFormat = VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT;
|
---|
416 | else /** @todo bitmaps */
|
---|
417 | fFormat = 0;
|
---|
418 | if (fFormat)
|
---|
419 | {
|
---|
420 | /*
|
---|
421 | * Query the data from the host.
|
---|
422 | * This might require two iterations because of buffer guessing.
|
---|
423 | */
|
---|
424 | uint32_t cb = _4K;
|
---|
425 | uint32_t cbAllocated = cb;
|
---|
426 | int rc = VERR_NO_MEMORY;
|
---|
427 | void *pv = RTMemPageAllocZ(cbAllocated);
|
---|
428 | if (pv)
|
---|
429 | {
|
---|
430 | VBoxServiceVerbose(4, "clipboard: reading host data (%#x)\n", fFormat);
|
---|
431 | rc = VbglR3ClipboardReadData(g_u32ClientId, fFormat, pv, cb, &cb);
|
---|
432 | if (rc == VINF_BUFFER_OVERFLOW)
|
---|
433 | {
|
---|
434 | RTMemPageFree(pv, cbAllocated);
|
---|
435 | cbAllocated = cb = RT_ALIGN_32(cb, PAGE_SIZE);
|
---|
436 | pv = RTMemPageAllocZ(cbAllocated);
|
---|
437 | rc = VbglR3ClipboardReadData(g_u32ClientId, fFormat, pv, cb, &cb);
|
---|
438 | }
|
---|
439 | if (RT_FAILURE(rc))
|
---|
440 | RTMemPageFree(pv, cbAllocated);
|
---|
441 | }
|
---|
442 | if (RT_SUCCESS(rc))
|
---|
443 | {
|
---|
444 | VBoxServiceVerbose(4, "clipboard: read %u bytes\n", cb);
|
---|
445 |
|
---|
446 | /*
|
---|
447 | * Convert the host clipboard data to PM clipboard data and set it.
|
---|
448 | */
|
---|
449 | PVOID pvPM = VBoxServiceClipboardOs2ConvertToPM(fFormat, usFmt, pv, cb);
|
---|
450 | if (pvPM)
|
---|
451 | {
|
---|
452 | if (WinSetClipbrdData(g_habWorker, (ULONG)pvPM, usFmt, CFI_POINTER))
|
---|
453 | fSucceeded = true;
|
---|
454 | else
|
---|
455 | {
|
---|
456 | VBoxServiceError("VBoxServiceClipboardOS2RenderFormat: WinSetClipbrdData(,%p,%#x, CF_POINTER) failed, lasterror=%lx\n",
|
---|
457 | pvPM, usFmt, WinGetLastError(g_habWorker));
|
---|
458 | DosFreeMem(pvPM);
|
---|
459 | }
|
---|
460 | }
|
---|
461 | RTMemPageFree(pv, cbAllocated);
|
---|
462 | }
|
---|
463 | else
|
---|
464 | VBoxServiceError("VBoxServiceClipboardOS2RenderFormat: Failed to query / allocate data. rc=%Rrc cb=%#RX32\n", rc, cb);
|
---|
465 | }
|
---|
466 |
|
---|
467 | /*
|
---|
468 | * Empty the clipboard on failure so we don't end up in any loops.
|
---|
469 | */
|
---|
470 | if (!fSucceeded)
|
---|
471 | {
|
---|
472 | WinSetClipbrdOwner(g_habWorker, NULLHANDLE);
|
---|
473 | g_enmState = kClipboardState_Destroying;
|
---|
474 | WinEmptyClipbrd(g_habWorker);
|
---|
475 | g_enmState = kClipboardState_Polling;
|
---|
476 | g_fEmptyClipboard = true;
|
---|
477 | VBoxServiceClipboardOS2PollViewer();
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 |
|
---|
482 | static void VBoxServiceClipboardOS2SendDataToHost(uint32_t fFormat)
|
---|
483 | {
|
---|
484 | if (WinOpenClipbrd(g_habWorker))
|
---|
485 | {
|
---|
486 | PRTUTF16 pwszFree = NULL;
|
---|
487 | void *pv = NULL;
|
---|
488 | uint32_t cb = 0;
|
---|
489 |
|
---|
490 | if (fFormat & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
|
---|
491 | {
|
---|
492 | /* Got any odin32 unicode text? */
|
---|
493 | PVOID pvPM;
|
---|
494 | PCLIPHEADER pHdr = (PCLIPHEADER)WinQueryClipbrdData(g_habWorker, g_atomOdin32UnicodeText);
|
---|
495 | if ( pHdr
|
---|
496 | && !memcmp(pHdr->achMagic, CLIPHEADER_MAGIC, sizeof(pHdr->achMagic)))
|
---|
497 | {
|
---|
498 | pv = pHdr + 1;
|
---|
499 | cb = pHdr->cbData;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /* Got any CF_TEXT? */
|
---|
503 | if ( !pv
|
---|
504 | && (pvPM = (PVOID)WinQueryClipbrdData(g_habWorker, CF_TEXT)) != NULL)
|
---|
505 | {
|
---|
506 | char *pszUtf8;
|
---|
507 | int rc = RTStrCurrentCPToUtf8(&pszUtf8, (const char *)pvPM);
|
---|
508 | if (RT_SUCCESS(rc))
|
---|
509 | {
|
---|
510 | PRTUTF16 pwsz;
|
---|
511 | rc = RTStrToUtf16(pszUtf8, &pwsz);
|
---|
512 | if (RT_SUCCESS(rc))
|
---|
513 | {
|
---|
514 | pv = pwszFree = pwsz;
|
---|
515 | cb = (RTUtf16Len(pwsz) + 1) * sizeof(RTUTF16);
|
---|
516 | }
|
---|
517 | RTStrFree(pszUtf8);
|
---|
518 | }
|
---|
519 | }
|
---|
520 | }
|
---|
521 | if (!pv)
|
---|
522 | VBoxServiceError("VBoxServiceClipboardOS2SendDataToHost: couldn't find data for %#x\n", fFormat);
|
---|
523 |
|
---|
524 | /*
|
---|
525 | * Now, sent whatever we've got to the host (it's waiting).
|
---|
526 | */
|
---|
527 | VBoxServiceVerbose(4, "clipboard: writing %pv/%#d (fFormat=%#x)\n", pv, cb, fFormat);
|
---|
528 | VbglR3ClipboardWriteData(g_u32ClientId, fFormat, pv, cb);
|
---|
529 | RTUtf16Free(pwszFree);
|
---|
530 |
|
---|
531 | WinCloseClipbrd(g_habWorker);
|
---|
532 | }
|
---|
533 | else
|
---|
534 | {
|
---|
535 | VBoxServiceError("VBoxServiceClipboardOS2SendDataToHost: WinOpenClipbrd failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
|
---|
536 | VBoxServiceVerbose(4, "clipboard: writing NULL/0 (fFormat=%x)\n", fFormat);
|
---|
537 | VbglR3ClipboardWriteData(g_u32ClientId, fFormat, NULL, 0);
|
---|
538 | }
|
---|
539 | }
|
---|
540 |
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * Figure out what's on the clipboard and report it to the host.
|
---|
544 | */
|
---|
545 | static void VBoxServiceClipboardOS2ReportFormats(void)
|
---|
546 | {
|
---|
547 | uint32_t fFormats = 0;
|
---|
548 | ULONG ulFormat = 0;
|
---|
549 | while ((ulFormat = WinEnumClipbrdFmts(g_habWorker, ulFormat)) != 0)
|
---|
550 | {
|
---|
551 | if ( ulFormat == CF_TEXT
|
---|
552 | || ulFormat == g_atomOdin32UnicodeText)
|
---|
553 | fFormats |= VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT;
|
---|
554 | /** @todo else bitmaps and stuff. */
|
---|
555 | }
|
---|
556 | VBoxServiceVerbose(4, "clipboard: reporting fFormats=%#x\n", fFormats);
|
---|
557 | VbglR3ClipboardReportFormats(g_u32ClientId, fFormats);
|
---|
558 | }
|
---|
559 |
|
---|
560 |
|
---|
561 | /**
|
---|
562 | * Poll the clipboard for changes.
|
---|
563 | *
|
---|
564 | * This is called both when we're the viewer and when we're
|
---|
565 | * falling back to polling. If something has changed it will
|
---|
566 | * notify the host.
|
---|
567 | */
|
---|
568 | static void VBoxServiceClipboardOS2Poll(void)
|
---|
569 | {
|
---|
570 | if (WinOpenClipbrd(g_habWorker))
|
---|
571 | {
|
---|
572 | /*
|
---|
573 | * If our dummy is no longer there, something has actually changed,
|
---|
574 | * unless the clipboard is really empty.
|
---|
575 | */
|
---|
576 | ULONG fFmtInfo;
|
---|
577 | if (!WinQueryClipbrdFmtInfo(g_habWorker, g_atomNothingChanged, &fFmtInfo))
|
---|
578 | {
|
---|
579 | if (WinEnumClipbrdFmts(g_habWorker, 0) != 0)
|
---|
580 | {
|
---|
581 | g_fEmptyClipboard = false;
|
---|
582 | VBoxServiceClipboardOS2ReportFormats();
|
---|
583 |
|
---|
584 | /* inject the dummy */
|
---|
585 | PVOID pv;
|
---|
586 | APIRET rc = DosAllocSharedMem(&pv, NULL, 1, OBJ_GIVEABLE | OBJ_GETTABLE | PAG_READ | PAG_WRITE | PAG_COMMIT);
|
---|
587 | if (rc == NO_ERROR)
|
---|
588 | {
|
---|
589 | if (WinSetClipbrdData(g_habWorker, (ULONG)pv, g_atomNothingChanged, CFI_POINTER))
|
---|
590 | VBoxServiceVerbose(4, "clipboard: Added dummy item.\n");
|
---|
591 | else
|
---|
592 | {
|
---|
593 | VBoxServiceError("VBoxServiceClipboardOS2Poll: WinSetClipbrdData failed, lasterr=%#lx\n", WinGetLastError(g_habWorker));
|
---|
594 | DosFreeMem(pv);
|
---|
595 | }
|
---|
596 | }
|
---|
597 | else
|
---|
598 | VBoxServiceError("VBoxServiceClipboardOS2Poll: DosAllocSharedMem(,,1,) -> %ld\n", rc);
|
---|
599 | }
|
---|
600 | else if (!g_fEmptyClipboard)
|
---|
601 | {
|
---|
602 | g_fEmptyClipboard = true;
|
---|
603 | VBoxServiceVerbose(3, "Reporting empty clipboard\n");
|
---|
604 | VbglR3ClipboardReportFormats(g_u32ClientId, 0);
|
---|
605 | }
|
---|
606 | }
|
---|
607 | WinCloseClipbrd(g_habWorker);
|
---|
608 | }
|
---|
609 | else
|
---|
610 | VBoxServiceError("VBoxServiceClipboardOS2Poll: WinOpenClipbrd failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
|
---|
611 | }
|
---|
612 |
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * The clipboard we owned was destroyed by someone else.
|
---|
616 | */
|
---|
617 | static void VBoxServiceClipboardOS2Destroyed(void)
|
---|
618 | {
|
---|
619 | /* make sure we're no longer the owner. */
|
---|
620 | if (WinQueryClipbrdOwner(g_habWorker) == g_hwndWorker)
|
---|
621 | WinSetClipbrdOwner(g_habWorker, NULLHANDLE);
|
---|
622 |
|
---|
623 | /* switch to polling state and notify the host. */
|
---|
624 | g_enmState = kClipboardState_Polling;
|
---|
625 | g_fEmptyClipboard = true;
|
---|
626 | VBoxServiceVerbose(3, "Reporting empty clipboard\n");
|
---|
627 | VbglR3ClipboardReportFormats(g_u32ClientId, 0);
|
---|
628 |
|
---|
629 | VBoxServiceClipboardOS2PollViewer();
|
---|
630 | }
|
---|
631 |
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * The window procedure for the object window.
|
---|
635 | *
|
---|
636 | * @returns Message result.
|
---|
637 | *
|
---|
638 | * @param hwnd The window handle.
|
---|
639 | * @param msg The message.
|
---|
640 | * @param mp1 Message parameter 1.
|
---|
641 | * @param mp2 Message parameter 2.
|
---|
642 | */
|
---|
643 | static MRESULT EXPENTRY VBoxServiceClipboardOS2WinProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
|
---|
644 | {
|
---|
645 | if (msg != WM_TIMER)
|
---|
646 | VBoxServiceVerbose(6, "VBoxServiceClipboardOS2WinProc: hwnd=%#lx msg=%#lx mp1=%#lx mp2=%#lx\n", hwnd, msg, mp1, mp2);
|
---|
647 |
|
---|
648 | switch (msg)
|
---|
649 | {
|
---|
650 | /*
|
---|
651 | * Handle the two system defined messages for object windows.
|
---|
652 | *
|
---|
653 | * We'll just use the CREATE/DESTROY message to create that timer we're
|
---|
654 | * using for the viewer checks and polling fallback.
|
---|
655 | */
|
---|
656 | case WM_CREATE:
|
---|
657 | g_idWorkerTimer = WinStartTimer(g_habWorker, hwnd, 1 /* id */, 1000 /* 1 second */);
|
---|
658 | g_fEmptyClipboard = true;
|
---|
659 | g_enmState = kClipboardState_Polling;
|
---|
660 | return NULL; /* FALSE(/NULL) == Continue*/
|
---|
661 |
|
---|
662 | case WM_DESTROY:
|
---|
663 | WinStopTimer(g_habWorker, hwnd, g_idWorkerTimer);
|
---|
664 | g_idWorkerTimer = ~0UL;
|
---|
665 | g_hwndWorker = NULLHANDLE;
|
---|
666 | break;
|
---|
667 |
|
---|
668 | /*
|
---|
669 | * Clipboard viewer message - the content has been changed.
|
---|
670 | * This is sent *after* releasing the clipboard sem
|
---|
671 | * and during the WinSetClipbrdViewer call.
|
---|
672 | */
|
---|
673 | case WM_DRAWCLIPBOARD:
|
---|
674 | if (g_enmState == kClipboardState_SettingViewer)
|
---|
675 | break;
|
---|
676 | AssertMsgBreak(g_enmState == kClipboardState_Viewer, ("g_enmState=%d\n", g_enmState));
|
---|
677 | VBoxServiceClipboardOS2Poll();
|
---|
678 | break;
|
---|
679 |
|
---|
680 | /*
|
---|
681 | * Clipboard owner message - the content was replaced.
|
---|
682 | * This is sent by someone with an open clipboard, so don't try open it now.
|
---|
683 | */
|
---|
684 | case WM_DESTROYCLIPBOARD:
|
---|
685 | if (g_enmState == kClipboardState_Destroying)
|
---|
686 | break; /* it's us doing the replacing, ignore. */
|
---|
687 | AssertMsgBreak(g_enmState == kClipboardState_Owner, ("g_enmState=%d\n", g_enmState));
|
---|
688 | VBoxServiceClipboardOS2Destroyed();
|
---|
689 | break;
|
---|
690 |
|
---|
691 | /*
|
---|
692 | * Clipboard owner message - somebody is requesting us to render a format.
|
---|
693 | * This is called by someone which owns the clipboard, but that's fine.
|
---|
694 | */
|
---|
695 | case WM_RENDERFMT:
|
---|
696 | AssertMsgBreak(g_enmState == kClipboardState_Owner, ("g_enmState=%d\n", g_enmState));
|
---|
697 | VBoxServiceClipboardOS2RenderFormat(SHORT1FROMMP(mp1));
|
---|
698 | break;
|
---|
699 |
|
---|
700 | /*
|
---|
701 | * Clipboard owner message - we're about to quit and should render all formats.
|
---|
702 | *
|
---|
703 | * However, because we're lazy, we'll just ASSUME that since we're quitting
|
---|
704 | * we're probably about to shutdown or something and there is no point in
|
---|
705 | * doing anything here except for emptying the clipboard and removing
|
---|
706 | * ourselves as owner. Any failures at this point are silently ignored.
|
---|
707 | */
|
---|
708 | case WM_RENDERALLFMTS:
|
---|
709 | WinOpenClipbrd(g_habWorker);
|
---|
710 | WinSetClipbrdOwner(g_habWorker, NULLHANDLE);
|
---|
711 | g_enmState = kClipboardState_Destroying;
|
---|
712 | WinEmptyClipbrd(g_habWorker);
|
---|
713 | g_enmState = kClipboardState_Polling;
|
---|
714 | g_fEmptyClipboard = true;
|
---|
715 | WinCloseClipbrd(g_habWorker);
|
---|
716 | break;
|
---|
717 |
|
---|
718 | /*
|
---|
719 | * Listener message - the host has new formats to offer.
|
---|
720 | */
|
---|
721 | case WM_USER + VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS:
|
---|
722 | VBoxServiceClipboardOS2AdvertiseHostFormats(LONGFROMMP(mp1));
|
---|
723 | break;
|
---|
724 |
|
---|
725 | /*
|
---|
726 | * Listener message - the host wish to read our clipboard data.
|
---|
727 | */
|
---|
728 | case WM_USER + VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA:
|
---|
729 | VBoxServiceClipboardOS2SendDataToHost(LONGFROMMP(mp1));
|
---|
730 | break;
|
---|
731 |
|
---|
732 | /*
|
---|
733 | * This is just a fallback polling strategy in case some other
|
---|
734 | * app is trying to view the clipboard too. We also use this
|
---|
735 | * to try recover from errors.
|
---|
736 | *
|
---|
737 | * Because the way the clipboard service works, we have to monitor
|
---|
738 | * it all the time and cannot get away with simpler solutions like
|
---|
739 | * synergy is employing (basically checking upon entering and leaving
|
---|
740 | * a desktop).
|
---|
741 | */
|
---|
742 | case WM_TIMER:
|
---|
743 | if ( g_enmState != kClipboardState_Viewer
|
---|
744 | && g_enmState != kClipboardState_Polling)
|
---|
745 | break;
|
---|
746 |
|
---|
747 | /* Lost the position as clipboard viwer?*/
|
---|
748 | if (g_enmState == kClipboardState_Viewer)
|
---|
749 | {
|
---|
750 | if (WinQueryClipbrdViewer(g_habWorker) == hwnd)
|
---|
751 | break;
|
---|
752 | g_enmState = kClipboardState_Polling;
|
---|
753 | }
|
---|
754 |
|
---|
755 | /* poll for changes */
|
---|
756 | VBoxServiceClipboardOS2Poll();
|
---|
757 | VBoxServiceClipboardOS2PollViewer();
|
---|
758 | break;
|
---|
759 |
|
---|
760 |
|
---|
761 | /*
|
---|
762 | * Clipboard owner messages dealing with owner drawn content.
|
---|
763 | * We shouldn't be seeing any of these.
|
---|
764 | */
|
---|
765 | case WM_PAINTCLIPBOARD:
|
---|
766 | case WM_SIZECLIPBOARD:
|
---|
767 | case WM_HSCROLLCLIPBOARD:
|
---|
768 | case WM_VSCROLLCLIPBOARD:
|
---|
769 | AssertMsgFailed(("msg=%lx (%ld)\n", msg, msg));
|
---|
770 | break;
|
---|
771 |
|
---|
772 | /*
|
---|
773 | * We shouldn't be seeing any other messages according to the docs.
|
---|
774 | * But for whatever reason, PM sends us a WM_ADJUSTWINDOWPOS message
|
---|
775 | * during WinCreateWindow. So, ignore that and assert on anything else.
|
---|
776 | */
|
---|
777 | default:
|
---|
778 | AssertMsgFailed(("msg=%lx (%ld)\n", msg, msg));
|
---|
779 | case WM_ADJUSTWINDOWPOS:
|
---|
780 | break;
|
---|
781 | }
|
---|
782 | return NULL;
|
---|
783 | }
|
---|
784 |
|
---|
785 |
|
---|
786 | /**
|
---|
787 | * The listener thread.
|
---|
788 | *
|
---|
789 | * This thread is dedicated to listening for host messages and forwarding
|
---|
790 | * these to the worker thread (using PM).
|
---|
791 | *
|
---|
792 | * The thread will set g_fListenerOkay and signal its user event when it has
|
---|
793 | * completed initialization. In the case of init failure g_fListenerOkay will
|
---|
794 | * not be set.
|
---|
795 | *
|
---|
796 | * @returns Init error code or VINF_SUCCESS.
|
---|
797 | * @param ThreadSelf Our thread handle.
|
---|
798 | * @param pvUser Pointer to the clipboard service shutdown indicator.
|
---|
799 | */
|
---|
800 | static DECLCALLBACK(int) VBoxServiceClipboardOS2Listener(RTTHREAD ThreadSelf, void *pvUser)
|
---|
801 | {
|
---|
802 | bool volatile *pfShutdown = (bool volatile *)pvUser;
|
---|
803 | int rc = VERR_GENERAL_FAILURE;
|
---|
804 | VBoxServiceVerbose(3, "VBoxServiceClipboardOS2Listener: ThreadSelf=%RTthrd\n", ThreadSelf);
|
---|
805 |
|
---|
806 | g_habListener = WinInitialize(0);
|
---|
807 | if (g_habListener != NULLHANDLE)
|
---|
808 | {
|
---|
809 | g_hmqListener = WinCreateMsgQueue(g_habListener, 0);
|
---|
810 | if (g_hmqListener != NULLHANDLE)
|
---|
811 | {
|
---|
812 | /*
|
---|
813 | * Tell the worker thread that we're good.
|
---|
814 | */
|
---|
815 | rc = VINF_SUCCESS;
|
---|
816 | ASMAtomicXchgBool(&g_fListenerOkay, true);
|
---|
817 | RTThreadUserSignal(ThreadSelf);
|
---|
818 | VBoxServiceVerbose(3, "VBoxServiceClipboardOS2Listener: Started successfully\n");
|
---|
819 |
|
---|
820 | /*
|
---|
821 | * Loop until termination is requested.
|
---|
822 | */
|
---|
823 | bool fQuit = false;
|
---|
824 | while (!*pfShutdown && !fQuit)
|
---|
825 | {
|
---|
826 | uint32_t Msg;
|
---|
827 | uint32_t fFormats;
|
---|
828 | rc = VbglR3ClipboardGetHostMsg(g_u32ClientId, &Msg, &fFormats);
|
---|
829 | if (RT_SUCCESS(rc))
|
---|
830 | {
|
---|
831 | VBoxServiceVerbose(3, "VBoxServiceClipboardOS2Listener: Msg=%#x fFormats=%#x\n", Msg, fFormats);
|
---|
832 | switch (Msg)
|
---|
833 | {
|
---|
834 | /*
|
---|
835 | * The host has announced available clipboard formats.
|
---|
836 | * Forward the information to the window, so it can later
|
---|
837 | * respond do WM_RENDERFORMAT message.
|
---|
838 | */
|
---|
839 | case VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS:
|
---|
840 | if (!WinPostMsg(g_hwndWorker, WM_USER + VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS,
|
---|
841 | MPFROMLONG(fFormats), 0))
|
---|
842 | VBoxServiceError("WinPostMsg(%lx, FORMATS,,) failed, lasterr=%#lx\n",
|
---|
843 | g_hwndWorker, WinGetLastError(g_habListener));
|
---|
844 | break;
|
---|
845 |
|
---|
846 | /*
|
---|
847 | * The host needs data in the specified format.
|
---|
848 | */
|
---|
849 | case VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA:
|
---|
850 | if (!WinPostMsg(g_hwndWorker, WM_USER + VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA,
|
---|
851 | MPFROMLONG(fFormats), 0))
|
---|
852 | VBoxServiceError("WinPostMsg(%lx, READ_DATA,,) failed, lasterr=%#lx\n",
|
---|
853 | g_hwndWorker, WinGetLastError(g_habListener));
|
---|
854 | break;
|
---|
855 |
|
---|
856 | /*
|
---|
857 | * The host is terminating.
|
---|
858 | */
|
---|
859 | case VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT:
|
---|
860 | fQuit = true;
|
---|
861 | break;
|
---|
862 |
|
---|
863 | default:
|
---|
864 | VBoxServiceVerbose(1, "VBoxServiceClipboardOS2Listener: Unknown message %RU32\n", Msg);
|
---|
865 | break;
|
---|
866 | }
|
---|
867 | }
|
---|
868 | else
|
---|
869 | {
|
---|
870 | if (*pfShutdown)
|
---|
871 | break;
|
---|
872 | VBoxServiceError("VbglR3ClipboardGetHostMsg failed, rc=%Rrc\n", rc);
|
---|
873 | RTThreadSleep(1000);
|
---|
874 | }
|
---|
875 | } /* the loop */
|
---|
876 |
|
---|
877 | WinDestroyMsgQueue(g_hmqListener);
|
---|
878 | }
|
---|
879 | WinTerminate(g_habListener);
|
---|
880 | g_habListener = NULLHANDLE;
|
---|
881 | }
|
---|
882 |
|
---|
883 | /* Signal our semaphore to make the worker catch on. */
|
---|
884 | RTThreadUserSignal(ThreadSelf);
|
---|
885 | VBoxServiceVerbose(3, "VBoxServiceClipboardOS2Listener: terminating, rc=%Rrc\n", rc);
|
---|
886 | return rc;
|
---|
887 | }
|
---|
888 |
|
---|
889 |
|
---|
890 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
891 | static DECLCALLBACK(int) VBoxServiceClipboardOS2Worker(bool volatile *pfShutdown)
|
---|
892 | {
|
---|
893 | int rc = VERR_GENERAL_FAILURE;
|
---|
894 |
|
---|
895 | /*
|
---|
896 | * Standard PM init.
|
---|
897 | */
|
---|
898 | g_habWorker = RTThreadSelf() != g_ThreadCtrl ? WinInitialize(0) : g_habCtrl;
|
---|
899 | if (g_habWorker != NULLHANDLE)
|
---|
900 | {
|
---|
901 | g_hmqWorker = RTThreadSelf() != g_ThreadCtrl ? WinCreateMsgQueue(g_habWorker, 0) : g_hmqCtrl;
|
---|
902 | if (g_hmqWorker != NULLHANDLE)
|
---|
903 | {
|
---|
904 | /*
|
---|
905 | * Create the object window.
|
---|
906 | */
|
---|
907 | if (WinRegisterClass(g_habWorker, (PCSZ)"VBoxServiceClipboardClass", VBoxServiceClipboardOS2WinProc, 0, 0))
|
---|
908 | {
|
---|
909 | g_hwndWorker = WinCreateWindow(HWND_OBJECT, /* hwndParent */
|
---|
910 | (PCSZ)"VBoxServiceClipboardClass", /* pszClass */
|
---|
911 | (PCSZ)"VirtualBox Clipboard Service", /* pszName */
|
---|
912 | 0, /* flStyle */
|
---|
913 | 0, 0, 0, 0, /* x, y, cx, cy */
|
---|
914 | NULLHANDLE, /* hwndOwner */
|
---|
915 | HWND_BOTTOM, /* hwndInsertBehind */
|
---|
916 | 42, /* id */
|
---|
917 | NULL, /* pCtlData */
|
---|
918 | NULL); /* pPresParams */
|
---|
919 | if (g_hwndWorker != NULLHANDLE)
|
---|
920 | {
|
---|
921 | VBoxServiceVerbose(3, "g_hwndWorker=%#lx g_habWorker=%#lx g_hmqWorker=%#lx\n", g_hwndWorker, g_habWorker, g_hmqWorker);
|
---|
922 |
|
---|
923 | /*
|
---|
924 | * Create the listener thread.
|
---|
925 | */
|
---|
926 | g_fListenerOkay = false;
|
---|
927 | rc = RTThreadCreate(&g_ThreadListener, VBoxServiceClipboardOS2Listener, (void *)pfShutdown, 0,
|
---|
928 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "CLIPLISTEN");
|
---|
929 | if (RT_SUCCESS(rc))
|
---|
930 | {
|
---|
931 | RTThreadUserWait(g_ThreadListener, 30*1000);
|
---|
932 | RTThreadUserReset(g_ThreadListener);
|
---|
933 | if (!g_fListenerOkay)
|
---|
934 | RTThreadWait(g_ThreadListener, 60*1000, NULL);
|
---|
935 | if (g_fListenerOkay)
|
---|
936 | {
|
---|
937 | /*
|
---|
938 | * Tell the control thread that it can continue
|
---|
939 | * spawning services.
|
---|
940 | */
|
---|
941 | RTThreadUserSignal(RTThreadSelf());
|
---|
942 |
|
---|
943 | /*
|
---|
944 | * The PM event pump.
|
---|
945 | */
|
---|
946 | VBoxServiceVerbose(2, "clipboard: Entering PM message loop.\n");
|
---|
947 | rc = VINF_SUCCESS;
|
---|
948 | QMSG qmsg;
|
---|
949 | while (WinGetMsg(g_habWorker, &qmsg, NULLHANDLE, NULLHANDLE, 0))
|
---|
950 | WinDispatchMsg(g_habWorker, &qmsg);
|
---|
951 | VBoxServiceVerbose(2, "clipboard: Exited PM message loop. *pfShutdown=%RTbool\n", *pfShutdown);
|
---|
952 |
|
---|
953 | RTThreadWait(g_ThreadListener, 60*1000, NULL);
|
---|
954 | }
|
---|
955 | g_ThreadListener = NIL_RTTHREAD;
|
---|
956 | }
|
---|
957 |
|
---|
958 | /*
|
---|
959 | * Got a WM_QUIT, clean up.
|
---|
960 | */
|
---|
961 | if (g_hwndWorker != NULLHANDLE)
|
---|
962 | {
|
---|
963 | WinDestroyWindow(g_hwndWorker);
|
---|
964 | g_hwndWorker = NULLHANDLE;
|
---|
965 | }
|
---|
966 | }
|
---|
967 | else
|
---|
968 | VBoxServiceError("WinCreateWindow() failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
|
---|
969 | /* no class deregistration in PM. */
|
---|
970 | }
|
---|
971 | else
|
---|
972 | VBoxServiceError("WinRegisterClass() failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
|
---|
973 |
|
---|
974 | if (g_hmqCtrl != g_hmqWorker)
|
---|
975 | WinDestroyMsgQueue(g_hmqWorker);
|
---|
976 | g_hmqWorker = NULLHANDLE;
|
---|
977 | }
|
---|
978 | else
|
---|
979 | VBoxServiceError("WinCreateMsgQueue(,0) failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
|
---|
980 |
|
---|
981 | if (g_habCtrl != g_habWorker)
|
---|
982 | WinTerminate(g_habWorker);
|
---|
983 | g_habWorker = NULLHANDLE;
|
---|
984 | }
|
---|
985 | else
|
---|
986 | VBoxServiceError("WinInitialize(0) failed, lasterr=%lx\n", WinGetLastError(NULLHANDLE));
|
---|
987 |
|
---|
988 | return rc;
|
---|
989 | }
|
---|
990 |
|
---|
991 |
|
---|
992 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
993 | static DECLCALLBACK(void) VBoxServiceClipboardOS2Stop(void)
|
---|
994 | {
|
---|
995 | if ( g_hmqWorker != NULLHANDLE
|
---|
996 | && !WinPostQueueMsg(g_hmqWorker, WM_QUIT, NULL, NULL))
|
---|
997 | VBoxServiceError("WinPostQueueMsg(g_hmqWorker, WM_QUIT, 0,0) failed, lasterr=%lx\n", WinGetLastError(g_habCtrl));
|
---|
998 | }
|
---|
999 |
|
---|
1000 |
|
---|
1001 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
1002 | static DECLCALLBACK(void) VBoxServiceClipboardOS2Term(void)
|
---|
1003 | {
|
---|
1004 | VBoxServiceVerbose(4, "clipboard: disconnecting %#x\n", g_u32ClientId);
|
---|
1005 | VbglR3ClipboardDisconnect(g_u32ClientId);
|
---|
1006 | g_u32ClientId = 0;
|
---|
1007 | WinDestroyMsgQueue(g_hmqCtrl);
|
---|
1008 | g_hmqCtrl = NULLHANDLE;
|
---|
1009 | WinTerminate(g_habCtrl);
|
---|
1010 | g_habCtrl = NULLHANDLE;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 |
|
---|
1014 | /**
|
---|
1015 | * The OS/2 'clipboard' service description.
|
---|
1016 | */
|
---|
1017 | VBOXSERVICE g_Clipboard =
|
---|
1018 | {
|
---|
1019 | /* pszName. */
|
---|
1020 | "clipboard",
|
---|
1021 | /* pszDescription. */
|
---|
1022 | "Shared Clipboard",
|
---|
1023 | /* pszUsage. */
|
---|
1024 | ""
|
---|
1025 | ,
|
---|
1026 | /* pszOptions. */
|
---|
1027 | ""
|
---|
1028 | ,
|
---|
1029 | /* methods */
|
---|
1030 | VBoxServiceClipboardOS2PreInit,
|
---|
1031 | VBoxServiceClipboardOS2Option,
|
---|
1032 | VBoxServiceClipboardOS2Init,
|
---|
1033 | VBoxServiceClipboardOS2Worker,
|
---|
1034 | VBoxServiceClipboardOS2Stop,
|
---|
1035 | VBoxServiceClipboardOS2Term
|
---|
1036 | };
|
---|
1037 |
|
---|