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 FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer(); // Hardwired to comm1 at the moment
211 label = "Not currently tuned to any ATC service";
212 mkDialog(label.c_str());
216 if(atcptr->GetType() == ATIS) {
217 label = "Tuned to ATIS - no communication possible";
218 mkDialog(label.c_str());
222 atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
223 atcmentry_vec_iterator current = atcmlist.begin();
224 atcmentry_vec_iterator last = atcmlist.end();
226 if(!atcmlist.size()) {
227 label = "No transmission available";
228 mkDialog(label.c_str());
232 const int bufsize = 32;
234 // loop over all entries in atcmentrylist
235 for (int n = 0; n < 10; ++n) {
236 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
237 fgSetBool(buf, false);
242 // add transmission button (modified copy of <button-template>)
243 SGPropertyNode *entry = button_group->getNode("button", n, true);
244 copyProperties(button_group->getNode("button-template", true), entry);
245 entry->removeChildren("enabled", true);
246 entry->setStringValue("property", buf);
247 entry->setIntValue("keynum", '1' + n);
249 entry->setBoolValue("default", true);
251 snprintf(buf, bufsize, "%d", n + 1);
252 string legend = string(buf) + ". " + current->menuentry;
253 entry->setStringValue("legend", legend.c_str());
254 entry->setIntValue("binding/value", n);
258 _gui->showDialog(dialog_name);
262 void FGATCDialog::PopupCallback(int num) {
263 FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer(); // FIXME - Hardwired to comm1 at the moment
268 if (atcptr->GetType() == TOWER) {
269 //cout << "TOWER " << endl;
270 //cout << "ident is " << atcptr->get_ident() << endl;
271 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
272 int size = atcmlist.size();
273 if(size && num < size) {
274 //cout << "Doing callback...\n";
275 ATCMenuEntry a = atcmlist[num];
276 atcptr->SetFreqInUse();
277 string pilot = atcptr->GenText(a.transmission, a.callback_code);
278 fgSetString("/sim/messages/pilot", pilot.c_str());
279 // This is the user's speech getting displayed.
280 _callbackPending = true;
281 _callbackTimer = 0.0;
283 _callbackPtr = atcptr;
284 _callbackCode = a.callback_code;
286 //cout << "No options available...\n";
288 //cout << "Donded" << endl;
292 class AirportsWithATC : public FGAirport::AirportFilter
295 virtual FGPositioned::Type maxType() const {
296 return FGPositioned::SEAPORT;
299 virtual bool passAirport(FGAirport* aApt) const
301 return (!aApt->commStations().empty());
305 void FGATCDialog::FreqDialog() {
306 const char *dialog_name = "atc-freq-search";
307 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
311 _gui->closeDialog(dialog_name);
313 SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
314 // remove all dynamic airport/ATC buttons
315 button_group->removeChildren("button", false);
318 SGGeod geod(SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"),
319 fgGetDouble("/position/latitude-deg"), fgGetDouble("/position/altitude-ft")));
321 AirportsWithATC filt;
322 FGPositioned::List results = FGPositioned::findWithinRange(geod, 50.0, &filt);
323 FGPositioned::sortByRange(results, geod);
324 for (unsigned int r=0; (r<results.size()) && (r < 6); ++r) {
326 SGPropertyNode *entry = button_group->getNode("button", r, true);
327 copyProperties(button_group->getNode("button-template", true), entry);
328 entry->removeChildren("enabled", true);
329 entry->setStringValue("legend", results[r]->ident());
330 entry->setStringValue("binding[0]/value", results[r]->ident());
333 // (un)hide message saying no things in range
334 SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
335 range_error->setBoolValue("enabled", !results.empty());
337 _gui->showDialog(dialog_name);
340 void FGATCDialog::FreqDisplay(string& ident) {
341 const char *dialog_name = "atc-freq-display";
342 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
346 _gui->closeDialog(dialog_name);
348 SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
349 // remove all frequency entries
350 freq_group->removeChildren("group", false);
355 const FGAirport *a = fgFindAirportID(ident);
357 label = "Airport " + ident + " not found in database.";
358 mkDialog(label.c_str());
363 label = ident + " Frequencies";
364 dlg->setStringValue("text/label", label.c_str());
366 const flightgear::CommStationList& comms(a->commStations());
368 label = "No frequencies found for airport " + ident;
369 mkDialog(label.c_str());
374 for (unsigned int c=0; c < comms.size(); ++c) {
375 flightgear::CommStation* comm = comms[c];
377 // add frequency line (modified copy of <group-template>)
378 SGPropertyNode *entry = freq_group->getNode("group", n, true);
379 copyProperties(freq_group->getNode("group-template", true), entry);
380 entry->removeChildren("enabled", true);
382 entry->setStringValue("text[0]/label", comm->ident());
385 snprintf(buf, 8, "%.2f", comm->freqMHz());
386 if(buf[5] == '3') buf[5] = '2';
387 if(buf[5] == '8') buf[5] = '7';
390 entry->setStringValue("text[1]/label", buf);
394 _gui->showDialog(dialog_name);