Skip to content

Commit

Permalink
[NUI][AT-SPI] Fix Accessibility.Say interop (#5612)
Browse files Browse the repository at this point in the history
This is one of the oldest interops and was actually problematic for
multiple reasons; the return type didn't match and the third parameter
was expected to have exactly the same value in every invocation. While
fixing these issues, parsing the status string was moved from C++ to C#
for readability.

Co-authored-by: Artur Świgoń <[email protected]>
  • Loading branch information
aswigon and Artur Świgoń authored Oct 16, 2023
1 parent c4e0357 commit 1820fed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/Tizen.NUI/src/internal/Interop/Interop.Accessibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ internal static partial class Interop
{
internal static partial class Accessibility
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SayCallback(string status);

[DllImport(NDalicPINVOKE.Lib, EntryPoint = "csharp_dali_accessibility_say")]
[return: MarshalAs(UnmanagedType.U1)]
public static extern bool Say(string jarg1, bool jarg2, IntPtr jarg3);
public static extern void Say(string arg1_text, bool arg2_discardable, SayCallback arg3_callback);

[DllImport(NDalicPINVOKE.Lib, EntryPoint = "csharp_dali_accessibility_pause_resume")]
public static extern void PauseResume(bool jarg1);
Expand Down
32 changes: 18 additions & 14 deletions src/Tizen.NUI/src/public/Accessibility/Accessibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using Tizen.NUI.BaseComponents;
using System.Diagnostics.CodeAnalysis;

namespace Tizen.NUI.Accessibility
{
Expand Down Expand Up @@ -105,12 +106,10 @@ public static bool IsScreenReaderEnabled
/// <returns></returns>
// This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
[EditorBrowsable(EditorBrowsableState.Never)]
public static bool Say(string sentence, bool discardable)
public static void Say(string sentence, bool discardable)
{
bool ret = Interop.Accessibility.Say(sentence, discardable, Marshal.GetFunctionPointerForDelegate<Delegate>(callback));

Interop.Accessibility.Say(sentence, discardable, SayFinishedEventCallback);
if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
return ret;
}

/// <summary>
Expand Down Expand Up @@ -314,12 +313,6 @@ public enum SayFinishedState
#endregion Event, Enum, Struct, ETC

#region Private

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void SayFinishedEventCallbackType(int result);

private static SayFinishedEventCallbackType callback = SayFinishedEventCallback;

private static Interop.Accessibility.EnabledDisabledSignalHandler enabledSignalHandler = null;

private static Interop.Accessibility.EnabledDisabledSignalHandler disabledSignalHandler = null;
Expand All @@ -328,9 +321,20 @@ public enum SayFinishedState

private static Interop.Accessibility.EnabledDisabledSignalHandler screenReaderDisabledSignalHandler = null;

private static void SayFinishedEventCallback(int result)
private static readonly IReadOnlyDictionary<string, SayFinishedState> sayFinishedStateDictionary = new Dictionary<string, SayFinishedState>
{
["ReadingCancelled"] = SayFinishedState.Cancelled,
["ReadingStopped"] = SayFinishedState.Stopped,
["ReadingSkipped"] = SayFinishedState.Skipped,
["ReadingPaused"] = SayFinishedState.Paused,
["ReadingResumed"] = SayFinishedState.Resumed,
};

private static void SayFinishedEventCallback(string status)
{
var result = sayFinishedStateDictionary.GetValueOrDefault(status, SayFinishedState.Invalid);
NUILog.Debug($"sayFinishedEventCallback(res={result}) called!");

SayFinished?.Invoke(typeof(Accessibility), new SayFinishedEventArgs(result));
}

Expand All @@ -357,9 +361,9 @@ public Accessibility.SayFinishedState State
get;
}

internal SayFinishedEventArgs(int result)
internal SayFinishedEventArgs(Accessibility.SayFinishedState state)
{
State = (Accessibility.SayFinishedState)(result);
State = state;
NUILog.Debug($"SayFinishedEventArgs Constructor! State={State}");
}
}
Expand Down

0 comments on commit 1820fed

Please sign in to comment.