Debian Codegraph @

src/cli/cli.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "cli.h"
21 #include "../ta-log.h"
22
23 bool cliQuit=false;
24
25 #ifndef _WIN32
26 static void handleTerm(int) {
27 cliQuit=true;
28 }
29 #endif
30
31 void FurnaceCLI::noStatus() {
32 disableStatus=true;
33 }
34
35 void FurnaceCLI::noControls() {
36 disableControls=true;
37 }
38
39 void FurnaceCLI::bindEngine(DivEngine* eng) {
40 e=eng;
41 }
42
43 bool FurnaceCLI::loop() {
44 if (disableControls) {
45 while (!cliQuit) {
46 #ifdef _WIN32
47 Sleep(1000);
48 #else
49 pause();
50 #endif
51 }
52 return true;
53 }
54
55 bool escape=false;
56 bool escapeSecondStage=false;
57 while (!cliQuit) {
58 #ifdef _WIN32
59 int c;
60 c=fgetc(stdin);
61 if (c==EOF) break;
62 #else
63 unsigned char c;
64 if (read(STDIN_FILENO,&c,1)<=0) continue;
65 #endif
66 if (escape) {
67 if (escapeSecondStage) {
68 switch (c) {
69 case 'C': // right
70 e->setOrder(e->getOrder()+1);
71 escape=false;
72 escapeSecondStage=false;
73 break;
74 case 'D': // left
75 e->setOrder(e->getOrder()-1);
76 escape=false;
77 escapeSecondStage=false;
78 break;
79 default:
80 escape=false;
81 escapeSecondStage=false;
82 break;
83 }
84 } else {
85 switch (c) {
86 case '[': case 'O':
87 escapeSecondStage=true;
88 break;
89 default:
90 escape=false;
91 break;
92 }
93 }
94 } else {
95 switch (c) {
96 case 0x1b: // <ESC>
97 escape=true;
98 break;
99 case 'h': // left
100 e->setOrder(e->getOrder()-1);
101 break;
102 case 'l': // right
103 e->setOrder(e->getOrder()+1);
104 break;
105 case ' ':
106 if (e->isHalted()) {
107 e->resume();
108 } else {
109 e->halt();
110 }
111 break;
112 }
113 }
114 }
115 printf("\n");
116 return true;
117 }
118
119 bool FurnaceCLI::finish() {
120 if (disableControls) return true;
121 #ifdef _WIN32
122 #else
123 if (tcsetattr(0,TCSAFLUSH,&termpropold)!=0) {
124 logE("could not set console attributes!");
125 logE("you may have to run `reset` on your terminal.");
126 return false;
127 }
128 #endif
129 return true;
130 }
131
132 // blatantly copied from tildearrow/tfmxplay
133 bool FurnaceCLI::init() {
134 #ifdef _WIN32
135 if (disableControls) return true;
136
137 winin=GetStdHandle(STD_INPUT_HANDLE);
138 winout=GetStdHandle(STD_OUTPUT_HANDLE);
139 int termprop=0;
140 int termpropi=0;
141 GetConsoleMode(winout,(LPDWORD)&termprop);
142 GetConsoleMode(winin,(LPDWORD)&termpropi);
143 termprop|=ENABLE_VIRTUAL_TERMINAL_PROCESSING;
144 termpropi&=~ENABLE_LINE_INPUT;
145 SetConsoleMode(winout,termprop);
146 SetConsoleMode(winin,termpropi);
147 #else
148 sigemptyset(&intsa.sa_mask);
149 intsa.sa_flags=0;
150 intsa.sa_handler=handleTerm;
151 sigaction(SIGINT,&intsa,NULL);
152
153 if (disableControls) return true;
154
155 if (tcgetattr(0,&termprop)!=0) {
156 logE("could not get console attributes!");
157 return false;
158 }
159 memcpy(&termpropold,&termprop,sizeof(struct termios));
160 termprop.c_lflag&=~ECHO;
161 termprop.c_lflag&=~ICANON;
162 if (tcsetattr(0,TCSAFLUSH,&termprop)!=0) {
163 logE("could not set console attributes!");
164 return false;
165 }
166 #endif
167 return true;
168 }
169
170 FurnaceCLI::FurnaceCLI():
171 e(NULL) {
172 }