1 /***************************************************************************
2 platform.cpp - Platoform dependent utilities, implementation
4 begin : Sun Sep 28 2007
5 copyright : (C) 2007 by Maurizio Monge
6 email : monge@linuz.sns.it
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 #if defined(_WIN32) || defined(_WIN64)
32 handle
= GetStdHandle(STD_INPUT_HANDLE
);
33 pipe
= !GetConsoleMode(handle
,&val
);
37 SetConsoleMode(handle
, val
& ~(ENABLE_MOUSE_INPUT
|ENABLE_WINDOW_INPUT
));
38 FlushConsoleInputBuffer(handle
);
42 bool input_available() {
50 if(!PeekNamedPipe(handle
, NULL
, 0, NULL
, &val
, NULL
) )
56 GetNumberOfConsoleInputEvents(handle
, &val
);
65 return GetTickCount() / 10;
68 bool start_engine(const char *prog
, FILE **in
, FILE **out
)
70 printf("Error, start_engine unimplemented on windows!\n");
74 void *map_file(const char *file_name
, uint32_t *size
)
81 file
= CreateFile(file_name
, GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
82 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
83 if(file
==INVALID_HANDLE_VALUE
)
86 loword
= GetFileSize(file
, &hiword
);
87 if(loword
== INVALID_FILE_SIZE
)
92 *size
= loword
| ((uint64_t)hiword
<<32);
94 fmap
= CreateFileMapping(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
95 if(fmap
== INVALID_HANDLE_VALUE
)
101 retv
= MapViewOfFile(fmap
, FILE_MAP_READ
, (DWORD
)(offset
>> 32), (DWORD
)(offset
% 0xffffffff), 0);
108 void unmap_file(void *addr
, uint32_t size
)
110 UnmapViewOfFile(addr
);
113 #else //defined(_WIN32) || defined(_WIN64)
117 #include <sys/time.h>
118 #include <sys/types.h>
119 #include <sys/stat.h>
120 #include <sys/mman.h>
125 #include "platform.h"
131 bool input_available()
133 int n
= fileno(stdin
);
136 struct timeval tv
= { 0, 0 };
143 while(select(n
+1, &ifds
, NULL
, &efds
, &tv
)==-1)
145 printf("select: %s\n", strerror(errno
));
147 return FD_ISSET(0, &ifds
) || FD_ISSET(0, &efds
);
153 gettimeofday(&tv
,NULL
);
154 return tv
.tv_sec
*100 + tv
.tv_usec
/10000;
157 void *map_file(const char *file_name
, uint32_t *size
)
163 fd
= open( file_name
, O_RDONLY
);
171 addr
= mmap( NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
172 if(addr
== (void*)-1)
182 void unmap_file(void*addr
, uint32_t size
)
187 bool start_engine(const char *prog
, FILE **in
, FILE **out
)
198 execlp(prog
, prog
, NULL
);
199 fprintf(stderr
, "Error, could not run %s!\n", prog
);
202 *in
= fdopen(pin
[0], "r");
203 *out
= fdopen(pout
[1], "w");
206 fprintf(*out
, "xboard\n");
207 fprintf(*out
, "protover 2\n");
208 //fprintf(*out, "post\n");
209 fprintf(*out
, "easy\n");
210 fprintf(*out
, "new\n");
211 fprintf(*out
, "force\n");
215 #endif //defined(_WIN32) || defined(_WIN64)