diff options
Diffstat (limited to 'stat.c')
| -rw-r--r-- | stat.c | 72 |
1 files changed, 72 insertions, 0 deletions
@@ -8,6 +8,7 @@ #include "para.h" #include "error.h" #include "string.h" +#include "sched.h" /** The minimal length of a status item buffer. */ #define MIN_STAT_ITEM_LEN 9 /* 5 + 2 + 2, e.g. '0005 00:\n' */ @@ -110,3 +111,74 @@ parse_error: PARA_ERROR_LOG("status item parse error\n"); return mask; } + +/* + * This function can go to gui_theme.c once the play_time status item has + * been disabled. + */ + +/* + * Compute the play time based on information of the current slot. + * + * Compute a string of the form "0:07 [3:33] (3%/3:40)". If there is a writer + * in any slot, use its start time. If audiod operates in standby mode, + * fall back to an approximation that is based on the status items only and + * prefix the returned string with "~". + * + * Returns a string as described above on success, the empty string if no + * stream is currently announced or if para_audiod is running in "off" mode, + * NULL if the stream is currently paused or repositioned. The memory for + * the returned string is allocated on the heap and must be freed by the caller. + */ +__malloc char *make_time_string(struct timeval *sss, struct timeval *wstime, + char **items) +{ + int length, seconds, offset; + struct timeval tmp; + char *si_seconds = items[SI_seconds_total], + *si_flags = items[SI_status_flags], + *si_offset = items[SI_offset]; + + if (!sss || !si_seconds || !si_flags || !si_offset) + return para_strdup(NULL); + length = atoi(items[SI_seconds_total]); + if (sss->tv_sec == 0 || length == 0) { /* no stream */ + if (strchr(items[SI_status_flags], 'P')) + return NULL; /* repositioning or about to change file */ + if (length > 0) { /* paused */ + seconds = atoi(items[SI_offset]); + goto out; + } + return para_strdup(NULL); /* stopped */ + } + offset = atoi(items[SI_offset]); + if (!wstime) { + struct timeval diff; + tv_diff(now, sss, &diff); + seconds = diff.tv_sec + offset; + goto out; + } + if (wstime->tv_sec == 0) + return NULL; + seconds = offset; + /* check if audiod was started in the middle of the stream */ + if (tv_diff(wstime, sss, &tmp) > 0 && tv2ms(&tmp) > 2000) { + tv_diff(now, sss, &tmp); /* assume it was */ + } else + tv_diff(now, wstime, &tmp); + seconds += tmp.tv_sec + (tmp.tv_usec + 500 * 1000) / 1000 / 1000; +out: + seconds = PARA_MIN(seconds, length); + seconds = PARA_MAX(seconds, 0); + return make_message( + "%s%d:%02d [%d:%02d] (%d%%/%d:%02d)", + wstime? "" : "~", + seconds / 60, + seconds % 60, + (length - seconds) / 60, + (length - seconds) % 60, + length? (seconds * 100 + length / 2) / length : 0, + length / 60, + length % 60 + ); +} |
