|
16 | 16 | #include "linux/x11clipboard.h" |
17 | 17 |
|
18 | 18 | static struct ptk_clipboard_module { |
19 | | - char *text; |
20 | | - size_t text_len; |
| 19 | + char *text; |
| 20 | + size_t text_len; |
21 | 21 | } ptk_clipboard; |
22 | 22 |
|
23 | 23 | int ptk_clipboard_request_text(ptk_clipboard_callback_t callback, void *arg) |
24 | 24 | { |
25 | 25 | #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 | + } |
29 | 29 | #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 }; |
33 | 33 |
|
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; |
43 | 47 | } |
44 | 48 |
|
45 | 49 | int ptk_clipboard_set_text(const wchar_t *text, size_t len) |
46 | 50 | { |
47 | 51 | #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 | + } |
51 | 55 | #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); |
55 | 59 |
|
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; |
63 | 67 | } |
64 | 68 |
|
65 | 69 | void ptk_clipboard_init(void) |
66 | 70 | { |
67 | 71 | #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 | + } |
72 | 76 | #endif |
73 | 77 | } |
74 | 78 |
|
75 | 79 | void ptk_clipboard_destroy(void) |
76 | 80 | { |
77 | 81 | #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 | + } |
82 | 86 | #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; |
87 | 91 | } |
0 commit comments