All pastes #896686 Raw Edit

Unnamed

public c v1 · immutable
·published 2008-02-08 21:50 UTC
rendered paste body
#include <X11/Xlib.h>#include <X11/Xatom.h>#include <stdio.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>Display *disp;Window win;void get_sel ( void ){	static Atom clip= 0;	Atom rtype;	unsigned long len, bytes;	int bits;	Window owner;	XEvent ev;	char *data;	if (!clip)		clip= XInternAtom(disp, "CLIPBOARD", False);	owner= XGetSelectionOwner(disp, clip);	if (owner==None)		return;	XDeleteProperty(disp, owner, clip);	XConvertSelection(disp, XA_PRIMARY, XA_STRING, clip, owner, CurrentTime);	while(!XCheckTypedWindowEvent(disp, owner, SelectionNotify, &ev))		;	if(ev.xselection.property==None)		return;	if (XGetWindowProperty(disp, owner, clip, 0, 1024, False, XA_STRING,				&rtype, &bits, &len, &bytes,				(unsigned char **) &data) != Success) {		return;	}	if(rtype!=XA_STRING || bits!=8) {		if (data)			XFree(data);		printf("Bad data type.\n");		return;	}	printf("String: [%s]\n", data);	XFree(data);}int main ( int argc, char **argv ){	char *data;	int size;	int loop;	XEvent ev;       	disp= XOpenDisplay(NULL);	if (!disp) {		perror("XOpenDisplay");		exit(-1);	}       	win= XCreateSimpleWindow(disp, DefaultRootWindow(disp), 10, 10,				100, 100, 0, 0, 0);	XSelectInput(disp, win, ButtonPressMask | KeyPressMask);	XMapWindow(disp, win);       	for (loop= 1; loop;) {		while(XPending(disp)) {			XNextEvent(disp, &ev);			if (ev.type==ButtonPress) {				data= XFetchBuffer(disp, &size, 0);				if (data) {					printf("Data size: %d\n", size);					printf("String: [%s]\n", data);					XFree(data);				}				else {					get_sel ();				}			}			else if (ev.type==KeyPress)				loop= 0;		}	}	XDestroyWindow(disp, win);	XCloseDisplay(disp);	return(0);}