Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / AccessibleUtil.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Runtime.InteropServices;
19 using System.Threading.Tasks;
20
21 namespace ElmSharp.Accessible
22 {
23     /// <summary>
24     /// Enumeration for ReadingStatus.
25     /// </summary>
26     public enum ReadingStatus
27     {
28         /// <summary>
29         /// Unknown status
30         /// </summary>
31         Unknown,
32         /// <summary>
33         /// Cancelled status
34         /// </summary>
35         Cancelled,
36         /// <summary>
37         /// Stopped status
38         /// </summary>
39         Stoppped,
40         /// <summary>
41         /// Skipped status
42         /// </summary>
43         Skipped
44     }
45
46     /// <summary>
47     /// AccessibleUtil provides a method to set the reading information.
48     /// </summary>
49     public static class AccessibleUtil
50     {
51
52         static void AtspiSignalCallback(IntPtr data, string say_signal)
53         {
54             GCHandle gch = GCHandle.FromIntPtr(data);
55             TaskCompletionSource<ReadingStatus> tcs = (TaskCompletionSource<ReadingStatus>) gch.Target;
56             if (say_signal.Equals("ReadingCancelled"))
57             {
58                 tcs.SetResult(ReadingStatus.Cancelled);
59             }
60             else if (say_signal.Equals("ReadingStopped"))
61             {
62                 tcs.SetResult(ReadingStatus.Stoppped);
63             }
64             else if (say_signal.Equals("ReadingSkipped"))
65             {
66                 tcs.SetResult(ReadingStatus.Skipped);
67             }
68             else
69             {
70                 tcs.SetException(new InvalidOperationException("unknown signal : " + say_signal));
71             }
72
73             gch.Free();
74         }
75
76         /// <summary>
77         /// Reads given text by screen reader.
78         /// </summary>
79         /// <param name="text">The reading text.</param>
80         /// <param name="discardable">If true, reading can be discarded by subsequent reading requests, if false the reading must finish before next reading request can be started.</param>
81         /// <returns>Return a task with reading status.</returns>
82         public static Task<ReadingStatus> Say(string text, bool discardable)
83         {
84             var tcs = new TaskCompletionSource<ReadingStatus>();
85             GCHandle gch = GCHandle.Alloc(tcs);
86             Interop.Elementary.elm_atspi_bridge_utils_say(text, discardable, AtspiSignalCallback, GCHandle.ToIntPtr(gch));
87             return tcs.Task;
88         }
89     }
90 }