* apps/games/brickmatch/bm_input_console.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <termios.h>
#include "bm_inputs.h"
* Preprocessor Definitions
****************************************************************************/
static struct termios g_old;
static struct termios g_new;
void init_termios(int echo)
{
tcgetattr(0, &g_old);
g_new = g_old;
g_new.c_lflag &= ~ICANON;
g_new.c_lflag &= ~ECHO;
g_new.c_lflag |= echo ? ECHO : 0;
tcsetattr(0, TCSANOW, &g_new);
}
void reset_termios(void)
{
tcsetattr(0, TCSANOW, &g_old);
}
char getch_(int echo)
{
char ch;
init_termios(echo);
ch = getchar();
reset_termios();
return ch;
}
char getch(void)
{
return getch_(0);
}
* dev_input_init
****************************************************************************/
int dev_input_init(FAR struct input_state_s *dev)
{
init_termios(0);
return OK;
}
* dev_read_input
****************************************************************************/
int dev_read_input(FAR struct input_state_s *dev)
{
char ch;
if ((ch = getch()) == 27)
{
if ((ch = getch()) == 91)
{
ch = getch();
if (ch == 65)
{
dev->dir = DIR_UP;
}
else if (ch == 66)
{
dev->dir = DIR_DOWN;
}
else if (ch == 67)
{
dev->dir = DIR_RIGHT;
}
else if (ch == 68)
{
dev->dir = DIR_LEFT;
}
}
else
{
dev->dir = DIR_NONE;
}
}
else
{
dev->dir = DIR_NONE;
}
return OK;
}