]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCDialog.cxx
search recursively for named node (i.e. one with matching <name> child);
[flightgear.git] / src / ATC / ATCDialog.cxx
1 // ATCDialog.cxx - Functions and classes to handle the pop-up ATC dialog
2 //
3 // Written by Alexander Kappes and David Luff, started February 2003.
4 //
5 // Copyright (C) 2003  Alexander Kappes and David Luff
6 //
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.
11 //
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.
16 //
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.
20
21 #include <simgear/compiler.h>
22
23 #include <simgear/structure/commands.hxx>
24
25 #include <Main/globals.hxx>
26 #include <GUI/gui.h>            // mkDialog
27 #include <GUI/new_gui.hxx>
28
29 #include "ATCDialog.hxx"
30 #include "ATC.hxx"
31 #include "ATCmgr.hxx"
32 #include "ATCdisplay.hxx"
33 #include "commlist.hxx"
34 #include "ATCutils.hxx"
35 #include <Airports/simple.hxx>
36
37 #include <sstream>
38 #include <map>
39
40 SG_USING_STD(ostringstream);
41 SG_USING_STD(map);
42
43 FGATCDialog *current_atcdialog;
44
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();
48         return(true);
49 }
50
51 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
52         current_atcdialog->FreqDialog();
53         return(true);
54 }
55
56 ATCMenuEntry::ATCMenuEntry() {
57   stationid    = "";
58   //stationfr    = 0;
59   transmission = "";
60   menuentry    = "";
61   callback_code = 0;
62 }
63
64 ATCMenuEntry::~ATCMenuEntry() {
65 }
66
67 static void atcUppercase(string &s) {
68         for(unsigned int i=0; i<s.size(); ++i) {
69                 s[i] = toupper(s[i]);
70         }
71 }
72
73 // find child whose <name>...</name> entry matches 'name'
74 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
75         SGPropertyNode* p;
76
77         for (int i = 0; i < prop->nChildren(); i++)
78                 if ((p = getNamedNode(prop->getChild(i), name)))
79                         return p;
80
81
82         if (!strcmp(prop->getStringValue("name"), name))
83                 return prop;
84
85         return 0;
86 }
87
88
89 FGATCDialog::FGATCDialog() {
90         _callbackPending = false;
91         _callbackTimer = 0.0;
92         _callbackWait = 0.0;
93         _callbackPtr = NULL;
94         _callbackCode = 0;
95         _gui = (NewGUI *)globals->get_subsystem("gui");
96 }
97
98 FGATCDialog::~FGATCDialog() {
99 }
100
101 void FGATCDialog::Init() {
102         // Add ATC-dialog to the command list
103         globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
104         // Add ATC-freq-search to the command list
105         globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
106
107         // initialize properties polled in Update()
108         globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
109         globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
110 }
111
112 void FGATCDialog::Update(double dt) {
113         static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
114         string s = airport->getStringValue();
115         if (!s.empty()) {
116                 airport->setStringValue("");
117                 FreqDisplay(s);
118         }
119
120         static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
121         int n = trans_num->getIntValue();
122         if (n >= 0) {
123                 trans_num->setIntValue(-1);
124                 PopupCallback(n);
125         }
126
127         if(_callbackPending) {
128                 if(_callbackTimer > _callbackWait) {
129                         _callbackPtr->ReceiveUserCallback(_callbackCode);
130                         _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
131                         _callbackPending = false;
132                 } else {
133                         _callbackTimer += dt;
134                 }
135         }
136 }
137
138 // Add an entry
139 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
140
141   ATCMenuEntry a;
142
143   a.stationid = station;
144   a.transmission = transmission;
145   a.menuentry = menutext;
146   a.callback_code = code;
147
148   (available_dialog[type])[station.c_str()].push_back(a);
149
150 }
151
152 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
153   atcmentry_vec_type* p = &((available_dialog[type])[station]);
154   atcmentry_vec_iterator current = p->begin();
155   while(current != p->end()) {
156     if(current->transmission == trans) current = p->erase(current);
157         else ++current;
158   }
159 }
160
161 void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
162   atcmentry_vec_type* p = &((available_dialog[type])[station]);
163   atcmentry_vec_iterator current = p->begin();
164   while(current != p->end()) {
165     if(current->callback_code == code) current = p->erase(current);
166         else ++current;
167   }
168 }
169
170 // query the database whether the transmission is already registered;
171 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
172   atcmentry_vec_type* p = &((available_dialog[type])[station]);
173   atcmentry_vec_iterator current = p->begin();
174   for ( ; current != p->end() ; ++current ) {
175     if ( current->transmission == trans ) return true;
176   }
177   return false;
178 }
179
180 // query the database whether the transmission is already registered;
181 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
182   atcmentry_vec_type* p = &((available_dialog[type])[station]);
183   atcmentry_vec_iterator current = p->begin();
184   for ( ; current != p->end() ; ++current ) {
185     if ( current->callback_code == code ) return true;
186   }
187   return false;
188 }
189
190 // Display the ATC popup dialog box with options relevant to the users current situation.
191 void FGATCDialog::PopupDialog() {
192         const char *dialog_name = "atc-dialog";
193         SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
194         if (!dlg)
195                 return;
196
197         _gui->closeDialog(dialog_name);
198
199         SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
200         // remove all transmission buttons
201         button_group->removeChildren("button", false);
202
203         string label;
204         FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // Hardwired to comm1 at the moment
205
206         if (!atcptr) {
207                 label = "Not currently tuned to any ATC service";
208                 mkDialog(label.c_str());
209                 return;
210         }
211
212         if(atcptr->GetType() == ATIS) {
213                 label = "Tuned to ATIS - no communication possible";
214                 mkDialog(label.c_str());
215                 return;
216         }
217
218         atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
219         atcmentry_vec_iterator current = atcmlist.begin();
220         atcmentry_vec_iterator last = atcmlist.end();
221         
222         if(!atcmlist.size()) {
223                 label = "No transmission available";
224                 mkDialog(label.c_str());
225                 return;
226         }
227
228         const int bufsize = 32;
229         char buf[bufsize];
230         // loop over all entries in atcmentrylist
231         for (int n = 0; n < 10; ++n) {
232                 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
233                 fgSetBool(buf, false);
234
235                 if (current == last)
236                         continue;
237
238                 // add transmission button (modified copy of <button-template>)
239                 SGPropertyNode *entry = button_group->getNode("button", n, true);
240                 copyProperties(button_group->getNode("button-template", true), entry);
241                 entry->removeChildren("hide", false);
242                 entry->setStringValue("property", buf);
243                 if (n == 0)
244                         entry->setBoolValue("default", true);
245
246                 snprintf(buf, bufsize, "%d", n + 1);
247                 string legend = string(buf) + ". " + current->menuentry;
248                 entry->setStringValue("legend", legend.c_str());
249                 entry->setIntValue("binding/value", n);
250                 current++;
251         }
252
253         _gui->showDialog(dialog_name);
254         return;
255 }
256
257 void FGATCDialog::PopupCallback(int num) {
258         FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // FIXME - Hardwired to comm1 at the moment
259
260         if (!atcptr)
261                 return;
262
263         if (atcptr->GetType() == TOWER) {
264                 //cout << "TOWER " << endl;
265                 //cout << "ident is " << atcptr->get_ident() << endl;
266                 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
267                 if(atcmlist.size()) {
268                         //cout << "Doing callback...\n";
269                         ATCMenuEntry a = atcmlist[num];
270                         atcptr->SetFreqInUse();
271                         // This is the user's speech getting displayed.
272                         globals->get_ATC_display()->RegisterSingleMessage(atcptr->GenText(a.transmission, a.callback_code));
273                         _callbackPending = true;
274                         _callbackTimer = 0.0;
275                         _callbackWait = 5.0;
276                         _callbackPtr = atcptr;
277                         _callbackCode = a.callback_code;
278                 } else {
279                         //cout << "No options available...\n";
280                 }
281                 //cout << "Donded" << endl;
282         }
283 }
284
285 // map() key data type (removes duplicates and sorts by distance)
286 struct atcdata {
287         atcdata() {}
288         atcdata(const string i, const string n, const double d) {
289                 id = i, name = n, distance = d;
290         }
291         bool operator<(const atcdata& a) const {
292                 return a.id != id && a.distance > distance;
293         }
294         bool operator==(const atcdata& a) const {
295                 return a.distance == distance && a.id == id;
296         }
297         string id;
298         string name;
299         double distance;
300 };
301
302 void FGATCDialog::FreqDialog() {
303         const char *dialog_name = "atc-freq-search";
304         SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
305         if (!dlg)
306                 return;
307
308         _gui->closeDialog(dialog_name);
309
310         SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
311         // remove all dynamic ATC buttons
312         button_group->removeChildren("button", false);
313
314         // Find the ATC stations within a reasonable range (about 40 miles?)
315         comm_list_type atc_stations;
316         comm_list_iterator atc_stat_itr;
317         
318         double lon = fgGetDouble("/position/longitude-deg");
319         double lat = fgGetDouble("/position/latitude-deg");
320         double elev = fgGetDouble("/position/altitude-ft");
321         Point3D aircraft = sgGeodToCart(Point3D(lon * SGD_DEGREES_TO_RADIANS,
322                 lat * SGD_DEGREES_TO_RADIANS, elev));
323
324         // search stations in range
325         int num_stat = current_commlist->FindByPos(lon, lat, elev, 40.0, &atc_stations);
326         if (num_stat != 0) {
327                 map<atcdata, bool> uniq;
328                 // fill map (sorts by distance)
329                 comm_list_iterator itr = atc_stations.begin();
330                 for (; itr != atc_stations.end(); ++itr) {
331                         Point3D station = Point3D(itr->x, itr->y, itr->z);
332                         double distance = aircraft.distance3Dsquared(station);
333                         uniq[atcdata(itr->ident, itr->name, distance)] = true;
334                 }
335                 // create button per map entry (modified copy of <button-template>)
336                 map<atcdata, bool>::iterator uit = uniq.begin();
337                 for (int n = 0; uit != uniq.end() && n < 6; ++uit, ++n) { // max 6 buttons
338                         SGPropertyNode *entry = button_group->getNode("button", n, true);
339                         copyProperties(button_group->getNode("button-template", true), entry);
340                         entry->removeChildren("hide", false);
341                         entry->setStringValue("legend", uit->first.id.c_str());
342                         entry->setStringValue("binding[0]/value", uit->first.id.c_str());
343                 }
344         }
345
346         // (un)hide message saying no things in range
347         SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
348         range_error->setBoolValue("hide", num_stat);
349
350         _gui->showDialog(dialog_name);
351 }
352
353 void FGATCDialog::FreqDisplay(string& ident) {
354         const char *dialog_name = "atc-freq-display";
355         SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
356         if (!dlg)
357                 return;
358
359         _gui->closeDialog(dialog_name);
360
361         SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
362         // remove all frequency entries
363         freq_group->removeChildren("group", false);
364
365         atcUppercase(ident);
366         string label;
367
368         FGAirport a;
369         if (!dclFindAirportID(ident, &a)) {
370                 label = "Airport " + ident + " not found in database.";
371                 mkDialog(label.c_str());
372                 return;
373         }
374
375         // set title
376         label = ident + " Frequencies";
377         dlg->setStringValue("text/label", label.c_str());
378
379         int n = 0;      // Number of ATC frequencies at this airport
380
381         comm_list_type stations;
382         int found = current_commlist->FindByPos(a.getLongitude(), a.getLatitude(), a.getElevation(), 20.0, &stations);
383         if(found) {
384                 ostringstream ostr;
385                 comm_list_iterator itr = stations.begin();
386                 for (n = 0; itr != stations.end(); ++itr) {
387                         if(itr->ident != ident)
388                                 continue;
389
390                         if(itr->type == INVALID)
391                                 continue;
392
393                         // add frequency line (modified copy of <group-template>)
394                         SGPropertyNode *entry = freq_group->getNode("group", n, true);
395                         copyProperties(freq_group->getNode("group-template", true), entry);
396                         entry->removeChildren("hide", false);
397
398                         ostr << itr->type;
399                         entry->setStringValue("text[0]/label", ostr.str().c_str());
400
401                         char buf[8];
402                         snprintf(buf, 8, "%.2f", (itr->freq / 100.0));  // Convert from KHz to MHz
403                         if(buf[5] == '3') buf[5] = '2';
404                         if(buf[5] == '8') buf[5] = '7';
405                         buf[7] = '\0';
406
407                         entry->setStringValue("text[1]/label", buf);
408
409                         ostr.seekp(0);
410                         n++;
411                 }
412         }
413         if(n == 0) {
414                 label = "No frequencies found for airport " + ident;
415                 mkDialog(label.c_str());
416                 return;
417         }
418
419         _gui->showDialog(dialog_name);
420 }
421