home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Talking KeyBoard / Source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-17  |  4.4 KB  |  208 lines  |  [TEXT/CWIE]

  1. // Program Author: Paul Baxter
  2. //    pbaxter@assistivetech.com
  3. //
  4. //
  5.  
  6. #include <Speech.h>
  7. #include <Ctype.h>
  8. #include <DeskBus.h>
  9. #include <Retrace.h>
  10.  
  11. #include "filter.h"
  12. #include "adb.h"
  13. #include "pref.h"
  14. #include "globals.h"
  15. #include "menu.h"
  16. #include "speech.h"
  17. #include "event.h"
  18. #include "command.h"
  19. #include "DeferredTask.h"
  20. #include "aeevents.h"
  21. #include "aeaccessors.h"
  22. #include "aecoercions.h"
  23.  
  24. // alert defines
  25. #define kNoSpeachAlertID    129
  26. #define kNoKeyboardDriver    130
  27. #define kOutofMemAlertID    131
  28. #define kCantLoadPrefsID    132
  29.  
  30. // prototypes
  31. static void InitMac(void);
  32. static void InitApp(void);
  33. static void KillApp(void);
  34. static Boolean SpeechExists(void);
  35.  
  36.  
  37. // * ****************************************************************************** *
  38. // *            main
  39. // *                                Main entry point
  40. // * ****************************************************************************** *
  41. void main(void)
  42. {
  43.     // Init the Macintosh
  44.     InitMac();
  45.  
  46.     // Init the application
  47.     InitApp();
  48.  
  49.     // Main Event Loop
  50.     EventLoop();
  51.  
  52.     // Kill the application
  53.     KillApp();
  54. }
  55.  
  56. // * ****************************************************************************** *
  57. // *            InitMac
  58. // *                                Init Macintosh ToolBax
  59. // * ****************************************************************************** *
  60. static void InitMac(void)
  61. {
  62.     /* Initialize all the needed managers. */
  63.     InitGraf(&qd.thePort);
  64.     InitFonts();
  65.     InitWindows();
  66.     InitMenus();
  67.     InitDialogs(nil);
  68.     InitCursor();
  69.     MoreMasters();
  70.     MoreMasters();
  71.     MoreMasters();
  72.     MoreMasters();
  73.     MaxApplZone();
  74. }
  75.  
  76. // * ****************************************************************************** *
  77. // *            InitApp
  78. // *                                Init application stuff
  79. // * ****************************************************************************** *
  80. static void InitApp(void)
  81. {
  82.     long saveA5;
  83.     OSErr err;
  84.  
  85.     // See if we have speech
  86.     if (!SpeechExists()) {
  87.         StopAlert(kNoSpeachAlertID, nil);
  88.         KillApp();
  89.         ExitToShell();
  90.     }
  91.  
  92.     // Init appleEvents
  93.     if (HasAppleEvents()) {
  94.         InitHLEvents();
  95.         // I really don't care about errors here
  96.         // This just makes it scriptable and recordable
  97.         err = AEObjectInit();
  98.         err = InstallAccessors();
  99.         err = InstallCoercions();
  100.     }
  101.  
  102.     //  Load our prefs
  103.     if (LoadPrefs()) {
  104.         StopAlert(kCantLoadPrefsID, nil);
  105.         KillApp();
  106.         ExitToShell();
  107.     }
  108.  
  109.     // Find the ADBAddress of the Keyboard
  110.     if (!InitADBAddress()) {
  111.         StopAlert(kNoKeyboardDriver, nil);
  112.         KillApp();
  113.         ExitToShell();
  114.     }
  115.  
  116.     // Init the speech buffers
  117.     if (!InitBuffers()) {
  118.         StopAlert(kOutofMemAlertID, nil);
  119.         KillApp();
  120.         ExitToShell();
  121.     }
  122.  
  123.     // Init our speech strings
  124.     InitSpeechStrings();
  125.  
  126.     // Install our menus
  127.     MenusInit();
  128.  
  129.     // Init ADB ServiceRoutines
  130.     InitADBService();
  131.  
  132.     // Get our process number for the event filter
  133.     GetCurrentProcess(&gPSN);
  134.  
  135.     // Install an event filter
  136.     SetA5(saveA5 = SetA5(0));
  137.     gFilterProc = (Ptr) InstallEventFilter((FilterHelperUPP) EventFilterHelper, (Ptr) saveA5);
  138.  
  139.     // install ADBService and 
  140.     if ((**gPrefs).speakChars) {
  141.         InstallADBServiceRoutine(gKeyBoardADBAddress);
  142.     }
  143.  
  144.     // Install DTask
  145.     err = InitDeferredTask();
  146.  
  147.     // install JADBProc in case ADBReInit is called
  148.     InstallJADBProc();
  149.  
  150.     // Do the AboutBox
  151.     ProcessCommand(AboutCmd, nil);
  152. }
  153.  
  154. // * ****************************************************************************** *
  155. // *            KillApp
  156. // *                                kill application stuff
  157. // * ****************************************************************************** *
  158. static void KillApp(void)
  159. {
  160.  
  161.     // Order is important!!
  162.     // If disposed of in the wrong order a crash will occur.
  163.  
  164.  
  165.     // KillSpeech or crash!
  166.     if (gSpeechChannel) {
  167.         StopSpeech(gSpeechChannel);
  168.         DisposeSpeechChannel(gSpeechChannel);
  169.         gSpeechChannel = nil;
  170.     }
  171.     // remove ADBService or crash
  172.     RemoveADBServiceRoutine(gKeyBoardADBAddress);
  173.  
  174.     // remove JADBProc or crash
  175.     UnInstallJADBProc();
  176.  
  177.     // remove Filter or crash
  178.     ReleaseEventFilter(gFilterProc);
  179.  
  180.     // RemoveDTask
  181.     RemoveDTask();
  182.  
  183.     // deallocate buffers
  184.     KillBuffers();
  185.  
  186.     // Save Prefs
  187.     SavePrefs();
  188. }
  189.  
  190. // * ****************************************************************************** *
  191. // *     SpeechExists
  192. // *             determine if the speech manager is present
  193. // * ****************************************************************************** *
  194. static Boolean SpeechExists(void)
  195. {
  196.     Boolean speechExists = false;
  197.     long response;
  198.     OSErr err;
  199.  
  200.     err = Gestalt(gestaltSpeechAttr, &response);
  201.     if(!err) {
  202.         if(response & (1L << gestaltSpeechMgrPresent)) {
  203.             speechExists = true;
  204.         }
  205.     }
  206.     return speechExists;
  207. }
  208.