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>
29 #include <Main/globals.hxx>
30 #include <GUI/gui.h> // mkDialog
31 #include <GUI/new_gui.hxx>
33 #include "ATCDialog.hxx"
36 #include "ATCdisplay.hxx"
37 #include "commlist.hxx"
38 #include "ATCutils.hxx"
39 #include <Airports/simple.hxx>
44 SG_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 current_atcdialog->PopupDialog();
55 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
56 current_atcdialog->FreqDialog();
60 ATCMenuEntry::ATCMenuEntry() {
68 ATCMenuEntry::~ATCMenuEntry() {
71 static void atcUppercase(string &s) {
72 for(unsigned int i=0; i<s.size(); ++i) {
77 // find child whose <name>...</name> entry matches 'name'
78 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
81 for (int i = 0; i < prop->nChildren(); i++)
82 if ((p = getNamedNode(prop->getChild(i), name)))
85 if (!strcmp(prop->getStringValue("name"), name))
92 FGATCDialog::FGATCDialog() {
93 _callbackPending = false;
98 _gui = (NewGUI *)globals->get_subsystem("gui");
101 FGATCDialog::~FGATCDialog() {
104 void FGATCDialog::Init() {
105 // Add ATC-dialog to the command list
106 globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
107 // Add ATC-freq-search to the command list
108 globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
110 // initialize properties polled in Update()
111 globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
112 globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
115 void FGATCDialog::Update(double dt) {
116 static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
117 string s = airport->getStringValue();
119 airport->setStringValue("");
123 static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
124 int n = trans_num->getIntValue();
126 trans_num->setIntValue(-1);
130 if(_callbackPending) {
131 if(_callbackTimer > _callbackWait) {
132 _callbackPtr->ReceiveUserCallback(_callbackCode);
133 _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
134 _callbackPending = false;
136 _callbackTimer += dt;
142 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
146 a.stationid = station;
147 a.transmission = transmission;
148 a.menuentry = menutext;
149 a.callback_code = code;
151 (available_dialog[type])[station.c_str()].push_back(a);
155 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
156 atcmentry_vec_type* p = &((available_dialog[type])[station]);
157 atcmentry_vec_iterator current = p->begin();
158 while(current != p->end()) {
159 if(current->transmission == trans) current = p->erase(current);
164 void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
165 atcmentry_vec_type* p = &((available_dialog[type])[station]);
166 atcmentry_vec_iterator current = p->begin();
167 while(current != p->end()) {
168 if(current->callback_code == code) current = p->erase(current);
173 // query the database whether the transmission is already registered;
174 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
175 atcmentry_vec_type* p = &((available_dialog[type])[station]);
176 atcmentry_vec_iterator current = p->begin();
177 for ( ; current != p->end() ; ++current ) {
178 if ( current->transmission == trans ) return true;
183 // query the database whether the transmission is already registered;
184 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
185 atcmentry_vec_type* p = &((available_dialog[type])[station]);
186 atcmentry_vec_iterator current = p->begin();
187 for ( ; current != p->end() ; ++current ) {
188 if ( current->callback_code == code ) return true;
193 // Display the ATC popup dialog box with options relevant to the users current situation.
194 void FGATCDialog::PopupDialog() {
195 const char *dialog_name = "atc-dialog";
196 SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
200 _gui->closeDialog(dialog_name);
202 SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
203 // remove all transmission buttons
204 button_group->removeChildren("button", false);
207 FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer(); // Hardwired to comm1 at the moment
210 label = "Not currently tuned to any ATC service";
211 mkDialog(label.c_str());
215 if(atcptr->GetType() == ATIS) {
216 label = "Tuned to ATIS - no communication possible";
217 mkDialog(label.c_str());
221 atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
222 atcmentry_vec_iterator current = atcmlist.begin();
223 atcmentry_vec_iterator last = atcmlist.end();
225 if(!atcmlist.size()) {
226 label = "No transmission available";
227 mkDialog(label.c_str());
231 const int bufsize = 32;
233 // loop over all entries in atcmentrylist
234 for (int n = 0; n < 10; ++n) {
235 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
236 fgSetBool(buf, false);
241 // add transmission button (modified copy of <button-template>)
242 SGPropertyNode *entry = button_group->getNode("button", n, true);
243 copyProperties(button_group->getNode("button-template", true), entry);
244 entry->removeChildren("hide", false);
245 entry->setStringValue("property", buf);
246 entry->setIntValue("keynum", '1' + n);
248 entry->setBoolValue("default", true);
250 snprintf(buf, bufsize, "%d", n + 1);
251 string legend = string(buf) + ". " + current->menuentry;
252 entry->setStringValue("legend", legend.c_str());
253 entry->setIntValue("binding/value", n);
257 _gui->showDialog(dialog_name);
261 void FGATCDialog::PopupCallback(int num) {
262 FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer(); // FIXME - Hardwired to comm1 at the moment
267 if (atcptr->GetType() == TOWER) {
268 //cout << "TOWER " << endl;
269 //cout << "ident is " << atcptr->get_ident() << endl;
270 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
271 unsigned int size = atcmlist.size();
272 if(size && num < size) {
273 //cout << "Doing callback...\n";
274 ATCMenuEntry a = atcmlist[num];
275 atcptr->SetFreqInUse();
276 string pilot = atcptr->GenText(a.transmission, a.callback_code);
277 fgSetString("/sim/messages/pilot", pilot.c_str());
278 // This is the user's speech getting displayed.
279 globals->get_ATC_display()->RegisterSingleMessage(pilot);
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 // map() key data type (removes duplicates and sorts by distance)
295 atcdata(const string i, const string n, const double d) {
296 id = i, name = n, distance = d;
298 bool operator<(const atcdata& a) const {
299 return id != a.id && distance < a.distance;
301 bool operator==(const atcdata& a) const {
302 return id == a.id && distance == a.distance;
309 void FGATCDialog::FreqDialog() {
310 const char *dialog_name = "atc-freq-search";
311 SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
315 _gui->closeDialog(dialog_name);
317 SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
318 // remove all dynamic airport/ATC buttons
319 button_group->removeChildren("button", false);
321 // Find the ATC stations within a reasonable range (about 40 miles?)
322 comm_list_type atc_stations;
323 comm_list_iterator atc_stat_itr;
325 double lon = fgGetDouble("/position/longitude-deg");
326 double lat = fgGetDouble("/position/latitude-deg");
327 double elev = fgGetDouble("/position/altitude-ft");
328 Point3D aircraft = sgGeodToCart(Point3D(lon * SGD_DEGREES_TO_RADIANS,
329 lat * SGD_DEGREES_TO_RADIANS, elev));
331 // search stations in range
332 int num_stat = current_commlist->FindByPos(lon, lat, elev, 40.0, &atc_stations);
334 map<atcdata, bool> uniq;
335 // fill map (sorts by distance and removes duplicates)
336 comm_list_iterator itr = atc_stations.begin();
337 for (; itr != atc_stations.end(); ++itr) {
338 Point3D station = Point3D(itr->x, itr->y, itr->z);
339 double distance = aircraft.distance3Dsquared(station);
340 uniq[atcdata(itr->ident, itr->name, distance)] = true;
342 // create button per map entry (modified copy of <button-template>)
343 map<atcdata, bool>::iterator uit = uniq.begin();
344 for (int n = 0; uit != uniq.end() && n < 6; ++uit, ++n) { // max 6 buttons
345 SGPropertyNode *entry = button_group->getNode("button", n, true);
346 copyProperties(button_group->getNode("button-template", true), entry);
347 entry->removeChildren("hide", false);
348 entry->setStringValue("legend", uit->first.id.c_str());
349 entry->setStringValue("binding[0]/value", uit->first.id.c_str());
353 // (un)hide message saying no things in range
354 SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
355 range_error->setBoolValue("hide", num_stat);
357 _gui->showDialog(dialog_name);
360 void FGATCDialog::FreqDisplay(string& ident) {
361 const char *dialog_name = "atc-freq-display";
362 SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
366 _gui->closeDialog(dialog_name);
368 SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
369 // remove all frequency entries
370 freq_group->removeChildren("group", false);
375 const FGAirport *a = fgFindAirportID(ident);
377 label = "Airport " + ident + " not found in database.";
378 mkDialog(label.c_str());
383 label = ident + " Frequencies";
384 dlg->setStringValue("text/label", label.c_str());
386 int n = 0; // Number of ATC frequencies at this airport
388 comm_list_type stations;
389 int found = current_commlist->FindByPos(a->getLongitude(), a->getLatitude(), a->getElevation(), 20.0, &stations);
392 comm_list_iterator itr = stations.begin();
393 for (n = 0; itr != stations.end(); ++itr) {
394 if(itr->ident != ident)
397 if(itr->type == INVALID)
400 // add frequency line (modified copy of <group-template>)
401 SGPropertyNode *entry = freq_group->getNode("group", n, true);
402 copyProperties(freq_group->getNode("group-template", true), entry);
403 entry->removeChildren("hide", false);
406 entry->setStringValue("text[0]/label", ostr.str().c_str());
409 snprintf(buf, 8, "%.2f", (itr->freq / 100.0)); // Convert from KHz to MHz
410 if(buf[5] == '3') buf[5] = '2';
411 if(buf[5] == '8') buf[5] = '7';
414 entry->setStringValue("text[1]/label", buf);
421 label = "No frequencies found for airport " + ident;
422 mkDialog(label.c_str());
426 _gui->showDialog(dialog_name);