Added SDL_AllocateEventString()

This commit is contained in:
Sam Lantinga 2024-06-18 13:50:18 -07:00
parent fa5367d379
commit 306c4164bc
5 changed files with 25 additions and 24 deletions

View File

@ -327,7 +327,7 @@ typedef struct SDL_TextEditingEvent
Uint32 reserved;
Uint64 timestamp; /**< In nanoseconds, populated using SDL_GetTicksNS() */
SDL_WindowID windowID; /**< The window with keyboard focus, if any */
char *text; /**< The editing text */
const char *text; /**< The editing text */
Sint32 start; /**< The start cursor of selected editing text */
Sint32 length; /**< The length of selected editing text */
} SDL_TextEditingEvent;
@ -352,7 +352,7 @@ typedef struct SDL_TextInputEvent
Uint32 reserved;
Uint64 timestamp; /**< In nanoseconds, populated using SDL_GetTicksNS() */
SDL_WindowID windowID; /**< The window with keyboard focus, if any */
char *text; /**< The input text, UTF-8 encoded */
const char *text; /**< The input text, UTF-8 encoded */
} SDL_TextInputEvent;
/**
@ -733,8 +733,7 @@ typedef struct SDL_PenButtonEvent
* An event used to drop text or request a file open by the system
* (event.drop.*)
*
* The `data` is owned by SDL and should be copied if the application wants to
* hold onto it beyond the scope of handling this event. Do not free it!
* The `source` and `data` are owned by SDL and should be copied if the application wants to hold onto them beyond the scope of handling this event.
*
* \since This struct is available since SDL 3.0.0.
*/
@ -746,8 +745,8 @@ typedef struct SDL_DropEvent
SDL_WindowID windowID; /**< The window that was dropped on, if any */
float x; /**< X coordinate, relative to window (not on begin) */
float y; /**< Y coordinate, relative to window (not on begin) */
char *source; /**< The source app that sent this drop event, or NULL if that isn't available */
char *data; /**< The text for SDL_EVENT_DROP_TEXT and the file name for SDL_EVENT_DROP_FILE, NULL for other events */
const char *source; /**< The source app that sent this drop event, or NULL if that isn't available */
const char *data; /**< The text for SDL_EVENT_DROP_TEXT and the file name for SDL_EVENT_DROP_FILE, NULL for other events */
} SDL_DropEvent;
/**

View File

@ -59,16 +59,16 @@ static int SDL_SendDrop(SDL_Window *window, const SDL_EventType evtype, const ch
event.type = evtype;
event.common.timestamp = 0;
if (source) {
event.drop.source = SDL_strdup(source);
}
if (data) {
size_t size = SDL_strlen(data) + 1;
event.drop.data = (char *)SDL_AllocateEventMemory(size);
if (!event.drop.data) {
SDL_free(event.drop.source);
event.drop.source = SDL_AllocateEventString(source);
if (!event.drop.source) {
return 0;
}
}
if (data) {
event.drop.data = SDL_AllocateEventString(data);
if (!event.drop.data) {
return 0;
}
SDL_memcpy(event.drop.data, data, size);
}
event.drop.windowID = window ? window->id : 0;

View File

@ -135,6 +135,14 @@ void *SDL_AllocateEventMemory(size_t size)
return SDL_FreeLater(SDL_malloc(size));
}
const char *SDL_AllocateEventString(const char *string)
{
if (string) {
return SDL_FreeLater(SDL_strdup(string));
}
return NULL;
}
static void SDL_FlushEventMemory(Uint32 eventID)
{
SDL_LockMutex(SDL_event_memory_lock);

View File

@ -40,6 +40,8 @@ extern int SDL_StartEventLoop(void);
extern void SDL_StopEventLoop(void);
extern void SDL_QuitInterrupt(void);
extern const char *SDL_AllocateEventString(const char *string);
extern int SDL_SendAppEvent(SDL_EventType eventType);
extern int SDL_SendKeymapChangedEvent(void);
extern int SDL_SendLocaleChangedEvent(void);

View File

@ -1205,14 +1205,10 @@ int SDL_SendKeyboardText(const char *text)
event.type = SDL_EVENT_TEXT_INPUT;
event.common.timestamp = 0;
event.text.windowID = keyboard->focus ? keyboard->focus->id : 0;
size_t size = SDL_strlen(text) + 1;
event.text.text = (char *)SDL_AllocateEventMemory(size);
event.text.text = SDL_AllocateEventString(text);
if (!event.text.text) {
return 0;
}
SDL_memcpy(event.text.text, text, size);
posted = (SDL_PushEvent(&event) > 0);
}
return posted;
@ -1241,14 +1237,10 @@ int SDL_SendEditingText(const char *text, int start, int length)
event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0;
event.edit.start = start;
event.edit.length = length;
size_t size = SDL_strlen(text) + 1;
event.edit.text = (char *)SDL_AllocateEventMemory(size);
event.edit.text = SDL_AllocateEventString(text);
if (!event.edit.text) {
return 0;
}
SDL_memcpy(event.edit.text, text, size);
posted = (SDL_PushEvent(&event) > 0);
}
return posted;