home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’92 / PatchWorks Kit / Examples / THINK Example / Example.c next >
Encoding:
C/C++ Source or Header  |  1992-05-19  |  2.2 KB  |  123 lines  |  [TEXT/KAHL]

  1. /*
  2.     Example.c
  3.     
  4.     Example extension. Demonstrates use of PatchWorks trap patching
  5.     system to patch MenuSelect. Uses new GenericPatch class.
  6.     
  7.     by Mouse Herrell & Patrick Beard.
  8.     
  9.     © 1991 Berkeley Systems Inc.
  10. */
  11.  
  12. #include <string.h>
  13. #include <Notification.h>
  14. #include <Traps.h>
  15. #include <stdarg.h>
  16. #include <stdio.h>
  17. #include <stddef.h>
  18.  
  19. #include <Exceptions.h>
  20. #include <Extension.h>
  21. #include <GenericPatch.h>
  22.  
  23. static int dprintf(const char* format, ...);
  24.  
  25. //
  26. //    MenuSelectPatch -- Example of a head-off patch.
  27. //
  28.  
  29. class MenuSelectPatch : public GenericPatch {
  30. public:
  31.     MenuSelectPatch();
  32.     virtual void Behavior(void);
  33.  
  34. private:
  35.     long itsCallCount;
  36. };
  37.  
  38. struct MenuSelectParameters {
  39.     Point itsStartPt;
  40.     long itsResult;
  41. };
  42.  
  43. typedef struct MenuSelectParameters MenuSelectParameters;
  44.  
  45. typedef pascal long (*MenuSelectProcPtr) (Point pt);
  46.  
  47. MenuSelectPatch::MenuSelectPatch()
  48. {
  49.     // initialize instance variables.
  50.     itsCallCount = 0;
  51.     
  52.     // install the appropriate patch.
  53.     GenericPatch::InitGenericPatch(_MenuSelect, offsetof(MenuSelectParameters, itsResult));
  54.     Install();
  55. }
  56.  
  57. void MenuSelectPatch::Behavior()
  58. {
  59.     KeyMap Keys;
  60.     
  61.     // announce our presence if control key held down.
  62.     ++itsCallCount;
  63.     GetKeys(Keys);
  64.     if (Keys[1] & 0x8) {
  65.         MenuSelectParameters* params = (MenuSelectParameters*)itsFrame->parameters;
  66.         
  67.         // head off the patch and return.
  68.         params->itsResult = 0;
  69.         AbortTrap();
  70.         
  71.         // print out where we are so far.
  72.         dprintf("MenuSelect:  startPt = (%d, %d), %ld calls.",
  73.                 params->itsStartPt, itsCallCount);
  74.     }
  75. }
  76.  
  77. //
  78. //    Install routine. This is where you allocate your patch objects.
  79. //    Throw an exception if something fails.
  80. //
  81.  
  82. void Install()
  83. {
  84.     try {
  85.         // if extension succeeds, show happy icon.
  86.         new MenuSelectPatch;
  87.         ShowIconFamily(128);
  88.     } catch {
  89.         // extension failed, show sad icon.
  90.         ShowIconFamily(129);
  91.         throw(theException);
  92.     }
  93. }
  94.  
  95. //
  96. //    Remove routine. This is called when system is shutdown.
  97. //    Perhaps this should be removed. Rob thinks it shouldn't even be
  98. //    part of the design.
  99. //
  100.  
  101. void Remove()
  102. {
  103.     Patch::RemoveAll();
  104. }
  105.  
  106. //
  107. // debugging printf.
  108. //
  109.  
  110. static int dprintf(const char* format, ...)
  111. {
  112.     va_list args;
  113.     static char str[256];
  114.     int count;
  115.     
  116.     va_start(args, format);
  117.     count = vsprintf(str, format, args);
  118.     va_end(args);
  119.     DebugStr(c2pstr(str));
  120.         
  121.     return count;
  122. }
  123.