home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / rkrm / clipboard / changehook_test.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  4KB  |  166 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.  * Changehook_Test.c
  29.  *
  30.  * Demonstrate the use of CBD_CHANGEHOOK command.
  31.  * The program will set a hook and wait for the clipboard data to change.
  32.  * You must put something in the clipboard in order for it to return.
  33.  *
  34.  * Compile with SAS C 5.10: LC -cfist -v -y -L+Hookface.o+cbio.o
  35.  *
  36.  * Requires Kickstart 36 or greater.
  37.  *
  38.  * Run from CLI only
  39.  */
  40.  
  41. #include <exec/types.h>
  42. #include <exec/memory.h>
  43. #include <exec/ports.h>
  44. #include <exec/tasks.h>
  45. #include <exec/io.h>
  46. #include <devices/clipboard.h>
  47. #include <dos/dos.h>
  48. #include <utility/hooks.h>
  49. #include "cb.h"
  50.  
  51. #include <clib/macros.h>
  52. #include <clib/alib_protos.h>
  53. #include <clib/exec_protos.h>
  54.  
  55. #include <stdio.h>
  56. #include <string.h>
  57.  
  58.  
  59. LONG version = 1L;
  60.  
  61. extern ULONG SysBase, DOSBase;
  62.  
  63. /* Data to pass around with the clipHook */
  64. struct CHData
  65. {
  66.     struct Task *ch_Task;
  67.     LONG ch_ClipID;
  68. };
  69.  
  70. struct MsgPort *clip_port;
  71. struct Hook hook;
  72. struct CHData ch;
  73.  
  74. ULONG clipHook (struct Hook * h, VOID * o, struct ClipHookMsg * msg)
  75. {
  76. struct CHData *ch = (struct CHData *) h->h_Data;
  77.  
  78. if (ch)
  79.    {
  80.    /* Remember the ID of clip */
  81.    ch->ch_ClipID = msg->chm_ClipID;
  82.  
  83.    /* Signal the task that started the hook */
  84.    Signal (ch->ch_Task, SIGBREAKF_CTRL_E);
  85.    }
  86.  
  87. return (0);
  88. }
  89.  
  90. struct IOClipReq *OpenCB (LONG unit)
  91. {
  92. struct IOClipReq *clipIO;
  93.  
  94. /* Open clipboard unit 0 */
  95.  
  96. if (clipIO = CBOpen( 0L ))
  97.     {
  98.     ULONG hookEntry ();
  99.  
  100.     /* Fill out the IORequest */
  101.     clipIO->io_Data = (char *) &hook;
  102.     clipIO->io_Length = 1;
  103.     clipIO->io_Command = CBD_CHANGEHOOK;
  104.  
  105.     /* Set up the hook data */
  106.     ch.ch_Task = FindTask (NULL);
  107.  
  108.     /* Prepare the hook */
  109.     hook.h_Entry = hookEntry;
  110.     hook.h_SubEntry = clipHook;
  111.     hook.h_Data = &ch;
  112.  
  113.     /* Start the hook */
  114.     if (DoIO (clipIO))
  115.         printf ("unable to set hook\n");
  116.     else
  117.         printf ("hook set\n");
  118.  
  119.     /* Return success */
  120.     return ( clipIO );
  121.     }
  122.  
  123. /* return failure */
  124. return (NULL);
  125. }
  126.  
  127. void CloseCB (struct IOClipReq *clipIO)
  128. {
  129.  
  130. /* Fill out the IO request */
  131. clipIO->io_Data = (char *) &hook;
  132. clipIO->io_Length = 0;
  133. clipIO->io_Command = CBD_CHANGEHOOK;
  134.  
  135.     /* Stop the hook */
  136. if (DoIO (clipIO))
  137.     printf ("unable to stop hook\n");
  138. else
  139.     /* Indicate success */
  140.     printf ("hook is stopped\n");
  141.  
  142. CBClose(clipIO);
  143. }
  144.  
  145. main (int argc, char **argv)
  146. {
  147. struct IOClipReq *clipIO;
  148.  
  149. ULONG sig_rcvd;
  150.  
  151. printf ("Test v%ld\n", version);
  152.  
  153. if (clipIO=OpenCB (0L))
  154.     {
  155.     sig_rcvd = Wait ((SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_E));
  156.  
  157.     if (sig_rcvd & SIGBREAKF_CTRL_C)
  158.         printf ("^C received\n");
  159.  
  160.     if (sig_rcvd & SIGBREAKF_CTRL_E)
  161.         printf ("clipboard change, current ID is %ld\n", ch.ch_ClipID);
  162.  
  163.     CloseCB(clipIO);
  164.     }
  165. }
  166.