Exemple #1
1
static void
cliprdr_get_clipboard(XEvent* e)
{
	Atom type;
	unsigned long len, bytes_left, dummy;
	int format, result;
	unsigned char *data;


	log_message(l_config, LOG_LEVEL_DEBUG, "vchannel_cliprdr[cliprdr_get_clipboard]: "
			"New owner %i", e->xselection.requestor);

	XGetWindowProperty (e->xselection.display,
											e->xselection.requestor,
											e->xselection.property,
											0, 0,
											False,
											AnyPropertyType,
											&type,
											&format,
											&len, &bytes_left,
											&data);
	// Check Format list
	if (type == XA_ATOM || format == 32)
	{
		result = XGetWindowProperty (e->xselection.display, e->xselection.requestor, e->xselection.property, 0, bytes_left, 0, XA_ATOM, &type, &format, &len, &dummy, &data);
		if (result == Success)
		{
			int i = 0;
			Atom atom;
			for (i = 0; i < len ; i++)
			{
				atom = ((Atom*)data)[i];
				log_message(l_config, LOG_LEVEL_DEBUG, "vchannel_cliprdr[cliprdr_get_clipboard]: "
						"format : %s", XGetAtomName(display, atom));
				if (clipboard_format_supported(&clipboard, atom))
				{
					clipboard_add_current_clipboard_format(&clipboard, atom);
				}
			}
			if (clipboard_current_clipboard_size(&clipboard) > 0)
			{
				atom = clipboard_get_current_clipboard_format(&clipboard, 0);
				log_message(l_config, LOG_LEVEL_DEBUG, "vchannel_cliprdr[cliprdr_get_clipboard]: "
						"Request for format %s", XGetAtomName(display, atom));

				XConvertSelection(display, clipboard_atom, atom, xrdp_clipboard, wclip, CurrentTime);
				XSync (e->xselectionclear.display, False);
			}
			return;
		}

		log_message(l_config, LOG_LEVEL_DEBUG, "vchannel_cliprdr[cliprdr_get_clipboard]: "
				"Failed to parse atom list");
		return;
	}

	// DATA is There
	log_message(l_config, LOG_LEVEL_DEBUG, "vchannel_cliprdr[cliprdr_get_clipboard]: "
			"Data type : %s\n", XGetAtomName(e->xselection.display, e->xselection.property));

	if (bytes_left > 0 && clipboard_format_supported(&clipboard, e->xselection.target))
	{
		unsigned char* clipboard_data = NULL;
		int clipboard_size = 0;

		result = XGetWindowProperty(e->xselection.display, e->xselection.requestor, e->xselection.property, 0, bytes_left, 0, e->xselection.target, &type, &format, &len, &dummy, &data);
		if (result == Success)
		{
			log_message(l_config, LOG_LEVEL_DEBUG_PLUS, "vchannel_cliprdr[cliprdr_get_clipboard]: "
					"New data in clipboard: %s", data);
			int index = -1;
			index = clipboard_get_current_clipboard_format_index(&clipboard, e->xselection.target);

			// Don't forget the null terminated character
			clipboard_data = g_malloc(bytes_left + 1, 1);
			clipboard_size = bytes_left;
			g_memcpy(clipboard_data, data, bytes_left);
			log_message(l_config, LOG_LEVEL_DEBUG, "vchannel_cliprdr[cliprdr_get_clipboard]: "
					"clipboard %s[%i] updated with '%s'", XGetAtomName(display, e->xselection.target), index, data);

			clipboard_add_current_clipboard_data(&clipboard, clipboard_data, clipboard_size, e->xselection.target);

			if (index < (clipboard_current_clipboard_size(&clipboard) - 1))
			{
				Atom format = clipboard_get_current_clipboard_format(&clipboard, index + 1);
				log_message(l_config, LOG_LEVEL_DEBUG, "vchannel_cliprdr[cliprdr_get_clipboard]: "
						"Request for format %s", XGetAtomName(display, format));

				XConvertSelection(display, clipboard_atom, format, xrdp_clipboard, wclip, CurrentTime);
				XSync (e->xselectionclear.display, False);
			}
			else
			{
				XSetSelectionOwner(display, clipboard_atom, wclip, CurrentTime);
				XSync(display, False);
				// File content is not supported for now
				if ((! clipboard_current_clipboard_format_exist(&clipboard, format_file_gnome_atom)) && (! clipboard_current_clipboard_format_exist(&clipboard, format_file_text_uri_list_atom)))
					cliprdr_send_format_list();
			}
		}
		else
		{
			log_message(l_config, LOG_LEVEL_DEBUG, "vchannel_cliprdr[cliprdr_get_clipboard]: "
					"Failed to get clipboard content");
		}
		XFree (data);
	}
}
Exemple #2
0
/**
 * Function description
 *
 * @return 0 on success, otherwise a Win32 error code
 */
static UINT xf_cliprdr_server_format_list(CliprdrClientContext* context,
        CLIPRDR_FORMAT_LIST* formatList)
{
	UINT32 i;
	int j;
	xfClipboard* clipboard = (xfClipboard*) context->custom;
	xfContext* xfc = clipboard->xfc;
	UINT ret;
	xf_cliprdr_clear_cached_data(clipboard);
	clipboard->data_format_id = -1;
	clipboard->data_format_name = NULL;

	if (clipboard->serverFormats)
	{
		for (j = 0; j < clipboard->numServerFormats; j++)
			free(clipboard->serverFormats[j].formatName);

		free(clipboard->serverFormats);
		clipboard->serverFormats = NULL;
		clipboard->numServerFormats = 0;
	}

	clipboard->numServerFormats = formatList->numFormats + 1; /* +1 for CF_RAW */

	if (!(clipboard->serverFormats = (CLIPRDR_FORMAT*) calloc(
	                                     clipboard->numServerFormats, sizeof(CLIPRDR_FORMAT))))
	{
		WLog_ERR(TAG, "failed to allocate %d CLIPRDR_FORMAT structs",
		         clipboard->numServerFormats);
		return CHANNEL_RC_NO_MEMORY;
	}

	for (i = 0; i < formatList->numFormats; i++)
	{
		CLIPRDR_FORMAT* format = &formatList->formats[i];
		clipboard->serverFormats[i].formatId = format->formatId;

		if (format->formatName)
		{
			clipboard->serverFormats[i].formatName = _strdup(format->formatName);

			if (!clipboard->serverFormats[i].formatName)
			{
				UINT32 k;

				for (k = 0; k < i; k++)
					free(clipboard->serverFormats[k].formatName);

				clipboard->numServerFormats = 0;
				free(clipboard->serverFormats);
				clipboard->serverFormats = NULL;
				return CHANNEL_RC_NO_MEMORY;
			}
		}
	}

	/* CF_RAW is always implicitly supported by the server */
	{
		CLIPRDR_FORMAT* format = &clipboard->serverFormats[formatList->numFormats];
		format->formatId = CF_RAW;
		format->formatName = NULL;
	}
	xf_cliprdr_provide_server_format_list(clipboard);
	clipboard->numTargets = 2;

	for (i = 0; i < formatList->numFormats; i++)
	{
		CLIPRDR_FORMAT* format = &formatList->formats[i];

		for (j = 0; j < clipboard->numClientFormats; j++)
		{
			if (xf_cliprdr_formats_equal(format, &clipboard->clientFormats[j]))
			{
				xf_cliprdr_append_target(clipboard, clipboard->clientFormats[j].atom);
			}
		}
	}

	ret = xf_cliprdr_send_client_format_list_response(clipboard, TRUE);
	XSetSelectionOwner(xfc->display, clipboard->clipboard_atom, xfc->drawable,
	                   CurrentTime);
	XFlush(xfc->display);
	return ret;
}
Exemple #3
0
static void setclipboard(void)
{
    XSetSelectionOwner(display, XA_CLIPBOARD, window, CurrentTime);
}
Exemple #4
0
bool
XimServer::setupConnection(bool useDefaultIM)
{
    const char *buf;
    if (!useDefaultIM) {
	return false;
    } else {
	buf = "@server=uim";
    }
    mServerAtom = XInternAtom(XimServer::gDpy, buf, 0);
    Window owner = XGetSelectionOwner(XimServer::gDpy, mServerAtom);
    if (owner != None) {
	if (!useDefaultIM)
	    printf("Another instance exists (uim-%s).\n", mIMName);
	else
	    printf("Another instance exists (uim).\n");
	return false;
    }
    mSelectionWin = XCreateSimpleWindow(XimServer::gDpy,
					DefaultRootWindow(XimServer::gDpy),
					0, 0, 1, 1,
					1, 0, 0);
    XSetSelectionOwner(XimServer::gDpy, mServerAtom, mSelectionWin, CurrentTime);
    XSelectInput(XimServer::gDpy, DefaultRootWindow(XimServer::gDpy), 0);
    XSync(XimServer::gDpy, False);

    Atom type;
    int format;
    unsigned long nr_prop, nr_bytes;
    Atom *prop;
    int mode = PropModePrepend;
    int valuechange = 1;

    XGetWindowProperty(XimServer::gDpy, DefaultRootWindow(XimServer::gDpy),
		       xim_servers, 0, 8192 ,False,
		       XA_ATOM, &type, &format,
		       &nr_prop, &nr_bytes, (unsigned char **)(uintptr_t)&prop);
    int i;
    if (type != XA_ATOM || format != 32)
	mode = PropModeReplace;
    else {
	for (i = 0; i < (int)nr_prop; i++) {
	    if (prop[i] == mServerAtom) {
		mode = PropModeAppend;
		valuechange = 0;
		break;
	    }
	}
    }
    if (nr_prop)
	XFree(prop);

    XChangeProperty(XimServer::gDpy, DefaultRootWindow(XimServer::gDpy),
		    xim_servers,
		    XA_ATOM, 32,
		    mode, (unsigned char *)&mServerAtom,
		    valuechange ? 1 : 0);
    std::pair<Window, XimServer *> p(mSelectionWin, this);
    gServerMap.insert(p);
    return true;
}
Exemple #5
0
/*
 * Hlavní cyklus zpracování událostí
 */
