1: /*
     2:  *  Guitar-ZyX(tm)::MasterControlProgram - portable guitar F/X controller
     3:  *  Copyright (C) 2009  Douglas McClendon
     4:  *
     5:  *  This program is free software: you can redistribute it and/or modify
     6:  *  it under the terms of the GNU General Public License as published by
     7:  *  the Free Software Foundation, version 3 of the License.
     8:  *
     9:  *  This program is distributed in the hope that it will be useful,
    10:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    11:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12:  *  GNU General Public License for more details.
    13:  *
    14:  *  You should have received a copy of the GNU General Public License
    15:  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16: */
    17: /*
    18: #############################################################################
    19: #############################################################################
    20: # 
    21: # gzmcpc::configfiles: configfile functions
    22: #
    23: #############################################################################
    24: ##
    25: ## Copyright 2008-2009 Douglas McClendon <dmc AT filteredperception DOT org>
    26: ##
    27: #############################################################################
    28: #############################################################################
    29: #
    30: */
    31: 
    32: 
    33: 
    34: 
    35: 
    36: #include <stdio.h> 
    37: #include <stdlib.h> 
    38: #include <string.h> 
    39: #include <dirent.h> 
    40: #include <sys/stat.h> 
    41: #include <unistd.h> 
    42: 
    43: 
    44: #include <fat.h> 
    45: 
    46: 
    47: #include "mode__tpw__jam.h" 
    48: 
    49: #include "configfiles.h" 
    50: 
    51: #include "debug.h" 
    52: 
    53: #include "network.h" 
    54: 
    55: #include "rak.h" 
    56: 
    57: 
    58: 
    59: int fs_avail = 0;
    60: 
    61: 
    62: char linebuf[MAX_CONFIG_LINE_SIZE + 1];
    63: char rest_buf[MAX_CONFIG_LINE_SIZE + 1];
    64: char parm_id_buf[MAX_CONFIG_LINE_SIZE + 1];
    65: char parm_name_buf[MAX_CONFIG_LINE_SIZE + 1];
    66: 
    67: 
    68: 
    69: 
    70: 
    71: void 
    72: gzmcpc_read_config(void) {
    73: 
    74:   FILE *fs_file;
    75: 
    76:   int param_id_dec;
    77:   int rv;
    78: 
    79:   // this is a bit gross, need to refactor configuration options
    80:   // initialize ssid
    81:   snprintf(ap_ssid, SSID_MAXLEN, "%s", ZYX_AP_NAME);
    82: 
    83:   // initialize filesystem access if possible
    84:   fs_avail=fatInitDefault();
    85:   
    86:   if (fs_avail) {
    87: 
    88:     // TODO
    89:     //        - (opt) default whammypad_[xy]_inverse (boolean:true/false)
    90:     //        - (opt) default whammypad_[xy]_minimum (integer 0..127) (err, should be per parameter)
    91:     //        - (opt) default whammypad_[xy]_maximum (integer 0..127) (err, should be per parameter)
    92:     //        - (opt) default final/master volume (integer for now)
    93: 
    94:     //      - does ///defaults exist?
    95:     //        - if so read it
    96:     //	- if not, write a template
    97:     //      - does ///midispec exist?
    98:     //        - if so read it
    99:     //	- if not, write a template
   100:         
   101:     // IDEA: have trapdoor goto, closes files if open, etc "configuration file reading failed badly"
   102:     
   103:     // TODO- errorcheck, mkdir, etc...
   104:     //    fs_dir = opendir("/boot/zyx/gzmcp");
   105: 
   106:     // XXX: this is probably stupid, should just try opening
   107:     //      files and watching return/error values for things
   108:     //      like file not exist
   109: 
   110:     //    found_conf_file = 0;
   111:     //    while ((fs_dirent = readdir(fs_dir)) != NULL) {
   112:       
   113:     ////    stat(fs_dirent->d_name, &fs_stat_info);
   114:     ////    fs_stat_info.st_size;
   115:       
   116:     //      if (! S_ISDIR(fs_stat_info.st_mode)) {
   117:     //	if (strcmp((const char *)(fs_dirent->d_name), "defaults") == 0) {
   118:     //	  found_conf_file = 1;
   119:     //	  break;
   120:     //	}
   121:     //      } // end if not a directory
   122:       
   123:     //    } // end iterating over directory entries
   124: 
   125: 
   126:     ////
   127:     //// defaults
   128:     ////
   129:     fs_file = fopen("/gzmcp/defaults", "r");
   130:     
   131:     while (fgets(linebuf, MAX_CONFIG_LINE_SIZE, fs_file) != NULL) {
   132:       rv = sscanf(linebuf, 
   133: 		  "%[^=]=%[^\n]\n", 
   134: 		  parm_name_buf,
   135: 		  parm_id_buf);
   136:       if (rv >= 2) {
   137: 	if (strncmp(parm_name_buf, "initial_effect", MAX_CONFIG_LINE_SIZE) == 0) {
   138: 	  // bah...
   139: 	} else if (strncmp(parm_name_buf, "initial_whammypad_x_parameter", MAX_CONFIG_LINE_SIZE) == 0) {
   140: 	  touch_whammy_x_midi_parm = (int)strtol(parm_id_buf, (char **) NULL, 0);
   141: 	} else if (strncmp(parm_name_buf, "initial_whammypad_y_parameter", MAX_CONFIG_LINE_SIZE) == 0) {
   142: 	  touch_whammy_y_midi_parm = (int)strtol(parm_id_buf, (char **) NULL, 0);
   143: 	} else if (strncmp(parm_name_buf, "button_a_preset_num", MAX_CONFIG_LINE_SIZE) == 0) {
   144: 	  button_a_preset_num = (int)strtol(parm_id_buf, (char **) NULL, 0);
   145: 	} else if (strncmp(parm_name_buf, "button_b_preset_num", MAX_CONFIG_LINE_SIZE) == 0) {
   146: 	  button_b_preset_num = (int)strtol(parm_id_buf, (char **) NULL, 0);
   147: 	} else if (strncmp(parm_name_buf, "button_x_preset_num", MAX_CONFIG_LINE_SIZE) == 0) {
   148: 	  button_x_preset_num = (int)strtol(parm_id_buf, (char **) NULL, 0);
   149: 	} else if (strncmp(parm_name_buf, "button_y_preset_num", MAX_CONFIG_LINE_SIZE) == 0) {
   150: 	  button_y_preset_num = (int)strtol(parm_id_buf, (char **) NULL, 0);
   151: 	} else if (strncmp(parm_name_buf, "vstrobe_bpm", MAX_CONFIG_LINE_SIZE) == 0) {
   152: 	  vstrobe_bpm = (int)strtol(parm_id_buf, (char **) NULL, 0);
   153: 	} else if (strncmp(parm_name_buf, "debug_log", MAX_CONFIG_LINE_SIZE) == 0) {
   154: 	  debug_log_init(parm_id_buf);
   155: 	} else if (strncmp(parm_name_buf, "ap_ssid", MAX_CONFIG_LINE_SIZE) == 0) {
   156: 	  snprintf(ap_ssid, SSID_MAXLEN, "%s", parm_id_buf);
   157: 	} else {
   158: 	  // unknown entry
   159: 	}
   160:       } else {
   161: 	// bad line
   162:       }
   163:     } // end while (!done)
   164: 
   165:     fclose(fs_file);
   166: 
   167:     ////
   168:     //// /defaults
   169:     ////
   170: 
   171: 
   172:     ////
   173:     //// midispec
   174:     ////
   175:     fs_file = fopen("/gzmcp/midispec", "r");
   176:     
   177:     while (fgets(linebuf, MAX_CONFIG_LINE_SIZE, fs_file) != NULL) {
   178:       rv = sscanf(linebuf, 
   179: 		  "midival:%[^:]:%[^:]:%[^\n]\n", 
   180: 		  parm_id_buf,
   181: 		  parm_name_buf,
   182: 		  rest_buf);
   183:       if (rv >= 2) {
   184: 	param_id_dec = strtol(parm_id_buf, (char **) NULL, 0);
   185: 	snprintf(rak_midi_labels[param_id_dec], MAX_CONFIG_LINE_SIZE, "%s", parm_name_buf);
   186:       } else {
   187: 	// bad line
   188:       }
   189:     } // end while (!done)
   190:     
   191:     fclose(fs_file);
   192: 
   193:     ////
   194:     //// /midispec
   195:     ////
   196: 
   197:     ////
   198:     //// presets
   199:     ////
   200:     fs_file = fopen("/gzmcp/presets", "r");
   201:     
   202:     while (fgets(linebuf, MAX_CONFIG_LINE_SIZE, fs_file) != NULL) {
   203:       rv = sscanf(linebuf, 
   204: 		  "preset:::%[^:]:::%[^\n]\n", 
   205: 		  parm_id_buf,
   206: 		  parm_name_buf);
   207:       if (rv >= 2) {
   208: 	param_id_dec = strtol(parm_id_buf, (char **) NULL, 0);
   209: 	// XXX constants, probably conflicting (string lengths)
   210: 	snprintf(rak_preset_labels[param_id_dec - 1], 
   211: 		 MAX_CONFIG_LINE_SIZE, 
   212: 		 "%s", 
   213: 		 parm_name_buf);
   214:       } else {
   215: 	// bad line
   216:       }
   217:     } // end while (!done)
   218:     
   219:     fclose(fs_file);
   220: 
   221:     ////
   222:     //// /presets
   223:     ////
   224:   
   225:   } // end if fs_avail
   226: 
   227: }
   228: 
   229: 
   230: void rewrite_defaults_with_new_ssid(void) {
   231: 
   232:   int rv;
   233: 
   234:   FILE *defaults_file;
   235:   FILE *tmp_defaults_file;
   236: 
   237:   if (! fs_avail) return;
   238: 
   239:   // sigh, no umask or chmod, so take pains to overwrite the original file
   240:   // TODO: check return values of everything, for now... a little faith
   241:   defaults_file = fopen("/gzmcp/defaults", "r");
   242:   tmp_defaults_file = fopen("/gzmcp/defaults.tmp", "w");
   243:   while (fgets(linebuf, MAX_CONFIG_LINE_SIZE, defaults_file) != NULL) {
   244:       fprintf(tmp_defaults_file, "%s", linebuf);
   245:   }
   246:   fclose(defaults_file);
   247:   fclose(tmp_defaults_file);
   248: 
   249:   tmp_defaults_file = fopen("/gzmcp/defaults.tmp", "r");
   250:   defaults_file = fopen("/gzmcp/defaults", "w");
   251:   
   252:   while (fgets(linebuf, MAX_CONFIG_LINE_SIZE, tmp_defaults_file) != NULL) {
   253:     rv = sscanf(linebuf, 
   254: 		"%[^=]=%[^\n]\n", 
   255: 		parm_name_buf,
   256: 		parm_id_buf);
   257:     if (rv >= 2) {
   258:       if (strncmp(parm_name_buf, "ap_ssid", MAX_CONFIG_LINE_SIZE) == 0) {
   259: 	// write out new entry
   260: 	fprintf(defaults_file, "ap_ssid=%s\n", ap_ssid);
   261:       } else {
   262: 	// normal entry
   263: 	fprintf(defaults_file, "%s", linebuf);
   264:       }
   265:     } else {
   266:       fprintf(defaults_file, "%s", linebuf);
   267:     }
   268:   } // end while (!done)
   269: 
   270:   fclose(defaults_file);
   271:   fclose(tmp_defaults_file);
   272: 
   273:   unlink("/gzmcp/defaults.tmp");
   274: }
   275: 
   276: