Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/clipboard.cpp

raw
1 /*
2 * Copyright (C) 2022 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2022 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-ws-lib
6 * Created on: 19 июл. 2022 г.
7 *
8 * lsp-ws-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-ws-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-ws-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/io/InMemoryStream.h>
23 #include <lsp-plug.in/io/OutMemoryStream.h>
24 #include <lsp-plug.in/stdlib/stdio.h>
25 #include <lsp-plug.in/test-fw/mtest.h>
26 #include <lsp-plug.in/ws/factory.h>
27 #include <lsp-plug.in/ws/IEventHandler.h>
28
29 namespace
30 {
31 static const char * const mimes[] =
32 {
33 "UTF8_STRING",
34 "text/plain;charset=utf-8",
35 "text/plain;charset=UTF-16LE",
36 "text/plain;charset=UTF-16BE",
37 "text/plain;charset=US-ASCII",
38 "text/plain",
39 NULL
40 };
41
42 static const char *characters =
43 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
44 }
45
46 MTEST_BEGIN("ws.display", clipboard)
47
48 // Clipboard data source
49 class CbDataSource: public ws::IDataSource
50 {
51 private:
52 LSPString sText;
53 test_type_t *pTest;
54
55 public:
56 explicit CbDataSource(test_type_t *test, const LSPString &text) : ws::IDataSource(mimes)
57 {
58 pTest = test;
59 sText.set(&text);
60 }
61
62 virtual ~CbDataSource()
63 {
64 }
65
66 public:
67 virtual io::IInStream *open(const char *mime) override
68 {
69 // Scan supported MIME types
70 ssize_t idx = -1, i=0;
71 for (const char *const *p = mimes; *p != NULL; ++p, ++i)
72 {
73 if (!::strcasecmp(mimes[i], mime))
74 {
75 idx = i;
76 break;
77 }
78 }
79
80 // Analyze found MIME type
81 void *data = NULL;
82 size_t bytes = 0;
83 switch (idx)
84 {
85 case 0: // UTF8_STRING
86 case 1: // text/plain;charset=utf-8
87 data = sText.clone_utf8(&bytes);
88 bytes -= sizeof(char); // 1 extra byte for zero character
89 break;
90 case 2: // text/plain;charset=UTF-16LE
91 data = __IF_LEBE(
92 sText.clone_utf16(&bytes),
93 sText.clone_native(&bytes, "UTF16-LE")
94 );
95 bytes -= sizeof(lsp_utf16_t); // 2 extra bytes for zero character
96 break;
97 case 3: // text/plain;charset=UTF-16BE
98 data = __IF_LEBE(
99 sText.clone_native(&bytes, "UTF16-BE"),
100 sText.clone_utf16(&bytes)
101 );
102 bytes -= sizeof(lsp_utf16_t); // 2 extra bytes for zero character
103 break;
104 case 4:
105 data = sText.clone_ascii(&bytes);
106 bytes -= sizeof(char); // 1 extra byte for zero character
107 break;
108 case 5:
109 data = sText.clone_native(&bytes);
110 bytes -= sizeof(char); // 4 extra byte for zero character
111 break;
112 default:
113 break;
114 }
115
116 // Format not supported?
117 if (data == NULL)
118 return NULL;
119
120 // Allocate memory stream;
121 io::InMemoryStream *stream = new io::InMemoryStream(data, bytes, MEMDROP_FREE);
122 if (stream == NULL)
123 {
124 ::free(data);
125 return NULL;
126 }
127
128 return stream;
129 }
130 };
131
132 // Clipboard data sink
133 class CbDataSink: public ws::IDataSink
134 {
135 protected:
136 test_type_t *pTest;
137 io::OutMemoryStream sOS;
138 ssize_t nMime;
139 const char *pMime;
140
141 public:
142 explicit CbDataSink(test_type_t *test)
143 {
144 pTest = test;
145 nMime = -1;
146 pMime = NULL;
147 }
148
149 virtual ~CbDataSink()
150 {
151 clear();
152 }
153
154 public:
155 inline const char *mime() const { return pMime; }
156
157 public:
158 void clear()
159 {
160 sOS.drop();
161 nMime = -1;
162 pMime = NULL;
163 }
164
165 virtual ssize_t open(const char * const *mime_types) override
166 {
167 ssize_t self_idx = 0, found = -1;
168
169 for (const char *const *p = mimes; (*p != NULL) && (found < 0); ++p, ++self_idx)
170 {
171 ssize_t src_idx = 0;
172 for (const char *const *v = mime_types; (*v != NULL) && (found < 0); ++v, ++src_idx)
173 {
174 if (!::strcasecmp(*p, *v))
175 {
176 found = src_idx;
177 nMime = self_idx;
178 pMime = *p;
179 break;
180 }
181 }
182 }
183
184 if (found < 0)
185 return -STATUS_UNSUPPORTED_FORMAT;
186
187 pTest->printf("Selected mime type: %s, index=%d\n", pMime, found);
188 return found;
189 }
190
191 virtual status_t write(const void *buf, size_t count) override
192 {
193 if (pMime == NULL)
194 return STATUS_CLOSED;
195 ssize_t written = sOS.write(buf, count);
196 return (written >= ssize_t(count)) ? STATUS_OK : STATUS_UNKNOWN_ERR;
197 }
198
199 virtual status_t close(status_t code) override
200 {
201 if (pMime == NULL)
202 {
203 clear();
204 return STATUS_OK;
205 }
206
207 // Commit data
208 LSPString tmp;
209
210 if (code == STATUS_OK)
211 {
212 bool ok = false;
213 code = STATUS_NO_MEM;
214
215 switch (nMime)
216 {
217 case 0: // text/plain;charset=utf-8
218 case 1: // UTF8_STRING
219 ok = tmp.set_utf8(reinterpret_cast<const char *>(sOS.data()), sOS.size());
220 break;
221 case 2: // text/plain;charset=UTF-16LE
222 ok =
223 __IF_LEBE(
224 tmp.set_utf16(reinterpret_cast<const lsp_utf16_t *>(sOS.data())),
225 tmp.set_native(reinterpret_cast<const char *>(sOS.data()), "UTF16-LE")
226 );
227 break;
228 case 3: // text/plain;charset=UTF-16BE
229 ok =
230 __IF_LEBE(
231 tmp.set_native(reinterpret_cast<const char *>(sOS.data()), "UTF16-BE"),
232 tmp.set_utf16(reinterpret_cast<const lsp_utf16_t *>(sOS.data()))
233 );
234 break;
235 case 4: // text/plain;charset=US-ASCII
236 ok = tmp.set_ascii(reinterpret_cast<const char *>(sOS.data()));
237 break;
238 case 5: // text/plain
239 ok = tmp.set_native(reinterpret_cast<const char *>(sOS.data()), sOS.size());
240 break;
241 default:
242 code = STATUS_UNSUPPORTED_FORMAT;
243 break;
244 }
245
246 // Successful set?
247 if (ok)
248 code = STATUS_OK;
249 }
250
251 // Drop data
252 clear();
253
254 // Submit fetched data to callback method
255 if (code == 0)
256 pTest->printf("Received clipboard data:\n%s\n", tmp.get_native());
257 else
258 pTest->printf("Failed to receive clipboard data: error=%d\n", int(code));
259
260 return code;
261 }
262 };
263
264 class Handler: public ws::IEventHandler
265 {
266 private:
267 test_type_t *pTest;
268 ws::IWindow *pWnd;
269
270 public:
271 inline Handler(test_type_t *test, ws::IWindow *wnd)
272 {
273 pTest = test;
274 pWnd = wnd;
275 }
276
277 void copy_to_clipboard(size_t id)
278 {
279 // Generate some random string
280 LSPString tmp;
281 size_t n = strlen(characters);
282
283 for (size_t r=0; r<8; ++r)
284 {
285 for (size_t c=0; c<64; ++c)
286 tmp.append(characters[rand() % n]);
287 tmp.append('\n');
288 }
289
290 // Create clpboard data source
291 CbDataSource *ds = new CbDataSource(pTest, tmp);
292 if (ds == NULL)
293 return;
294 ds->acquire();
295 lsp_finally{ ds->release(); };
296
297 // Commit data source to clipboard
298 pWnd->display()->set_clipboard(id, ds);
299
300 // Output message to console
301 pTest->printf("Submitted clipboard data:\n%s\n", tmp.get_native());
302 }
303
304 void paste_from_clipboard(size_t id)
305 {
306 CbDataSink *ds = new CbDataSink(pTest);
307 if (ds == NULL)
308 return;
309 ds->acquire();
310 lsp_finally{ ds->release(); };
311
312 pWnd->display()->get_clipboard(id, ds);
313 }
314
315 virtual status_t handle_event(const ws::event_t *ev) override
316 {
317 switch (ev->nType)
318 {
319 case ws::UIE_MOUSE_CLICK:
320 {
321 if (ev->nState & ws::MCF_CONTROL)
322 {
323 if (ev->nCode == ws::MCB_LEFT)
324 copy_to_clipboard(ws::CBUF_PRIMARY);
325 else if (ev->nCode == ws::MCB_RIGHT)
326 copy_to_clipboard(ws::CBUF_CLIPBOARD);
327 }
328 else if (ev->nState & ws::MCF_SHIFT)
329 {
330 if (ev->nCode == ws::MCB_LEFT)
331 paste_from_clipboard(ws::CBUF_PRIMARY);
332 else if (ev->nCode == ws::MCB_RIGHT)
333 paste_from_clipboard(ws::CBUF_CLIPBOARD);
334 }
335 return STATUS_OK;
336 }
337
338 // Redraw event
339 case ws::UIE_REDRAW:
340 {
341 Color c;
342 c.set_rgb24(0x0088cc);
343
344 ws::ISurface *s = pWnd->get_surface();
345 if (s == NULL)
346 return STATUS_OK;
347
348 // Perform drawing
349 s->begin();
350 s->clear(c);
351 s->end();
352
353 return STATUS_OK;
354 }
355
356 case ws::UIE_CLOSE:
357 {
358 pTest->printf("CLOSE\n");
359
360 pWnd->hide();
361 pWnd->display()->quit_main();
362 break;
363 }
364
365 default:
366 return IEventHandler::handle_event(ev);
367 }
368
369 return STATUS_OK;
370 }
371 };
372
373 MTEST_MAIN
374 {
375 ws::IDisplay *dpy = ws::create_display(0, NULL);
376 MTEST_ASSERT(dpy != NULL);
377 lsp_finally { ws::free_display(dpy); };
378
379 ws::IWindow *wnd = dpy->create_window();
380 MTEST_ASSERT(wnd != NULL);
381 lsp_finally {
382 wnd->destroy();
383 delete wnd;
384 };
385 MTEST_ASSERT(wnd->init() == STATUS_OK);
386 MTEST_ASSERT(wnd->set_caption("Test clipboard") == STATUS_OK);
387 MTEST_ASSERT(wnd->resize(320, 200) == STATUS_OK);
388 MTEST_ASSERT(wnd->set_window_actions(ws::WA_ALL) == STATUS_OK);
389 MTEST_ASSERT(wnd->set_size_constraints(160, 100, 640, 400) == STATUS_OK);
390
391 Handler h(this, wnd);
392 wnd->set_handler(&h);
393
394 MTEST_ASSERT(wnd->show() == STATUS_OK);
395 MTEST_ASSERT(dpy->main() == STATUS_OK);
396 }
397
398 MTEST_END
399
400
401
402