param.c 24.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * param.c:
 *****************************************************************************
4
 * Copyright (C) 2004 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * $Id$
 *
 * Authors: Cyril Deguet <asmax@videolan.org>
 *          code from projectM http://xmms-projectm.sourceforge.net
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28
 *****************************************************************************/



/* Basic Parameter Functions */

Rafaël Carré's avatar
Rafaël Carré committed
29
#include <stdio.h>
Christophe Mutricy's avatar
Christophe Mutricy committed
30 31
#include <string.h>
#include <stdlib.h>
32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <math.h>
#include "fatal.h"
#include "common.h"

#include "splaytree_types.h"
#include "splaytree.h"
#include "tree_types.h"

#include "param_types.h"
#include "param.h"

#include "expr_types.h"
#include "eval.h"

46
#include "engine_vars.h" 
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

void reset_param(param_t * param);

int is_valid_param_string(char * string); /* true if string is valid variable or function name */


/* A splay tree of builtin parameters */
splaytree_t * builtin_param_tree = NULL;

int insert_param_alt_name(param_t * param, char * alt_name);

int insert_builtin_param(param_t * param);

/* Private function prototypes */
int compare_param(char * name, char * name2);

63 64 65 66 67 68 69 70 71 72 73
int load_builtin_param_double(char * name, void * engine_val, void * matrix, short int flags, 
								double init_val, double upper_bound, double lower_bound, char * alt_name);

int load_builtin_param_int(char * name, void * engine_val, short int flags, 
								int init_val, int upper_bound, int lower_bound, char * alt_name);
								
int load_builtin_param_bool(char * name, void * engine_val, short int flags, 
								int init_val, char * alt_name);
								
								
								
74
param_t * create_param (char * name, short int type, short int flags, void * engine_val, void * matrix,
75
							value_t default_init_val, value_t upper_bound, value_t lower_bound) {
76 77 78 79 80 81

  param_t * param = NULL;

  param = (param_t*)malloc(sizeof(param_t));
 
  if (param == NULL) {
82 83
	printf("create_param: out of memory!!!\n");
	return NULL;
84
  }
85
  
86 87 88 89
  /* Clear name space, think the strncpy statement makes this redundant */
  //memset(param->name, 0, MAX_TOKEN_SIZE);

  /* Copy given name into parameter structure */
90 91
  strncpy(param->name, name, MAX_TOKEN_SIZE-1); 
  
92 93 94 95 96 97 98 99 100 101
  /* Assign other entries in a constructor like fashion */
  param->type = type;
  param->flags = flags;
  param->matrix_flag = 0;
  param->matrix = matrix;
  param->engine_val = engine_val;
  param->default_init_val = default_init_val;
  //*param->init_val = default_init_val;
  param->upper_bound = upper_bound;
  param->lower_bound = lower_bound;
102
  
103 104 105 106 107 108 109 110 111 112 113 114 115
  /* Return instantiated parameter */
  return param;

}

/* Creates a user defined parameter */
param_t * create_user_param(char * name) {

  param_t * param;
  value_t iv;
  value_t ub;
  value_t lb;
  double * engine_val;
116
  
117 118 119 120 121 122 123 124 125 126 127 128 129 130
  /* Set initial values to default */
  iv.double_val = DEFAULT_DOUBLE_IV;
  ub.double_val = DEFAULT_DOUBLE_UB;
  lb.double_val = DEFAULT_DOUBLE_LB;

  /* Argument checks */
  if (name == NULL)
    return NULL;

  /* Allocate space for the engine variable */
  if ((engine_val = (double*)malloc(sizeof(double))) == NULL)
    return NULL;

  (*engine_val) = iv.double_val; /* set some default init value */
131
  
132 133 134 135 136 137 138 139 140 141 142 143 144
  /* Create the new user parameter */
  if ((param = create_param(name, P_TYPE_DOUBLE, P_FLAG_USERDEF, engine_val, NULL, iv, ub, lb)) == NULL) {
    free(engine_val);
    return NULL;
  }
  if (PARAM_DEBUG) printf("create_param: \"%s\" initialized\n", param->name);
  /* Return the instantiated parameter */
  return param;
}

