VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceClipboard-os2.cpp@ 76553

Last change on this file since 76553 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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