Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/fallback-warning.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ obs_frontend_warn_fallback("browser.sandbox", obs_module_text("SandboxFallback.T

- `id` is a stable name. Use `<plugin>.<fallback>`.
- The caller translates `title` and `message`.
- `cb` is called once, on the UI thread, never inside the call. Keep `param`
valid until then. `cb` can be NULL.
- Without the OBS frontend (another host of the plugin), the call logs and
calls `cb` with `true` inside the call.
- `cb` is called once. While OBS runs, it is called on the UI thread and
never inside the call. Keep `param` valid until then. `cb` can be NULL.
- Without the OBS frontend (another host of the plugin), and after OBS has
begun to shut down, the call logs and calls `cb` with `true` inside the
call.

The API reference is in
[reference-frontend-api.rst](sphinx/reference-frontend-api.rst).
Expand Down
6 changes: 3 additions & 3 deletions docs/sphinx/reference-frontend-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1023,9 +1023,9 @@ Functions
once (after the log line). At exit, an open warning closes and queued
warnings accept.

*cb* is never called inside this call when OBS runs. In a host without
the OBS frontend, this call logs, then calls *cb* with *true* inside the
call.
*cb* is never called inside this call while OBS runs. In a host without
the OBS frontend, and after OBS has begun to shut down, this call logs,
then calls *cb* with *true* inside the call.

See ``docs/fallback-warning.md`` in OBS-Studio-Rust
(obs-rust/obs-studio#2218).
Expand Down
1 change: 1 addition & 0 deletions frontend/build/frontend-fallback-warning.pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@

#include <deque>
#include <memory>
#include <mutex>
#include <string>
17 changes: 17 additions & 0 deletions frontend/utility/FallbackWarning.cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ static struct {
QPointer<OBSFallbackWarning> open;
} fw_state;

/* Any thread. Set by SetMainWindow(nullptr): no event loop will run the
* queue, so Warn() answers at once (#2369). Warn() queues under the same lock,
* so a warning is either queued before shutdown (and answered by its flush)
* or answered inside the call. */
static std::mutex fw_shutdown_mutex;
static bool fw_shut_down = false;

static const char *fw_text(const char *s)
{
return s ? s : "";
Expand Down Expand Up @@ -135,6 +142,10 @@ static void fw_pump()

void SetMainWindow(QWidget *window, const Texts &texts)
{
{
std::lock_guard<std::mutex> lock(fw_shutdown_mutex);
fw_shut_down = !window;
}
fw_state.window = window;
fw_state.texts = texts;
fw_state.ready = true;
Expand All @@ -151,6 +162,12 @@ void Warn(const char *id, const char *title, const char *message, bool can_cance
fw_log(id, message);

FwRequest r{id, fw_text(title), message, can_cancel, cb, param};
std::unique_lock<std::mutex> lock(fw_shutdown_mutex);
if (fw_shut_down) {
lock.unlock();
fw_answer(r, true);
return;
}
QMetaObject::invokeMethod(
qApp,
[r]() {
Expand Down
5 changes: 3 additions & 2 deletions frontend/utility/FallbackWarning.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ struct Texts {
void SetMainWindow(QWidget *window, const Texts &texts);

/* Any thread. Logs now. Then, on the UI thread and never inside this call,
* shows the dialog or accepts, and calls `cb` (if not NULL) once. NULL
* strings are empty. */
* shows the dialog or accepts, and calls `cb` (if not NULL) once. After
* SetMainWindow(nullptr) (shutdown) it accepts at once, inside this call.
* NULL strings are empty. */
void Warn(const char *id, const char *title, const char *message, bool can_cancel, Callback cb, void *param);

} // namespace FallbackWarning
33 changes: 33 additions & 0 deletions test/cmocka/test_fallback_warning_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,38 @@ static void shutdown_answers_everyone(void **)
assert_true(queued.answers[0].accepted);
}

/* After shutdown (SetMainWindow(nullptr)) no event loop runs the queue: a
* later warning logs and is answered at once, inside the call, accepted
* (#2369). A new main window starts the queue again. */
static void after_shutdown_answers_inside_warn(void **)
{
FallbackWarning::SetMainWindow(main_window, texts());
FallbackWarning::SetMainWindow(nullptr, texts());
settle();
logged.clear();

Caller &caller = new_caller();
FallbackWarning::Warn("test.late", "Late", "After shutdown.", true, answer, &caller);
assert_int_equal(caller.answers.size(), 1);
assert_true(caller.answers[0].accepted);
assert_int_equal(logged.size(), 1);
assert_int_equal(logged[0].level, LOG_ERROR);
settle();
assert_int_equal(caller.answers.size(), 1);

FallbackWarning::SetMainWindow(main_window, texts());
Caller &again = new_caller();
FallbackWarning::Warn("test.again", "Again", "Running.", false, answer, &again);
assert_true(again.answers.empty());
settle();
#ifdef OBS_FRONTEND_RUST
for (OBSFallbackWarning *d : dialogs()) {
click(d, "acceptButton");
}
#endif
assert_int_equal(again.answers.size(), 1);
}

/* No callback and NULL strings are allowed. */
static void null_callback_and_strings(void **)
{
Expand Down Expand Up @@ -364,6 +396,7 @@ int main(int argc, char **argv)
cmocka_unit_test_setup_teardown(waits_while_another_dialog_is_modal, setup, teardown),
cmocka_unit_test_setup_teardown(shutdown_answers_everyone, setup, teardown),
cmocka_unit_test_setup_teardown(null_callback_and_strings, setup, teardown),
cmocka_unit_test_setup_teardown(after_shutdown_answers_inside_warn, setup, teardown),
};
if (argc > 1) {
cmocka_set_test_filter(argv[1]);
Expand Down
Loading