Преглед изворни кода

implement scene adding callbacks, make a few API tweaks

jp9000 пре 11 година
родитељ
комит
cec94b042e

+ 2 - 2
libobs/callback/proc.c

@@ -52,7 +52,7 @@ void proc_handler_destroy(proc_handler_t handler)
 }
 
 void proc_handler_add(proc_handler_t handler, const char *name,
-		void (*proc)(calldata_t, void*), void *data)
+		void (*proc)(void*, calldata_t), void *data)
 {
 	struct proc_info pi = {bstrdup(name), data, proc};
 	da_push_back(handler->procs, &pi);
@@ -65,7 +65,7 @@ bool proc_handler_call(proc_handler_t handler, const char *name,
 		struct proc_info *info = handler->procs.array+i;
 
 		if (strcmp(info->name, name) == 0) {
-			info->proc(params, info->data);
+			info->proc(info->data, params);
 			return true;
 		}
 	}

+ 1 - 1
libobs/callback/proc.h

@@ -39,7 +39,7 @@ EXPORT proc_handler_t proc_handler_create(void);
 EXPORT void proc_handler_destroy(proc_handler_t handler);
 
 EXPORT void proc_handler_add(proc_handler_t handler, const char *name,
-		void (*proc)(calldata_t, void*), void *data);
+		void (*proc)(void*, calldata_t), void *data);
 
 /**
  * Calls a function in a procedure handler.  Returns false if the named

+ 4 - 4
libobs/callback/signal.c

@@ -60,7 +60,7 @@ static inline void signal_info_destroy(struct signal_info *si)
 }
 
 static inline size_t signal_get_callback_idx(struct signal_info *si,
-		void (*callback)(calldata_t, void*), void *data)
+		void (*callback)(void*, calldata_t), void *data)
 {
 	for (size_t i = 0; i < si->callbacks.num; i++) {
 		struct signal_callback *sc = si->callbacks.array+i;
@@ -129,7 +129,7 @@ void signal_handler_destroy(signal_handler_t handler)
 }
 
 void signal_handler_connect(signal_handler_t handler, const char *signal,
-		void (*callback)(calldata_t, void*), void *data)
+		void (*callback)(void*, calldata_t), void *data)
 {
 	struct signal_info *sig, *last;
 	struct signal_callback cb_data = {callback, data};
@@ -174,7 +174,7 @@ static inline struct signal_info *getsignal_locked(signal_handler_t handler,
 }
 
 void signal_handler_disconnect(signal_handler_t handler, const char *signal,
-		void (*callback)(calldata_t, void*), void *data)
+		void (*callback)(void*, calldata_t), void *data)
 {
 	struct signal_info *sig = getsignal_locked(handler, signal);
 	size_t idx;
@@ -203,7 +203,7 @@ void signal_handler_signal(signal_handler_t handler, const char *signal,
 
 	for (size_t i = 0; i < sig->callbacks.num; i++) {
 		struct signal_callback *cb = sig->callbacks.array+i;
-		cb->callback(params, cb->data);
+		cb->callback(cb->data, params);
 	}
 
 	pthread_mutex_unlock(&sig->mutex);

+ 2 - 2
libobs/callback/signal.h

@@ -38,9 +38,9 @@ EXPORT signal_handler_t signal_handler_create(void);
 EXPORT void signal_handler_destroy(signal_handler_t handler);
 
 EXPORT void signal_handler_connect(signal_handler_t handler, const char *signal,
-		void (*callback)(calldata_t, void*), void *data);
+		void (*callback)(void*, calldata_t), void *data);
 EXPORT void signal_handler_disconnect(signal_handler_t handler,
-		const char *signal, void (*callback)(calldata_t, void*),
+		const char *signal, void (*callback)(void*, calldata_t),
 		void *data);
 
 EXPORT void signal_handler_signal(signal_handler_t handler, const char *signal,

+ 12 - 1
libobs/obs-scene.c

@@ -97,7 +97,7 @@ static const struct source_info scene_info =
 	.getheight        = scene_getsize,
 };
 
-obs_scene_t obs_scene_create(void)
+obs_scene_t obs_scene_create(const char *name)
 {
 	struct obs_source *source = bmalloc(sizeof(struct obs_source));
 	struct obs_scene  *scene  = scene_create(NULL, source);
@@ -110,6 +110,9 @@ obs_scene_t obs_scene_create(void)
 		return NULL;
 	}
 
+	source->name  = bstrdup(name);
+	source->type  = SOURCE_SCENE;
+
 	scene->source = source;
 	obs_source_init(source, NULL, &scene_info);
 	memcpy(&source->callbacks, &scene_info, sizeof(struct source_info));
@@ -127,6 +130,14 @@ obs_source_t obs_scene_getsource(obs_scene_t scene)
 	return scene->source;
 }
 
+obs_scene_t obs_scene_fromsource(obs_source_t source)
+{
+	if (source->type != SOURCE_SCENE)
+		return NULL;
+
+	return source->data;
+}
+
 obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source)
 {
 	struct obs_scene_item *item = bmalloc(sizeof(struct obs_scene_item));

+ 3 - 3
libobs/obs-source.c

@@ -954,11 +954,11 @@ void obs_source_setname(obs_source_t source, const char *name)
 	source->name = bstrdup(name);
 }
 
-void obs_source_getid(obs_source_t source, enum obs_source_type *type,
+void obs_source_gettype(obs_source_t source, enum obs_source_type *type,
 		const char **id)
 {
-	*type = source->type;
-	*id   = source->callbacks.id;
+	if (type) *type = source->type;
+	if (id)   *id   = source->callbacks.id;
 }
 
 static inline void render_filter_bypass(obs_source_t target, effect_t effect,

+ 6 - 0
libobs/obs.c

@@ -433,11 +433,17 @@ media_t obs_media(void)
 
 bool obs_add_source(obs_source_t source)
 {
+	struct calldata params = {0};
+
 	pthread_mutex_lock(&obs->data.sources_mutex);
 	da_push_back(obs->data.sources, &source);
 	obs_source_addref(source);
 	pthread_mutex_unlock(&obs->data.sources_mutex);
 
+	calldata_setptr(&params, "source", source);
+	signal_handler_signal(obs->signals, "source-add", &params);
+	calldata_free(&params);
+
 	return true;
 }
 

+ 5 - 2
libobs/obs.h

@@ -398,7 +398,7 @@ EXPORT const char *obs_source_getname(obs_source_t source);
 EXPORT void obs_source_setname(obs_source_t source, const char *name);
 
 /** Gets the source type and identifier */
