[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Remoting / MediaController / MediaControlServer.Events.cs
1 /*
2  * Copyright (c) 2018 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 Tizen.Applications;
18 using System;
19 using System.Collections.Generic;
20 using Native = Interop.MediaControllerServer;
21
22 namespace Tizen.Multimedia.Remoting
23 {
24     public static partial class MediaControlServer
25     {
26         private static Native.PlaybackStateCommandReceivedCallback _playbackCommandCallback;
27         private static Native.PlaybackActionCommandReceivedCallback _playbackActionCommandCallback;
28         private static Native.PlaybackPositionCommandReceivedCallback _playbackPositionCommandCallback;
29         private static Native.PlaylistCommandReceivedCallback _playlistCommandCallback;
30         private static Native.ShuffleModeCommandReceivedCallback _shuffleModeCommandCallback;
31         private static Native.RepeatModeCommandReceivedCallback _repeatModeCommandCallback;
32         private static Native.SimpleCommandReceivedCallback _subtitleModeCommandCallback;
33         private static Native.SimpleCommandReceivedCallback _mode360CommandCallback;
34         private static Native.DisplayModeCommandReceivedCallback _displayModeCommandCallback;
35         private static Native.DisplayRotationCommandReceivedCallback _displayRotationCommandCallback;
36
37         private static Native.CustomCommandReceivedCallback _customCommandCallback;
38         private static Native.SearchCommandReceivedCallback _searchCommandCallback;
39         private static Native.CommandCompletedCallback _commandCompletedCallback;
40
41
42         /// <summary>
43         /// Occurs when a client sends playback command.
44         /// </summary>
45         /// <since_tizen> 4 </since_tizen>
46         [Obsolete("Please do not use! This will be deprecated. Please use PlaybackActionCommandReceived instead.")]
47         public static event EventHandler<PlaybackCommandReceivedEventArgs> PlaybackCommandReceived;
48
49         /// <summary>
50         /// Occurs when a client sends playback command.
51         /// </summary>
52         /// <since_tizen> 5 </since_tizen>
53         public static event EventHandler<PlaybackActionCommandReceivedEventArgs> PlaybackActionCommandReceived;
54
55         /// <summary>
56         /// Occurs when a client sends playback position command.
57         /// </summary>
58         /// <since_tizen> 5 </since_tizen>
59         public static event EventHandler<PlaybackPositionCommandReceivedEventArgs> PlaybackPositionCommandReceived;
60
61         /// <summary>
62         /// Occurs when a client sends playlist command.
63         /// </summary>
64         /// <since_tizen> 5 </since_tizen>
65         public static event EventHandler<PlaylistCommandReceivedEventArgs> PlaylistCommandReceived;
66
67         /// <summary>
68         /// Occurs when a client sends shuffle mode command.
69         /// </summary>
70         /// <since_tizen> 5 </since_tizen>
71         public static event EventHandler<ShuffleModeCommandReceivedEventArgs> ShuffleModeCommandReceived;
72
73         /// <summary>
74         /// Occurs when a client sends repeat mode command.
75         /// </summary>
76         /// <since_tizen> 5 </since_tizen>
77         public static event EventHandler<RepeatModeCommandReceivedEventArgs> RepeatModeCommandReceived;
78
79         /// <summary>
80         /// Occurs when a client sends subtitle mode command.
81         /// </summary>
82         /// <since_tizen> 6 </since_tizen>
83         public static event EventHandler<SubtitleModeCommandReceivedEventArgs> SubtitleModeCommandReceived;
84
85         /// <summary>
86         /// Occurs when a client sends mode 360 command.
87         /// </summary>
88         /// <since_tizen> 6 </since_tizen>
89         public static event EventHandler<Mode360CommandReceivedEventArgs> Mode360CommandReceived;
90
91         /// <summary>
92         /// Occurs when a client sends display mode command.
93         /// </summary>
94         /// <since_tizen> 6 </since_tizen>
95         public static event EventHandler<DisplayModeCommandReceivedEventArgs> DisplayModeCommandReceived;
96
97         /// <summary>
98         /// Occurs when a client sends display rotation command.
99         /// </summary>
100         /// <since_tizen> 6 </since_tizen>
101         public static event EventHandler<DisplayRotationCommandReceivedEventArgs> DisplayRotationCommandReceived;
102
103         /// <summary>
104         /// Occurs when a client sends custom command.
105         /// </summary>
106         /// <since_tizen> 5 </since_tizen>
107         public static event EventHandler<CustomCommandReceivedEventArgs> CustomCommandReceived;
108
109         /// <summary>
110         /// Occurs when a client sends search command.
111         /// </summary>
112         /// <since_tizen> 5 </since_tizen>
113         public static event EventHandler<SearchCommandReceivedEventArgs> SearchCommandReceived;
114
115         /// <summary>
116         /// Occurs when a client sends custom command.
117         /// </summary>
118         /// <since_tizen> 5 </since_tizen>
119         internal static event EventHandler<CommandCompletedEventArgs> CommandCompleted;
120
121         private static void RegisterPlaybackActionCommandReceivedEvent()
122         {
123             _playbackActionCommandCallback = (clientName, requestId, playbackCommand, _) =>
124             {
125                 // SendPlaybackCommand doesn't use requestId. It'll be removed in Level 7.
126                 if (requestId == null)
127                 {
128 #pragma warning disable CS0618 // Type or member is obsolete
129                     PlaybackCommandReceived?.Invoke(null, new PlaybackCommandReceivedEventArgs(clientName, playbackCommand.ToPublic()));
130 #pragma warning restore CS0618 // Type or member is obsolete
131                 }
132                 else
133                 {
134                     var command = new PlaybackCommand(playbackCommand.ToPublic());
135                     command.SetResponseInformation(clientName, requestId);
136
137                     PlaybackActionCommandReceived?.Invoke(null, new PlaybackActionCommandReceivedEventArgs(command));
138                 }
139             };
140             Native.SetPlaybackActionCommandReceivedCb(Handle, _playbackActionCommandCallback).
141                 ThrowIfError("Failed to init PlaybackActionCommandReceived event.");
142         }
143
144         private static void RegisterPlaybackPositionCommandReceivedEvent()
145         {
146             _playbackPositionCommandCallback = (clientName, requestId, playbackPosition, _) =>
147             {
148                 var command = new PlaybackPositionCommand(playbackPosition);
149                 command.SetResponseInformation(clientName, requestId);
150
151                 PlaybackPositionCommandReceived?.Invoke(null, new PlaybackPositionCommandReceivedEventArgs(command));
152             };
153             Native.SetPlaybackPosotionCommandReceivedCb(Handle, _playbackPositionCommandCallback).
154                 ThrowIfError("Failed to init PlaybackPositionCommandReceived event.");
155         }
156
157         private static void RegisterPlaylistCommandReceivedEvent()
158         {
159             _playlistCommandCallback = (clientName, requestId, playlistName, index, playbackCommand, playbackPosition, _) =>
160             {
161                 var command = new PlaylistCommand(playbackCommand.ToPublic(), playlistName, index, playbackPosition);
162                 command.SetResponseInformation(clientName, requestId);
163
164                 PlaylistCommandReceived?.Invoke(null, new PlaylistCommandReceivedEventArgs(command));
165             };
166             Native.SetPlaylistCommandReceivedCb(Handle, _playlistCommandCallback).
167                 ThrowIfError("Failed to init PlaylistCommandReceived event.");
168         }
169
170         private static void RegisterShuffleModeCommandReceivedEvent()
171         {
172             _shuffleModeCommandCallback = (clientName, requestId, mode, _) =>
173             {
174                 var command = new ShuffleModeCommand(mode == MediaControllerNativeShuffleMode.On ? true : false);
175                 command.SetResponseInformation(clientName, requestId);
176
177                 ShuffleModeCommandReceived?.Invoke(null, new ShuffleModeCommandReceivedEventArgs(command));
178             };
179             Native.SetShuffleModeCommandReceivedCb(Handle, _shuffleModeCommandCallback).
180                 ThrowIfError("Failed to init ShuffleModeCommandReceived event.");
181         }
182
183         private static void RegisterRepeatModeCommandReceivedEvent()
184         {
185             _repeatModeCommandCallback = (clientName, requestId, mode, _) =>
186             {
187                 var command = new RepeatModeCommand(mode.ToPublic());
188                 command.SetResponseInformation(clientName, requestId);
189
190                 RepeatModeCommandReceived?.Invoke(null, new RepeatModeCommandReceivedEventArgs(command));
191             };
192             Native.SetRepeatModeCommandReceivedCb(Handle, _repeatModeCommandCallback).
193                 ThrowIfError("Failed to init RepeatModeCommandReceived event.");
194         }
195
196         private static void RegisterSubtitleModeCommandReceivedEvent()
197         {
198             _subtitleModeCommandCallback = (clientName, requestId, isEnabled, _) =>
199             {
200                 var command = new SubtitleModeCommand(isEnabled);
201                 command.SetResponseInformation(clientName, requestId);
202
203                 SubtitleModeCommandReceived?.Invoke(null, new SubtitleModeCommandReceivedEventArgs(command));
204             };
205             Native.SetSubtitleModeCommandReceivedCb(Handle, _subtitleModeCommandCallback).
206                 ThrowIfError("Failed to init SubtitleModeCommandReceived event.");
207         }
208
209         private static void RegisterMode360CommandReceivedEvent()
210         {
211             _mode360CommandCallback = (clientName, requestId, isEnabled, _) =>
212             {
213                 var command = new Mode360Command(isEnabled);
214                 command.SetResponseInformation(clientName, requestId);
215
216                 Mode360CommandReceived?.Invoke(null, new Mode360CommandReceivedEventArgs(command));
217             };
218             Native.SetMode360CommandReceivedCb(Handle, _mode360CommandCallback).
219                 ThrowIfError("Failed to init Mode360CommandReceived event.");
220         }
221
222         private static void RegisterDisplayModeCommandReceivedEvent()
223         {
224             _displayModeCommandCallback = (clientName, requestId, mode, _) =>
225             {
226                 var command = new DisplayModeCommand(mode.ToPublic());
227                 command.SetResponseInformation(clientName, requestId);
228
229                 DisplayModeCommandReceived?.Invoke(null, new DisplayModeCommandReceivedEventArgs(command));
230             };
231             Native.SetDisplayModeCommandReceivedCb(Handle, _displayModeCommandCallback).
232                 ThrowIfError("Failed to init DisplayModeCommandReceived event.");
233         }
234
235         private static void RegisterDisplayRotationCommandReceivedEvent()
236         {
237             _displayRotationCommandCallback = (clientName, requestId, rotation, _) =>
238             {
239                 var command = new DisplayRotationCommand(rotation.ToPublic());
240                 command.SetResponseInformation(clientName, requestId);
241
242                 DisplayRotationCommandReceived?.Invoke(null, new DisplayRotationCommandReceivedEventArgs(command));
243             };
244             Native.SetDisplayRotationCommandReceivedCb(Handle, _displayRotationCommandCallback).
245                 ThrowIfError("Failed to init DisplayRotationCommandReceived event.");
246         }
247
248         private static void RegisterCustomCommandReceivedEvent()
249         {
250             _customCommandCallback = (clientName, requestId, customCommand, bundleHandle, _) =>
251             {
252                 CustomCommand command = null;
253                 if (bundleHandle != IntPtr.Zero)
254                 {
255                     command = new CustomCommand(customCommand, new Bundle(new SafeBundleHandle(bundleHandle, true)));
256                 }
257                 else
258                 {
259                     command = new CustomCommand(customCommand);
260                 }
261
262                 command.SetResponseInformation(clientName, requestId);
263
264                 CustomCommandReceived?.Invoke(null, new CustomCommandReceivedEventArgs(command));
265             };
266             Native.SetCustomCommandReceivedCb(Handle, _customCommandCallback).
267                 ThrowIfError("Failed to init CustomCommandReceived event.");
268         }
269
270         private static SearchCommand CreateSearchCommandReceivedEventArgs(IntPtr searchHandle)
271         {
272             var searchConditions = new List<MediaControlSearchCondition>();
273
274             Native.SearchItemCallback searchItemCallback = (type, category, keyword, bundleHandle, _) =>
275             {
276                 Bundle bundle = null;
277
278                 if (bundleHandle != IntPtr.Zero)
279                 {
280                     bundle = new Bundle(new SafeBundleHandle(bundleHandle, true));
281                 }
282
283                 searchConditions.Add(new MediaControlSearchCondition(type, category, keyword, bundle));
284
285                 return true;
286             };
287
288             Native.ForeachSearchCondition(searchHandle, searchItemCallback).
289                 ThrowIfError("Failed to get search items.");
290
291             return new SearchCommand(searchConditions, searchHandle);
292         }
293
294         private static void RegisterSearchCommandReceivedEvent()
295         {
296             _searchCommandCallback = (clientName, requestId, searchHandle, _) =>
297             {
298                 var command = CreateSearchCommandReceivedEventArgs(searchHandle);
299
300                 command.SetResponseInformation(clientName, requestId);
301
302                 SearchCommandReceived?.Invoke(null, new SearchCommandReceivedEventArgs(command));
303             };
304             Native.SetSearchCommandReceivedCb(Handle, _searchCommandCallback).
305                 ThrowIfError("Failed to init SearchCommandReceived event.");
306         }
307
308         private static void RegisterCommandCompletedEvent()
309         {
310             _commandCompletedCallback = (clientName, requestId, result, bundleHandle, _) =>
311             {
312                 if (bundleHandle != IntPtr.Zero)
313                 {
314                     CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result, new Bundle(new SafeBundleHandle(bundleHandle, true))));
315                 }
316                 else
317                 {
318                     CommandCompleted?.Invoke(null, new CommandCompletedEventArgs(requestId, result));
319                 }
320             };
321             Native.SetEventReceivedCb(Handle, _commandCompletedCallback).
322                 ThrowIfError("Failed to init RegisterEventCompletedEvent.");
323         }
324     }
325 }