You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SimpleSkyCam/configuration.cc

194 lines
4.8 KiB

#include "configuration.h"
#include "video.h"
#include "gui.h"
#include <string>
extern VideoDev videodev;
extern GtkBuilder *_builder_; // work around for threads
Configuration::Configuration() {
};
Configuration::~Configuration() {
};
std::string Configuration::GetDefaultFileName() {
std::string fn = "";
char *hd = NULL;
if ((hd = getenv ("HOME")) == NULL) {
// no homedir defined?
return "";
}
fn = hd;
fn = fn + "/" + CONFIGURATION_DEFAULTFILE;
return fn;
};
void Configuration::SaveDefault() {
std::string fn = GetDefaultFileName();
if (fn.length() < 1) {
printf ("%s:%d %s could not read HOME environment variable\n", __FILE__, __LINE__, __FUNCTION__);
return;
}
SaveConfig (fn);
};
void Configuration::LoadDefault() {
std::string fn = GetDefaultFileName();
if (fn.length() < 1) {
printf ("%s:%d %s could not read HOME environment variable\n", __FILE__, __LINE__, __FUNCTION__);
return;
}
LoadConfig (fn);
};
/*
* collect all the settings, and push them into the JSONparse object
* save the json string.
*/
void Configuration::SaveConfig(std::string filename) {
JSONParse jp;
string vstr;
int i;
GtkWidget *cb;
GtkWidget *cbe;
//
// Add resolution, format and video device to config json element
cb = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "cb-videores"));
cbe = gtk_bin_get_child(GTK_BIN(cb));
jp.AddObject("video_resolution", (string) gtk_entry_get_text(GTK_ENTRY(cbe)));
cb = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "cb-videofmt"));
cbe = gtk_bin_get_child(GTK_BIN(cb));
jp.AddObject("video_format", (string) gtk_entry_get_text(GTK_ENTRY(cbe)));
jp.AddObject("device", videodev.GetDevice());
//
// save button config
for (i = 0; i < BTN_PRESET_MAX; i++) {
JSONElement je;
je.type = JSON_T_ARRAY;
je.name = "presetbtn"+ std::to_string(i);
list<VideoDevCtrl>::iterator vciter;
JSONParse jpbtn;
jpbtn.Clear();
// convert VideoDevCtrl to JSON
for (vciter = presetbtn[i].begin(); vciter != presetbtn[i].end(); vciter++) {
jpbtn.Clear();
jpbtn.AddObject("name", vciter->name);
jpbtn.AddObject("id", (signed int) vciter->id);
jpbtn.AddObject("max", vciter->max);
jpbtn.AddObject("min", vciter->min);
jpbtn.AddObject("value", vciter->value);
if (je.value.length()>0) je.value += "," + jpbtn.ToString();
else je.value = jpbtn.ToString();
}
jp.AddObject(je);
}
//
// save config
if (jp.SaveToFile(filename)) {
printf ("%s:%d %s could not save to file [%s] Error: %s\n", __FILE__, __LINE__, __FUNCTION__,
filename.c_str(), strerror(errno));
}
};
void Configuration::LoadConfig(std::string filename) {
JSONParse jp;
string vstr;
int i;
JSONElement je;
if (jp.LoadFromFile(filename)) {
printf ("%s:%d %s could not load from file [%s] Error: %s\n", __FILE__, __LINE__, __FUNCTION__,
filename.c_str(), strerror(errno));
}
else {
printf ("%s:%d %s\n", __FILE__, __LINE__, __FUNCTION__);
//
// set resolution and video stream mode
//
if (jp.GetValue("video_resolution", &vstr)) {
GtkWidget *cbox = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "cb-videores"));
GtkWidget *cbdevice = gtk_bin_get_child(GTK_BIN(cbox));
gtk_entry_set_text(GTK_ENTRY(cbdevice), vstr.c_str());
}
if (jp.GetValue("video_format", &vstr)) {
GtkWidget *cbox = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "cb-videofmt"));
GtkWidget *cbdevice = gtk_bin_get_child(GTK_BIN(cbox));
gtk_entry_set_text(GTK_ENTRY(cbdevice), vstr.c_str());
}
//
// start streaming if a device was selected.
//
if (jp.GetValue("device", &vstr)) {
GtkWidget *cbox = GTK_WIDGET(gtk_builder_get_object (GTK_BUILDER(_builder_), "cb-videodev"));
GtkWidget *cbdevice = gtk_bin_get_child(GTK_BIN(cbox));
gtk_entry_set_text(GTK_ENTRY(cbdevice), vstr.c_str());
cb_video_btnrec (NULL, NULL);
}
//
// load the button config
//
for (i = 0; i < BTN_PRESET_MAX; i++) {
VideoDevCtrl ctrl;
JSONParse jpbtn, jpctrl;
list<JSONElement> je;
list<JSONElement>::iterator iter;
int ctrli;
int64_t id;
//
// load all controls from JSONParse object
presetbtn[i].clear();
ctrli = 0;
while (jp.GetObjectIdx("presetbtn"+ std::to_string(i), ctrli, &jpctrl)) {
jpctrl.GetValue("name", &ctrl.name);
jpctrl.GetValueInt64("id", &id);
ctrl.id = id;
jpctrl.GetValueInt("min", &ctrl.min);
jpctrl.GetValueInt("max", &ctrl.max);
jpctrl.GetValueInt("value", &ctrl.value);
presetbtn[i].push_back(ctrl);
ctrli++;
}
}
}
};
/*
* presetbtn[btn]
*/
void Configuration::SetPresetButton (int btn, list<VideoDevCtrl> *parameters) {
if (btn < 0 || btn >= BTN_PRESET_MAX) return;
presetbtn[btn] = *parameters;
};
list<VideoDevCtrl> Configuration::GetPresetButton (int btn) {
list<VideoDevCtrl> emptylist;
if (btn >= 0 && btn < BTN_PRESET_MAX) return presetbtn[btn];
return emptylist;
};