2010-12-08 21:14:04 +00:00
|
|
|
|
2011-02-16 02:47:51 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2010-12-08 21:14:04 +00:00
|
|
|
*
|
2011-02-16 02:47:51 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2010-12-08 21:14:04 +00:00
|
|
|
*
|
2011-02-16 02:47:51 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2010-12-08 21:14:04 +00:00
|
|
|
*
|
2011-02-16 02:47:51 +00:00
|
|
|
* The Original Code is libguac-client-vnc.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Michael Jumper.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-04-30 07:47:38 +00:00
|
|
|
#include <pthread.h>
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
#include <rfb/rfbclient.h>
|
|
|
|
|
2011-11-26 00:40:56 +00:00
|
|
|
#include <guacamole/socket.h>
|
2010-12-08 21:14:04 +00:00
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <guacamole/client.h>
|
2013-04-30 07:47:38 +00:00
|
|
|
#include <guacamole/audio.h>
|
|
|
|
|
2011-12-22 02:35:42 +00:00
|
|
|
#include "client.h"
|
|
|
|
#include "vnc_handlers.h"
|
|
|
|
#include "guac_handlers.h"
|
2013-08-09 00:25:08 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_PULSE
|
2013-04-30 07:47:38 +00:00
|
|
|
#include "pa_handlers.h"
|
2013-08-09 00:25:08 +00:00
|
|
|
#endif
|
2011-12-22 02:35:42 +00:00
|
|
|
|
2011-01-01 21:23:53 +00:00
|
|
|
/* Client plugin arguments */
|
|
|
|
const char* GUAC_CLIENT_ARGS[] = {
|
|
|
|
"hostname",
|
|
|
|
"port",
|
2011-02-11 06:56:47 +00:00
|
|
|
"read-only",
|
2011-03-14 01:58:31 +00:00
|
|
|
"encodings",
|
2011-01-01 21:23:53 +00:00
|
|
|
"password",
|
2012-02-21 18:15:11 +00:00
|
|
|
"swap-red-blue",
|
2013-02-10 00:39:31 +00:00
|
|
|
"color-depth",
|
2013-08-09 00:25:08 +00:00
|
|
|
|
2013-07-08 20:03:04 +00:00
|
|
|
#ifdef ENABLE_VNC_REPEATER
|
2013-07-01 17:46:30 +00:00
|
|
|
"dest-host",
|
|
|
|
"dest-port",
|
2013-07-08 20:03:04 +00:00
|
|
|
#endif
|
2013-08-09 00:25:08 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_PULSE
|
2013-04-30 07:47:38 +00:00
|
|
|
"disable-audio",
|
2013-08-09 00:25:08 +00:00
|
|
|
#endif
|
|
|
|
|
2011-01-01 21:23:53 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2013-07-08 20:03:04 +00:00
|
|
|
enum VNC_ARGS_IDX {
|
|
|
|
|
|
|
|
IDX_HOSTNAME,
|
|
|
|
IDX_PORT,
|
|
|
|
IDX_READ_ONLY,
|
|
|
|
IDX_ENCODINGS,
|
|
|
|
IDX_PASSWORD,
|
|
|
|
IDX_SWAP_RED_BLUE,
|
|
|
|
IDX_COLOR_DEPTH,
|
2013-08-09 00:25:08 +00:00
|
|
|
|
2013-07-08 20:03:04 +00:00
|
|
|
#ifdef ENABLE_VNC_REPEATER
|
|
|
|
IDX_DEST_HOST,
|
|
|
|
IDX_DEST_PORT,
|
|
|
|
#endif
|
2013-08-09 00:25:08 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_PULSE
|
|
|
|
IDX_DISABLE_AUDIO,
|
|
|
|
#endif
|
|
|
|
|
2013-07-08 20:03:04 +00:00
|
|
|
VNC_ARGS_COUNT
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-12-22 02:35:42 +00:00
|
|
|
char* __GUAC_CLIENT = "GUAC_CLIENT";
|
2011-12-22 02:18:25 +00:00
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
int guac_client_init(guac_client* client, int argc, char** argv) {
|
|
|
|
|
|
|
|
rfbClient* rfb_client;
|
|
|
|
|
|
|
|
vnc_guac_client_data* guac_client_data;
|
2013-08-09 01:43:03 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_PULSE
|
2013-04-30 07:47:38 +00:00
|
|
|
pthread_t pa_read_thread, pa_send_thread;
|
2013-08-09 01:43:03 +00:00
|
|
|
#endif
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2012-02-21 18:15:11 +00:00
|
|
|
int read_only;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-12-22 02:18:25 +00:00
|
|
|
/* Set up libvncclient logging */
|
2011-12-22 02:35:42 +00:00
|
|
|
rfbClientLog = guac_vnc_client_log_info;
|
|
|
|
rfbClientErr = guac_vnc_client_log_error;
|
2011-03-20 07:22:22 +00:00
|
|
|
|
2011-02-11 06:56:47 +00:00
|
|
|
/*** PARSE ARGUMENTS ***/
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2013-07-08 20:03:04 +00:00
|
|
|
if (argc != VNC_ARGS_COUNT) {
|
2011-11-26 00:40:56 +00:00
|
|
|
guac_protocol_send_error(client->socket, "Wrong argument count received.");
|
|
|
|
guac_socket_flush(client->socket);
|
2010-12-08 21:14:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Alloc client data */
|
|
|
|
guac_client_data = malloc(sizeof(vnc_guac_client_data));
|
|
|
|
client->data = guac_client_data;
|
|
|
|
|
2012-02-21 18:15:11 +00:00
|
|
|
/* Set read-only flag */
|
2013-07-08 20:03:04 +00:00
|
|
|
read_only = (strcmp(argv[IDX_READ_ONLY], "true") == 0);
|
2012-02-21 18:15:11 +00:00
|
|
|
|
|
|
|
/* Set red/blue swap flag */
|
2013-07-08 20:03:04 +00:00
|
|
|
guac_client_data->swap_red_blue = (strcmp(argv[IDX_SWAP_RED_BLUE], "true") == 0);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-03-14 01:58:31 +00:00
|
|
|
/* Freed after use by libvncclient */
|
2013-07-08 20:03:04 +00:00
|
|
|
guac_client_data->password = strdup(argv[IDX_PASSWORD]);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-02-11 06:56:47 +00:00
|
|
|
/*** INIT RFB CLIENT ***/
|
|
|
|
|
|
|
|
rfb_client = rfbGetClient(8, 3, 4); /* 32-bpp client */
|
|
|
|
|
2011-03-14 17:59:47 +00:00
|
|
|
/* Store Guac client in rfb client */
|
|
|
|
rfbClientSetClientData(rfb_client, __GUAC_CLIENT, client);
|
|
|
|
|
2011-02-11 06:56:47 +00:00
|
|
|
/* Framebuffer update handler */
|
|
|
|
rfb_client->GotFrameBufferUpdate = guac_vnc_update;
|
|
|
|
rfb_client->GotCopyRect = guac_vnc_copyrect;
|
|
|
|
|
|
|
|
/* Do not handle clipboard and local cursor if read-only */
|
|
|
|
if (read_only == 0) {
|
|
|
|
/* Enable client-side cursor */
|
|
|
|
rfb_client->GotCursorShape = guac_vnc_cursor;
|
|
|
|
rfb_client->appData.useRemoteCursor = TRUE;
|
|
|
|
|
|
|
|
/* Clipboard */
|
|
|
|
rfb_client->GotXCutText = guac_vnc_cut_text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Password */
|
|
|
|
rfb_client->GetPassword = guac_vnc_get_password;
|
2013-02-10 00:39:31 +00:00
|
|
|
|
2013-02-10 00:46:29 +00:00
|
|
|
/* Depth */
|
2013-08-09 00:25:08 +00:00
|
|
|
guac_vnc_set_pixel_format(rfb_client, atoi(argv[IDX_COLOR_DEPTH]));
|
2013-02-10 00:46:29 +00:00
|
|
|
|
2013-08-09 00:25:08 +00:00
|
|
|
#ifdef ENABLE_PULSE
|
|
|
|
guac_client_data->audio_enabled = (strcmp(argv[IDX_DISABLE_AUDIO], "true") != 0);
|
2013-08-09 01:43:03 +00:00
|
|
|
|
|
|
|
/* If an encoding is available, load an audio stream */
|
|
|
|
if (guac_client_data->audio_enabled) {
|
|
|
|
|
|
|
|
guac_client_data->audio = guac_audio_stream_alloc(client, NULL);
|
|
|
|
|
|
|
|
/* If successful, init audio system */
|
2013-04-30 07:47:38 +00:00
|
|
|
if (guac_client_data->audio != NULL) {
|
|
|
|
|
2013-08-09 01:43:03 +00:00
|
|
|
guac_client_log_info(client,
|
|
|
|
"Audio will be encoded as %s",
|
|
|
|
guac_client_data->audio->encoder->mimetype);
|
|
|
|
|
2013-04-30 07:47:38 +00:00
|
|
|
/* Create a thread to read audio data */
|
2013-08-09 01:43:03 +00:00
|
|
|
if (pthread_create(&pa_read_thread, NULL, guac_pa_read_audio,
|
2013-08-09 02:05:42 +00:00
|
|
|
(void*) guac_client_data)) {
|
2013-08-09 01:43:03 +00:00
|
|
|
guac_protocol_send_error(client->socket,
|
|
|
|
"Error initializing PulseAudio thread");
|
2013-04-30 07:47:38 +00:00
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
guac_client_data->audio_read_thread = &pa_read_thread;
|
|
|
|
|
|
|
|
/* Create a thread to send audio data */
|
2013-08-09 01:43:03 +00:00
|
|
|
if (pthread_create(&pa_send_thread, NULL, guac_pa_send_audio,
|
2013-08-09 02:05:42 +00:00
|
|
|
(void*) guac_client_data)) {
|
2013-08-09 01:43:03 +00:00
|
|
|
guac_protocol_send_error(client->socket,
|
|
|
|
"Error initializing PulseAudio thread");
|
2013-04-30 07:47:38 +00:00
|
|
|
guac_socket_flush(client->socket);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
guac_client_data->audio_send_thread = &pa_send_thread;
|
|
|
|
}
|
2013-08-09 01:43:03 +00:00
|
|
|
|
|
|
|
/* Otherwise, audio loading failed */
|
2013-04-30 07:47:38 +00:00
|
|
|
else
|
2013-08-09 01:43:03 +00:00
|
|
|
guac_client_log_info(client,
|
|
|
|
"No available audio encoding. Sound disabled.");
|
|
|
|
|
|
|
|
/* Require threadsafe sockets if audio enabled */
|
|
|
|
guac_socket_require_threadsafe(client->socket);
|
|
|
|
|
2013-04-30 07:47:38 +00:00
|
|
|
} /* end if audio enabled */
|
2013-08-09 00:25:08 +00:00
|
|
|
#endif
|
2013-04-30 07:47:38 +00:00
|
|
|
|
2011-03-14 01:34:58 +00:00
|
|
|
/* Hook into allocation so we can handle resize. */
|
|
|
|
guac_client_data->rfb_MallocFrameBuffer = rfb_client->MallocFrameBuffer;
|
|
|
|
rfb_client->MallocFrameBuffer = guac_vnc_malloc_framebuffer;
|
|
|
|
rfb_client->canHandleNewFBSize = 1;
|
2011-02-11 06:56:47 +00:00
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
/* Set hostname and port */
|
2011-01-01 21:23:53 +00:00
|
|
|
rfb_client->serverHost = strdup(argv[0]);
|
|
|
|
rfb_client->serverPort = atoi(argv[1]);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2013-07-08 20:03:04 +00:00
|
|
|
#ifdef ENABLE_VNC_REPEATER
|
2013-07-01 17:46:30 +00:00
|
|
|
/* Set repeater parameters if specified */
|
2013-07-08 20:03:04 +00:00
|
|
|
if(argv[IDX_DEST_HOST][0] != '\0')
|
|
|
|
rfb_client->destHost = strdup(argv[IDX_DEST_HOST]);
|
2013-07-01 17:46:30 +00:00
|
|
|
|
2013-07-08 20:03:04 +00:00
|
|
|
if(argv[IDX_DEST_PORT][0] != '\0')
|
|
|
|
rfb_client->destPort = atoi(argv[IDX_DEST_PORT]);
|
|
|
|
#endif
|
2013-07-01 17:46:30 +00:00
|
|
|
|
2011-03-14 01:58:31 +00:00
|
|
|
/* Set encodings if specified */
|
2013-07-08 20:03:04 +00:00
|
|
|
if (argv[IDX_ENCODINGS][0] != '\0')
|
|
|
|
rfb_client->appData.encodingsString = guac_client_data->encodings
|
|
|
|
= strdup(argv[IDX_ENCODINGS]);
|
2011-03-14 01:58:31 +00:00
|
|
|
else
|
|
|
|
guac_client_data->encodings = NULL;
|
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
/* Connect */
|
|
|
|
if (!rfbInitClient(rfb_client, NULL, NULL)) {
|
2011-11-26 00:40:56 +00:00
|
|
|
guac_protocol_send_error(client->socket, "Error initializing VNC client");
|
|
|
|
guac_socket_flush(client->socket);
|
2010-12-08 21:14:04 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set remaining client data */
|
|
|
|
guac_client_data->rfb_client = rfb_client;
|
|
|
|
guac_client_data->copy_rect_used = 0;
|
2012-02-15 18:04:51 +00:00
|
|
|
guac_client_data->cursor = guac_client_alloc_buffer(client);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
/* Set handlers */
|
|
|
|
client->handle_messages = vnc_guac_client_handle_messages;
|
2011-03-19 08:02:29 +00:00
|
|
|
client->free_handler = vnc_guac_client_free_handler;
|
2011-02-11 06:56:47 +00:00
|
|
|
if (read_only == 0) {
|
|
|
|
/* Do not handle mouse/keyboard/clipboard if read-only */
|
|
|
|
client->mouse_handler = vnc_guac_client_mouse_handler;
|
|
|
|
client->key_handler = vnc_guac_client_key_handler;
|
|
|
|
client->clipboard_handler = vnc_guac_client_clipboard_handler;
|
|
|
|
}
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
/* Send name */
|
2011-11-26 00:40:56 +00:00
|
|
|
guac_protocol_send_name(client->socket, rfb_client->desktopName);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
/* Send size */
|
2012-02-15 18:04:51 +00:00
|
|
|
guac_protocol_send_size(client->socket,
|
|
|
|
GUAC_DEFAULT_LAYER, rfb_client->width, rfb_client->height);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|