|
| 1 | +/**************************************************************************** |
| 2 | + * apps/games/match4/match4_input_console.h |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <nuttx/config.h> |
| 28 | +#include <nuttx/ascii.h> |
| 29 | +#include <termios.h> |
| 30 | + |
| 31 | +#include "match4_inputs.h" |
| 32 | + |
| 33 | +/**************************************************************************** |
| 34 | + * Preprocessor Definitions |
| 35 | + ****************************************************************************/ |
| 36 | + |
| 37 | +/* Termios functions to have getch on Linux/NuttX */ |
| 38 | + |
| 39 | +static struct termios g_old; |
| 40 | +static struct termios g_new; |
| 41 | + |
| 42 | +/**************************************************************************** |
| 43 | + * Name: init_termios |
| 44 | + * |
| 45 | + * Description: |
| 46 | + * Initialize g_new terminal I/O settings. |
| 47 | + * |
| 48 | + * Parameters: |
| 49 | + * echo - Enable echo flag |
| 50 | + * |
| 51 | + * Returned Value: |
| 52 | + * None. |
| 53 | + * |
| 54 | + ****************************************************************************/ |
| 55 | + |
| 56 | +void init_termios(int echo) |
| 57 | +{ |
| 58 | + tcgetattr(0, &g_old); /* grab old terminal i/o settings */ |
| 59 | + g_new = g_old; /* use old settings as starting */ |
| 60 | + g_new.c_lflag &= ~ICANON; /* disable buffered I/O */ |
| 61 | + g_new.c_lflag &= ~ECHO; /* disable ECHO bit */ |
| 62 | + g_new.c_lflag |= echo ? ECHO : 0; /* set echo mode if requested */ |
| 63 | + tcsetattr(0, TCSANOW, &g_new); /* apply terminal I/O settings */ |
| 64 | +} |
| 65 | + |
| 66 | +/**************************************************************************** |
| 67 | + * Name: deinit_termios |
| 68 | + * |
| 69 | + * Description: |
| 70 | + * Restore back g_old terminal I/O settings. |
| 71 | + * |
| 72 | + * Parameters: |
| 73 | + * None |
| 74 | + * |
| 75 | + * Returned Value: |
| 76 | + * None. |
| 77 | + * |
| 78 | + ****************************************************************************/ |
| 79 | + |
| 80 | +void deinit_termios(void) |
| 81 | +{ |
| 82 | + tcsetattr(0, TCSANOW, &g_old); /* restore old terminal i/o settings */ |
| 83 | +} |
| 84 | + |
| 85 | +/**************************************************************************** |
| 86 | + * Name: reset_termios |
| 87 | + * |
| 88 | + * Description: |
| 89 | + * Restore g_old terminal i/o settings. |
| 90 | + * |
| 91 | + * Parameters: |
| 92 | + * None |
| 93 | + * |
| 94 | + * Returned Value: |
| 95 | + * None. |
| 96 | + * |
| 97 | + ****************************************************************************/ |
| 98 | + |
| 99 | +void reset_termios(void) |
| 100 | +{ |
| 101 | + tcsetattr(0, TCSANOW, &g_old); |
| 102 | +} |
| 103 | + |
| 104 | +/**************************************************************************** |
| 105 | + * Name: getch_ |
| 106 | + * |
| 107 | + * Description: |
| 108 | + * Read 1 character. |
| 109 | + * |
| 110 | + * Parameters: |
| 111 | + * echo - Enable echo mode flag |
| 112 | + * |
| 113 | + * Returned Value: |
| 114 | + * Read character. |
| 115 | + * |
| 116 | + ****************************************************************************/ |
| 117 | + |
| 118 | +char getch_(int echo) |
| 119 | +{ |
| 120 | + char ch; |
| 121 | + |
| 122 | + init_termios(echo); |
| 123 | + ch = getchar(); |
| 124 | + reset_termios(); |
| 125 | + |
| 126 | + return ch; |
| 127 | +} |
| 128 | + |
| 129 | +/**************************************************************************** |
| 130 | + * Name: getch |
| 131 | + * |
| 132 | + * Description: |
| 133 | + * Read 1 character without echo. |
| 134 | + * |
| 135 | + * Parameters: |
| 136 | + * None |
| 137 | + * |
| 138 | + * Returned Value: |
| 139 | + * Read character. |
| 140 | + * |
| 141 | + ****************************************************************************/ |
| 142 | + |
| 143 | +char getch(void) |
| 144 | +{ |
| 145 | + return getch_(0); |
| 146 | +} |
| 147 | + |
| 148 | +/**************************************************************************** |
| 149 | + * Name: dev_input_init |
| 150 | + * |
| 151 | + * Description: |
| 152 | + * Initialize input method. |
| 153 | + * |
| 154 | + * Parameters: |
| 155 | + * dev - Input state data |
| 156 | + * |
| 157 | + * Returned Value: |
| 158 | + * Zero (OK) |
| 159 | + * |
| 160 | + ****************************************************************************/ |
| 161 | + |
| 162 | +int dev_input_init(FAR struct input_state_s *dev) |
| 163 | +{ |
| 164 | + init_termios(0); |
| 165 | + |
| 166 | + return OK; |
| 167 | +} |
| 168 | + |
| 169 | +/**************************************************************************** |
| 170 | + * Name: dev_input_deinit |
| 171 | + * |
| 172 | + * Description: |
| 173 | + * Deinitialize input method. |
| 174 | + * |
| 175 | + * Parameters: |
| 176 | + * None |
| 177 | + * |
| 178 | + * Returned Value: |
| 179 | + * Zero (OK) |
| 180 | + * |
| 181 | + ****************************************************************************/ |
| 182 | + |
| 183 | +int dev_input_deinit(void) |
| 184 | +{ |
| 185 | + deinit_termios(); |
| 186 | + |
| 187 | + return OK; |
| 188 | +} |
| 189 | + |
| 190 | +/**************************************************************************** |
| 191 | + * Name: dev_read_input |
| 192 | + * |
| 193 | + * Description: |
| 194 | + * Read inputs and returns result in input state data. |
| 195 | + * |
| 196 | + * Parameters: |
| 197 | + * dev - Input state data |
| 198 | + * |
| 199 | + * Returned Value: |
| 200 | + * Zero (OK) |
| 201 | + * |
| 202 | + ****************************************************************************/ |
| 203 | + |
| 204 | +int dev_read_input(FAR struct input_state_s *dev) |
| 205 | +{ |
| 206 | + char ch; |
| 207 | + |
| 208 | + /* Arrows keys return three bytes: 27 91 [65-68] */ |
| 209 | + |
| 210 | + if ((ch = getch()) == ASCII_ESC) |
| 211 | + { |
| 212 | + if ((ch = getch()) == ASCII_LBRACKET) |
| 213 | + { |
| 214 | + ch = getch(); |
| 215 | + if (ch == ASCII_B) |
| 216 | + { |
| 217 | + dev->dir = DIR_DOWN; |
| 218 | + } |
| 219 | + else if (ch == ASCII_C) |
| 220 | + { |
| 221 | + dev->dir = DIR_RIGHT; |
| 222 | + } |
| 223 | + else if (ch == ASCII_D) |
| 224 | + { |
| 225 | + dev->dir = DIR_LEFT; |
| 226 | + } |
| 227 | + } |
| 228 | + else |
| 229 | + { |
| 230 | + dev->dir = DIR_NONE; |
| 231 | + } |
| 232 | + } |
| 233 | + else |
| 234 | + { |
| 235 | + dev->dir = DIR_NONE; |
| 236 | + } |
| 237 | + |
| 238 | + return OK; |
| 239 | +} |
0 commit comments