/[projects]/smsdaemon/kbhit.cpp
ViewVC logotype

Annotation of /smsdaemon/kbhit.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 196 - (hide annotations) (download)
Thu Dec 18 06:53:29 2008 UTC (15 years, 5 months ago) by torben
File size: 2525 byte(s)
Make pretty

astyle -t -b -N

1 torben 26 /*****************************************************************************
2     kbhit() and getch() for Linux/UNIX
3     Chris Giese <geezer@execpc.com> http://my.execpc.com/~geezer
4     Release date: ?
5     This code is public domain (no copyright).
6     You can do whatever you want with it.
7     *****************************************************************************/
8     #if !defined(linux)
9     #include <conio.h> /* kbhit(), getch() */
10    
11     #else
12     #include <sys/time.h> /* struct timeval, select() */
13     /* ICANON, ECHO, TCSANOW, struct termios */
14     #include <termios.h> /* tcgetattr(), tcsetattr() */
15     #include <stdlib.h> /* atexit(), exit() */
16     #include <unistd.h> /* read() */
17     #include <stdio.h> /* printf() */
18    
19     #include <string.h>
20    
21     #include "kbhit.h"
22    
23     static struct termios g_old_kbd_mode;
24     /*****************************************************************************
25     *****************************************************************************/
26     void cooked(void)
27     {
28     tcsetattr(0, TCSANOW, &g_old_kbd_mode);
29     }
30     /*****************************************************************************
31     *****************************************************************************/
32     void raw(void)
33     {
34     static char init;
35 torben 196 /**/
36 torben 26 struct termios new_kbd_mode;
37    
38 torben 196 if (init)
39 torben 26 return;
40 torben 196 /* put keyboard (stdin, actually) in raw, unbuffered mode */
41 torben 26 tcgetattr(0, &g_old_kbd_mode);
42     memcpy(&new_kbd_mode, &g_old_kbd_mode, sizeof(struct termios));
43     new_kbd_mode.c_lflag &= ~(ICANON | ECHO);
44     new_kbd_mode.c_cc[VTIME] = 0;
45     new_kbd_mode.c_cc[VMIN] = 1;
46     tcsetattr(0, TCSANOW, &new_kbd_mode);
47 torben 196 /* when we exit, go back to normal, "cooked" mode */
48 torben 26 atexit(cooked);
49    
50     init = 1;
51     }
52     /*****************************************************************************
53     *****************************************************************************/
54     int kbhit(void)
55     {
56     struct timeval timeout;
57     fd_set read_handles;
58     int status;
59    
60     raw();
61 torben 196 /* check stdin (fd 0) for activity */
62 torben 26 FD_ZERO(&read_handles);
63     FD_SET(0, &read_handles);
64     timeout.tv_sec = timeout.tv_usec = 0;
65     status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
66 torben 196 if (status < 0)
67 torben 26 {
68     printf("select() failed in kbhit()\n");
69     exit(1);
70     }
71     return status;
72     }
73     /*****************************************************************************
74     *****************************************************************************/
75     int getch(void)
76     {
77     unsigned char temp;
78    
79     raw();
80 torben 196 /* stdin = fd 0 */
81     if (read(0, &temp, 1) != 1)
82 torben 26 return 0;
83     return temp;
84     }
85     #endif

  ViewVC Help
Powered by ViewVC 1.1.20