home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / avril / cfg.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  10KB  |  354 lines

  1. /* Read an AVRIL-style CFG file */
  2.  
  3. /* Written by Bernie Roehl, July 1994 */
  4.  
  5. /* Copyright 1994 by Bernie Roehl */
  6.  
  7. /* You may use this code for your own non-commercial projects without
  8.    paying any fees or royalties.  "Non-commercial", in this context,
  9.    means that the software you write is given away for free to anyone
  10.    who wants it.
  11.    
  12.    Commercial use, including shareware, requires a licensing
  13.    fee and a specific written agreement with the author.
  14.  
  15.    All programs created using this software (both commercial and
  16.    non-commercial) must acknowledge the use of the AVRIL library,
  17.    both in the documentation and in a banner screen at the start or
  18.    end of the program.
  19.  
  20.    For more information, contact Bernie Roehl (broehl@uwaterloo.ca).
  21.  
  22. */
  23.  
  24. #include "avril.h"
  25. #include "avrildrv.h"
  26. #include <stdlib.h>   /* strtoul(), atof() */
  27. #include <string.h>
  28. #include <ctype.h>
  29.  
  30. /* These next few externs should really be in avrildrv.h */
  31.  
  32. extern vrl_DisplayDriverFunction vrl_DisplayDriverDefault;
  33. extern vrl_DisplayDriverFunction vrl_DisplayDriverModeY;
  34.  
  35. static int show_compass = 0, show_position = 0, show_framerate = 0;
  36.  
  37. static int getline(char *buff, int maxbytes, FILE *in)
  38.     {
  39.     char *p;
  40.     if (fgets(buff, maxbytes, in) == NULL) return 0;
  41.     if ((p = strchr(buff, '\n')) != NULL) *p = '\0';
  42.     return 1;
  43.     }
  44.  
  45. static int lookup(char *token, char *table[], int n)
  46.     {
  47.     int i;
  48.     for (i = 0; i < n; ++i)
  49.         if (!stricmp(token, table[i]))
  50.             return i;
  51.     return -1;
  52.     }
  53.  
  54. static int parse(char *buff, char *seps, char *argv[])
  55.     {
  56.     char *p;
  57.     int argc = 0;
  58.     if ((p = strchr(buff, '#')) != NULL) *p = '\0';
  59.     p = strtok(buff, seps);
  60.     while (p)
  61.         {
  62.         argv[argc++] = p;
  63.         p = strtok(NULL, seps);
  64.         }
  65.     return argc;
  66.     }
  67.  
  68. static char *statement_table[] =
  69.     {
  70.     "version", "loadpath", "include",
  71.     "device", "devconfig", "displaydriver", "videodriver", "sounddriver",
  72.     "compass", "framerate", "position", "cursor",
  73.     "stereoparams", "stereoleft", "stereoright", "stereotype"
  74.     };
  75.  
  76. static enum statements
  77.     {
  78.     st_version, st_loadpath, st_include,
  79.     st_device, st_devconfig, st_displaydriver, st_videodriver, st_sounddriver,
  80.     st_compass, st_framerate, st_position, st_cursor,
  81.     st_stereoparams, st_stereoleft, st_stereoright, st_stereotype,
  82.     ncmds
  83.     };
  84.  
  85. static char *stereotypes[] =
  86.     {
  87.     "NONE", "SEQUENTIAL",
  88.     "ANAGLYPH_SEQUENTIAL", "ANAGLYPH_WIRE_ALTERNATE",
  89.     "ENIGMA", "FRESNEL",
  90.     "CYBERSCOPE", "CRYSTALEYES",
  91.     "CHROMADEPTH", "SIRDS",
  92.     "TWOCARDS", "ANAGLYPH_SOLID_ALTERNATE"
  93.     };
  94.  
  95. static int nstereotypes = 12;
  96.  
  97. static int driver_setup(int argc, char *argv[])
  98.     {
  99.     int mode = 0, serial = 1, buffsize = 2000, irq = 3;
  100.     unsigned int address = 0x2F8;
  101.     char *nickname = "No name device", *drivername = "Unknown device";
  102.     vrl_SerialPort *port = NULL;
  103.     vrl_DeviceDriverFunction *fn;
  104.     vrl_Device *device;
  105.     switch (argc)
  106.         {
  107.         case 1: return -1;    /* bad syntax */
  108.         default:
  109.         case 7: buffsize = strtoul(argv[6], NULL, 0);
  110.         case 6: irq = strtoul(argv[5], NULL, 0);
  111.         case 5: address = strtoul(argv[4], NULL, 0);
  112.         case 4: mode = strtoul(argv[3], NULL, 0);
  113.         case 3: drivername = strdup(argv[2]);
  114.         case 2: nickname = strdup(argv[1]);
  115.         }
  116.     if (!stricmp(drivername, "mouse")) { fn = vrl_MouseDevice; serial = 0; }
  117.     else if (!stricmp(drivername, "GDC")) { fn = vrl_GlobalDevice; serial = 1; }
  118.     else if (!stricmp(drivername, "Cyberman")) { fn = vrl_CybermanDevice; serial = 1; }
  119.     else if (!stricmp(drivername, "Spaceball")) { fn = vrl_SpaceballDevice; serial = 1; }
  120.     else if (!stricmp(drivername, "RedBaron")) { fn = vrl_RedbaronDevice; serial = 1; }
  121.     else if (!stricmp(drivername, "CTM")) { fn = vrl_CTMDevice; serial = 1; }
  122.     else if (!stricmp(drivername, "Isotrak")) { fn = vrl_IsotrakDevice; serial = 1; }
  123.     else if (!stricmp(drivername, "VIO")) { fn = vrl_VIODevice; serial = 1; }
  124. #ifdef VRL_PC_COMPATABLE
  125.     /* these next few only exist on PC's */
  126.     else if (!stricmp(drivername, "keypad")) { fn = vrl_KeypadDevice; serial = 0; }
  127.     else if (!stricmp(drivername, "joystick")) { fn = vrl_JoystickDevice; serial = 0; }
  128.     else if (!stricmp(drivername, "Pad")) { fn = vrl_PadDevice; serial = 0; }
  129.     else if (!stricmp(drivername, "CyberWand")) { fn = vrl_CyberwandDevice; serial = 0; }
  130.     else if (!stricmp(drivername, "7thSense")) { fn = vrl_7thSenseDevice; serial = 0; }
  131.     else if (!stricmp(drivername, "FifthGlove")) { fn = vrl_FifthDevice; serial = 0; }
  132. #endif
  133.     /* add other devices in here, as well as making entries in avrildrv.h */
  134.     else return -2;  /* unknown device */
  135.     if (serial)
  136.         {
  137.         port = vrl_SerialOpen(address, irq, buffsize);
  138.         if (port == NULL)
  139.             return -3;  /* couldn't open serial port */
  140.         }
  141.     device = vrl_DeviceOpen(fn, port);
  142.     if (device == NULL)
  143.         return -4;   /* couldn't open device */
  144.     vrl_DeviceSetNickname(device, nickname);
  145.     vrl_DeviceSetMode(device, mode);
  146.     return 0;    
  147.     }
  148.  
  149. int vrl_ReadCFGProcessLine(char *buff)
  150.     {
  151.     char buffcopy[256];
  152.     int argc;
  153.     char *argv[20];
  154.     vrl_StereoConfiguration *conf;
  155.     strcpy(buffcopy, buff);  /* unparsed version */
  156.     argc = parse(buff, " \t,", argv);
  157.     if (argc < 1) return 0;  /* ignore blank lines */
  158.     if (argc < 2) return 0;  /* all statements currently have at least one parameter */
  159.     switch (lookup(argv[0], statement_table, ncmds))
  160.         {
  161.         st_version: break;
  162.         case st_loadpath: vrl_FileSetLoadpath(argv[1]); break;
  163.         case st_include:
  164.             {
  165.             FILE *new = fopen(vrl_FileFixupFilename(argv[1]), "r");
  166.             if (new)
  167.                 {
  168.                 vrl_ReadCFG(new);
  169.                 fclose(new);
  170.                 }
  171.             }
  172.             break;
  173.         case st_device: driver_setup(argc, argv); break;
  174.         case st_cursor: if (stricmp(argv[1], "on")) vrl_VideoCursorHide(); break;
  175.         case st_compass: show_compass = !stricmp(argv[1], "on"); break;
  176.         case st_position: show_position = !stricmp(argv[1], "on"); break;
  177.         case st_framerate: show_framerate = !stricmp(argv[1], "on"); break;
  178.         case st_displaydriver:
  179.             if (argc < 2) break;
  180.             if (!stricmp(argv[1], "ModeY"))
  181.                 vrl_DisplaySetDriver(vrl_DisplayDriverModeY);
  182.             else
  183.                 vrl_DisplaySetDriver(vrl_DisplayDriverDefault);
  184.             vrl_DisplayInit(NULL);
  185.             break;
  186.         case st_videodriver:
  187.             {
  188.             int mode = 0;
  189.             if (argc < 2) break;
  190.             if (argc > 2) mode = strtoul(argv[2], NULL, 0);
  191.             vrl_VideoShutdown();
  192.             if (!stricmp(argv[1], "Mode13"))
  193.                 vrl_VideoSetDriver(vrl_VideoDriverMode13);
  194.             else if (!stricmp(argv[1], "ModeY"))
  195.                 vrl_VideoSetDriver(vrl_VideoDriverModeY);
  196.             else if (!stricmp(argv[1], "7thSense"))
  197.                 vrl_VideoSetDriver(vrl_VideoDriver7thSense);
  198.             vrl_VideoSetup(mode);
  199.             vrl_DisplayInit(NULL);
  200.             vrl_MouseReset();
  201.             }
  202.             break;
  203.         case st_devconfig:
  204.             {
  205.             vrl_Device *device;
  206.             int channel;
  207.             if (argc < 3) break;
  208.             device = vrl_DeviceFind(argv[1]);
  209.             if (device == NULL) break;
  210.             if (isdigit(*argv[2]))
  211.                 channel = atoi(argv[2]);
  212.             else
  213.                 channel = toupper(*argv[2]) - 'X' + ((toupper(argv[2][1]) == 'R') ? 3 : 0);
  214.             if (argc > 3)
  215.                 {
  216.                 if (toupper(*argv[3]) == 'A')
  217.                     vrl_DeviceSetScale(device, channel, float2angle(atof(&argv[3][1])));
  218.                 else
  219.                     vrl_DeviceSetScale(device, channel, float2scalar(atof(argv[3])));
  220.                 }
  221.             if (argc > 4)
  222.                 vrl_DeviceSetDeadzone(device, channel, float2scalar(atof(argv[4])));
  223.             }
  224.             break;
  225.         case st_stereotype:
  226.             conf = vrl_WorldGetStereoConfiguration();
  227.             if (conf == NULL)
  228.                 vrl_WorldSetStereoConfiguration(conf = vrl_StereoCreateConfiguration());
  229.             if (conf && argc > 1)
  230.                 {
  231.                 int n = lookup(argv[1], stereotypes, nstereotypes);
  232.                 if (n >= 0)
  233.                     vrl_StereoSetType(conf, n);
  234.                 vrl_WorldSetStereo(n);  /* NONE (==0) is FALSE, non-zero is TRUE */
  235.                 }
  236.             break;
  237.         case st_stereoparams:
  238.             conf = vrl_WorldGetStereoConfiguration();
  239.             if (conf == NULL)
  240.                 vrl_WorldSetStereoConfiguration(conf = vrl_StereoCreateConfiguration());
  241.             if (conf == NULL)
  242.                 break;
  243.             if (vrl_StereoGetType(conf) == VRL_STEREOTYPE_CHROMADEPTH)
  244.                 {                    
  245.                 if (argc > 2)
  246.                     vrl_StereoSetChromaFar(conf, float2scalar(atof(argv[2])));
  247.                 if (argc > 1)
  248.                     vrl_StereoSetChromaNear(conf, float2scalar(atof(argv[1])));
  249.                 }
  250.             else
  251.                 {
  252.                 if (argc > 2)
  253.                     vrl_StereoSetConvergence(conf, atof(argv[2]));
  254.                 if (argc > 1)
  255.                     vrl_StereoSetEyespacing(conf, atof(argv[1]));
  256.                 }
  257.             break;
  258.         case st_stereoleft:
  259.             conf = vrl_WorldGetStereoConfiguration();
  260.             if (conf == NULL)
  261.                 vrl_WorldSetStereoConfiguration(conf = vrl_StereoCreateConfiguration());
  262.             if (conf && argc > 2)
  263.                 vrl_StereoSetLeftEyeRotation(conf, float2angle(atof(argv[2])));
  264.             if (conf && argc > 1)
  265.                 vrl_StereoSetLeftEyeShift(conf, float2angle(atof(argv[1])));
  266.             break;
  267.         case st_stereoright:
  268.             conf = vrl_WorldGetStereoConfiguration();
  269.             if (conf == NULL)
  270.                 vrl_WorldSetStereoConfiguration(conf = vrl_StereoCreateConfiguration());
  271.             if (conf && argc > 2)
  272.                 vrl_StereoSetRightEyeRotation(conf, float2angle(atof(argv[2])));
  273.             if (conf && argc > 1)
  274.                 vrl_StereoSetRightEyeShift(conf, float2angle(atof(argv[1])));
  275.             break;
  276.         default:
  277.             vrl_ReadWLDfeature(argc, argv, buffcopy);
  278.             break;
  279.         }
  280.     return 0;
  281.     }
  282.  
  283. int vrl_ReadCFG(FILE *in)
  284.     {
  285.     char buff[256];
  286.     while (getline(buff, sizeof(buff), in))
  287.         vrl_ReadCFGProcessLine(buff);
  288.     return 0;
  289.     }
  290.  
  291. void vrl_ConfigSetCompassDisplay(vrl_Boolean flag)
  292.     {
  293.     show_compass = flag;
  294.     }
  295.  
  296. vrl_Boolean vrl_ConfigGetCompassDisplay(void)
  297.     {
  298.     return show_compass;
  299.     }
  300.  
  301. void vrl_ConfigToggleCompassDisplay(void)
  302.     {
  303.     show_compass = !show_compass;
  304.     }
  305.  
  306. void vrl_ConfigSetPositionDisplay(vrl_Boolean flag)
  307.     {
  308.     show_position = flag;
  309.     }
  310.  
  311. vrl_Boolean vrl_ConfigGetPositionDisplay(void)
  312.     {
  313.     return show_position;
  314.     }
  315.  
  316. void vrl_ConfigTogglePositionDisplay(void)
  317.     {
  318.     show_position = !show_position;
  319.     }
  320.  
  321. void vrl_ConfigSetFramerateDisplay(vrl_Boolean flag)
  322.     {
  323.     show_framerate = flag;
  324.     }
  325.  
  326. vrl_Boolean vrl_ConfigGetFramerateDisplay(void)
  327.     {
  328.     return show_framerate;
  329.     }
  330.  
  331. void vrl_ConfigToggleFramerateDisplay(void)
  332.     {
  333.     show_framerate = !show_framerate;
  334.     }
  335.  
  336. void vrl_ConfigStartup(char *filename)
  337.     {
  338.     vrl_SystemStartup();
  339.     vrl_ReadCFGfile(filename);
  340.     }
  341.  
  342. int vrl_ReadCFGfile(char *filename)
  343.     {
  344.     FILE *cfgfile;
  345.     cfgfile = fopen(vrl_FileFixupFilename(filename ? filename : "avril.cfg"), "r");
  346.     if (cfgfile)
  347.         {
  348.         vrl_ReadCFG(cfgfile);
  349.         fclose(cfgfile);
  350.         return 0;
  351.         }
  352.     return -1;
  353.     }
  354.