[net.micro.amiga] ICONEXEC utility

jcz@ncsu.UUCP (02/21/86)

Here are two simple utilities that may make it easier to write code that
runs from workbench.  The second utility IconExec allows you to run any
arbitrary command from an Icon WITHOUT having to recompile a program
everytime you wish to change what is run.  The other utility allows you
to set the default window that will be used when an Icon is selected.

As a matter of short explaination, whenever an Icon is selected, the
ToolWindow field is (supposed to be) copied into the initial startup
message that is passed to your program.  The startup code (c.o) looks
at that message and opens up that window by default.  In this way you can
always associate a window with a program without having to open it in
your code.  Unfortunately, Intuition (or whomever is responsible) fails
to copy the ToolWindow field over and no window is opened currently.
ICONEXEC takes that into account and opens up the window anyway (Until
the parameter gets copied correctly in a later OS release) and sets it
as the default window.

Once a window is established, the commands that are contained in the
ToolTypes arrays are then executed one at a time.  More detailed information
about how the Icons are organizied is in Chapter 4 of Volume1 in the 1.1
Rom Kernal Manual, although the information is not critical to using
the program.  If enough interest exists, I will create a tutorial on
using Icons or whatever other information is desired in creating/accessing
them from a program.

These programs were created in response to a request for being able to run
programs such as MUSICRAFT which have no ICONS without Having to modify
the program.  If you plan on using ICONEXEC for a commercial product, just
contact me first.
-----cut here for SetWindow.c------------
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* SetWindow - Set the ToolWindow for an ICON                            */
/* (c) Copyright 1986 John A. Toebes, VIII   All rights reserved         */
/*     120-H Northington Place,   Cary NC 27511   (919) 469-4210         */
/*  This program may be used and modified for any purpose so long as     */
/*  this copyright notice remains with the code and the program is not   */
/*  sold and no charge is made for its use.                              */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* To use:
   1) Compile and link as any other program
   2) Issue command to modify whatever Icon you wish to
        SetWindow <icon> <window specifications>
*/

#include <stdio.h>
#include <exec/types.h>
#include <workbench/workbench.h>
#include <workbench/icon.h>
#include <workbench/startup.h>

int IconBase;
extern struct WBStartup *WBenchMsg;

main(argc,argv)
int argc;
char *argv[];
{
	struct DiskObject *diskobj, *GetDiskObject();
	char *savewindow;

	/* make sure we ran from CLI */
	if (argc == 0) exit(1);

	/* make sure we have enough parms */
	if (argc != 2 && argc != 3)
		{
		printf("Usage: %s <icon> [<windowspec>]\n", argv[0]);
		exit(2);
		}

	if ( (IconBase = OpenLibrary( ICONNAME, 1)) == NULL)
		exit(2);

	/* get the icon from disk */
	if ( (diskobj = GetDiskObject(argv[1])) == NULL)
		{
		printf("Cannot read icon for %s\n", argv[1]);
		CloseLibrary( IconBase );
		exit(3);
		}

	/* remember what the previous window was */
	savewindow = diskobj->do_ToolWindow;

	/* set it to what was requested */
	diskobj->do_ToolWindow = (argc > 2) ? argv[2] : NULL;

	/* write the corrected ICON to disk */
	if (!PutDiskObject( argv[1], diskobj))
		printf("Cannot write new icon - code %d\n", IoErr() );

	/* restore the oldwindow and free the object */
	diskobj->do_ToolWindow = savewindow;
	FreeDiskObject( diskobj );

	CloseLibrary( IconBase );
}
-----end of SetWindow.c-------------
-----cut here for IconExec.c------------
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* ICONEXEC (c) Copyright 1986 John A. Toebes, VIII. All rights reserved */
/*  This program may be used and modified for any purpose so long as     */
/*  this copyright notice remains with the code and the program is not   */
/*  sold and no charge is made for its use.  For permission to           */
/*  incorporate this into a commercial product, contact the author:      */
/*    John A. Toebes, VIII                                               */
/*    120 H Northington Place                                            */
/*    Cary NC 27511                                                      */
/*    (919) 469-4210                                                     */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-----------------------------------------------------------------------*/
/* This program allows you to associate any series of CLI commands to be */
/* run when you click on an ICON even though there is no CLI window open */
/* One problem associated with it is that a program that does input from */
/* the default input (*) always sees EOF.  I am researching why this     */
/* happens, but believe it is related to an AmigaDos problem.            */
/*-----------------------------------------------------------------------*/

