diff --git a/src/chess-engine.vala b/src/chess-engine.vala index fe12607..0792f3b 100644 --- a/src/chess-engine.vala +++ b/src/chess-engine.vala @@ -35,9 +35,9 @@ public class ChessEngine : Object try { Process.spawn_async_with_pipes (null, argv, null, - SpawnFlags.SEARCH_PATH, - null, - out pid, out stdin_fd, out stdout_fd, out stderr_fd); + SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD, + null, + out pid, out stdin_fd, out stdout_fd, out stderr_fd); } catch (SpawnError e) { @@ -45,6 +45,8 @@ public class ChessEngine : Object return false; } + ChildWatch.add (pid, engine_stopped_cb); + stdout_channel = new IOChannel.unix_new (stdout_fd); try { @@ -60,7 +62,12 @@ public class ChessEngine : Object return true; } - + + private void engine_stopped_cb (Pid pid, int status) + { + stopped (); + } + public virtual void start_game () { } @@ -79,8 +86,8 @@ public class ChessEngine : Object public void stop () { - // FIXME - stopped (); + if (pid != 0) + Posix.kill (pid, Posix.SIGTERM); } private bool read_cb (IOChannel source, IOCondition condition) diff --git a/src/glchess.vala b/src/glchess.vala index 41e624f..af9f4b3 100644 --- a/src/glchess.vala +++ b/src/glchess.vala @@ -124,6 +124,13 @@ public class Application : Gtk.Application settings_changed_cb (settings, "show-3d"); } + protected override void shutdown () + { + base.shutdown (); + if (opponent_engine != null) + opponent_engine.stop (); + } + public void quit_game () { if (save_duration_timeout != 0) @@ -298,7 +305,14 @@ public class Application : Gtk.Application black_level = "normal"; opponent = null; - opponent_engine = null; + if (opponent_engine != null) + { + opponent_engine.stop (); + opponent_engine.ready_changed.disconnect (engine_ready_cb); + opponent_engine.moved.disconnect (engine_move_cb); + opponent_engine.stopped.disconnect (engine_stopped_cb); + opponent_engine = null; + } if (white_engine != null) { opponent = game.white; @@ -316,6 +330,7 @@ public class Application : Gtk.Application { opponent_engine.ready_changed.connect (engine_ready_cb); opponent_engine.moved.connect (engine_move_cb); + opponent_engine.stopped.connect (engine_stopped_cb); opponent_engine.start (); } @@ -436,6 +451,11 @@ public class Application : Gtk.Application opponent.move (move); } + private void engine_stopped_cb (ChessEngine engine) + { + opponent.resign (); + } + private void game_start_cb (ChessGame game) { if (opponent_engine != null)