-EXPORT void obs_source_getid(obs_source_t source, enum obs_source_type *type,
+EXPORT void obs_source_gettype(obs_source_t source, enum obs_source_type *type,
 		const char **id);
 
 /** Returns the signal handler for a source */
@@ -444,12 +444,15 @@ EXPORT void obs_source_process_filter(obs_source_t filter,
  *   A scene is a source which is a container of other sources with specific
  * display oriantations.  Scenes can also be used like any other source.
  */
-EXPORT obs_scene_t obs_scene_create(void);
+EXPORT obs_scene_t obs_scene_create(const char *name);
 EXPORT void        obs_scene_destroy(obs_scene_t scene);
 
 /** Gets the scene's source context */
 EXPORT obs_source_t obs_scene_getsource(obs_scene_t scene);
 
+/** Gets the scene from its source, or NULL if not a scene */
+EXPORT obs_scene_t obs_scene_fromsource(obs_source_t source);
+
 /** Adds/creates a new scene item for a source */
 EXPORT obs_sceneitem_t obs_scene_add(obs_scene_t scene, obs_source_t source);
 

+ 41 - 5
obs/window-main-basic.cpp

@@ -22,6 +22,37 @@
 #include "window-settings-basic.hpp"
 #include "window-main-basic.hpp"
 
+void OBSBasic::SceneAdded(obs_source_t source)
+{
+	const char *name  = obs_source_getname(source);
+	obs_scene_t scene = obs_scene_fromsource(source);
+	scenes->Append(wxString(name, wxConvUTF8), scene);
+}
+
+void OBSBasic::SourceAdded(void *data, calldata_t params)
+{
+	OBSBasic *window = (OBSBasic*)data;
+
+	obs_source_t source;
+	calldata_getptr(params, "source", (void**)&source);
+
+	obs_source_type type;
+	obs_source_gettype(source, &type, NULL);
+
+	if (type == SOURCE_SCENE)
+		window->SceneAdded(source);
+}
+
+void OBSBasic::SourceDestroyed(void *data, calldata_t params)
+{
+	OBSBasic *window = (OBSBasic*)data;
+	obs_source_t source;
+
+	calldata_getptr(params, "source", (void**)&source);
+
+	/* TODO */
+}
+
 bool OBSBasic::Init()
 {
 	if (!obs_startup())
@@ -29,6 +60,16 @@ bool OBSBasic::Init()
 	if (!InitGraphics())
 		return false;
 
+	signal_handler_connect(obs_signalhandler(), "source-add",
+			OBSBasic::SourceAdded, this);
+	signal_handler_connect(obs_signalhandler(), "source-destroy",
+			OBSBasic::SourceDestroyed, this);
+
+	//obs_scene_t scene = obs_scene_create("test scene");
+	//obs_add_source(obs_scene_getsource(scene));
+
+	//obs_load_module("test-input");
+
 	return true;
 }
 
@@ -67,11 +108,6 @@ bool OBSBasic::InitGraphics()
 	return true;
 }
 
-bool OBSBasic::AddScene(const char *name)
-{
-	return false;
-}
-
 void OBSBasic::OnClose(wxCloseEvent &event)
 {
 	wxGetApp().ExitMainLoop();

+ 5 - 2
obs/window-main-basic.hpp

@@ -25,6 +25,11 @@
 using namespace std;
 
 class OBSBasic : public OBSBasicBase {
+	void SceneAdded(obs_source_t scene);
+
+	static void SourceAdded(void *data, calldata_t params);
+	static void SourceDestroyed(void *data, calldata_t params);
+
 	bool InitGraphics();
 
 	void NewProject();
@@ -60,8 +65,6 @@ public:
 
 	bool Init();
 
-	bool AddScene(const char *name);
-
 	inline wxPanel *GetPreviewPanel() {return previewPanel;}
 	inline wxSizer *GetPreviewContainer() {return previewContainer;}
 };

+ 1 - 1
test/osx/test.mm

@@ -163,7 +163,7 @@ static void test()
 
 		/* ------------------------------------------------------ */
 		/* create scene and add source to scene (twice) */
-		SceneContext scene{obs_scene_create()};
+		SceneContext scene{obs_scene_create("test scene")};
 		if (!scene)
 			throw "Couldn't create scene";
 

+ 1 - 1
test/win/test.cpp

@@ -167,7 +167,7 @@ int WINAPI WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine,
 
 		/* ------------------------------------------------------ */
 		/* create scene and add source to scene (twice) */
-		SceneContext scene = obs_scene_create();
+		SceneContext scene = obs_scene_create("test scene");
 		if (!scene)
 			throw "Couldn't create scene";