/*********************************************************************
 *
 *            EZFM---EZWGL based file manager for X11
 *
 *             Copyright (C) 1998  Maorong Zou
 *
 *
 *  This file is part of EZFM.
 *
 *  EZFM is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * EZFM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EZFM; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *************************************************************************/




/* hacked from
 * remote.c --- remote control of Netscape Navigator for Unix.
 * version 1.1.3, for Netscape Navigator 1.1 and newer.
 */
#define _NETSCAPE_C_
#include "ezfm.h"
/***************************************************************************/
int netscapeIsRunning MY_ANSIARGS((void));
int xerrCode = 0;
int xerrHdlr(dpy, err)Display *dpy; XErrorEvent *err; {xerrCode = err->error_code;}

/***************************************************************************/
#define MOZILLA_VERSION_PROP   "_MOZILLA_VERSION"
#define MOZILLA_LOCK_PROP      "_MOZILLA_LOCK"
#define MOZILLA_COMMAND_PROP   "_MOZILLA_COMMAND"
#define MOZILLA_RESPONSE_PROP  "_MOZILLA_RESPONSE"
static Atom XA_MOZILLA_VERSION  = 0;
static Atom XA_MOZILLA_LOCK     = 0;
static Atom XA_MOZILLA_COMMAND  = 0;
static Atom XA_MOZILLA_RESPONSE = 0;

static void mozilla_remote_init_atoms (Display *dpy)
{
  if (! XA_MOZILLA_VERSION)
    XA_MOZILLA_VERSION = XInternAtom (dpy, MOZILLA_VERSION_PROP, False);
  if (! XA_MOZILLA_LOCK)
    XA_MOZILLA_LOCK = XInternAtom (dpy, MOZILLA_LOCK_PROP, False);
  if (! XA_MOZILLA_COMMAND)
    XA_MOZILLA_COMMAND = XInternAtom (dpy, MOZILLA_COMMAND_PROP, False);
  if (! XA_MOZILLA_RESPONSE)
    XA_MOZILLA_RESPONSE = XInternAtom (dpy, MOZILLA_RESPONSE_PROP, False);
}


static Window mozilla_remote_find_window (Display *dpy)
{
  int i;
  Window root = RootWindowOfScreen (DefaultScreenOfDisplay (dpy));
  Window root2, parent, *kids;
  unsigned int nkids;
  Window result = 0;

  XGrabServer(dpy); /* this is the (olny) safe way to do this right */
  if (! XQueryTree (dpy, root, &root2, &parent, &kids, &nkids))
    {
      XUngrabServer(dpy);
      return(None);
    }

  if (! (kids && nkids)) 
    {
      XUngrabServer(dpy);
      return(None);
    }

  for (i = nkids-1; i >= 0; i--)
    {
      Atom type;
      int format;
      unsigned long nitems, bytesafter;
      unsigned char *version = 0;
      Window w = EZ_FindClientWindow(kids[i]);
      if(w != None)
	{
	  int status;
	  int           (*OldErrHdlr)();

	  xerrCode = 0;
	  OldErrHdlr = XSetErrorHandler(xerrHdlr);
	  status = XGetWindowProperty (dpy, w, XA_MOZILLA_VERSION,
				       0, (65536 / sizeof (long)),
				       False, XA_STRING,
				       &type, &format, &nitems, &bytesafter,
				       &version);
	  XSync(dpy, False);
	  XSetErrorHandler(OldErrHdlr);
	  if(xerrCode != 0) return(None);
	  if (! version)
	    continue;
	  XFree (version);
	  if (status == Success && type != None)
	    {
	      result = w;
	      break;
	    }
	}
    }
  XUngrabServer(dpy);
  return(result);
}

static int mozilla_remote_check_window (Display *dpy, Window window)
{
  Atom type;
  int format;
  unsigned long nitems, bytesafter;
  unsigned char *version = 0;
  int           (*OldErrHdlr)();
  int           status, ret = 0;

  XSync(dpy, False);

  XGrabServer(dpy);
  if(EZ_WindowExist(window))
    {
      xerrCode = 0;
      OldErrHdlr = XSetErrorHandler(xerrHdlr);

      status = XGetWindowProperty (dpy, window, XA_MOZILLA_VERSION,
				   0, (65536 / sizeof (long)),
				   False, XA_STRING,
				   &type, &format, &nitems, &bytesafter,
				   &version);
      XSync(dpy, False);
      XSetErrorHandler(OldErrHdlr);  
      ret = 1;
      if(version) XFree (version);
      if(status != Success || !version) ret = 0;
      if(xerrCode != 0) ret = 0;
    }
  XUngrabServer(dpy);
  return(ret);
}
/*****************************************************************************/
static  int initialized = 0;
static  Window netscapeWindow = None;
/*****************************************************************************/

int netscapeIsRunning()
{
  if(initialized == 0) 
    {
      mozilla_remote_init_atoms(EZ_GetDisplay());
      initialized = 1;
    }
  if(netscapeWindow != None)
    {
      if(mozilla_remote_check_window (EZ_GetDisplay(), netscapeWindow))
	return(1);
      else netscapeWindow = None;
    }
  netscapeWindow =  mozilla_remote_find_window(EZ_GetDisplay());
  if(netscapeWindow != None) return(1);
  return(0);
}
/*****************************************************************************/
  
#undef _NETSCAPE_C_
