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