PageRenderTime 61ms CodeModel.GetById 20ms 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

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

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
  2. /*
  3. * gximprotocol.c
  4. * Copyright (C) 2008-2011 Akira TAGOH
  5. *
  6. * Authors:
  7. * Akira TAGOH <akira@tagoh.org>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth
  22. * Floor, Boston, MA 02110-1301 USA
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #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_S

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