Add forgotten files 1.4.70b
[polyglot.git] / mainloop.c
1 // mainloop.c
2
3 // includes
4
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "main.h"
11 #include "engine.h"
12 #include "gui.h"
13 #include "option.h"
14 //#include "ini.h"
15 #include "xboard2uci.h"
16 #include "uci2uci.h"
17
18 // prototypes
19
20 static void mainloop_init            ();
21 static void mainloop_wait_for_event  ();
22 static void mainloop_engine_step(char * string);
23 static void mainloop_gui_step(char * string);
24
25 // functions
26
27 // mainloop_init()
28     
29 static void mainloop_init(){
30     if(!option_get_bool(Option,"UCI")){
31         xboard2uci_init();  // the default
32     }
33 }
34
35 // mainloop_engine_step()
36
37 static void mainloop_engine_step(char * string){
38     if(option_get_bool(Option,"UCI")){
39         uci2uci_engine_step(string); 
40     }else{
41         xboard2uci_engine_step(string);
42     }
43 }
44
45 // mainloop_gui_step()
46
47 static void mainloop_gui_step(char * string){
48     if(option_get_bool(Option,"UCI")){
49         uci2uci_gui_step(string); 
50     }else if(my_string_equal(string,"uci")){ // mode auto detection
51         my_log("POLYGLOT *** Switching to UCI mode ***\n");
52         option_set(Option,"UCI","true");
53         uci2uci_gui_step(string);
54     }else{
55         xboard2uci_gui_step(string);
56     }
57 }
58
59 // mainloop()
60
61 void mainloop() {
62     char string[StringSize];
63     my_log("POLYGLOT *** Mainloop started ***\n");
64     mainloop_init();
65     while (!engine_eof(Engine)) {
66             // process buffered lines
67         while(TRUE){
68             if(gui_get_non_blocking(GUI,string)){
69                 mainloop_gui_step(string);
70             }else if(!engine_eof(Engine) &&
71                      engine_get_non_blocking(Engine,string) ){
72                 mainloop_engine_step(string);
73             }else{
74                 break;
75             }
76         }
77         mainloop_wait_for_event();
78     }
79     my_log("POLYGLOT *** Mainloop has ended ***\n");
80     // This should be handled better.
81     engine_close(Engine);
82     my_log("POLYGLOT Calling exit\n");
83     exit(EXIT_SUCCESS);
84
85 }
86
87
88
89
90 // mainloop_wait_for_event()
91
92 static void mainloop_wait_for_event(){
93     pipex_t *pipex[3];
94     pipex[0]=GUI->pipex;
95     pipex[1]=Engine->pipex;
96     pipex[2]=NULL;
97     pipex_wait_event(pipex);
98 }
99
100
101