home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD1.img / d1xx / d105 / ipo2c / ipo2c.c < prev    next >
C/C++ Source or Header  |  1987-10-25  |  5KB  |  240 lines

  1. /* IPo2C.c */
  2. /* Convert current intuition pointer to C definition file */
  3. /* by Alex Livshits    26-Sep-87 */
  4.  
  5. #include <exec/types.h>
  6. #include <exec/ports.h>
  7. #include <exec/memory.h>
  8. #include <exec/io.h>
  9. #include <exec/tasks.h>
  10. #include <exec/interrupts.h>
  11. #include <libraries/dos.h>
  12. #include <devices/input.h>
  13. #define INTUITIONPRIVATE
  14. #include <intuition/intuitionbase.h>
  15.  
  16.  
  17. #define PORT      0x00000001
  18. #define REQUEST   0x00000002
  19. #define SIGNAL    0x00000004
  20. #define INPUTDEV  0x00000008
  21. #define MEMORY    0x00000010
  22.  
  23. /* ==== IMPORT ==== */
  24. extern struct IntuitionBase *IntuitionBase;
  25. extern struct InputEvent *InputHandler();
  26. extern int HandlerInterface();
  27. /****/
  28.  
  29. /* ==== EXPORT ==== */
  30. ULONG EVENT;
  31. APTR  MyTask;
  32. ULONG STOPFLAG=FALSE;
  33. int Suspend=FALSE;
  34. /***/
  35.  
  36.  
  37. static struct MsgPort *port;
  38. static struct IOStdReq *req;
  39. static struct Interrupt handler;
  40. static LONG signal;
  41. static int PointerNum = 0;
  42. static char *name;
  43.  
  44. static ULONG mask;
  45. static BPTR fp;
  46.  
  47.  
  48. main(argc,argv)
  49.    int argc;
  50.    char *argv[];
  51. {
  52.    int retc;
  53.    BPTR lock;
  54.  
  55.    mask = 0;
  56.    fp=0;
  57.  
  58.    printf("\n\t    ++++  IPo2C  v1.0   ++++");
  59.    printf("\n\t(C)FLam == A.Livshits J-M.Forgeas == 1987");
  60.  
  61.    if (argc==1)
  62.    {
  63.       printf("\nUsage:\nIPo2C [<FileName> -> valid DOS file]");
  64.       exit(0);
  65.    }
  66.  
  67.    name = argv[1];
  68.    IntuitionBase = OpenLibrary("intuition.library",0);
  69.    if (!IntuitionBase)
  70.    {
  71.         printf("\nFailed to open intuition.library");
  72.         exit(0);
  73.    }
  74.    port = CreatePort(0,0);
  75.    if (!port)
  76.    {
  77.       printf("\nFailed to open MsgPort");
  78.       cleanup();
  79.       exit(0);
  80.    }
  81.    mask |= PORT;
  82.  
  83.    req = CreateStdIO(port);
  84.    if (!req)
  85.    {
  86.       printf("\nFailed to create StdIO");
  87.       cleanup();
  88.       exit(0);
  89.    }
  90.    mask |= REQUEST;
  91.  
  92.    signal = AllocSignal(-1);
  93.    if (signal==-1)
  94.    {
  95.       printf("\nFailed to allocate SignalBit");
  96.       cleanup();
  97.       exit(0);
  98.    }
  99.    mask |= SIGNAL;
  100.    EVENT = 1L<<signal;
  101.    MyTask = (APTR)FindTask(0);
  102.  
  103.    handler.is_Data = 0;
  104.    handler.is_Code = HandlerInterface;
  105.    handler.is_Node.ln_Pri = 51;
  106.  
  107.    Suspend=FALSE;
  108.    retc = OpenDevice("input.device",0,req,0);
  109.    if (retc)
  110.    {
  111.       printf("\nFailed to open Input Device");
  112.       cleanup();
  113.       exit(0);
  114.    }
  115.  
  116.    STOPFLAG = FALSE;
  117.  
  118.    req->io_Command = IND_ADDHANDLER;
  119.    req->io_Data = &handler;
  120.    DoIO(req);
  121.    mask |= INPUTDEV;
  122.  
  123.    printf("\n\tALT-F -> Quit");
  124.    printf("\n\tPress ALT-P to write file.\n");
  125.    while(TRUE)
  126.    {
  127.       Wait(EVENT);
  128.       if (STOPFLAG) break;
  129.       lock = Lock(name,ACCESS_WRITE);
  130.       if (lock)
  131.       {
  132.         UnLock(lock);
  133.         fp = Open(name,MODE_OLDFILE);
  134.       }
  135.       else fp = Open(name,MODE_NEWFILE);
  136.       if (!fp)
  137.       {
  138.          printf("\nFailed to open output file %ls",name);
  139.          break;
  140.       }
  141.       Seek(fp,0,OFFSET_END);
  142.       if (!CopyPointer(fp)) break;
  143.       Close(fp);
  144.       fp = 0;
  145.       Suspend=FALSE;
  146.    }
  147.    cleanup();
  148.    exit(0);
  149. }
  150.  
  151.  
  152. static
  153. CopyPointer(fp)
  154.     BPTR fp;
  155. {
  156.     UWORD *buf;
  157.     WORD len;
  158.     ULONG ilock;
  159.     BYTE height,width,xoffs,yoffs;
  160.     int retc;
  161.  
  162.     ilock = LockIBase(0);
  163.     height = IntuitionBase->APtrHeight;
  164.     width = IntuitionBase->APtrWidth;
  165.     xoffs = IntuitionBase->AXOffset;
  166.     yoffs = IntuitionBase->AYOffset;
  167.  
  168.     len = (height+2)*sizeof(ULONG);
  169.     buf = AllocMem(len,MEMF_PUBLIC+MEMF_CLEAR);
  170.     if (!buf)
  171.     {
  172.         printf("\nOut of memory.");
  173.         return(FALSE);
  174.     }
  175.  
  176.     CopyMem(IntuitionBase->APointer,buf,len);
  177.     UnlockIBase(ilock);
  178.     retc=SavePointer(fp,buf,width,height,xoffs,yoffs);
  179.     FreeMem(buf,len);
  180.     return(retc);
  181.  
  182. }
  183.  
  184. static
  185. SavePointer(fp,buf,w,h,xofs,yofs)
  186.     BPTR fp;
  187.     USHORT *buf;
  188.     BYTE w,h,xofs,yofs;
  189. {
  190.     int i;
  191.  
  192.     fprintf(fp,"\n/* ============================================= */");
  193.     fprintf(fp,"\n/*              Pointer Data                     */");
  194.     fprintf(fp,"\n/* ============================================= */");
  195.  
  196.     PointerNum++;
  197.     if (!w) w = 16;
  198.     fprintf(fp,"\n#define POINTER%ld_HEIGHT %ld",PointerNum,h);
  199.     fprintf(fp,"\n#define POINTER%ld_WIDTH  %ld",PointerNum,w);
  200.     fprintf(fp,"\n#define POINTER%ld_HOTX   %ld",PointerNum,xofs);
  201.     fprintf(fp,"\n#define POINTER%ld_HOTY   %ld",PointerNum,yofs);
  202.  
  203.     fprintf(fp,"\n\nUSHORT pointer%ld_dat[] = {",PointerNum);
  204.  
  205.     fprintf(fp,"\n\t0x0000,0x0000,");
  206.     buf += 2;
  207.  
  208.     for (i=0;i<h;i++)
  209.     {
  210.         fprintf(fp,"\n\t0x%lx,0x%lx,",*buf,*(buf+1));
  211.         buf += 2;
  212.     }
  213.  
  214.     fprintf(fp,"\n\t0x0000,0x0000");
  215.  
  216.     fprintf(fp,"\n};\n");
  217.     printf("Pointer definition written to %ls.\n",name);
  218.     return(TRUE);
  219. }
  220.  
  221.  
  222. static
  223. cleanup()
  224. {
  225.    if (fp) Close(fp);
  226.    if (mask & INPUTDEV) {
  227.          req->io_Command=IND_REMHANDLER;
  228.          req->io_Data=&handler;
  229.          DoIO(req);
  230.          CloseDevice(req);
  231.    }
  232.    if (mask & SIGNAL) FreeSignal(signal);
  233.    if (mask & REQUEST) DeleteStdIO(req);
  234.    if (mask & PORT) DeletePort(port);
  235.    CloseLibrary(IntuitionBase);
  236. }
  237.  
  238.  
  239.  
  240.