AppMain.cpp

       1  /******************************************************************************
           *
           * Palm Interactive Boardroom (  PIB )
           * Handheld presentation tool using a networked projector as a display.
           *
           * This source code is based on default IDE source as well as royalty-free
           * samples provided with the IDE. This file shell was generated by the Palm OS
           * Developer Suite IDE.
           *
           * Other royalty-free source code from HelloNetLib.c (  provided by
           * Palm at www.palmsource.com ) to send TCP and UDP traffic to an IP
           * address + port number was modified significanlty in support of
           * the PIB requirements.
           *
           * Programming environment: Palm OS Developer Suite
           *
           * Created by Scott Armstrong for UIUC CS-427 Group 30,   Fall 2005
           * based on UML and requirements outlined by Group 30 members.
           *
           * File: AppMain.cpp
           *
           * Notes:
           * - This version is based on a C to C++ conversion.
           * - As of now,   only the GUI is updated.
           * - This update mainly designed as a "Let everyone know what's going on
           * with the GUI in C++.
           *
           * Version 4.0.0
           * - Work on integrating "Transfer" function set (  Project )
           * - "Connect" (  connect to network ) is automatically started on the device
           * based on a network call - an interactive function will be integrated
           * in future iterations.
           * - Issues connecting the Button trigger event to the Transfer form
           *
           *****************************************************************************/
          #include <PalmOS.h>
          
          #include "AppResources.h"
          #include "UIControl.h"
          //#include "NetworkControl.h"
          
          
          /***********************************************************************
           *
           * Entry Points
           *
           ***********************************************************************/
          
          
          /***********************************************************************
           *
           * Internal Constants
           *
           ***********************************************************************/
          #define appFileCreator 'xPIB' // register your own at http://www.palmos.com/dev/creatorid/
          #define appVersionNum 0x01
          #define appPrefID 0x00
          #define appPrefVersionNum 0x01
          
          /***********************************************************************
           *
           * Internal Functions
           *
           ***********************************************************************/
          
          
          /***********************************************************************
           *
           * FUNCTION: MainFormDoCommand
           *
           * DESCRIPTION: This routine performs the menu command specified.
           *
           * PARAMETERS: command - menu item id
           *
           * RETURNED: nothing
           *
           * REVISION HISTORY: 4.0
           *
           *
           ***********************************************************************/
      81  static Boolean MainFormDoCommand(  UInt16 command )
          {
           bool handled = false;
           FormType * pForm;
          
           switch (  command ) {
           case MainOptionsAboutStarterApp:
           pForm = FrmInitForm(  AboutForm );
           FrmDoDialog(  pForm ); // Display the About Box.
           FrmDeleteForm(  pForm );
           handled = true;
           break;
           case MainOptionsSelectFileStarterApp:
           pForm = FrmInitForm(  SelectFileForm );
           FrmDoDialog(  pForm ); // Display the Select File Box.
           FrmDeleteForm(  pForm );
           handled = true;
           break;
           case MainOptionsTransferSettingsStarterApp:
           //pForm = FrmInitForm(  MainTransferForm );
           FrmGotoForm(  MainTransferForm );
           //FrmDoDialog(  pForm ); // Display the Transfer Video Box.
           //FrmDeleteForm(  pForm );
           handled = true;
           break;
          
           }
          
           return handled;
          }
          
          
     113  static Boolean TransferFormHandleEvent(  EventType* pEvent )
          {
           Boolean handled = false;
           FormType* pForm;
          
           switch(  pEvent->eType )
           {
           case menuEvent:
           return MainFormDoCommand(  pEvent->data.menu.itemID );
          
           case frmOpenEvent:
           pForm = FrmGetActiveForm(   );
           FrmDrawForm(  pForm );
           handled = true;
           break;
           case ctlSelectEvent: //if a button was pressed
           switch (  pEvent->data.ctlSelect.controlID )
           { //find which button and act accordingly
           case MainSendOKButton:
           FrmGotoForm(  MainForm );
           handled = true;
           break;
          
           case MainSendTCPButton:
           break;
           default:
           break;
           }
           default:
           break;
           }
           return handled;
          
          
          }
          
          
          
          
          
          /***********************************************************************
           *
           * FUNCTION: MainFormHandleEvent
           *
           * DESCRIPTION: This routine is the event handler for the
           * "MainForm" of this application.
           *
           * PARAMETERS: pEvent - a pointer to an EventType structure
           *
           * RETURNED: true if the event has handle and should not be passed
           * to a higher level handler.
           *
           * REVISION HISTORY:
           *
           *
           ***********************************************************************/
     169  static Boolean MainFormHandleEvent(  EventType* pEvent )
          {
           bool handled = false;
           FormType* pForm;
          
           switch (  pEvent->eType ) {
           case menuEvent:
           return MainFormDoCommand(  pEvent->data.menu.itemID );
          
           case frmOpenEvent:
           pForm = FrmGetActiveForm(   );
           FrmDrawForm(  pForm );
           handled = true;
           break;
          
           //Here we look at which button was pressed in the mainForm
           //the IDs are defined in AppResources.h
           case ctlSelectEvent:
           switch (  pEvent->data.ctlSelect.controlID )
           {
           case NavigateBackButton:
           break;
          
           case NavigateNextButton:
           break;
          
           case ConnectNetworkButton:
           FrmGotoForm(  ConnectForm );
           break;
          
           case TransferVideoButton:
           break;
           }
          
           default:
           break;
           }
          
           return handled;
          }
          
          
          /***********************************************************************
           *
           * FUNCTION: AppHandleEvent
           *
           * DESCRIPTION: This routine loads form resources and set the event
           * handler for the form loaded.
           *
           * PARAMETERS: event - a pointer to an EventType structure
           *
           * RETURNED: true if the event has handle and should not be passed
           * to a higher level handler.
           *
           * REVISION HISTORY:
           *
           *
           ***********************************************************************/
     227  static Boolean AppHandleEvent(  EventType* pEvent )
          {
           UInt16 formId;
           FormType* pForm;
           bool handled = false;
          
           if (  pEvent->eType == frmLoadEvent ) {
           // Load the form resource.
           formId = pEvent->data.frmLoad.formID;
          
           pForm = FrmInitForm(  formId );
           FrmSetActiveForm(  pForm );
          
           // Set the event handler for the form. The handler of the currently
           // active form is called by FrmHandleEvent each time is receives an
           // event.
           switch (  formId ) {
           case MainForm:
           FrmSetEventHandler(  pForm,   MainFormHandleEvent );
           break;
           case ConnectForm:
           FrmSetEventHandler(  pForm,   UIControl::ConnectFormHandleEvent );
           break;
           case MainTransferForm:
           FrmSetEventHandler(  pForm,   TransferFormHandleEvent );
           break;
          
           default:
           break;
           }
           handled = true;
           }
          
           return handled;
          }
          
          
          /***********************************************************************
           *
           * FUNCTION: AppStart
           *
           * DESCRIPTION: Get the current application's preferences.
           *
           * PARAMETERS: nothing
           *
           * RETURNED: Err value errNone if nothing went wrong
           *
           * REVISION HISTORY:
           *
           *
           ***********************************************************************/
     278  static Err AppStart(  void )
          {
           FrmGotoForm(  MainForm );
           return errNone;
          }
          
          
          /***********************************************************************
           *
           * FUNCTION: AppStop
           *
           * DESCRIPTION: Save the current state of the application.
           *
           * PARAMETERS: nothing
           *
           * RETURNED: nothing
           *
           * REVISION HISTORY:
           *
           *
           ***********************************************************************/
     299  static void AppStop(  void )
          {
           // Close all the open forms.
           FrmCloseAllForms(   );
          }
          
          
          /***********************************************************************
           *
           * FUNCTION: AppEventLoop
           *
           * DESCRIPTION: This routine is the event loop for the application.
           *
           * PARAMETERS: nothing
           *
           * RETURNED: nothing
           *
           * REVISION HISTORY:
           *
           *
           ***********************************************************************/
     320  static void AppEventLoop(  void )
          {
           Err error;
           EventType event;
          
           do {
           EvtGetEvent(  &event,   evtWaitForever );
          
           if (  SysHandleEvent(  &event ) )
           continue;
          
           if (  MenuHandleEvent(  0,   &event,   &error ) )
           continue;
          
           if (  AppHandleEvent(  &event ) )
           continue;
          
           FrmDispatchEvent(  &event );
          
           } while (  event.eType != appStopEvent );
          }
          
          
          /***********************************************************************
           *
           * FUNCTION: PilotMain
           *
           * DESCRIPTION: This is the main entry point for the application.
           *
           * PARAMETERS: cmd - word value specifying the launch code.
           * cmdPB - pointer to a structure that is associated with the launch code.
           * launchFlags - word value providing extra information about the launch.
           * RETURNED: Result of launch
           *
           * REVISION HISTORY:
           *
           *
           ***********************************************************************/
     358  UInt32 PilotMain(  UInt16 cmd,   MemPtr cmdPBP,   UInt16 launchFlags )
          {
           Err error = errNone;
          
           switch (  cmd ) {
           case sysAppLaunchCmdNormalLaunch:
           if (  (  error = AppStart(   ) ) == 0 ) {
           AppEventLoop(   );
           AppStop(   );
           }
           break;
          
           default:
           break;
           }
          
           return error;
          }
          
          

