SDL_notification.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "SDL_notification_c.h"
  20. #include <SDL3/SDL_properties.h>
  21. SDL_NotificationID SDL_ShowNotificationWithProperties(SDL_PropertiesID props)
  22. {
  23. if (!props) {
  24. SDL_InvalidParamError("props");
  25. return 0;
  26. }
  27. // The title property is required.
  28. CHECK_PARAM (true) {
  29. const char *title = SDL_GetStringProperty(props, SDL_PROP_NOTIFICATION_TITLE_STRING, NULL);
  30. if (!title) {
  31. SDL_SetError("Notifications must have a title");
  32. return 0;
  33. }
  34. }
  35. return SDL_SYS_ShowNotification(props);
  36. }
  37. SDL_NotificationID SDL_ShowNotification(const char *title, const char *message, SDL_Surface *image, SDL_NotificationAction *actions, int num_actions)
  38. {
  39. SDL_NotificationID id = 0;
  40. SDL_PropertiesID props = SDL_CreateProperties();
  41. if (!props) {
  42. return 0;
  43. }
  44. if (title) {
  45. if (!SDL_SetStringProperty(props, SDL_PROP_NOTIFICATION_TITLE_STRING, title)) {
  46. goto cleanup;
  47. }
  48. } else {
  49. SDL_SetError("Notifications must have a title");
  50. goto cleanup;
  51. }
  52. if (message) {
  53. if (!SDL_SetStringProperty(props, SDL_PROP_NOTIFICATION_MESSAGE_STRING, message)) {
  54. goto cleanup;
  55. }
  56. }
  57. if (image) {
  58. if (!SDL_SetPointerProperty(props, SDL_PROP_NOTIFICATION_IMAGE_POINTER, image)) {
  59. goto cleanup;
  60. }
  61. }
  62. if (actions && num_actions) {
  63. if (!SDL_SetPointerProperty(props, SDL_PROP_NOTIFICATION_ACTIONS_POINTER, actions)) {
  64. goto cleanup;
  65. }
  66. if (!SDL_SetNumberProperty(props, SDL_PROP_NOTIFICATION_ACTION_COUNT_NUMBER, num_actions)) {
  67. goto cleanup;
  68. }
  69. }
  70. id = SDL_ShowNotificationWithProperties(props);
  71. cleanup:
  72. SDL_DestroyProperties(props);
  73. return id;
  74. }