static void event_loop(void)
{
    XEvent event;
    XSelectionEvent sel;
    Atom tgt_atoms[] = { targets, XA_STRING };
    Atom gwp_type;
    int gwp_format;
    unsigned long gwp_items, gwp_after;
    unsigned char *gwp_prop;
    
    while(1) {
	XNextEvent(display, &event);
	switch(event.type) {
	    case Expose:
		expose(&event.xexpose);
		break;
	    case ButtonPress:
		debug("Button %u pressed\n", event.xbutton.button);
		switch(event.xbutton.button) {
		    case 1: /* Pøivlastnìní výbìru */
			XSetSelectionOwner(display, XA_PRIMARY, topwin,
					   event.xbutton.time);
			if(XGetSelectionOwner(display, XA_PRIMARY) == topwin)
			    debug("Selection aquired\n");
			else
			    debug("Selection not aquired\n");
			break;
		    case 2: /* Pøeètení dat z výbìru */
			XConvertSelection(display, XA_PRIMARY, XA_STRING,
					  sel_prop, topwin, event.xbutton.time);
			break;
		}
		break;
	    case KeyPress:
		return;
		break;
	    case SelectionClear: /* Nìkdo jiný si pøivlastnil výbìr */
		debug("Selection ownership taken away\n");
		break;
	    case SelectionNotify: /* Vlastník poslal data */
		if(event.xselection.target != XA_STRING)
		    debug("Received unexpected target %s\n",
			  XGetAtomName(display, event.xselection.target));
		else if(event.xselection.property != sel_prop)
		    debug("Owner cannot provide selection\n");
		else
		    if(XGetWindowProperty(display, topwin, sel_prop, 0, 
					  LONG_MAX, True, AnyPropertyType,
					  &gwp_type, &gwp_format, &gwp_items,
					  &gwp_after, &gwp_prop) == Success) {
			if(gwp_type != XA_STRING || gwp_format != 8) {
			    fprintf(stderr, "Unexpected type %s, format %d\n",
				    XGetAtomName(display, gwp_type), 
				    gwp_format);
			    XFree(gwp_prop);
			} else {
			    if(text)
				XFree(text);
			    text = gwp_prop;
			    XClearArea(display, topwin, 0, 0, 0, 0, True);
			}
		    } else
			fprintf(stderr, "XGetWindowProperty() failed\n");
		break;
	    case SelectionRequest: /* Nìkdo chce obsah výbìru */
		sel.type = SelectionNotify;
		sel.requestor = event.xselectionrequest.requestor;
		sel.selection = XA_PRIMARY;
		sel.target = event.xselectionrequest.target;
		sel.property = event.xselectionrequest.property;
		sel.time = event.xselectionrequest.time;
		if(sel.target == XA_STRING) {
		    debug("Providing \"STRING\" to %#x\n", sel.requestor);
		    XChangeProperty(display, sel.requestor, sel.property,
				    XA_STRING, 8, PropModeReplace,
				    (char *)"Hello World", 11);
		} 
		else if(sel.target == targets) {
		    debug("Providing \"TARGETS\" to %#x\n", sel.requestor);
		    XChangeProperty(display, sel.requestor, sel.property,
				    XA_ATOM, sizeof(Atom)*8, PropModeReplace,
				    (char *)&tgt_atoms, 2);
		}
		else {
		    debug("Cannot convert selection to target %s "
			  "requested by %#x\n",
			  XGetAtomName(display, sel.target), sel.requestor);
		    sel.property = None;
		}
		XSendEvent(display, sel.requestor, False, 0, (XEvent *)&sel);
		break;
	    case ConfigureNotify:
		if((int)topwin_w != event.xconfigure.width ||
		   (int)topwin_h != event.xconfigure.height) {
		    topwin_w = event.xconfigure.width;
		    topwin_h = event.xconfigure.height;
		    debug("Window resized to %ux%u pixels\n",
			  topwin_w, topwin_h);
		    XClearWindow(display, topwin);
		}
		break;
	    case MappingNotify:
		/* Naèíst zmìnìné mapování kláves */
		if(event.xmapping.request == MappingKeyboard)
		    XRefreshKeyboardMapping(&event.xmapping);
		break;
	    default:
		/* Zbylé nezajímavé události */
		break;
	}
    }
}
LRESULT CALLBACK
winClipboardWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND s_hwndNextViewer;
    static Bool s_fCBCInitialized;
    static Display *pDisplay;
    static Window iWindow;
    static ClipboardAtoms *atoms;
    static Bool fRunning;

    /* Branch on message type */
    switch (message) {
    case WM_DESTROY:
    {
        winDebug("winClipboardWindowProc - WM_DESTROY\n");

        if (g_fHasModernClipboardApi)
            {
                /* Remove clipboard listener */
                g_fpRemoveClipboardFormatListener(hwnd);
            }
        else
            {
                /* Remove ourselves from the clipboard chain */
                ChangeClipboardChain(hwnd, s_hwndNextViewer);
            }

        s_hwndNextViewer = NULL;
    }
        return 0;

    case WM_WM_QUIT:
    {
        winDebug("winClipboardWindowProc - WM_WM_QUIT\n");
        fRunning = FALSE;
        PostQuitMessage(0);
    }
        return 0;

    case WM_CREATE:
    {
        ClipboardWindowCreationParams *cwcp = (ClipboardWindowCreationParams *)((CREATESTRUCT *)lParam)->lpCreateParams;

        winDebug("winClipboardWindowProc - WM_CREATE\n");

        pDisplay = cwcp->pClipboardDisplay;
        iWindow = cwcp->iClipboardWindow;
        atoms = cwcp->atoms;
        fRunning = TRUE;

        if (g_fHasModernClipboardApi)
            {
                g_fpAddClipboardFormatListener(hwnd);
            }
        else
            {
                HWND first, next;
                DWORD error_code = 0;

                first = GetClipboardViewer();   /* Get handle to first viewer in chain. */
                if (first == hwnd)
                    return 0;           /* Make sure it's not us! */
                /* Add ourselves to the clipboard viewer chain */
                next = SetClipboardViewer(hwnd);
                error_code = GetLastError();
                if (SUCCEEDED(error_code) && (next == first))   /* SetClipboardViewer must have succeeded, and the handle */
                    s_hwndNextViewer = next;    /* it returned must have been the first window in the chain */
                else
                    s_fCBCInitialized = FALSE;
            }
    }
        return 0;

    case WM_CHANGECBCHAIN:
    {
        winDebug("winClipboardWindowProc - WM_CHANGECBCHAIN: wParam(%p) "
                 "lParam(%p) s_hwndNextViewer(%p)\n",
                 (HWND)wParam, (HWND)lParam, s_hwndNextViewer);

        if ((HWND) wParam == s_hwndNextViewer) {
            s_hwndNextViewer = (HWND) lParam;
            if (s_hwndNextViewer == hwnd) {
                s_hwndNextViewer = NULL;
                ErrorF("winClipboardWindowProc - WM_CHANGECBCHAIN: "
                       "attempted to set next window to ourselves.");
            }
        }
        else if (s_hwndNextViewer)
            SendMessage(s_hwndNextViewer, message, wParam, lParam);

    }
        winDebug("winClipboardWindowProc - WM_CHANGECBCHAIN: Exit\n");
        return 0;

    case WM_WM_REINIT:
    {
        /* Ensure that we're in the clipboard chain.  Some apps,
         * WinXP's remote desktop for one, don't play nice with the
         * chain.  This message is called whenever we receive a
         * WM_ACTIVATEAPP message to ensure that we continue to
         * receive clipboard messages.
         *
         * It might be possible to detect if we're still in the chain
         * by calling SendMessage (GetClipboardViewer(),
         * WM_DRAWCLIPBOARD, 0, 0); and then seeing if we get the
         * WM_DRAWCLIPBOARD message.  That, however, might be more
         * expensive than just putting ourselves back into the chain.
         */

        HWND first, next;
        DWORD error_code = 0;

        winDebug("winClipboardWindowProc - WM_WM_REINIT: Enter\n");

        if (g_fHasModernClipboardApi)
            {
                return 0;
            }

        first = GetClipboardViewer();   /* Get handle to first viewer in chain. */
        if (first == hwnd)
            return 0;           /* Make sure it's not us! */
        winDebug("  WM_WM_REINIT: Replacing us(%p) with %p at head "
                 "of chain\n", hwnd, s_hwndNextViewer);
        s_fCBCInitialized = FALSE;
        ChangeClipboardChain(hwnd, s_hwndNextViewer);
        s_hwndNextViewer = NULL;
        s_fCBCInitialized = FALSE;
        winDebug("  WM_WM_REINIT: Putting us back at head of chain.\n");
        first = GetClipboardViewer();   /* Get handle to first viewer in chain. */
        if (first == hwnd)
            return 0;           /* Make sure it's not us! */
        next = SetClipboardViewer(hwnd);
        error_code = GetLastError();
        if (SUCCEEDED(error_code) && (next == first))   /* SetClipboardViewer must have succeeded, and the handle */
            s_hwndNextViewer = next;    /* it returned must have been the first window in the chain */
        else
            s_fCBCInitialized = FALSE;
    }
        winDebug("winClipboardWindowProc - WM_WM_REINIT: Exit\n");
        return 0;

    case WM_DRAWCLIPBOARD:
    case WM_CLIPBOARDUPDATE:
    {
        static Bool s_fProcessingDrawClipboard = FALSE;
        int iReturn;

        if (message == WM_DRAWCLIPBOARD)
            winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD: Enter\n");
        else
            winDebug("winClipboardWindowProc -  WM_CLIPBOARDUPDATE: Enter\n");

        if (!g_fHasModernClipboardApi)
            {
                /*
                 * We've occasionally seen a loop in the clipboard chain.
                 * Try and fix it on the first hint of recursion.
                 */
                if (!s_fProcessingDrawClipboard) {
                    s_fProcessingDrawClipboard = TRUE;
                }
                else {
                    /* Attempt to break the nesting by getting out of the chain, twice?, and then fix and bail */
                    s_fCBCInitialized = FALSE;
                    ChangeClipboardChain(hwnd, s_hwndNextViewer);
                    winFixClipboardChain();
                    ErrorF("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                           "Nested calls detected.  Re-initing.\n");
                    winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD: Exit\n");
                    s_fProcessingDrawClipboard = FALSE;
                    return 0;
                }

                /* Bail on first message */
                if (!s_fCBCInitialized) {
                    s_fCBCInitialized = TRUE;
                    s_fProcessingDrawClipboard = FALSE;
                    winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD: Exit\n");
                    return 0;
                }
            }

        /*
         * NOTE: We cannot bail out when NULL == GetClipboardOwner ()
         * because some applications deal with the clipboard in a manner
         * that causes the clipboard owner to be NULL when they are in
         * fact taking ownership.  One example of this is the Win32
         * native compile of emacs.
         */

        /* Bail when we still own the clipboard */
        if (hwnd == GetClipboardOwner()) {

            winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                     "We own the clipboard, returning.\n");
            winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD: Exit\n");
            s_fProcessingDrawClipboard = FALSE;
            if (s_hwndNextViewer)
                SendMessage(s_hwndNextViewer, message, wParam, lParam);
            return 0;
        }

        /* Bail when shutting down */
        if (!fRunning)
            return 0;

        /*
         * Do not take ownership of the X11 selections when something
         * other than CF_TEXT or CF_UNICODETEXT has been copied
         * into the Win32 clipboard.
         */
        if (!IsClipboardFormatAvailable(CF_TEXT)
            && !IsClipboardFormatAvailable(CF_UNICODETEXT)) {

            winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                     "Clipboard does not contain CF_TEXT nor "
                     "CF_UNICODETEXT.\n");

            /*
             * We need to make sure that the X Server has processed
             * previous XSetSelectionOwner messages.
             */
            XSync(pDisplay, FALSE);

            winDebug("winClipboardWindowProc - XSync done.\n");

            /* Release PRIMARY selection if owned */
            iReturn = XGetSelectionOwner(pDisplay, XA_PRIMARY);
            if (iReturn == iWindow) {
                winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                         "PRIMARY selection is owned by us.\n");
                XSetSelectionOwner(pDisplay, XA_PRIMARY, None, CurrentTime);
            }
            else if (BadWindow == iReturn || BadAtom == iReturn)
                ErrorF("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                       "XGetSelectionOwner failed for PRIMARY: %d\n",
                       iReturn);

            /* Release CLIPBOARD selection if owned */
            iReturn = XGetSelectionOwner(pDisplay, atoms->atomClipboard);
            if (iReturn == iWindow) {
                winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                         "CLIPBOARD selection is owned by us, releasing\n");
                XSetSelectionOwner(pDisplay, atoms->atomClipboard, None, CurrentTime);
            }
            else if (BadWindow == iReturn || BadAtom == iReturn)
                ErrorF("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                       "XGetSelectionOwner failed for CLIPBOARD: %d\n",
                       iReturn);

            winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD: Exit\n");
            s_fProcessingDrawClipboard = FALSE;
            if (s_hwndNextViewer)
                SendMessage(s_hwndNextViewer, message, wParam, lParam);
            return 0;
        }

        /* Reassert ownership of PRIMARY */
        iReturn = XSetSelectionOwner(pDisplay,
                                     XA_PRIMARY, iWindow, CurrentTime);
        if (iReturn == BadAtom || iReturn == BadWindow ||
            XGetSelectionOwner(pDisplay, XA_PRIMARY) != iWindow) {
            ErrorF("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                   "Could not reassert ownership of PRIMARY\n");
        }
        else {
            winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                     "Reasserted ownership of PRIMARY\n");
        }

        /* Reassert ownership of the CLIPBOARD */
        iReturn = XSetSelectionOwner(pDisplay,
                                     atoms->atomClipboard, iWindow, CurrentTime);

        if (iReturn == BadAtom || iReturn == BadWindow ||
            XGetSelectionOwner(pDisplay, atoms->atomClipboard) != iWindow) {
            ErrorF("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                    "Could not reassert ownership of CLIPBOARD\n");
        }
        else {
            winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
                     "Reasserted ownership of CLIPBOARD\n");
        }

        /* Flush the pending SetSelectionOwner event now */
        XFlush(pDisplay);

        s_fProcessingDrawClipboard = FALSE;
    }
        winDebug("winClipboardWindowProc - WM_DRAWCLIPBOARD: Exit\n");
        /* Pass the message on the next window in the clipboard viewer chain */
        if (s_hwndNextViewer)
            SendMessage(s_hwndNextViewer, message, wParam, lParam);
        return 0;

    case WM_DESTROYCLIPBOARD:
        /*
         * NOTE: Intentionally do nothing.
         * Changes in the Win32 clipboard are handled by WM_DRAWCLIPBOARD
         * above.  We only process this message to conform to the specs
         * for delayed clipboard rendering in Win32.  You might think
         * that we need to release ownership of the X11 selections, but
         * we do not, because a WM_DRAWCLIPBOARD message will closely
         * follow this message and reassert ownership of the X11
         * selections, handling the issue for us.
         */
        winDebug("winClipboardWindowProc - WM_DESTROYCLIPBOARD - Ignored.\n");
        return 0;

    case WM_RENDERALLFORMATS:
        winDebug("winClipboardWindowProc - WM_RENDERALLFORMATS - Hello.\n");

        /*
          WM_RENDERALLFORMATS is sent as we are shutting down, to render the
          clipboard so it's contents remains available to other applications.

          Unfortunately, this can't work without major changes. The server is
          already waiting for us to stop, so we can't ask for the rendering of
          clipboard text now.
        */

        return 0;

    case WM_RENDERFORMAT:
    {
        int iReturn;
        Bool fConvertToUnicode;
        Bool pasted = FALSE;
        Atom selection;
        ClipboardConversionData data;
        int best_target = 0;

        winDebug("winClipboardWindowProc - WM_RENDERFORMAT %d - Hello.\n",
                 (int)wParam);

        /* Flag whether to convert to Unicode or not */
        fConvertToUnicode = (CF_UNICODETEXT == wParam);

        selection = winClipboardGetLastOwnedSelectionAtom(atoms);
        if (selection == None) {
            ErrorF("winClipboardWindowProc - no monitored selection is owned\n");
            goto fake_paste;
        }

        winDebug("winClipboardWindowProc - requesting targets for selection from owner\n");

        /* Request the selection's supported conversion targets */
        XConvertSelection(pDisplay,
                          selection,
                          atoms->atomTargets,
                          atoms->atomLocalProperty,
                          iWindow, CurrentTime);

        /* Process X events */
        data.fUseUnicode = fConvertToUnicode;
        iReturn = winProcessXEventsTimeout(hwnd,
                                           iWindow,
                                           pDisplay,
                                           &data,
                                           atoms,
                                           WIN_POLL_TIMEOUT);

        if (WIN_XEVENTS_NOTIFY_TARGETS != iReturn) {
            ErrorF
                ("winClipboardWindowProc - timed out waiting for WIN_XEVENTS_NOTIFY_TARGETS\n");
            goto fake_paste;
        }

        /* Choose the most preferred target */
        {
            struct target_priority
            {
                Atom target;
                unsigned int priority;
            };

            struct target_priority target_priority_table[] =
                {
                    { atoms->atomCompoundText, 0 },
#ifdef X_HAVE_UTF8_STRING
                    { atoms->atomUTF8String,   1 },
#endif
                    { XA_STRING,               2 },
                };

            int best_priority = INT_MAX;

            int i,j;
            for (i = 0 ; data.targetList[i] != 0; i++)
                {
                    for (j = 0; j < sizeof(target_priority_table)/sizeof(struct target_priority); j ++)
                        {
                            if ((data.targetList[i] == target_priority_table[j].target) &&
                                (target_priority_table[j].priority < best_priority))
                                {
                                    best_target = target_priority_table[j].target;
                                    best_priority = target_priority_table[j].priority;
                                }
                        }
                }
        }

        free(data.targetList);
        data.targetList = 0;

        winDebug("winClipboardWindowProc - best target is %d\n", best_target);

        /* No useful targets found */
        if (best_target == 0)
          goto fake_paste;

        winDebug("winClipboardWindowProc - requesting selection from owner\n");

        /* Request the selection contents */
        XConvertSelection(pDisplay,
                          selection,
                          best_target,
                          atoms->atomLocalProperty,
                          iWindow, CurrentTime);

        /* Process X events */
        iReturn = winProcessXEventsTimeout(hwnd,
                                           iWindow,
                                           pDisplay,
                                           &data,
                                           atoms,
                                           WIN_POLL_TIMEOUT);

        /*
         * winProcessXEventsTimeout had better have seen a notify event,
         * or else we are dealing with a buggy or old X11 app.
         */
        if (WIN_XEVENTS_NOTIFY_DATA != iReturn) {
            ErrorF
                ("winClipboardWindowProc - timed out waiting for WIN_XEVENTS_NOTIFY_DATA\n");
        }
        else {
            pasted = TRUE;
        }

         /*
          * If we couldn't get the data from the X clipboard, we
          * have to paste some fake data to the Win32 clipboard to
          * satisfy the requirement that we write something to it.
          */
    fake_paste:
        if (!pasted)
          {
            /* Paste no data, to satisfy required call to SetClipboardData */
            SetClipboardData(CF_UNICODETEXT, NULL);
            SetClipboardData(CF_TEXT, NULL);
          }

        winDebug("winClipboardWindowProc - WM_RENDERFORMAT - Returning.\n");
        return 0;
    }
    }

    /* Let Windows perform default processing for unhandled messages */
    return DefWindowProc(hwnd, message, wParam, lParam);
}
Exemple #7
0
static void _clippy_copy_to_sys(int do_sel)
{
        int j;
        char *dst;
        char *freeme;
#if defined(__QNXNTO__)
        PhClipboardHdr clheader = {Ph_CLIPBOARD_TYPE_TEXT, 0, NULL};
        char *tmp;
        int *cldata;
        int status;
#endif

        freeme = NULL;
        if (!_current_selection) {
                dst = NULL;
                j = 0;
        } else
#if defined(WIN32)
        j = strlen(_current_selection);
#else
        if (has_sys_clip) {
                int i;
                /* convert to local */
                freeme = dst = malloc(strlen(_current_selection)+4);
                if (!dst) return;
                for (i = j = 0; _current_selection[i]; i++) {
                        dst[j] = _current_selection[i];
                        if (dst[j] != '\r') j++;
                }
                dst[j] = '\0';
        } else {
                dst = NULL;
                j = 0;
        }
#endif
#if defined(USE_X11)
        if (has_sys_clip) {
                lock_display();
                if (!dst) dst = (char *) ""; /* blah */
                if (j < 0) j = 0;
                if (do_sel) {
                        if (XGetSelectionOwner(SDL_Display, XA_PRIMARY) != SDL_Window) {
                                XSetSelectionOwner(SDL_Display, XA_PRIMARY, SDL_Window, CurrentTime);
                        }
                        XChangeProperty(SDL_Display,
                                DefaultRootWindow(SDL_Display),
                                XA_CUT_BUFFER1, XA_STRING, 8,
                                PropModeReplace, (unsigned char *)dst, j);
                } else {
                        if (XGetSelectionOwner(SDL_Display, atom_clip) != SDL_Window) {
                                XSetSelectionOwner(SDL_Display, atom_clip, SDL_Window, CurrentTime);
                        }
                        XChangeProperty(SDL_Display,
                                DefaultRootWindow(SDL_Display),
                                XA_CUT_BUFFER0, XA_STRING, 8,
                                PropModeReplace, (unsigned char *)dst, j);
                        XChangeProperty(SDL_Display,
                                DefaultRootWindow(SDL_Display),
                                XA_CUT_BUFFER1, XA_STRING, 8,
                                PropModeReplace, (unsigned char *)dst, j);
                }
                unlock_display();
        }
#elif defined(WIN32)
        if (!do_sel && OpenClipboard(SDL_Window)) {
                _hmem = GlobalAlloc((GMEM_MOVEABLE|GMEM_DDESHARE), j+1);
                if (_hmem) {
                        dst = (char *)GlobalLock(_hmem);
                        if (dst) {
                                /* this seems wrong, but msdn does this */
                                memcpy(dst, _current_selection, j);
                                dst[j] = '\0';
                                GlobalUnlock(_hmem);
                                EmptyClipboard();
                                SetClipboardData(CF_TEXT, _hmem);
                        }
                }
                CloseClipboard();
                _hmem = NULL;
                dst = 0;
        }
#elif defined(__QNXNTO__)
        if (!do_sel) {
                tmp = (char *)malloc(j+4);
                if (!tmp) {
                        cldata=(int*)tmp;
                        *cldata = Ph_CL_TEXT;
                        if (dst) memcpy(tmp+4, dst, j);
                        clheader.data = tmp;
#if (NTO_VERSION < 620)
                        if (clheader.length > 65535) clheader.length=65535;
#endif
                        clheader.length = j + 4;
#if (NTO_VERSION < 620)
                        PhClipboardCopy(inputgroup, 1, &clheader);
#else
                        PhClipboardWrite(inputgroup, 1, &clheader);
#endif
                        free(tmp);
                }
        }
#elif defined(MACOSX)
        if (!do_sel) macosx_clippy_put(_current_clipboard);
#else
        // some other system -- linux without x11, maybe
        // pretend we used the param to silence warnings
        (void) do_sel;
#endif
        if (freeme)
                free(freeme);
}
static gboolean
egg_tray_manager_manage_xscreen (EggTrayManager *manager, Screen *xscreen)
{
  GtkWidget *invisible;
  char *selection_atom_name;
  guint32 timestamp;
  GdkScreen *screen;
  GdkWindow *window;
  
  g_return_val_if_fail (EGG_IS_TRAY_MANAGER (manager), FALSE);
  g_return_val_if_fail (manager->screen == NULL, FALSE);

  /* If there's already a manager running on the screen
   * we can't create another one.
   */
#if 0
  if (egg_tray_manager_check_running_xscreen (xscreen))
    return FALSE;
#endif
  screen = gdk_display_get_screen (gdk_x11_lookup_xdisplay (DisplayOfScreen (xscreen)),
				   XScreenNumberOfScreen (xscreen));
  
  invisible = gtk_invisible_new_for_screen (screen);
  gtk_widget_realize (invisible);
  window = gtk_widget_get_window (GTK_WIDGET(invisible));
  
  gtk_widget_add_events (invisible, GDK_PROPERTY_CHANGE_MASK | GDK_STRUCTURE_MASK);

  selection_atom_name = g_strdup_printf ("_NET_SYSTEM_TRAY_S%d",
					 XScreenNumberOfScreen (xscreen));
  manager->selection_atom = XInternAtom (DisplayOfScreen (xscreen), selection_atom_name, False);

  g_free (selection_atom_name);
  
  timestamp = gdk_x11_get_server_time (gtk_widget_get_window (invisible));
  XSetSelectionOwner (DisplayOfScreen (xscreen), manager->selection_atom,
		      GDK_WINDOW_XID (gtk_widget_get_window (invisible)), timestamp);

  /* Check if we were could set the selection owner successfully */
  if (XGetSelectionOwner (DisplayOfScreen (xscreen), manager->selection_atom) ==
      GDK_WINDOW_XID (gtk_widget_get_window (invisible)))
    {
      XClientMessageEvent xev;

      xev.type = ClientMessage;
      xev.window = RootWindowOfScreen (xscreen);
      xev.message_type = XInternAtom (DisplayOfScreen (xscreen), "MANAGER", False);

      xev.format = 32;
      xev.data.l[0] = timestamp;
      xev.data.l[1] = manager->selection_atom;
      xev.data.l[2] = GDK_WINDOW_XID (gtk_widget_get_window (invisible));
      xev.data.l[3] = 0;	/* manager specific data */
      xev.data.l[4] = 0;	/* manager specific data */

      XSendEvent (DisplayOfScreen (xscreen),
		  RootWindowOfScreen (xscreen),
		  False, StructureNotifyMask, (XEvent *)&xev);

      manager->invisible = invisible;
      g_object_ref (G_OBJECT (manager->invisible));
      
      manager->opcode_atom = XInternAtom (DisplayOfScreen (xscreen),
					  "_NET_SYSTEM_TRAY_OPCODE",
					  False);

      manager->message_data_atom = XInternAtom (DisplayOfScreen (xscreen),
						"_NET_SYSTEM_TRAY_MESSAGE_DATA",
						False);

      /* Add a window filter */
      gdk_window_add_filter (gtk_widget_get_window (invisible), egg_tray_manager_window_filter, manager);
      return TRUE;
    }
  else
    {
      gtk_widget_destroy (invisible);
 
      return FALSE;
    }
}
Exemple #9
0
main()
{
Display *dpy = XOpenDisplay(NULL);
assert(dpy);
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 
                 200, 100, 0, 0, 0);
