From 872f9c3ab113a5387d59cdcb0591e3ec95fc06a7 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Fri, 18 Dec 2020 21:22:59 +0100 Subject: add download progress bar --- src/dxvk.c | 2 +- src/lutris.c | 2 +- src/net.c | 34 ++++++++++++++++++++++++++++++---- src/net.h | 6 +++++- src/wine.c | 2 +- 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/dxvk.c b/src/dxvk.c index 3fb24cc..706a119 100644 --- a/src/dxvk.c +++ b/src/dxvk.c @@ -64,7 +64,7 @@ COMMAND(dxvk, download) fprintf(stderr, "Downloading %s\n", name); - archive = downloadToRam(json_object_get_string(temp)); + archive = downloadToRam(json_object_get_string(temp), 0L); if (archive) { fprintf(stderr, "Extracting %s\n", name); diff --git a/src/lutris.c b/src/lutris.c index b53ebda..4bc0f0f 100644 --- a/src/lutris.c +++ b/src/lutris.c @@ -39,7 +39,7 @@ COMMAND(lutris, install) { char* filename = basename(installer.files[i]->url); printf("Dowloading %s...\n", filename); - files[i] = downloadToRam(installer.files[i]->url); + files[i] = downloadToRam(installer.files[i]->url, 0L); } diff --git a/src/net.c b/src/net.c index b435f4d..65880a2 100644 --- a/src/net.c +++ b/src/net.c @@ -9,7 +9,12 @@ #include "net.h" #include "common.h" -size_t memoryCallback(void* contents, size_t size, size_t nmemb, void* userp) +struct progress { + TIMETYPE lastruntime; + CURL *curl; +}; + +static size_t memoryCallback(void* contents, size_t size, size_t nmemb, void* userp) { size_t realsize = size * nmemb; struct MemoryStruct* mem = (struct MemoryStruct*)userp; @@ -29,10 +34,28 @@ size_t memoryCallback(void* contents, size_t size, size_t nmemb, void* userp) return realsize; } -struct MemoryStruct* downloadToRam(const char* URL) +static int xferinfo(void *p, curl_off_t dltotal, curl_off_t dlnow, UNUSED curl_off_t ultotal, UNUSED curl_off_t ulnow) +{ + struct progress *myp = (struct progress *)p; + CURL *curl = myp->curl; + TIMETYPE curtime = 0; + + curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &curtime); + + curl_off_t progress = 0; + if (dltotal != 0) + progress = ((float)dlnow / dltotal) * 100; + + fprintf(stderr, "Progress: %3" CURL_FORMAT_CURL_OFF_T "%% \r", progress); + + return 0; +} + +struct MemoryStruct* downloadToRam(const char* URL, long progress) { CURL* curl_handle; - CURLcode res; + CURLcode res = CURLE_OK; + struct progress prog; struct MemoryStruct* chunk = malloc(sizeof(struct MemoryStruct)); @@ -50,6 +73,9 @@ struct MemoryStruct* downloadToRam(const char* URL) curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void*)chunk); curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, USER_AGENT); curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl_handle, CURLOPT_XFERINFOFUNCTION, xferinfo); + curl_easy_setopt(curl_handle, CURLOPT_XFERINFODATA, &prog); + curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, progress); res = curl_easy_perform(curl_handle); @@ -77,7 +103,7 @@ struct MemoryStruct* downloadToRam(const char* URL) struct json_object* fetchJSON(const char* URL) { - struct MemoryStruct* chunk = downloadToRam(URL); + struct MemoryStruct* chunk = downloadToRam(URL, 1L); struct json_object* json = NULL; diff --git a/src/net.h b/src/net.h index 8c5b894..25790d9 100644 --- a/src/net.h +++ b/src/net.h @@ -1,10 +1,14 @@ #ifndef NET_H #define NET_H +#include #include +#define TIMETYPE curl_off_t +#define TIMEOPT CURLINFO_TOTAL_TIME_T + size_t WriteMemoryCallback(void*, size_t, size_t, void*); -struct MemoryStruct* downloadToRam(const char* URL); +struct MemoryStruct* downloadToRam(const char* URL, long); struct json_object* fetchJSON(const char*); #endif diff --git a/src/wine.c b/src/wine.c index c37f7bd..f1b3823 100644 --- a/src/wine.c +++ b/src/wine.c @@ -70,7 +70,7 @@ COMMAND(wine, download) fprintf(stderr, "Downloading %s\n", name); - archive = downloadToRam(json_object_get_string(temp)); + archive = downloadToRam(json_object_get_string(temp), 0L); if (archive) { fprintf(stderr, "Extracting %s\n", name); -- cgit v1.2.3