Skip to content

Commit 493931d

Browse files
committed
fix(ptk): request text failed when clipboard content is empty (#318)
1 parent ae9b228 commit 493931d

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

lib/ptk/src/clipboard.c

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,72 +16,76 @@
1616
#include "linux/x11clipboard.h"
1717

1818
static struct ptk_clipboard_module {
19-
char *text;
20-
size_t text_len;
19+
char *text;
20+
size_t text_len;
2121
} ptk_clipboard;
2222

2323
int ptk_clipboard_request_text(ptk_clipboard_callback_t callback, void *arg)
2424
{
2525
#ifdef PTK_HAS_LIBX11
26-
if (ptk_get_app_id() == PTK_APP_ID_LINUX_X11) {
27-
return ptk_x11clipboard_request_text(callback, arg);
28-
}
26+
if (ptk_get_app_id() == PTK_APP_ID_LINUX_X11) {
27+
return ptk_x11clipboard_request_text(callback, arg);
28+
}
2929
#endif
30-
size_t len = ptk_clipboard.text_len + 1;
31-
wchar_t *wstr = malloc(sizeof(wchar_t) * len);
32-
ptk_clipboard_t clipboard = { 0 };
30+
size_t len = ptk_clipboard.text_len + 1;
31+
wchar_t *wstr = malloc(sizeof(wchar_t) * len);
32+
ptk_clipboard_t clipboard = { 0 };
3333

34-
len = decode_utf8(wstr, ptk_clipboard.text, len);
35-
wstr[len] = 0;
36-
// Assign the data
37-
clipboard.text = wstr;
38-
clipboard.len = len;
39-
clipboard.image = NULL;
40-
callback(&clipboard, arg);
41-
free(wstr);
42-
return 0;
34+
if (len > 1) {
35+
len = decode_utf8(wstr, ptk_clipboard.text, len);
36+
wstr[len] = 0;
37+
} else {
38+
wstr[0] = 0;
39+
}
40+
// Assign the data
41+
clipboard.text = wstr;
42+
clipboard.len = len;
43+
clipboard.image = NULL;
44+
callback(&clipboard, arg);
45+
free(wstr);
46+
return 0;
4347
}
4448

4549
int ptk_clipboard_set_text(const wchar_t *text, size_t len)
4650
{
4751
#ifdef PTK_HAS_LIBX11
48-
if (ptk_get_app_id() == PTK_APP_ID_LINUX_X11) {
49-
return ptk_x11clipboard_set_text(text, len);
50-
}
52+
if (ptk_get_app_id() == PTK_APP_ID_LINUX_X11) {
53+
return ptk_x11clipboard_set_text(text, len);
54+
}
5155
#endif
52-
size_t raw_len = encode_utf8(NULL, text, 0);
53-
char *raw_text = malloc((raw_len + 1) * sizeof(char));
54-
raw_len = encode_utf8(raw_text, text, raw_len);
56+
size_t raw_len = encode_utf8(NULL, text, 0);
57+
char *raw_text = malloc((raw_len + 1) * sizeof(char));
58+
raw_len = encode_utf8(raw_text, text, raw_len);
5559

56-
if (ptk_clipboard.text) {
57-
free(ptk_clipboard.text);
58-
}
59-
raw_text[raw_len] = '\0';
60-
ptk_clipboard.text = raw_text;
61-
ptk_clipboard.text_len = raw_len;
62-
return 0;
60+
if (ptk_clipboard.text) {
61+
free(ptk_clipboard.text);
62+
}
63+
raw_text[raw_len] = '\0';
64+
ptk_clipboard.text = raw_text;
65+
ptk_clipboard.text_len = raw_len;
66+
return 0;
6367
}
6468

6569
void ptk_clipboard_init(void)
6670
{
6771
#ifdef PTK_HAS_LIBX11
68-
if (ptk_get_app_id() == PTK_APP_ID_LINUX_X11) {
69-
ptk_x11clipboard_init();
70-
return;
71-
}
72+
if (ptk_get_app_id() == PTK_APP_ID_LINUX_X11) {
73+
ptk_x11clipboard_init();
74+
return;
75+
}
7276
#endif
7377
}
7478

7579
void ptk_clipboard_destroy(void)
7680
{
7781
#ifdef PTK_HAS_LIBX11
78-
if (ptk_get_app_id() == PTK_APP_ID_LINUX_X11) {
79-
ptk_x11clipboard_destroy();
80-
return;
81-
}
82+
if (ptk_get_app_id() == PTK_APP_ID_LINUX_X11) {
83+
ptk_x11clipboard_destroy();
84+
return;
85+
}
8286
#endif
83-
if (ptk_clipboard.text) {
84-
free(ptk_clipboard.text);
85-
}
86-
ptk_clipboard.text = NULL;
87+
if (ptk_clipboard.text) {
88+
free(ptk_clipboard.text);
89+
}
90+
ptk_clipboard.text = NULL;
8791
}

0 commit comments

Comments
 (0)