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