/*////////////////////////////////////////////////////////////////////

 Created by Johan Sandgren in 1995-11-14, sandis@df.lth.se
 using GoldED3.1.3/DEMOŠ'95 Dietmar Eilert
       (folding!)

 Description
 -----------
 Connect things to the mouse position.


 Requirements
 ------------
 V36 or higher


 History
 -------

 V1.0   1995-07-30
        Plain, just snapshots MouseX & MouseY and sets globally
        in 2 env-variables {MouseX, MouseY}.

 V1.1   1995-11-14
        MouseX & MouseY can be offset as an option. An optional
        commandstring can be executed before exit.

 V1.2   1995-11-27, First public Release.

        Uses SetVar locally instead. Solves problem with Command
        string containing env-vars that was not up to date.

        <c:Mousepos command="echo $mousex"> did not work correctly.
        Since $mousex was a global env-variable, "echo $mousex" was
        pre-interpreted by CLI(?) to the current value before sent
        along to Mousepos who updated it too late.

        This is only the case for global-env varibles. Local varibles
        can only be interpreted if they exist. With local envs, they
        are created in runtime of Mousepos.

*/////////////////////////////////////////////////////////////////////

//( Includes & globals

#include <proto/exec.h>
#include <proto/intuition.h>
#include <intuition/intuition.h>
#include <proto/dos.h>
#include <dos/rdargs.h>

#include <string.h>

char version[]="$VER: MousePos 1.2 by sandis@df.lth.se==Johan Sandgren (1995-11-27)";
struct IntuitionBase * IntuitionBase;

//)

//( BOOL GetMousePos(STRPTR pubscreen, LONG * xoffset, LONG * yoffset)
/*//////////////////////////////////////////////////////////////

o try locking preferred screen, get mousecoords,
o offset it, put result in env variables

RESULT: TRUE if LockPubScreen(...) succeeds, else FALSE

*///////////////////////////////////////////////////////////////



BOOL GetMousePos(LONG * xoffset, LONG * yoffset)
{
 struct Screen * scr = LockPubScreen(NULL);
 if(scr)
 {
  WORD x,y;
  char xt[8], yt[8];

  x=scr->MouseX;
  y=scr->MouseY;

  UnlockPubScreen(NULL, scr);

  if(xoffset)
    x+=(WORD)*xoffset;
  if(yoffset)
    y+=(WORD)*yoffset;

  stci_d(xt, x);
  stci_d(yt, y);

  SetVar("MouseX", xt, -1, GVF_LOCAL_ONLY);
  SetVar("MouseY", yt, -1, GVF_LOCAL_ONLY);
  return TRUE;
 }
 return FALSE;
}
//)

//( main
/*//////////////////////////////////////////////////////////////

o read commandline arguments {script, screen, XOffset, YOffset}
o call GetMousePos(...)
o execute command

*///////////////////////////////////////////////////////////////



void main()
{
 DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",36L);
 if(DOSBase)
 {
  IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",36L);
  if(IntuitionBase)
  {
   LONG array[3] = {NULL, NULL, NULL};
   char * txt = "Cmd/K,XOffset/K/N,YOffset/K/N";

   struct RDArgs * args = ReadArgs(txt, array, NULL);
   BOOL grabbed = GetMousePos((LONG *)array[1], (LONG *)array[2]);

   if(grabbed)
    if(array[0])
     Execute((STRPTR)array[0], NULL, Output());

   FreeArgs(args);
   CloseLibrary((struct Library *)IntuitionBase);
  } else PutStr("Need intuition.library V36\n");
  CloseLibrary((struct Library *)DOSBase);
 } else PutStr("Need dos.library V36\n");
}
//)

