/*
** libfuncsc
** 19th May 2000
** David McMinn
** The actual public functions that are in our plugin. Remember, there
** is only one public function in a Scalos plugin, and it simply returns
** a pointer to a ScaClassInfo. The h_Entry field of the Hook structure
** within the ScaClassInfo structure points to the actual function which
** performs the task required (adding text to the titles of screens,
** windows etc).
*/

#include <exec/types.h>
#include <clib/alib_protos.h>
#include <clib/alib_stdio_protos.h>

#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/intuition.h>
#include <scalos/scalos.h>

#include <utility/hooks.h>

#include "defs.h"

/* This is the UWORD which contains the ASCII value of
** the characters that this plugin should be activated by.
** in this case the ASCII characters are 0x77 followed by
** 0x70 which corresponds to w and p. This means that to
** activate this plugin you need to have "%wp" enetered
** somewhere in the title string in the Scalos main prefs.
*/
#define PARSE   0x7770


/* A structure which defines the parameters that will be sent
** as part of the message to the plugin hook function.
** These are not defined as structures in scalos/scalos.h
** but the types and order are shown. These may or may not get
** defined in scalos/scalos.h in the future
*/
struct Title_Translate_Params
{
	ULONG   MethodID;       /* check that this is SCCM_Title_Translate */
	char    *TitleMem;      /* pointer to the end of the title string that you should tack your string onto */
	char    *TitleFormat;   /* dunno */
	UWORD   ParseID;        /* will be the pasre ID (i.e. the %wp or whatever else) for the plugin. make sure you only act when this is your parse ID! */
};



/* The name of the superclass for this class */
char    superclassname[] = "Title.sca";

/* The name of this plugin */
char    name[] = "Title FreePens";

/* Small description of what the plugin does - you should include the character
** string required to activate the plugin in the description */
char    description[] = "Adds a %wp function to the screentitle to show all free pens.";

/* Name of the author of the plugin */
char    makername[] = "David McMinn, Stefan Sommerfield";


/* Our own version of the strlen function, to save having to link
** the standard libraries
*/
ULONG strlen(STRPTR s0)
{
	ULONG n=0;
	while(*s0++)
	{
		n++;
	}
	return(n);
}


/* The actual (hook) function which does the work for this plugin

	NAME
		FreePens

	SYNOPSIS
		success = FreePens(cls, object, message)
		D0                 A0   A2      A1

		LONG = FreePens(Class *, struct ScaRootList *, struct Title_Translate_Params *)

	FUNCTION
		To add the number of free pens to the title (bar) in Scalos

	INPUTS
		cls     - Pointer to the class from which this function was called
		object  - the ScaRootList pointer from Scalos
		message - the message being passed to our function that we must act on
		NB: These will always be the same except for the type of message that is
		passed. Check scalos/scalos.h for what will be passed in the message for
		a particular method.

	RESULT
		success - boolean to show success or failure.
		NB: This is different for different methods, so check what the return
		value should be for a particular method in scalos/scalos.h

	SEE ALSO
		scalos/scalos.h
*/
LONG SAVEDS FreePens(REG(a0) Class *cls, REG(a2) struct ScaRootList *object, REG(a1) struct Title_Translate_Params *message)
{
	Class                           *keepclass = cls;   /* a copy of the class just in case: register A0 is a scratch register remember */
	struct Title_Translate_Params   *msg = message;     /* a copy of the message just in case: register A1 is a scratch register remember */
	ULONG                           freepens = 0;       /* a count of the number of free pens */

	/* Make sure that the method being called is the title translate method */
	if(msg->MethodID == SCCM_Title_Translate)
	{
		/* also check that the parse characters we have been passed are the ones that our
		** function will deal with */
		if(msg->ParseID == PARSE)
		{
			/* if the pointer to the Scalos screen is valid, then work our
			** way through the vast amounts of pointers :) to get the
			** number of free pens in the screen
			*/
			if(object->rl_internInfos->ii_Screen != NULL)
				freepens = (ULONG)object->rl_internInfos->ii_Screen->ViewPort.ColorMap->PalExtra->pe_NFree;

			/* Add our text to the end of the title text*/
			sprintf(msg->TitleMem, "%lu", freepens);

			/* Advance the pointer to the title text to the new end
			** of the title text (add on the number of characters we have added
			*/
			msg->TitleMem += strlen(msg->TitleMem);

			/* return boolean success value. Note that this may differ
			** depending on what method you are dealing with
			*/
			return(1);
		}
	}

	/* Call superclass method if we have failed to do something (this method does not get processed by
	** this function, the parse characters were not what we were looking for, etc) */
	return(DoSuperMethodA(keepclass, (Object *)object, (Msg) msg));
}



/* structure which holds the data that Scalos needs for this class/plugin */
static struct ScaClassInfo ClassInfo =
{
	/* Next three lines constitute the Hook structure within this structure */
	0,0,            /* previous and successor pointers, set to NULL */
	(APTR)FreePens, /* pointer to hook function which does the real work in this plugin */
	0,0,            /* h_SubEntry and h_Data are 0 by default */

	0,              /* Priority of this class */
	0,              /* Instance size */
	39,             /* Required version of Scalos */
	0,              /* Reserved */
	superclassname, /* name of this class */
	superclassname, /* name of the superclass */
	name,           /* real name of the plugin */
	description,    /* description of plugin */
	makername       /* name of author */
};


/* The only public function in a plugin, it returns a pointer to the above
** ScaClassInfo structure
*/
struct ScaClassInfo * SAVEDS ASM GetClassInfo(void)
{
	return(&ClassInfo);
}


