1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2021 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#if SDL_VIDEO_DRIVER_WAYLAND
24
25#include "SDL_waylanddatamanager.h"
26#include "SDL_waylandevents_c.h"
27
28int
29Wayland_SetClipboardText(_THIS, const char *text)
30{
31 SDL_VideoData *video_data = NULL;
32 SDL_WaylandDataDevice *data_device = NULL;
33
34 int status = 0;
35
36 if (_this == NULL || _this->driverdata == NULL) {
37 status = SDL_SetError("Video driver uninitialized");
38 } else {
39 video_data = _this->driverdata;
40 if (video_data->input != NULL && video_data->input->data_device != NULL) {
41 data_device = video_data->input->data_device;
42 if (text[0] != '\0') {
43 SDL_WaylandDataSource* source = Wayland_data_source_create(_this);
44 Wayland_data_source_add_data(source, TEXT_MIME, text,
45 SDL_strlen(text) + 1);
46
47 status = Wayland_data_device_set_selection(data_device, source);
48 if (status != 0) {
49 Wayland_data_source_destroy(source);
50 }
51 } else {
52 status = Wayland_data_device_clear_selection(data_device);
53 }
54 }
55 }
56
57 return status;
58}
59
60char *
61Wayland_GetClipboardText(_THIS)
62{
63 SDL_VideoData *video_data = NULL;
64 SDL_WaylandDataDevice *data_device = NULL;
65
66 char *text = NULL;
67
68 void *buffer = NULL;
69 size_t length = 0;
70
71 if (_this == NULL || _this->driverdata == NULL) {
72 SDL_SetError("Video driver uninitialized");
73 } else {
74 video_data = _this->driverdata;
75 if (video_data->input != NULL && video_data->input->data_device != NULL) {
76 data_device = video_data->input->data_device;
77 if (data_device->selection_offer != NULL) {
78 buffer = Wayland_data_offer_receive(data_device->selection_offer,
79 &length, TEXT_MIME, SDL_TRUE);
80 if (length > 0) {
81 text = (char*) buffer;
82 }
83 }
84 if (length == 0 && data_device->selection_source != NULL) {
85 buffer = Wayland_data_source_get_data(data_device->selection_source,
86 &length, TEXT_MIME, SDL_TRUE);
87 if (length > 0) {
88 text = (char*) buffer;
89 }
90 }
91 }
92 }
93
94 if (text == NULL) {
95 text = SDL_strdup("");
96 }
97
98 return text;
99}
100
101SDL_bool
102Wayland_HasClipboardText(_THIS)
103{
104 SDL_VideoData *video_data = NULL;
105 SDL_WaylandDataDevice *data_device = NULL;
106
107 SDL_bool result = SDL_FALSE;
108 if (_this == NULL || _this->driverdata == NULL) {
109 SDL_SetError("Video driver uninitialized");
110 } else {
111 video_data = _this->driverdata;
112 if (video_data->input != NULL && video_data->input->data_device != NULL) {
113 data_device = video_data->input->data_device;
114 if (Wayland_data_offer_has_mime(
115 data_device->selection_offer, TEXT_MIME)) {
116 result = SDL_TRUE;
117 } else if (Wayland_data_source_has_mime(
118 data_device->selection_source, TEXT_MIME)) {
119 result = SDL_TRUE;
120 }
121 }
122 }
123 return result;
124}
125
126#endif /* SDL_VIDEO_DRIVER_WAYLAND */
127
128/* vi: set ts=4 sw=4 expandtab: */
129