PageRenderTime 105ms CodeModel.GetById 4ms app.highlight 87ms RepoModel.GetById 1ms app.codeStats 1ms

/libgxim/gximprotocol.c

https://bitbucket.org/tagoh/libgxim
C | 3564 lines | 2825 code | 292 blank | 447 comment | 462 complexity | 393a595dccd62d79061f56801c644679 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
   2/* 
   3 * gximprotocol.c
   4 * Copyright (C) 2008-2011 Akira TAGOH
   5 * 
   6 * Authors:
   7 *   Akira TAGOH  <akira@tagoh.org>
   8 * 
   9 * This library is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU Lesser General Public
  11 * License as published by the Free Software Foundation; either
  12 * version 2 of the License, or (at your option) any later version.
  13 * 
  14 * This library is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17 * Lesser General Public License for more details.
  18 * 
  19 * You should have received a copy of the GNU Lesser General Public
  20 * License along with this library; if not, write to the
  21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth
  22 * Floor, Boston, MA  02110-1301  USA
  23 */
  24#ifdef HAVE_CONFIG_H
  25#include "config.h"
  26#endif
  27
  28#include <stdlib.h>
  29#ifdef HAVE_STRING_H
  30#include <string.h>
  31#endif
  32#include <glib/gi18n-lib.h>
  33#include <gdk/gdk.h>
  34#include <gdk/gdkx.h>
  35#include "gximacc.h"
  36#include "gximattr.h"
  37#include "gximconnection.h"
  38#include "gximmarshal.h"
  39#include "gximmessages.h"
  40#include "gximmisc.h"
  41#include "gximprotocol10.h"
  42#include "gximtransport.h"
  43#include "gximprotocol.h"
  44
  45#define G_XIM_MARKER_IS_ITEM_BASED(_m_)	((_m_)->type == G_XIM_TYPE_MARKER_N_ITEMS_2)
  46#define G_XIM_GET_MARKER_DATA(_m_)	((_m_)->type == G_XIM_TYPE_BYTE ? (_m_)->value.b : ((_m_)->type == G_XIM_TYPE_WORD || (_m_)->type == G_XIM_TYPE_MARKER_N_ITEMS_2 ? (_m_)->value.w : (_m_)->value.l))
  47
  48
  49/**
  50 * SECTION:gximprotocol
  51 * @Title: GXimProtocol
  52 * @Short_Description: Base interface for XIM protocol
  53 *
  54 * GXimProtocol provides an interface to deal with XIM protocol events.
  55 */
  56
  57typedef struct _GXimProtocolMarker {
  58	GXimValueType type;
  59	GXimValueType where;
  60	goffset       offset;
  61	union {
  62		guint8  b;
  63		guint16 w;
  64		guint32 l;
  65	} value;
  66} GXimProtocolMarker;
  67typedef struct _GXimProtocolSyncable {
  68	GXimOpcode  major_opcode;
  69	guint8      minor_opcode;
  70	gboolean    result;
  71	GError     *error;
  72} GXimProtocolSyncable;
  73typedef struct _GXimProtocolClosureNode {
  74	GCallback  func;
  75	gpointer   user_data;
  76} GXimProtocolClosureNode;
  77
  78enum {
  79	SIGNAL_0,
  80	PARSER_ERROR,
  81	LAST_SIGNAL
  82};
  83
  84static guint signals[LAST_SIGNAL] = { 0 };
  85
  86G_LOCK_DEFINE_STATIC (g_xim_protocol_marker);
  87G_LOCK_DEFINE_STATIC (g_xim_protocol_syncable);
  88
  89/*
  90 * Private functions
  91 */
  92static void
  93g_xim_protocol_base_class_init(gpointer g_iface)
  94{
  95	GXimProtocolIface *iface = g_iface;
  96	static gboolean initialized = FALSE;
  97
  98	if (!initialized) {
  99		initialized = TRUE;
 100
 101		iface->atom_xim_protocol = gdk_atom_intern_static_string("_XIM_PROTOCOL");
 102		iface->atom_xim_moredata = gdk_atom_intern_static_string("_XIM_MOREDATA");
 103		gdk_flush();
 104
 105		iface->message = g_xim_messages_new();
 106		signals[PARSER_ERROR] = g_signal_new("parser_error",
 107						     G_TYPE_FROM_INTERFACE (iface),
 108						     G_SIGNAL_RUN_LAST,
 109						     G_STRUCT_OFFSET (GXimProtocolIface, parser_error),
 110						     _gxim_acc_signal_accumulator__BOOLEAN,
 111						     NULL,
 112						     gxim_marshal_BOOLEAN__UINT_UINT_UINT_UINT,
 113						     G_TYPE_BOOLEAN, 4,
 114						     G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
 115	} else {
 116		g_object_ref(iface->message);
 117	}
 118}
 119
 120static void
 121g_xim_protocol_base_class_finalize(gpointer g_iface)
 122{
 123	GXimProtocolIface *iface = g_iface;
 124
 125	g_object_unref(iface->message);
 126}
 127
 128static gboolean
 129g_xim_protocol_send_header(GXimProtocol   *proto,
 130			   GXimOpcode      major_opcode,
 131			   guint8          minor_opcode,
 132			   GCancellable   *cancellable,
 133			   GError        **error)
 134{
 135	GXimProtocolPrivate *priv;
 136
 137	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
 138	g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, FALSE);
 139
 140	g_data_output_stream_set_byte_order(priv->send_ostream, priv->byte_order);
 141	g_data_output_stream_put_byte(priv->send_ostream, major_opcode, cancellable, error);
 142	g_data_output_stream_put_byte(priv->send_ostream, minor_opcode, cancellable, error);
 143	/* dummy for CARD16 */
 144	g_data_output_stream_put_byte(priv->send_ostream, 0, cancellable, error);
 145	g_data_output_stream_put_byte(priv->send_ostream, 0, cancellable, error);
 146	priv->n_sent += 4;
 147
 148	return error ? *error != NULL : TRUE;
 149}
 150
 151static gsize
 152g_xim_protocol_send_fixate_size(GXimProtocol   *proto,
 153				GCancellable   *cancellable,
 154				GError        **error)
 155{
 156	GXimProtocolIface *iface = G_XIM_PROTOCOL_GET_IFACE (proto);
 157	GXimProtocolPrivate *priv;
 158	goffset pos;
 159	gsize osize;
 160
 161	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), 0);
 162	g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, 0);
 163	g_return_val_if_fail (priv->n_sent > 0, 0);
 164	g_return_val_if_fail (error != NULL, 0);
 165
 166	/* Update the packet size */
 167	osize = priv->n_sent;
 168	if (osize % 4)
 169		g_xim_messages_warning(iface->message,
 170				       "Bad padding: the number of packets: %" G_GSIZE_FORMAT,
 171				       osize);
 172
 173	pos = g_seekable_tell(G_SEEKABLE (priv->base_send_ostream));
 174	g_seekable_seek(G_SEEKABLE (priv->base_send_ostream),
 175			2, G_SEEK_SET, cancellable, error);
 176	g_xim_protocol_send_format(proto, cancellable, error, 1,
 177				   G_XIM_TYPE_WORD, (osize - 4) / 4);
 178	g_seekable_seek(G_SEEKABLE (priv->base_send_ostream),
 179			pos, G_SEEK_SET, cancellable, error);
 180
 181	if (error && *error != NULL)
 182		osize = 0;
 183
 184	return osize;
 185}
 186
 187static gboolean
 188g_xim_protocol_send_terminate(GXimProtocol   *proto,
 189			      GCancellable   *cancellable,
 190			      GError        **error)
 191{
 192	GXimProtocolIface *iface = G_XIM_PROTOCOL_GET_IFACE (proto);
 193	GXimProtocolPrivate *priv;
 194
 195	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
 196	g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, FALSE);
 197
 198	/* XXX: workaround for failing to get the number of bytes written */
 199	g_seekable_seek(G_SEEKABLE (priv->base_send_ostream),
 200			0, G_SEEK_SET, cancellable, error);
 201	G_LOCK (g_xim_protocol_marker);
 202	if (!g_queue_is_empty(priv->markerq)) {
 203		g_xim_messages_bug(iface->message,
 204				   "Unused marker(s) given: %u is still in the queue.",
 205				   g_queue_get_length(priv->markerq));
 206		g_queue_foreach(priv->markerq, (GFunc)g_free, NULL);
 207		g_queue_clear(priv->markerq);
 208	}
 209	G_UNLOCK (g_xim_protocol_marker);
 210	priv->n_sent = 0;
 211
 212	return error ? *error != NULL : TRUE;
 213}
 214
 215static void
 216g_xim_protocol_closure_marshal_BOOLEAN__OBJECT_OBJECT_POINTER(GClosure     *closure,
 217							      GValue       *return_value,
 218							      guint         n_param_values,
 219							      const GValue *param_values,
 220							      gpointer      invocation_hint,
 221							      gpointer      marshal_data)
 222{
 223	register GCClosure *cc = (GCClosure *)closure;
 224	register GXimProtocol *proto;
 225	register GDataInputStream *stream;
 226	register GError **error;
 227	register gpointer data;
 228	register GXimProtocolClosureFunc callback;
 229	gboolean retval;
 230
 231	g_return_if_fail (n_param_values == 3);
 232
 233	proto = g_value_get_object(&param_values[0]);
 234	g_return_if_fail (G_IS_XIM_PROTOCOL (proto));
 235
 236	stream = g_value_get_object(&param_values[1]);
 237	g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
 238
 239	error = g_value_get_pointer(&param_values[2]);
 240
 241	data = closure->data;
 242	callback = cc->callback;
 243
 244	retval = callback((GXimProtocolClosure *)closure, proto, stream, error, data);
 245
 246	g_value_set_boolean(return_value, retval);
 247}
 248
 249static GXimProtocolMarker *
 250g_xim_protocol_find_marker(GQueue        *markerq,
 251			   GXimValueType  vtype)
 252{
 253	GXimProtocolMarker *marker_data = NULL;
 254
 255	G_LOCK (g_xim_protocol_marker);
 256	if (g_queue_is_empty(markerq))
 257		goto end;
 258
 259	marker_data = g_queue_peek_tail(markerq);
 260	if (marker_data->where == vtype)
 261		marker_data = g_queue_pop_tail(markerq);
 262	else
 263		marker_data = NULL;
 264  end:
 265	G_UNLOCK (g_xim_protocol_marker);
 266
 267	return marker_data;
 268}
 269
 270static void
 271_free_sendq(gpointer data,
 272	    gpointer user_data)
 273{
 274	GXimProtocolQueueNode *node = data;
 275
 276	g_free(node->data);
 277	g_free(node);
 278}
 279
 280/*
 281 * Public functions
 282 */
 283GType
 284g_xim_protocol_get_type(void)
 285{
 286	static volatile gsize type_id_volatile = 0;
 287
 288	if (g_once_init_enter(&type_id_volatile)) {
 289		GType type_id;
 290		GTypeInfo info = {
 291			.class_size     = sizeof (GXimProtocolIface),
 292			.base_init      = (GBaseInitFunc)g_xim_protocol_base_class_init,
 293			.base_finalize  = (GBaseFinalizeFunc)g_xim_protocol_base_class_finalize,
 294			.class_init     = NULL,
 295			.class_finalize = NULL,
 296			.class_data     = NULL,
 297			.instance_size  = 0,
 298			.n_preallocs    = 0,
 299			.instance_init  = NULL,
 300			.value_table    = NULL
 301		};
 302
 303		type_id = g_type_register_static(G_TYPE_INTERFACE, "GXimProtocol",
 304						 &info, 0);
 305		g_type_interface_add_prerequisite(type_id, G_TYPE_XIM_TRANSPORT);
 306
 307		g_once_init_leave(&type_id_volatile, type_id);
 308	}
 309
 310	return type_id_volatile;
 311}
 312
 313/**
 314 * g_xim_protocol_init:
 315 * @proto: a #GXimProtocol.
 316 *
 317 * Initializes @proto. this isn't being invoked from the class automatically
 318 * which has a #GXimProtocol interface. you need to call this function manually
 319 * before doing something with @proto.
 320 */
 321void
 322g_xim_protocol_init(GXimProtocol *proto)
 323{
 324	GXimProtocolPrivate *priv;
 325
 326	priv = g_new0(GXimProtocolPrivate, 1);
 327	G_XIM_CHECK_ALLOC_WITH_NO_RET (priv);
 328
 329	priv->base_send_ostream = g_memory_output_stream_new(NULL, 0, g_realloc, g_free);
 330	priv->base_recv_ostream = g_memory_output_stream_new(NULL, 0, g_realloc, g_free);
 331	priv->send_ostream = g_data_output_stream_new(priv->base_send_ostream);
 332	priv->recv_ostream = g_data_output_stream_new(priv->base_recv_ostream);
 333	priv->proto_table__named_index = g_hash_table_new_full(g_str_hash, g_str_equal,
 334							       g_free, (GDestroyNotify)g_closure_unref);
 335	priv->proto_table__id_index = g_hash_table_new(g_direct_hash, g_direct_equal);
 336	priv->markerq = g_queue_new();
 337	priv->syncableq = g_queue_new();
 338	priv->sendq = g_queue_new();
 339	priv->byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN;
 340	priv->is_disconnected = FALSE;
 341	/* XXX: workaround for failing to get the number of bytes written */
 342	priv->n_sent = 0;
 343	priv->n_received = 0;
 344
 345	g_object_set_data(G_OBJECT (proto), "libgxim-protocol-private", priv);
 346
 347	g_xim_protocol10_closure_init(proto, NULL);
 348}
 349
 350/**
 351 * g_xim_protocol_dispose:
 352 * @object: a #GObject.
 353 *
 354 * Releases all references to other objects. This can be used to break
 355 * reference cycles.
 356 *
 357 * This function should only be called from #GObject::dispose which has
 358 * the #GXimProtocol interface.
 359 */
 360void
 361g_xim_protocol_dispose(GObject *object)
 362{
 363	GXimProtocol *proto = G_XIM_PROTOCOL (object);
 364
 365	g_xim_protocol10_closure_finalize(proto);
 366}
 367
 368/**
 369 * g_xim_protocol_finalize:
 370 * @object: a #GObject.
 371 *
 372 * Finalizes the instance.
 373 *
 374 * This function should only be called from #GObject::finalize which has
 375 * the #GXimProtocol interface.
 376 */
 377void
 378g_xim_protocol_finalize(GObject *object)
 379{
 380	GXimProtocol *proto = G_XIM_PROTOCOL (object);
 381	GXimProtocolPrivate *priv = g_xim_protocol_get_private(proto);
 382
 383	if (priv) {
 384		G_LOCK (g_xim_protocol_marker);
 385		g_queue_foreach(priv->markerq, (GFunc)g_free, NULL);
 386		g_queue_free(priv->markerq);
 387		priv->markerq = NULL;
 388		G_UNLOCK (g_xim_protocol_marker);
 389		G_LOCK (g_xim_protocol_syncable);
 390		g_queue_foreach(priv->syncableq, (GFunc)g_free, NULL);
 391		g_queue_free(priv->syncableq);
 392		priv->syncableq = NULL;
 393		G_UNLOCK (g_xim_protocol_syncable);
 394		g_queue_foreach(priv->sendq, (GFunc)_free_sendq, NULL);
 395		g_queue_free(priv->sendq);
 396		priv->sendq = NULL;
 397		g_hash_table_destroy(priv->proto_table__id_index);
 398		g_hash_table_destroy(priv->proto_table__named_index);
 399		g_object_unref(priv->recv_ostream);
 400		g_object_unref(priv->send_ostream);
 401		g_object_unref(priv->base_recv_ostream);
 402		g_object_unref(priv->base_send_ostream);
 403		g_free(priv);
 404	}
 405}
 406
 407/**
 408 * g_xim_protocol_get_private:
 409 * @proto: a #GXimProtocol.
 410 *
 411 * Obtains the #GXimProtocolPrivate which is referenced @proto.
 412 */
 413GXimProtocolPrivate *
 414g_xim_protocol_get_private(GXimProtocol *proto)
 415{
 416	return g_object_get_data(G_OBJECT (proto), "libgxim-protocol-private");
 417}
 418
 419GQuark
 420g_xim_protocol_get_error_quark(void)
 421{
 422	static GQuark quark = 0;
 423
 424	if (!quark)
 425		quark = g_quark_from_static_string("g-xim-protocol-error");
 426
 427	return quark;
 428}
 429
 430/**
 431 * g_xim_protocol_process_event:
 432 * @proto: a #GXimProtocol.
 433 * @event: the #GdkEventClient which wants to process this time.
 434 * @error: a location to store error, or %NULL.
 435 *
 436 * Picks up XIM protocol packets from @event and deal with it as needed.
 437 *
 438 * Returns: %TRUE to be processed successfully.
 439 */
 440gboolean
 441g_xim_protocol_process_event(GXimProtocol    *proto,
 442			     GdkEventClient  *event,
 443			     GError         **error)
 444{
 445	GXimProtocolIface *iface;
 446	GXimProtocolPrivate *priv;
 447	GXimTransport *trans;
 448	gsize transport_size;
 449	gboolean retval = FALSE;
 450
 451	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
 452	g_return_val_if_fail (event != NULL, FALSE);
 453	g_return_val_if_fail (event->type == GDK_CLIENT_EVENT, FALSE);
 454	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
 455	g_return_val_if_fail (error != NULL, FALSE);
 456
 457	iface = G_XIM_PROTOCOL_GET_IFACE (proto);
 458	trans = G_XIM_TRANSPORT (proto);
 459	transport_size = g_xim_transport_get_transport_size(trans);
 460
 461	if (event->message_type == iface->atom_xim_protocol) {
 462		GdkAtom atom_type;
 463		gint i, format, bytes;
 464		guchar *prop = NULL;
 465		GdkDisplay *dpy = g_xim_transport_get_display(trans);
 466
 467		if (event->data_format == 32) {
 468			g_xim_transport_get_property(trans, event->window,
 469						     gdk_x11_xatom_to_atom_for_display(dpy, event->data.l[1]),
 470						     GDK_NONE, event->data.l[0],
 471						     &atom_type, &format, &bytes, &prop);
 472			if (prop) {
 473				for (i = 0; i < bytes; i++) {
 474					g_data_output_stream_put_byte(priv->recv_ostream,
 475								      prop[i],
 476								      NULL,
 477								      NULL);
 478					priv->n_received++;
 479				}
 480			}
 481		} else if (event->data_format == 8) {
 482			gsize i;
 483
 484			for (i = 0; i < transport_size; i++) {
 485				g_data_output_stream_put_byte(priv->recv_ostream,
 486							      event->data.b[i],
 487							      NULL,
 488							      NULL);
 489				priv->n_received++;
 490			}
 491		} else {
 492			g_set_error(error, G_XIM_PROTOCOL_ERROR,
 493				    G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_WARNING,
 494				    _("Invalid packets on _XIM_PROTOCOL: format: %d."),
 495				    event->data_format);
 496		}
 497		g_free(prop);
 498
 499		if (priv->n_received > 0) {
 500			gpointer p;
 501			gchar *data;
 502			gssize length = priv->n_received;
 503
 504			p = g_memory_output_stream_get_data(G_MEMORY_OUTPUT_STREAM (priv->base_recv_ostream));
 505			data = g_new0(gchar, sizeof (gchar) * length + 1);
 506			memcpy(data, p, sizeof (gchar) * length);
 507
 508			/* XXX: workaround for failing to get the number of bytes written */
 509			g_seekable_seek(G_SEEKABLE (priv->base_recv_ostream),
 510					0, G_SEEK_SET, NULL, NULL);
 511			priv->n_received = 0;
 512
 513			retval = g_xim_protocol_translate(proto, data, length, error);
 514
 515			g_free(data);
 516		}
 517	} else if (event->message_type == iface->atom_xim_moredata) {
 518		gsize i;
 519
 520		for (i = 0; i < transport_size; i++) {
 521			g_data_output_stream_put_byte(priv->recv_ostream,
 522						      event->data.b[i],
 523						      NULL,
 524						      NULL);
 525			priv->n_received++;
 526		}
 527		/* not yet process the packet.
 528		 * there are still more packets.
 529		 */
 530		retval = TRUE;
 531	}
 532	if (priv->is_disconnected) {
 533		/* unref here */
 534		g_object_unref(proto);
 535	}
 536
 537	return retval;
 538}
 539
 540/**
 541 * g_xim_protocol_translate:
 542 * @proto: a #GXimProtocol.
 543 * @data: a chunk of XIM protocol packets
 544 * @length: the number of @data.
 545 * @error: a location to store error, or %NULL.
 546 *
 547 * Translate the packets and deliver it to the appropriate places.
 548 *
 549 * Returns: %TRUE to be processed successfully.
 550 */
 551#pragma GCC diagnostic ignored "-Wformat-extra-args" /* to prevent an error of using G_GSIZE_FORMAT */
 552gboolean
 553g_xim_protocol_translate(GXimProtocol     *proto,
 554			 gpointer          data,
 555			 gssize            length,
 556			 GError          **error)
 557{
 558	GXimProtocolIface *iface;
 559	GXimProtocolPrivate *priv;
 560	GXimTransport *trans;
 561	GInputStream *base_istream = NULL;
 562	GDataInputStream *istream = NULL;
 563	guint8 major_opcode, minor_opcode;
 564	guint16 packlen = 0;
 565	gsize avail;
 566	gboolean ret = FALSE;
 567	GXimProtocolSyncable *on_sync = NULL;
 568	GClosure *closure;
 569
 570	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
 571	g_return_val_if_fail (data != NULL, FALSE);
 572	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
 573
 574	iface = G_XIM_PROTOCOL_GET_IFACE (proto);
 575	trans = G_XIM_TRANSPORT (proto);
 576
 577	if (length == 0) {
 578		g_set_error(error, G_XIM_PROTOCOL_ERROR,
 579			    G_XIM_PROTOCOL_ERROR_NO_DATA | G_XIM_NOTICE_ERROR,
 580			    "No data to translate.");
 581		goto end;
 582	}
 583	g_xim_transport_dump(trans, data, length, FALSE);
 584
 585	base_istream = g_memory_input_stream_new_from_data(data, length, NULL);
 586	istream = g_data_input_stream_new(base_istream);
 587
 588	major_opcode = g_data_input_stream_read_byte(istream, NULL, NULL);
 589	minor_opcode = g_data_input_stream_read_byte(istream, NULL, error);
 590	if (*error)
 591		goto end;
 592	if (major_opcode == G_XIM_CONNECT) {
 593		/* decide the byte order */
 594		if (((gchar *)data)[4] == 0x42) {
 595			/* this is a big endian */
 596			priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
 597		} else if (((gchar *)data)[4] == 0x6c) {
 598			/* this is a little endian */
 599			priv->byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
 600		} else {
 601			g_set_error(error, G_XIM_PROTOCOL_ERROR,
 602				    G_XIM_PROTOCOL_ERROR_UNKNOWN_ENDIAN | G_XIM_NOTICE_ERROR,
 603				    "Unknown endian `%02X` or invalid packet received.",
 604				    ((gchar *)data)[4]);
 605			goto end;
 606		}
 607		g_xim_messages_debug(iface->message, "proto",
 608				     "Byte order is a %s",
 609				     priv->byte_order == G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN ?
 610				     "big endian" : "little endian");
 611	}
 612	g_data_input_stream_set_byte_order(istream, priv->byte_order);
 613	packlen = g_data_input_stream_read_uint16(istream, NULL, error);
 614	if (*error)
 615		goto end;
 616	avail = g_buffered_input_stream_get_buffer_size(G_BUFFERED_INPUT_STREAM (istream));
 617	if (avail < (packlen * 4)) {
 618		g_set_error(error, G_XIM_PROTOCOL_ERROR,
 619			    G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
 620			    "Received packets size `%" G_GUINT16_FORMAT "' is less than `%" G_GSIZE_FORMAT "'",
 621			    packlen * 4, avail);
 622		goto end;
 623	}
 624	/* After here, all the read should succeeds */
 625
 626	G_LOCK (g_xim_protocol_syncable);
 627	if (!g_queue_is_empty(priv->syncableq)) {
 628		guint len, i;
 629		GXimProtocolSyncable *syncable;
 630
 631		len = g_queue_get_length(priv->syncableq);
 632		for (i = 0; i < len; i++) {
 633			syncable = g_queue_peek_nth(priv->syncableq, i);
 634			if (syncable->major_opcode == major_opcode &&
 635			    syncable->minor_opcode == minor_opcode) {
 636				on_sync = syncable;
 637				syncable->result = TRUE;
 638				break;
 639			} else if (major_opcode == G_XIM_ERROR &&
 640				   minor_opcode == 0) {
 641				on_sync = syncable;
 642				syncable->result = FALSE;
 643				break;
 644			}
 645		}
 646	}
 647	G_UNLOCK (g_xim_protocol_syncable);
 648
 649	closure = (GClosure *)g_xim_protocol_lookup_protocol_by_id(proto, major_opcode, minor_opcode);
 650	if (closure == NULL) {
 651		g_xim_messages_bug(iface->message,
 652				   "Unknown opcode in the packets: major: %d, minor: %d",
 653				   major_opcode, minor_opcode);
 654	} else {
 655		GValue v = { 0, }, *pv;
 656		guint n = 3, i;
 657
 658		g_value_init(&v, G_TYPE_BOOLEAN);
 659		pv = g_new0(GValue, n);
 660		G_XIM_GERROR_CHECK_ALLOC (pv, error,
 661					  G_XIM_PROTOCOL_ERROR, FALSE);
 662
 663		g_value_init(&pv[0], G_TYPE_OBJECT);
 664		g_value_set_object(&pv[0], proto);
 665		g_value_init(&pv[1], G_TYPE_DATA_INPUT_STREAM);
 666		g_value_set_object(&pv[1], istream);
 667		g_value_init(&pv[2], G_TYPE_POINTER);
 668		g_value_set_pointer(&pv[2], error);
 669		g_closure_invoke(closure, &v, n, pv, NULL);
 670
 671		for (i = 0; i < n; i++) {
 672			g_value_unset(&pv[i]);
 673		}
 674		ret = g_value_get_boolean(&v);
 675		if (!ret && *error == NULL) {
 676			g_set_error(error, G_XIM_PROTOCOL_ERROR,
 677				    G_XIM_PROTOCOL_ERROR_NO_PARSER | G_XIM_NOTICE_ERROR,
 678				    "No parser available for %s",
 679				    ((GXimProtocolClosure *)closure)->name);
 680		}
 681		g_free(pv);
 682	}
 683
 684  end:
 685	if (istream)
 686		g_object_unref(istream);
 687	if (base_istream)
 688		g_object_unref(base_istream);
 689	if (*error) {
 690		if (on_sync)
 691			on_sync->error = g_error_copy(*error);
 692	}
 693	if (on_sync) {
 694		if (*error == NULL && !ret)
 695			g_set_error(&on_sync->error, G_XIM_PROTOCOL_ERROR,
 696				    G_XIM_PROTOCOL_ERROR_DELIVERY_FAILURE | G_XIM_NOTICE_ERROR,
 697				    "Unable to marshal a signal.");
 698		on_sync->result &= ret;
 699		G_LOCK (g_xim_protocol_syncable);
 700		g_queue_remove(priv->syncableq, on_sync);
 701		G_UNLOCK (g_xim_protocol_syncable);
 702	}
 703	length -= (packlen * 4 + 4);
 704	if (ret &&
 705	    length > 4 &&
 706	    ((gchar *)data)[(packlen * 4) + 4] != 0) {
 707		/* some IMs sends the multiple protocols in one request. */
 708		ret = g_xim_protocol_translate(proto, &((gchar *)data)[packlen * 4 + 4], length, error);
 709	}
 710
 711	return ret; /* XXX */
 712}
 713#pragma GCC diagnostic error "-Wformat-extra-args" /* this might be wrong to get back the status */
 714
 715/**
 716 * g_xim_protocol_send_packets:
 717 * @proto: a #GXimProtocol.
 718 * @data: a chunk of XIM protocol packets.
 719 * @length: the number of @data.
 720 *
 721 * Sends @data to the opposite direction on the connection.
 722 *
 723 * Returns: %TRUE to be sent successfully.
 724 */
 725gboolean
 726g_xim_protocol_send_packets(GXimProtocol  *proto,
 727			    const gchar   *data,
 728			    gsize          length)
 729{
 730	GXimProtocolIface *iface;
 731	GXimTransport *trans;
 732	gboolean retval = FALSE;
 733	guint8 major, minor;
 734	gsize transport_size, transport_max;
 735
 736	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
 737	g_return_val_if_fail (data != NULL, FALSE);
 738
 739	trans = G_XIM_TRANSPORT (proto);
 740	g_return_val_if_fail (g_xim_transport_get_version(trans, &major, &minor), FALSE);
 741
 742	iface = G_XIM_PROTOCOL_GET_IFACE (proto);
 743	transport_size = g_xim_transport_get_transport_size(trans);
 744	transport_max = g_xim_transport_get_transport_max(trans);
 745
 746	switch (major) {
 747	    case 0:
 748		    switch (minor) {
 749			case 0:
 750				if (length > transport_size) {
 751					/* Send data via Property */
 752					retval = g_xim_transport_send_via_property(trans,
 753										   data,
 754										   length);
 755				} else {
 756					/* Send data via ClientMessage */
 757					retval = g_xim_transport_send_via_cm(trans,
 758									     data,
 759									     length,
 760									     transport_size);
 761				}
 762				break;
 763			case 1:
 764				/* Send data via ClientMessage or multiple ClientMessages */
 765				retval = g_xim_transport_send_via_cm(trans,
 766								     data,
 767								     length,
 768								     transport_size);
 769				break;
 770			case 2:
 771				if (length > transport_max) {
 772					/* Send data via Property */
 773					retval = g_xim_transport_send_via_property(trans,
 774										   data,
 775										   length);
 776				} else {
 777					/* Send data via ClientMessage or multiple ClientMessages */
 778					retval = g_xim_transport_send_via_cm(trans,
 779									     data,
 780									     length,
 781									     transport_size);
 782				}
 783				break;
 784			default:
 785				g_xim_messages_warning(iface->message,
 786						       "Unsupported protocol version: %d.%d",
 787						       major, minor);
 788				break;
 789		    }
 790		    break;
 791	    case 1:
 792		    switch (minor) {
 793			case 0:
 794				/* Send data via PropertyNotify */
 795				retval = g_xim_transport_send_via_property_notify(trans,
 796										  data,
 797										  length);
 798				break;
 799			default:
 800				g_xim_messages_warning(iface->message,
 801						       "Unsupported protocol version: %d.%d",
 802						       major, minor);
 803				break;
 804		    }
 805		    break;
 806	    case 2:
 807		    switch (minor) {
 808			case 0:
 809				if (length > transport_size) {
 810					/* Send data via PropertyNotify */
 811					retval = g_xim_transport_send_via_property_notify(trans,
 812											  data,
 813											  length);
 814				} else {
 815					/* Send data via ClientMessage */
 816					retval = g_xim_transport_send_via_cm(trans,
 817									     data,
 818									     length,
 819									     transport_size);
 820				}
 821				break;
 822			case 1:
 823				if (length > transport_max) {
 824					/* Send data via Property */
 825					retval = g_xim_transport_send_via_property_notify(trans,
 826											  data,
 827											  length);
 828				} else {
 829					/* Send data via ClientMessage or multiple ClientMessages */
 830					retval = g_xim_transport_send_via_cm(trans,
 831									     data,
 832									     length,
 833									     transport_size);
 834				}
 835				break;
 836			default:
 837				g_xim_messages_warning(iface->message,
 838						       "Unsupported protocol version: %d.%d",
 839						       major, minor);
 840				break;
 841		    }
 842		    break;
 843	    default:
 844		    g_xim_messages_warning(iface->message,
 845					   "Unsupported protocol version: %d.%d",
 846					   major, minor);
 847		    break;
 848	}
 849
 850	return retval;
 851}
 852
 853/**
 854 * g_xim_protocol_send_vformat:
 855 * @proto: a #GXimProtocol.
 856 * @cancellable: optional #GCancellable object, %NULL to ignore.
 857 * @error: a location to store error, or %NULL.
 858 * @n_params: the number of a set of parameters.
 859 * @args: a #va_list.
 860 *
 861 * Converts the objects to the XIM protocol packets according to format
 862 * and store it into the send buffer. you shouldn't use this function directly.
 863 * use g_xim_protocol_send() instead.
 864 *
 865 * Returns: the number of data stored.
 866 */
 867gsize
 868g_xim_protocol_send_vformat(GXimProtocol  *proto,
 869			    GCancellable  *cancellable,
 870			    GError       **error,
 871			    guint          n_params,
 872			    va_list        args)
 873{
 874	GXimProtocolIface *iface;
 875	GXimProtocolPrivate *priv;
 876	GXimTransport *trans;
 877	guint i, j;
 878	gsize n_sent, prev_size = 0;
 879
 880	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), 0);
 881	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), 0);
 882	g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, 0);
 883	g_return_val_if_fail (error != NULL, 0);
 884
 885	trans = G_XIM_TRANSPORT (proto);
 886	iface = G_XIM_PROTOCOL_GET_IFACE (proto);
 887	n_sent = priv->n_sent;
 888	for (i = 0; i < n_params; i++) {
 889		GXimValueType type, mtype;
 890		gchar *p;
 891		gsize sz, padding;
 892		GXimAttr *attr;
 893		goffset cur_pos = priv->n_sent;
 894		GXimProtocolMarker *marker_data;
 895		GSList *list, *l;
 896		gpointer value;
 897
 898		type = va_arg(args, GXimValueType);
 899		switch (type) {
 900		    case G_XIM_TYPE_PADDING:
 901			    padding = va_arg(args, unsigned int);
 902		    padding:
 903			    for (sz = 0; sz < padding; sz++) {
 904				    g_data_output_stream_put_byte(priv->send_ostream, 0,
 905								  cancellable, error);
 906				    if (*error)
 907					    goto end;
 908				    priv->n_sent++;
 909			    }
 910			    break;
 911		    case G_XIM_TYPE_AUTO_PADDING:
 912			    sz = va_arg(args, unsigned int);
 913			    padding = g_xim_protocol_n_pad4(prev_size + sz);
 914			    goto padding;
 915		    case G_XIM_TYPE_MARKER_N_BYTES_1:
 916			    mtype = va_arg(args, GXimValueType);
 917			    marker_data = g_new0(GXimProtocolMarker, 1);
 918			    G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
 919						      G_XIM_PROTOCOL_ERROR, 0);
 920
 921			    marker_data->type = G_XIM_TYPE_BYTE;
 922			    marker_data->where = mtype;
 923			    marker_data->offset = g_seekable_tell(G_SEEKABLE (priv->base_send_ostream));
 924			    g_queue_push_tail(priv->markerq, marker_data);
 925			    g_data_output_stream_put_byte(priv->send_ostream, 0, NULL, NULL);
 926			    priv->n_sent++;
 927			    break;
 928		    case G_XIM_TYPE_MARKER_N_BYTES_2:
 929			    mtype = va_arg(args, GXimValueType);
 930			    marker_data = g_new0(GXimProtocolMarker, 1);
 931			    G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
 932						      G_XIM_PROTOCOL_ERROR, 0);
 933
 934			    marker_data->type = G_XIM_TYPE_WORD;
 935			    marker_data->where = mtype;
 936			    marker_data->offset = g_seekable_tell(G_SEEKABLE (priv->base_send_ostream));
 937			    g_queue_push_tail(priv->markerq, marker_data);
 938			    g_data_output_stream_put_uint16(priv->send_ostream, 0,
 939							    cancellable, error);
 940			    priv->n_sent += 2;
 941			    break;
 942		    case G_XIM_TYPE_MARKER_N_BYTES_4:
 943			    mtype = va_arg(args, GXimValueType);
 944			    marker_data = g_new0(GXimProtocolMarker, 1);
 945			    G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
 946						      G_XIM_PROTOCOL_ERROR, 0);
 947
 948			    marker_data->type = G_XIM_TYPE_LONG;
 949			    marker_data->where = mtype;
 950			    marker_data->offset = g_seekable_tell(G_SEEKABLE (priv->base_send_ostream));
 951			    g_queue_push_tail(priv->markerq, marker_data);
 952			    g_data_output_stream_put_uint32(priv->send_ostream, 0,
 953							    cancellable, error);
 954			    priv->n_sent += 4;
 955			    break;
 956		    case G_XIM_TYPE_STR:
 957			    value = va_arg(args, gpointer);
 958			    priv->n_sent += g_xim_str_put_to_stream((GXimStr *)value,
 959								    priv->send_ostream,
 960								    cancellable, error);
 961			    break;
 962		    case G_XIM_TYPE_GSTRING:
 963			    value = va_arg(args, gpointer);
 964			    priv->n_sent += g_xim_gstring_put_to_stream((GString *)value,
 965									priv->send_ostream,
 966									cancellable, error);
 967			    /* We don't want to include the length of characters for markers */
 968			    cur_pos += 2;
 969			    break;
 970		    case G_XIM_TYPE_PREEDIT_CARET:
 971			    value = va_arg(args, gpointer);
 972			    priv->n_sent += g_xim_preedit_caret_put_to_stream((GXimPreeditCaret *)value,
 973									      proto, cancellable, error);
 974			    break;
 975		    case G_XIM_TYPE_PREEDIT_DRAW:
 976			    value = va_arg(args, gpointer);
 977			    priv->n_sent += g_xim_preedit_draw_put_to_stream((GXimPreeditDraw *)value,
 978									     proto, cancellable, error);
 979			    break;
 980		    case G_XIM_TYPE_GDKEVENT:
 981			    value = va_arg(args, gpointer);
 982			    priv->n_sent += g_xim_gdkevent_put_to_stream((GdkEvent *)value,
 983									 proto, cancellable, error);
 984			    break;
 985		    case G_XIM_TYPE_XIMTEXT:
 986			    value = va_arg(args, gpointer);
 987			    priv->n_sent += g_xim_text_put_to_stream((GXimText *)value,
 988								     proto, cancellable, error);
 989			    break;
 990		    case G_XIM_TYPE_HOTKEY_TRIGGER:
 991			    value = va_arg(args, gpointer);
 992			    priv->n_sent += g_xim_hotkey_trigger_put_to_stream((GXimHotkeyTrigger *)value,
 993									       proto, cancellable, error);
 994			    break;
 995		    case G_XIM_TYPE_PIXMAP:
 996			    value = va_arg(args, gpointer);
 997			    g_data_output_stream_put_uint32(priv->send_ostream,
 998							    g_xim_transport_get_native_channel_from(trans, value),
 999							    cancellable,
1000							    error);
1001			    priv->n_sent += 4;
1002			    break;
1003		    case G_XIM_TYPE_STATUS_DRAW:
1004			    value = va_arg(args, gpointer);
1005			    priv->n_sent += g_xim_status_draw_put_to_stream((GXimStatusDraw *)value,
1006									    proto, cancellable, error);
1007			    break;
1008		    case G_XIM_TYPE_LIST_OF_IMATTR:
1009		    case G_XIM_TYPE_LIST_OF_ICATTR:
1010			    list = va_arg(args, gpointer);
1011			    for (l = list; l != NULL; l = g_slist_next(l)) {
1012				    priv->n_sent += g_xim_raw_attr_put_to_stream(l->data, proto,
1013										 cancellable, error);
1014				    if (*error) {
1015					    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1016						    g_xim_messages_gerror(iface->message, *error);
1017						    g_clear_error(error);
1018					    } else {
1019						    goto end;
1020					    }
1021				    }
1022			    }
1023			    break;
1024		    case G_XIM_TYPE_LIST_OF_IMATTRIBUTE:
1025			    if (!G_IS_XIM_IM_ATTR (G_XIM_CONNECTION (proto)->imattr)) {
1026				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1027						G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
1028						"Protocol order might be invalid. XIM_OPEN must be brought up first.");
1029				    goto end;
1030			    } else {
1031				    attr = G_XIM_ATTR (G_XIM_CONNECTION (proto)->imattr);
1032			    }
1033			    goto process_attribute;
1034		    case G_XIM_TYPE_LIST_OF_ICATTRIBUTE:
1035			    if (!G_IS_XIM_IC_ATTR (G_XIM_CONNECTION (proto)->default_icattr)) {
1036				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1037						G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
1038						"Protocol order might be invalid. XIM_OPEN must be brought up first.");
1039				    goto end;
1040			    } else {
1041				    attr = G_XIM_ATTR (G_XIM_CONNECTION (proto)->default_icattr);
1042			    }
1043		    process_attribute:
1044			    list = va_arg(args, gpointer);
1045			    for (l = list; l != NULL; l = g_slist_next(l)) {
1046				    GXimAttribute *a = l->data;
1047				    GXimValueType vt = g_xim_gtype_to_value_type(g_xim_attr_get_gtype_by_id(attr, a->id));
1048
1049				    if (a->vtype != vt) {
1050					    g_xim_messages_warning(iface->message,
1051								   "Unknown attribute ID is specified: %d (%s)",
1052								   a->id, g_xim_value_type_name(a->vtype));
1053				    }
1054				    priv->n_sent += g_xim_attribute_put_to_stream(l->data, proto,
1055										  cancellable, error);
1056				    if (*error) {
1057					    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1058						    g_xim_messages_gerror(iface->message, *error);
1059						    g_clear_error(error);
1060					    } else {
1061						    goto end;
1062					    }
1063				    }
1064			    }
1065			    break;
1066		    case G_XIM_TYPE_LIST_OF_EXT:
1067			    list = va_arg(args, gpointer);
1068			    for (l = list; l != NULL; l = g_slist_next(l)) {
1069				    priv->n_sent += g_xim_ext_put_to_stream(l->data,
1070									    proto,
1071									    cancellable,
1072									    error);
1073				    if (*error) {
1074					    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1075						    g_xim_messages_gerror(iface->message, *error);
1076						    g_clear_error(error);
1077					    } else {
1078						    goto end;
1079					    }
1080				    }
1081			    }
1082			    break;
1083		    case G_XIM_TYPE_LIST_OF_STRING:
1084			    list = va_arg(args, GSList *);
1085			    for (l = list; l != NULL; l = g_slist_next(l)) {
1086				    priv->n_sent += g_xim_string_put_to_stream(l->data,
1087									       priv->send_ostream,
1088									       cancellable,
1089									       error);
1090				    if (*error) {
1091					    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1092						    g_xim_messages_gerror(iface->message, *error);
1093						    g_clear_error(error);
1094					    } else {
1095						    goto end;
1096					    }
1097				    }
1098			    }
1099			    break;
1100		    case G_XIM_TYPE_LIST_OF_STR:
1101			    list = va_arg(args, GSList *);
1102			    for (l = list; l != NULL; l = g_slist_next(l)) {
1103				    priv->n_sent += g_xim_str_put_to_stream(l->data,
1104									    priv->send_ostream,
1105									    cancellable,
1106									    error);
1107				    if (*error) {
1108					    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1109						    g_xim_messages_gerror(iface->message, *error);
1110						    g_clear_error(error);
1111					    } else {
1112						    goto end;
1113					    }
1114				    }
1115			    }
1116			    break;
1117		    case G_XIM_TYPE_LIST_OF_ENCODINGINFO:
1118			    list = va_arg(args, GSList *);
1119			    for (l = list; l != NULL; l = g_slist_next(l)) {
1120				    priv->n_sent += g_xim_encodinginfo_put_to_stream(l->data,
1121										     priv->send_ostream,
1122										     cancellable,
1123										     error);
1124				    if (*error) {
1125					    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1126						    g_xim_messages_gerror(iface->message, *error);
1127						    g_clear_error(error);
1128					    } else {
1129						    goto end;
1130					    }
1131				    }
1132			    }
1133			    break;
1134		    case G_XIM_TYPE_LIST_OF_BYTE:
1135			    value = va_arg(args, gpointer);
1136			    for (j = 0; j < ((GString *)value)->len; j++) {
1137				    g_data_output_stream_put_byte(priv->send_ostream,
1138								  ((GString *)value)->str[j],
1139								  cancellable, error);
1140				    priv->n_sent++;
1141				    if (*error)
1142					    goto end;
1143			    }
1144			    break;
1145		    case G_XIM_TYPE_LIST_OF_CARD16:
1146			    list = va_arg(args, GSList *);
1147			    for (l = list; l != NULL; l = g_slist_next(l)) {
1148				    g_data_output_stream_put_uint16(priv->send_ostream,
1149								    GPOINTER_TO_UINT (l->data),
1150								    cancellable,
1151								    error);
1152				    priv->n_sent += 2;
1153				    if (*error) {
1154					    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1155						    g_xim_messages_gerror(iface->message, *error);
1156						    g_clear_error(error);
1157					    } else {
1158						    goto end;
1159					    }
1160				    }
1161			    }
1162			    break;
1163		    case G_XIM_TYPE_LIST_OF_HOTKEY_TRIGGER:
1164			    list = va_arg(args, GSList *);
1165			    for (l = list; l != NULL; l = g_slist_next(l)) {
1166				    priv->n_sent += g_xim_hotkey_trigger_put_to_stream(l->data,
1167										       proto,
1168										       cancellable,
1169										       error);
1170				    if (*error) {
1171					    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1172						    g_xim_messages_gerror(iface->message, *error);
1173						    g_clear_error(error);
1174					    } else {
1175						    goto end;
1176					    }
1177				    }
1178			    }
1179			    break;
1180		    case G_XIM_TYPE_SEPARATOR:
1181			    break;
1182		    case G_XIM_TYPE_BYTE:
1183			    g_data_output_stream_put_byte(priv->send_ostream,
1184							  (guint8)va_arg(args, int),
1185							  cancellable, error);
1186			    priv->n_sent++;
1187			    break;
1188		    case G_XIM_TYPE_WORD:
1189			    g_data_output_stream_put_uint16(priv->send_ostream,
1190							    (guint16)va_arg(args, unsigned int),
1191							    cancellable, error);
1192			    priv->n_sent += 2;
1193			    break;
1194		    case G_XIM_TYPE_LONG:
1195			    g_data_output_stream_put_uint32(priv->send_ostream,
1196							    (guint32)va_arg(args, unsigned long),
1197							    cancellable, error);
1198			    priv->n_sent += 4;
1199			    break;
1200		    case G_XIM_TYPE_CHAR:
1201			    p = va_arg(args, gchar *);
1202			    for (; p != NULL && *p != 0; p++) {
1203				    g_data_output_stream_put_byte(priv->send_ostream, *p,
1204								  cancellable, error);
1205				    priv->n_sent++;
1206				    if (*error)
1207					    goto end;
1208			    }
1209			    break;
1210		    case G_XIM_TYPE_WINDOW:
1211			    value = va_arg(args, gpointer);
1212			    g_data_output_stream_put_uint32(priv->send_ostream,
1213							    g_xim_transport_get_native_channel_from(trans, value),
1214							    cancellable,
1215							    error);
1216			    priv->n_sent += 4;
1217			    break;
1218		    case G_XIM_TYPE_XIMSTYLES:
1219			    value = va_arg(args, gpointer);
1220			    priv->n_sent += g_xim_styles_put_to_stream((GXimStyles *)value,
1221								       proto,
1222								       cancellable,
1223								       error);
1224			    break;
1225		    case G_XIM_TYPE_XRECTANGLE:
1226			    priv->n_sent += g_xim_rectangle_put_to_stream(va_arg(args, GXimRectangle *),
1227									  proto,
1228									  cancellable,
1229									  error);
1230			    break;
1231		    case G_XIM_TYPE_XPOINT:
1232			    value = va_arg(args, gpointer);
1233			    priv->n_sent += g_xim_point_put_to_stream((GXimPoint *)value,
1234								      proto,
1235								      cancellable,
1236								      error);
1237			    break;
1238		    case G_XIM_TYPE_XFONTSET:
1239			    value = va_arg(args, gpointer);
1240			    priv->n_sent += g_xim_fontset_put_to_stream((GXimFontSet *)value,
1241									proto,
1242									cancellable,
1243									error);
1244			    break;
1245		    case G_XIM_TYPE_HOTKEYTRIGGERS:
1246		    case G_XIM_TYPE_HOTKEYSTATE:
1247		    case G_XIM_TYPE_STRINGCONVERSION:
1248		    case G_XIM_TYPE_PREEDITSTATE:
1249		    case G_XIM_TYPE_RESETSTATE:
1250			    goto nonsupported;
1251		    case G_XIM_TYPE_NESTEDLIST:
1252			    value = va_arg(args, gpointer);
1253			    priv->n_sent += g_xim_nested_list_put_to_stream((GXimNestedList *)value,
1254									    proto,
1255									    cancellable,
1256									    error);
1257			    break;
1258		    default:
1259		    nonsupported:
1260			    g_xim_messages_warning(iface->message,
1261						   "Unknown/unsupported value type to send packets: %s",
1262						   g_xim_value_type_name(type));
1263#ifdef GNOME_ENABLE_DEBUG
1264			    abort();
1265#endif
1266			    break;
1267		}
1268		if (!g_queue_is_empty(priv->markerq)) {
1269			marker_data = g_queue_peek_tail(priv->markerq);
1270			if (marker_data->where == type) {
1271				goffset cur = g_seekable_tell(G_SEEKABLE (priv->base_send_ostream));
1272				gsize save_n_sent = priv->n_sent;
1273
1274				g_seekable_seek(G_SEEKABLE (priv->base_send_ostream),
1275						marker_data->offset, G_SEEK_SET, NULL, NULL);
1276				g_xim_protocol_send_format(proto, cancellable, error, 1,
1277							   marker_data->type, cur - cur_pos);
1278				priv->n_sent = save_n_sent;
1279				g_seekable_seek(G_SEEKABLE (priv->base_send_ostream),
1280						cur, G_SEEK_SET, NULL, NULL);
1281				g_free(g_queue_pop_tail(priv->markerq));
1282			}
1283		}
1284		prev_size = priv->n_sent - cur_pos;
1285		if (*error) {
1286			if (!G_XIM_GERROR_IS_RECOVERABLE (*error))
1287				goto end;
1288			g_xim_messages_gerror(iface->message, *error);
1289			g_clear_error(error);
1290		}
1291	}
1292  end:
1293	if (*error)
1294		return 0;
1295
1296	return priv->n_sent - n_sent;
1297}
1298
1299/**
1300 * g_xim_protocol_send_format:
1301 * @proto: a #GXimProtocol.
1302 * @cancellable: optional #GCancellable object, %NULL to ignore.
1303 * @error: a location to store error, or %NULL.
1304 * @n_params: the number of a set of parameters.
1305 *
1306 * Converts the objects to the XIM protocol packets according to the format
1307 * and store it into the send buffer. you shouldn't use this function directly.
1308 * use g_xim_protocol_send() instead.
1309 *
1310 * Returns: the number of data stored.
1311 */
1312gsize
1313g_xim_protocol_send_format(GXimProtocol  *proto,
1314			   GCancellable  *cancellable,
1315			   GError       **error,
1316			   guint          n_params,
1317			   ...)
1318{
1319	va_list ap;
1320	gsize retval;
1321
1322	va_start(ap, n_params);
1323
1324	retval = g_xim_protocol_send_vformat(proto,
1325					     cancellable,
1326					     error,
1327					     n_params, ap);
1328
1329	va_end(ap);
1330
1331	return retval;
1332}
1333
1334/**
1335 * g_xim_protocol_send:
1336 * @proto: a #GXimProtocol.
1337 * @major_opcode: a major opcode in XIM protocol.
1338 * @minor_opcode: a minor opcode in XIM protocol.
1339 * @n_params: the number of a set of parameters.
1340 *
1341 * Sends XIM protocol according to the set of parameters. one is a parameter
1342 * type defined in #GXimValueType, and one is an object related to that.
1343 * See #GXimValueType documentation which objects are supposed to be specified
1344 * with them.
1345 *
1346 * Returns: %TRUE to be sent successfully.
1347 */
1348gboolean
1349g_xim_protocol_send(GXimProtocol  *proto,
1350		    GXimOpcode     major_opcode,
1351		    guint8         minor_opcode,
1352		    guint          n_params,
1353		    ...)
1354{
1355	GXimProtocolPrivate *priv;
1356	va_list ap;
1357	gpointer data;
1358	gboolean retval = FALSE;
1359	gsize osize;
1360	GError *error = NULL;
1361
1362	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
1363	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
1364	g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, FALSE);
1365
1366	va_start(ap, n_params);
1367
1368	if (!g_xim_protocol_send_header(proto, major_opcode, minor_opcode,
1369					NULL, NULL))
1370		goto end;
1371
1372	g_xim_protocol_send_vformat(proto, NULL, &error, n_params, ap);
1373	if (error) {
1374		g_xim_messages_gerror(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
1375				     error);
1376		if (!G_XIM_GERROR_IS_RECOVERABLE (error))
1377			goto end;
1378		g_clear_error(&error);
1379	}
1380
1381	if ((osize = g_xim_protocol_send_fixate_size(proto, NULL, &error)) == 0)
1382		goto end;
1383
1384	data = g_memory_output_stream_get_data(G_MEMORY_OUTPUT_STREAM (priv->base_send_ostream));
1385	if (g_queue_get_length(priv->sendq) > 0) {
1386		GXimProtocolQueueNode *node = g_queue_peek_tail(priv->sendq);
1387
1388		if (node->data == NULL) {
1389			/* if it's the queueing mode, do not send the packet */
1390			g_xim_messages_debug(G_XIM_PROTOCOL_GET_IFACE (proto)->message, "proto/event",
1391					     "Queued the packet for %s",
1392					     g_xim_protocol_name(major_opcode));
1393			node->data = g_new0(gchar, osize + 1);
1394			memcpy(node->data, data, osize);
1395			node->length = osize;
1396			retval = TRUE;
1397			goto end;
1398		}
1399	}
1400	retval = g_xim_protocol_send_packets(proto, data, osize);
1401
1402  end:
1403	if (error)
1404		g_error_free(error);
1405	retval &= g_xim_protocol_send_terminate(proto, NULL, NULL);
1406
1407	va_end(ap);
1408
1409	return retval;
1410}
1411
1412/**
1413 * g_xim_protocol_send_with_list:
1414 * @proto: a #GXimProtocol.
1415 * @major_opcode: a major opcode in XIM protocol.
1416 * @minor_opcode: a minor opcode in XIM protocol.
1417 * @types: a list of #GXimValueType.
1418 * @values: a list of objects fitting to each value types in @types.
1419 *
1420 * Sends XIM protocol according to @types and @values.
1421 * See #GXimValueType documentation which objects are supposed to be specified
1422 * with them.
1423 *
1424 * Returns: %TRUE to be sent successfully.
1425 */
1426gboolean
1427g_xim_protocol_send_with_list(GXimProtocol  *proto,
1428			      GXimOpcode     major_opcode,
1429			      guint8         minor_opcode,
1430			      GSList        *types,
1431			      GSList        *values)
1432{
1433	GXimProtocolPrivate *priv;
1434	GSList *lt, *lv;
1435	gpointer data, value;
1436	gboolean retval = FALSE;
1437	gsize osize, prev_size = 0;
1438	GError *error = NULL;
1439
1440	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
1441	g_return_val_if_fail (types != NULL, FALSE);
1442	g_return_val_if_fail (values != NULL, FALSE);
1443	g_return_val_if_fail (g_slist_length(types) == g_slist_length(values), FALSE);
1444	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
1445	g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, FALSE);
1446
1447	if (!g_xim_protocol_send_header(proto, major_opcode, minor_opcode,
1448					NULL, NULL))
1449		goto end;
1450
1451	for (lt = types, lv = values;
1452	     lt != NULL && lv != NULL;
1453	     lt = g_slist_next(lt), lv = g_slist_next(lv)) {
1454		value = lv->data;
1455		if (GPOINTER_TO_UINT (lt->data) == G_XIM_TYPE_AUTO_PADDING) {
1456			/* We have to deal with it here */
1457			value = GUINT_TO_POINTER (GPOINTER_TO_UINT (value) + prev_size);
1458		}
1459		prev_size = g_xim_protocol_send_format(proto, NULL, &error, 1, lt->data, value);
1460		if (GPOINTER_TO_UINT (lt->data) == G_XIM_TYPE_GSTRING) {
1461			/* XXX: hack to adjust the previous size... */
1462			prev_size -= 2;
1463		}
1464		if (error) {
1465			g_xim_messages_gerror(G_XIM_PROTOCOL_GET_IFACE (proto)->message, error);
1466			if (!G_XIM_GERROR_IS_RECOVERABLE (error))
1467				goto end;
1468			g_clear_error(&error);
1469		}
1470	}
1471
1472	if ((osize = g_xim_protocol_send_fixate_size(proto, NULL, &error)) == 0)
1473		goto end;
1474
1475	data = g_memory_output_stream_get_data(G_MEMORY_OUTPUT_STREAM (priv->base_send_ostream));
1476	if (g_queue_get_length(priv->sendq) > 0) {
1477		GXimProtocolQueueNode *node = g_queue_peek_tail(priv->sendq);
1478
1479		if (node->data == NULL) {
1480			/* if it's the queueing mode, do not send the packet */
1481			g_xim_messages_debug(G_XIM_PROTOCOL_GET_IFACE (proto)->message, "proto/event",
1482					     "Queued the packet for %s",
1483					     g_xim_protocol_name(major_opcode));
1484			node->data = g_new0(gchar, osize + 1);
1485			memcpy(node->data, data, osize);
1486			node->length = osize;
1487			retval = TRUE;
1488			goto end;
1489		}
1490	}
1491	retval = g_xim_protocol_send_packets(proto, data, osize);
1492
1493  end:
1494	if (error)
1495		g_error_free(error);
1496	retval &= g_xim_protocol_send_terminate(proto, NULL, NULL);
1497
1498	return retval;
1499}
1500
1501/**
1502 * g_xim_protocol_start_queue:
1503 * @proto: a #GXimProtocol.
1504 *
1505 * Prepares the queue to not send a request immediately. this function is
1506 * useful if you need to wait for any requests but want to send it after
1507 * the pending requests is done.
1508 *
1509 * Returns: %TRUE to be ready to queue.
1510 */
1511gboolean
1512g_xim_protocol_start_queue(GXimProtocol *proto)
1513{
1514	GXimProtocolPrivate *priv;
1515	GXimProtocolQueueNode *node;
1516
1517	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
1518	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
1519
1520	node = g_new0(GXimProtocolQueueNode, 1);
1521	G_XIM_CHECK_ALLOC (node, FALSE);
1522	g_queue_push_tail(priv->sendq, node);
1523
1524	return TRUE;
1525}
1526
1527/**
1528 * g_xim_protocol_end_queue:
1529 * @proto: a #GXimProtocol.
1530 *
1531 * Terminates the queue.
1532 *
1533 * Returns: the #GXimProtocolQueueNode which queued the request this time.
1534 */
1535GXimProtocolQueueNode *
1536g_xim_protocol_end_queue(GXimProtocol *proto)
1537{
1538	GXimProtocolPrivate *priv;
1539
1540	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), NULL);
1541	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), NULL);
1542
1543	return  g_queue_peek_tail(priv->sendq);
1544}
1545
1546/**
1547 * g_xim_protocol_cancel_queue:
1548 * @proto: a #GXimProtocol.
1549 *
1550 * Cancels queueing. Note that you have to invoke this function if you can't
1551 * proceed queueing process with any errors say. otherwise invalid node is kept
1552 * in the queue and you may get a trouble with it eveutnally.
1553 */
1554void
1555g_xim_protocol_cancel_queue(GXimProtocol *proto)
1556{
1557	GXimProtocolPrivate *priv;
1558
1559	g_return_if_fail (G_IS_XIM_PROTOCOL (proto));
1560	g_return_if_fail ((priv = g_xim_protocol_get_private(proto)));
1561	g_return_if_fail (g_xim_protocol_is_queued(proto));
1562
1563	g_free(g_queue_pop_tail(priv->sendq));
1564}
1565
1566/**
1567 * g_xim_protocol_is_queued:
1568 * @proto: a #GXimProtocol.
1569 *
1570 * Checks if @proto is in queueing process.
1571 *
1572 * Returns: %TRUE to be in the queueing process. otherwise %FALSE.
1573 */
1574gboolean
1575g_xim_protocol_is_queued(GXimProtocol *proto)
1576{
1577	GXimProtocolPrivate *priv;
1578	GXimProtocolQueueNode *node;
1579
1580	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
1581	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
1582
1583	node = g_queue_peek_tail(priv->sendq);
1584
1585	return node->data == NULL;
1586}
1587
1588/**
1589 * g_xim_protocol_get_queue_length:
1590 * @proto: a #GXimProtocol.
1591 *
1592 * Obtains how many requests are stored in the queue now.
1593 *
1594 * Returns: the number of requests in the queue.
1595 */
1596guint
1597g_xim_protocol_get_queue_length(GXimProtocol *proto)
1598{
1599	GXimProtocolPrivate *priv;
1600
1601	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), 0);
1602	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), 0);
1603
1604	return g_queue_get_length(priv->sendq);
1605}
1606
1607/**
1608 * g_xim_protocol_read_vformat:
1609 * @proto: a #GXimProtocol.
1610 * @stream: a #GDataInputStream which contains XIM protocol packets.
1611 * @cancellable: optional #GCancellable object, %NULL to ignore.
1612 * @error: a location to store error, or %NULL.
1613 * @n_params: the number of a set of parameters.
1614 * @args: a #va_list.
1615 *
1616 * Scans XIM protocol packets according to the set of parameters. if any,
1617 * objects generated against it is stored in the locations pointed to by
1618 * the pointer arguments. Each pointer argument must be of a type that is
1619 * appropriate for the value returned by the corresponding value type.
1620 * See #GXimValueType documentation which objects the value type is supposed
1621 * to be.
1622 *
1623 * Returns: %TRUE to be read successfully.
1624 */
1625gboolean
1626g_xim_protocol_read_vformat(GXimProtocol      *proto,
1627			    GDataInputStream  *stream,
1628			    GCancellable      *cancellable,
1629			    GError           **error,
1630			    guint              n_params,
1631			    va_list            args)
1632{
1633	GXimProtocolPrivate *priv;
1634	GXimProtocolIface *iface;
1635	guint i;
1636	GInputStream *istream;
1637	gboolean retval = TRUE;
1638	goffset base_pos;
1639	GXimProtocolMarker *marker_data = NULL;
1640	GQueue *markerq;
1641	GXimValueType vtype = G_XIM_TYPE_INVALID;
1642
1643	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
1644	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
1645	g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), FALSE);
1646	g_return_val_if_fail (error != NULL, FALSE);
1647
1648	iface = G_XIM_PROTOCOL_GET_IFACE (proto);
1649	markerq = g_queue_new();
1650	G_XIM_GERROR_CHECK_ALLOC (markerq, error, G_XIM_PROTOCOL_ERROR, FALSE);
1651	istream = g_filter_input_stream_get_base_stream(G_FILTER_INPUT_STREAM (stream));
1652	base_pos = g_seekable_tell(G_SEEKABLE (istream));
1653	for (i = 0; i < n_params; i++) {
1654		GXimValueType mtype;
1655		gpointer value;
1656		gsize padding, sz, size;
1657		goffset cur_pos = g_seekable_tell(G_S

Large files files are truncated, but you can click here to view the full file