/* LD_PRELOAD module to munge with visuals */
/* gcc -c -fPIC xstub.c
   ld -o xstub.so -G xstub.o */

#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

typedef Window(*XCWtype)( /*XCreateWindow*/
    Display*            /* display */,
    Window              /* parent */,
    int                 /* x */,
    int                 /* y */,
    unsigned int        /* width */,
    unsigned int        /* height */,
    unsigned int        /* border_width */,
    int                 /* depth */,
    unsigned int        /* class */,
    Visual*             /* visual */,
    unsigned long       /* valuemask */,
    XSetWindowAttributes*       /* attributes */
);

typedef Status(*XGWAtype)( /* XGetWindowAttributes */
    Display*		/* display */,
    Window		/* w */,
    XWindowAttributes*	/* window_attributes_return */
);

Window XCreateWindow(
  Display*            display,
  Window              parent,
  int                 x,
  int                 y,
  unsigned int        width,
  unsigned int        height,
  unsigned int        border_width,
  int                 depth,
  unsigned int        class,
  Visual*             visual,
  unsigned long       valuemask,
  XSetWindowAttributes*       attributes)
{
  void *handle;
  XCWtype fptr;
  Window ret;
  XVisualInfo xvi;
  Colormap cm;
  int *xd, xdi;

  handle = dlopen("libX11.so", RTLD_LAZY);
  fptr = dlsym(handle, "XCreateWindow");

  depth = 8;

  if (XMatchVisualInfo(display, DefaultScreen(display), depth,
		      PseudoColor, &xvi) == 0)
   fprintf(stderr, "XMVI 0\n");
  visual = xvi.visual;

  cm = XCreateColormap(display, parent, visual, AllocNone);
  attributes->colormap = cm;
  valuemask |= CWColormap;

  attributes->border_pixel = 0;
  valuemask |=  CWBorderPixel ;

  fprintf(stderr, "jhawk: depth was %d visual 0x%x\n", depth, visual?visual->visualid:-1);

  class = InputOutput;
#if 1
 XFlush(display); XSync(display, 0); 
#endif

#if 0
  cm = XCreateColormap(display, parent, visual, AllocNone);

 XFlush(display); XSync(display, 0);
#endif

  ret=(*fptr)(
    /* Display* */			display,
    /* Window */			parent,
    /* int */				x,
    /* int */				y,
    /* unsigned int */			width,
    /* unsigned int */			height,
    /* unsigned int */			border_width,
    /* int */				depth,
    /* unsigned int */			class,
    /* Visual* */			visual,
    /* unsigned long */			valuemask,
    /* XSetWindowAttributes* */		attributes);

#if 1
 XFlush(display); XSync(display, 0);
#endif

  dlclose(handle);
  return ret;
}

#if 0
Status XGetWindowAttributes(
    Display*		display,
    Window		w,
    XWindowAttributes*	window_attributes_return
    ) {
  void *handle; XGWAtype fptr; Status ret;

  handle = dlopen("libX11.so", RTLD_LAZY);
  fptr = dlsym(handle, "XGetWindowAttributes");

  ret = (*fptr)( display, w, window_attributes_return);

  fprintf(stderr, "before: for window 0x%x depth %d visual 0x%x cmap 0x%x\n",
    w, window_attributes_return->depth, window_attributes_return->visual,
    window_attributes_return->colormap);

#if 0
  window_attributes_return->depth = 8;
#endif

  fprintf(stderr, "after: for window 0x%x depth %d visual 0x%x cmap 0x%x\n",
    w, window_attributes_return->depth, window_attributes_return->visual,
    window_attributes_return->colormap);
 
  dlclose(handle);
  return ret;
}
#endif

