Skip to content

Commit

Permalink
Fix brightness jumping after inactivity
Browse files Browse the repository at this point in the history
Fix #11
  • Loading branch information
devnoname120 committed Feb 8, 2019
1 parent cf5e14b commit f8e33e4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
37 changes: 35 additions & 2 deletions lcd/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
// Required in order to jump to code that is in thumb mode
#define THUMB_BIT 1

// Default value corresponding do a dimmed screen after inactivity
#define LCD_DIMMED_VALUE 25

static SceUID lcd_table_inject = -1;
static tai_hook_ref_t lcd_set_brightness_ref = -1;


// static uint8_t old_brightness_values[17] = {31, 37, 43, 50, 58, 67,
// 77, 88, 100, 114, 129, 147,
Expand Down Expand Up @@ -40,6 +45,29 @@ int module_get_offset(SceUID pid, SceUID modid, int segidx, size_t offset,

int ksceKernelSysrootGetSystemSwVersion(void);

int lcd_brightness_to_index(unsigned int brightness) {
// 17 levels (0 to 16), brightness starts at 2, max 65536
return 16 * (brightness - 2) / 65534;
}

int hook_ksceLcdSetBrightness(unsigned int brightness) {
// Not trying to dim screen after inactivity
if (brightness != 1) {
return TAI_CONTINUE(int, lcd_set_brightness_ref, brightness);
}

int old_brightness = ksceLcdGetBrightness();
int old_index = lcd_brightness_to_index(old_brightness);

int new_brightness = old_brightness;
// If this doesn't make the screen brighter
if (old_brightness >= 2 && lcd_brightness_values[old_index] >= LCD_DIMMED_VALUE) {
new_brightness = brightness;
}

return TAI_CONTINUE(int, lcd_set_brightness_ref, new_brightness);
}

void lcd_enable_hooks() {
tai_module_info_t info;
info.size = sizeof(tai_module_info_t);
Expand Down Expand Up @@ -75,7 +103,7 @@ void lcd_enable_hooks() {
}

default: // Not supported
LOG("[OLED] Unsupported OS version: 0x%08X\n", sw_version);
LOG("[LCD] Unsupported OS version: 0x%08X\n", sw_version);
return;
}

Expand All @@ -101,10 +129,15 @@ void lcd_enable_hooks() {
if (ksceLcdGetBrightness != NULL && ksceLcdSetBrightness != NULL &&
res_offset1 >= 0 && res_offset2 >= 0) {
// Note: I'm calling by offset instead of importing them
// because importing LCD module on OLED device prevents module from
// because importing LCD module on OLED device prevents vitabright from
// loading
ksceLcdSetBrightness(ksceLcdGetBrightness());
}

int res_hook = taiHookFunctionExportForKernel(KERNEL_PID, &lcd_set_brightness_ref, "SceLcd", TAI_ANY_LIBRARY, 0x581D3A87, hook_ksceLcdSetBrightness);
if (res_hook < 0) {
LOG("[LCD] taiHookFunctionExportForKernel: 0x%08X\n", res_hook);
}
}

void lcd_disable_hooks() {
Expand Down
25 changes: 24 additions & 1 deletion oled/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,29 @@ static SceUID lut_inject = -1;
int (*ksceOledGetBrightness)() = NULL;
int (*ksceOledSetBrightness)(unsigned int brightness) = NULL;

static tai_hook_ref_t oled_set_brightness_ref = -1;

int module_get_offset(SceUID pid, SceUID modid, int segidx, size_t offset,
uintptr_t *addr);

int ksceKernelSysrootGetSystemSwVersion(void);


int hook_ksceOledSetBrightness(unsigned int brightness) {
// Trying to dim screen after inactivity
if (brightness == 1) {
int old_level = ksceOledGetBrightness();

// HACK: In modified vitabright_lut.txt, it corresponds to the line of screen dimmed after inactivity
if (old_level <= 16384) {
// Do nothing because it would increase brightness
return TAI_CONTINUE(int, oled_set_brightness_ref, old_level);
}
}

return TAI_CONTINUE(int, oled_set_brightness_ref, brightness);
}

void oled_enable_hooks() {
int ret = parse_lut(lookupNew);
if (ret < 0) {
Expand Down Expand Up @@ -85,9 +103,14 @@ void oled_enable_hooks() {
if (ksceOledGetBrightness != NULL && ksceOledSetBrightness != NULL &&
res_offset1 >= 0 && res_offset2 >= 0) {
// Note: I'm calling by offset instead of importing them
// because importing LCD module on OLED device prevents module from loading
// because importing OLED module on LCD devices prevents vitabright from loading
ksceOledSetBrightness(ksceOledGetBrightness());
}

int res_hook = taiHookFunctionExportForKernel(KERNEL_PID, &oled_set_brightness_ref, "SceOled", TAI_ANY_LIBRARY, 0xF9624C47, hook_ksceOledSetBrightness);
if (res_hook < 0) {
LOG("[OLED] taiHookFunctionExportForKernel: 0x%08X\n", res_hook);
}
}

void oled_disable_hooks() {
Expand Down

0 comments on commit f8e33e4

Please sign in to comment.