OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <fcntl.h> |
| 6 #include <stdio.h> |
| 7 #include <unistd.h> |
| 8 |
| 9 #include <iostream> |
| 10 |
| 11 #include "examples/media_test/keystroke.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace media { |
| 15 namespace examples { |
| 16 |
| 17 namespace { |
| 18 |
| 19 bool eat_newline = false; |
| 20 bool upped_already = false; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 char Keystroke(void) { |
| 25 const char *kUp = "\033[A"; |
| 26 |
| 27 fcntl(STDIN_FILENO, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK); |
| 28 char buf[1]; |
| 29 if (read(STDIN_FILENO, buf, 1) > 0) { |
| 30 if (!upped_already) { |
| 31 std::cout << kUp << std::flush; |
| 32 upped_already = true; |
| 33 } |
| 34 |
| 35 if (buf[0] == '\n') { |
| 36 upped_already = false; |
| 37 if (eat_newline) { |
| 38 eat_newline = false; |
| 39 } else { |
| 40 return buf[0]; |
| 41 } |
| 42 } else { |
| 43 eat_newline = true; |
| 44 return buf[0]; |
| 45 } |
| 46 } |
| 47 return 0; |
| 48 } |
| 49 |
| 50 } // namespace examples |
| 51 } // namespace media |
| 52 } // namespace mojo |
OLD | NEW |