diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e0b2860c..239f3a185 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1888,6 +1888,7 @@ set_src(ENGINE_INTERFACE GLOB src/engine keys.h map.h message.h + notifications.h rust.h server.h serverbrowser.h diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index fdd46fee4..37ba2d8a9 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -2683,6 +2683,7 @@ void CClient::InitInterfaces() #endif m_pDiscord = Kernel()->RequestInterface(); m_pSteam = Kernel()->RequestInterface(); + m_pNotifications = Kernel()->RequestInterface(); m_pStorage = Kernel()->RequestInterface(); m_DemoEditor.Init(m_pGameClient->NetVersion(), &m_SnapshotDelta, m_pConsole, m_pStorage); @@ -3959,7 +3960,7 @@ void CClient::Notify(const char *pTitle, const char *pMessage) if(m_pGraphics->WindowActive() || !g_Config.m_ClShowNotifications) return; - NotificationsNotify(pTitle, pMessage); + Notifications()->Notify(pTitle, pMessage); Graphics()->NotifyWindow(); } @@ -4290,9 +4291,6 @@ int main(int argc, const char **argv) if(!RandInitFailed) CleanerFunctions.emplace([]() { secure_random_uninit(); }); - NotificationsInit(); - CleanerFunctions.emplace([]() { NotificationsUninit(); }); - // Register SDL for cleanup before creating the kernel and client, // so SDL is shutdown after kernel and client. Otherwise the client // may crash when shutting down after SDL is already shutdown. @@ -4394,6 +4392,9 @@ int main(int argc, const char **argv) ISteam *pSteam = CreateSteam(); pKernel->RegisterInterface(pSteam); + INotifications *pNotifications = CreateNotifications(); + pKernel->RegisterInterface(pNotifications); + pKernel->RegisterInterface(CreateEditor(), false); pKernel->RegisterInterface(CreateFavorites().release()); pKernel->RegisterInterface(CreateGameClient()); @@ -4401,6 +4402,7 @@ int main(int argc, const char **argv) pEngine->Init(); pConsole->Init(); pConfigManager->Init(); + pNotifications->Init(GAME_NAME " Client"); // register all console commands pClient->RegisterCommands(); diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 8b4601ac2..48cfca823 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -39,6 +39,7 @@ class IEngineSound; class IFriends; class ILogger; class ISteam; +class INotifications; class IStorage; class IUpdater; @@ -71,6 +72,7 @@ class CClient : public IClient, public CDemoPlayer::IListener IEngineMap *m_pMap = nullptr; IEngineSound *m_pSound = nullptr; ISteam *m_pSteam = nullptr; + INotifications *m_pNotifications = nullptr; IStorage *m_pStorage = nullptr; IEngineTextRender *m_pTextRender = nullptr; IUpdater *m_pUpdater = nullptr; @@ -260,6 +262,7 @@ public: IEngineInput *Input() { return m_pInput; } IEngineSound *Sound() { return m_pSound; } ISteam *Steam() { return m_pSteam; } + INotifications *Notifications() { return m_pNotifications; } IStorage *Storage() { return m_pStorage; } IEngineTextRender *TextRender() { return m_pTextRender; } IUpdater *Updater() { return m_pUpdater; } diff --git a/src/engine/client/notifications.cpp b/src/engine/client/notifications.cpp index 4cacd6519..577b4f425 100644 --- a/src/engine/client/notifications.cpp +++ b/src/engine/client/notifications.cpp @@ -4,35 +4,41 @@ #if defined(CONF_PLATFORM_MACOS) // Code is in src/macos/notification.mm. +void NotificationsNotifyMacOsInternal(const char *pTitle, const char *pMessage); #elif defined(CONF_FAMILY_UNIX) && !defined(CONF_PLATFORM_ANDROID) && !defined(CONF_PLATFORM_HAIKU) && !defined(CONF_WEBASM) #include -void NotificationsInit() +#define NOTIFICATIONS_USE_LIBNOTIFY +#endif + +void CNotifications::Init(const char *pAppname) { - notify_init("DDNet Client"); +#if defined(NOTIFICATIONS_USE_LIBNOTIFY) + notify_init(pAppname); +#endif } -void NotificationsUninit() + +void CNotifications::Shutdown() { +#if defined(NOTIFICATIONS_USE_LIBNOTIFY) notify_uninit(); +#endif } -void NotificationsNotify(const char *pTitle, const char *pMessage) + +void CNotifications::Notify(const char *pTitle, const char *pMessage) { +#if defined(CONF_PLATFORM_MACOS) + NotificationsNotifyMacOsInternal(pTitle, pMessage); +#elif defined(NOTIFICATIONS_USE_LIBNOTIFY) NotifyNotification *pNotif = notify_notification_new(pTitle, pMessage, "ddnet"); if(pNotif) { notify_notification_show(pNotif, NULL); g_object_unref(G_OBJECT(pNotif)); } -} -#else -void NotificationsInit() -{ -} -void NotificationsUninit() -{ -} -void NotificationsNotify(const char *pTitle, const char *pMessage) -{ - (void)pTitle; - (void)pMessage; -} #endif +} + +INotifications *CreateNotifications() +{ + return new CNotifications(); +} diff --git a/src/engine/client/notifications.h b/src/engine/client/notifications.h index 35d7b588a..02463535e 100644 --- a/src/engine/client/notifications.h +++ b/src/engine/client/notifications.h @@ -1,6 +1,14 @@ #ifndef ENGINE_CLIENT_NOTIFICATIONS_H #define ENGINE_CLIENT_NOTIFICATIONS_H -void NotificationsInit(); -void NotificationsUninit(); -void NotificationsNotify(const char *pTitle, const char *pMessage); + +#include + +class CNotifications : public INotifications +{ +public: + void Init(const char *pAppname) override; + void Shutdown() override; + void Notify(const char *pTitle, const char *pMessage) override; +}; + #endif // ENGINE_CLIENT_NOTIFICATIONS_H diff --git a/src/engine/notifications.h b/src/engine/notifications.h new file mode 100644 index 000000000..38cfe88b0 --- /dev/null +++ b/src/engine/notifications.h @@ -0,0 +1,17 @@ +#ifndef ENGINE_NOTIFICATIONS_H +#define ENGINE_NOTIFICATIONS_H + +#include "kernel.h" + +class INotifications : public IInterface +{ + MACRO_INTERFACE("notifications") +public: + virtual void Init(const char *pAppname) = 0; + virtual void Shutdown() override = 0; + virtual void Notify(const char *pTitle, const char *pMessage) = 0; +}; + +INotifications *CreateNotifications(); + +#endif // ENGINE_NOTIFICATIONS_H diff --git a/src/macos/notifications.mm b/src/macos/notifications.mm index 39ed0aee1..2501fe0e6 100644 --- a/src/macos/notifications.mm +++ b/src/macos/notifications.mm @@ -1,16 +1,9 @@ -#import - #import #import #import -void NotificationsInit() -{ -} -void NotificationsUninit() -{ -} -void NotificationsNotify(const char *pTitle, const char *pMessage) +// TODO: NSUserNotification is deprecated. Use the User Notifications framework instead: https://developer.apple.com/documentation/usernotifications?language=objc +void NotificationsNotifyMacOsInternal(const char *pTitle, const char *pMessage) { NSString* pNsTitle = [NSString stringWithCString:pTitle encoding:NSUTF8StringEncoding]; NSString* pNsMsg = [NSString stringWithCString:pMessage encoding:NSUTF8StringEncoding];