/*
 * Copyright (C) 2009 Intel Corporation
 * This file is GPL 2.
 * Author: Ross Burton <ross@linux.intel.com>
 */

#include <glib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>

Bool
callback (XrmDatabase * database, XrmBindingList bindings, XrmQuarkList quarks, XrmRepresentation * type, XrmValue * value, XPointer closure)
{
  GString *s = (GString*)closure;
  int i;
  XrmQuark *q;

  for (i = 0, q = quarks; *q; q++, i++) {
    switch (bindings[i]) {
    case XrmBindTightly:
      if (i != 0)
        g_string_append_c (s, '.');
      break;
    case XrmBindLoosely:
      g_string_append_c (s, '*');
      break;
    }
    g_string_append (s, XrmQuarkToString (*q));
  }

  g_string_append (s, ":\t");
  g_string_append (s, value->addr);
  g_string_append_c (s, '\n');

  return False;
}

int
main (int argc, char **argv)
{
  Display *dpy;
  XrmDatabase db;
  XrmQuark null_quark[1] = { NULLQUARK };
  XTextProperty prop;
  GString *s;

  dpy = XOpenDisplay (NULL);
  db = XrmGetStringDatabase (XResourceManagerString (dpy));

  if (argc == 3) {
    XrmPutStringResource(&db, argv[1], argv[2]);
  }

  s = g_string_new (NULL);
  XrmEnumerateDatabase(db, null_quark, null_quark,
                       XrmEnumAllLevels, callback, (XPointer)s);

  prop.value = (unsigned char*)s->str;
  prop.nitems = s->len;
  prop.encoding = XA_STRING;
  prop.format = 8;
  XSetTextProperty (dpy, RootWindow (dpy, 0), &prop, XA_RESOURCE_MANAGER);

  XSync (dpy, False);

  return 0;
}
