Blender V4.5
GHOST_ISystem.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
12
13#include <vector>
14
15#include "GHOST_ISystem.hh"
17
18#if defined(WITH_GHOST_X11) && defined(WITH_GHOST_WAYLAND)
19# include "GHOST_SystemWayland.hh"
20# include "GHOST_SystemX11.hh"
21#elif defined(WITH_GHOST_X11)
22# include "GHOST_SystemX11.hh"
23#elif defined(WITH_GHOST_WAYLAND)
24# include "GHOST_SystemWayland.hh"
25#elif defined(WITH_GHOST_SDL)
26# include "GHOST_SystemSDL.hh"
27#elif defined(WIN32)
28# include "GHOST_SystemWin32.hh"
29#elif defined(__APPLE__)
30# include "GHOST_SystemCocoa.hh"
31#endif
32
34const char *GHOST_ISystem::m_system_backend_id = nullptr;
35
37
38GHOST_TSuccess GHOST_ISystem::createSystem(bool verbose, [[maybe_unused]] bool background)
39{
40
41 /* When GHOST fails to start, report the back-ends that were attempted.
42 * A Verbose argument could be supported in printing isn't always desired. */
43 struct GHOST_BackendInfo {
44 const char *id = nullptr;
46 std::string failure_msg;
47 };
48 std::vector<GHOST_BackendInfo> backends_attempted;
49
50 GHOST_TSuccess success;
51 if (!m_system) {
52
53#if defined(WITH_HEADLESS)
54 /* Pass. */
55#elif defined(WITH_GHOST_WAYLAND)
56# if defined(WITH_GHOST_WAYLAND_DYNLOAD)
57 const bool has_wayland_libraries = ghost_wl_dynload_libraries_init();
58# else
59 const bool has_wayland_libraries = true;
60# endif
61#endif
62
63#if defined(WITH_HEADLESS)
64 /* Pass. */
65#elif defined(WITH_GHOST_X11) && defined(WITH_GHOST_WAYLAND)
66 /* Special case, try Wayland, fall back to X11. */
67 if (has_wayland_libraries) {
68 backends_attempted.push_back({"WAYLAND"});
69 try {
70 m_system = new GHOST_SystemWayland(background);
71 }
72 catch (const std::runtime_error &e) {
73 if (verbose) {
74 backends_attempted.back().failure_msg = e.what();
75 }
76 delete m_system;
77 m_system = nullptr;
78# ifdef WITH_GHOST_WAYLAND_DYNLOAD
79 ghost_wl_dynload_libraries_exit();
80# endif
81 }
82 }
83 else {
84 m_system = nullptr;
85 }
86
87 if (!m_system) {
88 /* Try to fall back to X11. */
89 backends_attempted.push_back({"X11"});
90 try {
92 }
93 catch (const std::runtime_error &e) {
94 if (verbose) {
95 backends_attempted.back().failure_msg = e.what();
96 }
97 delete m_system;
98 m_system = nullptr;
99 }
100 }
101#elif defined(WITH_GHOST_X11)
102 backends_attempted.push_back({"X11"});
103 try {
105 }
106 catch (const std::runtime_error &e) {
107 if (verbose) {
108 backends_attempted.back().failure_msg = e.what();
109 }
110 delete m_system;
111 m_system = nullptr;
112 }
113#elif defined(WITH_GHOST_WAYLAND)
114 if (has_wayland_libraries) {
115 backends_attempted.push_back({"WAYLAND"});
116 try {
117 m_system = new GHOST_SystemWayland(background);
118 }
119 catch (const std::runtime_error &e) {
120 if (verbose) {
121 backends_attempted.back().failure_msg = e.what();
122 }
123 delete m_system;
124 m_system = nullptr;
125# ifdef WITH_GHOST_WAYLAND_DYNLOAD
126 ghost_wl_dynload_libraries_exit();
127# endif
128 }
129 }
130 else {
131 m_system = nullptr;
132 }
133#elif defined(WITH_GHOST_SDL)
134 backends_attempted.push_back({"SDL"});
135 try {
137 }
138 catch (const std::runtime_error &e) {
139 if (verbose) {
140 backends_attempted.back().failure_msg = e.what();
141 }
142 delete m_system;
143 m_system = nullptr;
144 }
145#elif defined(WIN32)
146 backends_attempted.push_back({"WIN32"});
148#elif defined(__APPLE__)
149 backends_attempted.push_back({"COCOA"});
151#endif
152
153 if (m_system) {
154 m_system_backend_id = backends_attempted.back().id;
155 }
156 else if (verbose) {
157 bool show_messages = false;
158 fprintf(stderr, "GHOST: failed to initialize display for back-end(s): [");
159 for (int i = 0; i < backends_attempted.size(); i++) {
160 const GHOST_BackendInfo &backend_item = backends_attempted[i];
161 if (i != 0) {
162 fprintf(stderr, ", ");
163 }
164 fprintf(stderr, "'%s'", backend_item.id);
165 if (!backend_item.failure_msg.empty()) {
166 show_messages = true;
167 }
168 }
169 fprintf(stderr, "]\n");
170 if (show_messages) {
171 for (int i = 0; i < backends_attempted.size(); i++) {
172 const GHOST_BackendInfo &backend_item = backends_attempted[i];
173 fprintf(stderr,
174 " '%s': %s\n",
175 backend_item.id,
176 backend_item.failure_msg.empty() ? "<unknown>" :
177 backend_item.failure_msg.c_str());
178 }
179 }
180 }
181 success = m_system != nullptr ? GHOST_kSuccess : GHOST_kFailure;
182 }
183 else {
184 success = GHOST_kFailure;
185 }
186 if (success) {
187 success = m_system->init();
188 }
189 return success;
190}
191
193{
194 GHOST_TSuccess success;
195 if (!m_system) {
196#if !defined(WITH_HEADLESS)
197 /* Try to create a off-screen render surface with the graphical systems. */
198 success = createSystem(false, true);
199 if (success) {
200 return success;
201 }
202 /* Try to fall back to headless mode if all else fails. */
203#endif
205 success = m_system != nullptr ? GHOST_kSuccess : GHOST_kFailure;
206 }
207 else {
208 success = GHOST_kFailure;
209 }
210 if (success) {
211 success = m_system->init();
212 }
213 return success;
214}
215
217{
219 if (m_system) {
220 delete m_system;
221 m_system = nullptr;
222 }
223 else {
224 success = GHOST_kFailure;
225 }
226 return success;
227}
228
233
235{
236 return m_system_backend_id;
237}
238
243
GHOST_TSuccess
Definition GHOST_Types.h:80
@ GHOST_kFailure
Definition GHOST_Types.h:80
@ GHOST_kSuccess
Definition GHOST_Types.h:80
void(* GHOST_TBacktraceFn)(void *file_handle)
Definition GHOST_Types.h:56
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
static int verbose
Definition cineonlib.cc:30
static GHOST_ISystem * getSystem()
static GHOST_TBacktraceFn getBacktraceFn()
static void setBacktraceFn(GHOST_TBacktraceFn backtrace_fn)
static GHOST_TSuccess createSystem(bool verbose, bool background)
static GHOST_TSuccess disposeSystem()
static const char * getSystemBackend()
static const char * m_system_backend_id
static GHOST_ISystem * m_system
static GHOST_TBacktraceFn m_backtrace_fn
static GHOST_TSuccess createSystemBackground()
i
Definition text_draw.cc:230