r123: Merged HEAD and TEST. New stuff shall be committed to HEAD from now on.
[cinelerra_cv/mob.git] / cinelerra / preferences.C
blobca0fb785f03f482c0ef20f7ec3f1fca5f3aa9349
1 #include "assets.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         renderfarm_port = DEAMON_PORT;
45         render_preroll = 0.5;
46         brender_preroll = 0;
47         renderfarm_mountpoint[0] = 0;
48         renderfarm_vfs = 1;
49         renderfarm_job_count = 1;
50         brender_asset = new Asset;
51         brender_asset->audio_data = 0;
52         brender_asset->video_data = 1;
53         use_brender = 0;
54         brender_fragment = 1;
55         local_rate = 0.0;
58 Preferences::~Preferences()
60         delete brender_asset;
61         delete preferences_lock;
64 void Preferences::copy_rates_from(Preferences *preferences)
66         preferences_lock->lock();
67 // Need to match node titles in case the order changed and in case
68 // one of the nodes in the source is the master node.
69         local_rate = preferences->local_rate;
71         for(int j = 0; 
72                 j < preferences->renderfarm_nodes.total; 
73                 j++)
74         {
75                 double new_rate = preferences->renderfarm_rate.values[j];
76 // Put in the master node
77                 if(preferences->renderfarm_nodes.values[j][0] == '/')
78                 {
79                         if(!EQUIV(new_rate, 0.0))
80                                 local_rate = new_rate;
81                 }
82                 else
83 // Search for local node and copy it to that node
84                 if(!EQUIV(new_rate, 0.0))
85                 {
86                         for(int i = 0; i < renderfarm_nodes.total; i++)
87                         {
88                                 if(!strcmp(preferences->renderfarm_nodes.values[j], renderfarm_nodes.values[i]) &&
89                                         preferences->renderfarm_ports.values[j] == renderfarm_ports.values[i])
90                                 {
91                                         renderfarm_rate.values[i] = new_rate;
92                                         break;
93                                 }
94                         }
95                 }
96         }
98 //printf("Preferences::copy_rates_from 1 %f %f\n", local_rate, preferences->local_rate);
99         preferences_lock->unlock();
102 Preferences& Preferences::operator=(Preferences &that)
104 // ================================= Performance ================================
105         strcpy(index_directory, that.index_directory);
106         index_size = that.index_size;
107         index_count = that.index_count;
108         use_thumbnails = that.use_thumbnails;
109         strcpy(global_plugin_dir, that.global_plugin_dir);
110         strcpy(local_plugin_dir, that.local_plugin_dir);
111         strcpy(theme, that.theme);
113         cache_size = that.cache_size;
114         renderfarm_nodes.remove_all_objects();
115         renderfarm_ports.remove_all();
116         renderfarm_enabled.remove_all();
117         renderfarm_rate.remove_all();
118         local_rate = that.local_rate;
119         for(int i = 0; i < that.renderfarm_nodes.total; i++)
120         {
121                 add_node(that.renderfarm_nodes.values[i], 
122                         that.renderfarm_ports.values[i],
123                         that.renderfarm_enabled.values[i],
124                         that.renderfarm_rate.values[i]);
125         }
126         use_renderfarm = that.use_renderfarm;
127         renderfarm_port = that.renderfarm_port;
128         render_preroll = that.render_preroll;
129         brender_preroll = that.brender_preroll;
130         renderfarm_job_count = that.renderfarm_job_count;
131         renderfarm_vfs = that.renderfarm_vfs;
132         strcpy(renderfarm_mountpoint, that.renderfarm_mountpoint);
133         renderfarm_consolidate = that.renderfarm_consolidate;
134         use_brender = that.use_brender;
135         brender_fragment = that.brender_fragment;
136         *brender_asset = *that.brender_asset;
138 // Check boundaries
140         FileSystem fs;
141         if(strlen(index_directory))
142         {
143                 fs.complete_path(index_directory);
144                 fs.add_end_slash(index_directory);
145         }
146         
147         if(strlen(global_plugin_dir))
148         {
149                 fs.complete_path(global_plugin_dir);
150                 fs.add_end_slash(global_plugin_dir);
151         }
152         
153         if(strlen(local_plugin_dir))
154         {
155                 fs.complete_path(local_plugin_dir);
156                 fs.add_end_slash(local_plugin_dir);
157         }
159         renderfarm_job_count = MAX(renderfarm_job_count, 1);
160         CLAMP(cache_size, 1, 100);
162         return *this;
165 int Preferences::load_defaults(Defaults *defaults)
167         char string[BCTEXTLEN];
169         defaults->get("INDEX_DIRECTORY", index_directory);
170         index_size = defaults->get("INDEX_SIZE", index_size);
171         index_count = defaults->get("INDEX_COUNT", index_count);
172         use_thumbnails = defaults->get("USE_THUMBNAILS", use_thumbnails);
174         sprintf(global_plugin_dir, PLUGINDIR);
175         sprintf(local_plugin_dir, "");
176         strcpy(theme, DEFAULT_THEME);
177         defaults->get("GLOBAL_PLUGIN_DIR", global_plugin_dir);
178         defaults->get("LOCAL_PLUGIN_DIR", local_plugin_dir);
179         defaults->get("THEME", theme);
181         sprintf(brender_asset->path, "/tmp/brender");
182         brender_asset->format = FILE_JPEG_LIST;
183         brender_asset->jpeg_quality = 80;
184         brender_asset->load_defaults(defaults, "BRENDER_", 1);
185         use_brender = defaults->get("USE_BRENDER", use_brender);
186         brender_fragment = defaults->get("BRENDER_FRAGMENT", brender_fragment);
187         cache_size = defaults->get("CACHE_SIZE", cache_size);
188         local_rate = defaults->get("LOCAL_RATE", local_rate);
189         use_renderfarm = defaults->get("USE_RENDERFARM", use_renderfarm);
190         renderfarm_port = defaults->get("RENDERFARM_PORT", renderfarm_port);
191         render_preroll = defaults->get("RENDERFARM_PREROLL", render_preroll);
192         brender_preroll = defaults->get("BRENDER_PREROLL", brender_preroll);
193         renderfarm_job_count = defaults->get("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
194         renderfarm_consolidate = defaults->get("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
195         renderfarm_vfs = defaults->get("RENDERFARM_VFS", renderfarm_vfs);
196         defaults->get("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
197         int renderfarm_total = defaults->get("RENDERFARM_TOTAL", 0);
199         for(int i = 0; i < renderfarm_total; i++)
200         {
201                 sprintf(string, "RENDERFARM_NODE%d", i);
202                 char result[BCTEXTLEN];
203                 int result_port = 0;
204                 int result_enabled = 0;
205                 float result_rate = 0.0;
207                 result[0] = 0;
208                 defaults->get(string, result);
210                 sprintf(string, "RENDERFARM_PORT%d", i);
211                 result_port = defaults->get(string, renderfarm_port);
213                 sprintf(string, "RENDERFARM_ENABLED%d", i);
214                 result_enabled = defaults->get(string, result_enabled);
216                 sprintf(string, "RENDERFARM_RATE%d", i);
217                 result_rate = defaults->get(string, result_rate);
219                 if(result[0] != 0)
220                 {
221                         add_node(result, result_port, result_enabled, result_rate);
222                 }
223         }
225         return 0;
228 int Preferences::save_defaults(Defaults *defaults)
230         char string[BCTEXTLEN];
231         defaults->update("CACHE_SIZE", cache_size);
232         defaults->update("INDEX_DIRECTORY", index_directory);
233         defaults->update("INDEX_SIZE", index_size);
234         defaults->update("INDEX_COUNT", index_count);
235         defaults->update("USE_THUMBNAILS", use_thumbnails);
236         defaults->update("GLOBAL_PLUGIN_DIR", global_plugin_dir);
237         defaults->update("LOCAL_PLUGIN_DIR", local_plugin_dir);
238         defaults->update("THEME", theme);
242         brender_asset->save_defaults(defaults, "BRENDER_");
243         defaults->update("USE_BRENDER", use_brender);
244         defaults->update("BRENDER_FRAGMENT", brender_fragment);
245         defaults->update("USE_RENDERFARM", use_renderfarm);
246         defaults->update("LOCAL_RATE", local_rate);
247         defaults->update("RENDERFARM_PORT", renderfarm_port);
248         defaults->update("RENDERFARM_PREROLL", render_preroll);
249         defaults->update("BRENDER_PREROLL", brender_preroll);
250         defaults->update("RENDERFARM_VFS", renderfarm_vfs);
251         defaults->update("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
252         defaults->update("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
253         defaults->update("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
254         defaults->update("RENDERFARM_TOTAL", (int64_t)renderfarm_nodes.total);
255         for(int i = 0; i < renderfarm_nodes.total; i++)
256         {
257                 sprintf(string, "RENDERFARM_NODE%d", i);
258                 defaults->update(string, renderfarm_nodes.values[i]);
259                 sprintf(string, "RENDERFARM_PORT%d", i);
260                 defaults->update(string, renderfarm_ports.values[i]);
261                 sprintf(string, "RENDERFARM_ENABLED%d", i);
262                 defaults->update(string, renderfarm_enabled.values[i]);
263                 sprintf(string, "RENDERFARM_RATE%d", i);
264                 defaults->update(string, renderfarm_rate.values[i]);
265         }
266         return 0;
270 void Preferences::add_node(char *text, int port, int enabled, float rate)
272         if(text[0] == 0) return;
274         preferences_lock->lock();
275         char *new_item = new char[strlen(text) + 1];
276         strcpy(new_item, text);
277         renderfarm_nodes.append(new_item);
278         renderfarm_ports.append(port);
279         renderfarm_enabled.append(enabled);
280         renderfarm_rate.append(rate);
281         preferences_lock->unlock();
284 void Preferences::delete_node(int number)
286         preferences_lock->lock();
287         if(number < renderfarm_nodes.total)
288         {
289                 delete [] renderfarm_nodes.values[number];
290                 renderfarm_nodes.remove_number(number);
291                 renderfarm_ports.remove_number(number);
292                 renderfarm_enabled.remove_number(number);
293                 renderfarm_rate.remove_number(number);
294         }
295         preferences_lock->unlock();
298 void Preferences::delete_nodes()
300         preferences_lock->lock();
301         for(int i = 0; i < renderfarm_nodes.total; i++)
302                 delete [] renderfarm_nodes.values[i];
303         renderfarm_nodes.remove_all();
304         renderfarm_ports.remove_all();
305         renderfarm_enabled.remove_all();
306         renderfarm_rate.remove_all();
307         preferences_lock->unlock();
310 void Preferences::reset_rates()
312         for(int i = 0; i < renderfarm_nodes.total; i++)
313         {
314                 renderfarm_rate.values[i] = 0.0;
315         }
316         local_rate = 0.0;
319 void Preferences::set_rate(float rate, int node)
321 //printf("Preferences::set_rate %f %d\n", rate, node);
322         if(node < 0)
323         {
324                 local_rate = rate;
325         }
326         else
327         {
328                 int total = 0;
329                 for(int i = 0; i < renderfarm_nodes.total; i++)
330                 {
331                         if(renderfarm_enabled.values[i]) total++;
332                         if(total == node + 1)
333                         {
334                                 renderfarm_rate.values[i] = rate;
335                                 return;
336                         }
337                 }
338         }
341 float Preferences::get_avg_rate(int use_master_node)
343         preferences_lock->lock();
344         float total = 0.0;
345         if(renderfarm_rate.total)
346         {
347                 int enabled = 0;
348                 if(use_master_node)
349                 {
350                         if(EQUIV(local_rate, 0.0))
351                         {
352                                 preferences_lock->unlock();
353                                 return 0.0;
354                         }
355                         else
356                         {
357                                 enabled++;
358                                 total += local_rate;
359                         }
360                 }
362                 for(int i = 0; i < renderfarm_rate.total; i++)
363                 {
364                         if(renderfarm_enabled.values[i])
365                         {
366                                 enabled++;
367                                 total += renderfarm_rate.values[i];
368                                 if(EQUIV(renderfarm_rate.values[i], 0.0)) 
369                                 {
370                                         preferences_lock->unlock();
371                                         return 0.0;
372                                 }
373                         }
374                 }
376                 if(enabled)
377                         total /= enabled;
378                 else
379                         total = 0.0;
380         }
381         preferences_lock->unlock();
383         return total;
386 void Preferences::sort_nodes()
388         int done = 0;
390         while(!done)
391         {
392                 done = 1;
393                 for(int i = 0; i < renderfarm_nodes.total - 1; i++)
394                 {
395                         if(strcmp(renderfarm_nodes.values[i], renderfarm_nodes.values[i + 1]) > 0)
396                         {
397                                 char *temp = renderfarm_nodes.values[i];
398                                 int temp_port = renderfarm_ports.values[i];
400                                 renderfarm_nodes.values[i] = renderfarm_nodes.values[i + 1];
401                                 renderfarm_nodes.values[i + 1] = temp;
403                                 renderfarm_ports.values[i] = renderfarm_ports.values[i + 1];
404                                 renderfarm_ports.values[i + 1] = temp_port;
406                                 renderfarm_enabled.values[i] = renderfarm_enabled.values[i + 1];
407                                 renderfarm_enabled.values[i + 1] = temp_port;
409                                 renderfarm_rate.values[i] = renderfarm_rate.values[i + 1];
410                                 renderfarm_rate.values[i + 1] = temp_port;
411                                 done = 0;
412                         }
413                 }
414         }
417 void Preferences::edit_node(int number, 
418         char *new_text, 
419         int new_port, 
420         int new_enabled)
422         char *new_item = new char[strlen(new_text) + 1];
423         strcpy(new_item, new_text);
425         delete [] renderfarm_nodes.values[number];
426         renderfarm_nodes.values[number] = new_item;
427         renderfarm_ports.values[number] = new_port;
428         renderfarm_enabled.values[number] = new_enabled;
431 int Preferences::get_enabled_nodes()
433         int result = 0;
434         for(int i = 0; i < renderfarm_enabled.total; i++)
435                 if(renderfarm_enabled.values[i]) result++;
436         return result;
439 char* Preferences::get_node_hostname(int number)
441         int total = 0;
442         for(int i = 0; i < renderfarm_nodes.total; i++)
443         {
444                 if(renderfarm_enabled.values[i])
445                 {
446                         if(total == number)
447                                 return renderfarm_nodes.values[i];
448                         else
449                                 total++;
450                 }
451         }
452         return "";
455 int Preferences::get_node_port(int number)
457         int total = 0;
458         for(int i = 0; i < renderfarm_ports.total; i++)
459         {
460                 if(renderfarm_enabled.values[i])
461                 {
462                         if(total == number)
463                                 return renderfarm_ports.values[i];
464                         else
465                                 total++;
466                 }
467         }
468         return -1;