weblog d’un abbe

20060825

Xfixes or ऍक्स-जुगाड़

Filed under: Hacking, Research — abbe @ 0335

One of my recent researches are how to monitor X clipboard. After searching for X Windows Protocol specification, I didn’t find any reference to such functionality like being notified on selection ownership. I became quite disappointed. So, I thought may be ICCCM might have something for this but then I realized that since ICCCM is a layer on top of X server, so if X server doesn’t provide anything, how is it possible for ICCCM to provide something for this.

So, I thought this functionality must be provided by some extension that plugs in to X server. So, then I headed to my first choice for finding X extensions docs, FreeDesktop.org . The first extension listed there is Xfixes . After reading its description, I realized that this is the extension I needed. Then I just did a xdpyinfo to check whether I’m running this extension or not. And yes, I’m running it already. So I then started reading its specification and wrote the following piece of code, that monitors for ownership of PRIMARY selection.

/****** look.c ******/
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xfixes.h>
#include <stdio.h>
#include <signal.h>

static int	running = 0;

void
ctrl_c(int signum)
{
	running = ~running;
}

int
main()
{
	Display        *display;
	int		ver_major , ver_minor;
	int		evt_base  , err_base;
	XEvent		event;
	XFixesSelectionNotifyEvent *selection_event;
	char           *sz;
	Window		win;

	if (!(display = XOpenDisplay(NULL)))
	{
		fprintf(stderr, "Error opening X display\n");
		return 1;
	}
	if (!XFixesQueryExtension(display, &evt_base, &err_base))
	{
		fprintf(stderr, "XFixes n/a.\n");
		XCloseDisplay(display);
		return 2;
	}
	if (!XFixesQueryVersion(display, &ver_major, &ver_minor))
	{
		fprintf(stderr, "XFixes version not queriable.\n");
		XCloseDisplay(display);
	}
	printf("XFixes v%d.%d available with ", ver_major, ver_minor);
	printf("Event Base: 0x%x and Error Base: 0x%x\n", evt_base, err_base);

	win = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 1, 1, 0, 0, InputOnly,
			    NULL, 0, NULL);

	XFixesSelectSelectionInput(display, win, XA_PRIMARY, XFixesSetSelectionOwnerNotifyMask);

	running = ~running;

	/* install a ctrl+c handler */
	signal(SIGINT, ctrl_c);

	while (running)
	{
		XNextEvent(display, &event);
		if (event.type == XFixesSelectionNotify + evt_base)
		{
			selection_event = (XFixesSelectionNotifyEvent *) & event;
			if (selection_event->subtype == XFixesSetSelectionOwnerNotify)
			{
				fprintf(stderr, "\"%s\" is now owned by ", 
					(sz = XGetAtomName(display, 
						selection_event->selection)));
				XFree(sz);
				XFetchName(display, selection_event->owner, &sz);
				fprintf(stderr, "\"%s\" (WM_NAME of %d).\n",
					sz, selection_event->owner);
				XFree(sz);
			}
		}
	}

	XDestroyWindow(display, win);

	XCloseDisplay(display);

	return 0;
}

/*
 * vim:ts=4
 */

###### Makefile #######
look: look.c
	$(CC) -o $@ $^ `pkg-config --cflags --libs x11 xfixes`
# if you're user of X11Ry where y < 7, then uncomment following line (in BSD make syntax)
# and commentabove line (in GNU make syntax)
#	$(CC) -o $@ $> -I/usr/X11R6/include -L/usr/X11R6/lib -lX11 -lXfixes

To clean shutdown the above program you need to change selection after CTRL-C in the terminal window. And to compile it, just make or gmake.Anyways, this is the extension acting as fix for this situation, that’s why it is called Xfixes (or localized in Hindi as ऍक्स-जुगाड़) ;-)

Advertisements

2 Comments »

  1. thanks dude, i am new to this stuff. Why do we need ‘+ evt_base’ ?

    Comment by suns — 20120710 @ 1620

    • IIRC, all event constants were defined relative to evt_base. I’ve not done xcb programming since quite some time.

      Comment by abbe — 20120710 @ 2006


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: