Browse Source

added initial config file stuff, set up a 'dummy' window to cause the graphics system to persist through window changes

jp9000 12 years ago
parent
commit
0aad78819c
5 changed files with 189 additions and 16 deletions
  1. 100 13
      obs/obs-app.cpp
  2. 19 3
      obs/obs-app.hpp
  3. 62 0
      obs/obs-wrappers.hpp
  4. 7 0
      obs/window-obs-basic.cpp
  5. 1 0
      obs/window-obs-basic.hpp

+ 100 - 13
obs/obs-app.cpp

@@ -15,40 +15,121 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
 
+#include <sstream>
+using namespace std;
+
+#include <sys/stat.h>
+
 #include <util/bmem.h>
+#include <util/platform.h>
+
 #include "obs-app.hpp"
 #include "window-obs-basic.hpp"
 #include "obs-wrappers.hpp"
 #include "wx-wrappers.hpp"
 
-IMPLEMENT_APP(OBSApp)
+IMPLEMENT_APP(OBSApp);
+
+OBSAppBase::~OBSAppBase()
+{
+	blog(LOG_INFO, "Number of memory leaks: %u", bnum_allocs());
+}
+
+static void do_log(enum log_type type, const char *msg, va_list args)
+{
+	char bla[4096];
+	vsnprintf(bla, 4095, msg, args);
+
+	OutputDebugStringA(bla);
+	OutputDebugStringA("\n");
+
+	if (type >= LOG_WARNING)
+		__debugbreak();
+}
+
+void OBSApp::InitGlobalConfigDefaults()
+{
+	config_set_default_int(globalConfig, "Window", "PosX",  -1);
+	config_set_default_int(globalConfig, "Window", "PosY",  -1);
+	config_set_default_int(globalConfig, "Window", "SizeX", -1);
+	config_set_default_int(globalConfig, "Window", "SizeY", -1);
+}
+
+static bool do_mkdir(const char *path)
+{
+	if (os_mkdir(path) == MKDIR_ERROR) {
+		blog(LOG_ERROR, "Failed to create directory %s", path);
+		return false;
+	}
+
+	return true;
+}
+
+static bool MakeUserDirs()
+{
+	BPtr<char*> homePath = os_get_home_path();
+	stringstream str;
+
+	str << homePath << "/obs-studio";
+	if (!do_mkdir(str.str().c_str()))
+		return false;
+
+	return true;
+}
+
+bool OBSApp::InitGlobalConfig()
+{
+	BPtr<char*> homePath = os_get_home_path();
+	stringstream str;
+
+	if (!homePath) {
+		blog(LOG_ERROR, "Failed to get home path");
+		return false;
+	}
+
+	str << homePath << "/obs-studio/global.ini";
+	string path = move(str.str());
+
+	int errorcode = globalConfig.Open(path.c_str(), CONFIG_OPEN_ALWAYS);
+	if (errorcode != CONFIG_SUCCESS) {
+		blog(LOG_ERROR, "Failed to open global.ini: %d", errorcode);
+		return false;
+	}
+
+	InitGlobalConfigDefaults();
+	return true;
+}
 
 bool OBSApp::OnInit()
 {
+	base_set_log_handler(do_log);
+
 	if (!wxApp::OnInit())
 		return false;
-
+	if (!MakeUserDirs())
+		return false;
+	if (!InitGlobalConfig())
+		return false;
 	if (!obs_startup())
 		return false;
 
 	wxInitAllImageHandlers();
 
-	OBSBasic *mainWindow = new OBSBasic();
+	dummyWindow = new wxFrame(NULL, wxID_ANY, "Dummy Window");
 
-	const wxPanel *preview = mainWindow->GetPreviewPanel();
-	wxRect rc = mainWindow->GetPreviewPanel()->GetClientRect();
+	OBSBasic *mainWindow = new OBSBasic();
 
 	struct obs_video_info ovi;
 	ovi.adapter         = 0;
-	ovi.base_width      = rc.width;
-	ovi.base_height     = rc.height;
+	ovi.base_width      = 2;
+	ovi.base_height     = 2;
 	ovi.fps_num         = 30000;
 	ovi.fps_den         = 1001;
 	ovi.graphics_module = "libobs-opengl";
 	ovi.output_format   = VIDEO_FORMAT_RGBA;
-	ovi.output_width    = rc.width;
-	ovi.output_height   = rc.height;
-	ovi.window          = WxToGSWindow(preview);
+	ovi.output_width    = 2;
+	ovi.output_height   = 2;
+	ovi.window          = WxToGSWindow(dummyWindow);
 
 	if (!obs_reset_video(&ovi))
 		return false;
@@ -60,8 +141,14 @@ bool OBSApp::OnInit()
 int OBSApp::OnExit()
 {
 	obs_shutdown();
-	blog(LOG_INFO, "Number of memory leaks: %u", bnum_allocs());
 
-	wxApp::OnExit();
-	return 0;
+	delete dummyWindow;
+	dummyWindow = NULL;
+
+	return wxApp::OnExit();
+}
+
+void OBSApp::CleanUp()
+{
+	OBSAppBase::CleanUp();
 }

+ 19 - 3
obs/obs-app.hpp

@@ -17,11 +17,27 @@
 
 #pragma once
 
-#include <obs.h>
 #include <wx/app.h>
 
-class OBSApp : public wxApp {
+#include "obs-wrappers.hpp"
+
+class OBSAppBase : public wxApp {
+public:
+	virtual ~OBSAppBase();
+};
+
+class OBSApp : public OBSAppBase {
+	ConfigFile globalConfig;
+	wxFrame *dummyWindow;
+
+	bool InitGlobalConfig();
+	void InitGlobalConfigDefaults();
+	bool InitConfigDefaults();
+
 public:
 	virtual bool OnInit();
-	virtual int OnExit();
+	virtual int  OnExit();
+	virtual void CleanUp();
 };
+
+wxDECLARE_APP(OBSApp);

+ 62 - 0
obs/obs-wrappers.hpp

@@ -17,8 +17,68 @@
 
 #pragma once
 
+#include <string.h>
+#include <stdarg.h>
+
+#include <util/config-file.h>
 #include <obs.h>
 
+/* RAII wrappers */
+
+template<typename T> class BPtr {
+	T ptr;
+
+public:
+	inline BPtr() : ptr(NULL)   {}
+	inline BPtr(T p) : ptr(p)   {}
+	inline ~BPtr()              {bfree(ptr);}
+
+	inline T operator=(T p)     {bfree(ptr); ptr = p;}
+	inline operator T()         {return ptr;}
+	inline T *operator&()       {bfree(ptr); ptr = NULL; return &ptr;}
+
+	inline bool operator!()     {return ptr == NULL;}
+	inline bool operator==(T p) {return ptr == p;}
+	inline bool operator!=(T p) {return ptr != p;}
+};
+
+class ConfigFile {
+	config_t config;
+
+public:
+	inline ConfigFile() : config(NULL) {}
+	inline ~ConfigFile()
+	{
+		config_close(config);
+	}
+
+	inline bool Create(const char *file)
+	{
+		Close();
+		config = config_create(file);
+		return config != NULL;
+	}
+
+	int Open(const char *file, config_open_type openType)
+	{
+		Close();
+		return config_open(&config, file, openType);
+	}
+
+	int Save()
+	{
+		return config_save(config);
+	}
+
+	void Close()
+	{
+		config_close(config);
+		config = NULL;
+	}
+
+	inline operator config_t() {return config;}
+};
+
 class OBSSource {
 	obs_source_t source;
 
@@ -26,6 +86,8 @@ public:
 	inline OBSSource(obs_source_t source) : source(source) {}
 	inline ~OBSSource() {obs_source_release(source);}
 
+	inline OBSSource& operator=(obs_source_t p) {source = p;}
+
 	inline operator obs_source_t() {return source;}
 
 	inline bool operator==(obs_source_t p) const {return source == p;}

+ 7 - 0
obs/window-obs-basic.cpp

@@ -15,8 +15,15 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
 
+#include <wx/app.h>
+#include "obs-app.hpp"
 #include "window-obs-basic.hpp"
 
+void OBSBasic::OnClose(wxCloseEvent& event)
+{
+	wxGetApp().ExitMainLoop();
+}
+
 void OBSBasic::file_newOnMenuSelection(wxCommandEvent& event)
 {
 }

+ 1 - 0
obs/window-obs-basic.hpp

@@ -21,6 +21,7 @@
 
 class OBSBasic : public OBSBasicBase {
 protected:
+	virtual void OnClose(wxCloseEvent& event);
 	virtual void file_newOnMenuSelection(wxCommandEvent& event);
 	virtual void file_openOnMenuSelection(wxCommandEvent& event);
 	virtual void file_saveOnMenuSelection(wxCommandEvent& event);