home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / devices / clipboard / cbio.c next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  5.6 KB  |  218 lines

  1. /*
  2.  *  Program name:  cbio
  3.  *  Purpose:  Provide standard clipboard device interface routines
  4.  *            such as Open, Post, Read, Write, etc.
  5.  *            (link with clip.c example)
  6.  *
  7.  * Copyright (c) 1990 Commodore-Amiga, Inc.
  8.  *
  9.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  10.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  11.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  12.  * information on the correct usage of the techniques and operating system
  13.  * functions presented in this example.  The source and executable code of
  14.  * this example may only be distributed in free electronic form, via bulletin
  15.  * board or as part of a fully non-commercial and freely redistributable
  16.  * diskette.  Both the source and executable code (including comments) must
  17.  * be included, without modification, in any copy.  This example may not be
  18.  * published in printed form or distributed with any commercial product.
  19.  * However, the programming techniques and support routines set forth in
  20.  * this example may be used in the development of original executable
  21.  * software products for Commodore Amiga computers.
  22.  * All other rights reserved.
  23.  * This example is provided "as-is" and is subject to change; no warranties
  24.  * are made.  All use is at your own risk.  No liability or responsibility
  25.  * is assumed.
  26.  *
  27.  */
  28.  
  29. #include <exec/types.h>
  30. #include <exec/ports.h>
  31. #include <exec/io.h>
  32. #include <devices/clipboard.h>
  33.  
  34. #ifdef LATTICE
  35. #include <proto/all.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #endif
  40.  
  41. struct IOClipReq *clipboardIO = 0;
  42. struct MsgPort *clipboardMsgPort= 0;
  43. struct MsgPort *satisfyMsgPort = 0;
  44.  
  45. long CBOpen(long);
  46. void CBClose(void);
  47. void writeLong(long *);
  48. void CBCutS(UBYTE *);
  49. long CBPasteS(UBYTE *);
  50. long CBPost(void);
  51. long CBCurrentReadID(void);
  52. long CBCurrentWriteID(void);
  53. BOOL CBCheckSatisfy(long *);
  54. void CBSatisfyPost(UBYTE *);
  55. void CBCut(UBYTE *,long);
  56.  
  57. long CBOpen(unit)
  58. long unit;
  59. {
  60.     long error;
  61.  
  62.     /* open the clipboard device */
  63.     clipboardMsgPort = CreatePort(0L,0L);
  64.     satisfyMsgPort = CreatePort(0L,0L);
  65.  
  66.     clipboardIO=(struct IOClipReq *)
  67.     CreateExtIO(clipboardMsgPort,sizeof(struct IOClipReq));
  68.  
  69.     if ((error = OpenDevice("clipboard.device", unit, clipboardIO, 0)))
  70.     return(error);
  71.     return(0);
  72. }
  73.  
  74. void CBClose()
  75. {
  76.     CloseDevice(clipboardIO);
  77.     DeletePort(satisfyMsgPort);
  78.     DeletePort(clipboardMsgPort);
  79.     DeleteExtIO(clipboardIO);
  80. }
  81.  
  82. void CBCut(stream, length)
  83. UBYTE *stream;
  84. long length;
  85. {
  86.     clipboardIO->io_Command = CMD_WRITE;
  87.     clipboardIO->io_Data = stream;
  88.     clipboardIO->io_Length = length;
  89.     clipboardIO->io_Offset = 0;
  90.     clipboardIO->io_ClipID = 0;
  91.     DoIO(clipboardIO);
  92.     clipboardIO->io_Command = CMD_UPDATE;
  93.     DoIO(clipboardIO);
  94. }
  95.  
  96.  
  97. void CBCutS(string)
  98. UBYTE *string;
  99. {
  100.     clipboardIO->io_ClipID = 0;
  101.     CBSatisfyPost(string);
  102. }
  103.  
  104. void writeLong(ldata)
  105. long *ldata;
  106. {
  107.     clipboardIO->io_Command = CMD_WRITE;
  108.     clipboardIO->io_Data = (char *)ldata;
  109.     clipboardIO->io_Length = 4;
  110.     DoIO(clipboardIO);
  111. }
  112.  
  113. void CBSatisfyPost(string)
  114. UBYTE *string;
  115. {
  116.     long length,slen=strlen(string);
  117.     BOOL odd = (slen & 1); /* pad byte flag */
  118.  
  119.     length= (odd) ? slen+1 : slen;
  120.     clipboardIO->io_Offset = 0;
  121.  
  122.     writeLong((long *)"FORM");          /* "FORM" */
  123.     length += 12;
  124.     writeLong(&length);             /* #  */
  125.     writeLong((long *)"FTXT");          /* "FTXT" for example */
  126.     writeLong((long *)"CHRS");          /* "CHRS" for example */
  127.     writeLong(&slen);                           /* #  (length of string) */
  128.  
  129.     clipboardIO->io_Command = CMD_WRITE;
  130.     clipboardIO->io_Data = (char *)string;
  131.     clipboardIO->io_Length = slen;      /* length of string */
  132.     DoIO(clipboardIO);              /* text string */
  133.  
  134.     if(odd)
  135.     {
  136.         clipboardIO->io_Command = CMD_WRITE;
  137.         clipboardIO->io_Data = "";
  138.         clipboardIO->io_Length = 1;
  139.         DoIO(clipboardIO);          /* pad byte */
  140.     }
  141.     clipboardIO->io_Command = CMD_UPDATE;
  142.     DoIO(clipboardIO);
  143. }
  144.  
  145.  
  146. long CBPasteS(string)
  147. UBYTE *string;
  148. {
  149.     long length,slen;
  150.     long len[5];
  151.  
  152.     clipboardIO->io_Command = CMD_READ; /* assume FORM # FTXTCHRS # */
  153.     clipboardIO->io_ClipID = 0;
  154.     clipboardIO->io_Offset = 0;     /* offset must be 0 on initial read */
  155.     clipboardIO->io_Data = (char *)len; 
  156.     clipboardIO->io_Length = 20;
  157.     DoIO(clipboardIO);
  158.  
  159.     length=len[1];  /* the length of the cut */
  160.     slen=len[4];    /* the length of string */
  161.  
  162.     clipboardIO->io_Data = (char *)string;  /* read the string */
  163.     clipboardIO->io_Length = slen;
  164.     DoIO(clipboardIO);
  165.  
  166.     clipboardIO->io_Offset  += length;  /* read past end of current clip to */
  167.     clipboardIO->io_Length = 1;     /* close clip for reading */
  168.     clipboardIO->io_Data = 0;
  169.     DoIO(clipboardIO);
  170.  
  171.     string[slen] = '\0';   /* NULL terminate the string */
  172.     return(slen);
  173. }
  174.  
  175. long CBPost()
  176. {
  177.     clipboardIO->io_Command = CBD_POST;
  178.     clipboardIO->io_Data = (char *)satisfyMsgPort;
  179.     clipboardIO->io_ClipID = 0;
  180.     DoIO(clipboardIO);
  181.     return(clipboardIO->io_ClipID);
  182. }
  183.  
  184. long CBCurrentReadID()
  185. {
  186.     clipboardIO->io_Command = CBD_CURRENTREADID;
  187.     DoIO(clipboardIO);
  188.     return(clipboardIO->io_ClipID);
  189. }
  190.  
  191. long CBCurrentWriteID()
  192. {
  193.     clipboardIO->io_Command = CBD_CURRENTWRITEID;
  194.     DoIO(clipboardIO);
  195.     return(clipboardIO->io_ClipID);
  196. }
  197.  
  198. BOOL CBCheckSatisfy(idVar)
  199. long *idVar;
  200. {
  201.     struct SatisfyMsg *sm;
  202.  
  203.     if (*idVar == 0)
  204.     return(TRUE);
  205.     if (*idVar < CBCurrentWriteID())
  206.     {
  207.     *idVar = 0;
  208.     return(TRUE);
  209.     }
  210.     if (sm = (struct SatisfyMsg *)GetMsg(satisfyMsgPort))
  211.     {
  212.     if (*idVar == sm->sm_ClipID)
  213.         return(TRUE);
  214.     }
  215.     return(FALSE);
  216. }
  217.  
  218.