[comp.sys.amiga.tech] Finding Windows

elrick@physics.utoronto.ca (Bruce Elrick) (07/20/90)

  Question:

If I open a window using the dos Open() command, how can I find the
window structure (for, say, Windowtofront()) from the filehandle?

peterk@cbmger.UUCP (Peter Kittel GERMANY) (07/20/90)

In article <1990Jul19.184648.11351@helios.physics.utoronto.ca> elrick@physics.utoronto.ca (Bruce Elrick) writes:
>If I open a window using the dos Open() command, how can I find the
>window structure (for, say, Windowtofront()) from the filehandle?

I only know a dirty way that won't be totally waterproof:
If the window is on Workbench then find Intuitionbase, scan the pointer
chain of screens for the Workbench screen and take the pointer chain for
windows inside this screen to search for your one. To identify your
window you could search for your UNIQUE window title string. And this
is already the caveat: You must be ABSOLUTELY sure that no other window
on this screen has the same title string. As I said, a rather dirty way.


-- 
Best regards, Dr. Peter Kittel      //     E-Mail to 
Commodore Frankfurt, Germany      \X/      rutgers!cbmvax!cbmger!peterk

daveh@cbmvax.commodore.com (Dave Haynie) (07/20/90)

In article <1990Jul19.184648.11351@helios.physics.utoronto.ca> elrick@physics.utoronto.ca (Bruce Elrick) writes:
>
>  Question:

>If I open a window using the dos Open() command, how can I find the
>window structure (for, say, Windowtofront()) from the filehandle?

Here's about the best I've done.  I don't how you can get a window directly
from a filehandle, since a file isn't necessarily associated with any 
window.  However, you can get a window pointer if you know the console
associated with that window.  Here's an excerpt from SetCPU V2.7 that does
this:


// =======================================================================

// From SetFont 2.7 - by Dave Haynie

#include <exec/types.h>
#include <exec/io.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <graphics/gfxbase.h>
#include <graphics/text.h>
#include <graphics/rastport.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <intuition/intuition.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

// =========================================================================

// Miscellaneous stuff.

inline MsgPort *contask(MsgPort *port) {
   return (MsgPort *) ((port->mp_SigTask->ln_Type == NT_PROCESS)
          ? ((Process *)port->mp_SigTask)->pr_ConsoleTask 
          : NULL);
}

// =========================================================================
 
// These classes manage the places that fonts are hidden.

// This is the basic place node

class PlaceNode : public Node {
   private:
      static Window *pwin;
      static Screen *pscr;

   public:
      PlaceNode();      
      PlaceNode *next() { return (PlaceNode *)Node::next(); }

      void testwindow() { if (!pwin) fail("Window not found\n"); }
      void testscreen() { if (!pscr) fail("Screen not found\n"); }

      Window *window()  { return pwin; }
      Screen *screen()  { return pscr; }

      virtual void set(SmartFont *f) {
         testwindow();
         SetFont(window()->graphic(),f->font());
         printf("\033c");  			// Re-init window's conunit
         (void)flushall();
      }
};

// Initialize the static stuff, once.

PlaceNode::PlaceNode() {
   if (pwin) return;

   StandardPacket *packet = new StandardPacket;
   InfoData *info = new InfoData;   
   MsgPort *port = new StdPort;

  // Find the window
   if (contask(port)) {
      packet->sendio(contask(port),port,ACTION_DISK_INFO,CADDR(info));
      (void)port->wait();
      pwin = (Window *)info->id_VolumeNode;
   } else 
      pwin = NULL;
   delete port;
   delete info;
   delete packet;

  // Find the screen
   pscr = (pwin) ? pwin->screen() : NULL;
}

Or, in english, if you send the ACTION_DISK_INFO packet to a console, the
console's window is returned as the id_VolumeNode field of the InfoData
structure.  You can't the console task from a FileHandle, but you can
get it from a FileLock.  Note that the Lock() function returns a BPTR to
a FileLock, so you'll have to convert this into a 680x0 pointer before
examining the fields.
-- 
Dave Haynie Commodore-Amiga (Amiga 3000) "The Crew That Never Rests"
   {uunet|pyramid|rutgers}!cbmvax!daveh      PLINK: hazy     BIX: hazy
	"I have been given the freedom to do as I see fit" -REM

deven@rpi.edu (Deven T. Corzine) (07/20/90)

In article <1990Jul19.184648.11351@helios.physics.utoronto.ca>
   elrick@physics.utoronto.ca (Bruce Elrick) writes:

