Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement screen_set_keep_on for macOS #63900

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions platform/macos/display_server_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#import <ApplicationServices/ApplicationServices.h>
#import <CoreVideo/CoreVideo.h>
#import <Foundation/Foundation.h>
#import <IOKit/pwr_mgt/IOPMLib.h>

#undef BitMap
#undef CursorShape
Expand Down Expand Up @@ -171,6 +172,8 @@ class DisplayServerMacOS : public DisplayServer {

HashMap<WindowID, WindowData> windows;

IOPMAssertionID screen_keep_on_assertion = kIOPMNullAssertionID;

const NSMenu *_get_menu_root(const String &p_menu_root) const;
NSMenu *_get_menu_root(const String &p_menu_root);

Expand Down Expand Up @@ -299,6 +302,7 @@ class DisplayServerMacOS : public DisplayServer {
virtual float screen_get_max_scale() const override;
virtual Rect2i screen_get_usable_rect(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
virtual float screen_get_refresh_rate(int p_screen = SCREEN_OF_MAIN_WINDOW) const override;
virtual void screen_set_keep_on(bool p_enable) override;

virtual Vector<int> get_window_list() const override;

Expand Down
22 changes: 22 additions & 0 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include "tts_macos.h"

#include "core/config/project_settings.h"
#include "core/io/marshalls.h"
#include "core/math/geometry_2d.h"
#include "core/os/keyboard.h"
Expand Down Expand Up @@ -1891,6 +1892,20 @@
return SCREEN_REFRESH_RATE_FALLBACK;
}

void DisplayServerMacOS::screen_set_keep_on(bool p_enable) {
if (screen_keep_on_assertion) {
IOPMAssertionRelease(screen_keep_on_assertion);
screen_keep_on_assertion = kIOPMNullAssertionID;
}

if (p_enable) {
String app_name_string = ProjectSettings::get_singleton()->get("application/config/name");
NSString *name = [NSString stringWithUTF8String:(app_name_string.is_empty() ? "Godot Engine" : app_name_string.utf8().get_data())];
NSString *reason = @"Godot Engine running with display/window/energy_saving/keep_screen_on = true";
IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep, (__bridge CFStringRef)name, (__bridge CFStringRef)reason, (__bridge CFStringRef)reason, nullptr, 0, nullptr, &screen_keep_on_assertion);
}
}

Vector<DisplayServer::WindowID> DisplayServerMacOS::get_window_list() const {
_THREAD_SAFE_METHOD_

Expand Down Expand Up @@ -3266,9 +3281,16 @@ Point2i window_position(
RendererCompositorRD::make_current();
}
#endif

screen_set_keep_on(GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true));
}

DisplayServerMacOS::~DisplayServerMacOS() {
if (screen_keep_on_assertion) {
IOPMAssertionRelease(screen_keep_on_assertion);
screen_keep_on_assertion = kIOPMNullAssertionID;
}

// Destroy all windows.
for (HashMap<WindowID, WindowData>::Iterator E = windows.begin(); E;) {
HashMap<WindowID, WindowData>::Iterator F = E;
Expand Down