瀏覽代碼

libobs: Add scene item hotkeys

Palana 10 年之前
父節點
當前提交
23a2228142
共有 6 個文件被更改,包括 100 次插入0 次删除
  1. 10 0
      libobs/obs-hotkey.c
  2. 3 0
      libobs/obs-hotkey.h
  3. 2 0
      libobs/obs-internal.h
  4. 79 0
      libobs/obs-scene.c
  5. 2 0
      libobs/obs-scene.h
  6. 4 0
      libobs/obs.c

+ 10 - 0
libobs/obs-hotkey.c

@@ -1507,3 +1507,13 @@ void obs_hotkeys_set_audio_hotkeys_translations(
 	SET_T(push_to_talk);
 #undef SET_T
 }
+
+void obs_hotkeys_set_sceneitem_hotkeys_translations(
+		const char *show, const char *hide)
+{
+#define SET_T(n) bfree(obs->hotkeys.sceneitem_##n); \
+	obs->hotkeys.sceneitem_##n = bstrdup(n)
+	SET_T(show);
+	SET_T(hide);
+#undef SET_T
+}

+ 3 - 0
libobs/obs-hotkey.h

@@ -124,6 +124,9 @@ EXPORT void obs_hotkeys_set_audio_hotkeys_translations(
 		const char *mute, const char *unmute,
 		const char *push_to_mute, const char *push_to_talk);
 
+EXPORT void obs_hotkeys_set_sceneitem_hotkeys_translations(
+		const char *show, const char *hide);
+
 /* registering hotkeys (giving hotkeys a name and a function) */
 
 typedef void (*obs_hotkey_func)(void *data,

+ 2 - 0
libobs/obs-internal.h

@@ -303,6 +303,8 @@ struct obs_core_hotkeys {
 	char                            *unmute;
 	char                            *push_to_mute;
 	char                            *push_to_talk;
+	char                            *sceneitem_show;
+	char                            *sceneitem_hide;
 };
 
 struct obs_core {

+ 79 - 0
libobs/obs-scene.c

@@ -555,6 +555,82 @@ void obs_scene_enum_items(obs_scene_t *scene,
 	pthread_mutex_unlock(&scene->mutex);
 }
 
+static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
+{
+	long owners = si->ref;
+	while (owners > 0) {
+		if (os_atomic_compare_swap_long(&si->ref, owners, owners + 1))
+			return si;
+
+		owners = si->ref;
+	}
+	return NULL;
+}
+
+static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
+		obs_hotkey_t *hotkey, bool pressed)
+{
+	UNUSED_PARAMETER(id);
+	UNUSED_PARAMETER(hotkey);
+
+	obs_sceneitem_t *si = sceneitem_get_ref(data);
+	if (pressed && si && !si->visible) {
+		obs_sceneitem_set_visible(si, true);
+		obs_sceneitem_release(si);
+		return true;
+	}
+
+	obs_sceneitem_release(si);
+	return false;
+}
+
+static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
+		obs_hotkey_t *hotkey, bool pressed)
+{
+	UNUSED_PARAMETER(id);
+	UNUSED_PARAMETER(hotkey);
+
+	obs_sceneitem_t *si = sceneitem_get_ref(data);
+	if (pressed && si && si->visible) {
+		obs_sceneitem_set_visible(si, false);
+		obs_sceneitem_release(si);
+		return true;
+	}
+
+	obs_sceneitem_release(si);
+	return false;
+}
+
+static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
+		const char *name)
+{
+	struct dstr show = {0};
+	struct dstr hide = {0};
+	struct dstr show_desc = {0};
+	struct dstr hide_desc = {0};
+
+	dstr_copy(&show, "libobs.show_scene_item.%1");
+	dstr_replace(&show, "%1", name);
+	dstr_copy(&hide, "libobs.hide_scene_item.%1");
+	dstr_replace(&hide, "%1", name);
+
+	dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
+	dstr_replace(&show_desc, "%1", name);
+	dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
+	dstr_replace(&hide_desc, "%1", name);
+
+	item->toggle_visibility = obs_hotkey_pair_register_source(scene->source,
+			show.array, show_desc.array,
+			hide.array, hide_desc.array,
+			hotkey_show_sceneitem, hotkey_hide_sceneitem,
+			item, item);
+
+	dstr_free(&show);
+	dstr_free(&hide);
+	dstr_free(&show_desc);
+	dstr_free(&hide_desc);
+}
+
 obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
 {
 	struct obs_scene_item *last;
@@ -602,6 +678,8 @@ obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
 
 	pthread_mutex_unlock(&scene->mutex);
 
+	init_hotkeys(scene, item, obs_source_get_name(source));
+
 	calldata_set_ptr(&params, "scene", scene);
 	calldata_set_ptr(&params, "item", item);
 	signal_handler_signal(scene->source->context.signals, "item_add",
@@ -614,6 +692,7 @@ obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
 static void obs_sceneitem_destroy(obs_sceneitem_t *item)
 {
 	if (item) {
+		obs_hotkey_pair_unregister(item->toggle_visibility);
 		if (item->source)
 			obs_source_release(item->source);
 		bfree(item);

+ 2 - 0
libobs/obs-scene.h

@@ -49,6 +49,8 @@ struct obs_scene_item {
 	uint32_t              bounds_align;
 	struct vec2           bounds;
 
+	obs_hotkey_pair_id    toggle_visibility;
+
 	/* would do **prev_next, but not really great for reordering */
 	struct obs_scene_item *prev;
 	struct obs_scene_item *next;

+ 4 - 0
libobs/obs.c

@@ -646,6 +646,8 @@ static inline bool obs_init_hotkeys(void)
 	hotkeys->unmute = bstrdup("Unmute");
 	hotkeys->push_to_mute = bstrdup("Push-to-mute");
 	hotkeys->push_to_talk = bstrdup("Push-to-talk");
+	hotkeys->sceneitem_show = bstrdup("Show '%1'");
+	hotkeys->sceneitem_hide = bstrdup("Hide '%1'");
 
 	if (!obs_hotkeys_platform_init(hotkeys))
 		return false;
@@ -695,6 +697,8 @@ static inline void obs_free_hotkeys(void)
 	bfree(hotkeys->unmute);
 	bfree(hotkeys->push_to_mute);
 	bfree(hotkeys->push_to_talk);
+	bfree(hotkeys->sceneitem_show);
+	bfree(hotkeys->sceneitem_hide);
 
 	obs_hotkey_name_map_free();