PageRenderTime 146ms CodeModel.GetById 30ms app.highlight 97ms RepoModel.GetById 1ms app.codeStats 0ms

/libgxim/gximprotocol.c

https://bitbucket.org/tagoh/libgxim
C | 3564 lines | 2825 code | 292 blank | 447 comment | 462 complexity | 393a595dccd62d79061f56801c644679 MD5 | raw file
Possible License(s): LGPL-2.1
   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_SEEKABLE (istream));
1658		GType gtype G_GNUC_UNUSED;
1659		GXimAttr *attr;
1660		GdkNativeWindow nw;
1661
1662		vtype = va_arg(args, GXimValueType);
1663		marker_data = g_xim_protocol_find_marker(markerq, vtype);
1664		switch (vtype) {
1665		    case G_XIM_TYPE_PADDING:
1666			    padding = va_arg(args, unsigned int);
1667		    padding:
1668			    if (marker_data) {
1669				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1670						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1671						"Invalid the length-of-data marker for value type %s",
1672						g_xim_value_type_name(vtype));
1673				    goto end;
1674			    }
1675			    if (!g_seekable_seek(G_SEEKABLE (istream), padding,
1676						 G_SEEK_CUR, cancellable, error)) {
1677				    goto end;
1678			    }
1679			    break;
1680		    case G_XIM_TYPE_AUTO_PADDING:
1681			    if (marker_data) {
1682				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1683						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1684						"Invalid the length-of-data marker for value type %s",
1685						g_xim_value_type_name(vtype));
1686				    goto end;
1687			    }
1688			    sz = va_arg(args, unsigned int);
1689			    padding = g_xim_protocol_n_pad4(cur_pos - base_pos + sz);
1690			    goto padding;
1691		    case G_XIM_TYPE_MARKER_N_BYTES_1:
1692			    if (marker_data) {
1693				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1694						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1695						"Invalid the length-of-data marker for value type %s",
1696						g_xim_value_type_name(vtype));
1697				    goto end;
1698			    }
1699			    mtype = va_arg(args, GXimValueType);
1700			    marker_data = g_new0(GXimProtocolMarker, 1);
1701			    G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
1702						      G_XIM_PROTOCOL_ERROR, FALSE);
1703
1704			    marker_data->type = G_XIM_TYPE_BYTE;
1705			    marker_data->where = mtype;
1706			    marker_data->value.b = g_data_input_stream_read_byte(stream, cancellable, error);
1707			    g_queue_push_tail(markerq, marker_data);
1708			    marker_data = NULL;
1709			    break;
1710		    case G_XIM_TYPE_MARKER_N_BYTES_2:
1711			    if (marker_data) {
1712				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1713						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1714						"Invalid the length-of-data marker for value type %s",
1715						g_xim_value_type_name(vtype));
1716				    goto end;
1717			    }
1718			    mtype = va_arg(args, GXimValueType);
1719			    marker_data = g_new0(GXimProtocolMarker, 1);
1720			    G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
1721						      G_XIM_PROTOCOL_ERROR, FALSE);
1722
1723			    marker_data->type = G_XIM_TYPE_WORD;
1724			    marker_data->where = mtype;
1725			    marker_data->value.w = g_data_input_stream_read_uint16(stream, cancellable, error);
1726			    g_queue_push_tail(markerq, marker_data);
1727			    marker_data = NULL;
1728			    break;
1729		    case G_XIM_TYPE_MARKER_N_BYTES_4:
1730			    if (marker_data) {
1731				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1732						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1733						"Invalid the length-of-data marker for value type %s",
1734						g_xim_value_type_name(vtype));
1735				    goto end;
1736			    }
1737			    mtype = va_arg(args, GXimValueType);
1738			    marker_data = g_new0(GXimProtocolMarker, 1);
1739			    G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
1740						      G_XIM_PROTOCOL_ERROR, FALSE);
1741
1742			    marker_data->type = G_XIM_TYPE_LONG;
1743			    marker_data->where = mtype;
1744			    marker_data->value.l = g_data_input_stream_read_uint32(stream, cancellable, error);
1745			    g_queue_push_tail(markerq, marker_data);
1746			    marker_data = NULL;
1747			    break;
1748		    case G_XIM_TYPE_MARKER_N_ITEMS_2:
1749			    if (marker_data) {
1750				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1751						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1752						"Invalid the length-of-data marker for value type %s",
1753						g_xim_value_type_name(vtype));
1754				    goto end;
1755			    }
1756			    mtype = va_arg(args, GXimValueType);
1757			    marker_data = g_new0(GXimProtocolMarker, 1);
1758			    G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
1759						      G_XIM_PROTOCOL_ERROR, FALSE);
1760
1761			    marker_data->type = vtype;
1762			    marker_data->where = mtype;
1763			    marker_data->value.w = g_data_input_stream_read_uint16(stream, cancellable, error);
1764			    g_queue_push_tail(markerq, marker_data);
1765			    marker_data = NULL;
1766			    break;
1767		    case G_XIM_TYPE_STR:
1768			    value = va_arg(args, gpointer);
1769			    if (value == NULL) {
1770				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1771						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1772						"value type %s requires an argument to store the result.",
1773						g_xim_value_type_name(vtype));
1774				    goto end;
1775			    }
1776			    *((GXimStr **)value) = g_xim_str_get_from_stream(stream, NULL, error);
1777			    break;
1778		    case G_XIM_TYPE_GSTRING:
1779			    value = va_arg(args, gpointer);
1780			    if (value == NULL) {
1781				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1782						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1783						"value type %s requires an argument to store the result.",
1784						g_xim_value_type_name(vtype));
1785				    goto end;
1786			    }
1787			    /* we don't want to include the length of characters for markers */
1788			    cur_pos += 2;
1789			    *((GString **)value) = g_xim_gstring_get_from_stream(stream, NULL, error);
1790			    break;
1791		    case G_XIM_TYPE_PREEDIT_CARET:
1792			    value = va_arg(args, gpointer);
1793			    if (value == NULL) {
1794				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1795						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1796						"value type %s requires an argument to store the result.",
1797						g_xim_value_type_name(vtype));
1798				    goto end;
1799			    }
1800			    *((GXimPreeditCaret **)value) = g_xim_preedit_caret_get_from_stream(proto, stream, cancellable, error);
1801			    break;
1802		    case G_XIM_TYPE_PREEDIT_DRAW:
1803			    value = va_arg(args, gpointer);
1804			    if (value == NULL) {
1805				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1806						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1807						"value type %s requires an argument to store the result.",
1808						g_xim_value_type_name(vtype));
1809				    goto end;
1810			    }
1811			    *((GXimPreeditDraw **)value) = g_xim_preedit_draw_get_from_stream(proto, stream, cancellable, error);
1812			    break;
1813		    case G_XIM_TYPE_GDKEVENT:
1814			    value = va_arg(args, gpointer);
1815			    if (value == NULL) {
1816				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1817						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1818						"value type %s requires an argument to store the result.",
1819						g_xim_value_type_name(vtype));
1820				    goto end;
1821			    }
1822			    *((GdkEvent **)value) = g_xim_gdkevent_get_from_stream(proto, stream, cancellable, error);
1823			    break;
1824		    case G_XIM_TYPE_XIMTEXT:
1825			    value = va_arg(args, gpointer);
1826			    if (value == NULL) {
1827				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1828						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1829						"value type %s requires an argument to store the result.",
1830						g_xim_value_type_name(vtype));
1831				    goto end;
1832			    }
1833			    *((GXimText **)value) = g_xim_text_get_from_stream(proto, stream, cancellable, error);
1834			    break;
1835		    case G_XIM_TYPE_HOTKEY_TRIGGER:
1836			    value = va_arg(args, gpointer);
1837			    if (value == NULL) {
1838				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1839						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1840						"value type %s requires an argument to store the result.",
1841						g_xim_value_type_name(vtype));
1842				    goto end;
1843			    }
1844			    *((GXimHotkeyTrigger **)value) = g_xim_hotkey_trigger_get_from_stream(proto, stream, cancellable, error);
1845			    break;
1846		    case G_XIM_TYPE_PIXMAP:
1847			    value = va_arg(args, gpointer);
1848			    if (value == NULL) {
1849				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1850						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1851						"value type %s requires an argument to store the result.",
1852						g_xim_value_type_name(vtype));
1853				    goto end;
1854			    }
1855			    nw = g_data_input_stream_read_uint32(stream, cancellable, error);
1856			    *((GdkPixmap **)value) = g_xim_get_pixmap(g_xim_transport_get_display(G_XIM_TRANSPORT (proto)),
1857								      nw);
1858			    if (*((GdkPixmap **)value) == NULL) {
1859				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1860						G_XIM_PROTOCOL_ERROR_NO_DATA | G_XIM_NOTICE_ERROR,
1861						"Unable to get a valid pixmap for %p",
1862						G_XIM_NATIVE_WINDOW_TO_POINTER (nw));
1863				    goto end;
1864			    }
1865			    break;
1866		    case G_XIM_TYPE_STATUS_DRAW:
1867			    value = va_arg(args, gpointer);
1868			    if (value == NULL) {
1869				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1870						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1871						"value type %s requires an argument to store the result.",
1872						g_xim_value_type_name(vtype));
1873				    goto end;
1874			    }
1875			    *((GXimStatusDraw **)value) = g_xim_status_draw_get_from_stream(proto, stream, cancellable, error);
1876			    break;
1877		    case G_XIM_TYPE_LIST_OF_IMATTR:
1878			    gtype = G_TYPE_XIM_IM_ATTR;
1879			    goto process_attr;
1880		    case G_XIM_TYPE_LIST_OF_ICATTR:
1881			    gtype = G_TYPE_XIM_IC_ATTR;
1882		    process_attr:
1883			    value = va_arg(args, gpointer);
1884			    if (value == NULL) {
1885				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1886						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1887						"value type %s requires an argument to store the result.",
1888						g_xim_value_type_name(vtype));
1889				    goto end;
1890			    }
1891			    if (marker_data == NULL) {
1892				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1893						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1894						"No the markers for value type %s to determine how much the number of chunk available",
1895						g_xim_value_type_name(vtype));
1896				    goto end;
1897			    }
1898			    if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
1899				    size = G_XIM_GET_MARKER_DATA (marker_data);
1900				    g_free(marker_data);
1901				    marker_data = NULL;
1902
1903				    for (sz = 0; sz < size; sz++) {
1904					    GXimRawAttr *a;
1905
1906					    a = g_xim_raw_attr_get_from_stream(proto, stream, cancellable, error);
1907					    if (*error) {
1908						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1909							    g_prefix_error(error, "Error while processing a value type %s: ",
1910									   g_xim_value_type_name(vtype));
1911							    g_xim_messages_gerror(iface->message, *error);
1912							    g_clear_error(error);
1913						    } else {
1914							    goto end;
1915						    }
1916					    }
1917					    *((GSList **)value) = g_slist_append(*((GSList **)value), a);
1918				    }
1919			    } else {
1920				    size = G_XIM_GET_MARKER_DATA (marker_data);
1921				    g_free(marker_data);
1922				    marker_data = NULL;
1923
1924				    while (size > 0) {
1925					    goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
1926					    GXimRawAttr *a;
1927
1928					    a = g_xim_raw_attr_get_from_stream(proto, stream, cancellable, error);
1929					    if (*error) {
1930						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1931							    g_prefix_error(error, "Error while processing a value type %s: ",
1932									   g_xim_value_type_name(vtype));
1933							    g_xim_messages_gerror(iface->message, *error);
1934							    g_clear_error(error);
1935						    } else {
1936							    goto end;
1937						    }
1938					    }
1939					    pos = g_seekable_tell(G_SEEKABLE (istream));
1940					    if ((pos - cur_pos) > size) {
1941						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1942								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
1943								"Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
1944								g_xim_value_type_name(vtype), size, pos - cur_pos);
1945						    goto end;
1946					    }
1947					    *((GSList **)value) = g_slist_append(*((GSList **)value), a);
1948					    size -= (pos - cur_pos);
1949				    }
1950			    }
1951			    break;
1952		    case G_XIM_TYPE_LIST_OF_IMATTRIBUTE:
1953			    if (!G_IS_XIM_IM_ATTR (G_XIM_CONNECTION (proto)->imattr)) {
1954				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1955						G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
1956						"Protocol order might be invalid. XIM_OPEN must be brought up first.");
1957				    goto end;
1958			    } else {
1959				    attr = G_XIM_ATTR (G_XIM_CONNECTION (proto)->imattr);
1960			    }
1961			    goto process_attribute;
1962		    case G_XIM_TYPE_LIST_OF_ICATTRIBUTE:
1963			    if (!G_IS_XIM_IC_ATTR (G_XIM_CONNECTION (proto)->default_icattr)) {
1964				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1965						G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
1966						"Protocol order might be invalid. XIM_OPEN must be brought up first.");
1967				    goto end;
1968			    } else {
1969				    attr = G_XIM_ATTR (G_XIM_CONNECTION (proto)->default_icattr);
1970			    }
1971		    process_attribute:
1972			    value = va_arg(args, gpointer);
1973			    if (value == NULL) {
1974				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1975						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1976						"value type %s requires an argument to store the result.",
1977						g_xim_value_type_name(vtype));
1978				    goto end;
1979			    }
1980			    if (marker_data == NULL) {
1981				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
1982						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
1983						"No the markers for value type %s to determine how much the number of chunk available",
1984						g_xim_value_type_name(vtype));
1985				    goto end;
1986			    }
1987			    if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
1988				    size = G_XIM_GET_MARKER_DATA (marker_data);
1989				    g_free(marker_data);
1990				    marker_data = NULL;
1991
1992				    for (sz = 0; sz < size; sz++) {
1993					    GXimAttribute *a;
1994
1995					    a = g_xim_attr_get_attribute_from_stream(proto, attr, stream, cancellable, error);
1996					    if (*error) {
1997						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
1998							    g_prefix_error(error, "Error while processing a value type %s: ",
1999									   g_xim_value_type_name(vtype));
2000							    g_xim_messages_gerror(iface->message, *error);
2001							    g_clear_error(error);
2002						    } else {
2003							    goto end;
2004						    }
2005					    }
2006					    *((GSList **)value) = g_slist_append(*((GSList **)value), a);
2007				    }
2008			    } else {
2009				    size = G_XIM_GET_MARKER_DATA (marker_data);
2010				    g_free(marker_data);
2011				    marker_data = NULL;
2012
2013				    while (size > 0) {
2014					    goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
2015					    GXimAttribute *a;
2016
2017					    a = g_xim_attr_get_attribute_from_stream(proto, attr, stream, cancellable, error);
2018					    if (*error) {
2019						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2020							    g_prefix_error(error, "Error while processing a value type %s: ",
2021									   g_xim_value_type_name(vtype));
2022							    g_xim_messages_gerror(iface->message, *error);
2023							    g_clear_error(error);
2024						    } else {
2025							    goto end;
2026						    }
2027					    }
2028					    pos = g_seekable_tell(G_SEEKABLE (istream));
2029					    if ((pos - cur_pos) > size) {
2030						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2031								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2032								"Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
2033								g_xim_value_type_name(vtype), size, pos - cur_pos);
2034						    goto end;
2035					    }
2036					    *((GSList **)value) = g_slist_append(*((GSList **)value), a);
2037					    size -= (pos - cur_pos);
2038				    }
2039			    }
2040			    break;
2041		    case G_XIM_TYPE_LIST_OF_EXT:
2042			    value = va_arg(args, gpointer);
2043			    if (value == NULL) {
2044				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2045						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2046						"value type %s requires an argument to store the result.",
2047						g_xim_value_type_name(vtype));
2048				    goto end;
2049			    }
2050			    if (marker_data == NULL) {
2051				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2052						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2053						"No the markers for value type %s to determine how much the number of chunk available",
2054						g_xim_value_type_name(vtype));
2055				    goto end;
2056			    }
2057			    if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
2058				    size = G_XIM_GET_MARKER_DATA (marker_data);
2059				    g_free(marker_data);
2060				    marker_data = NULL;
2061
2062				    for (sz = 0; sz < size; sz++) {
2063					    GXimExt *e;
2064
2065					    e = g_xim_ext_get_from_stream(proto, stream, NULL, error);
2066					    if (*error) {
2067						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2068							    g_prefix_error(error, "Error while processing a value type %s: ",
2069									   g_xim_value_type_name(vtype));
2070							    g_xim_messages_gerror(iface->message, *error);
2071							    g_clear_error(error);
2072						    } else {
2073							    goto end;
2074						    }
2075					    }
2076					    *((GSList **)value) = g_slist_append(*((GSList **)value), e);
2077				    }
2078			    } else {
2079				    size = G_XIM_GET_MARKER_DATA (marker_data);
2080				    g_free(marker_data);
2081				    marker_data = NULL;
2082
2083				    while (size > 0) {
2084					    GXimExt *e;
2085					    goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
2086
2087					    e = g_xim_ext_get_from_stream(proto, stream, NULL, error);
2088					    if (*error) {
2089						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2090							    g_prefix_error(error, "Error while processing a value type %s: ",
2091									   g_xim_value_type_name(vtype));
2092							    g_xim_messages_gerror(iface->message, *error);
2093							    g_clear_error(error);
2094						    } else {
2095							    goto end;
2096						    }
2097					    }
2098					    pos = g_seekable_tell(G_SEEKABLE (istream));
2099					    if ((pos - cur_pos) > size) {
2100						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2101								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2102								"Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
2103								g_xim_value_type_name(vtype), size, pos - cur_pos);
2104						    goto end;
2105					    }
2106					    *((GSList **)value) = g_slist_append(*((GSList **)value), e);
2107					    size -= (pos - cur_pos);
2108				    }
2109			    }
2110			    break;
2111		    case G_XIM_TYPE_LIST_OF_STRING:
2112			    value = va_arg(args, gpointer);
2113			    if (value == NULL) {
2114				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2115						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2116						"value type %s requires an argument to store the result.",
2117						g_xim_value_type_name(vtype));
2118				    goto end;
2119			    }
2120			    if (marker_data == NULL) {
2121				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2122						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2123						"No the markers for value type %s to determine how much the number of chunk available",
2124						g_xim_value_type_name(vtype));
2125				    goto end;
2126			    }
2127			    if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
2128				    size = G_XIM_GET_MARKER_DATA (marker_data);
2129				    g_free(marker_data);
2130				    marker_data = NULL;
2131
2132				    for (sz = 0; sz < size; sz++) {
2133					    GXimString *s;
2134
2135					    s = g_xim_string_get_from_stream(stream, NULL, error);
2136					    if (*error) {
2137						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2138							    g_prefix_error(error, "Error while processing a value type %s: ",
2139									   g_xim_value_type_name(vtype));
2140							    g_xim_messages_gerror(iface->message, *error);
2141							    g_clear_error(error);
2142						    } else {
2143							    goto end;
2144						    }
2145					    }
2146					    *((GSList **)value) = g_slist_append(*((GSList **)value), s);
2147				    }
2148			    } else {
2149				    size = G_XIM_GET_MARKER_DATA (marker_data);
2150				    g_free(marker_data);
2151				    marker_data = NULL;
2152
2153				    while (size > 0) {
2154					    goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
2155					    GXimString *s;
2156
2157					    s = g_xim_string_get_from_stream(stream, NULL, error);
2158					    if (*error) {
2159						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2160							    g_prefix_error(error, "Error while processing a value type %s: ",
2161									   g_xim_value_type_name(vtype));
2162							    g_xim_messages_gerror(iface->message, *error);
2163							    g_clear_error(error);
2164						    } else {
2165							    goto end;
2166						    }
2167					    }
2168					    pos = g_seekable_tell(G_SEEKABLE (istream));
2169					    if ((pos - cur_pos) > size) {
2170						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2171								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2172								"Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
2173								g_xim_value_type_name(vtype), size, pos - cur_pos);
2174						    goto end;
2175					    }
2176					    *((GSList **)value) = g_slist_append(*((GSList **)value), s);
2177					    size -= (pos - cur_pos);
2178				    }
2179			    }
2180			    break;
2181		    case G_XIM_TYPE_LIST_OF_STR:
2182			    value = va_arg(args, gpointer);
2183			    if (value == NULL) {
2184				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2185						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2186						"value type %s requires an argument to store the result.",
2187						g_xim_value_type_name(vtype));
2188				    goto end;
2189			    }
2190			    if (marker_data == NULL) {
2191				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2192						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2193						"No the markers for value type %s to determine how much the number of chunk available",
2194						g_xim_value_type_name(vtype));
2195				    goto end;
2196			    }
2197			    if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
2198				    size = G_XIM_GET_MARKER_DATA (marker_data);
2199				    g_free(marker_data);
2200				    marker_data = NULL;
2201
2202				    for (sz = 0; sz < size; sz++) {
2203					    GXimStr *s;
2204
2205					    s = g_xim_str_get_from_stream(stream, NULL, error);
2206					    if (*error) {
2207						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2208							    g_prefix_error(error, "Error while processing a value type %s: ",
2209									   g_xim_value_type_name(vtype));
2210							    g_xim_messages_gerror(iface->message, *error);
2211							    g_clear_error(error);
2212						    } else {
2213							    goto end;
2214						    }
2215					    }
2216					    *((GSList **)value) = g_slist_append(*((GSList **)value), s);
2217				    }
2218			    } else {
2219				    size = G_XIM_GET_MARKER_DATA (marker_data);
2220				    g_free(marker_data);
2221				    marker_data = NULL;
2222
2223				    while (size > 0) {
2224					    goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
2225					    GXimStr *s;
2226
2227					    s = g_xim_str_get_from_stream(stream, NULL, error);
2228					    if (*error) {
2229						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2230							    g_prefix_error(error, "Error while processing a value type %s: ",
2231									   g_xim_value_type_name(vtype));
2232							    g_xim_messages_gerror(iface->message, *error);
2233							    g_clear_error(error);
2234						    } else {
2235							    goto end;
2236						    }
2237					    }
2238					    pos = g_seekable_tell(G_SEEKABLE (istream));
2239					    if ((pos - cur_pos) > size) {
2240						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2241								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2242								"Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
2243								g_xim_value_type_name(vtype), size, pos - cur_pos);
2244						    goto end;
2245					    }
2246					    *((GSList **)value) = g_slist_append(*((GSList **)value), s);
2247					    size -= (pos - cur_pos);
2248				    }
2249			    }
2250			    break;
2251		    case G_XIM_TYPE_LIST_OF_ENCODINGINFO:
2252			    value = va_arg(args, gpointer);
2253			    if (value == NULL) {
2254				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2255						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2256						"value type %s requires an argument to store the result.",
2257						g_xim_value_type_name(vtype));
2258				    goto end;
2259			    }
2260			    if (marker_data == NULL) {
2261				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2262						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2263						"No the markers for value type %s to determine how much the number of chunk available",
2264						g_xim_value_type_name(vtype));
2265				    goto end;
2266			    }
2267			    if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
2268				    size = G_XIM_GET_MARKER_DATA (marker_data);
2269				    g_free(marker_data);
2270				    marker_data = NULL;
2271
2272				    for (sz = 0; sz < size; sz++) {
2273					    GXimEncodingInfo *e;
2274
2275					    e = g_xim_encodinginfo_get_from_stream(stream, NULL, error);
2276					    if (*error) {
2277						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2278							    g_prefix_error(error, "Error while processing a value type %s: ",
2279									   g_xim_value_type_name(vtype));
2280							    g_xim_messages_gerror(iface->message, *error);
2281							    g_clear_error(error);
2282						    } else {
2283							    goto end;
2284						    }
2285					    }
2286					    *((GSList **)value) = g_slist_append(*((GSList **)value), e);
2287				    }
2288			    } else {
2289				    size = G_XIM_GET_MARKER_DATA (marker_data);
2290				    g_free(marker_data);
2291				    marker_data = NULL;
2292
2293				    while (size > 0) {
2294					    goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
2295					    GXimEncodingInfo *e;
2296
2297					    e = g_xim_encodinginfo_get_from_stream(stream, NULL, error);
2298					    if (*error) {
2299						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2300							    g_prefix_error(error, "Error while processing a value type %s: ",
2301									   g_xim_value_type_name(vtype));
2302							    g_xim_messages_gerror(iface->message, *error);
2303							    g_clear_error(error);
2304						    } else {
2305							    goto end;
2306						    }
2307					    }
2308					    pos = g_seekable_tell(G_SEEKABLE (istream));
2309					    if ((pos - cur_pos) > size) {
2310						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2311								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2312								"Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
2313								g_xim_value_type_name(vtype), size, pos - cur_pos);
2314						    goto end;
2315					    }
2316					    *((GSList **)value) = g_slist_append(*((GSList **)value), e);
2317					    size -= (pos - cur_pos);
2318				    }
2319			    }
2320			    break;
2321		    case G_XIM_TYPE_LIST_OF_BYTE:
2322			    value = va_arg(args, gpointer);
2323			    if (value == NULL) {
2324				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2325						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2326						"value type %s requires an argument to store the result.",
2327						g_xim_value_type_name(vtype));
2328				    goto end;
2329			    }
2330			    if (marker_data == NULL) {
2331				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2332						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2333						"No the markers for value type %s to determine how much the number of chunk available",
2334						g_xim_value_type_name(vtype));
2335				    goto end;
2336			    }
2337			    /* assuming the length of item == the length of bytes */
2338			    size = G_XIM_GET_MARKER_DATA (marker_data);
2339			    g_free(marker_data);
2340			    marker_data = NULL;
2341
2342			    *((GString **)value) = g_string_sized_new(size);
2343			    for (sz = 0; sz < size; sz++) {
2344				    gchar c;
2345
2346				    c = g_data_input_stream_read_byte(stream, cancellable, error);
2347				    if (*error)
2348					    goto end;
2349
2350				    g_string_append_c(*((GString **)value), c);
2351			    }
2352			    break;
2353		    case G_XIM_TYPE_LIST_OF_CARD16:
2354			    value = va_arg(args, gpointer);
2355			    if (value == NULL) {
2356				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2357						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2358						"value type %s requires an argument to store the result.",
2359						g_xim_value_type_name(vtype));
2360				    goto end;
2361			    }
2362			    if (marker_data == NULL) {
2363				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2364						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2365						"No the markers for value type %s to determine how much the number of chunk available",
2366						g_xim_value_type_name(vtype));
2367				    goto end;
2368			    }
2369			    if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
2370				    size = G_XIM_GET_MARKER_DATA (marker_data);
2371				    g_free(marker_data);
2372				    marker_data = NULL;
2373
2374				    for (sz = 0; sz < size; sz++) {
2375					    guint16 n;
2376
2377					    n = g_data_input_stream_read_uint16(stream, NULL, error);
2378					    if (*error)
2379						    goto end;
2380					    *((GSList **)value) = g_slist_append(*((GSList **)value), GUINT_TO_POINTER ((guint)n));
2381				    }
2382			    } else {
2383				    size = G_XIM_GET_MARKER_DATA (marker_data);
2384				    g_free(marker_data);
2385				    marker_data = NULL;
2386
2387				    if ((size % 2) != 0) {
2388					    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2389							G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2390							"The length of bytes for value type %s has to be an even number: %" G_GSIZE_FORMAT,
2391							g_xim_value_type_name(vtype), size);
2392					    goto end;
2393				    }
2394				    while (size > 0) {
2395					    guint16 n;
2396
2397					    n = g_data_input_stream_read_uint16(stream, NULL, error);
2398					    if (*error)
2399						    goto end;
2400					    *((GSList **)value) = g_slist_append(*((GSList **)value), GUINT_TO_POINTER ((guint)n));
2401					    size -= 2;
2402				    }
2403			    }
2404			    break;
2405		    case G_XIM_TYPE_LIST_OF_HOTKEY_TRIGGER:
2406			    value = va_arg(args, gpointer);
2407			    if (value == NULL) {
2408				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2409						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2410						"value type %s requires an argument to store the result.",
2411						g_xim_value_type_name(vtype));
2412				    goto end;
2413			    }
2414			    if (marker_data == NULL) {
2415				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2416						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2417						"No the markers for value type %s to determine how much the number of chunk available",
2418						g_xim_value_type_name(vtype));
2419				    goto end;
2420			    }
2421			    if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
2422				    size = G_XIM_GET_MARKER_DATA (marker_data);
2423				    g_free(marker_data);
2424				    marker_data = NULL;
2425
2426				    for (sz = 0; sz < size; sz++) {
2427					    GXimHotkeyTrigger *v;
2428
2429					    v = g_xim_hotkey_trigger_get_from_stream(proto, stream, cancellable, error);
2430					    if (*error) {
2431						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2432							    g_prefix_error(error, "Error while processing a value type %s: ",
2433									   g_xim_value_type_name(vtype));
2434							    g_xim_messages_gerror(iface->message, *error);
2435							    g_clear_error(error);
2436						    } else {
2437							    goto end;
2438						    }
2439					    }
2440					    *((GSList **)value) = g_slist_append(*((GSList **)value), v);
2441				    }
2442			    } else {
2443				    size = G_XIM_GET_MARKER_DATA (marker_data);
2444				    g_free(marker_data);
2445				    marker_data = NULL;
2446
2447				    while (size > 0) {
2448					    goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
2449					    GXimHotkeyTrigger *v;
2450
2451					    v = g_xim_hotkey_trigger_get_from_stream(proto, stream, cancellable, error);
2452					    if (*error) {
2453						    if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
2454							    g_prefix_error(error, "Error while processing a value type %s: ",
2455									   g_xim_value_type_name(vtype));
2456							    g_xim_messages_gerror(iface->message, *error);
2457							    g_clear_error(error);
2458						    } else {
2459							    goto end;
2460						    }
2461					    }
2462					    pos = g_seekable_tell(G_SEEKABLE (istream));
2463					    if ((pos - cur_pos) > size) {
2464						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2465								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2466								"Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
2467								g_xim_value_type_name(vtype), size, pos - cur_pos);
2468						    goto end;
2469					    }
2470					    *((GSList **)value) = g_slist_append(*((GSList **)value), v);
2471					    size -= (pos - cur_pos);
2472				    }
2473			    }
2474			    break;
2475		    case G_XIM_TYPE_SEPARATOR:
2476			    value = va_arg(args, gpointer);
2477			    if (value == NULL) {
2478				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2479						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2480						"value type %s requires an argument to store the result.",
2481						g_xim_value_type_name(vtype));
2482				    goto end;
2483			    }
2484			    *((GXimSepNestedList **)value) = g_xim_sep_nested_list_new();
2485			    break;
2486		    case G_XIM_TYPE_BYTE:
2487			    value = va_arg(args, gpointer);
2488			    if (value == NULL) {
2489				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2490						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2491						"value type %s requires an argument to store the result.",
2492						g_xim_value_type_name(vtype));
2493				    goto end;
2494			    }
2495			    *((gchar *)value) = g_data_input_stream_read_byte(stream, cancellable, error);
2496			    break;
2497		    case G_XIM_TYPE_WORD:
2498			    value = va_arg(args, gpointer);
2499			    if (value == NULL) {
2500				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2501						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2502						"value type %s requires an argument to store the result.",
2503						g_xim_value_type_name(vtype));
2504				    goto end;
2505			    }
2506			    *((guint16 *)value) = g_data_input_stream_read_uint16(stream, cancellable, error);
2507			    break;
2508		    case G_XIM_TYPE_LONG:
2509			    value = va_arg(args, gpointer);
2510			    if (value == NULL) {
2511				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2512						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2513						"value type %s requires an argument to store the result.",
2514						g_xim_value_type_name(vtype));
2515				    goto end;
2516			    }
2517			    *((guint32 *)value) = g_data_input_stream_read_uint32(stream, cancellable, error);
2518			    break;
2519		    case G_XIM_TYPE_CHAR:
2520			    value = va_arg(args, gpointer);
2521			    if (value == NULL) {
2522				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2523						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2524						"value type %s requires an argument to store the result.",
2525						g_xim_value_type_name(vtype));
2526				    goto end;
2527			    }
2528			    if (marker_data == NULL) {
2529				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2530						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2531						"No the markers for value type %s to determine how much the number of chunk available",
2532						g_xim_value_type_name(vtype));
2533				    goto end;
2534			    }
2535			    /* assuming the length of item == the length of bytes */
2536			    size = G_XIM_GET_MARKER_DATA (marker_data);
2537			    g_free(marker_data);
2538			    marker_data = NULL;
2539
2540			    *((gchar **)value) = g_new0(gchar, size + 1);
2541			    for (sz = 0; sz < size; sz++) {
2542				    gchar c;
2543
2544				    c = g_data_input_stream_read_byte(stream, cancellable, error);
2545				    if (*error)
2546					    goto end;
2547
2548				    (*((gchar **)value))[sz] = c;
2549			    }
2550			    (*((gchar **)value))[sz] = 0;
2551			    break;
2552		    case G_XIM_TYPE_WINDOW:
2553			    value = va_arg(args, gpointer);
2554			    if (value == NULL) {
2555				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2556						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2557						"value type %s requires an argument to store the result.",
2558						g_xim_value_type_name(vtype));
2559				    goto end;
2560			    }
2561			    nw = g_data_input_stream_read_uint32(stream, cancellable, error);
2562			    if (nw) {
2563				    *((GdkWindow **)value) = g_xim_get_window(g_xim_transport_get_display(G_XIM_TRANSPORT (proto)),
2564									      nw);
2565				    if (*((GdkWindow **)value) == NULL) {
2566					    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2567							G_XIM_PROTOCOL_ERROR_NO_DATA | G_XIM_NOTICE_ERROR,
2568							"Unable to get a valid window for %p",
2569							G_XIM_NATIVE_WINDOW_TO_POINTER (nw));
2570					    goto end;
2571				    }
2572			    } else {
2573				    /* Window is possible to be set 0 */
2574				    *((GdkWindow **)value) = NULL;
2575			    }
2576			    break;
2577		    case G_XIM_TYPE_XIMSTYLES:
2578			    value = va_arg(args, gpointer);
2579			    if (value == NULL) {
2580				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2581						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2582						"value type %s requires an argument to store the result.",
2583						g_xim_value_type_name(vtype));
2584				    goto end;
2585			    }
2586			    *((GXimStyles **)value) = g_xim_styles_get_from_stream(proto, stream, NULL, error);
2587			    break;
2588		    case G_XIM_TYPE_XRECTANGLE:
2589			    value = va_arg(args, gpointer);
2590			    if (value == NULL) {
2591				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2592						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2593						"value type %s requires an argument to store the result.",
2594						g_xim_value_type_name(vtype));
2595				    goto end;
2596			    }
2597			    *((GXimRectangle **)value) = g_xim_rectangle_get_from_stream(proto, stream, NULL, error);
2598			    break;
2599		    case G_XIM_TYPE_XPOINT:
2600			    value = va_arg(args, gpointer);
2601			    if (value == NULL) {
2602				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2603						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2604						"value type %s requires an argument to store the result.",
2605						g_xim_value_type_name(vtype));
2606				    goto end;
2607			    }
2608			    *((GXimPoint **)value) = g_xim_point_get_from_stream(proto, stream, NULL, error);
2609			    break;
2610		    case G_XIM_TYPE_XFONTSET:
2611			    value = va_arg(args, gpointer);
2612			    if (value == NULL) {
2613				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2614						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2615						"value type %s requires an argument to store the result.",
2616						g_xim_value_type_name(vtype));
2617				    goto end;
2618			    }
2619			    *((GXimFontSet **)value) = g_xim_fontset_get_from_stream(proto, stream, NULL, error);
2620			    break;
2621		    case G_XIM_TYPE_HOTKEYTRIGGERS:
2622		    case G_XIM_TYPE_HOTKEYSTATE:
2623		    case G_XIM_TYPE_STRINGCONVERSION:
2624		    case G_XIM_TYPE_PREEDITSTATE:
2625		    case G_XIM_TYPE_RESETSTATE:
2626			    goto nonsupported;
2627		    case G_XIM_TYPE_NESTEDLIST:
2628			    value = va_arg(args, gpointer);
2629			    if (value == NULL) {
2630				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2631						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2632						"value type %s requires an argument to store the result.",
2633						g_xim_value_type_name(vtype));
2634				    goto end;
2635			    }
2636			    if (marker_data == NULL) {
2637				    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2638						G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
2639						"No the markers for value type %s to determine how much the number of chunk available",
2640						g_xim_value_type_name(vtype));
2641				    goto end;
2642			    }
2643			    if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
2644				    GXimNestedList *list;
2645				    GXimConnection *conn;
2646
2647				    size = G_XIM_GET_MARKER_DATA (marker_data);
2648				    g_free(marker_data);
2649				    marker_data = NULL;
2650
2651				    conn = G_XIM_CONNECTION (proto);
2652				    /* XXX: NESTEDLIST is only used for IC attributes? */
2653				    list = g_xim_nested_list_new(G_XIM_ATTR (conn->default_icattr), size);
2654				    for (sz = 0; sz < size; sz++) {
2655					    gint16 attr_id;
2656					    guint16 n;
2657					    gpointer v;
2658					    GType gtype;
2659					    GXimValueType ntype;
2660					    gchar *name;
2661
2662					    if (!g_xim_protocol_read_format(proto, stream, cancellable, error, 1,
2663									    G_XIM_TYPE_WORD, &attr_id)) {
2664					      fail_nested_list_1:
2665						    g_prefix_error(error, "Error while processing arg %" G_GSIZE_FORMAT " of %" G_GSIZE_FORMAT " in a value type %s: ",
2666								   sz, size, g_xim_value_type_name(vtype));
2667						    g_xim_messages_gerror(iface->message, *error);
2668						    g_clear_error(error);
2669						    n = g_data_input_stream_read_uint16(stream, cancellable, error);
2670						    g_seekable_seek(G_SEEKABLE (istream), n,
2671								    G_SEEK_CUR, cancellable, error);
2672						    if (*error)
2673							    goto end;
2674						    continue;
2675					    }
2676					    gtype = g_xim_attr_get_gtype_by_id(G_XIM_ATTR (conn->default_icattr), attr_id);
2677					    if (gtype == G_TYPE_INVALID) {
2678						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2679								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2680								"Unable to get the value type for attr_id %d",
2681								attr_id);
2682						    goto fail_nested_list_1;
2683					    }
2684					    ntype = g_xim_gtype_to_value_type(gtype);
2685					    if (ntype == G_XIM_TYPE_INVALID) {
2686						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2687								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2688								"Unable to convert the value type `%s' for attr_id %d",
2689								g_type_name(gtype), attr_id);
2690						    goto fail_nested_list_1;
2691					    }
2692					    if (!g_xim_protocol_read_format(proto, stream, cancellable, error, 2,
2693									    G_XIM_TYPE_MARKER_N_BYTES_2, ntype,
2694									    ntype, &v)) {
2695						    /* maybe hard to recover the error here */
2696						    goto end;
2697					    }
2698					    name = g_xim_attr_get_attribute_name(G_XIM_ATTR (conn->default_icattr), attr_id);
2699					    g_xim_nested_list_append(list, name, v);
2700					    g_free(name);
2701					    g_xim_free_by_gtype(gtype, v);
2702				    }
2703				    *((GXimNestedList **)value) = list;
2704			    } else {
2705				    GXimNestedList *list;
2706				    GXimConnection *conn;
2707
2708				    size = G_XIM_GET_MARKER_DATA (marker_data);
2709				    g_free(marker_data);
2710				    marker_data = NULL;
2711
2712				    conn = G_XIM_CONNECTION (proto);
2713				    /* XXX: NESTEDLIST is only used for IC attributes? */
2714				    list = g_xim_nested_list_new(G_XIM_ATTR (conn->default_icattr), size);
2715				    /* try to fetch a value until remaining size is more than minimum requirement */
2716				    while (size > 3) {
2717					    goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
2718					    gint16 attr_id;
2719					    guint16 n;
2720					    gpointer v;
2721					    GType gtype = G_TYPE_INVALID;
2722					    GXimValueType ntype;
2723					    gchar *name = NULL;
2724
2725					    if (!g_xim_protocol_read_format(proto, stream, cancellable, error, 1,
2726									    G_XIM_TYPE_WORD, &attr_id)) {
2727					      fail_nested_list_2:
2728						    g_prefix_error(error, "Error while processing a value type %s: ",
2729								   g_xim_value_type_name(vtype));
2730						    g_xim_messages_gerror(iface->message, *error);
2731						    g_clear_error(error);
2732						    n = g_data_input_stream_read_uint16(stream, cancellable, error);
2733						    g_seekable_seek(G_SEEKABLE (istream), n,
2734								    G_SEEK_CUR, cancellable, error);
2735						    if (*error) {
2736							    g_free(name);
2737							    goto end;
2738						    }
2739						    goto size_check_nested_list;
2740					    }
2741					    gtype = g_xim_attr_get_gtype_by_id(G_XIM_ATTR (conn->default_icattr), attr_id);
2742					    if (gtype == G_TYPE_INVALID) {
2743						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2744								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2745								"Unable to get the value type for attr_id %d",
2746								attr_id);
2747						    goto fail_nested_list_2;
2748					    }
2749					    ntype = g_xim_gtype_to_value_type(gtype);
2750					    if (ntype == G_XIM_TYPE_INVALID) {
2751						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2752								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2753								"Unable to convert the value type `%s' for attr_id %d",
2754								g_type_name(gtype), attr_id);
2755						    goto fail_nested_list_2;
2756					    }
2757
2758					    name = g_xim_attr_get_attribute_name(G_XIM_ATTR (conn->default_icattr), attr_id);
2759					    g_xim_messages_debug(iface->message, "proto/attr",
2760								 "    %d: Attribute: %s [%s]",
2761								 attr_id, name, g_xim_value_type_name(ntype));
2762
2763					    if (!g_xim_protocol_read_format(proto, stream, cancellable, error, 2,
2764									    G_XIM_TYPE_MARKER_N_BYTES_2, ntype,
2765									    ntype, &v)) {
2766						    /* maybe hard to recover the error here */
2767						    g_free(name);
2768						    /* however try to recover the existing data in the nested list as much as possible */
2769						    G_XIM_GERROR_RESET_NOTICE_FLAG (*error);
2770						    G_XIM_GERROR_SET_NOTICE_FLAG (*error, G_XIM_NOTICE_WARNING);
2771						    break;
2772					    }
2773
2774				      size_check_nested_list:
2775					    pos = g_seekable_tell(G_SEEKABLE (istream));
2776					    if ((pos - cur_pos) > size) {
2777						    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2778								G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2779								"Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
2780								g_xim_value_type_name(vtype), size, pos - cur_pos);
2781						    g_free(name);
2782						    goto end;
2783					    }
2784
2785					    if (name) {
2786						    g_xim_nested_list_append(list, name, v);
2787						    g_free(name);
2788						    name = NULL;
2789					    }
2790					    g_xim_free_by_gtype(gtype, v);
2791
2792					    size -= (pos - cur_pos);
2793				    }
2794				    *((GXimNestedList **)value) = list;
2795			    }
2796			    break;
2797		    default:
2798		    nonsupported:
2799			    g_set_error(error, G_XIM_PROTOCOL_ERROR,
2800					G_XIM_STD_ERROR_UNSUPPORTED | G_XIM_NOTICE_ERROR,
2801					"Unknown/unsupported value type to read: %s",
2802					g_xim_value_type_name(vtype));
2803			    goto end;
2804		}
2805		if (marker_data) {
2806			goffset pos;
2807
2808			if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
2809				g_set_error(error, G_XIM_PROTOCOL_ERROR,
2810					    G_XIM_STD_ERROR_UNSUPPORTED | G_XIM_NOTICE_ERROR,
2811					    "Value type %s doesn't support the number-of-item marker",
2812					    g_xim_value_type_name(vtype));
2813				goto end;
2814			}
2815			size = G_XIM_GET_MARKER_DATA (marker_data);
2816			pos = g_seekable_tell(G_SEEKABLE (istream));
2817			if ((pos - cur_pos) != size) {
2818				g_set_error(error, G_XIM_PROTOCOL_ERROR,
2819					    G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
2820					    "Unable to compose a value type %s properly due to the size difference: expected size: %" G_GSIZE_FORMAT ", actual size: %" G_GINT64_FORMAT,
2821					    g_xim_value_type_name(vtype), size, pos - cur_pos);
2822				goto end;
2823			}
2824			g_free(marker_data);
2825			marker_data = NULL;
2826		}
2827		base_pos = cur_pos;
2828		if (*error) {
2829			if (!G_XIM_GERROR_IS_RECOVERABLE (*error))
2830				goto end;
2831			g_prefix_error(error, "Error while processing a value type %s: ",
2832				       g_xim_value_type_name(vtype));
2833			g_xim_messages_gerror(iface->message, *error);
2834			g_clear_error(error);
2835		}
2836	}
2837
2838  end:
2839	if (marker_data) {
2840		/* to avoid the memory leak for the case broke out for errors */
2841		g_free(marker_data);
2842		marker_data = NULL;
2843	}
2844	if (*error) {
2845		g_prefix_error(error, "Error while processing a value type %s: ",
2846			       g_xim_value_type_name(vtype));
2847		retval = FALSE;
2848	}
2849	if (!g_queue_is_empty(markerq)) {
2850		g_xim_messages_bug(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
2851				   "Unused marker(s) given: %u is still in the queue for reading.",
2852				   g_queue_get_length(markerq));
2853		while (!g_queue_is_empty(markerq)) {
2854			marker_data = g_queue_pop_tail(markerq);
2855			g_xim_messages_bug(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
2856					   "  marker type: %s",
2857					   g_xim_value_type_name(marker_data->where));
2858			g_free(marker_data);
2859		}
2860	}
2861	g_queue_free(markerq);
2862
2863	return retval;
2864}
2865
2866/**
2867 * g_xim_protocol_read_format:
2868 * @proto: a #GXimProtocol.
2869 * @stream: a #GDataInputStream which contains XIM protocol packets.
2870 * @cancellable: optional #GCancellable object, %NULL to ignore.
2871 * @error: a location to store error, or %NULL.
2872 * @n_params: the number of a set of parameters.
2873 *
2874 * Scans XIM protocol packets according to the set of parameters. if any,
2875 * objects generated against it is stored in the locations pointed to by
2876 * the pointer arguments. Each pointer argument must be of a type that is
2877 * appropriate for the value returned by the corresponding value type.
2878 * See #GXimValueType documentation which objects the value type is supposed
2879 * to be.
2880 *
2881 * Returns: %TRUE to be read successfully.
2882 */
2883gboolean
2884g_xim_protocol_read_format(GXimProtocol      *proto,
2885			   GDataInputStream  *stream,
2886			   GCancellable      *cancellable,
2887			   GError           **error,
2888			   guint              n_params,
2889			   ...)
2890{
2891	va_list ap;
2892	gboolean retval;
2893
2894	va_start(ap, n_params);
2895
2896	retval = g_xim_protocol_read_vformat(proto, stream, cancellable, error, n_params, ap);
2897
2898	va_end(ap);
2899
2900	return retval;
2901}
2902
2903/**
2904 * g_xim_protocol_wait_for_reply:
2905 * @proto: a #GXimProtocol.
2906 * @major_opcode: a major opcode in XIM protocol.
2907 * @minor_opcode: a minor opcode in XIM protocol.
2908 * @error: a location to store error, or %NULL.
2909 *
2910 * Waits for a response of @major_opcode and @minor_opcode or %G_XIM_ERROR.
2911 * this can be used if you need to send a request synchronously.  Note that
2912 * using this function in the server instance won't works due to how GObject
2913 * delivers signals and iterates it.
2914 *
2915 * Returns: %TRUE the request was successfully done.
2916 */
2917gboolean
2918g_xim_protocol_wait_for_reply(GXimProtocol *proto,
2919			      GXimOpcode    major_opcode,
2920			      guint8        minor_opcode,
2921			      GError      **error)
2922{
2923	GXimProtocolPrivate *priv;
2924	GXimProtocolSyncable *syncable;
2925	gboolean retval = TRUE;
2926
2927	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
2928	g_return_val_if_fail (error != NULL, FALSE);
2929
2930	priv = g_xim_protocol_get_private(proto);
2931	syncable = g_new0(GXimProtocolSyncable, 1);
2932	G_XIM_GERROR_CHECK_ALLOC (syncable, error,
2933				  G_XIM_PROTOCOL_ERROR, FALSE);
2934
2935	syncable->major_opcode = major_opcode;
2936	syncable->minor_opcode = minor_opcode;
2937	syncable->error        = NULL;
2938
2939	G_LOCK (g_xim_protocol_syncable);
2940	g_queue_push_tail(priv->syncableq, syncable);
2941	G_UNLOCK (g_xim_protocol_syncable);
2942
2943	while (g_queue_find(priv->syncableq, syncable))
2944		g_main_context_iteration(NULL, TRUE);
2945
2946	retval = syncable->result;
2947	if (syncable->error) {
2948		retval = FALSE;
2949		*error = g_error_copy(syncable->error);
2950		g_error_free(syncable->error);
2951	}
2952	g_free(syncable);
2953
2954	return retval;
2955}
2956
2957/**
2958 * g_xim_protocol_raise_parser_error:
2959 * @proto: a #GXimProtocol.
2960 * @major_opcode: a major opcode in XIM protocol.
2961 * @minor_opcode: a minor opcode in XIM protocol.
2962 * @imid: input-method ID.
2963 * @icid: input-context ID.
2964 *
2965 * Raises an appropriate error to the opposite direction on the connection
2966 * according to @major_opcode and @minor_opcode.
2967 *
2968 * Returns: %TRUE to be sent an error successfully.
2969 */
2970gboolean
2971g_xim_protocol_raise_parser_error(GXimProtocol *proto,
2972				  guint         major_opcode,
2973				  guint         minor_opcode,
2974				  guint         imid,
2975				  guint         icid)
2976{
2977	gboolean ret = FALSE;
2978
2979	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
2980
2981	g_signal_emit(proto, signals[PARSER_ERROR], 0,
2982		      major_opcode, minor_opcode, imid, icid, &ret);
2983
2984	return ret;
2985}
2986
2987/**
2988 * g_xim_protocol_add_protocol:
2989 * @proto: a #GXimProtocol.
2990 * @closure: a structure of closure.
2991 *
2992 * Registers a @closure to add the XIM protocol handler.
2993 */
2994void
2995g_xim_protocol_add_protocol(GXimProtocol        *proto,
2996			    GXimProtocolClosure *closure)
2997{
2998	GXimProtocolPrivate *priv;
2999	GXimProtocolClosure *c;
3000
3001	g_return_if_fail (G_IS_XIM_PROTOCOL (proto));
3002	g_return_if_fail (closure != NULL);
3003	g_return_if_fail ((priv = g_xim_protocol_get_private(proto)));
3004
3005	c = g_xim_protocol_lookup_protocol_by_id(proto,
3006						 closure->major_opcode.v2,
3007						 closure->minor_opcode);
3008	if (c != NULL) {
3009		g_xim_messages_warning(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
3010				       "Protocol (major: 0x%x, minor: 0x%x) has already been registered. replacing\n  details: old: %s, new: %s",
3011				       c->major_opcode.v2, c->minor_opcode,
3012				       c->name, closure->name);
3013	}
3014	g_hash_table_replace(priv->proto_table__named_index,
3015			     g_ascii_strup(closure->name, -1),
3016			     closure);
3017	g_hash_table_replace(priv->proto_table__id_index,
3018			     G_XIM_OPCODE_KEY (closure->major_opcode.v2, closure->minor_opcode),
3019			     closure);
3020}
3021
3022/**
3023 * g_xim_protocol_remove_protocol:
3024 * @proto: a #GXimProtocol.
3025 * @closure: a structure of closure.
3026 *
3027 * Gets rid of @closure to stop XIM protocol handling in @proto.
3028 */
3029void
3030g_xim_protocol_remove_protocol(GXimProtocol        *proto,
3031			       GXimProtocolClosure *closure)
3032{
3033	g_xim_protocol_remove_protocol_by_id(proto,
3034					     closure->major_opcode.v2,
3035					     closure->minor_opcode);
3036}
3037
3038/**
3039 * g_xim_protocol_remove_protocol_by_id:
3040 * @proto: a #GXimProtocol.
3041 * @major_opcode: a major opcode in XIM protocol.
3042 * @minor_opcode: a minor opcode in XIM protocol.
3043 *
3044 * Gets rid of XIM protocol handling to stop dealing with @major_opcode and
3045 * @minor_opcode in @proto.
3046 */
3047void
3048g_xim_protocol_remove_protocol_by_id(GXimProtocol *proto,
3049				     guint8        major_opcode,
3050				     guint8        minor_opcode)
3051{
3052	GXimProtocolClosure *c;
3053	GXimProtocolPrivate *priv;
3054
3055	g_return_if_fail (G_IS_XIM_PROTOCOL (proto));
3056	g_return_if_fail ((priv = g_xim_protocol_get_private(proto)));
3057
3058	c = g_xim_protocol_lookup_protocol_by_id(proto, major_opcode, minor_opcode);
3059	if (c == NULL) {
3060		g_xim_messages_warning(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
3061				       "Protocol (major: 0x%x, minor: 0x%x) isn't yet registered.",
3062				       major_opcode, minor_opcode);
3063	} else {
3064		gchar *name = g_ascii_strup(c->name, -1);
3065
3066		g_hash_table_remove(priv->proto_table__named_index,
3067				    name);
3068		g_hash_table_remove(priv->proto_table__id_index,
3069				    G_XIM_OPCODE_KEY (major_opcode, minor_opcode));
3070		g_free(name);
3071	}
3072}
3073
3074/**
3075 * g_xim_protocol_lookup_protocol_by_name:
3076 * @proto: a #GXimProtocol.
3077 * @name: the name of XIM protocol event.
3078 *
3079 * Looks up the XIM protocol closure with @name which @proto is dealing with.
3080 *
3081 * Returns: a #GXimProtocolClosure corresponding to @name.
3082 */
3083GXimProtocolClosure *
3084g_xim_protocol_lookup_protocol_by_name(GXimProtocol *proto,
3085				       const gchar  *name)
3086{
3087	GXimProtocolPrivate *priv;
3088	GXimProtocolClosure *retval;
3089	gchar *s;
3090
3091	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), NULL);
3092	g_return_val_if_fail (name != NULL, NULL);
3093	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), NULL);
3094
3095	s = g_ascii_strup(name, -1);
3096	retval = g_hash_table_lookup(priv->proto_table__named_index,
3097				     s);
3098	g_free(s);
3099
3100	return retval;
3101}
3102
3103/**
3104 * g_xim_protocol_lookup_protocol_by_id:
3105 * @proto: a #GXimProtocol.
3106 * @major_opcode: a major opcode in XIM protocol.
3107 * @minor_opcode: a minor opcode in XIM protocol.
3108 *
3109 * Looks up the XIM protocol closure with @major_opcode and @minor_opcode which
3110 * @proto is dealing with.
3111 *
3112 * Returns: a #GXimProtocolClosure corresponding to @major_opcode and @minor_opcode.
3113 */
3114GXimProtocolClosure *
3115g_xim_protocol_lookup_protocol_by_id(GXimProtocol *proto,
3116				     guint8        major_opcode,
3117				     guint8        minor_opcode)
3118{
3119	GXimProtocolPrivate *priv;
3120	GXimProtocolClosure *retval;
3121
3122	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), NULL);
3123	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), NULL);
3124
3125	retval = g_hash_table_lookup(priv->proto_table__id_index,
3126				     G_XIM_OPCODE_KEY (major_opcode, minor_opcode));
3127
3128	return retval;
3129}
3130
3131/**
3132 * g_xim_protocol_connect_closure_by_id:
3133 * @proto: a #GXimProtocol.
3134 * @major_opcode: a major opcode in XIM protocol.
3135 * @minor_opcode: a minor opcode in XIM protocol.
3136 * @func: the #GCallback to connect.
3137 * @data: data to pass to @func.
3138 *
3139 * Connects a #GCallback function to a signal for XIM protocol events.
3140 *
3141 * Returns: the handler id.
3142 */
3143gulong
3144g_xim_protocol_connect_closure_by_id(GXimProtocol *proto,
3145				     guint8        major_opcode,
3146				     guint8        minor_opcode,
3147				     GCallback     func,
3148				     gpointer      data)
3149{
3150	GXimProtocolClosure *closure;
3151
3152	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), 0);
3153
3154	closure = g_xim_protocol_lookup_protocol_by_id(proto, major_opcode, minor_opcode);
3155	if (closure == NULL) {
3156		g_xim_messages_bug(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
3157				   "No closure for protocol major:0x%x, minor:0x%x",
3158				   major_opcode, minor_opcode);
3159		return 0;
3160	}
3161
3162	return g_xim_protocol_closure_connect(closure, func, data);
3163}
3164
3165/**
3166 * g_xim_protocol_connect_closure_by_name:
3167 * @proto: a #GXimProtocol.
3168 * @signal_name: a XIM protocol event name.
3169 * @func: the #GCallback to connect.
3170 * @data: data to pass to @func.
3171 *
3172 * Connects a #GCallback function to a signal for XIM protocol events.
3173 *
3174 * Returns: the handler id.
3175 */
3176gulong
3177g_xim_protocol_connect_closure_by_name(GXimProtocol *proto,
3178				       const gchar  *signal_name,
3179				       GCallback     func,
3180				       gpointer      data)
3181{
3182	GXimProtocolClosure *closure;
3183
3184	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), 0);
3185
3186	closure = g_xim_protocol_lookup_protocol_by_name(proto, signal_name);
3187	if (closure == NULL) {
3188		g_xim_messages_bug(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
3189				   "No closure for protocol `%s'",
3190				   signal_name);
3191		return 0;
3192	}
3193
3194	return g_xim_protocol_closure_connect(closure, func, data);
3195}
3196
3197/**
3198 * g_xim_protocol_get_extensions:
3199 * @proto: a #GXimProtocol.
3200 *
3201 * Obtains a set of #GXimProtocolClosure registered as an extension. this is
3202 * helpful to deal with %G_XIM_QUERY_EXTENSION and so on.
3203 *
3204 * Returns: the #GSList which contains #GXimProtocolClosure.
3205 */
3206GSList *
3207g_xim_protocol_get_extensions(GXimProtocol *proto)
3208{
3209	GXimProtocolPrivate *priv;
3210	GXimProtocolClosure *c;
3211	GHashTableIter iter;
3212	GSList *retval = NULL;
3213	gpointer key, val;
3214
3215	g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), NULL);
3216	g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), NULL);
3217
3218	g_hash_table_iter_init(&iter, priv->proto_table__id_index);
3219	while (g_hash_table_iter_next(&iter, &key, &val)) {
3220		c = val;
3221
3222		if (g_xim_protocol_closure_is_an_extension(c))
3223			retval = g_slist_append(retval, val);
3224	}
3225
3226	return retval;
3227}
3228
3229/**
3230 * g_xim_protocol_closure_new:
3231 * @major_opcode: a major opcode in XIM protocol.
3232 * @minor_opcode: a minor opcode in XIM protocol.
3233 * @name: the XIM protocol name.
3234 * @is_an_extension: %TRUE to define an extension in XIM protocol, otherwise %FALSE.
3235 *
3236 * Creates an instance of the #GXimProtocolClosure.
3237 *
3238 * Returns: the #GXimProtocolClosure.
3239 */
3240GXimProtocolClosure *
3241g_xim_protocol_closure_new(guint8       major_opcode,
3242			   guint8       minor_opcode,
3243			   const gchar *name,
3244			   gboolean     is_an_extension)
3245{
3246	GClosure *closure;
3247	GXimProtocolClosure *retval;
3248
3249	g_return_val_if_fail (name != NULL, NULL);
3250
3251	closure = g_closure_new_simple(sizeof (GXimProtocolClosure), NULL);
3252	G_XIM_CHECK_ALLOC (closure, NULL);
3253
3254	retval = (GXimProtocolClosure *)closure;
3255	retval->name = g_strdup(name);
3256	retval->major_opcode.v2 = major_opcode;
3257	retval->minor_opcode = minor_opcode;
3258	retval->is_an_extension = is_an_extension;
3259
3260	g_closure_add_finalize_notifier(closure, NULL, g_xim_protocol_closure_free);
3261
3262	return retval;
3263}
3264
3265void
3266g_xim_protocol_closure_free(gpointer  data,
3267			    GClosure *closure)
3268{
3269	GXimProtocolClosure *c;
3270
3271	g_return_if_fail (closure != NULL);
3272
3273	c = (GXimProtocolClosure *)closure;
3274	g_free(c->param_types);
3275	g_free(c->name);
3276	g_slist_foreach(c->signal_handlers, (GFunc)g_free, NULL);
3277	g_slist_free(c->signal_handlers);
3278}
3279
3280/**
3281 * g_xim_protocol_closure_is_an_extension:
3282 * @closure: a #GXimProtocolClosure.
3283 *
3284 * Checks if the @closure was created as an extension protocol in XIM.
3285 *
3286 * Returns: %TRUE to be an extension.
3287 */
3288gboolean
3289g_xim_protocol_closure_is_an_extension(GXimProtocolClosure *closure)
3290{
3291	g_return_val_if_fail (closure != NULL, FALSE);
3292
3293	return closure->is_an_extension;
3294}
3295
3296/**
3297 * g_xim_protocol_closure_set_marshal:
3298 * @closure: a #GXimProtocolClosure.
3299 * @func: the #GXimProtocolClosureFunc function to scan the XIM protocol packet
3300 *  and emits the proper signal.
3301 * @data: the data to store in the @data field of the #GClosure.
3302 *
3303 * Sets the marshaller of @closure which scans the XIM protocol packets and emit
3304 * the proper signal as needed.  Note that g_xim_protocol_closure_emit_signal()
3305 * has to be invoked in @func otherwise any signals added by
3306 * g_xim_protocol_closure_add_signal() won't do anything at all.
3307 */
3308void
3309g_xim_protocol_closure_set_marshal(GXimProtocolClosure     *closure,
3310				   GXimProtocolClosureFunc  func,
3311				   gpointer                 data)
3312{
3313	g_return_if_fail (closure != NULL);
3314	g_return_if_fail (func != NULL);
3315
3316	((GCClosure *)closure)->callback = (gpointer)func;
3317	((GClosure *)closure)->data = data;
3318	g_closure_set_marshal((GClosure *)closure,
3319			      g_xim_protocol_closure_marshal_BOOLEAN__OBJECT_OBJECT_POINTER);
3320}
3321
3322/**
3323 * g_xim_protocol_closure_add_signal:
3324 * @closure: a #GXimProtocolClosure.
3325 * @marshaller: the #GClosureMarshal to deal with the signal.
3326 * @n_params: the number of parameters for the signal.
3327 * @...: a list of types, one for each parameter.
3328 *
3329 * Adds a marshaller for signal.
3330 */
3331void
3332g_xim_protocol_closure_add_signal(GXimProtocolClosure *closure,
3333				  GClosureMarshal      marshaller,
3334				  guint                n_params,
3335				  ...)
3336{
3337	va_list ap;
3338	guint i;
3339	GType gtype;
3340
3341	g_return_if_fail (closure != NULL);
3342	g_return_if_fail (marshaller != NULL);
3343	g_return_if_fail (closure->param_types == NULL);
3344
3345	va_start(ap, n_params);
3346
3347	closure->param_types = g_new0(GType, n_params);
3348	for (i = 0; i < n_params; i++) {
3349		gtype = va_arg(ap, GType);
3350		if (!G_TYPE_IS_VALUE (gtype & ~G_SIGNAL_TYPE_STATIC_SCOPE)) {
3351			const gchar *name;
3352
3353			g_warning("parameter %d of type `%s' for protocol \"%s[0x%x:0x%x]\" is not a value type",
3354				  i + 1, gtype ? ((name = g_type_name(gtype)) ? name : "<unknown>") : "<invalid>",
3355				  closure->name, closure->major_opcode.v1, closure->minor_opcode);
3356			g_free(closure->param_types);
3357			closure->param_types = NULL;
3358			goto end;
3359		}
3360		closure->param_types[i] = gtype;
3361	}
3362	closure->signal_marshaller = marshaller;
3363	closure->n_params = n_params;
3364  end:
3365
3366	va_end(ap);
3367}
3368
3369/**
3370 * g_xim_protocol_closure_connect:
3371 * @closure: a #GXimProtocolClosure.
3372 * @func: the #GCallback to connect.
3373 * @data: data to pass to @func.
3374 *
3375 * Connects a closure to a signal.
3376 *
3377 * Returns: the handler id.
3378 */
3379gulong
3380g_xim_protocol_closure_connect(GXimProtocolClosure *closure,
3381			       GCallback            func,
3382			       gpointer             data)
3383{
3384	GXimProtocolClosureNode *node;
3385
3386	g_return_val_if_fail (closure != NULL, 0);
3387	g_return_val_if_fail (closure->signal_marshaller != NULL, 0);
3388	g_return_val_if_fail (func != NULL, 0);
3389
3390	node = g_new0(GXimProtocolClosureNode, 1);
3391	G_XIM_CHECK_ALLOC (node, 0);
3392
3393	node->func = func;
3394	node->user_data = data;
3395	closure->signal_handlers = g_slist_prepend(closure->signal_handlers, node);
3396
3397	return (gulong)node;
3398}
3399
3400/**
3401 * g_xim_protocol_closure_disconnect:
3402 * @closure: a #GXimProtocolClosure.
3403 * @id: Handler id of the handler to be disconnected.
3404 *
3405 * Disconnects a handler from an instance so it will not be called during
3406 * any future or currently ongoing emissions of the signal if has been
3407 * connected to. The @id becomes invalid and may be reused.
3408 *
3409 * The @id has to be a valid signal handler id, connected to a
3410 * signal of @closure.
3411 */
3412void
3413g_xim_protocol_closure_disconnect(GXimProtocolClosure *closure,
3414				  gulong               id)
3415{
3416	GSList *l;
3417
3418	g_return_if_fail (closure != NULL);
3419	g_return_if_fail (id != 0);
3420
3421	l = g_slist_find(closure->signal_handlers, (gpointer)id);
3422	if (l) {
3423		g_free(l->data);
3424		closure->signal_handlers = g_slist_delete_link(closure->signal_handlers, l);
3425	}
3426}
3427
3428/**
3429 * g_xim_protocol_closure_emit_signal:
3430 * @closure: a #GXimProtocolClosure.
3431 * @proto: the instance which wants to raise a signal with @closure.
3432 * @...: parameters to be passed to the signal.
3433 *
3434 * Emits a signal.
3435 *
3436 * Returns: %TRUE to be proceeded a signal in the handlers. %FALSE to be failed
3437 *  in any handlers or no handlers for this signal.
3438 */
3439gboolean
3440g_xim_protocol_closure_emit_signal(GXimProtocolClosure *closure,
3441				   GXimProtocol        *proto,
3442				   ...)
3443{
3444	va_list ap;
3445	GValue v = { 0, }, *pv;
3446	GClosure *c;
3447	GSList *l;
3448	gboolean ret = FALSE;
3449	guint i;
3450	const gchar *name;
3451
3452	g_return_val_if_fail (closure != NULL, FALSE);
3453	g_return_val_if_fail (closure->signal_marshaller != NULL, FALSE);
3454
3455	g_value_init(&v, G_TYPE_BOOLEAN);
3456	pv = g_new0(GValue, closure->n_params + 1);
3457	G_XIM_CHECK_ALLOC (pv, FALSE);
3458
3459	g_value_init(&pv[0], G_TYPE_OBJECT);
3460	g_value_set_object(&pv[0], proto);
3461
3462	va_start(ap, proto);
3463
3464	for (i = 0; i < closure->n_params; i++) {
3465		if (g_type_is_a(closure->param_types[i], G_TYPE_OBJECT))
3466			g_value_init(&pv[i + 1], G_TYPE_OBJECT);
3467		else
3468			g_value_init(&pv[i + 1], closure->param_types[i]);
3469		switch (closure->param_types[i]) {
3470		    case G_TYPE_BOOLEAN:
3471			    g_value_set_boolean(&pv[i + 1], va_arg(ap, gboolean));
3472			    break;
3473		    case G_TYPE_CHAR:
3474			    g_value_set_schar(&pv[i + 1], va_arg(ap, gint));
3475			    break;
3476		    case G_TYPE_UCHAR:
3477			    g_value_set_uchar(&pv[i + 1], va_arg(ap, guint));
3478			    break;
3479		    case G_TYPE_INT:
3480			    g_value_set_int(&pv[i + 1], va_arg(ap, gint));
3481			    break;
3482		    case G_TYPE_UINT:
3483			    g_value_set_uint(&pv[i + 1], va_arg(ap, guint));
3484			    break;
3485		    case G_TYPE_LONG:
3486			    g_value_set_long(&pv[i + 1], va_arg(ap, glong));
3487			    break;
3488		    case G_TYPE_ULONG:
3489			    g_value_set_ulong(&pv[i + 1], va_arg(ap, gulong));
3490			    break;
3491		    case G_TYPE_INT64:
3492			    g_value_set_int64(&pv[i + 1], va_arg(ap, gint64));
3493			    break;
3494		    case G_TYPE_UINT64:
3495			    g_value_set_uint64(&pv[i + 1], va_arg(ap, guint64));
3496			    break;
3497		    case G_TYPE_FLOAT:
3498			    g_value_set_float(&pv[i + 1], va_arg(ap, gdouble));
3499			    break;
3500		    case G_TYPE_DOUBLE:
3501			    g_value_set_double(&pv[i + 1], va_arg(ap, gdouble));
3502			    break;
3503		    case G_TYPE_STRING:
3504			    g_value_set_string(&pv[i + 1], va_arg(ap, gchar *));
3505			    break;
3506		    case G_TYPE_POINTER:
3507			    g_value_set_pointer(&pv[i + 1], va_arg(ap, gpointer));
3508			    break;
3509		    case G_TYPE_ENUM:
3510			    g_value_set_enum(&pv[i + 1], va_arg(ap, gint));
3511			    break;
3512		    case G_TYPE_FLAGS:
3513			    g_value_set_flags(&pv[i + 1], va_arg(ap, guint));
3514			    break;
3515		    case G_TYPE_PARAM:
3516			    g_value_set_param(&pv[i + 1], va_arg(ap, GParamSpec *));
3517			    break;
3518		    case G_TYPE_BOXED:
3519			    g_value_set_boxed(&pv[i + 1], va_arg(ap, gpointer));
3520			    break;
3521		    default:
3522			    if (g_type_is_a(closure->param_types[i], G_TYPE_GTYPE)) {
3523				    g_value_set_gtype(&pv[i + 1], va_arg(ap, GType));
3524			    } else if (g_type_is_a(closure->param_types[i], G_TYPE_OBJECT)) {
3525				    g_value_set_object(&pv[i + 1], va_arg(ap, gpointer));
3526			    } else if (G_TYPE_IS_VALUE (closure->param_types[i])) {
3527				    g_value_set_boxed(&pv[i + 1], va_arg(ap, gpointer));
3528			    } else {
3529				    g_xim_messages_warning(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
3530							   "Unsupported value type `%s' to emit a signal for %s",
3531							   closure->param_types[i] ? ((name = g_type_name(closure->param_types[i])) ? name : "<unknown>") : "<invalid>",
3532							   closure->name);
3533				    goto end;
3534			    }
3535			    break;
3536		}
3537	}
3538
3539	for (l = closure->signal_handlers; l != NULL; l = g_slist_next(l)) {
3540		GXimProtocolClosureNode *node = l->data;
3541
3542		c = g_cclosure_new(node->func, node->user_data, NULL);
3543		G_XIM_CHECK_ALLOC (c, FALSE);
3544
3545		g_closure_set_marshal(c, closure->signal_marshaller);
3546		g_closure_invoke(c, &v, closure->n_params + 1, pv, NULL);
3547
3548		ret = g_value_get_boolean(&v);
3549
3550		g_closure_unref(c);
3551		if (ret)
3552			break;
3553	}
3554	for (i = 0; i < closure->n_params + 1; i++) {
3555		g_value_unset(&pv[i]);
3556	}
3557
3558  end:
3559	g_free(pv);
3560
3561	va_end(ap);
3562
3563	return ret;
3564}