Skip to content

Commit a9e39a1

Browse files
committed
refactor the notification
1 parent b865715 commit a9e39a1

File tree

11 files changed

+105
-49
lines changed

11 files changed

+105
-49
lines changed

include/SDL3/SDL_notification.h

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,39 +52,55 @@ typedef enum
5252
typedef enum
5353
{
5454
SDL_ICON_TYPE_SINGLE_FILE = 0x00000010, /**< A single icon file. */
55-
SDL_ICON_TYPE_ICON_DIRECTORY = 0x00000020, /**< A directory containing icons in "heightxwidth.bmp" format. */
56-
SDL_ICON_TYPE_SURFACE = 0x00000040 /**< Icon inside an SDL surface. */
55+
SDL_ICON_TYPE_SURFACE = 0x00000020, /**< Icon inside an SDL surface. */
56+
SDL_ICON_TYPE_WINDOW = 0x00000040 /**< Icon is same as SDL window. */
5757
} SDL_IconFlags;
5858

59+
/**
60+
* Notification structure containing title, text, window, etc.
61+
*/
5962
typedef struct
6063
{
61-
Uint32 flags;
62-
union {
63-
const char *path;
64-
SDL_Surface *surface;
65-
} icon;
66-
} SDL_Icon;
64+
Uint32 flags; /**< ::SDL_NotificationFlags */
65+
const char *title; /**< UTF-8 title */
66+
const char *message; /**< UTF-8 message text */
67+
68+
struct
69+
{
70+
Uint32 flags; /**< ::SDL_IconFlags */
71+
union {
72+
const char *path;
73+
SDL_Surface *surface;
74+
SDL_Window *window;
75+
} data;
76+
} icon;
77+
78+
} SDL_NotificationData;
6779

6880
/**
69-
* \brief Create a simple system notification.
81+
* \brief Create a system notification.
7082
*
71-
* If a window is specified it's icon will be used for the notification,
72-
* and the notifcation will open that window when clicked on supported
73-
* platforms. Alternatively if an icon is specified for the icon this will,
74-
* be used instead.
83+
* \param notificationdata the SDL_NotificationData structure with title, text and other options
84+
* \returns 0 on success or a negative error code on failure; call
85+
* SDL_GetError() for more information.
86+
*
87+
* \sa SDL_ShowSimpleNotification
88+
* \sa SDL_NotificationData
89+
*/
90+
extern DECLSPEC int SDLCALL SDL_ShowNotification(const SDL_NotificationData *notificationdata);
91+
92+
/**
93+
* \brief Create a simple system notification.
7594
*
76-
* \param flags Notification/Priority Flags
7795
* \param title UTF-8 title text
7896
* \param message UTF-8 message text
79-
* \param window Window to associate to the notfication.
80-
* \param icon Icon (if different to window icon).
81-
*
82-
* \return 0 on success, -1 on error
97+
* \returns 0 on success or a negative error code on failure; call
98+
* SDL_GetError() for more information.
8399
*
84-
* \sa SDL_ShowSimpleNotification
100+
* \sa SDL_ShowNotification
101+
* \sa SDL_NotificationData
85102
*/
86-
extern DECLSPEC int SDLCALL SDL_ShowSimpleNotification(Uint32 flags, const char *title, const char *message,
87-
SDL_Window *window, SDL_Icon *icon);
103+
extern DECLSPEC int SDLCALL SDL_ShowSimpleNotification(const char *title, const char *message);
88104

89105
/* Ends C function definitions when using C++ */
90106
#ifdef __cplusplus

src/core/linux/SDL_dbus.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ SDL_DBus_ScreensaverInhibit(SDL_bool inhibit)
499499
return SDL_TRUE;
500500
}
501501

