Skip to content

Commit

Permalink
Windows: use RegNotifyChangeKeyValue instead of polling
Browse files Browse the repository at this point in the history
  • Loading branch information
airsquared committed Sep 7, 2021
1 parent 292ec4c commit 1f97041
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ dependencies {
testImplementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'

//JNA
implementation 'net.java.dev.jna:jna:5.8.0'
implementation 'net.java.dev.jna:jna-platform:5.8.0'
implementation 'net.java.dev.jna:jna:5.9.0'
implementation 'net.java.dev.jna:jna-platform:5.9.0'

//JFA
implementation 'de.jangassen:jfa:1.2.0'
implementation ('de.jangassen:jfa:1.2.0') {
exclude group: 'net.java.dev.jna', module: 'jna' // different jna version
}

//OSHI
implementation 'com.github.oshi:oshi-core:5.7.5'
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/com/jthemedetecor/WindowsThemeDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

package com.jthemedetecor;

import com.sun.jna.platform.win32.Advapi32;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.W32Errors;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.platform.win32.WinReg;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -32,6 +36,7 @@
* Works on a Windows 10 system.
*
* @author Daniel Gyorffy
* @author airsquared
*/
class WindowsThemeDetector extends OsThemeDetector {

Expand Down Expand Up @@ -80,7 +85,6 @@ public synchronized void removeListener(@Nullable Consumer<Boolean> darkThemeLis
*/
private static final class DetectorThread extends Thread {

private final Object lock = new Object();
private final WindowsThemeDetector themeDetector;

private boolean lastValue;
Expand All @@ -95,7 +99,18 @@ private static final class DetectorThread extends Thread {

@Override
public void run() {
WinReg.HKEYByReference hkey = new WinReg.HKEYByReference();
int err = Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_CURRENT_USER, REGISTRY_PATH, 0, WinNT.KEY_READ, hkey);
if (err != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(err);
}

while (!this.isInterrupted()) {
err = Advapi32.INSTANCE.RegNotifyChangeKeyValue(hkey.getValue(), false, WinNT.REG_NOTIFY_CHANGE_LAST_SET, null, false);
if (err != W32Errors.ERROR_SUCCESS) {
throw new Win32Exception(err);
}

boolean currentDetection = themeDetector.isDark();
if (currentDetection != this.lastValue) {
lastValue = currentDetection;
Expand All @@ -108,15 +123,8 @@ public void run() {
}
}
}

synchronized (lock) {
try {
lock.wait(1000);
} catch (InterruptedException e) {
break;
}
}
}
Advapi32Util.registryCloseKey(hkey.getValue());
}
}
}

0 comments on commit 1f97041

Please sign in to comment.