/*
** libfuncsc
** 21st 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 <powerup/proto/ppc.h>
#include <powerup/ppclib/ppc.h>

#include <scalos/scalos.h>

#include <utility/hooks.h>
#include <utility/tagitem.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 0x70 followed by
** 0x70 which corresponds to p and p. This means that to
** activate this plugin you need to have "%pp" entered
** somewhere in the title string in the Scalos main prefs.
** Same also applies for the other placeholder "%pc".
*/
#define PARSE1   0x7070  // %pp
#define PARSE2   0x7063  // %pc


/* 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 parse 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 PPCProcessor";

/* Small description of what the plugin does - you should include the character
** string required to activate the plugin in the description */
char    description[] = "Adds PPC processor attributes %pp=CPU %pc=CPUClock.";

/* Name of the author of the plugin */
char    makername[] = "David McMinn [dave@satanicdreams.com]";


/* 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
		Title PPC Processor

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

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

	FUNCTION
		To add the PPC processor type 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 PPCType(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                           ppctype = 0;        /* the type of ppc processor */
	ULONG                           ppcclock = 0;       /* the ppc CPU clock (in MHz) */
	struct TagItem                  ppcattr;            /* used to pass what we are after to the ppclib */

	/* Make sure that the method being called is the title translate method */
	if(msg->MethodID == SCCM_Title_Translate)
	{
		/* if the ppc.library was opened, then we get type ppc processor stats*/
		if(PPCLibBase)
		{
			/* also check that the parse characters we have been passed are the ones that our
			** function will deal with */
			switch(msg->ParseID)
			{

				case PARSE1:
					ppcattr.ti_Tag = PPCINFOTAG_CPU;
					ppctype = PPCGetAttrs(&ppcattr);

					switch(ppctype)
					{
						case 0:
							sprintf(msg->TitleMem, "%s", "None");
							break;

						case CPU_603:
							sprintf(msg->TitleMem, "%s", "603");
							break;

						case CPU_604:
							sprintf(msg->TitleMem, "%s", "604");
							break;

						case CPU_602:
							sprintf(msg->TitleMem, "%s", "602");
							break;

						case CPU_603e:
							sprintf(msg->TitleMem, "%s", "603e");
							break;

						case CPU_603p:
							sprintf(msg->TitleMem, "%s", "603p");
							break;

						case CPU_604e:
							sprintf(msg->TitleMem, "%s", "604e");
							break;
					}

					/* 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);
					break;

				case PARSE2:
					ppcattr.ti_Tag = PPCINFOTAG_CPUCLOCK;
					ppcclock = PPCGetAttrs(&ppcattr);

					sprintf(msg->TitleMem, "%ld", ppcclock);

					/* 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);
					break;

			} //end-switch
		}
	}

	/* 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)PPCType,  /* 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);
}


