20
20
#include "config.h"
25
22
#include <string.h>
26
#ifdef HAVE_SYS_PARAM_H
27
#include <sys/param.h>
29
#include <sys/types.h>
26
#include <libgimpcolor/gimpcolortypes.h>
41
28
#include "gimpwire.h"
44
typedef struct _WireHandler WireHandler;
31
typedef struct _GimpWireHandler GimpWireHandler;
33
struct _GimpWireHandler
49
WireReadFunc read_func;
50
WireWriteFunc write_func;
51
WireDestroyFunc destroy_func;
36
GimpWireReadFunc read_func;
37
GimpWireWriteFunc write_func;
38
GimpWireDestroyFunc destroy_func;
55
static void wire_init (void);
56
static guint wire_hash (guint32 *key);
57
static gboolean wire_compare (guint32 *a,
61
static GHashTable *wire_ht = NULL;
62
static WireIOFunc wire_read_func = NULL;
63
static WireIOFunc wire_write_func = NULL;
64
static WireFlushFunc wire_flush_func = NULL;
65
static gboolean wire_error_val = FALSE;
42
static GHashTable *wire_ht = NULL;
43
static GimpWireIOFunc wire_read_func = NULL;
44
static GimpWireIOFunc wire_write_func = NULL;
45
static GimpWireFlushFunc wire_flush_func = NULL;
46
static gboolean wire_error_val = FALSE;
49
static void gimp_wire_init (void);
69
wire_register (guint32 type,
70
WireReadFunc read_func,
71
WireWriteFunc write_func,
72
WireDestroyFunc destroy_func)
53
gimp_wire_register (guint32 type,
54
GimpWireReadFunc read_func,
55
GimpWireWriteFunc write_func,
56
GimpWireDestroyFunc destroy_func)
58
GimpWireHandler *handler;
79
63
handler = g_hash_table_lookup (wire_ht, &type);
81
handler = g_new0 (WireHandler, 1);
66
handler = g_new0 (GimpWireHandler, 1);
83
68
handler->type = type;
84
69
handler->read_func = read_func;
92
wire_set_reader (WireIOFunc read_func)
77
gimp_wire_set_reader (GimpWireIOFunc read_func)
94
79
wire_read_func = read_func;
98
wire_set_writer (WireIOFunc write_func)
83
gimp_wire_set_writer (GimpWireIOFunc write_func)
100
85
wire_write_func = write_func;
104
wire_set_flusher (WireFlushFunc flush_func)
89
gimp_wire_set_flusher (GimpWireFlushFunc flush_func)
106
91
wire_flush_func = flush_func;
110
wire_read (GIOChannel *channel,
95
gimp_wire_read (GIOChannel *channel,
115
100
g_return_val_if_fail (count >= 0, FALSE);
178
wire_write (GIOChannel *channel,
163
gimp_wire_write (GIOChannel *channel,
183
168
g_return_val_if_fail (count >= 0, FALSE);
185
170
if (wire_write_func)
187
if (!(* wire_write_func) (channel, buf, count, user_data))
172
if (!(* wire_write_func) (channel, (guint8 *) buf, count, user_data))
189
174
g_warning ("%s: wire_write: error", g_get_prgname ());
190
175
wire_error_val = TRUE;
205
190
status = g_io_channel_write_chars (channel,
206
(gchar *) buf, count,
191
(const gchar *) buf, count,
210
while (status == G_IO_STATUS_AGAIN);
195
while (G_UNLIKELY (status == G_IO_STATUS_AGAIN));
212
if (status != G_IO_STATUS_NORMAL)
197
if (G_UNLIKELY (status != G_IO_STATUS_NORMAL))
239
wire_flush (GIOChannel *channel,
224
gimp_wire_flush (GIOChannel *channel,
242
227
if (wire_flush_func)
243
228
return (* wire_flush_func) (channel, user_data);
234
gimp_wire_error (void)
251
236
return wire_error_val;
255
wire_clear_error (void)
240
gimp_wire_clear_error (void)
257
242
wire_error_val = FALSE;
261
wire_read_msg (GIOChannel *channel,
246
gimp_wire_read_msg (GIOChannel *channel,
247
GimpWireMessage *msg,
265
WireHandler *handler;
250
GimpWireHandler *handler;
267
252
if (wire_error_val)
268
253
return !wire_error_val;
270
if (! wire_read_int32 (channel, &msg->type, 1, user_data))
255
if (! _gimp_wire_read_int32 (channel, &msg->type, 1, user_data))
273
258
handler = g_hash_table_lookup (wire_ht, &msg->type);
275
260
g_error ("could not find handler for message: %d", msg->type);
277
262
(* handler->read_func) (channel, msg, user_data);
283
wire_write_msg (GIOChannel *channel,
268
gimp_wire_write_msg (GIOChannel *channel,
269
GimpWireMessage *msg,
287
WireHandler *handler;
272
GimpWireHandler *handler;
289
274
if (wire_error_val)
290
275
return !wire_error_val;
292
277
handler = g_hash_table_lookup (wire_ht, &msg->type);
294
279
g_error ("could not find handler for message: %d", msg->type);
296
if (! wire_write_int32 (channel, &msg->type, 1, user_data))
281
if (! _gimp_wire_write_int32 (channel, &msg->type, 1, user_data))
299
284
(* handler->write_func) (channel, msg, user_data);
305
wire_destroy (WireMessage *msg)
290
gimp_wire_destroy (GimpWireMessage *msg)
307
WireHandler *handler;
292
GimpWireHandler *handler;
309
294
handler = g_hash_table_lookup (wire_ht, &msg->type);
317
wire_read_int32 (GIOChannel *channel,
302
_gimp_wire_read_int32 (GIOChannel *channel,
322
307
g_return_val_if_fail (count >= 0, FALSE);
326
if (! wire_read_int8 (channel, (guint8 *) data, count * 4, user_data))
311
if (! _gimp_wire_read_int8 (channel,
312
(guint8 *) data, count * 4, user_data))
340
wire_read_int16 (GIOChannel *channel,
326
_gimp_wire_read_int16 (GIOChannel *channel,
345
331
g_return_val_if_fail (count >= 0, FALSE);
349
if (! wire_read_int8 (channel, (guint8 *) data, count * 2, user_data))
335
if (! _gimp_wire_read_int8 (channel,
336
(guint8 *) data, count * 2, user_data))
363
wire_read_int8 (GIOChannel *channel,
350
_gimp_wire_read_int8 (GIOChannel *channel,
368
355
g_return_val_if_fail (count >= 0, FALSE);
370
return wire_read (channel, data, count, user_data);
357
return gimp_wire_read (channel, data, count, user_data);
374
wire_read_double (GIOChannel *channel,
361
_gimp_wire_read_double (GIOChannel *channel,
382
369
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
387
373
g_return_val_if_fail (count >= 0, FALSE);
391
377
for (i = 0; i < count; i++)
393
if (! wire_read_int8 (channel, tmp, 8, user_data))
379
if (! _gimp_wire_read_int8 (channel, tmp, 8, user_data))
396
382
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
397
383
for (j = 0; j < 4; j++)
401
389
tmp[7 - j] = swap;
422
410
for (i = 0; i < count; i++)
424
if (!wire_read_int32 (channel, &tmp, 1, user_data))
412
if (! _gimp_wire_read_int32 (channel, &tmp, 1, user_data))
429
417
data[i] = g_new (gchar, tmp);
430
if (! wire_read_int8 (channel, (guint8 *) data[i], tmp, user_data))
418
if (! _gimp_wire_read_int8 (channel,
419
(guint8 *) data[i], tmp, user_data))
432
421
g_free (data[i]);
446
wire_write_int32 (GIOChannel *channel,
435
_gimp_wire_read_color (GIOChannel *channel,
440
g_return_val_if_fail (count >= 0, FALSE);
442
return _gimp_wire_read_double (channel,
443
(gdouble *) data, 4 * count, user_data);
447
_gimp_wire_write_int32 (GIOChannel *channel,
458
459
for (i = 0; i < count; i++)
460
461
tmp = g_htonl (data[i]);
461
if (! wire_write_int8 (channel, (guint8 *) &tmp, 4, user_data))
462
if (! _gimp_wire_write_int8 (channel,
463
(const guint8 *) &tmp, 4, user_data))
482
484
for (i = 0; i < count; i++)
484
486
tmp = g_htons (data[i]);
485
if (! wire_write_int8 (channel, (guint8 *) &tmp, 2, user_data))
487
if (! _gimp_wire_write_int8 (channel,
488
(const guint8 *) &tmp, 2, user_data))
494
wire_write_int8 (GIOChannel *channel,
497
_gimp_wire_write_int8 (GIOChannel *channel,
499
502
g_return_val_if_fail (count >= 0, FALSE);
501
return wire_write (channel, data, count, user_data);
504
return gimp_wire_write (channel, data, count, user_data);
505
wire_write_double (GIOChannel *channel,
508
_gimp_wire_write_double (GIOChannel *channel,
513
516
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
518
520
g_return_val_if_fail (count >= 0, FALSE);
526
528
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
527
529
for (j = 0; j < 4; j++)
531
535
tmp[7 - j] = swap;
535
if (! wire_write_int8 (channel, tmp, 8, user_data))
539
if (! _gimp_wire_write_int8 (channel, tmp, 8, user_data))
573
if (! wire_write_int32 (channel, &tmp, 1, user_data))
577
if (! _gimp_wire_write_int32 (channel, &tmp, 1, user_data))
576
if (! wire_write_int8 (channel, (guint8 *) data[i], tmp, user_data))
581
if (! _gimp_wire_write_int8 (channel,
582
(const guint8 *) data[i], tmp, user_data))
590
_gimp_wire_write_color (GIOChannel *channel,
587
wire_ht = g_hash_table_new ((GHashFunc) wire_hash,
588
(GCompareFunc) wire_compare);
595
g_return_val_if_fail (count >= 0, FALSE);
597
return _gimp_wire_write_double (channel,
598
(gdouble *) data, 4 * count, user_data);
592
wire_hash (guint32 *key)
602
gimp_wire_hash (const guint32 *key)
598
wire_compare (guint32 *a,
608
gimp_wire_compare (const guint32 *a,
601
611
return (*a == *b);
615
gimp_wire_init (void)
618
wire_ht = g_hash_table_new ((GHashFunc) gimp_wire_hash,
619
(GCompareFunc) gimp_wire_compare);