2010-11-16 11:42:53 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2011-04-18 18:23:19 +00:00
|
|
|
#include <cairo.h>
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2012-12-23 21:05:02 +00:00
|
|
|
#include "broadway-output.h"
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* Basic I/O primitives *
|
|
|
|
************************************************************************/
|
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
struct BroadwayOutput {
|
2011-04-18 17:52:05 +00:00
|
|
|
GOutputStream *out;
|
2012-10-01 10:09:07 +00:00
|
|
|
GString *buf;
|
2010-11-24 21:12:29 +00:00
|
|
|
int error;
|
2011-03-10 13:58:47 +00:00
|
|
|
guint32 serial;
|
2010-11-24 21:12:29 +00:00
|
|
|
};
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
static void
|
2011-11-10 09:12:28 +00:00
|
|
|
broadway_output_send_cmd (BroadwayOutput *output,
|
|
|
|
gboolean fin, BroadwayWSOpCode code,
|
|
|
|
const void *buf, gsize count)
|
|
|
|
{
|
|
|
|
gboolean mask = FALSE;
|
|
|
|
guchar header[16];
|
|
|
|
size_t p;
|
|
|
|
|
|
|
|
gboolean mid_header = count > 125 && count <= 65535;
|
|
|
|
gboolean long_header = count > 65535;
|
|
|
|
|
|
|
|
/* NB. big-endian spec => bit 0 == MSB */
|
|
|
|
header[0] = ( (fin ? 0x80 : 0) | (code & 0x0f) );
|
|
|
|
header[1] = ( (mask ? 0x80 : 0) |
|
|
|
|
(mid_header ? 126 : long_header ? 127 : count) );
|
|
|
|
p = 2;
|
|
|
|
if (mid_header)
|
|
|
|
{
|
|
|
|
*(guint16 *)(header + p) = GUINT16_TO_BE( (guint16)count );
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
else if (long_header)
|
|
|
|
{
|
|
|
|
*(guint64 *)(header + p) = GUINT64_TO_BE( count );
|
|
|
|
p += 8;
|
|
|
|
}
|
|
|
|
// FIXME: if we are paranoid we should 'mask' the data
|
|
|
|
// FIXME: we should really emit these as a single write
|
|
|
|
g_output_stream_write_all (output->out, header, p, NULL, NULL, NULL);
|
|
|
|
g_output_stream_write_all (output->out, buf, count, NULL, NULL, NULL);
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2011-11-10 09:12:28 +00:00
|
|
|
void broadway_output_pong (BroadwayOutput *output)
|
|
|
|
{
|
2013-09-25 15:59:40 +00:00
|
|
|
broadway_output_send_cmd (output, TRUE, BROADWAY_WS_CNX_PONG, NULL, 0);
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2011-11-10 09:12:28 +00:00
|
|
|
int
|
|
|
|
broadway_output_flush (BroadwayOutput *output)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2012-10-01 10:09:07 +00:00
|
|
|
if (output->buf->len == 0)
|
|
|
|
return TRUE;
|
|
|
|
|
2013-09-25 15:59:40 +00:00
|
|
|
broadway_output_send_cmd (output, TRUE, BROADWAY_WS_BINARY,
|
|
|
|
output->buf->str, output->buf->len);
|
2012-10-01 10:09:07 +00:00
|
|
|
|
|
|
|
g_string_set_size (output->buf, 0);
|
|
|
|
|
|
|
|
return !output->error;
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
BroadwayOutput *
|
2013-09-25 15:59:40 +00:00
|
|
|
broadway_output_new (GOutputStream *out, guint32 serial)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2010-11-24 20:40:25 +00:00
|
|
|
BroadwayOutput *output;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:57:30 +00:00
|
|
|
output = g_new0 (BroadwayOutput, 1);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2011-04-18 17:52:05 +00:00
|
|
|
output->out = g_object_ref (out);
|
2012-10-01 10:09:07 +00:00
|
|
|
output->buf = g_string_new ("");
|
2011-03-10 13:58:47 +00:00
|
|
|
output->serial = serial;
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-24 20:40:25 +00:00
|
|
|
return output;
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-24 21:12:29 +00:00
|
|
|
broadway_output_free (BroadwayOutput *output)
|
|
|
|
{
|
2011-04-18 17:52:05 +00:00
|
|
|
g_object_unref (output->out);
|
2010-11-24 21:12:29 +00:00
|
|
|
free (output);
|
|
|
|
}
|
|
|
|
|
2011-03-10 13:58:47 +00:00
|
|
|
guint32
|
|
|
|
broadway_output_get_next_serial (BroadwayOutput *output)
|
|
|
|
{
|
|
|
|
return output->serial;
|
|
|
|
}
|
|
|
|
|
2013-03-28 21:57:33 +00:00
|
|
|
void
|
|
|
|
broadway_output_set_next_serial (BroadwayOutput *output,
|
|
|
|
guint32 serial)
|
|
|
|
{
|
|
|
|
output->serial = serial;
|
|
|
|
}
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* Core rendering operations *
|
|
|
|
************************************************************************/
|
|
|
|
|
2012-10-01 11:02:21 +00:00
|
|
|
static void
|
|
|
|
append_char (BroadwayOutput *output, char c)
|
|
|
|
{
|
|
|
|
g_string_append_c (output->buf, c);
|
|
|
|
}
|
2011-03-10 12:59:30 +00:00
|
|
|
|
|
|
|
static void
|
2012-10-01 11:02:21 +00:00
|
|
|
append_bool (BroadwayOutput *output, gboolean val)
|
2011-03-10 12:59:30 +00:00
|
|
|
{
|
2013-09-25 15:59:40 +00:00
|
|
|
g_string_append_c (output->buf, val ? 1: 0);
|
2011-03-10 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-01 11:02:21 +00:00
|
|
|
append_flags (BroadwayOutput *output, guint32 val)
|
2011-03-10 12:59:30 +00:00
|
|
|
{
|
2013-09-25 15:59:40 +00:00
|
|
|
g_string_append_c (output->buf, val);
|
2011-03-10 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
2012-10-01 11:02:21 +00:00
|
|
|
static void
|
|
|
|
append_uint16 (BroadwayOutput *output, guint32 v)
|
2011-03-10 12:59:30 +00:00
|
|
|
{
|
2012-10-01 11:02:21 +00:00
|
|
|
gsize old_len = output->buf->len;
|
2012-12-19 20:57:58 +00:00
|
|
|
guint8 *buf;
|
2012-10-01 12:53:18 +00:00
|
|
|
|
2013-09-25 15:59:40 +00:00
|
|
|
g_string_set_size (output->buf, old_len + 2);
|
|
|
|
buf = (guint8 *)output->buf->str + old_len;
|
|
|
|
buf[0] = (v >> 0) & 0xff;
|
|
|
|
buf[1] = (v >> 8) & 0xff;
|
2012-10-01 11:02:21 +00:00
|
|
|
}
|
2011-03-10 12:59:30 +00:00
|
|
|
|
2012-10-01 11:02:21 +00:00
|
|
|
static void
|
|
|
|
append_uint32 (BroadwayOutput *output, guint32 v)
|
|
|
|
{
|
|
|
|
gsize old_len = output->buf->len;
|
2012-12-19 20:57:58 +00:00
|
|
|
guint8 *buf;
|
2012-10-01 12:53:18 +00:00
|
|
|
|
2013-09-25 15:59:40 +00:00
|
|
|
g_string_set_size (output->buf, old_len + 4);
|
|
|
|
buf = (guint8 *)output->buf->str + old_len;
|
|
|
|
buf[0] = (v >> 0) & 0xff;
|
|
|
|
buf[1] = (v >> 8) & 0xff;
|
|
|
|
buf[2] = (v >> 16) & 0xff;
|
|
|
|
buf[3] = (v >> 24) & 0xff;
|
2012-10-01 11:02:21 +00:00
|
|
|
}
|
2011-03-10 12:59:30 +00:00
|
|
|
|
2012-10-01 11:02:21 +00:00
|
|
|
static void
|
|
|
|
write_header(BroadwayOutput *output, char op)
|
|
|
|
{
|
|
|
|
append_char (output, op);
|
|
|
|
append_uint32 (output, output->serial++);
|
2011-03-10 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
2011-04-01 14:29:03 +00:00
|
|
|
void
|
2011-03-14 10:46:43 +00:00
|
|
|
broadway_output_grab_pointer (BroadwayOutput *output,
|
|
|
|
int id,
|
2011-04-01 14:29:03 +00:00
|
|
|
gboolean owner_event)
|
2011-03-14 10:46:43 +00:00
|
|
|
{
|
2012-12-20 13:20:20 +00:00
|
|
|
write_header (output, BROADWAY_OP_GRAB_POINTER);
|
2012-10-01 11:02:21 +00:00
|
|
|
append_uint16 (output, id);
|
|
|
|
append_bool (output, owner_event);
|
2011-03-14 10:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
guint32
|
2011-04-01 14:29:03 +00:00
|
|
|
broadway_output_ungrab_pointer (BroadwayOutput *output)
|
2011-03-14 10:46:43 +00:00
|
|
|
{
|
|
|
|
guint32 serial;
|
|
|
|
|
|
|
|
serial = output->serial;
|
2012-12-20 13:20:20 +00:00
|
|
|
write_header (output, BROADWAY_OP_UNGRAB_POINTER);
|
2011-03-14 10:46:43 +00:00
|
|
|
|
|
|
|
return serial;
|
|
|
|
}
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
void
|
2011-04-05 10:27:43 +00:00
|
|
|
broadway_output_new_surface(BroadwayOutput *output,
|
|
|
|
int id, int x, int y, int w, int h,
|
|
|
|
gboolean is_temp)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2012-12-20 13:20:20 +00:00
|
|
|
write_header (output, BROADWAY_OP_NEW_SURFACE);
|
2012-10-01 11:02:21 +00:00
|
|
|
append_uint16 (output, id);
|
|
|
|
append_uint16 (output, x);
|
|
|
|
append_uint16 (output, y);
|
|
|
|
append_uint16 (output, w);
|
|
|
|
append_uint16 (output, h);
|
|
|
|
append_bool (output, is_temp);
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 21:57:33 +00:00
|
|
|
void
|
|
|
|
broadway_output_disconnected (BroadwayOutput *output)
|
|
|
|
{
|
|
|
|
write_header (output, BROADWAY_OP_DISCONNECTED);
|
|
|
|
}
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_show_surface(BroadwayOutput *output, int id)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2012-12-20 13:20:20 +00:00
|
|
|
write_header (output, BROADWAY_OP_SHOW_SURFACE);
|
2012-10-01 11:02:21 +00:00
|
|
|
append_uint16 (output, id);
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_hide_surface(BroadwayOutput *output, int id)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2012-12-20 13:20:20 +00:00
|
|
|
write_header (output, BROADWAY_OP_HIDE_SURFACE);
|
2012-10-01 11:02:21 +00:00
|
|
|
append_uint16 (output, id);
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-12 11:23:58 +00:00
|
|
|
void
|
|
|
|
broadway_output_raise_surface(BroadwayOutput *output, int id)
|
|
|
|
{
|
|
|
|
write_header (output, BROADWAY_OP_RAISE_SURFACE);
|
|
|
|
append_uint16 (output, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
broadway_output_lower_surface(BroadwayOutput *output, int id)
|
|
|
|
{
|
|
|
|
write_header (output, BROADWAY_OP_LOWER_SURFACE);
|
|
|
|
append_uint16 (output, id);
|
|
|
|
}
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
void
|
2010-11-24 20:40:25 +00:00
|
|
|
broadway_output_destroy_surface(BroadwayOutput *output, int id)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2012-12-20 13:20:20 +00:00
|
|
|
write_header (output, BROADWAY_OP_DESTROY_SURFACE);
|
2012-10-01 11:02:21 +00:00
|
|
|
append_uint16 (output, id);
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 11:20:07 +00:00
|
|
|
void
|
|
|
|
broadway_output_set_show_keyboard (BroadwayOutput *output,
|
|
|
|
gboolean show)
|
|
|
|
{
|
|
|
|
write_header (output, BROADWAY_OP_SET_SHOW_KEYBOARD);
|
|
|
|
append_uint16 (output, show);
|
|
|
|
}
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2010-11-23 11:52:10 +00:00
|
|
|
void
|
2011-04-12 09:11:36 +00:00
|
|
|
broadway_output_move_resize_surface (BroadwayOutput *output,
|
|
|
|
int id,
|
|
|
|
gboolean has_pos,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
gboolean has_size,
|
|
|
|
int w,
|
|
|
|
int h)
|
|
|
|
{
|
|
|
|
int val;
|
2011-03-10 12:59:30 +00:00
|
|
|
|
2011-04-12 09:11:36 +00:00
|
|
|
if (!has_pos && !has_size)
|
|
|
|
return;
|
2011-03-10 12:59:30 +00:00
|
|
|
|
2012-12-20 13:20:20 +00:00
|
|
|
write_header (output, BROADWAY_OP_MOVE_RESIZE);
|
2011-04-12 09:11:36 +00:00
|
|
|
val = (!!has_pos) | ((!!has_size) << 1);
|
2012-10-01 11:02:21 +00:00
|
|
|
append_uint16 (output, id);
|
|
|
|
append_flags (output, val);
|
2011-04-12 09:11:36 +00:00
|
|
|
if (has_pos)
|
|
|
|
{
|
2012-10-01 11:02:21 +00:00
|
|
|
append_uint16 (output, x);
|
|
|
|
append_uint16 (output, y);
|
2011-04-12 09:11:36 +00:00
|
|
|
}
|
|
|
|
if (has_size)
|
|
|
|
{
|
2012-10-01 11:02:21 +00:00
|
|
|
append_uint16 (output, w);
|
|
|
|
append_uint16 (output, h);
|
2011-04-12 09:11:36 +00:00
|
|
|
}
|
2010-11-23 11:52:10 +00:00
|
|
|
}
|
|
|
|
|
2011-04-05 10:02:46 +00:00
|
|
|
void
|
|
|
|
broadway_output_set_transient_for (BroadwayOutput *output,
|
|
|
|
int id,
|
|
|
|
int parent_id)
|
|
|
|
{
|
2012-12-20 13:20:20 +00:00
|
|
|
write_header (output, BROADWAY_OP_SET_TRANSIENT_FOR);
|
2012-10-01 11:02:21 +00:00
|
|
|
append_uint16 (output, id);
|
|
|
|
append_uint16 (output, parent_id);
|
2011-04-05 10:02:46 +00:00
|
|
|
}
|
|
|
|
|
2010-11-16 11:42:53 +00:00
|
|
|
void
|
2013-11-07 11:22:38 +00:00
|
|
|
broadway_output_put_buffer (BroadwayOutput *output,
|
|
|
|
int id,
|
|
|
|
BroadwayBuffer *prev_buffer,
|
|
|
|
BroadwayBuffer *buffer)
|
2010-11-16 11:42:53 +00:00
|
|
|
{
|
2013-11-07 12:54:54 +00:00
|
|
|
gsize len;
|
2013-11-07 11:22:38 +00:00
|
|
|
int w, h;
|
2013-11-07 12:54:54 +00:00
|
|
|
GZlibCompressor *compressor;
|
|
|
|
GOutputStream *out, *out_mem;
|
|
|
|
GString *encoded;
|
2013-11-07 11:22:38 +00:00
|
|
|
|
|
|
|
write_header (output, BROADWAY_OP_PUT_BUFFER);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2013-11-07 11:22:38 +00:00
|
|
|
w = broadway_buffer_get_width (buffer);
|
|
|
|
h = broadway_buffer_get_height (buffer);
|
2012-10-01 10:44:38 +00:00
|
|
|
|
2012-10-01 11:02:21 +00:00
|
|
|
append_uint16 (output, id);
|
2013-11-07 11:22:38 +00:00
|
|
|
append_uint16 (output, w);
|
|
|
|
append_uint16 (output, h);
|
2012-10-01 10:44:38 +00:00
|
|
|
|
2013-11-07 12:54:54 +00:00
|
|
|
encoded = g_string_new ("");
|
|
|
|
broadway_buffer_encode (buffer, prev_buffer, encoded);
|
|
|
|
|
|
|
|
compressor = g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_RAW, -1);
|
|
|
|
out_mem = g_memory_output_stream_new_resizable ();
|
|
|
|
out = g_converter_output_stream_new (out_mem, G_CONVERTER (compressor));
|
|
|
|
g_object_unref (compressor);
|
|
|
|
|
|
|
|
if (!g_output_stream_write_all (out, encoded->str, encoded->len,
|
|
|
|
NULL, NULL, NULL) ||
|
|
|
|
!g_output_stream_close (out, NULL, NULL))
|
2016-02-28 17:19:31 +00:00
|
|
|
g_warning ("compression failed");
|
2013-11-07 12:54:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
len = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (out_mem));
|
|
|
|
append_uint32 (output, len);
|
2010-11-16 11:42:53 +00:00
|
|
|
|
2013-11-07 12:54:54 +00:00
|
|
|
g_string_append_len (output->buf, g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (out_mem)), len);
|
2012-10-01 11:02:21 +00:00
|
|
|
|
2013-11-07 12:54:54 +00:00
|
|
|
g_string_free (encoded, TRUE);
|
|
|
|
g_object_unref (out);
|
|
|
|
g_object_unref (out_mem);
|
2010-11-16 11:42:53 +00:00
|
|
|
}
|