502-
SDL_bool SDL_DBus_SendNotification(const char *app, const char *title, const char *body)
502+
int SDL_DBus_ShowNotification(const SDL_NotificationData *notificationdata)
503503
{
504504
dbus_bool_t status = 0;
505505
DBusConnection *conn = dbus.session_conn;
@@ -510,6 +510,15 @@ SDL_bool SDL_DBus_SendNotification(const char *app, const char *title, const cha
510510
DBusMessageIter iter;
511511
DBusMessageIter sub;
512512

513+
const char *app;
514+
const char *title;
515+
const char *body;
516+
517+
518+
app = notificationdata->title;
519+
title = notificationdata->title;
520+
body = notificationdata->message;
521+
513522
if (conn == NULL) {
514523
return SDL_FALSE;
515524
}

src/core/linux/SDL_dbus.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ extern SDL_bool SDL_DBus_QueryPropertyOnConnection(DBusConnection *conn, const c
9393

9494
extern void SDL_DBus_ScreensaverTickle(void);
9595
extern SDL_bool SDL_DBus_ScreensaverInhibit(SDL_bool inhibit);
96-
extern SDL_bool SDL_DBus_SendNotification(const char *app, const char *title, const char *body);
96+
extern int SDL_DBus_ShowNotification(const SDL_NotificationData *notificationdata);
9797

9898
#endif /* HAVE_DBUS_DBUS_H */
9999

src/dynapi/SDL_dynapi.sym

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,7 @@ SDL3_0.0.0 {
841841
SDL_GetSystemTheme;
842842
SDL_CreatePopupWindow;
843843
SDL_GetWindowParent;
844+
SDL_ShowNotification;
844845
SDL_ShowSimpleNotification;
845846
# extra symbols go here (don't modify this line)
846847
local: *;

src/dynapi/SDL_dynapi_overrides.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,4 +868,5 @@
868868
#define SDL_GetSystemTheme SDL_GetSystemTheme_REAL
869869
#define SDL_CreatePopupWindow SDL_CreatePopupWindow_REAL
870870
#define SDL_GetWindowParent SDL_GetWindowParent_REAL
871+
#define SDL_ShowNotification SDL_ShowNotification_REAL
871872
#define SDL_ShowSimpleNotification SDL_ShowSimpleNotification_REAL

src/dynapi/SDL_dynapi_procs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,4 +913,5 @@ SDL_DYNAPI_PROC(int,SDL_GetRenderWindowSize,(SDL_Renderer *a, int *b, int *c),(a
913913
SDL_DYNAPI_PROC(SDL_SystemTheme,SDL_GetSystemTheme,(void),(),return)
914914
SDL_DYNAPI_PROC(SDL_Window*,SDL_CreatePopupWindow,(SDL_Window *a, int b, int c, int d, int e, Uint32 f),(a,b,c,d,e,f),return)
915915
SDL_DYNAPI_PROC(SDL_Window*,SDL_GetWindowParent,(SDL_Window *a),(a),return)
916-
SDL_DYNAPI_PROC(int,SDL_ShowSimpleNotification,(Uint32 a, const char *b, const char *c, SDL_Window *d, SDL_Icon *e),(a,b,c,d,e),return)
916+
SDL_DYNAPI_PROC(int,SDL_ShowNotification,(const SDL_NotificationData *a),(a),return)
917+
SDL_DYNAPI_PROC(int,SDL_ShowSimpleNotification,(const char *a, const char *b),(a,b),return)

src/video/SDL_sysvideo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ struct SDL_VideoDevice
345345
int (*SetWindowHitTest)(SDL_Window *window, SDL_bool enabled);
346346

347347
/* Notification */
348-
int (*ShowNotification)(_THIS, const SDL_MessageBoxData *messageboxdata, int *buttonid);
348+
int (*ShowNotification)(_THIS, const SDL_NotificationData *notificationdata);
349349

350350
/* Tell window that app enabled drag'n'drop events */
351351
void (*AcceptDragAndDrop)(SDL_Window *window, SDL_bool accept);

src/video/SDL_video.c

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5044,34 +5044,45 @@ void *SDL_Metal_GetLayer(SDL_MetalView view)
50445044
}
50455045
}
50465046

5047+
int SDL_ShowNotification(const SDL_NotificationData *notificationdata)
5048+
{
5049+
if (notificationdata == NULL) {
5050+
return SDL_InvalidParamError("notificationdata");
5051+
}
50475052

5048-
#if defined(__LINUX__)
5049-
#include "../core/linux/SDL_dbus.h"
5050-
#endif
5053+
if (notificationdata->title == NULL) {
5054+
return SDL_InvalidParamError("notificationdata->title");
5055+
}
50515056

5052-
static int SDL_PrivateShowNotification(Uint32 flags, const char *title, const char *message, SDL_Window *window, SDL_Icon *icon)
5053-
{
5054-
int retval = -1;
5057+
if (notificationdata->message == NULL) {
5058+
return SDL_InvalidParamError("notificationdata->message");
5059+
}
50555060

5056-
#if SDL_USE_LIBDBUS
5057-
/* Currently DBUS is initialized in X11 Video Init and needs to moved out */
5058-
if (retval == -1 &&
5059-
SDL_DBus_SendNotification(title, title, message) == SDL_TRUE) {
5060-
retval = 0;
5061+
if (notificationdata->icon.flags == SDL_ICON_TYPE_SINGLE_FILE && notificationdata->icon.data.path == NULL) {
5062+
return SDL_InvalidParamError("notificationdata->icon.dta.path");
50615063
}
5062-
#endif
50635064

5064-
if (retval == -1) {
5065-
retval = SDL_ShowSimpleMessageBox(0, title, message, window);
5065+
if (notificationdata->icon.flags == SDL_ICON_TYPE_SINGLE_FILE && notificationdata->icon.data.surface == NULL) {
5066+
return SDL_InvalidParamError("notificationdata->icon.data.surface");
50665067
}
50675068

5068-
return retval;
5069+
if (notificationdata->icon.flags == SDL_ICON_TYPE_WINDOW && notificationdata->icon.data.window == NULL) {
5070+
return SDL_InvalidParamError("notificationdata->icon.data.window");
5071+
}
5072+
5073+
if (_this && _this->ShowNotification) {
5074+
return _this->ShowNotification(_this, notificationdata);
5075+
}
5076+
5077+
return SDL_Unsupported();
50695078
}
50705079

5071-
int SDL_ShowSimpleNotification(Uint32 flags, const char *title, const char *message, SDL_Window *window, SDL_Icon *icon)
5080+
5081+
int SDL_ShowSimpleNotification(const char *title, const char *message)
50725082
{
5073-
/* This is to allow expansion of the API later on.
5074-
Assuming you would create a notification which could then be updated later via a pointer.
5075-
This would be useful for progress notifications etc. */
5076-
return SDL_PrivateShowNotification(flags, title, message, window, icon);
5083+
SDL_NotificationData notificationdata;
5084+
SDL_zero(notificationdata);
5085+
notificationdata.title = title;
5086+
notificationdata.message = message;
5087+
return SDL_ShowNotification(&notificationdata);
50775088
}

src/video/wayland/SDL_waylandvideo.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ static void Wayland_DeleteDevice(SDL_VideoDevice *device)
162162
SDL_WAYLAND_UnloadSymbols();
163163
}
164164

165+
#ifdef SDL_USE_LIBDBUS
166+
static int Wayland_ShowNotification(_THIS, const SDL_NotificationData *notificationdata) {
167+
return SDL_DBus_ShowNotification(notificationdata);
168+
}
169+
#endif
170+
165171
static SDL_VideoDevice *Wayland_CreateDevice(void)
166172
{
167173
SDL_VideoDevice *device;
@@ -266,6 +272,10 @@ static SDL_VideoDevice *Wayland_CreateDevice(void)
266272
device->StopTextInput = Wayland_StopTextInput;
267273
device->SetTextInputRect = Wayland_SetTextInputRect;
268274

275+
#ifdef SDL_USE_LIBDBUS
276+
device->ShowNotification = Wayland_ShowNotification;
277+
#endif
278+
269279
#if SDL_VIDEO_VULKAN
270280
device->Vulkan_LoadLibrary = Wayland_Vulkan_LoadLibrary;
271281
device->Vulkan_UnloadLibrary = Wayland_Vulkan_UnloadLibrary;

src/video/x11/SDL_x11video.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ static int X11_SafetyNetErrHandler(Display *d, XErrorEvent *e)
140140
return 0;
141141
}
142142

143+
#ifdef SDL_USE_LIBDBUS
144+
static int X11_ShowNotification(_THIS, const SDL_NotificationData *notificationdata) {
145+
return SDL_DBus_ShowNotification(notificationdata);
146+
}
147+
#endif
148+
143149
static SDL_VideoDevice *X11_CreateDevice(void)
144150
{
145151
SDL_VideoDevice *device;
@@ -307,6 +313,10 @@ static SDL_VideoDevice *X11_CreateDevice(void)
307313
device->HideScreenKeyboard = X11_HideScreenKeyboard;
308314
device->IsScreenKeyboardShown = X11_IsScreenKeyboardShown;
309315

316+
#ifdef SDL_USE_LIBDBUS
317+
device->ShowNotification = X11_ShowNotification;
318+
#endif
319+
310320
device->free = X11_DeleteDevice;
311321

312322
#if SDL_VIDEO_VULKAN

0 commit comments

Comments
 (0)