/* $Id: ps_utils.c,v 1.1.1.1 90/11/28 16:45:37 altenhof Exp $ */

/*
 * Copyright (C) 1990 by Digital Equipment Corporation.
 * 
 * Author: Michael P. Altenhofen, CEC Karlsruhe e-mail:
 * Altenhofen@kampus.enet.dec.com
 * 
 * This file ist part of Shared X
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation without fee is hereby granted, but only for non-profit  use
 * and distribution,  and provided  that the copyright notice and this notice
 * is preserved on all copies.
 * 
 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#include <X11/Xos.h>
#include <X11/X.h>
#include "input.h"

#define PUBLIC extern

#define _USE_STRUCTS_
#include "glob.h"

#undef PUBLIC

#include <sys/types.h>		/* extern char *sbrk (); */

static pointer minfree = NULL;

static int ErrorfOn = 1;
static int MessagefOn = 0;

char *dev_tty_from_init = NULL;	/* since we need to parse it anyway */

/* Force connections to close on SIGHUP from init */

void
_AutoResetServer ()
{
  isItTimeToYield = TRUE;
  exit (0);
}

/* Force connections to close and then exit on SIGTERM, SIGINT */

void
_GiveUp ()
{
  exit (0);
}


/* VARARGS1 */
void
debug (enabled, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9)	/* limit of ten args */
  int enabled;
  char *f;
  char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
{
  if (enabled) {
    fprintf (stderr, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
    fflush (stderr);
  }
}

/* VARARGS1 */

void
ErrorF (f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9)	/* limit of ten args */
  char *f;
  char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
{
  if (ErrorfOn) {
    fprintf (stderr, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
    fflush (stderr);
  }
}

/* VARARGS1 */
void
MessageF (f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9)	/* limit of ten args */
  char *f;
  char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
{
  if (MessagefOn)
    fprintf (stderr, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
}

static void
AbortServer ()
{
  exit (1);
}

void
Error (str)
  char *str;
{
  perror (str);
}

/*
 * This is private to the OS layer.
 */
void
_Notice ()
{
}

/* VARARGS1 */
void
FatalError (f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9)	/* limit of ten args */
  char *f;
  char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
{
  ErrorF ("\nFatal shXbridge bug!\n");
  ErrorF (f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  ErrorF ("\n");
  AbortServer ();
  /* NOTREACHED */
}

long
GetTimeInMillis ()
{
  static long now;
  struct timeval tp;

  gettimeofday (&tp, 0);
  if (!now) {
    now = tp.tv_sec;
  }
  return ((tp.tv_sec - now) * 1000) + (tp.tv_usec / 1000);
}

ClientPtr
ClientFromDisplay (dpy)
  Display *dpy;

{
  register int i;

  for (i = 0; i < MAXSOCKS; i++)
    if (Connections[i] && (XDisplay (Connections[i]) == dpy))
      return (Connections[i]);
  return (NULL);
}

Display *
DisplayFromFd (fd)
  int fd;

{
  register int i;

  if (ConnectionNumber (XDisplay (muxClient)) == fd)
    return (XDisplay (muxClient));
  for (i = 0; i < MAXSOCKS; i++)
    if (Connections[i] && (ConnectionNumber (XDisplay (Connections[i])) == fd))
      return (XDisplay (Connections[i]));
  return (NULL);
}
