r136: This commit was manufactured by cvs2svn to create tag 'hv_1_1_8'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / preferences.C
blob27762008069a07a99a9fcdd015b9f35761470116
1 #include "asset.h"
2 #include "audioconfig.h"
3 #include "audiodevice.inc"
4 #include "bcmeter.inc"
5 #include "clip.h"
6 #include "defaults.h"
7 #include "file.inc"
8 #include "filesystem.h"
9 #include "guicast.h"
10 #include "preferences.h"
11 #include "theme.h"
12 #include "videoconfig.h"
13 #include "videodevice.inc"
14 #include <string.h>
16 //#define CLAMP(x, y, z) (x) = ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x)))
29 Preferences::Preferences()
31 // Set defaults
32         FileSystem fs;
34         preferences_lock = new Mutex;
35         sprintf(index_directory, BCASTDIR);
36         if(strlen(index_directory))
37                 fs.complete_path(index_directory);
38         cache_size = 5;
39         index_size = 3000000;
40         index_count = 100;
41         use_thumbnails = 1;
42         theme[0] = 0;
43         use_renderfarm = 0;
44         force_uniprocessor = 0;
45         renderfarm_port = DEAMON_PORT;
46         render_preroll = 0.5;
47         brender_preroll = 0;
48         renderfarm_mountpoint[0] = 0;
49         renderfarm_vfs = 1;
50         renderfarm_job_count = 1;
51         processors = calculate_processors();
53 // Default brender asset
54         brender_asset = new Asset;
55         brender_asset->audio_data = 0;
56         brender_asset->video_data = 1;
57         sprintf(brender_asset->path, "/tmp/brender");
58         brender_asset->format = FILE_JPEG_LIST;
59         brender_asset->jpeg_quality = 80;
61         use_brender = 0;
62         brender_fragment = 1;
63         local_rate = 0.0;
66 Preferences::~Preferences()
68         delete brender_asset;
69         delete preferences_lock;
72 void Preferences::copy_rates_from(Preferences *preferences)
74         preferences_lock->lock();
75 // Need to match node titles in case the order changed and in case
76 // one of the nodes in the source is the master node.
77         local_rate = preferences->local_rate;
79         for(int j = 0; 
80                 j < preferences->renderfarm_nodes.total; 
81                 j++)
82         {
83                 double new_rate = preferences->renderfarm_rate.values[j];
84 // Put in the master node
85                 if(preferences->renderfarm_nodes.values[j][0] == '/')
86                 {
87                         if(!EQUIV(new_rate, 0.0))
88                                 local_rate = new_rate;
89                 }
90                 else
91 // Search for local node and copy it to that node
92                 if(!EQUIV(new_rate, 0.0))
93                 {
94                         for(int i = 0; i < renderfarm_nodes.total; i++)
95                         {
96                                 if(!strcmp(preferences->renderfarm_nodes.values[j], renderfarm_nodes.values[i]) &&
97                                         preferences->renderfarm_ports.values[j] == renderfarm_ports.values[i])
98                                 {
99                                         renderfarm_rate.values[i] = new_rate;
100                                         break;
101                                 }
102                         }
103                 }
104         }
106 //printf("Preferences::copy_rates_from 1 %f %f\n", local_rate, preferences->local_rate);
107         preferences_lock->unlock();
110 Preferences& Preferences::operator=(Preferences &that)
112 // ================================= Performance ================================
113         strcpy(index_directory, that.index_directory);
114         index_size = that.index_size;
115         index_count = that.index_count;
116         use_thumbnails = that.use_thumbnails;
117         strcpy(global_plugin_dir, that.global_plugin_dir);
118         strcpy(theme, that.theme);
120         cache_size = that.cache_size;
121         force_uniprocessor = that.force_uniprocessor;
122         processors = calculate_processors();
123         renderfarm_nodes.remove_all_objects();
124         renderfarm_ports.remove_all();
125         renderfarm_enabled.remove_all();
126         renderfarm_rate.remove_all();
127         local_rate = that.local_rate;
128         for(int i = 0; i < that.renderfarm_nodes.total; i++)
129         {
130                 add_node(that.renderfarm_nodes.values[i], 
131                         that.renderfarm_ports.values[i],
132                         that.renderfarm_enabled.values[i],
133                         that.renderfarm_rate.values[i]);
134         }
135         use_renderfarm = that.use_renderfarm;
136         renderfarm_port = that.renderfarm_port;
137         render_preroll = that.render_preroll;
138         brender_preroll = that.brender_preroll;
139         renderfarm_job_count = that.renderfarm_job_count;
140         renderfarm_vfs = that.renderfarm_vfs;
141         strcpy(renderfarm_mountpoint, that.renderfarm_mountpoint);
142         renderfarm_consolidate = that.renderfarm_consolidate;
143         use_brender = that.use_brender;
144         brender_fragment = that.brender_fragment;
145         *brender_asset = *that.brender_asset;
147 // Check boundaries
149         FileSystem fs;
150         if(strlen(index_directory))
151         {
152                 fs.complete_path(index_directory);
153                 fs.add_end_slash(index_directory);
154         }
155         
156         if(strlen(global_plugin_dir))
157         {
158                 fs.complete_path(global_plugin_dir);
159                 fs.add_end_slash(global_plugin_dir);
160         }
161         
162         renderfarm_job_count = MAX(renderfarm_job_count, 1);
163         CLAMP(cache_size, 1, 100);
165         return *this;
168 int Preferences::load_defaults(Defaults *defaults)
170         char string[BCTEXTLEN];
172         defaults->get("INDEX_DIRECTORY", index_directory);
173         index_size = defaults->get("INDEX_SIZE", index_size);
174         index_count = defaults->get("INDEX_COUNT", index_count);
175         use_thumbnails = defaults->get("USE_THUMBNAILS", use_thumbnails);
177         sprintf(global_plugin_dir, "/usr/lib/cinelerra");
178         defaults->get("GLOBAL_PLUGIN_DIR", global_plugin_dir);
179         if(getenv("GLOBAL_PLUGIN_DIR"))
180         {
181                 strcpy(global_plugin_dir, getenv("GLOBAL_PLUGIN_DIR"));
182         }
184         strcpy(theme, DEFAULT_THEME);
185         defaults->get("THEME", theme);
188         brender_asset->load_defaults(defaults, 
189                 "BRENDER_", 
190                 1,
191                 1,
192                 1,
193                 0,
194                 0);
198         force_uniprocessor = defaults->get("FORCE_UNIPROCESSOR", 0);
199         processors = calculate_processors();
200         use_brender = defaults->get("USE_BRENDER", use_brender);
201         brender_fragment = defaults->get("BRENDER_FRAGMENT", brender_fragment);
202         cache_size = defaults->get("CACHE_SIZE", cache_size);
203         local_rate = defaults->get("LOCAL_RATE", local_rate);
204         use_renderfarm = defaults->get("USE_RENDERFARM", use_renderfarm);
205         renderfarm_port = defaults->get("RENDERFARM_PORT", renderfarm_port);
206         render_preroll = defaults->get("RENDERFARM_PREROLL", render_preroll);
207         brender_preroll = defaults->get("BRENDER_PREROLL", brender_preroll);
208         renderfarm_job_count = defaults->get("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
209         renderfarm_consolidate = defaults->get("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
210         renderfarm_vfs = defaults->get("RENDERFARM_VFS", renderfarm_vfs);
211         defaults->get("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
212         int renderfarm_total = defaults->get("RENDERFARM_TOTAL", 0);
214         for(int i = 0; i < renderfarm_total; i++)
215         {
216                 sprintf(string, "RENDERFARM_NODE%d", i);
217                 char result[BCTEXTLEN];
218                 int result_port = 0;
219                 int result_enabled = 0;
220                 float result_rate = 0.0;
222                 result[0] = 0;
223                 defaults->get(string, result);
225                 sprintf(string, "RENDERFARM_PORT%d", i);
226                 result_port = defaults->get(string, renderfarm_port);
228                 sprintf(string, "RENDERFARM_ENABLED%d", i);
229                 result_enabled = defaults->get(string, result_enabled);
231                 sprintf(string, "RENDERFARM_RATE%d", i);
232                 result_rate = defaults->get(string, result_rate);
234                 if(result[0] != 0)
235                 {
236                         add_node(result, result_port, result_enabled, result_rate);
237                 }
238         }
240         return 0;
243 int Preferences::save_defaults(Defaults *defaults)
245         char string[BCTEXTLEN];
246         defaults->update("CACHE_SIZE", cache_size);
247         defaults->update("INDEX_DIRECTORY", index_directory);
248         defaults->update("INDEX_SIZE", index_size);
249         defaults->update("INDEX_COUNT", index_count);
250         defaults->update("USE_THUMBNAILS", use_thumbnails);
251         defaults->update("GLOBAL_PLUGIN_DIR", global_plugin_dir);
252         defaults->update("THEME", theme);
256         defaults->update("FORCE_UNIPROCESSOR", force_uniprocessor);
257         brender_asset->save_defaults(defaults, 
258                 "BRENDER_",
259                 1,
260                 1,
261                 1,
262                 0,
263                 0);
264         defaults->update("USE_BRENDER", use_brender);
265         defaults->update("BRENDER_FRAGMENT", brender_fragment);
266         defaults->update("USE_RENDERFARM", use_renderfarm);
267         defaults->update("LOCAL_RATE", local_rate);
268         defaults->update("RENDERFARM_PORT", renderfarm_port);
269         defaults->update("RENDERFARM_PREROLL", render_preroll);
270         defaults->update("BRENDER_PREROLL", brender_preroll);
271         defaults->update("RENDERFARM_VFS", renderfarm_vfs);
272         defaults->update("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
273         defaults->update("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
274         defaults->update("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
275         defaults->update("RENDERFARM_TOTAL", (int64_t)renderfarm_nodes.total);
276         for(int i = 0; i < renderfarm_nodes.total; i++)
277         {
278                 sprintf(string, "RENDERFARM_NODE%d", i);
279                 defaults->update(string, renderfarm_nodes.values[i]);
280                 sprintf(string, "RENDERFARM_PORT%d", i);
281                 defaults->update(string, renderfarm_ports.values[i]);
282                 sprintf(string, "RENDERFARM_ENABLED%d", i);
283                 defaults->update(string, renderfarm_enabled.values[i]);
284                 sprintf(string, "RENDERFARM_RATE%d", i);
285                 defaults->update(string, renderfarm_rate.values[i]);
286         }
287         return 0;
291 void Preferences::add_node(char *text, int port, int enabled, float rate)
293         if(text[0] == 0) return;
295         preferences_lock->lock();
296         char *new_item = new char[strlen(text) + 1];
297         strcpy(new_item, text);
298         renderfarm_nodes.append(new_item);
299         renderfarm_ports.append(port);
300         renderfarm_enabled.append(enabled);
301         renderfarm_rate.append(rate);
302         preferences_lock->unlock();
305 void Preferences::delete_node(int number)
307         preferences_lock->lock();
308         if(number < renderfarm_nodes.total)
309         {
310                 delete [] renderfarm_nodes.values[number];
311                 renderfarm_nodes.remove_number(number);
312                 renderfarm_ports.remove_number(number);
313                 renderfarm_enabled.remove_number(number);
314                 renderfarm_rate.remove_number(number);
315         }
316         preferences_lock->unlock();
319 void Preferences::delete_nodes()
321         preferences_lock->lock();
322         for(int i = 0; i < renderfarm_nodes.total; i++)
323                 delete [] renderfarm_nodes.values[i];
324         renderfarm_nodes.remove_all();
325         renderfarm_ports.remove_all();
326         renderfarm_enabled.remove_all();
327         renderfarm_rate.remove_all();
328         preferences_lock->unlock();
331 void Preferences::reset_rates()
333         for(int i = 0; i < renderfarm_nodes.total; i++)
334         {
335                 renderfarm_rate.values[i] = 0.0;
336         }
337         local_rate = 0.0;
340 void Preferences::set_rate(float rate, int node)
342 //printf("Preferences::set_rate %f %d\n", rate, node);
343         if(node < 0)
344         {
345                 local_rate = rate;
346         }
347         else
348         {
349                 int total = 0;
350                 for(int i = 0; i < renderfarm_nodes.total; i++)
351                 {
352                         if(renderfarm_enabled.values[i]) total++;
353                         if(total == node + 1)
354                         {
355                                 renderfarm_rate.values[i] = rate;
356                                 return;
357                         }
358                 }
359         }
362 float Preferences::get_avg_rate(int use_master_node)
364         preferences_lock->lock();
365         float total = 0.0;
366         if(renderfarm_rate.total)
367         {
368                 int enabled = 0;
369                 if(use_master_node)
370                 {
371                         if(EQUIV(local_rate, 0.0))
372                         {
373                                 preferences_lock->unlock();
374                                 return 0.0;
375                         }
376                         else
377                         {
378                                 enabled++;
379                                 total += local_rate;
380                         }
381                 }
383                 for(int i = 0; i < renderfarm_rate.total; i++)
384                 {
385                         if(renderfarm_enabled.values[i])
386                         {
387                                 enabled++;
388                                 total += renderfarm_rate.values[i];
389                                 if(EQUIV(renderfarm_rate.values[i], 0.0)) 
390                                 {
391                                         preferences_lock->unlock();
392                                         return 0.0;
393                                 }
394                         }
395                 }
397                 if(enabled)
398                         total /= enabled;
399                 else
400                         total = 0.0;
401         }
402         preferences_lock->unlock();
404         return total;
407 void Preferences::sort_nodes()
409         int done = 0;
411         while(!done)
412         {
413                 done = 1;
414                 for(int i = 0; i < renderfarm_nodes.total - 1; i++)
415                 {
416                         if(strcmp(renderfarm_nodes.values[i], renderfarm_nodes.values[i + 1]) > 0)
417                         {
418                                 char *temp = renderfarm_nodes.values[i];
419                                 int temp_port = renderfarm_ports.values[i];
421                                 renderfarm_nodes.values[i] = renderfarm_nodes.values[i + 1];
422                                 renderfarm_nodes.values[i + 1] = temp;
424                                 renderfarm_ports.values[i] = renderfarm_ports.values[i + 1];
425                                 renderfarm_ports.values[i + 1] = temp_port;
427                                 renderfarm_enabled.values[i] = renderfarm_enabled.values[i + 1];
428                                 renderfarm_enabled.values[i + 1] = temp_port;
430                                 renderfarm_rate.values[i] = renderfarm_rate.values[i + 1];
431                                 renderfarm_rate.values[i + 1] = temp_port;
432                                 done = 0;
433                         }
434                 }
435         }
438 void Preferences::edit_node(int number, 
439         char *new_text, 
440         int new_port, 
441         int new_enabled)
443         char *new_item = new char[strlen(new_text) + 1];
444         strcpy(new_item, new_text);
446         delete [] renderfarm_nodes.values[number];
447         renderfarm_nodes.values[number] = new_item;
448         renderfarm_ports.values[number] = new_port;
449         renderfarm_enabled.values[number] = new_enabled;
452 int Preferences::get_enabled_nodes()
454         int result = 0;
455         for(int i = 0; i < renderfarm_enabled.total; i++)
456                 if(renderfarm_enabled.values[i]) result++;
457         return result;
460 char* Preferences::get_node_hostname(int number)
462         int total = 0;
463         for(int i = 0; i < renderfarm_nodes.total; i++)
464         {
465                 if(renderfarm_enabled.values[i])
466                 {
467                         if(total == number)
468                                 return renderfarm_nodes.values[i];
469                         else
470                                 total++;
471                 }
472         }
473         return "";
476 int Preferences::get_node_port(int number)
478         int total = 0;
479         for(int i = 0; i < renderfarm_ports.total; i++)
480         {
481                 if(renderfarm_enabled.values[i])
482                 {
483                         if(total == number)
484                                 return renderfarm_ports.values[i];
485                         else
486                                 total++;
487                 }
488         }
489         return -1;
493 int Preferences::calculate_processors()
495 /* Get processor count */
496         int result = 1;
497         FILE *proc;
499         if(force_uniprocessor) return 1;
501         if(proc = fopen("/proc/cpuinfo", "r"))
502         {
503                 char string[1024];
504                 while(!feof(proc))
505                 {
506                         fgets(string, 1024, proc);
507                         if(!strncasecmp(string, "processor", 9))
508                         {
509                                 char *ptr = strchr(string, ':');
510                                 if(ptr)
511                                 {
512                                         ptr++;
513                                         result = atol(ptr) + 1;
514                                 }
515                         }
516                         else
517                         if(!strncasecmp(string, "cpus detected", 13))
518                         {
519                                 char *ptr = strchr(string, ':');
520                                 if(ptr)
521                                 {
522                                         ptr++;
523                                         result = atol(ptr);
524                                 }
525                         }
526                 }
527                 fclose(proc);
528         }
530         return result;