//
// MavRaise (c) Edwin Groothuis, edwin@mavetju.org
//
// Parts of this code (well, everything except for main()) is stolen
// from dsimple.c of the XFree86 project (xc/programs/xlsfonts)
//
// $Id: mavraise.c,v 1.6 2005/06/02 07:45:40 mavetju Exp $
//
/*

Copyright 1993, 1998  The Open Group

All Rights Reserved.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.
*/


#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Display *	dpy;
Window		w;
int		screen;

Display *Open_Display(char *display_name) {
    if ((dpy=XOpenDisplay(display_name))==NULL) {
	fprintf(stderr,"unable to open display '%s'\n",
		XDisplayName(display_name));
	exit(0);
    }

    return(dpy);
}


char *Get_Display_Name(int *pargc,char **argv) {
    int argc = *pargc;
    char **pargv = argv+1;
    char *displayname = NULL;
    int i;

    for (i = 1; i < argc; i++) {
	char *arg = argv[i];

	if (!strcmp (arg, "-display") || !strcmp (arg, "-d")) {
	    displayname = argv[i];
	    *pargc -= 2;
	    continue;
	}
	if (!strcmp(arg,"-")) {
	    while (i<argc)
		*pargv++ = argv[i++];
	    break;
	}
	*pargv++ = arg;
    }

    *pargv = NULL;
    return (displayname);
}


void Setup_Display_And_Screen(void) {
    dpy=Open_Display(NULL);
}


Window Window_With_Name(Display *dpy,Window top,char *name) {
    Window *children, dummy;
    unsigned int nchildren;
    int i;
    Window w=0;
    char *window_name;

    if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name))
        return(top);
                        
    if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
        return(0);
         
    for (i=0; i<nchildren; i++) {
	w = Window_With_Name(dpy, children[i], name);
	if (w)
	    break;
    }
    if (children) XFree ((char *)children);
    if (window_name) XFree ((char *)window_name);
    return(w);
}


Window Select_Window_Args(char *xterm) {

    w=Window_With_Name(dpy, RootWindow(dpy, screen), xterm);
    if (w==0) {
	fprintf(stderr,"No window with name %s exists!",xterm);
	return NULL;
    }

    return (w);
}

void init_X(void) {
}

void close_X(void) {
}

int mavraise(char *xterm) {
    Setup_Display_And_Screen();
    if ((w=Select_Window_Args(xterm))==NULL) {
	perror("Cannot find window");
	return 1;
    }
    XRaiseWindow(dpy,w);
    XCloseDisplay(dpy);

    return 0;
}


syntax highlighted by Code2HTML, v. 0.9.1