1 // ATCDialog.cxx - Functions and classes to handle the pop-up ATC dialog
3 // Written by Alexander Kappes and David Luff, started February 2003.
5 // Copyright (C) 2003 Alexander Kappes and David Luff
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <simgear/compiler.h>
27 #include <simgear/structure/commands.hxx>
28 #include <simgear/props/props_io.hxx>
30 #include <Main/globals.hxx>
31 #include <GUI/gui.h> // mkDialog
32 #include <GUI/new_gui.hxx>
33 #include <Main/fg_props.hxx>
35 #include "ATCDialogOld.hxx"
38 #include "ATCutils.hxx"
39 #include <Airports/simple.hxx>
40 #include <ATC/CommStation.hxx>
44 using std::ostringstream;
48 FGATCDialog *current_atcdialog;
50 // For the command manager - maybe eventually this should go in the built in command list
51 static bool do_ATC_dialog(const SGPropertyNode* arg) {
52 cerr << "Running ATCDCL do_ATC_dialog" << endl;
53 current_atcdialog->PopupDialog();
57 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
58 current_atcdialog->FreqDialog();
62 ATCMenuEntry::ATCMenuEntry() {
70 ATCMenuEntry::~ATCMenuEntry() {
73 void atcUppercase(string &s) {
74 for(unsigned int i=0; i<s.size(); ++i) {
79 // find child whose <name>...</name> entry matches 'name'
80 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
83 for (int i = 0; i < prop->nChildren(); i++)
84 if ((p = getNamedNode(prop->getChild(i), name)))
87 if (!strcmp(prop->getStringValue("name"), name))
94 FGATCDialog::FGATCDialog() {
95 _callbackPending = false;
100 _gui = (NewGUI *)globals->get_subsystem("gui");
103 FGATCDialog::~FGATCDialog() {
106 void FGATCDialog::Init() {
107 // Add ATC-dialog to the command list
108 //globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
109 // Add ATC-freq-search to the command list
110 //globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
112 // initialize properties polled in Update()
113 //globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
114 //globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
117 void FGATCDialog::Update(double dt) {
118 //static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
119 //string s = airport->getStringValue();
121 // airport->setStringValue("");
125 //static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
126 //int n = trans_num->getIntValue();
128 // trans_num->setIntValue(-1);
132 //if(_callbackPending) {
133 // if(_callbackTimer > _callbackWait) {
134 // _callbackPtr->ReceiveUserCallback(_callbackCode);
135 // _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
136 // _callbackPending = false;
138 // _callbackTimer += dt;
144 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
148 a.stationid = station;
149 a.transmission = transmission;
150 a.menuentry = menutext;
151 a.callback_code = code;
153 (available_dialog[type])[station.c_str()].push_back(a);
157 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
158 atcmentry_vec_type* p = &((available_dialog[type])[station]);
159 atcmentry_vec_iterator current = p->begin();
160 while(current != p->end()) {
161 if(current->transmission == trans) current = p->erase(current);
166 void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
167 atcmentry_vec_type* p = &((available_dialog[type])[station]);
168 atcmentry_vec_iterator current = p->begin();
169 while(current != p->end()) {
170 if(current->callback_code == code) current = p->erase(current);
175 // query the database whether the transmission is already registered;
176 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
177 atcmentry_vec_type* p = &((available_dialog[type])[station]);
178 atcmentry_vec_iterator current = p->begin();
179 for ( ; current != p->end() ; ++current ) {
180 if ( current->transmission == trans ) return true;
185 // query the database whether the transmission is already registered;
186 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
187 atcmentry_vec_type* p = &((available_dialog[type])[station]);
188 atcmentry_vec_iterator current = p->begin();
189 for ( ; current != p->end() ; ++current ) {
190 if ( current->callback_code == code ) return true;
195 // Display the ATC popup dialog box with options relevant to the users current situation.
196 void FGATCDialog::PopupDialog() {
197 const char *dialog_name = "atc-dialog";
198 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
202 _gui->closeDialog(dialog_name);
204 SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
205 // remove all transmission buttons
206 button_group->removeChildren("button", false);
209 FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
212 SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
215 FGATC* atcptr = pAtcMgr->GetComm1ATCPointer(); // Hardwired to comm1 at the moment
218 label = "Not currently tuned to any ATC service";
219 mkDialog(label.c_str());
223 if(atcptr->GetType() == ATIS) {
224 label = "Tuned to ATIS - no communication possible";
225 mkDialog(label.c_str());
229 atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
230 atcmentry_vec_iterator current = atcmlist.begin();
231 atcmentry_vec_iterator last = atcmlist.end();
233 if(!atcmlist.size()) {
234 label = "No transmission available";
235 mkDialog(label.c_str());
239 const int bufsize = 32;
241 // loop over all entries in atcmentrylist
242 for (int n = 0; n < 10; ++n) {
243 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
244 fgSetBool(buf, false);
249 // add transmission button (modified copy of <button-template>)
250 SGPropertyNode *entry = button_group->getNode("button", n, true);
251 copyProperties(button_group->getNode("button-template", true), entry);
252 entry->removeChildren("enabled", true);
253 entry->setStringValue("property", buf);
254 entry->setIntValue("keynum", '1' + n);
256 entry->setBoolValue("default", true);
258 snprintf(buf, bufsize, "%d", n + 1);
259 string legend = string(buf) + ". " + current->menuentry;
260 entry->setStringValue("legend", legend.c_str());
261 entry->setIntValue("binding/value", n);
265 _gui->showDialog(dialog_name);
269 void FGATCDialog::PopupCallback(int num) {
270 FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
273 SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
276 FGATC* atcptr = pAtcMgr->GetComm1ATCPointer(); // FIXME - Hardwired to comm1 at the moment
281 if (atcptr->GetType() == TOWER) {
282 //cout << "TOWER " << endl;
283 //cout << "ident is " << atcptr->get_ident() << endl;
284 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
285 int size = atcmlist.size();
286 if(size && num < size) {
287 //cout << "Doing callback...\n";
288 ATCMenuEntry a = atcmlist[num];
289 atcptr->SetFreqInUse();
290 string pilot = atcptr->GenText(a.transmission, a.callback_code);
291 fgSetString("/sim/messages/pilot", pilot.c_str());
292 // This is the user's speech getting displayed.
293 _callbackPending = true;
294 _callbackTimer = 0.0;
296 _callbackPtr = atcptr;
297 _callbackCode = a.callback_code;
299 //cout << "No options available...\n";
301 //cout << "Donded" << endl;
305 class AirportsWithATC : public FGAirport::AirportFilter
308 virtual FGPositioned::Type maxType() const {
309 return FGPositioned::SEAPORT;
312 virtual bool passAirport(FGAirport* aApt) const
314 return (!aApt->commStations().empty());
318 void FGATCDialog::FreqDialog() {
319 const char *dialog_name = "atc-freq-search";
320 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
324 _gui->closeDialog(dialog_name);
326 SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
327 // remove all dynamic airport/ATC buttons
328 button_group->removeChildren("button", false);
331 SGGeod geod(SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"),
332 fgGetDouble("/position/latitude-deg"), fgGetDouble("/position/altitude-ft")));
334 AirportsWithATC filt;
335 FGPositioned::List results = FGPositioned::findWithinRange(geod, 50.0, &filt);
336 FGPositioned::sortByRange(results, geod);
337 for (unsigned int r=0; (r<results.size()) && (r < 6); ++r) {
339 SGPropertyNode *entry = button_group->getNode("button", r, true);
340 copyProperties(button_group->getNode("button-template", true), entry);
341 entry->removeChildren("enabled", true);
342 entry->setStringValue("legend", results[r]->ident());
343 entry->setStringValue("binding[0]/value", results[r]->ident());
346 // (un)hide message saying no things in range
347 SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
348 range_error->setBoolValue("enabled", !results.empty());
350 _gui->showDialog(dialog_name);
353 void FGATCDialog::FreqDisplay(string& ident) {
354 const char *dialog_name = "atc-freq-display";
355 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
359 _gui->closeDialog(dialog_name);
361 SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
362 // remove all frequency entries
363 freq_group->removeChildren("group", false);
368 const FGAirport *a = fgFindAirportID(ident);
370 label = "Airport " + ident + " not found in database.";
371 mkDialog(label.c_str());
376 label = ident + " Frequencies";
377 dlg->setStringValue("text/label", label.c_str());
379 const flightgear::CommStationList& comms(a->commStations());
381 label = "No frequencies found for airport " + ident;
382 mkDialog(label.c_str());
387 for (unsigned int c=0; c < comms.size(); ++c) {
388 flightgear::CommStation* comm = comms[c];
390 // add frequency line (modified copy of <group-template>)
391 SGPropertyNode *entry = freq_group->getNode("group", n, true);
392 copyProperties(freq_group->getNode("group-template", true), entry);
393 entry->removeChildren("enabled", true);
395 entry->setStringValue("text[0]/label", comm->ident());
398 snprintf(buf, 8, "%.2f", comm->freqMHz());
399 if(buf[5] == '3') buf[5] = '2';
400 if(buf[5] == '8') buf[5] = '7';
403 entry->setStringValue("text[1]/label", buf);
407 _gui->showDialog(dialog_name);