Skip to content

Commit

Permalink
testing native compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Til7701 committed Jul 26, 2024
1 parent f50314c commit 1bd3689
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/dev-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
workflow_dispatch:

jobs:
publish:
Expand Down
6 changes: 6 additions & 0 deletions Makefile
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
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version="$1"
echo "Called with version: ${version}"

echo "Compiling with maven"
mvn --batch-mode --update-snapshots compile verify
mvn --batch-mode --update-snapshots compile install

echo "Running jpackage"
jpackage --type exe \
Expand Down
45 changes: 45 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,51 @@
<target>21</target>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>compile-native</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>make</executable>
<workingDirectory>${project.basedir}</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-native</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/native</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources/native/windows</directory>
<includes>
<include>PowerEventLib.dll</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/holube/batterystatus/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.jbrienen.vbs_sc.ShortcutFactory;
import de.holube.batterystatus.jna.Kernel32;
import de.holube.batterystatus.jni.PowerEventListener;

import java.awt.AWTException;
import java.awt.Color;
Expand All @@ -22,10 +23,14 @@
public class Main {

private static final Kernel32.SYSTEM_POWER_STATUS batteryStatus = new Kernel32.SYSTEM_POWER_STATUS();
private static final PowerEventListener listener = new PowerEventListener();

private static final TrayIcon trayIcon;


static {
listener.initPowerEventListener();

final MenuItem exitMenuItem = new MenuItem("Exit");
exitMenuItem.addActionListener(a -> System.exit(0));
final PopupMenu popup = new PopupMenu();
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/de/holube/batterystatus/jni/PowerEventListener.java
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");
}

}
55 changes: 55 additions & 0 deletions src/main/resources/native/windows/PowerEventListener.c
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);
}

0 comments on commit 1bd3689

Please sign in to comment.