#include <X11/Intrinsic.h>
#include <X11/Xaw/Label.h>
#include <stdio.h>
#include <string.h>

#define REPARENT_LOOPS 50

void do_swallow(void);

void main (int argc, char **argv)
    {
	do_swallow();
	}

void do_swallow(void)
	{
	int dummy=0;
    Widget toplevel, hello;
    XtAppContext app;
    int ready = FALSE, number_of_subkids,number_of_kids,number_of_subsubkids,i,j,k,l;
    Window root, parent, *children=NULL,*subchildren=NULL,*subsubchildren=NULL;
	Window victim;
    char *windowname;
    XWMHints *leader_change;

    toplevel = XtAppInitialize (&app, "XtHello", NULL, 0,
                                &dummy, NULL, NULL, NULL, 0);
    hello = XtCreateManagedWidget ("Hello", labelWidgetClass,
                                   toplevel, NULL, 0);

/*
 * * addition
 */
    XtRealizeWidget (toplevel);
    XtMapWidget (toplevel);
    XtMapWidget (hello);
    XSync (XtDisplay (toplevel), FALSE);
    while (ready == FALSE)
        {
        if (children != (Window *) NULL)
            XFree (children);
        XQueryTree (XtDisplay (hello), RootWindowOfScreen (XtScreen (hello)), &root,
                    &parent, &children, &number_of_kids);
        for (i = 0; i < number_of_kids; i++)
            {
            if (0 != XFetchName (XtDisplay (hello), children[i], &windowname))
                {
                if (!strcmp (windowname, "barf"))
                    {
                    ready = TRUE;
                    victim = children[i];
					fprintf(stderr,"Found It\n");
                    }
				else
					fprintf(stderr,"Found %s\n",windowname);
                XFree (windowname);
                }
			/*for each of these toplevel windows check their children*/
			/*if it wasnt found*/
			if (ready == FALSE)
				{
    		    if (subchildren != (Window *) NULL)
            		XFree (subchildren);
        		XQueryTree (XtDisplay (hello), children[i], &root,
                    &parent, &subchildren, &number_of_subkids);
	        	for (k = 0; k < number_of_subkids; k++)
   		         	{
   		         	if (0 != XFetchName (XtDisplay (hello), subchildren[k], &windowname))
       		         	{
           		     	if (!strcmp (windowname, "barf"))
               		     	{
                    		ready = TRUE;
	                    	victim = subchildren[k];
							fprintf(stderr,"Found %s!!!\n",windowname);
   	    	             	}
						else
							fprintf(stderr,"Found %s\n",windowname);
                		XFree (windowname);
                		}
                    /**/
                    /*for each of these sublevel windows check their children*/
                    /*if it wasnt found*/
                    if (ready == FALSE)
                        {
                        if (subsubchildren != (Window *) NULL)
                            XFree (subsubchildren);
                        XQueryTree (XtDisplay (hello), subchildren[k], &root,
                            &parent, &subsubchildren, &number_of_subsubkids);
                        for (l = 0; l < number_of_subsubkids; l++)
                            {
                            if (0 != XFetchName (XtDisplay (hello), subsubchildren[l], &windowname))
                                {
                                if (!strcmp (windowname, "barf"))
                                    {
                                    ready = TRUE;
                                    victim = subsubchildren[l];
                                    fprintf(stderr,"Found %s!!!\n",windowname);
                                    }
                                else
                                    fprintf(stderr,"Found %s\n",windowname);
                                XFree (windowname);
                                }
                            }
                        }
                    }
                }
            }
        fprintf(stderr,"Loop");
        }
    XSync (XtDisplay (hello), FALSE);
    XWithdrawWindow (XtDisplay (hello), victim, XScreenNumberOfScreen (XtScreen (hello)));
    XSync (XtDisplay (hello), FALSE);


	leader_change = XGetWMHints(XtDisplay(hello),victim);
    leader_change->flags = (leader_change->flags | WindowGroupHint);
    leader_change->window_group = RootWindowOfScreen(XtScreen (hello));
                                
    XSetWMHints(XtDisplay(hello),victim,leader_change);

    for (j = 0; j < REPARENT_LOOPS; j++)
        {
        XReparentWindow (XtDisplay (hello), victim, XtWindow (hello), 0, 0);
        XSync (XtDisplay (hello), FALSE);
/*
 * bloody serious dodginess
 */
        }
    XMapWindow (XtDisplay (hello), victim);
    XSync (XtDisplay (hello), FALSE);

    if (children != (Window *) NULL)
    	XFree (children);
    if (subchildren != (Window *) NULL)
    	XFree (subchildren);
    if (subsubchildren != (Window *) NULL)
        XFree (subsubchildren);
    XtAppMainLoop (app);
    }