/****************
  To compile this program under Lattice C:
    1) Compile the 
	LC1 -oRAM: -i<includedir> ICONEXEC
	LC2 -v -oICONEXEC.O RAM:ICONEXEC

    2) Create a file 'link_iconexec' with the linker commands (assuming 
       that df0:lib contains the appropriate linking modules):
	FROM     df0:lib/c.o+*
	IconExec.o
	TO       IconExec
	LIBRARY  df0:lib/lc.lib+df0:lib/amiga.lib
	MAP      nil:

    3) link it using alink
	ALINK WITH LINK_ICONEXEC

   To create and execute a user customized Icon Exec

    1) Create an Icon (copy the CLI Icon) for IconExec

    2) Run the SetWindow program to set a the tool window on the Icon
	SETWINDOW ICONEXEC "CON:0/0/100/100/MYWINDOW"
       If you do not do this, then by default ICONEXEC will open a full
       screen window with a title of "ICONEXEC by John A. Toebes, VIII"

    3) Using the Workbench INFO meno option, enter the commands into the
       tool types window.

    4) Click on the Icon and watch it go.
**********/

#include <stdio.h>
#include <exec/types.h>
#include <workbench/workbench.h>
#include <workbench/icon.h>
#include <workbench/startup.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>

#define DEFAULT_WINDOW "CON:0/1/640/199/ICONEXEC by John A. Toebes, VIII"

LONG IconBase;
extern struct WBStartup *WBenchMsg;

_main(line)
char *line;
{
	char **tools;
	char *thistool;
	struct WBArg *wbarg;
	struct DiskObject *diskobj, *GetDiskObject();
	int olddir;
	char *window;
	struct FileHandle *handle;
	BPTR file;
	struct Process *process;

	if ( (IconBase = OpenLibrary( ICONNAME, 1) ) == NULL)
		{
		diag("Cannot open Icon Library", NULL);
		_exit(2);
		}

	/* Locate the first argument passed from workbench */
	wbarg	= WBenchMsg->sm_ArgList;

	/* change to its home dir */
	olddir	= CurrentDir( wbarg->wa_Lock );

	/* read in its disk object structure */
	diskobj	= GetDiskObject( wbarg->wa_Name );

	/* did we have a default tool window opened for us ? */
	if (WBenchMsg->sm_ToolWindow == NULL)
		{
		/* figure out the window to open */
		if ( (window = diskobj->do_ToolWindow) == NULL)
			window = DEFAULT_WINDOW;

		/* open the desired window if we can */
		if ( (file = Open(window,MODE_OLDFILE)) == NULL)
			{
			diag("Cannot open Icon Window", window);
			CurrentDir( olddir );
			CloseLibrary( IconBase );
			_exit(4);
			}

		/* fool system into thinking we have a CLI based window */
		handle = (struct FileHandle *)(file << 2);
		process = (struct Process *)FindTask(0);
		process->pr_ConsoleTask = (APTR)handle->fh_Type;
		}
	else
		file = Output();

	/* run through the tools and execute them one at a time */
	for (tools = diskobj->do_ToolTypes; thistool=*tools; tools++)
		Execute(thistool, NULL, NULL);

	/* close down the window */
	if (WBenchMsg->sm_ToolWindow == NULL)
		Close(file);

	/* restore the previous directory */
	CurrentDir( olddir );
	CloseLibrary( IconBase );
	_exit(0);
}

diag(msg, parm)
char *msg;
char *parm;
{
	BPTR logfile;
	char c;

	logfile = Open("con:0/0/50/200/ICONEXEC Error", MODE_OLDFILE);
	Write(logfile, "ICONEXEC Error:\n", 16);
	Write(logfile, msg, strlen(msg));
	if (parm != NULL)
		Write(logfile, parm, strlen(parm));
	Write(logfile, "Press <RETURN> to continue\n", 27);
	Read(logfile, &c, 1);
	Close(logfile);
}
-----end of IconExec.c

Good Luck,

John A. Toebes, VIII       through ...!mcnc!ncsu!jcz
120 H Northington Place
Cary NC 27511              (919)-469-4210

Disclaimer: My Amiga thinks it's an A.I. machine and wrote the above itself