Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.System.MediaKey / Tizen.System / MediaKey.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
19 namespace Tizen.System
20 {
21     /// <summary>
22     /// Class for event arguments of the media key.
23     /// </summary>
24     public class MediaKeyEventArgs : EventArgs
25     {
26         /// <summary>
27         /// Enumeration for Key value.
28         /// </summary>
29         /// <since_tizen>3</since_tizen>
30         public enum KeyValue
31         {
32             /// <summary>
33             /// Play key.
34             /// </summary>
35             Play,
36
37             /// <summary>
38             /// Stop key.
39             /// </summary>
40             Stop,
41
42             /// <summary>
43             /// Pause key.
44             /// </summary>
45             Pause,
46
47             /// <summary>
48             /// Previous key.
49             /// </summary>
50             Previous,
51
52             /// <summary>
53             /// Next key.
54             /// </summary>
55             Next,
56
57             /// <summary>
58             /// Fast forward key.
59             /// </summary>
60             FastForward,
61
62             /// <summary>
63             /// Rewind key.
64             /// </summary>
65             Rewind,
66
67             /// <summary>
68             /// Play-pause key.
69             /// </summary>
70             PlayPause,
71
72             /// <summary>
73             /// Media key for earjack.
74             /// </summary>
75             Media,
76
77             /// <summary>
78             /// Unknown key.
79             /// </summary>
80             Unknown
81         }
82
83         /// <summary>
84         /// Enumeration for Key status.
85         /// </summary>
86         /// <since_tizen>3</since_tizen>
87         public enum KeyStatus
88         {
89             /// <summary>
90             /// Pressed status.
91             /// </summary>
92             Pressed,
93
94             /// <summary>
95             /// Released status.
96             /// </summary>
97             Released,
98
99             /// <summary>
100             /// Unknown status.
101             /// </summary>
102             Unknown
103         }
104
105         /// <summary>
106         /// Key value.
107         /// </summary>
108         /// <since_tizen>3</since_tizen>
109         public KeyValue Value { get; internal set; }
110
111         /// <summary>
112         /// Key status.
113         /// </summary>
114         /// <since_tizen>3</since_tizen>
115         public KeyStatus Status { get; internal set; }
116     }
117
118     /// <summary>
119     /// Class for receiving events of media keys.
120     /// </summary>
121     public static class MediaKey
122     {
123         private static EventHandler<MediaKeyEventArgs> s_eventHandler;
124         private static Interop.MediaKey.EventCallback s_callback;
125
126         private static void OnEvent(Interop.MediaKey.KeyValue value, Interop.MediaKey.KeyStatus status, IntPtr userData)
127         {
128             s_eventHandler?.Invoke(null, new MediaKeyEventArgs()
129             {
130                 Value = (MediaKeyEventArgs.KeyValue)value,
131                 Status = (MediaKeyEventArgs.KeyStatus)status
132             });
133         }
134
135         /// <summary>
136         /// Adds or removes events for all media keys.
137         /// </summary>
138         /// <since_tizen>3</since_tizen>
139         /// <exception cref="InvalidOperationException">Failed to reserve or release key.</exception>
140         public static event EventHandler<MediaKeyEventArgs> Event
141         {
142             add
143             {
144                 if (s_eventHandler == null)
145                 {
146                     if (s_callback == null)
147                         s_callback = new Interop.MediaKey.EventCallback(OnEvent);
148                     Interop.MediaKey.ErrorCode err = Interop.MediaKey.Reserve(s_callback, IntPtr.Zero);
149
150                     if (err != Interop.MediaKey.ErrorCode.None)
151                     {
152                         throw new InvalidOperationException("Failed to reserve key. err = " + err);
153                     }
154
155                 }
156                 s_eventHandler += value;
157
158             }
159             remove
160             {
161                 s_eventHandler -= value;
162                 if (s_eventHandler == null)
163                 {
164                     Interop.MediaKey.ErrorCode err = Interop.MediaKey.Release();
165
166                     if (err != Interop.MediaKey.ErrorCode.None)
167                     {
168                         throw new InvalidOperationException("Failed to release key. err = " + err);
169                     }
170                     s_callback = null;
171                 }
172             }
173         }
174     }
175 }
176