Convert C code to Vala

This commit is contained in:
LAGonauta 2020-10-25 09:06:23 -03:00
parent d36de2b9ea
commit 642ed6ab1b
5 changed files with 76 additions and 110 deletions

View file

@ -11,6 +11,7 @@ SOURCES
src/plugin.vala src/plugin.vala
src/register_plugin.vala src/register_plugin.vala
src/helper.vala src/helper.vala
src/wintoast.vala
CUSTOM_VAPIS CUSTOM_VAPIS
${CMAKE_BINARY_DIR}/exports/xmpp-vala.vapi ${CMAKE_BINARY_DIR}/exports/xmpp-vala.vapi
${CMAKE_BINARY_DIR}/exports/dino.vapi ${CMAKE_BINARY_DIR}/exports/dino.vapi
@ -19,34 +20,9 @@ PACKAGES
${WINDOWS_NOTIFICATION_PACKAGES} ${WINDOWS_NOTIFICATION_PACKAGES}
) )
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/exports/wintoast.h
COMMAND
cp ${CMAKE_CURRENT_SOURCE_DIR}/src/wintoast.h ${CMAKE_BINARY_DIR}/exports/wintoast.h
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/src/wintoast.h
COMMENT
Copy header file wintoast.h
)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/exports/wintoast.c
COMMAND
cp ${CMAKE_CURRENT_SOURCE_DIR}/src/wintoast.c ${CMAKE_BINARY_DIR}/exports/wintoast.c
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/src/wintoast.c
COMMENT
Copy header file wintoast.c
)
add_custom_target(wintoast
DEPENDS
${CMAKE_BINARY_DIR}/exports/wintoast.h
${CMAKE_BINARY_DIR}/exports/wintoast.c
)
add_definitions(${VALA_CFLAGS}) add_definitions(${VALA_CFLAGS})
add_library(windows-notification SHARED ${WINDOWS_NOTIFICATION_VALA_C} src/wintoast.c) add_library(windows-notification SHARED ${WINDOWS_NOTIFICATION_VALA_C} src/wintoast.c)
target_link_libraries(windows-notification libdino ${WINDOWS_NOTIFICATION_PACKAGES}) target_link_libraries(windows-notification libdino ${WINDOWS_NOTIFICATION_PACKAGES})
add_dependencies(windows-notification wintoast)
set_target_properties(windows-notification PROPERTIES PREFIX "") set_target_properties(windows-notification PROPERTIES PREFIX "")
set_target_properties(windows-notification PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins/) set_target_properties(windows-notification PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins/)

View file

