2010-12-08 21:14:04 +00:00
|
|
|
|
2011-02-16 02:04:36 +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:04:36 +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:04:36 +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:04:36 +00:00
|
|
|
* The Original Code is libguac.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2011-03-27 23:32:49 +00:00
|
|
|
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_NANOSLEEP)
|
2011-03-18 04:16:29 +00:00
|
|
|
#include <time.h>
|
2011-03-27 23:32:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_CLOCK_GETTIME
|
2011-03-18 04:16:29 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2011-09-10 06:59:07 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <inttypes.h>
|
2010-12-08 21:14:04 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2011-03-27 23:32:49 +00:00
|
|
|
#include <cairo/cairo.h>
|
2011-02-09 02:23:10 +00:00
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
#include <sys/types.h>
|
2011-02-09 02:23:10 +00:00
|
|
|
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
#include <winsock2.h>
|
|
|
|
#else
|
2010-12-08 21:14:04 +00:00
|
|
|
#include <sys/socket.h>
|
2011-02-09 02:23:10 +00:00
|
|
|
#endif
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
#include "guacio.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
ssize_t __guac_write_length_string(GUACIO* io, const char* str) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
return
|
|
|
|
guac_write_int(io, strlen(str))
|
|
|
|
|| guac_write_string(io, ".")
|
|
|
|
|| guac_write_string(io, str);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
ssize_t __guac_write_length_int(GUACIO* io, int64_t i) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
char buffer[128];
|
|
|
|
snprintf(buffer, sizeof(buffer), "%"PRIi64, i);
|
|
|
|
return __guac_write_length_string(io, buffer);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-19 23:32:35 +00:00
|
|
|
int guac_send_args(GUACIO* io, const char** args) {
|
2011-01-01 21:22:17 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
if (guac_write_string(io, "4.args")) return -1;
|
2011-01-01 21:22:17 +00:00
|
|
|
|
|
|
|
for (i=0; args[i] != NULL; i++) {
|
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
if (guac_write_string(io, ","))
|
|
|
|
return -1;
|
2011-01-01 21:22:17 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
if (__guac_write_length_string(io, args[i]))
|
2011-03-19 23:32:35 +00:00
|
|
|
return -1;
|
2011-01-01 21:22:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-19 23:32:35 +00:00
|
|
|
return guac_write_string(io, ";");
|
2011-01-01 21:22:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-19 23:32:35 +00:00
|
|
|
int guac_send_name(GUACIO* io, const char* name) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
return
|
|
|
|
guac_write_string(io, "4.name,")
|
|
|
|
|| __guac_write_length_string(io, name)
|
|
|
|
|| guac_write_string(io, ";");
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-19 23:32:35 +00:00
|
|
|
int guac_send_size(GUACIO* io, int w, int h) {
|
|
|
|
|
|
|
|
return
|
2011-09-10 06:59:07 +00:00
|
|
|
guac_write_string(io, "4.size,")
|
|
|
|
|| __guac_write_length_int(io, w)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, h)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ";");
|
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
2011-03-19 23:32:35 +00:00
|
|
|
int guac_send_clipboard(GUACIO* io, const char* data) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
return
|
|
|
|
guac_write_string(io, "9.clipboard,")
|
|
|
|
|| __guac_write_length_string(io, data)
|
|
|
|
|| guac_write_string(io, ";");
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-19 23:32:35 +00:00
|
|
|
int guac_send_error(GUACIO* io, const char* error) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
return
|
|
|
|
guac_write_string(io, "5.error,")
|
|
|
|
|| __guac_write_length_string(io, error)
|
|
|
|
|| guac_write_string(io, ";");
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
2011-04-29 07:45:38 +00:00
|
|
|
int guac_send_sync(GUACIO* io, guac_timestamp_t timestamp) {
|
2011-03-12 02:57:53 +00:00
|
|
|
|
2011-03-19 23:32:35 +00:00
|
|
|
return
|
2011-09-10 06:59:07 +00:00
|
|
|
guac_write_string(io, "4.sync,")
|
|
|
|
|| __guac_write_length_int(io, timestamp)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ";");
|
2011-03-12 02:57:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-03-30 07:03:32 +00:00
|
|
|
int guac_send_copy(GUACIO* io,
|
2011-07-20 19:36:02 +00:00
|
|
|
const guac_layer* srcl, int srcx, int srcy, int w, int h,
|
|
|
|
guac_composite_mode_t mode, const guac_layer* dstl, int dstx, int dsty) {
|
2011-03-19 23:32:35 +00:00
|
|
|
|
|
|
|
return
|
2011-09-10 06:59:07 +00:00
|
|
|
guac_write_string(io, "4.copy,")
|
|
|
|
|| __guac_write_length_int(io, srcl->index)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, srcx)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, srcy)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, w)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, h)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, mode)
|
2011-03-30 07:03:32 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, dstl->index)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, dstx)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, dsty)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ";");
|
|
|
|
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 22:15:58 +00:00
|
|
|
int guac_send_rect(GUACIO* io,
|
|
|
|
guac_composite_mode_t mode, const guac_layer* layer,
|
|
|
|
int x, int y, int width, int height,
|
|
|
|
int r, int g, int b, int a) {
|
|
|
|
|
|
|
|
return
|
2011-09-11 22:56:34 +00:00
|
|
|
guac_write_string(io, "4.rect,")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, mode)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, layer->index)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, x)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, y)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, width)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, height)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, r)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, g)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, b)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, a)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ";");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int guac_send_clip(GUACIO* io, const guac_layer* layer,
|
|
|
|
int x, int y, int width, int height) {
|
|
|
|
|
|
|
|
return
|
2011-09-10 06:59:07 +00:00
|
|
|
guac_write_string(io, "4.clip,")
|
|
|
|
|| __guac_write_length_int(io, layer->index)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, x)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, y)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, width)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, height)
|
2011-07-21 22:15:58 +00:00
|
|
|
|| guac_write_string(io, ";");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
typedef struct __guac_write_png_data {
|
|
|
|
|
|
|
|
GUACIO* io;
|
|
|
|
|
|
|
|
char* buffer;
|
|
|
|
int buffer_size;
|
|
|
|
int data_size;
|
|
|
|
|
|
|
|
} __guac_write_png_data;
|
|
|
|
|
2011-03-27 23:32:49 +00:00
|
|
|
cairo_status_t __guac_write_png(void* closure, const unsigned char* data, unsigned int length) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
__guac_write_png_data* png_data = (__guac_write_png_data*) closure;
|
2011-02-16 02:07:19 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
/* Calculate next buffer size */
|
|
|
|
int next_size = png_data->data_size + length;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
/* If need resizing, double buffer size until big enough */
|
|
|
|
if (next_size > png_data->buffer_size) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
char* new_buffer;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
do {
|
|
|
|
png_data->buffer_size <<= 1;
|
|
|
|
} while (next_size > png_data->buffer_size);
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
/* Resize buffer */
|
|
|
|
new_buffer = realloc(png_data->buffer, png_data->buffer_size);
|
|
|
|
png_data->buffer = new_buffer;
|
2011-03-21 02:34:45 +00:00
|
|
|
|
2011-03-19 23:32:35 +00:00
|
|
|
}
|
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
/* Append data to buffer */
|
|
|
|
memcpy(png_data->buffer + png_data->data_size, data, length);
|
2011-09-10 07:40:22 +00:00
|
|
|
png_data->data_size += length;
|
2011-09-10 06:59:07 +00:00
|
|
|
|
|
|
|
return CAIRO_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int __guac_write_length_png(GUACIO* io, cairo_surface_t* surface) {
|
|
|
|
|
|
|
|
__guac_write_png_data png_data;
|
|
|
|
int base64_length;
|
|
|
|
|
2011-03-27 23:32:49 +00:00
|
|
|
/* Write surface */
|
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
png_data.io = io;
|
|
|
|
png_data.buffer_size = 8192;
|
|
|
|
png_data.buffer = malloc(png_data.buffer_size);
|
|
|
|
png_data.data_size = 0;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
if (cairo_surface_write_to_png_stream(surface, __guac_write_png, &png_data) != CAIRO_STATUS_SUCCESS) {
|
2011-03-19 23:32:35 +00:00
|
|
|
return -1;
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
base64_length = (png_data.data_size + 2) / 3 * 4;
|
2011-03-27 23:32:49 +00:00
|
|
|
|
2011-09-10 07:40:22 +00:00
|
|
|
/* Write length and data */
|
2011-09-10 06:59:07 +00:00
|
|
|
if (
|
|
|
|
guac_write_int(io, base64_length)
|
|
|
|
|| guac_write_string(io, ".")
|
|
|
|
|| guac_write_base64(io, png_data.buffer, png_data.data_size)
|
2011-09-10 07:40:22 +00:00
|
|
|
|| guac_flush_base64(io)) {
|
|
|
|
free(png_data.buffer);
|
2011-09-10 06:59:07 +00:00
|
|
|
return -1;
|
2011-09-10 07:40:22 +00:00
|
|
|
}
|
2011-09-10 06:59:07 +00:00
|
|
|
|
|
|
|
free(png_data.buffer);
|
|
|
|
return 0;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
int guac_send_png(GUACIO* io, guac_composite_mode_t mode,
|
|
|
|
const guac_layer* layer, int x, int y, cairo_surface_t* surface) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
return
|
|
|
|
guac_write_string(io, "3.png,")
|
|
|
|
|| __guac_write_length_int(io, mode)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, layer->index)
|
2011-03-19 23:32:35 +00:00
|
|
|
|| guac_write_string(io, ",")
|
2011-09-10 06:59:07 +00:00
|
|
|
|| __guac_write_length_int(io, x)
|
|
|
|
|| guac_write_string(io, ",")
|
|
|
|
|| __guac_write_length_int(io, y)
|
|
|
|
|| guac_write_string(io, ",")
|
|
|
|
|| __guac_write_length_png(io, surface)
|
|
|
|
|| guac_write_string(io, ";");
|
2011-03-27 23:32:49 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
}
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
int guac_send_cursor(GUACIO* io, int x, int y, cairo_surface_t* surface) {
|
2011-03-27 23:32:49 +00:00
|
|
|
|
2011-09-10 06:59:07 +00:00
|
|
|
return
|
|
|
|
guac_write_string(io, "6.cursor,")
|
|
|
|
|| __guac_write_length_int(io, x)
|
|
|
|
|| guac_write_string(io, ",")
|
|
|
|
|| __guac_write_length_int(io, y)
|
|
|
|
|| guac_write_string(io, ",")
|
|
|
|
|| __guac_write_length_png(io, surface)
|
|
|
|
|| guac_write_string(io, ";");
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int __guac_fill_instructionbuf(GUACIO* io) {
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
/* Attempt to fill buffer */
|
|
|
|
retval = recv(
|
|
|
|
io->fd,
|
|
|
|
io->instructionbuf + io->instructionbuf_used_length,
|
|
|
|
io->instructionbuf_size - io->instructionbuf_used_length,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
io->instructionbuf_used_length += retval;
|
|
|
|
|
|
|
|
/* Expand buffer if necessary */
|
|
|
|
if (io->instructionbuf_used_length > io->instructionbuf_size / 2) {
|
|
|
|
io->instructionbuf_size *= 2;
|
|
|
|
io->instructionbuf = realloc(io->instructionbuf, io->instructionbuf_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns new instruction if one exists, or NULL if no more instructions. */
|
|
|
|
int guac_read_instruction(GUACIO* io, guac_instruction* parsed_instruction) {
|
|
|
|
|
|
|
|
int retval;
|
2011-10-19 08:28:18 +00:00
|
|
|
int i = io->instructionbuf_parse_start;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
/* Loop until a instruction is read */
|
|
|
|
for (;;) {
|
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* Length of element */
|
|
|
|
int element_length = 0;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* Parse instruction in buffe */
|
|
|
|
while (i < io->instructionbuf_used_length) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* Read character from buffer */
|
|
|
|
char c = io->instructionbuf[i++];
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* If digit, calculate element length */
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
element_length = element_length * 10 + c - '0';
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* Otherwise, if end of length */
|
|
|
|
else if (c == '.') {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* Verify element is fully read */
|
|
|
|
if (i + element_length < io->instructionbuf_used_length) {
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* Get element value */
|
|
|
|
char* elementv = &(io->instructionbuf[i]);
|
|
|
|
|
|
|
|
/* Get terminator, set null terminator of elementv */
|
|
|
|
char terminator = elementv[element_length];
|
|
|
|
elementv[element_length] = '\0';
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* Move to terminator of element */
|
|
|
|
i += element_length;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* Reset element length */
|
|
|
|
element_length = 0;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* As element has been read successfully, update
|
|
|
|
* parse start */
|
|
|
|
io->instructionbuf_parse_start = i;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
2011-10-19 08:28:18 +00:00
|
|
|
/* Save element */
|
|
|
|
io->instructionbuf_elementv[io->instructionbuf_elementc++] = elementv;
|
|
|
|
|
|
|
|
/* Finish parse if terminator is a semicolon */
|
|
|
|
if (terminator == ';') {
|
|
|
|
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* Init parsed instruction */
|
|
|
|
parsed_instruction->argc = io->instructionbuf_elementc - 1;
|
|
|
|
parsed_instruction->argv = malloc(sizeof(char*) * parsed_instruction->argc);
|
|
|
|
|
|
|
|
/* Set opcode */
|
|
|
|
parsed_instruction->opcode = strdup(io->instructionbuf_elementv[0]);
|
|
|
|
|
|
|
|
/* Copy element values to parsed instruction */
|
|
|
|
for (j=0; j<parsed_instruction->argc; j++)
|
|
|
|
parsed_instruction->argv[j] = strdup(io->instructionbuf_elementv[j+1]);
|
|
|
|
|
|
|
|
/* Reset buffer */
|
|
|
|
memmove(io->instructionbuf, io->instructionbuf + i + 1, io->instructionbuf_used_length - i - 1);
|
|
|
|
io->instructionbuf_used_length -= i + 1;
|
|
|
|
io->instructionbuf_parse_start = 0;
|
|
|
|
io->instructionbuf_elementc = 0;
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
} /* end if terminator */
|
|
|
|
|
|
|
|
} /* end if element fully read */
|
|
|
|
|
|
|
|
/* Otherwise, read more data */
|
|
|
|
else
|
|
|
|
break;
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No instruction yet? Get more data ... */
|
2011-03-17 07:25:35 +00:00
|
|
|
retval = guac_select(io, GUAC_USEC_TIMEOUT);
|
2010-12-08 21:14:04 +00:00
|
|
|
if (retval <= 0)
|
|
|
|
return retval;
|
|
|
|
|
2011-03-17 07:25:35 +00:00
|
|
|
/* If more data is available, fill into buffer */
|
2010-12-08 21:14:04 +00:00
|
|
|
retval = __guac_fill_instructionbuf(io);
|
2011-03-17 07:25:35 +00:00
|
|
|
if (retval < 0) return retval; /* Error */
|
|
|
|
if (retval == 0) return -1; /* EOF */
|
2010-12-08 21:14:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_free_instruction_data(guac_instruction* instruction) {
|
|
|
|
free(instruction->opcode);
|
|
|
|
|
|
|
|
if (instruction->argv)
|
|
|
|
free(instruction->argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_free_instruction(guac_instruction* instruction) {
|
|
|
|
guac_free_instruction_data(instruction);
|
|
|
|
free(instruction);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int guac_instructions_waiting(GUACIO* io) {
|
|
|
|
|
|
|
|
if (io->instructionbuf_used_length > 0)
|
|
|
|
return 1;
|
|
|
|
|
2011-03-16 08:24:17 +00:00
|
|
|
return guac_select(io, GUAC_USEC_TIMEOUT);
|
2010-12-08 21:14:04 +00:00
|
|
|
}
|
|
|
|
|
2011-04-29 07:45:38 +00:00
|
|
|
guac_timestamp_t guac_current_timestamp() {
|
2011-03-18 04:16:29 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CLOCK_GETTIME
|
|
|
|
|
|
|
|
struct timespec current;
|
|
|
|
|
|
|
|
/* Get current time */
|
|
|
|
clock_gettime(CLOCK_REALTIME, ¤t);
|
|
|
|
|
|
|
|
/* Calculate milliseconds */
|
2011-04-29 07:45:38 +00:00
|
|
|
return (guac_timestamp_t) current.tv_sec * 1000 + current.tv_nsec / 1000000;
|
2011-03-18 04:16:29 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
struct timeval current;
|
|
|
|
|
|
|
|
/* Get current time */
|
|
|
|
gettimeofday(¤t, NULL);
|
|
|
|
|
|
|
|
/* Calculate milliseconds */
|
2011-04-29 07:45:38 +00:00
|
|
|
return (guac_timestamp_t) current.tv_sec * 1000 + current.tv_usec / 1000;
|
2011-03-18 04:16:29 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void guac_sleep(int millis) {
|
|
|
|
|
|
|
|
#ifdef HAVE_NANOSLEEP
|
|
|
|
struct timespec sleep_period;
|
|
|
|
|
2011-04-01 07:30:40 +00:00
|
|
|
sleep_period.tv_sec = millis / 1000;
|
|
|
|
sleep_period.tv_nsec = (millis % 1000) * 1000000L;
|
2011-03-18 04:16:29 +00:00
|
|
|
|
|
|
|
nanosleep(&sleep_period, NULL);
|
|
|
|
#elif defined(__MINGW32__)
|
2011-04-21 22:23:53 +00:00
|
|
|
Sleep(millis);
|
2011-03-18 04:16:29 +00:00
|
|
|
#else
|
|
|
|
#warning No sleep/nanosleep function available. Clients may not perform as expected. Consider patching libguac to add support for your platform.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|