/* Initialize the builtin parameter database.
   Should only be necessary once */
int init_builtin_param_db() {
145
	
146 147
  /* Create the builtin parameter splay tree (go Sleator...) */
  if ((builtin_param_tree = create_splaytree(compare_string, copy_string, free_string)) == NULL) {
148 149 150
	  if (PARAM_DEBUG) printf("init_builtin_param_db: failed to initialize database (FATAL)\n");  
	  return OUTOFMEM_ERROR;
  } 
151 152

  if (PARAM_DEBUG) {
153 154
	  printf("init_builtin_param: loading database...");
	  fflush(stdout);
155
  }
156
  
157 158
  /* Loads all builtin parameters into the database */
  if (load_all_builtin_param() < 0) {
159
	if (PARAM_DEBUG) printf("failed loading builtin parameters (FATAL)\n");
160 161
    return ERROR;
  }
162
  
163
  if (PARAM_DEBUG) printf("success!\n");
164
	  
165 166 167 168 169 170 171
  /* Finished, no errors */
  return SUCCESS;
}

/* Destroy the builtin parameter database.
   Generally, do this on projectm exit */
int destroy_builtin_param_db() {
172
  
173 174 175
  splay_traverse(free_param, builtin_param_tree);
  destroy_splaytree(builtin_param_tree);
  builtin_param_tree = NULL;
176
  return SUCCESS;	
177 178 179 180 181 182

}


/* Insert a parameter into the database with an alternate name */
int insert_param_alt_name(param_t * param, char * alt_name) {
183
  
184 185 186
  if (param == NULL)
    return ERROR;
  if (alt_name == NULL)
187 188
	  return ERROR;
  	
189 190 191 192 193 194 195 196 197 198
  splay_insert_link(alt_name, param->name, builtin_param_tree);

  return SUCCESS;
}


param_t * find_builtin_param(char * name) {

  /* Null argument checks */
  if (name == NULL)
199 200
	  return NULL;
    
201 202 203 204 205 206 207 208 209 210 211
  return splay_find(name, builtin_param_tree);

}

/* Find a parameter given its name, will create one if not found */
param_t * find_param(char * name, preset_t * preset, int flags) {

  param_t * param = NULL;

  /* Null argument checks */
  if (name == NULL)
212
	  return NULL;
213
  if (preset == NULL)
214 215
	  return NULL;
  
216 217 218 219 220 221 222
  /* First look in the builtin database */
  param = (param_t *)splay_find(name, builtin_param_tree);

  /* If the search failed, check the user database */
  if (param == NULL) {
    param = (param_t*)splay_find(name, preset->user_param_tree);
  }
223 224
  /* If it doesn't exist in the user (or builtin) database and 
  	  create_flag is set, then make it and insert into the database 
225
  */
226
  
227
  if ((param == NULL) && (flags & P_CREATE)) {
228 229
	
	/* Check if string is valid */
230 231 232 233 234 235 236 237 238 239 240 241 242 243
    if (!is_valid_param_string(name)) {
      if (PARAM_DEBUG) printf("find_param: invalid parameter name:\"%s\"\n", name);
      return NULL;
    }
    /* Now, create the user defined parameter given the passed name */
    if ((param = create_user_param(name)) == NULL) {
      if (PARAM_DEBUG) printf("find_param: failed to create a new user parameter!\n");
      return NULL;
    }
    /* Finally, insert the new parameter into this preset's proper splaytree */
    if (splay_insert(param, param->name, preset->user_param_tree) < 0) {
      if (PARAM_DEBUG) printf("PARAM \"%s\" already exists in user parameter tree!\n", param->name);
      free_param(param);
      return NULL;
244 245 246 247
    }	 
    
  }	  
  
248 249
  /* Return the found (or created) parameter. Note that if P_CREATE is not set, this could be null */
  return param;
250
  
251 252 253 254 255 256 257 258 259
}