Bruce> If I open a window using the dos Open() command, how can I find the
Bruce> window structure (for, say, Windowtofront()) from the filehandle?

On 20 Jul 90 07:18:22 GMT, peterk@cbmger.UUCP (Peter Kittel GERMANY) said:

Peter> I only know a dirty way that won't be totally waterproof: If
Peter> the window is on Workbench then find Intuitionbase, scan the
Peter> pointer chain of screens for the Workbench screen and take the
Peter> pointer chain for windows inside this screen to search for your
Peter> one. To identify your window you could search for your UNIQUE
Peter> window title string. And this is already the caveat: You must
Peter> be ABSOLUTELY sure that no other window on this screen has the
Peter> same title string. As I said, a rather dirty way.

You're right, that is a rather dirty way.  There is, however, a better
way.  You can send an ACTION_DISK_INFO packet to the CON: handler and
it will return the address of the window in the Info structure.

Here's a routine which will return a Window pointer to the current
"console" window the one referred to by "*":
--------
/* findwindow.c - utility routine to find window of a CLI. */

#include <exec/types.h>
#include <exec/memory.h>
#include <proto/exec.h>
#include <proto/dos.h>

struct Window __regargs *FindWindow()
{
   register struct DosLibrary *DOSBase;
   register struct Window *win;
   register struct Process *proc;
   register struct CommandLineInterface *cli;
   register struct InfoData *id;
   register struct StandardPacket *pkt;
   register struct FileHandle *fh;
   register BPTR file;
   register long ret1,ret2;

   if (DOSBase=(struct DosLibrary *) OpenLibrary(DOSNAME,0)) {
      if (id=(struct InfoData *)
        AllocMem(sizeof(struct InfoData),MEMF_PUBLIC|MEMF_CLEAR)) {
         if (pkt=(struct StandardPacket *)
           AllocMem(sizeof(struct StandardPacket),MEMF_PUBLIC|MEMF_CLEAR)) {
            proc=(struct Process *) FindTask(NULL);
            if (cli=(struct CommandLineInterface *) (proc->pr_CLI<<2)) {
               ret1=cli->cli_ReturnCode;
               ret2=cli->cli_Result2;
               if (file=Open("*",MODE_NEWFILE)) {
                  if (IsInteractive(file)) {
                     pkt->sp_Msg.mn_Node.ln_Name=(char *) &(pkt->sp_Pkt);
                     pkt->sp_Pkt.dp_Link=&(pkt->sp_Msg);
                     pkt->sp_Pkt.dp_Port=&(proc->pr_MsgPort);
                     pkt->sp_Pkt.dp_Type=ACTION_DISK_INFO;
                     pkt->sp_Pkt.dp_Arg1=((ULONG) id)>>2;
                     fh=(struct FileHandle *) (file<<2);
                     PutMsg(fh->fh_Type,(struct Message *) pkt);
                     WaitPort(&(proc->pr_MsgPort));
                     GetMsg(&(proc->pr_MsgPort));
                     win=(struct Window *) id->id_VolumeNode;
                  }
                  Close(file);
               }
               cli->cli_Result2=ret2;
               cli->cli_ReturnCode=ret1;
            }
            FreeMem(pkt,sizeof(struct StandardPacket));
         }
         FreeMem(id,sizeof(struct InfoData));
      }
      CloseLibrary((struct Library *) DOSBase);
   }
   return(win);
}
--------

Enjoy.

Deven
-- 
Deven T. Corzine        Internet:  deven@rpi.edu, shadow@pawl.rpi.edu
Snail:  2214 12th St. Apt. 2, Troy, NY 12180   Phone:  (518) 271-0750
Bitnet:  deven@rpitsmts, userfxb6@rpitsmts     UUCP:  uunet!rpi!deven
Simple things should be simple and complex things should be possible.

deven@rpi.edu (Deven T. Corzine) (07/22/90)

On 20 Jul 90 15:38:41 GMT, daveh@cbmvax.commodore.com (Dave Haynie) said:

Dave> Here's an excerpt from SetCPU V2.7 that does this:
                             ^^^^^^^^^^^
Dave> // From SetFont 2.7 - by Dave Haynie
              ^^^^^^^^^^^

Freudian slip, Dave?  :-)

Deven
-- 
Deven T. Corzine        Internet:  deven@rpi.edu, shadow@pawl.rpi.edu
Snail:  2214 12th St. Apt. 2, Troy, NY 12180   Phone:  (518) 271-0750
Bitnet:  deven@rpitsmts, userfxb6@rpitsmts     UUCP:  uunet!rpi!deven
Simple things should be simple and complex things should be possible.