Improvements to the eval, try to checkmate sacrifying as much as possible.
[rattatechess.git] / platform.cpp
blob5400af2fa973ac06f189576794c16f54ef17f7b4
1 /***************************************************************************
2 platform.cpp - Platoform dependent utilities, implementation
3 -------------------
4 begin : Sun Nov 28 2007
5 copyright : (C) 2007 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
18 #if defined(_WIN32) || defined(_WIN64)
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <windows.h>
24 static bool pipe;
25 static HANDLE handle;
27 void init_stdin()
29 DWORD val;
31 handle = GetStdHandle(STD_INPUT_HANDLE);
32 pipe = !GetConsoleMode(handle,&val);
34 if (!pipe)
36 SetConsoleMode(handle, val & ~(ENABLE_MOUSE_INPUT|ENABLE_WINDOW_INPUT));
37 FlushConsoleInputBuffer(handle);
41 bool input_available() {
42 DWORD val;
44 if (stdin->_cnt > 0)
45 return true;
47 if(pipe)
49 if(!PeekNamedPipe(handle, NULL, 0, NULL, &val, NULL) )
50 return true;
51 return val > 0;
53 else
55 GetNumberOfConsoleInputEvents(handle, &val);
56 return val > 1;
59 return false;
62 int current_time()
64 return GetTickCount() / 10;
67 bool start_engine(const char *prog, FILE **in, FILE **out)
69 printf("Error, start_engine unimplemented on windows!\n");
70 return false;
73 #else //defined(_WIN32) || defined(_WIN64)
75 #include <unistd.h>
76 #include <stdlib.h>
77 #include <sys/time.h>
78 #include <sys/types.h>
79 #include <sys/stat.h>
80 #include <sys/mman.h>
81 #include <fcntl.h>
82 #include <unistd.h>
83 #include <string.h>
84 #include <errno.h>
85 #include "platform.h"
87 void init_stdin()
91 bool input_available()
93 int n = fileno(stdin);
94 fd_set ifds;
95 fd_set efds;
96 struct timeval tv = { 0, 0 };
98 FD_ZERO(&ifds);
99 FD_SET(n, &ifds);
100 FD_ZERO(&efds);
101 FD_SET(n, &efds);
103 while(select(n+1, &ifds, NULL, &efds, &tv)==-1)
104 if(errno != EINTR)
105 printf("select: %s\n", strerror(errno));
107 return FD_ISSET(0, &ifds) || FD_ISSET(0, &efds);
110 int current_time()
112 timeval tv;
113 gettimeofday(&tv,NULL);
114 return tv.tv_sec*100 + tv.tv_usec/10000;
117 // void *map_file(const char *file_name, uint32_t *size)
118 // {
119 // struct stat st;
120 // void *addr;
121 // int fd;
123 // fd = open( file_name, O_RDONLY);
124 // if(fd == -1)
125 // return NULL;
127 // fstat(fd, &st);
128 // if(size)
129 // *size = st.st_size;
131 // addr = mmap( NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
132 // if(addr == (void*)-1)
133 // {
134 // close(fd);
135 // return NULL;
136 // }
138 // close(fd);
139 // return addr;
141 // }
143 // void unmap_file(void*addr, uint32_t size)
144 // {
145 // munmap(addr, size);
146 // }
148 bool start_engine(const char *prog, FILE **in, FILE **out)
150 int pin[2];
151 int pout[2];
152 pipe(pin);
153 pipe(pout);
154 if(!fork())
156 dup2(pout[0], 0);
157 dup2(pin[1], 1);
158 dup2(pin[1], 2);
159 execlp(prog, prog, NULL);
160 fprintf(stderr, "Error, could not run %s!\n", prog);
161 exit(0);
163 *in = fdopen(pin[0], "r");
164 *out = fdopen(pout[1], "w");
165 setbuf(*in, NULL);
166 setbuf(*out, NULL);
167 fprintf(*out, "xboard\n");
168 fprintf(*out, "protover 2\n");
169 //fprintf(*out, "post\n");
170 fprintf(*out, "easy\n");
171 fprintf(*out, "new\n");
172 fprintf(*out, "force\n");
174 return true;
176 #endif //defined(_WIN32) || defined(_WIN64)