Use dynamic linking instead of runtime loading

Also made me notice that the signature of the function with the callback was wrong. Oops.
This commit is contained in:
LAGonauta 2020-10-25 12:52:49 -03:00
parent 198bce4a84
commit 5702b323c9
5 changed files with 48 additions and 57 deletions

View file

@ -20,9 +20,18 @@ PACKAGES
${WINDOWS_NOTIFICATION_PACKAGES}
)
add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/exports/DinoWinToastLib.h"
COMMAND
cp "${CMAKE_CURRENT_SOURCE_DIR}/src/DinoWinToastLib.h" "${CMAKE_BINARY_DIR}/exports/DinoWinToastLib.h"
DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/src/DinoWinToastLib.h"
COMMENT
Copy header file DinoWinToastLib.h
)
add_definitions(${VALA_CFLAGS})
add_library(windows-notification SHARED ${WINDOWS_NOTIFICATION_VALA_C} src/wintoast.c)
target_link_libraries(windows-notification libdino ${WINDOWS_NOTIFICATION_PACKAGES})
add_library(windows-notification SHARED ${WINDOWS_NOTIFICATION_VALA_C} ${CMAKE_BINARY_DIR}/exports/DinoWinToastLib.h)
target_link_libraries(windows-notification libdino ${WINDOWS_NOTIFICATION_PACKAGES} "${CMAKE_CURRENT_SOURCE_DIR}/src/DinoWinToastLib.lib")
set_target_properties(windows-notification PROPERTIES PREFIX "")
set_target_properties(windows-notification PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins/)

View file

@ -0,0 +1,27 @@
#pragma once
#ifdef _WIN32
#ifdef DINOWINTOASTLIB_EXPORTS
#define DINOWINTOASTLIB_API __declspec(dllexport)
#else
#define DINOWINTOASTLIB_API __declspec(dllimport)
#endif
#else
#define DINOWINTOASTLIB_API
#endif
#ifdef __cplusplus
extern "C" {
#endif
int DINOWINTOASTLIB_API dinoWinToastLibInit();
#ifdef __cplusplus
} // extern "C"
#endif
#ifdef __cplusplus
extern "C" {
#endif
int DINOWINTOASTLIB_API dinoWinToastLibShowMessage(const char* sender, const char* message, const char* imagePath, int conv_id, void(*click_callback)(int conv_id, void* callback_target), void* callback_target);
#ifdef __cplusplus
} // extern "C"
#endif

Binary file not shown.

View file

@ -59,7 +59,7 @@ public class Plugin : RootInterface, Object {
}
var avatar_manager = app.stream_interactor.get_module(AvatarManager.IDENTITY);
var avatar = avatar_manager.get_avatar_filepath(conversation.account, conversation.counterpart);
if (!toaster.show_message(display_name, text, avatar, conversation.id, this, onclick_callback)) {
if (!toaster.show_message(display_name, text, avatar, conversation.id, onclick_callback)) {
stderr.printf("Error sending notification.");
};
}

View file

@ -1,70 +1,25 @@
namespace Dino.Plugins.WindowsNotification {
public class WinToast {
[CCode (has_target = false)]
private delegate void FunctionPointer();
[CCode (has_target = true)]
public delegate void NotificationCallback(int conv_id);
[CCode (has_target = false)]
private delegate int DinoWinToastLibInitType();
[CCode (cname = "dinoWinToastLibInit", cheader_filename = "DinoWinToastLib.h")]
private static extern int DinoWinToastLibInit();
[CCode (has_target = false)]
private delegate int DinoWinToastLibShowMessageType(char* sender, char* message, char* image_path, int conv_id, void* class_obj, NotificationCallback callback);
[CCode (cname = "LoadLibrary", cheader_filename = "libloaderapi.h")]
private static extern void* load_library(char* lib_name);
[CCode (cname = "FreeLibrary", cheader_filename = "libloaderapi.h")]
private static extern int free_library(void* handle);
[CCode (cname = "GetProcAddress", cheader_filename = "libloaderapi.h")]
private static extern FunctionPointer get_proc_address(void* lib_handle, char* func_name);
private void* library_handle = null;
private DinoWinToastLibInitType library_init = null;
private DinoWinToastLibShowMessageType library_show_message = null;
[CCode (cname = "dinoWinToastLibShowMessage", cheader_filename = "DinoWinToastLib.h")]
private static extern int DinoWinToastLibShowMessage(char* sender, char* message, char* image_path, int conv_id, NotificationCallback callback);
public bool valid { get; private set; }
public WinToast() {
valid = load();
valid = DinoWinToastLibInit() == 0;
}
public bool show_message(string sender, string message, string? image_path, int conv_id, NotificationCallback callback) {
if (valid) {
valid = library_init() == 0;
}
}
~WinToast() {
if (library_handle != null) {
free_library(library_handle);
}
}
public bool show_message(string sender, string message, string? image_path, int conv_id, void* class_obj, NotificationCallback callback) {
if (valid && library_show_message != null) {
return library_show_message(sender, message, image_path, conv_id, class_obj, callback) == 0;
return DinoWinToastLibShowMessage(sender, message, image_path, conv_id, callback) == 0;
}
return false;
}
private bool load() {
library_handle = load_library("DinoWinToastLib.dll");
if (library_handle == null) {
return false;
}
FunctionPointer function = get_proc_address(library_handle, "dinoWinToastLibInit");
if (function == null) {
return false;
}
library_init = (DinoWinToastLibInitType)function;
function = get_proc_address(library_handle, "dinoWinToastLibShowMessage");
if (function == null) {
return false;
}
library_show_message = (DinoWinToastLibShowMessageType)function;
return true;
}
}
}