/*
 * foo.c:
 *
 * Copyright (c) 2000 James McKenzie <james@fishsoup.dhs.org>.
 * All rights reserved.
 *
 * $Log: main.c,v $
 * Revision 1.37  2001/10/12 13:51:17  root
 * #
 *
 * Revision 1.36  2001/01/30 06:16:31  root
 * #
 *
 * Revision 1.35  2000/12/01 04:42:19  root
 * #
 *
 * Revision 1.34  2000/12/01 04:13:52  root
 * #
 *
 * Revision 1.33  2000/11/23 23:45:28  root
 * #
 *
 * Revision 1.32  2000/10/07 18:30:10  root
 * #
 *
 * Revision 1.31  2000/10/06 19:52:45  root
 * #
 *
 * Revision 1.30  2000/09/29 11:07:42  root
 * #
 *
 * Revision 1.29  2000/09/29 07:53:48  root
 * #
 *
 * Revision 1.28  2000/09/29 07:53:02  root
 * #
 *
 * Revision 1.27  2000/09/24 01:48:14  root
 * #
 *
 * Revision 1.26  2000/09/24 01:46:07  root
 * #
 *
 * Revision 1.25  2000/09/24 01:12:53  root
 * #
 *
 * Revision 1.24  2000/09/19 09:36:01  root
 * #
 *
 * Revision 1.23  2000/02/20 02:51:44  root
 * #
 *
 * Revision 1.22  2000/02/16 11:34:56  root
 * #
 *
 * Revision 1.21  2000/02/16 11:34:44  root
 * #
 *
 * Revision 1.20  2000/02/16 11:34:21  root
 * #
 *
 * Revision 1.19  2000/02/15 22:14:16  root
 * #
 *
 * Revision 1.18  2000/02/15 22:13:52  root
 * #
 *
 * Revision 1.17  2000/02/15 22:13:40  root
 * #
 *
 * Revision 1.16  2000/02/15 16:58:01  root
 * #
 *
 * Revision 1.15  2000/02/15 04:15:06  root
 * #
 *
 * Revision 1.14  2000/02/15 04:14:52  root
 * #
 *
 * Revision 1.13  2000/02/14 22:17:05  root
 * #
 *
 * Revision 1.12  2000/02/14 22:04:58  root
 * #
 *
 * Revision 1.11  2000/02/14 22:04:24  root
 * #
 *
 * Revision 1.10  2000/02/14 21:44:59  root
 * #
 *
 * Revision 1.9  2000/02/14 18:50:14  root
 * #
 *
 * Revision 1.8  2000/02/14 18:36:28  root
 * #
 *
 * Revision 1.7  2000/02/14 03:46:11  root
 * #
 *
 * Revision 1.6  2000/02/14 03:45:03  root
 * #
 *
 * Revision 1.5  2000/02/13 21:13:27  root
 * #
 *
 * Revision 1.4  2000/02/13 21:13:14  root
 * #
 *
 * Revision 1.3  2000/02/13 19:15:34  root
 * #
 *
 * Revision 1.2  2000/02/13 18:57:48  root
 * #
 *
 * Revision 1.1  2000/02/13 17:44:10  root
 * Initial revision
 *
 *
 */

static char rcsid[] =
  "$Id: main.c,v 1.37 2001/10/12 13:51:17 root Exp root $";

#include "project.h"
#include <Core/UI/ScrDriver.h>

void
dispatch_hook ()
{
  irda_dispatch ();
//      status_dispatch ();
}


DWord
PilotMain (Word cmd, Ptr cmdPBP, Word launchFlags)
{
  short err;
  EventType e;
  Word id;
  DWord width;
  DWord height;
  DWord depth;
  Boolean color;
  DWord two = 2;


  if (cmd)
    return 0;


  INIT_DEBUG;
  BREAKPOINT;

  preferences_init ();

  if (!serial_init ())
    return 0;

  hifi_me (HIFI_CLASS_DISPLAY, displayunit);

//   FrmAlert (SayHello);

  FrmGotoForm (SystemForm);

  ScrDisplayMode (scrDisplayModeGet, &width, &height, &depth, &color);

  while (1)
    {
      EvtGetEvent (&e, 10);
      dispatch_hook ();

      if (SysHandleEvent (&e))
        continue;
      if (MenuHandleEvent ((void *) 0, &e, &err))
        continue;


      switch (e.eType)
        {

        case frmLoadEvent:
          FrmSetActiveForm (FrmInitForm (e.data.frmLoad.formID));
          break;

        case frmOpenEvent:


          FrmDrawForm (FrmGetActiveForm ());

          break;

        case ctlSelectEvent:

          switch (e.data.ctlEnter.controlID)
            {
            case AboutDone:
              ScrDisplayMode (scrDisplayModeSet, &width, &height, &depth,
                              &color);
              FrmGotoForm (SystemForm);

              break;
            }
          FrmHandleEvent (FrmGetActiveForm (), &e);
          break;

        case menuEvent:
          switch (e.data.menu.itemID)
            {

            case MenuAboutId:
              ScrDisplayMode (scrDisplayModeSet, &width, &height, &two,
                              &color);
              FrmGotoForm (AboutForm);
              break;
            case MenuPrefId:
              FrmGotoForm (PrefForm);
              break;
            case MenuFavId:
              FrmGotoForm (FavForm);
              break;


            }

          break;

        case appStopEvent:
          serial_done ();
          mpeg_do_cleanup ();
          wave_do_cleanup ();
          ScrDisplayMode (scrDisplayModeSet, &width, &height, &depth, &color);
          return 0;

        case popSelectEvent:

          switch (e.data.popSelect.controlID)
            {
              /*Dispatchers called from tab event rt */
            case FavPop:
            case MpegFavrPop:
            case MpegHistPop:


              break;
            default:
              FrmHandleEvent (FrmGetActiveForm (), &e);
            }
          break;
        default:
          FrmHandleEvent (FrmGetActiveForm (), &e);
        }
      id = FrmGetActiveFormID ();

      switch (id)
        {
        case AboutForm:
          break;
        case PrefForm:
          preferences_dispatch (&e);
          break;
        case FavForm:
          favorite_dispatch (&e);
          break;
        default:

          TabDispatch (&e);


        }
    }
}
