Support UI Thread
[platform/core/appfw/app-core.git] / tizen-cpp / common / glib_private.cc
1 /*
2  * Copyright (c) 2022 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 #include "common/glib_private.hh"
18
19 namespace tizen_cpp {
20
21 guint GLib::IdleAdd(GSourceFunc func, gpointer data) {
22   return IdleAddFull(G_PRIORITY_DEFAULT, g_main_context_get_thread_default(),
23       func, data);
24 }
25
26 guint GLib::IdleAdd(GMainContext* context, GSourceFunc func, gpointer data) {
27   return IdleAddFull(G_PRIORITY_DEFAULT, context, func, data);
28 }
29
30 guint GLib::IdleAddFull(guint priority, GMainContext* context,
31     GSourceFunc func, gpointer data) {
32   auto* source = g_idle_source_new();
33   if (source == nullptr)
34     return 0;
35
36   g_source_set_callback(source, func, data, nullptr);
37   g_source_set_priority(source, priority);
38   guint source_id = g_source_attach(source, context);
39   g_source_unref(source);
40   return source_id;
41 }
42
43 guint GLib::TimeoutAdd(guint interval, GSourceFunc func, gpointer data) {
44   return TimeoutAdd(g_main_context_get_thread_default(), interval, func, data);
45 }
46
47 guint GLib::TimeoutAdd(GMainContext* context, guint interval,
48     GSourceFunc func, gpointer data) {
49   auto* source = g_timeout_source_new(interval);
50   if (source == nullptr)
51     return 0;
52
53   g_source_set_callback(source, func, data, nullptr);
54   guint source_id = g_source_attach(source, context);
55   g_source_unref(source);
56   return source_id;
57 }
58
59 void GLib::SourceRemove(guint source_id) {
60   SourceRemove(g_main_context_get_thread_default(), source_id);
61 }
62
63 void GLib::SourceRemove(GMainContext* context, guint source_id) {
64   auto* source = g_main_context_find_source_by_id(context, source_id);
65   if (source != nullptr && !g_source_is_destroyed(source))
66     g_source_destroy(source);
67 }
68
69 }  // namespace tizen_cpp