Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

preferences.C

Go to the documentation of this file.
00001 #include "asset.h"
00002 #include "audioconfig.h"
00003 #include "audiodevice.inc"
00004 #include "bcmeter.inc"
00005 #include "cache.inc"
00006 #include "clip.h"
00007 #include "defaults.h"
00008 #include "file.inc"
00009 #include "filesystem.h"
00010 #include "guicast.h"
00011 #include "preferences.h"
00012 #include "theme.h"
00013 #include "videoconfig.h"
00014 #include "videodevice.inc"
00015 #include <string.h>
00016 
00017 //#define CLAMP(x, y, z) (x) = ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x)))
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 Preferences::Preferences()
00031 {
00032 // Set defaults
00033         FileSystem fs;
00034 
00035         preferences_lock = new Mutex("Preferences::preferences_lock");
00036         sprintf(index_directory, BCASTDIR);
00037         if(strlen(index_directory))
00038                 fs.complete_path(index_directory);
00039         cache_size = 0xa00000;
00040         index_size = 0x300000;
00041         index_count = 100;
00042         use_thumbnails = 1;
00043         theme[0] = 0;
00044         use_renderfarm = 0;
00045         force_uniprocessor = 0;
00046         renderfarm_port = DEAMON_PORT;
00047         render_preroll = 0.5;
00048         brender_preroll = 0;
00049         renderfarm_mountpoint[0] = 0;
00050         renderfarm_vfs = 0;
00051         renderfarm_job_count = 20;
00052         processors = calculate_processors();
00053 
00054 // Default brender asset
00055         brender_asset = new Asset;
00056         brender_asset->audio_data = 0;
00057         brender_asset->video_data = 1;
00058         sprintf(brender_asset->path, "/tmp/brender");
00059         brender_asset->format = FILE_JPEG_LIST;
00060         brender_asset->jpeg_quality = 80;
00061 
00062         use_brender = 0;
00063         brender_fragment = 1;
00064         local_rate = 0.0;
00065 
00066         use_tipwindow = 1;
00067 }
00068 
00069 Preferences::~Preferences()
00070 {
00071         delete brender_asset;
00072         delete preferences_lock;
00073 }
00074 
00075 void Preferences::copy_rates_from(Preferences *preferences)
00076 {
00077         preferences_lock->lock("Preferences::copy_rates_from");
00078 // Need to match node titles in case the order changed and in case
00079 // one of the nodes in the source is the master node.
00080         local_rate = preferences->local_rate;
00081 
00082         for(int j = 0; 
00083                 j < preferences->renderfarm_nodes.total; 
00084                 j++)
00085         {
00086                 double new_rate = preferences->renderfarm_rate.values[j];
00087 // Put in the master node
00088                 if(preferences->renderfarm_nodes.values[j][0] == '/')
00089                 {
00090                         if(!EQUIV(new_rate, 0.0))
00091                                 local_rate = new_rate;
00092                 }
00093                 else
00094 // Search for local node and copy it to that node
00095                 if(!EQUIV(new_rate, 0.0))
00096                 {
00097                         for(int i = 0; i < renderfarm_nodes.total; i++)
00098                         {
00099                                 if(!strcmp(preferences->renderfarm_nodes.values[j], renderfarm_nodes.values[i]) &&
00100                                         preferences->renderfarm_ports.values[j] == renderfarm_ports.values[i])
00101                                 {
00102                                         renderfarm_rate.values[i] = new_rate;
00103                                         break;
00104                                 }
00105                         }
00106                 }
00107         }
00108 
00109 //printf("Preferences::copy_rates_from 1 %f %f\n", local_rate, preferences->local_rate);
00110         preferences_lock->unlock();
00111 }
00112 
00113 void Preferences::copy_from(Preferences *that)
00114 {
00115 // ================================= Performance ================================
00116         strcpy(index_directory, that->index_directory);
00117         index_size = that->index_size;
00118         index_count = that->index_count;
00119         use_thumbnails = that->use_thumbnails;
00120         strcpy(global_plugin_dir, that->global_plugin_dir);
00121         strcpy(theme, that->theme);
00122 
00123         use_tipwindow = that->use_tipwindow;
00124 
00125         cache_size = that->cache_size;
00126         force_uniprocessor = that->force_uniprocessor;
00127         processors = calculate_processors();
00128         renderfarm_nodes.remove_all_objects();
00129         renderfarm_ports.remove_all();
00130         renderfarm_enabled.remove_all();
00131         renderfarm_rate.remove_all();
00132         local_rate = that->local_rate;
00133         for(int i = 0; i < that->renderfarm_nodes.total; i++)
00134         {
00135                 add_node(that->renderfarm_nodes.values[i], 
00136                         that->renderfarm_ports.values[i],
00137                         that->renderfarm_enabled.values[i],
00138                         that->renderfarm_rate.values[i]);
00139         }
00140         use_renderfarm = that->use_renderfarm;
00141         renderfarm_port = that->renderfarm_port;
00142         render_preroll = that->render_preroll;
00143         brender_preroll = that->brender_preroll;
00144         renderfarm_job_count = that->renderfarm_job_count;
00145         renderfarm_vfs = that->renderfarm_vfs;
00146         strcpy(renderfarm_mountpoint, that->renderfarm_mountpoint);
00147         renderfarm_consolidate = that->renderfarm_consolidate;
00148         use_brender = that->use_brender;
00149         brender_fragment = that->brender_fragment;
00150         *brender_asset = *that->brender_asset;
00151 
00152 // Check boundaries
00153 
00154         FileSystem fs;
00155         if(strlen(index_directory))
00156         {
00157                 fs.complete_path(index_directory);
00158                 fs.add_end_slash(index_directory);
00159         }
00160         
00161         if(strlen(global_plugin_dir))
00162         {
00163                 fs.complete_path(global_plugin_dir);
00164                 fs.add_end_slash(global_plugin_dir);
00165         }
00166 
00167         boundaries();
00168 }
00169 
00170 void Preferences::boundaries()
00171 {
00172         renderfarm_job_count = MAX(renderfarm_job_count, 1);
00173         CLAMP(cache_size, MIN_CACHE_SIZE, MAX_CACHE_SIZE);
00174 }
00175 
00176 Preferences& Preferences::operator=(Preferences &that)
00177 {
00178 printf("Preferences::operator=\n");
00179         copy_from(&that);
00180         return *this;
00181 }
00182 
00183 int Preferences::load_defaults(Defaults *defaults)
00184 {
00185         char string[BCTEXTLEN];
00186 
00187         use_tipwindow = defaults->get("USE_TIPWINDOW", use_tipwindow);
00188         defaults->get("INDEX_DIRECTORY", index_directory);
00189         index_size = defaults->get("INDEX_SIZE", index_size);
00190         index_count = defaults->get("INDEX_COUNT", index_count);
00191         use_thumbnails = defaults->get("USE_THUMBNAILS", use_thumbnails);
00192 
00193         sprintf(global_plugin_dir, PLUGIN_DIR);
00194 //      defaults->get("GLOBAL_PLUGIN_DIR", global_plugin_dir);
00195 //      if(getenv("GLOBAL_PLUGIN_DIR"))
00196 //      {
00197 //              strcpy(global_plugin_dir, getenv("GLOBAL_PLUGIN_DIR"));
00198 //      }
00199 
00200         strcpy(theme, DEFAULT_THEME);
00201         defaults->get("THEME", theme);
00202 
00203 
00204         brender_asset->load_defaults(defaults, 
00205                 "BRENDER_", 
00206                 1,
00207                 1,
00208                 1,
00209                 0,
00210                 0);
00211 
00212 
00213 
00214         force_uniprocessor = defaults->get("FORCE_UNIPROCESSOR", 0);
00215         processors = calculate_processors();
00216         use_brender = defaults->get("USE_BRENDER", use_brender);
00217         brender_fragment = defaults->get("BRENDER_FRAGMENT", brender_fragment);
00218         cache_size = defaults->get("CACHE_SIZE", cache_size);
00219         local_rate = defaults->get("LOCAL_RATE", local_rate);
00220         use_renderfarm = defaults->get("USE_RENDERFARM", use_renderfarm);
00221         renderfarm_port = defaults->get("RENDERFARM_PORT", renderfarm_port);
00222         render_preroll = defaults->get("RENDERFARM_PREROLL", render_preroll);
00223         brender_preroll = defaults->get("BRENDER_PREROLL", brender_preroll);
00224         renderfarm_job_count = defaults->get("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
00225         renderfarm_consolidate = defaults->get("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
00226 //      renderfarm_vfs = defaults->get("RENDERFARM_VFS", renderfarm_vfs);
00227         defaults->get("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
00228         int renderfarm_total = defaults->get("RENDERFARM_TOTAL", 0);
00229 
00230         for(int i = 0; i < renderfarm_total; i++)
00231         {
00232                 sprintf(string, "RENDERFARM_NODE%d", i);
00233                 char result[BCTEXTLEN];
00234                 int result_port = 0;
00235                 int result_enabled = 0;
00236                 float result_rate = 0.0;
00237 
00238                 result[0] = 0;
00239                 defaults->get(string, result);
00240 
00241                 sprintf(string, "RENDERFARM_PORT%d", i);
00242                 result_port = defaults->get(string, renderfarm_port);
00243 
00244                 sprintf(string, "RENDERFARM_ENABLED%d", i);
00245                 result_enabled = defaults->get(string, result_enabled);
00246 
00247                 sprintf(string, "RENDERFARM_RATE%d", i);
00248                 result_rate = defaults->get(string, result_rate);
00249 
00250                 if(result[0] != 0)
00251                 {
00252                         add_node(result, result_port, result_enabled, result_rate);
00253                 }
00254         }
00255 
00256         boundaries();
00257 
00258         return 0;
00259 }
00260 
00261 int Preferences::save_defaults(Defaults *defaults)
00262 {
00263         char string[BCTEXTLEN];
00264 
00265 
00266         defaults->update("USE_TIPWINDOW", use_tipwindow);
00267 
00268         defaults->update("CACHE_SIZE", cache_size);
00269         defaults->update("INDEX_DIRECTORY", index_directory);
00270         defaults->update("INDEX_SIZE", index_size);
00271         defaults->update("INDEX_COUNT", index_count);
00272         defaults->update("USE_THUMBNAILS", use_thumbnails);
00273 //      defaults->update("GLOBAL_PLUGIN_DIR", global_plugin_dir);
00274         defaults->update("THEME", theme);
00275 
00276 
00277 
00278         defaults->update("FORCE_UNIPROCESSOR", force_uniprocessor);
00279         brender_asset->save_defaults(defaults, 
00280                 "BRENDER_",
00281                 1,
00282                 1,
00283                 1,
00284                 0,
00285                 0);
00286         defaults->update("USE_BRENDER", use_brender);
00287         defaults->update("BRENDER_FRAGMENT", brender_fragment);
00288         defaults->update("USE_RENDERFARM", use_renderfarm);
00289         defaults->update("LOCAL_RATE", local_rate);
00290         defaults->update("RENDERFARM_PORT", renderfarm_port);
00291         defaults->update("RENDERFARM_PREROLL", render_preroll);
00292         defaults->update("BRENDER_PREROLL", brender_preroll);
00293 //      defaults->update("RENDERFARM_VFS", renderfarm_vfs);
00294         defaults->update("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
00295         defaults->update("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
00296         defaults->update("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
00297         defaults->update("RENDERFARM_TOTAL", (int64_t)renderfarm_nodes.total);
00298         for(int i = 0; i < renderfarm_nodes.total; i++)
00299         {
00300                 sprintf(string, "RENDERFARM_NODE%d", i);
00301                 defaults->update(string, renderfarm_nodes.values[i]);
00302                 sprintf(string, "RENDERFARM_PORT%d", i);
00303                 defaults->update(string, renderfarm_ports.values[i]);
00304                 sprintf(string, "RENDERFARM_ENABLED%d", i);
00305                 defaults->update(string, renderfarm_enabled.values[i]);
00306                 sprintf(string, "RENDERFARM_RATE%d", i);
00307                 defaults->update(string, renderfarm_rate.values[i]);
00308         }
00309         return 0;
00310 }
00311 
00312 
00313 void Preferences::add_node(char *text, int port, int enabled, float rate)
00314 {
00315         if(text[0] == 0) return;
00316 
00317         preferences_lock->lock("Preferences::add_node");
00318         char *new_item = new char[strlen(text) + 1];
00319         strcpy(new_item, text);
00320         renderfarm_nodes.append(new_item);
00321         renderfarm_ports.append(port);
00322         renderfarm_enabled.append(enabled);
00323         renderfarm_rate.append(rate);
00324         preferences_lock->unlock();
00325 }
00326 
00327 void Preferences::delete_node(int number)
00328 {
00329         preferences_lock->lock("Preferences::delete_node");
00330         if(number < renderfarm_nodes.total)
00331         {
00332                 delete [] renderfarm_nodes.values[number];
00333                 renderfarm_nodes.remove_number(number);
00334                 renderfarm_ports.remove_number(number);
00335                 renderfarm_enabled.remove_number(number);
00336                 renderfarm_rate.remove_number(number);
00337         }
00338         preferences_lock->unlock();
00339 }
00340 
00341 void Preferences::delete_nodes()
00342 {
00343         preferences_lock->lock("Preferences::delete_nodes");
00344         for(int i = 0; i < renderfarm_nodes.total; i++)
00345                 delete [] renderfarm_nodes.values[i];
00346         renderfarm_nodes.remove_all();
00347         renderfarm_ports.remove_all();
00348         renderfarm_enabled.remove_all();
00349         renderfarm_rate.remove_all();
00350         preferences_lock->unlock();
00351 }
00352 
00353 void Preferences::reset_rates()
00354 {
00355         for(int i = 0; i < renderfarm_nodes.total; i++)
00356         {
00357                 renderfarm_rate.values[i] = 0.0;
00358         }
00359         local_rate = 0.0;
00360 }
00361 
00362 void Preferences::set_rate(float rate, int node)
00363 {
00364 //printf("Preferences::set_rate %f %d\n", rate, node);
00365         if(node < 0)
00366         {
00367                 local_rate = rate;
00368         }
00369         else
00370         {
00371                 int total = 0;
00372                 for(int i = 0; i < renderfarm_nodes.total; i++)
00373                 {
00374                         if(renderfarm_enabled.values[i]) total++;
00375                         if(total == node + 1)
00376                         {
00377                                 renderfarm_rate.values[i] = rate;
00378                                 return;
00379                         }
00380                 }
00381         }
00382 }
00383 
00384 float Preferences::get_avg_rate(int use_master_node)
00385 {
00386         preferences_lock->lock("Preferences::get_avg_rate");
00387         float total = 0.0;
00388         if(renderfarm_rate.total)
00389         {
00390                 int enabled = 0;
00391                 if(use_master_node)
00392                 {
00393                         if(EQUIV(local_rate, 0.0))
00394                         {
00395                                 preferences_lock->unlock();
00396                                 return 0.0;
00397                         }
00398                         else
00399                         {
00400                                 enabled++;
00401                                 total += local_rate;
00402                         }
00403                 }
00404 
00405                 for(int i = 0; i < renderfarm_rate.total; i++)
00406                 {
00407                         if(renderfarm_enabled.values[i])
00408                         {
00409                                 enabled++;
00410                                 total += renderfarm_rate.values[i];
00411                                 if(EQUIV(renderfarm_rate.values[i], 0.0)) 
00412                                 {
00413                                         preferences_lock->unlock();
00414                                         return 0.0;
00415                                 }
00416                         }
00417                 }
00418 
00419                 if(enabled)
00420                         total /= enabled;
00421                 else
00422                         total = 0.0;
00423         }
00424         preferences_lock->unlock();
00425 
00426         return total;
00427 }
00428 
00429 void Preferences::sort_nodes()
00430 {
00431         int done = 0;
00432 
00433         while(!done)
00434         {
00435                 done = 1;
00436                 for(int i = 0; i < renderfarm_nodes.total - 1; i++)
00437                 {
00438                         if(strcmp(renderfarm_nodes.values[i], renderfarm_nodes.values[i + 1]) > 0)
00439                         {
00440                                 char *temp = renderfarm_nodes.values[i];
00441                                 int temp_port = renderfarm_ports.values[i];
00442 
00443                                 renderfarm_nodes.values[i] = renderfarm_nodes.values[i + 1];
00444                                 renderfarm_nodes.values[i + 1] = temp;
00445 
00446                                 renderfarm_ports.values[i] = renderfarm_ports.values[i + 1];
00447                                 renderfarm_ports.values[i + 1] = temp_port;
00448 
00449                                 renderfarm_enabled.values[i] = renderfarm_enabled.values[i + 1];
00450                                 renderfarm_enabled.values[i + 1] = temp_port;
00451 
00452                                 renderfarm_rate.values[i] = renderfarm_rate.values[i + 1];
00453                                 renderfarm_rate.values[i + 1] = temp_port;
00454                                 done = 0;
00455                         }
00456                 }
00457         }
00458 }
00459 
00460 void Preferences::edit_node(int number, 
00461         char *new_text, 
00462         int new_port, 
00463         int new_enabled)
00464 {
00465         char *new_item = new char[strlen(new_text) + 1];
00466         strcpy(new_item, new_text);
00467 
00468         delete [] renderfarm_nodes.values[number];
00469         renderfarm_nodes.values[number] = new_item;
00470         renderfarm_ports.values[number] = new_port;
00471         renderfarm_enabled.values[number] = new_enabled;
00472 }
00473 
00474 int Preferences::get_enabled_nodes()
00475 {
00476         int result = 0;
00477         for(int i = 0; i < renderfarm_enabled.total; i++)
00478                 if(renderfarm_enabled.values[i]) result++;
00479         return result;
00480 }
00481 
00482 char* Preferences::get_node_hostname(int number)
00483 {
00484         int total = 0;
00485         for(int i = 0; i < renderfarm_nodes.total; i++)
00486         {
00487                 if(renderfarm_enabled.values[i])
00488                 {
00489                         if(total == number)
00490                                 return renderfarm_nodes.values[i];
00491                         else
00492                                 total++;
00493                 }
00494         }
00495         return "";
00496 }
00497 
00498 int Preferences::get_node_port(int number)
00499 {
00500         int total = 0;
00501         for(int i = 0; i < renderfarm_ports.total; i++)
00502         {
00503                 if(renderfarm_enabled.values[i])
00504                 {
00505                         if(total == number)
00506                                 return renderfarm_ports.values[i];
00507                         else
00508                                 total++;
00509                 }
00510         }
00511         return -1;
00512 }
00513 
00514 
00515 int Preferences::calculate_processors()
00516 {
00517 /* Get processor count */
00518         int result = 1;
00519         FILE *proc;
00520 
00521         if(force_uniprocessor) return 1;
00522 
00523         if(proc = fopen("/proc/cpuinfo", "r"))
00524         {
00525                 char string[BCTEXTLEN];
00526                 while(!feof(proc))
00527                 {
00528                         fgets(string, BCTEXTLEN, proc);
00529                         if(!strncasecmp(string, "processor", 9))
00530                         {
00531                                 char *ptr = strchr(string, ':');
00532                                 if(ptr)
00533                                 {
00534                                         ptr++;
00535                                         result = atol(ptr) + 1;
00536                                 }
00537                         }
00538                         else
00539                         if(!strncasecmp(string, "cpus detected", 13))
00540                         {
00541                                 char *ptr = strchr(string, ':');
00542                                 if(ptr)
00543                                 {
00544                                         ptr++;
00545                                         result = atol(ptr);
00546                                 }
00547                         }
00548                 }
00549                 fclose(proc);
00550         }
00551 
00552         return result;
00553 }
00554 
00555 
00556 
00557 
00558 
00559 
00560 
00561 
00562 
00563 
00564 
00565 
00566 
00567 

Generated on Sun Jan 8 13:38:59 2006 for Cinelerra-svn by  doxygen 1.4.4