5123131d4dce8499da5b6498fed125227ad7921f
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / glib / framework-glib.cpp
1 /*
2  * Copyright (c) 2023 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 // CLASS HEADER
18 #include <dali/internal/adaptor/glib/framework-glib.h>
19
20 // EXTERNAL INCLUDES
21 #include <glib.h>
22 #include <cstdio>
23 #include <cstring>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace Adaptor
30 {
31 thread_local GMainLoop* gMainLoop{nullptr};
32
33 GMainContext* GetMainLoopContext()
34 {
35   if(gMainLoop != nullptr)
36   {
37     return g_main_loop_get_context(gMainLoop);
38   }
39   return nullptr;
40 }
41
42 /**
43  * Impl to hide GLib data members
44  */
45 struct FrameworkGlib::Impl
46 {
47   // Constructor
48   Impl(void* data)
49   {
50     GMainContext* context = g_main_context_new();
51     gMainLoop = mMainLoop = g_main_loop_new(context, false);
52   }
53
54   ~Impl()
55   {
56     g_main_loop_unref(mMainLoop);
57     gMainLoop = nullptr;
58   }
59
60   void Run()
61   {
62     g_main_loop_run(mMainLoop);
63   }
64
65   void Quit()
66   {
67     g_main_loop_quit(mMainLoop);
68   }
69
70   // Data
71   GMainLoop*    mMainLoop{nullptr};
72   GMainContext* mContext{nullptr};
73
74 private:
75   Impl(const Impl& impl) = delete;
76   Impl& operator=(const Impl& impl) = delete;
77 };
78
79 FrameworkGlib::FrameworkGlib(Framework::Observer& observer, TaskObserver& taskObserver, int* argc, char*** argv, Type type, bool useUiThread)
80 : Framework(observer, taskObserver, argc, argv, type, useUiThread),
81   mImpl(NULL)
82 {
83   mImpl = new Impl(this);
84 }
85
86 FrameworkGlib::~FrameworkGlib()
87 {
88   if(mRunning)
89   {
90     Quit();
91   }
92
93   delete mImpl;
94 }
95
96 void FrameworkGlib::Run()
97 {
98   mRunning = true;
99   mObserver.OnInit();
100   mImpl->Run();
101
102   mRunning = false;
103 }
104
105 void FrameworkGlib::Quit()
106 {
107   mObserver.OnTerminate();
108   mImpl->Quit();
109 }
110
111 } // namespace Adaptor
112
113 } // namespace Internal
114
115 } // namespace Dali