1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 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 
28 int
Wayland_SetClipboardText(_THIS,const char * text)29 Wayland_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         /* TODO: Support more than one seat */
41         data_device = Wayland_get_data_device(video_data->input);
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                                          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     return status;
57 }
58 
59 char *
Wayland_GetClipboardText(_THIS)60 Wayland_GetClipboardText(_THIS)
61 {
62     SDL_VideoData *video_data = NULL;
63     SDL_WaylandDataDevice *data_device = NULL;
64 
65     char *text = NULL;
66 
67     void *buffer = NULL;
68     size_t length = 0;
69 
70     if (_this == NULL || _this->driverdata == NULL) {
71         SDL_SetError("Video driver uninitialized");
72     } else {
73         video_data = _this->driverdata;
74         /* TODO: Support more than one seat */
75         data_device = Wayland_get_data_device(video_data->input);
76         if (data_device->selection_offer != NULL) {
77             buffer = Wayland_data_offer_receive(data_device->selection_offer,
78                                                 &length, TEXT_MIME, SDL_TRUE);
79             if (length > 0) {
80                 text = (char*) buffer;
81             }
82         } else if (data_device->selection_source != NULL) {
83             buffer = Wayland_data_source_get_data(data_device->selection_source,
84                                                   &length, TEXT_MIME, SDL_TRUE);
85             if (length > 0) {
86                 text = (char*) buffer;
87             }
88         }
89     }
90 
91     if (text == NULL) {
92         text = SDL_strdup("");
93     }
94 
95     return text;
96 }
97 
98 SDL_bool
Wayland_HasClipboardText(_THIS)99 Wayland_HasClipboardText(_THIS)
100 {
101     SDL_VideoData *video_data = NULL;
102     SDL_WaylandDataDevice *data_device = NULL;
103 
104     SDL_bool result = SDL_FALSE;
105     if (_this == NULL || _this->driverdata == NULL) {
106         SDL_SetError("Video driver uninitialized");
107     } else {
108         video_data = _this->driverdata;
109         data_device = Wayland_get_data_device(video_data->input);
110         if (data_device != NULL && Wayland_data_offer_has_mime(
111                 data_device->selection_offer, TEXT_MIME)) {
112             result = SDL_TRUE;
113         } else if(data_device != NULL && Wayland_data_source_has_mime(
114                 data_device->selection_source, TEXT_MIME)) {
115             result = SDL_TRUE;
116         }
117     }
118     return result;
119 }
120 
121 #endif /* SDL_VIDEO_DRIVER_WAYLAND */
122 
123 /* vi: set ts=4 sw=4 expandtab: */
124