home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Workbench / dosdrivers / GTDriver.lha / Distrib / Programming / SetBut / SetBut.c next >
Encoding:
C/C++ Source or Header  |  1994-11-03  |  2.2 KB  |  103 lines

  1. #include <exec/types.h>
  2. #include <workbench/startup.h>
  3. #include <string.h>
  4. #include "/include/GTDriverMPC.h"
  5.  
  6. #include <proto/exec.h>
  7. #include <proto/dos.h>
  8.  
  9. void CloseAll(char *msg);
  10. short SendCommand(ULONG comm, ULONG data);
  11. void exit(int);
  12.  
  13. struct MsgPort *myport = NULL,*port = NULL,*repport = NULL;
  14. struct TabletCommand Tcmd;
  15. ULONG  Temp;
  16.  
  17. main(int argc, char **argv)
  18. {
  19.     short ret;
  20.     char *butname;
  21.     char buff[256];
  22.  
  23.     /*
  24.      * Create message port to get messages from driver
  25.      */
  26.     myport = CreateMsgPort();
  27.     if (! myport) CloseAll("Unable to create message port\n");
  28.  
  29.     /*
  30.      * Create Msg port to receive replies to command sent to the driver
  31.      */
  32.     repport = CreateMsgPort();
  33.     if (! repport) CloseAll("Unable to create message port\n");
  34.  
  35.  
  36.     /*
  37.      * Get PButton file name
  38.      */
  39.  
  40.      /* started from WB */
  41.     if (argc == 0) {
  42.         if (((struct WBStartup *)argv)->sm_NumArgs >= 2) {
  43.             if (! NameFromLock(((struct WBStartup *)argv)->sm_ArgList[1].wa_Lock,buff,256))
  44.                 CloseAll("Error: unable to get full filename\n");
  45.             else
  46.                 AddPart(buff,((struct WBStartup *)argv)->sm_ArgList[1].wa_Name,256);
  47.             butname = buff;
  48.         }
  49.         else
  50.             return;
  51.     }
  52.     else {
  53.         if (argc >= 2)
  54.             butname = argv[1];
  55.         else
  56.             return;
  57.     }
  58.  
  59.     /*
  60.      * Send command to driver: Use a new Pbutton file
  61.      */
  62.  
  63.     ret  = SendCommand(MPC_NEWBUTTONS,(ULONG )butname);
  64.     if(ret == NO_PUBPORT) CloseAll("Unable to find "GTDPUBPORTNAME".\n");
  65.     else if (ret == COMMAND_FAILED) CloseAll("Unable to use new Pbutton file.\n");
  66.     CloseAll(NULL);
  67. }
  68.  
  69. short SendCommand(ULONG comm, ULONG data)
  70. {
  71.     /*
  72.      * Initialize the TabletCommand structure to set GTDriver to
  73.      * work sending data to this program (to myport).
  74.      */
  75.     Tcmd.msg.mn_Node.ln_Type = NT_MESSAGE;
  76.     Tcmd.msg.mn_ReplyPort = repport;
  77.     Tcmd.msg.mn_Length = sizeof(struct TabletCommand);
  78.     Tcmd.Command = comm;
  79.     Tcmd.Data = (void *)data;
  80.  
  81.     /*
  82.      * Search the GTDriver message port and set it to use my message port
  83.      */
  84.     Forbid();
  85.     port = FindPort(GTDPUBPORTNAME);
  86.     if (! port){
  87.         Permit();
  88.         return NO_PUBPORT;
  89.     }
  90.     PutMsg(port,(struct Message *)&Tcmd);
  91.     Permit();
  92.     WaitPort(repport);
  93.     GetMsg(repport);
  94.     return (short )Tcmd.Command;
  95. }
  96.  
  97. void CloseAll(char *msg)
  98. {
  99.     if (msg) Printf(msg);
  100.     if (repport) DeleteMsgPort(repport);
  101.     if (myport) DeleteMsgPort(myport);
  102.     exit(0);
  103. }