[VoiceControl] Base Code
[platform/core/csapi/uix-voice-control.git] / Tizen.Uix.VoiceControl / Interop / Interop.VoiceControlCommand.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
18 using System;
19 using System.Runtime.InteropServices;
20 using Tizen;
21 using Tizen.Uix.VoiceControl;
22
23 /// <summary>
24 /// Partial Interop Class
25 /// </summary>
26 internal static partial class Interop
27 {
28     /// <summary>
29     /// VoiceControlCommand Interop Class
30     /// </summary>
31     internal static class VoiceControlCommand
32     {
33         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_create")]
34         internal static extern Interop.VoiceControl.ErrorCode VcCmdListCreate(out SafeCommandListHandle cmdList);
35
36         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_destroy")]
37         internal static extern Interop.VoiceControl.ErrorCode VcCmdListDestroy(IntPtr cmdList, bool freeCommand);
38
39         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_get_count")]
40         internal static extern Interop.VoiceControl.ErrorCode VcCmdListGetCount(SafeCommandListHandle cmdList, out int count);
41
42         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_add")]
43         internal static extern Interop.VoiceControl.ErrorCode VcCmdListAdd(SafeCommandListHandle cmdList, SafeCommandHandle vcCommand);
44
45         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_remove")]
46         internal static extern Interop.VoiceControl.ErrorCode VcCmdListRemove(SafeCommandListHandle cmdList, SafeCommandHandle vcCommand);
47
48         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_foreach_commands")]
49         internal static extern Interop.VoiceControl.ErrorCode VcCmdListForeachCommands(SafeCommandListHandle cmdList, VcCmdListCb callback, IntPtr userData);
50
51         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_first")]
52         internal static extern Interop.VoiceControl.ErrorCode VcCmdListFirst(SafeCommandListHandle cmdList);
53
54         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_last")]
55         internal static extern Interop.VoiceControl.ErrorCode VcCmdListLast(SafeCommandListHandle cmdList);
56
57         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_next")]
58         internal static extern Interop.VoiceControl.ErrorCode VcCmdListNext(SafeCommandListHandle cmdList);
59
60         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_prev")]
61         internal static extern Interop.VoiceControl.ErrorCode VcCmdListPrev(SafeCommandListHandle cmdList);
62
63         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_list_get_current")]
64         internal static extern Interop.VoiceControl.ErrorCode VcCmdListGetCurrent(SafeCommandListHandle cmdList, out SafeCommandHandle vcCommand);
65
66         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_create")]
67         internal static extern Interop.VoiceControl.ErrorCode VcCmdCreate(out SafeCommandHandle vcCommand);
68
69         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_destroy")]
70         internal static extern Interop.VoiceControl.ErrorCode VcCmdDestroy(IntPtr vcCommand);
71
72         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_set_command")]
73         internal static extern Interop.VoiceControl.ErrorCode VcCmdSetCommand(SafeCommandHandle vcCommand, string command);
74
75         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_get_command")]
76         internal static extern Interop.VoiceControl.ErrorCode VcCmdGetCommand(SafeCommandHandle vcCommand, out string command);
77
78         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_get_unfixed_command")]
79         internal static extern Interop.VoiceControl.ErrorCode VcCmdGetUnfixedCommand(SafeCommandHandle vcCommand, out string command);
80
81         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_set_type")]
82         internal static extern Interop.VoiceControl.ErrorCode VcCmdSetType(SafeCommandHandle vcCommand, CommandType type);
83
84         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_get_type")]
85         internal static extern Interop.VoiceControl.ErrorCode VcCmdGetType(SafeCommandHandle vcCommand, out int type);
86
87         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_set_format")]
88         internal static extern Interop.VoiceControl.ErrorCode VcCmdSetFormat(SafeCommandHandle vcCommand, CommandFormat format);
89
90         [DllImport(Libraries.VoiceControl, EntryPoint = "vc_cmd_get_format")]
91         internal static extern Interop.VoiceControl.ErrorCode VcCmdGetFormat(SafeCommandHandle vcCommand, out CommandFormat format);
92
93         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
94         internal delegate bool VcCmdListCb(IntPtr vcCommand, IntPtr userData);
95
96         internal sealed class SafeCommandListHandle : SafeHandle
97         {
98             public SafeCommandListHandle(IntPtr handle)
99                 : base(handle, true)
100             {
101             }
102
103             public SafeCommandListHandle()
104                 : base(IntPtr.Zero, true)
105             {
106             }
107
108             public override bool IsInvalid
109             {
110                 get { return this.handle == IntPtr.Zero; }
111             }
112
113             protected override bool ReleaseHandle()
114             {
115                 if (!IsInvalid)
116                 {
117                     Interop.VoiceControl.ErrorCode error = VcCmdListDestroy(this.handle, false);
118                     if (error != Interop.VoiceControl.ErrorCode.None)
119                     {
120                         Log.Error(VoiceControl.LogTag, "Destroy Failed with error " + error);
121                     }
122                 }
123                 this.SetHandle(IntPtr.Zero);
124                 return true;
125             }
126         }
127
128         internal sealed class SafeCommandHandle : SafeHandle
129         {
130             internal bool _ownership;
131
132             public SafeCommandHandle(IntPtr handle)
133                 : base(handle, true)
134             {
135                 _ownership = true;
136             }
137
138             public SafeCommandHandle()
139                 : base(IntPtr.Zero, true)
140             {
141                 _ownership = true;
142             }
143
144             public override bool IsInvalid
145             {
146                 get { return this.handle == IntPtr.Zero; }
147             }
148
149             protected override bool ReleaseHandle()
150             {
151                 Interop.VoiceControl.ErrorCode error = VoiceControl.ErrorCode.None;
152                 if (_ownership && !IsInvalid)
153                 {
154                     error = VcCmdDestroy(this.handle);
155                     if (error != Interop.VoiceControl.ErrorCode.None)
156                     {
157                         Log.Error(VoiceControl.LogTag, "Destroy Failed with error " + error);
158                     }
159                 }
160                 this.SetHandle(IntPtr.Zero);
161                 return true;
162             }
163         }
164     }
165 }