[NUI] Fix Svace issue (#949)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / NUICoreBackend.cs
1 /*
2  * Copyright (c) 2018 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
18 using System;
19 using System.Collections.Generic;
20
21 using Tizen.Applications.CoreBackend;
22 using Tizen.Applications;
23
24 namespace Tizen.NUI
25 {
26     class NUICoreBackend : ICoreBackend
27     {
28         /// <summary>
29         /// The Application instance to connect event.
30         /// </summary>
31         protected Application _application;
32         private string _stylesheet = "";
33         private NUIApplication.WindowMode _windowMode = NUIApplication.WindowMode.Opaque;
34
35         /// <summary>
36         /// The Dictionary to contain each type of event callback.
37         /// </summary>
38         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
39
40         /// <summary>
41         /// The default Constructor.
42         /// </summary>
43         public NUICoreBackend()
44         {
45         }
46
47         /// <summary>
48         /// The constructor with stylesheet.
49         /// </summary>
50         public NUICoreBackend(string stylesheet)
51         {
52             _stylesheet = stylesheet;
53         }
54
55         /// <summary>
56         /// The constructor with stylesheet and window mode.
57         /// </summary>
58         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
59         {
60             _stylesheet = stylesheet;
61             _windowMode = windowMode;
62         }
63
64         /// <summary>
65         /// Adds NUIApplication event to Application.
66         /// Puts each type of event callback in Dictionary.
67         /// </summary>
68         /// <param name="evType">The type of event.</param>
69         /// <param name="handler">The event callback.</param>
70         public void AddEventHandler(EventType evType, Action handler)
71         {
72             Handlers.Add(evType, handler);
73         }
74
75         /// <summary>
76         /// Adds NUIApplication event to Application.
77         /// Puts each type of event callback in Dictionary.
78         /// </summary>
79         /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
80         /// <param name="evType">The type of event.</param>
81         /// <param name="handler">The event callback.</param>
82         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
83         {
84             Handlers.Add(evType, handler);
85         }
86
87
88         /// <summary>
89         /// The Dispose function.
90         /// </summary>
91         public void Dispose()
92         {
93             if(_application != null)
94             {
95                 _application.Dispose();
96             }
97         }
98
99         /// <summary>
100         /// The Exit application.
101         /// </summary>
102         public void Exit()
103         {
104             if(_application != null)
105             {
106                 _application.Quit();
107             }
108         }
109
110         /// <summary>
111         /// Ensures that the function passed in is called from the main loop when it is idle.
112         /// </summary>
113         /// <param name="func">The function to call</param>
114         /// <returns>true if added successfully, false otherwise</returns>
115         public bool AddIdle(System.Delegate func)
116         {
117             return _application.AddIdle(func);
118         }
119
120         /// <summary>
121         /// The Run application.
122         /// </summary>
123         /// <param name="args">The arguments from commandline.</param>
124         public void Run(string[] args)
125         {
126             TizenSynchronizationContext.Initialize();
127             NDalicPINVOKE.SWIGStringHelper.RegistCallback();
128
129             args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
130             if (args.Length == 1)
131             {
132                 _application = Application.NewApplication();
133             }
134             else if (args.Length > 1)
135             {
136                 _application = Application.NewApplication(args, _stylesheet, (Application.WindowMode)_windowMode);
137             }
138
139             _application.BatteryLow += OnBatteryLow;
140             _application.LanguageChanged += OnLanguageChanged;
141             _application.MemoryLow += OnMemoryLow;
142             _application.RegionChanged += OnRegionChanged;
143
144             _application.Initialized += OnInitialized;
145             _application.Resumed += OnResumed;
146             _application.Terminating += OnTerminated;
147             _application.Paused += OnPaused;
148             _application.AppControl += OnAppControl;
149
150             _application.MainLoop();
151             _application.Dispose();
152         }
153
154         /// <summary>
155         /// The Region changed event callback function.
156         /// </summary>
157         /// <param name="source">The application instance.</param>
158         /// <param name="e">The event argument for RegionChanged.</param>
159         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
160         {
161             Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
162             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
163             handler?.Invoke( new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
164         }
165
166         /// <summary>
167         /// The Memory Low event callback function.
168         /// </summary>
169         /// <param name="source">The application instance.</param>
170         /// <param name="e">The event argument for MemoryLow.</param>
171         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
172         {
173             Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
174             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
175
176             switch ( e.MemoryStatus )
177             {
178                 case Application.MemoryStatus.Normal:
179                 {
180                     handler?.Invoke( new LowMemoryEventArgs(LowMemoryStatus.None));
181                     break;
182                 }
183                 case Application.MemoryStatus.Low:
184                 {
185                     handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
186                     break;
187                 }
188                 case Application.MemoryStatus.CriticallyLow:
189                 {
190                     handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
191                     break;
192                 }
193             }
194         }
195
196         /// <summary>
197         /// The Language changed event callback function.
198         /// </summary>
199         /// <param name="source">The application instance.</param>
200         /// <param name="e">The event argument for LanguageChanged.</param>
201         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
202         {
203             Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
204             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
205             handler?.Invoke( new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
206         }
207
208         /// <summary>
209         /// The Battery Low event callback function.
210         /// </summary>
211         /// <param name="source">The application instance.</param>
212         /// <param name="e">The event argument for BatteryLow.</param>
213         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
214         {
215             Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
216             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
217             switch( e.BatteryStatus )
218             {
219                 case Application.BatteryStatus.Normal:
220                 {
221                     handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
222                     break;
223                 }
224                 case Application.BatteryStatus.CriticallyLow:
225                 {
226                     handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
227                     break;
228                 }
229                 case Application.BatteryStatus.PowerOff:
230                 {
231                     handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
232                     break;
233                 }
234             }
235         }
236
237         /// <summary>
238         /// The Initialized event callback function.
239         /// </summary>
240         /// <param name="source">The application instance.</param>
241         /// <param name="e">The event argument for Initialized.</param>
242         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
243         {
244             Log.Info("NUI", "NUICorebackend OnPreCreated Called");
245             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
246             preCreateHandler?.Invoke();
247
248             Log.Info("NUI", "NUICorebackend OnCreate Called");
249             var createHandler = Handlers[EventType.Created] as Action;
250             createHandler?.Invoke();
251         }
252
253         /// <summary>
254         /// The Terminated event callback function.
255         /// </summary>
256         /// <param name="source">The application instance.</param>
257         /// <param name="e">The event argument for Terminated.</param>
258         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
259         {
260             Log.Info("NUI", "NUICorebackend OnTerminated Called");
261             var handler = Handlers[EventType.Terminated] as Action;
262             handler?.Invoke();
263         }
264
265         /// <summary>
266         /// The Resumed event callback function.
267         /// </summary>
268         /// <param name="source">The application instance.</param>
269         /// <param name="e">The event argument for Resumed.</param>
270         private void OnResumed(object source, NUIApplicationResumedEventArgs e)
271         {
272             Log.Info("NUI", "NUICorebackend OnResumed Called");
273             var handler = Handlers[EventType.Resumed] as Action;
274             handler?.Invoke();
275         }
276
277         /// <summary>
278         /// The App control event callback function.
279         /// </summary>
280         /// <param name="source">The application instance.</param>
281         /// <param name="e">The event argument for AppControl.</param>
282         private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
283         {
284             Log.Info("NUI", "NUICorebackend OnAppControl Called");
285             var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
286             SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP,false);
287             handler?.Invoke( new AppControlReceivedEventArgs(new ReceivedAppControl(handle)) );
288         }
289
290         /// <summary>
291         /// The Paused event callback function.
292         /// </summary>
293         /// <param name="source">The application instance.</param>
294         /// <param name="e">The event argument for Paused.</param>
295         private void OnPaused(object source, NUIApplicationPausedEventArgs e)
296         {
297             Log.Info("NUI", "NUICorebackend OnPaused Called");
298             var handler = Handlers[EventType.Paused] as Action;
299             handler?.Invoke();
300         }
301
302
303         internal Application ApplicationHandle
304         {
305                 get
306                 {
307                         return _application;
308                 }
309         }
310
311     }
312 }