Fix unintended memory free issue
[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             internal bool _ownership;
99
100             public SafeCommandListHandle(IntPtr handle)
101                 : base(handle, true)
102             {
103                 _ownership = true;
104             }
105
106             public SafeCommandListHandle()
107                 : base(IntPtr.Zero, true)
108             {
109                 _ownership = true;
110             }
111
112             public override bool IsInvalid
113             {
114                 get { return this.handle == IntPtr.Zero; }
115             }
116
117             protected override bool ReleaseHandle()
118             {
119                 if (_ownership && !IsInvalid)
120                 {
121                     Interop.VoiceControl.ErrorCode error = VcCmdListDestroy(this.handle, false);
122                     if (error != Interop.VoiceControl.ErrorCode.None)
123                     {
124                         Log.Error(VoiceControl.LogTag, "Destroy Failed with error " + error);
125                     }
126                 }
127                 this.SetHandle(IntPtr.Zero);
128                 return true;
129             }
130         }
131
132         internal sealed class SafeCommandHandle : SafeHandle
133         {
134             internal bool _ownership;
135
136             public SafeCommandHandle(IntPtr handle)
137                 : base(handle, true)
138             {
139                 _ownership = true;
140             }
141
142             public SafeCommandHandle()
143                 : base(IntPtr.Zero, true)
144             {
145                 _ownership = true;
146             }
147
148             public override bool IsInvalid
149             {
150                 get { return this.handle == IntPtr.Zero; }
151             }
152
153             protected override bool ReleaseHandle()
154             {
155                 Interop.VoiceControl.ErrorCode error = VoiceControl.ErrorCode.None;
156                 if (_ownership && !IsInvalid)
157                 {
158                     error = VcCmdDestroy(this.handle);
159                     if (error != Interop.VoiceControl.ErrorCode.None)
160                     {
161                         Log.Error(VoiceControl.LogTag, "Destroy Failed with error " + error);
162                     }
163                 }
164                 this.SetHandle(IntPtr.Zero);
165                 return true;
166             }
167         }
168     }
169 }