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 "ATCDialog.hxx"
37 #include "ATCutils.hxx"
38 #include <Airports/simple.hxx>
39 #include <ATC/CommStation.hxx>
43 using std::ostringstream;
45 FGATCDialog *current_atcdialog;
47 // For the command manager - maybe eventually this should go in the built in command list
48 static bool do_ATC_dialog(const SGPropertyNode* arg) {
49 cerr << "Running ATCDCL do_ATC_dialog" << endl;
50 current_atcdialog->PopupDialog();
54 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
55 current_atcdialog->FreqDialog();
59 ATCMenuEntry::ATCMenuEntry() {
67 ATCMenuEntry::~ATCMenuEntry() {
70 void atcUppercase(string &s) {
71 for(unsigned int i=0; i<s.size(); ++i) {
76 // find child whose <name>...</name> entry matches 'name'
77 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
80 for (int i = 0; i < prop->nChildren(); i++)
81 if ((p = getNamedNode(prop->getChild(i), name)))
84 if (!strcmp(prop->getStringValue("name"), name))
91 FGATCDialog::FGATCDialog() {
92 _callbackPending = false;
97 _gui = (NewGUI *)globals->get_subsystem("gui");
100 FGATCDialog::~FGATCDialog() {
103 void FGATCDialog::Init() {
104 // Add ATC-dialog to the command list
105 //globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
106 // Add ATC-freq-search to the command list
107 //globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
109 // initialize properties polled in Update()
110 //globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
111 //globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
114 void FGATCDialog::Update(double dt) {
115 //static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
116 //string s = airport->getStringValue();
118 // airport->setStringValue("");
122 //static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
123 //int n = trans_num->getIntValue();
125 // trans_num->setIntValue(-1);
129 //if(_callbackPending) {
130 // if(_callbackTimer > _callbackWait) {
131 // _callbackPtr->ReceiveUserCallback(_callbackCode);
132 // _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
133 // _callbackPending = false;
135 // _callbackTimer += dt;
141 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
145 a.stationid = station;
146 a.transmission = transmission;
147 a.menuentry = menutext;
148 a.callback_code = code;
150 (available_dialog[type])[station.c_str()].push_back(a);
154 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
155 atcmentry_vec_type* p = &((available_dialog[type])[station]);
156 atcmentry_vec_iterator current = p->begin();
157 while(current != p->end()) {
158 if(current->transmission == trans) current = p->erase(current);
163 void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
164 atcmentry_vec_type* p = &((available_dialog[type])[station]);
165 atcmentry_vec_iterator current = p->begin();
166 while(current != p->end()) {
167 if(current->callback_code == code) current = p->erase(current);
172 // query the database whether the transmission is already registered;
173 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
174 atcmentry_vec_type* p = &((available_dialog[type])[station]);
175 atcmentry_vec_iterator current = p->begin();
176 for ( ; current != p->end() ; ++current ) {
177 if ( current->transmission == trans ) return true;
182 // query the database whether the transmission is already registered;
183 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
184 atcmentry_vec_type* p = &((available_dialog[type])[station]);
185 atcmentry_vec_iterator current = p->begin();
186 for ( ; current != p->end() ; ++current ) {
187 if ( current->callback_code == code ) return true;
192 // Display the ATC popup dialog box with options relevant to the users current situation.
193 void FGATCDialog::PopupDialog() {
194 const char *dialog_name = "atc-dialog";
195 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
199 _gui->closeDialog(dialog_name);
201 SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
202 // remove all transmission buttons
203 button_group->removeChildren("button", false);
206 FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer(); // Hardwired to comm1 at the moment
209 label = "Not currently tuned to any ATC service";
210 mkDialog(label.c_str());
214 if(atcptr->GetType() == ATIS) {
215 label = "Tuned to ATIS - no communication possible";
216 mkDialog(label.c_str());
220 atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
221 atcmentry_vec_iterator current = atcmlist.begin();
222 atcmentry_vec_iterator last = atcmlist.end();
224 if(!atcmlist.size()) {
225 label = "No transmission available";
226 mkDialog(label.c_str());
230 const int bufsize = 32;
232 // loop over all entries in atcmentrylist
233 for (int n = 0; n < 10; ++n) {
234 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
235 fgSetBool(buf, false);
240 // add transmission button (modified copy of <button-template>)
241 SGPropertyNode *entry = button_group->getNode("button", n, true);
242 copyProperties(button_group->getNode("button-template", true), entry);
243 entry->removeChildren("enabled", true);
244 entry->setStringValue("property", buf);
245 entry->setIntValue("keynum", '1' + n);
247 entry->setBoolValue("default", true);
249 snprintf(buf, bufsize, "%d", n + 1);
250 string legend = string(buf) + ". " + current->menuentry;
251 entry->setStringValue("legend", legend.c_str());
252 entry->setIntValue("binding/value", n);
256 _gui->showDialog(dialog_name);
260 void FGATCDialog::PopupCallback(int num) {
261 FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer(); // FIXME - Hardwired to comm1 at the moment
266 if (atcptr->GetType() == TOWER) {
267 //cout << "TOWER " << endl;
268 //cout << "ident is " << atcptr->get_ident() << endl;
269 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
270 int size = atcmlist.size();
271 if(size && num < size) {
272 //cout << "Doing callback...\n";
273 ATCMenuEntry a = atcmlist[num];
274 atcptr->SetFreqInUse();
275 string pilot = atcptr->GenText(a.transmission, a.callback_code);
276 fgSetString("/sim/messages/pilot", pilot.c_str());
277 // This is the user's speech getting displayed.
278 _callbackPending = true;
279 _callbackTimer = 0.0;
281 _callbackPtr = atcptr;
282 _callbackCode = a.callback_code;
284 //cout << "No options available...\n";
286 //cout << "Donded" << endl;
290 class AirportsWithATC : public FGAirport::AirportFilter
293 virtual FGPositioned::Type maxType() const {
294 return FGPositioned::SEAPORT;
297 virtual bool passAirport(FGAirport* aApt) const
299 return (!aApt->commStations().empty());
303 void FGATCDialog::FreqDialog() {
304 const char *dialog_name = "atc-freq-search";
305 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
309 _gui->closeDialog(dialog_name);
311 SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
312 // remove all dynamic airport/ATC buttons
313 button_group->removeChildren("button", false);
316 SGGeod geod(SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"),
317 fgGetDouble("/position/latitude-deg"), fgGetDouble("/position/altitude-ft")));
319 AirportsWithATC filt;
320 FGPositioned::List results = FGPositioned::findWithinRange(geod, 50.0, &filt);
321 FGPositioned::sortByRange(results, geod);
322 for (unsigned int r=0; (r<results.size()) && (r < 6); ++r) {
324 SGPropertyNode *entry = button_group->getNode("button", r, true);
325 copyProperties(button_group->getNode("button-template", true), entry);
326 entry->removeChildren("enabled", true);
327 entry->setStringValue("legend", results[r]->ident());
328 entry->setStringValue("binding[0]/value", results[r]->ident());
331 // (un)hide message saying no things in range
332 SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
333 range_error->setBoolValue("enabled", !results.empty());
335 _gui->showDialog(dialog_name);
338 void FGATCDialog::FreqDisplay(string& ident) {
339 const char *dialog_name = "atc-freq-display";
340 SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
344 _gui->closeDialog(dialog_name);
346 SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
347 // remove all frequency entries
348 freq_group->removeChildren("group", false);
353 const FGAirport *a = fgFindAirportID(ident);
355 label = "Airport " + ident + " not found in database.";
356 mkDialog(label.c_str());
361 label = ident + " Frequencies";
362 dlg->setStringValue("text/label", label.c_str());
364 const flightgear::CommStationList& comms(a->commStations());
366 label = "No frequencies found for airport " + ident;
367 mkDialog(label.c_str());
372 for (unsigned int c=0; c < comms.size(); ++c) {
373 flightgear::CommStation* comm = comms[c];
375 // add frequency line (modified copy of <group-template>)
376 SGPropertyNode *entry = freq_group->getNode("group", n, true);
377 copyProperties(freq_group->getNode("group-template", true), entry);
378 entry->removeChildren("enabled", true);
380 entry->setStringValue("text[0]/label", comm->ident());
383 snprintf(buf, 8, "%.2f", comm->freqMHz());
384 if(buf[5] == '3') buf[5] = '2';
385 if(buf[5] == '8') buf[5] = '7';
388 entry->setStringValue("text[1]/label", buf);
392 _gui->showDialog(dialog_name);