From 78326188c4dd99e87f5620d3cde50a853d934f65 Mon Sep 17 00:00:00 2001
From: Barry Rowe <barry_j_rowe@hotmail.com>
Date: Thu, 2 Feb 2023 23:40:53 -0800
Subject: [PATCH] Fix nvda switching to powershell on speak. (#14944)

---
 frontend/drivers/platform_win32.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/frontend/drivers/platform_win32.c b/frontend/drivers/platform_win32.c
index 28df117fdb..70c7191fe3 100644
--- a/frontend/drivers/platform_win32.c
+++ b/frontend/drivers/platform_win32.c
@@ -466,8 +466,13 @@ static void init_nvda(void)
       }
    }
 #endif
-   g_plat_win32_flags &= ~PLAT_WIN32_FLAG_USE_NVDA;
-   g_plat_win32_flags |=  PLAT_WIN32_FLAG_USE_POWERSHELL;
+   /* The above code is executed on each accessibility speak event, so
+    * we should only revert to powershell if nvda_lib wasn't loaded previously,
+    * and we weren't able to load it on this call, or we don't HAVE_DYLIB */
+   if ((g_plat_win32_flags & PLAT_WIN32_FLAG_USE_NVDA) && !nvda_lib) {
+      g_plat_win32_flags &= ~PLAT_WIN32_FLAG_USE_NVDA;
+      g_plat_win32_flags |=  PLAT_WIN32_FLAG_USE_POWERSHELL;
+   }
 }
 #endif