From 5288467aeb6b022cf6ce16386a3c34df2425869d Mon Sep 17 00:00:00 2001 From: jp9000 Date: Mon, 10 Mar 2014 13:39:51 -0700 Subject: [PATCH] Ensure names are valid Ensure that a source has a valid name. Duplicates aren't a big deal internally, but sources without a name are probably something that should be avoided. Made is so that if a source is programmatically created without a name, it's assigned an index based name. In the main basic-mode window, made it check to make sure the name was valid as well. --- build/data/obs-studio/locale/en.txt | 4 +++- libobs/obs-internal.h | 2 ++ libobs/obs-scene.c | 4 ++-- libobs/obs-source.c | 15 ++++++++++++++- obs/window-basic-main.cpp | 16 ++++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/build/data/obs-studio/locale/en.txt b/build/data/obs-studio/locale/en.txt index db3007f2e..fc7e16028 100644 --- a/build/data/obs-studio/locale/en.txt +++ b/build/data/obs-studio/locale/en.txt @@ -18,6 +18,8 @@ MainWindow.AddSourceDlg.Text="Please enter the name of the source" MainWindow.NameExists.Title="Name already exists" MainWindow.NameExists.Text="The name is already in use by another source." +MainWindow.NoNameEntered="Please enter a valid name" + MainWindow.Exit="Exit" MainWindow.Lock="Lock Preview" MainWindow.Preview="Enable Preview" @@ -50,7 +52,7 @@ Settings.Video.DisableAeroWindows="Disable Aero (Windows only)" Settings.Video.FPS="FPS:" Settings.Video.FPS.Common="Common FPS Values" Settings.Video.FPS.Integer="Integer FPS Value" -Settings.VIdeo.FPS.Fraction="Fractional FPS Value" +Settings.Video.FPS.Fraction="Fractional FPS Value" Settings.Video.FPS.Nanoseconds="Frame Interval (nanoseconds)" Settings.Video.FPS.Numerator="Numerator:" Settings.Video.FPS.Denominator="Denominator:" diff --git a/libobs/obs-internal.h b/libobs/obs-internal.h index a3b4ddf70..c3f2414ba 100644 --- a/libobs/obs-internal.h +++ b/libobs/obs-internal.h @@ -141,6 +141,8 @@ struct obs_core_data { struct obs_view main_view; + long long unnamed_index; + volatile bool valid; }; diff --git a/libobs/obs-scene.c b/libobs/obs-scene.c index 85b6e4840..7697ba8ab 100644 --- a/libobs/obs-scene.c +++ b/libobs/obs-scene.c @@ -202,7 +202,7 @@ static const char *obs_scene_signals[] = { NULL }; - +void source_init_name(struct obs_source *source, const char *name); obs_scene_t obs_scene_create(const char *name) { @@ -229,7 +229,7 @@ obs_scene_t obs_scene_create(const char *name) return NULL; } - source->name = bstrdup(name); + source_init_name(source, name); scene->source = source; obs_source_init(source, &scene_info); diff --git a/libobs/obs-source.c b/libobs/obs-source.c index b158e97b9..fb0e6f859 100644 --- a/libobs/obs-source.c +++ b/libobs/obs-source.c @@ -151,6 +151,18 @@ static inline void obs_source_dosignal(struct obs_source *source, calldata_free(&data); } +void source_init_name(struct obs_source *source, const char *name) +{ + if (!name || !*name) { + struct dstr unnamed = {0}; + dstr_printf(&unnamed, "__unnamed%004lld", + obs->data.unnamed_index++); + source->name = unnamed.array; + } else { + source->name = bstrdup(name); + } +} + obs_source_t obs_source_create(enum obs_source_type type, const char *id, const char *name, obs_data_t settings) { @@ -167,7 +179,8 @@ obs_source_t obs_source_create(enum obs_source_type type, const char *id, if (!obs_source_init_handlers(source)) goto fail; - source->name = bstrdup(name); + source_init_name(source, name); + source->settings = obs_data_newref(settings); source->data = info->create(source->settings, source); diff --git a/obs/window-basic-main.cpp b/obs/window-basic-main.cpp index 5000f7cad..4f33b9de9 100644 --- a/obs/window-basic-main.cpp +++ b/obs/window-basic-main.cpp @@ -586,6 +586,14 @@ void OBSBasic::on_actionAddScene_triggered() name); if (accepted) { + if (name.empty()) { + QMessageBox::information(this, + QTStr("MainWindow.NoNameEntered"), + QTStr("MainWindow.NoNameEntered")); + on_actionAddScene_triggered(); + return; + } + obs_source_t source = obs_get_source_by_name(name.c_str()); if (source) { QMessageBox::information(this, @@ -661,9 +669,17 @@ void OBSBasic::AddSource(obs_scene_t scene, const char *id) if (!accepted) break; + if (name.empty()) { + QMessageBox::information(this, + QTStr("MainWindow.NoNameEntered"), + QTStr("MainWindow.NoNameEntered")); + continue; + } + obs_source_t source = obs_get_source_by_name(name.c_str()); if (!source) { success = true; + break; } else { QMessageBox::information(this, QTStr("MainWindow.NameExists.Title"),