diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/AsyncImageLoader.cpp | 14 | ||||
-rw-r--r-- | src/Config.cpp | 31 | ||||
-rw-r--r-- | src/Utils.cpp | 7 | ||||
-rw-r--r-- | src/plugins/youtube/YoutubeMediaProxy.cpp | 11 |
4 files changed, 33 insertions, 30 deletions
diff --git a/src/AsyncImageLoader.cpp b/src/AsyncImageLoader.cpp index fce745a..f926a8d 100644 --- a/src/AsyncImageLoader.cpp +++ b/src/AsyncImageLoader.cpp @@ -4,6 +4,7 @@ #include "../include/ImageUtils.hpp" #include "../include/Scale.hpp" #include "../include/SfmlFixes.hpp" +#include "../include/Utils.hpp" #include "../external/hash-library/sha256.h" #include <unistd.h> @@ -206,10 +207,6 @@ namespace QuickMedia { thumbnail_load_data.thumbnail_data->loading_state = LoadingState::FINISHED_LOADING; } - static int64_t timeval_to_milliseconds(struct timeval &time) { - return (int64_t)time.tv_sec * 1000 + (int64_t)time.tv_usec / 1000; - } - AsyncImageLoader::AsyncImageLoader() { for(int i = 0; i < NUM_IMAGE_LOAD_PARALLEL; ++i) { downloads[i].read_program.pid = -1; @@ -233,10 +230,7 @@ namespace QuickMedia { Path tmp_thumbnail_path = download.thumbnail_path; tmp_thumbnail_path.append(".tmp"); if(status == 0 && rename_atomic(tmp_thumbnail_path.data.c_str(), download.thumbnail_path.data.c_str()) == 0) { - struct timeval time; - gettimeofday(&time, nullptr); - fprintf(stderr, "Download duration for %s: %ld ms\n", download.url.c_str(), timeval_to_milliseconds(time) - download.download_start); - + fprintf(stderr, "Download duration for %s: %ld ms\n", download.url.c_str(), get_boottime_milliseconds() - download.download_start); ThumbnailLoadData load_data = { std::move(download.url), std::move(download.thumbnail_path), false, download.thumbnail_data, download.resize_target_size }; process_thumbnail(load_data); } else { @@ -321,9 +315,7 @@ namespace QuickMedia { return; } - struct timeval time; - gettimeofday(&time, nullptr); - downloads[free_index].download_start = timeval_to_milliseconds(time); + downloads[free_index].download_start = get_boottime_milliseconds(); downloads[free_index].thumbnail_path = std::move(thumbnail_path); downloads[free_index].thumbnail_data = thumbnail_data; downloads[free_index].resize_target_size = resize_target_size; diff --git a/src/Config.cpp b/src/Config.cpp index 266c45c..ada2113 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -2,6 +2,8 @@ #include "../include/Storage.hpp" #include <json/value.h> #include <assert.h> +#include <X11/Xlib.h> +#include <X11/Xresource.h> namespace QuickMedia { static bool config_initialized = false; @@ -14,20 +16,27 @@ namespace QuickMedia { // Returns XFT_DPI_DEFAULT on error static int xrdb_get_dpi() { int xft_dpi = XFT_DPI_DEFAULT; - - FILE *xrdb_query = popen("xrdb -get Xft.dpi", "r"); - if(!xrdb_query) + Display *display = XOpenDisplay(nullptr); + if(!display) { + fprintf(stderr, "Failed to open x display\n"); return xft_dpi; + } - char line[32]; - line[0] = '\0'; - fread(line, 1, sizeof(line), xrdb_query); - - const int xft_dpi_file = atoi(line); - if(xft_dpi_file > 0) - xft_dpi = xft_dpi_file; + XrmInitialize(); + + char *resource_manager = XResourceManagerString(display); + if(resource_manager) { + XrmDatabase db = XrmGetStringDatabase(resource_manager); + if(db) { + char *type; + XrmValue val; + if(XrmGetResource(db, "Xft.dpi", "*", &type, &val)) + xft_dpi = strtol(val.addr, nullptr, 10); + XrmDestroyDatabase(db); + } + } - pclose(xrdb_query); + XCloseDisplay(display); return xft_dpi; } diff --git a/src/Utils.cpp b/src/Utils.cpp index 3e9fe7d..d466bee 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -78,6 +78,13 @@ namespace QuickMedia { return time_str; } + int64_t get_boottime_milliseconds() { + struct timespec time; + if(clock_gettime(CLOCK_BOOTTIME, &time) == -1 && errno == EINVAL) + clock_gettime(CLOCK_MONOTONIC, &time); + return (int64_t)time.tv_sec * 1000 + (int64_t)time.tv_nsec / 1000000; + } + sf::Vector2f vec2f_floor(float x, float y) { return sf::Vector2f(int(x), int(y)); } diff --git a/src/plugins/youtube/YoutubeMediaProxy.cpp b/src/plugins/youtube/YoutubeMediaProxy.cpp index c5af20e..4fe6c74 100644 --- a/src/plugins/youtube/YoutubeMediaProxy.cpp +++ b/src/plugins/youtube/YoutubeMediaProxy.cpp @@ -1,5 +1,6 @@ #include "../../../plugins/youtube/YoutubeMediaProxy.hpp" #include "../../../include/NetUtils.hpp" +#include "../../../include/Utils.hpp" #include <vector> #include <stdio.h> @@ -465,10 +466,7 @@ namespace QuickMedia { total_downloaded_bytes = 0; download_started = true; throttle_started = false; - - struct timespec tp; - clock_gettime(CLOCK_MONOTONIC, &tp); - download_start_time = tp.tv_sec; + download_start_time = get_boottime_milliseconds(); } total_downloaded_bytes += downloader_num_read_bytes; } @@ -476,10 +474,7 @@ namespace QuickMedia { #if 0 if(download_started) { - struct timespec tp; - clock_gettime(CLOCK_MONOTONIC, &tp); - - int64_t time_elapsed = tp.tv_sec - download_start_time; + const int64_t time_elapsed_sec = (get_boottime_milliseconds() - download_start_time) / 1000; int64_t download_speed_kb_sec = 0; if(time_elapsed > 0) download_speed_kb_sec = (total_downloaded_bytes / time_elapsed) / 1024; |