Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <string>
- #include "scinterface.hpp"
- #include <SimConnect.h>
- #include <stdexcept>
- #include "ui_mainwindow.h"
- #include "mainwindow.h"
- #include <stdio.h>
- #include <iostream>
- #include <strsafe.h>
- SimConnectInterface *parent;
- HANDLE hSimConnect;
- void CALLBACK scDispatch(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext);
- struct Struct1
- {
- char title[256];
- double kohlsmann;
- double altitude;
- double latitude;
- double longitude;
- };
- enum EVENT_ID{
- EVENT_SIM_START,
- };
- enum DATA_DEFINE_ID {
- DEFINITION_1,
- };
- enum DATA_REQUEST_ID {
- REQUEST_1,
- };
- SimConnectInterface::SimConnectInterface(MainWindow *caller)
- {
- quit=0;
- SimConnectInterface::caller = caller;
- int i=1;
- while(!SUCCEEDED(SimConnect_Open(&hSimConnect, "SimConnect Thing", NULL, 0, NULL, 0))) {
- if(i==6) {
- delete hSimConnect;
- throw std::runtime_error("Could not connect to simulator!");
- }
- caller->appendToLog(QStringLiteral("SC connection attempt #%1 failed.").arg(i++));
- Sleep(500);
- }
- caller->appendToLog(QStringLiteral("Connected to SimConnect after %1 attempts.").arg(i));
- }
- void SimConnectInterface::pollSim() {
- HRESULT hr;
- if (SUCCEEDED(SimConnect_Open(&hSimConnect, "Request Data", NULL, 0, 0, 0)))
- {
- std::cout << "Succeeded" << std::endl;
- // Set up the data definition, but do not yet do anything with it
- hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "title", NULL, SIMCONNECT_DATATYPE_STRING256);
- hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Kohlsman setting hg", "inHg");
- hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Altitude", "feet");
- hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Latitude", "degrees");
- hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "Plane Longitude", "degrees");
- // Request an event when the simulation starts
- hr = SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_SIM_START, "SimStart");
- /*
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::EVENT_BRAKES, "brakes");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::EVENT_BRAKES);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::VELOCITY_WORLD_Z, "VELOCITY WORLD Z");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::VELOCITY_WORLD_Z);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::VELOCITY_WORLD_X, "VELOCITY WORLD X");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::VELOCITY_WORLD_X);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::VELOCITY_WORLD_Y, "VELOCITY WORLD Y");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::VELOCITY_WORLD_Y);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ACCELERATION_WORLD_X, "ACCELERATION WORLD X");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ACCELERATION_WORLD_X);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ACCELERATION_WORLD_Y, "ACCELERATION WORLD Y");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ACCELERATION_WORLD_Y);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ACCELERATION_WORLD_Z, "ACCELERATION WORLD Z");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ACCELERATION_WORLD_Z);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ROTATION_VELOCITY_BODY_X, "ROTATION VELOCITY BODY X");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ROTATION_VELOCITY_BODY_X);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ROTATION_VELOCITY_BODY_Y, "ROTATION VELOCITY BODY Y");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ROTATION_VELOCITY_BODY_Y);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::ROTATION_VELOCITY_BODY_Z, "ROTATION VELOCITY BODY Z");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::ROTATION_VELOCITY_BODY_Z);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_ALTITUDE, "PLANE ALTITUDE");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_ALTITUDE);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_LATITUDE, "PLANE LATITUDE");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_LATITUDE);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_LONGITUDE, "PLANE LONGITUDE");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_LONGITUDE);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_PITCH_DEGREES, "PLANE PITCH DEGREES");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_PITCH_DEGREES);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_BANK_DEGREES, "PLANE BANK DEGREES");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_BANK_DEGREES);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::PLANE_HEADING_DEGREES_TRUE, "PLANE HEADING DEGREES TRUE");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::PLANE_HEADING_DEGREES_TRUE);
- SimConnect_MapClientEventToSimEvent(hSimConnect, SC_EVENT_ID::SIM_ON_GROUND, "SIM ON GROUND");
- SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, SC_EVENT_ID::SIM_ON_GROUND);
- SimConnect_SetNotificationGroupPriority(hSimConnect, GROUP0, SIMCONNECT_GROUP_PRIORITY_HIGHEST);
- */
- ::parent = this;
- while (0 == quit) {
- SimConnect_CallDispatch(hSimConnect, scDispatch, this);
- std::cout << "SimConnect Call Dispatch" << std::endl;
- Sleep(50);
- }
- if(quit==1)
- hr = SimConnect_Close(hSimConnect);
- }
- }
- void CALLBACK scDispatch(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext) {
- HRESULT hr;
- std::cout << "scDispatch" << std::endl;
- SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
- switch(pData->dwID) {
- case SIMCONNECT_RECV_ID_EVENT:
- std::cout << evt->uEventID << " Hello" << std::endl;
- switch(evt->uEventID) {
- case EVENT_SIM_START:
- // Now the sim is running, request information on the user aircraft
- hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1, DEFINITION_1, 0, SIMCONNECT_SIMOBJECT_TYPE_USER);
- std::cout << "START" << std::endl;
- break;
- case SC_EVENT_ID::EVENT_BRAKES:
- parent->caller->appendToLog(QStringLiteral("Event brakes: %1").arg(evt->dwData));
- std::cout << "BREAKS" << std::endl;
- break;
- default:
- std::cout << "STUFF" << std::endl;
- parent->caller->appendToLog(QStringLiteral("%1: %2").arg(std::to_string(evt->uEventID).c_str()).arg(evt->dwData));
- break;
- }
- break;
- case SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE:
- {
- std::cout << "BTYPE" << std::endl;
- SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE *pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE*)pData;
- switch(pObjData->dwRequestID)
- {
- case REQUEST_1:
- {
- DWORD ObjectID = pObjData->dwObjectID;
- Struct1 *pS = (Struct1*)&pObjData->dwData;
- if (SUCCEEDED(StringCbLengthA(&pS->title[0], sizeof(pS->title), NULL))) // security check
- {
- printf("\nObjectID=%d title=\"%s\"\nLat=%f Lon=%f Alt=%f Kohlsman=%.2f", ObjectID, pS->title, pS->latitude, pS->longitude, pS->altitude, pS->kohlsmann );
- }
- break;
- }
- default:
- break;
- }
- break;
- }
- case SIMCONNECT_RECV_ID_QUIT:
- std::cout << "SC Stuffs" << std::endl;
- parent->caller->appendToLog(QStringLiteral("Disconnected from simulator"));
- parent->quit = 1;
- break;
- case SIMCONNECT_RECV_ID_EXCEPTION:
- {
- SIMCONNECT_RECV_EXCEPTION *e = (SIMCONNECT_RECV_EXCEPTION*)pData;
- std::cout << "Exception ID: " << e->dwException << std::endl;
- }
- case SIMCONNECT_RECV_ID_OPEN:
- {
- SIMCONNECT_RECV_OPEN *pOpen = (SIMCONNECT_RECV_OPEN*)pData;
- std::cout << "Open: AppName=" << pOpen->szApplicationName << " AppVersion=" << pOpen->dwApplicationVersionMajor << "." << pOpen->dwApplicationVersionMinor
- << "." << pOpen->dwApplicationBuildMajor << "." << pOpen->dwApplicationBuildMinor << " SimConnectVersion=" << pOpen->dwSimConnectVersionMajor << "." <<
- pOpen->dwSimConnectVersionMinor << "." << pOpen->dwSimConnectBuildMajor << "." << pOpen->dwSimConnectBuildMinor << std::endl;
- std::cout << "Done" << std::endl;
- break;
- }
- default:
- std::cout << pData->dwID << " Unknown Event" << std::endl;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement