diff --git a/.github/workflows/static_analysis.yml b/.github/workflows/static_analysis.yml index afc47777..94efc212 100644 --- a/.github/workflows/static_analysis.yml +++ b/.github/workflows/static_analysis.yml @@ -19,4 +19,4 @@ jobs: use_cmake: false # (Optional) cppcheck args - cppcheck_args: -i$GITHUB_WORKSPACE/lib -i$GITHUB_WORKSPACE/components/gbc/gnuboy -i$GITHUB_WORKSPACE/components/nes/nofrendo -i$GITHUB_WORKSPACE/components/jpegdec -i$GITHUB_WORKSPACE/components/codec -i$GITHUB_WORKSPACE/components/espp --force --enable=all --inline-suppr --inconclusive --platform=mips32 --suppressions-list=$GITHUB_WORKSPACE/suppressions.txt + cppcheck_args: -i$GITHUB_WORKSPACE/lib -i$GITHUB_WORKSPACE/components/gbc/gnuboy -i$GITHUB_WORKSPACE/components/nes/nofrendo -i$GITHUB_WORKSPACE/components/gui/generated -i$GITHUB_WORKSPACE/components/menu/generated -i$GITHUB_WORKSPACE/components/jpegdec -i$GITHUB_WORKSPACE/components/codec -i$GITHUB_WORKSPACE/components/espp --force --enable=all --inline-suppr --inconclusive --platform=mips32 --suppressions-list=$GITHUB_WORKSPACE/suppressions.txt diff --git a/components/gui/include/gui.hpp b/components/gui/include/gui.hpp index 5c14673f..d357a57c 100644 --- a/components/gui/include/gui.hpp +++ b/components/gui/include/gui.hpp @@ -33,7 +33,7 @@ class Gui { espp::Logger::Verbosity log_level{espp::Logger::Verbosity::WARN}; }; - Gui(const Config& config) + explicit Gui(const Config& config) : play_haptic_(config.play_haptic), set_waveform_(config.set_waveform), display_(config.display), @@ -64,7 +64,7 @@ class Gui { ready_to_play_ = new_state; } - bool ready_to_play() { + bool ready_to_play() const { return ready_to_play_; } @@ -84,7 +84,7 @@ class Gui { void add_rom(const RomInfo& rom); - std::optional get_selected_rom() { + std::optional get_selected_rom() const { if (focused_rom_ < 0 || focused_rom_ >= rom_infos_.size()) { return std::nullopt; } diff --git a/components/jpeg/include/jpeg.hpp b/components/jpeg/include/jpeg.hpp index b73779df..2df912b1 100644 --- a/components/jpeg/include/jpeg.hpp +++ b/components/jpeg/include/jpeg.hpp @@ -80,6 +80,7 @@ class Jpeg { return; } // get size from current location (end) + // cppcheck-suppress unreadVariable *size = (size_t)imgfile_.tellg(); // reset file pointer to beginning imgfile_.seekg(0, std::ios::beg); @@ -110,7 +111,7 @@ class Jpeg { auto ys = pDraw->y; auto ye = pDraw->y + height - 1; uint16_t *dst_buffer = (uint16_t*)decoded_data_; - uint16_t *src_buffer = (uint16_t*)pDraw->pPixels; + const uint16_t *src_buffer = (const uint16_t*)pDraw->pPixels; // two bytes per pixel for RGB565 auto num_bytes_per_row = width * 2; for (int y=ys; y<=ye; y++) { diff --git a/components/menu/include/menu.hpp b/components/menu/include/menu.hpp index 0314856b..1ec836bd 100644 --- a/components/menu/include/menu.hpp +++ b/components/menu/include/menu.hpp @@ -32,7 +32,7 @@ class Menu { espp::Logger::Verbosity log_level{espp::Logger::Verbosity::WARN}; }; - Menu(const Config& config) + explicit Menu(const Config& config) : display_(config.display), paused_image_path_(config.paused_image_path), action_callback_(config.action_callback), @@ -97,7 +97,7 @@ class Menu { void set_video_setting(VideoSetting setting); - bool is_paused() { return paused_; } + bool is_paused() const { return paused_; } void pause() { paused_ = true; lv_group_focus_freeze(group_, true); diff --git a/components/nes/src/nes.cpp b/components/nes/src/nes.cpp index 91bae685..0db9c35f 100644 --- a/components/nes/src/nes.cpp +++ b/components/nes/src/nes.cpp @@ -94,8 +94,8 @@ std::vector get_nes_video_buffer() { std::vector frame(NES_SCREEN_WIDTH * NES_VISIBLE_HEIGHT * 2); // the frame data for the NES is stored in frame_buffer0 as a 8 bit index into the palette // we need to convert this to a 16 bit RGB565 value - uint8_t *frame_buffer0 = get_frame_buffer0(); - uint16_t *palette = get_nes_palette(); + const uint8_t *frame_buffer0 = get_frame_buffer0(); + const uint16_t *palette = get_nes_palette(); for (int i = 0; i < NES_SCREEN_WIDTH * NES_VISIBLE_HEIGHT; i++) { uint8_t index = frame_buffer0[i]; uint16_t color = palette[index]; diff --git a/components/nes/src/video_audio.c b/components/nes/src/video_audio.c index 1682c86b..9ea538d6 100644 --- a/components/nes/src/video_audio.c +++ b/components/nes/src/video_audio.c @@ -149,7 +149,6 @@ void osd_set_video_scale(bool new_video_scale) { } void ili9341_write_frame_nes(const uint8_t* buffer, uint16_t* myPalette) { - short x, y; static const int x_offset = (320-256)/2; static const int y_offset = (240-224)/2; if (buffer == NULL) { @@ -163,11 +162,12 @@ void ili9341_write_frame_nes(const uint8_t* buffer, uint16_t* myPalette) { lcd_write_frame(0,0,320,240,NULL); } if (scale_video) { - uint8_t* framePtr = buffer; + const uint8_t* framePtr = buffer; static int buffer_index = 0; static const int LINE_COUNT = NUM_ROWS_IN_FRAME_BUFFER; float x_scale = 1.25f; float y_scale = 1.0f; + short x, y; for (y = 0; y < 240; y+= LINE_COUNT) { uint16_t* line_buffer = buffer_index ? (uint16_t*)get_vram1() : (uint16_t*)get_vram0(); buffer_index = buffer_index ? 0 : 1; @@ -186,9 +186,10 @@ void ili9341_write_frame_nes(const uint8_t* buffer, uint16_t* myPalette) { lcd_write_frame(0, y_offset+y, 320, num_lines_written, (uint8_t*)&line_buffer[0]); } } else { - uint8_t* framePtr = buffer; + const uint8_t* framePtr = buffer; static int buffer_index = 0; static const int LINE_COUNT = NUM_ROWS_IN_FRAME_BUFFER; + short x, y; for (y = 0; y < NES_GAME_HEIGHT; y+= LINE_COUNT) { uint16_t* line_buffer = buffer_index ? (uint16_t*)get_vram1() : (uint16_t*)get_vram0(); buffer_index = buffer_index ? 0 : 1; diff --git a/main/button_handlers.hpp b/main/button_handlers.hpp deleted file mode 100644 index 3d5267f9..00000000 --- a/main/button_handlers.hpp +++ /dev/null @@ -1,194 +0,0 @@ -#pragma once - -#include - -#include "format.hpp" - -#include "joypad_buttons.hpp" -#include "nes_lib/InputDevice.h" - -class Report { -public: - enum class Field { - TYPE, - LEFT_ANALOG_X, - LEFT_ANALOG_Y, - RIGHT_ANALOG_X, - RIGHT_ANALOG_Y, - BUTTON_CODE_1, - BUTTON_CODE_2, - UNKNOWN, - LEFT_ANALOG_TRIGGER, - RIGHT_ANALOG_TRIGGER, - }; - - enum class ButtonCode1 { - Y = 4, // left - B = 5, // down - A = 6, // right - X = 7, // up - }; - enum class ButtonCode2 { - L1 = 0, - R1 = 1, - L2 = 2, - R2 = 3, - SELECT = 4, - START = 5, - }; - - Report(const std::vector& data) { - left_stick_x = parse_analog(data[(int)Field::LEFT_ANALOG_X]); - left_stick_y = parse_analog(data[(int)Field::LEFT_ANALOG_Y]); - right_stick_x = parse_analog(data[(int)Field::RIGHT_ANALOG_X]); - right_stick_y = parse_analog(data[(int)Field::RIGHT_ANALOG_Y]); - - uint8_t button_code_1 = data[(int)Field::BUTTON_CODE_1]; - decode_dpad(button_code_1); - a = button_code_1 & (1 << (int)ButtonCode1::A); - b = button_code_1 & (1 << (int)ButtonCode1::B); - x = button_code_1 & (1 << (int)ButtonCode1::X); - y = button_code_1 & (1 << (int)ButtonCode1::Y); - - uint8_t button_code_2 = data[(int)Field::BUTTON_CODE_2]; - l1 = button_code_2 & (1 << (int)ButtonCode2::L1); - r1 = button_code_2 & (1 << (int)ButtonCode2::R1); - l2 = button_code_2 & (1 << (int)ButtonCode2::L2); - r2 = button_code_2 & (1 << (int)ButtonCode2::R2); - start = button_code_2 & (1 << (int)ButtonCode2::START); - select = button_code_2 & (1 << (int)ButtonCode2::SELECT); - - left_trigger = parse_analog(data[(int)Field::LEFT_ANALOG_TRIGGER], 0.0f, 255.0f); - right_trigger = parse_analog(data[(int)Field::RIGHT_ANALOG_TRIGGER], 0.0f, 255.0f); - } - - void set_joypad_state(InputDevice *joypad) { - joypad->externState[(int)JoypadButtons::A] = a; - joypad->externState[(int)JoypadButtons::B] = b; - joypad->externState[(int)JoypadButtons::Select] = select; - joypad->externState[(int)JoypadButtons::Start] = start; - joypad->externState[(int)JoypadButtons::Up] = dpad_up; - joypad->externState[(int)JoypadButtons::Down] = dpad_down; - joypad->externState[(int)JoypadButtons::Left] = dpad_left; - joypad->externState[(int)JoypadButtons::Right] = dpad_right; - } - - void decode_dpad(uint8_t value) { - dpad_up = dpad_down = dpad_left = dpad_right = false; - switch (value) { - case 0: - dpad_up = true; - break; - case 1: - dpad_up = true; - dpad_right = true; - break; - case 2: - dpad_right = true; - break; - case 3: - dpad_down = true; - dpad_right = true; - break; - case 4: - dpad_down = true; - break; - case 5: - dpad_down = true; - dpad_left = true; - break; - case 6: - dpad_left = true; - break; - case 7: - dpad_left = true; - dpad_up = true; - break; - case 8: - // nothing is pressed - break; - default: - break; - } - } - - static float parse_analog(uint8_t value, float center=127.0f, float range=127.0f) { - return ((float)value - center) / range; - } - - bool wants_to_quit() const { - return select && start; - } - -protected: - float left_stick_x; - float left_stick_y; - float right_stick_x; - float right_stick_y; - bool dpad_up; - bool dpad_down; - bool dpad_left; - bool dpad_right; - bool a; - bool b; - bool x; - bool y; - bool l1; - bool r1; - bool l2; - bool r2; - bool start; - bool select; - float left_trigger; - float right_trigger; -}; - -bool handle_input_report(const std::vector& report_data, InputDevice* joypad) { - if (report_data.size() != 10) { - fmt::print("Bad report size {}\n", report_data.size()); - return false; - } - Report report(report_data); - report.set_joypad_state(joypad); - return report.wants_to_quit(); -} - -bool string_to_input(InputDevice* joypad, const std::string& strdata) { - static JoypadButtons prev_button = JoypadButtons::NONE; - if (strdata.find("quit") != std::string::npos) { - fmt::print("QUITTING\n"); - return true; - } else if (strdata.find("start") != std::string::npos) { - fmt::print("start pressed\n"); - prev_button = JoypadButtons::Start; - } else if (strdata.find("select") != std::string::npos) { - fmt::print("select pressed\n"); - prev_button = JoypadButtons::Select; - } else if (strdata.find("clear") != std::string::npos) { - fmt::print("clearing button: {}\n", (int)prev_button); - joypad->externState[(int)prev_button] = false; - prev_button = JoypadButtons::NONE; - } else if (strdata.find("a") != std::string::npos) { - fmt::print("A pressed\n"); - prev_button = JoypadButtons::A; - } else if (strdata.find("b") != std::string::npos) { - fmt::print("B pressed\n"); - prev_button = JoypadButtons::B; - } else if (strdata.find("up") != std::string::npos) { - fmt::print("Up pressed\n"); - prev_button = JoypadButtons::Up; - } else if (strdata.find("down") != std::string::npos) { - fmt::print("Down pressed\n"); - prev_button = JoypadButtons::Down; - } else if (strdata.find("left") != std::string::npos) { - fmt::print("Left pressed\n"); - prev_button = JoypadButtons::Left; - } else if (strdata.find("right") != std::string::npos) { - fmt::print("Right pressed\n"); - prev_button = JoypadButtons::Right; - } - if (prev_button != JoypadButtons::NONE) { - joypad->externState[(int)prev_button] = true; - } - return false; -} diff --git a/main/cart.hpp b/main/cart.hpp index cea6b04e..2542b6c4 100644 --- a/main/cart.hpp +++ b/main/cart.hpp @@ -81,10 +81,10 @@ class Cart { std::filesystem::remove(paused_image_path, ec); } // copy the screenshot to the paused image - std::fstream screenshot(screenshot_path, std::ios::binary | std::ios::in); + std::fstream screenshot_file(screenshot_path, std::ios::binary | std::ios::in); std::fstream paused_image(paused_image_path, std::ios::binary | std::ios::out); - paused_image << screenshot.rdbuf(); - screenshot.close(); + paused_image << screenshot_file.rdbuf(); + screenshot_file.close(); paused_image.close(); } @@ -101,10 +101,10 @@ class Cart { } // copy the paused image to the screenshot std::fstream paused_image(paused_image_path, std::ios::binary | std::ios::in); - std::fstream screenshot(screenshot_path, std::ios::binary | std::ios::out); - screenshot << paused_image.rdbuf(); + std::fstream screenshot_file(screenshot_path, std::ios::binary | std::ios::out); + screenshot_file << paused_image.rdbuf(); paused_image.close(); - screenshot.close(); + screenshot_file.close(); } else { logger_.warn("paused image does not exist"); } @@ -229,7 +229,7 @@ class Cart { return ".sav"; } - virtual std::string get_screenshot_extension() const { + std::string get_screenshot_extension() const { return ".bin"; } @@ -278,7 +278,7 @@ class Cart { } } - std::string get_save_path(bool bypass_exist_check=false) { + std::string get_save_path(bool bypass_exist_check=false) const { namespace fs = std::filesystem; auto save_path = savedir_ + "/" + @@ -286,15 +286,12 @@ class Cart { fmt::format("_{}", menu_->get_selected_slot()) + get_save_extension(); if (bypass_exist_check || fs::exists(save_path)) { - logger_.info("found: {}", save_path); return save_path; - } else { - logger_.warn("Could not find {}", save_path); } return ""; } - std::string get_paused_image_path() { + std::string get_paused_image_path() const { namespace fs = std::filesystem; auto save_path = savedir_ + "/paused" + @@ -302,7 +299,7 @@ class Cart { return save_path; } - std::string get_screenshot_path(bool bypass_exist_check=false) { + std::string get_screenshot_path(bool bypass_exist_check=false) const { auto save_path = get_save_path(bypass_exist_check); if (!save_path.empty()) { return save_path + get_screenshot_extension(); diff --git a/main/gbc_cart.hpp b/main/gbc_cart.hpp index 2567e3de..7ac2afdc 100644 --- a/main/gbc_cart.hpp +++ b/main/gbc_cart.hpp @@ -8,7 +8,7 @@ class GbcCart : public Cart { public: - GbcCart(const Cart::Config& config) + explicit GbcCart(const Cart::Config& config) : Cart(config) { init(); } @@ -17,6 +17,7 @@ class GbcCart : public Cart { deinit(); } + // cppcheck-suppress uselessOverride virtual void reset() override { Cart::reset(); #if defined(ENABLE_GBC) @@ -24,6 +25,7 @@ class GbcCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void load() override { Cart::load(); #if defined(ENABLE_GBC) @@ -31,6 +33,7 @@ class GbcCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void save() override { Cart::save(); #if defined(ENABLE_GBC) @@ -38,6 +41,7 @@ class GbcCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void init() override { Cart::init(); #if defined(ENABLE_GBC) @@ -46,6 +50,7 @@ class GbcCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void deinit() override { #if defined(ENABLE_GBC) stop_gameboy_tasks(); @@ -53,6 +58,7 @@ class GbcCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual bool run() override { #if defined(ENABLE_GBC) run_gameboy_rom(); @@ -65,6 +71,7 @@ class GbcCart : public Cart { static constexpr size_t GAMEBOY_WIDTH = 160; static constexpr size_t GAMEBOY_HEIGHT = 144; + // cppcheck-suppress uselessOverride virtual void pre_menu() override { Cart::pre_menu(); #if defined(ENABLE_GBC) @@ -73,6 +80,7 @@ class GbcCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void post_menu() override { Cart::post_menu(); #if defined(ENABLE_GBC) @@ -81,6 +89,7 @@ class GbcCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void set_original_video_setting() override { #if defined(ENABLE_GBC) logger_.info("gbc::video: original"); @@ -92,6 +101,7 @@ class GbcCart : public Cart { return std::make_pair(GAMEBOY_WIDTH, GAMEBOY_HEIGHT); } + // cppcheck-suppress uselessOverride virtual std::vector get_video_buffer() const override { #if defined(ENABLE_GBC) return get_gameboy_video_buffer(); @@ -100,6 +110,7 @@ class GbcCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void set_fit_video_setting() override { #if defined(ENABLE_GBC) logger_.info("gbc::video: fit"); @@ -107,6 +118,7 @@ class GbcCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void set_fill_video_setting() override { #if defined(ENABLE_GBC) logger_.info("gbc::video: fill"); diff --git a/main/nes_cart.hpp b/main/nes_cart.hpp index 7b52f962..024fb9e9 100644 --- a/main/nes_cart.hpp +++ b/main/nes_cart.hpp @@ -8,7 +8,7 @@ class NesCart : public Cart { public: - NesCart(const Cart::Config& config) + explicit NesCart(const Cart::Config& config) : Cart(config) { init(); } @@ -17,6 +17,7 @@ class NesCart : public Cart { deinit(); } + // cppcheck-suppress uselessOverride virtual void reset() override { Cart::reset(); #if defined(ENABLE_NES) @@ -24,6 +25,7 @@ class NesCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void load() override { Cart::load(); #if defined(ENABLE_NES) @@ -31,6 +33,7 @@ class NesCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void save() override { Cart::save(); #if defined(ENABLE_NES) @@ -38,6 +41,7 @@ class NesCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void init() override { Cart::init(); #if defined(ENABLE_NES) @@ -46,6 +50,7 @@ class NesCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void deinit() override { #if defined(ENABLE_NES) stop_nes_tasks(); @@ -53,6 +58,7 @@ class NesCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual bool run() override { #if defined(ENABLE_NES) run_nes_rom(); @@ -64,6 +70,7 @@ class NesCart : public Cart { static constexpr size_t NES_WIDTH = 256; static constexpr size_t NES_HEIGHT = 240; + // cppcheck-suppress uselessOverride virtual void pre_menu() override { Cart::pre_menu(); #if defined(ENABLE_NES) @@ -72,6 +79,7 @@ class NesCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void post_menu() override { Cart::post_menu(); #if defined(ENABLE_NES) @@ -84,6 +92,7 @@ class NesCart : public Cart { return std::make_pair(NES_WIDTH, NES_HEIGHT); } + // cppcheck-suppress uselessOverride virtual std::vector get_video_buffer() const override { #if defined(ENABLE_NES) return get_nes_video_buffer(); @@ -92,18 +101,21 @@ class NesCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void set_original_video_setting() override { #if defined(ENABLE_NES) set_nes_video_original(); #endif } + // cppcheck-suppress uselessOverride virtual void set_fit_video_setting() override { #if defined(ENABLE_NES) set_nes_video_fit(); #endif } + // cppcheck-suppress uselessOverride virtual void set_fill_video_setting() override { #if defined(ENABLE_NES) set_nes_video_fill(); diff --git a/main/sms_cart.hpp b/main/sms_cart.hpp index e81b8db8..26dfbc7c 100644 --- a/main/sms_cart.hpp +++ b/main/sms_cart.hpp @@ -8,7 +8,7 @@ class SmsCart : public Cart { public: - SmsCart(const Cart::Config& config) + explicit SmsCart(const Cart::Config& config) : Cart(config) { init(); } @@ -18,6 +18,7 @@ class SmsCart : public Cart { deinit(); } + // cppcheck-suppress uselessOverride virtual void reset() override { Cart::reset(); #if defined(ENABLE_SMS) @@ -25,6 +26,7 @@ class SmsCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void load() override { Cart::load(); #if defined(ENABLE_SMS) @@ -32,6 +34,7 @@ class SmsCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void save() override { Cart::save(); #if defined(ENABLE_SMS) @@ -39,6 +42,7 @@ class SmsCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void init() override { Cart::init(); #if defined(ENABLE_SMS) @@ -59,6 +63,7 @@ class SmsCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void deinit() override { logger_.info("deinit()"); #if defined(ENABLE_SMS) @@ -67,6 +72,7 @@ class SmsCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual bool run() override { #if defined(ENABLE_SMS) run_sms_rom(); @@ -79,6 +85,7 @@ class SmsCart : public Cart { static constexpr size_t SMS_WIDTH = 256; static constexpr size_t SMS_HEIGHT = 192; + // cppcheck-suppress uselessOverride virtual void pre_menu() override { Cart::pre_menu(); #if defined(ENABLE_SMS) @@ -87,6 +94,7 @@ class SmsCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void post_menu() override { Cart::post_menu(); #if defined(ENABLE_SMS) @@ -95,6 +103,7 @@ class SmsCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void set_original_video_setting() override { #if defined(ENABLE_SMS) logger_.info("sms::video: original"); @@ -106,6 +115,7 @@ class SmsCart : public Cart { return std::make_pair(SMS_WIDTH, SMS_HEIGHT); } + // cppcheck-suppress uselessOverride virtual std::vector get_video_buffer() const override { #if defined(ENABLE_SMS) return get_sms_video_buffer(); @@ -114,6 +124,7 @@ class SmsCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void set_fit_video_setting() override { #if defined(ENABLE_SMS) logger_.info("sms::video: fit"); @@ -121,6 +132,7 @@ class SmsCart : public Cart { #endif } + // cppcheck-suppress uselessOverride virtual void set_fill_video_setting() override { #if defined(ENABLE_SMS) logger_.info("sms::video: fill");