home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 18 / amigaformatcd18.iso / -readerstuff- / steve_boxall / ezprinter.c < prev    next >
C/C++ Source or Header  |  1997-07-18  |  2KB  |  111 lines

  1. /*
  2. ** EZPrinter.c
  3. ** Version 1.00     By The Reaper
  4. **
  5. ** These little routines have been written to make it easier for you to
  6. ** use the printer device in your C programs without needing to worry about
  7. ** all the allocs/opens needed.
  8. **
  9. ** This was based on the RKRM example source but made to look a bit nicer
  10. ** and Dice 3.01 compatible.
  11. **
  12. ** Functions:
  13. **
  14. ** int open_printer(void)
  15. **     Open the printer device and setup msg ports etc. Returns 0 on success
  16. **
  17. ** void close_printer(void)
  18. **     Should be called even if open_printer fails as it frees all
  19. **     succesfull allocs made by open_printer()
  20. **
  21. ** void init_printer(void)
  22. **     Sends the init code to the printer
  23. **
  24. ** void send_text(char *text)
  25. **     Sends the string text to the printer. Note: Uses DoIO()
  26. **
  27. ** void queue_write(char *text)
  28. **     Same as send_text but uses SendIO() instead. Note: Don't forget to
  29. **     wait for it to return! You can make an Abort gadget by calling
  30. **     AbortIO() for the queued write.
  31. */
  32.  
  33. #define Prototype extern
  34.  
  35. #include <exec/types.h>
  36. #include <devices/printer.h>
  37. #include <devices/prtbase.h>
  38.  
  39. #include <clib/exec_protos.h>
  40.  
  41. /* Unions */
  42. union printerIO
  43. {
  44.     struct IOStdReq    ios;
  45.     struct IODRPReq    iodrp;
  46.     struct IOPrtCmdReq iopc;
  47. };
  48.  
  49. /* Globals */
  50. struct MsgPort *printMsgPort;
  51. union printerIO *pio;
  52.  
  53. /* Prototypes */
  54. Prototype int open_printer(void);
  55. Prototype void close_printer(void);
  56. Prototype void init_printer(void);
  57. Prototype void send_text(char *text);
  58. Prototype void queue_write(char *text);
  59.  
  60. int open_printer(void)
  61. {
  62.     if(printMsgPort = CreatePort(0L,0L))
  63.     {
  64.         if(pio = (union printerIO *)CreateExtIO(printMsgPort,sizeof(union printerIO)))
  65.         {
  66.             if(!(OpenDevice("printer.device",0L,(struct IORequest *)pio,0L)))
  67.             {
  68.                 return(0L);
  69.             }
  70.             else return(3L);
  71.         }
  72.         else return(2L);
  73.     }
  74.     else return(1L);
  75. }
  76. void close_printer(void)
  77. {
  78.     if(pio)
  79.     {
  80.         CloseDevice((struct IORequest *)pio);
  81.         DeleteExtIO((struct IORequest *)pio);
  82.     }
  83.     if(printMsgPort)
  84.     {
  85.         DeletePort(printMsgPort);
  86.     }
  87. }
  88. void init_printer(void)
  89. {
  90.     pio->ios.io_Command = CMD_WRITE;
  91.     pio->ios.io_Data = "\033#1";
  92.     pio->ios.io_Length = -1L;
  93.     
  94.     DoIO((struct IORequest *)pio);
  95. }
  96. void send_text(char *text)
  97. {
  98.     pio->ios.io_Command = CMD_WRITE;
  99.     pio->ios.io_Data = text;
  100.     pio->ios.io_Length = -1L;
  101.     
  102.     DoIO((struct IORequest *)pio);
  103. }
  104. void queue_write(char *text)
  105. {
  106.     pio->ios.io_Command = CMD_WRITE;
  107.     pio->ios.io_Data = text;
  108.     pio->ios.io_Length = -1L;
  109.     
  110.     SendIO((struct IORequest *)pio);
  111. }