]> git.jsancho.org Git - lugaru.git/blob - Source/wincompat.h
Added GPL license and headers.
[lugaru.git] / Source / wincompat.h
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3
4 This file is part of Lugaru.
5
6 Lugaru is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
14
15 See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21
22 #if !defined(WINCOMPAT_INCLUDED) && !defined(PLATFORM_WINDOWS) && !defined(WIN32) && !defined(WINDOWS) && !defined(__WIN32__)
23 #define WINCOMPAT_INCLUDED
24
25 /**
26  * 
27  * Author: Magnus Naeslund (mag@fbab.net, mag@bahnhof.se)
28  * (c) 2000 Magnus Naeslund, all rights reserved
29  * 
30  */
31
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <termios.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38
39 #ifndef TRUE
40   #define TRUE 1
41 #endif
42 #ifndef FALSE
43   #define FALSE 0
44 #endif
45
46 #define _kbhit kbhit
47 #define stricmp strcasecmp
48 #define strnicmp strncasecmp
49
50 #define Sleep(x) usleep((x)*1000)
51
52 static int            inited=0;
53 static struct termios ori;
54
55 static void tcatexit(){
56    tcsetattr(0,0,&ori);
57 }
58
59 static void init_terminal(){
60    struct termios t;
61    tcgetattr(0,&t);
62    tcgetattr(0,&ori);
63    t.c_lflag &= ~(ICANON);
64    tcsetattr(0,0,&t);
65    atexit(tcatexit);
66 }
67
68 static inline int kbhit(){
69   fd_set rfds;
70   struct timeval tv;
71
72    if (!inited){
73           inited=1;
74           init_terminal();
75    }
76    
77    FD_ZERO(&rfds);
78    FD_SET(0, &rfds);
79    tv.tv_sec = 0;
80    tv.tv_usec = 10*1000;
81    return select(1, &rfds, NULL, NULL, &tv)>0;
82 }
83
84 static inline int getch(){
85    fd_set rfds;
86    
87    if (!inited){
88           inited=1;
89           init_terminal();
90    }
91    
92    FD_ZERO(&rfds);
93    FD_SET(0, &rfds);
94    if (select(1, &rfds, NULL, NULL, NULL)>0)
95          return getchar();
96    else{
97           printf("wincompat.h: select() on fd 0 failed\n");
98           return 0xDeadBeef;
99    }     
100 }
101
102 #endif
103