/*
 * Copyright 1990 by Baylor College of Medicine ALL RIGHTS RESERVED. 
 *
 * This program is subject to a license agreement between 
 * Baylor College of Medicine and MIT. Any use inconsistent with
 * said license and any use by persons other than the faculty, 
 * students and staff at MIT or any use on a computer not operated 
 * as part of the Athena Computing Environment (ACE) is expressly 
 * prohibited.
 */
#include <X11/Xlib.h>

/* Converts bytes to long */
long
bytes_to_long(b)
	unsigned char *b ;
{
	/* If first bit set then number is negative */
	int i ;
	int neg = b[0] & 0x80 ;
	long l = 0 ;

	/* Put in the bytes */
	for(i = 0; i < sizeof(long); i++)
	{
		int j ;

		/* Get a byte and invert if negative */
		unsigned char tmp = neg ? (~b[i]) : (b[i]) ;

		for(j = 7; j >= 0; j--)
		{
			l *= 2 ;
			l += (tmp >> j) & 1 ;
		}
	}

	/* If negative then negate */
	return neg ? (-1 - l) : l ;
}

/* Converts long to bytes */
long_to_bytes(l,b)
	long l ;
	unsigned char *b ;
{
	int neg = 0 ;
	int i ;

	/* If negative then negate the value */
	if (l < 0)
	{
		neg = !neg ;
		l = -1 - l ;
	}

	/* Copy in the bits */
	for(i = sizeof(long) - 1; i >= 0; i--)
	{
		int j ;
		unsigned char tmp = 0 ;
		for(j = 0; j < 8; j++)
		{
			tmp |= (l % 2) << j ;
			l /= 2 ;
		}

		/* Invert if negative */
		b[i] = neg ? (~tmp) : tmp ;
	}
}

fill_up_message(l1,l2,l3,l4,l5,event)
	long l1,l2,l3,l4,l5 ;
	XClientMessageEvent *event ;
{
	event->format = 8 ;
	event->type = ClientMessage ;
	event->send_event = 1 ;
	long_to_bytes(l1,(unsigned char *)event->data.b) ;
	long_to_bytes(l2,(unsigned char *)event->data.b + 4) ;
	long_to_bytes(l3,(unsigned char *)event->data.b + 8) ;
	long_to_bytes(l4,(unsigned char *)event->data.b + 12) ;
	long_to_bytes(l5,(unsigned char *)event->data.b + 16) ;
}

long
long_event_data(event,i)
	XClientMessageEvent *event ;
{
	return
		(event->format == 32) ?
			event->data.l[i] :
			bytes_to_long((unsigned char *)event->data.b + i * sizeof(unsigned long)) ;
}