UIControl.cpp

       1  #include "UIControl.h"
          #include "AppResources.h"
          
          /***********************************************************************
           *
           * Internal Structures
           *
           ***********************************************************************/
          
          typedef struct
          {
           NetIPAddr addr;
           UInt16 port;
          } NetLibPreferenceType;
          
          /***********************************************************************
           *
           * Globals
           *
           ***********************************************************************/
          
          NetLibPreferenceType gPrefs = {};
          const UInt16 bufLen = 16;
          char buf[16] = "testing 1-2\n";
          
           UIControl netcontrol;
          
      28  void UIControl::ShowResult(  UInt16 id,   Err error )
          {
           char resultString[11];
           FormType *formPtr;
           UInt16 objIndex;
          
           // Prepare the result as a string
           switch (  error ){
           case CLEAR_RESULT:
           StrCopy(  resultString,   " " );
           break;
           case PROCESSING_RESULT:
           StrCopy(  resultString,   " .... " ); // indicates no results yet
           break;
           default:
           StrPrintF(  resultString,   "0x%04X",   error );
           break;
           }
          
           // Update the specified field label
           formPtr = FrmGetFormPtr(  MainForm );
           objIndex = FrmGetObjectIndex(  formPtr,   id );
           FrmHideObject(  formPtr,   objIndex );
           FrmCopyLabel(  formPtr,   id,   resultString );
           FrmShowObject(  formPtr,   objIndex );
          }
          
      55  void UIControl::ClearResults(  void )
          {
           ShowResult(  MainSysLibFindResultLabel,   -1 );
           ShowResult(  MainNetLibOpenResultLabel,   -1 );
           ShowResult(  MainNetLibOpenIFResultLabel,   -1 );
           ShowResult(  MainNetLibSocketOpenResultLabel,   -1 );
           ShowResult(  MainNetLibSocketConnectResultLabel,   -1 );
           ShowResult(  MainNetLibSendResultLabel,   -1 );
           ShowResult(  MainBytesSentResultLabel,   -1 );
           ShowResult(  MainNetLibSocketCloseResultLabel,   -1 );
           ShowResult(  MainNetLibCloseResultLabel,   -1 );
          }
          
      68  bool UIControl::GetValue(  UInt16 objID,   Int32 *valueP )
          {
           FormType *formPtr;
           UInt16 objIndex;
           FieldType *fieldPtr;
           Char *text;
          
           formPtr = FrmGetFormPtr(  MainForm );
           if (  !formPtr ) return false;
          
           objIndex = FrmGetObjectIndex(  formPtr,   objID );
           fieldPtr = (  FieldType* )FrmGetObjectPtr(  formPtr,   objIndex );
           if (  !fieldPtr ) return false;
          
           text = FldGetTextPtr(  fieldPtr );
           if (  !text ) return false;
          
           *valueP = StrAToI(  text );
           return true;
          }
      88  bool UIControl::SetValue(  UInt16 objID,   Int32 value )
          {
           FormType *formPtr;
           UInt16 objIndex;
           FieldType *fieldPtr;
           MemHandle h,   oldHandle;
           char * text;
          
           formPtr = FrmGetFormPtr(  MainForm );
           if (  !formPtr ) return false;
          
           objIndex = FrmGetObjectIndex(  formPtr,   objID );
           fieldPtr = (  FieldType* )FrmGetObjectPtr(  formPtr,   objIndex );
           if (  !fieldPtr ) return false;
          
           if (  MainPortField == objID ) {
           h = MemHandleNew(  6 );
           } else {
           h = MemHandleNew(  4 );
           }
           if (  !h ) return false;
          
           text = (  char * )MemHandleLock(  h );
           if (  MainPortField == objID ) {
           StrPrintF(  text,   "%u",   (  UInt16 )value );
           } else {
           StrPrintF(  text,   "%u",   (  UInt8 )value );
           }
           MemHandleUnlock(  h );
           oldHandle = FldGetTextHandle(  fieldPtr );
           FldSetTextHandle(  fieldPtr,   h );
           if (  oldHandle ) MemHandleFree(  oldHandle );
          
           return true;
          }
          
     124  bool UIControl::GetAddr(  UInt16 AppNetRefnum,   NetIPAddr *addrP,   UInt16 *portP )
          {
           UInt8 i;
           Int32 ip[4];
           Int32 port;
           Char textIP[16];
          
           // Get the values
           for (  i = 0; i < 4; i++ ) {
           if (  !GetValue(  MainIP1Field + i,   &(  ip[i] ) ) ||
           ip[i] > 255 ||
           ip[i] < 0 ) {
           return false;
           }
           }
          
           if (  !GetValue(  MainPortField,   &port ) ||
           port > 65535 ||
           port < 1 ) {
           return false;
           }
          
           StrPrintF(  textIP,   "%u.%u.%u.%u",   (  UInt8 )ip[0],   (  UInt8 )ip[1],   (  UInt8 )ip[2],   (  UInt8 )ip[3] );
           *addrP = NetLibAddrAToIN(  AppNetRefnum,   textIP );
           *portP = (  UInt16 )port;
           return true;
          }
          
     152  void UIControl::SendTCP(  void )
          {
          
           Err error;
           UInt16 AppNetRefnum;
           UInt16 ifErrs;
           NetSocketRef socket;
           Int16 result;
           NetSocketAddrINType destAddr;
          // const UInt16 bufLen = 15; // using global variables for now
          // char buf[bufLen] = "xxxxxx\n"; // will need to substitute a file
           // wrapped for TCP/IP to be received
           // and used as a video signal
           UInt16 sentBytes = 0;
          
           ClearResults(   );
          
           // Find the network library and start tne network connection (  Connect function )
           ShowResult(  MainSysLibFindResultLabel,   PROCESSING_RESULT );
           error = SysLibFind(  "Net.lib",   &AppNetRefnum );
           ShowResult(  MainSysLibFindResultLabel,   error );
           if (  error ) return;
          
           // Get and validate the destination
           if (  !GetAddr(  AppNetRefnum,   &gPrefs.addr,   &gPrefs.port ) ) {
           FrmAlert(  InvalidDestAlert );
           return;
           }
          
           // Open the network library
           ShowResult(  MainNetLibOpenResultLabel,   PROCESSING_RESULT );
           ShowResult(  MainNetLibOpenIFResultLabel,   PROCESSING_RESULT );
           error = NetLibOpen(  AppNetRefnum,   &ifErrs );
           ShowResult(  MainNetLibOpenResultLabel,   error );
           ShowResult(  MainNetLibOpenIFResultLabel,   ifErrs );
           if (  ifErrs || (  error && error != netErrAlreadyOpen ) ) goto CloseNetLib;
          
           // Open a socket
           ShowResult(  MainNetLibSocketOpenResultLabel,   PROCESSING_RESULT );
           socket = NetLibSocketOpen(  AppNetRefnum,   // Network library
           netSocketAddrINET,   // Address domain
           netSocketTypeStream,   // Socket type
           netSocketProtoIPTCP,   // Protocol
           -1,   // Timeout
           &error // Error result
            );
           ShowResult(  MainNetLibSocketOpenResultLabel,   error );
           if (  error ) goto CloseNetLib;
          
           // Connect the socket to its destination
           MemSet(  &destAddr,   sizeof(  destAddr ),   0 );
           destAddr.family = netSocketAddrINET; // This should match the second argument to NetLibSocketOpen
           destAddr.port = gPrefs.port;
           destAddr.addr = gPrefs.addr;
           error = 0;
           ShowResult(  MainNetLibSocketConnectResultLabel,   PROCESSING_RESULT );
           result = NetLibSocketConnect(  AppNetRefnum,   // Network library
           socket,   // Socket reference
           (  NetSocketAddrType* )&destAddr,   // Destination address
           sizeof(  destAddr ),   // Length of destAddr
           -1,   // Timeout
           &error // Error result
            );
           if (  result == -1 ) {
           ShowResult(  MainNetLibSocketConnectResultLabel,   error );
           goto CloseSocket;
           } else {
           ShowResult(  MainNetLibSocketConnectResultLabel,   0 );
           }
          
           // Send data
           ShowResult(  MainBytesSentResultLabel,   PROCESSING_RESULT );
           while (  sentBytes < bufLen ) {
           ShowResult(  MainNetLibSendResultLabel,   PROCESSING_RESULT );
           result = NetLibSend (  AppNetRefnum,   // Network library
           socket,   // Socket reference
           buf + sentBytes,   // Buffer to send
           bufLen - sentBytes,   // Bytes to send from buffer
           0,   // Flags
           NULL,   // Destination address -- does not apply to TCP sockets
           0,   // Length of destination address
           -1,   // Timeout
           &error // Error result
            );
           if (  result == -1 ) {
           ShowResult(  MainNetLibSendResultLabel,   error );
           goto CloseSocket;
           }
           sentBytes += result;
           ShowResult(  MainBytesSentResultLabel,   sentBytes );
           }
           ShowResult(  MainNetLibSendResultLabel,   0 );
          
           // Close the socket
           CloseSocket:
           ShowResult(  MainNetLibSocketCloseResultLabel,   PROCESSING_RESULT );
           result = NetLibSocketClose (  AppNetRefnum,   // Network Library
           socket,   // Socket reference
           -1,   // Timeout
           &error // Error result
            );
           if (  result == -1 ) {
           ShowResult(  MainNetLibSocketCloseResultLabel,   error );
           } else {
           ShowResult(  MainNetLibSocketCloseResultLabel,   0 );
           }
          
           // Close the network library
           CloseNetLib:
           ShowResult(  MainNetLibCloseResultLabel,   PROCESSING_RESULT );
           error = NetLibClose(  AppNetRefnum,   false );
           ShowResult(  MainNetLibCloseResultLabel,   error );
          }
          
          
     267  Boolean UIControl::ConnectFormHandleEvent(  EventType* pEvent )
          { //event handler inside the ConnectForm
          
           bool handled = false;
           FormType* pForm;
          
           switch(  pEvent->eType )
           {
           case frmOpenEvent:
           pForm = FrmGetActiveForm(   );
          
           //MainFormInit(   pForm  ); // need to include MainFormInit method
           // to call ClearResults(   );
          
          // UIControl uiControls;
          
           // Load initial values from prefs
           netcontrol.SetValue(  MainIP1Field,   (  gPrefs.addr & 0xFF000000 ) >> 24 );
           netcontrol.SetValue(  MainIP2Field,   (  gPrefs.addr & 0x00FF0000 ) >> 16 );
           netcontrol.SetValue(  MainIP3Field,   (  gPrefs.addr & 0x0000FF00 ) >> 8 );
           netcontrol.SetValue(  MainIP4Field,   gPrefs.addr & 0x000000FF );
           netcontrol.SetValue(  MainPortField,   gPrefs.port );
          
           FrmDrawForm(  pForm );
           handled = true;
           break;
           case ctlSelectEvent:
           FrmGotoForm(  MainForm );
           handled = true;
           break;
           ////////WILL NEED TO ADD MORE ONCE CONNECT DOES ITS THING
           default:
           break;
           }
           return handled;
          }