Blender V5.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
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
33#include "CLG_log.h"
34
35static CLG_LogRef LOG = {"ghost.system"};
36
38const char *GHOST_ISystem::system_backend_id_ = nullptr;
39
41
43
44GHOST_TSuccess GHOST_ISystem::createSystem(bool verbose, [[maybe_unused]] bool background)
45{
46 /* When GHOST fails to start, report the back-ends that were attempted.
47 * A Verbose argument could be supported in printing isn't always desired. */
48 struct GHOST_BackendInfo {
49 const char *id = nullptr;
51 std::string failure_msg;
52 };
53 std::vector<GHOST_BackendInfo> backends_attempted;
54
55 GHOST_TSuccess success;
56 if (!system_) {
57#if defined(WITH_HEADLESS)
58 /* Pass. */
59#elif defined(WITH_GHOST_WAYLAND)
60# if defined(WITH_GHOST_WAYLAND_DYNLOAD)
61 const bool has_wayland_libraries = ghost_wl_dynload_libraries_init();
62# else
63 const bool has_wayland_libraries = true;
64# endif
65#endif
66
67#if defined(WITH_HEADLESS)
68 /* Pass. */
69#elif defined(WITH_GHOST_X11) && defined(WITH_GHOST_WAYLAND)
70 /* Special case, try Wayland, fall back to X11. */
71 if (has_wayland_libraries) {
72 backends_attempted.push_back({"WAYLAND"});
73 try {
74 CLOG_INFO(&LOG, "Create Wayland system");
75 system_ = new GHOST_SystemWayland(background);
76 }
77 catch (const std::runtime_error &e) {
78 if (verbose) {
79 backends_attempted.back().failure_msg = e.what();
80 }
81 CLOG_INFO(&LOG, "Wayland system not created, falling back to X11");
82 delete system_;
83 system_ = nullptr;
84# ifdef WITH_GHOST_WAYLAND_DYNLOAD
85 ghost_wl_dynload_libraries_exit();
86# endif
87 }
88 }
89 else {
90 system_ = nullptr;
91 }
92
93 if (!system_) {
94 /* Try to fall back to X11. */
95 backends_attempted.push_back({"X11"});
96 try {
97 CLOG_INFO(&LOG, "Create X11 system");
99 }
100 catch (const std::runtime_error &e) {
101 if (verbose) {
102 backends_attempted.back().failure_msg = e.what();
103 }
104 delete system_;
105 system_ = nullptr;
106 }
107 }
108#elif defined(WITH_GHOST_X11)
109 backends_attempted.push_back({"X11"});
110 try {
111 CLOG_INFO(&LOG, "Create X11 system");
112 system_ = new GHOST_SystemX11();
113 }
114 catch (const std::runtime_error &e) {
115 if (verbose) {
116 backends_attempted.back().failure_msg = e.what();
117 }
118 delete system_;
119 system_ = nullptr;
120 }
121#elif defined(WITH_GHOST_WAYLAND)
122 if (has_wayland_libraries) {
123 backends_attempted.push_back({"WAYLAND"});
124 try {
125 CLOG_INFO(&LOG, "Create Wayland system");
126 system_ = new GHOST_SystemWayland(background);
127 }
128 catch (const std::runtime_error &e) {
129 if (verbose) {
130 backends_attempted.back().failure_msg = e.what();
131 }
132 delete system_;
133 system_ = nullptr;
134# ifdef WITH_GHOST_WAYLAND_DYNLOAD
135 ghost_wl_dynload_libraries_exit();
136# endif
137 }
138 }
139 else {
140 system_ = nullptr;
141 }
142#elif defined(WITH_GHOST_SDL)
143 backends_attempted.push_back({"SDL"});
144 try {
145 CLOG_INFO(&LOG, "Create SDL system");
146 system_ = new GHOST_SystemSDL();
147 }
148 catch (const std::runtime_error &e) {
149 if (verbose) {
150 backends_attempted.back().failure_msg = e.what();
151 }
152 delete system_;
153 system_ = nullptr;
154 }
155#elif defined(WIN32)
156 backends_attempted.push_back({"WIN32"});
157 CLOG_INFO(&LOG, "Create Windows system");
159#elif defined(__APPLE__)
160 backends_attempted.push_back({"COCOA"});
161 CLOG_INFO(&LOG, "Create Cocoa system");
162 system_ = new GHOST_SystemCocoa(background);
163#endif
164
165 if (system_) {
166 system_backend_id_ = backends_attempted.back().id;
167 }
168 else if (verbose || CLOG_CHECK(&LOG, CLG_LEVEL_INFO)) {
169 bool show_messages = false;
170 std::string msg = "Failed to initialize display for back-end(s): [";
171 for (int i = 0; i < backends_attempted.size(); i++) {
172 const GHOST_BackendInfo &backend_item = backends_attempted[i];
173 if (i != 0) {
174 msg += ", ";
175 }
176 msg += "'" + std::string(backend_item.id) + "'";
177 if (!backend_item.failure_msg.empty()) {
178 show_messages = true;
179 }
180 }
181 msg += "]\n";
182 if (show_messages) {
183 for (int i = 0; i < backends_attempted.size(); i++) {
184 const GHOST_BackendInfo &backend_item = backends_attempted[i];
185 msg += " '";
186 msg += backend_item.id;
187 msg += "': ";
188 msg += backend_item.failure_msg.empty() ? "<unknown>" : backend_item.failure_msg.c_str();
189 msg += "\n";
190 }
191 }
192 CLOG_STR_INFO_NOCHECK(&LOG, msg.c_str());
193
194#ifdef WITH_GHOST_WAYLAND
195# ifndef WITH_GHOST_X11
196 /* Special case, Blender will eventually drop X11.
197 * show a more user friendly warning while this is in the process
198 * of being disabled by default. */
199
200 const char *wayland_display = getenv("WAYLAND_DISPLAY");
201 if (!(wayland_display && wayland_display[0])) {
202 const char *x11_display = getenv("DISPLAY");
203 if (x11_display && x11_display[0]) {
204 /* WAYLAND isn't available but X11 is.
205 * Print a message to let the users know how to enable X11.
206 * Since this is a non-standard option (currently - but used for "lite" builds),
207 * the user is likely a developer who created their own build. */
209 &LOG,
210 "Built without X11 support, enable \"WITH_GHOST_X11\" build-option to use X11.\n");
211 }
212 }
213# endif /* !WITH_GHOST_X11 */
214#endif /* WITH_GHOST_WAYLAND */
215 }
216 success = system_ != nullptr ? GHOST_kSuccess : GHOST_kFailure;
217 }
218 else {
219 success = GHOST_kFailure;
220 }
221 if (success) {
222 success = system_->init();
223 }
224 return success;
225}
226
228{
229 GHOST_TSuccess success;
230 if (!system_) {
231#if !defined(WITH_HEADLESS)
232 /* Try to create a off-screen render surface with the graphical systems. */
233 CLOG_INFO(&LOG, "Create background system");
234 success = createSystem(false, true);
235 if (success) {
236 return success;
237 }
238 /* Try to fall back to headless mode if all else fails. */
239#endif
240 CLOG_INFO(&LOG, "Create headless system");
242 success = system_ != nullptr ? GHOST_kSuccess : GHOST_kFailure;
243 }
244 else {
245 success = GHOST_kFailure;
246 }
247 if (success) {
248 success = system_->init();
249 }
250 return success;
251}
252
254{
255 CLOG_DEBUG(&LOG, "Dispose system");
257 if (system_) {
258 delete system_;
259 system_ = nullptr;
260 }
261 else {
262 success = GHOST_kFailure;
263 }
264 return success;
265}
266
271
273{
274 return system_backend_id_;
275}
276
281
286
291
292void GHOST_ISystem::setUseWindowFrame(bool use_window_frame)
293{
294 GHOST_ISystem::use_window_frame_ = use_window_frame;
295}
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_STR_INFO_NOCHECK(clg_ref, str)
Definition CLG_log.h:206
#define CLOG_CHECK(clg_ref, verbose_level,...)
Definition CLG_log.h:147
@ CLG_LEVEL_INFO
Definition CLG_log.h:60
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
void(*)(FILE *file_handle) GHOST_TBacktraceFn
GHOST_TSuccess
@ GHOST_kFailure
@ GHOST_kSuccess
static GHOST_TBacktraceFn backtrace_fn_
static GHOST_ISystem * getSystem()
static GHOST_TSuccess createSystem(bool verbose=true, bool background=false)
static void setUseWindowFrame(bool use_window_frame)
static GHOST_TBacktraceFn getBacktraceFn()
static void setBacktraceFn(GHOST_TBacktraceFn backtrace_fn)
static GHOST_ISystem * system_
static const char * system_backend_id_
static bool use_window_frame_
static GHOST_TSuccess disposeSystem()
static bool getUseWindowFrame()
static const char * getSystemBackend()
static GHOST_TSuccess createSystemBackground()
virtual GHOST_TSuccess init()=0
#define LOG(level)
Definition log.h:97