home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / dev / c / rkrm / clipboard / clipdemo.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  9KB  |  372 lines

  1. /*
  2.  * Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  * 
  4.  * This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5.  * use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6.  * published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  * 
  8.  * The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9.  * information on the correct usage of the techniques and operating system 
  10.  * functions presented in these examples.  The source and executable code 
  11.  * of these examples may only be distributed in free electronic form, via 
  12.  * bulletin board or as part of a fully non-commercial and freely 
  13.  * redistributable diskette.  Both the source and executable code (including 
  14.  * comments) must be included, without modification, in any copy.  This 
  15.  * example may not be published in printed form or distributed with any
  16.  * commercial product.  However, the programming techniques and support
  17.  * routines set forth in these examples may be used in the development
  18.  * of original executable software products for Commodore Amiga computers.
  19.  * 
  20.  * All other rights reserved.
  21.  * 
  22.  * This example is provided "as-is" and is subject to change; no
  23.  * warranties are made.  All use is at your own risk. No liability or
  24.  * responsibility is assumed.
  25.  *
  26.  ***************************************************************************
  27.  *
  28.  * Clipdemo.c
  29.  *
  30.  * Demonstrate use of clipboard I/O.  Uses general functions
  31.  * provided in cbio.c.  Important note: when this code is run 
  32.  * with older versions of the Amiga OS (i.e., before V36) a memory 
  33.  * loss of 536 bytes will occur due to bugs in the clipboard device.
  34.  *
  35.  * Compile with SAS C 5.10: LC -b1 -cfistq -v -y -L+cbio.o
  36.  *
  37.  * Run from CLI only
  38.  */
  39.  
  40. #include "exec/types.h"
  41. #include "exec/ports.h"
  42. #include "exec/io.h"
  43. #include "exec/memory.h"
  44. #include "devices/clipboard.h"
  45. #include "libraries/dosextens.h"
  46. #include "libraries/dos.h"
  47.  
  48. #include "cb.h"
  49.  
  50. #include <clib/exec_protos.h>
  51. #include <clib/alib_protos.h>
  52.  
  53. #include <stdlib.h>
  54. #include <stdio.h>
  55. #include <string.h>
  56.  
  57. #ifdef LATTICE
  58. int CXBRK(void) { return(0); }  /* Disable SAS CTRL/C handling */
  59. int chkabort(void) { return(0); }  /* really */
  60. #endif
  61.  
  62. #define FORGETIT 0
  63. #define READIT   1
  64. #define WRITEIT  2
  65. #define POSTIT   3
  66.  
  67. /* prototypes */
  68.  
  69. int ReadClip( void );           /* Demonstrate reading clipboard data      */
  70. int WriteClip( char * );        /* Demonstrate write to clipboard        */
  71. int PostClip( char * );         /* Demonstrate posting data to clipboard */
  72.  
  73. void main( USHORT, char **);
  74.  
  75. char message[] = "\
  76. \nPossible switches are:\n\n\
  77. -r            Read, and output contents of clipboard.\n\
  78. -w [string]   Write string to clipboard.\n\n\
  79. -p [string]   Write string to clipboard using the clipboard POST mechanism.\n\n\
  80.               The Post can be satisfied by reading data from\n\
  81.               the clipboard.  Note that the message may never\n\
  82.               be received if some other application posts, or\n\
  83.               performs an immediate write to the clipboard.\n\n\
  84.               To run this test you must run two copies of this example.\n\
  85.               Use the -p switch with one to post data, and the -r switch\n\
  86.               with another to read the data.\n\n\
  87.               The process can be stopped by using the BREAK command,\n\
  88.               in which case this example checks the CLIP write ID\n\
  89.               to determine if it should write to the clipboard before\n\
  90.               exiting.\n\n";
  91.  
  92.  
  93.  
  94. void main(argc,argv)
  95. USHORT argc;
  96. char **argv;
  97. {
  98.  
  99. int todo;
  100. char *string;
  101.  
  102. todo = FORGETIT;
  103.  
  104. if (argc)     /* from CLI ? */
  105.     {
  106.  
  107.     /* Very simple code to parse for arguments - will suffice for
  108.      * the sake of this example
  109.      */
  110.  
  111.     if (argc > 1)
  112.        {
  113.        if (!(strcmp(argv[1],"-r")))
  114.            todo = READIT;
  115.        if (!(strcmp(argv[1],"-w")))
  116.            todo = WRITEIT;
  117.        if (!(strcmp(argv[1],"-p")))
  118.            todo = POSTIT;
  119.  
  120.        string = NULL;
  121.  
  122.        if (argc > 2)
  123.            string=argv[2];
  124.  
  125.        }
  126.  
  127.     switch (todo)
  128.             {
  129.  
  130.             case READIT:
  131.  
  132.                  ReadClip();
  133.                  break;
  134.  
  135.             case POSTIT:
  136.  
  137.                  PostClip(string);
  138.                  break;
  139.  
  140.             case WRITEIT:
  141.  
  142.                  WriteClip(string);
  143.                  break;
  144.  
  145.             default:
  146.  
  147.                  printf("%s",message);
  148.                  break;
  149.  
  150.             }
  151.  
  152.  
  153.     }
  154. }
  155.  
  156. /*
  157.  * Read, and output FTXT in the clipboard.
  158.  *
  159.  */
  160.  
  161. ReadClip()
  162. {
  163. struct IOClipReq *ior;
  164. struct cbbuf *buf;
  165.  
  166.  
  167. /* Open clipboard.device unit 0 */
  168.  
  169. if (ior=CBOpen(0L))
  170.     {
  171.  
  172.     /* Look for FTXT in clipboard */
  173.  
  174.     if (CBQueryFTXT(ior))
  175.         {
  176.  
  177.         /* Obtain a copy of the contents of each CHRS chunk */
  178.  
  179.         while (buf=CBReadCHRS(ior))
  180.               {
  181.               /* Process data */
  182.  
  183.               printf("%s\n",buf->mem);
  184.  
  185.               /* Free buffer allocated by CBReadCHRS() */
  186.  
  187.               CBFreeBuf(buf);
  188.               }
  189.  
  190.         /* The next call is not really needed if you are sure */
  191.         /* you read to the end of the clip.                   */
  192.  
  193.         CBReadDone(ior);
  194.         }
  195.     else
  196.         {
  197.         puts("No FTXT in clipboard");
  198.         }
  199.  
  200.     CBClose(ior);
  201.     }
  202.  
  203. else
  204.     {
  205.     puts("Error opening clipboard unit 0");
  206.     }
  207.  
  208. return(0L);
  209. }
  210.  
  211. /*
  212.  * Write a string to the clipboard
  213.  *
  214.  */
  215.  
  216. WriteClip(string)
  217. char *string;
  218. {
  219.  
  220. struct IOClipReq *ior;
  221.  
  222. if (string == NULL)
  223.     {
  224.     puts("No string argument given");
  225.     return(0L);
  226.     }
  227.  
  228. /* Open clipboard.device unit 0 */
  229.  
  230. if (ior = CBOpen(0L))
  231.     {
  232.     if (!(CBWriteFTXT(ior,string)))
  233.         {
  234.         printf("Error writing to clipboard: io_Error = %ld\n",ior->io_Error);
  235.         }
  236.     CBClose(ior);
  237.     }
  238. else
  239.     {
  240.     puts("Error opening clipboard.device");
  241.     }
  242.  
  243. return(0);
  244. }
  245.  
  246.  
  247. /*
  248.  * Write a string to the clipboard using the POST mechanism
  249.  *
  250.  * The POST mechanism can be used by applications which want to
  251.  * defer writing text to the clipboard until another application
  252.  * needs it (by attempting to read it via CMD_READ).  However
  253.  * note that you still need to keep a copy of the data until you
  254.  * receive a SatisfyMsg from the clipboard.device, or your program
  255.  * exits.
  256.  *
  257.  * In most cases it is easier to write the data immediately.
  258.  *
  259.  * If your program receives the SatisfyMsg from the clipboard.device,
  260.  * you MUST write some data.  This is also how you reply to the message.
  261.  *
  262.  * If your program wants to exit before it has received the SatisfyMsg,
  263.  * you must check the io_ClipID field at the time of the post against
  264.  * the current post ID which is obtained by sending the CBD_CURRENTWRITEID
  265.  * command.
  266.  *
  267.  * If the value in io_ClipID (returned by CBD_CURRENTWRITEID) is greater
  268.  * than your post ID, it means that some other application has performed
  269.  * a post, or immediate write after your post, and that you're application
  270.  * will never receive the SatisfyMsg.
  271.  *
  272.  * If the value in io_ClipID (returned by CBD_CURRENTWRITEID) is equal
  273.  * to your post ID, then you must write your data, and send CMD_UPDATE
  274.  * before exiting.
  275.  *
  276.  */
  277.  
  278. PostClip(string)
  279. char *string;
  280. {
  281.  
  282. struct MsgPort *satisfy;
  283. struct SatisfyMsg *sm;
  284. struct IOClipReq *ior;
  285. int mustwrite;
  286. ULONG postID;
  287.  
  288. if (string == NULL)
  289.     {
  290.     puts("No string argument given");
  291.     return(0L);
  292.     }
  293.  
  294. if (satisfy = CreatePort(0L,0L))
  295.     {
  296.  
  297.     /* Open clipboard.device unit 0 */
  298.  
  299.     if (ior = CBOpen(0L))
  300.         {
  301.         mustwrite = FALSE;
  302.  
  303.         /* Notify clipboard we have data */
  304.  
  305.         ior->io_Data    = (STRPTR)satisfy;
  306.         ior->io_ClipID  = 0L;
  307.         ior->io_Command = CBD_POST;
  308.         DoIO( (struct IORequest *) ior);
  309.  
  310.         postID = ior->io_ClipID;
  311.  
  312.         printf("\nClipID = %ld\n",postID);
  313.  
  314.         /* Wait for CTRL-C break, or message from clipboard */
  315.         Wait(SIGBREAKF_CTRL_C|(1L << satisfy->mp_SigBit));
  316.  
  317.         /* see if we got a message, or a break */
  318.         puts("Woke up");
  319.  
  320.  
  321.         if (sm = (struct SatisfyMsg *)GetMsg(satisfy))
  322.             {
  323.             puts("Got a message from the clipboard\n");
  324.  
  325.             /* We got a message - we MUST write some data */
  326.             mustwrite = TRUE;
  327.             }
  328.         else
  329.             {
  330.             /* Determine if we must write before exiting by
  331.              * checking to see if our POST is still valid
  332.              */
  333.  
  334.             ior->io_Command = CBD_CURRENTWRITEID;
  335.             DoIO( (struct IORequest *) ior);
  336.  
  337.             printf("CURRENTWRITEID = %ld\n",ior->io_ClipID);
  338.  
  339.             if (postID >= ior->io_ClipID)
  340.                 mustwrite = TRUE;
  341.  
  342.             }
  343.  
  344.         /* Write the string of text */
  345.  
  346.         if (mustwrite)
  347.             {
  348.             if (!(CBWriteFTXT(ior,string)))
  349.                 puts("Error writing to clipboard");
  350.             }
  351.         else
  352.             {
  353.             puts("No need to write to clipboard");
  354.             }
  355.  
  356.         CBClose(ior);
  357.         }
  358.     else
  359.         {
  360.         puts("Error opening clipboard.device");
  361.         }
  362.  
  363.     DeletePort(satisfy);
  364.     }
  365. else
  366.     {
  367.     puts("Error creating message port");
  368.     }
  369.  
  370. return(0);
  371. }
  372.