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>
34 #include "ATCDialogOld.hxx"
37 #include "ATCutils.hxx"
38 #include <Airports/simple.hxx>
39 #include <ATC/CommStation.hxx>
43 using std::ostringstream;
47 FGATCDialog *current_atcdialog;
49 // For the command manager - maybe eventually this should go in the built in command list
50 static bool do_ATC_dialog(const SGPropertyNode* arg) {
51 cerr << "Running ATCDCL do_ATC_dialog" << endl;
52 current_atcdialog->PopupDialog();
56 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
57 current_atcdialog->FreqDialog();
61 ATCMenuEntry::ATCMenuEntry() {
69 ATCMenuEntry::~ATCMenuEntry() {
72 void atcUppercase(string &s) {
73 for(unsigned int i=0; i<s.size(); ++i) {
78 // find child whose <name>...</name> entry matches 'name'
79 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
82 for (int i = 0; i < prop->nChildren(); i++)
83 if ((p = getNamedNode(prop->getChild(i), name)))
86 if (!strcmp(prop->getStringValue("name"), name))
93 FGATCDialog::FGATCDialog() {
94 _callbackPending = false;
99 _gui = (NewGUI *)globals->get_subsystem("gui");
102 FGATCDialog::~FGATCDialog() {
105 void FGATCDialog::Init() {
106 // Add ATC-dialog to the command list
107 //globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
108 // Add ATC-freq-search to the command list
109 //globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
111 // initialize properties polled in Update()
112 //globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
113 //globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
116 void FGATCDialog::Update(double dt) {
117 //static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
118 //string s = airport->getStringValue();
120 // airport->setStringValue("");
124 //static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
125 //int n = trans_num->getIntValue();
127 // trans_num->setIntValue(-1);
131 //if(_callbackPending) {
132 // if(_callbackTimer > _callbackWait) {
133 // _callbackPtr->ReceiveUserCallback(_callbackCode);
134 // _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
135 // _callbackPending = false;
137 // _callbackTimer += dt;
143 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
147 a.stationid = station;
148 a.transmission = transmission;
149 a.menuentry = menutext;
150 a.callback_code = code;
152 (available_dialog[type])[station.c_str()].push_back(a);
156 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
157 atcmentry_vec_type* p = &((available_dialog[type])[station]);
158 atcmentry_vec_iterator current = p->begin();
159 while(current != p->end()) {
160 if(current->transmission == trans) current = p->erase(current);
165 void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
166 atcmentry_vec_type* p = &((available_dialog[type])[station]);
167 atcmentry_vec_iterator current = p->begin();
168 while(current != p->end()) {
169 if(current->callback_code == code) current = p->erase(current);
174 // query the database whether the transmission is already registered;
175 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
176 atcmentry_vec_type* p = &((available_dialog[type])[station]);
177 atcmentry_vec_iterator current = p->begin();
178 for ( ; current != p->end() ; ++current ) {
179 if ( current->transmission == trans ) return true;
184 // query the database whether the transmission is already registered;
185 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
186 atcmentry_vec_type* p = &((available_dialog[type])[station]);
187 atcmentry_vec_iterator current = p->begin();
188 for ( ; current != p->end() ; ++current ) {
189 if ( current->callback_code == code ) return true;
194 // Display the ATC popup dialog box with options relevant to the users current situation.
195 void FGATCDialog::PopupDialog() {
196 const char *dialog_name = "atc-dialog";
197 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
201 _gui->closeDialog(dialog_name);
203 SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
204 // remove all transmission buttons
205 button_group->removeChildren("button", false);
208 FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
211 SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
214 FGATC* atcptr = pAtcMgr->GetComm1ATCPointer(); // Hardwired to comm1 at the moment
217 label = "Not currently tuned to any ATC service";
218 mkDialog(label.c_str());
222 if(atcptr->GetType() == ATIS) {
223 label = "Tuned to ATIS - no communication possible";
224 mkDialog(label.c_str());
228 atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
229 atcmentry_vec_iterator current = atcmlist.begin();
230 atcmentry_vec_iterator last = atcmlist.end();
232 if(!atcmlist.size()) {
233 label = "No transmission available";
234 mkDialog(label.c_str());
238 const int bufsize = 32;
240 // loop over all entries in atcmentrylist
241 for (int n = 0; n < 10; ++n) {
242 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
243 fgSetBool(buf, false);
248 // add transmission button (modified copy of <button-template>)
249 SGPropertyNode *entry = button_group->getNode("button", n, true);
250 copyProperties(button_group->getNode("button-template", true), entry);
251 entry->removeChildren("enabled", true);
252 entry->setStringValue("property", buf);
253 entry->setIntValue("keynum", '1' + n);
255 entry->setBoolValue("default", true);
257 snprintf(buf, bufsize, "%d", n + 1);
258 string legend = string(buf) + ". " + current->menuentry;
259 entry->setStringValue("legend", legend.c_str());
260 entry->setIntValue("binding/value", n);
264 _gui->showDialog(dialog_name);
268 void FGATCDialog::PopupCallback(int num) {
269 FGATCMgr* pAtcMgr = globals->get_ATC_mgr();
272 SG_LOG(SG_ATC, SG_ALERT, "ERROR! No ATC manager! Oops...");
275 FGATC* atcptr = pAtcMgr->GetComm1ATCPointer(); // FIXME - Hardwired to comm1 at the moment
280 if (atcptr->GetType() == TOWER) {
281 //cout << "TOWER " << endl;
282 //cout << "ident is " << atcptr->get_ident() << endl;
283 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
284 int size = atcmlist.size();
285 if(size && num < size) {
286 //cout << "Doing callback...\n";
287 ATCMenuEntry a = atcmlist[num];
288 atcptr->SetFreqInUse();
289 string pilot = atcptr->GenText(a.transmission, a.callback_code);
290 fgSetString("/sim/messages/pilot", pilot.c_str());
291 // This is the user's speech getting displayed.
292 _callbackPending = true;
293 _callbackTimer = 0.0;
295 _callbackPtr = atcptr;
296 _callbackCode = a.callback_code;
298 //cout << "No options available...\n";
300 //cout << "Donded" << endl;
304 class AirportsWithATC : public FGAirport::AirportFilter
307 virtual FGPositioned::Type maxType() const {
308 return FGPositioned::SEAPORT;
311 virtual bool passAirport(FGAirport* aApt) const
313 return (!aApt->commStations().empty());
317 void FGATCDialog::FreqDialog() {
318 const char *dialog_name = "atc-freq-search";
319 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
323 _gui->closeDialog(dialog_name);
325 SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
326 // remove all dynamic airport/ATC buttons
327 button_group->removeChildren("button", false);
330 SGGeod geod(SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"),
331 fgGetDouble("/position/latitude-deg"), fgGetDouble("/position/altitude-ft")));
333 AirportsWithATC filt;
334 FGPositioned::List results = FGPositioned::findWithinRange(geod, 50.0, &filt);
335 FGPositioned::sortByRange(results, geod);
336 for (unsigned int r=0; (r<results.size()) && (r < 6); ++r) {
338 SGPropertyNode *entry = button_group->getNode("button", r, true);
339 copyProperties(button_group->getNode("button-template", true), entry);
340 entry->removeChildren("enabled", true);
341 entry->setStringValue("legend", results[r]->ident());
342 entry->setStringValue("binding[0]/value", results[r]->ident());
345 // (un)hide message saying no things in range
346 SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
347 range_error->setBoolValue("enabled", !results.empty());
349 _gui->showDialog(dialog_name);
352 void FGATCDialog::FreqDisplay(string& ident) {
353 const char *dialog_name = "atc-freq-display";
354 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
358 _gui->closeDialog(dialog_name);
360 SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
361 // remove all frequency entries
362 freq_group->removeChildren("group", false);
367 const FGAirport *a = fgFindAirportID(ident);
369 label = "Airport " + ident + " not found in database.";
370 mkDialog(label.c_str());
375 label = ident + " Frequencies";
376 dlg->setStringValue("text/label", label.c_str());
378 const flightgear::CommStationList& comms(a->commStations());
380 label = "No frequencies found for airport " + ident;
381 mkDialog(label.c_str());
386 for (unsigned int c=0; c < comms.size(); ++c) {
387 flightgear::CommStation* comm = comms[c];
389 // add frequency line (modified copy of <group-template>)
390 SGPropertyNode *entry = freq_group->getNode("group", n, true);
391 copyProperties(freq_group->getNode("group-template", true), entry);
392 entry->removeChildren("enabled", true);
394 entry->setStringValue("text[0]/label", comm->ident());
397 snprintf(buf, 8, "%.2f", comm->freqMHz());
398 if(buf[5] == '3') buf[5] = '2';
399 if(buf[5] == '8') buf[5] = '7';
402 entry->setStringValue("text[1]/label", buf);
406 _gui->showDialog(dialog_name);