ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
render_progress.c File Reference

Go to the source code of this file.

Data Structures

struct  progress_snapshot_t
struct  render_progress
 render progress keeps the values that move through the subsystem: mutex, snapshot, fd, last_preview_ns. Some members are pointers into caller- or subsystem-owned memory, so lifetime follows the API that populated them rather than the struct copy itself. More...

Macros

#define TUI_RENDER_PROGRESS_LABEL_BUFFER_SIZE   160
#define TUI_RENDER_PROGRESS_TOTAL_BUFFER_SIZE   32
#define TUI_RENDER_PROGRESS_TITLE_BUFFER_SIZE   64
#define TUI_RENDER_PROGRESS_BAR_BUFFER_SIZE   4096
#define PREVIEW_MAX_BYTES   (1024 * 1024)

Functions

render_progress_t * render_progress_create (bool enabled, const char *destination)
void render_progress_frame (render_progress_t *progress, const char *frame, bool succeeded)
void render_progress_begin (render_progress_t *progress, uint64_t total, bool total_known)
void render_progress_finalize (render_progress_t *progress)
void render_progress_destroy (render_progress_t *progress)

Macro Definition Documentation

◆ PREVIEW_MAX_BYTES

#define PREVIEW_MAX_BYTES   (1024 * 1024)

Definition at line 25 of file render_progress.c.

Referenced by render_progress_frame().

◆ TUI_RENDER_PROGRESS_BAR_BUFFER_SIZE

#define TUI_RENDER_PROGRESS_BAR_BUFFER_SIZE   4096

Definition at line 6 of file render_progress.c.

◆ TUI_RENDER_PROGRESS_LABEL_BUFFER_SIZE

#define TUI_RENDER_PROGRESS_LABEL_BUFFER_SIZE   160

Definition at line 3 of file render_progress.c.

◆ TUI_RENDER_PROGRESS_TITLE_BUFFER_SIZE

#define TUI_RENDER_PROGRESS_TITLE_BUFFER_SIZE   64

Definition at line 5 of file render_progress.c.

◆ TUI_RENDER_PROGRESS_TOTAL_BUFFER_SIZE

#define TUI_RENDER_PROGRESS_TOTAL_BUFFER_SIZE   32

Definition at line 4 of file render_progress.c.

Function Documentation

◆ render_progress_begin()

void render_progress_begin ( render_progress_t * progress,
uint64_t total,
bool total_known )

Definition at line 196 of file render_progress.c.

196 {
197 if (!progress)
198 return;
199 mutex_lock(&progress->mutex);
200 progress->active = true;
201 progress->snapshot->total = total;
202 progress->snapshot->total_known = total_known;
203 progress_publish(progress);
204 mutex_unlock(&progress->mutex);
205}
progress_snapshot_t * snapshot

References render_progress::active, render_progress::mutex, render_progress::snapshot, progress_snapshot_t::total, and progress_snapshot_t::total_known.

Referenced by render_file_begin_drain().

◆ render_progress_create()

render_progress_t * render_progress_create ( bool enabled,
const char * destination )

Retains only the latest ASCII frame. All updates are serialized; callbacks own copied snapshots.

Definition at line 135 of file render_progress.c.

135 {
136 if (!enabled || !terminal_is_stdout_tty())
137 return NULL;
138 render_progress_t *progress = SAFE_CALLOC(1, sizeof(*progress), render_progress_t *);
139 progress->fd = platform_dup(STDOUT_FILENO);
140 if (progress->fd < 0) {
141 SAFE_FREE(progress);
142 return NULL;
143 }
144 mutex_init(&progress->mutex, "render_progress");
145 size_t destination_length = strlen(destination);
146 progress->snapshot = SAFE_CALLOC(1, sizeof(*progress->snapshot) + destination_length + 2, progress_snapshot_t *);
147 progress->snapshot->destination_length = destination_length;
148 char *name = progress->snapshot->frame + 1;
149 memcpy(name, destination, destination_length);
150 // A path must not inject terminal control sequences into the overlay.
151 for (size_t i = 0; i < destination_length; ++i) {
152 if ((unsigned char)name[i] < 0x20 || name[i] == 0x7f)
153 name[i] = '?';
154 }
155 progress->snapshot->started_ns = time_get_ns();
156 return progress;
157}
bool enabled
Is filtering active?
Definition grep.c:62
#define SAFE_FREE(ptr)
Definition common.h:377
#define SAFE_CALLOC(count, size, cast)
Definition common.h:275
int platform_dup(int fd)
struct render_progress render_progress_t

