; /*
echo "Compiling shared library"
sc libcode nostackcheck constlib streq strmerge ignore=73 define=SHARED optimize optsize client_library.c
slink LIBFD client.fd TO LIBS:client.library FROM LIB:libent.o LIB:libinit.o client_library.o LIB lib:sc.lib libid "client 1.3 (2.2.95) Copyright 1994-1995 by Stefano Reksten" smalldata smallcode stripdebug noicons
copy LIBS:client.library ///Libraries

echo "Compiling linked library"
sc parms=registers optimize optsize constlib nostackcheck streq IGNORE=73 define=LINKED client_library.c
oml /client.lib r client_library.o

delete client_library.o
quit

 Client.lib  1.3
 Copyright © 1994-1995 by Stefano Reksten of 3AM - The Three Amigos!!!
 All rights reserved.

*/


#include <exec/memory.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <graphics/gfxmacros.h>
#include <hardware/custom.h>
#include <hardware/dmabits.h>

#include <proto/exec.h>
#include <proto/dos.h>
#include <string.h>
#include <stdio.h>

#include "//include/server.h"

#ifdef SHARED

 /****************************
 *                           *
 * "LOW-LEVEL" COMMUNICATION *
 *        PRIMITIVES         *
 *                           *
 ****************************/

static struct ClientMessage *__asm __saveds __AllocClientMessage( register __d0 ULONG action,register __a1 struct MsgPort *CommunicationPort )
{
struct ClientMessage *bmsg;

if ( bmsg = AllocVec( sizeof(struct ClientMessage), MEMF_PUBLIC | MEMF_CLEAR ) )
	{
	bmsg->cm_Msg.mn_Node.ln_Type = NT_MESSAGE;
	bmsg->cm_Msg.mn_ReplyPort = CommunicationPort;
	bmsg->cm_Msg.mn_Length = sizeof(struct ClientMessage);
	bmsg->cm_Action = action;
	}
return( bmsg );
}


static void __asm __saveds __FreeClientMessage( register __a1 struct ClientMessage *bmsg )
{
FreeVec( bmsg );
}


BOOL __asm __saveds __SendClientMsg( register __d0 ULONG action, register __a1 struct MsgPort *CommunicationPort )
{
struct MsgPort *ServerPort;
struct ClientMessage *bmsg, *replymsg = NULL;
BOOL talked = FALSE;

if ( bmsg = __AllocClientMessage( action, CommunicationPort ) )
	{
	Forbid();
	if ( ServerPort = FindPort( SERVERPORTNAME ) )
		PutMsg( ServerPort, (struct Message *)bmsg );
	Permit();

	if ( ServerPort )
		{
		while ( !replymsg )
			{
			WaitPort( CommunicationPort );
			replymsg = (struct ClientMessage *)GetMsg( CommunicationPort );
			}
		talked = TRUE;
		}
	__FreeClientMessage( bmsg );
	}
return( talked );
}


ULONG __asm __saveds __GetServerCommand( register __a1 struct MsgPort *CommunicationPort )
{
register struct ServerMessage *msg;
register ULONG command;

if ( msg = (struct ServerMessage *)GetMsg( CommunicationPort ) )
	{
	command = msg->sm_Command;
	ReplyMsg( (struct Message *)msg );
	return( command );
	}
else
	return( COMMAND_IDLE );
}


ULONG __asm __saveds __WaitServerCommand( register __a1 struct MsgPort *CommunicationPort )
{
WaitPort( CommunicationPort );
return( __GetServerCommand( CommunicationPort ) );
}


struct DisplayIDInformation * __asm __saveds __OpenCommunication( register __a1 struct MsgPort **mp )
{
struct MsgPort *ServerPort;
struct MsgPort *CommunicationPort;
struct ClientMessage *cm, *replymsg = NULL;
struct DisplayIDInformation *dinfo;
BOOL talked = FALSE;

if ( CommunicationPort = CreateMsgPort() )
	{
	if ( cm = __AllocClientMessage( ACTION_ARRIVED, CommunicationPort ) )
		{
		Forbid();
		if ( ServerPort = FindPort( SERVERPORTNAME ) )
			PutMsg( ServerPort, (struct Message *)cm );
			Permit();

		if ( ServerPort )
			{
			while ( !replymsg )
				{
				WaitPort( CommunicationPort );
				replymsg = (struct ClientMessage *)GetMsg( CommunicationPort );
				}
			talked = TRUE;
			dinfo = replymsg->DInfo;
			*mp = CommunicationPort;
				}

		__FreeClientMessage( cm );
		if ( talked )
			return dinfo;
		}
	DeleteMsgPort( CommunicationPort );
	}
return NULL;
}


BOOL __asm __saveds __CheckAA( void )
{
struct GfxBase *GfxBase;
BOOL AA = FALSE;

if ( GfxBase = (struct GfxBase *)OpenLibrary( "graphics.library", 0L ) )
	{
	if ( (GfxBase->LibNode.lib_Version >= 39) && (GfxBase->ChipRevBits0 & 1<<GFXB_AA_ALICE) )
		AA = TRUE;
	CloseLibrary( (struct Library *)GfxBase );
	}
return( AA );
}

#endif

#ifdef LINKED
#include "//include/client_pragmas.h"

 /***********************
 *                      *
 * Server.lib functions *
 *                      *
 ***********************/

struct Library *ClientBase;
static struct MsgPort *CommunicationPort;


struct DisplayIDInformation *__asm __saveds OpenCommunication( void )
{
register struct DisplayIDInformation *dinfo;

if ( ClientBase = OpenLibrary( "client.library", 0L ) )
	{
	if ( dinfo = __OpenCommunication( &CommunicationPort ) )
		return dinfo;
	CloseLibrary( ClientBase );
	}
return NULL;
}


void __asm __saveds CloseCommunication( register __a1 struct DisplayIDInformation *dinfo )
{
if ( CommunicationPort ) DeleteMsgPort( CommunicationPort );
if ( dinfo ) FreeVec( dinfo );
if ( ClientBase ) CloseLibrary( ClientBase );
}


BOOL __asm __saveds SendClientMsg( register __d0 ULONG action )
{
return( __SendClientMsg( action, CommunicationPort ) );
}


ULONG __asm __saveds GetServerCommand( void )
{
return( __GetServerCommand( CommunicationPort ) );
}


ULONG __asm __saveds WaitServerCommand( void )
{
return( __WaitServerCommand( CommunicationPort ) );
}


extern far struct Custom custom;

void __asm __saveds SpritesOff( void )
{
OFF_SPRITE
}

void __asm __saveds SpritesOn( void )
{
ON_SPRITE
}

BOOL __asm __saveds CheckAA( void )
{
return( __CheckAA() );
}

#endif
