Skip to content

Commit

Permalink
fixed a minor issue
Browse files Browse the repository at this point in the history
  • Loading branch information
opiopan committed Nov 26, 2024
1 parent e77c0e0 commit 9605f8f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/core/dcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ DCSWorld::DCSWorld(SimHostManager &manager, int id): SimHostManager::Simulator(m
client.reopen();
tx_buf->reset(false);
lock.unlock();
reportConnectivity(false, MAPPER_SIM_NONE, nullptr, nullptr, 0);
reportConnectivity(false, MAPPER_SIM_NONE, nullptr, nullptr);
lock.lock();
};

Expand Down Expand Up @@ -480,7 +480,7 @@ DCSWorld::DCSWorld(SimHostManager &manager, int id): SimHostManager::Simulator(m
client.event_select(FD_READ | FD_CLOSE);
mapper_EngineInstance()->putLog(MCONSOLE_DEBUG, "dcs: connection with DCS World exporter has been established");
lock.unlock();
reportConnectivity(true, MAPPER_SIM_DCS, "dcs", nullptr, 0);
reportConnectivity(true, MAPPER_SIM_DCS, "dcs", nullptr);
lock.lock();
}else{
status = STATUS::retrying;
Expand Down Expand Up @@ -554,7 +554,7 @@ void DCSWorld::A_command(std::unique_lock<std::mutex> &lock, const DCSPacket &pa
aircraft_name.append(packet, packet.get_data_length());
mapper_EngineInstance()->putLog(MCONSOLE_DEBUG, std::format("dcs: Aircraft name has been received: {}", aircraft_name));
lock.unlock();
reportConnectivity(true, MAPPER_SIM_DCS, "dcs", aircraft_name.c_str(), 0);
reportConnectivity(true, MAPPER_SIM_DCS, "dcs", aircraft_name.c_str());
lock.lock();
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/dcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class DCSWorld : public SimHostManager::Simulator {
sync_chunks(lock);
}
}
HWND getRepresentativeWindow() override {
return nullptr;
}

protected:
size_t process_received_data(std::unique_lock<std::mutex>& lock, char* buf, size_t len, DCSPacket& packet);
Expand Down
20 changes: 10 additions & 10 deletions src/core/fs2020.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ FS2020::FS2020(SimHostManager& manager, int id): SimHostManager::Simulator(manag
mfwasm_stop();
lock.lock();
simconnect = nullptr;
representativeWindow = 0;
lock.unlock();
this->reportConnectivity(false, MAPPER_SIM_NONE, nullptr, nullptr, 0);
this->reportConnectivity(false, MAPPER_SIM_NONE, nullptr, nullptr);
::WaitForSingleObject(event_interrupt, 5 * 1000);
lock.lock();
::ResetEvent(event_interrupt);
Expand Down Expand Up @@ -245,7 +246,7 @@ void FS2020::processSimConnectReceivedData(SIMCONNECT_RECV* pData, DWORD cbData)
mapper_EngineInstance()->putLog(MCONSOLE_DEBUG, "msfs: Detected that the simulator is started");
status = Status::start;
lock.unlock();
this->reportConnectivity(true, simid, simname, nullptr, 0);
this->reportConnectivity(true, simid, simname, nullptr);
lock.lock();
}
if (is_waiting_enum_input_event){
Expand All @@ -264,10 +265,10 @@ void FS2020::processSimConnectReceivedData(SIMCONNECT_RECV* pData, DWORD cbData)
aircraftName = std::move(new_name);
status = Status::start;
watch_dog = std::chrono::steady_clock::now();
auto hwnd = updateRepresentativeWindow();
updateRepresentativeWindow();
lock.unlock();
mfwasm_start(*this, simconnect);
this->reportConnectivity(true, simid, simname, aircraftName.c_str(), hwnd);
this->reportConnectivity(true, simid, simname, aircraftName.c_str());
lock.lock();
for (auto i = 0; i < simvar_groups.size(); i++){
subscribeSimVarGroup(i);
Expand Down Expand Up @@ -342,26 +343,25 @@ void FS2020::updateMfwasm(){
SetEvent(event_interrupt);
}

HWND FS2020::updateRepresentativeWindow(){
HWND representativeWindow{};
void FS2020::updateRepresentativeWindow(){
representativeWindow = 0;
::EnumWindows([](HWND hwnd, LPARAM lparam)->BOOL{
static std::string class_name{"AceApp"};
static const char title[] = "Microsoft Flight Simulator";
char buf[256];
auto window = reinterpret_cast<HWND*>(lparam);
auto self = reinterpret_cast<FS2020*>(lparam);
if (::GetClassNameA(hwnd, buf, sizeof(buf)) > 0){
if (class_name == buf){
if (::GetWindowTextA(hwnd, buf, sizeof(buf)) > 0){
if (strncmp(title, buf, sizeof(title) - 1) == 0){
*window = hwnd;
self->representativeWindow = hwnd;
return false;
}
}
}
}
return true;
}, reinterpret_cast<LPARAM>(&representativeWindow));
return representativeWindow;
}, reinterpret_cast<LPARAM>(this));
}

//============================================================================================
Expand Down
4 changes: 3 additions & 1 deletion src/core/fs2020.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class FS2020 : public SimHostManager::Simulator{
std::string aircraftName;
std::thread scheduler;
std::chrono::steady_clock::time_point watch_dog;
HWND representativeWindow {0};

SimConnectHandle simconnect;
WinHandle event_simconnect;
Expand All @@ -78,6 +79,7 @@ class FS2020 : public SimHostManager::Simulator{
virtual ~FS2020();
void initLuaEnv(sol::state& lua) override;
void changeActivity(bool isActive) override;
HWND getRepresentativeWindow() override{return representativeWindow;};

void updateMfwasm();
template <typename FUNC>
Expand All @@ -89,7 +91,7 @@ class FS2020 : public SimHostManager::Simulator{
protected:
void processSimConnectReceivedData(SIMCONNECT_RECV* pData, DWORD cbData);

HWND updateRepresentativeWindow();
void updateRepresentativeWindow();

SIMCONNECT_CLIENT_EVENT_ID getSimEventId(const std::string& event_name);
void sendSimEventId(SIMCONNECT_CLIENT_EVENT_ID eventid, DWORD param1=0, DWORD param2=0, DWORD param3=0, DWORD param4=0, DWORD param5=0);
Expand Down
12 changes: 6 additions & 6 deletions src/core/simhost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ static const char *simtype_dict[] = {"msfs", "dcs"};
// SimHostManager::Simulator
// base class of object to represent each flight simulator connection
//============================================================================================
void SimHostManager::Simulator::reportConnectivity(bool connectivity, MAPPER_SIM_CONNECTION simkind, const char* simname, const char* aircraftname, HWND representative_window){
manager.changeConnectivity(id, connectivity, simkind, simname, aircraftname, representative_window);
void SimHostManager::Simulator::reportConnectivity(bool connectivity, MAPPER_SIM_CONNECTION simkind, const char* simname, const char* aircraftname){
manager.changeConnectivity(id, connectivity, simkind, simname, aircraftname);
}


Expand Down Expand Up @@ -67,7 +67,8 @@ SimHostManager::SimHostManager(MapperEngine& engine, uint64_t event_changeAircra
}
}
}

mapper_EngineInstance()->getViewportManager()->get_mouse_emulator().set_window_for_restore(activeSim < 0 ? 0 : simulators.at(activeSim)->getRepresentativeWindow());

lock.unlock();
if (activeSim != oldActiveSim){
if (activeSim >= 0){
Expand Down Expand Up @@ -133,13 +134,12 @@ std::string SimHostManager::getAircraftName(){
}


void SimHostManager::changeConnectivity(int simid, bool isActive, MAPPER_SIM_CONNECTION simkind, const char* simname, const char* aircraftName, HWND representative_window){
mapper_EngineInstance()->getViewportManager()->get_mouse_emulator().set_window_for_restore(representative_window);
void SimHostManager::changeConnectivity(int simid, bool isActive, MAPPER_SIM_CONNECTION simkind, const char* simname, const char* aircraftName){
aircraftName = aircraftName ? aircraftName : "";
std::lock_guard lock(mutex);
queue.push(std::move(Message(
simid,
Connectivity(isActive, simkind, simname ? simname : "", std::move(std::string(aircraftName ? aircraftName : "")), representative_window)
Connectivity(isActive, simkind, simname ? simname : "", std::move(std::string(aircraftName ? aircraftName : "")))
)));
cv.notify_all();
}
11 changes: 5 additions & 6 deletions src/core/simhost.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class SimHostManager {
virtual ~Simulator() = default;
virtual void initLuaEnv(sol::state& lua) = 0;
virtual void changeActivity(bool isActive) = 0;
virtual HWND getRepresentativeWindow() = 0;
protected:
void reportConnectivity(bool connectivity, MAPPER_SIM_CONNECTION simkind, const char* simname, const char* aircraftname, HWND representative_window);
void reportConnectivity(bool connectivity, MAPPER_SIM_CONNECTION simkind, const char* simname, const char* aircraftname);
SimHostManager& getManager(){return manager;};
};

Expand All @@ -46,11 +47,10 @@ class SimHostManager {
MAPPER_SIM_CONNECTION simKind;
std::string simName;
std::string aircraftName;
HWND representativeWindow;

Connectivity() : isConnected(false){};
Connectivity(bool isConnected, MAPPER_SIM_CONNECTION simKind, std::string&& simName, std::string&& aircraftName, HWND window):
isConnected(isConnected), simKind(simKind), simName(std::move(simName)), aircraftName(std::move(aircraftName)), representativeWindow(window){};
Connectivity(bool isConnected, MAPPER_SIM_CONNECTION simKind, std::string&& simName, std::string&& aircraftName):
isConnected(isConnected), simKind(simKind), simName(std::move(simName)), aircraftName(std::move(aircraftName)){};
Connectivity(const Connectivity&) = default;
Connectivity(Connectivity&&) = default;
~Connectivity() = default;
Expand All @@ -59,7 +59,6 @@ class SimHostManager {
simKind = src.simKind;
simName = std::move(src.simName);
aircraftName = std::move(src.aircraftName);
representativeWindow = src.representativeWindow;
return *this;
};
};
Expand Down Expand Up @@ -91,5 +90,5 @@ class SimHostManager {

// fuctions for each Simulator instance
MapperEngine& getEngine(){return engine;};
void changeConnectivity(int simid, bool isActive, MAPPER_SIM_CONNECTION simkind, const char* simname, const char* aircraftName, HWND representative_window);
void changeConnectivity(int simid, bool isActive, MAPPER_SIM_CONNECTION simkind, const char* simname, const char* aircraftName);
};

0 comments on commit 9605f8f

Please sign in to comment.