| 1 | /** @file
|
|---|
| 2 | *
|
|---|
| 3 | * X11 clipboard format viewer
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (C) 2006-2007 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 | #include <X11/Xlib.h>
|
|---|
| 19 | #include <X11/Xatom.h>
|
|---|
| 20 | #include <X11/Intrinsic.h>
|
|---|
| 21 | #include <X11/Shell.h>
|
|---|
| 22 | #include <X11/Xproto.h>
|
|---|
| 23 | #include <X11/StringDefs.h>
|
|---|
| 24 |
|
|---|
| 25 | #include <stdio.h>
|
|---|
| 26 | #include <stdlib.h>
|
|---|
| 27 |
|
|---|
| 28 | /** Convert an atom name string to an X11 atom, looking it up in a cache
|
|---|
| 29 | * before asking the server */
|
|---|
| 30 | static Atom clipGetAtom(Widget widget, const char *pszName)
|
|---|
| 31 | {
|
|---|
| 32 | Atom retval = None;
|
|---|
| 33 | XrmValue nameVal, atomVal;
|
|---|
| 34 | nameVal.addr = (char *) pszName;
|
|---|
| 35 | nameVal.size = strlen(pszName);
|
|---|
| 36 | atomVal.size = sizeof(Atom);
|
|---|
| 37 | atomVal.addr = (char *) &retval;
|
|---|
| 38 | XtConvertAndStore(widget, XtRString, &nameVal, XtRAtom, &atomVal);
|
|---|
| 39 | return retval;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Print out information about available data formats, based on the "targets"
|
|---|
| 44 | * information obtained from the X11 clipboard.
|
|---|
| 45 | * @note callback for XtGetSelectionValue
|
|---|
| 46 | */
|
|---|
| 47 | static void clipConvertX11Targets(Widget, XtPointer pClientData,
|
|---|
| 48 | Atom * /* selection */, Atom *atomType,
|
|---|
| 49 | XtPointer pValue, long unsigned int *pcLen,
|
|---|
| 50 | int *piFormat)
|
|---|
| 51 | {
|
|---|
| 52 | Widget widget = (Widget) pClientData;
|
|---|
| 53 | if (*atomType == XT_CONVERT_FAIL) /* timeout */
|
|---|
| 54 | printf("Time out getting clipboard format data.\n");
|
|---|
| 55 | else if (pValue == NULL) /* No data available */
|
|---|
| 56 | printf("No clipboard format data available.\n");
|
|---|
| 57 | else
|
|---|
| 58 | {
|
|---|
| 59 | Atom *pTargets = (Atom *) pValue;
|
|---|
| 60 | for (unsigned i = 0; i < *pcLen; ++i)
|
|---|
| 61 | {
|
|---|
| 62 | char *pszName = XGetAtomName(XtDisplay(widget), pTargets[i]);
|
|---|
| 63 | printf("Found clipboard format: %s\n", pszName);
|
|---|
| 64 | }
|
|---|
| 65 | XtFree(reinterpret_cast<char *>(pValue));
|
|---|
| 66 | }
|
|---|
| 67 | XtAppSetExitFlag(XtWidgetToApplicationContext(widget));
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | int main()
|
|---|
| 71 | {
|
|---|
| 72 | int cArgc = 0;
|
|---|
| 73 | char *pcArgv = 0;
|
|---|
| 74 | XtToolkitInitialize();
|
|---|
| 75 | XtAppContext ctx = XtCreateApplicationContext();
|
|---|
| 76 | Display *pDisplay = XtOpenDisplay(ctx, 0, 0, "ViewClipFormats", 0, 0,
|
|---|
| 77 | &cArgc, &pcArgv);
|
|---|
| 78 | if (!pDisplay)
|
|---|
| 79 | {
|
|---|
| 80 | printf("Unable to access the X11 window system. Exiting.\n");
|
|---|
| 81 | exit(1);
|
|---|
| 82 | }
|
|---|
| 83 | Widget widget = XtVaAppCreateShell(0, "ViewClipFormats",
|
|---|
| 84 | applicationShellWidgetClass,
|
|---|
| 85 | pDisplay, XtNwidth, 1, XtNheight,
|
|---|
| 86 | 1, NULL);
|
|---|
| 87 | if (!widget)
|
|---|
| 88 | {
|
|---|
| 89 | printf("Failed to create a hidden window for the clipboard format viewer. Exiting.\n");
|
|---|
| 90 | exit(1);
|
|---|
| 91 | }
|
|---|
| 92 | XtSetMappedWhenManaged(widget, false);
|
|---|
| 93 | XtRealizeWidget(widget);
|
|---|
| 94 | XtGetSelectionValue(widget, clipGetAtom(widget, "CLIPBOARD"),
|
|---|
| 95 | clipGetAtom(widget, "TARGETS"), clipConvertX11Targets,
|
|---|
| 96 | widget, CurrentTime);
|
|---|
| 97 | XtAppMainLoop(ctx);
|
|---|
| 98 | }
|
|---|