Skip to content

Instantly share code, notes, and snippets.

@keks
Created January 24, 2011 03:19
Show Gist options
  • Save keks/792776 to your computer and use it in GitHub Desktop.
Save keks/792776 to your computer and use it in GitHub Desktop.
CC=clang
CFLAGS=-Wall -Wextra -pedantic -std=c89
LIBS=-lXmuu -lX11 -lXcursor
SRC=xfetchname.c
OBJS=$(SRC:.c=.o)
xfetchname: $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
clean:
rm -f $(OBJS) xfetchname
/**
Boilerplate (everything except 5 lines or so)
stolen from xsetroot, rest by Jan Winkelmann <[email protected]>
I don't care what you do with this, just don't blame me if it breaks stuff.
**/
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#define Dynamic 1
static char *program_name;
static Display *dpy;
static int screen;
static Window root;
static int save_colors = 0;
static int unsave_past = 0;
static Pixmap save_pixmap = (Pixmap)None;
static void FixupState(void);
int
main()
{
char *display_name = NULL;
char *name = NULL;
name = malloc(64);
if(!name)
fprintf(stderr, "unable to allocate memory"), exit(3);
dpy = XOpenDisplay(display_name);
if (!dpy) {
fprintf(stderr, "%s: unable to open display '%s'\n",
program_name, XDisplayName (display_name));
exit (2);
}
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
XFetchName(dpy, root, &name);
name[63]=NULL // dumb way to protect against buffer overflows.
printf("%s", name);
FixupState();
XCloseDisplay(dpy);
exit (0);
}
/* Free past incarnation if needed, and retain state if needed. */
static void
FixupState(void)
{
Atom prop, type;
int format;
unsigned long length, after;
unsigned char *data;
if (!(DefaultVisual(dpy, screen)->class & Dynamic))
unsave_past = 0;
if (!unsave_past && !save_colors)
return;
prop = XInternAtom(dpy, "_XSETROOT_ID", False);
if (unsave_past) {
(void)XGetWindowProperty(dpy, root, prop, 0L, 1L, True, AnyPropertyType,
&type, &format, &length, &after, &data);
if ((type == XA_PIXMAP) && (format == 32) &&
(length == 1) && (after == 0))
XKillClient(dpy, *((Pixmap *)data));
else if (type != None)
fprintf(stderr, "%s: warning: _XSETROOT_ID property is garbage\n",
program_name);
}
if (save_colors) {
if (!save_pixmap)
save_pixmap = XCreatePixmap(dpy, root, 1, 1, 1);
XChangeProperty(dpy, root, prop, XA_PIXMAP, 32, PropModeReplace,
(unsigned char *) &save_pixmap, 1);
XSetCloseDownMode(dpy, RetainPermanent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment