home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / COMM / MISC / SRC26_2.ZIP / SRC / PRINTER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-04  |  3.4 KB  |  185 lines

  1. /*
  2.  * printer.c: hterm printer control
  3.  *
  4.  * Author: HIRANO Satoshi
  5.  * (C) 1989  Halca Computer Science Laboratory TM
  6.  *           University of Tokyo
  7.  *
  8.  * 2.2 89/05/16 Halca.Hirano V2.2 distribution
  9.  * 2.3 89/08/03 Halca.Hirano add checkPrinterReady()
  10.  *    ---- V2.4.0 distribution ----
  11.  * 2.4 89/11/10 Tominaga@Titech ported to J3100 (NOT TESTED!)
  12.  * ---- V2.4.2 distribution ----
  13.  * ---- V2.5.1 distribution ----
  14.  * 2.5 89/12/16 Tominaga@Titech fix printer ready checking bug
  15.  *        (I missed BIOS calling interrupt nubmer)
  16.  *
  17.  * $Header: printer.cv  1.9  90/07/04 00:59:46  hirano  Exp $
  18.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <io.h>
  24. #include "config.h"
  25. #include "hterm.h"
  26. #include "option.h"
  27. #include "default.h"
  28. #include "global.h"
  29.  
  30. #define STATE_ASCII 0
  31. #define STATE_KANJI 1    
  32. static char kanBeg[] = "\x1bK";
  33. static char kanEnd[] = "\x1bH";
  34. static int    printStat;
  35. static int    prPath;
  36.  
  37. static void outKanjiPrinter(unsigned short c);
  38.  
  39. void printInit()
  40. /*
  41.  * initialize printer 
  42.  */
  43. {
  44.     spacing = DEFAULT_PRINTER_SPACING;
  45.     printMode = DEFAULT_PRINT_MODE;
  46.     printStat = STATE_ASCII;
  47.  
  48.     if ((prPath = open(DEFAULT_PRINTER_DEVICE, O_WRONLY|O_BINARY)) == ERR) {
  49.         prPath = 0;
  50.     }
  51. }
  52.  
  53. /*
  54. ** void printSetup()
  55.  *
  56.  * enable printer if printMode is YES
  57.  */
  58. void printSetup()
  59. {
  60.     if (printMode && checkPrinterReady() == OK)
  61.         printOn();
  62.     else
  63.         printMode = NO;        /* if printer is not ready, disable print mode */
  64. }
  65.  
  66. void printEnd()
  67. {
  68.     if (prPath)
  69.         close(prPath);
  70. }
  71.  
  72. void printOn()
  73. /*
  74.  * enable printer;
  75.  * return:
  76.  *    OK    printer is ready
  77.  *    ERR    printer is not ready
  78.  */
  79. {
  80. #ifdef PC98
  81.     printStat = STATE_ASCII;
  82.     write(prPath, kanEnd, 2);
  83. #endif /* PC98 */
  84. }
  85.  
  86. #if defined IBMPC || defined J3100
  87. void outPrinter(c)
  88. /* output a char to printer */
  89. u_short c;
  90. {
  91.     char buf[2];
  92.  
  93. #ifdef KANJI
  94.     if (c & 0xff00) {
  95.         outKanjiPrinter(c);
  96.         return;
  97.     }
  98. #endif
  99.     buf[0] = c & 0xff;
  100.     write(prPath, buf, 1);
  101. }
  102.  
  103. #ifdef KANJI
  104. static void outKanjiPrinter(c)
  105. u_short c;
  106. {
  107.     u_char buf[2];
  108.  
  109.     c = JIStoSJIS(c>>8, c & 0xff);
  110.     buf[0] = (c>>8);
  111.     buf[1] = c;
  112.     write (prPath, buf, 2);
  113. }
  114. #endif /*KANJI*/
  115.  
  116. int checkPrinterReady()
  117. {
  118.     rg.h.ah = 2;
  119.     rg.x.dx = 0;            /* printer 0            */
  120.     int86(PRINTER_BIOS, &rg, &rg);    /* check status            */
  121.     if ((rg.h.ah & 0x28) || !(rg.h.ah & 0x80))
  122.         return(ERR);        /* I/O error, paper end or not ready */
  123.     return(OK);
  124. }
  125. #endif /* IBMPC || J3100 */
  126.  
  127. #ifdef PC98
  128. void outPrinter(c)
  129. u_short c;
  130. {
  131.     u_char buf[2];
  132.  
  133.     if (c & 0xff00) {
  134.         outKanjiPrinter(c);
  135.         return;
  136.     }
  137.     if (printStat == STATE_KANJI)
  138.         write(prPath, kanEnd, 2);
  139.     buf[0] = c;
  140.     write(prPath, buf, 1);
  141.     printStat = STATE_ASCII;
  142. }
  143.  
  144. static void outKanjiPrinter(c)
  145. u_short c;
  146. {
  147.     u_char buf[4];
  148.  
  149.     if (printStat == STATE_ASCII) {
  150.         write(prPath, kanBeg, 2);
  151.     }
  152.     c &= 0x7f7f;
  153.     buf[0] = c >> 8; buf[1] = c & 0xff; buf[2] = ESC; buf[3] = spacing;
  154.     write(prPath, buf, 4);
  155.     printStat = STATE_KANJI;
  156. }
  157.  
  158. #ifdef PC98XA
  159. /* Rewrite by H.Dohi for ONLY PC98XA and MSC5.1     89/10/17 */
  160. int checkPrinterReady()
  161. {
  162.     unsigned port;
  163.     int result;
  164.  
  165.     result = inp(PRINTER_PORT);
  166.     if ((result & 0xf4) == 0x64)
  167.         return(OK);
  168.     return(ERR);
  169. }
  170.  
  171. #else
  172.  
  173. int checkPrinterReady()
  174. {
  175.     rg.h.ah = 0x12;
  176.     int86(PRINTER_BIOS, &rg, &rg);    /* call printer BIOS        */
  177.     if (rg.h.ah & 1)
  178.         return(OK);        /* ready            */
  179.     return(ERR);
  180. }
  181.  
  182. #endif /* PC98XA */
  183. #endif /* PC98 */
  184.  
  185.