summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testclipboard.c
blob: 9dad02be79c520a0bec18dabedfaa187f1f4b382 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
  Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely.
*/

#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>

#include "icon.h"

static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;

static const char *mime_types[] = {
    "text/plain",
    "image/bmp",
};

static const void *ClipboardDataCallback(void *userdata, const char *mime_type, size_t *size)
{
    if (SDL_strcmp(mime_type, "text/plain") == 0) {
        const char *text = "Hello world!";
        *size = SDL_strlen(text);
        return text;
    } else if (SDL_strcmp(mime_type, "image/bmp") == 0) {
        *size = icon_bmp_len;
        return icon_bmp;
    } else {
        return NULL;
    }
}

SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
    if (!SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }

    if (!SDL_CreateWindowAndRenderer("testclipboard", 640, 480, 0, &window, &renderer)) {
        SDL_Log("Couldn't create window and renderer: %s", SDL_GetError());
        return SDL_APP_FAILURE;
    }
    return SDL_APP_CONTINUE;
}


SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
    switch (event->type) {
    case SDL_EVENT_KEY_DOWN:
        if (event->key.key == SDLK_ESCAPE) {
            return SDL_APP_SUCCESS;
        }
        if (event->key.key == SDLK_C && event->key.mod & SDL_KMOD_CTRL) {
            SDL_SetClipboardData(ClipboardDataCallback, NULL, NULL, mime_types, SDL_arraysize(mime_types));
            break;
        } else if (event->key.key == SDLK_P && event->key.mod & SDL_KMOD_CTRL) {
            SDL_SetPrimarySelectionText("SDL Primary Selection Text!");
        }
        break;

    case SDL_EVENT_CLIPBOARD_UPDATE:
        if (event->clipboard.num_mime_types > 0) {
            int i;
            SDL_Log("Clipboard updated:");
            for (i = 0; event->clipboard.mime_types[i]; ++i) {
                SDL_Log("    %s", event->clipboard.mime_types[i]);
            }
        } else {
            SDL_Log("Clipboard cleared");
        }
        break;

    case SDL_EVENT_QUIT:
        return SDL_APP_SUCCESS;

    default:
        break;
    }

    return SDL_APP_CONTINUE;
}

static float PrintClipboardText(float x, float y, const char *mime_type)
{
    void *data = SDL_GetClipboardData(mime_type, NULL);
    if (data) {
        SDL_RenderDebugText(renderer, x, y, (const char *)data);
        SDL_free(data);
        return SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2.0f;
    }
    return 0.0f;
}

static float PrintPrimarySelectionText(float x, float y)
{
    if (SDL_HasPrimarySelectionText()) {
        SDL_RenderDebugText(renderer, x, y, SDL_GetPrimarySelectionText());
        return SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2.0f;
    }
    return 0.0f;
}

static float PrintClipboardImage(float x, float y, const char *mime_type)
{
    /* We don't actually need to read this data each frame, but this is a simple example */
    if (SDL_strcmp(mime_type, "image/bmp") == 0) {
        size_t size;
        void *data = SDL_GetClipboardData(mime_type, &size);
        if (data) {
            float w = 0.0f, h = 0.0f;
            bool rendered = false;
            SDL_IOStream *stream = SDL_IOFromConstMem(data, size);
            if (stream) {
                SDL_Surface *surface = SDL_LoadBMP_IO(stream, false);
                if (surface) {
                    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
                    if (texture) {
                        SDL_GetTextureSize(texture, &w, &h);

                        SDL_FRect dst = { x, y, w, h };
                        rendered = SDL_RenderTexture(renderer, texture, NULL, &dst);
                        SDL_DestroyTexture(texture);
                    }
                    SDL_DestroySurface(surface);
                }
                SDL_CloseIO(stream);
            }
            if (!rendered) {
                SDL_RenderDebugText(renderer, x, y, SDL_GetError());
            }
            SDL_free(data);
            return h + 2.0f;
        }
    }
    return 0.0f;
}

static float PrintClipboardContents(float x, float y)
{
    char **clipboard_mime_types = SDL_GetClipboardMimeTypes(NULL);
    if (clipboard_mime_types) {
        int i;

        for (i = 0; clipboard_mime_types[i]; ++i) {
            const char *mime_type = clipboard_mime_types[i];
            SDL_RenderDebugText(renderer, x, y, mime_type);
            y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2;
            if (SDL_strncmp(mime_type, "text/", 5) == 0) {
                y += PrintClipboardText(x + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2, y, mime_type);
            } else if (SDL_strncmp(mime_type, "image/", 6) == 0) {
                y += PrintClipboardImage(x + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2, y, mime_type);
            }
        }
        SDL_free(clipboard_mime_types);
    }

    return y;
}

SDL_AppResult SDL_AppIterate(void *appstate)
{
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    float x = 4.0f;
    float y = 4.0f;
    SDL_RenderDebugText(renderer, x, y, "Press Ctrl+C to copy content to the clipboard");
    y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2;
    SDL_RenderDebugText(renderer, x, y, "Press Ctrl+P to set the primary selection text");
    y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2;
    SDL_RenderDebugText(renderer, x, y, "Clipboard contents:");
    x += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2;
    y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2;
    y = PrintClipboardContents(x, y);
    if (SDL_HasPrimarySelectionText()) {
        x = 4.0f;
        SDL_RenderDebugText(renderer, x, y, "Primary selection text contents:");
        y += SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE + 2;
        PrintPrimarySelectionText(x + SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2, y);
    }

    SDL_RenderPresent(renderer);

    return SDL_APP_CONTINUE;
}

void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
}