From e315c94900be47e61152a2958011e14b7e635a3e Mon Sep 17 00:00:00 2001 From: Emmanuel Leblond Date: Wed, 3 Jan 2018 18:26:44 +0100 Subject: [PATCH] Change OS::initialize signature to return Error (fix segfault on x11) --- core/os/os.h | 2 +- drivers/unix/os_unix.h | 2 +- main/main.cpp | 5 ++++- platform/android/os_android.cpp | 4 +++- platform/android/os_android.h | 2 +- platform/haiku/os_haiku.cpp | 6 ++++-- platform/haiku/os_haiku.h | 2 +- platform/iphone/os_iphone.cpp | 4 +++- platform/iphone/os_iphone.h | 2 +- platform/javascript/os_javascript.cpp | 6 ++++-- platform/javascript/os_javascript.h | 2 +- platform/osx/os_osx.h | 2 +- platform/osx/os_osx.mm | 10 ++++++---- platform/server/os_server.cpp | 6 ++++-- platform/server/os_server.h | 2 +- platform/uwp/os_uwp.cpp | 4 +++- platform/uwp/os_uwp.h | 2 +- platform/windows/os_windows.cpp | 10 ++++++---- platform/windows/os_windows.h | 2 +- platform/x11/os_x11.cpp | 27 +++++++++++++++------------ platform/x11/os_x11.h | 2 +- 21 files changed, 63 insertions(+), 41 deletions(-) diff --git a/core/os/os.h b/core/os/os.h index c9c228cfafd..a4f07df0e88 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -121,7 +121,7 @@ protected: void add_logger(Logger *p_logger); virtual void initialize_core() = 0; - virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) = 0; + virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) = 0; virtual void set_main_loop(MainLoop *p_main_loop) = 0; virtual void delete_main_loop() = 0; diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h index 7929a791cb5..d224a556474 100644 --- a/drivers/unix/os_unix.h +++ b/drivers/unix/os_unix.h @@ -55,7 +55,7 @@ protected: virtual void initialize_core(); virtual int unix_initialize_audio(int p_audio_driver); - //virtual void initialize(int p_video_driver,int p_audio_driver); + //virtual Error initialize(int p_video_driver,int p_audio_driver); virtual void finalize_core(); diff --git a/main/main.cpp b/main/main.cpp index ac68fe12963..1cb89e87ef8 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -983,7 +983,10 @@ Error Main::setup2(Thread::ID p_main_tid_override) { Thread::_main_thread_id = p_main_tid_override; } - OS::get_singleton()->initialize(video_mode, video_driver_idx, audio_driver_idx); + Error err = OS::get_singleton()->initialize(video_mode, video_driver_idx, audio_driver_idx); + if (err != OK) { + return err; + } if (init_use_custom_pos) { OS::get_singleton()->set_window_position(init_custom_pos); } diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index 97e81874c91..1542f4a5e46 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -120,7 +120,7 @@ void OS_Android::set_opengl_extensions(const char *p_gl_extensions) { gl_extensions = p_gl_extensions; } -void OS_Android::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { +Error OS_Android::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { use_gl2 = p_video_driver != 1; @@ -146,6 +146,8 @@ void OS_Android::initialize(const VideoMode &p_desired, int p_video_driver, int input->set_fallback_mapping("Default Android Gamepad"); //power_manager = memnew(power_android); + + return OK; } void OS_Android::set_main_loop(MainLoop *p_main_loop) { diff --git a/platform/android/os_android.h b/platform/android/os_android.h index adfd88b59bd..c0ab2acd2e4 100644 --- a/platform/android/os_android.h +++ b/platform/android/os_android.h @@ -145,7 +145,7 @@ public: virtual const char *get_audio_driver_name(int p_driver) const; virtual void initialize_core(); - virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); + virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); virtual void set_main_loop(MainLoop *p_main_loop); virtual void delete_main_loop(); diff --git a/platform/haiku/os_haiku.cpp b/platform/haiku/os_haiku.cpp index 65a220f8ca4..2d1d976399b 100644 --- a/platform/haiku/os_haiku.cpp +++ b/platform/haiku/os_haiku.cpp @@ -79,7 +79,7 @@ const char *OS_Haiku::get_video_driver_name(int p_driver) const { return "GLES3"; } -void OS_Haiku::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { +Error OS_Haiku::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { main_loop = NULL; current_video_mode = p_desired; @@ -114,7 +114,7 @@ void OS_Haiku::initialize(const VideoMode &p_desired, int p_video_driver, int p_ visual_server = memnew(VisualServerRaster(rasterizer)); - ERR_FAIL_COND(!visual_server); + ERR_FAIL_COND(!visual_server, ERR_UNAVAILABLE); // TODO: enable multithreaded VS /* @@ -132,6 +132,8 @@ void OS_Haiku::initialize(const VideoMode &p_desired, int p_video_driver, int p_ AudioDriverManager::initialize(p_audio_driver); power_manager = memnew(PowerHaiku); + + return OK; } void OS_Haiku::finalize() { diff --git a/platform/haiku/os_haiku.h b/platform/haiku/os_haiku.h index 6d69c1997f7..9af15eefc07 100644 --- a/platform/haiku/os_haiku.h +++ b/platform/haiku/os_haiku.h @@ -66,7 +66,7 @@ protected: virtual int get_video_driver_count() const; virtual const char *get_video_driver_name(int p_driver) const; - virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); + virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); virtual void finalize(); virtual void set_main_loop(MainLoop *p_main_loop); diff --git a/platform/iphone/os_iphone.cpp b/platform/iphone/os_iphone.cpp index 507d4f22dbc..d420f95e336 100644 --- a/platform/iphone/os_iphone.cpp +++ b/platform/iphone/os_iphone.cpp @@ -97,7 +97,7 @@ void OSIPhone::initialize_core() { set_data_dir(data_dir); }; -void OSIPhone::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { +Error OSIPhone::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { supported_orientations = 0; supported_orientations |= ((GLOBAL_DEF("video_mode/allow_horizontal", true) ? 1 : 0) << LandscapeLeft); @@ -144,6 +144,8 @@ void OSIPhone::initialize(const VideoMode &p_desired, int p_video_driver, int p_ //icloud->connect(); #endif Engine::get_singleton()->add_singleton(Engine::Singleton("iOS", memnew(iOS))); + + return OK; }; MainLoop *OSIPhone::get_main_loop() const { diff --git a/platform/iphone/os_iphone.h b/platform/iphone/os_iphone.h index 8e701a7fab8..611bc2d4cb9 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -87,7 +87,7 @@ private: virtual const char *get_video_driver_name(int p_driver) const; virtual void initialize_core(); - virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); + virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); virtual void set_main_loop(MainLoop *p_main_loop); virtual MainLoop *get_main_loop() const; diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 0bdd090ba9b..665280df963 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -419,7 +419,7 @@ void send_notification(int notif) { } } -void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { +Error OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { print_line("Init OS"); @@ -429,7 +429,7 @@ void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, i attributes.antialias = false; attributes.majorVersion = 2; EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context(NULL, &attributes); - ERR_FAIL_COND(emscripten_webgl_make_context_current(ctx) != EMSCRIPTEN_RESULT_SUCCESS); + ERR_FAIL_COND(emscripten_webgl_make_context_current(ctx) != EMSCRIPTEN_RESULT_SUCCESS, ERR_UNAVAILABLE); video_mode = p_desired; // can't fulfil fullscreen request due to browser security @@ -507,6 +507,8 @@ void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, i #undef EM_CHECK visual_server->init(); + + return OK; } void OS_JavaScript::set_main_loop(MainLoop *p_main_loop) { diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h index 8acaac9ef38..eaf8465be94 100644 --- a/platform/javascript/os_javascript.h +++ b/platform/javascript/os_javascript.h @@ -82,7 +82,7 @@ public: virtual const char *get_audio_driver_name(int p_driver) const; virtual void initialize_core(); - virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); + virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); virtual void set_main_loop(MainLoop *p_main_loop); virtual void delete_main_loop(); diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index 0411b4e72b5..2a71de12e96 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -123,7 +123,7 @@ protected: virtual const char *get_video_driver_name(int p_driver) const; virtual void initialize_core(); - virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); + virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); virtual void finalize(); virtual void set_main_loop(MainLoop *p_main_loop); diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index dbc9cdf3b70..a16391c30f3 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -976,7 +976,7 @@ static void displays_arrangement_changed(CGDirectDisplayID display_id, CGDisplay displays_arrangement_dirty = true; } -void OS_OSX::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { +Error OS_OSX::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { /*** OSX INITIALIZATION ***/ /*** OSX INITIALIZATION ***/ @@ -1013,7 +1013,7 @@ void OS_OSX::initialize(const VideoMode &p_desired, int p_video_driver, int p_au backing:NSBackingStoreBuffered defer:NO]; - ERR_FAIL_COND(window_object == nil); + ERR_FAIL_COND(window_object == nil, ERR_UNAVAILABLE); window_view = [[GodotContentView alloc] init]; @@ -1100,11 +1100,11 @@ void OS_OSX::initialize(const VideoMode &p_desired, int p_video_driver, int p_au #undef ADD_ATTR2 pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]; - ERR_FAIL_COND(pixelFormat == nil); + ERR_FAIL_COND(pixelFormat == nil, ERR_UNAVAILABLE); context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil]; - ERR_FAIL_COND(context == nil); + ERR_FAIL_COND(context == nil, ERR_UNAVAILABLE); [context setView:window_view]; @@ -1148,6 +1148,8 @@ void OS_OSX::initialize(const VideoMode &p_desired, int p_video_driver, int p_au _ensure_user_data_dir(); restore_rect = Rect2(get_window_position(), get_window_size()); + + return OK; } void OS_OSX::finalize() { diff --git a/platform/server/os_server.cpp b/platform/server/os_server.cpp index 13264ed46e0..e8d076322c0 100644 --- a/platform/server/os_server.cpp +++ b/platform/server/os_server.cpp @@ -47,7 +47,7 @@ const char *OS_Server::get_video_driver_name(int p_driver) const { return "Dummy"; } -void OS_Server::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { +Error OS_Server::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { args = OS::get_singleton()->get_cmdline_args(); current_videomode = p_desired; @@ -67,13 +67,15 @@ void OS_Server::initialize(const VideoMode &p_desired, int p_video_driver, int p spatial_sound_2d_server = memnew(SpatialSound2DServerSW); spatial_sound_2d_server->init(); - ERR_FAIL_COND(!visual_server); + ERR_FAIL_COND(!visual_server, ERR_UNAVAILABLE); visual_server->init(); input = memnew(InputDefault); _ensure_user_data_dir(); + + return OK; } void OS_Server::finalize() { diff --git a/platform/server/os_server.h b/platform/server/os_server.h index 7b7d4a38a84..67393754680 100644 --- a/platform/server/os_server.h +++ b/platform/server/os_server.h @@ -66,7 +66,7 @@ protected: virtual int get_video_driver_count() const; virtual const char *get_video_driver_name(int p_driver) const; - virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); + virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); virtual void finalize(); virtual void set_main_loop(MainLoop *p_main_loop); diff --git a/platform/uwp/os_uwp.cpp b/platform/uwp/os_uwp.cpp index 603a918cca3..96f9c895bcd 100644 --- a/platform/uwp/os_uwp.cpp +++ b/platform/uwp/os_uwp.cpp @@ -194,7 +194,7 @@ void OSUWP::screen_size_changed() { gl_context->reset(); }; -void OSUWP::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { +Error OSUWP::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { main_loop = NULL; outside = true; @@ -297,6 +297,8 @@ void OSUWP::initialize(const VideoMode &p_desired, int p_video_driver, int p_aud display_request->RequestActive(); set_keep_screen_on(GLOBAL_DEF("display/window/keep_screen_on", true)); + + return OK; } void OSUWP::set_clipboard(const String &p_text) { diff --git a/platform/uwp/os_uwp.h b/platform/uwp/os_uwp.h index 976eda1fe12..ad12b6286f1 100644 --- a/platform/uwp/os_uwp.h +++ b/platform/uwp/os_uwp.h @@ -158,7 +158,7 @@ protected: virtual const char *get_audio_driver_name(int p_driver) const; virtual void initialize_core(); - virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); + virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); virtual void set_main_loop(MainLoop *p_main_loop); virtual void delete_main_loop(); diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index e3af82b629d..a48ae8cc8c7 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -929,7 +929,7 @@ typedef enum _SHC_PROCESS_DPI_AWARENESS { SHC_PROCESS_PER_MONITOR_DPI_AWARE = 2 } SHC_PROCESS_DPI_AWARENESS; -void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { +Error OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { main_loop = NULL; outside = true; @@ -975,7 +975,7 @@ void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int if (!RegisterClassExW(&wc)) { MessageBox(NULL, "Failed To Register The Window Class.", "ERROR", MB_OK | MB_ICONEXCLAMATION); - return; // Return + return ERR_UNAVAILABLE; } pre_fs_valid = true; @@ -1045,7 +1045,7 @@ void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int RECT rect; if (!GetClientRect(hWnd, &rect)) { MessageBoxW(NULL, L"Window Creation Error.", L"ERROR", MB_OK | MB_ICONEXCLAMATION); - return; // Return FALSE + return ERR_UNAVAILABLE; }; video_mode.width = rect.right; video_mode.height = rect.bottom; @@ -1063,7 +1063,7 @@ void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int NULL, NULL, hInstance, NULL); if (!hWnd) { MessageBoxW(NULL, L"Window Creation Error.", L"ERROR", MB_OK | MB_ICONEXCLAMATION); - return; // Return FALSE + return ERR_UNAVAILABLE; } }; @@ -1127,6 +1127,8 @@ void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int SetForegroundWindow(hWnd); // Slightly Higher Priority SetFocus(hWnd); // Sets Keyboard Focus To } + + return OK; } void OS_Windows::set_clipboard(const String &p_text) { diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index 9d254ccf27b..1863642f398 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -148,7 +148,7 @@ protected: virtual const char *get_audio_driver_name(int p_driver) const; virtual void initialize_core(); - virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); + virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); virtual void set_main_loop(MainLoop *p_main_loop); virtual void delete_main_loop(); diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index e9920b18a4d..5ca3ad45b81 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -100,7 +100,7 @@ void OS_X11::initialize_core() { OS_Unix::initialize_core(); } -void OS_X11::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { +Error OS_X11::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { long im_event_mask = 0; last_button_state = 0; @@ -123,21 +123,24 @@ void OS_X11::initialize(const VideoMode &p_desired, int p_video_driver, int p_au /** XLIB INITIALIZATION **/ x11_display = XOpenDisplay(NULL); + if (!x11_display) { + ERR_PRINT("X11 Display is not available"); + return ERR_UNAVAILABLE; + } + char *modifiers = NULL; Bool xkb_dar = False; - if (x11_display) { - XAutoRepeatOn(x11_display); - xkb_dar = XkbSetDetectableAutoRepeat(x11_display, True, NULL); + XAutoRepeatOn(x11_display); + xkb_dar = XkbSetDetectableAutoRepeat(x11_display, True, NULL); - // Try to support IME if detectable auto-repeat is supported - if (xkb_dar == True) { + // Try to support IME if detectable auto-repeat is supported + if (xkb_dar == True) { #ifdef X_HAVE_UTF8_STRING - // Xutf8LookupString will be used later instead of XmbLookupString before - // the multibyte sequences can be converted to unicode string. - modifiers = XSetLocaleModifiers(""); + // Xutf8LookupString will be used later instead of XmbLookupString before + // the multibyte sequences can be converted to unicode string. + modifiers = XSetLocaleModifiers(""); #endif - } } if (modifiers == NULL) { @@ -331,8 +334,8 @@ void OS_X11::initialize(const VideoMode &p_desired, int p_video_driver, int p_au AudioDriverManager::initialize(p_audio_driver); - ERR_FAIL_COND(!visual_server); - ERR_FAIL_COND(x11_window == 0); + ERR_FAIL_COND_V(!visual_server, ERR_UNAVAILABLE); + ERR_FAIL_COND_V(x11_window == 0, ERR_UNAVAILABLE); XSetWindowAttributes new_attr; diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h index 04243a9b36c..2bf2f3cf265 100644 --- a/platform/x11/os_x11.h +++ b/platform/x11/os_x11.h @@ -193,7 +193,7 @@ protected: virtual const char *get_audio_driver_name(int p_driver) const; virtual void initialize_core(); - virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); + virtual Error initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); virtual void finalize(); virtual void set_main_loop(MainLoop *p_main_loop);