clipboard.c (4865B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#include "config.h" 21#include "client.h" 22#include "clipboard.h" 23#include "common/clipboard.h" 24#include "common/iconv.h" 25#include "user.h" 26#include "vnc.h" 27 28#include <guacamole/client.h> 29#include <guacamole/stream.h> 30#include <guacamole/user.h> 31#include <rfb/rfbclient.h> 32#include <rfb/rfbproto.h> 33 34int guac_vnc_set_clipboard_encoding(guac_client* client, 35 const char* name) { 36 37 guac_vnc_client* vnc_client = (guac_vnc_client*) client->data; 38 39 /* Use ISO8859-1 if explicitly selected or NULL */ 40 if (name == NULL || strcmp(name, "ISO8859-1") == 0) { 41 vnc_client->clipboard_reader = GUAC_READ_ISO8859_1; 42 vnc_client->clipboard_writer = GUAC_WRITE_ISO8859_1; 43 return 0; 44 } 45 46 /* UTF-8 */ 47 if (strcmp(name, "UTF-8") == 0) { 48 vnc_client->clipboard_reader = GUAC_READ_UTF8; 49 vnc_client->clipboard_writer = GUAC_WRITE_UTF8; 50 return 1; 51 } 52 53 /* UTF-16 */ 54 if (strcmp(name, "UTF-16") == 0) { 55 vnc_client->clipboard_reader = GUAC_READ_UTF16; 56 vnc_client->clipboard_writer = GUAC_WRITE_UTF16; 57 return 1; 58 } 59 60 /* CP1252 */ 61 if (strcmp(name, "CP1252") == 0) { 62 vnc_client->clipboard_reader = GUAC_READ_CP1252; 63 vnc_client->clipboard_writer = GUAC_WRITE_CP1252; 64 return 1; 65 } 66 67 /* If encoding unrecognized, warn and default to ISO8859-1 */ 68 guac_client_log(client, GUAC_LOG_WARNING, 69 "Encoding '%s' is invalid. Defaulting to ISO8859-1.", name); 70 71 vnc_client->clipboard_reader = GUAC_READ_ISO8859_1; 72 vnc_client->clipboard_writer = GUAC_WRITE_ISO8859_1; 73 return 0; 74 75} 76 77int guac_vnc_clipboard_handler(guac_user* user, guac_stream* stream, 78 char* mimetype) { 79 80 /* Clear clipboard and prepare for new data */ 81 guac_vnc_client* vnc_client = (guac_vnc_client*) user->client->data; 82 guac_common_clipboard_reset(vnc_client->clipboard, mimetype); 83 84 /* Set handlers for clipboard stream */ 85 stream->blob_handler = guac_vnc_clipboard_blob_handler; 86 stream->end_handler = guac_vnc_clipboard_end_handler; 87 88 return 0; 89} 90 91int guac_vnc_clipboard_blob_handler(guac_user* user, guac_stream* stream, 92 void* data, int length) { 93 94 /* Append new data */ 95 guac_vnc_client* vnc_client = (guac_vnc_client*) user->client->data; 96 guac_common_clipboard_append(vnc_client->clipboard, (char*) data, length); 97 98 return 0; 99} 100 101int guac_vnc_clipboard_end_handler(guac_user* user, guac_stream* stream) { 102 103 guac_vnc_client* vnc_client = (guac_vnc_client*) user->client->data; 104 rfbClient* rfb_client = vnc_client->rfb_client; 105 106 char output_data[GUAC_COMMON_CLIPBOARD_MAX_LENGTH]; 107 108 const char* input = vnc_client->clipboard->buffer; 109 char* output = output_data; 110 guac_iconv_write* writer = vnc_client->clipboard_writer; 111 112 /* Convert clipboard contents */ 113 guac_iconv(GUAC_READ_UTF8, &input, vnc_client->clipboard->length, 114 writer, &output, sizeof(output_data)); 115 116 /* Send via VNC only if finished connecting */ 117 if (rfb_client != NULL) 118 SendClientCutText(rfb_client, output_data, output - output_data); 119 120 return 0; 121} 122 123void guac_vnc_cut_text(rfbClient* client, const char* text, int textlen) { 124 125 guac_client* gc = rfbClientGetClientData(client, GUAC_VNC_CLIENT_KEY); 126 guac_vnc_client* vnc_client = (guac_vnc_client*) gc->data; 127 128 /* Ignore received text if outbound clipboard transfer is disabled */ 129 if (vnc_client->settings->disable_copy) 130 return; 131 132 char received_data[GUAC_COMMON_CLIPBOARD_MAX_LENGTH]; 133 134 const char* input = text; 135 char* output = received_data; 136 guac_iconv_read* reader = vnc_client->clipboard_reader; 137 138 /* Convert clipboard contents */ 139 guac_iconv(reader, &input, textlen, 140 GUAC_WRITE_UTF8, &output, sizeof(received_data)); 141 142 /* Send converted data */ 143 guac_common_clipboard_reset(vnc_client->clipboard, "text/plain"); 144 guac_common_clipboard_append(vnc_client->clipboard, received_data, output - received_data); 145 guac_common_clipboard_send(vnc_client->clipboard, gc); 146 147} 148