Library of Assembled Shared Sources
keyboard.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "keyboard.h"
45
46#if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
47# define LASS_IO_KEYBOARD_HAVE_WIN32
48# include <conio.h>
49#elif LASS_HAVE_TERMIOS_H && LASS_HAVE_SYS_IOCTL_H
50# define LASS_IO_KEYBOARD_HAVE_TERMIOS_H_AND_IOCTL_H
51# include <termios.h>
52# include <sys/ioctl.h>
53# if LASS_HAVE_UNISTD_H
54# include <unistd.h>
55# endif
56# if LASS_HAVE_SYS_SOCKET_H
57# include <sys/socket.h>
58# endif
59# if LASS_HAVE_SYS_FILIO_H
60# include <sys/filio.h>
61# endif
63#else
64# error lass/io/keyboard.cpp not supported
65#endif
66
67#
68namespace lass
69{
70namespace io
71{
72
73#if defined(LASS_IO_KEYBOARD_HAVE_TERMIOS_H_AND_IOCTL_H)
74namespace impl
75{
76 class TerminalAttributeSaver
77 {
78 public:
79 TerminalAttributeSaver()
80 {
81 LASS_ENFORCE_CLIB(tcgetattr(STDIN_FILENO, &savedAttributes_));
82 }
83 ~TerminalAttributeSaver()
84 {
85 const int rc = tcsetattr(STDIN_FILENO, TCSANOW, &savedAttributes_);
86 LASS_ASSERT(rc == 0);
87 if (rc == -1)
88 {
89 const int errnum = util::impl::lass_errno();
90 std::cerr << "[LASS RUN MSG] UNDEFINED BEHAVIOUR WARNING: "
91 << "failed to restore terminal attributes: ("
92 << errnum << ") " << util::impl::lass_strerror(errnum)
93 << std::endl;
94 }
95 }
96 const termios& savedAttributes() const
97 {
98 return savedAttributes_;
99 }
100 private:
101 termios savedAttributes_;
102 };
103}
104#endif
105
106/** return true if a key is pressed and which is waiting in the buffer ...
107 * @ingroup Keyboard
108 */
110{
111#if defined(LASS_IO_KEYBOARD_HAVE_WIN32)
112 return _kbhit() != 0;
113#elif defined(LASS_IO_KEYBOARD_HAVE_TERMIOS_H_AND_IOCTL_H)
114 impl::TerminalAttributeSaver attributeSaver;
115 termios rawMode = attributeSaver.savedAttributes();
116 rawMode.c_lflag &= ~static_cast<tcflag_t>(ICANON);
117 LASS_ENFORCE_CLIB(tcsetattr(STDIN_FILENO, TCSANOW, &rawMode));
118
119 int count = 0;
120 LASS_ENFORCE_CLIB(ioctl(STDIN_FILENO, FIONREAD, &count));
121 return count > 0;
122#else
123# error keyboardIsHit not supported
124#endif
125}
126
127/** get a character from the console without echo
128 * @ingroup Keyboard
129 */
131{
132#if defined(LASS_IO_KEYBOARD_HAVE_WIN32)
133 return _getch();
134#elif defined(LASS_IO_KEYBOARD_HAVE_TERMIOS_H_AND_IOCTL_H)
135 impl::TerminalAttributeSaver attributeSaver;
136 termios rawMode = attributeSaver.savedAttributes();
137 rawMode.c_lflag &= ~static_cast<tcflag_t>(ICANON);
138 rawMode.c_lflag &= ~static_cast<tcflag_t>(ECHO);
139 rawMode.c_cc[VMIN] = 1; // block for input
140 rawMode.c_cc[VTIME] = 0; // timer is ignored
141 LASS_ENFORCE_CLIB(tcsetattr(STDIN_FILENO, TCSANOW, &rawMode));
142
143 unsigned char character = 0;
144 const ssize_t charactersRead = LASS_ENFORCE_CLIB(read(STDIN_FILENO, &character, 1));
145 LASS_ENFORCE(charactersRead == 1);
146 return static_cast<int>(character);
147#else
148# error keyboardIsHit not supported
149#endif
150}
151
152}
153}
154
155// EOF
bool keyboardIsHit()
return true if a key is pressed and which is waiting in the buffer ...
Definition keyboard.cpp:109
int keyboardGetKey()
get a character from the console without echo
Definition keyboard.cpp:130
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53