References progress_snapshot_t::destination_length, enabled, render_progress::fd, progress_snapshot_t::frame, render_progress::mutex, platform_dup(), SAFE_CALLOC, SAFE_FREE, render_progress::snapshot, and progress_snapshot_t::started_ns.

Referenced by render_file_create().

◆ render_progress_destroy()

void render_progress_destroy ( render_progress_t * progress)

Definition at line 221 of file render_progress.c.

221 {
222 if (!progress)
223 return;
225 platform_close(progress->fd);
226 SAFE_FREE(progress->snapshot);
227 mutex_destroy(&progress->mutex);
228 SAFE_FREE(progress);
229}
void ui_controller_remove(ui_screen_t screen)
@ UI_SCREEN_RENDER_PROGRESS

References render_progress::fd, render_progress::mutex, SAFE_FREE, render_progress::snapshot, ui_controller_remove(), and UI_SCREEN_RENDER_PROGRESS.

Referenced by render_file_destroy().

◆ render_progress_finalize()

void render_progress_finalize ( render_progress_t * progress)

Definition at line 207 of file render_progress.c.

207 {
208 if (!progress)
209 return;
210 mutex_lock(&progress->mutex);
211 progress->active = true;
212 progress->snapshot->finalizing = true;
213 if (!progress->snapshot->total_known) {
214 progress->snapshot->total = progress->snapshot->completed;
215 progress->snapshot->total_known = true;
216 }
217 progress_publish(progress);
218 mutex_unlock(&progress->mutex);
219}

References render_progress::active, progress_snapshot_t::completed, progress_snapshot_t::finalizing, render_progress::mutex, render_progress::snapshot, progress_snapshot_t::total, and progress_snapshot_t::total_known.

Referenced by render_file_destroy().

◆ render_progress_frame()

void render_progress_frame ( render_progress_t * progress,
const char * frame,
bool succeeded )

Definition at line 159 of file render_progress.c.

159 {
160 if (!progress)
161 return;
162 mutex_lock(&progress->mutex);
163 uint64_t now = time_get_ns();
164 // The overlay refreshes at most eight times per second. Expanding and copying
165 // every full-resolution frame only slows the encoder and cannot be displayed.
166 if (succeeded && !progress->snapshot->failed && progress->last_preview_ns && now >= progress->last_preview_ns &&
167 now - progress->last_preview_ns < 125 * NS_PER_MS_INT) {
168 ++progress->snapshot->completed;
169 mutex_unlock(&progress->mutex);
170 return;
171 }
172 progress->last_preview_ns = now;
173 size_t length = strnlen(frame, (size_t)PREVIEW_MAX_BYTES + 1u);
174 // Oversized frames still encode normally; omit their terminal preview.
175 if (length > PREVIEW_MAX_BYTES)
176 length = 0;
177 progress_snapshot_t *next =
178 SAFE_CALLOC(1, sizeof(*next) + length + progress->snapshot->destination_length + 2, progress_snapshot_t *);
179 memcpy(next, progress->snapshot, sizeof(*next));
180 next->length = length;
181 memcpy(next->frame, frame, length);
182 memcpy(next->frame + length + 1, progress->snapshot->frame + progress->snapshot->length + 1,
183 next->destination_length + 1);
184 if (!next->started_ns)
185 next->started_ns = time_get_ns();
186 if (succeeded)
187 next->completed++;
188 else
189 next->failed = true;
190 SAFE_FREE(progress->snapshot);
191 progress->snapshot = next;
192 progress_publish(progress);
193 mutex_unlock(&progress->mutex);
194}
unsigned long long uint64_t
Definition common.h:63
#define NS_PER_MS_INT
Definition time.h:168
#define PREVIEW_MAX_BYTES
uint64_t last_preview_ns

References progress_snapshot_t::completed, progress_snapshot_t::destination_length, progress_snapshot_t::failed, progress_snapshot_t::frame, render_progress::last_preview_ns, progress_snapshot_t::length, render_progress::mutex, NS_PER_MS_INT, PREVIEW_MAX_BYTES, SAFE_CALLOC, SAFE_FREE, render_progress::snapshot, and progress_snapshot_t::started_ns.

Referenced by render_file_write_frame().