home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0935.lha / ClipWindow / ClipWindow.c < prev    next >
C/C++ Source or Header  |  1993-12-20  |  12KB  |  372 lines

  1. char cc1[] = "\0$VER: ClipWindow 1.0  August 22, 1993";
  2. char cc2[] = "Jim Harvey, 18538 Inkster, Redford Mich, 48240";
  3. /* An AppWindow that stuffs the clipboard from Project Icons.
  4.    Text can be stored in either a text file associated with
  5.    the Project icon, or directly in the icon ToolTypes.  A
  6.    Project icon with the word RESET as the default tool will
  7.    flush the clipboard.
  8.  
  9.    Requires the CBIO.C and CB.H ClipBoard IO library files from
  10.    the ROM Kernal Reference Manual - Devices chapter 3.  These
  11.    are available on Fish Disk 741 in the RKRM_Devices archive.
  12.  
  13.    Makefile for Manx 5.0D (with debug) is:
  14.  
  15.     ClipWindow: ClipWindow.o Cbio.o
  16.      ln -g ClipWindow Cbio -lc
  17.  
  18.     ClipWindow.o: ClipWindow.c
  19.      cc -wl -pl -bs ClipWindow.c
  20.  
  21.     Cbio.o: Cbio.c
  22.      cc -wn -pl -bs Cbio.c
  23.  
  24.    Revisions: 1.0 initial release August 22, 1993
  25. */
  26.  
  27. #define BUFFSIZE 8192L        /* maxsize of clipboard */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. #include <functions.h>
  33. #include <exec/types.h>        /* Need this for the Amiga variable types  */
  34. #include <workbench/workbench.h>/* This has DiskObject and AppWindow       */
  35. #include <workbench/startup.h>    /* This has WBStartup and WBArg structs    */
  36. #include <exec/libraries.h>    /* Need this to check library versions     */
  37.  
  38. #include <stdio.h>
  39.  
  40. #include <clib/intuition_protos.h>
  41. #include <clib/exec_protos.h>
  42. #include <clib/wb_protos.h>
  43.  
  44. /* includes for clipboard */
  45.  
  46. #include "exec/ports.h"
  47. #include "exec/io.h"
  48. #include "exec/memory.h"
  49. #include "devices/clipboard.h"
  50. #include "cb.h"
  51.  
  52. #include <clib/exec_protos.h>
  53. #include <clib/alib_protos.h>
  54.  
  55. struct IntuitionBase *IntuitionBase;
  56. struct WorkbenchBase *WorkbenchBase;
  57. struct IconBase *IconBase;
  58.  
  59. struct MsgPort *myport;
  60. struct AppWindow *appwin;
  61. struct IntuiMessage *imsg;
  62. struct AppMessage *appmsg;
  63. struct WBArg *wbargs;
  64.  
  65. char *conname = "CON:200/30/500/100/ Clip Window /CLOSE";
  66. FILE *conwin = NULL;        /* will be handle for the above window */
  67. struct Screen *tstscr;        /* dummy for finding ptr to window */
  68. struct Window *homewindow;    /* pointer to AppWindow opened */
  69. struct MsgPort *myport = NULL;    /* message port for AppWindow */
  70. struct DiskObject *dobject;
  71. char **tooltypes = NULL;
  72. ULONG id = 1, userdata = 0;
  73.  
  74. /* function prototypes */
  75. void cleanexit (char *, LONG);
  76. void openup ();
  77. void cleanup (void);
  78. void message (char *);
  79. BOOL getToolTypes (struct WBArg *, char **);
  80.  
  81. LONG olddir = -1;        /* will remember if we changed directory */
  82. extern int Enable_Abort;    /* MANX C Reference abort enable */
  83.  
  84. void
  85. main (int argc, char **argv)
  86. {
  87.   ULONG winsig, appwinsig, signals;    /* Wait() signals */
  88.   BOOL done = FALSE;
  89.   int x, i;            /* gen purpose counters */
  90.   char tempname[33];        /* holds a filename */
  91.   char tempstring[] = "\n";    /* holds a dummy line to clipboard */
  92.   struct IOClipReq *ior = NULL;    /* IO request for clipboard use */
  93.   struct cbbuf *bigbuf = NULL;    /* holds contents of retrieved clipboard */
  94.   char *newstuff = NULL;    /* holds contents of dropped icon tooltypes */
  95.   char *stuffend = NULL;    /* point to position of next avail space in newstuff */
  96.   char *deftool = NULL;        /* Default icon Tool */
  97.   FILE *textfile;        /* for text file associated with Project icon */
  98.   int inchar, laschar;        /* for reading associated text file */
  99.  
  100.   Enable_Abort = 0;        /* turn off CTRL-C */
  101.   openup ();            /* gets things started */
  102.  
  103. InitializeClip:        /* target for a RESET */
  104.  
  105.   /* initalize clipboard zero by writing a short string */
  106.   /* note: must write at least one byte */
  107.  
  108.   if ((ior = CBOpen (0l)) == NULL)
  109.     cleanexit ("Can't open ClipBoard\n", RETURN_FAIL);
  110.  
  111.   if ((CBWriteFTXT (ior, tempstring)) == FALSE)
  112.     cleanexit ("Can't write to ClipBoard\n", RETURN_FAIL);
  113.  
  114.   CBClose (ior);
  115.  
  116.   fprintf (conwin, " Drag Project icons here, tooltypes\n");
  117.   fprintf (conwin, "      will be appended to ClipBoard\n");
  118.  
  119.   winsig = 1L << homewindow->UserPort->mp_SigBit;    /* for close gadget */
  120.   appwinsig = 1L << myport->mp_SigBit;    /* for AppWindow */
  121.  
  122.   while (!done) {
  123.     signals = Wait (winsig | appwinsig);    /* Wait for IDCMP and AppMessages */
  124.  
  125.     if (signals & winsig) {    /** Got an IDCMP message **/
  126.  
  127.       while (imsg = (struct IntuiMessage *) GetMsg (homewindow->UserPort)) {
  128.     if (imsg->Class = CLOSEWINDOW)    /* check for close button */
  129.       done = TRUE;
  130.     ReplyMsg ((struct Message *) imsg);    /* spit it back */
  131.       }
  132.     }
  133.     if (signals & appwinsig) {    /** Got an AppMessage **/
  134.  
  135.       if ((stuffend = newstuff =
  136.        AllocMem (BUFFSIZE, MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
  137.     cleanexit ("Can't allocate memory for buffer\n", RETURN_FAIL);
  138.  
  139.       /* read entire clipboard into struct cbbuf buffer */
  140.       if ((ior = CBOpen (0l)) == NULL)
  141.     cleanexit ("Can't open ClipBoard\n", RETURN_FAIL);
  142.  
  143.       if (CBQueryFTXT (ior)) {    /* Look for FTXT */
  144.     bigbuf = CBReadCHRS (ior);    /* Obtain a copy of the contents */
  145.     CBReadDone (ior);    /* must read to EOF */
  146.     CBClose (ior);
  147.       }
  148.       else {
  149.     CBClose (ior);
  150.     cleanexit ("ClipBoard is Empty!\n", RETURN_FAIL);
  151.       }
  152.  
  153.       /* copy the contents to temp buffer */
  154.       strcpy (newstuff, (char *) bigbuf->mem);
  155.  
  156.       /* if first time, point stuffend to 1st data byte, */
  157.       /*if > first point to byte following the string */
  158.       x = strlen (newstuff);
  159.       stuffend += (x > 1) ? x : x - 1;
  160.  
  161.       /* read through all messages (may be more than one if a multiple select) */
  162.       while (appmsg = (struct AppMessage *) GetMsg (myport)) {
  163.  
  164.     /* get information for this icon */
  165.     for (x = 0, wbargs = appmsg->am_ArgList;
  166.          x < appmsg->am_NumArgs;
  167.          x++, wbargs++) {
  168.  
  169.       if (!(*wbargs->wa_Name)) {
  170.         message (" Sorry, must be a Project Icon!\n");
  171.         break;
  172.       }
  173.  
  174.       strcpy (tempname, wbargs->wa_Name);    /* copy name of icon */
  175.  
  176.       /* CD to directory of this icon */
  177.       olddir = -1;        /* will remember if CD was necessary */
  178.       if ((wbargs->wa_Lock) && (*wbargs->wa_Name))
  179.         olddir = CurrentDir (wbargs->wa_Lock);    /* remember old directory */
  180.  
  181.       if (getToolTypes (wbargs, &deftool)) {    /* now parse tooltypes */
  182.  
  183.         if (dobject->do_Type != WBPROJECT) {
  184.           message (" Sorry, must be a Project Icon!\n");
  185.           break;        /* proceed to process next icon */
  186.         }
  187.  
  188.         /* see if user wants to flush clipboard */
  189.         if (!strcmp (deftool, "RESET") || !strcmp (deftool, "reset")) {
  190.           fprintf (conwin,
  191.                "\f      %c3;7m%s%c0m\n", 0x9b, "* Resetting Clipboard *", 0x9b);
  192.           FreeDiskObject (dobject);    /* Free the diskobject we got */
  193.  
  194.           /* Clear away any messages that arrived at the last moment */
  195.           while (appmsg = (struct AppMessage *) GetMsg (myport))
  196.         ReplyMsg ((struct Message *) appmsg);    /* spit it back */
  197.           while (imsg = (struct IntuiMessage *) GetMsg (homewindow->UserPort))
  198.         ReplyMsg ((struct Message *) imsg);    /* spit it back */
  199.  
  200.           if (bigbuf)
  201.         CBFreeBuf (bigbuf);    /* discard clip read buffer */
  202.           if (newstuff)
  203.         FreeMem (newstuff, BUFFSIZE);    /* discard clip write buffer */
  204.           goto InitializeClip;    /* start over */
  205.         }
  206.  
  207.         /* now check for a text file associated with the Project icon */
  208.         if ((textfile = fopen (tempname, "r")) != NULL) {
  209.  
  210.           laschar = 0;
  211.           fprintf (conwin, "%c1;7m%s:%c0m", 0x9b, tempname, 0x9b);
  212.           /* associated textfile was found, append contents to temp buffer */
  213.           while ((inchar = fgetc (textfile)) != EOF) {
  214.         *stuffend++ = (char) inchar;
  215.         if (laschar == 0x0a)
  216.           fprintf (conwin, "%c1;7m%s:%c0m", 0x9b, tempname, 0x9b);
  217.         fputc (inchar, conwin);    /* echo */
  218.         laschar = inchar;    /* remember last real character */
  219.           }
  220.           if (laschar != 0x0a) {
  221.         *stuffend++ = '\n';    /* ensure last line has line feed */
  222.         fputc (0x0a, conwin);    /* echo */
  223.           }
  224.           fclose (textfile);
  225.         }
  226.  
  227.         /* no associated text file found, get info from tooltypes */
  228.         else {
  229.           for (i = 0; tooltypes[i]; i++) {    /* exit on NULL tooltype */
  230.         fprintf (conwin, "%c1m%s:%c0m%s\n", 0x9b, tempname, 0x9b, tooltypes[i]);
  231.         strcpy (stuffend, tooltypes[i]);    /* append tooltype to buffer */
  232.         stuffend += strlen (tooltypes[i]);    /* pos to end string */
  233.         *stuffend++ = '\n';    /* append a line feed */
  234.         if ((int) (stuffend - newstuff) >= BUFFSIZE)    /* check memory */
  235.           cleanexit ("Buffer Memory Overflow\n", RETURN_FAIL);
  236.           }
  237.         }
  238.       }
  239.       FreeDiskObject (dobject);    /* Free the diskobject we got */
  240.  
  241.       if (olddir != -1)
  242.         CurrentDir (olddir);/* cd back home if necessary */
  243.     }
  244.     ReplyMsg ((struct Message *) appmsg);    /* spit message back */
  245.       }
  246.  
  247.       /** write entire appended buffer to clipboard **/
  248.       if ((ior = CBOpen (0l)) == NULL)
  249.     cleanexit ("Can't open ClipBoard\n", RETURN_FAIL);
  250.  
  251.       if (!CBWriteFTXT (ior, newstuff))
  252.     cleanexit ("ClipBoard rewrite error!\n", RETURN_FAIL);
  253.  
  254.  
  255.       /* Free buffer allocated by CBReadCHRS() */
  256.       if (bigbuf)
  257.     CBFreeBuf (bigbuf);
  258.       /* Free buffer used for incoming tooltypes */
  259.       if (newstuff)
  260.     FreeMem (newstuff, BUFFSIZE);
  261.     }
  262.   }                /* done */
  263.  
  264.   /* Clear away any messages that arrived at the last moment */
  265.   while (appmsg = (struct AppMessage *) GetMsg (myport))
  266.     ReplyMsg ((struct Message *) appmsg);
  267.   cleanup ();
  268. }
  269.  
  270. void
  271. openup ()
  272. {
  273.   if ((IntuitionBase = (struct IntuitionBase *)
  274.        OpenLibrary ((STRPTR) "intuition.library", 0l)) == NULL)
  275.     cleanexit ("Can't open Intuition\n", RETURN_FAIL);
  276.  
  277.   if ((WorkbenchBase = (struct WorkbenchBase *)
  278.        OpenLibrary ((STRPTR) "workbench.library", 37)) == NULL)
  279.     cleanexit ("Can't open Workbench.library\n", RETURN_FAIL);
  280.  
  281.   if ((IconBase = (struct IconBase *)
  282.        OpenLibrary ((STRPTR) "icon.library", 37)) == NULL)
  283.     cleanexit ("Can't open Icon.library\n", RETURN_FAIL);
  284.  
  285.   if ((conwin = fopen (conname, "r+")) == NULL)
  286.     cleanexit ("Can't open output window\n", RETURN_FAIL);
  287.  
  288.   if ((tstscr =            /* get copy of wkbnch screen */
  289.        AllocMem (sizeof (struct Screen), MEMF_FAST)) == NULL)
  290.       cleanexit ("Memory too low for screen\n", RETURN_FAIL);
  291.  
  292.   if ((GetScreenData
  293.      (tstscr, sizeof (struct Screen), (ULONG) WBENCHSCREEN, NULL)) == FALSE)
  294.       cleanexit ("Can't get WorkBench screen data\n", RETURN_FAIL);
  295.   homewindow = tstscr->FirstWindow;
  296.  
  297.   if (strcmp ((char *) homewindow->Title, " Clip Window ") != 0)
  298.     cleanexit ("Wrong Window found\n", RETURN_FAIL);
  299.  
  300.   /* this creates a message port automatically */
  301.   ModifyIDCMP (homewindow, IDCMP_CLOSEWINDOW);
  302.  
  303.   /* The CreateMsgPort() function is in Exec version 37 and later only */
  304.   if ((myport = CreateMsgPort ()) == NULL)
  305.     cleanexit ("Can't get Message Port\n", RETURN_FAIL);
  306.  
  307.   if ((appwin = AddAppWindowA (id, userdata, homewindow, myport, NULL)) == NULL)
  308.     cleanexit ("Appwindow atachment failed\n", RETURN_FAIL);
  309. }
  310.  
  311. void
  312. cleanexit (char *s, LONG n)
  313. {
  314.   if (*s)
  315.     message (s);
  316.   Delay (100l);
  317.   cleanup ();
  318.   exit (n);
  319. }
  320.  
  321. void
  322. cleanup ()
  323. {
  324.   if (olddir != -1)
  325.     CurrentDir (olddir);    /* cd back home */
  326.   if (myport)
  327.     DeleteMsgPort (myport);
  328.   if (conwin)
  329.     RemoveAppWindow (appwin);
  330.   if (conwin)
  331.     fclose (conwin);
  332.   if (tstscr)
  333.     FreeMem (tstscr, sizeof (struct Screen));
  334.   if (IconBase)
  335.     CloseLibrary ((struct Library *) IconBase);
  336.   if (WorkbenchBase)
  337.     CloseLibrary ((struct Library *) WorkbenchBase);
  338.   if (IntuitionBase)
  339.     CloseLibrary ((struct Library *) IntuitionBase);
  340. }
  341.  
  342. void
  343. message (char *s)
  344. {
  345.   if (conwin)
  346.     fprintf (conwin, s, strlen (s));
  347.   else
  348.     printf (s, strlen (s));
  349. }
  350.  
  351.  
  352. /*  Extract all ToolTypes */
  353. BOOL
  354. getToolTypes (struct WBArg *wbarg, char **defaulttool)
  355. {
  356.   BOOL success = FALSE;
  357.  
  358.   dobject = GetDiskObject ((UBYTE *) (wbarg->wa_Name));
  359.   if ((*wbarg->wa_Name) && dobject) {
  360.     tooltypes = (char **) dobject->do_ToolTypes;
  361.  
  362.     *defaulttool = dobject->do_DefaultTool;
  363.  
  364.     success = TRUE;
  365.   }
  366.   else if (!(*wbarg->wa_Name))
  367.     fprintf (conwin, "  Must be a disk or drawer icon\n");
  368.   else
  369.     fprintf (conwin, "  Can't find any DiskObject (icon) for this WBArg\n");
  370.   return (success);
  371. }
  372.