/* $Id: shXchange.c,v 1.1 91/04/25 15:32:35 sao 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 "XmuX.h"
#include <stdio.h>
#include <strings.h>
/*#include <stdlib.h>*/
/*#include <malloc.h>*/
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>

int
XmuXSetHints (dpy, target, hints)
  Display *dpy;
  Window target;
  XmuXHints *hints;
{
  Atom atom;

  if (hints->type == XMUXProperty) {
    if (!hints->u.strings[XMUXType] || !hints->u.strings[XMUXSubtype]) {
      printf ("Invalid data! \n");
      return (BadValue);
    }
    atom = XInternAtom (dpy, hints->u.strings[XMUXType], True);
    if (!atom) {
      printf ("Atom %s not valid!\n", hints->u.strings[XMUXType]);
      return (BadAtom);
    }
/*
    printf ("Change property on window 0x%lx , value = %s (%d)\n",
	    target, hints->u.strings[XMUXSubtype],
	    strlen (hints->u.strings[XMUXSubtype]));
*/
    XChangeProperty (dpy, target, atom, XA_STRING, 8,
		     PropModeReplace, hints->u.strings[XMUXSubtype],
		     strlen (hints->u.strings[XMUXSubtype]) + 1);
  }
  else {
    XEvent message;

    message.type = ClientMessage;
    message.xclient.display = dpy;
    message.xclient.window = target;
    atom = XInternAtom (dpy, XMUX_MESSAGE_NAME, False);
    if (!atom) {
      printf ("Atom %s not valid!\n", XMUX_MESSAGE_NAME);
      return (BadAtom);
    }
    message.xclient.message_type = atom;
    message.xclient.format = XMUX_MESSAGE_FORMAT;
    message.xclient.data.l[XMUXType] = hints->u.mess[XMUXType];
    message.xclient.data.l[XMUXSubtype] = hints->u.mess[XMUXSubtype];
    message.xclient.data.l[XMUXDetail] = hints->u.mess[XMUXDetail];
/*
    printf ("Send event w =  0x%lx , atom = %d", target, atom);
    if (hints->u.mess[XMUXSubtype] == XMUXPassChalk)
      printf ("message = pass chalk\n");
*/
    XSendEvent (dpy, target, False, PropertyChangeMask, &message);
  }
  XFlush (dpy);
  return (Success);
}


static void
usage (s)
  char* s;
{
  fprintf (stderr, "usage: %s [pc] window_id string\n", s);
  exit (1);
}

#ifdef	HELL_FROZEN_OVER
int
main (argc, argv)
  int argc;
  char *argv[];
{
  Display *dpy;
  XmuXHints mux_hints;

  if (argc < 4) {
    usage (argv[0]);
  }

  if ((dpy = XOpenDisplay (NULL)) == NULL)
    exit (1);

  if (strcmp (argv[1], "p") == 0) { /* Add display to the list */
    mux_hints.type = XMUXProperty;
    mux_hints.u.strings[0] = XMUX_CPORT_NAME;
    mux_hints.u.strings[1] = argv[3];
  }
  else if (strcmp (argv[1], "c") == 0) { /* pass chalk */
    mux_hints.type = XMUXMessage;
    mux_hints.u.mess[XMUXType] = XMUXAction;
    mux_hints.u.mess[XMUXSubtype] = XMUXPassChalk;
    mux_hints.u.mess[XMUXDetail] = atoi (argv[3]);
  }
  else
    usage (argv[0]);
  XmuXSetHints (dpy, (unsigned long) atoi (argv[2]), &mux_hints);
  exit (0);
}
#endif
