Skip to content

Instantly share code, notes, and snippets.

@i8degrees
Created July 21, 2025 06:11
Show Gist options
  • Save i8degrees/b86bb66b8dca42d3bdf522bf2ed2a23b to your computer and use it in GitHub Desktop.
Save i8degrees/b86bb66b8dca42d3bdf522bf2ed2a23b to your computer and use it in GitHub Desktop.

Revisions

  1. i8degrees created this gist Jul 21, 2025.
    82 changes: 82 additions & 0 deletions app.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    /*
    * Copyright (C) 2023 - 2024 Vadym Hrynchyshyn <[email protected]>
    */

    #include "app.h"
    #include "utils.h"
    #include "wusbip.h"

    #include <libusbip/src/file_ver.h>

    using namespace usbip;

    App::App()
    {
    Bind(wxEVT_END_SESSION, &App::on_end_session, this);
    }

    bool App::OnInit()
    {
    /* wxString app_instance = */
    auto app_instance = wxString::Format(wxT("my_app_instance_%s"),
    wxGetUserId().c_str());

    // 1. https://docs.wxwidgets.org/3.0/classwx_single_instance_checker.html#
    m_checker = new wxSingleInstanceChecker(app_instance);
    if (m_checker->IsAnotherRunning()) {
    wxLogError(wxT("Another instance is already running."));
    return false; // Don't start a new instance
    }

    if (wxApp::OnInit()) {
    set_names();
    } else {
    return false;
    }

    wxString err;

    if (auto read = init(err) ? vhci::open() : Handle()) {
    if (auto &frame = *new MainFrame(std::move(read)); frame.start_in_tray()) {
    frame.iconize_to_tray();
    } else {
    frame.Show();
    }
    return true;
    }

    if (err.empty()) {
    err = GetLastErrorMsg();
    }

    wxSafeShowMessage(_("Fatal error"), err);
    return false;
    }

    void App::set_names()
    {
    auto &v = win::get_file_version();


    SetAppName(wx_string(v.GetProductName()));
    SetVendorName(wx_string(v.GetCompanyName()));
    }

    /*
    * wxEVT_CLOSE_WINDOW will not be sent to MainFrame, but m_read_thread must be joined.
    * @see MainFrame::on_close
    */
    void App::on_end_session(_In_ wxCloseEvent&)
    {
    if (auto wnd = GetMainTopWindow()) {
    wnd->Close(true);
    }
    }

    bool App::OnExit()
    {
    delete m_checker;
    return false; // 0
    }

    wxIMPLEMENT_APP(App);
    27 changes: 27 additions & 0 deletions app.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    /*
    * Copyright (C) 2023 - 2024 Vadym Hrynchyshyn <[email protected]>
    */

    #pragma once

    #include <wx/app.h>
    #include <wx/singleinst.h>
    #include <wx/log.h>


    class App : public wxApp
    {
    public:
    App();
    bool OnInit() override;
    // 1. https://docs.wxwidgets.org/3.0/classwx_single_instance_checker.html#
    bool OnExit() override;

    private:
    void set_names();
    void on_end_session(_In_ wxCloseEvent &event);
    // 1. https://docs.wxwidgets.org/3.0/classwx_single_instance_checker.html#
    wxSingleInstanceChecker *m_checker;
    };

    wxDECLARE_APP(App);