]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCDialog.cxx
Merge branch 'next' into durk-atc
[flightgear.git] / src / ATCDCL / 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <simgear/compiler.h>
26
27 #include <simgear/structure/commands.hxx>
28 #include <simgear/props/props_io.hxx>
29
30 #include <Main/globals.hxx>
31 #include <GUI/gui.h>            // mkDialog
32 #include <GUI/new_gui.hxx>
33
34 #include "ATCDialog.hxx"
35 #include "ATC.hxx"
36 #include "ATCmgr.hxx"
37 #include "commlist.hxx"
38 #include "ATCutils.hxx"
39 #include <Airports/simple.hxx>
40
41 #include <sstream>
42
43 using std::ostringstream;
44
45 FGATCDialog *current_atcdialog;
46
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();
51         return(true);
52 }
53
54 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
55         current_atcdialog->FreqDialog();
56         return(true);
57 }
58
59 ATCMenuEntry::ATCMenuEntry() {
60   stationid    = "";
61   //stationfr    = 0;
62   transmission = "";
63   menuentry    = "";
64   callback_code = 0;
65 }
66
67 ATCMenuEntry::~ATCMenuEntry() {
68 }
69
70 void atcUppercase(string &s) {
71         for(unsigned int i=0; i<s.size(); ++i) {
72                 s[i] = toupper(s[i]);
73         }
74 }
75
76 // find child whose <name>...</name> entry matches 'name'
77 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
78         SGPropertyNode* p;
79
80         for (int i = 0; i < prop->nChildren(); i++)
81                 if ((p = getNamedNode(prop->getChild(i), name)))
82                         return p;
83
84         if (!strcmp(prop->getStringValue("name"), name))
85                 return prop;
86
87         return 0;
88 }
89
90
91 FGATCDialog::FGATCDialog() {
92         _callbackPending = false;
93         _callbackTimer = 0.0;
94         _callbackWait = 0.0;
95         _callbackPtr = NULL;
96         _callbackCode = 0;
97         _gui = (NewGUI *)globals->get_subsystem("gui");
98 }
99
100 FGATCDialog::~FGATCDialog() {
101 }
102
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);
108
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);
112 }
113
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();
117         //if (!s.empty()) {
118         //      airport->setStringValue("");
119         //      FreqDisplay(s);
120         //}
121
122         //static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
123         //int n = trans_num->getIntValue();
124         //if (n >= 0) {
125         //      trans_num->setIntValue(-1);
126         //      PopupCallback(n);
127         //}
128
129         //if(_callbackPending) {
130         //      if(_callbackTimer > _callbackWait) {
131         //              _callbackPtr->ReceiveUserCallback(_callbackCode);
132         //              _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
133         //              _callbackPending = false;
134         //      } else {
135         //              _callbackTimer += dt;
136         //      }
137         //}
138 }
139
140 // Add an entry
141 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
142
143   ATCMenuEntry a;
144
145   a.stationid = station;
146   a.transmission = transmission;
147   a.menuentry = menutext;
148   a.callback_code = code;
149
150   (available_dialog[type])[station.c_str()].push_back(a);
151
152 }
153
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);
159         else ++current;
160   }
161 }
162
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);
168         else ++current;
169   }
170 }
171
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;
178   }
179   return false;
180 }
181
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;
188   }
189   return false;
190 }
191
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);
196         if (!dlg)
197                 return;
198
199         _gui->closeDialog(dialog_name);
200
201         SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
202         // remove all transmission buttons
203         button_group->removeChildren("button", false);
204
205         string label;
206         FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // Hardwired to comm1 at the moment
207
208         if (!atcptr) {
209                 label = "Not currently tuned to any ATC service";
210                 mkDialog(label.c_str());
211                 return;
212         }
213
214         if(atcptr->GetType() == ATIS) {
215                 label = "Tuned to ATIS - no communication possible";
216                 mkDialog(label.c_str());
217                 return;
218         }
219
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();
223         
224         if(!atcmlist.size()) {
225                 label = "No transmission available";
226                 mkDialog(label.c_str());
227                 return;
228         }
229
230         const int bufsize = 32;
231         char buf[bufsize];
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);
236
237                 if (current == last)
238                         continue;
239
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);
246                 if (n == 0)
247                         entry->setBoolValue("default", true);
248
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);
253                 current++;
254         }
255
256         _gui->showDialog(dialog_name);
257         return;
258 }
259
260 void FGATCDialog::PopupCallback(int num) {
261         FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // FIXME - Hardwired to comm1 at the moment
262
263         if (!atcptr)
264                 return;
265
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;
280                         _callbackWait = 5.0;
281                         _callbackPtr = atcptr;
282                         _callbackCode = a.callback_code;
283                 } else {
284                         //cout << "No options available...\n";
285                 }
286                 //cout << "Donded" << endl;
287         }
288 }
289
290 // map() key data type (removes duplicates and sorts by distance)
291 struct atcdata {
292         atcdata() {}
293         atcdata(const string i, const string n, const double d) {
294                 id = i, name = n, distance = d;
295         }
296         bool operator<(const atcdata& a) const {
297                 return id != a.id && distance < a.distance;
298         }
299         bool operator==(const atcdata& a) const {
300                 return id == a.id && distance == a.distance;
301         }
302         string id;
303         string name;
304         double distance;
305 };
306
307 void FGATCDialog::FreqDialog() {
308         const char *dialog_name = "atc-freq-search";
309         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
310         if (!dlg)
311                 return;
312
313         _gui->closeDialog(dialog_name);
314
315         SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
316         // remove all dynamic airport/ATC buttons
317         button_group->removeChildren("button", false);
318
319         // Find the ATC stations within a reasonable range
320         comm_list_type atc_stations;
321         comm_list_iterator atc_stat_itr;
322
323   SGGeod geod(SGGeod::fromDegFt(fgGetDouble("/position/longitude-deg"),
324     fgGetDouble("/position/latitude-deg"), fgGetDouble("/position/altitude-ft")));
325         SGVec3d aircraft = SGVec3d::fromGeod(geod);
326
327         // search stations in range
328         int num_stat = current_commlist->FindByPos(geod, 50.0, &atc_stations);
329         if (num_stat != 0) {
330                 map<atcdata, bool> uniq;
331                 // fill map (sorts by distance and removes duplicates)
332                 comm_list_iterator itr = atc_stations.begin();
333                 for (; itr != atc_stations.end(); ++itr) {
334                         double distance = distSqr(aircraft, itr->cart);
335                         uniq[atcdata(itr->ident, itr->name, distance)] = true;
336                 }
337                 // create button per map entry (modified copy of <button-template>)
338                 map<atcdata, bool>::iterator uit = uniq.begin();
339                 for (int n = 0; uit != uniq.end() && n < 6; ++uit, ++n) { // max 6 buttons
340                         SGPropertyNode *entry = button_group->getNode("button", n, true);
341                         copyProperties(button_group->getNode("button-template", true), entry);
342                         entry->removeChildren("enabled", true);
343                         entry->setStringValue("legend", uit->first.id.c_str());
344                         entry->setStringValue("binding[0]/value", uit->first.id.c_str());
345                 }
346         }
347
348         // (un)hide message saying no things in range
349         SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
350         range_error->setBoolValue("enabled", !num_stat);
351
352         _gui->showDialog(dialog_name);
353 }
354
355 void FGATCDialog::FreqDisplay(string& ident) {
356         const char *dialog_name = "atc-freq-display";
357         SGPropertyNode_ptr dlg = _gui->getDialogProperties(dialog_name);
358         if (!dlg)
359                 return;
360
361         _gui->closeDialog(dialog_name);
362
363         SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
364         // remove all frequency entries
365         freq_group->removeChildren("group", false);
366
367         atcUppercase(ident);
368         string label;
369
370         const FGAirport *a = fgFindAirportID(ident);
371         if (!a) {
372                 label = "Airport " + ident + " not found in database.";
373                 mkDialog(label.c_str());
374                 return;
375         }
376
377         // set title
378         label = ident + " Frequencies";
379         dlg->setStringValue("text/label", label.c_str());
380
381         int n = 0;      // Number of ATC frequencies at this airport
382
383         comm_list_type stations;
384         int found = current_commlist->FindByPos(a->geod(), 20.0, &stations);
385         if(found) {
386                 comm_list_iterator itr = stations.begin();
387                 for (n = 0; itr != stations.end(); ++itr) {
388                         if(itr->ident != ident)
389                                 continue;
390
391                         if(itr->type == INVALID)
392                                 continue;
393
394                         // add frequency line (modified copy of <group-template>)
395                         SGPropertyNode *entry = freq_group->getNode("group", n, true);
396                         copyProperties(freq_group->getNode("group-template", true), entry);
397                         entry->removeChildren("enabled", true);
398
399                         ostringstream ostr;
400                         ostr << itr->type;
401                         entry->setStringValue("text[0]/label", ostr.str());
402
403                         char buf[8];
404                         snprintf(buf, 8, "%.2f", (itr->freq / 100.0));  // Convert from KHz to MHz
405                         if(buf[5] == '3') buf[5] = '2';
406                         if(buf[5] == '8') buf[5] = '7';
407                         buf[7] = '\0';
408
409                         entry->setStringValue("text[1]/label", buf);
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