home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk393.lzh / LibTool / CComplex / Complex.c next >
C/C++ Source or Header  |  1990-10-28  |  7KB  |  244 lines

  1. /**************************************************************************
  2.   Complex.c
  3.  
  4.   A set of C routines to be turned into a library called "complex.library".
  5.   We use LibTool to make our lib startup asm code (-m option) to be linked with
  6.   this module. This creates a library. We also use LibTool to create glue
  7.   code for a C application which expects to call C library functions (-c
  8.   option). Note that this module should NOT be compiled with small code/data
  9.   because we do not bother setting up and restoring a4 (which is what you
  10.   would have to do at the start and end of each callable function).
  11.  
  12. Manx 3.6
  13.  
  14. cc +pb Complex.c
  15. LibTool -cmo glue.asm ComplexC.fd
  16. as -cd -o LibStart.o ComplexC.src
  17. ln -o libs:complex.library LibStart.o Complex.o -lcl32
  18.  
  19. Lattice 5.0
  20.  
  21. lc -b0 Complex.c
  22. LibTool -cmo glue.asm ComplexC.fd
  23. assemble ComplexC.src as LibStart.o
  24. blink LibStart.o Complex.o LIB lib:lcnb.lib TO libs:complex.library
  25.  
  26. ***************************************************************************/
  27.  
  28. extern struct LibBase; /* this library's base */
  29.  
  30. #ifdef AZTEC_C
  31. #define NARGS
  32. #define NO_PRAGMAS
  33. #endif
  34.  
  35. #include "exec/types.h"
  36. #include "exec/tasks.h"
  37. #include "exec/memory.h"
  38. #include "libraries/dos.h"
  39. #include "libraries/dosextens.h"
  40.  
  41. #ifdef AZTEC_C
  42. #include "functions.h"
  43. #else
  44. #include "proto/all.h"
  45. #endif
  46.  
  47. #include "intuition/intuition.h"
  48. #include "intuition/intuitionbase.h"
  49.  
  50. #include "graphics/gfx.h"
  51. #include "graphics/gfxbase.h"
  52. #include "graphics/rastport.h"
  53. #include "graphics/gfxmacros.h"
  54. #include "graphics/view.h"
  55. #include "graphics/text.h"
  56.  
  57.  
  58. /* We allocate one of these in OpenUp for every task that opens the lib */
  59. struct workBuf {
  60.     struct workBuf     *wb_Succ;
  61.     struct workBuf     *wb_Pred;
  62.     struct Process   *taskAddr;
  63.     struct NewWindow nWind;
  64. };
  65.  
  66. struct List memyList;
  67.  
  68. struct NewWindow newWind =
  69.     {
  70.     20, 20,        /* LeftEdge, TopEdge */
  71.     300, 145,    /* Width, Height */
  72.     -1, -1,        /* Detail/BlockPens */
  73.     CLOSEWINDOW,    /* IDCMP Flags */
  74.     WINDOWDRAG|WINDOWDEPTH|SMART_REFRESH|WINDOWCLOSE|ACTIVATE,
  75.             /* Window Specification Flags */
  76.     0L,        /* FirstGadget */
  77.     0L,        /* Checkmark */
  78.     0L,        /* WindowTitle */
  79.     0L,        /* Screen */
  80.     0L,        /* SuperBitMap */
  81.     303, 145,    /* MinWidth, MinHeight */
  82.     600, 200,    /* MaxWidth, MaxHeight */
  83.     WBENCHSCREEN,
  84.     };
  85.  
  86.  
  87. /**************************************************************************
  88.  This function finds the app's workBuf from within our memyList. We tagged
  89.  the mem with the app's task address, so we just look for a workBuf whose
  90.  taskAddr field is the same. Note the Forbid/Permit around the examining of
  91.  the memyList. This is because memyList is a global. When accessing globals
  92.  in a shared library, you must prevent other tasks from using the functions
  93.  until done accessing the libs globals. This is why you should always use
  94.  local (i.e. register) variables, allocate mem per task as we did the work
  95.  buffer, or only operate on application data structures passed into these
  96.  lib functions.
  97.  Also note that this function is for internal use only and so does not
  98.  appear listed in our fd file.
  99. ***************************************************************************/
  100.  
  101. struct workBuf *FindMem()
  102. {
  103. register struct workBuf *thisBuf;
  104. register struct Process *thisTask;
  105.  
  106.     /* Find the application's address */
  107.     thisTask = (struct Process *) FindTask(0L);
  108.  
  109.     /* Forbid so that we can examine our global memyList without another
  110.      task messing with it */
  111.     Forbid();
  112.  
  113.     /* Search the list for this task's work buffer */
  114.     thisBuf = (struct workBuf *) memyList.lh_Head;
  115.     while ( thisBuf->wb_Succ )
  116.     {
  117.  
  118.       if ( thisTask == thisBuf->taskAddr )
  119.       {
  120.         /* Permit and return the work buffer */
  121.         Permit();
  122.         return(thisBuf);
  123.       }
  124.       thisBuf = thisBuf->wb_Succ;
  125.  
  126.     }
  127.  
  128.     /* Permit and return(0), we couldn't find its work buffer!! */
  129.     Permit();
  130.     return(0);
  131. }
  132.  
  133.  
  134. /**************************************************************************
  135.  
  136. ***************************************************************************/
  137.  
  138. struct Window *MakeWindow()
  139. {
  140. register struct workBuf *wBuf;
  141. register struct Window *myWind;
  142.  
  143.     /* Find this task's work buffer */
  144.     if ( !( wBuf = FindMem() ) )
  145.       return(0);
  146.  
  147.     /* Copy the newWindow template to our work buffer's new window */
  148.     wBuf->nWind = newWind;
  149.  
  150.     /* Open the window and return result */
  151.     myWind = OpenWindow(&wBuf->nWind);
  152.     return(myWind);
  153. }
  154.  
  155.  
  156. /**************************************************************************
  157.  
  158. ***************************************************************************/
  159.  
  160. VOID PrintMsg(x, y, myWind, msg)
  161. SHORT x, y;
  162. UBYTE *msg;
  163. struct Window *myWind;
  164. {
  165. register struct Message *reply;
  166.  
  167.     /* Move to (x,y) */
  168.     Move( myWind->RPort, x, y );
  169.  
  170.     /* Print the msg */
  171.     Text( myWind->RPort, msg, (strlen(msg)) );
  172.  
  173.     /* Wait for CLOSEWINDOW */
  174.     WaitPort(myWind->UserPort);
  175.     reply = GetMsg(myWind->UserPort);
  176. }
  177.  
  178.  
  179. /**************************************************************************
  180.  
  181. ***************************************************************************/
  182.  
  183. VOID RemWindow(myWind)
  184. struct Window *myWind;
  185. {
  186.     /* Close the window */
  187.     CloseWindow(myWind);
  188. }
  189.  
  190.  
  191. /**************************************************************************
  192.  This is called each time an application opens the lib. It allocates the app's
  193.  workBuf, links it into our memyList, and tags it with the app's task address.
  194.  This is the Open vector function (i.e. ##open in the fd file).
  195. ***************************************************************************/
  196.  
  197. BOOL OpenUp()
  198. {
  199. register struct workBuf *wBuf;
  200.  
  201.     /* Allocate a work buffer */
  202.     wBuf = AllocMem(sizeof(struct workBuf),MEMF_CLEAR|MEMF_PUBLIC);
  203.  
  204.     /* Link it into our memyList */
  205.     if (wBuf)
  206.     {
  207.       AddTail(&memyList, wBuf);
  208.       wBuf->taskAddr = (struct Process *) FindTask(0L);
  209.       return(TRUE);
  210.     }
  211.     return(FALSE);
  212. }
  213.  
  214.  
  215. /**************************************************************************
  216.  This is called each time as application closes the lib. It finds the app's
  217.  workBuf within our memyList, removes it from the list, and frees it. This is
  218.  the Close vector function (i.e. ##clos in the fd file).
  219. ***************************************************************************/
  220.  
  221. VOID CloseUp()
  222. {
  223. register struct workBuf *wBuf;
  224.  
  225.     /* Find this task's work buffer, remove from list, and free it */
  226.     if ( wBuf = FindMem() )
  227.     {
  228.       Remove(wBuf);
  229.       FreeMem(wBuf, sizeof(struct workBuf));
  230.     }
  231. }
  232.  
  233.  
  234. /**************************************************************************
  235.  This is called only once when the lib is first loaded. It just initializes
  236.  our memyList. This is the Init vector function (i.e. ##init in the fd file).
  237. ***************************************************************************/
  238.  
  239. BOOL myInit()
  240. {
  241.     NewList(&memyList);
  242.     return(TRUE);
  243. }
  244.