summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testclipboard.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testclipboard.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testclipboard.c201
1 files changed, 201 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testclipboard.c b/src/contrib/SDL-3.2.20/test/testclipboard.c
new file mode 100644
index 0000000..9dad02b
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testclipboard.c
@@ -0,0 +1,201 @@
1/*
2 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
3
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
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12
13#define SDL_MAIN_USE_CALLBACKS 1
14#include <SDL3/SDL.h>
15#include <SDL3/SDL_main.h>
16
17#include "icon.h"
18
19static SDL_Window *window = NULL;
20static SDL_Renderer *renderer = NULL;
21
22static const char *mime_types[] = {
23 "text/plain",
24 "image/bmp",
25};
26
27static const void *ClipboardDataCallback(void *userdata, const char *mime_type, size_t *size)
28{
29 if (SDL_strcmp(mime_type, "text/plain") == 0) {
30 const char *text = "Hello world!";
31 *size = SDL_strlen(text);
32 return text;
33 } else if (SDL_strcmp(mime_type, "image/bmp") == 0) {
34 *size = icon_bmp_len;
35 return icon_bmp;
36 } else {
37 return NULL;
38 }
39}
40
41SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
42{
43 if (!SDL_Init(SDL_INIT_VIDEO)) {
44 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
45 return SDL_APP_FAILURE;
46 }
47
48 if (!SDL_CreateWindowAndRenderer("testclipboard", 640, 480, 0, &window, &renderer)) {
49 SDL_Log("Couldn't create window and renderer: %s", SDL_GetError());
50 return SDL_APP_FAILURE;
51 }
52 return SDL_APP_CONTINUE;
53}
54
55
56SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
57{
58 switch (event->type) {
59 case SDL_EVENT_KEY_DOWN:
60 if (event->key.key == SDLK_ESCAPE) {
61 return SDL_APP_SUCCESS;
62 }
63 if (event->key.key == SDLK_C && event->key.mod & SDL_KMOD_CTRL) {
64 SDL_SetClipboardData(ClipboardDataCallback, NULL, NULL, mime_types, SDL_arraysize(mime_types));
65 break;
66 } else if (event->key.key == SDLK_P && event->key.mod & SDL_KMOD_CTRL) {
67 SDL_SetPrimarySelectionText("SDL Primary Selection Text!");
68 }
69 break;
70
71 case SDL_EVENT_CLIPBOARD_UPDATE:
72 if (event->clipboard.num_mime_types > 0) {
73 int i;
74 SDL_Log("Clipboard updated:");
75 for (i = 0; event->clipboard.mime_types[i]; ++i) {
76 SDL_Log(" %s", event->clipboard.mime_types[i]);
77 }
78 } else {
79 SDL_Log("Clipboard cleared");
80 }
81 break;
82
83 case SDL_EVENT_QUIT:
84 return SDL_APP_SUCCESS;
85
86 default:
87 break;
88 }
89
90 return SDL_APP_CONTINUE;
91}
92
93static float PrintClipboardText(float x, float y, const char *mime_type)
94{
95 void *data = SDL_GetClipboardData(mime_type, NULL);
96 if (data) {
97 SDL_RenderDebugText(renderer, x, y, (const char *)data);
98 SDL_free(data);
99 return SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2.0f;
100 }
101 return 0.0f;
102}
103
104static float PrintPrimarySelectionText(float x, float y)
105{
106 if (SDL_HasPrimarySelectionText()) {
107 SDL_RenderDebugText(renderer, x, y, SDL_GetPrimarySelectionText());
108 return SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2.0f;
109 }
110 return 0.0f;
111}
112
113static float PrintClipboardImage(float x, float y, const char *mime_type)
114{
115 /* We don't actually need to read this data each frame, but this is a simple example */
116 if (SDL_strcmp(mime_type, "image/bmp") == 0) {
117 size_t size;
118 void *data = SDL_GetClipboardData(mime_type, &size);
119 if (data) {
120 float w = 0.0f, h = 0.0f;
121 bool rendered = false;
122 SDL_IOStream *stream = SDL_IOFromConstMem(data, size);
123 if (stream) {
124 SDL_Surface *surface = SDL_LoadBMP_IO(stream, false);
125 if (surface) {
126 SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
127 if (texture) {
128 SDL_GetTextureSize(texture, &w, &h);
129
130 SDL_FRect dst = { x, y, w, h };
131 rendered = SDL_RenderTexture(renderer, texture, NULL, &dst);
132 SDL_DestroyTexture(texture);
133 }
134 SDL_DestroySurface(surface);
135 }
136 SDL_CloseIO(stream);
137 }
138 if (!rendered) {
139 SDL_RenderDebugText(renderer, x, y, SDL_GetError());
140 }
141 SDL_free(data);
142 return h + 2.0f;
143 }
144 }
145 return 0.0f;
146}
147
148static float PrintClipboardContents(float x, float y)
149{
150 char **clipboard_mime_types = SDL_GetClipboardMimeTypes(NULL);
151 if (clipboard_mime_types) {
152 int i;
153
154 for (i = 0; clipboard_mime_types[i]; ++i) {
155 const char *mime_type = clipboard_mime_types[i];
156 SDL_RenderDebugText(renderer, x, y, mime_type);
157 y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2;
158 if (SDL_strncmp(mime_type, "text/", 5) == 0) {
159 y += PrintClipboardText(x + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2, y, mime_type);
160 } else if (SDL_strncmp(mime_type, "image/", 6) == 0) {
161 y += PrintClipboardImage(x + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2, y, mime_type);
162 }
163 }
164 SDL_free(clipboard_mime_types);
165 }
166
167 return y;
168}
169
170SDL_AppResult SDL_AppIterate(void *appstate)
171{
172 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
173 SDL_RenderClear(renderer);
174
175 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
176 float x = 4.0f;
177 float y = 4.0f;
178 SDL_RenderDebugText(renderer, x, y, "Press Ctrl+C to copy content to the clipboard");
179 y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2;
180 SDL_RenderDebugText(renderer, x, y, "Press Ctrl+P to set the primary selection text");
181 y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2;
182 SDL_RenderDebugText(renderer, x, y, "Clipboard contents:");
183 x += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2;
184 y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2;
185 y = PrintClipboardContents(x, y);
186 if (SDL_HasPrimarySelectionText()) {
187 x = 4.0f;
188 SDL_RenderDebugText(renderer, x, y, "Primary selection text contents:");
189 y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2;
190 PrintPrimarySelectionText(x + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2, y);
191 }
192
193 SDL_RenderPresent(renderer);
194
195 return SDL_APP_CONTINUE;
196}
197
198void SDL_AppQuit(void *appstate, SDL_AppResult result)
199{
200}
201