diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2020-12-18 21:22:59 +0100 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2020-12-18 21:22:59 +0100 |
commit | 872f9c3ab113a5387d59cdcb0591e3ec95fc06a7 (patch) | |
tree | 0c86bf51a6059a8bbf1dd0b365408aa6f9cf5d23 | |
parent | 3c0fae06b5ae2c59bacd2e3d6a1033f864cd368e (diff) | |
download | polecat-872f9c3ab113a5387d59cdcb0591e3ec95fc06a7.tar.gz polecat-872f9c3ab113a5387d59cdcb0591e3ec95fc06a7.zip |
add download progress bar
-rw-r--r-- | src/dxvk.c | 2 | ||||
-rw-r--r-- | src/lutris.c | 2 | ||||
-rw-r--r-- | src/net.c | 34 | ||||
-rw-r--r-- | src/net.h | 6 | ||||
-rw-r--r-- | src/wine.c | 2 |
5 files changed, 38 insertions, 8 deletions
@@ -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); } @@ -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; @@ -1,10 +1,14 @@ #ifndef NET_H #define NET_H +#include <curl/curl.h> #include <json.h> +#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 @@ -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); |