VirtualBox

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

Last change on this file since 17610 was 8580, checked in by vboxsync, 16 years ago

AssertMsgBreakVoid -> AssertMsgBreak.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 37.3 KB
Line 
1/** $Id: VBoxServiceClipboard-os2.cpp 8580 2008-05-05 13:56:33Z 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/VBoxGuest.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 */
50typedef 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. */
70static RTTHREAD g_ThreadCtrl = NIL_RTTHREAD;
71/** The HAB of the control thread (main). */
72static HAB g_habCtrl = NULLHANDLE;
73/** The HMQ of the control thread (main). */
74static HMQ g_hmqCtrl = NULLHANDLE;
75
76/** The Listener thread handle. */
77static RTTHREAD g_ThreadListener = NIL_RTTHREAD;
78/** The HAB of the listener thread. */
79static HAB g_habListener = NULLHANDLE;
80/** The HMQ of the listener thread. */
81static HMQ g_hmqListener = NULLHANDLE;
82/** Indicator that gets set if the listener thread is successfully initialized. */
83static bool volatile g_fListenerOkay = false;
84
85/** The HAB of the worker thread. */
86static HAB g_habWorker = NULLHANDLE;
87/** The HMQ of the worker thread. */
88static HMQ g_hmqWorker = NULLHANDLE;
89/** The object window handle. */
90static HWND g_hwndWorker = NULLHANDLE;
91/** The timer id returned by WinStartTimer. */
92static 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. */
95static 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. */
112static 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. */
118static ATOM g_atomNothingChanged = 0;
119
120/** The clipboard connection client ID. */
121static uint32_t g_u32ClientId;
122/** Odin32 CF_UNICODETEXT. See user32.cpp. */
123static 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 */
131static DECLCALLBACK(int) VBoxServiceClipboardOS2PreInit(void)
132{
133 return VINF_SUCCESS;
134}
135
136
137/** @copydoc VBOXSERVICE::pfnOption */
138static DECLCALLBACK(int) VBoxServiceClipboardOS2Option(const char **ppszShort, int argc, char **argv, int *pi)
139{
140 return -1;
141}
142
143
144/** @copydoc VBOXSERVICE::pfnInit */
145static 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 */
219static 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 */
251static 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
316static 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
340static 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 */
405static 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 int rc = VERR_NO_MEMORY;
426 void *pv = RTMemPageAllocZ(cb);
427 if (pv)
428 {
429 VBoxServiceVerbose(4, "clipboard: reading host data (%#x)\n", fFormat);
430 rc = VbglR3ClipboardReadData(g_u32ClientId, fFormat, pv, cb, &cb);
431 if (rc == VINF_BUFFER_OVERFLOW)
432 {
433 RTMemPageFree(pv);
434 cb = RT_ALIGN_32(cb, PAGE_SIZE);
435 pv = RTMemPageAllocZ(cb);
436 rc = VbglR3ClipboardReadData(g_u32ClientId, fFormat, pv, cb, &cb);
437 }
438 if (RT_FAILURE(rc))
439 RTMemPageFree(pv);
440 }
441 if (RT_SUCCESS(rc))
442 {
443 VBoxServiceVerbose(4, "clipboard: read %u bytes\n", cb);
444
445 /*
446 * Convert the host clipboard data to PM clipboard data and set it.
447 */
448 PVOID pvPM = VBoxServiceClipboardOs2ConvertToPM(fFormat, usFmt, pv, cb);
449 if (pvPM)
450 {
451 if (WinSetClipbrdData(g_habWorker, (ULONG)pvPM, usFmt, CFI_POINTER))
452 fSucceeded = true;
453 else
454 {
455 VBoxServiceError("VBoxServiceClipboardOS2RenderFormat: WinSetClipbrdData(,%p,%#x, CF_POINTER) failed, lasterror=%lx\n",
456 pvPM, usFmt, WinGetLastError(g_habWorker));
457 DosFreeMem(pvPM);
458 }
459 }
460 RTMemPageFree(pv);
461 }
462 else
463 VBoxServiceError("VBoxServiceClipboardOS2RenderFormat: Failed to query / allocate data. rc=%Rrc cb=%#RX32\n", rc, cb);
464 }
465
466 /*
467 * Empty the clipboard on failure so we don't end up in any loops.
468 */
469 if (!fSucceeded)
470 {
471 WinSetClipbrdOwner(g_habWorker, NULLHANDLE);
472 g_enmState = kClipboardState_Destroying;
473 WinEmptyClipbrd(g_habWorker);
474 g_enmState = kClipboardState_Polling;
475 g_fEmptyClipboard = true;
476 VBoxServiceClipboardOS2PollViewer();
477 }
478}
479
480
481static void VBoxServiceClipboardOS2SendDataToHost(uint32_t fFormat)
482{
483 if (WinOpenClipbrd(g_habWorker))
484 {
485 PRTUTF16 pwszFree = NULL;
486 void *pv = NULL;
487 uint32_t cb = 0;
488
489 if (fFormat & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
490 {
491 /* Got any odin32 unicode text? */
492 PVOID pvPM;
493 PCLIPHEADER pHdr = (PCLIPHEADER)WinQueryClipbrdData(g_habWorker, g_atomOdin32UnicodeText);
494 if ( pHdr
495 && !memcmp(pHdr->achMagic, CLIPHEADER_MAGIC, sizeof(pHdr->achMagic)))
496 {
497 pv = pHdr + 1;
498 cb = pHdr->cbData;
499 }
500
501 /* Got any CF_TEXT? */
502 if ( !pv
503 && (pvPM = (PVOID)WinQueryClipbrdData(g_habWorker, CF_TEXT)) != NULL)
504 {
505 char *pszUtf8;
506 int rc = RTStrCurrentCPToUtf8(&pszUtf8, (const char *)pvPM);
507 if (RT_SUCCESS(rc))
508 {
509 PRTUTF16 pwsz;
510 rc = RTStrToUtf16(pszUtf8, &pwsz);
511 if (RT_SUCCESS(rc))
512 {
513 pv = pwszFree = pwsz;
514 cb = (RTUtf16Len(pwsz) + 1) * sizeof(RTUTF16);
515 }
516 RTStrFree(pszUtf8);
517 }
518 }
519 }
520 if (!pv)
521 VBoxServiceError("VBoxServiceClipboardOS2SendDataToHost: couldn't find data for %#x\n", fFormat);
522
523 /*
524 * Now, sent whatever we've got to the host (it's waiting).
525 */
526 VBoxServiceVerbose(4, "clipboard: writing %pv/%#d (fFormat=%#x)\n", pv, cb, fFormat);
527 VbglR3ClipboardWriteData(g_u32ClientId, fFormat, pv, cb);
528 RTUtf16Free(pwszFree);
529
530 WinCloseClipbrd(g_habWorker);
531 }
532 else
533 {
534 VBoxServiceError("VBoxServiceClipboardOS2SendDataToHost: WinOpenClipbrd failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
535 VBoxServiceVerbose(4, "clipboard: writing NULL/0 (fFormat=%x)\n", fFormat);
536 VbglR3ClipboardWriteData(g_u32ClientId, fFormat, NULL, 0);
537 }
538}
539
540
541/**
542 * Figure out what's on the clipboard and report it to the host.
543 */
544static void VBoxServiceClipboardOS2ReportFormats(void)
545{
546 uint32_t fFormats = 0;
547 ULONG ulFormat = 0;
548 while ((ulFormat = WinEnumClipbrdFmts(g_habWorker, ulFormat)) != 0)
549 {
550 if ( ulFormat == CF_TEXT
551 || ulFormat == g_atomOdin32UnicodeText)
552 fFormats |= VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT;
553 /** @todo else bitmaps and stuff. */
554 }
555 VBoxServiceVerbose(4, "clipboard: reporting fFormats=%#x\n", fFormats);
556 VbglR3ClipboardReportFormats(g_u32ClientId, fFormats);
557}
558
559
560/**
561 * Poll the clipboard for changes.
562 *
563 * This is called both when we're the viewer and when we're
564 * falling back to polling. If something has changed it will
565 * notify the host.
566 */
567static void VBoxServiceClipboardOS2Poll(void)
568{
569 if (WinOpenClipbrd(g_habWorker))
570 {
571 /*
572 * If our dummy is no longer there, something has actually changed,
573 * unless the clipboard is really empty.
574 */
575 ULONG fFmtInfo;
576 if (!WinQueryClipbrdFmtInfo(g_habWorker, g_atomNothingChanged, &fFmtInfo))
577 {
578 if (WinEnumClipbrdFmts(g_habWorker, 0) != 0)
579 {
580 g_fEmptyClipboard = false;
581 VBoxServiceClipboardOS2ReportFormats();
582
583 /* inject the dummy */
584 PVOID pv;
585 APIRET rc = DosAllocSharedMem(&pv, NULL, 1, OBJ_GIVEABLE | OBJ_GETTABLE | PAG_READ | PAG_WRITE | PAG_COMMIT);
586 if (rc == NO_ERROR)
587 {
588 if (WinSetClipbrdData(g_habWorker, (ULONG)pv, g_atomNothingChanged, CFI_POINTER))
589 VBoxServiceVerbose(4, "clipboard: Added dummy item.\n");
590 else
591 {
592 VBoxServiceError("VBoxServiceClipboardOS2Poll: WinSetClipbrdData failed, lasterr=%#lx\n", WinGetLastError(g_habWorker));
593 DosFreeMem(pv);
594 }
595 }
596 else
597 VBoxServiceError("VBoxServiceClipboardOS2Poll: DosAllocSharedMem(,,1,) -> %ld\n", rc);
598 }
599 else if (!g_fEmptyClipboard)
600 {
601 g_fEmptyClipboard = true;
602 VBoxServiceVerbose(3, "Reporting empty clipboard\n");
603 VbglR3ClipboardReportFormats(g_u32ClientId, 0);
604 }
605 }
606 WinCloseClipbrd(g_habWorker);
607 }
608 else
609 VBoxServiceError("VBoxServiceClipboardOS2Poll: WinOpenClipbrd failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
610}
611
612
613/**
614 * The clipboard we owned was destroyed by someone else.
615 */
616static void VBoxServiceClipboardOS2Destroyed(void)
617{
618 /* make sure we're no longer the owner. */
619 if (WinQueryClipbrdOwner(g_habWorker) == g_hwndWorker)
620 WinSetClipbrdOwner(g_habWorker, NULLHANDLE);
621
622 /* switch to polling state and notify the host. */
623 g_enmState = kClipboardState_Polling;
624 g_fEmptyClipboard = true;
625 VBoxServiceVerbose(3, "Reporting empty clipboard\n");
626 VbglR3ClipboardReportFormats(g_u32ClientId, 0);
627
628 VBoxServiceClipboardOS2PollViewer();
629}
630
631
632/**
633 * The window procedure for the object window.
634 *
635 * @returns Message result.
636 *
637 * @param hwnd The window handle.
638 * @param msg The message.
639 * @param mp1 Message parameter 1.
640 * @param mp2 Message parameter 2.
641 */
642static MRESULT EXPENTRY VBoxServiceClipboardOS2WinProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
643{
644 if (msg != WM_TIMER)
645 VBoxServiceVerbose(6, "VBoxServiceClipboardOS2WinProc: hwnd=%#lx msg=%#lx mp1=%#lx mp2=%#lx\n", hwnd, msg, mp1, mp2);
646
647 switch (msg)
648 {
649 /*
650 * Handle the two system defined messages for object windows.
651 *
652 * We'll just use the CREATE/DESTROY message to create that timer we're
653 * using for the viewer checks and polling fallback.
654 */
655 case WM_CREATE:
656 g_idWorkerTimer = WinStartTimer(g_habWorker, hwnd, 1 /* id */, 1000 /* 1 second */);
657 g_fEmptyClipboard = true;
658 g_enmState = kClipboardState_Polling;
659 return NULL; /* FALSE(/NULL) == Continue*/
660
661 case WM_DESTROY:
662 WinStopTimer(g_habWorker, hwnd, g_idWorkerTimer);
663 g_idWorkerTimer = ~0UL;
664 g_hwndWorker = NULLHANDLE;
665 break;
666
667 /*
668 * Clipboard viewer message - the content has been changed.
669 * This is sent *after* releasing the clipboard sem
670 * and during the WinSetClipbrdViewer call.
671 */
672 case WM_DRAWCLIPBOARD:
673 if (g_enmState == kClipboardState_SettingViewer)
674 break;
675 AssertMsgBreak(g_enmState == kClipboardState_Viewer, ("g_enmState=%d\n", g_enmState));
676 VBoxServiceClipboardOS2Poll();
677 break;
678
679 /*
680 * Clipboard owner message - the content was replaced.
681 * This is sent by someone with an open clipboard, so don't try open it now.
682 */
683 case WM_DESTROYCLIPBOARD:
684 if (g_enmState == kClipboardState_Destroying)
685 break; /* it's us doing the replacing, ignore. */
686 AssertMsgBreak(g_enmState == kClipboardState_Owner, ("g_enmState=%d\n", g_enmState));
687 VBoxServiceClipboardOS2Destroyed();
688 break;
689
690 /*
691 * Clipboard owner message - somebody is requesting us to render a format.
692 * This is called by someone which owns the clipboard, but that's fine.
693 */
694 case WM_RENDERFMT:
695 AssertMsgBreak(g_enmState == kClipboardState_Owner, ("g_enmState=%d\n", g_enmState));
696 VBoxServiceClipboardOS2RenderFormat(SHORT1FROMMP(mp1));
697 break;
698
699 /*
700 * Clipboard owner message - we're about to quit and should render all formats.
701 *
702 * However, because we're lazy, we'll just ASSUME that since we're quitting
703 * we're probably about to shutdown or something and there is no point in
704 * doing anything here except for emptying the clipboard and removing
705 * ourselves as owner. Any failures at this point are silently ignored.
706 */
707 case WM_RENDERALLFMTS:
708 WinOpenClipbrd(g_habWorker);
709 WinSetClipbrdOwner(g_habWorker, NULLHANDLE);
710 g_enmState = kClipboardState_Destroying;
711 WinEmptyClipbrd(g_habWorker);
712 g_enmState = kClipboardState_Polling;
713 g_fEmptyClipboard = true;
714 WinCloseClipbrd(g_habWorker);
715 break;
716
717 /*
718 * Listener message - the host has new formats to offer.
719 */
720 case WM_USER + VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS:
721 VBoxServiceClipboardOS2AdvertiseHostFormats(LONGFROMMP(mp1));
722 break;
723
724 /*
725 * Listener message - the host wish to read our clipboard data.
726 */
727 case WM_USER + VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA:
728 VBoxServiceClipboardOS2SendDataToHost(LONGFROMMP(mp1));
729 break;
730
731 /*
732 * This is just a fallback polling strategy in case some other
733 * app is trying to view the clipboard too. We also use this
734 * to try recover from errors.
735 *
736 * Because the way the clipboard service works, we have to monitor
737 * it all the time and cannot get away with simpler solutions like
738 * synergy is employing (basically checking upon entering and leaving
739 * a desktop).
740 */
741 case WM_TIMER:
742 if ( g_enmState != kClipboardState_Viewer
743 && g_enmState != kClipboardState_Polling)
744 break;
745
746 /* Lost the position as clipboard viwer?*/
747 if (g_enmState == kClipboardState_Viewer)
748 {
749 if (WinQueryClipbrdViewer(g_habWorker) == hwnd)
750 break;
751 g_enmState = kClipboardState_Polling;
752 }
753
754 /* poll for changes */
755 VBoxServiceClipboardOS2Poll();
756 VBoxServiceClipboardOS2PollViewer();
757 break;
758
759
760 /*
761 * Clipboard owner messages dealing with owner drawn content.
762 * We shouldn't be seeing any of these.
763 */
764 case WM_PAINTCLIPBOARD:
765 case WM_SIZECLIPBOARD:
766 case WM_HSCROLLCLIPBOARD:
767 case WM_VSCROLLCLIPBOARD:
768 AssertMsgFailed(("msg=%lx (%ld)\n", msg, msg));
769 break;
770
771 /*
772 * We shouldn't be seeing any other messages according to the docs.
773 * But for whatever reason, PM sends us a WM_ADJUSTWINDOWPOS message
774 * during WinCreateWindow. So, ignore that and assert on anything else.
775 */
776 default:
777 AssertMsgFailed(("msg=%lx (%ld)\n", msg, msg));
778 case WM_ADJUSTWINDOWPOS:
779 break;
780 }
781 return NULL;
782}
783
784
785/**
786 * The listener thread.
787 *
788 * This thread is dedicated to listening for host messages and forwarding
789 * these to the worker thread (using PM).
790 *
791 * The thread will set g_fListenerOkay and signal its user event when it has
792 * completed initialization. In the case of init failure g_fListenerOkay will
793 * not be set.
794 *
795 * @returns Init error code or VINF_SUCCESS.
796 * @param ThreadSelf Our thread handle.
797 * @param pvUser Pointer to the clipboard service shutdown indicator.
798 */
799static DECLCALLBACK(int) VBoxServiceClipboardOS2Listener(RTTHREAD ThreadSelf, void *pvUser)
800{
801 bool volatile *pfShutdown = (bool volatile *)pvUser;
802 int rc = VERR_GENERAL_FAILURE;
803 VBoxServiceVerbose(3, "VBoxServiceClipboardOS2Listener: ThreadSelf=%RTthrd\n", ThreadSelf);
804
805 g_habListener = WinInitialize(0);
806 if (g_habListener != NULLHANDLE)
807 {
808 g_hmqListener = WinCreateMsgQueue(g_habListener, 0);
809 if (g_hmqListener != NULLHANDLE)
810 {
811 /*
812 * Tell the worker thread that we're good.
813 */
814 rc = VINF_SUCCESS;
815 ASMAtomicXchgBool(&g_fListenerOkay, true);
816 RTThreadUserSignal(ThreadSelf);
817 VBoxServiceVerbose(3, "VBoxServiceClipboardOS2Listener: Started successfully\n");
818
819 /*
820 * Loop until termination is requested.
821 */
822 bool fQuit = false;
823 while (!*pfShutdown && !fQuit)
824 {
825 uint32_t Msg;
826 uint32_t fFormats;
827 rc = VbglR3ClipboardGetHostMsg(g_u32ClientId, &Msg, &fFormats);
828 if (RT_SUCCESS(rc))
829 {
830 VBoxServiceVerbose(3, "VBoxServiceClipboardOS2Listener: Msg=%#x fFormats=%#x\n", Msg, fFormats);
831 switch (Msg)
832 {
833 /*
834 * The host has announced available clipboard formats.
835 * Forward the information to the window, so it can later
836 * respond do WM_RENDERFORMAT message.
837 */
838 case VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS:
839 if (!WinPostMsg(g_hwndWorker, WM_USER + VBOX_SHARED_CLIPBOARD_HOST_MSG_FORMATS,
840 MPFROMLONG(fFormats), 0))
841 VBoxServiceError("WinPostMsg(%lx, FORMATS,,) failed, lasterr=%#lx\n",
842 g_hwndWorker, WinGetLastError(g_habListener));
843 break;
844
845 /*
846 * The host needs data in the specified format.
847 */
848 case VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA:
849 if (!WinPostMsg(g_hwndWorker, WM_USER + VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA,
850 MPFROMLONG(fFormats), 0))
851 VBoxServiceError("WinPostMsg(%lx, READ_DATA,,) failed, lasterr=%#lx\n",
852 g_hwndWorker, WinGetLastError(g_habListener));
853 break;
854
855 /*
856 * The host is terminating.
857 */
858 case VBOX_SHARED_CLIPBOARD_HOST_MSG_QUIT:
859 fQuit = true;
860 break;
861
862 default:
863 VBoxServiceVerbose(1, "VBoxServiceClipboardOS2Listener: Unknown message %RU32\n", Msg);
864 break;
865 }
866 }
867 else
868 {
869 if (*pfShutdown)
870 break;
871 VBoxServiceError("VbglR3ClipboardGetHostMsg failed, rc=%Rrc\n", rc);
872 RTThreadSleep(1000);
873 }
874 } /* the loop */
875
876 WinDestroyMsgQueue(g_hmqListener);
877 }
878 WinTerminate(g_habListener);
879 g_habListener = NULLHANDLE;
880 }
881
882 /* Signal our semaphore to make the worker catch on. */
883 RTThreadUserSignal(ThreadSelf);
884 VBoxServiceVerbose(3, "VBoxServiceClipboardOS2Listener: terminating, rc=%Rrc\n", rc);
885 return rc;
886}
887
888
889/** @copydoc VBOXSERVICE::pfnWorker */
890static DECLCALLBACK(int) VBoxServiceClipboardOS2Worker(bool volatile *pfShutdown)
891{
892 int rc = VERR_GENERAL_FAILURE;
893
894 /*
895 * Standard PM init.
896 */
897 g_habWorker = RTThreadSelf() != g_ThreadCtrl ? WinInitialize(0) : g_habCtrl;
898 if (g_habWorker != NULLHANDLE)
899 {
900 g_hmqWorker = RTThreadSelf() != g_ThreadCtrl ? WinCreateMsgQueue(g_habWorker, 0) : g_hmqCtrl;
901 if (g_hmqWorker != NULLHANDLE)
902 {
903 /*
904 * Create the object window.
905 */
906 if (WinRegisterClass(g_habWorker, (PCSZ)"VBoxServiceClipboardClass", VBoxServiceClipboardOS2WinProc, 0, 0))
907 {
908 g_hwndWorker = WinCreateWindow(HWND_OBJECT, /* hwndParent */
909 (PCSZ)"VBoxServiceClipboardClass", /* pszClass */
910 (PCSZ)"VirtualBox Clipboard Service", /* pszName */
911 0, /* flStyle */
912 0, 0, 0, 0, /* x, y, cx, cy */
913 NULLHANDLE, /* hwndOwner */
914 HWND_BOTTOM, /* hwndInsertBehind */
915 42, /* id */
916 NULL, /* pCtlData */
917 NULL); /* pPresParams */
918 if (g_hwndWorker != NULLHANDLE)
919 {
920 VBoxServiceVerbose(3, "g_hwndWorker=%#lx g_habWorker=%#lx g_hmqWorker=%#lx\n", g_hwndWorker, g_habWorker, g_hmqWorker);
921
922 /*
923 * Create the listener thread.
924 */
925 g_fListenerOkay = false;
926 rc = RTThreadCreate(&g_ThreadListener, VBoxServiceClipboardOS2Listener, (void *)pfShutdown, 0,
927 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "CLIPLISTEN");
928 if (RT_SUCCESS(rc))
929 {
930 RTThreadUserWait(g_ThreadListener, 30*1000);
931 RTThreadUserReset(g_ThreadListener);
932 if (!g_fListenerOkay)
933 RTThreadWait(g_ThreadListener, 60*1000, NULL);
934 if (g_fListenerOkay)
935 {
936 /*
937 * Tell the control thread that it can continue
938 * spawning services.
939 */
940 RTThreadUserSignal(RTThreadSelf());
941
942 /*
943 * The PM event pump.
944 */
945 VBoxServiceVerbose(2, "clipboard: Entering PM message loop.\n");
946 rc = VINF_SUCCESS;
947 QMSG qmsg;
948 while (WinGetMsg(g_habWorker, &qmsg, NULLHANDLE, NULLHANDLE, 0))
949 WinDispatchMsg(g_habWorker, &qmsg);
950 VBoxServiceVerbose(2, "clipboard: Exited PM message loop. *pfShutdown=%RTbool\n", *pfShutdown);
951
952 RTThreadWait(g_ThreadListener, 60*1000, NULL);
953 }
954 g_ThreadListener = NIL_RTTHREAD;
955 }
956
957 /*
958 * Got a WM_QUIT, clean up.
959 */
960 if (g_hwndWorker != NULLHANDLE)
961 {
962 WinDestroyWindow(g_hwndWorker);
963 g_hwndWorker = NULLHANDLE;
964 }
965 }
966 else
967 VBoxServiceError("WinCreateWindow() failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
968 /* no class deregistration in PM. */
969 }
970 else
971 VBoxServiceError("WinRegisterClass() failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
972
973 if (g_hmqCtrl != g_hmqWorker)
974 WinDestroyMsgQueue(g_hmqWorker);
975 g_hmqWorker = NULLHANDLE;
976 }
977 else
978 VBoxServiceError("WinCreateMsgQueue(,0) failed, lasterr=%lx\n", WinGetLastError(g_habWorker));
979
980 if (g_habCtrl != g_habWorker)
981 WinTerminate(g_habWorker);
982 g_habWorker = NULLHANDLE;
983 }
984 else
985 VBoxServiceError("WinInitialize(0) failed, lasterr=%lx\n", WinGetLastError(NULLHANDLE));
986
987 return rc;
988}
989
990
991/** @copydoc VBOXSERVICE::pfnStop */
992static DECLCALLBACK(void) VBoxServiceClipboardOS2Stop(void)
993{
994 if ( g_hmqWorker != NULLHANDLE
995 && !WinPostQueueMsg(g_hmqWorker, WM_QUIT, NULL, NULL))
996 VBoxServiceError("WinPostQueueMsg(g_hmqWorker, WM_QUIT, 0,0) failed, lasterr=%lx\n", WinGetLastError(g_habCtrl));
997}
998
999
1000/** @copydoc VBOXSERVICE::pfnTerm */
1001static DECLCALLBACK(void) VBoxServiceClipboardOS2Term(void)
1002{
1003 VBoxServiceVerbose(4, "clipboard: disconnecting %#x\n", g_u32ClientId);
1004 VbglR3ClipboardDisconnect(g_u32ClientId);
1005 g_u32ClientId = 0;
1006 WinDestroyMsgQueue(g_hmqCtrl);
1007 g_hmqCtrl = NULLHANDLE;
1008 WinTerminate(g_habCtrl);
1009 g_habCtrl = NULLHANDLE;
1010}
1011
1012
1013/**
1014 * The OS/2 'clipboard' service description.
1015 */
1016VBOXSERVICE g_Clipboard =
1017{
1018 /* pszName. */
1019 "clipboard",
1020 /* pszDescription. */
1021 "Shared Clipboard",
1022 /* pszUsage. */
1023 ""
1024 ,
1025 /* pszOptions. */
1026 ""
1027 ,
1028 /* methods */
1029 VBoxServiceClipboardOS2PreInit,
1030 VBoxServiceClipboardOS2Option,
1031 VBoxServiceClipboardOS2Init,
1032 VBoxServiceClipboardOS2Worker,
1033 VBoxServiceClipboardOS2Stop,
1034 VBoxServiceClipboardOS2Term
1035};
1036
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