]> git.jsancho.org Git - lugaru.git/blob - Source/wincompat.h
c2713f2b78bc1f430eb6dd0ab0b83fdeb52429c1
[lugaru.git] / Source / wincompat.h
1 #if !defined(WINCOMPAT_INCLUDED) && !defined(PLATFORM_WINDOWS) && !defined(WIN32) && !defined(WINDOWS) && !defined(__WIN32__)
2 #define WINCOMPAT_INCLUDED
3
4 /**
5  * 
6  * Author: Magnus Naeslund (mag@fbab.net, mag@bahnhof.se)
7  * (c) 2000 Magnus Naeslund, all rights reserved
8  * 
9  */
10
11 #include <sys/time.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <termios.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #ifndef TRUE
19   #define TRUE 1
20 #endif
21 #ifndef FALSE
22   #define FALSE 0
23 #endif
24
25 #define _kbhit kbhit
26 #define stricmp strcasecmp
27 #define strnicmp strncasecmp
28
29 #define Sleep(x) usleep((x)*1000)
30
31 static int            inited=0;
32 static struct termios ori;
33
34 static void tcatexit(){
35    tcsetattr(0,0,&ori);
36 }
37
38 static void init_terminal(){
39    struct termios t;
40    tcgetattr(0,&t);
41    tcgetattr(0,&ori);
42    t.c_lflag &= ~(ICANON);
43    tcsetattr(0,0,&t);
44    atexit(tcatexit);
45 }
46
47 static inline int kbhit(){
48   fd_set rfds;
49   struct timeval tv;
50
51    if (!inited){
52           inited=1;
53           init_terminal();
54    }
55    
56    FD_ZERO(&rfds);
57    FD_SET(0, &rfds);
58    tv.tv_sec = 0;
59    tv.tv_usec = 10*1000;
60    return select(1, &rfds, NULL, NULL, &tv)>0;
61 }
62
63 static inline int getch(){
64    fd_set rfds;
65    
66    if (!inited){
67           inited=1;
68           init_terminal();
69    }
70    
71    FD_ZERO(&rfds);
72    FD_SET(0, &rfds);
73    if (select(1, &rfds, NULL, NULL, NULL)>0)
74          return getchar();
75    else{
76           printf("wincompat.h: select() on fd 0 failed\n");
77           return 0xDeadBeef;
78    }     
79 }
80
81 #endif
82