PageRenderTime 55ms CodeModel.GetById 11ms 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. #include <stdlib.h>
  28. #ifdef HAVE_STRING_H
  29. #include <string.h>
  30. #endif
  31. #include <glib/gi18n-lib.h>
  32. #include <gdk/gdk.h>
  33. #include <gdk/gdkx.h>
  34. #include "gximacc.h"
  35. #include "gximattr.h"
  36. #include "gximconnection.h"
  37. #include "gximmarshal.h"
  38. #include "gximmessages.h"
  39. #include "gximmisc.h"
  40. #include "gximprotocol10.h"
  41. #include "gximtransport.h"
  42. #include "gximprotocol.h"
  43. #define G_XIM_MARKER_IS_ITEM_BASED(_m_) ((_m_)->type == G_XIM_TYPE_MARKER_N_ITEMS_2)
  44. #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))
  45. /**
  46. * SECTION:gximprotocol
  47. * @Title: GXimProtocol
  48. * @Short_Description: Base interface for XIM protocol
  49. *
  50. * GXimProtocol provides an interface to deal with XIM protocol events.
  51. */
  52. typedef struct _GXimProtocolMarker {
  53. GXimValueType type;
  54. GXimValueType where;
  55. goffset offset;
  56. union {
  57. guint8 b;
  58. guint16 w;
  59. guint32 l;
  60. } value;
  61. } GXimProtocolMarker;
  62. typedef struct _GXimProtocolSyncable {
  63. GXimOpcode major_opcode;
  64. guint8 minor_opcode;
  65. gboolean result;
  66. GError *error;
  67. } GXimProtocolSyncable;
  68. typedef struct _GXimProtocolClosureNode {
  69. GCallback func;
  70. gpointer user_data;
  71. } GXimProtocolClosureNode;
  72. enum {
  73. SIGNAL_0,
  74. PARSER_ERROR,
  75. LAST_SIGNAL
  76. };
  77. static guint signals[LAST_SIGNAL] = { 0 };
  78. G_LOCK_DEFINE_STATIC (g_xim_protocol_marker);
  79. G_LOCK_DEFINE_STATIC (g_xim_protocol_syncable);
  80. /*
  81. * Private functions
  82. */
  83. static void
  84. g_xim_protocol_base_class_init(gpointer g_iface)
  85. {
  86. GXimProtocolIface *iface = g_iface;
  87. static gboolean initialized = FALSE;
  88. if (!initialized) {
  89. initialized = TRUE;
  90. iface->atom_xim_protocol = gdk_atom_intern_static_string("_XIM_PROTOCOL");
  91. iface->atom_xim_moredata = gdk_atom_intern_static_string("_XIM_MOREDATA");
  92. gdk_flush();
  93. iface->message = g_xim_messages_new();
  94. signals[PARSER_ERROR] = g_signal_new("parser_error",
  95. G_TYPE_FROM_INTERFACE (iface),
  96. G_SIGNAL_RUN_LAST,
  97. G_STRUCT_OFFSET (GXimProtocolIface, parser_error),
  98. _gxim_acc_signal_accumulator__BOOLEAN,
  99. NULL,
  100. gxim_marshal_BOOLEAN__UINT_UINT_UINT_UINT,
  101. G_TYPE_BOOLEAN, 4,
  102. G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
  103. } else {
  104. g_object_ref(iface->message);
  105. }
  106. }
  107. static void
  108. g_xim_protocol_base_class_finalize(gpointer g_iface)
  109. {
  110. GXimProtocolIface *iface = g_iface;
  111. g_object_unref(iface->message);
  112. }
  113. static gboolean
  114. g_xim_protocol_send_header(GXimProtocol *proto,
  115. GXimOpcode major_opcode,
  116. guint8 minor_opcode,
  117. GCancellable *cancellable,
  118. GError **error)
  119. {
  120. GXimProtocolPrivate *priv;
  121. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
  122. g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, FALSE);
  123. g_data_output_stream_set_byte_order(priv->send_ostream, priv->byte_order);
  124. g_data_output_stream_put_byte(priv->send_ostream, major_opcode, cancellable, error);
  125. g_data_output_stream_put_byte(priv->send_ostream, minor_opcode, cancellable, error);
  126. /* dummy for CARD16 */
  127. g_data_output_stream_put_byte(priv->send_ostream, 0, cancellable, error);
  128. g_data_output_stream_put_byte(priv->send_ostream, 0, cancellable, error);
  129. priv->n_sent += 4;
  130. return error ? *error != NULL : TRUE;
  131. }
  132. static gsize
  133. g_xim_protocol_send_fixate_size(GXimProtocol *proto,
  134. GCancellable *cancellable,
  135. GError **error)
  136. {
  137. GXimProtocolIface *iface = G_XIM_PROTOCOL_GET_IFACE (proto);
  138. GXimProtocolPrivate *priv;
  139. goffset pos;
  140. gsize osize;
  141. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), 0);
  142. g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, 0);
  143. g_return_val_if_fail (priv->n_sent > 0, 0);
  144. g_return_val_if_fail (error != NULL, 0);
  145. /* Update the packet size */
  146. osize = priv->n_sent;
  147. if (osize % 4)
  148. g_xim_messages_warning(iface->message,
  149. "Bad padding: the number of packets: %" G_GSIZE_FORMAT,
  150. osize);
  151. pos = g_seekable_tell(G_SEEKABLE (priv->base_send_ostream));
  152. g_seekable_seek(G_SEEKABLE (priv->base_send_ostream),
  153. 2, G_SEEK_SET, cancellable, error);
  154. g_xim_protocol_send_format(proto, cancellable, error, 1,
  155. G_XIM_TYPE_WORD, (osize - 4) / 4);
  156. g_seekable_seek(G_SEEKABLE (priv->base_send_ostream),
  157. pos, G_SEEK_SET, cancellable, error);
  158. if (error && *error != NULL)
  159. osize = 0;
  160. return osize;
  161. }
  162. static gboolean
  163. g_xim_protocol_send_terminate(GXimProtocol *proto,
  164. GCancellable *cancellable,
  165. GError **error)
  166. {
  167. GXimProtocolIface *iface = G_XIM_PROTOCOL_GET_IFACE (proto);
  168. GXimProtocolPrivate *priv;
  169. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
  170. g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, FALSE);
  171. /* XXX: workaround for failing to get the number of bytes written */
  172. g_seekable_seek(G_SEEKABLE (priv->base_send_ostream),
  173. 0, G_SEEK_SET, cancellable, error);
  174. G_LOCK (g_xim_protocol_marker);
  175. if (!g_queue_is_empty(priv->markerq)) {
  176. g_xim_messages_bug(iface->message,
  177. "Unused marker(s) given: %u is still in the queue.",
  178. g_queue_get_length(priv->markerq));
  179. g_queue_foreach(priv->markerq, (GFunc)g_free, NULL);
  180. g_queue_clear(priv->markerq);
  181. }
  182. G_UNLOCK (g_xim_protocol_marker);
  183. priv->n_sent = 0;
  184. return error ? *error != NULL : TRUE;
  185. }
  186. static void
  187. g_xim_protocol_closure_marshal_BOOLEAN__OBJECT_OBJECT_POINTER(GClosure *closure,
  188. GValue *return_value,
  189. guint n_param_values,
  190. const GValue *param_values,
  191. gpointer invocation_hint,
  192. gpointer marshal_data)
  193. {
  194. register GCClosure *cc = (GCClosure *)closure;
  195. register GXimProtocol *proto;
  196. register GDataInputStream *stream;
  197. register GError **error;
  198. register gpointer data;
  199. register GXimProtocolClosureFunc callback;
  200. gboolean retval;
  201. g_return_if_fail (n_param_values == 3);
  202. proto = g_value_get_object(&param_values[0]);
  203. g_return_if_fail (G_IS_XIM_PROTOCOL (proto));
  204. stream = g_value_get_object(&param_values[1]);
  205. g_return_if_fail (G_IS_DATA_INPUT_STREAM (stream));
  206. error = g_value_get_pointer(&param_values[2]);
  207. data = closure->data;
  208. callback = cc->callback;
  209. retval = callback((GXimProtocolClosure *)closure, proto, stream, error, data);
  210. g_value_set_boolean(return_value, retval);
  211. }
  212. static GXimProtocolMarker *
  213. g_xim_protocol_find_marker(GQueue *markerq,
  214. GXimValueType vtype)
  215. {
  216. GXimProtocolMarker *marker_data = NULL;
  217. G_LOCK (g_xim_protocol_marker);
  218. if (g_queue_is_empty(markerq))
  219. goto end;
  220. marker_data = g_queue_peek_tail(markerq);
  221. if (marker_data->where == vtype)
  222. marker_data = g_queue_pop_tail(markerq);
  223. else
  224. marker_data = NULL;
  225. end:
  226. G_UNLOCK (g_xim_protocol_marker);
  227. return marker_data;
  228. }
  229. static void
  230. _free_sendq(gpointer data,
  231. gpointer user_data)
  232. {
  233. GXimProtocolQueueNode *node = data;
  234. g_free(node->data);
  235. g_free(node);
  236. }
  237. /*
  238. * Public functions
  239. */
  240. GType
  241. g_xim_protocol_get_type(void)
  242. {
  243. static volatile gsize type_id_volatile = 0;
  244. if (g_once_init_enter(&type_id_volatile)) {
  245. GType type_id;
  246. GTypeInfo info = {
  247. .class_size = sizeof (GXimProtocolIface),
  248. .base_init = (GBaseInitFunc)g_xim_protocol_base_class_init,
  249. .base_finalize = (GBaseFinalizeFunc)g_xim_protocol_base_class_finalize,
  250. .class_init = NULL,
  251. .class_finalize = NULL,
  252. .class_data = NULL,
  253. .instance_size = 0,
  254. .n_preallocs = 0,
  255. .instance_init = NULL,
  256. .value_table = NULL
  257. };
  258. type_id = g_type_register_static(G_TYPE_INTERFACE, "GXimProtocol",
  259. &info, 0);
  260. g_type_interface_add_prerequisite(type_id, G_TYPE_XIM_TRANSPORT);
  261. g_once_init_leave(&type_id_volatile, type_id);
  262. }
  263. return type_id_volatile;
  264. }
  265. /**
  266. * g_xim_protocol_init:
  267. * @proto: a #GXimProtocol.
  268. *
  269. * Initializes @proto. this isn't being invoked from the class automatically
  270. * which has a #GXimProtocol interface. you need to call this function manually
  271. * before doing something with @proto.
  272. */
  273. void
  274. g_xim_protocol_init(GXimProtocol *proto)
  275. {
  276. GXimProtocolPrivate *priv;
  277. priv = g_new0(GXimProtocolPrivate, 1);
  278. G_XIM_CHECK_ALLOC_WITH_NO_RET (priv);
  279. priv->base_send_ostream = g_memory_output_stream_new(NULL, 0, g_realloc, g_free);
  280. priv->base_recv_ostream = g_memory_output_stream_new(NULL, 0, g_realloc, g_free);
  281. priv->send_ostream = g_data_output_stream_new(priv->base_send_ostream);
  282. priv->recv_ostream = g_data_output_stream_new(priv->base_recv_ostream);
  283. priv->proto_table__named_index = g_hash_table_new_full(g_str_hash, g_str_equal,
  284. g_free, (GDestroyNotify)g_closure_unref);
  285. priv->proto_table__id_index = g_hash_table_new(g_direct_hash, g_direct_equal);
  286. priv->markerq = g_queue_new();
  287. priv->syncableq = g_queue_new();
  288. priv->sendq = g_queue_new();
  289. priv->byte_order = G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN;
  290. priv->is_disconnected = FALSE;
  291. /* XXX: workaround for failing to get the number of bytes written */
  292. priv->n_sent = 0;
  293. priv->n_received = 0;
  294. g_object_set_data(G_OBJECT (proto), "libgxim-protocol-private", priv);
  295. g_xim_protocol10_closure_init(proto, NULL);
  296. }
  297. /**
  298. * g_xim_protocol_dispose:
  299. * @object: a #GObject.
  300. *
  301. * Releases all references to other objects. This can be used to break
  302. * reference cycles.
  303. *
  304. * This function should only be called from #GObject::dispose which has
  305. * the #GXimProtocol interface.
  306. */
  307. void
  308. g_xim_protocol_dispose(GObject *object)
  309. {
  310. GXimProtocol *proto = G_XIM_PROTOCOL (object);
  311. g_xim_protocol10_closure_finalize(proto);
  312. }
  313. /**
  314. * g_xim_protocol_finalize:
  315. * @object: a #GObject.
  316. *
  317. * Finalizes the instance.
  318. *
  319. * This function should only be called from #GObject::finalize which has
  320. * the #GXimProtocol interface.
  321. */
  322. void
  323. g_xim_protocol_finalize(GObject *object)
  324. {
  325. GXimProtocol *proto = G_XIM_PROTOCOL (object);
  326. GXimProtocolPrivate *priv = g_xim_protocol_get_private(proto);
  327. if (priv) {
  328. G_LOCK (g_xim_protocol_marker);
  329. g_queue_foreach(priv->markerq, (GFunc)g_free, NULL);
  330. g_queue_free(priv->markerq);
  331. priv->markerq = NULL;
  332. G_UNLOCK (g_xim_protocol_marker);
  333. G_LOCK (g_xim_protocol_syncable);
  334. g_queue_foreach(priv->syncableq, (GFunc)g_free, NULL);
  335. g_queue_free(priv->syncableq);
  336. priv->syncableq = NULL;
  337. G_UNLOCK (g_xim_protocol_syncable);
  338. g_queue_foreach(priv->sendq, (GFunc)_free_sendq, NULL);
  339. g_queue_free(priv->sendq);
  340. priv->sendq = NULL;
  341. g_hash_table_destroy(priv->proto_table__id_index);
  342. g_hash_table_destroy(priv->proto_table__named_index);
  343. g_object_unref(priv->recv_ostream);
  344. g_object_unref(priv->send_ostream);
  345. g_object_unref(priv->base_recv_ostream);
  346. g_object_unref(priv->base_send_ostream);
  347. g_free(priv);
  348. }
  349. }
  350. /**
  351. * g_xim_protocol_get_private:
  352. * @proto: a #GXimProtocol.
  353. *
  354. * Obtains the #GXimProtocolPrivate which is referenced @proto.
  355. */
  356. GXimProtocolPrivate *
  357. g_xim_protocol_get_private(GXimProtocol *proto)
  358. {
  359. return g_object_get_data(G_OBJECT (proto), "libgxim-protocol-private");
  360. }
  361. GQuark
  362. g_xim_protocol_get_error_quark(void)
  363. {
  364. static GQuark quark = 0;
  365. if (!quark)
  366. quark = g_quark_from_static_string("g-xim-protocol-error");
  367. return quark;
  368. }
  369. /**
  370. * g_xim_protocol_process_event:
  371. * @proto: a #GXimProtocol.
  372. * @event: the #GdkEventClient which wants to process this time.
  373. * @error: a location to store error, or %NULL.
  374. *
  375. * Picks up XIM protocol packets from @event and deal with it as needed.
  376. *
  377. * Returns: %TRUE to be processed successfully.
  378. */
  379. gboolean
  380. g_xim_protocol_process_event(GXimProtocol *proto,
  381. GdkEventClient *event,
  382. GError **error)
  383. {
  384. GXimProtocolIface *iface;
  385. GXimProtocolPrivate *priv;
  386. GXimTransport *trans;
  387. gsize transport_size;
  388. gboolean retval = FALSE;
  389. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
  390. g_return_val_if_fail (event != NULL, FALSE);
  391. g_return_val_if_fail (event->type == GDK_CLIENT_EVENT, FALSE);
  392. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
  393. g_return_val_if_fail (error != NULL, FALSE);
  394. iface = G_XIM_PROTOCOL_GET_IFACE (proto);
  395. trans = G_XIM_TRANSPORT (proto);
  396. transport_size = g_xim_transport_get_transport_size(trans);
  397. if (event->message_type == iface->atom_xim_protocol) {
  398. GdkAtom atom_type;
  399. gint i, format, bytes;
  400. guchar *prop = NULL;
  401. GdkDisplay *dpy = g_xim_transport_get_display(trans);
  402. if (event->data_format == 32) {
  403. g_xim_transport_get_property(trans, event->window,
  404. gdk_x11_xatom_to_atom_for_display(dpy, event->data.l[1]),
  405. GDK_NONE, event->data.l[0],
  406. &atom_type, &format, &bytes, &prop);
  407. if (prop) {
  408. for (i = 0; i < bytes; i++) {
  409. g_data_output_stream_put_byte(priv->recv_ostream,
  410. prop[i],
  411. NULL,
  412. NULL);
  413. priv->n_received++;
  414. }
  415. }
  416. } else if (event->data_format == 8) {
  417. gsize i;
  418. for (i = 0; i < transport_size; i++) {
  419. g_data_output_stream_put_byte(priv->recv_ostream,
  420. event->data.b[i],
  421. NULL,
  422. NULL);
  423. priv->n_received++;
  424. }
  425. } else {
  426. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  427. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_WARNING,
  428. _("Invalid packets on _XIM_PROTOCOL: format: %d."),
  429. event->data_format);
  430. }
  431. g_free(prop);
  432. if (priv->n_received > 0) {
  433. gpointer p;
  434. gchar *data;
  435. gssize length = priv->n_received;
  436. p = g_memory_output_stream_get_data(G_MEMORY_OUTPUT_STREAM (priv->base_recv_ostream));
  437. data = g_new0(gchar, sizeof (gchar) * length + 1);
  438. memcpy(data, p, sizeof (gchar) * length);
  439. /* XXX: workaround for failing to get the number of bytes written */
  440. g_seekable_seek(G_SEEKABLE (priv->base_recv_ostream),
  441. 0, G_SEEK_SET, NULL, NULL);
  442. priv->n_received = 0;
  443. retval = g_xim_protocol_translate(proto, data, length, error);
  444. g_free(data);
  445. }
  446. } else if (event->message_type == iface->atom_xim_moredata) {
  447. gsize i;
  448. for (i = 0; i < transport_size; i++) {
  449. g_data_output_stream_put_byte(priv->recv_ostream,
  450. event->data.b[i],
  451. NULL,
  452. NULL);
  453. priv->n_received++;
  454. }
  455. /* not yet process the packet.
  456. * there are still more packets.
  457. */
  458. retval = TRUE;
  459. }
  460. if (priv->is_disconnected) {
  461. /* unref here */
  462. g_object_unref(proto);
  463. }
  464. return retval;
  465. }
  466. /**
  467. * g_xim_protocol_translate:
  468. * @proto: a #GXimProtocol.
  469. * @data: a chunk of XIM protocol packets
  470. * @length: the number of @data.
  471. * @error: a location to store error, or %NULL.
  472. *
  473. * Translate the packets and deliver it to the appropriate places.
  474. *
  475. * Returns: %TRUE to be processed successfully.
  476. */
  477. #pragma GCC diagnostic ignored "-Wformat-extra-args" /* to prevent an error of using G_GSIZE_FORMAT */
  478. gboolean
  479. g_xim_protocol_translate(GXimProtocol *proto,
  480. gpointer data,
  481. gssize length,
  482. GError **error)
  483. {
  484. GXimProtocolIface *iface;
  485. GXimProtocolPrivate *priv;
  486. GXimTransport *trans;
  487. GInputStream *base_istream = NULL;
  488. GDataInputStream *istream = NULL;
  489. guint8 major_opcode, minor_opcode;
  490. guint16 packlen = 0;
  491. gsize avail;
  492. gboolean ret = FALSE;
  493. GXimProtocolSyncable *on_sync = NULL;
  494. GClosure *closure;
  495. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
  496. g_return_val_if_fail (data != NULL, FALSE);
  497. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
  498. iface = G_XIM_PROTOCOL_GET_IFACE (proto);
  499. trans = G_XIM_TRANSPORT (proto);
  500. if (length == 0) {
  501. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  502. G_XIM_PROTOCOL_ERROR_NO_DATA | G_XIM_NOTICE_ERROR,
  503. "No data to translate.");
  504. goto end;
  505. }
  506. g_xim_transport_dump(trans, data, length, FALSE);
  507. base_istream = g_memory_input_stream_new_from_data(data, length, NULL);
  508. istream = g_data_input_stream_new(base_istream);
  509. major_opcode = g_data_input_stream_read_byte(istream, NULL, NULL);
  510. minor_opcode = g_data_input_stream_read_byte(istream, NULL, error);
  511. if (*error)
  512. goto end;
  513. if (major_opcode == G_XIM_CONNECT) {
  514. /* decide the byte order */
  515. if (((gchar *)data)[4] == 0x42) {
  516. /* this is a big endian */
  517. priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
  518. } else if (((gchar *)data)[4] == 0x6c) {
  519. /* this is a little endian */
  520. priv->byte_order = G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
  521. } else {
  522. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  523. G_XIM_PROTOCOL_ERROR_UNKNOWN_ENDIAN | G_XIM_NOTICE_ERROR,
  524. "Unknown endian `%02X` or invalid packet received.",
  525. ((gchar *)data)[4]);
  526. goto end;
  527. }
  528. g_xim_messages_debug(iface->message, "proto",
  529. "Byte order is a %s",
  530. priv->byte_order == G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN ?
  531. "big endian" : "little endian");
  532. }
  533. g_data_input_stream_set_byte_order(istream, priv->byte_order);
  534. packlen = g_data_input_stream_read_uint16(istream, NULL, error);
  535. if (*error)
  536. goto end;
  537. avail = g_buffered_input_stream_get_buffer_size(G_BUFFERED_INPUT_STREAM (istream));
  538. if (avail < (packlen * 4)) {
  539. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  540. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  541. "Received packets size `%" G_GUINT16_FORMAT "' is less than `%" G_GSIZE_FORMAT "'",
  542. packlen * 4, avail);
  543. goto end;
  544. }
  545. /* After here, all the read should succeeds */
  546. G_LOCK (g_xim_protocol_syncable);
  547. if (!g_queue_is_empty(priv->syncableq)) {
  548. guint len, i;
  549. GXimProtocolSyncable *syncable;
  550. len = g_queue_get_length(priv->syncableq);
  551. for (i = 0; i < len; i++) {
  552. syncable = g_queue_peek_nth(priv->syncableq, i);
  553. if (syncable->major_opcode == major_opcode &&
  554. syncable->minor_opcode == minor_opcode) {
  555. on_sync = syncable;
  556. syncable->result = TRUE;
  557. break;
  558. } else if (major_opcode == G_XIM_ERROR &&
  559. minor_opcode == 0) {
  560. on_sync = syncable;
  561. syncable->result = FALSE;
  562. break;
  563. }
  564. }
  565. }
  566. G_UNLOCK (g_xim_protocol_syncable);
  567. closure = (GClosure *)g_xim_protocol_lookup_protocol_by_id(proto, major_opcode, minor_opcode);
  568. if (closure == NULL) {
  569. g_xim_messages_bug(iface->message,
  570. "Unknown opcode in the packets: major: %d, minor: %d",
  571. major_opcode, minor_opcode);
  572. } else {
  573. GValue v = { 0, }, *pv;
  574. guint n = 3, i;
  575. g_value_init(&v, G_TYPE_BOOLEAN);
  576. pv = g_new0(GValue, n);
  577. G_XIM_GERROR_CHECK_ALLOC (pv, error,
  578. G_XIM_PROTOCOL_ERROR, FALSE);
  579. g_value_init(&pv[0], G_TYPE_OBJECT);
  580. g_value_set_object(&pv[0], proto);
  581. g_value_init(&pv[1], G_TYPE_DATA_INPUT_STREAM);
  582. g_value_set_object(&pv[1], istream);
  583. g_value_init(&pv[2], G_TYPE_POINTER);
  584. g_value_set_pointer(&pv[2], error);
  585. g_closure_invoke(closure, &v, n, pv, NULL);
  586. for (i = 0; i < n; i++) {
  587. g_value_unset(&pv[i]);
  588. }
  589. ret = g_value_get_boolean(&v);
  590. if (!ret && *error == NULL) {
  591. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  592. G_XIM_PROTOCOL_ERROR_NO_PARSER | G_XIM_NOTICE_ERROR,
  593. "No parser available for %s",
  594. ((GXimProtocolClosure *)closure)->name);
  595. }
  596. g_free(pv);
  597. }
  598. end:
  599. if (istream)
  600. g_object_unref(istream);
  601. if (base_istream)
  602. g_object_unref(base_istream);
  603. if (*error) {
  604. if (on_sync)
  605. on_sync->error = g_error_copy(*error);
  606. }
  607. if (on_sync) {
  608. if (*error == NULL && !ret)
  609. g_set_error(&on_sync->error, G_XIM_PROTOCOL_ERROR,
  610. G_XIM_PROTOCOL_ERROR_DELIVERY_FAILURE | G_XIM_NOTICE_ERROR,
  611. "Unable to marshal a signal.");
  612. on_sync->result &= ret;
  613. G_LOCK (g_xim_protocol_syncable);
  614. g_queue_remove(priv->syncableq, on_sync);
  615. G_UNLOCK (g_xim_protocol_syncable);
  616. }
  617. length -= (packlen * 4 + 4);
  618. if (ret &&
  619. length > 4 &&
  620. ((gchar *)data)[(packlen * 4) + 4] != 0) {
  621. /* some IMs sends the multiple protocols in one request. */
  622. ret = g_xim_protocol_translate(proto, &((gchar *)data)[packlen * 4 + 4], length, error);
  623. }
  624. return ret; /* XXX */
  625. }
  626. #pragma GCC diagnostic error "-Wformat-extra-args" /* this might be wrong to get back the status */
  627. /**
  628. * g_xim_protocol_send_packets:
  629. * @proto: a #GXimProtocol.
  630. * @data: a chunk of XIM protocol packets.
  631. * @length: the number of @data.
  632. *
  633. * Sends @data to the opposite direction on the connection.
  634. *
  635. * Returns: %TRUE to be sent successfully.
  636. */
  637. gboolean
  638. g_xim_protocol_send_packets(GXimProtocol *proto,
  639. const gchar *data,
  640. gsize length)
  641. {
  642. GXimProtocolIface *iface;
  643. GXimTransport *trans;
  644. gboolean retval = FALSE;
  645. guint8 major, minor;
  646. gsize transport_size, transport_max;
  647. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
  648. g_return_val_if_fail (data != NULL, FALSE);
  649. trans = G_XIM_TRANSPORT (proto);
  650. g_return_val_if_fail (g_xim_transport_get_version(trans, &major, &minor), FALSE);
  651. iface = G_XIM_PROTOCOL_GET_IFACE (proto);
  652. transport_size = g_xim_transport_get_transport_size(trans);
  653. transport_max = g_xim_transport_get_transport_max(trans);
  654. switch (major) {
  655. case 0:
  656. switch (minor) {
  657. case 0:
  658. if (length > transport_size) {
  659. /* Send data via Property */
  660. retval = g_xim_transport_send_via_property(trans,
  661. data,
  662. length);
  663. } else {
  664. /* Send data via ClientMessage */
  665. retval = g_xim_transport_send_via_cm(trans,
  666. data,
  667. length,
  668. transport_size);
  669. }
  670. break;
  671. case 1:
  672. /* Send data via ClientMessage or multiple ClientMessages */
  673. retval = g_xim_transport_send_via_cm(trans,
  674. data,
  675. length,
  676. transport_size);
  677. break;
  678. case 2:
  679. if (length > transport_max) {
  680. /* Send data via Property */
  681. retval = g_xim_transport_send_via_property(trans,
  682. data,
  683. length);
  684. } else {
  685. /* Send data via ClientMessage or multiple ClientMessages */
  686. retval = g_xim_transport_send_via_cm(trans,
  687. data,
  688. length,
  689. transport_size);
  690. }
  691. break;
  692. default:
  693. g_xim_messages_warning(iface->message,
  694. "Unsupported protocol version: %d.%d",
  695. major, minor);
  696. break;
  697. }
  698. break;
  699. case 1:
  700. switch (minor) {
  701. case 0:
  702. /* Send data via PropertyNotify */
  703. retval = g_xim_transport_send_via_property_notify(trans,
  704. data,
  705. length);
  706. break;
  707. default:
  708. g_xim_messages_warning(iface->message,
  709. "Unsupported protocol version: %d.%d",
  710. major, minor);
  711. break;
  712. }
  713. break;
  714. case 2:
  715. switch (minor) {
  716. case 0:
  717. if (length > transport_size) {
  718. /* Send data via PropertyNotify */
  719. retval = g_xim_transport_send_via_property_notify(trans,
  720. data,
  721. length);
  722. } else {
  723. /* Send data via ClientMessage */
  724. retval = g_xim_transport_send_via_cm(trans,
  725. data,
  726. length,
  727. transport_size);
  728. }
  729. break;
  730. case 1:
  731. if (length > transport_max) {
  732. /* Send data via Property */
  733. retval = g_xim_transport_send_via_property_notify(trans,
  734. data,
  735. length);
  736. } else {
  737. /* Send data via ClientMessage or multiple ClientMessages */
  738. retval = g_xim_transport_send_via_cm(trans,
  739. data,
  740. length,
  741. transport_size);
  742. }
  743. break;
  744. default:
  745. g_xim_messages_warning(iface->message,
  746. "Unsupported protocol version: %d.%d",
  747. major, minor);
  748. break;
  749. }
  750. break;
  751. default:
  752. g_xim_messages_warning(iface->message,
  753. "Unsupported protocol version: %d.%d",
  754. major, minor);
  755. break;
  756. }
  757. return retval;
  758. }
  759. /**
  760. * g_xim_protocol_send_vformat:
  761. * @proto: a #GXimProtocol.
  762. * @cancellable: optional #GCancellable object, %NULL to ignore.
  763. * @error: a location to store error, or %NULL.
  764. * @n_params: the number of a set of parameters.
  765. * @args: a #va_list.
  766. *
  767. * Converts the objects to the XIM protocol packets according to format
  768. * and store it into the send buffer. you shouldn't use this function directly.
  769. * use g_xim_protocol_send() instead.
  770. *
  771. * Returns: the number of data stored.
  772. */
  773. gsize
  774. g_xim_protocol_send_vformat(GXimProtocol *proto,
  775. GCancellable *cancellable,
  776. GError **error,
  777. guint n_params,
  778. va_list args)
  779. {
  780. GXimProtocolIface *iface;
  781. GXimProtocolPrivate *priv;
  782. GXimTransport *trans;
  783. guint i, j;
  784. gsize n_sent, prev_size = 0;
  785. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), 0);
  786. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), 0);
  787. g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, 0);
  788. g_return_val_if_fail (error != NULL, 0);
  789. trans = G_XIM_TRANSPORT (proto);
  790. iface = G_XIM_PROTOCOL_GET_IFACE (proto);
  791. n_sent = priv->n_sent;
  792. for (i = 0; i < n_params; i++) {
  793. GXimValueType type, mtype;
  794. gchar *p;
  795. gsize sz, padding;
  796. GXimAttr *attr;
  797. goffset cur_pos = priv->n_sent;
  798. GXimProtocolMarker *marker_data;
  799. GSList *list, *l;
  800. gpointer value;
  801. type = va_arg(args, GXimValueType);
  802. switch (type) {
  803. case G_XIM_TYPE_PADDING:
  804. padding = va_arg(args, unsigned int);
  805. padding:
  806. for (sz = 0; sz < padding; sz++) {
  807. g_data_output_stream_put_byte(priv->send_ostream, 0,
  808. cancellable, error);
  809. if (*error)
  810. goto end;
  811. priv->n_sent++;
  812. }
  813. break;
  814. case G_XIM_TYPE_AUTO_PADDING:
  815. sz = va_arg(args, unsigned int);
  816. padding = g_xim_protocol_n_pad4(prev_size + sz);
  817. goto padding;
  818. case G_XIM_TYPE_MARKER_N_BYTES_1:
  819. mtype = va_arg(args, GXimValueType);
  820. marker_data = g_new0(GXimProtocolMarker, 1);
  821. G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
  822. G_XIM_PROTOCOL_ERROR, 0);
  823. marker_data->type = G_XIM_TYPE_BYTE;
  824. marker_data->where = mtype;
  825. marker_data->offset = g_seekable_tell(G_SEEKABLE (priv->base_send_ostream));
  826. g_queue_push_tail(priv->markerq, marker_data);
  827. g_data_output_stream_put_byte(priv->send_ostream, 0, NULL, NULL);
  828. priv->n_sent++;
  829. break;
  830. case G_XIM_TYPE_MARKER_N_BYTES_2:
  831. mtype = va_arg(args, GXimValueType);
  832. marker_data = g_new0(GXimProtocolMarker, 1);
  833. G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
  834. G_XIM_PROTOCOL_ERROR, 0);
  835. marker_data->type = G_XIM_TYPE_WORD;
  836. marker_data->where = mtype;
  837. marker_data->offset = g_seekable_tell(G_SEEKABLE (priv->base_send_ostream));
  838. g_queue_push_tail(priv->markerq, marker_data);
  839. g_data_output_stream_put_uint16(priv->send_ostream, 0,
  840. cancellable, error);
  841. priv->n_sent += 2;
  842. break;
  843. case G_XIM_TYPE_MARKER_N_BYTES_4:
  844. mtype = va_arg(args, GXimValueType);
  845. marker_data = g_new0(GXimProtocolMarker, 1);
  846. G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
  847. G_XIM_PROTOCOL_ERROR, 0);
  848. marker_data->type = G_XIM_TYPE_LONG;
  849. marker_data->where = mtype;
  850. marker_data->offset = g_seekable_tell(G_SEEKABLE (priv->base_send_ostream));
  851. g_queue_push_tail(priv->markerq, marker_data);
  852. g_data_output_stream_put_uint32(priv->send_ostream, 0,
  853. cancellable, error);
  854. priv->n_sent += 4;
  855. break;
  856. case G_XIM_TYPE_STR:
  857. value = va_arg(args, gpointer);
  858. priv->n_sent += g_xim_str_put_to_stream((GXimStr *)value,
  859. priv->send_ostream,
  860. cancellable, error);
  861. break;
  862. case G_XIM_TYPE_GSTRING:
  863. value = va_arg(args, gpointer);
  864. priv->n_sent += g_xim_gstring_put_to_stream((GString *)value,
  865. priv->send_ostream,
  866. cancellable, error);
  867. /* We don't want to include the length of characters for markers */
  868. cur_pos += 2;
  869. break;
  870. case G_XIM_TYPE_PREEDIT_CARET:
  871. value = va_arg(args, gpointer);
  872. priv->n_sent += g_xim_preedit_caret_put_to_stream((GXimPreeditCaret *)value,
  873. proto, cancellable, error);
  874. break;
  875. case G_XIM_TYPE_PREEDIT_DRAW:
  876. value = va_arg(args, gpointer);
  877. priv->n_sent += g_xim_preedit_draw_put_to_stream((GXimPreeditDraw *)value,
  878. proto, cancellable, error);
  879. break;
  880. case G_XIM_TYPE_GDKEVENT:
  881. value = va_arg(args, gpointer);
  882. priv->n_sent += g_xim_gdkevent_put_to_stream((GdkEvent *)value,
  883. proto, cancellable, error);
  884. break;
  885. case G_XIM_TYPE_XIMTEXT:
  886. value = va_arg(args, gpointer);
  887. priv->n_sent += g_xim_text_put_to_stream((GXimText *)value,
  888. proto, cancellable, error);
  889. break;
  890. case G_XIM_TYPE_HOTKEY_TRIGGER:
  891. value = va_arg(args, gpointer);
  892. priv->n_sent += g_xim_hotkey_trigger_put_to_stream((GXimHotkeyTrigger *)value,
  893. proto, cancellable, error);
  894. break;
  895. case G_XIM_TYPE_PIXMAP:
  896. value = va_arg(args, gpointer);
  897. g_data_output_stream_put_uint32(priv->send_ostream,
  898. g_xim_transport_get_native_channel_from(trans, value),
  899. cancellable,
  900. error);
  901. priv->n_sent += 4;
  902. break;
  903. case G_XIM_TYPE_STATUS_DRAW:
  904. value = va_arg(args, gpointer);
  905. priv->n_sent += g_xim_status_draw_put_to_stream((GXimStatusDraw *)value,
  906. proto, cancellable, error);
  907. break;
  908. case G_XIM_TYPE_LIST_OF_IMATTR:
  909. case G_XIM_TYPE_LIST_OF_ICATTR:
  910. list = va_arg(args, gpointer);
  911. for (l = list; l != NULL; l = g_slist_next(l)) {
  912. priv->n_sent += g_xim_raw_attr_put_to_stream(l->data, proto,
  913. cancellable, error);
  914. if (*error) {
  915. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  916. g_xim_messages_gerror(iface->message, *error);
  917. g_clear_error(error);
  918. } else {
  919. goto end;
  920. }
  921. }
  922. }
  923. break;
  924. case G_XIM_TYPE_LIST_OF_IMATTRIBUTE:
  925. if (!G_IS_XIM_IM_ATTR (G_XIM_CONNECTION (proto)->imattr)) {
  926. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  927. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  928. "Protocol order might be invalid. XIM_OPEN must be brought up first.");
  929. goto end;
  930. } else {
  931. attr = G_XIM_ATTR (G_XIM_CONNECTION (proto)->imattr);
  932. }
  933. goto process_attribute;
  934. case G_XIM_TYPE_LIST_OF_ICATTRIBUTE:
  935. if (!G_IS_XIM_IC_ATTR (G_XIM_CONNECTION (proto)->default_icattr)) {
  936. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  937. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  938. "Protocol order might be invalid. XIM_OPEN must be brought up first.");
  939. goto end;
  940. } else {
  941. attr = G_XIM_ATTR (G_XIM_CONNECTION (proto)->default_icattr);
  942. }
  943. process_attribute:
  944. list = va_arg(args, gpointer);
  945. for (l = list; l != NULL; l = g_slist_next(l)) {
  946. GXimAttribute *a = l->data;
  947. GXimValueType vt = g_xim_gtype_to_value_type(g_xim_attr_get_gtype_by_id(attr, a->id));
  948. if (a->vtype != vt) {
  949. g_xim_messages_warning(iface->message,
  950. "Unknown attribute ID is specified: %d (%s)",
  951. a->id, g_xim_value_type_name(a->vtype));
  952. }
  953. priv->n_sent += g_xim_attribute_put_to_stream(l->data, proto,
  954. cancellable, error);
  955. if (*error) {
  956. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  957. g_xim_messages_gerror(iface->message, *error);
  958. g_clear_error(error);
  959. } else {
  960. goto end;
  961. }
  962. }
  963. }
  964. break;
  965. case G_XIM_TYPE_LIST_OF_EXT:
  966. list = va_arg(args, gpointer);
  967. for (l = list; l != NULL; l = g_slist_next(l)) {
  968. priv->n_sent += g_xim_ext_put_to_stream(l->data,
  969. proto,
  970. cancellable,
  971. error);
  972. if (*error) {
  973. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  974. g_xim_messages_gerror(iface->message, *error);
  975. g_clear_error(error);
  976. } else {
  977. goto end;
  978. }
  979. }
  980. }
  981. break;
  982. case G_XIM_TYPE_LIST_OF_STRING:
  983. list = va_arg(args, GSList *);
  984. for (l = list; l != NULL; l = g_slist_next(l)) {
  985. priv->n_sent += g_xim_string_put_to_stream(l->data,
  986. priv->send_ostream,
  987. cancellable,
  988. error);
  989. if (*error) {
  990. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  991. g_xim_messages_gerror(iface->message, *error);
  992. g_clear_error(error);
  993. } else {
  994. goto end;
  995. }
  996. }
  997. }
  998. break;
  999. case G_XIM_TYPE_LIST_OF_STR:
  1000. list = va_arg(args, GSList *);
  1001. for (l = list; l != NULL; l = g_slist_next(l)) {
  1002. priv->n_sent += g_xim_str_put_to_stream(l->data,
  1003. priv->send_ostream,
  1004. cancellable,
  1005. error);
  1006. if (*error) {
  1007. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1008. g_xim_messages_gerror(iface->message, *error);
  1009. g_clear_error(error);
  1010. } else {
  1011. goto end;
  1012. }
  1013. }
  1014. }
  1015. break;
  1016. case G_XIM_TYPE_LIST_OF_ENCODINGINFO:
  1017. list = va_arg(args, GSList *);
  1018. for (l = list; l != NULL; l = g_slist_next(l)) {
  1019. priv->n_sent += g_xim_encodinginfo_put_to_stream(l->data,
  1020. priv->send_ostream,
  1021. cancellable,
  1022. error);
  1023. if (*error) {
  1024. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1025. g_xim_messages_gerror(iface->message, *error);
  1026. g_clear_error(error);
  1027. } else {
  1028. goto end;
  1029. }
  1030. }
  1031. }
  1032. break;
  1033. case G_XIM_TYPE_LIST_OF_BYTE:
  1034. value = va_arg(args, gpointer);
  1035. for (j = 0; j < ((GString *)value)->len; j++) {
  1036. g_data_output_stream_put_byte(priv->send_ostream,
  1037. ((GString *)value)->str[j],
  1038. cancellable, error);
  1039. priv->n_sent++;
  1040. if (*error)
  1041. goto end;
  1042. }
  1043. break;
  1044. case G_XIM_TYPE_LIST_OF_CARD16:
  1045. list = va_arg(args, GSList *);
  1046. for (l = list; l != NULL; l = g_slist_next(l)) {
  1047. g_data_output_stream_put_uint16(priv->send_ostream,
  1048. GPOINTER_TO_UINT (l->data),
  1049. cancellable,
  1050. error);
  1051. priv->n_sent += 2;
  1052. if (*error) {
  1053. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1054. g_xim_messages_gerror(iface->message, *error);
  1055. g_clear_error(error);
  1056. } else {
  1057. goto end;
  1058. }
  1059. }
  1060. }
  1061. break;
  1062. case G_XIM_TYPE_LIST_OF_HOTKEY_TRIGGER:
  1063. list = va_arg(args, GSList *);
  1064. for (l = list; l != NULL; l = g_slist_next(l)) {
  1065. priv->n_sent += g_xim_hotkey_trigger_put_to_stream(l->data,
  1066. proto,
  1067. cancellable,
  1068. error);
  1069. if (*error) {
  1070. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1071. g_xim_messages_gerror(iface->message, *error);
  1072. g_clear_error(error);
  1073. } else {
  1074. goto end;
  1075. }
  1076. }
  1077. }
  1078. break;
  1079. case G_XIM_TYPE_SEPARATOR:
  1080. break;
  1081. case G_XIM_TYPE_BYTE:
  1082. g_data_output_stream_put_byte(priv->send_ostream,
  1083. (guint8)va_arg(args, int),
  1084. cancellable, error);
  1085. priv->n_sent++;
  1086. break;
  1087. case G_XIM_TYPE_WORD:
  1088. g_data_output_stream_put_uint16(priv->send_ostream,
  1089. (guint16)va_arg(args, unsigned int),
  1090. cancellable, error);
  1091. priv->n_sent += 2;
  1092. break;
  1093. case G_XIM_TYPE_LONG:
  1094. g_data_output_stream_put_uint32(priv->send_ostream,
  1095. (guint32)va_arg(args, unsigned long),
  1096. cancellable, error);
  1097. priv->n_sent += 4;
  1098. break;
  1099. case G_XIM_TYPE_CHAR:
  1100. p = va_arg(args, gchar *);
  1101. for (; p != NULL && *p != 0; p++) {
  1102. g_data_output_stream_put_byte(priv->send_ostream, *p,
  1103. cancellable, error);
  1104. priv->n_sent++;
  1105. if (*error)
  1106. goto end;
  1107. }
  1108. break;
  1109. case G_XIM_TYPE_WINDOW:
  1110. value = va_arg(args, gpointer);
  1111. g_data_output_stream_put_uint32(priv->send_ostream,
  1112. g_xim_transport_get_native_channel_from(trans, value),
  1113. cancellable,
  1114. error);
  1115. priv->n_sent += 4;
  1116. break;
  1117. case G_XIM_TYPE_XIMSTYLES:
  1118. value = va_arg(args, gpointer);
  1119. priv->n_sent += g_xim_styles_put_to_stream((GXimStyles *)value,
  1120. proto,
  1121. cancellable,
  1122. error);
  1123. break;
  1124. case G_XIM_TYPE_XRECTANGLE:
  1125. priv->n_sent += g_xim_rectangle_put_to_stream(va_arg(args, GXimRectangle *),
  1126. proto,
  1127. cancellable,
  1128. error);
  1129. break;
  1130. case G_XIM_TYPE_XPOINT:
  1131. value = va_arg(args, gpointer);
  1132. priv->n_sent += g_xim_point_put_to_stream((GXimPoint *)value,
  1133. proto,
  1134. cancellable,
  1135. error);
  1136. break;
  1137. case G_XIM_TYPE_XFONTSET:
  1138. value = va_arg(args, gpointer);
  1139. priv->n_sent += g_xim_fontset_put_to_stream((GXimFontSet *)value,
  1140. proto,
  1141. cancellable,
  1142. error);
  1143. break;
  1144. case G_XIM_TYPE_HOTKEYTRIGGERS:
  1145. case G_XIM_TYPE_HOTKEYSTATE:
  1146. case G_XIM_TYPE_STRINGCONVERSION:
  1147. case G_XIM_TYPE_PREEDITSTATE:
  1148. case G_XIM_TYPE_RESETSTATE:
  1149. goto nonsupported;
  1150. case G_XIM_TYPE_NESTEDLIST:
  1151. value = va_arg(args, gpointer);
  1152. priv->n_sent += g_xim_nested_list_put_to_stream((GXimNestedList *)value,
  1153. proto,
  1154. cancellable,
  1155. error);
  1156. break;
  1157. default:
  1158. nonsupported:
  1159. g_xim_messages_warning(iface->message,
  1160. "Unknown/unsupported value type to send packets: %s",
  1161. g_xim_value_type_name(type));
  1162. #ifdef GNOME_ENABLE_DEBUG
  1163. abort();
  1164. #endif
  1165. break;
  1166. }
  1167. if (!g_queue_is_empty(priv->markerq)) {
  1168. marker_data = g_queue_peek_tail(priv->markerq);
  1169. if (marker_data->where == type) {
  1170. goffset cur = g_seekable_tell(G_SEEKABLE (priv->base_send_ostream));
  1171. gsize save_n_sent = priv->n_sent;
  1172. g_seekable_seek(G_SEEKABLE (priv->base_send_ostream),
  1173. marker_data->offset, G_SEEK_SET, NULL, NULL);
  1174. g_xim_protocol_send_format(proto, cancellable, error, 1,
  1175. marker_data->type, cur - cur_pos);
  1176. priv->n_sent = save_n_sent;
  1177. g_seekable_seek(G_SEEKABLE (priv->base_send_ostream),
  1178. cur, G_SEEK_SET, NULL, NULL);
  1179. g_free(g_queue_pop_tail(priv->markerq));
  1180. }
  1181. }
  1182. prev_size = priv->n_sent - cur_pos;
  1183. if (*error) {
  1184. if (!G_XIM_GERROR_IS_RECOVERABLE (*error))
  1185. goto end;
  1186. g_xim_messages_gerror(iface->message, *error);
  1187. g_clear_error(error);
  1188. }
  1189. }
  1190. end:
  1191. if (*error)
  1192. return 0;
  1193. return priv->n_sent - n_sent;
  1194. }
  1195. /**
  1196. * g_xim_protocol_send_format:
  1197. * @proto: a #GXimProtocol.
  1198. * @cancellable: optional #GCancellable object, %NULL to ignore.
  1199. * @error: a location to store error, or %NULL.
  1200. * @n_params: the number of a set of parameters.
  1201. *
  1202. * Converts the objects to the XIM protocol packets according to the format
  1203. * and store it into the send buffer. you shouldn't use this function directly.
  1204. * use g_xim_protocol_send() instead.
  1205. *
  1206. * Returns: the number of data stored.
  1207. */
  1208. gsize
  1209. g_xim_protocol_send_format(GXimProtocol *proto,
  1210. GCancellable *cancellable,
  1211. GError **error,
  1212. guint n_params,
  1213. ...)
  1214. {
  1215. va_list ap;
  1216. gsize retval;
  1217. va_start(ap, n_params);
  1218. retval = g_xim_protocol_send_vformat(proto,
  1219. cancellable,
  1220. error,
  1221. n_params, ap);
  1222. va_end(ap);
  1223. return retval;
  1224. }
  1225. /**
  1226. * g_xim_protocol_send:
  1227. * @proto: a #GXimProtocol.
  1228. * @major_opcode: a major opcode in XIM protocol.
  1229. * @minor_opcode: a minor opcode in XIM protocol.
  1230. * @n_params: the number of a set of parameters.
  1231. *
  1232. * Sends XIM protocol according to the set of parameters. one is a parameter
  1233. * type defined in #GXimValueType, and one is an object related to that.
  1234. * See #GXimValueType documentation which objects are supposed to be specified
  1235. * with them.
  1236. *
  1237. * Returns: %TRUE to be sent successfully.
  1238. */
  1239. gboolean
  1240. g_xim_protocol_send(GXimProtocol *proto,
  1241. GXimOpcode major_opcode,
  1242. guint8 minor_opcode,
  1243. guint n_params,
  1244. ...)
  1245. {
  1246. GXimProtocolPrivate *priv;
  1247. va_list ap;
  1248. gpointer data;
  1249. gboolean retval = FALSE;
  1250. gsize osize;
  1251. GError *error = NULL;
  1252. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
  1253. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
  1254. g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, FALSE);
  1255. va_start(ap, n_params);
  1256. if (!g_xim_protocol_send_header(proto, major_opcode, minor_opcode,
  1257. NULL, NULL))
  1258. goto end;
  1259. g_xim_protocol_send_vformat(proto, NULL, &error, n_params, ap);
  1260. if (error) {
  1261. g_xim_messages_gerror(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
  1262. error);
  1263. if (!G_XIM_GERROR_IS_RECOVERABLE (error))
  1264. goto end;
  1265. g_clear_error(&error);
  1266. }
  1267. if ((osize = g_xim_protocol_send_fixate_size(proto, NULL, &error)) == 0)
  1268. goto end;
  1269. data = g_memory_output_stream_get_data(G_MEMORY_OUTPUT_STREAM (priv->base_send_ostream));
  1270. if (g_queue_get_length(priv->sendq) > 0) {
  1271. GXimProtocolQueueNode *node = g_queue_peek_tail(priv->sendq);
  1272. if (node->data == NULL) {
  1273. /* if it's the queueing mode, do not send the packet */
  1274. g_xim_messages_debug(G_XIM_PROTOCOL_GET_IFACE (proto)->message, "proto/event",
  1275. "Queued the packet for %s",
  1276. g_xim_protocol_name(major_opcode));
  1277. node->data = g_new0(gchar, osize + 1);
  1278. memcpy(node->data, data, osize);
  1279. node->length = osize;
  1280. retval = TRUE;
  1281. goto end;
  1282. }
  1283. }
  1284. retval = g_xim_protocol_send_packets(proto, data, osize);
  1285. end:
  1286. if (error)
  1287. g_error_free(error);
  1288. retval &= g_xim_protocol_send_terminate(proto, NULL, NULL);
  1289. va_end(ap);
  1290. return retval;
  1291. }
  1292. /**
  1293. * g_xim_protocol_send_with_list:
  1294. * @proto: a #GXimProtocol.
  1295. * @major_opcode: a major opcode in XIM protocol.
  1296. * @minor_opcode: a minor opcode in XIM protocol.
  1297. * @types: a list of #GXimValueType.
  1298. * @values: a list of objects fitting to each value types in @types.
  1299. *
  1300. * Sends XIM protocol according to @types and @values.
  1301. * See #GXimValueType documentation which objects are supposed to be specified
  1302. * with them.
  1303. *
  1304. * Returns: %TRUE to be sent successfully.
  1305. */
  1306. gboolean
  1307. g_xim_protocol_send_with_list(GXimProtocol *proto,
  1308. GXimOpcode major_opcode,
  1309. guint8 minor_opcode,
  1310. GSList *types,
  1311. GSList *values)
  1312. {
  1313. GXimProtocolPrivate *priv;
  1314. GSList *lt, *lv;
  1315. gpointer data, value;
  1316. gboolean retval = FALSE;
  1317. gsize osize, prev_size = 0;
  1318. GError *error = NULL;
  1319. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
  1320. g_return_val_if_fail (types != NULL, FALSE);
  1321. g_return_val_if_fail (values != NULL, FALSE);
  1322. g_return_val_if_fail (g_slist_length(types) == g_slist_length(values), FALSE);
  1323. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
  1324. g_return_val_if_fail (priv->byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN, FALSE);
  1325. if (!g_xim_protocol_send_header(proto, major_opcode, minor_opcode,
  1326. NULL, NULL))
  1327. goto end;
  1328. for (lt = types, lv = values;
  1329. lt != NULL && lv != NULL;
  1330. lt = g_slist_next(lt), lv = g_slist_next(lv)) {
  1331. value = lv->data;
  1332. if (GPOINTER_TO_UINT (lt->data) == G_XIM_TYPE_AUTO_PADDING) {
  1333. /* We have to deal with it here */
  1334. value = GUINT_TO_POINTER (GPOINTER_TO_UINT (value) + prev_size);
  1335. }
  1336. prev_size = g_xim_protocol_send_format(proto, NULL, &error, 1, lt->data, value);
  1337. if (GPOINTER_TO_UINT (lt->data) == G_XIM_TYPE_GSTRING) {
  1338. /* XXX: hack to adjust the previous size... */
  1339. prev_size -= 2;
  1340. }
  1341. if (error) {
  1342. g_xim_messages_gerror(G_XIM_PROTOCOL_GET_IFACE (proto)->message, error);
  1343. if (!G_XIM_GERROR_IS_RECOVERABLE (error))
  1344. goto end;
  1345. g_clear_error(&error);
  1346. }
  1347. }
  1348. if ((osize = g_xim_protocol_send_fixate_size(proto, NULL, &error)) == 0)
  1349. goto end;
  1350. data = g_memory_output_stream_get_data(G_MEMORY_OUTPUT_STREAM (priv->base_send_ostream));
  1351. if (g_queue_get_length(priv->sendq) > 0) {
  1352. GXimProtocolQueueNode *node = g_queue_peek_tail(priv->sendq);
  1353. if (node->data == NULL) {
  1354. /* if it's the queueing mode, do not send the packet */
  1355. g_xim_messages_debug(G_XIM_PROTOCOL_GET_IFACE (proto)->message, "proto/event",
  1356. "Queued the packet for %s",
  1357. g_xim_protocol_name(major_opcode));
  1358. node->data = g_new0(gchar, osize + 1);
  1359. memcpy(node->data, data, osize);
  1360. node->length = osize;
  1361. retval = TRUE;
  1362. goto end;
  1363. }
  1364. }
  1365. retval = g_xim_protocol_send_packets(proto, data, osize);
  1366. end:
  1367. if (error)
  1368. g_error_free(error);
  1369. retval &= g_xim_protocol_send_terminate(proto, NULL, NULL);
  1370. return retval;
  1371. }
  1372. /**
  1373. * g_xim_protocol_start_queue:
  1374. * @proto: a #GXimProtocol.
  1375. *
  1376. * Prepares the queue to not send a request immediately. this function is
  1377. * useful if you need to wait for any requests but want to send it after
  1378. * the pending requests is done.
  1379. *
  1380. * Returns: %TRUE to be ready to queue.
  1381. */
  1382. gboolean
  1383. g_xim_protocol_start_queue(GXimProtocol *proto)
  1384. {
  1385. GXimProtocolPrivate *priv;
  1386. GXimProtocolQueueNode *node;
  1387. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
  1388. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
  1389. node = g_new0(GXimProtocolQueueNode, 1);
  1390. G_XIM_CHECK_ALLOC (node, FALSE);
  1391. g_queue_push_tail(priv->sendq, node);
  1392. return TRUE;
  1393. }
  1394. /**
  1395. * g_xim_protocol_end_queue:
  1396. * @proto: a #GXimProtocol.
  1397. *
  1398. * Terminates the queue.
  1399. *
  1400. * Returns: the #GXimProtocolQueueNode which queued the request this time.
  1401. */
  1402. GXimProtocolQueueNode *
  1403. g_xim_protocol_end_queue(GXimProtocol *proto)
  1404. {
  1405. GXimProtocolPrivate *priv;
  1406. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), NULL);
  1407. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), NULL);
  1408. return g_queue_peek_tail(priv->sendq);
  1409. }
  1410. /**
  1411. * g_xim_protocol_cancel_queue:
  1412. * @proto: a #GXimProtocol.
  1413. *
  1414. * Cancels queueing. Note that you have to invoke this function if you can't
  1415. * proceed queueing process with any errors say. otherwise invalid node is kept
  1416. * in the queue and you may get a trouble with it eveutnally.
  1417. */
  1418. void
  1419. g_xim_protocol_cancel_queue(GXimProtocol *proto)
  1420. {
  1421. GXimProtocolPrivate *priv;
  1422. g_return_if_fail (G_IS_XIM_PROTOCOL (proto));
  1423. g_return_if_fail ((priv = g_xim_protocol_get_private(proto)));
  1424. g_return_if_fail (g_xim_protocol_is_queued(proto));
  1425. g_free(g_queue_pop_tail(priv->sendq));
  1426. }
  1427. /**
  1428. * g_xim_protocol_is_queued:
  1429. * @proto: a #GXimProtocol.
  1430. *
  1431. * Checks if @proto is in queueing process.
  1432. *
  1433. * Returns: %TRUE to be in the queueing process. otherwise %FALSE.
  1434. */
  1435. gboolean
  1436. g_xim_protocol_is_queued(GXimProtocol *proto)
  1437. {
  1438. GXimProtocolPrivate *priv;
  1439. GXimProtocolQueueNode *node;
  1440. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
  1441. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
  1442. node = g_queue_peek_tail(priv->sendq);
  1443. return node->data == NULL;
  1444. }
  1445. /**
  1446. * g_xim_protocol_get_queue_length:
  1447. * @proto: a #GXimProtocol.
  1448. *
  1449. * Obtains how many requests are stored in the queue now.
  1450. *
  1451. * Returns: the number of requests in the queue.
  1452. */
  1453. guint
  1454. g_xim_protocol_get_queue_length(GXimProtocol *proto)
  1455. {
  1456. GXimProtocolPrivate *priv;
  1457. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), 0);
  1458. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), 0);
  1459. return g_queue_get_length(priv->sendq);
  1460. }
  1461. /**
  1462. * g_xim_protocol_read_vformat:
  1463. * @proto: a #GXimProtocol.
  1464. * @stream: a #GDataInputStream which contains XIM protocol packets.
  1465. * @cancellable: optional #GCancellable object, %NULL to ignore.
  1466. * @error: a location to store error, or %NULL.
  1467. * @n_params: the number of a set of parameters.
  1468. * @args: a #va_list.
  1469. *
  1470. * Scans XIM protocol packets according to the set of parameters. if any,
  1471. * objects generated against it is stored in the locations pointed to by
  1472. * the pointer arguments. Each pointer argument must be of a type that is
  1473. * appropriate for the value returned by the corresponding value type.
  1474. * See #GXimValueType documentation which objects the value type is supposed
  1475. * to be.
  1476. *
  1477. * Returns: %TRUE to be read successfully.
  1478. */
  1479. gboolean
  1480. g_xim_protocol_read_vformat(GXimProtocol *proto,
  1481. GDataInputStream *stream,
  1482. GCancellable *cancellable,
  1483. GError **error,
  1484. guint n_params,
  1485. va_list args)
  1486. {
  1487. GXimProtocolPrivate *priv;
  1488. GXimProtocolIface *iface;
  1489. guint i;
  1490. GInputStream *istream;
  1491. gboolean retval = TRUE;
  1492. goffset base_pos;
  1493. GXimProtocolMarker *marker_data = NULL;
  1494. GQueue *markerq;
  1495. GXimValueType vtype = G_XIM_TYPE_INVALID;
  1496. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
  1497. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), FALSE);
  1498. g_return_val_if_fail (G_IS_DATA_INPUT_STREAM (stream), FALSE);
  1499. g_return_val_if_fail (error != NULL, FALSE);
  1500. iface = G_XIM_PROTOCOL_GET_IFACE (proto);
  1501. markerq = g_queue_new();
  1502. G_XIM_GERROR_CHECK_ALLOC (markerq, error, G_XIM_PROTOCOL_ERROR, FALSE);
  1503. istream = g_filter_input_stream_get_base_stream(G_FILTER_INPUT_STREAM (stream));
  1504. base_pos = g_seekable_tell(G_SEEKABLE (istream));
  1505. for (i = 0; i < n_params; i++) {
  1506. GXimValueType mtype;
  1507. gpointer value;
  1508. gsize padding, sz, size;
  1509. goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream));
  1510. GType gtype G_GNUC_UNUSED;
  1511. GXimAttr *attr;
  1512. GdkNativeWindow nw;
  1513. vtype = va_arg(args, GXimValueType);
  1514. marker_data = g_xim_protocol_find_marker(markerq, vtype);
  1515. switch (vtype) {
  1516. case G_XIM_TYPE_PADDING:
  1517. padding = va_arg(args, unsigned int);
  1518. padding:
  1519. if (marker_data) {
  1520. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1521. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1522. "Invalid the length-of-data marker for value type %s",
  1523. g_xim_value_type_name(vtype));
  1524. goto end;
  1525. }
  1526. if (!g_seekable_seek(G_SEEKABLE (istream), padding,
  1527. G_SEEK_CUR, cancellable, error)) {
  1528. goto end;
  1529. }
  1530. break;
  1531. case G_XIM_TYPE_AUTO_PADDING:
  1532. if (marker_data) {
  1533. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1534. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1535. "Invalid the length-of-data marker for value type %s",
  1536. g_xim_value_type_name(vtype));
  1537. goto end;
  1538. }
  1539. sz = va_arg(args, unsigned int);
  1540. padding = g_xim_protocol_n_pad4(cur_pos - base_pos + sz);
  1541. goto padding;
  1542. case G_XIM_TYPE_MARKER_N_BYTES_1:
  1543. if (marker_data) {
  1544. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1545. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1546. "Invalid the length-of-data marker for value type %s",
  1547. g_xim_value_type_name(vtype));
  1548. goto end;
  1549. }
  1550. mtype = va_arg(args, GXimValueType);
  1551. marker_data = g_new0(GXimProtocolMarker, 1);
  1552. G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
  1553. G_XIM_PROTOCOL_ERROR, FALSE);
  1554. marker_data->type = G_XIM_TYPE_BYTE;
  1555. marker_data->where = mtype;
  1556. marker_data->value.b = g_data_input_stream_read_byte(stream, cancellable, error);
  1557. g_queue_push_tail(markerq, marker_data);
  1558. marker_data = NULL;
  1559. break;
  1560. case G_XIM_TYPE_MARKER_N_BYTES_2:
  1561. if (marker_data) {
  1562. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1563. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1564. "Invalid the length-of-data marker for value type %s",
  1565. g_xim_value_type_name(vtype));
  1566. goto end;
  1567. }
  1568. mtype = va_arg(args, GXimValueType);
  1569. marker_data = g_new0(GXimProtocolMarker, 1);
  1570. G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
  1571. G_XIM_PROTOCOL_ERROR, FALSE);
  1572. marker_data->type = G_XIM_TYPE_WORD;
  1573. marker_data->where = mtype;
  1574. marker_data->value.w = g_data_input_stream_read_uint16(stream, cancellable, error);
  1575. g_queue_push_tail(markerq, marker_data);
  1576. marker_data = NULL;
  1577. break;
  1578. case G_XIM_TYPE_MARKER_N_BYTES_4:
  1579. if (marker_data) {
  1580. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1581. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1582. "Invalid the length-of-data marker for value type %s",
  1583. g_xim_value_type_name(vtype));
  1584. goto end;
  1585. }
  1586. mtype = va_arg(args, GXimValueType);
  1587. marker_data = g_new0(GXimProtocolMarker, 1);
  1588. G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
  1589. G_XIM_PROTOCOL_ERROR, FALSE);
  1590. marker_data->type = G_XIM_TYPE_LONG;
  1591. marker_data->where = mtype;
  1592. marker_data->value.l = g_data_input_stream_read_uint32(stream, cancellable, error);
  1593. g_queue_push_tail(markerq, marker_data);
  1594. marker_data = NULL;
  1595. break;
  1596. case G_XIM_TYPE_MARKER_N_ITEMS_2:
  1597. if (marker_data) {
  1598. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1599. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1600. "Invalid the length-of-data marker for value type %s",
  1601. g_xim_value_type_name(vtype));
  1602. goto end;
  1603. }
  1604. mtype = va_arg(args, GXimValueType);
  1605. marker_data = g_new0(GXimProtocolMarker, 1);
  1606. G_XIM_GERROR_CHECK_ALLOC (marker_data, error,
  1607. G_XIM_PROTOCOL_ERROR, FALSE);
  1608. marker_data->type = vtype;
  1609. marker_data->where = mtype;
  1610. marker_data->value.w = g_data_input_stream_read_uint16(stream, cancellable, error);
  1611. g_queue_push_tail(markerq, marker_data);
  1612. marker_data = NULL;
  1613. break;
  1614. case G_XIM_TYPE_STR:
  1615. value = va_arg(args, gpointer);
  1616. if (value == NULL) {
  1617. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1618. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1619. "value type %s requires an argument to store the result.",
  1620. g_xim_value_type_name(vtype));
  1621. goto end;
  1622. }
  1623. *((GXimStr **)value) = g_xim_str_get_from_stream(stream, NULL, error);
  1624. break;
  1625. case G_XIM_TYPE_GSTRING:
  1626. value = va_arg(args, gpointer);
  1627. if (value == NULL) {
  1628. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1629. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1630. "value type %s requires an argument to store the result.",
  1631. g_xim_value_type_name(vtype));
  1632. goto end;
  1633. }
  1634. /* we don't want to include the length of characters for markers */
  1635. cur_pos += 2;
  1636. *((GString **)value) = g_xim_gstring_get_from_stream(stream, NULL, error);
  1637. break;
  1638. case G_XIM_TYPE_PREEDIT_CARET:
  1639. value = va_arg(args, gpointer);
  1640. if (value == NULL) {
  1641. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1642. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1643. "value type %s requires an argument to store the result.",
  1644. g_xim_value_type_name(vtype));
  1645. goto end;
  1646. }
  1647. *((GXimPreeditCaret **)value) = g_xim_preedit_caret_get_from_stream(proto, stream, cancellable, error);
  1648. break;
  1649. case G_XIM_TYPE_PREEDIT_DRAW:
  1650. value = va_arg(args, gpointer);
  1651. if (value == NULL) {
  1652. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1653. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1654. "value type %s requires an argument to store the result.",
  1655. g_xim_value_type_name(vtype));
  1656. goto end;
  1657. }
  1658. *((GXimPreeditDraw **)value) = g_xim_preedit_draw_get_from_stream(proto, stream, cancellable, error);
  1659. break;
  1660. case G_XIM_TYPE_GDKEVENT:
  1661. value = va_arg(args, gpointer);
  1662. if (value == NULL) {
  1663. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1664. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1665. "value type %s requires an argument to store the result.",
  1666. g_xim_value_type_name(vtype));
  1667. goto end;
  1668. }
  1669. *((GdkEvent **)value) = g_xim_gdkevent_get_from_stream(proto, stream, cancellable, error);
  1670. break;
  1671. case G_XIM_TYPE_XIMTEXT:
  1672. value = va_arg(args, gpointer);
  1673. if (value == NULL) {
  1674. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1675. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1676. "value type %s requires an argument to store the result.",
  1677. g_xim_value_type_name(vtype));
  1678. goto end;
  1679. }
  1680. *((GXimText **)value) = g_xim_text_get_from_stream(proto, stream, cancellable, error);
  1681. break;
  1682. case G_XIM_TYPE_HOTKEY_TRIGGER:
  1683. value = va_arg(args, gpointer);
  1684. if (value == NULL) {
  1685. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1686. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1687. "value type %s requires an argument to store the result.",
  1688. g_xim_value_type_name(vtype));
  1689. goto end;
  1690. }
  1691. *((GXimHotkeyTrigger **)value) = g_xim_hotkey_trigger_get_from_stream(proto, stream, cancellable, error);
  1692. break;
  1693. case G_XIM_TYPE_PIXMAP:
  1694. value = va_arg(args, gpointer);
  1695. if (value == NULL) {
  1696. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1697. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1698. "value type %s requires an argument to store the result.",
  1699. g_xim_value_type_name(vtype));
  1700. goto end;
  1701. }
  1702. nw = g_data_input_stream_read_uint32(stream, cancellable, error);
  1703. *((GdkPixmap **)value) = g_xim_get_pixmap(g_xim_transport_get_display(G_XIM_TRANSPORT (proto)),
  1704. nw);
  1705. if (*((GdkPixmap **)value) == NULL) {
  1706. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1707. G_XIM_PROTOCOL_ERROR_NO_DATA | G_XIM_NOTICE_ERROR,
  1708. "Unable to get a valid pixmap for %p",
  1709. G_XIM_NATIVE_WINDOW_TO_POINTER (nw));
  1710. goto end;
  1711. }
  1712. break;
  1713. case G_XIM_TYPE_STATUS_DRAW:
  1714. value = va_arg(args, gpointer);
  1715. if (value == NULL) {
  1716. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1717. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1718. "value type %s requires an argument to store the result.",
  1719. g_xim_value_type_name(vtype));
  1720. goto end;
  1721. }
  1722. *((GXimStatusDraw **)value) = g_xim_status_draw_get_from_stream(proto, stream, cancellable, error);
  1723. break;
  1724. case G_XIM_TYPE_LIST_OF_IMATTR:
  1725. gtype = G_TYPE_XIM_IM_ATTR;
  1726. goto process_attr;
  1727. case G_XIM_TYPE_LIST_OF_ICATTR:
  1728. gtype = G_TYPE_XIM_IC_ATTR;
  1729. process_attr:
  1730. value = va_arg(args, gpointer);
  1731. if (value == NULL) {
  1732. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1733. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1734. "value type %s requires an argument to store the result.",
  1735. g_xim_value_type_name(vtype));
  1736. goto end;
  1737. }
  1738. if (marker_data == NULL) {
  1739. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1740. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1741. "No the markers for value type %s to determine how much the number of chunk available",
  1742. g_xim_value_type_name(vtype));
  1743. goto end;
  1744. }
  1745. if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
  1746. size = G_XIM_GET_MARKER_DATA (marker_data);
  1747. g_free(marker_data);
  1748. marker_data = NULL;
  1749. for (sz = 0; sz < size; sz++) {
  1750. GXimRawAttr *a;
  1751. a = g_xim_raw_attr_get_from_stream(proto, stream, cancellable, error);
  1752. if (*error) {
  1753. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1754. g_prefix_error(error, "Error while processing a value type %s: ",
  1755. g_xim_value_type_name(vtype));
  1756. g_xim_messages_gerror(iface->message, *error);
  1757. g_clear_error(error);
  1758. } else {
  1759. goto end;
  1760. }
  1761. }
  1762. *((GSList **)value) = g_slist_append(*((GSList **)value), a);
  1763. }
  1764. } else {
  1765. size = G_XIM_GET_MARKER_DATA (marker_data);
  1766. g_free(marker_data);
  1767. marker_data = NULL;
  1768. while (size > 0) {
  1769. goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
  1770. GXimRawAttr *a;
  1771. a = g_xim_raw_attr_get_from_stream(proto, stream, cancellable, error);
  1772. if (*error) {
  1773. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1774. g_prefix_error(error, "Error while processing a value type %s: ",
  1775. g_xim_value_type_name(vtype));
  1776. g_xim_messages_gerror(iface->message, *error);
  1777. g_clear_error(error);
  1778. } else {
  1779. goto end;
  1780. }
  1781. }
  1782. pos = g_seekable_tell(G_SEEKABLE (istream));
  1783. if ((pos - cur_pos) > size) {
  1784. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1785. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  1786. "Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
  1787. g_xim_value_type_name(vtype), size, pos - cur_pos);
  1788. goto end;
  1789. }
  1790. *((GSList **)value) = g_slist_append(*((GSList **)value), a);
  1791. size -= (pos - cur_pos);
  1792. }
  1793. }
  1794. break;
  1795. case G_XIM_TYPE_LIST_OF_IMATTRIBUTE:
  1796. if (!G_IS_XIM_IM_ATTR (G_XIM_CONNECTION (proto)->imattr)) {
  1797. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1798. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  1799. "Protocol order might be invalid. XIM_OPEN must be brought up first.");
  1800. goto end;
  1801. } else {
  1802. attr = G_XIM_ATTR (G_XIM_CONNECTION (proto)->imattr);
  1803. }
  1804. goto process_attribute;
  1805. case G_XIM_TYPE_LIST_OF_ICATTRIBUTE:
  1806. if (!G_IS_XIM_IC_ATTR (G_XIM_CONNECTION (proto)->default_icattr)) {
  1807. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1808. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  1809. "Protocol order might be invalid. XIM_OPEN must be brought up first.");
  1810. goto end;
  1811. } else {
  1812. attr = G_XIM_ATTR (G_XIM_CONNECTION (proto)->default_icattr);
  1813. }
  1814. process_attribute:
  1815. value = va_arg(args, gpointer);
  1816. if (value == NULL) {
  1817. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1818. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1819. "value type %s requires an argument to store the result.",
  1820. g_xim_value_type_name(vtype));
  1821. goto end;
  1822. }
  1823. if (marker_data == NULL) {
  1824. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1825. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1826. "No the markers for value type %s to determine how much the number of chunk available",
  1827. g_xim_value_type_name(vtype));
  1828. goto end;
  1829. }
  1830. if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
  1831. size = G_XIM_GET_MARKER_DATA (marker_data);
  1832. g_free(marker_data);
  1833. marker_data = NULL;
  1834. for (sz = 0; sz < size; sz++) {
  1835. GXimAttribute *a;
  1836. a = g_xim_attr_get_attribute_from_stream(proto, attr, stream, cancellable, error);
  1837. if (*error) {
  1838. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1839. g_prefix_error(error, "Error while processing a value type %s: ",
  1840. g_xim_value_type_name(vtype));
  1841. g_xim_messages_gerror(iface->message, *error);
  1842. g_clear_error(error);
  1843. } else {
  1844. goto end;
  1845. }
  1846. }
  1847. *((GSList **)value) = g_slist_append(*((GSList **)value), a);
  1848. }
  1849. } else {
  1850. size = G_XIM_GET_MARKER_DATA (marker_data);
  1851. g_free(marker_data);
  1852. marker_data = NULL;
  1853. while (size > 0) {
  1854. goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
  1855. GXimAttribute *a;
  1856. a = g_xim_attr_get_attribute_from_stream(proto, attr, stream, cancellable, error);
  1857. if (*error) {
  1858. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1859. g_prefix_error(error, "Error while processing a value type %s: ",
  1860. g_xim_value_type_name(vtype));
  1861. g_xim_messages_gerror(iface->message, *error);
  1862. g_clear_error(error);
  1863. } else {
  1864. goto end;
  1865. }
  1866. }
  1867. pos = g_seekable_tell(G_SEEKABLE (istream));
  1868. if ((pos - cur_pos) > size) {
  1869. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1870. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  1871. "Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
  1872. g_xim_value_type_name(vtype), size, pos - cur_pos);
  1873. goto end;
  1874. }
  1875. *((GSList **)value) = g_slist_append(*((GSList **)value), a);
  1876. size -= (pos - cur_pos);
  1877. }
  1878. }
  1879. break;
  1880. case G_XIM_TYPE_LIST_OF_EXT:
  1881. value = va_arg(args, gpointer);
  1882. if (value == NULL) {
  1883. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1884. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1885. "value type %s requires an argument to store the result.",
  1886. g_xim_value_type_name(vtype));
  1887. goto end;
  1888. }
  1889. if (marker_data == NULL) {
  1890. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1891. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1892. "No the markers for value type %s to determine how much the number of chunk available",
  1893. g_xim_value_type_name(vtype));
  1894. goto end;
  1895. }
  1896. if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
  1897. size = G_XIM_GET_MARKER_DATA (marker_data);
  1898. g_free(marker_data);
  1899. marker_data = NULL;
  1900. for (sz = 0; sz < size; sz++) {
  1901. GXimExt *e;
  1902. e = g_xim_ext_get_from_stream(proto, stream, NULL, error);
  1903. if (*error) {
  1904. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1905. g_prefix_error(error, "Error while processing a value type %s: ",
  1906. g_xim_value_type_name(vtype));
  1907. g_xim_messages_gerror(iface->message, *error);
  1908. g_clear_error(error);
  1909. } else {
  1910. goto end;
  1911. }
  1912. }
  1913. *((GSList **)value) = g_slist_append(*((GSList **)value), e);
  1914. }
  1915. } else {
  1916. size = G_XIM_GET_MARKER_DATA (marker_data);
  1917. g_free(marker_data);
  1918. marker_data = NULL;
  1919. while (size > 0) {
  1920. GXimExt *e;
  1921. goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
  1922. e = g_xim_ext_get_from_stream(proto, stream, NULL, error);
  1923. if (*error) {
  1924. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1925. g_prefix_error(error, "Error while processing a value type %s: ",
  1926. g_xim_value_type_name(vtype));
  1927. g_xim_messages_gerror(iface->message, *error);
  1928. g_clear_error(error);
  1929. } else {
  1930. goto end;
  1931. }
  1932. }
  1933. pos = g_seekable_tell(G_SEEKABLE (istream));
  1934. if ((pos - cur_pos) > size) {
  1935. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1936. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  1937. "Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
  1938. g_xim_value_type_name(vtype), size, pos - cur_pos);
  1939. goto end;
  1940. }
  1941. *((GSList **)value) = g_slist_append(*((GSList **)value), e);
  1942. size -= (pos - cur_pos);
  1943. }
  1944. }
  1945. break;
  1946. case G_XIM_TYPE_LIST_OF_STRING:
  1947. value = va_arg(args, gpointer);
  1948. if (value == NULL) {
  1949. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1950. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1951. "value type %s requires an argument to store the result.",
  1952. g_xim_value_type_name(vtype));
  1953. goto end;
  1954. }
  1955. if (marker_data == NULL) {
  1956. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  1957. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  1958. "No the markers for value type %s to determine how much the number of chunk available",
  1959. g_xim_value_type_name(vtype));
  1960. goto end;
  1961. }
  1962. if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
  1963. size = G_XIM_GET_MARKER_DATA (marker_data);
  1964. g_free(marker_data);
  1965. marker_data = NULL;
  1966. for (sz = 0; sz < size; sz++) {
  1967. GXimString *s;
  1968. s = g_xim_string_get_from_stream(stream, NULL, error);
  1969. if (*error) {
  1970. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1971. g_prefix_error(error, "Error while processing a value type %s: ",
  1972. g_xim_value_type_name(vtype));
  1973. g_xim_messages_gerror(iface->message, *error);
  1974. g_clear_error(error);
  1975. } else {
  1976. goto end;
  1977. }
  1978. }
  1979. *((GSList **)value) = g_slist_append(*((GSList **)value), s);
  1980. }
  1981. } else {
  1982. size = G_XIM_GET_MARKER_DATA (marker_data);
  1983. g_free(marker_data);
  1984. marker_data = NULL;
  1985. while (size > 0) {
  1986. goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
  1987. GXimString *s;
  1988. s = g_xim_string_get_from_stream(stream, NULL, error);
  1989. if (*error) {
  1990. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  1991. g_prefix_error(error, "Error while processing a value type %s: ",
  1992. g_xim_value_type_name(vtype));
  1993. g_xim_messages_gerror(iface->message, *error);
  1994. g_clear_error(error);
  1995. } else {
  1996. goto end;
  1997. }
  1998. }
  1999. pos = g_seekable_tell(G_SEEKABLE (istream));
  2000. if ((pos - cur_pos) > size) {
  2001. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2002. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2003. "Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
  2004. g_xim_value_type_name(vtype), size, pos - cur_pos);
  2005. goto end;
  2006. }
  2007. *((GSList **)value) = g_slist_append(*((GSList **)value), s);
  2008. size -= (pos - cur_pos);
  2009. }
  2010. }
  2011. break;
  2012. case G_XIM_TYPE_LIST_OF_STR:
  2013. value = va_arg(args, gpointer);
  2014. if (value == NULL) {
  2015. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2016. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2017. "value type %s requires an argument to store the result.",
  2018. g_xim_value_type_name(vtype));
  2019. goto end;
  2020. }
  2021. if (marker_data == NULL) {
  2022. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2023. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2024. "No the markers for value type %s to determine how much the number of chunk available",
  2025. g_xim_value_type_name(vtype));
  2026. goto end;
  2027. }
  2028. if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
  2029. size = G_XIM_GET_MARKER_DATA (marker_data);
  2030. g_free(marker_data);
  2031. marker_data = NULL;
  2032. for (sz = 0; sz < size; sz++) {
  2033. GXimStr *s;
  2034. s = g_xim_str_get_from_stream(stream, NULL, error);
  2035. if (*error) {
  2036. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  2037. g_prefix_error(error, "Error while processing a value type %s: ",
  2038. g_xim_value_type_name(vtype));
  2039. g_xim_messages_gerror(iface->message, *error);
  2040. g_clear_error(error);
  2041. } else {
  2042. goto end;
  2043. }
  2044. }
  2045. *((GSList **)value) = g_slist_append(*((GSList **)value), s);
  2046. }
  2047. } else {
  2048. size = G_XIM_GET_MARKER_DATA (marker_data);
  2049. g_free(marker_data);
  2050. marker_data = NULL;
  2051. while (size > 0) {
  2052. goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
  2053. GXimStr *s;
  2054. s = g_xim_str_get_from_stream(stream, NULL, error);
  2055. if (*error) {
  2056. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  2057. g_prefix_error(error, "Error while processing a value type %s: ",
  2058. g_xim_value_type_name(vtype));
  2059. g_xim_messages_gerror(iface->message, *error);
  2060. g_clear_error(error);
  2061. } else {
  2062. goto end;
  2063. }
  2064. }
  2065. pos = g_seekable_tell(G_SEEKABLE (istream));
  2066. if ((pos - cur_pos) > size) {
  2067. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2068. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2069. "Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
  2070. g_xim_value_type_name(vtype), size, pos - cur_pos);
  2071. goto end;
  2072. }
  2073. *((GSList **)value) = g_slist_append(*((GSList **)value), s);
  2074. size -= (pos - cur_pos);
  2075. }
  2076. }
  2077. break;
  2078. case G_XIM_TYPE_LIST_OF_ENCODINGINFO:
  2079. value = va_arg(args, gpointer);
  2080. if (value == NULL) {
  2081. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2082. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2083. "value type %s requires an argument to store the result.",
  2084. g_xim_value_type_name(vtype));
  2085. goto end;
  2086. }
  2087. if (marker_data == NULL) {
  2088. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2089. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2090. "No the markers for value type %s to determine how much the number of chunk available",
  2091. g_xim_value_type_name(vtype));
  2092. goto end;
  2093. }
  2094. if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
  2095. size = G_XIM_GET_MARKER_DATA (marker_data);
  2096. g_free(marker_data);
  2097. marker_data = NULL;
  2098. for (sz = 0; sz < size; sz++) {
  2099. GXimEncodingInfo *e;
  2100. e = g_xim_encodinginfo_get_from_stream(stream, NULL, error);
  2101. if (*error) {
  2102. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  2103. g_prefix_error(error, "Error while processing a value type %s: ",
  2104. g_xim_value_type_name(vtype));
  2105. g_xim_messages_gerror(iface->message, *error);
  2106. g_clear_error(error);
  2107. } else {
  2108. goto end;
  2109. }
  2110. }
  2111. *((GSList **)value) = g_slist_append(*((GSList **)value), e);
  2112. }
  2113. } else {
  2114. size = G_XIM_GET_MARKER_DATA (marker_data);
  2115. g_free(marker_data);
  2116. marker_data = NULL;
  2117. while (size > 0) {
  2118. goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
  2119. GXimEncodingInfo *e;
  2120. e = g_xim_encodinginfo_get_from_stream(stream, NULL, error);
  2121. if (*error) {
  2122. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  2123. g_prefix_error(error, "Error while processing a value type %s: ",
  2124. g_xim_value_type_name(vtype));
  2125. g_xim_messages_gerror(iface->message, *error);
  2126. g_clear_error(error);
  2127. } else {
  2128. goto end;
  2129. }
  2130. }
  2131. pos = g_seekable_tell(G_SEEKABLE (istream));
  2132. if ((pos - cur_pos) > size) {
  2133. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2134. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2135. "Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
  2136. g_xim_value_type_name(vtype), size, pos - cur_pos);
  2137. goto end;
  2138. }
  2139. *((GSList **)value) = g_slist_append(*((GSList **)value), e);
  2140. size -= (pos - cur_pos);
  2141. }
  2142. }
  2143. break;
  2144. case G_XIM_TYPE_LIST_OF_BYTE:
  2145. value = va_arg(args, gpointer);
  2146. if (value == NULL) {
  2147. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2148. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2149. "value type %s requires an argument to store the result.",
  2150. g_xim_value_type_name(vtype));
  2151. goto end;
  2152. }
  2153. if (marker_data == NULL) {
  2154. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2155. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2156. "No the markers for value type %s to determine how much the number of chunk available",
  2157. g_xim_value_type_name(vtype));
  2158. goto end;
  2159. }
  2160. /* assuming the length of item == the length of bytes */
  2161. size = G_XIM_GET_MARKER_DATA (marker_data);
  2162. g_free(marker_data);
  2163. marker_data = NULL;
  2164. *((GString **)value) = g_string_sized_new(size);
  2165. for (sz = 0; sz < size; sz++) {
  2166. gchar c;
  2167. c = g_data_input_stream_read_byte(stream, cancellable, error);
  2168. if (*error)
  2169. goto end;
  2170. g_string_append_c(*((GString **)value), c);
  2171. }
  2172. break;
  2173. case G_XIM_TYPE_LIST_OF_CARD16:
  2174. value = va_arg(args, gpointer);
  2175. if (value == NULL) {
  2176. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2177. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2178. "value type %s requires an argument to store the result.",
  2179. g_xim_value_type_name(vtype));
  2180. goto end;
  2181. }
  2182. if (marker_data == NULL) {
  2183. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2184. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2185. "No the markers for value type %s to determine how much the number of chunk available",
  2186. g_xim_value_type_name(vtype));
  2187. goto end;
  2188. }
  2189. if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
  2190. size = G_XIM_GET_MARKER_DATA (marker_data);
  2191. g_free(marker_data);
  2192. marker_data = NULL;
  2193. for (sz = 0; sz < size; sz++) {
  2194. guint16 n;
  2195. n = g_data_input_stream_read_uint16(stream, NULL, error);
  2196. if (*error)
  2197. goto end;
  2198. *((GSList **)value) = g_slist_append(*((GSList **)value), GUINT_TO_POINTER ((guint)n));
  2199. }
  2200. } else {
  2201. size = G_XIM_GET_MARKER_DATA (marker_data);
  2202. g_free(marker_data);
  2203. marker_data = NULL;
  2204. if ((size % 2) != 0) {
  2205. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2206. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2207. "The length of bytes for value type %s has to be an even number: %" G_GSIZE_FORMAT,
  2208. g_xim_value_type_name(vtype), size);
  2209. goto end;
  2210. }
  2211. while (size > 0) {
  2212. guint16 n;
  2213. n = g_data_input_stream_read_uint16(stream, NULL, error);
  2214. if (*error)
  2215. goto end;
  2216. *((GSList **)value) = g_slist_append(*((GSList **)value), GUINT_TO_POINTER ((guint)n));
  2217. size -= 2;
  2218. }
  2219. }
  2220. break;
  2221. case G_XIM_TYPE_LIST_OF_HOTKEY_TRIGGER:
  2222. value = va_arg(args, gpointer);
  2223. if (value == NULL) {
  2224. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2225. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2226. "value type %s requires an argument to store the result.",
  2227. g_xim_value_type_name(vtype));
  2228. goto end;
  2229. }
  2230. if (marker_data == NULL) {
  2231. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2232. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2233. "No the markers for value type %s to determine how much the number of chunk available",
  2234. g_xim_value_type_name(vtype));
  2235. goto end;
  2236. }
  2237. if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
  2238. size = G_XIM_GET_MARKER_DATA (marker_data);
  2239. g_free(marker_data);
  2240. marker_data = NULL;
  2241. for (sz = 0; sz < size; sz++) {
  2242. GXimHotkeyTrigger *v;
  2243. v = g_xim_hotkey_trigger_get_from_stream(proto, stream, cancellable, error);
  2244. if (*error) {
  2245. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  2246. g_prefix_error(error, "Error while processing a value type %s: ",
  2247. g_xim_value_type_name(vtype));
  2248. g_xim_messages_gerror(iface->message, *error);
  2249. g_clear_error(error);
  2250. } else {
  2251. goto end;
  2252. }
  2253. }
  2254. *((GSList **)value) = g_slist_append(*((GSList **)value), v);
  2255. }
  2256. } else {
  2257. size = G_XIM_GET_MARKER_DATA (marker_data);
  2258. g_free(marker_data);
  2259. marker_data = NULL;
  2260. while (size > 0) {
  2261. goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
  2262. GXimHotkeyTrigger *v;
  2263. v = g_xim_hotkey_trigger_get_from_stream(proto, stream, cancellable, error);
  2264. if (*error) {
  2265. if (G_XIM_GERROR_IS_RECOVERABLE (*error)) {
  2266. g_prefix_error(error, "Error while processing a value type %s: ",
  2267. g_xim_value_type_name(vtype));
  2268. g_xim_messages_gerror(iface->message, *error);
  2269. g_clear_error(error);
  2270. } else {
  2271. goto end;
  2272. }
  2273. }
  2274. pos = g_seekable_tell(G_SEEKABLE (istream));
  2275. if ((pos - cur_pos) > size) {
  2276. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2277. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2278. "Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
  2279. g_xim_value_type_name(vtype), size, pos - cur_pos);
  2280. goto end;
  2281. }
  2282. *((GSList **)value) = g_slist_append(*((GSList **)value), v);
  2283. size -= (pos - cur_pos);
  2284. }
  2285. }
  2286. break;
  2287. case G_XIM_TYPE_SEPARATOR:
  2288. value = va_arg(args, gpointer);
  2289. if (value == NULL) {
  2290. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2291. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2292. "value type %s requires an argument to store the result.",
  2293. g_xim_value_type_name(vtype));
  2294. goto end;
  2295. }
  2296. *((GXimSepNestedList **)value) = g_xim_sep_nested_list_new();
  2297. break;
  2298. case G_XIM_TYPE_BYTE:
  2299. value = va_arg(args, gpointer);
  2300. if (value == NULL) {
  2301. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2302. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2303. "value type %s requires an argument to store the result.",
  2304. g_xim_value_type_name(vtype));
  2305. goto end;
  2306. }
  2307. *((gchar *)value) = g_data_input_stream_read_byte(stream, cancellable, error);
  2308. break;
  2309. case G_XIM_TYPE_WORD:
  2310. value = va_arg(args, gpointer);
  2311. if (value == NULL) {
  2312. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2313. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2314. "value type %s requires an argument to store the result.",
  2315. g_xim_value_type_name(vtype));
  2316. goto end;
  2317. }
  2318. *((guint16 *)value) = g_data_input_stream_read_uint16(stream, cancellable, error);
  2319. break;
  2320. case G_XIM_TYPE_LONG:
  2321. value = va_arg(args, gpointer);
  2322. if (value == NULL) {
  2323. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2324. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2325. "value type %s requires an argument to store the result.",
  2326. g_xim_value_type_name(vtype));
  2327. goto end;
  2328. }
  2329. *((guint32 *)value) = g_data_input_stream_read_uint32(stream, cancellable, error);
  2330. break;
  2331. case G_XIM_TYPE_CHAR:
  2332. value = va_arg(args, gpointer);
  2333. if (value == NULL) {
  2334. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2335. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2336. "value type %s requires an argument to store the result.",
  2337. g_xim_value_type_name(vtype));
  2338. goto end;
  2339. }
  2340. if (marker_data == NULL) {
  2341. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2342. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2343. "No the markers for value type %s to determine how much the number of chunk available",
  2344. g_xim_value_type_name(vtype));
  2345. goto end;
  2346. }
  2347. /* assuming the length of item == the length of bytes */
  2348. size = G_XIM_GET_MARKER_DATA (marker_data);
  2349. g_free(marker_data);
  2350. marker_data = NULL;
  2351. *((gchar **)value) = g_new0(gchar, size + 1);
  2352. for (sz = 0; sz < size; sz++) {
  2353. gchar c;
  2354. c = g_data_input_stream_read_byte(stream, cancellable, error);
  2355. if (*error)
  2356. goto end;
  2357. (*((gchar **)value))[sz] = c;
  2358. }
  2359. (*((gchar **)value))[sz] = 0;
  2360. break;
  2361. case G_XIM_TYPE_WINDOW:
  2362. value = va_arg(args, gpointer);
  2363. if (value == NULL) {
  2364. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2365. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2366. "value type %s requires an argument to store the result.",
  2367. g_xim_value_type_name(vtype));
  2368. goto end;
  2369. }
  2370. nw = g_data_input_stream_read_uint32(stream, cancellable, error);
  2371. if (nw) {
  2372. *((GdkWindow **)value) = g_xim_get_window(g_xim_transport_get_display(G_XIM_TRANSPORT (proto)),
  2373. nw);
  2374. if (*((GdkWindow **)value) == NULL) {
  2375. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2376. G_XIM_PROTOCOL_ERROR_NO_DATA | G_XIM_NOTICE_ERROR,
  2377. "Unable to get a valid window for %p",
  2378. G_XIM_NATIVE_WINDOW_TO_POINTER (nw));
  2379. goto end;
  2380. }
  2381. } else {
  2382. /* Window is possible to be set 0 */
  2383. *((GdkWindow **)value) = NULL;
  2384. }
  2385. break;
  2386. case G_XIM_TYPE_XIMSTYLES:
  2387. value = va_arg(args, gpointer);
  2388. if (value == NULL) {
  2389. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2390. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2391. "value type %s requires an argument to store the result.",
  2392. g_xim_value_type_name(vtype));
  2393. goto end;
  2394. }
  2395. *((GXimStyles **)value) = g_xim_styles_get_from_stream(proto, stream, NULL, error);
  2396. break;
  2397. case G_XIM_TYPE_XRECTANGLE:
  2398. value = va_arg(args, gpointer);
  2399. if (value == NULL) {
  2400. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2401. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2402. "value type %s requires an argument to store the result.",
  2403. g_xim_value_type_name(vtype));
  2404. goto end;
  2405. }
  2406. *((GXimRectangle **)value) = g_xim_rectangle_get_from_stream(proto, stream, NULL, error);
  2407. break;
  2408. case G_XIM_TYPE_XPOINT:
  2409. value = va_arg(args, gpointer);
  2410. if (value == NULL) {
  2411. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2412. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2413. "value type %s requires an argument to store the result.",
  2414. g_xim_value_type_name(vtype));
  2415. goto end;
  2416. }
  2417. *((GXimPoint **)value) = g_xim_point_get_from_stream(proto, stream, NULL, error);
  2418. break;
  2419. case G_XIM_TYPE_XFONTSET:
  2420. value = va_arg(args, gpointer);
  2421. if (value == NULL) {
  2422. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2423. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2424. "value type %s requires an argument to store the result.",
  2425. g_xim_value_type_name(vtype));
  2426. goto end;
  2427. }
  2428. *((GXimFontSet **)value) = g_xim_fontset_get_from_stream(proto, stream, NULL, error);
  2429. break;
  2430. case G_XIM_TYPE_HOTKEYTRIGGERS:
  2431. case G_XIM_TYPE_HOTKEYSTATE:
  2432. case G_XIM_TYPE_STRINGCONVERSION:
  2433. case G_XIM_TYPE_PREEDITSTATE:
  2434. case G_XIM_TYPE_RESETSTATE:
  2435. goto nonsupported;
  2436. case G_XIM_TYPE_NESTEDLIST:
  2437. value = va_arg(args, gpointer);
  2438. if (value == NULL) {
  2439. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2440. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2441. "value type %s requires an argument to store the result.",
  2442. g_xim_value_type_name(vtype));
  2443. goto end;
  2444. }
  2445. if (marker_data == NULL) {
  2446. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2447. G_XIM_STD_ERROR_INVALID_ARGUMENT | G_XIM_NOTICE_ERROR,
  2448. "No the markers for value type %s to determine how much the number of chunk available",
  2449. g_xim_value_type_name(vtype));
  2450. goto end;
  2451. }
  2452. if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
  2453. GXimNestedList *list;
  2454. GXimConnection *conn;
  2455. size = G_XIM_GET_MARKER_DATA (marker_data);
  2456. g_free(marker_data);
  2457. marker_data = NULL;
  2458. conn = G_XIM_CONNECTION (proto);
  2459. /* XXX: NESTEDLIST is only used for IC attributes? */
  2460. list = g_xim_nested_list_new(G_XIM_ATTR (conn->default_icattr), size);
  2461. for (sz = 0; sz < size; sz++) {
  2462. gint16 attr_id;
  2463. guint16 n;
  2464. gpointer v;
  2465. GType gtype;
  2466. GXimValueType ntype;
  2467. gchar *name;
  2468. if (!g_xim_protocol_read_format(proto, stream, cancellable, error, 1,
  2469. G_XIM_TYPE_WORD, &attr_id)) {
  2470. fail_nested_list_1:
  2471. g_prefix_error(error, "Error while processing arg %" G_GSIZE_FORMAT " of %" G_GSIZE_FORMAT " in a value type %s: ",
  2472. sz, size, g_xim_value_type_name(vtype));
  2473. g_xim_messages_gerror(iface->message, *error);
  2474. g_clear_error(error);
  2475. n = g_data_input_stream_read_uint16(stream, cancellable, error);
  2476. g_seekable_seek(G_SEEKABLE (istream), n,
  2477. G_SEEK_CUR, cancellable, error);
  2478. if (*error)
  2479. goto end;
  2480. continue;
  2481. }
  2482. gtype = g_xim_attr_get_gtype_by_id(G_XIM_ATTR (conn->default_icattr), attr_id);
  2483. if (gtype == G_TYPE_INVALID) {
  2484. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2485. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2486. "Unable to get the value type for attr_id %d",
  2487. attr_id);
  2488. goto fail_nested_list_1;
  2489. }
  2490. ntype = g_xim_gtype_to_value_type(gtype);
  2491. if (ntype == G_XIM_TYPE_INVALID) {
  2492. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2493. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2494. "Unable to convert the value type `%s' for attr_id %d",
  2495. g_type_name(gtype), attr_id);
  2496. goto fail_nested_list_1;
  2497. }
  2498. if (!g_xim_protocol_read_format(proto, stream, cancellable, error, 2,
  2499. G_XIM_TYPE_MARKER_N_BYTES_2, ntype,
  2500. ntype, &v)) {
  2501. /* maybe hard to recover the error here */
  2502. goto end;
  2503. }
  2504. name = g_xim_attr_get_attribute_name(G_XIM_ATTR (conn->default_icattr), attr_id);
  2505. g_xim_nested_list_append(list, name, v);
  2506. g_free(name);
  2507. g_xim_free_by_gtype(gtype, v);
  2508. }
  2509. *((GXimNestedList **)value) = list;
  2510. } else {
  2511. GXimNestedList *list;
  2512. GXimConnection *conn;
  2513. size = G_XIM_GET_MARKER_DATA (marker_data);
  2514. g_free(marker_data);
  2515. marker_data = NULL;
  2516. conn = G_XIM_CONNECTION (proto);
  2517. /* XXX: NESTEDLIST is only used for IC attributes? */
  2518. list = g_xim_nested_list_new(G_XIM_ATTR (conn->default_icattr), size);
  2519. /* try to fetch a value until remaining size is more than minimum requirement */
  2520. while (size > 3) {
  2521. goffset cur_pos = g_seekable_tell(G_SEEKABLE (istream)), pos;
  2522. gint16 attr_id;
  2523. guint16 n;
  2524. gpointer v;
  2525. GType gtype = G_TYPE_INVALID;
  2526. GXimValueType ntype;
  2527. gchar *name = NULL;
  2528. if (!g_xim_protocol_read_format(proto, stream, cancellable, error, 1,
  2529. G_XIM_TYPE_WORD, &attr_id)) {
  2530. fail_nested_list_2:
  2531. g_prefix_error(error, "Error while processing a value type %s: ",
  2532. g_xim_value_type_name(vtype));
  2533. g_xim_messages_gerror(iface->message, *error);
  2534. g_clear_error(error);
  2535. n = g_data_input_stream_read_uint16(stream, cancellable, error);
  2536. g_seekable_seek(G_SEEKABLE (istream), n,
  2537. G_SEEK_CUR, cancellable, error);
  2538. if (*error) {
  2539. g_free(name);
  2540. goto end;
  2541. }
  2542. goto size_check_nested_list;
  2543. }
  2544. gtype = g_xim_attr_get_gtype_by_id(G_XIM_ATTR (conn->default_icattr), attr_id);
  2545. if (gtype == G_TYPE_INVALID) {
  2546. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2547. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2548. "Unable to get the value type for attr_id %d",
  2549. attr_id);
  2550. goto fail_nested_list_2;
  2551. }
  2552. ntype = g_xim_gtype_to_value_type(gtype);
  2553. if (ntype == G_XIM_TYPE_INVALID) {
  2554. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2555. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2556. "Unable to convert the value type `%s' for attr_id %d",
  2557. g_type_name(gtype), attr_id);
  2558. goto fail_nested_list_2;
  2559. }
  2560. name = g_xim_attr_get_attribute_name(G_XIM_ATTR (conn->default_icattr), attr_id);
  2561. g_xim_messages_debug(iface->message, "proto/attr",
  2562. " %d: Attribute: %s [%s]",
  2563. attr_id, name, g_xim_value_type_name(ntype));
  2564. if (!g_xim_protocol_read_format(proto, stream, cancellable, error, 2,
  2565. G_XIM_TYPE_MARKER_N_BYTES_2, ntype,
  2566. ntype, &v)) {
  2567. /* maybe hard to recover the error here */
  2568. g_free(name);
  2569. /* however try to recover the existing data in the nested list as much as possible */
  2570. G_XIM_GERROR_RESET_NOTICE_FLAG (*error);
  2571. G_XIM_GERROR_SET_NOTICE_FLAG (*error, G_XIM_NOTICE_WARNING);
  2572. break;
  2573. }
  2574. size_check_nested_list:
  2575. pos = g_seekable_tell(G_SEEKABLE (istream));
  2576. if ((pos - cur_pos) > size) {
  2577. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2578. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2579. "Unable to compose a value type %s with the remaining packet size: %" G_GSIZE_FORMAT " for %" G_GINT64_FORMAT,
  2580. g_xim_value_type_name(vtype), size, pos - cur_pos);
  2581. g_free(name);
  2582. goto end;
  2583. }
  2584. if (name) {
  2585. g_xim_nested_list_append(list, name, v);
  2586. g_free(name);
  2587. name = NULL;
  2588. }
  2589. g_xim_free_by_gtype(gtype, v);
  2590. size -= (pos - cur_pos);
  2591. }
  2592. *((GXimNestedList **)value) = list;
  2593. }
  2594. break;
  2595. default:
  2596. nonsupported:
  2597. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2598. G_XIM_STD_ERROR_UNSUPPORTED | G_XIM_NOTICE_ERROR,
  2599. "Unknown/unsupported value type to read: %s",
  2600. g_xim_value_type_name(vtype));
  2601. goto end;
  2602. }
  2603. if (marker_data) {
  2604. goffset pos;
  2605. if (G_XIM_MARKER_IS_ITEM_BASED (marker_data)) {
  2606. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2607. G_XIM_STD_ERROR_UNSUPPORTED | G_XIM_NOTICE_ERROR,
  2608. "Value type %s doesn't support the number-of-item marker",
  2609. g_xim_value_type_name(vtype));
  2610. goto end;
  2611. }
  2612. size = G_XIM_GET_MARKER_DATA (marker_data);
  2613. pos = g_seekable_tell(G_SEEKABLE (istream));
  2614. if ((pos - cur_pos) != size) {
  2615. g_set_error(error, G_XIM_PROTOCOL_ERROR,
  2616. G_XIM_PROTOCOL_ERROR_INVALID_PACKETS_RECEIVED | G_XIM_NOTICE_ERROR,
  2617. "Unable to compose a value type %s properly due to the size difference: expected size: %" G_GSIZE_FORMAT ", actual size: %" G_GINT64_FORMAT,
  2618. g_xim_value_type_name(vtype), size, pos - cur_pos);
  2619. goto end;
  2620. }
  2621. g_free(marker_data);
  2622. marker_data = NULL;
  2623. }
  2624. base_pos = cur_pos;
  2625. if (*error) {
  2626. if (!G_XIM_GERROR_IS_RECOVERABLE (*error))
  2627. goto end;
  2628. g_prefix_error(error, "Error while processing a value type %s: ",
  2629. g_xim_value_type_name(vtype));
  2630. g_xim_messages_gerror(iface->message, *error);
  2631. g_clear_error(error);
  2632. }
  2633. }
  2634. end:
  2635. if (marker_data) {
  2636. /* to avoid the memory leak for the case broke out for errors */
  2637. g_free(marker_data);
  2638. marker_data = NULL;
  2639. }
  2640. if (*error) {
  2641. g_prefix_error(error, "Error while processing a value type %s: ",
  2642. g_xim_value_type_name(vtype));
  2643. retval = FALSE;
  2644. }
  2645. if (!g_queue_is_empty(markerq)) {
  2646. g_xim_messages_bug(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
  2647. "Unused marker(s) given: %u is still in the queue for reading.",
  2648. g_queue_get_length(markerq));
  2649. while (!g_queue_is_empty(markerq)) {
  2650. marker_data = g_queue_pop_tail(markerq);
  2651. g_xim_messages_bug(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
  2652. " marker type: %s",
  2653. g_xim_value_type_name(marker_data->where));
  2654. g_free(marker_data);
  2655. }
  2656. }
  2657. g_queue_free(markerq);
  2658. return retval;
  2659. }
  2660. /**
  2661. * g_xim_protocol_read_format:
  2662. * @proto: a #GXimProtocol.
  2663. * @stream: a #GDataInputStream which contains XIM protocol packets.
  2664. * @cancellable: optional #GCancellable object, %NULL to ignore.
  2665. * @error: a location to store error, or %NULL.
  2666. * @n_params: the number of a set of parameters.
  2667. *
  2668. * Scans XIM protocol packets according to the set of parameters. if any,
  2669. * objects generated against it is stored in the locations pointed to by
  2670. * the pointer arguments. Each pointer argument must be of a type that is
  2671. * appropriate for the value returned by the corresponding value type.
  2672. * See #GXimValueType documentation which objects the value type is supposed
  2673. * to be.
  2674. *
  2675. * Returns: %TRUE to be read successfully.
  2676. */
  2677. gboolean
  2678. g_xim_protocol_read_format(GXimProtocol *proto,
  2679. GDataInputStream *stream,
  2680. GCancellable *cancellable,
  2681. GError **error,
  2682. guint n_params,
  2683. ...)
  2684. {
  2685. va_list ap;
  2686. gboolean retval;
  2687. va_start(ap, n_params);
  2688. retval = g_xim_protocol_read_vformat(proto, stream, cancellable, error, n_params, ap);
  2689. va_end(ap);
  2690. return retval;
  2691. }
  2692. /**
  2693. * g_xim_protocol_wait_for_reply:
  2694. * @proto: a #GXimProtocol.
  2695. * @major_opcode: a major opcode in XIM protocol.
  2696. * @minor_opcode: a minor opcode in XIM protocol.
  2697. * @error: a location to store error, or %NULL.
  2698. *
  2699. * Waits for a response of @major_opcode and @minor_opcode or %G_XIM_ERROR.
  2700. * this can be used if you need to send a request synchronously. Note that
  2701. * using this function in the server instance won't works due to how GObject
  2702. * delivers signals and iterates it.
  2703. *
  2704. * Returns: %TRUE the request was successfully done.
  2705. */
  2706. gboolean
  2707. g_xim_protocol_wait_for_reply(GXimProtocol *proto,
  2708. GXimOpcode major_opcode,
  2709. guint8 minor_opcode,
  2710. GError **error)
  2711. {
  2712. GXimProtocolPrivate *priv;
  2713. GXimProtocolSyncable *syncable;
  2714. gboolean retval = TRUE;
  2715. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
  2716. g_return_val_if_fail (error != NULL, FALSE);
  2717. priv = g_xim_protocol_get_private(proto);
  2718. syncable = g_new0(GXimProtocolSyncable, 1);
  2719. G_XIM_GERROR_CHECK_ALLOC (syncable, error,
  2720. G_XIM_PROTOCOL_ERROR, FALSE);
  2721. syncable->major_opcode = major_opcode;
  2722. syncable->minor_opcode = minor_opcode;
  2723. syncable->error = NULL;
  2724. G_LOCK (g_xim_protocol_syncable);
  2725. g_queue_push_tail(priv->syncableq, syncable);
  2726. G_UNLOCK (g_xim_protocol_syncable);
  2727. while (g_queue_find(priv->syncableq, syncable))
  2728. g_main_context_iteration(NULL, TRUE);
  2729. retval = syncable->result;
  2730. if (syncable->error) {
  2731. retval = FALSE;
  2732. *error = g_error_copy(syncable->error);
  2733. g_error_free(syncable->error);
  2734. }
  2735. g_free(syncable);
  2736. return retval;
  2737. }
  2738. /**
  2739. * g_xim_protocol_raise_parser_error:
  2740. * @proto: a #GXimProtocol.
  2741. * @major_opcode: a major opcode in XIM protocol.
  2742. * @minor_opcode: a minor opcode in XIM protocol.
  2743. * @imid: input-method ID.
  2744. * @icid: input-context ID.
  2745. *
  2746. * Raises an appropriate error to the opposite direction on the connection
  2747. * according to @major_opcode and @minor_opcode.
  2748. *
  2749. * Returns: %TRUE to be sent an error successfully.
  2750. */
  2751. gboolean
  2752. g_xim_protocol_raise_parser_error(GXimProtocol *proto,
  2753. guint major_opcode,
  2754. guint minor_opcode,
  2755. guint imid,
  2756. guint icid)
  2757. {
  2758. gboolean ret = FALSE;
  2759. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), FALSE);
  2760. g_signal_emit(proto, signals[PARSER_ERROR], 0,
  2761. major_opcode, minor_opcode, imid, icid, &ret);
  2762. return ret;
  2763. }
  2764. /**
  2765. * g_xim_protocol_add_protocol:
  2766. * @proto: a #GXimProtocol.
  2767. * @closure: a structure of closure.
  2768. *
  2769. * Registers a @closure to add the XIM protocol handler.
  2770. */
  2771. void
  2772. g_xim_protocol_add_protocol(GXimProtocol *proto,
  2773. GXimProtocolClosure *closure)
  2774. {
  2775. GXimProtocolPrivate *priv;
  2776. GXimProtocolClosure *c;
  2777. g_return_if_fail (G_IS_XIM_PROTOCOL (proto));
  2778. g_return_if_fail (closure != NULL);
  2779. g_return_if_fail ((priv = g_xim_protocol_get_private(proto)));
  2780. c = g_xim_protocol_lookup_protocol_by_id(proto,
  2781. closure->major_opcode.v2,
  2782. closure->minor_opcode);
  2783. if (c != NULL) {
  2784. g_xim_messages_warning(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
  2785. "Protocol (major: 0x%x, minor: 0x%x) has already been registered. replacing\n details: old: %s, new: %s",
  2786. c->major_opcode.v2, c->minor_opcode,
  2787. c->name, closure->name);
  2788. }
  2789. g_hash_table_replace(priv->proto_table__named_index,
  2790. g_ascii_strup(closure->name, -1),
  2791. closure);
  2792. g_hash_table_replace(priv->proto_table__id_index,
  2793. G_XIM_OPCODE_KEY (closure->major_opcode.v2, closure->minor_opcode),
  2794. closure);
  2795. }
  2796. /**
  2797. * g_xim_protocol_remove_protocol:
  2798. * @proto: a #GXimProtocol.
  2799. * @closure: a structure of closure.
  2800. *
  2801. * Gets rid of @closure to stop XIM protocol handling in @proto.
  2802. */
  2803. void
  2804. g_xim_protocol_remove_protocol(GXimProtocol *proto,
  2805. GXimProtocolClosure *closure)
  2806. {
  2807. g_xim_protocol_remove_protocol_by_id(proto,
  2808. closure->major_opcode.v2,
  2809. closure->minor_opcode);
  2810. }
  2811. /**
  2812. * g_xim_protocol_remove_protocol_by_id:
  2813. * @proto: a #GXimProtocol.
  2814. * @major_opcode: a major opcode in XIM protocol.
  2815. * @minor_opcode: a minor opcode in XIM protocol.
  2816. *
  2817. * Gets rid of XIM protocol handling to stop dealing with @major_opcode and
  2818. * @minor_opcode in @proto.
  2819. */
  2820. void
  2821. g_xim_protocol_remove_protocol_by_id(GXimProtocol *proto,
  2822. guint8 major_opcode,
  2823. guint8 minor_opcode)
  2824. {
  2825. GXimProtocolClosure *c;
  2826. GXimProtocolPrivate *priv;
  2827. g_return_if_fail (G_IS_XIM_PROTOCOL (proto));
  2828. g_return_if_fail ((priv = g_xim_protocol_get_private(proto)));
  2829. c = g_xim_protocol_lookup_protocol_by_id(proto, major_opcode, minor_opcode);
  2830. if (c == NULL) {
  2831. g_xim_messages_warning(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
  2832. "Protocol (major: 0x%x, minor: 0x%x) isn't yet registered.",
  2833. major_opcode, minor_opcode);
  2834. } else {
  2835. gchar *name = g_ascii_strup(c->name, -1);
  2836. g_hash_table_remove(priv->proto_table__named_index,
  2837. name);
  2838. g_hash_table_remove(priv->proto_table__id_index,
  2839. G_XIM_OPCODE_KEY (major_opcode, minor_opcode));
  2840. g_free(name);
  2841. }
  2842. }
  2843. /**
  2844. * g_xim_protocol_lookup_protocol_by_name:
  2845. * @proto: a #GXimProtocol.
  2846. * @name: the name of XIM protocol event.
  2847. *
  2848. * Looks up the XIM protocol closure with @name which @proto is dealing with.
  2849. *
  2850. * Returns: a #GXimProtocolClosure corresponding to @name.
  2851. */
  2852. GXimProtocolClosure *
  2853. g_xim_protocol_lookup_protocol_by_name(GXimProtocol *proto,
  2854. const gchar *name)
  2855. {
  2856. GXimProtocolPrivate *priv;
  2857. GXimProtocolClosure *retval;
  2858. gchar *s;
  2859. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), NULL);
  2860. g_return_val_if_fail (name != NULL, NULL);
  2861. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), NULL);
  2862. s = g_ascii_strup(name, -1);
  2863. retval = g_hash_table_lookup(priv->proto_table__named_index,
  2864. s);
  2865. g_free(s);
  2866. return retval;
  2867. }
  2868. /**
  2869. * g_xim_protocol_lookup_protocol_by_id:
  2870. * @proto: a #GXimProtocol.
  2871. * @major_opcode: a major opcode in XIM protocol.
  2872. * @minor_opcode: a minor opcode in XIM protocol.
  2873. *
  2874. * Looks up the XIM protocol closure with @major_opcode and @minor_opcode which
  2875. * @proto is dealing with.
  2876. *
  2877. * Returns: a #GXimProtocolClosure corresponding to @major_opcode and @minor_opcode.
  2878. */
  2879. GXimProtocolClosure *
  2880. g_xim_protocol_lookup_protocol_by_id(GXimProtocol *proto,
  2881. guint8 major_opcode,
  2882. guint8 minor_opcode)
  2883. {
  2884. GXimProtocolPrivate *priv;
  2885. GXimProtocolClosure *retval;
  2886. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), NULL);
  2887. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), NULL);
  2888. retval = g_hash_table_lookup(priv->proto_table__id_index,
  2889. G_XIM_OPCODE_KEY (major_opcode, minor_opcode));
  2890. return retval;
  2891. }
  2892. /**
  2893. * g_xim_protocol_connect_closure_by_id:
  2894. * @proto: a #GXimProtocol.
  2895. * @major_opcode: a major opcode in XIM protocol.
  2896. * @minor_opcode: a minor opcode in XIM protocol.
  2897. * @func: the #GCallback to connect.
  2898. * @data: data to pass to @func.
  2899. *
  2900. * Connects a #GCallback function to a signal for XIM protocol events.
  2901. *
  2902. * Returns: the handler id.
  2903. */
  2904. gulong
  2905. g_xim_protocol_connect_closure_by_id(GXimProtocol *proto,
  2906. guint8 major_opcode,
  2907. guint8 minor_opcode,
  2908. GCallback func,
  2909. gpointer data)
  2910. {
  2911. GXimProtocolClosure *closure;
  2912. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), 0);
  2913. closure = g_xim_protocol_lookup_protocol_by_id(proto, major_opcode, minor_opcode);
  2914. if (closure == NULL) {
  2915. g_xim_messages_bug(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
  2916. "No closure for protocol major:0x%x, minor:0x%x",
  2917. major_opcode, minor_opcode);
  2918. return 0;
  2919. }
  2920. return g_xim_protocol_closure_connect(closure, func, data);
  2921. }
  2922. /**
  2923. * g_xim_protocol_connect_closure_by_name:
  2924. * @proto: a #GXimProtocol.
  2925. * @signal_name: a XIM protocol event name.
  2926. * @func: the #GCallback to connect.
  2927. * @data: data to pass to @func.
  2928. *
  2929. * Connects a #GCallback function to a signal for XIM protocol events.
  2930. *
  2931. * Returns: the handler id.
  2932. */
  2933. gulong
  2934. g_xim_protocol_connect_closure_by_name(GXimProtocol *proto,
  2935. const gchar *signal_name,
  2936. GCallback func,
  2937. gpointer data)
  2938. {
  2939. GXimProtocolClosure *closure;
  2940. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), 0);
  2941. closure = g_xim_protocol_lookup_protocol_by_name(proto, signal_name);
  2942. if (closure == NULL) {
  2943. g_xim_messages_bug(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
  2944. "No closure for protocol `%s'",
  2945. signal_name);
  2946. return 0;
  2947. }
  2948. return g_xim_protocol_closure_connect(closure, func, data);
  2949. }
  2950. /**
  2951. * g_xim_protocol_get_extensions:
  2952. * @proto: a #GXimProtocol.
  2953. *
  2954. * Obtains a set of #GXimProtocolClosure registered as an extension. this is
  2955. * helpful to deal with %G_XIM_QUERY_EXTENSION and so on.
  2956. *
  2957. * Returns: the #GSList which contains #GXimProtocolClosure.
  2958. */
  2959. GSList *
  2960. g_xim_protocol_get_extensions(GXimProtocol *proto)
  2961. {
  2962. GXimProtocolPrivate *priv;
  2963. GXimProtocolClosure *c;
  2964. GHashTableIter iter;
  2965. GSList *retval = NULL;
  2966. gpointer key, val;
  2967. g_return_val_if_fail (G_IS_XIM_PROTOCOL (proto), NULL);
  2968. g_return_val_if_fail ((priv = g_xim_protocol_get_private(proto)), NULL);
  2969. g_hash_table_iter_init(&iter, priv->proto_table__id_index);
  2970. while (g_hash_table_iter_next(&iter, &key, &val)) {
  2971. c = val;
  2972. if (g_xim_protocol_closure_is_an_extension(c))
  2973. retval = g_slist_append(retval, val);
  2974. }
  2975. return retval;
  2976. }
  2977. /**
  2978. * g_xim_protocol_closure_new:
  2979. * @major_opcode: a major opcode in XIM protocol.
  2980. * @minor_opcode: a minor opcode in XIM protocol.
  2981. * @name: the XIM protocol name.
  2982. * @is_an_extension: %TRUE to define an extension in XIM protocol, otherwise %FALSE.
  2983. *
  2984. * Creates an instance of the #GXimProtocolClosure.
  2985. *
  2986. * Returns: the #GXimProtocolClosure.
  2987. */
  2988. GXimProtocolClosure *
  2989. g_xim_protocol_closure_new(guint8 major_opcode,
  2990. guint8 minor_opcode,
  2991. const gchar *name,
  2992. gboolean is_an_extension)
  2993. {
  2994. GClosure *closure;
  2995. GXimProtocolClosure *retval;
  2996. g_return_val_if_fail (name != NULL, NULL);
  2997. closure = g_closure_new_simple(sizeof (GXimProtocolClosure), NULL);
  2998. G_XIM_CHECK_ALLOC (closure, NULL);
  2999. retval = (GXimProtocolClosure *)closure;
  3000. retval->name = g_strdup(name);
  3001. retval->major_opcode.v2 = major_opcode;
  3002. retval->minor_opcode = minor_opcode;
  3003. retval->is_an_extension = is_an_extension;
  3004. g_closure_add_finalize_notifier(closure, NULL, g_xim_protocol_closure_free);
  3005. return retval;
  3006. }
  3007. void
  3008. g_xim_protocol_closure_free(gpointer data,
  3009. GClosure *closure)
  3010. {
  3011. GXimProtocolClosure *c;
  3012. g_return_if_fail (closure != NULL);
  3013. c = (GXimProtocolClosure *)closure;
  3014. g_free(c->param_types);
  3015. g_free(c->name);
  3016. g_slist_foreach(c->signal_handlers, (GFunc)g_free, NULL);
  3017. g_slist_free(c->signal_handlers);
  3018. }
  3019. /**
  3020. * g_xim_protocol_closure_is_an_extension:
  3021. * @closure: a #GXimProtocolClosure.
  3022. *
  3023. * Checks if the @closure was created as an extension protocol in XIM.
  3024. *
  3025. * Returns: %TRUE to be an extension.
  3026. */
  3027. gboolean
  3028. g_xim_protocol_closure_is_an_extension(GXimProtocolClosure *closure)
  3029. {
  3030. g_return_val_if_fail (closure != NULL, FALSE);
  3031. return closure->is_an_extension;
  3032. }
  3033. /**
  3034. * g_xim_protocol_closure_set_marshal:
  3035. * @closure: a #GXimProtocolClosure.
  3036. * @func: the #GXimProtocolClosureFunc function to scan the XIM protocol packet
  3037. * and emits the proper signal.
  3038. * @data: the data to store in the @data field of the #GClosure.
  3039. *
  3040. * Sets the marshaller of @closure which scans the XIM protocol packets and emit
  3041. * the proper signal as needed. Note that g_xim_protocol_closure_emit_signal()
  3042. * has to be invoked in @func otherwise any signals added by
  3043. * g_xim_protocol_closure_add_signal() won't do anything at all.
  3044. */
  3045. void
  3046. g_xim_protocol_closure_set_marshal(GXimProtocolClosure *closure,
  3047. GXimProtocolClosureFunc func,
  3048. gpointer data)
  3049. {
  3050. g_return_if_fail (closure != NULL);
  3051. g_return_if_fail (func != NULL);
  3052. ((GCClosure *)closure)->callback = (gpointer)func;
  3053. ((GClosure *)closure)->data = data;
  3054. g_closure_set_marshal((GClosure *)closure,
  3055. g_xim_protocol_closure_marshal_BOOLEAN__OBJECT_OBJECT_POINTER);
  3056. }
  3057. /**
  3058. * g_xim_protocol_closure_add_signal:
  3059. * @closure: a #GXimProtocolClosure.
  3060. * @marshaller: the #GClosureMarshal to deal with the signal.
  3061. * @n_params: the number of parameters for the signal.
  3062. * @...: a list of types, one for each parameter.
  3063. *
  3064. * Adds a marshaller for signal.
  3065. */
  3066. void
  3067. g_xim_protocol_closure_add_signal(GXimProtocolClosure *closure,
  3068. GClosureMarshal marshaller,
  3069. guint n_params,
  3070. ...)
  3071. {
  3072. va_list ap;
  3073. guint i;
  3074. GType gtype;
  3075. g_return_if_fail (closure != NULL);
  3076. g_return_if_fail (marshaller != NULL);
  3077. g_return_if_fail (closure->param_types == NULL);
  3078. va_start(ap, n_params);
  3079. closure->param_types = g_new0(GType, n_params);
  3080. for (i = 0; i < n_params; i++) {
  3081. gtype = va_arg(ap, GType);
  3082. if (!G_TYPE_IS_VALUE (gtype & ~G_SIGNAL_TYPE_STATIC_SCOPE)) {
  3083. const gchar *name;
  3084. g_warning("parameter %d of type `%s' for protocol \"%s[0x%x:0x%x]\" is not a value type",
  3085. i + 1, gtype ? ((name = g_type_name(gtype)) ? name : "<unknown>") : "<invalid>",
  3086. closure->name, closure->major_opcode.v1, closure->minor_opcode);
  3087. g_free(closure->param_types);
  3088. closure->param_types = NULL;
  3089. goto end;
  3090. }
  3091. closure->param_types[i] = gtype;
  3092. }
  3093. closure->signal_marshaller = marshaller;
  3094. closure->n_params = n_params;
  3095. end:
  3096. va_end(ap);
  3097. }
  3098. /**
  3099. * g_xim_protocol_closure_connect:
  3100. * @closure: a #GXimProtocolClosure.
  3101. * @func: the #GCallback to connect.
  3102. * @data: data to pass to @func.
  3103. *
  3104. * Connects a closure to a signal.
  3105. *
  3106. * Returns: the handler id.
  3107. */
  3108. gulong
  3109. g_xim_protocol_closure_connect(GXimProtocolClosure *closure,
  3110. GCallback func,
  3111. gpointer data)
  3112. {
  3113. GXimProtocolClosureNode *node;
  3114. g_return_val_if_fail (closure != NULL, 0);
  3115. g_return_val_if_fail (closure->signal_marshaller != NULL, 0);
  3116. g_return_val_if_fail (func != NULL, 0);
  3117. node = g_new0(GXimProtocolClosureNode, 1);
  3118. G_XIM_CHECK_ALLOC (node, 0);
  3119. node->func = func;
  3120. node->user_data = data;
  3121. closure->signal_handlers = g_slist_prepend(closure->signal_handlers, node);
  3122. return (gulong)node;
  3123. }
  3124. /**
  3125. * g_xim_protocol_closure_disconnect:
  3126. * @closure: a #GXimProtocolClosure.
  3127. * @id: Handler id of the handler to be disconnected.
  3128. *
  3129. * Disconnects a handler from an instance so it will not be called during
  3130. * any future or currently ongoing emissions of the signal if has been
  3131. * connected to. The @id becomes invalid and may be reused.
  3132. *
  3133. * The @id has to be a valid signal handler id, connected to a
  3134. * signal of @closure.
  3135. */
  3136. void
  3137. g_xim_protocol_closure_disconnect(GXimProtocolClosure *closure,
  3138. gulong id)
  3139. {
  3140. GSList *l;
  3141. g_return_if_fail (closure != NULL);
  3142. g_return_if_fail (id != 0);
  3143. l = g_slist_find(closure->signal_handlers, (gpointer)id);
  3144. if (l) {
  3145. g_free(l->data);
  3146. closure->signal_handlers = g_slist_delete_link(closure->signal_handlers, l);
  3147. }
  3148. }
  3149. /**
  3150. * g_xim_protocol_closure_emit_signal:
  3151. * @closure: a #GXimProtocolClosure.
  3152. * @proto: the instance which wants to raise a signal with @closure.
  3153. * @...: parameters to be passed to the signal.
  3154. *
  3155. * Emits a signal.
  3156. *
  3157. * Returns: %TRUE to be proceeded a signal in the handlers. %FALSE to be failed
  3158. * in any handlers or no handlers for this signal.
  3159. */
  3160. gboolean
  3161. g_xim_protocol_closure_emit_signal(GXimProtocolClosure *closure,
  3162. GXimProtocol *proto,
  3163. ...)
  3164. {
  3165. va_list ap;
  3166. GValue v = { 0, }, *pv;
  3167. GClosure *c;
  3168. GSList *l;
  3169. gboolean ret = FALSE;
  3170. guint i;
  3171. const gchar *name;
  3172. g_return_val_if_fail (closure != NULL, FALSE);
  3173. g_return_val_if_fail (closure->signal_marshaller != NULL, FALSE);
  3174. g_value_init(&v, G_TYPE_BOOLEAN);
  3175. pv = g_new0(GValue, closure->n_params + 1);
  3176. G_XIM_CHECK_ALLOC (pv, FALSE);
  3177. g_value_init(&pv[0], G_TYPE_OBJECT);
  3178. g_value_set_object(&pv[0], proto);
  3179. va_start(ap, proto);
  3180. for (i = 0; i < closure->n_params; i++) {
  3181. if (g_type_is_a(closure->param_types[i], G_TYPE_OBJECT))
  3182. g_value_init(&pv[i + 1], G_TYPE_OBJECT);
  3183. else
  3184. g_value_init(&pv[i + 1], closure->param_types[i]);
  3185. switch (closure->param_types[i]) {
  3186. case G_TYPE_BOOLEAN:
  3187. g_value_set_boolean(&pv[i + 1], va_arg(ap, gboolean));
  3188. break;
  3189. case G_TYPE_CHAR:
  3190. g_value_set_schar(&pv[i + 1], va_arg(ap, gint));
  3191. break;
  3192. case G_TYPE_UCHAR:
  3193. g_value_set_uchar(&pv[i + 1], va_arg(ap, guint));
  3194. break;
  3195. case G_TYPE_INT:
  3196. g_value_set_int(&pv[i + 1], va_arg(ap, gint));
  3197. break;
  3198. case G_TYPE_UINT:
  3199. g_value_set_uint(&pv[i + 1], va_arg(ap, guint));
  3200. break;
  3201. case G_TYPE_LONG:
  3202. g_value_set_long(&pv[i + 1], va_arg(ap, glong));
  3203. break;
  3204. case G_TYPE_ULONG:
  3205. g_value_set_ulong(&pv[i + 1], va_arg(ap, gulong));
  3206. break;
  3207. case G_TYPE_INT64:
  3208. g_value_set_int64(&pv[i + 1], va_arg(ap, gint64));
  3209. break;
  3210. case G_TYPE_UINT64:
  3211. g_value_set_uint64(&pv[i + 1], va_arg(ap, guint64));
  3212. break;
  3213. case G_TYPE_FLOAT:
  3214. g_value_set_float(&pv[i + 1], va_arg(ap, gdouble));
  3215. break;
  3216. case G_TYPE_DOUBLE:
  3217. g_value_set_double(&pv[i + 1], va_arg(ap, gdouble));
  3218. break;
  3219. case G_TYPE_STRING:
  3220. g_value_set_string(&pv[i + 1], va_arg(ap, gchar *));
  3221. break;
  3222. case G_TYPE_POINTER:
  3223. g_value_set_pointer(&pv[i + 1], va_arg(ap, gpointer));
  3224. break;
  3225. case G_TYPE_ENUM:
  3226. g_value_set_enum(&pv[i + 1], va_arg(ap, gint));
  3227. break;
  3228. case G_TYPE_FLAGS:
  3229. g_value_set_flags(&pv[i + 1], va_arg(ap, guint));
  3230. break;
  3231. case G_TYPE_PARAM:
  3232. g_value_set_param(&pv[i + 1], va_arg(ap, GParamSpec *));
  3233. break;
  3234. case G_TYPE_BOXED:
  3235. g_value_set_boxed(&pv[i + 1], va_arg(ap, gpointer));
  3236. break;
  3237. default:
  3238. if (g_type_is_a(closure->param_types[i], G_TYPE_GTYPE)) {
  3239. g_value_set_gtype(&pv[i + 1], va_arg(ap, GType));
  3240. } else if (g_type_is_a(closure->param_types[i], G_TYPE_OBJECT)) {
  3241. g_value_set_object(&pv[i + 1], va_arg(ap, gpointer));
  3242. } else if (G_TYPE_IS_VALUE (closure->param_types[i])) {
  3243. g_value_set_boxed(&pv[i + 1], va_arg(ap, gpointer));
  3244. } else {
  3245. g_xim_messages_warning(G_XIM_PROTOCOL_GET_IFACE (proto)->message,
  3246. "Unsupported value type `%s' to emit a signal for %s",
  3247. closure->param_types[i] ? ((name = g_type_name(closure->param_types[i])) ? name : "<unknown>") : "<invalid>",
  3248. closure->name);
  3249. goto end;
  3250. }
  3251. break;
  3252. }
  3253. }
  3254. for (l = closure->signal_handlers; l != NULL; l = g_slist_next(l)) {
  3255. GXimProtocolClosureNode *node = l->data;
  3256. c = g_cclosure_new(node->func, node->user_data, NULL);
  3257. G_XIM_CHECK_ALLOC (c, FALSE);
  3258. g_closure_set_marshal(c, closure->signal_marshaller);
  3259. g_closure_invoke(c, &v, closure->n_params + 1, pv, NULL);
  3260. ret = g_value_get_boolean(&v);
  3261. g_closure_unref(c);
  3262. if (ret)
  3263. break;
  3264. }
  3265. for (i = 0; i < closure->n_params + 1; i++) {
  3266. g_value_unset(&pv[i]);
  3267. }
  3268. end:
  3269. g_free(pv);
  3270. va_end(ap);
  3271. return ret;
  3272. }