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

Support disabling the screen blank/lock #4538

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type Device interface {
IsBrowser() bool
HasKeyboard() bool
SystemScaleForWindow(Window) float32

// SetDisableScreenBlanking allows an app to ask the device not to sleep/lock/blank displays
dweymouth marked this conversation as resolved.
Show resolved Hide resolved
//
// Since: 2.5
SetDisableScreenBlanking(bool)
}

// CurrentDevice returns the device information for the current hardware (via the driver)
Expand Down
4 changes: 4 additions & 0 deletions internal/driver/glfw/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ func (*glDevice) HasKeyboard() bool {
func (*glDevice) IsBrowser() bool {
return runtime.GOARCH == "js" || runtime.GOOS == "js"
}

func (*glDevice) SetDisableScreenBlanking(bool) {
// TODO implement for Windows, macOS, X11 and Wayland
}
22 changes: 22 additions & 0 deletions internal/driver/mobile/android.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,25 @@ char* listURI(uintptr_t jni_env, uintptr_t ctx, char* uriCstr) {
LOG_FATAL("Unrecognized scheme: %s", uriCstr);
return "";
}

void keepScreenOn(uintptr_t jni_env, uintptr_t ctx, bool disabled) {
JNIEnv *env = (JNIEnv*)jni_env;
jclass activityClass = find_class(env, "android/app/Activity");
jmethodID getWindow = find_method(env, activityClass, "getWindow", "()Landroid/view/Window;");

jobject win = (*env)->CallObjectMethod(env, (jobject)ctx, getWindow);
jclass windowClass = find_class(env, "android/view/Window");

jmethodID action = NULL;
if (disabled) {
action = find_method(env, windowClass, "addFlags", "(I)V");
} else {
action = find_method(env, windowClass, "clearFlags", "(I)V");
}

jclass paramsClass = find_class(env, "android/view/WindowManager$LayoutParams" );
jfieldID screenFlagField = (*env)->GetStaticFieldID(env, paramsClass, "FLAG_KEEP_SCREEN_ON", "I" );
int screenFlag = (*env)->GetStaticIntField(env, paramsClass, screenFlagField);

(*env)->CallVoidMethod(env, win, action, screenFlag);
}
4 changes: 4 additions & 0 deletions internal/driver/mobile/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (*device) HasKeyboard() bool {
return false
}

func (*device) SetDisableScreenBlanking(disable bool) {
setDisableScreenBlank(disable)
}

func (*device) ShowVirtualKeyboard() {
showVirtualKeyboard(mobile.DefaultKeyboard)
}
Expand Down
23 changes: 22 additions & 1 deletion internal/driver/mobile/device_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

package mobile

import "fyne.io/fyne/v2"
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver"
)

/*
#include <stdbool.h>
#include <stdlib.h>

void keepScreenOn(uintptr_t jni_env, uintptr_t ctx, bool disabled);
*/
import "C"

const tapYOffset = -12.0 // to compensate for how we hold our fingers on the device

Expand All @@ -18,3 +29,13 @@ func (*device) SystemScaleForWindow(_ fyne.Window) float32 {
}
return 1
}

func setDisableScreenBlank(disable bool) {
driver.RunNative(func(ctx any) error {
ac := ctx.(*driver.AndroidContext)

C.keepScreenOn(C.uintptr_t(ac.Env), C.uintptr_t(ac.Ctx), C.bool(disable))

return nil
})
}
4 changes: 4 additions & 0 deletions internal/driver/mobile/device_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ const tapYOffset = 0 // no finger compensation on desktop (simulation)
func (*device) SystemScaleForWindow(_ fyne.Window) float32 {
return 2 // this is simply due to the high number of pixels on a mobile device - just an approximation
}

func setDisableScreenBlank(_ bool) {
// ignore in mobile simulation mode
}
12 changes: 12 additions & 0 deletions internal/driver/mobile/device_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ package mobile

import "fyne.io/fyne/v2"

/*
#cgo darwin LDFLAGS: -framework UIKit
#import <Foundation/Foundation.h>

void disableIdleTimer(BOOL disabled);
*/
import "C"

const tapYOffset = -12.0 // to compensate for how we hold our fingers on the device

func (*device) SystemScaleForWindow(_ fyne.Window) float32 {
Expand All @@ -14,3 +22,7 @@ func (*device) SystemScaleForWindow(_ fyne.Window) float32 {
}
return 2
}

func setDisableScreenBlank(disable bool) {
C.disableIdleTimer(C.BOOL(disable))
}
4 changes: 4 additions & 0 deletions internal/driver/mobile/device_wayland.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ const tapYOffset = -4.0 // to compensate for how we hold our fingers on the devi
func (*device) SystemScaleForWindow(_ fyne.Window) float32 {
return 1 // PinePhone simplification, our only wayland mobile currently
}

func setDisableScreenBlank(_ bool) {
// ignore in mobile simulation mode
}
4 changes: 4 additions & 0 deletions test/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ func (d *device) SystemScaleForWindow(fyne.Window) float32 {
func (*device) IsBrowser() bool {
return runtime.GOARCH == "js" || runtime.GOOS == "js"
}

func (*device) SetDisableScreenBlanking(_ bool) {
// no-op for test
}
Loading