guacamole-spice-protocol/src/protocols/vnc/clipboard.c

147 lines
4.9 KiB
C
Raw Normal View History

/*
* Copyright (C) 2014 Glyptodon LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "config.h"
#include "client.h"
#include "clipboard.h"
#include "guac_clipboard.h"
#include "guac_iconv.h"
#include "user.h"
#include "vnc.h"
2014-06-11 18:15:35 +00:00
#include <guacamole/client.h>
#include <guacamole/stream.h>
#include <guacamole/user.h>
2014-06-11 18:15:35 +00:00
#include <rfb/rfbclient.h>
#include <rfb/rfbproto.h>
int guac_vnc_set_clipboard_encoding(guac_client* client,
const char* name) {
guac_vnc_client* vnc_client = (guac_vnc_client*) client->data;
/* Use ISO8859-1 if explicitly selected or NULL */
if (name == NULL || strcmp(name, "ISO8859-1") == 0) {
vnc_client->clipboard_reader = GUAC_READ_ISO8859_1;
vnc_client->clipboard_writer = GUAC_WRITE_ISO8859_1;
return 0;
}
/* UTF-8 */
if (strcmp(name, "UTF-8") == 0) {
vnc_client->clipboard_reader = GUAC_READ_UTF8;
vnc_client->clipboard_writer = GUAC_WRITE_UTF8;
return 1;
}
/* UTF-16 */
if (strcmp(name, "UTF-16") == 0) {
vnc_client->clipboard_reader = GUAC_READ_UTF16;
vnc_client->clipboard_writer = GUAC_WRITE_UTF16;
return 1;
}
/* CP1252 */
if (strcmp(name, "CP1252") == 0) {
vnc_client->clipboard_reader = GUAC_READ_CP1252;
vnc_client->clipboard_writer = GUAC_WRITE_CP1252;
return 1;
}
/* If encoding unrecognized, warn and default to ISO8859-1 */
guac_client_log(client, GUAC_LOG_WARNING,
"Encoding '%s' is invalid. Defaulting to ISO8859-1.", name);
vnc_client->clipboard_reader = GUAC_READ_ISO8859_1;
vnc_client->clipboard_writer = GUAC_WRITE_ISO8859_1;
return 0;
}
2014-06-11 18:15:35 +00:00
int guac_vnc_clipboard_handler(guac_user* user, guac_stream* stream,
char* mimetype) {
/* Clear clipboard and prepare for new data */
guac_vnc_client* vnc_client = (guac_vnc_client*) user->client->data;
guac_common_clipboard_reset(vnc_client->clipboard, mimetype);
/* Set handlers for clipboard stream */
stream->blob_handler = guac_vnc_clipboard_blob_handler;
stream->end_handler = guac_vnc_clipboard_end_handler;
return 0;
}
int guac_vnc_clipboard_blob_handler(guac_user* user, guac_stream* stream,
void* data, int length) {
/* Append new data */
guac_vnc_client* vnc_client = (guac_vnc_client*) user->client->data;
guac_common_clipboard_append(vnc_client->clipboard, (char*) data, length);
return 0;
}
int guac_vnc_clipboard_end_handler(guac_user* user, guac_stream* stream) {
guac_vnc_client* vnc_client = (guac_vnc_client*) user->client->data;
rfbClient* rfb_client = vnc_client->rfb_client;
char output_data[GUAC_VNC_CLIPBOARD_MAX_LENGTH];
const char* input = vnc_client->clipboard->buffer;
char* output = output_data;
guac_iconv_write* writer = vnc_client->clipboard_writer;
/* Convert clipboard contents */
guac_iconv(GUAC_READ_UTF8, &input, vnc_client->clipboard->length,
writer, &output, sizeof(output_data));
/* Send via VNC */
SendClientCutText(rfb_client, output_data, output - output_data);
return 0;
}
void guac_vnc_cut_text(rfbClient* client, const char* text, int textlen) {
guac_client* gc = rfbClientGetClientData(client, GUAC_VNC_CLIENT_KEY);
guac_vnc_client* vnc_client = (guac_vnc_client*) gc->data;
char received_data[GUAC_VNC_CLIPBOARD_MAX_LENGTH];
const char* input = text;
char* output = received_data;
guac_iconv_read* reader = vnc_client->clipboard_reader;
/* Convert clipboard contents */
guac_iconv(reader, &input, textlen,
GUAC_WRITE_UTF8, &output, sizeof(received_data));
/* Send converted data */
guac_common_clipboard_reset(vnc_client->clipboard, "text/plain");
guac_common_clipboard_append(vnc_client->clipboard, received_data, output - received_data);
guac_common_clipboard_send(vnc_client->clipboard, gc);
}