home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / workbench / stickit2 / source.lha / source / main.c < prev    next >
C/C++ Source or Header  |  1995-01-09  |  6KB  |  267 lines

  1. /*********************************************
  2.  **************    main.c   ******************
  3.  *********************************************/
  4.  
  5. #define INTUI_V36_NAMES_ONLY
  6.  
  7. #include <exec/exec.h>
  8. #include <exec/libraries.h>
  9. #include <intuition/intuition.h>
  10. #include <graphics/rastport.h>
  11. #include <libraries/commodities.h>
  12. #include <dos/dos.h>
  13. #include <workbench/startup.h>
  14. #include <libraries/gadtools.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/graphics_protos.h>
  19. #include <clib/commodities_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include <clib/alib_protos.h>
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #include <string.h> /* <-------- Remove this later */
  27.  
  28. #include "stickit2.h"
  29.  
  30. #include "consts.h"
  31. #include "structs.h"
  32. #include "proto.h"
  33.  
  34. /* Set up the version string */
  35.  
  36. char *versionstring = VERSION_STRING;
  37.  
  38. struct IntuitionBase  *IntuitionBase = NULL;
  39. struct Library *GfxBase = NULL;
  40. struct Library *CxBase = NULL;
  41. struct Library *IconBase = NULL;
  42. struct Library *DiskfontBase = NULL;
  43. struct Library *UtilityBase = NULL;
  44. struct Library *AslBase = NULL;
  45. struct Library *IFFParseBase = NULL;
  46. struct Library *GadToolsBase = NULL;
  47.  
  48. struct Library *ConsoleDevice = NULL;     /* <--- For cursor keys !!! */
  49. struct IOStdReq ioreq;             /* <--- For cursor keys !!! */
  50.  
  51. prj_p prj = NULL;
  52.  
  53. BOOL fromwb = FALSE;
  54.  
  55. long _stack_fudge = 10000;        /* Let's have a decent stack */
  56.  
  57. /* Started from Workbench ? */
  58.  
  59. int wbmain(struct WBStartup *wbmsg)
  60. {
  61.     /* Show that we started from workbench */
  62.  
  63.     fromwb = TRUE;
  64.  
  65.     /* Make it look like an SAS call */
  66.  
  67.     main(0,(char **)wbmsg);
  68.  
  69.     return(0);
  70. }
  71.  
  72. int main(int argc, char **argv)
  73. {
  74.     int l;
  75.  
  76.     /***   Open Libraries   ***/
  77.  
  78.     if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary
  79.                         ("intuition.library",OS_VER)))
  80.         cleanup();
  81.  
  82.     if (!(GfxBase = OpenLibrary("graphics.library",OS_VER)))
  83.         error("Can't open v37+ of graphics library",ERR_FATAL,
  84. __LINE__,__FILE__);
  85.  
  86.     if (!(CxBase = OpenLibrary("commodities.library",OS_VER)))
  87.         error("Can't open v37+ of commodities library",ERR_FATAL,
  88. __LINE__,__FILE__);
  89.  
  90.     if (!(IconBase = OpenLibrary("icon.library",OS_VER)))
  91.         error("Can't open v37+ of icon library",ERR_FATAL,
  92. __LINE__,__FILE__);
  93.  
  94.     if (!(DiskfontBase = OpenLibrary("diskfont.library",36)))
  95.         error("Can't open v36+ of diskfont library",ERR_FATAL,
  96. __LINE__,__FILE__);
  97.  
  98.     if (!(UtilityBase = OpenLibrary("utility.library",OS_VER)))
  99.         error("Can't open v37+ of utility library",ERR_FATAL,
  100. __LINE__,__FILE__);
  101.  
  102.     if (!(AslBase = OpenLibrary("asl.library",OS_VER)))
  103.         error("Can't open v37+ of asl library",ERR_FATAL,
  104. __LINE__,__FILE__);
  105.  
  106.     if (!(IFFParseBase = OpenLibrary("iffparse.library",OS_VER)))
  107.         error("Can't open v37+ of iffparse library",ERR_FATAL,
  108. __LINE__,__FILE__);
  109.  
  110.     if (!(GadToolsBase = OpenLibrary("gadtools.library",OS_VER)))
  111.         error("Can't open v37+ of gadtools library",ERR_FATAL,
  112. __LINE__,__FILE__);
  113.  
  114.     /***   Open devices   ***/
  115.  
  116.     if (!(OpenDevice("console.device",-1,(struct IORequest *)&ioreq,0)))
  117.         ConsoleDevice = (struct Library *)ioreq.io_Device;
  118.     else
  119.         error("Can't open console device",ERR_FATAL,__LINE__,__FILE__);
  120.  
  121.     /***   Allocate main project structure ***/
  122.  
  123.     prj = (prj_p)malloc(sizeof(struct project));
  124.  
  125.     if (!prj)
  126.         error("Can't allocate main project structure",ERR_FATAL,
  127. __LINE__,__FILE__);
  128.  
  129.     /***   Set up defaults etc...   ***************************************/
  130.  
  131.     startup(argc,argv);
  132.  
  133.     /* Set up program as a commodity */
  134.  
  135.     commod_startup();
  136.  
  137.     /***   Main program   *************************************************/
  138.  
  139.     /* Startup delay to stop disk thrashing */
  140.  
  141.     Delay(prj->prefs.delay * 50);
  142.  
  143.     /* Read notefile */
  144.  
  145.     file_readnotes();
  146.  
  147.     /* If we haven't read in any notes and the user wants to quit */
  148.  
  149.     if ((prj->prefs.emptyquit) && (next_free_note() == 0))
  150.         cleanup();
  151.  
  152.     /* Open all the notes */
  153.  
  154.     for (l = NO_NOTES -1; l >= 0; l--) {
  155.         if (prj->notes[l]) {
  156.             note_open(prj->notes[l]);
  157.             note_refresh(prj->notes[l]);
  158.         }
  159.     }
  160.  
  161.     /* If user wants the Cx window */
  162.  
  163.     if (prj->prefs.cx_popup)
  164.         openwindowcommod();
  165.  
  166.     /***   Main events loop   *********************************************/
  167.  
  168.     events_mainloop();
  169.  
  170.     /**********************************************************************/
  171.  
  172.     /***   Exit and cleanup   *********************************************/
  173.  
  174.     cleanup();
  175.  
  176.     return(0);
  177. }
  178.  
  179. /*
  180. Function : void cleanup()
  181. Purpose : Deallocates all memory, closes all libraries etc...
  182. */
  183.  
  184. void cleanup()
  185. {
  186.     CxMsg *cxmsg;
  187.  
  188.     int l;
  189.  
  190.     if (prj) {
  191.         /* Close the commodities window */
  192.  
  193.         closewindowcommod();
  194.  
  195.         /* Close all notes */
  196.  
  197.         for (l = 0; l < NO_NOTES; l++) {
  198.             if (prj->notes[l])
  199.                 note_close(prj->notes[l],TRUE);
  200.         }
  201.  
  202.         /* Remove the commodities broker and all attached objects */
  203.  
  204.         if (prj->broker)
  205.             DeleteCxObjAll(prj->broker);
  206.  
  207.         /* Remove messages from broker port */
  208.  
  209.         if (prj->broker_msg_port) {
  210.             while(cxmsg = (CxMsg *)GetMsg(prj->broker_msg_port))
  211.                 ReplyMsg((struct Message *)cxmsg);
  212.         }
  213.  
  214.         if (prj->broker_msg_port)
  215.             DeleteMsgPort(prj->broker_msg_port);
  216.  
  217.         if (prj->main_msg_port)
  218.             DeleteMsgPort(prj->main_msg_port);
  219.  
  220.         /* If font opened */
  221.  
  222.         if (prj->prefs.font)
  223.             CloseFont(prj->prefs.font);
  224.  
  225.         /* Last of all, deallocate project structure */
  226.  
  227.         free((void *)prj);
  228.     }
  229.  
  230.     /* Close devices */
  231.  
  232.     if (ConsoleDevice)
  233.         CloseDevice((struct IORequest *)&ioreq);
  234.  
  235.     /* Close libraries */
  236.  
  237.     if (GadToolsBase)
  238.         CloseLibrary(GadToolsBase);
  239.  
  240.     if (IFFParseBase)
  241.         CloseLibrary(IFFParseBase);
  242.  
  243.     if (AslBase)
  244.         CloseLibrary(AslBase);
  245.  
  246.     if (UtilityBase)
  247.         CloseLibrary(UtilityBase);
  248.  
  249.     if (DiskfontBase)
  250.         CloseLibrary(DiskfontBase);
  251.  
  252.     if (IconBase)
  253.         CloseLibrary(IconBase);
  254.  
  255.     if (CxBase)
  256.         CloseLibrary(CxBase);
  257.  
  258.     if (GfxBase)
  259.         CloseLibrary(GfxBase);
  260.  
  261.     if (IntuitionBase)
  262.         CloseLibrary((struct Library *)IntuitionBase);
  263.  
  264.     exit(0);
  265. }
  266.  
  267.