summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/video/wayland/SDL_waylandclipboard.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/video/wayland/SDL_waylandclipboard.c
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/wayland/SDL_waylandclipboard.c')
-rw-r--r--contrib/SDL-3.2.8/src/video/wayland/SDL_waylandclipboard.c174
1 files changed, 174 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/wayland/SDL_waylandclipboard.c b/contrib/SDL-3.2.8/src/video/wayland/SDL_waylandclipboard.c
new file mode 100644
index 0000000..6218733
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/video/wayland/SDL_waylandclipboard.c
@@ -0,0 +1,174 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23#ifdef SDL_VIDEO_DRIVER_WAYLAND
24
25#include "SDL_waylanddatamanager.h"
26#include "SDL_waylandevents_c.h"
27#include "SDL_waylandclipboard.h"
28#include "../SDL_clipboard_c.h"
29#include "../../events/SDL_events_c.h"
30
31
32bool Wayland_SetClipboardData(SDL_VideoDevice *_this)
33{
34 SDL_VideoData *video_data = _this->internal;
35 SDL_WaylandDataDevice *data_device = NULL;
36 bool result = true;
37
38 if (video_data->input && video_data->input->data_device) {
39 data_device = video_data->input->data_device;
40
41 if (_this->clipboard_callback && _this->clipboard_mime_types) {
42 SDL_WaylandDataSource *source = Wayland_data_source_create(_this);
43 Wayland_data_source_set_callback(source, _this->clipboard_callback, _this->clipboard_userdata, _this->clipboard_sequence);
44
45 result = Wayland_data_device_set_selection(data_device, source, (const char **)_this->clipboard_mime_types, _this->num_clipboard_mime_types);
46 if (!result) {
47 Wayland_data_source_destroy(source);
48 }
49 } else {
50 result = Wayland_data_device_clear_selection(data_device);
51 }
52 }
53
54 return result;
55}
56
57void *Wayland_GetClipboardData(SDL_VideoDevice *_this, const char *mime_type, size_t *length)
58{
59 SDL_VideoData *video_data = _this->internal;
60 SDL_WaylandDataDevice *data_device = NULL;
61 void *buffer = NULL;
62
63 if (video_data->input && video_data->input->data_device) {
64 data_device = video_data->input->data_device;
65 if (data_device->selection_source) {
66 buffer = SDL_GetInternalClipboardData(_this, mime_type, length);
67 } else if (Wayland_data_offer_has_mime(data_device->selection_offer, mime_type)) {
68 buffer = Wayland_data_offer_receive(data_device->selection_offer, mime_type, length);
69 }
70 }
71
72 return buffer;
73}
74
75bool Wayland_HasClipboardData(SDL_VideoDevice *_this, const char *mime_type)
76{
77 SDL_VideoData *video_data = _this->internal;
78 SDL_WaylandDataDevice *data_device = NULL;
79 bool result = false;
80
81 if (video_data->input && video_data->input->data_device) {
82 data_device = video_data->input->data_device;
83 if (data_device->selection_source) {
84 result = SDL_HasInternalClipboardData(_this, mime_type);
85 } else {
86 result = Wayland_data_offer_has_mime(data_device->selection_offer, mime_type);
87 }
88 }
89 return result;
90}
91
92static const char *text_mime_types[] = {
93 TEXT_MIME,
94 "text/plain",
95 "TEXT",
96 "UTF8_STRING",
97 "STRING"
98};
99
100const char **Wayland_GetTextMimeTypes(SDL_VideoDevice *_this, size_t *num_mime_types)
101{
102 *num_mime_types = SDL_arraysize(text_mime_types);
103 return text_mime_types;
104}
105
106bool Wayland_SetPrimarySelectionText(SDL_VideoDevice *_this, const char *text)
107{
108 SDL_VideoData *video_data = _this->internal;
109 SDL_WaylandPrimarySelectionDevice *primary_selection_device = NULL;
110 bool result;
111
112 if (video_data->input && video_data->input->primary_selection_device) {
113 primary_selection_device = video_data->input->primary_selection_device;
114 if (text[0] != '\0') {
115 SDL_WaylandPrimarySelectionSource *source = Wayland_primary_selection_source_create(_this);
116 Wayland_primary_selection_source_set_callback(source, SDL_ClipboardTextCallback, SDL_strdup(text));
117
118 result = Wayland_primary_selection_device_set_selection(primary_selection_device,
119 source,
120 text_mime_types,
121 SDL_arraysize(text_mime_types));
122 if (!result) {
123 Wayland_primary_selection_source_destroy(source);
124 }
125 } else {
126 result = Wayland_primary_selection_device_clear_selection(primary_selection_device);
127 }
128 } else {
129 result = SDL_SetError("Primary selection not supported");
130 }
131 return result;
132}
133
134char *Wayland_GetPrimarySelectionText(SDL_VideoDevice *_this)
135{
136 SDL_VideoData *video_data = _this->internal;
137 SDL_WaylandPrimarySelectionDevice *primary_selection_device = NULL;
138 char *text = NULL;
139 size_t length = 0;
140
141 if (video_data->input && video_data->input->primary_selection_device) {
142 primary_selection_device = video_data->input->primary_selection_device;
143 if (primary_selection_device->selection_source) {
144 text = Wayland_primary_selection_source_get_data(primary_selection_device->selection_source, TEXT_MIME, &length);
145 } else if (Wayland_primary_selection_offer_has_mime(primary_selection_device->selection_offer, TEXT_MIME)) {
146 text = Wayland_primary_selection_offer_receive(primary_selection_device->selection_offer, TEXT_MIME, &length);
147 }
148 }
149
150 if (!text) {
151 text = SDL_strdup("");
152 }
153
154 return text;
155}
156
157bool Wayland_HasPrimarySelectionText(SDL_VideoDevice *_this)
158{
159 SDL_VideoData *video_data = _this->internal;
160 SDL_WaylandPrimarySelectionDevice *primary_selection_device = NULL;
161 bool result = false;
162
163 if (video_data->input && video_data->input->primary_selection_device) {
164 primary_selection_device = video_data->input->primary_selection_device;
165 if (primary_selection_device->selection_source) {
166 result = true;
167 } else {
168 result = Wayland_primary_selection_offer_has_mime(primary_selection_device->selection_offer, TEXT_MIME);
169 }
170 }
171 return result;
172}
173
174#endif // SDL_VIDEO_DRIVER_WAYLAND