source: multiio/pcmmio/original/kbhit.c @ 2bae2aa

basewinsystems
Last change on this file since 2bae2aa was 2bae2aa, checked in by Joel Sherrill <joel.sherrill@…>, on 06/08/09 at 14:52:43

Initial import.

  • Property mode set to 100644
File size: 1.5 KB
Line 
1/* $Header$
2*
3*
4*   Filename : $RCSfile$
5*
6*   $Id$
7*
8*   $Log$
9*   Revision 1.1  2005/04/19 20:39:11  steve
10*   Initial revision
11*
12*
13*
14*/
15
16
17/* These functions are from the Wrox Press Book "Beginning Linux
18   Programming - Second Edition" by Richard Stones and Neil Matthew.
19   (C) 1996 & 1999 and are included here in compliance with the GNU
20   Public license.
21
22*/
23
24#include <stdio.h>
25#include <termios.h>
26#include <term.h>
27#include <curses.h>
28#include <unistd.h>
29
30static struct termios initial_settings, new_settings;
31static int peek_character = -1;
32
33void init_keyboard(void);
34void close_keyboard(void);
35int kbhit(void);
36int readch(void);
37
38void init_keyboard(void)
39{
40    tcgetattr(0,&initial_settings);
41    new_settings = initial_settings;
42    new_settings.c_lflag &= ~ICANON;
43    new_settings.c_lflag &= ~ECHO;
44    new_settings.c_lflag &= ~ISIG;
45    new_settings.c_cc[VMIN] = 1;
46    new_settings.c_cc[VTIME] = 0;
47    tcsetattr(0, TCSANOW, &new_settings);
48}
49
50void close_keyboard(void)
51{
52    tcsetattr(0, TCSANOW, &initial_settings);
53}
54
55
56int kbhit(void)
57{
58char ch;
59int nread;
60
61    if(peek_character != -1)
62        return 1;
63
64    new_settings.c_cc[VMIN] = 0;
65    tcsetattr(0, TCSANOW, &new_settings);
66    nread = read(0, &ch, 1);
67    new_settings.c_cc[VMIN] = 1;
68    tcsetattr(0, TCSANOW, &new_settings);
69
70    if(nread == 1)
71    {
72        peek_character = ch;
73        return 1;
74    }
75
76    return 0;
77}
78
79int readch(void)
80{
81char ch;
82
83    if(peek_character != -1)
84    {
85        ch = peek_character;
86        peek_character = -1;
87        return ch;
88    }
89    read(0,&ch,1);
90    return ch;
91}
92
Note: See TracBrowser for help on using the repository browser.