home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / web2c / mf / MFwindow / next.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-28  |  4.5 KB  |  178 lines

  1. /* next.c: display interface for the NeXT, joe@rilgp.tamri.com.
  2.  
  3. this device code requires a second program ("DrawingServant") to
  4. act as the interface between sm and the WindowServer.  The second program
  5. just serves to listen for commands on a pipe, but otherwise
  6. send everything else with a DPSPrintf to the WindowServer.  The servant can
  7. also do things like implement it's own event loop and handle things
  8. like printing, saving .eps  and the like.  This seems (to me) to be the
  9. best way to put a multiplatform program into a NeXT application.
  10.  
  11. You can get DrawingServant from sonata.cc.purdue.edu.
  12. */
  13.  
  14. /* with not too much work, the client side of things probably 
  15. could be on another machine */
  16. #define EXTERN extern
  17. #include "../mfd.h"
  18.  
  19. #ifdef NEXTWIN        /* the whole file */
  20. #define DRAWSERVER "DrawingServant"
  21. #define DEFWIDTH 400    /* default width and height */
  22. #define DEFHEIGHT 500
  23.  
  24. /*these default values are taken from plain.mf
  25. and are used if we can't see anything better */
  26. static int nextheight=DEFHEIGHT;
  27. static int nextwidth=DEFWIDTH;
  28. static int outpipe[2],inpipe[2];
  29. static int pid;
  30. static int nextscreenlooksOK = 0;
  31. char outstring[1024];    /* the longest string pushed though a pipe */
  32. /* these are used a lot, so macro-ize these two lines */
  33. #define SENDPS write(outpipe[1],outstring,strlen(outstring)+1)
  34. #define GETACK do{\
  35.         read(inpipe[0],outstring,sizeof(outstring)-1);\
  36.         } while(strncmp(outstring,"Ok",2))
  37. #ifdef read
  38. #undef read
  39. #endif
  40.  
  41. mf_next_initscreen()
  42. {
  43.     int i;
  44.     void mf_next_closescreen();
  45.     /* strings for height, width, in and out pipes */
  46.     char hstr[20],wstr[20],instr[20],outstr[20];
  47.     
  48.     /* I should figure out how to use screen_rows and screen_cols
  49.     to size the window. what I think I need is one of leftcol,rightcol
  50.     toprow and botrow.  Let's find the first which is non-zero,
  51.     at least until someone tells me what the real answer is.*/
  52.  
  53.     for(i=0;i<16;i++) {
  54.         if((leftcol[i]-rightcol[i]) && (toprow[i]-botrow[i])) {
  55.             nextwidth = rightcol[i]-leftcol[i];
  56.             nextheight = botrow[i]-toprow[i];
  57.             break;
  58.         }
  59.     }
  60.  
  61.     /* fork a process and assign some pipes.  return if unsuccessful */
  62.     if( pipe(outpipe)== -1)
  63.             return 0;
  64.     if( pipe(inpipe)== -1)
  65.             return 0;
  66.     if( (pid=fork())== -1)
  67.             return 0;
  68.  
  69.     if(pid==0) {
  70.         /* things done by the child. we pass it height,width and
  71.         input and output pipes */
  72.         sprintf(hstr,"h %d ",nextheight);
  73.         sprintf(wstr,"w %d ",nextwidth);
  74.         sprintf(outstr,"i %d",outpipe[0]);
  75.         sprintf(instr,"o %d",inpipe[1]);
  76.         execl(DRAWSERVER,DRAWSERVER,hstr,wstr,instr,outstr,0);
  77.         exit(0);
  78.     }
  79.     sprintf(outstring,"initgraphics\n");
  80.     SENDPS;
  81.     GETACK;
  82.     nextscreenlooksOK = 1;
  83.  
  84.     /* The prior version used a hacked version of uexit to kill the
  85.     server...at the urging of karl berry, here is a more legit way to
  86.     kill the server */
  87.     atexit(*mf_next_closescreen);
  88.  
  89.     return 1;
  90. }
  91. /*
  92.  *    void updatescreen;
  93.  *     does nothing
  94.  *
  95.  */
  96. mf_next_updatescreen()
  97. {
  98. }
  99. /*
  100.  *    void blankrectangle(int left,int right,int top,int bottom);
  101.  *
  102.  *        blank out a port of the screen.
  103.  */
  104. mf_next_blankrectangle(left, right, top, bottom)
  105. screencol left, right;
  106. screenrow top, bottom;
  107. {
  108.  
  109.     if(left==0 && top==nextheight && right==nextwidth && bottom==0 ) {
  110.          /* clear and forgets PS strings */
  111.         sprintf(outstring,"DSclear");
  112.     } else {
  113.         sprintf(outstring,
  114.             " 1 setgray %d %d %d %d rectfill 0 setgray \n",
  115.             left+1,top+1,right,bottom+1);
  116.     }
  117.         SENDPS;
  118.         GETACK;
  119. }
  120.  
  121. /*
  122.  *    void paintrow(int row,int init_color,int transition_vector,
  123.  *                        int vector_size);
  124.  *
  125.  *        Paint "row" starting with color "init_color", up to next
  126.  *        transition specified by "transition_vector", switch colors,
  127.  *        and continue for "vector_size" transitions.
  128.  */
  129. mf_next_paintrow(row, init_color, transition_vector, vector_size)
  130. screenrow   row;
  131. pixelcolor  init_color;
  132. transspec   transition_vector;
  133. screencol   vector_size;
  134. {
  135.     int i,whereami;
  136.     if(init_color) {
  137.         init_color = 1;
  138.     } else {
  139.         init_color = 0;
  140.     }
  141.     whereami = 0;
  142.  
  143.     for(i=0;i<vector_size;i++) {
  144.         if(init_color) {
  145.             sprintf(outstring+whereami,
  146.                 "newpath %d %d moveto %d %d lineto stroke ",
  147.                     transition_vector[i],nextheight-row,
  148.                     transition_vector[i+1],nextheight-row);
  149.             whereami = strlen(outstring);
  150.             /* buffering is good.  perhaps. */
  151.             if(whereami > 500) {
  152.                 SENDPS;
  153.                 GETACK;
  154.                 *outstring = 0;
  155.                 whereami = 0;
  156.             }
  157.         }
  158.         init_color = 1-init_color;
  159.     }
  160.     if(whereami) {
  161.         SENDPS;
  162.         GETACK;
  163.     }
  164. }
  165. /* this isn't part of the online display routines.  We need it to
  166. kill DrawingServant.  This is called during exit */
  167. void mf_next_closescreen()
  168. {
  169.     if(nextscreenlooksOK) {
  170.         sprintf(outstring,"DSquit");
  171.         SENDPS;
  172.     }
  173. }
  174.  
  175. #else
  176. int next_dummy;
  177. #endif    /* NEXTWIN */
  178.