2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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.
17 #include "common/glib_private.hh"
21 guint GLib::IdleAdd(GSourceFunc func, gpointer data) {
22 return IdleAddFull(G_PRIORITY_DEFAULT, g_main_context_get_thread_default(),
26 guint GLib::IdleAdd(GMainContext* context, GSourceFunc func, gpointer data) {
27 return IdleAddFull(G_PRIORITY_DEFAULT, context, func, data);
30 guint GLib::IdleAddFull(guint priority, GMainContext* context,
31 GSourceFunc func, gpointer data) {
32 auto* source = g_idle_source_new();
33 if (source == nullptr)
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);
43 guint GLib::TimeoutAdd(guint interval, GSourceFunc func, gpointer data) {
44 return TimeoutAdd(g_main_context_get_thread_default(), interval, func, data);
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)
53 g_source_set_callback(source, func, data, nullptr);
54 guint source_id = g_source_attach(source, context);
55 g_source_unref(source);
59 void GLib::SourceRemove(guint source_id) {
60 SourceRemove(g_main_context_get_thread_default(), source_id);
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);
69 } // namespace tizen_cpp