version 1.4.30b
[polyglot.git] / mainloop.c
diff --git a/mainloop.c b/mainloop.c
new file mode 100644 (file)
index 0000000..d41e713
--- /dev/null
@@ -0,0 +1,103 @@
+// mainloop.c\r
+\r
+// constants\r
+\r
+static const int StringSize = 4096;\r
+\r
+// includes\r
+\r
+#include <errno.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+\r
+#include "main.h"\r
+#include "engine.h"\r
+#include "gui.h"\r
+#include "option.h"\r
+#include "xboard2uci.h"\r
+#include "uci2uci.h"\r
+\r
+// prototypes\r
+\r
+static void mainloop_init            ();\r
+static void mainloop_wait_for_event  ();\r
+static void mainloop_engine_step(char * string);\r
+static void mainloop_gui_step(char * string);\r
+\r
+// functions\r
+\r
+// mainloop_init()\r
+    \r
+static void mainloop_init(){\r
+    if(!option_get_bool("UCI")){\r
+        xboard2uci_init();  // the default\r
+    }\r
+}\r
+\r
+// mainloop_engine_step()\r
+\r
+static void mainloop_engine_step(char * string){\r
+    if(option_get_bool("UCI")){\r
+        uci2uci_engine_step(string); \r
+    }else{\r
+        xboard2uci_engine_step(string);\r
+    }\r
+}\r
+\r
+// mainloop_gui_step()\r
+\r
+static void mainloop_gui_step(char * string){\r
+    if(option_get_bool("UCI")){\r
+        uci2uci_gui_step(string); \r
+    }else if(my_string_equal(string,"uci")){ // mode auto detection\r
+        my_log("POLYGLOT *** Switching to UCI mode ***\n");\r
+        option_set("UCI","TRUE");\r
+        uci2uci_gui_step(string);\r
+    }else{\r
+        xboard2uci_gui_step(string);\r
+    }\r
+}\r
+\r
+// mainloop()\r
+\r
+void mainloop() {\r
+    char string[StringSize];\r
+    mainloop_init();\r
+    while (!engine_eof(Engine)) {\r
+            // process buffered lines\r
+        while(TRUE){\r
+            if(gui_get_non_blocking(GUI,string)){\r
+                mainloop_gui_step(string);\r
+            }else if(!engine_eof(Engine) &&\r
+                     engine_get_non_blocking(Engine,string) ){\r
+                mainloop_engine_step(string);\r
+            }else{\r
+                break;\r
+            }\r
+        }\r
+        mainloop_wait_for_event();\r
+    }\r
+    my_log("POLYGLOT *** Mainloop has ended ***\n");\r
+    // This should be handled better.\r
+    engine_close(Engine);\r
+    my_log("POLYGLOT Calling exit\n");\r
+    exit(EXIT_SUCCESS);\r
+\r
+}\r
+\r
+\r
+\r
+\r
+// mainloop_wait_for_event()\r
+\r
+static void mainloop_wait_for_event(){\r
+    pipex_t *pipex[3];\r
+    pipex[0]=GUI->pipex;\r
+    pipex[1]=Engine->pipex;\r
+    pipex[2]=NULL;\r
+    pipex_wait_event(pipex);\r
+}\r
+\r
+\r
+\r