diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/QuickMedia.cpp | 52 | ||||
-rw-r--r-- | src/Vsync.cpp | 20 |
2 files changed, 61 insertions, 11 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 6935995..b7ed090 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -13,6 +13,7 @@ #include "../include/GoogleCaptcha.hpp" #include "../include/Notification.hpp" #include "../include/ImageViewer.hpp" +#include "../include/Vsync.hpp" #include <cppcodec/base64_rfc4648.hpp> #include <SFML/Graphics/RectangleShape.hpp> @@ -26,6 +27,7 @@ #include <string.h> #include <signal.h> #include <X11/keysym.h> +#include <X11/extensions/Xrandr.h> static const sf::Color back_color(34, 34, 34); static const int DOUBLE_CLICK_TIME = 500; @@ -46,6 +48,21 @@ static int x_io_error_handler(Display *display) { return 0; } +static int get_monitor_max_hz(Display *display) { + XRRScreenResources *screen_res = XRRGetScreenResources(display, DefaultRootWindow(display)); + if(screen_res) { + unsigned long max_hz = 0; + for(int i = 0; i < screen_res->nmode; ++i) { + unsigned long total = screen_res->modes[i].hTotal*screen_res->modes[i].vTotal; + if(total > 0) + max_hz = std::max(max_hz, screen_res->modes[i].dotClock/total); + } + XRRFreeScreenResources(screen_res); + return std::min(max_hz, 144UL); + } + return 60; +} + namespace QuickMedia { Program::Program() : disp(nullptr), @@ -65,7 +82,7 @@ namespace QuickMedia { resources_root = "/usr/share/quickmedia/"; } - window.setVerticalSyncEnabled(true); + window.setVerticalSyncEnabled(false); if(!font.loadFromFile(resources_root + "fonts/Lato-Regular.ttf")) { fprintf(stderr, "Failed to load font: Lato-Regular.ttf\n"); abort(); @@ -86,6 +103,9 @@ namespace QuickMedia { XSetErrorHandler(x_error_handler); XSetIOErrorHandler(x_io_error_handler); + + monitor_hz = get_monitor_max_hz(disp); + fprintf(stderr, "Monitor hz: %d\n", monitor_hz); } Program::~Program() { @@ -769,6 +789,7 @@ namespace QuickMedia { sf::RectangleShape tab_drop_shadow; tab_drop_shadow.setFillColor(sf::Color(23, 25, 27)); + VSync vsync(60); while (current_page == Page::SEARCH_SUGGESTION) { while (window.pollEvent(event)) { base_event_handler(event, Page::EXIT, false); @@ -876,7 +897,7 @@ namespace QuickMedia { window.draw(tab_drop_shadow); } search_bar->draw(window, false); - window.display(); + vsync.display(window); } search_bar->onTextBeginTypingCallback = nullptr; @@ -923,6 +944,7 @@ namespace QuickMedia { body->draw(window, body_pos, body_size); search_bar->draw(window); window.display(); + faefeaf } #endif } @@ -1096,7 +1118,7 @@ namespace QuickMedia { related_media_window_size.x = window_size.x * RELATED_MEDIA_WINDOW_WIDTH; related_media_window_size.y = window_size.y; related_media_window = std::make_unique<sf::RenderWindow>(sf::VideoMode(related_media_window_size.x, related_media_window_size.y), "", 0); - related_media_window->setVerticalSyncEnabled(true); + related_media_window->setVerticalSyncEnabled(false); related_media_window->setVisible(false); XReparentWindow(disp, related_media_window->getSystemHandle(), video_player_window, window_size.x - related_media_window_size.x, 0); } @@ -1218,6 +1240,7 @@ namespace QuickMedia { bool is_youtube = current_plugin->name == "youtube"; + VSync vsync(monitor_hz); while (current_page == Page::VIDEO_CONTENT) { while (window.pollEvent(event)) { base_event_handler(event, previous_page, true, false, false); @@ -1327,7 +1350,7 @@ namespace QuickMedia { related_videos_text.setPosition(body_pos.x, 10.0f); related_media_window->draw(related_videos_text); related_media_body->draw(*related_media_window, body_pos, body_size); - related_media_window->display(); + vsync.display(*related_media_window); continue; } @@ -1428,6 +1451,7 @@ namespace QuickMedia { bool redraw = true; sf::Event event; + VSync vsync(monitor_hz); while (current_page == Page::EPISODE_LIST) { while (window.pollEvent(event)) { base_event_handler(event, Page::SEARCH_SUGGESTION); @@ -1457,7 +1481,7 @@ namespace QuickMedia { window.clear(back_color); body->draw(window, body_pos, body_size, json_chapters); search_bar->draw(window); - window.display(); + vsync.display(window); } } @@ -1668,6 +1692,7 @@ namespace QuickMedia { sf::Clock check_downloaded_timer; const sf::Int32 check_downloaded_timeout_ms = 500; + VSync vsync(4); // TODO: Show to user if a certain page is missing (by checking page name (number) and checking if some are skipped) while (current_page == Page::IMAGES) { while(window.pollEvent(event)) { @@ -1768,7 +1793,7 @@ namespace QuickMedia { window.draw(chapter_text); } - window.display(); + vsync.display(window); } } @@ -1813,6 +1838,7 @@ namespace QuickMedia { show_notification("Manga progress", "Failed to save manga progress", Urgency::CRITICAL); } + VSync vsync(monitor_hz); while(current_page == Page::IMAGES_CONTINUOUS) { window.clear(back_color); ImageViewerAction action = image_viewer.draw(window); @@ -1827,7 +1853,7 @@ namespace QuickMedia { current_page = Page::IMAGES; break; } - window.display(); + vsync.display(window); int focused_page = image_viewer.get_focused_page(); image_index = focused_page - 1; @@ -1871,6 +1897,7 @@ namespace QuickMedia { bool redraw = true; sf::Event event; + VSync vsync(monitor_hz); while (current_page == Page::CONTENT_LIST) { while (window.pollEvent(event)) { base_event_handler(event, Page::SEARCH_SUGGESTION); @@ -1890,7 +1917,7 @@ namespace QuickMedia { window.clear(back_color); body->draw(window, body_pos, body_size); search_bar->draw(window); - window.display(); + vsync.display(window); } } @@ -1920,6 +1947,7 @@ namespace QuickMedia { bool redraw = true; sf::Event event; + VSync vsync(monitor_hz); while (current_page == Page::CONTENT_DETAILS) { while (window.pollEvent(event)) { base_event_handler(event, Page::CONTENT_LIST); @@ -1938,7 +1966,7 @@ namespace QuickMedia { window.clear(back_color); body->draw(window, body_pos, body_size); search_bar->draw(window); - window.display(); + vsync.display(window); } } @@ -1973,6 +2001,7 @@ namespace QuickMedia { bool redraw = true; sf::Event event; + VSync vsync(monitor_hz); while (current_page == Page::IMAGE_BOARD_THREAD_LIST) { while (window.pollEvent(event)) { base_event_handler(event, Page::SEARCH_SUGGESTION); @@ -1991,7 +2020,7 @@ namespace QuickMedia { window.clear(back_color); body->draw(window, body_pos, body_size); search_bar->draw(window); - window.display(); + vsync.display(window); } } @@ -2156,6 +2185,7 @@ namespace QuickMedia { std::stack<int> comment_navigation_stack; + VSync vsync(monitor_hz); while (current_page == Page::IMAGE_BOARD_THREAD) { while (window.pollEvent(event)) { search_bar->on_event(event); @@ -2398,7 +2428,7 @@ namespace QuickMedia { body->draw(window, body_pos, body_size); search_bar->draw(window); } - window.display(); + vsync.display(window); } // TODO: Instead of waiting for them, kill them somehow diff --git a/src/Vsync.cpp b/src/Vsync.cpp new file mode 100644 index 0000000..ab88205 --- /dev/null +++ b/src/Vsync.cpp @@ -0,0 +1,20 @@ +#include "../include/Vsync.hpp" +#include <time.h> +#include <unistd.h> + +namespace QuickMedia { + VSync::VSync(int framerate) : target_frame_delta_micro(1000.0 / (double)framerate * 1000.0), overflow(0) { + + } + + void VSync::display(sf::RenderWindow &window) { + sf::Int64 sleep_time_micro = target_frame_delta_micro - timer.getElapsedTime().asMicroseconds(); + window.display(); + if(sleep_time_micro > 0) { + if(usleep(sleep_time_micro) != 0) { + fprintf(stderr, "failed to sleep!\n"); + } + timer.restart(); + } + } +}
\ No newline at end of file |