Atlas - SDL_waylandclipboard.c

Home / ext / SDL / src / video / wayland Lines: 1 | Size: 7618 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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_WaylandSeat *seat = video_data->last_implicit_grab_seat; 36 bool result = false; 37 38 // If no implicit grab is available yet, just attach it to the first available seat. 39 if (!seat && !WAYLAND_wl_list_empty(&video_data->seat_list)) { 40 seat = wl_container_of(video_data->seat_list.next, seat, link); 41 } 42 43 video_data->last_incoming_data_offer_seat = seat; 44 45 if (seat && seat->data_device) { 46 SDL_WaylandDataDevice *data_device = seat->data_device; 47 48 if (_this->clipboard_callback && _this->clipboard_mime_types) { 49 SDL_WaylandDataSource *source = Wayland_data_source_create(_this); 50 Wayland_data_source_set_callback(source, _this->clipboard_callback, _this->clipboard_userdata, _this->clipboard_sequence); 51 52 result = Wayland_data_device_set_selection(data_device, source, (const char **)_this->clipboard_mime_types, _this->num_clipboard_mime_types); 53 if (!result) { 54 Wayland_data_source_destroy(source); 55 } 56 } else { 57 result = Wayland_data_device_clear_selection(data_device); 58 } 59 } 60 61 return result; 62} 63 64void *Wayland_GetClipboardData(SDL_VideoDevice *_this, const char *mime_type, size_t *length) 65{ 66 SDL_VideoData *video_data = _this->internal; 67 SDL_WaylandSeat *seat = video_data->last_incoming_data_offer_seat; 68 void *buffer = NULL; 69 70 if (seat && seat->data_device) { 71 SDL_WaylandDataDevice *data_device = seat->data_device; 72 if (data_device->selection_source) { 73 buffer = SDL_GetInternalClipboardData(_this, mime_type, length); 74 } else if (Wayland_data_offer_has_mime(data_device->selection_offer, mime_type)) { 75 buffer = Wayland_data_offer_receive(data_device->selection_offer, mime_type, length); 76 } 77 } 78 79 return buffer; 80} 81 82bool Wayland_HasClipboardData(SDL_VideoDevice *_this, const char *mime_type) 83{ 84 SDL_VideoData *video_data = _this->internal; 85 SDL_WaylandSeat *seat = video_data->last_incoming_data_offer_seat; 86 bool result = false; 87 88 if (seat && seat->data_device) { 89 SDL_WaylandDataDevice *data_device = seat->data_device; 90 if (data_device->selection_source) { 91 result = SDL_HasInternalClipboardData(_this, mime_type); 92 } else { 93 result = Wayland_data_offer_has_mime(data_device->selection_offer, mime_type); 94 } 95 } 96 return result; 97} 98 99static const char *text_mime_types[] = { 100 TEXT_MIME, 101 "text/plain", 102 "TEXT", 103 "UTF8_STRING", 104 "STRING" 105}; 106 107const char **Wayland_GetTextMimeTypes(SDL_VideoDevice *_this, size_t *num_mime_types) 108{ 109 *num_mime_types = SDL_arraysize(text_mime_types); 110 return text_mime_types; 111} 112 113bool Wayland_SetPrimarySelectionText(SDL_VideoDevice *_this, const char *text) 114{ 115 SDL_VideoData *video_data = _this->internal; 116 SDL_WaylandSeat *seat = video_data->last_implicit_grab_seat; 117 bool result; 118 119 // If no implicit grab is available yet, just attach it to the first available seat. 120 if (!seat && !WAYLAND_wl_list_empty(&video_data->seat_list)) { 121 seat = wl_container_of(video_data->seat_list.next, seat, link); 122 } 123 124 video_data->last_incoming_primary_selection_seat = seat; 125 126 if (seat && seat->primary_selection_device) { 127 SDL_WaylandPrimarySelectionDevice *primary_selection_device = seat->primary_selection_device; 128 if (text[0] != '\0') { 129 SDL_WaylandPrimarySelectionSource *source = Wayland_primary_selection_source_create(_this); 130 Wayland_primary_selection_source_set_callback(source, SDL_ClipboardTextCallback, SDL_strdup(text)); 131 132 result = Wayland_primary_selection_device_set_selection(primary_selection_device, 133 source, 134 text_mime_types, 135 SDL_arraysize(text_mime_types)); 136 if (!result) { 137 Wayland_primary_selection_source_destroy(source); 138 } 139 } else { 140 result = Wayland_primary_selection_device_clear_selection(primary_selection_device); 141 } 142 } else { 143 result = SDL_SetError("Primary selection not supported"); 144 } 145 return result; 146} 147 148char *Wayland_GetPrimarySelectionText(SDL_VideoDevice *_this) 149{ 150 SDL_VideoData *video_data = _this->internal; 151 SDL_WaylandSeat *seat = video_data->last_incoming_primary_selection_seat; 152 char *text = NULL; 153 size_t length = 0; 154 155 if (seat && seat->primary_selection_device) { 156 SDL_WaylandPrimarySelectionDevice *primary_selection_device = seat->primary_selection_device; 157 if (primary_selection_device->selection_source) { 158 text = Wayland_primary_selection_source_get_data(primary_selection_device->selection_source, TEXT_MIME, &length); 159 } else { 160 for (size_t i = 0; i < SDL_arraysize(text_mime_types); i++) { 161 if (Wayland_primary_selection_offer_has_mime(primary_selection_device->selection_offer, text_mime_types[i])) { 162 text = Wayland_primary_selection_offer_receive(primary_selection_device->selection_offer, text_mime_types[i], &length); 163 break; 164 } 165 } 166 } 167 } 168 169 if (!text) { 170 text = SDL_strdup(""); 171 } 172 173 return text; 174} 175 176bool Wayland_HasPrimarySelectionText(SDL_VideoDevice *_this) 177{ 178 SDL_VideoData *video_data = _this->internal; 179 SDL_WaylandSeat *seat = video_data->last_incoming_primary_selection_seat; 180 bool result = false; 181 182 if (seat && seat->primary_selection_device) { 183 SDL_WaylandPrimarySelectionDevice *primary_selection_device = seat->primary_selection_device; 184 if (primary_selection_device->selection_source) { 185 result = true; 186 } else { 187 size_t mime_count = 0; 188 const char **mime_types = Wayland_GetTextMimeTypes(_this, &mime_count); 189 for (size_t i = 0; i < mime_count; i++) { 190 if (Wayland_primary_selection_offer_has_mime(primary_selection_device->selection_offer, mime_types[i])) { 191 result = true; 192 break; 193 } 194 } 195 } 196 } 197 return result; 198} 199 200#endif // SDL_VIDEO_DRIVER_WAYLAND 201
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.