Release 4.0.0-preview1-00271
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Adaptor.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16 using System;
17 using System.Runtime.InteropServices;
18
19 namespace Tizen.NUI
20 {
21     /// <summary>
22     /// An Adaptor object is used to initialize and control how Dali runs.
23     ///
24     /// It provides the lifecycle interface that allows the application
25     /// writer to provide their own main loop and other platform related
26     /// features.
27     ///
28     /// The Adaptor class provides a means for initialising the resources required by the Dali::Core.
29     ///
30     /// When dealing with platform events, the application writer must ensure that DALi is called in a
31     /// thread-safe manner.
32     ///
33     /// As soon as the Adaptor class is created and started, the application writer can initialise their
34     /// view objects straight away or as required by the main loop they intend to use (there is no
35     /// need to wait for an initialize signal as per the Tizen.NUI.Application class).
36     ///
37     /// </summary>
38     public class Adaptor : global::System.IDisposable
39     {
40         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
41         protected bool swigCMemOwn;
42
43         internal Adaptor(global::System.IntPtr cPtr, bool cMemoryOwn)
44         {
45             swigCMemOwn = cMemoryOwn;
46             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
47         }
48
49         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Adaptor obj)
50         {
51             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
52         }
53
54         //A Flag to check who called Dispose(). (By User or DisposeQueue)
55         private bool isDisposeQueued = false;
56         //A Flat to check if it is already disposed.
57         protected bool disposed = false;
58
59         ~Adaptor()
60         {
61             if (!isDisposeQueued)
62             {
63                 isDisposeQueued = true;
64                 DisposeQueue.Instance.Add(this);
65             }
66         }
67
68         /// <since_tizen> 4 </since_tizen>
69         public void Dispose()
70         {
71             //Throw excpetion if Dispose() is called in separate thread.
72             if (!Window.IsInstalled())
73             {
74                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
75             }
76
77             if (isDisposeQueued)
78             {
79                 Dispose(DisposeTypes.Implicit);
80             }
81             else
82             {
83                 Dispose(DisposeTypes.Explicit);
84                 System.GC.SuppressFinalize(this);
85             }
86         }
87
88         protected virtual void Dispose(DisposeTypes type)
89         {
90             if (disposed)
91             {
92                 return;
93             }
94
95             if (type == DisposeTypes.Explicit)
96             {
97                 //Called by User
98                 //Release your own managed resources here.
99                 //You should release all of your own disposable objects here.
100
101             }
102
103             //Release your own unmanaged resources here.
104             //You should not access any managed member here except static instance.
105             //because the execution order of Finalizes is non-deterministic.
106
107             if (swigCPtr.Handle != global::System.IntPtr.Zero)
108             {
109                 swigCMemOwn = false;
110                 NDalicManualPINVOKE.delete_Adaptor(swigCPtr);
111                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
112             }
113
114             disposed = true;
115         }
116
117         internal static Adaptor GetAdaptorFromPtr(global::System.IntPtr cPtr)
118         {
119             Adaptor ret = new Adaptor(cPtr, false);
120             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
121             return ret;
122         }
123
124         internal static Adaptor New(Window window)
125         {
126             Adaptor ret = new Adaptor(NDalicManualPINVOKE.Adaptor_New__SWIG_0(Window.getCPtr(window)), false);
127             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
128             return ret;
129         }
130
131         internal static Adaptor New(Window window, SWIGTYPE_p_Configuration__ContextLoss configuration)
132         {
133             Adaptor ret = new Adaptor(NDalicManualPINVOKE.Adaptor_New__SWIG_1(Window.getCPtr(window), SWIGTYPE_p_Configuration__ContextLoss.getCPtr(configuration)), false);
134             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
135             return ret;
136         }
137
138         internal static Adaptor New(Any nativeWindow, SWIGTYPE_p_Dali__RenderSurface surface)
139         {
140             Adaptor ret = new Adaptor(NDalicManualPINVOKE.Adaptor_New__SWIG_2(Any.getCPtr(nativeWindow), SWIGTYPE_p_Dali__RenderSurface.getCPtr(surface)), false);
141             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
142             return ret;
143         }
144
145         internal static Adaptor New(Any nativeWindow, SWIGTYPE_p_Dali__RenderSurface surface, SWIGTYPE_p_Configuration__ContextLoss configuration)
146         {
147             Adaptor ret = new Adaptor(NDalicManualPINVOKE.Adaptor_New__SWIG_3(Any.getCPtr(nativeWindow), SWIGTYPE_p_Dali__RenderSurface.getCPtr(surface), SWIGTYPE_p_Configuration__ContextLoss.getCPtr(configuration)), false);
148             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
149             return ret;
150         }
151
152         /// <summary>
153         /// Starts the adaptor.
154         /// </summary>
155         internal void Start()
156         {
157             NDalicManualPINVOKE.Adaptor_Start(swigCPtr);
158             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
159         }
160
161         /// <summary>
162         /// Pauses the adaptor.
163         /// </summary>
164         internal void Pause()
165         {
166             NDalicManualPINVOKE.Adaptor_Pause(swigCPtr);
167             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
168         }
169
170         /// <summary>
171         /// Resumes the adaptor, if previously paused.
172         /// </summary>
173         /// <remarks>If the adaptor is not paused, this does not do anything.</remarks>
174         internal void Resume()
175         {
176             NDalicManualPINVOKE.Adaptor_Resume(swigCPtr);
177             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
178         }
179
180         /// <summary>
181         /// Stops the adaptor.
182         /// </summary>
183         internal void Stop()
184         {
185             NDalicManualPINVOKE.Adaptor_Stop(swigCPtr);
186             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
187         }
188
189         internal bool AddIdle(SWIGTYPE_p_Dali__CallbackBase callback)
190         {
191             bool ret = NDalicManualPINVOKE.Adaptor_AddIdle(swigCPtr, SWIGTYPE_p_Dali__CallbackBase.getCPtr(callback));
192             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
193             return ret;
194         }
195
196         internal void RemoveIdle(SWIGTYPE_p_Dali__CallbackBase callback)
197         {
198             NDalicManualPINVOKE.Adaptor_RemoveIdle(swigCPtr, SWIGTYPE_p_Dali__CallbackBase.getCPtr(callback));
199             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200         }
201
202         internal void ReplaceSurface(Any nativeWindow, SWIGTYPE_p_Dali__RenderSurface surface)
203         {
204             NDalicManualPINVOKE.Adaptor_ReplaceSurface(swigCPtr, Any.getCPtr(nativeWindow), SWIGTYPE_p_Dali__RenderSurface.getCPtr(surface));
205             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
206         }
207
208         internal SWIGTYPE_p_Dali__RenderSurface GetSurface()
209         {
210             SWIGTYPE_p_Dali__RenderSurface ret = new SWIGTYPE_p_Dali__RenderSurface(NDalicManualPINVOKE.Adaptor_GetSurface(swigCPtr), false);
211             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
212             return ret;
213         }
214
215         internal Any GetNativeWindowHandle()
216         {
217             Any ret = new Any(NDalicManualPINVOKE.Adaptor_GetNativeWindowHandle(swigCPtr), true);
218             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
219             return ret;
220         }
221
222         /// <summary>
223         /// Releases any locks the surface may hold.
224         /// </summary>
225         /// <remarks>
226         /// For example, after compositing an offscreen surface, use this method to allow rendering to continue.
227         /// </remarks>
228         internal void ReleaseSurfaceLock()
229         {
230             NDalicManualPINVOKE.Adaptor_ReleaseSurfaceLock(swigCPtr);
231             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
232         }
233
234         /// <summary>
235         /// Sets the number of frames per render.
236         /// </summary>
237         /// <param name="numberOfVSyncsPerRender">The number of vsyncs between successive renders.</param>
238         /// <remarks>
239         /// Suggest this is a power of two:
240         /// 1 - render each vsync frame.
241         /// 2 - render every other vsync frame.
242         /// 4 - render every fourth vsync frame.
243         /// 8 - render every eighth vsync frame.
244         ///</remarks>
245         internal void SetRenderRefreshRate(uint numberOfVSyncsPerRender)
246         {
247             NDalicManualPINVOKE.Adaptor_SetRenderRefreshRate(swigCPtr, numberOfVSyncsPerRender);
248             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
249         }
250
251         /// <summary>
252         /// Sets whether the frame count per render is managed using the hardware vsync or manually timed.
253         /// </summary>
254         /// <param name="useHardware">True if the hardware vsync should be used.</param>
255         internal void SetUseHardwareVSync(bool useHardware)
256         {
257             NDalicManualPINVOKE.Adaptor_SetUseHardwareVSync(swigCPtr, useHardware);
258             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
259         }
260
261         private static readonly Adaptor instance = Adaptor.Get();
262
263         internal static Adaptor Get()
264         {
265             Adaptor ret = new Adaptor(NDalicManualPINVOKE.Adaptor_Get(), false);
266             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
267             return ret;
268         }
269
270         /// <summary>
271         /// Returns a reference to the instance of the adaptor used by the current thread.
272         /// </summary>
273         /// <remarks>The adaptor has been initialized. This is only valid in the main thread.</remarks>
274         /// <since_tizen> 4 </since_tizen>
275         public static Adaptor Instance
276         {
277             get
278             {
279                 return instance;
280             }
281         }
282
283         /// <summary>
284         /// Checks whether the adaptor is available.
285         /// </summary>
286         /// <returns>True if it is available, false otherwise.</returns>
287         internal static bool IsAvailable()
288         {
289             bool ret = NDalicManualPINVOKE.Adaptor_IsAvailable();
290             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
291             return ret;
292         }
293
294         /// <summary>
295         /// Calls this method to notify DALi when a scene is created and initialized.
296         /// Notify the adaptor that the scene has been created.
297         /// </summary>
298         internal void NotifySceneCreated()
299         {
300             NDalicManualPINVOKE.Adaptor_NotifySceneCreated(swigCPtr);
301             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
302         }
303
304         /// <summary>
305         /// Calls this method to notify DALi when the system language changes.
306         ///
307         /// Use this only when not using Dali::Application. As the application is created, using the
308         /// application will automatically receive notification of the language change.
309         /// When Dali::Application is not used, the application developer should
310         /// use app-core to receive the language change notifications and should update DALi
311         /// by calling this method.
312         /// </summary>
313         internal void NotifyLanguageChanged()
314         {
315             NDalicManualPINVOKE.Adaptor_NotifyLanguageChanged(swigCPtr);
316             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
317         }
318
319         /// <summary>
320         /// Sets the minimum distance in pixels that the fingers must move towards or away from each other in order to trigger a pinch gesture.
321         /// </summary>
322         /// <param name="distance">The minimum pinch distance in pixels.</param>
323         internal void SetMinimumPinchDistance(float distance)
324         {
325             NDalicManualPINVOKE.Adaptor_SetMinimumPinchDistance(swigCPtr, distance);
326             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
327         }
328
329         internal void FeedTouchPoint(TouchPoint point, int timeStamp)
330         {
331             NDalicManualPINVOKE.Adaptor_FeedTouchPoint(swigCPtr, TouchPoint.getCPtr(point), timeStamp);
332             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
333         }
334
335         /// <summary>
336         /// Feeds a wheel event to the adaptor.
337         /// </summary>
338         /// <param name="wheelEvent">The wheel event.</param>
339         /// <since_tizen> 4 </since_tizen>
340         public void FeedWheelEvent(Wheel wheelEvent)
341         {
342             NDalicManualPINVOKE.Adaptor_FeedWheelEvent(swigCPtr, Wheel.getCPtr(wheelEvent));
343             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
344         }
345
346         /// <summary>
347         /// Feeds a key event to the adaptor.
348         /// </summary>
349         /// <param name="keyEvent">The key event holding the key information.</param>
350         /// <since_tizen> 4 </since_tizen>
351         public void FeedKeyEvent(Key keyEvent)
352         {
353             NDalicManualPINVOKE.Adaptor_FeedKeyEvent(swigCPtr, Key.getCPtr(keyEvent));
354             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
355         }
356
357         /// <summary>
358         /// Notifies core that the scene has been created.
359         /// </summary>
360         internal void SceneCreated()
361         {
362             NDalicManualPINVOKE.Adaptor_SceneCreated(swigCPtr);
363             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
364         }
365
366         internal void SetViewMode(ViewMode viewMode)
367         {
368             NDalicManualPINVOKE.Adaptor_SetViewMode(swigCPtr, (int)viewMode);
369             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
370         }
371
372         /// <summary>
373         /// Sets the stereo base (eye separation) for stereoscopic 3D.
374         /// The stereo base is the distance in millimetres between the eyes. Typical values are
375         /// between 50mm and 70mm. The default value is 65mm.
376         /// </summary>
377         /// <param name="stereoBase">The stereo base (eye separation) for stereoscopic 3D.</param>
378         internal void SetStereoBase(float stereoBase)
379         {
380             NDalicManualPINVOKE.Adaptor_SetStereoBase(swigCPtr, stereoBase);
381             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
382         }
383
384         /// <summary>
385         /// Event arguments that passed via the Resized signal.
386         /// </summary>
387         internal class ResizedEventArgs : EventArgs
388         {
389
390             /// <summary>
391             /// Adaptor - is the adaptor which has size changed.
392             /// </summary>
393             /// <since_tizen> 4 </since_tizen>
394             public Adaptor Adaptor
395             {
396                 get;
397                 set;
398             }
399         }
400
401         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
402         private delegate void ResizedCallbackDelegate(IntPtr adaptor);
403         private EventHandler<ResizedEventArgs> _resizedEventHandler;
404         private ResizedCallbackDelegate _resizedCallbackDelegate;
405
406         /// <summary>
407         /// An event for the Resized signal which can be used to subscribe or unsubscribe the event handler
408         /// provided by the user. The Resized signal is emitted when the size changes.<br>
409         /// </summary>
410         internal event EventHandler<ResizedEventArgs> Resized
411         {
412             add
413             {
414                 if (_resizedEventHandler == null)
415                 {
416                     _resizedCallbackDelegate = (OnResized);
417                     ResizedSignal().Connect(_resizedCallbackDelegate);
418                 }
419                 _resizedEventHandler += value;
420             }
421             remove
422             {
423                 _resizedEventHandler -= value;
424                 if (_resizedEventHandler == null && ResizedSignal().Empty() == false)
425                 {
426                     ResizedSignal().Disconnect(_resizedCallbackDelegate);
427                 }
428             }
429         }
430
431         private void OnResized(IntPtr adaptor)
432         {
433             ResizedEventArgs e = new ResizedEventArgs();
434             if (adaptor != null)
435             {
436                 e.Adaptor = Adaptor.GetAdaptorFromPtr(adaptor);
437             }
438
439             if (_resizedEventHandler != null)
440             {
441                 //here we send all data to user event handlers
442                 _resizedEventHandler(this, e);
443             }
444         }
445
446         internal AdaptorSignalType ResizedSignal()
447         {
448             AdaptorSignalType ret = new AdaptorSignalType(NDalicManualPINVOKE.Adaptor_ResizedSignal(swigCPtr), false);
449             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
450             return ret;
451         }
452
453         /// <summary>
454         /// Event arguments that passed via the LanguageChanged signal.
455         /// </summary>
456         internal class LanguageChangedEventArgs : EventArgs
457         {
458
459             /// <summary>
460             /// Adaptor - is the adaptor which has language changed.
461             /// </summary>
462             /// <since_tizen> 4 </since_tizen>
463             public Adaptor Adaptor
464             {
465                 get;
466                 set;
467             }
468         }
469
470         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
471         private delegate void LanguageChangedCallbackDelegate(IntPtr adaptor);
472         private EventHandler<LanguageChangedEventArgs> _languageChangedEventHandler;
473         private LanguageChangedCallbackDelegate _languageChangedCallbackDelegate;
474
475         /// <summary>
476         /// An event for LanguageChanged signal which can be used to subscribe or unsubscribe the event handler
477         /// provided by the user. The LanguageChanged signal is emitted when the language changes.<br>
478         /// </summary>
479         internal event EventHandler<LanguageChangedEventArgs> LanguageChanged
480         {
481             add
482             {
483                 if (_languageChangedEventHandler == null)
484                 {
485                     _languageChangedCallbackDelegate = (OnLanguageChanged);
486                     LanguageChangedSignal().Connect(_languageChangedCallbackDelegate);
487                 }
488                 _languageChangedEventHandler += value;
489             }
490             remove
491             {
492                 _languageChangedEventHandler -= value;
493                 if (_languageChangedEventHandler == null && LanguageChangedSignal().Empty() == false)
494                 {
495                     LanguageChangedSignal().Disconnect(_languageChangedCallbackDelegate);
496                 }
497             }
498         }
499
500         private void OnLanguageChanged(IntPtr adaptor)
501         {
502             LanguageChangedEventArgs e = new LanguageChangedEventArgs();
503             if (adaptor != null)
504             {
505                 e.Adaptor = Adaptor.GetAdaptorFromPtr(adaptor);
506             }
507
508             if (_languageChangedEventHandler != null)
509             {
510                 //here we send all data to user event handlers
511                 _languageChangedEventHandler(this, e);
512             }
513         }
514
515         internal AdaptorSignalType LanguageChangedSignal()
516         {
517             AdaptorSignalType ret = new AdaptorSignalType(NDalicManualPINVOKE.Adaptor_LanguageChangedSignal(swigCPtr), false);
518             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
519             return ret;
520         }
521
522     }
523
524 }