/* this is another example of a synchronous tool.
   just for your examination.

   of interest is the letemknow function
   also shows how to hide and select polys

   use mirror.c as a template for your synchronous tools
*/

/* some general includes. We tried to get all you would normally need
   change to fit your tool if needed.
*/
#include "exec/types.h"

/* these two statements allow more efficient allocmems, see
   SC readme for 6.0
   DON'T call pragmas/exec_pragmas.h
*/

#define __USE_SYSBASE
#include <proto/exec.h>

#include "stdio.h"
#include "string.h"
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/libraries.h>
#include <intuition/intuition.h>
#include <graphics/text.h>
#include <proto/graphics.h>
#include <proto/intuition.h>
#include "exec/io.h"
#include "exec/memory.h"
#include "dos.h"
#include "libraries/dosextens.h"
#include "libraries/dos.h"
#include "libraries/filehandler.h"
#include "devices/inputevent.h"
#include "graphics/gfxmacros.h"

#include <graphics/gfx.h>
#include <hardware/blit.h>
#include <graphics/copper.h>
#include <graphics/view.h>
#include <graphics/rastport.h>
#include <graphics/gels.h>
#include <graphics/regions.h>
#include <graphics/clip.h>
#include <exec/exec.h>
#include <graphics/gfxbase.h>
#include <m68881.h>

#include "math.h"
#include "float.h"
#include "limits.h"
#include "proto/dos.h"
#include "stdlib.h"
#include "workbench/workbench.h"
#include "workbench/startup.h"
#include "proto/icon.h"
#include "time.h"

/* for our run time libraries */
#include "alad_pragmas.h"
#include "alad_protos.h"

#include "defines.h"
#include "externs.h"

int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
void chkabort __ARGS((void)) { return; }  /* really */
void Chk_Abort __ARGS((void)) { return; } /* really */

static void getdefaults(int);
void selectpolys(void);

struct Library *AladBase;
struct IntuitionBase *IntuitionBase;
struct SignalSemaphore *polysema;

int main(int,char **);
int getselectsamdef(void);

int main(argc,argv)
int argc;
char **argv;
{
 int t;

 /* open necessary libraries */
 IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",33);
 if(IntuitionBase == NULL) return(-1);
 AladBase = OpenLibrary("Aladdin4D:support/alad.library",0);
 if(AladBase == NULL) {
  CloseLibrary((struct Library *)IntuitionBase);
  return(-1);
 }
 /* find out if polysema exists */
 Forbid();
 polysema = FindSemaphore("Aladdin4DSemaphore");
 Permit();
 if(!polysema) {
  CloseLibrary((struct Library *)IntuitionBase);
  CloseLibrary(AladBase);
  return(-1);
 }
 /* load last user choices */
 getdefaults(0);
 /* Part of what Aladdin has stored in the hardware key
    is the user request
    use readHSLdevice() to get it
    if it returns a 1, user clicked the tool with a LMB
    if it returns a 2, user clicked the tool with a RMB
 */
 t = readHSLdevice();
 if(t == 1) { /* LMB */
  /* if user used a left mouse button, just do the work */
  selectpolys();
 } else if(t == 2) { /* RMB */
  /* if user used a right mouse button, open the defaults window
     and if he hits the perform gadget, do the work
  */
  if(getselectsamdef()) selectpolys();
  getdefaults(1);
 }
 /* finally cleanup and return
    ALWAYS return 0
 */
 CloseLibrary((struct Library *)IntuitionBase);
 CloseLibrary(AladBase);
 return(0);
}

/* these are the variables the user can change */
long attlists,bmlists,paths,lines,complex,hide,deselect;

static void getdefaults(int sav)
{
 /* establish a defaults file
    if program run before, get them
    if first time, file won't exist, so return
 */
 LONG file;
 char *nam = "aladdin4ddef:{ALAD}_selectsam.def";
 double version;

 if(sav) {
  file=Open(nam,MODE_NEWFILE);
  if(file==0) { return; }
  version = 3.0;
  Write(file,(BYTE *)&version,sizeof(version));
  Write(file,(BYTE *)&attlists,sizeof(attlists));
  Write(file,(BYTE *)&bmlists,sizeof(bmlists));
  Write(file,(BYTE *)&paths,sizeof(paths));
  Write(file,(BYTE *)&lines,sizeof(lines));
  Write(file,(BYTE *)&complex,sizeof(complex));
  Write(file,(BYTE *)&hide,sizeof(hide));
  Write(file,(BYTE *)&deselect,sizeof(deselect));
 } else {
  file=Open(nam,MODE_OLDFILE);
  if(file==0) { return; }
  Read(file,(BYTE *)&version,sizeof(version));
  if(version == 3.0) {
   Read(file,(BYTE *)&attlists,sizeof(attlists));
   Read(file,(BYTE *)&bmlists,sizeof(bmlists));
   Read(file,(BYTE *)&paths,sizeof(paths));
   Read(file,(BYTE *)&lines,sizeof(lines));
   Read(file,(BYTE *)&complex,sizeof(complex));
   Read(file,(BYTE *)&hide,sizeof(hide));
   Read(file,(BYTE *)&deselect,sizeof(deselect));
  }
 }
 Close(file);
}

void selectpolys()
{
 struct PolygonInfo *top;
 short selected;
 struct attlist *atl,*oatl;
 struct BMList *bml,*obml;
 struct PathInfo *path,*opath;
 short diffatl,diffbml,diffpath;

 /* if user only wants to select complex polys, or lines,
    don't need a selected poly
    otherwise, make sure all selected polys have same as
    requested compares
 */
 if(attlists|bmlists|paths) {
  diffatl = diffbml = diffpath = -1;
  bml = obml = NULL;
  atl = oatl = NULL;
  path = opath = NULL;
  selected = 0;
  ObtainSemaphore(polysema);
  for(top=firstpoly();top;top=top->next) {
   if(ISSELECTED(top)) {
    selected++;
    if(attlists) {
     atl = top->atl;
     if(atl != oatl) { oatl = atl; diffatl++; }
    }
    if(bmlists) {
     bml = top->bml;
     if(bml != obml) { obml = bml; diffbml++; }
    }
    if(paths) {
     path = top->path;
     if(path != opath) { opath = path; diffpath++; }
    }
   }
  }
  ReleaseSemaphore(polysema);
  if(!selected) { letemknow("Compare requires selected polys"); return; }
  if(attlists) {
   if(diffatl > 0) { letemknow("Polys have different Attribute Lists"); return; }
  }
  if(bmlists) {
   if(diffbml > 0) { letemknow("Polys have different Bitmap Lists"); return; }
  }
  if(paths) {
   if(diffpath > 0) { letemknow("Polys have different Paths"); return; }
  }
 }
 ObtainSemaphore(polysema);
 for(top=firstpoly(); top; top=top->next) {
  if(ISSELECTED(top)) { if(deselect) UNSELECT(top); }
  if(attlists) {        if(top->atl == atl) SELECT(top); }
  if(bmlists) {         if(top->bml == bml) SELECT(top); }
  if(paths) {           if(top->path == path) SELECT(top); }
  if(lines) {           if(top->points < 3) SELECT(top); }
  if(complex) {         if(top->points > 4) SELECT(top); }
  if(hide) {            if(!(ISSELECTED(top))) HIDE(top); }
 }
 ReleaseSemaphore(polysema);
}
