File: mainloop.c

package info (click to toggle)
polyglot 2.0.1%2Bgit20140926-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,228 kB
  • ctags: 1,163
  • sloc: ansic: 10,416; sh: 994; makefile: 18
file content (101 lines) | stat: -rw-r--r-- 2,181 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// mainloop.c

// includes

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "main.h"
#include "engine.h"
#include "gui.h"
#include "option.h"
//#include "ini.h"
#include "xboard2uci.h"
#include "uci2uci.h"

// prototypes

static void mainloop_init            ();
static void mainloop_wait_for_event  ();
static void mainloop_engine_step(char * string);
static void mainloop_gui_step(char * string);

// functions

// mainloop_init()
    
static void mainloop_init(){
    if(!option_get_bool(Option,"UCI")){
        xboard2uci_init();  // the default
    }
}

// mainloop_engine_step()

static void mainloop_engine_step(char * string){
    if(option_get_bool(Option,"UCI")){
        uci2uci_engine_step(string); 
    }else{
        xboard2uci_engine_step(string);
    }
}

// mainloop_gui_step()

static void mainloop_gui_step(char * string){
    if(option_get_bool(Option,"UCI")){
        uci2uci_gui_step(string); 
    }else if(my_string_equal(string,"uci")){ // mode auto detection
        my_log("POLYGLOT *** Switching to UCI mode ***\n");
        option_set(Option,"UCI","true");
        uci2uci_gui_step(string);
    }else{
        xboard2uci_gui_step(string);
    }
}

// mainloop()

void mainloop() {
    char string[StringSize];
    my_log("POLYGLOT *** Mainloop started ***\n");
    mainloop_init();
    while (!engine_eof(Engine)) {
            // process buffered lines
        while(TRUE){
            if(gui_get_non_blocking(GUI,string)){
                mainloop_gui_step(string);
            }else if(!engine_eof(Engine) &&
                     engine_get_non_blocking(Engine,string) ){
                mainloop_engine_step(string);
            }else{
                break;
            }
        }
        mainloop_wait_for_event();
    }
    my_log("POLYGLOT *** Mainloop has ended ***\n");
    // This should be handled better.
    engine_close(Engine);
    my_log("POLYGLOT Calling exit\n");
    exit(EXIT_SUCCESS);

}




// mainloop_wait_for_event()

static void mainloop_wait_for_event(){
    pipex_t *pipex[3];
    pipex[0]=GUI->pipex;
    pipex[1]=Engine->pipex;
    pipex[2]=NULL;
    pipex_wait_event(pipex);
}