4cba51072dd4d7953f2bfad8099f3b2f11c1bcd7
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Application / 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.ComponentModel;
20 using System.Collections.Generic;
21 using Tizen.Applications.CoreBackend;
22 using Tizen.Applications;
23
24 namespace Tizen.NUI
25 {
26     class NUICoreBackend : ICoreTaskBackend
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         private Rectangle windowRectangle = null;
35         private WindowType defaultWindowType = WindowType.Normal;
36         private ICoreTask coreTask;
37         private WindowData windowData = null;
38
39         /// <summary>
40         /// The Dictionary to contain each type of event callback.
41         /// </summary>
42         protected IDictionary<EventType, object> Handlers = new Dictionary<EventType, object>();
43         protected IDictionary<EventType, object> TaskHandlers = new Dictionary<EventType, object>();
44
45         /// <summary>
46         /// The default Constructor.
47         /// </summary>
48         public NUICoreBackend()
49         {
50         }
51
52         /// <summary>
53         /// The constructor with stylesheet.
54         /// </summary>
55         public NUICoreBackend(string stylesheet)
56         {
57             this.stylesheet = stylesheet;
58         }
59
60         /// <summary>
61         /// The constructor with stylesheet and window mode.
62         /// </summary>
63         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode)
64         {
65             this.stylesheet = stylesheet;
66             this.windowMode = windowMode;
67         }
68
69         /// <summary>
70         /// The constructor with stylesheet, window mode, window size and window position.
71         /// </summary>
72         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, Size2D windowSize, Position2D windowPosition)
73         {
74             this.stylesheet = stylesheet;
75             this.windowMode = windowMode;
76             if (windowSize != null && windowPosition != null)
77             {
78                 this.windowRectangle = new Rectangle(windowPosition.X, windowPosition.Y, windowSize.Width, windowSize.Height);
79             }
80         }
81
82         /// <summary>
83         /// The constructor with stylesheet, window mode, window size, window position and default window type.
84         /// This will be hidden as inhouse API. Because it is only for internal IME window.
85         /// </summary>
86         [EditorBrowsable(EditorBrowsableState.Never)]
87         public NUICoreBackend(string stylesheet, NUIApplication.WindowMode windowMode, WindowType type)
88         {
89             this.stylesheet = stylesheet;
90             this.windowMode = windowMode;
91             this.defaultWindowType = type;
92         }
93
94         [EditorBrowsable(EditorBrowsableState.Never)]
95         public NUICoreBackend(WindowData windowData)
96         {
97             //NOTE: windowMode, defaultWindowType, windowRectangle are not used.
98             this.windowData = windowData;
99         }
100
101         /// <summary>
102         /// Adds NUIApplication event to Application.
103         /// Puts each type of event callback in Dictionary.
104         /// </summary>
105         /// <param name="evType">The type of event.</param>
106         /// <param name="handler">The event callback.</param>
107         public void AddEventHandler(EventType evType, Action handler)
108         {
109             Handlers.Add(evType, handler);
110         }
111
112         /// <summary>
113         /// Adds NUIApplication event to Application.
114         /// Puts each type of event callback in Dictionary.
115         /// </summary>
116         /// <typeparam name="TEventArgs">The argument type for the event.</typeparam>
117         /// <param name="evType">The type of event.</param>
118         /// <param name="handler">The event callback.</param>
119         public void AddEventHandler<TEventArgs>(EventType evType, Action<TEventArgs> handler) where TEventArgs : EventArgs
120         {
121             Handlers.Add(evType, handler);
122         }
123
124         /// <summary>
125         /// The Dispose function.
126         /// </summary>
127         public void Dispose()
128         {
129             if (application != null)
130             {
131                 application.Dispose();
132             }
133             if (windowRectangle != null)
134             {
135                 windowRectangle.Dispose();
136             }
137         }
138
139         /// <summary>
140         /// The Exit application.
141         /// </summary>
142         public void Exit()
143         {
144             if (application != null)
145             {
146                 application.Quit();
147             }
148         }
149
150         /// <summary>
151         /// Ensures that the function passed in is called from the main loop when it is idle.
152         /// </summary>
153         /// <param name="func">The function to call</param>
154         /// <returns>true if added successfully, false otherwise</returns>
155         public bool AddIdle(System.Delegate func)
156         {
157             return application.AddIdle(func);
158         }
159
160         /// <summary>
161         /// The Run application.
162         /// </summary>
163         /// <param name="args">The arguments from commandline.</param>
164         public void Run(string[] args)
165         {
166             Tizen.Tracer.Begin("[NUI] NUICorebackend Run()");
167
168             Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): TizenSynchronizationContext.Initialize() called");
169             TizenSynchronizationContext.Initialize();
170             Tizen.Tracer.End();
171
172             Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): args of main set, window type set");
173             if (Tizen.Applications.Application.Current?.ApplicationInfo != null)
174             {
175                 args[0] = Tizen.Applications.Application.Current.ApplicationInfo.ExecutablePath;
176             }
177             if (string.IsNullOrEmpty(args[0]))
178             {
179                 args[0] = this.GetType().Assembly.FullName.Replace(" ", "");
180             }
181
182             if (windowData != null)
183             {
184                 bool enableUIThread = coreTask != null;
185                 application = Application.NewApplication(args, stylesheet, enableUIThread, windowData);
186             }
187             else if (defaultWindowType != WindowType.Normal)
188             {
189                 application = Application.NewApplication(args, stylesheet, windowMode, defaultWindowType);
190             }
191             else
192             {
193                 if (windowRectangle != null)
194                 {
195                     if (coreTask != null)
196                     {
197                         application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle, true);
198                     }
199                     else
200                     {
201                         application = Application.NewApplication(args, stylesheet, windowMode, windowRectangle);
202                     }
203                 }
204                 else
205                 {
206                     if (coreTask != null)
207                     {
208                         // The Rectangle(0, 0, 0, 0) means that want to use the full screen size window at 0,0.
209                         using (Rectangle rec = new Rectangle(0, 0, 0, 0))
210                         {
211                             application = Application.NewApplication(args, stylesheet, windowMode, rec, true);
212                         }
213                     }
214                     else
215                     {
216                         application = Application.NewApplication(args, stylesheet, windowMode);
217                     }
218                 }
219             }
220             Tizen.Tracer.End();
221
222             Tizen.Tracer.Begin("[NUI] NUICorebackend Run(): add application related events");
223             application.BatteryLow += OnBatteryLow;
224             application.LanguageChanged += OnLanguageChanged;
225             application.MemoryLow += OnMemoryLow;
226             application.RegionChanged += OnRegionChanged;
227             application.DeviceOrientationChanged += OnDeviceOrientationChanged;
228
229             application.Initialized += OnInitialized;
230             application.Resumed += OnResumed;
231             application.Terminating += OnTerminated;
232             application.Paused += OnPaused;
233             application.AppControl += OnAppControl;
234             Tizen.Tracer.End();
235
236             Tizen.Tracer.End();
237
238             if (coreTask != null)
239             {
240                 application.TaskBatteryLow += OnTaskBatteryLow;
241                 application.TaskLanguageChanged += OnTaskLanguageChanged;
242                 application.TaskMemoryLow += OnTaskMemoryLow;
243                 application.TaskRegionChanged += OnTaskRegionChanged;
244                 application.TaskDeviceOrientationChanged += OnTaskDeviceOrientationChanged;
245
246                 application.TaskInitialized += OnTaskInitialized;
247                 application.TaskTerminating += OnTaskTerminated;
248                 application.TaskAppControl += OnTaskAppControl;
249                 // Note: UIEvent, DeviceOrientationChanged are not implemented.
250             }
251
252             application.MainLoop();
253             application.Dispose();
254         }
255
256         /// <summary>
257         /// Sets the core task.
258         /// </summary>
259         /// <param name="task">The core task interface.</param>
260         /// <since_tizen> 10 </since_tizen>
261         public void SetCoreTask(ICoreTask task)
262         {
263             coreTask = task;
264         }
265
266         /// <summary>
267         /// The Region changed event callback function.
268         /// </summary>
269         /// <param name="source">The application instance.</param>
270         /// <param name="e">The event argument for RegionChanged.</param>
271         private void OnRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
272         {
273             Log.Info("NUI", "NUICorebackend OnRegionChanged Called");
274             var handler = Handlers[EventType.RegionFormatChanged] as Action<RegionFormatChangedEventArgs>;
275             handler?.Invoke(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
276         }
277
278         /// <summary>
279         /// The Memory Low event callback function.
280         /// </summary>
281         /// <param name="source">The application instance.</param>
282         /// <param name="e">The event argument for MemoryLow.</param>
283         private void OnMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
284         {
285             Log.Info("NUI", "NUICorebackend OnMemoryLow Called");
286             var handler = Handlers[EventType.LowMemory] as Action<LowMemoryEventArgs>;
287
288             switch (e.MemoryStatus)
289             {
290                 case Application.MemoryStatus.Normal:
291                     {
292                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.None));
293                         break;
294                     }
295                 case Application.MemoryStatus.Low:
296                     {
297                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
298                         break;
299                     }
300                 case Application.MemoryStatus.CriticallyLow:
301                     {
302                         handler?.Invoke(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
303                         break;
304                     }
305             }
306         }
307
308         /// <summary>
309         /// The Language changed event callback function.
310         /// </summary>
311         /// <param name="source">The application instance.</param>
312         /// <param name="e">The event argument for LanguageChanged.</param>
313         private void OnLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
314         {
315             Log.Info("NUI", "NUICorebackend OnLanguageChanged Called");
316             var handler = Handlers[EventType.LocaleChanged] as Action<LocaleChangedEventArgs>;
317             handler?.Invoke(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
318         }
319
320         /// <summary>
321         /// The Battery Low event callback function.
322         /// </summary>
323         /// <param name="source">The application instance.</param>
324         /// <param name="e">The event argument for BatteryLow.</param>
325         private void OnBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
326         {
327             Log.Info("NUI", "NUICorebackend OnBatteryLow Called");
328             var handler = Handlers[EventType.LowBattery] as Action<LowBatteryEventArgs>;
329             switch (e.BatteryStatus)
330             {
331                 case Application.BatteryStatus.Normal:
332                     {
333                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.None));
334                         break;
335                     }
336                 case Application.BatteryStatus.CriticallyLow:
337                     {
338                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
339                         break;
340                     }
341                 case Application.BatteryStatus.PowerOff:
342                     {
343                         handler?.Invoke(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
344                         break;
345                     }
346             }
347         }
348
349         /// <summary>
350         /// The Device Orientation changed event callback function.
351         /// </summary>
352         /// <param name="source">The application instance.</param>
353         /// <param name="e">The event argument for DeviceOrientationChanged.</param>
354         private void OnDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
355         {
356             Log.Info("NUI", "NUICorebackend OnDeviceOrientationChanged Called");
357             var handler = Handlers[EventType.DeviceOrientationChanged] as Action<DeviceOrientationEventArgs>;
358
359             switch (e.DeviceOrientationStatus)
360             {
361                 case Application.DeviceOrientationStatus.Orientation_0:
362                     {
363                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
364                         break;
365                     }
366                 case Application.DeviceOrientationStatus.Orientation_90:
367                     {
368                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
369                         break;
370                     }
371                 case Application.DeviceOrientationStatus.Orientation_180:
372                     {
373                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
374                         break;
375                     }
376                 case Application.DeviceOrientationStatus.Orientation_270:
377                     {
378                         handler?.Invoke(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
379                         break;
380                     }
381             }
382         }
383
384         /// <summary>
385         /// The Initialized event callback function.
386         /// </summary>
387         /// <param name="source">The application instance.</param>
388         /// <param name="e">The event argument for Initialized.</param>
389         private void OnInitialized(object source, NUIApplicationInitEventArgs e)
390         {
391             Tizen.Tracer.Begin("[NUI] OnInitialized()");
392
393             Log.Info("NUI", $"NUICorebackend OnPreCreated Called IsUsingXaml={NUIApplication.IsUsingXaml}");
394
395             Tizen.Tracer.Begin("[NUI] OnInitialized(): OnPreCreated event handler");
396             var preCreateHandler = Handlers[EventType.PreCreated] as Action;
397             preCreateHandler?.Invoke();
398             Tizen.Tracer.End();
399
400             Log.Info("NUI", "NUICorebackend OnCreate Called");
401
402             Tizen.Tracer.Begin("[NUI] OnInitialized(): OnCreated event handler");
403             var createHandler = Handlers[EventType.Created] as Action;
404             createHandler?.Invoke();
405             Tizen.Tracer.End();
406
407             Tizen.Tracer.End();
408         }
409
410         /// <summary>
411         /// The Terminated event callback function.
412         /// </summary>
413         /// <param name="source">The application instance.</param>
414         /// <param name="e">The event argument for Terminated.</param>
415         private void OnTerminated(object source, NUIApplicationTerminatingEventArgs e)
416         {
417             Log.Info("NUI", "NUICorebackend OnTerminated Called");
418             var handler = Handlers[EventType.Terminated] as Action;
419             handler?.Invoke();
420         }
421
422         /// <summary>
423         /// The Resumed event callback function.
424         /// </summary>
425         /// <param name="source">The application instance.</param>
426         /// <param name="e">The event argument for Resumed.</param>
427         private void OnResumed(object source, NUIApplicationResumedEventArgs e)
428         {
429             Tizen.Tracer.Begin("[NUI] OnResumed()");
430
431             Log.Info("NUI", "NUICorebackend OnResumed Called");
432
433             Tizen.Tracer.Begin("[NUI] OnResumed(): OnResumed event handler");
434             var handler = Handlers[EventType.Resumed] as Action;
435             handler?.Invoke();
436             Tizen.Tracer.End();
437
438             Tizen.Tracer.End();
439         }
440
441         /// <summary>
442         /// The App control event callback function.
443         /// </summary>
444         /// <param name="source">The application instance.</param>
445         /// <param name="e">The event argument for AppControl.</param>
446         private void OnAppControl(object source, NUIApplicationAppControlEventArgs e)
447         {
448             Log.Info("NUI", "NUICorebackend OnAppControl Called");
449             var handler = Handlers[EventType.AppControlReceived] as Action<AppControlReceivedEventArgs>;
450             using SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
451             handler?.Invoke(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
452         }
453
454         /// <summary>
455         /// The Paused event callback function.
456         /// </summary>
457         /// <param name="source">The application instance.</param>
458         /// <param name="e">The event argument for Paused.</param>
459         private void OnPaused(object source, NUIApplicationPausedEventArgs e)
460         {
461             Log.Info("NUI", "NUICorebackend OnPaused Called");
462             var handler = Handlers[EventType.Paused] as Action;
463             handler?.Invoke();
464         }
465
466         /// <summary>
467         /// The Region changed event callback function. The callback is emitted on the main thread.
468         /// </summary>
469         /// <param name="source">The application instance.</param>
470         /// <param name="e">The event argument for RegionChanged.</param>
471         private void OnTaskRegionChanged(object source, NUIApplicationRegionChangedEventArgs e)
472         {
473             Log.Info("NUI", "NUICorebackend OnTaskRegionChanged Called");
474             coreTask.OnRegionFormatChanged(new RegionFormatChangedEventArgs((source as Application)?.GetRegion()));
475         }
476
477         /// <summary>
478         /// The Memory Low event callback function. The callback is emitted on the main thread.
479         /// </summary>
480         /// <param name="source">The application instance.</param>
481         /// <param name="e">The event argument for MemoryLow.</param>
482         private void OnTaskMemoryLow(object source, NUIApplicationMemoryLowEventArgs e)
483         {
484             Log.Info("NUI", "NUICorebackend OnTaskMemoryLow Called");
485             switch (e.MemoryStatus)
486             {
487                 case Application.MemoryStatus.Normal:
488                     {
489                         coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.None));
490                         break;
491                     }
492                 case Application.MemoryStatus.Low:
493                     {
494                         coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.SoftWarning));
495                         break;
496                     }
497                 case Application.MemoryStatus.CriticallyLow:
498                     {
499                         coreTask.OnLowMemory(new LowMemoryEventArgs(LowMemoryStatus.HardWarning));
500                         break;
501                     }
502             }
503         }
504
505         /// <summary>
506         /// The Language changed event callback function. The callback is emitted on the main thread.
507         /// </summary>
508         /// <param name="source">The application instance.</param>
509         /// <param name="e">The event argument for LanguageChanged.</param>
510         private void OnTaskLanguageChanged(object source, NUIApplicationLanguageChangedEventArgs e)
511         {
512             Log.Info("NUI", "NUICorebackend OnTaskLanguageChanged Called");
513             coreTask.OnLocaleChanged(new LocaleChangedEventArgs((source as Application)?.GetLanguage()));
514         }
515
516         /// <summary>
517         /// The Battery Low event callback function. The callback is emitted on the main thread.
518         /// </summary>
519         /// <param name="source">The application instance.</param>
520         /// <param name="e">The event argument for BatteryLow.</param>
521         private void OnTaskBatteryLow(object source, NUIApplicationBatteryLowEventArgs e)
522         {
523             Log.Info("NUI", "NUICorebackend OnTaskBatteryLow Called");
524             switch (e.BatteryStatus)
525             {
526                 case Application.BatteryStatus.Normal:
527                     {
528                         coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.None));
529                         break;
530                     }
531                 case Application.BatteryStatus.CriticallyLow:
532                     {
533                         coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.CriticalLow));
534                         break;
535                     }
536                 case Application.BatteryStatus.PowerOff:
537                     {
538                         coreTask?.OnLowBattery(new LowBatteryEventArgs(LowBatteryStatus.PowerOff));
539                         break;
540                     }
541             }
542         }
543
544         /// <summary>
545         /// The Orientation Changed event callback function. The callback is emitted on the main thread.
546         /// </summary>
547         /// <param name="source">The application instance.</param>
548         /// <param name="e">The event argument for changing device orientation.</param>
549         private void OnTaskDeviceOrientationChanged(object source, NUIApplicationDeviceOrientationChangedEventArgs e)
550         {
551             Log.Info("NUI", "NUICorebackend OnTaskBatteryLow Called");
552             switch (e.DeviceOrientationStatus)
553             {
554                 case Application.DeviceOrientationStatus.Orientation_0:
555                     {
556                         coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_0));
557                         break;
558                     }
559                 case Application.DeviceOrientationStatus.Orientation_90:
560                     {
561                         coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_90));
562                         break;
563                     }
564                 case Application.DeviceOrientationStatus.Orientation_180:
565                     {
566                         coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_180));
567                         break;
568                     }
569                 case Application.DeviceOrientationStatus.Orientation_270:
570                     {
571                         coreTask?.OnDeviceOrientationChanged(new DeviceOrientationEventArgs(DeviceOrientation.Orientation_270));
572                         break;
573                     }
574             }
575         }
576
577         /// <summary>
578         /// The Initialized event callback function. The callback is emitted on the main thread.
579         /// </summary>
580         /// <param name="source">The application instance.</param>
581         /// <param name="e">The event argument for Initialized.</param>
582         private void OnTaskInitialized(object source, NUIApplicationInitEventArgs e)
583         {
584             Log.Info("NUI", "NUICorebackend OnTaskInitialized Called");
585             coreTask.OnCreate();
586         }
587
588         /// <summary>
589         /// The Terminated event callback function. The callback is emitted on the main thread.
590         /// </summary>
591         /// <param name="source">The application instance.</param>
592         /// <param name="e">The event argument for Terminated.</param>
593         private void OnTaskTerminated(object source, NUIApplicationTerminatingEventArgs e)
594         {
595             Log.Info("NUI", "NUICorebackend OnTaskTerminated Called");
596             coreTask.OnTerminate();
597         }
598
599         /// <summary>
600         /// The App control event callback function. The callback is emitted on the main thread.
601         /// </summary>
602         /// <param name="source">The application instance.</param>
603         /// <param name="e">The event argument for AppControl.</param>
604         private void OnTaskAppControl(object source, NUIApplicationAppControlEventArgs e)
605         {
606             Log.Info("NUI", "NUICorebackend OnTaskAppControl Called");
607             using SafeAppControlHandle handle = new SafeAppControlHandle(e.VoidP, false);
608             coreTask.OnAppControlReceived(new AppControlReceivedEventArgs(new ReceivedAppControl(handle)));
609         }
610
611         internal Application ApplicationHandle
612         {
613             get
614             {
615                 return application;
616             }
617         }
618     }
619 }