/* Compare string name with parameter name */
int compare_param(char * name, char * name2) {

  int cmpval;
  printf("am i used\n");
  /* Uses string comparison function */
  cmpval = strncmp(name, name2, MAX_TOKEN_SIZE-1);
260
  
261 262 263 264 265 266 267 268 269 270 271 272
  return cmpval;
}

/* Loads all builtin parameters, limits are also defined here */
int load_all_builtin_param() {

  load_builtin_param_double("fRating", (void*)&fRating, NULL, P_FLAG_NONE, 0.0 , 5.0, 0.0, NULL);
  load_builtin_param_double("fWaveScale", (void*)&fWaveScale, NULL, P_FLAG_NONE, 0.0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
  load_builtin_param_double("gamma", (void*)&fGammaAdj, NULL, P_FLAG_NONE, 0.0, MAX_DOUBLE_SIZE, 0, "fGammaAdj");
  load_builtin_param_double("echo_zoom", (void*)&fVideoEchoZoom, NULL, P_FLAG_NONE, 0.0, MAX_DOUBLE_SIZE, 0, "fVideoEchoZoom");
  load_builtin_param_double("echo_alpha", (void*)&fVideoEchoAlpha, NULL, P_FLAG_NONE, 0.0, MAX_DOUBLE_SIZE, 0, "fVideoEchoAlpha");
  load_builtin_param_double("wave_a", (void*)&fWaveAlpha, NULL, P_FLAG_NONE, 0.0, 1.0, 0, "fWaveAlpha");
273
  load_builtin_param_double("fWaveSmoothing", (void*)&fWaveSmoothing, NULL, P_FLAG_NONE, 0.0, 1.0, -1.0, NULL);  
274 275 276 277
  load_builtin_param_double("fModWaveAlphaStart", (void*)&fModWaveAlphaStart, NULL, P_FLAG_NONE, 0.0, 1.0, -1.0, NULL);
  load_builtin_param_double("fModWaveAlphaEnd", (void*)&fModWaveAlphaEnd, NULL, P_FLAG_NONE, 0.0, 1.0, -1.0, NULL);
  load_builtin_param_double("fWarpAnimSpeed",  (void*)&fWarpAnimSpeed, NULL, P_FLAG_NONE, 0.0, 1.0, -1.0, NULL);
  //  load_builtin_param_double("warp", (void*)&warp, warp_mesh, P_FLAG_NONE, 0.0, MAX_DOUBLE_SIZE, 0, NULL);
278
	
279 280 281 282 283
  load_builtin_param_double("fShader", (void*)&fShader, NULL, P_FLAG_NONE, 0.0, 1.0, -1.0, NULL);
  load_builtin_param_double("decay", (void*)&decay, NULL, P_FLAG_NONE, 0.0, 1.0, 0, "fDecay");

  load_builtin_param_int("echo_orient", (void*)&nVideoEchoOrientation, P_FLAG_NONE, 0, 3, 0, "nVideoEchoOrientation");
  load_builtin_param_int("wave_mode", (void*)&nWaveMode, P_FLAG_NONE, 0, 7, 0, "nWaveMode");
284
  
285 286 287 288 289 290 291 292 293 294 295 296 297 298
  load_builtin_param_bool("wave_additive", (void*)&bAdditiveWaves, P_FLAG_NONE, FALSE, "bAdditiveWaves");
  load_builtin_param_bool("bModWaveAlphaByVolume", (void*)&bModWaveAlphaByVolume, P_FLAG_NONE, FALSE, NULL);
  load_builtin_param_bool("wave_brighten", (void*)&bMaximizeWaveColor, P_FLAG_NONE, FALSE, "bMaximizeWaveColor");
  load_builtin_param_bool("wrap", (void*)&bTexWrap, P_FLAG_NONE, FALSE, "bTexWrap");
  load_builtin_param_bool("darken_center", (void*)&bDarkenCenter, P_FLAG_NONE, FALSE, "bDarkenCenter");
  load_builtin_param_bool("bRedBlueStereo", (void*)&bRedBlueStereo, P_FLAG_NONE, FALSE, NULL);
  load_builtin_param_bool("brighten", (void*)&bBrighten, P_FLAG_NONE, FALSE, "bBrighten");
  load_builtin_param_bool("darken", (void*)&bDarken, P_FLAG_NONE, FALSE, "bDarken");
  load_builtin_param_bool("solarize", (void*)&bSolarize, P_FLAG_NONE, FALSE, "bSolarize");
  load_builtin_param_bool("invert", (void*)&bInvert, P_FLAG_NONE, FALSE, "bInvert");
  load_builtin_param_bool("bMotionVectorsOn", (void*)&bMotionVectorsOn, P_FLAG_NONE, FALSE, NULL);
  load_builtin_param_bool("wave_dots", (void*)&bWaveDots, P_FLAG_NONE, FALSE, "bWaveDots");
  load_builtin_param_bool("wave_thick", (void*)&bWaveThick, P_FLAG_NONE, FALSE, "bWaveThick");
 
299 300
  
  
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
  load_builtin_param_double("zoom", (void*)&zoom, zoom_mesh,  P_FLAG_PER_PIXEL |P_FLAG_DONT_FREE_MATRIX, 0.0, MAX_DOUBLE_SIZE, 0, NULL);
  load_builtin_param_double("rot", (void*)&rot, rot_mesh,  P_FLAG_PER_PIXEL |P_FLAG_DONT_FREE_MATRIX, 0.0, MAX_DOUBLE_SIZE, MIN_DOUBLE_SIZE, NULL);
  load_builtin_param_double("zoomexp", (void*)&zoomexp, zoomexp_mesh,  P_FLAG_PER_PIXEL |P_FLAG_NONE, 0.0, MAX_DOUBLE_SIZE, 0, "fZoomExponent");
 
  load_builtin_param_double("cx", (void*)&cx, cx_mesh, P_FLAG_PER_PIXEL | P_FLAG_DONT_FREE_MATRIX, 0.0, 1.0, 0, NULL);
  load_builtin_param_double("cy", (void*)&cy, cy_mesh, P_FLAG_PER_PIXEL | P_FLAG_DONT_FREE_MATRIX, 0.0, 1.0, 0, NULL);
  load_builtin_param_double("dx", (void*)&dx, dx_mesh,  P_FLAG_PER_PIXEL | P_FLAG_DONT_FREE_MATRIX, 0.0, MAX_DOUBLE_SIZE, MIN_DOUBLE_SIZE, NULL);
  load_builtin_param_double("dy", (void*)&dy, dy_mesh,  P_FLAG_PER_PIXEL |P_FLAG_DONT_FREE_MATRIX, 0.0, MAX_DOUBLE_SIZE, MIN_DOUBLE_SIZE, NULL);
  load_builtin_param_double("sx", (void*)&sx, sx_mesh,  P_FLAG_PER_PIXEL |P_FLAG_DONT_FREE_MATRIX, 0.0, MAX_DOUBLE_SIZE, 0, NULL);
  load_builtin_param_double("sy", (void*)&sy, sy_mesh,  P_FLAG_PER_PIXEL |P_FLAG_DONT_FREE_MATRIX, 0.0, MAX_DOUBLE_SIZE, 0, NULL);

  load_builtin_param_double("wave_r", (void*)&wave_r, NULL, P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("wave_g", (void*)&wave_g, NULL, P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("wave_b", (void*)&wave_b, NULL, P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("wave_x", (void*)&wave_x, NULL, P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("wave_y", (void*)&wave_y, NULL, P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("wave_mystery", (void*)&wave_mystery, NULL, P_FLAG_NONE, 0.0, 1.0, -1.0, "fWaveParam");
318
  
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
  load_builtin_param_double("ob_size", (void*)&ob_size, NULL, P_FLAG_NONE, 0.0, 0.5, 0, NULL);
  load_builtin_param_double("ob_r", (void*)&ob_r, NULL, P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("ob_g", (void*)&ob_g, NULL, P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("ob_b", (void*)&ob_b, NULL, P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("ob_a", (void*)&ob_a, NULL, P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);

  load_builtin_param_double("ib_size", (void*)&ib_size,  NULL,P_FLAG_NONE, 0.0, .5, 0.0, NULL);
  load_builtin_param_double("ib_r", (void*)&ib_r,  NULL,P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("ib_g", (void*)&ib_g,  NULL,P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("ib_b", (void*)&ib_b,  NULL,P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("ib_a", (void*)&ib_a,  NULL,P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);

  load_builtin_param_double("mv_r", (void*)&mv_r,  NULL,P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("mv_g", (void*)&mv_g,  NULL,P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("mv_b", (void*)&mv_b,  NULL,P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);
  load_builtin_param_double("mv_x", (void*)&mv_x,  NULL,P_FLAG_NONE, 0.0, 64.0, 0.0, "nMotionVectorsX");
  load_builtin_param_double("mv_y", (void*)&mv_y,  NULL,P_FLAG_NONE, 0.0, 48.0, 0.0, "nMotionVectorsY");
  load_builtin_param_double("mv_l", (void*)&mv_l,  NULL,P_FLAG_NONE, 0.0, 5.0, 0.0, NULL);
  load_builtin_param_double("mv_dy", (void*)&mv_dy, NULL, P_FLAG_NONE, 0.0, 1.0, -1.0, NULL);
  load_builtin_param_double("mv_dx", (void*)&mv_dx,  NULL,P_FLAG_NONE, 0.0, 1.0, -1.0, NULL);
  load_builtin_param_double("mv_a", (void*)&mv_a,  NULL,P_FLAG_NONE, 0.0, 1.0, 0.0, NULL);

341
  load_builtin_param_double("time", (void*)&Time,  NULL,P_FLAG_READONLY, 0.0, MAX_DOUBLE_SIZE, 0.0, NULL);        
342
  load_builtin_param_double("bass", (void*)&bass,  NULL,P_FLAG_READONLY, 0.0, MAX_DOUBLE_SIZE, 0.0, NULL);
343
  load_builtin_param_double("mid", (void*)&mid,  NULL,P_FLAG_READONLY, 0.0, MAX_DOUBLE_SIZE, 0, NULL);      
344 345 346 347 348 349 350 351 352
  load_builtin_param_double("bass_att", (void*)&bass_att,  NULL,P_FLAG_READONLY, 0.0, MAX_DOUBLE_SIZE, 0, NULL);
  load_builtin_param_double("mid_att", (void*)&mid_att,  NULL,P_FLAG_READONLY, 0.0, MAX_DOUBLE_SIZE, 0, NULL);
  load_builtin_param_double("treb_att", (void*)&treb_att,  NULL,P_FLAG_READONLY, 0.0, MAX_DOUBLE_SIZE, 0, NULL);
  load_builtin_param_int("frame", (void*)&frame, P_FLAG_READONLY, 0, MAX_INT_SIZE, 0, NULL);
  load_builtin_param_double("progress", (void*)&progress,  NULL,P_FLAG_READONLY, 0.0, 1, 0, NULL);
  load_builtin_param_int("fps", (void*)&fps, P_FLAG_NONE, 15, MAX_INT_SIZE, 0, NULL);



353 354 355 356 357 358 359 360
  load_builtin_param_double("x", (void*)&x_per_pixel, x_mesh,  P_FLAG_PER_PIXEL |P_FLAG_ALWAYS_MATRIX | P_FLAG_READONLY | P_FLAG_DONT_FREE_MATRIX, 
			    0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
  load_builtin_param_double("y", (void*)&y_per_pixel, y_mesh,  P_FLAG_PER_PIXEL |P_FLAG_ALWAYS_MATRIX |P_FLAG_READONLY | P_FLAG_DONT_FREE_MATRIX, 
			    0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
  load_builtin_param_double("ang", (void*)&ang_per_pixel, theta_mesh,  P_FLAG_PER_PIXEL |P_FLAG_ALWAYS_MATRIX | P_FLAG_READONLY | P_FLAG_DONT_FREE_MATRIX, 
			    0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);      
  load_builtin_param_double("rad", (void*)&rad_per_pixel, rad_mesh,  P_FLAG_PER_PIXEL |P_FLAG_ALWAYS_MATRIX | P_FLAG_READONLY | P_FLAG_DONT_FREE_MATRIX, 
			    0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377


  load_builtin_param_double("q1", (void*)&q1,  NULL, P_FLAG_PER_PIXEL |P_FLAG_QVAR, 0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
  load_builtin_param_double("q2", (void*)&q2,  NULL, P_FLAG_PER_PIXEL |P_FLAG_QVAR, 0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
  load_builtin_param_double("q3", (void*)&q3,  NULL, P_FLAG_PER_PIXEL |P_FLAG_QVAR, 0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
  load_builtin_param_double("q4", (void*)&q4,  NULL, P_FLAG_PER_PIXEL |P_FLAG_QVAR, 0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
  load_builtin_param_double("q5", (void*)&q5,  NULL, P_FLAG_PER_PIXEL |P_FLAG_QVAR, 0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
  load_builtin_param_double("q6", (void*)&q6,  NULL, P_FLAG_PER_PIXEL |P_FLAG_QVAR, 0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
  load_builtin_param_double("q7", (void*)&q7,  NULL, P_FLAG_PER_PIXEL |P_FLAG_QVAR, 0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);
  load_builtin_param_double("q8", (void*)&q8,  NULL, P_FLAG_PER_PIXEL |P_FLAG_QVAR, 0, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, NULL);



  /* variables added in 1.04 */
  load_builtin_param_int("meshx", (void*)&gx, P_FLAG_READONLY, 32, 96, 8, NULL);
  load_builtin_param_int("meshy", (void*)&gy, P_FLAG_READONLY, 24, 72, 6, NULL);

378 379
  return SUCCESS;  
  
380 381 382 383 384 385
}

/* Free's a parameter type */
void free_param(param_t * param) {
  int x;
  if (param == NULL)
386 387
	return;
  
388 389 390 391 392 393 394 395 396 397 398
  if (param->flags & P_FLAG_USERDEF) {
    free(param->engine_val);

  }

  if (!(param->flags & P_FLAG_DONT_FREE_MATRIX)) {

    if (param->flags & P_FLAG_PER_POINT)
      free(param->matrix);

    else if (param->flags & P_FLAG_PER_PIXEL) {
399 400
       for(x = 0; x < gx; x++) 
	   free(((double**)param->matrix)[x]);
401 402 403 404 405 406 407 408 409 410
       free(param->matrix);
      }
  }

  if (PARAM_DEBUG) printf("free_param: freeing \"%s\".\n", param->name);
  free(param);

}

/* Loads a double parameter into the builtin database */
411 412
int load_builtin_param_double(char * name, void * engine_val, void * matrix, short int flags, 
						double init_val, double upper_bound, double lower_bound, char * alt_name) {
413 414 415 416 417 418 419

  param_t * param = NULL;
  value_t iv, ub, lb;

  iv.double_val = init_val;
  ub.double_val = upper_bound;
  lb.double_val = lower_bound;
420
							
421 422
  /* Create new parameter of type double */
  if (PARAM_DEBUG == 2) {
423 424 425 426
	  printf("load_builtin_param_double: (name \"%s\") (alt_name = \"%s\") ", name, alt_name);
	  fflush(stdout);
  }  
  
427 428 429
 if ((param = create_param(name, P_TYPE_DOUBLE, flags, engine_val, matrix, iv, ub, lb)) == NULL) {
    return OUTOFMEM_ERROR;
  }
430
  
431
  if (PARAM_DEBUG == 2) {
432 433 434 435
	printf("created...");
	fflush(stdout);
   }	  
   
436 437 438
  /* Insert the paremeter into the database */

  if (insert_builtin_param(param) < 0) {
439
	free_param(param);
440 441 442 443
    return ERROR;
  }

  if (PARAM_DEBUG == 2) {
444 445 446 447
	  printf("inserted...");
	  fflush(stdout);
  }  
  
448
  /* If this parameter has an alternate name, insert it into the database as link */
449
  
450
  if (alt_name != NULL) {
451
	insert_param_alt_name(param, alt_name); 
452 453

  if (PARAM_DEBUG == 2) {
454 455 456 457 458 459 460 461
	  printf("alt_name inserted...");
	  fflush(stdout);
  	}
  
	
  }  	  

  if (PARAM_DEBUG == 2) printf("finished\n");	  
462 463 464 465 466 467 468 469
  /* Finished, return success */
  return SUCCESS;
}



/* Loads a double parameter into the builtin database */
param_t * new_param_double(char * name, short int flags, void * engine_val, void * matrix,
470
						double upper_bound, double lower_bound, double init_val) {
471 472 473 474 475 476 477

  param_t * param;
  value_t iv, ub, lb;

  iv.double_val = init_val;
  ub.double_val = upper_bound;
  lb.double_val = lower_bound;
478 479
			     			
  if ((param = create_param(name, P_TYPE_DOUBLE, flags, engine_val, matrix,iv, ub, lb)) == NULL) 
480
    return NULL;
481 482
  
  
483 484 485 486 487 488 489
  /* Finished, return success */
  return param;
}


/* Creates a new parameter of type int */
param_t * new_param_int(char * name, short int flags, void * engine_val,
490
						int upper_bound, int lower_bound, int init_val) {
491 492 493 494 495 496 497

  param_t * param;
  value_t iv, ub, lb;

  iv.int_val = init_val;
  ub.int_val = upper_bound;
  lb.int_val = lower_bound;
498 499
							
  if ((param = create_param(name, P_TYPE_INT, flags, engine_val, NULL, iv, ub, lb)) == NULL) 
500
    return NULL;
501
  
502 503 504 505 506 507 508
 
  /* Finished, return success */
  return param;
}

/* Creates a new parameter of type bool */
param_t * new_param_bool(char * name, short int flags, void * engine_val,
509
						int upper_bound, int lower_bound, int init_val) {
510 511 512 513 514 515 516

  param_t * param;
  value_t iv, ub, lb;

  iv.bool_val = init_val;
  ub.bool_val = upper_bound;
  lb.bool_val = lower_bound;
517
							
518 519
  if ((param = create_param(name, P_TYPE_BOOL, flags, engine_val, NULL, iv, ub, lb)) == NULL)
    return NULL;
520
  
521 522 523 524 525 526 527 528
 
  /* Finished, return success */
  return param;
}


/* Loads a integer parameter into the builtin database */
int load_builtin_param_int(char * name, void * engine_val, short int flags,
529
						int init_val, int upper_bound, int lower_bound, char * alt_name) {
530 531 532 533 534 535

  param_t * param;
  value_t iv, ub, lb;

  iv.int_val = init_val;
  ub.int_val = upper_bound;
536 537
  lb.int_val = lower_bound;	
							
538 539 540 541 542 543 544
  param = create_param(name, P_TYPE_INT, flags, engine_val, NULL, iv, ub, lb);

  if (param == NULL) {
    return OUTOFMEM_ERROR;
  }

  if (insert_builtin_param(param) < 0) {
545
	free_param(param);
546 547
    return ERROR;
  }
548
  
549
  if (alt_name != NULL) {
550 551 552
	insert_param_alt_name(param, alt_name);  	 
  }  
  
553 554
  return SUCCESS;

555 556
}							
							
557
/* Loads a boolean parameter */
558 559
int load_builtin_param_bool(char * name, void * engine_val, short int flags, 
								int init_val, char * alt_name) {
560 561 562 563 564 565

  param_t * param;
  value_t iv, ub, lb;

  iv.int_val = init_val;
  ub.int_val = TRUE;
566 567
  lb.int_val = FALSE;	
																
568 569 570 571 572 573 574
  param = create_param(name, P_TYPE_BOOL, flags, engine_val, NULL, iv, ub, lb);

  if (param == NULL) {
    return OUTOFMEM_ERROR;
  }

  if (insert_builtin_param(param) < 0) {
575
	free_param(param);
576 577
    return ERROR;
  }
578
  
579
  if (alt_name != NULL) {
580 581 582
	insert_param_alt_name(param, alt_name);  	 
  }  
  
583 584 585 586 587
  return SUCCESS;

}


588
	
589 590 591

/* Returns nonzero if the string is valid parameter name */
int is_valid_param_string(char * string) {
592
  
593 594
  if (string == NULL)
    return FALSE;
595
  
596 597
  /* This ensures the first character is non numeric */
  if( ((*string) >= 48) && ((*string) <= 57))
598
    return FALSE; 
599 600 601

  /* These probably should never happen */
  if (*string == '.')
602 603
	return FALSE;
  
604
  if (*string == '+')
605 606
	return FALSE;
  
607
  if (*string == '-')
608 609
	return FALSE;
  
610
  /* Could also add checks for other symbols. May do later */
611
  
612
  return TRUE;
613
   
614 615 616 617 618
}

/* Inserts a parameter into the builtin database */
int insert_builtin_param(param_t * param) {

619 620 621 622
	if (param == NULL)
		return FAILURE;
	
	return splay_insert(param, param->name, builtin_param_tree);	
623 624 625 626 627
}

/* Inserts a parameter into the builtin database */
int insert_param(param_t * param, splaytree_t * database) {

628 629 630 631
	if (param == NULL)
	  return FAILURE;
	if (database == NULL)
	  return FAILURE;
632

633
	return splay_insert(param, param->name, database);	
634 635 636 637
}


/* Sets the parameter engine value to value val.
638
	clipping occurs if necessary */
639 640
void set_param(param_t * param, double val) {

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	switch (param->type) {
		
	case P_TYPE_BOOL:
		if (val < 0)
			*((int*)param->engine_val) = 0;
		else if (val > 0)
			*((int*)param->engine_val) = 1;
		else
			*((int*)param->engine_val) = 0;
		break;
	case P_TYPE_INT:
		/* Make sure value is an integer */
		val = floor(val);
		if (val < param->lower_bound.int_val)
				*((int*)param->engine_val) = param->lower_bound.int_val;
		else if (val > param->upper_bound.int_val)
				*((int*)param->engine_val) = param->upper_bound.int_val;
		else
				*((int*)param->engine_val) = val;
		break;
	case P_TYPE_DOUBLE:
	  /* Make sure value is an integer */	

	 
	  if (val < param->lower_bound.double_val) 
	    *((double*)param->engine_val) = param->lower_bound.double_val;	  
	  else if (val > param->upper_bound.double_val)
	    *((double*)param->engine_val) = param->upper_bound.double_val;
	  else
	    *((double*)param->engine_val) = val;
	  break;
	default:
	  break;

	}
	
	return;
678 679 680 681 682
}




683
/* Search for parameter 'name' in 'database', if create_flag is true, then generate the parameter 
684 685 686 687 688 689 690 691 692 693
   and insert it into 'database' */
param_t * find_param_db(char * name, splaytree_t * database, int create_flag) {

  param_t * param = NULL;

  /* Null argument checks */
  if (name == NULL)
    return NULL;
  if (database == NULL)
    return NULL;
694
  
695 696 697
  /* First look in the builtin database */
  param = (param_t *)splay_find(name, database);

698
  
699
  if (((param = (param_t *)splay_find(name, database)) == NULL) && (create_flag == TRUE)) {
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	
	/* Check if string is valid */
	if (!is_valid_param_string(name))
		return NULL;
	
	/* Now, create the user defined parameter given the passed name */
	if ((param = create_user_param(name)) == NULL)
		return NULL;
	
	/* Finally, insert the new parameter into this preset's proper splaytree */
	if (splay_insert(param, param->name, database) < 0) {
		free_param(param);
		return NULL;
	}	 
	
  }	  
  
717 718 719 720 721
  /* Return the found (or created) parameter. Note that this could be null */
  return param;

}