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