Back to index

radiance  4R0+20100331
devmain.c
Go to the documentation of this file.
00001 #ifndef lint
00002 static const char    RCSid[] = "$Id: devmain.c,v 2.4 2003/02/25 02:47:22 greg Exp $";
00003 #endif
00004 /*
00005  *  devmain.c - main for independent drivers.
00006  *
00007  *     Redefine your initialization routine to dinit.
00008  */
00009 
00010 #include "copyright.h"
00011 
00012 #include "standard.h"
00013 
00014 #include "color.h"
00015 
00016 #include "driver.h"
00017 
00018 struct driver *dev = NULL;                /* output device */
00019 
00020 FILE   *devin, *devout;                   /* communications channels */
00021 
00022 char   *progname;                         /* driver name */
00023 
00024 int    r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(), r_flush();
00025 
00026 int    (*dev_func[NREQUESTS])() = {              /* request handlers */
00027               r_clear, r_paintr,
00028               r_getcur, r_comout,
00029               r_comin, r_flush
00030        };
00031 
00032 
00033 main(argc, argv)            /* set up communications and main loop */
00034 int    argc;
00035 char   *argv[];
00036 {
00037        extern struct driver *dinit();
00038        int  com;
00039                                           /* set up I/O */
00040        progname = argv[0];
00041        if (argc < 3) {
00042               eputs("arg count\n");
00043               quit(1);
00044        }
00045        devin = fdopen(atoi(argv[1]), "r");
00046        devout = fdopen(atoi(argv[2]), "w");
00047        if (devin == NULL || devout == NULL || getw(devin) != COM_SENDM) {
00048               eputs("connection failure\n");
00049               quit(1);
00050        }
00051                                           /* open device */
00052        if ((dev = dinit(argv[0], argv[3])) == NULL)
00053               quit(1);
00054        putw(COM_RECVM, devout);           /* verify initialization */
00055        sendstate();
00056        fflush(devout);
00057                                           /* loop on requests */
00058        while ((com = getc(devin)) != EOF) {
00059               if (com >= NREQUESTS || dev_func[com] == NULL) {
00060                      eputs("invalid request\n");
00061                      quit(1);
00062               }
00063               (*dev_func[com])();         /* process request */
00064        }
00065        quit(0);                    /* all done, clean up and exit */
00066 }
00067 
00068 
00069 void
00070 quit(code)                         /* close device and exit */
00071 int    code;
00072 {
00073        if (dev != NULL)
00074               (*dev->close)();
00075        exit(code);
00076 }
00077 
00078 
00079 r_clear()                          /* clear screen */
00080 {
00081        int    xres, yres;
00082 
00083        xres = getw(devin);
00084        yres = getw(devin);
00085        (*dev->clear)(xres, yres);
00086 }
00087 
00088 
00089 r_paintr()                         /* paint a rectangle */
00090 {
00091        COLOR  col;
00092        int    xmin, ymin, xmax, ymax;
00093 
00094        fread((char *)col, sizeof(COLOR), 1, devin);
00095        xmin = getw(devin); ymin = getw(devin);
00096        xmax = getw(devin); ymax = getw(devin);
00097        (*dev->paintr)(col, xmin, ymin, xmax, ymax);
00098 }
00099 
00100 
00101 r_flush()                          /* flush output */
00102 {
00103        if (dev->flush != NULL)
00104               (*dev->flush)();
00105        putc(COM_FLUSH, devout);
00106        sendstate();
00107        fflush(devout);
00108 }
00109 
00110 
00111 r_getcur()                         /* get and return cursor position */
00112 {
00113        int    c;
00114        int    x, y;
00115                                    /* get it if we can */
00116        if (dev->getcur == NULL) {
00117               c = ABORT;
00118               x = y = 0;
00119        } else
00120               c = (*dev->getcur)(&x, &y);
00121                                    /* reply */
00122        putc(COM_GETCUR, devout);
00123        putc(c, devout);
00124        putw(x, devout);
00125        putw(y, devout);
00126        fflush(devout);
00127 }
00128 
00129 
00130 r_comout()                         /* print string to command line */
00131 {
00132        char   str[256];
00133 
00134        mygets(str, devin);
00135        (*dev->comout)(str);
00136 }
00137 
00138 
00139 r_comin()                          /* read string from command line */
00140 {
00141        char   buf[256], *prompt;
00142                                    /* get prompt */
00143        if (getc(devin)) {
00144               mygets(buf, devin);
00145               prompt = buf;
00146        } else
00147               prompt = NULL;
00148                                    /* get string */
00149        (*dev->comin)(buf, prompt);
00150                                    /* reply */
00151        putc(COM_COMIN, devout);
00152        myputs(buf, devout);
00153        sendstate();
00154        fflush(devout);
00155 }
00156 
00157 
00158 mygets(s, fp)                      /* get string from file (with nul) */
00159 register char *s;
00160 register FILE *fp;
00161 {
00162        register int  c;
00163 
00164        while ((c = getc(fp)) != EOF)
00165               if ((*s++ = c) == '\0')
00166                      return;
00167        *s = '\0';
00168 }
00169 
00170 
00171 myputs(s, fp)                      /* put string to file (with nul) */
00172 register char *s;
00173 register FILE *fp;
00174 {
00175        do
00176               putc(*s, fp);
00177        while (*s++);
00178 }
00179 
00180 
00181 void
00182 eputs(s)                           /* put string to stderr */
00183 register char  *s;
00184 {
00185        static int  midline = 0;
00186 
00187        if (!*s)
00188               return;
00189        if (!midline++) {
00190               fputs(progname, stderr);
00191               fputs(": ", stderr);
00192        }
00193        fputs(s, stderr);
00194        if (s[strlen(s)-1] == '\n') {
00195               fflush(stderr);
00196               midline = 0;
00197        }
00198 }
00199 
00200 
00201 sendstate()                        /* send driver state variables */
00202 {
00203        fwrite((char *)&dev->pixaspect, sizeof(dev->pixaspect), 1, devout);
00204        putw(dev->xsiz, devout);
00205        putw(dev->ysiz, devout);
00206        putw(dev->inpready, devout);
00207 }