ui::input: Remove "gs" from non-globals table.
[Ale.git] / ui / ui_tty.h
blobb38ad251104b01b72763b1e27defcef7c5f916ef
1 // Copyright 2004 David Hilvert <dhilvert@auricle.dyndns.org>,
2 // <dhilvert@ugcs.caltech.edu>
4 /* This file is part of the Anti-Lamenessing Engine.
6 The Anti-Lamenessing Engine is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 The Anti-Lamenessing Engine is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the Anti-Lamenessing Engine; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifndef __ui_tty_h__
22 #define __ui_tty_h__
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
29 #if HAVE_TIME_H
30 #include <time.h>
31 #endif
32 #if HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #endif
36 #include "../d2.h"
37 #include "ui.h"
38 #include "util.h"
41 * TTY user interface
44 class ui_tty : public ui {
45 private:
46 int terminal_width;
47 char *buffer;
48 int buffer_index;
49 int status_index;
50 int dirty;
52 #ifdef USE_PTHREAD
53 pthread_mutex_t lock;
54 pthread_t update_thread;
55 #endif
57 void clear_buffer() {
58 buffer[0] = '\0';
59 buffer_index = 0;
62 void write_buffer() {
63 if (buffer_index < 0)
64 return;
65 fputc('\r', ui_stream);
66 fputs(buffer, ui_stream);
67 status_index = buffer_index;
70 void status_clear() {
71 while (status_index < terminal_width) {
72 status_index++;
73 fprintf(ui_stream, " ");
77 void line_clear() {
78 fputc('\r', ui_stream);
79 for (int i = 0; i < terminal_width; i++) {
80 fprintf(ui_stream, " ");
85 void status_printf(int count, ...) {
86 if (buffer_index < 0)
87 return;
89 int n;
91 char *local_buffer = (char *) calloc(terminal_width - status_index, sizeof(char));
93 assert(local_buffer);
95 if (!local_buffer)
96 return;
98 for (int i = 0; i < count; i++) {
99 char *format = NULL;
100 va_list ap;
102 va_start(ap, count);
104 for (int arg = 0; arg < i + 1; arg++)
105 format = va_arg(ap, char *);
106 for (int arg = i + 1; arg < count; arg++)
107 va_arg(ap, char *);
109 assert (format);
111 n = vsnprintf(local_buffer, terminal_width - status_index, format, ap);
112 va_end(ap);
114 if (n < 0 || n > terminal_width - status_index - 1)
115 continue;
117 fputs(local_buffer, ui_stream);
119 status_index += n;
121 free(local_buffer);
123 return;
128 * If we reach this point, then there was no valid string produced.
131 status_clear();
133 free(local_buffer);
136 void pad_align_status() {
137 for (int i = 0; i < status.steps - status.steps_completed; i++) {
138 status_printf(1, " ");
141 void pad_match_status() {
142 status_printf(1, " ");
145 void write_match_status() {
146 status_printf(1, format_string_working(), status.match_value);
149 void write_status() {
151 if (status.code == status.UNDEFINED
152 || status.code == status.FRAME_DONE
153 || status.code == status.SET_DONE
154 || status.code == status.IP_STEP_DONE) {
155 status_clear();
156 return;
159 if (status.code == status.GLOBAL_ALIGN
160 || status.code == status.ALIGN
161 || status.code == status.POSTMATCH
162 || status.code == status.EXPOSURE_PASS_2) {
163 pad_align_status();
164 write_match_status();
167 status_printf(1, " | ");
169 switch (status.code) {
170 case status_type::LOAD_FILE:
171 status_printf(4, "Loading Image (%s)",
172 "Loading Image", "Loading", "load",
173 status.cache ? "caching" : "cache is full");
174 break;
175 case status_type::EXPOSURE_PASS_1:
176 status_printf(3, "Registering exposure (first pass)", "Registering exposure", "regexp1");
177 break;
178 case status_type::LODCLUSTER_CREATE:
179 status_printf(3, "Creating LOD cluster, scale %g", "Creating LOD clusters", "lodcluster",
180 pow(2, -status.align_lod));
181 break;
182 case status_type::PREMATCH:
183 status_printf(3, "Calculating pre-alignment match", "Calculating match", "prematch");
184 break;
185 case status_type::GLOBAL_ALIGN:
186 case status_type::ALIGN:
187 status_printf(5, "%s [perturb=%6.3g] [lod=%6.3g] [exp_mult=%6.3g %6.3g %6.3g]",
188 "%s [perturb=%6.3g] [lod=%6.3g]",
189 "%s [perturb=%6.3g]",
190 "%s...",
191 "align",
192 (status.code == status_type::GLOBAL_ALIGN)
193 ? "Global alignment"
194 : "Aligning",
195 status.perturb_size,
196 pow(2, -status.align_lod),
197 status.exp_multiplier[0],
198 status.exp_multiplier[1],
199 status.exp_multiplier[2]);
200 break;
201 case status_type::POSTMATCH:
202 status_printf(3, "Calculating post-alignment match", "Calculating match", "postmatch");
203 break;
204 case status_type::EXPOSURE_PASS_2:
205 status_printf(3, "Registering exposure (second pass)", "Registering exposure", "regexp2");
206 break;
207 case status_type::RENDERA:
208 status_printf(3, "Rendering alignment reference image", "Rendering", "render-a");
209 break;
210 case status_type::RENDERD:
211 status_printf(3, "Rendering default chain", "Rendering", "render-d");
212 break;
213 case status_type::RENDERO:
214 status_printf(3, "Rendering chain %d", "Rendering", "render-o%d", status.onum);
215 break;
216 case status_type::WRITED:
217 status_printf(4, "Writing default chain to '%s'",
218 "Writing '%s'", "Writing", "write-d", d2::image_rw::output_name());
219 break;
220 case status_type::WRITEO:
221 status_printf(3, "Writing image for chain %d", "Writing", "write-o%d", status.onum);
222 break;
223 case status_type::IP_RENDER:
224 status_printf(3, "Frame '%s'%s",
225 "Frame '%s'", "Processing",
226 d2::image_rw::name(status.frame_num), status.irani_peleg_stage
227 ? ((status.irani_peleg_stage == 1) ? " [simulate ]" : " [backproject]")
228 : "");
229 break;
230 case status_type::IP_UPDATE:
231 status_printf(3, "Updating approximation", "Updating", "update");
232 break;
233 case status_type::IP_WRITE:
234 status_printf(3, "Writing '%s'", "Writing", "write", d2::image_rw::output_name());
235 break;
236 case status_type::D3_CONTROL_POINT_SOLVE:
237 status_printf(1, "Aligning control points, %g%% done, error=%g",
238 log(status.cp_cur_perturb / status.cp_max_perturb)
239 / log(status.cp_min_perturb / status.cp_max_perturb),
240 status.cp_cur_error);
241 break;
242 case status_type::D3_SUBDIVIDING_SPACE:
243 status_printf(2, "Subdividing space, frame pair (%u, %u), y=%u, x=%u, spaces=%u",
244 "Subdividing space", status.frame_num,
245 status.secondary_frame_num, status.y_coordinate, status.x_coordinate,
246 status.total_spaces);
247 break;
248 case status_type::D3_UPDATING_OCCUPANCY:
249 status_printf(2, "Updating occupancy, step %u/%u, frame %u, space %u/%u",
250 "Updating occupancy", status.steps_completed,
251 status.steps, status.frame_num,
252 status.space_num, status.total_spaces);
253 break;
254 case status_type::D3_RENDER:
255 if (status.filtering == 0 && status.focusing == 0) {
256 status_printf(1, "space %u/%u",
257 status.space_num, status.total_spaces);
258 } else if (status.filtering == 1 && status.focusing == 0) {
259 status_printf(1, "frame %u, y=%u, x=%u",
260 status.frame_num, status.y_coordinate, status.x_coordinate);
261 } else if (status.filtering == 0 && status.focusing == 1) {
262 status_printf(1, "y=%u, x=%u, view=%u", status.y_coordinate, status.x_coordinate,
263 status.view_num);
264 } else if (status.filtering == 1 && status.focusing == 1) {
265 status_printf(1, "view=%u, y=%u, x=%u, frame=%u",
266 status.view_num, status.y_coordinate, status.x_coordinate,
267 status.frame_num);
269 break;
271 default:
272 break;
275 status_clear();
278 void write_all() {
279 write_buffer();
280 write_status();
284 void printf(const char *format, ...) {
285 #ifdef USE_PTHREAD
286 pthread_mutex_lock(&lock);
287 #endif
288 va_list ap;
289 int n = -1;
291 if (buffer_index >= 0 && buffer_index < terminal_width /* && format[strlen(format) - 1] != '\n' */) {
292 va_start(ap, format);
293 n = vsnprintf(buffer + buffer_index, terminal_width - buffer_index, format, ap);
294 va_end(ap);
297 if (n >= 0 && n < terminal_width - buffer_index) {
299 * The message fits in the buffer, so update the index
300 * and write buffer and status information.
303 buffer_index += n;
305 if (format[strlen(format) - 1] == '\n') {
306 line_clear();
307 write_buffer();
308 } else
309 write_all();
311 } else {
313 * The message does not fit in the buffer, so write any
314 * existing buffer and append the new text to the stream.
316 if (buffer_index >= 0) {
317 assert(buffer_index < terminal_width);
318 buffer[buffer_index] = '\0';
319 write_buffer();
321 buffer_index = -1;
322 va_start(ap, format);
323 vfprintf(ui_stream, format, ap);
324 va_end(ap);
328 * This is not the only case that produces a newline,
329 * but ignoring other cases should be safe.
331 if (format[strlen(format) - 1] == '\n') {
332 buffer_index = 0;
333 buffer[0] = '\0';
335 #ifdef USE_PTHREAD
336 pthread_mutex_unlock(&lock);
337 #endif
340 void update() {
341 static time_t last_update = 0;
342 time_t now = time(NULL);
345 * Handle DONE status.
348 if (status.code == status_type::FRAME_DONE) {
349 printf(".");
350 #ifdef USE_PTHREAD
351 pthread_mutex_lock(&lock);
352 #endif
353 fputc('\n', ui_stream);
354 buffer_index = 0;
355 buffer[0] = '\0';
356 #ifdef USE_PTHREAD
357 pthread_mutex_unlock(&lock);
358 #endif
359 } else if (status.code == status_type::SET_DONE) {
360 #ifdef USE_PTHREAD
361 pthread_mutex_lock(&lock);
362 #endif
363 fputc('\n', ui_stream);
364 buffer_index = 0;
365 buffer[0] = '\0';
366 #ifdef USE_PTHREAD
367 pthread_mutex_unlock(&lock);
368 #endif
369 } else {
372 * Handle optional output.
375 #ifdef USE_PTHREAD
376 pthread_mutex_lock(&lock);
377 #endif
378 if (now == last_update) {
379 dirty = 1;
380 } else {
381 dirty = 0;
382 last_update = now;
383 write_all();
385 #ifdef USE_PTHREAD
386 pthread_mutex_unlock(&lock);
387 #endif
391 public:
393 #ifdef USE_PTHREAD
394 static void *update_loop(void *vu) {
395 ui_tty *u = (ui_tty *) vu;
397 for(;;) {
398 #ifdef HAVE_NANOSLEEP
399 struct timespec t;
400 t.tv_sec = 0;
401 t.tv_nsec = 100000000;
402 nanosleep(&t, NULL);
403 #else
404 sleep(1);
405 #endif
406 if (u->dirty) {
407 pthread_mutex_lock(&u->lock);
408 u->write_all();
409 u->dirty = 0;
410 pthread_mutex_unlock(&u->lock);
414 return NULL;
416 #endif
419 * Constructor may throw an exception to signal that using ui_wo would
420 * be more appropriate.
422 ui_tty() {
423 int exception_value = 1;
425 if (!isatty(fileno(ui_stream)))
426 throw exception_value;
429 * Don't use the last column, as this may cause
430 * wrapping in some environments (BSD, Hurd).
433 terminal_width = get_terminal_width(ui_stream) - 1;
435 if (terminal_width < 0)
436 throw exception_value;
438 buffer = (char *) calloc(terminal_width + 1, sizeof(char));
440 assert (buffer);
442 if (!buffer)
443 throw exception_value;
445 buffer[0] = '\0';
446 buffer_index = 0;
448 dirty = 0;
451 * Start an updating thread if possible.
453 #ifdef USE_PTHREAD
454 pthread_mutex_init(&lock, NULL);
455 pthread_attr_t pattr;
456 pthread_attr_init(&pattr);
457 pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
458 pthread_create(&update_thread, NULL, update_loop, (void *) this);
459 #endif
462 ~ui_tty() {
463 #ifdef USE_PTHREAD
464 pthread_cancel(update_thread);
465 pthread_join(update_thread, NULL);
466 #endif
467 free(buffer);
471 #endif