@ -6,21 +6,7 @@ public class Plugin : RootInterface, Object {
private Dino.Application app; private Dino.Application app;
private ulong signal_handler = 0; private ulong signal_handler = 0;
private WinToast toaster;
[CCode (has_target = false)]
private delegate void notification_callback(void* conv);
[CCode (cname = "load_library", cheader_filename = "wintoast.h")]
private static extern int load_library();
[CCode (cname = "init_library", cheader_filename = "wintoast.h")]
private static extern int init_library(notification_callback callback);
[CCode (cname = "uninit_library", cheader_filename = "wintoast.h")]
private static extern void uninit_library();
[CCode (cname = "show_message", cheader_filename = "wintoast.h")]
private static extern int show_message(char* sender, char* message, char* imagePath, char* protocolName, void *conv);
private void onclick_callback() { private void onclick_callback() {
// TODO: // TODO:
@ -33,19 +19,16 @@ public class Plugin : RootInterface, Object {
public void registered(Dino.Application app) { public void registered(Dino.Application app) {
this.app = app; this.app = app;
if (load_library() != 1 || this.toaster = new WinToast(onclick_callback);
init_library(onclick_callback) != 1) { if (toaster.valid) {
return;
}
signal_handler = app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.connect(on_notify); signal_handler = app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.connect(on_notify);
} }
}
public void shutdown() { public void shutdown() {
if (signal_handler > 0) { if (signal_handler > 0) {
app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.disconnect(on_notify); app.stream_interactor.get_module(NotificationEvents.IDENTITY).notify_content_item.disconnect(on_notify);
} }
uninit_library();
} }
private void on_notify(ContentItem content_item, Conversation conversation) { private void on_notify(ContentItem content_item, Conversation conversation) {
@ -81,7 +64,7 @@ public class Plugin : RootInterface, Object {
} }
var avatar_manager = app.stream_interactor.get_module(AvatarManager.IDENTITY); var avatar_manager = app.stream_interactor.get_module(AvatarManager.IDENTITY);
var avatar = avatar_manager.get_avatar_filepath(conversation.account, conversation.counterpart); var avatar = avatar_manager.get_avatar_filepath(conversation.account, conversation.counterpart);
if (show_message(display_name, text, avatar, null, this) != 0) { if (!toaster.show_message(display_name, text, avatar, this)) {
stderr.printf("Error sending notification."); stderr.printf("Error sending notification.");
}; };
} }

View file

@ -1,50 +0,0 @@
#include <stdio.h>
#include <libloaderapi.h>
#include "wintoast.h"
static HMODULE library_handle = NULL;
static PidginWinToastLibInitType library_init = NULL;
static PidginWinToastLibShowMessageType library_show_message = NULL;
int load_library() {
library_handle = LoadLibrary("PidginWinToastLib.dll");
if (!library_handle) {
return FALSE;
}
FARPROC function = GetProcAddress(library_handle, "pidginWinToastLibInit");
if (!function) {
return FALSE;
}
library_init = (PidginWinToastLibInitType)function;
function = GetProcAddress(library_handle, "pidginWinToastLibShowMessage");
if (!function) {
return FALSE;
}
library_show_message = (PidginWinToastLibShowMessageType)function;
return TRUE;
}
int init_library(ClickCallbackType notification_click_callback) {
if (!library_init) {
return FALSE;
}
library_init(notification_click_callback);
return TRUE;
}
void uninit_library() {
if (library_handle) {
FreeLibrary(library_handle);
}
}
int show_message(const char * sender, const char * message, const char * imagePath, const char * protocolName, void *conv) {
if (library_show_message) {
return library_show_message(sender, message, imagePath, protocolName, conv);
}
return -1;
}

View file

@ -1,13 +0,0 @@
#ifndef WINTOAST
#define WINTOAST 1
typedef void(*ClickCallbackType)(void*);
typedef int(*PidginWinToastLibInitType)(ClickCallbackType);
typedef int(*PidginWinToastLibShowMessageType)(const char*, const char*, const char*, const char*, void*);
int load_library();
int init_library(ClickCallbackType click_callback);
void uninit_library();
int show_message(const char * sender, const char * message, const char * imagePath, const char * protocolName, void *conv);
#endif

View file

@ -0,0 +1,70 @@
namespace Dino.Plugins.WindowsNotification {
public class WinToast {
[CCode (has_target = false)]
private delegate void FunctionPointer();
[CCode (has_target = false)]
public delegate void NotificationCallback(void* conv);
[CCode (has_target = false)]
private delegate int PidginWinToastLibInitType(NotificationCallback callback);
[CCode (has_target = false)]
private delegate int PidginWinToastLibShowMessageType(char* sender, char* message, char* image_path, char* protocolName, void *conv);
[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 PidginWinToastLibInitType library_init = null;
private PidginWinToastLibShowMessageType library_show_message = null;
public bool valid { get; private set; }
public WinToast(NotificationCallback callback) {
valid = load();
if (valid) {
valid = library_init(callback) == 0;
}
}
~WinToast() {
if (library_handle != null) {
free_library(library_handle);
}
}
public bool show_message(string sender, string message, string image_path, void *conv) {
if (valid && library_show_message != null) {
return library_show_message(sender, message, image_path, null, conv) == 0;
}
return false;
}
private bool load() {
library_handle = load_library("PidginWinToastLib.dll");
if (library_handle == null) {
return false;
}
FunctionPointer function = get_proc_address(library_handle, "pidginWinToastLibInit");
if (function == null) {
return false;
}
library_init = (PidginWinToastLibInitType)function;
function = get_proc_address(library_handle, "pidginWinToastLibShowMessage");
if (function == null) {
return false;
}
library_show_message = (PidginWinToastLibShowMessageType)function;
return true;
}
}
}