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

  1. #include <exec/types.h>
  2. #include <dos/dos.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <intuition/intuition.h>
  6.  
  7. #include <clib/exec_protos.h>
  8. #include <clib/Intuition_protos.h>
  9. #include <clib/graphics_protos.h>
  10. #include <clib/alib_protos.h>
  11.  
  12. #include "/include/GTDriverMPC.h"
  13.  
  14. #define NOPUBPORT     -1
  15.  
  16. void PrintMsg(struct TabletMessage *Tmsg);
  17. void CloseAll(char *msg);
  18. short SendCommand(ULONG comm, ULONG data);
  19.  
  20. struct Library *IntuitionBase = NULL;
  21. struct Library *GfxBase = NULL;
  22. struct MsgPort *myport = NULL,*port = NULL,*repport = NULL;
  23. struct Window *wnd = NULL;
  24. struct TabletCommand Tcmd;
  25. ULONG  Temp;
  26. short  xb,yb,fontheight;
  27. struct RastPort *rp;
  28. struct GTDPrefs PData;
  29.  
  30. main()
  31. {
  32.     struct TabletMessage *Tmsg;
  33.     char stayin = TRUE;
  34.     ULONG WaitMask;
  35.     short ret;
  36.  
  37.     /*
  38.      * Open Intuition and Graphics library
  39.      */
  40.     IntuitionBase = OpenLibrary("intuition.library",36L);
  41.      if (! IntuitionBase) CloseAll("Unable to open V36 Inuition Library\n");
  42.  
  43.     GfxBase = OpenLibrary("graphics.library",36L);
  44.      if (! GfxBase) CloseAll("Unable to open V36 Gfx Library\n");
  45.  
  46.     /*
  47.      * Now let's open the window to display data
  48.      */
  49.     wnd = OpenWindowTags(NULL, WA_Left,0, WA_Top,0, WA_Width,200, WA_Height,100,
  50.                  WA_DragBar,TRUE, WA_SmartRefresh,TRUE, WA_NoCareRefresh,TRUE,
  51.                  WA_CloseGadget, TRUE, WA_IDCMP, IDCMP_CLOSEWINDOW,
  52.                  WA_Title,"ShowCoords", TAG_END);
  53.     if (!wnd) CloseAll("Unable to open window\n");
  54.  
  55.     rp = wnd->RPort;
  56.     xb = wnd->BorderLeft + 4;
  57.     yb = wnd->BorderTop + rp->Font->tf_Baseline + 4;
  58.     fontheight = rp->Font->tf_YSize;
  59.     SetAPen(rp,1);
  60.  
  61.     /*
  62.      * Create message port to get messages from driver
  63.      */
  64.     myport = CreateMsgPort();
  65.     if (! myport) CloseAll("Unable to create message port\n");
  66.  
  67.     /*
  68.      * Create Msg port to receive replies to command sent to the driver
  69.      */
  70.     repport = CreateMsgPort();
  71.     if (! repport) CloseAll("Unable to create message port\n");
  72.  
  73.     /*
  74.      * Send command to driver: Get driver preferences: Prefs will be
  75.      * filled with driver settings
  76.      */
  77.  
  78.     ret  = SendCommand(MPC_GETPREFS,(ULONG )&PData);
  79.     if(ret == NO_PUBPORT) CloseAll("Unable to find "GTDPUBPORTNAME".\n");
  80.     else if (ret == COMMAND_FAILED) CloseAll("Unable to get driver prefs.\n");
  81.     printf("Driver version: %2f\n",PData.Version);
  82.     printf("Working mode:   %s\n",(PData.Mode == MODE_SERVER) ? "server" : "driver");
  83.     printf("Emulation:      %s\n",PData.Emulation);
  84.     printf("Init string:    %s\n",PData.InitString);
  85.     printf("Baud Rate:      %d\n",PData.Baud);
  86.     printf("Priority:       %d\n",PData.Priority);
  87.     printf("DPI:            %d\n",PData.DPI);
  88.     printf("Metric:         %s\n",(PData.Metric == METRIC_CM)? "centimeters" : "inches");
  89.     printf("Dimensions:     %.2f %.2f\n",PData.XDim,PData.YDim);
  90.     printf("Clip region:    (%.2f,%.2f)-(%.2f,%.2f)\n",PData.ULC_x,PData.ULC_y,PData.LRC_x,PData.LRC_y);
  91.     if (PData.SwapXY) printf("X and Y axes swapped\n");
  92.     if (PData.MirrorX) printf("X axis mirrored\n");
  93.     if (PData.MirrorY) printf("Y axis mirrored\n");
  94.  
  95.     /*
  96.      * Send command to driver: switch to server mode
  97.      */
  98.     ret = SendCommand(MPC_SERVERMODE,(ULONG )myport);
  99.     if(ret == NO_PUBPORT) CloseAll("Unable to find "GTDPUBPORTNAME".\n");
  100.     else if (ret == COMMAND_FAILED) CloseAll("Unable to switch to server mode\n");
  101.     /*
  102.      * Main event loop
  103.      */
  104.     WaitMask = (SIGBREAKF_CTRL_C | (1L << myport->mp_SigBit) | (1L << wnd->UserPort->mp_SigBit));
  105.     do{
  106.         /*
  107.          * Now wait for a CTRL-C,the close gadget or a message from GTDriver; if
  108.          * is a CTRL-C or the close gadget exit, else print the message data in the
  109.          * window
  110.          */
  111.         Temp = Wait(WaitMask);
  112.         if (Temp & SIGBREAKF_CTRL_C)
  113.             stayin = FALSE;
  114.         if (Temp & (1L << wnd->UserPort->mp_SigBit)) {
  115.             WaitPort(wnd->UserPort);
  116.             stayin = FALSE;
  117.         }
  118.         if (Temp & (1L << myport->mp_SigBit)){
  119.             while(Tmsg = (struct TabletMessage *)GetMsg(myport)){
  120.                 PrintMsg(Tmsg);
  121.                 ReplyMsg((struct Message *)Tmsg);
  122.                 Temp = SetSignal(0,0);
  123.                 if (Temp & SIGBREAKF_CTRL_C){
  124.                     stayin=FALSE;
  125.                     SetSignal(0,SIGBREAKF_CTRL_C);
  126.                     break;
  127.                 }
  128.             }
  129.         }
  130.     }while(stayin);
  131.     /*
  132.      * switch again to driver mode this can't fail because we own
  133.      * the driver!.
  134.      */
  135.  
  136.     ret = SendCommand(MPC_DRIVERMODE,0L);
  137.     if(ret == NO_PUBPORT) CloseAll("Unable to find "GTDPUBPORTNAME". Someone already closed GTDriver ?\n");
  138.     else if (ret == COMMAND_FAILED) CloseAll("Unable to switch to server mode.\n");
  139.  
  140.  
  141.     while (Tmsg = (struct TabletMessage *)GetMsg(myport))
  142.         ReplyMsg((struct Message *)Tmsg);
  143.  
  144.     CloseAll("Program ended. All right\n");
  145. }
  146.  
  147. void CloseAll(char *msg)
  148. {
  149.     printf(msg);
  150.     if (wnd) CloseWindow(wnd);
  151.     if (repport) DeleteMsgPort(repport);
  152.     if (myport) DeleteMsgPort(myport);
  153.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  154.     if (GfxBase) CloseLibrary(GfxBase);
  155.     exit(0);
  156. }
  157.  
  158.  
  159. void PrintMsg(struct TabletMessage *Tmsg)
  160. {
  161.     char buff[32];
  162.     float x,y;
  163.  
  164.     x = ((float)Tmsg->x)/PData.DPI;
  165.     y = ((float)Tmsg->y)/PData.DPI;
  166.  
  167.     switch(Tmsg->type){
  168.         case TMTYPE_COORDS:
  169.             sprintf(buff,"Coords:  (%.2f,%.2f)   ",x,y);
  170.             Move(rp,xb,yb);
  171.             if (x < PData.ULC_x || x > PData.LRC_x || y < PData.ULC_y || y > PData.LRC_y)
  172.                 SetAPen(rp,2);
  173.             else
  174.                 SetAPen(rp,1);
  175.             Text(rp,buff,strlen(buff));
  176.             sprintf(buff,"Buttons: %X        ",Tmsg->buttons);
  177.             Move(rp,xb, yb+fontheight*1);
  178.             Text(rp,buff,strlen(buff));
  179.             break;
  180.         case TMTYPE_PBUTTON:
  181.             sprintf(buff,"Pbutton: %c       ",Tmsg->key);
  182.             SetAPen(rp,1);
  183.             Move(rp,xb, yb+fontheight*2);
  184.             Text(rp, buff, strlen(buff));
  185.             break;
  186.     }
  187. }
  188.  
  189. short SendCommand(ULONG comm, ULONG data)
  190. {
  191.     /*
  192.      * Initialize the TabletCommand structure to set GTDriver to
  193.      * work sending data to this program (to myport).
  194.      */
  195.     Tcmd.msg.mn_Node.ln_Type = NT_MESSAGE;
  196.     Tcmd.msg.mn_ReplyPort = repport;
  197.     Tcmd.msg.mn_Length = sizeof(struct TabletCommand);
  198.     Tcmd.Command = comm;
  199.     Tcmd.Data = (void *)data;
  200.  
  201.     /*
  202.      * Search the GTDriver message port and set it to use my message port
  203.      */
  204.     Forbid();
  205.     port = FindPort(GTDPUBPORTNAME);
  206.     if (! port){
  207.         Permit();
  208.         return NO_PUBPORT;
  209.     }
  210.     PutMsg(port,(struct Message *)&Tcmd);
  211.     Permit();
  212.     WaitPort(repport);
  213.     GetMsg(repport);
  214.     return (short )Tcmd.Command;
  215. }