home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Files / AsyncPB / AsyncPB.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  8.7 KB  |  464 lines  |  [TEXT/MPS ]

  1. /*
  2. ** AsyncPB is an example of how File System calls can be made 
  3. **    in a chain from an interrupt handler like the Time Manager.
  4. ** Once you select DoIt from the menu give it a little time to actually
  5. **    process the input file.
  6. **
  7. ** James "im" Beninghaus. DTS.
  8. */
  9.  
  10. #include    <Aliases.h>
  11. #include    <AppleEvents.h>
  12. #include    <Balloons.h>
  13. #include    <Controls.h>
  14. #include    <Desk.h>
  15. #include    <Dialogs.h>
  16. #include    <Events.h>
  17. #include    <Errors.h>
  18. #include    <Files.h>
  19. #include    <Folders.h>
  20. #include    <Fonts.h>
  21. #include    <GestaltEqu.h>
  22. #include    <Icons.h>
  23. #include    <Memory.h>
  24. #include    <Menus.h>
  25. #include     <OSUtils.h>
  26. #include    <Packages.h>
  27. #include    <Printing.h>
  28. #include    <Quickdraw.h>
  29. #include    <Resources.h>
  30. #include    <Script.h>
  31. #include    <StandardFile.h>
  32. #include    <String.h>
  33. #include    <Strings.h>
  34. #include     <SysEqu.h>
  35. #include    <Terminals.h>
  36. #include    <TerminalTools.h>
  37. #include    <TextEdit.h>
  38. #include    <Timer.h>
  39. #include     <ToolUtils.h>
  40. #include    <Traps.h>
  41. #include    <Types.h>
  42. #include    <Windows.h>
  43.  
  44. #define                isStationary            2048
  45.  
  46. #define                ApplicationMenuBar        128
  47. #define                AppleMenu                128
  48. #define                AboutItem                  1
  49. #define                FileMenu                129
  50. #define                DoIt                      1
  51. #define                QuitItem                  2
  52.  
  53. #define                AboutAlert                128
  54. #define                kWindowKind                'im'
  55.  
  56. Boolean                gInForeGround            = true;
  57. Boolean                gProcessing                = true;
  58. unsigned long        gSleep                    = 0;
  59.  
  60. void                DoMenu                    (short menu, short item);
  61. void                EventLoop                (void);
  62. int                    main                    (void);
  63.  
  64.  
  65.  
  66. typedef    struct MyParamBlockRec
  67. {
  68.     IOParam            ioParam;
  69.     
  70.     ProcPtr            setFPosIOCompletion;
  71.     ProcPtr            readIOCompletion;
  72.     ProcPtr            writeIOCompletion;
  73.     
  74.     short            readFileRefNum;
  75.     short            writeFileRefNum;
  76.     
  77.     TMTaskPtr        tm;
  78.     
  79. } MyParamBlockRec, *MyParamBlockPtr;
  80.  
  81.  
  82.  
  83.  
  84. typedef    struct TaskRec
  85. {
  86.     QElemPtr        qLink;
  87.     short            qType;
  88.     TimerProcPtr    tmAddr;
  89.     long             tmCount;
  90.     
  91.     MyParamBlockPtr    pb;
  92.     
  93. } MyTaskRec, *MyTaskPtr;
  94.  
  95.  
  96.  
  97.  
  98. MyTaskRec            gTM;
  99. MyParamBlockRec        gPB;
  100. char                gBuffer;
  101.  
  102.  
  103. MyParamBlockPtr        GetParmBlockPtr()        = { 0x2008 };     // move.l a0, d0 ;move the pointer to where MPW C places function results
  104. MyTaskPtr            GetTMTaskPtr()            = { 0x2009 };    // move.l a1, d0 ;move the pointer to where MPW C places function results
  105.  
  106.  
  107. void PBWriteIOCompletion()
  108. {
  109.     auto    MyParamBlockPtr        pb = GetParmBlockPtr();
  110.     
  111.     if (noErr == (*pb).ioParam.ioResult)
  112.     {
  113.         PrimeTime((QElemPtr) (*pb).tm, 1000 * 1);
  114.     }
  115. }
  116.  
  117. void PBReadIOCompletion()
  118. {    
  119.     auto    MyParamBlockPtr        pb = GetParmBlockPtr();
  120.     
  121.     if (noErr == (*pb).ioParam.ioResult)
  122.     {
  123.         (*pb).ioParam.ioCompletion    = (*pb).writeIOCompletion;
  124.         (*pb).ioParam.ioRefNum        = (*pb).writeFileRefNum;
  125.         (*pb).ioParam.ioReqCount    = (*pb).ioParam.ioActCount;
  126.         (*pb).ioParam.ioPosMode        = fsAtMark;
  127.         (*pb).ioParam.ioPosOffset    = 0;
  128.         (void) PBWriteAsync((ParmBlkPtr) pb);
  129.     }
  130. }
  131.  
  132. void PBSetFPosIOCompletion()
  133. {
  134.     auto    MyParamBlockPtr        pb = GetParmBlockPtr();
  135.  
  136.     if (noErr == (*pb).ioParam.ioResult)
  137.     {
  138.         (*pb).ioParam.ioCompletion    = (*pb).readIOCompletion;
  139.         (*pb).ioParam.ioRefNum        = (*pb).readFileRefNum;
  140.         (*pb).ioParam.ioReqCount    = 1;
  141.         (*pb).ioParam.ioPosMode        = fsAtMark;
  142.         (*pb).ioParam.ioPosOffset    = 0;
  143.         (void) PBReadAsync((ParmBlkPtr) pb);
  144.     }
  145. }
  146.  
  147. pascal void TMTaskProc()
  148. {
  149.     auto    MyTaskPtr        tm = GetTMTaskPtr();
  150.         
  151.     (*(*tm).pb).ioParam.ioCompletion    = (*(*tm).pb).setFPosIOCompletion;
  152.     (*(*tm).pb).ioParam.ioRefNum        = (*(*tm).pb).readFileRefNum;
  153.     (*(*tm).pb).ioParam.ioPosMode        = fsFromMark;
  154.     (*(*tm).pb).ioParam.ioPosOffset        = 2;
  155.     (void) PBSetFPosAsync((ParmBlkPtr) (*tm).pb);
  156. }
  157.  
  158. void DoMenu(short menu, short item)
  159. {
  160.     OSErr                error        = noErr;
  161.     Str255                daName;
  162.     short                daRefNum;
  163.     Point                where        = { 30, 10 };
  164.  
  165.     switch (menu)
  166.     {
  167.         case AppleMenu :
  168.         {
  169.             switch (item)
  170.             {
  171.                 case AboutItem :
  172.                 {
  173.                     Alert(AboutAlert, nil);
  174.                 }
  175.                 break;
  176.                 
  177.                 default :
  178.                 {
  179.                     GetItem(GetMHandle(AppleMenu), item, daName);
  180.                     daRefNum = OpenDeskAcc(daName); 
  181.                 }
  182.                 break;
  183.             }
  184.         }
  185.         break;
  186.         
  187.         case FileMenu :
  188.         {            
  189.             switch (item)
  190.             {
  191.                 case DoIt :
  192.                 {
  193.                     DisableItem(GetMHandle(FileMenu), DoIt);
  194.  
  195.                     error = FSOpen("\pinData", 0, &gPB.readFileRefNum);    
  196.                     
  197.                     error = FSDelete("\pOutData", 0);
  198.                     error = Create("\pOutData", 0, 'MPS ', 'TEXT');
  199.                     error = FSOpen("\pOutData", 0, &gPB.writeFileRefNum);    
  200.  
  201.                     gPB.ioParam.ioBuffer        = &gBuffer;
  202.                     gPB.tm                        = (TMTaskPtr) &gTM;
  203.                     
  204.                     gPB.setFPosIOCompletion        = (ProcPtr) PBSetFPosIOCompletion;        
  205.                     gPB.readIOCompletion        = (ProcPtr) PBReadIOCompletion;        
  206.                     gPB.writeIOCompletion        = (ProcPtr) PBWriteIOCompletion;        
  207.  
  208.                     
  209.                 #if false
  210.                     gPB.ioParam.ioCompletion    = gPB.setFPosIOCompletion;
  211.                     gPB.ioParam.ioRefNum        = gPB.readFileRefNum;
  212.                     gPB.ioParam.ioPosMode        = fsFromMark;
  213.                     gPB.ioParam.ioPosOffset        = 3;
  214.                     (void) PBSetFPosAsync((ParmBlkPtr) &gPB);
  215.                 #else
  216.                     gTM.qType    = 0;
  217.                     gTM.tmCount    = 0;
  218.                     gTM.tmAddr    = &TMTaskProc;
  219.                     gTM.pb        = &gPB;
  220.                     InsTime((QElemPtr) &gTM);
  221.                     PrimeTime((QElemPtr) &gTM, 1000 * 1);
  222.                 #endif
  223.                 
  224.                 }
  225.                 break;
  226.                                 
  227.                 case QuitItem :
  228.                 {
  229.                     RmvTime((QElemPtr)&gTM);
  230.                     gProcessing = false;
  231.                 }
  232.                 break;
  233.             }
  234.         }
  235.         break;
  236.         
  237.         default :
  238.         {
  239.         }
  240.         break;
  241.         
  242.     }
  243. }
  244.         
  245. void EventLoop()
  246. {
  247.     OSErr                error         = noErr;
  248.     EventRecord            event;
  249.     Boolean                gotEvent;
  250.     WindowPtr            window;
  251.     DialogPtr            dialog;
  252.     short                itemHit;
  253.     short                part;
  254.     long                menuItem;
  255.     short                menu;
  256.     short                item;
  257.  
  258.     gotEvent = WaitNextEvent(everyEvent, &event, gSleep, nil);
  259.     
  260.     if (IsDialogEvent(&event))
  261.     {
  262.         if (! ((keyDown == event.what || autoKey == event.what) && (event.modifiers & cmdKey)))
  263.         {
  264.             if (DialogSelect(&event, &dialog, &itemHit))
  265.             {
  266.             }
  267.         }
  268.     }
  269.     else
  270.     {
  271.         switch (event.what)
  272.         {
  273.             case nullEvent:
  274.             {
  275.             }
  276.             break;
  277.                         
  278.             case mouseDown:
  279.             {
  280.                 switch (part = FindWindow(event.where, &window))
  281.                 {
  282.                     case inMenuBar:
  283.                     {
  284.                         menuItem = MenuSelect(event.where);
  285.                         menu = HiWord(menuItem);
  286.                         item = LoWord(menuItem);
  287.                         DoMenu(menu, item);
  288.                         HiliteMenu(0);
  289.                     }
  290.                     break;
  291.     
  292.                     case inContent :
  293.                     {
  294.                         if (! (*(WindowPeek)window).hilited)
  295.                         {
  296.                             SelectWindow(window);
  297.                         }
  298.                         else
  299.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  300.                         {
  301.                         }
  302.                     }
  303.                     break;
  304.                     
  305.                     case inGrow :
  306.                     {
  307.                         #define        kMinWidth        128
  308.                         #define        kMinWeight        64
  309.                         auto        Rect            limitRect;
  310.                         auto        long            newSize;
  311.                         
  312.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  313.                         {
  314.                             limitRect = qd.screenBits.bounds;
  315.                             InsetRect(&limitRect, 8, 8);
  316.                             limitRect.top    = kMinWeight;
  317.                             limitRect.left    = kMinWidth;
  318.                             newSize = GrowWindow(window, event.where, &limitRect);
  319.                             if (0 != newSize)
  320.                             {
  321.                                 SizeWindow(window, LoWord(newSize), HiWord(newSize), true);
  322.                             }
  323.                         }
  324.                     }
  325.                     break;
  326.                     
  327.                     case inDrag:
  328.                     {
  329.                         DragWindow(window, event.where, &qd.screenBits.bounds);
  330.                     }
  331.                     break;
  332.                     
  333.                     case inGoAway :
  334.                     {
  335.                         if (TrackGoAway(window, event.where))
  336.                         {
  337.                         }
  338.                     }
  339.                     break;
  340.                 
  341.                     case inZoomIn :
  342.                     case inZoomOut :
  343.                     {
  344.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  345.                         {
  346.                             if ( TrackBox(window, event.where, part) )
  347.                             {
  348.                                 SetPort(window);
  349.                                 EraseRect(&window->portRect);
  350.                                 ZoomWindow(window, part, true);
  351.                                 InvalRect(&window->portRect);
  352.                             }
  353.                         }
  354.                     }
  355.                     break;
  356.                     
  357.                 }
  358.             }
  359.             break;
  360.             
  361.             case updateEvt:
  362.             {
  363.                 window = (WindowPtr) event.message;
  364.                 if (kWindowKind == (*(WindowPeek)window).windowKind)
  365.                 {
  366.                     SetPort(window);
  367.                     BeginUpdate(window);
  368.                     {
  369.                     }
  370.                     EndUpdate(window);
  371.                 }
  372.             }
  373.             break;
  374.             
  375.             case activateEvt:
  376.             {
  377.                 window = (WindowPtr) event.message;
  378.                         if (kWindowKind == (*(WindowPeek)window).windowKind)
  379.                 {
  380.                 }
  381.             }
  382.             break;
  383.             
  384.             case autoKey:        
  385.             case keyDown:
  386.             {
  387.                 auto    char    key;
  388.                 
  389.                 key = event.message & charCodeMask;
  390.                 if (event.modifiers & cmdKey)
  391.                 {
  392.                     menuItem = MenuKey(key);
  393.                     menu = HiWord(menuItem);
  394.                     item = LoWord(menuItem);
  395.                     DoMenu(menu, item);
  396.                 }
  397.                 else
  398.                 {
  399.                     window = FrontWindow();
  400.                     if (kWindowKind == (*(WindowPeek)window).windowKind)
  401.                     {
  402.                     }
  403.                 }
  404.             }
  405.             break;
  406.             
  407.             case osEvt:
  408.             {
  409.                 switch ((event.message >> 24) & 0x0FF)
  410.                 {
  411.                     case suspendResumeMessage:
  412.                     {
  413.                         gInForeGround = 0 != (event.message & resumeFlag);
  414.                     }
  415.                     break;
  416.                 }
  417.             }
  418.             break;
  419.             
  420.             case kHighLevelEvent:
  421.             {
  422.                 error = AEProcessAppleEvent(&event);
  423.             }
  424.             break;
  425.         }
  426.     }
  427. }
  428.  
  429. main ()
  430. {            
  431.     OSErr                error         = noErr;
  432.     Handle                menuBar;
  433.     
  434.     InitGraf(&qd.thePort);
  435.     InitFonts();
  436.     InitWindows();
  437.     InitMenus();
  438.     TEInit();
  439.     InitDialogs(nil);
  440.     InitCursor();
  441.  
  442.  
  443.     menuBar = GetNewMBar(ApplicationMenuBar);
  444.     if (nil == menuBar)
  445.     {
  446.         error = ResError();
  447.         if (noErr == error)
  448.         {
  449.             error = resNotFound;
  450.             
  451.             ExitToShell();
  452.         }
  453.     }
  454.     else
  455.     {
  456.         SetMenuBar(menuBar);
  457.         DisposHandle(menuBar);
  458.         AddResMenu(GetMHandle(AppleMenu), 'DRVR');
  459.         DrawMenuBar();
  460.     }
  461.     
  462.     while (gProcessing)
  463.         EventLoop();
  464. }