-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ on: | |
push: | ||
branches: | ||
- master | ||
workflow_dispatch: | ||
|
||
jobs: | ||
publish: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
JAVA_HOME := $(shell echo $$JAVA_HOME) | ||
|
||
all: PowerEventLib.dll | ||
|
||
PowerEventLib.dll: src/main/resources/native/windows/PowerEventListener.c | ||
gcc -shared -o src/main/resources/native/windows/PowerEventLib.dll -I"$(JAVA_HOME)/include" -I"$(JAVA_HOME)/include/win32" src/main/resources/native/windows/PowerEventListener.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/de/holube/batterystatus/jni/PowerEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package de.holube.batterystatus.jni; | ||
|
||
public class PowerEventListener { | ||
|
||
static { | ||
System.loadLibrary("PowerEventLib"); | ||
} | ||
|
||
public native void initPowerEventListener(); | ||
|
||
public void onSystemSuspend() { | ||
System.out.println("System is going to sleep"); | ||
} | ||
|
||
public void onSystemResume() { | ||
System.out.println("System has resumed from sleep"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <jni.h> | ||
#include <windows.h> | ||
#include "PowerEventListener.h" | ||
|
||
static jobject listenerObject; | ||
static jmethodID onSystemSuspendMethod; | ||
static jmethodID onSystemResumeMethod; | ||
|
||
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { | ||
if (uMsg == WM_POWERBROADCAST) { | ||
if (wParam == PBT_APMSUSPEND) { | ||
JNIEnv* env; | ||
JavaVM* jvm; | ||
(*listenerObject)->GetJavaVM(listenerObject, &jvm); | ||
(*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL); | ||
(*env)->CallVoidMethod(env, listenerObject, onSystemSuspendMethod); | ||
(*jvm)->DetachCurrentThread(jvm); | ||
} else if (wParam == PBT_APMRESUMESUSPEND) { | ||
JNIEnv* env; | ||
JavaVM* jvm; | ||
(*listenerObject)->GetJavaVM(listenerObject, &jvm); | ||
(*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL); | ||
(*env)->CallVoidMethod(env, listenerObject, onSystemResumeMethod); | ||
(*jvm)->DetachCurrentThread(jvm); | ||
} | ||
} | ||
return DefWindowProc(hwnd, uMsg, wParam, lParam); | ||
} | ||
|
||
JNIEXPORT void JNICALL Java_PowerEventListener_initPowerEventListener(JNIEnv* env, jobject obj) { | ||
listenerObject = (*env)->NewGlobalRef(env, obj); | ||
|
||
jclass cls = (*env)->GetObjectClass(env, obj); | ||
onSystemSuspendMethod = (*env)->GetMethodID(env, cls, "onSystemSuspend", "()V"); | ||
onSystemResumeMethod = (*env)->GetMethodID(env, cls, "onSystemResume", "()V"); | ||
|
||
WNDCLASS wndClass = {0}; | ||
wndClass.lpfnWndProc = WindowProc; | ||
wndClass.hInstance = GetModuleHandle(NULL); | ||
wndClass.lpszClassName = "PowerEventListenerClass"; | ||
|
||
RegisterClass(&wndClass); | ||
|
||
HWND hwnd = CreateWindow("PowerEventListenerClass", "Power Event Listener", | ||
0, 0, 0, 0, 0, | ||
NULL, NULL, GetModuleHandle(NULL), NULL); | ||
|
||
MSG msg; | ||
while (GetMessage(&msg, hwnd, 0, 0)) { | ||
TranslateMessage(&msg); | ||
DispatchMessage(&msg); | ||
} | ||
|
||
(*env)->DeleteGlobalRef(env, listenerObject); | ||
} |