XSelectInput(dpy, w, StructureNotifyMask);
XMapWindow(dpy, w);
XSelectionRequestEvent *req;
XEvent e, respond;
for(;;) {
    XNextEvent(dpy, &e);
    if (e.type == MapNotify) break;
}
XFlush(dpy);
//
Atom a1, a2, a3, type;
XSelectInput(dpy, w, StructureNotifyMask+ExposureMask);
int format, result;
unsigned long len, bytes_left, dummy;
unsigned char *data;
Window Sown;
for (int ii = 0; ii < 50; ii++) {
    XSetSelectionOwner (dpy, XA_PRIMARY, w, CurrentTime);
    XFlush (dpy);
    XNextEvent (dpy, &e);
    if (e.type == SelectionRequest)
    //
    // Somebody wants our data
    //
    {
        req=&(e.xselectionrequest);
        printf ("Selection Request from Mr %i I am %i\n",
            (int)e.xselection.requestor, (int)w);
        printf ("prop:%i tar:%i sel:%i\n", req->property,
            req->target, req->selection);
        if (req->target == XA_STRING)
        {
            XChangeProperty (dpy,
                req->requestor,
                req->property,
                XA_STRING,
                8,
                PropModeReplace,
                (unsigned char*) "It Works",
                8);
            respond.xselection.property=req->property;
        }
        else // Strings only please
        {
            printf ("No String %i\n",
                (int)req->target);
            respond.xselection.property= None;
        }
        respond.xselection.type= SelectionNotify;
        respond.xselection.display= req->display;
        respond.xselection.requestor= req->requestor;
        respond.xselection.selection=req->selection;
        respond.xselection.target= req->target;
        respond.xselection.time = req->time;
        XSendEvent (dpy, req->requestor,0,0,&respond);
        XFlush (dpy);
    }
}
}
Exemple #10
0
void init_atom_property()
{
  hime_atom = get_hime_atom(dpy);
  XSetSelectionOwner(dpy, hime_atom, xim_xwin, CurrentTime);
}
Exemple #11
0
int main(int argc, char *argv[])
 {
   // Declare variables
   char *in = NULL;				// piped input char array
   char inc;					// current char of input
   int inelems = 0;                             // number of used elements in in
   int inalloc = 0;				// size of in
   Display *display; 		                // Display connection
   Window w;					// Window
   XSelectionRequestEvent *req;
   XEvent ev, re;				// X Event Structures

   int optc;                                    // option char
   int opti;                                    // option index
   
   int dloop = 0;				// done loops counter

   // Stuff that gets set
   int   sloop = 1;
   char *sdisp = "";

   // Flags for command line options
   static int fverb = 1;		// verbose mode (default)
   static int fhelp = 0;		// dispay help
   static int fvers = 0;		// dispay version info
   
   // options
   static struct option long_options[] = {
     { "quiet"  	, 0, &fverb, 0 },
     { "help"		, 0, &fhelp, 1 },
     { "version"	, 0, &fvers, 1 },
     { "display"	, 1,      0, 0 },
     { "loops"  	, 1,      0, 0 }
   };

   while (1){
     // Get the option into optc
     optc = getopt_long_only(
       argc,
       argv,
       "abc",
       long_options,
       &opti
     );
     
     // Escape the loop if there are no more options
     if (optc == -1)
       break;

       if (optarg){  // the if below segfaults if optarg is null. Why???
         if ( long_options[opti].flag == 0 ) {
	       
	     // display option
	     if ( strncmp(long_options[opti].name, "display") == 0) {
		     sdisp = optarg;
		     printf("Display: %s\n", optarg);
             }
	     // loops option
	     if ( strncmp(long_options[opti].name, "loops"  ) == 0) {
		     sloop = atoi(optarg);
		     printf("Loops: %s\n", optarg);
	     }
          }
       }
   }
   
   if (fhelp){
     printf("Usage: xclip [OPTIONS]\n");
     printf("Puts data from standard input into a X server selection for pasting.\n");
     printf("\n");
     printf("-l, --loops     number of selection requests to wait for before exiting\n");
     printf("-d, --display   X display to connect to (eg \"localhost:0\"\n");
     printf("-v, --verbose   verbose mode (default)\n");
     printf("-q, --quiet     terse mode (for use in scripts, etc)\n");
     printf("-h, --help      usage information\n");
     printf("\n");
     printf("Report bugs to <*****@*****.**>\n");
     exit(EXIT_SUCCESS);
   }
   if (fvers){
     printf("xclip version %1.2f\n", VERSION);
     printf("Copyright (C) 2001 Kim Saunders\n");
     printf("Distributed under the terms of the GNU GPL\n");
     exit(EXIT_SUCCESS);
   }
   while (optind < argc){
     printf("Arg: %s\n", argv[optind++]);
   }

   if ( display = XOpenDisplay(sdisp) )          // Display connection
   {
     printf("Connected to X server.\n");
   }
   else {
     printf("Could not connect to X server.\n");
     perror(NULL);
     exit(EXIT_FAILURE);
   }

   // Put chars into inc from stdin until we hit EOF
   while ( (inc = getchar()) != EOF){
     // If in is full (used elems = allocated elems)
     if (inelems == inalloc){
       // Allocate another 10 elems
       inalloc += 10;
       in = (char *)realloc((char *)in, inalloc * sizeof(char));
       
       if(in == NULL){
         printf("Error: Could not allocate memory\n");
         exit(EXIT_FAILURE);
       }
     }
     in[inelems] = inc;
     inelems++;
   }
	 
   // Create a window to own that will trap events
   w = XCreateSimpleWindow(
     display,
     DefaultRootWindow(display),
     0,
     0,
     1,
     1,
     0,
     0,
     0
   );
   
   // Take control of the selection 
   XSetSelectionOwner(display, XA_PRIMARY, w, CurrentTime);

   if (sloop == 1){
     printf("Waiting for one selection request.\n");
   }
   if (sloop < 1){
     printf("Waiting for selection requests, Control-C to quit\n");
   }
   if (sloop > 1){
     printf("Waiting for %i selection requests, Control-C to quit\n", sloop);
   }
   while (dloop < sloop || sloop < 1){
     if (sloop > 1){
       printf("  Waiting for selection request %i ", dloop + 1);
       printf("of %i.\n"       , sloop);
     }
     if (sloop == 1){
       printf("  Waiting for a selection request.\n");
     }
     if (sloop < 1){
       printf("  Waiting for selection request number %i\n", dloop + 1);
     }
    
     XNextEvent(display, &ev);        // Get the next event
     while (ev.type != SelectionRequest && ev.type != SelectionClear ){
       XNextEvent(display, &ev);
     }
	   
     if (ev.type == SelectionRequest){
       // printf("  Selection has been pasted.\n");
       req = &(ev.xselectionrequest);
	     
       XChangeProperty(
         display,
         req->requestor,
         req->property,
         XA_STRING,
         8,
         PropModeReplace,
         (unsigned char*) in,
         inelems
       );

       re.xselection.property  = req->property;
       re.xselection.type      = SelectionNotify;
       re.xselection.display   = req->display;
       re.xselection.requestor = req->requestor;
       re.xselection.selection = req->selection;
       re.xselection.target    = req->target;
       re.xselection.time      = req->time;
		     
       XSendEvent(display, req->requestor, 0, 0, &re);
       XFlush(display);
     }

     if (ev.type == SelectionClear){
       // Another app is telling us that it now owns the selection,
       // so we don't get selectionrequest events from the x server
       // any more, time to exit...
       printf("Error: Another application took ownership of the selection.\n");
       exit(EXIT_FAILURE);
     }
     dloop++;
   }
     
   // Disconnect from the X server
   XCloseDisplay(display);

   // Exit
   return(EXIT_SUCCESS);
}
Exemple #12
0
void *
winClipboardProc (void *pArg)
{
  Atom			atomClipboard, atomClipboardManager;
  Atom			atomLocalProperty, atomCompoundText;
  Atom			atomUTF8String, atomTargets;
  int			iReturn;
  HWND			hwnd = NULL;
  int			iConnectionNumber;
  int			fdMessageQueue;
  fd_set		fdsRead;
  int			iMaxDescriptor;
  Display		*pDisplay;
  Window		iWindow;
  Atom			atomDeleteWindow;
  Bool			fReturn;
  int			iRetries;
  Bool			fUnicodeSupport;
  char			szDisplay[512];
  ClipboardProcArgPtr	pProcArg = (ClipboardProcArgPtr) pArg;

  ErrorF ("winClipboardProc - Hello\n");

  /* Check that argument pointer is not invalid */
  if (pArg == NULL)
    {
      ErrorF ("winClipboardProc - pArg is NULL, bailing.\n");
      pthread_exit (NULL);
    }

  ErrorF ("winClipboardProc - Calling pthread_mutex_lock ()\n");

  /* Grab the server started mutex - pause until we get it */
  iReturn = pthread_mutex_lock (pProcArg->ppmServerStarted);
  if (iReturn != 0)
    {
      ErrorF ("winClipboardProc - pthread_mutex_lock () failed: %d\n",
	      iReturn);
      pthread_exit (NULL);
    }

  ErrorF ("winClipboardProc - pthread_mutex_lock () returned.\n");

  /* Do we have Unicode support? */
  fUnicodeSupport = winClipboardDetectUnicodeSupport ();

  /* Set the current locale?  What does this do? */
  if (fUnicodeSupport && !g_fCalledSetLocale)
    {
      ErrorF ("winClipboardProc - Calling setlocale ()\n");
      if (!setlocale (LC_ALL, ""))
	{
	  ErrorF ("winClipboardProc - setlocale () error\n");
	  pthread_exit (NULL);
	}
      ErrorF ("winClipboardProc - setlocale () returned\n");

      /* See if X supports the current locale */
      if (XSupportsLocale () == False)
	{
	  ErrorF ("winClipboardProc - Locale not supported by X\n");
	  pthread_exit (NULL);
	}
    }

  /* Flag that we have called setlocale */
  g_fCalledSetLocale = TRUE;

  /* Allow multiple threads to access Xlib */
  if (XInitThreads () == 0)
    {
      ErrorF ("winClipboardProc - XInitThreads failed.\n");
      pthread_exit (NULL);
    }

  ErrorF ("winClipboardProc - XInitThreads () returned.\n");

  /* Release the server started mutex */
  pthread_mutex_unlock (pProcArg->ppmServerStarted);

  ErrorF ("winClipboardProc - pthread_mutex_unlock () returned.\n");

  /* Set jump point for Error exits */
  iReturn = setjmp (g_jmpEntry);
  
  /* Check if we should continue operations */
  if (iReturn != WIN_JMP_ERROR_IO
      && iReturn != WIN_JMP_OKAY)
    {
      /* setjmp returned an unknown value, exit */
      ErrorF ("winClipboardProc - setjmp returned: %d exiting\n",
	      iReturn);
      pthread_exit (NULL);
    }
  else if (g_shutdown) 
    {
      /* Shutting down, the X server severed out connection! */
      ErrorF ("winClipboardProc - Detected shutdown in progress\n");
      pthread_exit (NULL);
    }
  else if (iReturn == WIN_JMP_ERROR_IO)
    {
      ErrorF ("winClipboardProc - setjmp returned and hwnd: %08x\n", hwnd);
    }

  /* Initialize retry count */
  iRetries = 0;

  /* Setup the display connection string x */
  snprintf (szDisplay,
	    512,
	    "127.0.0.1:%s.%d",
	    display,
	    (int) pProcArg->dwScreen);

  /* Print the display connection string */
  ErrorF ("winClipboardProc - DISPLAY=%s\n", szDisplay);

  /* Open the X display */
  do
    {
      pDisplay = XOpenDisplay (szDisplay);
      if (pDisplay == NULL)
	{
	  ErrorF ("winClipboardProc - Could not open display, "
		  "try: %d, sleeping: %d\n",
		  iRetries + 1, WIN_CONNECT_DELAY);
	  ++iRetries;
	  sleep (WIN_CONNECT_DELAY);
	  continue;
	}
      else
	break;
    }
  while (pDisplay == NULL && iRetries < WIN_CONNECT_RETRIES);

  /* Make sure that the display opened */
  if (pDisplay == NULL)
    {
      ErrorF ("winClipboardProc - Failed opening the display, giving up\n");
      pthread_exit (NULL);
    }

  ErrorF ("winClipboardProc - XOpenDisplay () returned and "
	  "successfully opened the display.\n");

  /* Create Windows messaging window */
  hwnd = winClipboardCreateMessagingWindow ();

  /* Get our connection number */
  iConnectionNumber = ConnectionNumber (pDisplay);

  /* Open a file descriptor for the windows message queue */
  fdMessageQueue = open (WIN_MSG_QUEUE_FNAME, O_RDONLY);
  if (fdMessageQueue == -1)
    {
      ErrorF ("winClipboardProc - Failed opening %s\n", WIN_MSG_QUEUE_FNAME);
      pthread_exit (NULL);
    }

  /* Find max of our file descriptors */
  iMaxDescriptor = max (fdMessageQueue, iConnectionNumber) + 1;

  /* Select event types to watch */
  if (XSelectInput (pDisplay,
		    DefaultRootWindow (pDisplay),
		    SubstructureNotifyMask |
		    StructureNotifyMask |
		    PropertyChangeMask) == BadWindow)
    ErrorF ("winClipboardProc - XSelectInput generated BadWindow "
	    "on RootWindow\n\n");

  /* Create a messaging window */
  iWindow = XCreateSimpleWindow (pDisplay,
				 DefaultRootWindow (pDisplay),
				 1, 1,
				 500, 500,
				 0,
				 BlackPixel (pDisplay, 0),
				 BlackPixel (pDisplay, 0));
  if (iWindow == 0)
    {
      ErrorF ("winClipboardProc - Could not create a window\n");
      pthread_exit (NULL);
    }

  /* This looks like our only hope for getting a message before shutdown */
  /* Register for WM_DELETE_WINDOW message from window manager */
  atomDeleteWindow = XInternAtom (pDisplay, "WM_DELETE_WINDOW", False);
  XSetWMProtocols (pDisplay, iWindow, &atomDeleteWindow, 1);

  /* Set error handler */
  XSetErrorHandler (winClipboardErrorHandler);
  XSetIOErrorHandler (winClipboardIOErrorHandler);

  /* Create an atom for CLIPBOARD_MANAGER */
  atomClipboardManager = XInternAtom (pDisplay, "CLIPBOARD_MANAGER", False);
  if (atomClipboardManager == None)
    {
      ErrorF ("winClipboardProc - Could not create CLIPBOARD_MANAGER atom\n");
      pthread_exit (NULL);
    }

  /* Assert ownership of CLIPBOARD_MANAGER */
  iReturn = XSetSelectionOwner (pDisplay, atomClipboardManager,
				iWindow, CurrentTime);
  if (iReturn == BadAtom || iReturn == BadWindow)
    {
      ErrorF ("winClipboardProc - Could not set CLIPBOARD_MANAGER owner\n");
      pthread_exit (NULL);
    }

  /* Create an atom for CLIPBOARD */
  atomClipboard = XInternAtom (pDisplay, "CLIPBOARD", False);
  if (atomClipboard == None)
    {
      ErrorF ("winClipboardProc - Could not create CLIPBOARD atom\n");
      pthread_exit (NULL);
    }

  /* Assert ownership of CLIPBOARD */
  iReturn = XSetSelectionOwner (pDisplay, atomClipboard,
				iWindow, CurrentTime);
  if (iReturn == BadAtom || iReturn == BadWindow)
    {
      ErrorF ("winClipboardProc - Could not set CLIPBOARD owner\n");
      pthread_exit (NULL);
    }

  /* Assert ownership of PRIMARY */
  iReturn = XSetSelectionOwner (pDisplay, XA_PRIMARY,
				iWindow, CurrentTime);
  if (iReturn == BadAtom || iReturn == BadWindow)
    {
      ErrorF ("winClipboardProc - Could not set PRIMARY owner\n");
      pthread_exit (NULL);
    }

  /* Local property to hold pasted data */
  atomLocalProperty = XInternAtom (pDisplay, "CYGX_CUT_BUFFER", False);
  if (atomLocalProperty == None)
    {
      ErrorF ("winClipboardProc - Could not create CYGX_CUT_BUFFER atom\n");
      pthread_exit (NULL);
    }

  /* Create an atom for UTF8_STRING */
  atomUTF8String = XInternAtom (pDisplay, "UTF8_STRING", False);
  if (atomUTF8String == None)
    {
      ErrorF ("winClipboardProc - Could not create UTF8_STRING atom\n");
      pthread_exit (NULL);
    }

  /* Create an atom for COMPOUND_TEXT */
  atomCompoundText = XInternAtom (pDisplay, "COMPOUND_TEXT", False);
  if (atomCompoundText == None)
    {
      ErrorF ("winClipboardProc - Could not create COMPOUND_TEXT atom\n");
      pthread_exit (NULL);
    }

  /* Create an atom for TARGETS */
  atomTargets = XInternAtom (pDisplay, "TARGETS", False);
  if (atomTargets == None)
    {
      ErrorF ("winClipboardProc - Could not create TARGETS atom\n");
      pthread_exit (NULL);
    }

  /* Pre-flush X events */
  /* 
   * NOTE: Apparently you'll freeze if you don't do this,
   *	   because there may be events in local data structures
   *	   already.
   */
  winClipboardFlushXEvents (hwnd,
			    atomClipboard,
			    atomLocalProperty,
			    atomUTF8String,
			    atomCompoundText,
			    atomTargets,
			    atomDeleteWindow,
			    iWindow,
			    pDisplay,
			    fUnicodeSupport);

  /* Pre-flush Windows messages */
  if (!winClipboardFlushWindowsMessageQueue (hwnd))
    return 0;

  /* Loop for X events */
  while (1)
    {
      /* Setup the file descriptor set */
      /*
       * NOTE: You have to do this before every call to select
       *       because select modifies the mask to indicate
       *       which descriptors are ready.
       */
      FD_ZERO (&fdsRead);
      FD_SET (fdMessageQueue, &fdsRead);
      FD_SET (iConnectionNumber, &fdsRead);

      /* Wait for a Windows event or an X event */
      iReturn = select (iMaxDescriptor,	/* Highest fds number */
			&fdsRead,	/* Read mask */
			NULL,		/* No write mask */
			NULL,		/* No exception mask */
			NULL);		/* No timeout */
      if (iReturn <= 0)
	{
	  ErrorF ("winClipboardProc - Call to select () failed: %d.  "
		  "Bailing.\n", iReturn);
	  break;
	}
      
      /* Branch on which descriptor became active */
      if (FD_ISSET (iConnectionNumber, &fdsRead))
	{
	  /* X event ready */
#if 0
	  ErrorF ("winClipboardProc - X event ready\n");
#endif

	  /* Process X events */
	  /* Exit when we see that server is shutting down */
	  fReturn = winClipboardFlushXEvents (hwnd,
					      atomClipboard,
					      atomLocalProperty,
					      atomUTF8String,
					      atomCompoundText,
					      atomTargets,
					      atomDeleteWindow,
					      iWindow,
					      pDisplay,
					      fUnicodeSupport);
	  if (!fReturn)
	    {
	      ErrorF ("winClipboardProc - Caught WM_DELETE_WINDOW - "
		      "shutting down\n");
	      break;
	    }
	}

      /* Check for Windows event ready */
      if (FD_ISSET (fdMessageQueue, &fdsRead))
	{
	  /* Windows event ready */
#if 0
	  ErrorF ("winClipboardProc - Windows event ready\n");
#endif
	  
	  /* Process Windows messages */
	  if (!winClipboardFlushWindowsMessageQueue (hwnd))
	    break;
	}
    }

  return 0;
}
Exemple #13
0
void Set_Clipboard_Content( std::string str )
{
#ifdef _WIN32
	if( OpenClipboard( NULL ) )
	{
		if( !EmptyClipboard() )
		{
			printf( "Failed to empty clipboard\n" );
			return;
		}

		unsigned int length = ( str.length() + 1 ) * sizeof(std::string::allocator_type);
		HANDLE h = GlobalAlloc( (GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT), length );

		if( !h )
		{
			printf( "Could not allocate clipboard memory\n" );
			return;
		}

		void *data = GlobalLock( h );

		if( !data )
		{
			GlobalFree( h );
			CloseClipboard();
			printf( "Could not lock clipboard memory\n" );
			return;
		}

		memcpy( data, str.c_str(), length );
		GlobalUnlock( h );

		HANDLE data_result = SetClipboardData( CF_TEXT, h );

		if( !data_result )
		{
			GlobalFree( h );
			CloseClipboard();
			printf( "Could not set clipboard data\n" );
		}

		CloseClipboard();
	}
#elif __APPLE__
	// not implemented
#elif __unix__
	SDL_SysWMinfo sdlinfo;
	SDL_VERSION( &sdlinfo.version );
	if( SDL_GetWMInfo( &sdlinfo ) )
	{
		sdlinfo.info.x11.lock_func();
		Display *display = sdlinfo.info.x11.display;
		Window window = sdlinfo.info.x11.window;

		XChangeProperty( display, DefaultRootWindow(display), XA_CUT_BUFFER0, XA_STRING, 8, PropModeReplace, static_cast<const unsigned char *>(static_cast<const void *>(str.c_str())), str.length() );

		if( XGetSelectionOwner( display, XA_PRIMARY ) != window )
		{
			XSetSelectionOwner( display, XA_PRIMARY, window, CurrentTime );
		}

		sdlinfo.info.x11.unlock_func();
	}
#endif
}
LRESULT CALLBACK
winClipboardWindowProc (HWND hwnd, UINT message, 
			WPARAM wParam, LPARAM lParam)
{
  static HWND		s_hwndNextViewer;
  static Bool		s_fCBCInitialized;

  /* Branch on message type */
  switch (message)
    {
    case WM_DESTROY:
      {
	winDebug ("winClipboardWindowProc - WM_DESTROY\n");

	/* Remove ourselves from the clipboard chain */
	ChangeClipboardChain (hwnd, s_hwndNextViewer);
	
	s_hwndNextViewer = NULL;

	PostQuitMessage (0);
      }
      return 0;


    case WM_CREATE:
      {
	HWND first, next;
	DWORD error_code = 0;
	winDebug ("winClipboardWindowProc - WM_CREATE\n");
	
	first = GetClipboardViewer();			/* Get handle to first viewer in chain. */
	if (first == hwnd) return 0;			/* Make sure it's not us! */
	/* Add ourselves to the clipboard viewer chain */
	next = SetClipboardViewer (hwnd);
	error_code = GetLastError();
	if (SUCCEEDED(error_code) && (next == first))	/* SetClipboardViewer must have succeeded, and the handle */
		s_hwndNextViewer = next;		/* it returned must have been the first window in the chain */
	else
		s_fCBCInitialized = FALSE;
      }
      return 0;


    case WM_CHANGECBCHAIN:
      {
	winDebug ("winClipboardWindowProc - WM_CHANGECBCHAIN: wParam(%x) "
		  "lParam(%x) s_hwndNextViewer(%x)\n", 
		  wParam, lParam, s_hwndNextViewer);

	if ((HWND) wParam == s_hwndNextViewer)
	  {
	    s_hwndNextViewer = (HWND) lParam;
	    if (s_hwndNextViewer == hwnd)
	      {
		s_hwndNextViewer = NULL;
		winErrorFVerb (1, "winClipboardWindowProc - WM_CHANGECBCHAIN: "
			       "attempted to set next window to ourselves.");
	      }
	  }
	else if (s_hwndNextViewer)
	  SendMessage (s_hwndNextViewer, message,
		       wParam, lParam);

      }
      winDebug ("winClipboardWindowProc - WM_CHANGECBCHAIN: Exit\n");
      return 0;

    case WM_WM_REINIT:
      {
        /* Ensure that we're in the clipboard chain.  Some apps,
         * WinXP's remote desktop for one, don't play nice with the
         * chain.  This message is called whenever we receive a
         * WM_ACTIVATEAPP message to ensure that we continue to
         * receive clipboard messages.
	 *
	 * It might be possible to detect if we're still in the chain
	 * by calling SendMessage (GetClipboardViewer(),
	 * WM_DRAWCLIPBOARD, 0, 0); and then seeing if we get the
	 * WM_DRAWCLIPBOARD message.  That, however, might be more
	 * expensive than just putting ourselves back into the chain.
	 */

	HWND first, next;
	DWORD error_code = 0;
	winDebug ("winClipboardWindowProc - WM_WM_REINIT: Enter\n");

	first = GetClipboardViewer();			/* Get handle to first viewer in chain. */
	if (first == hwnd) return 0;			/* Make sure it's not us! */
	winDebug ("  WM_WM_REINIT: Replacing us(%x) with %x at head "
		  "of chain\n", hwnd, s_hwndNextViewer);
	s_fCBCInitialized = FALSE;
	ChangeClipboardChain (hwnd, s_hwndNextViewer);
	s_hwndNextViewer = NULL;
	s_fCBCInitialized = FALSE;
	winDebug ("  WM_WM_REINIT: Putting us back at head of chain.\n");
	first = GetClipboardViewer();			/* Get handle to first viewer in chain. */
	if (first == hwnd) return 0;			/* Make sure it's not us! */
	next = SetClipboardViewer (hwnd);
	error_code = GetLastError();
	if (SUCCEEDED(error_code) && (next == first))	/* SetClipboardViewer must have succeeded, and the handle */
		s_hwndNextViewer = next;		/* it returned must have been the first window in the chain */
	else
		s_fCBCInitialized = FALSE;
      }
      winDebug ("winClipboardWindowProc - WM_WM_REINIT: Exit\n");
      return 0;


    case WM_DRAWCLIPBOARD:
      {
	static Atom atomClipboard;
	static int generation;
	static Bool s_fProcessingDrawClipboard = FALSE;
	Display	*pDisplay = g_pClipboardDisplay;
	Window	iWindow = g_iClipboardWindow;
	int	iReturn;

	winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD: Enter\n");

	if (generation != serverGeneration)
          {
            generation = serverGeneration;
            atomClipboard = XInternAtom (pDisplay, "CLIPBOARD", False);
          }

	/*
	 * We've occasionally seen a loop in the clipboard chain.
	 * Try and fix it on the first hint of recursion.
	 */
	if (! s_fProcessingDrawClipboard) 
	  {
	    s_fProcessingDrawClipboard = TRUE;
	  }
	else
	  {
	    /* Attempt to break the nesting by getting out of the chain, twice?, and then fix and bail */
	    s_fCBCInitialized = FALSE;
	    ChangeClipboardChain (hwnd, s_hwndNextViewer);
	    winFixClipboardChain();
	    winErrorFVerb (1, "winClipboardWindowProc - WM_DRAWCLIPBOARD - "
			   "Nested calls detected.  Re-initing.\n");
	    winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD: Exit\n");
	    s_fProcessingDrawClipboard = FALSE;
	    return 0;
	  }

	/* Bail on first message */
	if (!s_fCBCInitialized)
	  {
	    s_fCBCInitialized = TRUE;
	    s_fProcessingDrawClipboard = FALSE;
	    winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD: Exit\n");
	    return 0;
	  }

	/*
	 * NOTE: We cannot bail out when NULL == GetClipboardOwner ()
	 * because some applications deal with the clipboard in a manner
	 * that causes the clipboard owner to be NULL when they are in
	 * fact taking ownership.  One example of this is the Win32
	 * native compile of emacs.
	 */
	
	/* Bail when we still own the clipboard */
	if (hwnd == GetClipboardOwner ())
	  {

	    winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
		    "We own the clipboard, returning.\n");
	    winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD: Exit\n");
	    s_fProcessingDrawClipboard = FALSE;
	    if (s_hwndNextViewer)
		SendMessage (s_hwndNextViewer, message, wParam, lParam);
	    return 0;
	  }

	/*
	 * Do not take ownership of the X11 selections when something
	 * other than CF_TEXT or CF_UNICODETEXT has been copied
	 * into the Win32 clipboard.
	 */
	if (!IsClipboardFormatAvailable (CF_TEXT)
	    && !IsClipboardFormatAvailable (CF_UNICODETEXT))
	  {

	    winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
		    "Clipboard does not contain CF_TEXT nor "
		    "CF_UNICODETEXT.\n");

	    /*
	     * We need to make sure that the X Server has processed
	     * previous XSetSelectionOwner messages.
	     */
	    XSync (pDisplay, FALSE);

            winDebug("winClipboardWindowProc - XSync done.\n");
	    
	    /* Release PRIMARY selection if owned */
	    iReturn = XGetSelectionOwner (pDisplay, XA_PRIMARY);
	    if (iReturn == g_iClipboardWindow)
	      {
		winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
			"PRIMARY selection is owned by us.\n");
		XSetSelectionOwner (pDisplay,
				    XA_PRIMARY,
				    None,
				    CurrentTime);
	      }
	    else if (BadWindow == iReturn || BadAtom == iReturn)
	      winErrorFVerb (1, "winClipboardWindowProc - WM_DRAWCLIPBOARD - "
		      "XGetSelection failed for PRIMARY: %d\n", iReturn);

	    /* Release CLIPBOARD selection if owned */
	    iReturn = XGetSelectionOwner (pDisplay,
					  atomClipboard);
	    if (iReturn == g_iClipboardWindow)
	      {
		winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
			"CLIPBOARD selection is owned by us.\n");
		XSetSelectionOwner (pDisplay,
				    atomClipboard,
				    None,
				    CurrentTime);
	      }
	    else if (BadWindow == iReturn || BadAtom == iReturn)
	      winErrorFVerb (1, "winClipboardWindowProc - WM_DRAWCLIPBOARD - "
		      "XGetSelection failed for CLIPBOARD: %d\n", iReturn);

	    winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD: Exit\n");
	    s_fProcessingDrawClipboard = FALSE;
	    if (s_hwndNextViewer)
		SendMessage (s_hwndNextViewer, message, wParam, lParam);
	    return 0;
	  }

	/* Reassert ownership of PRIMARY */	  
	iReturn = XSetSelectionOwner (pDisplay,
				      XA_PRIMARY,
				      iWindow,
				      CurrentTime);
	if (iReturn == BadAtom || iReturn == BadWindow ||
	    XGetSelectionOwner (pDisplay, XA_PRIMARY) != iWindow)
	  {
	    winErrorFVerb (1, "winClipboardWindowProc - WM_DRAWCLIPBOARD - "
		    "Could not reassert ownership of PRIMARY\n");
	  }
	else
	  {
	    winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
		    "Reasserted ownership of PRIMARY\n");
	  }
	
	/* Reassert ownership of the CLIPBOARD */	  
	iReturn = XSetSelectionOwner (pDisplay,
				      atomClipboard,
				      iWindow,
				      CurrentTime);

	if (iReturn == BadAtom || iReturn == BadWindow ||
	    XGetSelectionOwner (pDisplay, atomClipboard) != iWindow)
	  {
	    winErrorFVerb (1, "winClipboardWindowProc - WM_DRAWCLIPBOARD - "
		    "Could not reassert ownership of CLIPBOARD\n");
	  }
	else
	  {
	    winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD - "
		    "Reasserted ownership of CLIPBOARD\n");
	  }
	
	/* Flush the pending SetSelectionOwner event now */
	XFlush (pDisplay);

	s_fProcessingDrawClipboard = FALSE;
      }
      winDebug ("winClipboardWindowProc - WM_DRAWCLIPBOARD: Exit\n");
      /* Pass the message on the next window in the clipboard viewer chain */
      if (s_hwndNextViewer)
	SendMessage (s_hwndNextViewer, message, wParam, lParam);
      return 0;


    case WM_DESTROYCLIPBOARD:
      /*
       * NOTE: Intentionally do nothing.
       * Changes in the Win32 clipboard are handled by WM_DRAWCLIPBOARD
       * above.  We only process this message to conform to the specs
       * for delayed clipboard rendering in Win32.  You might think
       * that we need to release ownership of the X11 selections, but
       * we do not, because a WM_DRAWCLIPBOARD message will closely
       * follow this message and reassert ownership of the X11
       * selections, handling the issue for us.
       */
      winDebug ("winClipboardWindowProc - WM_DESTROYCLIPBOARD - Ignored.\n");
      return 0;

    case WM_RENDERFORMAT:
    case WM_RENDERALLFORMATS:
      {
	int	iReturn;
	Display *pDisplay = g_pClipboardDisplay;
	Window	iWindow = g_iClipboardWindow;
	Bool	fConvertToUnicode;

	winDebug ("winClipboardWindowProc - WM_RENDER*FORMAT - Hello.\n");

	/* Flag whether to convert to Unicode or not */
	if (message == WM_RENDERALLFORMATS)
	  fConvertToUnicode = FALSE;
	else
	  fConvertToUnicode = g_fUnicodeSupport && (CF_UNICODETEXT == wParam);

	/* Request the selection contents */
	iReturn = XConvertSelection (pDisplay,
				     g_atomLastOwnedSelection,
				     XInternAtom (pDisplay,
						  "COMPOUND_TEXT", False),
				     XInternAtom (pDisplay,
						  "CYGX_CUT_BUFFER", False),
				     iWindow,
				     CurrentTime);
	if (iReturn == BadAtom || iReturn == BadWindow)
	  {
	    winErrorFVerb (1, "winClipboardWindowProc - WM_RENDER*FORMAT - "
		    "XConvertSelection () failed\n");
	    break;
	  }

	/* Special handling for WM_RENDERALLFORMATS */
	if (message == WM_RENDERALLFORMATS)
	  {
	    /* We must open and empty the clipboard */

	    /* Close clipboard if we have it open already */
	    if (GetOpenClipboardWindow () == hwnd)
	      {
		CloseClipboard ();
	      }	    

	    if (!OpenClipboard (hwnd))
	      {
		winErrorFVerb (1, "winClipboardWindowProc - WM_RENDER*FORMATS - "
			"OpenClipboard () failed: %08x\n",
			GetLastError ());
		break;
	      }
	    
	    if (!EmptyClipboard ())
	      {
		winErrorFVerb (1, "winClipboardWindowProc - WM_RENDER*FORMATS - "
			"EmptyClipboard () failed: %08x\n",
		      GetLastError ());
		break;
	      }
	  }

	/* Process the SelectionNotify event */
	iReturn = winProcessXEventsTimeout (hwnd,
					    iWindow,
					    pDisplay,
					    fConvertToUnicode,
					    WIN_POLL_TIMEOUT);
	if (WIN_XEVENTS_CONVERT == iReturn)
	  {
	    /*
	     * The selection was offered for conversion first, so we have
	     * to process a second SelectionNotify event to get the actual
	     * data in the selection.
	     */
	    iReturn = winProcessXEventsTimeout (hwnd,
						iWindow,
						pDisplay,
						fConvertToUnicode,
						WIN_POLL_TIMEOUT);
	  }
	
	/*
	 * The last of the up-to two calls to winProcessXEventsTimeout
	 * from above had better have seen a notify event, or else we
	 * are dealing with a buggy or old X11 app.  In these cases we
	 * have to paste some fake data to the Win32 clipboard to
	 * satisfy the requirement that we write something to it.
	 */
	if (WIN_XEVENTS_NOTIFY != iReturn)
	  {
	    /* Paste no data, to satisfy required call to SetClipboardData */
	    if (g_fUnicodeSupport)
	      SetClipboardData (CF_UNICODETEXT, NULL);
	    SetClipboardData (CF_TEXT, NULL);

            ErrorF("winClipboardWindowProc - timed out waiting for WIN_XEVENTS_NOTIFY\n");
	  }

	/* Special handling for WM_RENDERALLFORMATS */
	if (message == WM_RENDERALLFORMATS)
	  {
	    /* We must close the clipboard */
	    
	    if (!CloseClipboard ())
	      {
	      winErrorFVerb (1, "winClipboardWindowProc - WM_RENDERALLFORMATS - "
		      "CloseClipboard () failed: %08x\n",
		      GetLastError ());
	      break;
	      }
	  }

	winDebug ("winClipboardWindowProc - WM_RENDER*FORMAT - Returning.\n");
	return 0;
      }
    }

  /* Let Windows perform default processing for unhandled messages */
  return DefWindowProc (hwnd, message, wParam, lParam);
}
Exemple #15
0
static int
doIn(Window win, const char *progname)
{
    unsigned char *sel_buf;	/* buffer for selection data */
    unsigned long sel_len = 0;	/* length of sel_buf */
    unsigned long sel_all = 0;	/* allocated size of sel_buf */
    XEvent evt;			/* X Event Structures */
    int dloop = 0;		/* done loops counter */

    /* in mode */
    sel_all = 16;		/* Reasonable ballpark figure */
    sel_buf = xcmalloc(sel_all * sizeof(char));

    /* Put chars into inc from stdin or files until we hit EOF */
    do {
	if (fil_number == 0) {
	    /* read from stdin if no files specified */
	    fil_handle = stdin;
	}
	else {
	    if ((fil_handle = fopen(fil_names[fil_current], "r")) == NULL) {
		errperror(3, progname, ": ", fil_names[fil_current]
		    );
		return EXIT_FAILURE;
	    }
	    else {
		/* file opened successfully. Print
		 * message (verbose mode only).
		 */
		if (fverb == OVERBOSE)
		    fprintf(stderr, "Reading %s...\n", fil_names[fil_current]
			);
	    }
	}

	fil_current++;
	while (!feof(fil_handle)) {
	    /* If sel_buf is full (used elems =
	     * allocated elems)
	     */
	    if (sel_len == sel_all) {
		/* double the number of
		 * allocated elements
		 */
		sel_all *= 2;
		sel_buf = (unsigned char *) xcrealloc(sel_buf, sel_all * sizeof(char)
		    );
	    }
	    sel_len += fread(sel_buf + sel_len, sizeof(char), sel_all - sel_len, fil_handle);
	}
    } while (fil_current < fil_number);

    /* if there are no files being read from (i.e., input
     * is from stdin not files, and we are in filter mode,
     * spit all the input back out to stdout
     */
    if ((fil_number == 0) && ffilt) {
	fwrite(sel_buf, sizeof(char), sel_len, stdout);
	fclose(stdout);
    }

    /* Handle cut buffer if needed */
    if (sseln == XA_STRING) {
	XStoreBuffer(dpy, (char *) sel_buf, (int) sel_len, 0);
	return EXIT_SUCCESS;
    }

    /* take control of the selection so that we receive
     * SelectionRequest events from other windows
     */
    /* FIXME: Should not use CurrentTime, according to ICCCM section 2.1 */
    XSetSelectionOwner(dpy, sseln, win, CurrentTime);

    /* fork into the background, exit parent process if we
     * are in silent mode
     */
    if (fverb == OSILENT) {
	pid_t pid;

	pid = fork();
	/* exit the parent process; */
	if (pid)
	    exit(EXIT_SUCCESS);
    }

    /* print a message saying what we're waiting for */
    if (fverb > OSILENT) {
	if (sloop == 1)
	    fprintf(stderr, "Waiting for one selection request.\n");

	if (sloop < 1)
	    fprintf(stderr, "Waiting for selection requests, Control-C to quit\n");

	if (sloop > 1)
	    fprintf(stderr, "Waiting for %i selection requests, Control-C to quit\n", sloop);
    }

    /* Avoid making the current directory in use, in case it will need to be umounted */
    chdir("/");

    /* loop and wait for the expected number of
     * SelectionRequest events
     */
    while (dloop < sloop || sloop < 1) {
	/* print messages about what we're waiting for
	 * if not in silent mode
	 */
	if (fverb > OSILENT) {
	    if (sloop > 1)
		fprintf(stderr, "  Waiting for selection request %i of %i.\n", dloop + 1, sloop);

	    if (sloop == 1)
		fprintf(stderr, "  Waiting for a selection request.\n");

	    if (sloop < 1)
		fprintf(stderr, "  Waiting for selection request number %i\n", dloop + 1);
	}

	/* wait for a SelectionRequest event */
	while (1) {
	    static unsigned int clear = 0;
	    static unsigned int context = XCLIB_XCIN_NONE;
	    static unsigned long sel_pos = 0;
	    static Window cwin;
	    static Atom pty;
	    int finished;

	    XNextEvent(dpy, &evt);

	    finished = xcin(dpy, &cwin, evt, &pty, target, sel_buf, sel_len, &sel_pos, &context);

	    if (evt.type == SelectionClear)
		clear = 1;

	    if ((context == XCLIB_XCIN_NONE) && clear)
		return EXIT_SUCCESS;

	    if (finished)
		break;
	}

	dloop++;		/* increment loop counter */
    }

    return EXIT_SUCCESS;
}
Exemple #16
0
static int xf_cliprdr_server_format_list(CliprdrClientContext* context, CLIPRDR_FORMAT_LIST* formatList)
{
	int i, j;
	CLIPRDR_FORMAT* format;
	xfClipboard* clipboard = (xfClipboard*) context->custom;
	xfContext* xfc = clipboard->xfc;

	if (clipboard->data)
	{
		free(clipboard->data);
		clipboard->data = NULL;
	}

	if (clipboard->serverFormats)
	{
		for (i = 0; i < clipboard->numServerFormats; i++)
			free(clipboard->serverFormats[i].formatName);

		free(clipboard->serverFormats);
		clipboard->serverFormats = NULL;

		clipboard->numServerFormats = 0;
	}

	clipboard->numServerFormats = formatList->numFormats;
	clipboard->serverFormats = (CLIPRDR_FORMAT*) calloc(clipboard->numServerFormats, sizeof(CLIPRDR_FORMAT));

	if (!clipboard->serverFormats)
		return -1;

	for (i = 0; i < formatList->numFormats; i++)
	{
		format = &formatList->formats[i];
		clipboard->serverFormats[i].formatId = format->formatId;
		if (format->formatName)
		{
			clipboard->serverFormats[i].formatName = _strdup(format->formatName);
			if (!clipboard->serverFormats[i].formatName)
			{
				for (--i; i >= 0; --i)
					free(clipboard->serverFormats[i].formatName);

				clipboard->numServerFormats = 0;
				free(clipboard->serverFormats);
				clipboard->serverFormats = NULL;
				return -1;
			}
		}
	}

	clipboard->numTargets = 2;

	for (i = 0; i < formatList->numFormats; i++)
	{
		format = &formatList->formats[i];

		for (j = 0; j < clipboard->numClientFormats; j++)
		{
			if (format->formatId == clipboard->clientFormats[j].formatId)
			{
				xf_cliprdr_append_target(clipboard, clipboard->clientFormats[j].atom);
			}
		}
	}

	xf_cliprdr_send_client_format_list_response(clipboard, TRUE);

	XSetSelectionOwner(xfc->display, clipboard->clipboard_atom, xfc->drawable, CurrentTime);

	XFlush(xfc->display);

	return 1;
}
Exemple #17
0
int main (int argc, char *argv[])
{
	/* Declare variables */
	unsigned char *sel_buf;		/* buffer for selection data */
	unsigned long sel_len = 0;	/* length of sel_buf */
	unsigned long sel_all = 0;	/* allocated size of sel_buf */
	Window win;			/* Window */
	XEvent evt;			/* X Event Structures */
	int dloop = 0;			/* done loops counter */

	pid_t pid;			/* child pid if forking */

	/* set up option table. I can't figure out a better way than this to
	 * do it while sticking to pure ANSI C. The option and specifier
	 * members have a type of volatile char *, so they need to be allocated
	 * by strdup or malloc, you can't set them to a string constant at
	 * declare time, this is note pure ANSI C apparently, although it does
	 * work with gcc
	 */
	
	/* loop option entry */
	opt_tab[0].option 	=	xcstrdup("-loops");
	opt_tab[0].specifier	=	xcstrdup(".loops");
	opt_tab[0].argKind	=	XrmoptionSepArg;
	opt_tab[0].value	=	(XPointer) NULL;

	/* display option entry */
	opt_tab[1].option	=	xcstrdup("-display");
	opt_tab[1].specifier	=	xcstrdup(".display");
	opt_tab[1].argKind	=	XrmoptionSepArg;
	opt_tab[1].value	=	(XPointer) NULL;
				
	/* selection option entry */
	opt_tab[2].option 	=	xcstrdup("-selection");
	opt_tab[2].specifier	=	xcstrdup(".selection");
	opt_tab[2].argKind	=	XrmoptionSepArg;
	opt_tab[2].value	=	(XPointer) NULL;
		
	/* filter option entry */
	opt_tab[3].option	=	xcstrdup("-filter");
	opt_tab[3].specifier	=	xcstrdup(".filter");
	opt_tab[3].argKind	=	XrmoptionNoArg;	
	opt_tab[3].value	=	(XPointer) xcstrdup(ST);
		
	/* in option entry */
	opt_tab[4].option	=	xcstrdup("-in");
	opt_tab[4].specifier	=	xcstrdup(".direction");
	opt_tab[4].argKind	=	XrmoptionNoArg;
	opt_tab[4].value	=	(XPointer) xcstrdup("I");
		
	/* out option entry */
	opt_tab[5].option	=	xcstrdup("-out");
	opt_tab[5].specifier	=	xcstrdup(".direction");
	opt_tab[5].argKind	=	XrmoptionNoArg;
	opt_tab[5].value	=	(XPointer) xcstrdup("O");
		
	/* version option entry */
	opt_tab[6].option	=	xcstrdup("-version");
	opt_tab[6].specifier	=	xcstrdup(".print");
	opt_tab[6].argKind	=	XrmoptionNoArg;
	opt_tab[6].value	=	(XPointer) xcstrdup("V");
		
	/* help option entry */
	opt_tab[7].option	=	xcstrdup("-help");
	opt_tab[7].specifier	=	xcstrdup(".print");
	opt_tab[7].argKind	=	XrmoptionNoArg;
	opt_tab[7].value	=	(XPointer) xcstrdup("H");
		
	/* silent option entry */
	opt_tab[8].option	=	xcstrdup("-silent");
	opt_tab[8].specifier	=	xcstrdup(".olevel");
	opt_tab[8].argKind	=	XrmoptionNoArg;
	opt_tab[8].value	=	(XPointer) xcstrdup("S");
		
	/* quiet option entry */
	opt_tab[9].option	=	xcstrdup("-quiet");
	opt_tab[9].specifier	=	xcstrdup(".olevel");
	opt_tab[9].argKind	=	XrmoptionNoArg;
	opt_tab[9].value	=	(XPointer) xcstrdup("Q");
		
	/* verbose option entry */
	opt_tab[10].option	=	xcstrdup("-verbose");
	opt_tab[10].specifier	=	xcstrdup(".olevel");
	opt_tab[10].argKind	=	XrmoptionNoArg;
	opt_tab[10].value	=	(XPointer) xcstrdup("V");

	/* parse command line options */
	doOptMain(argc, argv);
   
	/* Connect to the X server. */
	if ( (dpy = XOpenDisplay(sdisp)) )
	{
		/* successful */
		if (fverb == OVERBOSE)
			fprintf(stderr, "Connected to X server.\n");
	} else
	{
		/* couldn't connect to X server. Print error and exit */
		errxdisplay(sdisp);
	}

	/* parse selection command line option */
	doOptSel();
  
	/* Create a window to trap events */
	win = XCreateSimpleWindow(
		dpy,
		DefaultRootWindow(dpy),
		0,
		0,
		1,
		1,
		0,
		0,
		0
	);

	/* get events about property changes */
	XSelectInput(dpy, win, PropertyChangeMask);
  
	if (fdiri)
	{
		/* in mode */
		sel_all = 16;		/* Reasonable ballpark figure */
		sel_buf = xcmalloc(sel_all * sizeof(char));

		/* Put chars into inc from stdin or files until we hit EOF */
		do {
			if (fil_number == 0)
			{
				/* read from stdin if no files specified */
				fil_handle = stdin;
			} else
			{
				if (
					(fil_handle = fopen(
						fil_names[fil_current],
						"r"
					)) == NULL
				)
				{
					errperror(
						3,
						argv[0],
						": ",
						fil_names[fil_current]
					);
					exit(EXIT_FAILURE);
				} else
				{
					/* file opened successfully. Print
					 * message (verbose mode only).
					 */
					if (fverb == OVERBOSE)
						fprintf(
							stderr,
							"Reading %s...\n",
							fil_names[fil_current]
						);
				}
			}

			fil_current++;
			while (!feof(fil_handle))
			{
				/* If sel_buf is full (used elems =
				 * allocated elems)
				 */
				if (sel_len == sel_all)
				{
					/* double the number of
					 * allocated elements
					 */
					sel_all *= 2;
					sel_buf = (unsigned char *)xcrealloc(
						sel_buf,
						sel_all * sizeof(char)
					);
				}
				sel_len += fread(
					sel_buf + sel_len,
					sizeof(char),
					sel_all - sel_len,
					fil_handle
				);
			}
		} while (fil_current < fil_number);

		/* if there are no files being read from (i.e., input
		 * is from stdin not files, and we are in filter mode,
		 * spit all the input back out to stdout
		 */
		if ((fil_number == 0) && ffilt)
			fwrite(sel_buf, sizeof(char), sel_len, stdout); 
    
		/* take control of the selection so that we receive
		 * SelectionRequest events from other windows
		 */
		XSetSelectionOwner(dpy, sseln, win, CurrentTime);

		/* fork into the background, exit parent process if we
		 * are in silent mode
		 */
		if (fverb == OSILENT)
		{
			pid = fork();
			/* exit the parent process; */
			if (pid)
				exit(EXIT_SUCCESS);
		}

		/* print a message saying what we're waiting for */
		if (fverb > OSILENT)
		{
			if (sloop == 1)
				fprintf(
					stderr,
					"Waiting for one selection request.\n"
				);
				
			if (sloop  < 1)
				fprintf(
					stderr,
					"Waiting for selection requests, Control-C to quit\n"
				);
				
			if (sloop  > 1)
				fprintf(
					stderr,
					"Waiting for %i selection requests, Control-C to quit\n",
					sloop
				);
		}
  
		/* loop and wait for the expected number of
		 * SelectionRequest events
		 */
		while (dloop < sloop || sloop < 1)
		{
			/* print messages about what we're waiting for
			 * if not in silent mode
			 */
			if (fverb > OSILENT)
			{
				if (sloop  > 1)
					fprintf(
						stderr,
						"  Waiting for selection request %i of %i.\n",
						dloop + 1,
						sloop
					);
					
				if (sloop == 1)
					fprintf(
						stderr,
						"  Waiting for a selection request.\n"
					);
					
				if (sloop  < 1)
					fprintf(
						stderr,
						"  Waiting for selection request number %i\n",
						dloop + 1
					);
			}
     
			/* wait for a SelectionRequest event */
			while (1)
			{
				static unsigned int clear = 0;
				static unsigned int context = XCLIB_XCIN_NONE;
				static unsigned long sel_pos = 0;
				static Window cwin;
				static Atom pty;
				int finished;

				XNextEvent(dpy, &evt);

				finished = xcin(
					dpy,
					&cwin,
					evt,
					&pty,
					sel_buf,
					sel_len,
					&sel_pos,
					&context
				);

				if (evt.type == SelectionClear)
					clear = 1;

				if ( (context == XCLIB_XCIN_NONE) && clear)
					exit(EXIT_SUCCESS);

				if (finished)
					break;
			}

			dloop++;	/* increment loop counter */
		}
	} else
	{
		unsigned int context = XCLIB_XCOUT_NONE;

		while (1)
		{
			/* only get an event if xcout() is doing something */
			if (context != XCLIB_XCOUT_NONE)
				XNextEvent(dpy, &evt);

			/* fetch the selection, or part of it */
			xcout(
				dpy,
				win,
				evt,
				sseln,
				&sel_buf,
				&sel_len,
				&context
			);

			/* only continue if xcout() is doing something */
			if (context == XCLIB_XCOUT_NONE)
				break;
		}
		
		if (sel_len)
		{
			/* only print the buffer out, and free it, if it's not
			 * empty
			 */
			fwrite(sel_buf, sizeof(char), sel_len, stdout);
			free(sel_buf);
		}
	}

	/* Disconnect from the X server */
	XCloseDisplay(dpy);

	/* exit */
	return(EXIT_SUCCESS);
}
Exemple #18
0
void Systemtray::initSystray( void )
{
  bool existing = false;
  //bool content = false;
  Display *display = tqt_xdisplay();
  no_of_systray_windows = 0;

  twin_module = new KWinModule();
  systemTrayWindows = twin_module->systemTrayWindows();
  TQValueList<WId>::ConstIterator end(systemTrayWindows.end());
  for (TQValueList<WId>::ConstIterator it = systemTrayWindows.begin(); it!=end; ++it)
  {
    no_of_systray_windows++;
    QXEmbed *emb;

    emb = new QXEmbed(this);
    emb->setBackgroundMode(FixedPixmap);

    emb->setAutoDelete(false);

    connect(emb, TQT_SIGNAL(embeddedWindowDestroyed()), TQT_SLOT(updateTrayWindows()));

    m_Wins.append(emb);

    emb->embed(*it);
    emb->resize(24, 24);
    emb->show();
    existing = true;
  }

  updateTrayWindows();

  connect(twin_module, TQT_SIGNAL(systemTrayWindowAdded(WId)), TQT_SLOT(systemTrayWindowAdded(WId)));
  connect(twin_module, TQT_SIGNAL(systemTrayWindowRemoved(WId)), TQT_SLOT(systemTrayWindowRemoved(WId)));

  TQCString screenstr;
  screenstr.setNum(tqt_xscreen());
  TQCString trayatom = "_NET_SYSTEM_TRAY_S" + screenstr;

  net_system_tray_selection = XInternAtom( display, trayatom, false );
  net_system_tray_opcode = XInternAtom( display, "_NET_SYSTEM_TRAY_OPCODE", false );

  // Acquire system tray
  XSetSelectionOwner( display,
    net_system_tray_selection,
    winId(),
    CurrentTime );

  WId root = tqt_xrootwin();

  if (XGetSelectionOwner(display, net_system_tray_selection) == winId())
  {
    XClientMessageEvent xev;

    xev.type = ClientMessage;
    xev.window = root;

    xev.message_type = XInternAtom(display, "MANAGER", false);
    xev.format = 32;

    xev.data.l[0] = CurrentTime;
    xev.data.l[1] = net_system_tray_selection;
    xev.data.l[2] = winId();
    xev.data.l[3] = 0;       /* Manager specific data */
    xev.data.l[4] = 0;       /* Manager specific data */

    XSendEvent( display, root, false, StructureNotifyMask, (XEvent *)&xev );
  }
}
Exemple #19
0
static int SetXi18nSelectionOwner(Xi18n i18n_core)
{
    Display *dpy = i18n_core->address.dpy;
    Window ims_win = i18n_core->address.im_window;
    Window root = RootWindow (dpy, DefaultScreen (dpy));
    Atom realtype;
    int realformat;
    unsigned long bytesafter;
    long *data=NULL;
    unsigned long length;
    Atom atom;
    int i;
    int found;
    int forse = False;
    char buf[256];

    (void)sprintf(buf, "@server=%s", i18n_core->address.im_name);
    if ((atom = XInternAtom(dpy, buf, False)) == 0)
        return False;
    i18n_core->address.selection = atom;

    if (XIM_Servers == None)
        XIM_Servers = XInternAtom (dpy, XIM_SERVERS, False);
    /*endif*/
    XGetWindowProperty (dpy,
                        root,
                        XIM_Servers,
                        0L,
                        1000000L,
                        False,
                        XA_ATOM,
                        &realtype,
                        &realformat,
                        &length,
                        &bytesafter,
                        (unsigned char **) (&data));
    if (realtype != None && (realtype != XA_ATOM || realformat != 32)) {
        if (data != NULL)
            XFree ((char *) data);
        return False;
    }

    found = False;
    for (i = 0; i < length; i++) {
        if (data[i] == atom) {
            Window owner;
            found = True;
            if ((owner = XGetSelectionOwner (dpy, atom)) != ims_win) {
                if (owner == None  ||  forse == True)
                    XSetSelectionOwner (dpy, atom, ims_win, CurrentTime);
                else
                    return False;
            }
            break;
        }
    }

    if (found == False) {
        XSetSelectionOwner (dpy, atom, ims_win, CurrentTime);
        XChangeProperty (dpy,
                         root,
                         XIM_Servers,
                         XA_ATOM,
                         32,
                         PropModePrepend,
                         (unsigned char *) &atom,
                         1);
    }
    else {
	/* 
	 * We always need to generate the PropertyNotify to the Root Window 
	 */
        XChangeProperty (dpy,
                         root,
                         XIM_Servers,
                         XA_ATOM,
                         32,
                         PropModePrepend,
                         (unsigned char *) data,
                         0);
    }
    if (data != NULL)
        XFree ((char *) data);
    
    /* Intern "LOCALES" and "TRANSOPORT" Target Atoms */
    i18n_core->address.Localename = XInternAtom (dpy, LOCALES, False);
    i18n_core->address.Transportname = XInternAtom (dpy, TRANSPORT, False);
    return (XGetSelectionOwner (dpy, atom) == ims_win);
}
Exemple #20
0
int initlgwin()
{
	XSetWindowAttributes winattr;	/* window attributes */
	XGCValues gcvals;		/* for setting the graphics context */
	char wintitle[80];	/* The window's title */

	unsigned long pmtmp[1];	/* temporary for plane masks */
	XVisualInfo vTemplate;		/* The template for our visual */
	XVisualInfo *visualList;	/* The visuals which matched */
	int visualsMatched;		/* how many matched? */
	int newmap;			/* do we need our own color map */
	Pixmap lgcurspmap;		/* pixmap for line graphics cursor */
	int valuemask;			/* the values set in XCreateWindow */

	Window foobar;

	Pixmap XCreatePixmap();
	void initlgluts();	/* initialize the LUTs */
	char *malloc();
	void initwmattr();

	/* Initialize the line graphics LUTs */
	initlgluts();

	/* initialize the line graphics wininfo structure */
	lg.height=lg.imheight=res.lggeo.h;
	lg.width=lg.imwidth=res.lggeo.w;
	lg.cursx=lg.width>>1;
	lg.cursy=lg.height>>1;
	lg.xzoom=lg.yzoom=0;
	lg.curxsc=lg.curysc=1.0;
	lg.curxoff=lg.curyoff=0.0;
	lg.winxoff=lg.winyoff=0;

	/* Load the cursor icon */
	if (res.lgcross)
	{
	        XColor bg;  /* Background cursor color */
	        XColor fg;  /* Foreground cursor color */
	
        /* Query the X-server for the background and foreground colors */

	        bg.pixel = lg.pix[0];
                XQueryColor(display, linecmap, &bg);
	        fg.pixel = lg.pix[1];
                XQueryColor(display, linecmap, &fg);

		lgcurspmap=XCreateBitmapFromData(display,
			RootWindow(display,screen), nocursor_bits,
			nocursor_width, nocursor_height);
		winattr.cursor=XCreatePixmapCursor(display, lgcurspmap,
			None, &fg, &bg, 0, 0);
	}
	
	/* create a window for line graphics */
	winattr.background_pixmap=None;
	winattr.background_pixel=lg.pix[0];
	winattr.colormap=linecmap;
	winattr.border_pixel=lg.pix[0];
	winattr.bit_gravity=SouthWestGravity;

	valuemask= CWBackPixel | CWColormap | CWBitGravity | CWBackPixmap |
		CWBorderPixel;
	if (res.lgcross)
		valuemask |= CWCursor;
	lg.win=XCreateWindow(display, RootWindow(display,screen), res.lggeo.x,
		res.lggeo.y, lg.width, lg.height, BORDER_WIDTH, (int)linedepth,
		InputOutput, linevisual, valuemask, &winattr);
	lg.mapped=0;	/* it's not mapped yet */

	/* Load the icon */
	lg.icon=XCreateBitmapFromData(display, lg.win, (char *)&figdisp_bits[0],
		figdisp_width, figdisp_height);
	
	/* set up the window manager hints */
	(void)sprintf(&wintitle[0],"line graphics #%d",res.id);
	initwmattr(lg, &wintitle[0], "line graphics", &res.lggeo);

	/* The lock and selection atoms need to be owned by someone */
	XSetSelectionOwner(display,lock,lg.win,CurrentTime);
	XSetSelectionOwner(display,selatom,lg.win,CurrentTime);

	/* was there a problem owning the locking atom? */
	if (XGetSelectionOwner(display,lock) != lg.win)
		(void)fprintf(stderr,MSG_NOLOCK);

	/* if we can't get ownership of the selection atom, we won't */
	/* be able to receive any commands */
	if (XGetSelectionOwner(display,selatom) != lg.win)
	{
		(void)fprintf(stderr,MSG_BADSELOWN);
		return(-1);
	}

	/* Create the line graphics pixmap */
	lg.pixmap=XCreatePixmap(display, RootWindow(display,screen), lg.width,
		lg.height, linedepth);
	
	/* set up a graphics contexts for the line graphics window */
	gcvals.foreground=lg.pix[0];
	linegcclear=XCreateGC(display,lg.win,GCForeground,&gcvals);
	gcvals.background=lg.pix[0];
	gcvals.foreground=lg.pix[1];
	gcvals.fill_style=FillSolid;
	gcvals.fill_rule=EvenOddRule;
	linegc=XCreateGC(display, lg.win,
		GCForeground|GCBackground|GCFillStyle|GCFillRule,&gcvals);
	
	XSetFont(display, linegc, res.textfont->fid);
	XSetFont(display, linegcclear, res.textfont->fid);
	
	/* Clear the pixmap */
	XFillRectangle(display, lg.pixmap, linegcclear, 0, 0, lg.width,
		lg.height);

	/* we need to listen for new data */
	XSelectInput(display,lg.win,PropertyChangeMask | ExposureMask |
		StructureNotifyMask | ButtonPressMask | PointerMotionMask |
		KeyPressMask);

	XFlush(display);
	return(SUCCEED);
}
Exemple #21
0
int
_xtoplan9mouse(XEvent *e, Mouse *m)
{
	int s;
	XButtonEvent *be;
	XMotionEvent *me;

	if(_x.putsnarf != _x.assertsnarf){
		_x.assertsnarf = _x.putsnarf;
		XSetSelectionOwner(_x.display, XA_PRIMARY, _x.drawable, CurrentTime);
		if(_x.clipboard != None)
			XSetSelectionOwner(_x.display, _x.clipboard, _x.drawable, CurrentTime);
		XFlush(_x.display);
	}

	switch(e->type){
	case ButtonPress:
		be = (XButtonEvent*)e;
		/* 
		 * Fake message, just sent to make us announce snarf.
		 * Apparently state and button are 16 and 8 bits on
		 * the wire, since they are truncated by the time they
		 * get to us.
		 */
		if(be->send_event
		&& (~be->state&0xFFFF)==0
		&& (~be->button&0xFF)==0)
			return -1;
		/* BUG? on mac need to inherit these from elsewhere? */
		m->xy.x = be->x;
		m->xy.y = be->y;
		s = be->state;
		m->msec = be->time;
		switch(be->button){
		case 1:
			s |= Button1Mask;
			break;
		case 2:
			s |= Button2Mask;
			break;
		case 3:
			s |= Button3Mask;
			break;
		case 4:
			s |= Button4Mask;
			break;
		case 5:
			s |= Button5Mask;
			break;
		}
		break;
	case ButtonRelease:
		be = (XButtonEvent*)e;
		m->xy.x = be->x;
		m->xy.y = be->y;
		s = be->state;
		m->msec = be->time;
		switch(be->button){
		case 1:
			s &= ~Button1Mask;
			break;
		case 2:
			s &= ~Button2Mask;
			break;
		case 3:
			s &= ~Button3Mask;
			break;
		case 4:
			s &= ~Button4Mask;
			break;
		case 5:
			s &= ~Button5Mask;
			break;
		}
		break;

	case MotionNotify:
		me = (XMotionEvent*)e;
		s = me->state;
		m->xy.x = me->x;
		m->xy.y = me->y;
		m->msec = me->time;
		break;

	default:
		return -1;
	}

	m->buttons = 0;
	if(s & Button1Mask)
		m->buttons |= 1;
	if(s & Button2Mask)
		m->buttons |= 2;
	if(s & Button3Mask)
		m->buttons |= 4;
	if(s & Button4Mask)
		m->buttons |= 8;
	if(s & Button5Mask)
		m->buttons |= 16;
	return 0;
}
Exemple #22
0
void
put_scrap(int type, int srclen, char *src)
{
	scrap_type format;
	int dstlen;
#if (defined(WZ_WS_X11) || defined(WZ_WS_WIN) || defined(WZ_WS_QNX))
	char *dst;
#endif

	format = convert_format(type);
	dstlen = convert_data(type, NULL, src, srclen);

#if defined(WZ_WS_X11)
	dst = (char *)malloc(dstlen);
	if ( dst != NULL )
	{
		Lock_Display();
		convert_data(type, dst, src, srclen);
		XChangeProperty(SDL_Display, DefaultRootWindow(SDL_Display),
			XA_CUT_BUFFER0, format, 8, PropModeReplace, (unsigned char *)dst, dstlen);
		free(dst);
		if ( lost_scrap() )
			XSetSelectionOwner(SDL_Display, XA_PRIMARY, SDL_Window, CurrentTime);
		Unlock_Display();
	}

#elif defined(WZ_WS_WIN)
/* * */
if ( OpenClipboard(SDL_Window) )
	{
	HANDLE hMem;

	hMem = GlobalAlloc((GMEM_MOVEABLE|GMEM_DDESHARE), dstlen);
	if ( hMem != NULL )
		{
		dst = (char *)GlobalLock(hMem);
		convert_data(type, dst, src, srclen);
		GlobalUnlock(hMem);
		EmptyClipboard();
		SetClipboardData(format, hMem);
		}
	CloseClipboard();
	}

#elif defined(WZ_WS_QNX)
/* * */
#if (_NTO_VERSION < 620) /* before 6.2.0 releases */
{
	PhClipHeader clheader={Ph_CLIPBOARD_TYPE_TEXT, 0, NULL};
	int* cldata;
	int status;

	dst = (char *)malloc(dstlen+4);
	if (dst != NULL)
	{
		cldata=(int*)dst;
		*cldata=type;
		convert_data(type, dst+4, src, srclen);
		clheader.data=dst;
		if (dstlen>65535)
		{
		clheader.length=65535; /* maximum photon clipboard size :( */
		}
		else
		{
		clheader.length=dstlen+4;
		}
		status=PhClipboardCopy(InputGroup, 1, &clheader);
		if (status==-1)
		{
		fprintf(stderr, "Photon: copy to clipboard was failed !\n");
		}
		free(dst);
	}
}
#else /* 6.2.0 and 6.2.1 and future releases */
{
	PhClipboardHdr clheader={Ph_CLIPBOARD_TYPE_TEXT, 0, NULL};
	int* cldata;
	int status;

	dst = (char *)malloc(dstlen+4);
	if (dst != NULL)
	{
		cldata=(int*)dst;
		*cldata=type;
		convert_data(type, dst+4, src, srclen);
		clheader.data=dst;
		clheader.length=dstlen+4;
		status=PhClipboardWrite(InputGroup, 1, &clheader);
		if (status==-1)
		{
		fprintf(stderr, "Photon: copy to clipboard was failed !\n");
		}
		free(dst);
	}
}
#endif
#endif /* scrap type */
}
Exemple #23
0
void start_net()
{
	if (systray_profile)
		fprintf(stderr, "[%f] %s:%d\n", profiling_get_time(), __FUNCTION__, __LINE__);
	if (net_sel_win) {
		// protocol already started
		if (!systray_enabled)
			stop_net();
		return;
	} else {
		if (!systray_enabled)
			return;
	}

	Window win = XGetSelectionOwner(server.display, server.atom._NET_SYSTEM_TRAY_SCREEN);

	// freedesktop systray specification
	if (win != None) {
		// search pid
		Atom _NET_WM_PID, actual_type;
		int actual_format;
		unsigned long nitems;
		unsigned long bytes_after;
		unsigned char *prop = 0;
		int pid;

		_NET_WM_PID = XInternAtom(server.display, "_NET_WM_PID", True);
		int ret = XGetWindowProperty(server.display,
		                             win,
		                             _NET_WM_PID,
		                             0,
		                             1024,
		                             False,
		                             AnyPropertyType,
		                             &actual_type,
		                             &actual_format,
		                             &nitems,
		                             &bytes_after,
		                             &prop);

		fprintf(stderr, RED "tint2 : another systray is running" RESET);
		if (ret == Success && prop) {
			pid = prop[1] * 256;
			pid += prop[0];
			fprintf(stderr, " pid=%d", pid);
		}
		fprintf(stderr, RESET "\n");
		return;
	}

	// init systray protocol
	net_sel_win = XCreateSimpleWindow(server.display, server.root_win, -1, -1, 1, 1, 0, 0, 0);
	fprintf(stderr, "systray window %ld\n", net_sel_win);

	// v0.3 trayer specification. tint2 always horizontal.
	// Vertical panel will draw the systray horizontal.
	long orientation = 0;
	XChangeProperty(server.display,
	                net_sel_win,
	                server.atom._NET_SYSTEM_TRAY_ORIENTATION,
	                XA_CARDINAL,
	                32,
	                PropModeReplace,
	                (unsigned char *)&orientation,
	                1);
	if (systray.icon_size > 0) {
		long icon_size = systray.icon_size;
		XChangeProperty(server.display,
		                net_sel_win,
		                server.atom._NET_SYSTEM_TRAY_ICON_SIZE,
		                XA_CARDINAL,
		                32,
		                PropModeReplace,
		                (unsigned char *)&icon_size,
		                1);
	}
	long padding = 0;
	XChangeProperty(server.display,
	                net_sel_win,
	                server.atom._NET_SYSTEM_TRAY_PADDING,
	                XA_CARDINAL,
	                32,
	                PropModeReplace,
	                (unsigned char *)&padding,
	                1);

	VisualID vid;
	if (systray_composited)
		vid = XVisualIDFromVisual(server.visual32);
	else
		vid = XVisualIDFromVisual(server.visual);
	XChangeProperty(server.display,
	                net_sel_win,
					XInternAtom(server.display, "_NET_SYSTEM_TRAY_VISUAL", False),
	                XA_VISUALID,
	                32,
	                PropModeReplace,
	                (unsigned char *)&vid,
	                1);

	XSetSelectionOwner(server.display, server.atom._NET_SYSTEM_TRAY_SCREEN, net_sel_win, CurrentTime);
	if (XGetSelectionOwner(server.display, server.atom._NET_SYSTEM_TRAY_SCREEN) != net_sel_win) {
		stop_net();
		fprintf(stderr, RED "tint2 : can't get systray manager" RESET "\n");
		return;
	}

	fprintf(stderr, GREEN "tint2 : systray started" RESET "\n");
	if (systray_profile)
		fprintf(stderr, "[%f] %s:%d\n", profiling_get_time(), __FUNCTION__, __LINE__);
	XClientMessageEvent ev;
	ev.type = ClientMessage;
	ev.window = server.root_win;
	ev.message_type = server.atom.MANAGER;
	ev.format = 32;
	ev.data.l[0] = CurrentTime;
	ev.data.l[1] = server.atom._NET_SYSTEM_TRAY_SCREEN;
	ev.data.l[2] = net_sel_win;
	ev.data.l[3] = 0;
	ev.data.l[4] = 0;
	XSendEvent(server.display, server.root_win, False, StructureNotifyMask, (XEvent *)&ev);
}