From 257cb6d783ab96f77f187ead1b6032cd4b660a0e Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 16 Mar 2021 20:47:24 +0100 Subject: Temporary add --low-cpu-mode to reduce cpu usage when idle --- src/QuickMedia.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 9f8c7bd..3e1e61a 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -50,6 +50,8 @@ static const float tab_text_size = 16.0f; static const float tab_height = tab_text_size + 10.0f; static const sf::Color tab_selected_color(55, 60, 68); static const float tab_margin_x = 10.0f; +static int FPS_IDLE = 2; +static const double IDLE_TIMEOUT_SEC = 2.0; // Prevent writing to broken pipe from exiting the program static void sigpipe_handler(int) { @@ -409,10 +411,11 @@ namespace QuickMedia { XSetErrorHandler(x_error_handler); XSetIOErrorHandler(x_io_error_handler); - window.setFramerateLimit(0); + window.setVerticalSyncEnabled(false); monitor_hz = get_monitor_max_hz(disp); - window.setVerticalSyncEnabled(true); - vsync_set = true; + window.setFramerateLimit(FPS_IDLE); + idle = true; + vsync_set = false; /* if(enable_vsync(disp, window.getSystemHandle())) { vsync_set = true; @@ -535,6 +538,8 @@ namespace QuickMedia { usage(); return -1; } + } else if(strcmp(argv[i], "--low-cpu-mode") == 0) { + low_cpu_mode = true; } else if(argv[i][0] == '-') { fprintf(stderr, "Invalid option %s\n", argv[i]); usage(); @@ -542,6 +547,11 @@ namespace QuickMedia { } } + if(low_cpu_mode) + FPS_IDLE = 2; + else + FPS_IDLE = 20; + if(!plugin_name) { fprintf(stderr, "Missing plugin argument\n"); usage(); @@ -736,6 +746,25 @@ namespace QuickMedia { } } + void Program::event_idle_handler(const sf::Event &event) { + if(event.type == sf::Event::KeyPressed || event.type == sf::Event::TextEntered) { + if(idle) + window.setFramerateLimit(monitor_hz); + idle = false; + idle_timer.restart(); + } + } + + void Program::update_idle_state() { + if(idle) + return; + + if(idle_timer.getElapsedTime().asSeconds() > IDLE_TIMEOUT_SEC) { + window.setFramerateLimit(FPS_IDLE); + idle = true; + } + } + static std::string base64_encode(const std::string &data) { return base64_url::encode(data); } @@ -1029,6 +1058,9 @@ namespace QuickMedia { return; } + window.setFramerateLimit(FPS_IDLE); + idle = true; + for(Tab &tab : tabs) { tab.body->thumbnail_max_size = tab.page->get_thumbnail_max_size(); tab.page->on_navigate_to_page(tab.body.get()); @@ -1131,10 +1163,8 @@ namespace QuickMedia { manga_images_page->change_chapter(chapters_body->get_selected()->get_title(), chapters_body->get_selected()->url); } } - if(vsync_set) - window.setFramerateLimit(0); - else - window.setFramerateLimit(monitor_hz); + window.setFramerateLimit(FPS_IDLE); + idle = true; } else if(current_page == PageType::IMAGES_CONTINUOUS) { image_continuous_page(manga_images_page); } else { @@ -1217,6 +1247,7 @@ namespace QuickMedia { while (window.isOpen() && loop_running) { sf::Int32 frame_time_ms = frame_timer.restart().asMilliseconds(); while (window.pollEvent(event)) { + event_idle_handler(event); if (event.type == sf::Event::Closed) { window.close(); } else if(event.type == sf::Event::Resized) { @@ -1310,6 +1341,7 @@ namespace QuickMedia { } } } + update_idle_state(); if(redraw) { redraw = false; @@ -2479,6 +2511,8 @@ namespace QuickMedia { show_notification("QuickMedia", "Failed to save manga progress", Urgency::CRITICAL); } + window.setFramerateLimit(monitor_hz); + while(current_page == PageType::IMAGES_CONTINUOUS && window.isOpen()) { window.clear(back_color); ImageViewerAction action = image_viewer.draw(window); @@ -2520,6 +2554,9 @@ namespace QuickMedia { window_size.x = window.getSize().x; window_size.y = window.getSize().y; + + window.setFramerateLimit(FPS_IDLE); + idle = true; } static bool is_url_video(const std::string &url) { @@ -2693,6 +2730,7 @@ namespace QuickMedia { while (current_page == PageType::IMAGE_BOARD_THREAD && window.isOpen()) { while (window.pollEvent(event)) { + event_idle_handler(event); if(navigation_stage == NavigationStage::REPLYING && !frame_skip_text_entry) { comment_input.process_event(event); // To prevent pressing enter in comment_input text submit from also immediately sending captcha solution.. is there no better solution? @@ -2868,6 +2906,7 @@ namespace QuickMedia { } } frame_skip_text_entry = false; + update_idle_state(); chat_input_height_full = comment_input.get_height() + chat_input_padding_y * 2.0f; @@ -3053,6 +3092,7 @@ namespace QuickMedia { while (current_page == PageType::CHAT_LOGIN && window.isOpen()) { while (window.pollEvent(event)) { + event_idle_handler(event); if (event.type == sf::Event::Closed) { current_page = PageType::EXIT; window.close(); @@ -3073,6 +3113,7 @@ namespace QuickMedia { } inputs[focused_input]->on_event(event); } + update_idle_state(); if(redraw) { redraw = false; @@ -4225,6 +4266,7 @@ namespace QuickMedia { sf::Int32 frame_time_ms = frame_timer.restart().asMilliseconds(); while (window.pollEvent(event)) { base_event_handler(event, PageType::EXIT, tabs[selected_tab].body.get(), nullptr, false, false); + event_idle_handler(event); if(event.type == sf::Event::KeyPressed && event.key.control && event.key.alt && (chat_state == ChatState::NAVIGATING || chat_state == ChatState::URL_SELECTION)) { if(event.key.code == sf::Keyboard::Up || (event.key.control && event.key.code == sf::Keyboard::K)) { @@ -4496,6 +4538,7 @@ namespace QuickMedia { } } frame_skip_text_entry = false; + update_idle_state(); matrix_chat_page->update(); -- cgit v1.2.3