[ACR-564] deprecate unused API
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / EcoreMainloop.cs
1 /*
2  * Copyright (c) 2016 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 System;
18 using System.Collections.Concurrent;
19
20 namespace ElmSharp
21 {
22     /// <summary>
23     /// EcoreMainloop is a helper class, which provides the functions relative to Ecore's main loop.
24     /// </summary>
25     /// <since_tizen> preview </since_tizen>
26     [Obsolete("This has been deprecated in API12")]
27     public static class EcoreMainloop
28     {
29
30         static readonly ConcurrentDictionary<int, Func<bool>> _taskMap = new ConcurrentDictionary<int, Func<bool>>();
31         static readonly Object _taskLock = new Object();
32         static int _newTaskId = 0;
33
34         static Interop.Ecore.EcoreTaskCallback _nativeHandler;
35
36         static EcoreMainloop()
37         {
38             Interop.Ecore.ecore_init();
39             Interop.Ecore.ecore_main_loop_glib_integrate();
40             _nativeHandler = NativeHandler;
41         }
42
43         /// <summary>
44         /// Checks if you are calling this function from the main thread.
45         /// </summary>
46         /// <remarks>True if the calling function is the same thread, false otherwise.</remarks>
47         /// <since_tizen> preview </since_tizen>
48         [Obsolete("This has been deprecated in API12")]
49         public static bool IsMainThread => Interop.Eina.eina_main_loop_is();
50
51         /// <summary>
52         /// Runs the application main loop.
53         /// </summary>
54         /// <since_tizen> preview </since_tizen>
55         [Obsolete("This has been deprecated in API12")]
56         public static void Begin()
57         {
58             Interop.Ecore.ecore_main_loop_begin();
59         }
60
61         /// <summary>
62         /// Quits the main loop, once all the events currently on the queue have been processed.
63         /// </summary>
64         /// <since_tizen> preview </since_tizen>
65         [Obsolete("This has been deprecated in API12")]
66         public static void Quit()
67         {
68             Interop.Ecore.ecore_main_loop_quit();
69         }
70
71         /// <summary>
72         /// Adds an idler handler.
73         /// </summary>
74         /// <param name="task">The action to call when idle.</param>
75         /// <since_tizen> preview </since_tizen>
76         [Obsolete("This has been deprecated in API12")]
77         public static void Post(Action task)
78         {
79             int id = RegistHandler(() => { task(); return false; });
80             Interop.Ecore.ecore_idler_add(_nativeHandler, (IntPtr)id);
81         }
82
83         /// <summary>
84         /// Calls the callback asynchronously in the main loop.
85         /// </summary>
86         /// <param name="task">The action wanted to be called.</param>
87         /// <since_tizen> preview </since_tizen>
88         [Obsolete("This has been deprecated in API12")]
89         public static void PostAndWakeUp(Action task)
90         {
91             if (IsMainThread)
92             {
93                 Post(task);
94             }
95             else
96             {
97                 int id = RegistHandler(() => { task(); return false; });
98                 Interop.Ecore.ecore_main_loop_thread_safe_call_async(_nativeHandler, (IntPtr)id);
99             }
100         }
101
102         /// <summary>
103         /// Calls the callback synchronously in the main loop.
104         /// </summary>
105         /// <param name="task">The action wanted to be called.</param>
106         /// <since_tizen> preview </since_tizen>
107         [Obsolete("This has been deprecated in API12")]
108         public static void Send(Action task)
109         {
110             int id = RegistHandler(() => { task(); return false; });
111             Interop.Ecore.ecore_main_loop_thread_safe_call_sync(_nativeHandler, (IntPtr)id);
112         }
113
114         /// <summary>
115         /// Creates a timer to call the given function in the given period of time.
116         /// </summary>
117         /// <param name="interval">The interval in seconds.</param>
118         /// <param name="handler">The given function.</param>
119         /// <returns>A timer object handler on success, or null on failure.</returns>
120         /// <since_tizen> preview </since_tizen>
121         [Obsolete("This has been deprecated in API12")]
122         public static IntPtr AddTimer(double interval, Func<bool> handler)
123         {
124             int id = RegistHandler(handler);
125             return Interop.Ecore.ecore_timer_add(interval, _nativeHandler, (IntPtr)id);
126         }
127
128         /// <summary>
129         /// Removes the specified timer from the timer list.
130         /// </summary>
131         /// <param name="id">The specified timer handler</param>
132         /// <since_tizen> preview </since_tizen>
133         [Obsolete("This has been deprecated in API12")]
134         public static void RemoveTimer(IntPtr id)
135         {
136             int taskId = (int)Interop.Ecore.ecore_timer_del(id);
137             Func<bool> unused;
138             _taskMap.TryRemove(taskId, out unused);
139         }
140
141         static int RegistHandler(Func<bool> task)
142         {
143             int taskId;
144             lock (_taskLock)
145             {
146                 taskId = _newTaskId++;
147             }
148             _taskMap[taskId] = task;
149             return taskId;
150         }
151
152         static bool NativeHandler(IntPtr user_data)
153         {
154             int task_id = (int)user_data;
155             Func<bool> userAction = null;
156             if (_taskMap.TryGetValue(task_id, out userAction))
157             {
158                 bool result = false;
159
160                 if (userAction != null)
161                 {
162                     result = userAction();
163                 }
164
165                 if (!result)
166                 {
167                     _taskMap.TryRemove(task_id, out userAction);
168                 }
169                 return result;
170             }
171             return false;
172         }
173     }
174 }