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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <simgear/compiler.h>
23 #include <simgear/structure/commands.hxx>
25 #include <Main/globals.hxx>
26 #include <GUI/gui.h> // mkDialog
27 #include <GUI/new_gui.hxx>
29 #include "ATCDialog.hxx"
32 #include "ATCdisplay.hxx"
33 #include "commlist.hxx"
34 #include "ATCutils.hxx"
35 #include <Airports/simple.hxx>
40 SG_USING_STD(ostringstream);
43 FGATCDialog *current_atcdialog;
45 // For the command manager - maybe eventually this should go in the built in command list
46 static bool do_ATC_dialog(const SGPropertyNode* arg) {
47 current_atcdialog->PopupDialog();
51 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
52 current_atcdialog->FreqDialog();
56 ATCMenuEntry::ATCMenuEntry() {
64 ATCMenuEntry::~ATCMenuEntry() {
67 static void atcUppercase(string &s) {
68 for(unsigned int i=0; i<s.size(); ++i) {
73 // find child whose <name>...</name> entry matches 'name'
74 static SGPropertyNode_ptr namedNode(const vector<SGPropertyNode_ptr> &children, const char *name) {
75 SGPropertyNode_ptr ret;
76 for (int i = 0; i < children.size(); i++)
77 if (!strcmp(children[i]->getStringValue("name"), name)) {
85 FGATCDialog::FGATCDialog() {
86 _callbackPending = false;
91 _gui = (NewGUI *)globals->get_subsystem("gui");
94 FGATCDialog::~FGATCDialog() {
97 void FGATCDialog::Init() {
98 // Add ATC-dialog to the command list
99 globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
100 // Add ATC-freq-search to the command list
101 globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
103 // initialize properties polled in Update()
104 globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
105 globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
108 void FGATCDialog::Update(double dt) {
109 static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
110 string s = airport->getStringValue();
112 airport->setStringValue("");
116 static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
117 int n = trans_num->getIntValue();
119 trans_num->setIntValue(-1);
123 if(_callbackPending) {
124 if(_callbackTimer > _callbackWait) {
125 _callbackPtr->ReceiveUserCallback(_callbackCode);
126 _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
127 _callbackPending = false;
129 _callbackTimer += dt;
135 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
139 a.stationid = station;
140 a.transmission = transmission;
141 a.menuentry = menutext;
142 a.callback_code = code;
144 (available_dialog[type])[station.c_str()].push_back(a);
148 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
149 atcmentry_vec_type* p = &((available_dialog[type])[station]);
150 atcmentry_vec_iterator current = p->begin();
151 while(current != p->end()) {
152 if(current->transmission == trans) current = p->erase(current);
157 void FGATCDialog::remove_entry( const string &station, int code, 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->callback_code == code) current = p->erase(current);
166 // query the database whether the transmission is already registered;
167 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
168 atcmentry_vec_type* p = &((available_dialog[type])[station]);
169 atcmentry_vec_iterator current = p->begin();
170 for ( ; current != p->end() ; ++current ) {
171 if ( current->transmission == trans ) return true;
176 // query the database whether the transmission is already registered;
177 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
178 atcmentry_vec_type* p = &((available_dialog[type])[station]);
179 atcmentry_vec_iterator current = p->begin();
180 for ( ; current != p->end() ; ++current ) {
181 if ( current->callback_code == code ) return true;
186 // Display the ATC popup dialog box with options relevant to the users current situation.
187 void FGATCDialog::PopupDialog() {
188 const char *dialog_name = "atc-dialog";
189 SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
193 _gui->closeDialog(dialog_name);
195 SGPropertyNode_ptr button_group = namedNode(dlg->getChildren("group"), "transmission-choice");
196 // remove all transmission buttons
197 button_group->removeChildren("button", false);
200 FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer(); // Hardwired to comm1 at the moment
203 label = "Not currently tuned to any ATC service";
204 mkDialog(label.c_str());
208 if(atcptr->GetType() == ATIS) {
209 label = "Tuned to ATIS - no communication possible";
210 mkDialog(label.c_str());
214 atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
215 atcmentry_vec_iterator current = atcmlist.begin();
216 atcmentry_vec_iterator last = atcmlist.end();
218 if(!atcmlist.size()) {
219 label = "No transmission available";
220 mkDialog(label.c_str());
224 const int bufsize = 32;
226 // loop over all entries in atcmentrylist
227 for (int n = 0; n < 10; ++n) {
228 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
229 fgSetBool(buf, false);
234 // add transmission button (modified copy of <button-template>)
235 SGPropertyNode *entry = button_group->getNode("button", n, true);
236 copyProperties(button_group->getNode("button-template", true), entry);
237 entry->removeChildren("hide", false);
238 entry->setStringValue("property", buf);
240 entry->setBoolValue("default", true);
242 snprintf(buf, bufsize, "%d", n + 1);
243 string legend = string(buf) + ". " + current->menuentry;
244 entry->setStringValue("legend", legend.c_str());
245 entry->setIntValue("binding/value", n);
249 _gui->showDialog(dialog_name);
253 void FGATCDialog::PopupCallback(int num) {
254 FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer(); // FIXME - Hardwired to comm1 at the moment
259 if (atcptr->GetType() == TOWER) {
260 //cout << "TOWER " << endl;
261 //cout << "ident is " << atcptr->get_ident() << endl;
262 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
263 if(atcmlist.size()) {
264 //cout << "Doing callback...\n";
265 ATCMenuEntry a = atcmlist[num];
266 atcptr->SetFreqInUse();
267 // This is the user's speech getting displayed.
268 globals->get_ATC_display()->RegisterSingleMessage(atcptr->GenText(a.transmission, a.callback_code));
269 _callbackPending = true;
270 _callbackTimer = 0.0;
272 _callbackPtr = atcptr;
273 _callbackCode = a.callback_code;
275 //cout << "No options available...\n";
277 //cout << "Donded" << endl;
281 // map() key data type (removes duplicates and sorts by distance)
284 atcdata(const string i, const string n, const double d) {
285 id = i, name = n, distance = d;
287 bool operator<(const atcdata& a) const {
288 return a.id != id && a.distance > distance;
290 bool operator==(const atcdata& a) const {
291 return a.distance == distance && a.id == id;
298 void FGATCDialog::FreqDialog() {
299 const char *dialog_name = "atc-freq-search";
300 SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
304 _gui->closeDialog(dialog_name);
306 SGPropertyNode_ptr button_group = namedNode(dlg->getChildren("group"), "quick-buttons");
307 // remove all dynamic ATC buttons
308 button_group->removeChildren("button", false);
310 // Find the ATC stations within a reasonable range (about 40 miles?)
311 comm_list_type atc_stations;
312 comm_list_iterator atc_stat_itr;
314 double lon = fgGetDouble("/position/longitude-deg");
315 double lat = fgGetDouble("/position/latitude-deg");
316 double elev = fgGetDouble("/position/altitude-ft");
317 Point3D aircraft = sgGeodToCart(Point3D(lon * SGD_DEGREES_TO_RADIANS,
318 lat * SGD_DEGREES_TO_RADIANS, elev));
320 // search stations in range
321 int num_stat = current_commlist->FindByPos(lon, lat, elev, 40.0, &atc_stations);
323 map<atcdata, bool> uniq;
324 // fill map (sorts by distance)
325 comm_list_iterator itr = atc_stations.begin();
326 for (; itr != atc_stations.end(); ++itr) {
327 Point3D station = Point3D(itr->x, itr->y, itr->z);
328 double distance = aircraft.distance3Dsquared(station);
329 uniq[atcdata(itr->ident, itr->name, distance)] = true;
331 // create button per map entry (modified copy of <button-template>)
332 map<atcdata, bool>::iterator uit = uniq.begin();
333 for (int n = 0; uit != uniq.end() && n < 6; ++uit, ++n) { // max 6 buttons
334 SGPropertyNode *entry = button_group->getNode("button", n, true);
335 copyProperties(button_group->getNode("button-template", true), entry);
336 entry->removeChildren("hide", false);
337 entry->setStringValue("legend", uit->first.id.c_str());
338 entry->setStringValue("binding[0]/value", uit->first.id.c_str());
342 // (un)hide message saying no things in range
343 SGPropertyNode_ptr range_error = namedNode(dlg->getChildren("text"), "no-atc-in-range");
344 range_error->setBoolValue("hide", num_stat);
346 _gui->showDialog(dialog_name);
349 void FGATCDialog::FreqDisplay(string& ident) {
350 const char *dialog_name = "atc-freq-display";
351 SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
355 _gui->closeDialog(dialog_name);
357 SGPropertyNode_ptr freq_group = namedNode(dlg->getChildren("group"), "frequency-list");
358 // remove all frequency entries
359 freq_group->removeChildren("group", false);
365 if (!dclFindAirportID(ident, &a)) {
366 label = "Airport " + ident + " not found in database.";
367 mkDialog(label.c_str());
372 label = ident + " Frequencies";
373 dlg->setStringValue("text/label", label.c_str());
375 int n = 0; // Number of ATC frequencies at this airport
377 comm_list_type stations;
378 int found = current_commlist->FindByPos(a.getLongitude(), a.getLatitude(), a.getElevation(), 20.0, &stations);
381 comm_list_iterator itr = stations.begin();
382 for (n = 0; itr != stations.end(); ++itr) {
383 if(itr->ident != ident)
386 if(itr->type == INVALID)
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("hide", false);
395 entry->setStringValue("text[0]/label", ostr.str().c_str());
398 snprintf(buf, 8, "%.2f", (itr->freq / 100.0)); // Convert from KHz to MHz
399 if(buf[5] == '3') buf[5] = '2';
400 if(buf[5] == '8') buf[5] = '7';
403 entry->setStringValue("text[1]/label", buf);
410 label = "No frequencies found for airport " + ident;
411 mkDialog(label.c_str());
415 _gui->showDialog(dialog_name);