1
/* chess-engine.c generated by valac 0.12.0, the Vala compiler
2
* generated from chess-engine.vala, do not modify */
6
#include <glib-object.h>
13
#define TYPE_CHESS_ENGINE (chess_engine_get_type ())
14
#define CHESS_ENGINE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CHESS_ENGINE, ChessEngine))
15
#define CHESS_ENGINE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CHESS_ENGINE, ChessEngineClass))
16
#define IS_CHESS_ENGINE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CHESS_ENGINE))
17
#define IS_CHESS_ENGINE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CHESS_ENGINE))
18
#define CHESS_ENGINE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CHESS_ENGINE, ChessEngineClass))
20
typedef struct _ChessEngine ChessEngine;
21
typedef struct _ChessEngineClass ChessEngineClass;
22
typedef struct _ChessEnginePrivate ChessEnginePrivate;
24
#define TYPE_CHESS_MOVE (chess_move_get_type ())
25
#define CHESS_MOVE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CHESS_MOVE, ChessMove))
26
#define CHESS_MOVE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CHESS_MOVE, ChessMoveClass))
27
#define IS_CHESS_MOVE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CHESS_MOVE))
28
#define IS_CHESS_MOVE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CHESS_MOVE))
29
#define CHESS_MOVE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CHESS_MOVE, ChessMoveClass))
31
typedef struct _ChessMove ChessMove;
32
typedef struct _ChessMoveClass ChessMoveClass;
33
#define _g_free0(var) (var = (g_free (var), NULL))
34
#define _g_io_channel_unref0(var) ((var == NULL) ? NULL : (var = (g_io_channel_unref (var), NULL)))
35
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
38
GObject parent_instance;
39
ChessEnginePrivate * priv;
43
struct _ChessEngineClass {
44
GObjectClass parent_class;
45
void (*process_input) (ChessEngine* self, gchar* data, int data_length1);
46
void (*start_game) (ChessEngine* self);
47
void (*request_move) (ChessEngine* self);
48
void (*report_move) (ChessEngine* self, ChessMove* move);
49
void (*undo) (ChessEngine* self);
52
struct _ChessEnginePrivate {
56
GIOChannel* stdout_channel;
61
static gpointer chess_engine_parent_class = NULL;
63
GType chess_engine_get_type (void) G_GNUC_CONST;
64
gpointer chess_move_ref (gpointer instance);
65
void chess_move_unref (gpointer instance);
66
GParamSpec* param_spec_chess_move (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
67
void value_set_chess_move (GValue* value, gpointer v_object);
68
void value_take_chess_move (GValue* value, gpointer v_object);
69
gpointer value_get_chess_move (const GValue* value);
70
GType chess_move_get_type (void) G_GNUC_CONST;
71
#define CHESS_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_CHESS_ENGINE, ChessEnginePrivate))
73
CHESS_ENGINE_DUMMY_PROPERTY,
76
void chess_engine_process_input (ChessEngine* self, gchar* data, int data_length1);
77
static void chess_engine_real_process_input (ChessEngine* self, gchar* data, int data_length1);
78
gboolean chess_engine_start (ChessEngine* self);
79
static gboolean chess_engine_read_cb (ChessEngine* self, GIOChannel* source, GIOCondition condition);
80
static gboolean _chess_engine_read_cb_gio_func (GIOChannel* source, GIOCondition condition, gpointer self);
81
void chess_engine_start_game (ChessEngine* self);
82
static void chess_engine_real_start_game (ChessEngine* self);
83
void chess_engine_request_move (ChessEngine* self);
84
static void chess_engine_real_request_move (ChessEngine* self);
85
void chess_engine_report_move (ChessEngine* self, ChessMove* move);
86
static void chess_engine_real_report_move (ChessEngine* self, ChessMove* move);
87
void chess_engine_undo (ChessEngine* self);
88
static void chess_engine_real_undo (ChessEngine* self);
89
void chess_engine_stop (ChessEngine* self);
90
void chess_engine_write (ChessEngine* self, gchar* data, int data_length1);
91
void chess_engine_write_line (ChessEngine* self, const gchar* line);
92
ChessEngine* chess_engine_new (void);
93
ChessEngine* chess_engine_construct (GType object_type);
94
gboolean chess_engine_get_ready (ChessEngine* self);
95
void chess_engine_set_ready (ChessEngine* self, gboolean value);
96
static void chess_engine_finalize (GObject* obj);
97
static void _vala_chess_engine_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
98
static void _vala_chess_engine_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);
99
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
100
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);
103
static void chess_engine_real_process_input (ChessEngine* self, gchar* data, int data_length1) {
104
g_return_if_fail (self != NULL);
108
void chess_engine_process_input (ChessEngine* self, gchar* data, int data_length1) {
109
CHESS_ENGINE_GET_CLASS (self)->process_input (self, data, data_length1);
113
static gboolean _chess_engine_read_cb_gio_func (GIOChannel* source, GIOCondition condition, gpointer self) {
115
result = chess_engine_read_cb (self, source, condition);
120
gboolean chess_engine_start (ChessEngine* self) {
121
gboolean result = FALSE;
123
gchar** _tmp1_ = NULL;
132
GIOChannel* _tmp6_ = NULL;
133
GError * _inner_error_ = NULL;
134
g_return_val_if_fail (self != NULL, FALSE);
135
_tmp0_ = g_strdup (self->binary);
136
_tmp1_ = g_new0 (gchar*, 2 + 1);
142
g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &_tmp2_, &_tmp3_, &_tmp4_, &_tmp5_, &_inner_error_);
143
self->priv->pid = _tmp2_;
144
self->priv->stdin_fd = _tmp3_;
146
self->priv->stderr_fd = _tmp5_;
147
if (_inner_error_ != NULL) {
148
if (_inner_error_->domain == G_SPAWN_ERROR) {
149
goto __catch9_g_spawn_error;
151
argv = (_vala_array_free (argv, argv_length1, (GDestroyNotify) g_free), NULL);
152
g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
153
g_clear_error (&_inner_error_);
157
__catch9_g_spawn_error:
161
_inner_error_ = NULL;
162
fprintf (stderr, "Failed to execute chess engine: %s\n", e->message);
165
argv = (_vala_array_free (argv, argv_length1, (GDestroyNotify) g_free), NULL);
169
if (_inner_error_ != NULL) {
170
argv = (_vala_array_free (argv, argv_length1, (GDestroyNotify) g_free), NULL);
171
g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
172
g_clear_error (&_inner_error_);
175
_tmp6_ = g_io_channel_unix_new (stdout_fd);
176
_g_io_channel_unref0 (self->priv->stdout_channel);
177
self->priv->stdout_channel = _tmp6_;
178
g_io_channel_set_flags (self->priv->stdout_channel, G_IO_FLAG_NONBLOCK, &_inner_error_);
179
if (_inner_error_ != NULL) {
180
if (_inner_error_->domain == G_IO_CHANNEL_ERROR) {
181
goto __catch10_g_io_channel_error;
183
argv = (_vala_array_free (argv, argv_length1, (GDestroyNotify) g_free), NULL);
184
g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
185
g_clear_error (&_inner_error_);
189
__catch10_g_io_channel_error:
193
_inner_error_ = NULL;
194
fprintf (stderr, "Failed to set input from chess engine to non-blocking: %s", e->message);
198
if (_inner_error_ != NULL) {
199
argv = (_vala_array_free (argv, argv_length1, (GDestroyNotify) g_free), NULL);
200
g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
201
g_clear_error (&_inner_error_);
204
g_io_add_watch (self->priv->stdout_channel, G_IO_IN, _chess_engine_read_cb_gio_func, self);
205
g_signal_emit_by_name (self, "starting");
207
argv = (_vala_array_free (argv, argv_length1, (GDestroyNotify) g_free), NULL);
212
static void chess_engine_real_start_game (ChessEngine* self) {
213
g_return_if_fail (self != NULL);
217
void chess_engine_start_game (ChessEngine* self) {
218
CHESS_ENGINE_GET_CLASS (self)->start_game (self);
222
static void chess_engine_real_request_move (ChessEngine* self) {
223
g_return_if_fail (self != NULL);
227
void chess_engine_request_move (ChessEngine* self) {
228
CHESS_ENGINE_GET_CLASS (self)->request_move (self);
232
static void chess_engine_real_report_move (ChessEngine* self, ChessMove* move) {
233
g_return_if_fail (self != NULL);
234
g_return_if_fail (move != NULL);
238
void chess_engine_report_move (ChessEngine* self, ChessMove* move) {
239
CHESS_ENGINE_GET_CLASS (self)->report_move (self, move);
243
static void chess_engine_real_undo (ChessEngine* self) {
244
g_return_if_fail (self != NULL);
248
void chess_engine_undo (ChessEngine* self) {
249
CHESS_ENGINE_GET_CLASS (self)->undo (self);
253
void chess_engine_stop (ChessEngine* self) {
254
g_return_if_fail (self != NULL);
255
g_signal_emit_by_name (self, "stopped");
259
static gboolean chess_engine_read_cb (ChessEngine* self, GIOChannel* source, GIOCondition condition) {
260
gboolean result = FALSE;
262
gint buf_length1 = 0;
265
GIOStatus status = 0;
266
gchar* _tmp0_ = NULL;
270
GError * _inner_error_ = NULL;
271
g_return_val_if_fail (self != NULL, FALSE);
272
g_return_val_if_fail (source != NULL, FALSE);
273
_tmp0_ = g_new0 (gchar, 1024);
274
buf = (g_free (buf), NULL);
278
_tmp2_ = g_io_channel_read_chars (source, buf, buf_length1, &_tmp1_, &_inner_error_);
281
if (_inner_error_ != NULL) {
282
if (_inner_error_->domain == G_CONVERT_ERROR) {
283
goto __catch11_g_convert_error;
285
if (_inner_error_->domain == G_IO_CHANNEL_ERROR) {
286
goto __catch11_g_io_channel_error;
288
buf = (g_free (buf), NULL);
289
g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
290
g_clear_error (&_inner_error_);
295
__catch11_g_convert_error:
299
_inner_error_ = NULL;
302
buf = (g_free (buf), NULL);
306
__catch11_g_io_channel_error:
310
_inner_error_ = NULL;
313
buf = (g_free (buf), NULL);
317
if (_inner_error_ != NULL) {
318
buf = (g_free (buf), NULL);
319
g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
320
g_clear_error (&_inner_error_);
323
if (status == G_IO_STATUS_EOF) {
324
fprintf (stdout, "EOF\n");
326
buf = (g_free (buf), NULL);
329
if (status == G_IO_STATUS_NORMAL) {
331
_tmp4_ = (gint) n_read;
332
buf = g_renew (gchar, buf, (gint) n_read);
333
(_tmp4_ > buf_length1) ? memset (buf + buf_length1, 0, sizeof (gchar) * (_tmp4_ - buf_length1)) : NULL;
334
buf_length1 = _tmp4_;
336
chess_engine_process_input (self, buf, buf_length1);
339
buf = (g_free (buf), NULL);
344
void chess_engine_write (ChessEngine* self, gchar* data, int data_length1) {
346
gsize n_written = 0UL;
347
g_return_if_fail (self != NULL);
351
gchar* _tmp1_ = NULL;
356
if (!(offset < data_length1)) {
359
_tmp1_ = g_new0 (gchar, _tmp0_ = data_length1 - offset);
374
if (!(i < (data_length1 - offset))) {
377
d[i] = data[offset + i];
381
_tmp3_ = write (self->priv->stdin_fd, d, (gsize) d_length1);
382
n_written = (gsize) _tmp3_;
384
d = (g_free (d), NULL);
387
offset = offset + n_written;
388
d = (g_free (d), NULL);
393
static gchar* string_to_utf8 (const gchar* self, int* result_length1) {
394
gchar* result = NULL;
396
gchar* _tmp1_ = NULL;
398
gint _result__length1;
399
gint __result__size_;
402
g_return_val_if_fail (self != NULL, NULL);
403
_tmp0_ = strlen (self);
404
_tmp1_ = g_new0 (gchar, _tmp0_ + 1);
406
_result__length1 = _tmp0_ + 1;
407
__result__size_ = _tmp0_ + 1;
409
_tmp2_ = strlen (self);
410
memcpy (_result_, self, (gsize) _tmp2_);
412
*result_length1 = _result__length1;
418
void chess_engine_write_line (ChessEngine* self, const gchar* line) {
422
gchar* _tmp2_ = NULL;
426
g_return_if_fail (self != NULL);
427
g_return_if_fail (line != NULL);
428
_tmp0_ = g_strconcat (line, "\n", NULL);
430
g_debug ("chess-engine.vala:146: Writing line to engine: '%s'", line);
431
_tmp2_ = string_to_utf8 (l, &_tmp1_);
436
chess_engine_write (self, d, d_length1);
438
d = (g_free (d), NULL);
443
ChessEngine* chess_engine_construct (GType object_type) {
444
ChessEngine * self = NULL;
445
self = (ChessEngine*) g_object_new (object_type, NULL);
450
ChessEngine* chess_engine_new (void) {
451
return chess_engine_construct (TYPE_CHESS_ENGINE);
455
gboolean chess_engine_get_ready (ChessEngine* self) {
457
g_return_val_if_fail (self != NULL, FALSE);
458
result = self->priv->_ready;
463
void chess_engine_set_ready (ChessEngine* self, gboolean value) {
464
g_return_if_fail (self != NULL);
465
self->priv->_ready = value;
466
g_signal_emit_by_name (self, "ready-changed");
467
g_object_notify ((GObject *) self, "ready");
471
static void chess_engine_class_init (ChessEngineClass * klass) {
472
chess_engine_parent_class = g_type_class_peek_parent (klass);
473
g_type_class_add_private (klass, sizeof (ChessEnginePrivate));
474
CHESS_ENGINE_CLASS (klass)->process_input = chess_engine_real_process_input;
475
CHESS_ENGINE_CLASS (klass)->start_game = chess_engine_real_start_game;
476
CHESS_ENGINE_CLASS (klass)->request_move = chess_engine_real_request_move;
477
CHESS_ENGINE_CLASS (klass)->report_move = chess_engine_real_report_move;
478
CHESS_ENGINE_CLASS (klass)->undo = chess_engine_real_undo;
479
G_OBJECT_CLASS (klass)->get_property = _vala_chess_engine_get_property;
480
G_OBJECT_CLASS (klass)->set_property = _vala_chess_engine_set_property;
481
G_OBJECT_CLASS (klass)->finalize = chess_engine_finalize;
482
g_object_class_install_property (G_OBJECT_CLASS (klass), CHESS_ENGINE_READY, g_param_spec_boolean ("ready", "ready", "ready", FALSE, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
483
g_signal_new ("starting", TYPE_CHESS_ENGINE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
484
g_signal_new ("ready_changed", TYPE_CHESS_ENGINE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
485
g_signal_new ("moved", TYPE_CHESS_ENGINE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
486
g_signal_new ("resigned", TYPE_CHESS_ENGINE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
487
g_signal_new ("stopped", TYPE_CHESS_ENGINE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
491
static void chess_engine_instance_init (ChessEngine * self) {
492
self->priv = CHESS_ENGINE_GET_PRIVATE (self);
493
self->priv->_ready = FALSE;
497
static void chess_engine_finalize (GObject* obj) {
499
self = CHESS_ENGINE (obj);
500
_g_free0 (self->binary);
501
_g_io_channel_unref0 (self->priv->stdout_channel);
502
G_OBJECT_CLASS (chess_engine_parent_class)->finalize (obj);
506
GType chess_engine_get_type (void) {
507
static volatile gsize chess_engine_type_id__volatile = 0;
508
if (g_once_init_enter (&chess_engine_type_id__volatile)) {
509
static const GTypeInfo g_define_type_info = { sizeof (ChessEngineClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chess_engine_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChessEngine), 0, (GInstanceInitFunc) chess_engine_instance_init, NULL };
510
GType chess_engine_type_id;
511
chess_engine_type_id = g_type_register_static (G_TYPE_OBJECT, "ChessEngine", &g_define_type_info, 0);
512
g_once_init_leave (&chess_engine_type_id__volatile, chess_engine_type_id);
514
return chess_engine_type_id__volatile;
518
static void _vala_chess_engine_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
520
self = CHESS_ENGINE (object);
521
switch (property_id) {
522
case CHESS_ENGINE_READY:
523
g_value_set_boolean (value, chess_engine_get_ready (self));
526
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
532
static void _vala_chess_engine_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
534
self = CHESS_ENGINE (object);
535
switch (property_id) {
536
case CHESS_ENGINE_READY:
537
chess_engine_set_ready (self, g_value_get_boolean (value));
540
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
546
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
547
if ((array != NULL) && (destroy_func != NULL)) {
549
for (i = 0; i < array_length; i = i + 1) {
550
if (((gpointer*) array)[i] != NULL) {
551
destroy_func (((gpointer*) array)[i]);
558
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
559
_vala_array_destroy (array, array_length, destroy_func);