add missing GPL information in guicast program files
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / preferences.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "asset.h"
23 #include "audioconfig.h"
24 #include "audiodevice.inc"
25 #include "bcmeter.inc"
26 #include "bcsignals.h"
27 #include "cache.inc"
28 #include "clip.h"
29 #include "bchash.h"
30 #include "file.h"
31 #include "filesystem.h"
32 #include "guicast.h"
33 #include "indexfile.h"
34 #include "maxchannels.h"
35 #include "mutex.h"
36 #include "preferences.h"
37 #include "probeprefs.h"
38 #include "shbtnprefs.h"
39 #include "theme.h"
40 #include "videoconfig.h"
41 #include "videodevice.inc"
42
43 #include <ctype.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48
49
50 Preferences::Preferences()
51 {
52 // Set defaults
53         FileSystem fs;
54         preferences_lock = new Mutex("Preferences::preferences_lock");
55
56 // initial plugin path from build -DPLUGIN_DIR="..."
57         sprintf(plugin_dir, "%s/", File::get_plugin_path());
58         sprintf(index_directory, "%s/", File::get_config_path());
59         if( strlen(index_directory) )
60                 fs.complete_path(index_directory);
61         cache_size = 0x10000000;
62         cache_transitions = 1;
63         index_size = 0x400000;
64         index_count = 500;
65         use_thumbnails = 1;
66         keyframe_reticle = HAIRLINE_DRAGGING;
67         perpetual_session = 0;
68         strcpy(lv2_path, DEFAULT_LV2_PATH);
69         strcpy(nested_proxy_path, DEFAULT_NESTED_PROXY_PATH);
70         autostart_lv2ui = 0;
71         trap_sigsegv = 1;
72         trap_sigintr = 1;
73         awindow_picon_h = 50;
74         layout_scale = 0; // auto scale
75         vicon_size = 50;
76         vicon_color_mode = VICON_COLOR_MODE_LOW;
77         theme[0] = 0;
78         strcpy(locale, DEFAULT_LOCALE);
79         plugin_icons[0] = 0;
80         strcpy(snapshot_path, DEFAULT_SNAPSHOT_PATH);
81         use_renderfarm = 0;
82         force_uniprocessor = 0;
83         renderfarm_port = DEAMON_PORT;
84         renderfarm_consolidate = 0;
85         render_preroll = 0.5;
86         brender_preroll = 0;
87         renderfarm_mountpoint[0] = 0;
88         renderfarm_vfs = 0;
89         renderfarm_job_count = 20;
90         renderfarm_watchdog_timeout = 60;
91         project_smp = processors = calculate_processors(0);
92         real_processors = calculate_processors(1);
93         ffmpeg_marker_indexes = 1;
94         memset(&use_hw_dev, 0, sizeof(use_hw_dev));
95         warn_indexes = 1;
96         unsafe_gui = 0;
97         ongoing_backups = 0;
98         warn_fileref = 1;
99         bd_warn_root = 1;
100         popupmenu_btnup = 1;
101         grab_input_focus = 1;
102         textbox_focus_policy = CLICK_ACTIVATE | CLICK_DEACTIVATE;
103         auto_rotate = 1;
104         forward_render_displacement = 0;
105         dvd_yuv420p_interlace = 0;
106         highlight_inverse = 0xffffff;
107         yuv_color_space = BC_COLORS_BT601_NTSC;
108         yuv_color_range = BC_COLORS_JPEG;
109         autocolor_assets = 0;
110         ctrl_toggle = 1;
111         rectify_audio = 0;
112
113 // Default brender asset
114         brender_asset = new Asset;
115         brender_asset->audio_data = 0;
116         brender_asset->video_data = 1;
117         sprintf(brender_asset->path, "/tmp/brender");
118         brender_asset->format = FILE_JPEG_LIST;
119         brender_asset->jpeg_quality = 80;
120
121         use_brender = 0;
122         brender_fragment = 1;
123         local_rate = 0.0;
124
125         use_tipwindow = 1;
126         scan_commercials = 0;
127
128         android_remote = 0;
129         android_port = 23432;
130         strcpy(android_pin, "cinelerra");
131         
132         fast_speed = 2.0;
133         slow_speed = 0.5;
134
135         memset(channel_positions, 0, sizeof(channel_positions));
136         int channels = 0;
137         while( channels < MAXCHANNELS ) {
138                 int *positions = channel_positions[channels++];
139                 for( int i=0; i<channels; ++i )
140                         positions[i] = default_audio_channel_position(i, channels);
141         }
142 }
143
144 Preferences::~Preferences()
145 {
146         brender_asset->Garbage::remove_user();
147         shbtn_prefs.remove_all_objects();
148         file_probes.remove_all_objects();
149         renderfarm_nodes.remove_all_objects();
150         delete preferences_lock;
151 }
152
153 void Preferences::copy_rates_from(Preferences *preferences)
154 {
155         preferences_lock->lock("Preferences::copy_rates_from");
156 // Need to match node titles in case the order changed and in case
157 // one of the nodes in the source is the master node.
158         local_rate = preferences->local_rate;
159
160         for( int j=0; j<preferences->renderfarm_nodes.total; ++j ) {
161                 double new_rate = preferences->renderfarm_rate.values[j];
162 // Put in the master node
163                 if( preferences->renderfarm_nodes.values[j][0] == '/' ) {
164                         if( !EQUIV(new_rate, 0.0) )
165                                 local_rate = new_rate;
166                 }
167                 else
168 // Search for local node and copy it to that node
169                 if( !EQUIV(new_rate, 0.0) ) {
170                         for( int i=0; i<renderfarm_nodes.total; ++i ) {
171                                 if( !strcmp(preferences->renderfarm_nodes.values[j], renderfarm_nodes.values[i]) &&
172                                         preferences->renderfarm_ports.values[j] == renderfarm_ports.values[i] ) {
173                                         renderfarm_rate.values[i] = new_rate;
174                                         break;
175                                 }
176                         }
177                 }
178         }
179
180 //printf("Preferences::copy_rates_from 1 %f %f\n", local_rate, preferences->local_rate);
181         preferences_lock->unlock();
182 }
183
184 void Preferences::copy_from(Preferences *that)
185 {
186 // ================================= Performance ================================
187         strcpy(index_directory, that->index_directory);
188         index_size = that->index_size;
189         index_count = that->index_count;
190         use_thumbnails = that->use_thumbnails;
191         keyframe_reticle = that->keyframe_reticle;
192         perpetual_session = that->perpetual_session;
193         awindow_picon_h = that->awindow_picon_h;
194         layout_scale = that->layout_scale;
195         vicon_size = that->vicon_size;
196         vicon_color_mode = that->vicon_color_mode;
197         strcpy(theme, that->theme);
198         strcpy(locale, that->locale);
199         strcpy(plugin_icons, that->plugin_icons);
200         strcpy(snapshot_path, that->snapshot_path);
201
202         use_tipwindow = that->use_tipwindow;
203         scan_commercials = that->scan_commercials;
204         android_remote = that->android_remote;
205         android_port = that->android_port;
206         strcpy(android_pin, that->android_pin);
207         
208         slow_speed = that->slow_speed;
209         fast_speed = that->fast_speed;
210         
211         this->shbtn_prefs.remove_all_objects();
212         for( int i=0; i<that->shbtn_prefs.size(); ++i )
213                 this->shbtn_prefs.append(new ShBtnPref(*that->shbtn_prefs[i]));
214         this->file_probes.remove_all_objects();
215         for( int i=0; i<that->file_probes.size(); ++i )
216                 this->file_probes.append(new ProbePref(*that->file_probes[i]));
217         cache_size = that->cache_size;
218         cache_transitions = that->cache_transitions;
219         project_smp = that->project_smp;
220         force_uniprocessor = that->force_uniprocessor;
221         strcpy(lv2_path, that->lv2_path);
222         autostart_lv2ui = that->autostart_lv2ui;
223         strcpy(nested_proxy_path, that->nested_proxy_path);
224         trap_sigsegv = that->trap_sigsegv;
225         trap_sigintr = that->trap_sigintr;
226         processors = that->processors;
227         real_processors = that->real_processors;
228         ffmpeg_marker_indexes = that->ffmpeg_marker_indexes;
229         strcpy(use_hw_dev, &that->use_hw_dev[0]);
230         warn_indexes = that->warn_indexes;
231         unsafe_gui = that->unsafe_gui;
232         ongoing_backups = that->ongoing_backups;
233         warn_fileref = that->warn_fileref;
234         bd_warn_root = that->bd_warn_root;
235         popupmenu_btnup = that->popupmenu_btnup;
236         grab_input_focus = that->grab_input_focus;
237         textbox_focus_policy = that->textbox_focus_policy;
238         auto_rotate = that->auto_rotate;
239         forward_render_displacement = that->forward_render_displacement;
240         dvd_yuv420p_interlace = that->dvd_yuv420p_interlace;
241         highlight_inverse = that->highlight_inverse;
242         yuv_color_space = that->yuv_color_space;
243         yuv_color_range = that->yuv_color_range;
244         autocolor_assets = that->autocolor_assets;
245         ctrl_toggle = that->ctrl_toggle;
246         rectify_audio = that->rectify_audio;
247         renderfarm_nodes.remove_all_objects();
248         renderfarm_ports.remove_all();
249         renderfarm_enabled.remove_all();
250         renderfarm_rate.remove_all();
251         local_rate = that->local_rate;
252         for( int i=0; i<that->renderfarm_nodes.size(); ++i ) {
253                 add_node(that->renderfarm_nodes.get(i),
254                         that->renderfarm_ports.get(i),
255                         that->renderfarm_enabled.get(i),
256                         that->renderfarm_rate.get(i));
257         }
258         use_renderfarm = that->use_renderfarm;
259         renderfarm_port = that->renderfarm_port;
260         render_preroll = that->render_preroll;
261         brender_preroll = that->brender_preroll;
262         renderfarm_job_count = that->renderfarm_job_count;
263         renderfarm_watchdog_timeout = that->renderfarm_watchdog_timeout;
264         renderfarm_vfs = that->renderfarm_vfs;
265         strcpy(renderfarm_mountpoint, that->renderfarm_mountpoint);
266         renderfarm_consolidate = that->renderfarm_consolidate;
267         use_brender = that->use_brender;
268         brender_fragment = that->brender_fragment;
269         brender_asset->copy_from(that->brender_asset, 0);
270
271 // Check boundaries
272
273         FileSystem fs;
274         if( strlen(index_directory) ) {
275                 fs.complete_path(index_directory);
276                 fs.add_end_slash(index_directory);
277         }
278
279 //      if( strlen(global_plugin_dir) )
280 //      {
281 //              fs.complete_path(global_plugin_dir);
282 //              fs.add_end_slash(global_plugin_dir);
283 //      }
284 //
285
286 // Redo with the proper value of force_uniprocessor
287         processors = calculate_processors(0);
288         boundaries();
289 }
290
291 void Preferences::boundaries()
292 {
293         renderfarm_job_count = MAX(renderfarm_job_count, 1);
294         CLAMP(cache_size, MIN_CACHE_SIZE, MAX_CACHE_SIZE);
295 }
296
297 Preferences& Preferences::operator=(Preferences &that)
298 {
299 printf("Preferences::operator=\n");
300         copy_from(&that);
301         return *this;
302 }
303
304 void Preferences::print_channels(char *string, int *channel_positions, int channels)
305 {
306         char *cp = string, *ep = cp+BCTEXTLEN-1;
307         for( int i=0; i<channels; ++i ) {
308                 if( i ) cp += snprintf(cp, ep-cp, ", ");
309                 cp += snprintf(cp, ep-cp, "%d", channel_positions[i]);
310         }
311         *cp = 0;
312 }
313
314 void Preferences::scan_channels(char *string, int *channel_positions, int channels)
315 {
316         char *cp = string;
317         int current_channel = 0;
318         for(;;) {
319                 while( isspace(*cp) ) ++cp;
320                 if( !cp ) break;
321                 channel_positions[current_channel++] = strtol(cp, &cp, 0);
322                 if( current_channel >= channels ) break;
323                 while( isspace(*cp) ) ++cp;
324                 if( *cp == ',' ) ++cp;
325         }
326         while( current_channel < channels ) {
327                 int pos = default_audio_channel_position(current_channel, channels);
328                 channel_positions[current_channel++] = pos;
329         }
330 }
331
332
333 // TODO validation, otherwise corrupted/edited Cinelerra_rc crash app!
334 int Preferences::load_defaults(BC_Hash *defaults)
335 {
336         char string[BCTEXTLEN];
337
338         use_tipwindow = defaults->get("USE_TIPWINDOW", use_tipwindow);
339         scan_commercials = defaults->get("SCAN_COMMERCIALS", scan_commercials);
340         android_remote = defaults->get("ANDROID_REMOTE", android_remote);
341         android_port = defaults->get("ANDROID_PORT", android_port);
342         fast_speed = defaults->get("FAST_SPEED", fast_speed);
343         slow_speed = defaults->get("SLOW_SPEED", slow_speed);
344         defaults->get("ANDROID_PIN", android_pin);
345         defaults->get("INDEX_DIRECTORY", index_directory);
346         index_size = defaults->get("INDEX_SIZE", index_size);
347         index_count = defaults->get("INDEX_COUNT", index_count);
348         use_thumbnails = defaults->get("USE_THUMBNAILS", use_thumbnails);
349         keyframe_reticle = defaults->get("KEYFRAME_RETICLE", keyframe_reticle);
350         perpetual_session = defaults->get("PERPETUAL_SESSION", perpetual_session);
351         strcpy(lv2_path, DEFAULT_LV2_PATH);
352         defaults->get("LV2_PATH", lv2_path);
353         autostart_lv2ui = defaults->get("AUTOSTART_LV2UI", autostart_lv2ui);
354         strcpy(nested_proxy_path, DEFAULT_NESTED_PROXY_PATH);
355         defaults->get("NESTED_PROXY_PATH", nested_proxy_path);
356         trap_sigsegv = defaults->get("TRAP_SIGSEGV", trap_sigsegv);
357         trap_sigintr = defaults->get("TRAP_SIGINTR", trap_sigintr);
358
359         awindow_picon_h = defaults->get("AWINDOW_PICON_H", awindow_picon_h);
360         layout_scale = defaults->get("LAYOUT_SCALE",layout_scale);
361         vicon_size = defaults->get("VICON_SIZE",vicon_size);
362         vicon_color_mode = defaults->get("VICON_COLOR_MODE",vicon_color_mode);
363         if (vicon_color_mode > ( MAX_VICON_COLOR_MODE -1 ) || vicon_color_mode <0) vicon_color_mode = 0;
364         strcpy(theme, _(DEFAULT_THEME));
365         strcpy(locale, DEFAULT_LOCALE);
366         strcpy(plugin_icons, DEFAULT_PICON);
367         defaults->get("THEME", theme);
368         defaults->get("LOCALE", locale);
369         defaults->get("PLUGIN_ICONS", plugin_icons);
370         strcpy(snapshot_path, DEFAULT_SNAPSHOT_PATH);
371         defaults->get("SNAPSHOT_PATH", snapshot_path);
372
373         for( int i=0; i<MAXCHANNELS; ++i ) {
374                 char string2[BCTEXTLEN];
375                 sprintf(string, "CHANNEL_POSITIONS%d", i);
376                 print_channels(string2, &channel_positions[i][0], i+1);
377                 defaults->get(string, string2);
378                 scan_channels(string2, &channel_positions[i][0], i+1);
379         }
380         brender_asset->load_defaults(defaults, "BRENDER_", 1, 1, 1, 0, 0);
381
382         project_smp = defaults->get("PROJECT_SMP", project_smp);
383         force_uniprocessor = defaults->get("FORCE_UNIPROCESSOR", force_uniprocessor);
384         ffmpeg_marker_indexes = defaults->get("FFMPEG_MARKER_INDEXES", ffmpeg_marker_indexes);
385         memset(&use_hw_dev, 0, sizeof(use_hw_dev));
386         defaults->get("USE_HW_DEV", use_hw_dev);
387         warn_indexes = defaults->get("WARN_INDEXES", warn_indexes);
388         unsafe_gui = defaults->get("UNSAFE_GUI", unsafe_gui);
389         ongoing_backups = defaults->get("ONGOING_BACKUPS", ongoing_backups);
390         warn_fileref = defaults->get("WARN_FILEREF", warn_fileref);
391         bd_warn_root = defaults->get("BD_WARN_ROOT", bd_warn_root);
392         popupmenu_btnup = defaults->get("POPUPMENU_BTNUP", popupmenu_btnup);
393         grab_input_focus = defaults->get("GRAB_FOCUS", grab_input_focus);
394         textbox_focus_policy = defaults->get("TEXTBOX_FOCUS_POLICY", textbox_focus_policy);
395         auto_rotate = defaults->get("AUTO_ROTATE", auto_rotate);
396         forward_render_displacement = defaults->get("FORWARD_RENDER_DISPLACEMENT", forward_render_displacement);
397         dvd_yuv420p_interlace = defaults->get("DVD_YUV420P_INTERLACE", dvd_yuv420p_interlace);
398         highlight_inverse = defaults->get("HIGHLIGHT_INVERSE", highlight_inverse);
399         yuv_color_space = defaults->get("YUV_COLOR_SPACE", yuv_color_space);
400         if (yuv_color_space > (MAX_COLOR_SPACE - 1) || yuv_color_space < 0) yuv_color_space = 0;
401         yuv_color_range = defaults->get("YUV_COLOR_RANGE", yuv_color_range);
402         if (yuv_color_range > (MAX_COLOR_RANGE - 1) || yuv_color_range < 0) yuv_color_range = 0;
403         autocolor_assets = defaults->get("AUTOCOLOR_ASSETS", autocolor_assets);
404         ctrl_toggle = defaults->get("CTRL_TOGGLE", ctrl_toggle);
405         rectify_audio = defaults->get("RECTIFY_AUDIO", rectify_audio);
406         use_brender = defaults->get("USE_BRENDER", use_brender);
407         brender_fragment = defaults->get("BRENDER_FRAGMENT", brender_fragment);
408         cache_size = defaults->get("CACHE_SIZE", cache_size);
409         cache_transitions = defaults->get("CACHE_TRANSITIONS", cache_transitions);
410         local_rate = defaults->get("LOCAL_RATE", local_rate);
411         use_renderfarm = defaults->get("USE_RENDERFARM", use_renderfarm);
412         renderfarm_port = defaults->get("RENDERFARM_PORT", renderfarm_port);
413         render_preroll = defaults->get("RENDERFARM_PREROLL", render_preroll);
414         brender_preroll = defaults->get("BRENDER_PREROLL", brender_preroll);
415         renderfarm_job_count = defaults->get("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
416         renderfarm_watchdog_timeout = defaults->get("RENDERFARM_WATCHDOG_TIMEOUT", renderfarm_watchdog_timeout);
417         renderfarm_consolidate = defaults->get("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
418 //      renderfarm_vfs = defaults->get("RENDERFARM_VFS", renderfarm_vfs);
419         defaults->get("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
420         int renderfarm_total = defaults->get("RENDERFARM_TOTAL", 0);
421
422         for( int i = 0; i < renderfarm_total; i++ ) {
423                 sprintf(string, "RENDERFARM_NODE%d", i);
424                 char result[BCTEXTLEN];
425                 int result_port = 0;
426                 int result_enabled = 0;
427                 float result_rate = 0.0;
428
429                 result[0] = 0;
430                 defaults->get(string, result);
431
432                 sprintf(string, "RENDERFARM_PORT%d", i);
433                 result_port = defaults->get(string, renderfarm_port);
434
435                 sprintf(string, "RENDERFARM_ENABLED%d", i);
436                 result_enabled = defaults->get(string, result_enabled);
437
438                 sprintf(string, "RENDERFARM_RATE%d", i);
439                 result_rate = defaults->get(string, result_rate);
440
441                 if( result[0] != 0 ) {
442                         add_node(result, result_port, result_enabled, result_rate);
443                 }
444         }
445
446         shbtn_prefs.remove_all_objects();
447         int shbtns_total = defaults->get("SHBTNS_TOTAL", -1);
448         if( shbtns_total < 0 ) {
449                 shbtn_prefs.append(new ShBtnPref(_("Current Manual"), "$CIN_BROWSER https://cinelerra-gg.org/download/CinelerraGG_Manual.pdf"));
450                 shbtn_prefs.append(new ShBtnPref(_("HTML Current Manual"), "$CIN_BROWSER https://cinelerra-gg.org/download/CinelerraGG_Manual"));
451                 shbtn_prefs.append(new ShBtnPref(_("Setting Shell Commands"), "$CIN_BROWSER file://$CIN_DAT/doc/ShellCmds.html"));
452                 shbtn_prefs.append(new ShBtnPref(_("Shortcuts"), "$CIN_BROWSER file://$CIN_DAT/doc/shortcuts.html"));
453                 shbtn_prefs.append(new ShBtnPref(_("RenderMux"), "$CIN_DAT/doc/RenderMux.sh"));
454                 shbtns_total = 0;
455         }
456         for( int i=0; i<shbtns_total; ++i ) {
457                 char name[BCTEXTLEN], commands[BCTEXTLEN];
458                 sprintf(string, "SHBTN%d_NAME", i);
459                 defaults->get(string, name);
460                 sprintf(string, "SHBTN%d_COMMANDS", i);
461                 defaults->get(string, commands);
462                 sprintf(string, "SHBTN%d_WARN", i);
463                 int warn = defaults->get(string, 0);
464                 sprintf(string, "SHBTN%d_RUN_SCRIPT", i);
465                 int run_script = defaults->get(string, 0);
466                 shbtn_prefs.append(new ShBtnPref(name, commands, warn, run_script));
467         }
468
469         file_probes.remove_all_objects();
470         int file_probe_total = defaults->get("FILE_PROBE_TOTAL", 0);
471         for( int i=0; i<file_probe_total; ++i ) {
472                 char name[BCTEXTLEN];
473                 sprintf(string, "FILE_PROBE%d_NAME", i);
474                 defaults->get(string, name);
475                 sprintf(string, "FILE_PROBE%d_ARMED", i);
476                 int armed = defaults->get(string, 1);
477                 file_probes.append(new ProbePref(name, armed));
478         }
479         // append any missing probes
480         for( int i=0; i<File::nb_probes; ++i ) {
481                 const char *nm = File::default_probes[i];
482                 int k = file_probes.size();
483                 while( --k>=0 && strcmp(nm, file_probes[k]->name) );
484                 if( k >= 0 ) continue;
485                 int armed = 1;
486                 if( !strcmp(nm, "FFMPEG_Late") ||
487                     !strcmp(nm, "CR2") ) armed = 0;
488                 file_probes.append(new ProbePref(nm, armed));
489         }
490
491 // Redo with the proper value of force_uniprocessor
492         processors = calculate_processors(0);
493         boundaries();
494         return 0;
495 }
496
497 int Preferences::save_defaults(BC_Hash *defaults)
498 {
499         char string[BCTEXTLEN];
500
501
502         defaults->update("USE_TIPWINDOW", use_tipwindow);
503         defaults->update("SCAN_COMMERCIALS", scan_commercials);
504         defaults->update("ANDROID_REMOTE", android_remote);
505         defaults->update("ANDROID_PIN", android_pin);
506         defaults->update("ANDROID_PORT", android_port);
507         defaults->update("SLOW_SPEED", slow_speed);
508         defaults->update("FAST_SPEED", fast_speed);
509         defaults->update("CACHE_SIZE", cache_size);
510         defaults->update("CACHE_TRANSITIONS", cache_transitions);
511         defaults->update("INDEX_DIRECTORY", index_directory);
512         defaults->update("INDEX_SIZE", index_size);
513         defaults->update("INDEX_COUNT", index_count);
514         defaults->update("USE_THUMBNAILS", use_thumbnails);
515         defaults->update("KEYFRAME_RETICLE", keyframe_reticle);
516         defaults->update("PERPETUAL_SESSION", perpetual_session);
517         defaults->update("LV2_PATH", lv2_path);
518         defaults->update("AUTOSTART_LV2UI", autostart_lv2ui);
519         defaults->update("NESTED_PROXY_PATH", nested_proxy_path);
520         defaults->update("TRAP_SIGSEGV", trap_sigsegv);
521         defaults->update("TRAP_SIGINTR", trap_sigintr);
522         defaults->update("AWINDOW_PICON_H", awindow_picon_h);
523         defaults->update("LAYOUT_SCALE",layout_scale);
524         defaults->update("VICON_SIZE",vicon_size);
525         defaults->update("VICON_COLOR_MODE",vicon_color_mode);
526         defaults->update("THEME", theme);
527         defaults->update("LOCALE", locale);
528         defaults->update("PLUGIN_ICONS", plugin_icons);
529         defaults->update("SNAPSHOT_PATH", snapshot_path);
530
531         for( int i = 0; i < MAXCHANNELS; i++ ) {
532                 char string2[BCTEXTLEN];
533                 sprintf(string, "CHANNEL_POSITIONS%d", i);
534                 print_channels(string2, &channel_positions[i][0], i + 1);
535                 defaults->update(string, string2);
536         }
537
538         defaults->update("PROJECT_SMP", project_smp);
539         defaults->update("FORCE_UNIPROCESSOR", force_uniprocessor);
540         defaults->update("FFMPEG_MARKER_INDEXES", ffmpeg_marker_indexes);
541         defaults->update("USE_HW_DEV", use_hw_dev);
542         defaults->update("WARN_INDEXES", warn_indexes);
543         defaults->update("UNSAFE_GUI", unsafe_gui);
544         defaults->update("ONGOING_BACKUPS", ongoing_backups);
545         defaults->update("WARN_FILEREF", warn_fileref);
546         defaults->update("BD_WARN_ROOT", bd_warn_root);
547         defaults->update("POPUPMENU_BTNUP", popupmenu_btnup);
548         defaults->update("GRAB_FOCUS", grab_input_focus);
549         defaults->update("TEXTBOX_FOCUS_POLICY", textbox_focus_policy);
550         defaults->update("AUTO_ROTATE", auto_rotate);
551         defaults->update("FORWARD_RENDER_DISPLACEMENT", forward_render_displacement);
552         defaults->update("DVD_YUV420P_INTERLACE", dvd_yuv420p_interlace);
553         defaults->update("HIGHLIGHT_INVERSE", highlight_inverse);
554         defaults->update("YUV_COLOR_SPACE", yuv_color_space);
555         defaults->update("YUV_COLOR_RANGE", yuv_color_range);
556         defaults->update("AUTOCOLOR_ASSETS", autocolor_assets);
557         defaults->update("CTRL_TOGGLE", ctrl_toggle);
558         defaults->update("RECTIFY_AUDIO", rectify_audio);
559         brender_asset->save_defaults(defaults, "BRENDER_", 1, 1, 1, 0, 0);
560         defaults->update("USE_BRENDER", use_brender);
561         defaults->update("BRENDER_FRAGMENT", brender_fragment);
562         defaults->update("USE_RENDERFARM", use_renderfarm);
563         defaults->update("LOCAL_RATE", local_rate);
564         defaults->update("RENDERFARM_PORT", renderfarm_port);
565         defaults->update("RENDERFARM_PREROLL", render_preroll);
566         defaults->update("BRENDER_PREROLL", brender_preroll);
567 //      defaults->update("RENDERFARM_VFS", renderfarm_vfs);
568         defaults->update("RENDERFARM_MOUNTPOINT", renderfarm_mountpoint);
569         defaults->update("RENDERFARM_JOBS_COUNT", renderfarm_job_count);
570         defaults->update("RENDERFARM_WATCHDOG_TIMEOUT", renderfarm_watchdog_timeout);
571         defaults->update("RENDERFARM_CONSOLIDATE", renderfarm_consolidate);
572         defaults->update("RENDERFARM_TOTAL", (int64_t)renderfarm_nodes.total);
573         for( int i = 0; i < renderfarm_nodes.total; i++ ) {
574                 sprintf(string, "RENDERFARM_NODE%d", i);
575                 defaults->update(string, renderfarm_nodes.values[i]);
576                 sprintf(string, "RENDERFARM_PORT%d", i);
577                 defaults->update(string, renderfarm_ports.values[i]);
578                 sprintf(string, "RENDERFARM_ENABLED%d", i);
579                 defaults->update(string, renderfarm_enabled.values[i]);
580                 sprintf(string, "RENDERFARM_RATE%d", i);
581                 defaults->update(string, renderfarm_rate.values[i]);
582         }
583         defaults->update("SHBTNS_TOTAL", shbtn_prefs.size());
584         for( int i=0; i<shbtn_prefs.size(); ++i ) {
585                 ShBtnPref *pref = shbtn_prefs[i];
586                 sprintf(string, "SHBTN%d_NAME", i);
587                 defaults->update(string, pref->name);
588                 sprintf(string, "SHBTN%d_COMMANDS", i);
589                 defaults->update(string, pref->commands);
590                 sprintf(string, "SHBTN%d_WARN", i);
591                 defaults->update(string, pref->warn);
592                 sprintf(string, "SHBTN%d_RUN_SCRIPT", i);
593                 defaults->update(string, pref->run_script);
594         }
595         defaults->update("FILE_PROBE_TOTAL", file_probes.size());
596         for( int i=0; i<file_probes.size(); ++i ) {
597                 ProbePref *pref = file_probes[i];
598                 sprintf(string, "FILE_PROBE%d_NAME", i);
599                 defaults->update(string, pref->name);
600                 sprintf(string, "FILE_PROBE%d_ARMED", i);
601                 defaults->update(string, pref->armed);
602         }
603         return 0;
604 }
605
606
607 void Preferences::add_node(const char *text, int port, int enabled, float rate)
608 {
609         if( text[0] == 0 ) return;
610
611         preferences_lock->lock("Preferences::add_node");
612         char *new_item = new char[strlen(text) + 1];
613         strcpy(new_item, text);
614         renderfarm_nodes.append(new_item);
615         renderfarm_nodes.set_array_delete();
616         renderfarm_ports.append(port);
617         renderfarm_enabled.append(enabled);
618         renderfarm_rate.append(rate);
619         preferences_lock->unlock();
620 }
621
622 void Preferences::delete_node(int number)
623 {
624         preferences_lock->lock("Preferences::delete_node");
625         if( number < renderfarm_nodes.total && number >= 0 ) {
626                 delete [] renderfarm_nodes.values[number];
627                 renderfarm_nodes.remove_number(number);
628                 renderfarm_ports.remove_number(number);
629                 renderfarm_enabled.remove_number(number);
630                 renderfarm_rate.remove_number(number);
631         }
632         preferences_lock->unlock();
633 }
634
635 void Preferences::delete_nodes()
636 {
637         preferences_lock->lock("Preferences::delete_nodes");
638         for( int i = 0; i < renderfarm_nodes.total; i++ )
639                 delete [] renderfarm_nodes.values[i];
640         renderfarm_nodes.remove_all();
641         renderfarm_ports.remove_all();
642         renderfarm_enabled.remove_all();
643         renderfarm_rate.remove_all();
644         preferences_lock->unlock();
645 }
646
647 void Preferences::reset_rates()
648 {
649         for( int i = 0; i < renderfarm_nodes.total; i++ ) {
650                 renderfarm_rate.values[i] = 0.0;
651         }
652         local_rate = 0.0;
653 }
654
655 float Preferences::get_rate(int node)
656 {
657         if( node < 0 ) {
658                 return local_rate;
659         }
660         else {
661                 int total = 0;
662                 for( int i = 0; i < renderfarm_nodes.size(); i++ ) {
663                         if( renderfarm_enabled.get(i) ) total++;
664                         if( total == node + 1 ) {
665                                 return renderfarm_rate.get(i);
666                         }
667                 }
668         }
669
670         return 0;
671 }
672
673 void Preferences::set_rate(float rate, int node)
674 {
675 //printf("Preferences::set_rate %f %d\n", rate, node);
676         if( node < 0 ) {
677                 local_rate = rate;
678         }
679         else {
680                 int total = 0;
681                 for( int i = 0; i < renderfarm_nodes.size(); i++ ) {
682                         if( renderfarm_enabled.get(i) ) total++;
683                         if( total == node + 1 ) {
684                                 renderfarm_rate.set(i, rate);
685                                 return;
686                         }
687                 }
688         }
689 }
690
691 float Preferences::get_avg_rate(int use_master_node)
692 {
693         preferences_lock->lock("Preferences::get_avg_rate");
694         float total = 0.0;
695         if( renderfarm_rate.total ) {
696                 int enabled = 0;
697                 if( use_master_node ) {
698                         if( EQUIV(local_rate, 0.0) ) {
699                                 preferences_lock->unlock();
700                                 return 0.0;
701                         }
702                         else {
703                                 enabled++;
704                                 total += local_rate;
705                         }
706                 }
707
708                 for( int i = 0; i < renderfarm_rate.total; i++ ) {
709                         if( renderfarm_enabled.values[i] ) {
710                                 enabled++;
711                                 total += renderfarm_rate.values[i];
712                                 if( EQUIV(renderfarm_rate.values[i], 0.0) ) {
713                                         preferences_lock->unlock();
714                                         return 0.0;
715                                 }
716                         }
717                 }
718
719                 if( enabled )
720                         total /= enabled;
721                 else
722                         total = 0.0;
723         }
724         preferences_lock->unlock();
725
726         return total;
727 }
728
729 void Preferences::sort_nodes()
730 {
731         int done = 0;
732
733         while(!done)
734         {
735                 done = 1;
736                 for( int i = 0; i < renderfarm_nodes.total - 1; i++ ) {
737                         if( strcmp(renderfarm_nodes.values[i], renderfarm_nodes.values[i + 1]) > 0 ) {
738                                 char *temp = renderfarm_nodes.values[i];
739                                 int temp_port = renderfarm_ports.values[i];
740
741                                 renderfarm_nodes.values[i] = renderfarm_nodes.values[i + 1];
742                                 renderfarm_nodes.values[i + 1] = temp;
743
744                                 renderfarm_ports.values[i] = renderfarm_ports.values[i + 1];
745                                 renderfarm_ports.values[i + 1] = temp_port;
746
747                                 renderfarm_enabled.values[i] = renderfarm_enabled.values[i + 1];
748                                 renderfarm_enabled.values[i + 1] = temp_port;
749
750                                 renderfarm_rate.values[i] = renderfarm_rate.values[i + 1];
751                                 renderfarm_rate.values[i + 1] = temp_port;
752                                 done = 0;
753                         }
754                 }
755         }
756 }
757
758 void Preferences::edit_node(int number,
759         const char *new_text,
760         int new_port,
761         int new_enabled)
762 {
763         char *new_item = new char[strlen(new_text) + 1];
764         strcpy(new_item, new_text);
765
766         delete [] renderfarm_nodes.values[number];
767         renderfarm_nodes.values[number] = new_item;
768         renderfarm_ports.values[number] = new_port;
769         renderfarm_enabled.values[number] = new_enabled;
770 }
771
772 int Preferences::get_enabled_nodes()
773 {
774         int result = 0;
775         for( int i = 0; i < renderfarm_enabled.total; i++ )
776                 if( renderfarm_enabled.values[i] ) result++;
777         return result;
778 }
779
780 const char* Preferences::get_node_hostname(int number)
781 {
782         int total = 0;
783         for( int i = 0; i < renderfarm_nodes.total; i++ ) {
784                 if( renderfarm_enabled.values[i] ) {
785                         if( total == number )
786                                 return renderfarm_nodes.values[i];
787                         else
788                                 total++;
789                 }
790         }
791         return "";
792 }
793
794 int Preferences::get_node_port(int number)
795 {
796         int total = 0;
797         for( int i = 0; i < renderfarm_ports.total; i++ ) {
798                 if( renderfarm_enabled.values[i] ) {
799                         if( total == number )
800                                 return renderfarm_ports.values[i];
801                         else
802                                 total++;
803                 }
804         }
805         return -1;
806 }
807
808 int Preferences::get_asset_file_path(Asset *asset, char *path)
809 {
810         strcpy(path, asset->path);
811         int result = !access(path, R_OK) ? 0 : -1;
812         if( !result && ( asset->format == FILE_MPEG || asset->format == FILE_AC3 ||
813                 asset->format == FILE_VMPEG || asset->format == FILE_AMPEG ) ) {
814                 char source_filename[BCTEXTLEN], index_filename[BCTEXTLEN];
815                 IndexFile::get_index_filename(source_filename,
816                         index_directory, index_filename, asset->path, ".toc");
817                 strcpy(path, index_filename);
818                 if( access(path, R_OK) )
819                         result = 1;
820         }
821 // result = 0, asset->path/toc exist, -1 no asset, 1 no toc
822         return result;
823 }
824
825
826 int Preferences::calculate_processors(int interactive)
827 {
828         if( force_uniprocessor && !interactive ) return 1;
829         return BC_WindowBase::get_resources()->machine_cpus;
830 }
831
832 int Preferences::get_file_probe_armed(const char *nm)
833 {
834         int k = file_probes.size();
835         while( --k>=0 && strcmp(nm, file_probes[k]->name) );
836         if( k < 0 ) return -1;
837         return file_probes[k]->armed;
838 }
839
840 void Preferences::set_file_probe_armed(const char *nm, int v)
841 {
842         int k = file_probes.size();
843         while( --k>=0 && strcmp(nm, file_probes[k]->name) );
844         if( k < 0 ) return;
845         file_probes[k]->armed = v;
846 }
847