From b074c2c0ef472fc4ae5476edc6b8ea316970baf2 Mon Sep 17 00:00:00 2001 From: rc-swag <58423624+rc-swag@users.noreply.github.com> Date: Mon, 10 Feb 2025 13:50:18 +1000 Subject: [PATCH 1/2] feat(windows): add release ownership rename mutex owned Add a ReleaseOwnership member to TKeymanMutex. Rename MutexOwned to TakeOwnership. Fixes: #13167 --- windows/src/desktop/kmshell/main/initprog.pas | 4 ++-- windows/src/desktop/kmshell/startup/UfrmSplash.pas | 2 +- .../src/engine/kmcomapi/com/system/keymancontrol.pas | 4 ++-- windows/src/global/delphi/general/KeymanMutex.pas | 10 ++++++++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/windows/src/desktop/kmshell/main/initprog.pas b/windows/src/desktop/kmshell/main/initprog.pas index 3d0ea7675a..efc69cfe70 100644 --- a/windows/src/desktop/kmshell/main/initprog.pas +++ b/windows/src/desktop/kmshell/main/initprog.pas @@ -526,12 +526,12 @@ begin fmMain, fmAbout: begin // I2720 FMutex := TKeymanMutex.Create('KeymanConfiguration'); - if FMutex.MutexOwned then Main else FocusConfiguration; + if FMutex.TakeOwnership then Main else FocusConfiguration; end; fmTextEditor: begin // I2720 FMutex := TKeymanMutex.Create('KeymanTextEditor'); - if FMutex.MutexOwned then OpenTextEditor else FocusTextEditor; + if FMutex.TakeOwnership then OpenTextEditor else FocusTextEditor; end; fmBaseKeyboard: // I4169 diff --git a/windows/src/desktop/kmshell/startup/UfrmSplash.pas b/windows/src/desktop/kmshell/startup/UfrmSplash.pas index 5a6cccd529..d10379fc3e 100644 --- a/windows/src/desktop/kmshell/startup/UfrmSplash.pas +++ b/windows/src/desktop/kmshell/startup/UfrmSplash.pas @@ -220,7 +220,7 @@ begin begin FMutex := TKeymanMutex.Create('KeymanSplash'); try - if not FMutex.MutexOwned then + if not FMutex.TakeOwnership then begin FocusSplash; // I2562 Exit; diff --git a/windows/src/engine/kmcomapi/com/system/keymancontrol.pas b/windows/src/engine/kmcomapi/com/system/keymancontrol.pas index 6b429b985e..9bca81482c 100644 --- a/windows/src/engine/kmcomapi/com/system/keymancontrol.pas +++ b/windows/src/engine/kmcomapi/com/system/keymancontrol.pas @@ -281,7 +281,7 @@ function TKeymanControl.IsConfigurationOpen: WordBool; begin with TKeymanMutex.Create('KeymanConfiguration') do try - Result := MutexOwned; + Result := TakeOwnership; finally Free; end; @@ -302,7 +302,7 @@ function TKeymanControl.IsTextEditorOpen: WordBool; begin with TKeymanMutex.Create('KeymanTextEditor') do try - Result := MutexOwned; + Result := TakeOwnership; finally Free; end; diff --git a/windows/src/global/delphi/general/KeymanMutex.pas b/windows/src/global/delphi/general/KeymanMutex.pas index 4e9d92921a..4be88a189f 100644 --- a/windows/src/global/delphi/general/KeymanMutex.pas +++ b/windows/src/global/delphi/general/KeymanMutex.pas @@ -29,7 +29,8 @@ type public constructor Create(FName: string); destructor Destroy; override; - function MutexOwned: Boolean; + function TakeOwnership: Boolean; + function ReleaseOwnership: Boolean; end; implementation @@ -52,9 +53,14 @@ begin inherited Destroy; end; -function TKeymanMutex.MutexOwned: Boolean; +function TKeymanMutex.TakeOwnership: Boolean; begin Result := WaitForSingleObject(hMutex, 0) = WAIT_OBJECT_0; end; +function TKeymanMutex.ReleaseOwnership: Boolean; +begin + Result := ReleaseMutex(hMutex); +end; + end. From 96b3c4f983284695f57a924e7c672411716b8418 Mon Sep 17 00:00:00 2001 From: rc-swag <58423624+rc-swag@users.noreply.github.com> Date: Mon, 10 Feb 2025 15:00:17 +1000 Subject: [PATCH 2/2] feat(windows): review suggestions Co-authored-by: Marc Durdin --- windows/src/desktop/kmshell/main/initprog.pas | 8 ++++++-- windows/src/engine/kmcomapi/com/system/keymancontrol.pas | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/windows/src/desktop/kmshell/main/initprog.pas b/windows/src/desktop/kmshell/main/initprog.pas index efc69cfe70..27d1ea95b5 100644 --- a/windows/src/desktop/kmshell/main/initprog.pas +++ b/windows/src/desktop/kmshell/main/initprog.pas @@ -526,12 +526,16 @@ begin fmMain, fmAbout: begin // I2720 FMutex := TKeymanMutex.Create('KeymanConfiguration'); - if FMutex.TakeOwnership then Main else FocusConfiguration; + if FMutex.TakeOwnership + then Main + else FocusConfiguration; end; fmTextEditor: begin // I2720 FMutex := TKeymanMutex.Create('KeymanTextEditor'); - if FMutex.TakeOwnership then OpenTextEditor else FocusTextEditor; + if FMutex.TakeOwnership + then OpenTextEditor + else FocusTextEditor; end; fmBaseKeyboard: // I4169 diff --git a/windows/src/engine/kmcomapi/com/system/keymancontrol.pas b/windows/src/engine/kmcomapi/com/system/keymancontrol.pas index 9bca81482c..4adf198844 100644 --- a/windows/src/engine/kmcomapi/com/system/keymancontrol.pas +++ b/windows/src/engine/kmcomapi/com/system/keymancontrol.pas @@ -281,7 +281,7 @@ function TKeymanControl.IsConfigurationOpen: WordBool; begin with TKeymanMutex.Create('KeymanConfiguration') do try - Result := TakeOwnership; + Result := not TakeOwnership; finally Free; end; @@ -302,7 +302,7 @@ function TKeymanControl.IsTextEditorOpen: WordBool; begin with TKeymanMutex.Create('KeymanTextEditor') do try - Result := TakeOwnership; + Result := not TakeOwnership; finally Free; end;