d2::render::ipc: Fix compile errors.
[Ale.git] / ui / ui_tty.h
blob9a2d6fe2d6aa2ca500a43046be499c689452c71d
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(6, "%s [mc=%6.3g] [perturb=%6.3g] [lod=%6.3g] [exp_mult=%6.3g %6.3g %6.3g]",
188 "%s [mc=%6.3g] [perturb=%6.3g] [lod=%6.3g]",
189 "%s [mc=%6.3g] [perturb=%6.3g]",
190 "%s [mc=%6.3g]",
191 "%s...",
192 "align",
193 (status.code == status_type::GLOBAL_ALIGN)
194 ? "Global alignment"
195 : "Aligning",
196 status.mc,
197 status.perturb_size,
198 pow(2, -status.align_lod),
199 status.exp_multiplier[0],
200 status.exp_multiplier[1],
201 status.exp_multiplier[2]);
202 break;
203 case status_type::POSTMATCH:
204 status_printf(3, "Calculating post-alignment match", "Calculating match", "postmatch");
205 break;
206 case status_type::EXPOSURE_PASS_2:
207 status_printf(3, "Registering exposure (second pass)", "Registering exposure", "regexp2");
208 break;
209 case status_type::RENDERA:
210 status_printf(3, "Rendering alignment reference image", "Rendering", "render-a");
211 break;
212 case status_type::RENDERD:
213 status_printf(3, "Rendering default chain", "Rendering", "render-d");
214 break;
215 case status_type::RENDERO:
216 status_printf(3, "Rendering chain %d", "Rendering", "render-o%d", status.onum);
217 break;
218 case status_type::WRITED:
219 status_printf(4, "Writing default chain to '%s'",
220 "Writing '%s'", "Writing", "write-d", d2::image_rw::output_name());
221 break;
222 case status_type::WRITEO:
223 status_printf(3, "Writing image for chain %d", "Writing", "write-o%d", status.onum);
224 break;
225 case status_type::IP_RENDER:
226 status_printf(3, "Frame '%s'%s",
227 "Frame '%s'", "Processing",
228 d2::image_rw::name(status.frame_num), status.irani_peleg_stage
229 ? ((status.irani_peleg_stage == 1) ? " [simulate ]" : " [backproject]")
230 : "");
231 break;
232 case status_type::IP_UPDATE:
233 status_printf(3, "Updating approximation", "Updating", "update");
234 break;
235 case status_type::IP_WRITE:
236 status_printf(3, "Writing '%s'", "Writing", "write", d2::image_rw::output_name());
237 break;
238 case status_type::D3_CONTROL_POINT_SOLVE:
239 status_printf(1, "Aligning control points, %g%% done, error=%g",
240 log(status.cp_cur_perturb / status.cp_max_perturb)
241 / log(status.cp_min_perturb / status.cp_max_perturb),
242 status.cp_cur_error);
243 break;
244 case status_type::D3_SUBDIVIDING_SPACE:
245 status_printf(2, "Subdividing space, frame pair (%u, %u), y=%u, x=%u, spaces=%u",
246 "Subdividing space", status.frame_num,
247 status.secondary_frame_num, status.y_coordinate, status.x_coordinate,
248 status.total_spaces);
249 break;
250 case status_type::D3_UPDATING_OCCUPANCY:
251 status_printf(2, "Updating occupancy, step %u/%u, frame %u, space %u/%u",
252 "Updating occupancy", status.steps_completed,
253 status.steps, status.frame_num,
254 status.space_num, status.total_spaces);
255 break;
256 case status_type::D3_RENDER:
257 if (status.filtering == 0 && status.focusing == 0) {
258 status_printf(1, "space %u/%u",
259 status.space_num, status.total_spaces);
260 } else if (status.filtering == 1 && status.focusing == 0) {
261 status_printf(1, "frame %u, y=%u, x=%u",
262 status.frame_num, status.y_coordinate, status.x_coordinate);
263 } else if (status.filtering == 0 && status.focusing == 1) {
264 status_printf(1, "y=%u, x=%u, view=%u", status.y_coordinate, status.x_coordinate,
265 status.view_num);
266 } else if (status.filtering == 1 && status.focusing == 1) {
267 status_printf(1, "view=%u, y=%u, x=%u, frame=%u",
268 status.view_num, status.y_coordinate, status.x_coordinate,
269 status.frame_num);
271 break;
273 default:
274 break;
277 status_clear();
280 void write_all() {
281 write_buffer();
282 write_status();
286 void printf(const char *format, ...) {
287 #ifdef USE_PTHREAD
288 pthread_mutex_lock(&lock);
289 #endif
290 va_list ap;
291 int n = -1;
293 if (buffer_index >= 0 && buffer_index < terminal_width /* && format[strlen(format) - 1] != '\n' */) {
294 va_start(ap, format);
295 n = vsnprintf(buffer + buffer_index, terminal_width - buffer_index, format, ap);
296 va_end(ap);
299 if (n >= 0 && n < terminal_width - buffer_index) {
301 * The message fits in the buffer, so update the index
302 * and write buffer and status information.
305 buffer_index += n;
307 if (format[strlen(format) - 1] == '\n') {
308 line_clear();
309 write_buffer();
310 } else
311 write_all();
313 } else {
315 * The message does not fit in the buffer, so write any
316 * existing buffer and append the new text to the stream.
318 if (buffer_index >= 0) {
319 assert(buffer_index < terminal_width);
320 buffer[buffer_index] = '\0';
321 write_buffer();
323 buffer_index = -1;
324 va_start(ap, format);
325 vfprintf(ui_stream, format, ap);
326 va_end(ap);
330 * This is not the only case that produces a newline,
331 * but ignoring other cases should be safe.
333 if (format[strlen(format) - 1] == '\n') {
334 buffer_index = 0;
335 buffer[0] = '\0';
337 #ifdef USE_PTHREAD
338 pthread_mutex_unlock(&lock);
339 #endif
342 void update() {
343 static time_t last_update = 0;
344 time_t now = time(NULL);
347 * Handle DONE status.
350 if (status.code == status_type::FRAME_DONE) {
351 printf(".");
352 #ifdef USE_PTHREAD
353 pthread_mutex_lock(&lock);
354 #endif
355 fputc('\n', ui_stream);
356 buffer_index = 0;
357 buffer[0] = '\0';
358 #ifdef USE_PTHREAD
359 pthread_mutex_unlock(&lock);
360 #endif
361 } else if (status.code == status_type::SET_DONE) {
362 #ifdef USE_PTHREAD
363 pthread_mutex_lock(&lock);
364 #endif
365 fputc('\n', ui_stream);
366 buffer_index = 0;
367 buffer[0] = '\0';
368 #ifdef USE_PTHREAD
369 pthread_mutex_unlock(&lock);
370 #endif
371 } else {
374 * Handle optional output.
377 #ifdef USE_PTHREAD
378 pthread_mutex_lock(&lock);
379 #endif
380 if (now == last_update) {
381 dirty = 1;
382 } else {
383 dirty = 0;
384 last_update = now;
385 write_all();
387 #ifdef USE_PTHREAD
388 pthread_mutex_unlock(&lock);
389 #endif
393 public:
395 #ifdef USE_PTHREAD
396 static void *update_loop(void *vu) {
397 ui_tty *u = (ui_tty *) vu;
399 for(;;) {
400 #ifdef HAVE_NANOSLEEP
401 struct timespec t;
402 t.tv_sec = 0;
403 t.tv_nsec = 100000000;
404 nanosleep(&t, NULL);
405 #else
406 sleep(1);
407 #endif
408 if (u->dirty) {
409 pthread_mutex_lock(&u->lock);
410 u->write_all();
411 u->dirty = 0;
412 pthread_mutex_unlock(&u->lock);
416 return NULL;
418 #endif
421 * Constructor may throw an exception to signal that using ui_wo would
422 * be more appropriate.
424 ui_tty() {
425 int exception_value = 1;
427 if (!isatty(fileno(ui_stream)))
428 throw exception_value;
431 * Don't use the last column, as this may cause
432 * wrapping in some environments (BSD, Hurd).
435 terminal_width = get_terminal_width(ui_stream) - 1;
437 if (terminal_width < 0)
438 throw exception_value;
440 buffer = (char *) calloc(terminal_width + 1, sizeof(char));
442 assert (buffer);
444 if (!buffer)
445 throw exception_value;
447 buffer[0] = '\0';
448 buffer_index = 0;
450 dirty = 0;
453 * Start an updating thread if possible.
455 #ifdef USE_PTHREAD
456 pthread_mutex_init(&lock, NULL);
457 pthread_attr_t pattr;
458 pthread_attr_init(&pattr);
459 pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
460 pthread_create(&update_thread, NULL, update_loop, (void *) this);
461 #endif
464 ~ui_tty() {
465 #ifdef USE_PTHREAD
466 pthread_cancel(update_thread);
467 pthread_join(update_thread, NULL);
468 #endif
469 free(buffer);
473 #endif