|
| 1 | +// Copyright 2024 Samsung Electronics Co., Ltd. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "tizen_clipboard.h" |
| 6 | + |
| 7 | +#include "flutter/shell/platform/tizen/logger.h" |
| 8 | +#include "flutter/shell/platform/tizen/tizen_window.h" |
| 9 | +#include "flutter/shell/platform/tizen/tizen_window_ecore_wl2.h" |
| 10 | + |
| 11 | +namespace flutter { |
| 12 | + |
| 13 | +namespace { |
| 14 | + |
| 15 | +constexpr char kMimeTypeTextPlain[] = "text/plain;charset=utf-8"; |
| 16 | + |
| 17 | +} // namespace |
| 18 | + |
| 19 | +TizenClipboard::TizenClipboard(TizenViewBase* view) { |
| 20 | + if (auto* window = dynamic_cast<TizenWindowEcoreWl2*>(view)) { |
| 21 | + auto* ecore_wl2_window = |
| 22 | + static_cast<Ecore_Wl2_Window*>(window->GetNativeHandle()); |
| 23 | + display_ = ecore_wl2_window_display_get(ecore_wl2_window); |
| 24 | + } else { |
| 25 | + display_ = ecore_wl2_connected_display_get(NULL); |
| 26 | + } |
| 27 | + |
| 28 | + send_handler = ecore_event_handler_add( |
| 29 | + ECORE_WL2_EVENT_DATA_SOURCE_SEND, |
| 30 | + [](void* data, int type, void* event) -> Eina_Bool { |
| 31 | + auto* self = reinterpret_cast<TizenClipboard*>(data); |
| 32 | + self->SendData(event); |
| 33 | + return ECORE_CALLBACK_PASS_ON; |
| 34 | + }, |
| 35 | + this); |
| 36 | + receive_handler = ecore_event_handler_add( |
| 37 | + ECORE_WL2_EVENT_OFFER_DATA_READY, |
| 38 | + [](void* data, int type, void* event) -> Eina_Bool { |
| 39 | + auto* self = reinterpret_cast<TizenClipboard*>(data); |
| 40 | + self->ReceiveData(event); |
| 41 | + return ECORE_CALLBACK_PASS_ON; |
| 42 | + }, |
| 43 | + this); |
| 44 | +} |
| 45 | + |
| 46 | +TizenClipboard::~TizenClipboard() { |
| 47 | + ecore_event_handler_del(send_handler); |
| 48 | + ecore_event_handler_del(receive_handler); |
| 49 | +} |
| 50 | + |
| 51 | +void TizenClipboard::SendData(void* event) { |
| 52 | + auto* send_event = reinterpret_cast<Ecore_Wl2_Event_Data_Source_Send*>(event); |
| 53 | + if (!send_event->type || strcmp(send_event->type, kMimeTypeTextPlain)) { |
| 54 | + FT_LOG(Error) << "Invaild mime type."; |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (send_event->serial != selection_serial_) { |
| 59 | + FT_LOG(Error) << "The serial doesn't match."; |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + write(send_event->fd, data_.c_str(), data_.length()); |
| 64 | + close(send_event->fd); |
| 65 | +} |
| 66 | + |
| 67 | +void TizenClipboard::ReceiveData(void* event) { |
| 68 | + auto* ready_event = |
| 69 | + reinterpret_cast<Ecore_Wl2_Event_Offer_Data_Ready*>(event); |
| 70 | + if (ready_event->data == nullptr || ready_event->len < 1) { |
| 71 | + FT_LOG(Info) << "No data available."; |
| 72 | + if (on_data_callback_) { |
| 73 | + on_data_callback_(""); |
| 74 | + on_data_callback_ = nullptr; |
| 75 | + } |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if (ready_event->offer != selection_offer_) { |
| 80 | + FT_LOG(Error) << "The offer doesn't match."; |
| 81 | + if (on_data_callback_) { |
| 82 | + on_data_callback_(std::nullopt); |
| 83 | + on_data_callback_ = nullptr; |
| 84 | + } |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + size_t data_length = strlen(ready_event->data); |
| 89 | + size_t buffer_size = ready_event->len; |
| 90 | + std::string content; |
| 91 | + |
| 92 | + if (data_length < buffer_size) { |
| 93 | + content.append(ready_event->data, data_length); |
| 94 | + } else { |
| 95 | + content.append(ready_event->data, buffer_size); |
| 96 | + } |
| 97 | + |
| 98 | + if (on_data_callback_) { |
| 99 | + on_data_callback_(content); |
| 100 | + on_data_callback_ = nullptr; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +void TizenClipboard::SetData(const std::string& data) { |
| 105 | + data_ = data; |
| 106 | + |
| 107 | + const char* mime_types[3]; |
| 108 | + mime_types[0] = kMimeTypeTextPlain; |
| 109 | + // TODO(jsuya): There is an issue where ECORE_WL2_EVENT_DATA_SOURCE_SEND event |
| 110 | + // does not work properly even if ecore_wl2_dnd_selection_set() is called in |
| 111 | + // Tizen 6.5 or lower. Therefore, add empty mimetype for event call from the |
| 112 | + // cbhm module. Since it works normally from Tizen 8.0, this part may be |
| 113 | + // modified in the future. |
| 114 | + mime_types[1] = ""; |
| 115 | + mime_types[2] = nullptr; |
| 116 | + |
| 117 | + Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get(display_); |
| 118 | + selection_serial_ = ecore_wl2_dnd_selection_set(input, mime_types); |
| 119 | + ecore_wl2_display_flush(display_); |
| 120 | +} |
| 121 | + |
| 122 | +bool TizenClipboard::GetData(ClipboardCallback on_data_callback) { |
| 123 | + on_data_callback_ = std::move(on_data_callback); |
| 124 | + |
| 125 | + Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get(display_); |
| 126 | + selection_offer_ = ecore_wl2_dnd_selection_get(input); |
| 127 | + |
| 128 | + if (!selection_offer_) { |
| 129 | + FT_LOG(Error) << "ecore_wl2_dnd_selection_get() failed."; |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + ecore_wl2_offer_receive(selection_offer_, |
| 134 | + const_cast<char*>(kMimeTypeTextPlain)); |
| 135 | + return true; |
| 136 | +} |
| 137 | + |
| 138 | +bool TizenClipboard::HasStrings() { |
| 139 | + Ecore_Wl2_Input* input = ecore_wl2_input_default_input_get(display_); |
| 140 | + selection_offer_ = ecore_wl2_dnd_selection_get(input); |
| 141 | + |
| 142 | + if (!selection_offer_) { |
| 143 | + FT_LOG(Error) << "ecore_wl2_dnd_selection_get() failed."; |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + Eina_Array* available_types = ecore_wl2_offer_mimes_get(selection_offer_); |
| 148 | + unsigned int type_count = eina_array_count(available_types); |
| 149 | + |
| 150 | + for (unsigned int i = 0; i < type_count; ++i) { |
| 151 | + auto* available_type = |
| 152 | + static_cast<char*>(eina_array_data_get(available_types, i)); |
| 153 | + if (!strcmp(kMimeTypeTextPlain, available_type)) { |
| 154 | + return true; |
| 155 | + } |
| 156 | + } |
| 157 | + return false; |
| 158 | +} |
| 159 | + |
| 160 | +} // namespace flutter |
0 commit comments