2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
22 public static class EcoreMainloop
24 static readonly Dictionary<int, Func<bool>> _taskMap = new Dictionary<int, Func<bool>>();
25 static readonly Object _taskLock = new Object();
26 static int _newTaskId = 0;
28 static Interop.Ecore.EcoreTaskCallback _nativeHandler;
30 static EcoreMainloop()
32 Interop.Ecore.ecore_init();
33 Interop.Ecore.ecore_main_loop_glib_integrate();
34 _nativeHandler = NativeHandler;
37 public static bool IsMainThread => Interop.Eina.eina_main_loop_is();
39 public static void Begin()
41 Interop.Ecore.ecore_main_loop_begin();
44 public static void Quit()
46 Interop.Ecore.ecore_main_loop_quit();
49 public static void Post(Action task)
51 int id = RegistHandler(() => { task(); return false; });
52 Interop.Ecore.ecore_idler_add(_nativeHandler, (IntPtr)id);
55 public static void PostAndWakeUp(Action task)
57 int id = RegistHandler(() => { task(); return false; });
58 Interop.Ecore.ecore_main_loop_thread_safe_call_async(_nativeHandler, (IntPtr)id);
61 public static void Send(Action task)
63 int id = RegistHandler(() => { task(); return false; });
64 Interop.Ecore.ecore_main_loop_thread_safe_call_sync(_nativeHandler, (IntPtr)id);
67 public static IntPtr AddTimer(double interval, Func<bool> handler)
69 int id = RegistHandler(handler);
70 return Interop.Ecore.ecore_timer_add(interval, _nativeHandler, (IntPtr)id);
73 public static void RemoveTimer(IntPtr id)
75 int taskId = (int)Interop.Ecore.ecore_timer_del(id);
76 _taskMap.Remove(taskId);
79 static int RegistHandler(Func<bool> task)
84 taskId = _newTaskId++;
86 _taskMap[taskId] = task;
90 static bool NativeHandler(IntPtr user_data)
92 int task_id = (int)user_data;
93 Func<bool> userAction = null;
94 _taskMap.TryGetValue(task_id, out userAction);
95 if (userAction != null)
97 _taskMap.Remove(task_id);