Merge pull request #1310 from vokama/updated-hotkey-names

libobs, UI: reset hotkey description on target rename
This commit is contained in:
Jim
2018-06-16 23:09:06 -07:00
committed by GitHub
5 changed files with 135 additions and 3 deletions

View File

@@ -79,6 +79,62 @@ obs_hotkey_t *obs_hotkey_binding_get_hotkey(obs_hotkey_binding_t *binding)
return binding->hotkey;
}
static inline bool find_id(obs_hotkey_id id, size_t *idx);
void obs_hotkey_set_name(obs_hotkey_id id, const char *name)
{
size_t idx;
if (!find_id(id, &idx))
return;
obs_hotkey_t *hotkey = &obs->hotkeys.hotkeys.array[idx];
bfree(hotkey->name);
hotkey->name = bstrdup(name);
}
void obs_hotkey_set_description(obs_hotkey_id id, const char *desc)
{
size_t idx;
if (!find_id(id, &idx))
return;
obs_hotkey_t *hotkey = &obs->hotkeys.hotkeys.array[idx];
bfree(hotkey->description);
hotkey->description = bstrdup(desc);
}
static inline bool find_pair_id(obs_hotkey_pair_id id, size_t *idx);
void obs_hotkey_pair_set_names(obs_hotkey_pair_id id,
const char *name0, const char *name1)
{
size_t idx;
obs_hotkey_pair_t pair;
if (!find_pair_id(id, &idx))
return;
pair = obs->hotkeys.hotkey_pairs.array[idx];
obs_hotkey_set_name(pair.id[0], name0);
obs_hotkey_set_name(pair.id[1], name1);
}
void obs_hotkey_pair_set_descriptions(obs_hotkey_pair_id id,
const char *desc0, const char *desc1)
{
size_t idx;
obs_hotkey_pair_t pair;
if (!find_pair_id(id, &idx))
return;
pair = obs->hotkeys.hotkey_pairs.array[idx];
obs_hotkey_set_description(pair.id[0], desc0);
obs_hotkey_set_description(pair.id[1], desc1);
}
static void hotkey_signal(const char *signal, obs_hotkey_t *hotkey)
{
calldata_t data;

View File

@@ -60,6 +60,8 @@ enum obs_hotkey_registerer_type {
};
typedef enum obs_hotkey_registerer_type obs_hotkey_registerer_t;
/* getter functions */
EXPORT obs_hotkey_id obs_hotkey_get_id(const obs_hotkey_t *key);
EXPORT const char *obs_hotkey_get_name(const obs_hotkey_t *key);
EXPORT const char *obs_hotkey_get_description(const obs_hotkey_t *key);
@@ -76,6 +78,15 @@ EXPORT obs_hotkey_id obs_hotkey_binding_get_hotkey_id(
EXPORT obs_hotkey_t *obs_hotkey_binding_get_hotkey(
obs_hotkey_binding_t *binding);
/* setter functions */
EXPORT void obs_hotkey_set_name(obs_hotkey_id id, const char *name);
EXPORT void obs_hotkey_set_description(obs_hotkey_id id, const char *desc);
EXPORT void obs_hotkey_pair_set_names(obs_hotkey_pair_id id,
const char *name0, const char *name1);
EXPORT void obs_hotkey_pair_set_descriptions(obs_hotkey_pair_id id,
const char *desc0, const char *desc1);
#ifndef SWIG
struct obs_hotkeys_translations {
const char *insert;

View File

@@ -1449,6 +1449,44 @@ static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
dstr_free(&hide_desc);
}
static void sceneitem_rename_hotkey(const obs_sceneitem_t *scene_item,
const char *new_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", new_name);
dstr_copy(&hide, "libobs.hide_scene_item.%1");
dstr_replace(&hide, "%1", new_name);
obs_hotkey_pair_set_names(scene_item->toggle_visibility,
show.array, hide.array);
dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
dstr_replace(&show_desc, "%1", new_name);
dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
dstr_replace(&hide_desc, "%1", new_name);
obs_hotkey_pair_set_descriptions(scene_item->toggle_visibility,
show_desc.array, hide_desc.array);
dstr_free(&show);
dstr_free(&hide);
dstr_free(&show_desc);
dstr_free(&hide_desc);
}
static void sceneitem_renamed(void *param, calldata_t *data)
{
obs_sceneitem_t *scene_item = param;
const char *name = calldata_string(data, "new_name");
sceneitem_rename_hotkey(scene_item, name);
}
static inline bool source_has_audio(obs_source_t *source)
{
return (source->info.output_flags &
@@ -1545,6 +1583,9 @@ static obs_sceneitem_t *obs_scene_add_internal(obs_scene_t *scene,
if (!scene->source->context.private)
init_hotkeys(scene, item, obs_source_get_name(source));
signal_handler_connect(obs_source_get_signal_handler(source), "rename",
sceneitem_renamed, item);
return item;
}
@@ -1574,6 +1615,9 @@ static void obs_sceneitem_destroy(obs_sceneitem_t *item)
obs_data_release(item->private_settings);
obs_hotkey_pair_unregister(item->toggle_visibility);
pthread_mutex_destroy(&item->actions_mutex);
signal_handler_disconnect(
obs_source_get_signal_handler(item->source),
"rename", sceneitem_renamed, item);
if (item->source)
obs_source_release(item->source);
da_free(item->audio_actions);