My Project
ui.c
Go to the documentation of this file.
1
11#include "options.h"
12#include "ui.h"
13#include "util.h"
14#include <assert.h>
15
16void gtp_preprocess(char *line);
17
25bool ui_switch(UI *ui, const char *ui_type)
26{
27 if (strcmp(ui_type, "edax") == 0) {
28 ui->type = UI_EDAX;
29 ui->init = ui_init_edax;
30 ui->free = ui_free_edax;
31 ui->loop = ui_loop_edax;
32 return true;
33 } else if (strcmp(ui_type, "gtp") == 0) {
34 ui->type = UI_GTP;
35 ui->init = ui_init_gtp;
36 ui->free = ui_free_gtp;
37 ui->loop = ui_loop_gtp;
38 return true;
39 } else if (strcmp(ui_type, "nboard") == 0) {
40 ui->type = UI_NBOARD;
41 ui->init = ui_init_nboard;
42 ui->free = ui_free_nboard;
43 ui->loop = ui_loop_nboard;
44 return true;
45 } else if (strcmp(ui_type, "xboard") == 0) {
46 ui->type = UI_XBOARD;
47 ui->init = ui_init_xboard;
48 ui->free = ui_free_xboard;
49 ui->loop = ui_loop_xboard;
50 return true;
51 } else if (strcmp(ui_type, "ggs") == 0) {
52 ui->type = UI_GGS;
53 ui->init = ui_init_ggs;
54 ui->free = ui_free_ggs;
55 ui->loop = ui_loop_ggs;
56 return true;
57 } else if (strcmp(ui_type, "cassio") == 0) {
58 ui->type = UI_CASSIO;
59 return true;
60 }
61
62 return false;
63}
64
65
73static void ui_read_input(UI *ui)
74{
75 char *buffer;
76 char cmd[8];
77 Event *event = ui->event;
78 Play *play = ui->play;
79
80 buffer = string_read_line(stdin);
81
82 if (buffer == NULL) {
83 if (ferror(stdin)) {
84 if (errno == EINTR) clearerr(stdin);
85 else fatal_error("stdin is broken\n");
86 } else if (feof(stdin)) {
87 buffer = string_duplicate("eof");
88 }
89 event->loop = false;
90 } else {
91 if (ui->type == UI_GTP) gtp_preprocess(buffer);
92 parse_word(buffer, cmd, 5);
94 if (strcmp(cmd, "stop") == 0) {
96 info("<stop>\n");
97 play_stop(play);
98 if (ui->type == UI_GGS) play_stop(play + 1);
99 } else if (ui->type == UI_NBOARD && strcmp(cmd, "ping") == 0) {
100 play_stop(play);
101 } else if (ui->type == UI_XBOARD && strcmp(cmd, "?") == 0) {
102 play_stop(play);
103 } else {
104 if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0) {
106 play_stop(play);
107 if (ui->type == UI_GGS) play_stop(play + 1);
108 event->loop = false;
109 }
110 }
111 }
112 if (buffer) {
113 lock(event);
114 event_add_message(event, buffer);
115 condition_signal(event);
116 unlock(event);
117 }
118}
119
120
126static void* ui_read_input_loop(void *v)
127{
128 UI *ui = (UI*) v;
129
130 while (ui->event->loop && !feof(stdin) && !ferror(stdin)) {
131 ui_read_input(ui);
132 }
133
134 info("<exit ui_read_input>\n");
135
136 return NULL;
137}
138
139
147void ui_event_wait(UI *ui, char **cmd, char **param)
148{
149 event_wait(ui->event, cmd, param);
150 if (options.echo && *cmd) printf(" %s %s\n", *cmd, *param ? *param : "");
151}
152
160bool ui_event_peek(UI *ui, char **cmd, char **param)
161{
162 int n;
163 char *message;
164
165 if ((message = event_peek_message(ui->event)) != NULL) {
166
167 free(*cmd); *cmd = NULL;
168 free(*param); *param = NULL;
169
170 n = strlen(message);
171 *cmd = (char*) malloc(n + 1);
172 *param = (char*) malloc(n + 1);
173 parse_command(message, *cmd, *param, n);
174 free(message);
175 return true;
176 } else {
177 return false;
178 }
179}
180
190{
191 bool ok;
192
193 spin_lock(ui->event);
194 ok = event_exist(ui->event);
195 spin_unlock(ui->event);
196
197 return ok;
198}
199
207{
208 event_init(ui->event);
209
210 thread_create2(&ui->event->thread, ui_read_input_loop, ui); // modified for iOS by lavox. 2018/1/16
211 thread_detach(ui->event->thread);
212}
213
221{
222 event_free(ui->event);
223}
@ UI_EDAX
Definition const.h:110
@ UI_GGS
Definition const.h:111
@ UI_NBOARD
Definition const.h:113
@ UI_CASSIO
Definition const.h:109
@ UI_GTP
Definition const.h:112
@ UI_XBOARD
Definition const.h:114
void ui_loop_edax(UI *ui)
Loop event.
Definition edax.c:284
void ui_init_edax(UI *ui)
initialize edax protocol.
Definition edax.c:137
void ui_free_edax(UI *ui)
free resources used by edax protocol.
Definition edax.c:157
void event_clear_messages(Event *event)
Remove all unprocessed messages.
Definition event.c:45
bool event_exist(Event *event)
Check if there is a message.
Definition event.c:176
void event_init(Event *event)
Initialize a message event.
Definition event.c:23
void event_wait(Event *event, char **cmd, char **param)
Wait input.
Definition event.c:133
void event_free(Event *event)
Free a message event.
Definition event.c:67
void event_add_message(Event *event, char *message)
Add a new message at the bottom of the list.
Definition event.c:93
char * event_peek_message(Event *event)
Peek the first message from the list.
Definition event.c:187
void ui_free_ggs(UI *ui)
ui_free_ggs
Definition ggs.c:1599
void ui_loop_ggs(UI *ui)
ui_loop_ggs
Definition ggs.c:1418
void ui_init_ggs(UI *ui)
ui_init_ggs
Definition ggs.c:1390
void ui_init_gtp(UI *ui)
initialize edax protocol
Definition gtp.c:93
void ui_loop_gtp(UI *ui)
Loop event.
Definition gtp.c:127
void ui_free_gtp(UI *ui)
free resources used by edax protocol
Definition gtp.c:115
void ui_loop_nboard(UI *ui)
Loop event.
Definition nboard.c:113
void ui_init_nboard(UI *ui)
initialize edax protocol
Definition nboard.c:80
void ui_free_nboard(UI *ui)
free resources used by edax protocol
Definition nboard.c:101
Options options
Definition options.c:22
void play_stop(Play *play)
Stop thinking.
Definition play.c:769
Definition event.h:18
Thread thread
Definition event.h:24
volatile bool loop
Definition event.h:19
bool echo
Definition options.h:35
Definition play.h:25
Definition ui.h:31
Event event[1]
Definition ui.h:38
int type
Definition ui.h:36
Play play[2]
Definition ui.h:32
void(* init)(struct UI *)
Definition ui.h:40
void(* loop)(struct UI *)
Definition ui.h:41
void(* free)(struct UI *)
Definition ui.h:42
static void ui_read_input(UI *ui)
Get an event.
Definition ui.c:73
void ui_event_init(UI *ui)
Create a new Othello User Interface.
Definition ui.c:206
bool ui_switch(UI *ui, const char *ui_type)
Switch between different User Interface.
Definition ui.c:25
void gtp_preprocess(char *line)
Definition gtp.c:34
bool ui_event_exist(UI *ui)
ui_event_exist
Definition ui.c:189
static void * ui_read_input_loop(void *v)
Read event loop.
Definition ui.c:126
void ui_event_wait(UI *ui, char **cmd, char **param)
Wait input.
Definition ui.c:147
bool ui_event_peek(UI *ui, char **cmd, char **param)
Wait input.
Definition ui.c:160
void ui_event_free(UI *ui)
Free events.
Definition ui.c:220
User interface header.
void ui_free_xboard(UI *)
free resources used by xboard protocol.
Definition xboard.c:97
void ui_init_xboard(UI *)
initialize xboard protocol.
Definition xboard.c:72
void ui_loop_xboard(UI *)
Loop event.
Definition xboard.c:440
char * string_duplicate(const char *s)
Duplicate a string.
Definition util.c:299
void thread_create2(Thread *thread, void *(*function)(void *), void *data)
Create a thread.
Definition util.c:922
void string_to_lowercase(char *s)
Change all char of a string to lowercase.
Definition util.c:355
char * string_read_line(FILE *f)
Read a line.
Definition util.c:265
char * parse_command(const char *string, char *cmd, char *param, const unsigned int size)
Parse a command.
Definition util.c:867
char * parse_word(const char *string, char *word, unsigned int n)
Parse a word.
Definition util.c:562
Miscellaneous utilities header.
#define fatal_error(...)
Display an error message as "FATAL_ERROR : file name : function name : line number : ....
Definition util.h:349
#define info(...)
Display a message.
Definition util.h:382